aboutsummaryrefslogtreecommitdiff
path: root/test/sun/nio
diff options
context:
space:
mode:
authorsherman <none@none>2012-07-17 19:57:31 -0700
committersherman <none@none>2012-07-17 19:57:31 -0700
commiteaa347739fa9e3ce6753773fef5696ab8b06b424 (patch)
treedde79ca5cfc29fc18c2ed7e2d3ea4c8aa162b6f5 /test/sun/nio
parentb022fc5ce74c0522be147dc3810d0ba9fcafe022 (diff)
7183053: Optimize DoubleByte charset for String.getBytes()/new String(byte[])
Summary: DoubleByte implements sun/nio.cs/ArrayDe/Encoder interface Reviewed-by: alanb
Diffstat (limited to 'test/sun/nio')
-rw-r--r--test/sun/nio/cs/StrCodingBenchmark.java3
-rw-r--r--test/sun/nio/cs/StrCodingBenchmarkDB.java139
-rw-r--r--test/sun/nio/cs/TestStringCoding.java64
3 files changed, 201 insertions, 5 deletions
diff --git a/test/sun/nio/cs/StrCodingBenchmark.java b/test/sun/nio/cs/StrCodingBenchmark.java
index 9c97d4715..1d28ba5aa 100644
--- a/test/sun/nio/cs/StrCodingBenchmark.java
+++ b/test/sun/nio/cs/StrCodingBenchmark.java
@@ -75,7 +75,7 @@ public class StrCodingBenchmark {
return nanoss;
}
- public static void time(Job ... jobs) throws Throwable {
+ public static long[] time(Job ... jobs) throws Throwable {
long[] warmup = time0(jobs); // Warm up run
long[] nanoss = time0(jobs); // Real timing run
@@ -110,6 +110,7 @@ public class StrCodingBenchmark {
// Print out absolute and relative times, calibrated against first job
for (int i = 0; i < jobs.length; i++)
System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]);
+ return milliss;
}
public static Job[] filter(Pattern filter, Job[] jobs) {
diff --git a/test/sun/nio/cs/StrCodingBenchmarkDB.java b/test/sun/nio/cs/StrCodingBenchmarkDB.java
new file mode 100644
index 000000000..bb7210227
--- /dev/null
+++ b/test/sun/nio/cs/StrCodingBenchmarkDB.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2009, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.util.*;
+import java.nio.*;
+import java.nio.charset.*;
+import java.util.concurrent.*;
+import java.util.regex.Pattern;
+
+public class StrCodingBenchmarkDB extends StrCodingBenchmark {
+
+
+ public static void main(String[] args) throws Throwable {
+ final int itrs = Integer.getInteger("iterations", 100000);
+ //final int itrs = Integer.getInteger("iterations", 12);
+ final int size = Integer.getInteger("size", 2048);
+ final int subsize = Integer.getInteger("subsize", 128);
+ final int maxchar = Integer.getInteger("maxchar", 128);
+ final String regex = System.getProperty("filter");
+ final Pattern filter = (regex == null) ? null : Pattern.compile(regex);
+ final boolean useSecurityManager = Boolean.getBoolean("SecurityManager");
+ if (useSecurityManager)
+ System.setSecurityManager(new PermissiveSecurityManger());
+ final Random rnd = new Random();
+
+ String[] csns = new String[] {
+ "Big5",
+ "Johab",
+ "EUC_CN",
+ "EUC_KR",
+ "MS932",
+ "MS936",
+ "MS949",
+ "MS950",
+ "GBK",
+
+ "Big5_HKSCS",
+ "Big5_HKSCS_2001",
+ "Big5_Solaris",
+ "MS950_HKSCS",
+ "MS950_HKSCS_XP",
+ "IBM1364",
+ "IBM1381",
+ "IBM1383",
+ "IBM930",
+ "IBM933",
+ "IBM935",
+ "IBM937",
+ "IBM939",
+ "IBM942",
+ "IBM943",
+ "IBM948",
+ "IBM949",
+ "IBM950",
+ "IBM970",
+ };
+
+ ArrayList<long[]> sum = new ArrayList<>();
+
+ for (final String csn : csns) {
+ final Charset cs = Charset.forName(csn);
+ List<Integer> cps = new ArrayList<>(0x4000);
+ int off = 0;
+ int cp = 0;
+ int n = 0;
+ CharsetEncoder enc = cs.newEncoder();
+ while (cp < 0x10000 && n < cps.size()) {
+ if (enc.canEncode((char)cp)) {
+ cps.add(cp);
+ n++;
+ }
+ cp++;
+ }
+ Collections.shuffle(cps);
+ char[] ca = new char[cps.size()];
+ for (int i = 0; i < cps.size(); i++)
+ ca[i] = (char)(int)cps.get(i);
+
+
+ System.out.printf("%n--------%s---------%n", csn);
+ for (int sz = 8; sz <= 2048; sz *= 2) {
+ System.out.printf(" [len=%d]%n", sz);
+
+ final char[] chars = Arrays.copyOf(ca, sz);
+ final String str = new String(chars);
+ final byte[] bs = str.getBytes(cs);
+
+ Job[] jobs = {
+
+ new Job("String decode: csn") {
+ public void work() throws Throwable {
+ for (int i = 0; i < itrs; i++)
+ new String(bs, csn);
+ }},
+
+ new Job("String decode: cs") {
+ public void work() throws Throwable {
+ for (int i = 0; i < itrs; i++)
+ new String(bs, cs);
+ }},
+
+ new Job("String encode: csn") {
+ public void work() throws Throwable {
+ for (int i = 0; i < itrs; i++)
+ str.getBytes(csn);
+ }},
+
+ new Job("String encode: cs") {
+ public void work() throws Throwable {
+ for (int i = 0; i < itrs; i++)
+ str.getBytes(cs);
+ }},
+ };
+ sum.add(time(jobs));
+
+ }
+ }
+ }
+}
diff --git a/test/sun/nio/cs/TestStringCoding.java b/test/sun/nio/cs/TestStringCoding.java
index 09e614448..76f31d889 100644
--- a/test/sun/nio/cs/TestStringCoding.java
+++ b/test/sun/nio/cs/TestStringCoding.java
@@ -24,7 +24,7 @@
*/
/* @test
- @bug 6636323 6636319 7040220 7096080
+ @bug 6636323 6636319 7040220 7096080 7183053
@summary Test if StringCoding and NIO result have the same de/encoding result
* @run main/othervm/timeout=2000 TestStringCoding
*/
@@ -70,11 +70,62 @@ public class TestStringCoding {
}
test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
}
+
+ testMixed(cs);
System.out.println("done!");
}
}
}
+ static void testMixed(Charset cs) throws Throwable {
+ CharsetDecoder dec = cs.newDecoder()
+ .onMalformedInput(CodingErrorAction.REPLACE)
+ .onUnmappableCharacter(CodingErrorAction.REPLACE);
+ CharsetEncoder enc = cs.newEncoder()
+ .onMalformedInput(CodingErrorAction.REPLACE)
+ .onUnmappableCharacter(CodingErrorAction.REPLACE);
+ List<Integer> cps = new ArrayList<>(0x10000);
+ int off = 0;
+ int cp = 0;
+ while (cp < 0x10000) {
+ if (enc.canEncode((char)cp)) {
+ cps.add(cp);
+ }
+ cp++;
+ }
+ 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());
+
+ }
+
static void test(Charset cs, char[] bmpCA, byte[] sbBA) throws Throwable {
String bmpStr = new String(bmpCA);
CharsetDecoder dec = cs.newDecoder()
@@ -100,6 +151,7 @@ public class TestStringCoding {
//new String(csn);
String strSC = new String(sbBA, cs.name());
String strNIO = dec.reset().decode(ByteBuffer.wrap(sbBA)).toString();
+
if(!strNIO.equals(strSC))
throw new RuntimeException("new String(csn) failed -> " + cs.name());
@@ -112,7 +164,7 @@ public class TestStringCoding {
if (enc instanceof sun.nio.cs.ArrayEncoder &&
cs.contains(Charset.forName("ASCII"))) {
if (cs.name().equals("UTF-8") || // utf8 handles surrogates
- cs.name().equals("CESU-8")) // utf8 handles surrogates
+ cs.name().equals("CESU-8")) // utf8 handles surrogates
return;
enc.replaceWith(new byte[] { (byte)'A'});
sun.nio.cs.ArrayEncoder cae = (sun.nio.cs.ArrayEncoder)enc;
@@ -137,12 +189,16 @@ public class TestStringCoding {
cs.name())))
throw new RuntimeException("encode3(surrogates) failed -> "
+ cs.name());
+ /* sun.nio.cs.ArrayDeEncoder works on the assumption that the
+ invoker (StringCoder) allocates enough output buf, utf8
+ and double-byte coder does not check the output buffer limit.
ba = new byte[str.length() - 1];
n = cae.encode(str.toCharArray(), 0, str.length(), ba);
- if (n != 7 || !"abABABc".equals(new String(ba, 0, n,
- cs.name())))
+ if (n != 7 || !"abABABc".equals(new String(ba, 0, n, cs.name()))) {
throw new RuntimeException("encode4(surrogates) failed -> "
+ cs.name());
+ }
+ */
}
}