test/jdk/sun/nio/cs/TestStringCoding.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -20,11 +20,11 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /* @test
- * @bug 6636323 6636319 7040220 7096080 7183053 8080248 8054307
+ * @bug 6636323 6636319 7040220 7096080 7183053 8080248 8054307 8021560
  * @summary Test if StringCoding and NIO result have the same de/encoding result
  * @modules java.base/sun.nio.cs
  * @run main/othervm/timeout=2000 TestStringCoding
  * @key randomness
  */

@@ -32,10 +32,13 @@
 import java.util.*;
 import java.nio.*;
 import java.nio.charset.*;
 
 public class TestStringCoding {
+
+    private static Random r = new Random();
+
     public static void main(String[] args) throws Throwable {
 
         // full bmp first
         char[] bmp = new char[0x10000];
         for (int i = 0; i < 0x10000; i++) {

@@ -53,14 +56,19 @@
         for (Boolean hasSM: new boolean[] { false, true }) {
             if (hasSM) {
                 System.setSecurityManager(new PermissiveSecurityManger());
             }
             for (Charset cs:  Charset.availableCharsets().values()) {
+
+if (!cs.name().contains("Big5-HKSCS"))
+    continue;
+
                 if ("ISO-2022-CN".equals(cs.name()) ||
                     "x-COMPOUND_TEXT".equals(cs.name()) ||
                     "x-JISAutoDetect".equals(cs.name()))
                     continue;
+
                 System.out.printf("Testing(sm=%b) " + cs.name() + "....", hasSM);
 
                 testNewString(cs, testGetBytes(cs, new String(bmp)));
                 testNewString(cs, testGetBytes(cs, new String(latin)));
                 testNewString(cs, testGetBytes(cs, new String(ascii)));

@@ -116,78 +124,184 @@
         Collections.shuffle(cps);
         char[] bmpCA = new char[cps.size()];
         for (int i = 0; i < cps.size(); i++)
             bmpCA[i] = (char)(int)cps.get(i);
         String bmpStr = new String(bmpCA);
-        //getBytes(csn);
-        byte[] bmpBA = bmpStr.getBytes(cs.name());
-        ByteBuffer bf = enc.reset().encode(CharBuffer.wrap(bmpCA));
-        byte[] baNIO = new byte[bf.limit()];
-        bf.get(baNIO, 0, baNIO.length);
-        if (!Arrays.equals(bmpBA, baNIO)) {
-            throw new RuntimeException("getBytes(csn) failed  -> " + cs.name());
-        }
-
-        //getBytes(cs);
-        bmpBA = bmpStr.getBytes(cs);
-        if (!Arrays.equals(bmpBA, baNIO)) {
-            throw new RuntimeException("getBytes(cs) failed  -> " + cs.name());
-        }
-
-        //new String(csn);
-        String strSC = new String(bmpBA, cs.name());
-        String strNIO = dec.reset().decode(ByteBuffer.wrap(bmpBA)).toString();
-        if(!strNIO.equals(strSC)) {
-            throw new RuntimeException("new String(csn) failed  -> " + cs.name());
-        }
-        //new String(cs);
-        strSC = new String(bmpBA, cs);
-        if (!strNIO.equals(strSC)) {
-            throw new RuntimeException("new String(cs) failed  -> " + cs.name());
-        }
+        // test getBytes()
+        byte[] bmpBA = testGetBytes(cs, bmpStr);
+        // test new String()
+        testNewString(cs, bmpBA);
     }
 
     static byte[] getBytes(CharsetEncoder enc, String str) throws Throwable {
         ByteBuffer bf = enc.reset().encode(CharBuffer.wrap(str.toCharArray()));
         byte[] ba = new byte[bf.limit()];
         bf.get(ba, 0, ba.length);
         return ba;
     }
 
+    static void assertEquals(int len1, int len2, String msg) {
+        if (len1 != len2)
+            throw new RuntimeException("FAILED: " + msg);
+    }
+
+    static void assertEquals(byte[] a, int afrom, int ato,
+                             byte[] b, int bfrom, int bto,
+                             String msg) {
+        if (!Arrays.equals(a, afrom, ato, b, bfrom, bto)) {
+            throw new RuntimeException("FAILED: " + msg);
+        }
+    }
+
+    static void assertEquals(ByteBuffer bb, int pos, byte[] b, int from, int to,
+                             byte[] buf, String msg) {
+        bb.flip().position(pos);
+        int len = bb.remaining();
+        bb.get(buf, 0, len);
+        if (!Arrays.equals(buf, 0, len, b, from, to)) {
+            throw new RuntimeException("FAILED: " + msg);
+        }
+    }
+
     static byte[] testGetBytes(Charset cs, String str) throws Throwable {
         CharsetEncoder enc = cs.newEncoder()
             .onMalformedInput(CodingErrorAction.REPLACE)
-            .onUnmappableCharacter(CodingErrorAction.REPLACE);
+            .onUnmappableCharacter(CodingErrorAction.REPLACE)
+            .reset();
+
         //getBytes(csn);
-        byte[] baSC = str.getBytes(cs.name());
+        String csn = cs.name();
+        byte[] baSC = str.getBytes(csn);
         byte[] baNIO = getBytes(enc, str);
-        if (!Arrays.equals(baSC, baNIO)) {
-            throw new RuntimeException("getBytes(csn) failed  -> " + cs.name());
-        }
+        assertEquals(baSC, 0, baSC.length, baNIO, 0, baNIO.length, "getBytes(csn)");
+
         //getBytes(cs);
         baSC = str.getBytes(cs);
-        if (!Arrays.equals(baSC, baNIO)) {
-            throw new RuntimeException("getBytes(cs) failed  -> " + cs.name());
+        assertEquals(baSC, 0, baSC.length, baNIO, 0, baNIO.length, "getBytes(cs)");
+
+        byte[] buf = new byte[baNIO.length + 100];
+        int shortenLen = Math.max(0, baNIO.length - 10);
+        int slen = str.length();
+
+        // getBytes(int, int ByteBuffer, cs);
+        ByteBuffer bb = ByteBuffer.wrap(buf);
+        int n = str.getBytes(0, slen, bb, cs);
+        int bn = bb.position();
+        assertEquals(n, slen, "getBytes(bb cs)");
+        assertEquals(buf, 0, bn, baNIO, 0, baNIO.length, "getBytes(bb, cs)");
+
+        bb.clear().position(3);
+        n = str.getBytes(0, slen, bb, cs);
+        bn = bb.position();
+        assertEquals(n, slen, "getBytes(bb/3, cs)");
+        assertEquals(buf, 3, bn, baNIO, 0, baNIO.length, "getBytes(bb/3, cs)");
+
+        bb.clear().limit(shortenLen);
+        n = str.getBytes(0, slen, bb, cs);
+        bn = bb.position();
+        assertEquals(buf, 0, bn, baNIO, 0, bn, "getBytes(bb, cs)/shortenLen");
+
+System.out.printf ("testGetBytes.loop: str.len=%d, buf.len=%d, baNIO.len=%d%n",
+                   slen, buf.length, baNIO.length);
+
+        int off = 0;
+        int m = 0;
+        bb.clear();
+        while (off < slen) {
+            int nn = slen - off;
+            if (m++ < 10 && nn > 10) {
+                nn = r.nextInt(nn);
+            }
+            n = str.getBytes(off, off + nn, bb, cs);
+            off += n;
+System.out.printf ("         --->  off=%d, slen=%d, bb.rem=%d, n=%d, nn=%d m=%d%n", off, slen, bb.remaining(), n, nn, m);
+        }
+
+        bb.flip();
+        // assertEquals(buf, 0, bn, baNIO, 0, bn, "getBytes(bb, cs)/loop");
+
+        if (!new String(bb, cs).equals(new String(baNIO, cs))) {
+bb.rewind();
+String s1 = new String(bb, cs);
+String s2 = new String(baNIO, cs);
+
+System.out.printf("  bb.str.len=%d, nio.str.len=%d%n",  s1.length(), s2.length());
+for (int i = 0 ;i < s1.length(); i++) {
+    if (s1.charAt(i) != s2.charAt(i)) {
+        System.out.printf(" [%d] %x   %x%n", i, s1.charAt(i) & 0xffff, s2.charAt(i) & 0xffff);
+    }
+}
+
+
+            throw new RuntimeException("getBytes(bb, cs)/loop");
+            //new RuntimeException("getBytes(bb, cs)/loop").printStackTrace();
         }
+
+        // getBytes(int, int, ByteBuffer/direct, cs);
+        bb = ByteBuffer.allocateDirect(buf.length);
+        n = str.getBytes(0, slen, bb, cs);
+        assertEquals(n, slen, "getBytes(bb cs)");
+        assertEquals(bb, 0, baNIO, 0, baNIO.length, buf, "getBytes(bb, cs)");
+
+        bb.clear().position(3);
+        n = str.getBytes(0, slen, bb, cs);
+        assertEquals(n, slen, "getBytes(bb, cs)");
+        assertEquals(bb, 3, baNIO, 0, baNIO.length, buf, "getBytes(bb/3, cs)");
+
+        bb.clear().limit(shortenLen);
+        n = str.getBytes(0, slen, bb, cs);
+        bn = bb.position();
+        assertEquals(bb, 0, baNIO, 0, bn, buf, "getBytes(bb, cs)/shortenLen");
+
         return baSC;
     }
 
     static String testNewString(Charset cs, byte[] ba) throws Throwable {
         CharsetDecoder dec = cs.newDecoder()
             .onMalformedInput(CodingErrorAction.REPLACE)
             .onUnmappableCharacter(CodingErrorAction.REPLACE);
+
+        String csn = cs.name();
+
         //new String(csn);
         String strSC = new String(ba, cs.name());
         String strNIO = dec.reset().decode(ByteBuffer.wrap(ba)).toString();
         if(!strNIO.equals(strSC)) {
-            throw new RuntimeException("new String(csn) failed  -> " + cs.name());
+            throw new RuntimeException("new String(csn) failed  -> " + csn);
         }
         //new String(cs);
         strSC = new String(ba, cs);
         if (!strNIO.equals(strSC)) {
-            throw new RuntimeException("new String(cs)/bmp failed  -> " + cs.name());
+            throw new RuntimeException("new String(cs) failed  -> " + csn);
+        }
+
+        //new String(bytebuffer, cs);
+        ByteBuffer bb = ByteBuffer.allocate(ba.length + 10);
+        bb.put(ba);
+        bb.flip();
+        if (!strNIO.equals(new String(bb, cs))) {
+            throw new RuntimeException("new String(bb, cs) failed  -> " + csn);
+        }
+
+        bb.clear().position(3).put(ba).flip();
+        if (!strNIO.equals(new String(bb.position(3), cs))) {
+            throw new RuntimeException("new String(cs)/pos=3 failed  -> " + csn);
+        }
+
+        //new String(bytebuffer/direct, cs);
+        bb = ByteBuffer.allocateDirect(ba.length + 10);
+        bb.put(ba);
+        bb.flip();
+        if (!strNIO.equals(new String(bb, cs))) {
+            throw new RuntimeException("new String(cs)/bmp failed  -> " + csn);
         }
+
+        bb.clear().position(3).put(ba).flip();
+        if (!strNIO.equals(new String(bb.position(3), cs))) {
+            throw new RuntimeException("new String(cs)/pos=3 failed  -> " + csn);
+        }
+
         return strSC;
     }
 
     static void testSurrogates(Charset cs) throws Throwable {
         //encode unmappable surrogates