11/*
2- * Copyright (c) 2011, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2011, 2026 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24- /* @test
25- @bug 7040220 8054307
26- @summary Test if StringCoding and NIO result have the same de/encoding result for UTF-8
27- * @run main/othervm/timeout=2000 TestStringCodingUTF8
24+ /*
25+ * @test
26+ * @bug 7040220 8054307
27+ * @summary Test if StringCoding and NIO result have the same de/encoding result for UTF-8
2828 * @key randomness
29+ * @library /test/lib
30+ * @build jdk.test.lib.RandomFactory
31+ * @run main/othervm/timeout=2000 TestStringCodingUTF8
2932 */
3033
3134import java .util .*;
3235import java .nio .*;
3336import java .nio .charset .*;
3437
38+ import jdk .test .lib .RandomFactory ;
39+
3540public class TestStringCodingUTF8 {
41+
42+ private static final Random rnd = RandomFactory .getRandom ();
43+
3644 public static void main (String [] args ) throws Throwable {
3745 test ("UTF-8" );
3846 test ("CESU-8" );
@@ -62,7 +70,7 @@ static void test(String csn) throws Throwable {
6270 for (int i = 0 ; i < 0x20000 ; i ++) {
6371 list .add (i , i );
6472 }
65- Collections .shuffle (list );
73+ Collections .shuffle (list , rnd );
6674 int j = 0 ;
6775 char [] bmpsupp = new char [0x30000 ];
6876 for (int i = 0 ; i < 0x20000 ; i ++) {
@@ -72,7 +80,6 @@ static void test(String csn) throws Throwable {
7280 test (cs , bmpsupp , 0 , bmpsupp .length );
7381
7482 // randomed "off" and "len" on shuffled data
75- Random rnd = new Random ();
7683 int maxlen = 1000 ;
7784 int itr = 5000 ;
7885 for (int i = 0 ; i < itr ; i ++) {
@@ -88,11 +95,13 @@ static void test(String csn) throws Throwable {
8895 //new String(csn);
8996 if (!new String (ba , cs .name ()).equals (
9097 new String (decode (cs , ba , 0 , ba .length ))))
91- throw new RuntimeException ("new String(csn) failed" );
98+ throw new RuntimeException ("new String(csn) failed for charset " + cs
99+ + " for byte array of length " + ba .length );
92100 //new String(cs);
93101 if (!new String (ba , cs ).equals (
94102 new String (decode (cs , ba , 0 , ba .length ))))
95- throw new RuntimeException ("new String(cs) failed" );
103+ throw new RuntimeException ("new String(cs) failed for charset " + cs
104+ + " for byte array of length " + ba .length );
96105 }
97106 System .out .println ("done!" );
98107 }
@@ -103,21 +112,23 @@ static void test(Charset cs, char[] ca, int off, int len) throws Throwable {
103112
104113 //getBytes(csn);
105114 byte [] baStr = str .getBytes (cs .name ());
106- if (! Arrays . equals ( ba , baStr ))
107- throw new RuntimeException ( "getBytes(csn) failed" );
115+ failIfMismatch ( ba , baStr , "getBytes(csn) failed for charset " + cs
116+ + ", character array length=" + ca . length + ", offset=" + off + ", len=" + len );
108117
109118 //getBytes(cs);
110119 baStr = str .getBytes (cs );
111- if (! Arrays . equals ( ba , baStr ))
112- throw new RuntimeException ( "getBytes(cs) failed" );
120+ failIfMismatch ( ba , baStr , "getBytes(cs) failed for charset " + cs
121+ + ", character array length=" + ca . length + ", offset=" + off + ", len=" + len );
113122
114123 //new String(csn);
115124 if (!new String (ba , cs .name ()).equals (new String (decode (cs , ba , 0 , ba .length ))))
116- throw new RuntimeException ("new String(csn) failed" );
125+ throw new RuntimeException ("new String(csn) failed for charset " + cs
126+ + ", character array length=" + ca .length + ", offset=" + off + ", len=" + len );
117127
118128 //new String(cs);
119129 if (!new String (ba , cs ).equals (new String (decode (cs , ba , 0 , ba .length ))))
120- throw new RuntimeException ("new String(cs) failed" );
130+ throw new RuntimeException ("new String(cs) failed for charset " + cs
131+ + ", character array length=" + ca .length + ", offset=" + off + ", len=" + len );
121132 }
122133
123134 // copy/paste of the StringCoding.decode()
@@ -170,4 +181,45 @@ static byte[] encode(Charset cs, char[] ca, int off, int len) {
170181 }
171182 return Arrays .copyOf (ba , bb .position ());
172183 }
184+
185+ private static void failIfMismatch (final byte [] expected , final byte [] actual ,
186+ final String failureMsg ) {
187+ final int firstMismatchIndex = Arrays .mismatch (expected , actual );
188+ if (firstMismatchIndex == -1 ) {
189+ // no mismatch
190+ return ;
191+ }
192+ System .err .println ("Arrays mismatch starts at index: " + firstMismatchIndex );
193+ System .err .println ("Printing few indexes before and after the mismatch:" );
194+ int printStartIdx = firstMismatchIndex - 20 ;
195+ if (printStartIdx < 0 ) {
196+ printStartIdx = 0 ;
197+ }
198+ for (int i = printStartIdx ; i < firstMismatchIndex + 20 ; i ++) {
199+ if (i >= expected .length && i >= actual .length ) {
200+ // no more elements in either arrays, we are done
201+ break ;
202+ }
203+ final StringBuilder sb = new StringBuilder ();
204+ sb .append ("Index=" ).append (i ).append (", expected=" );
205+ if (i >= expected .length ) {
206+ // "expected" array isn't that big
207+ sb .append ("<no element>" );
208+ } else {
209+ sb .append (expected [i ]);
210+ }
211+ sb .append (", actual=" );
212+ if (i >= actual .length ) {
213+ // "actual" array isn't that big
214+ sb .append ("<no element>" );
215+ } else {
216+ sb .append (actual [i ]);
217+ }
218+ if (i == firstMismatchIndex ) {
219+ sb .append (" <--- first mismatch" );
220+ }
221+ System .err .println (sb .toString ());
222+ }
223+ throw new RuntimeException (failureMsg );
224+ }
173225}
0 commit comments