--- old/test/jdk/sun/nio/cs/TestStringCodingUTF8.java 2018-03-22 14:00:54.632635692 -0700 +++ new/test/jdk/sun/nio/cs/TestStringCodingUTF8.java 2018-03-22 14:00:54.228599081 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 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 @@ -22,7 +22,7 @@ */ /* @test - @bug 7040220 8054307 + @bug 7040220 8054307 8021560 @summary Test if StringCoding and NIO result have the same de/encoding result for UTF-8 * @run main/othervm/timeout=2000 TestStringCodingUTF8 * @key randomness @@ -33,6 +33,9 @@ import java.nio.charset.*; public class TestStringCodingUTF8 { + + private static Random rnd = new Random(); + public static void main(String[] args) throws Throwable { test("UTF-8"); test("CESU-8"); @@ -76,7 +79,6 @@ test(cs, bmpsupp, 0, bmpsupp.length); // randomed "off" and "len" on shuffled data - Random rnd = new Random(); int maxlen = 1000; int itr = 5000; for (int i = 0; i < itr; i++) { @@ -101,12 +103,36 @@ System.out.println("done!"); } + 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 void test(Charset cs, char[] ca, int off, int len) throws Throwable { String str = new String(ca, off, len); byte[] ba = encode(cs, ca, off, len); + String csn = cs.name(); //getBytes(csn); - byte[] baStr = str.getBytes(cs.name()); + byte[] baStr = str.getBytes(csn); if (!Arrays.equals(ba, baStr)) throw new RuntimeException("getBytes(csn) failed"); @@ -115,13 +141,116 @@ if (!Arrays.equals(ba, baStr)) throw new RuntimeException("getBytes(cs) failed"); + + byte[] buf = new byte[ba.length + 10]; + int shortenLen = Math.max(0, ba.length - 10); + int slen = str.length(); + + // getBytes(ByteBuffer, cs); + ByteBuffer bb = ByteBuffer.wrap(buf); + int n = str.getBytes(0, slen, bb, cs); + int pos = bb.position(); + assertEquals(n, slen, "getBytes(bb cs)"); + assertEquals(buf, 0, pos, ba, 0, ba.length, "getBytes(bb, cs)"); + + bb.clear().position(3); + n = str.getBytes(0, str.length(), bb, cs); + pos = bb.position(); + assertEquals(n, slen, "getBytes(bb, cs)"); + assertEquals(buf, 3, pos, ba, 0, ba.length, "getBytes(bb/3, cs)"); + + bb.clear().limit(shortenLen); + n = str.getBytes(0, slen, bb, cs); + pos = bb.position(); + assertEquals(buf, 0, pos, ba, 0, pos, "getBytes(bb, cs)/shortenLen"); + + // loop + off = 0; + int m = 0; + bb.clear(); + while (off < slen) { + int nn = slen - off; + if (m++ < 10 && nn > 10) { + nn = rnd.nextInt(nn); + } + n = str.getBytes(off, off + nn, bb, cs); + off += n; + } + bb.flip(); + if (!new String(bb, cs).equals(new String(ba, cs))) { + +bb.rewind(); +String s1 = new String(bb, cs); +String s2 = new String(ba, 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(ByteBuffer/direct, cs); + bb = ByteBuffer.allocateDirect(buf.length); + n = str.getBytes(0, slen, bb, cs); + pos = bb.position(); + + // assertEquals(n, ba.length, "getBytes(bb cs)"); + assertEquals(bb, 0, ba, 0, ba.length, buf, "getBytes(bb, cs)"); + + bb.clear().position(3); + n = str.getBytes(0, slen, bb, cs); + pos = bb.position(); + // assertEquals(n, ba.length, "getBytes(bb, cs)"); + assertEquals(bb, 3, ba, 0, ba.length, buf, "getBytes(bb/3, cs)"); + + bb.clear().limit(shortenLen); + n = str.getBytes(0, slen, bb, cs); + pos = bb.position(); + assertEquals(bb, 0, ba, 0, pos, buf, "getBytes(bb, cs)/shortenLen"); + + + + ////////////////////////////////////////////////////////////// + + String expected = new String(decode(cs, ba, 0, ba.length)); + //new String(csn); - if (!new String(ba, cs.name()).equals(new String(decode(cs, ba, 0, ba.length)))) + if (!expected.equals(new String(ba, csn))) throw new RuntimeException("new String(csn) failed"); //new String(cs); - if (!new String(ba, cs).equals(new String(decode(cs, ba, 0, ba.length)))) + if (!expected.equals(new String(ba, cs))) throw new RuntimeException("new String(cs) failed"); + + //new String(bytebuffer, cs); + bb = ByteBuffer.allocate(ba.length + 10); + bb.put(ba); + bb.flip(); + if (!expected.equals(new String(bb, cs))) { + throw new RuntimeException("new String(bb, cs) failed"); + } + + bb.clear().position(3).put(ba).flip(); + if (!expected.equals(new String(bb.position(3), cs))) { + throw new RuntimeException("new String(cs)/pos=3 failed"); + } + + //new String(bytebuffer/direct, cs); + bb = ByteBuffer.allocateDirect(ba.length + 10); + bb.put(ba); + bb.flip(); + if (!expected.equals(new String(bb, cs))) { + throw new RuntimeException("new String(cs)/bmp failed"); + } + + bb.clear().position(3).put(ba).flip(); + if (!expected.equals(new String(bb.position(3), cs))) { + throw new RuntimeException("new String(cs)/pos=3 failed"); + } } // copy/paste of the StringCoding.decode()