2115N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2115N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2115N/A *
2115N/A * This code is free software; you can redistribute it and/or modify it
2115N/A * under the terms of the GNU General Public License version 2 only, as
2115N/A * published by the Free Software Foundation.
2115N/A *
2115N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2115N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2115N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2115N/A * version 2 for more details (a copy is included in the LICENSE file that
2115N/A * accompanied this code).
2115N/A *
2115N/A * You should have received a copy of the GNU General Public License version
2115N/A * 2 along with this work; if not, write to the Free Software Foundation,
2115N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2115N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2115N/A */
2115N/A
2115N/A/* @test
2115N/A * @bug 4712786
2115N/A * @summary Check charsets against reference files
2115N/A *
2115N/A * @build Util
2115N/A * @run main Check shift_jis ref.shift_jis
2115N/A * @run main/othervm -Dsun.nio.cs.map=Windows-31J/Shift_JIS Check shift_jis ref.windows-31j
2115N/A */
2115N/A
2115N/Aimport java.io.*;
2115N/Aimport java.nio.*;
2115N/Aimport java.nio.channels.*;
2115N/Aimport java.nio.charset.*;
2115N/Aimport java.util.*;
2115N/Aimport java.util.regex.*;
2115N/A
2115N/A
2115N/Apublic class Check {
2115N/A
2115N/A private static PrintStream log = System.err;
2115N/A
2115N/A private static final int UNICODE_SIZE = (1 << 16);
2115N/A
2115N/A private final String csName;
2115N/A private final String refName;
2115N/A private byte[][] bytes = new byte[UNICODE_SIZE][]; // Indexed by char
2115N/A
2115N/A private int errors = 0;
2115N/A
2115N/A private Check(String csn, String refn) {
2115N/A csName = csn;
2115N/A refName = refn;
2115N/A }
2115N/A
2115N/A private Check load()
2115N/A throws IOException
2115N/A {
2115N/A File fn = new File(System.getProperty("test.src", "."), refName);
2115N/A FileChannel fc = new FileInputStream(fn).getChannel();
2115N/A ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
2115N/A CharBuffer cb = Charset.forName("US-ASCII").decode(bb);
2115N/A Pattern p = Pattern.compile("^(\\p{XDigit}+) +(\\p{XDigit}+)$",
2115N/A Pattern.MULTILINE);
2115N/A Matcher m = p.matcher(cb);
2115N/A while (m.find()) {
2115N/A char c = (char)Integer.parseInt(m.group(1), 16);
2115N/A String v = m.group(2);
2115N/A int nb = v.length() >> 1;
2115N/A byte[] ba = new byte[nb];
2115N/A for (int i = 0; i < nb; i++) {
2115N/A ba[i] = (byte)Integer.parseInt(v.substring(i << 1, (i << 1) + 2),
2115N/A 16);
2115N/A }
2115N/A bytes[c] = ba;
2115N/A }
2115N/A return this;
2115N/A }
2115N/A
2115N/A private void error() {
2115N/A if (++errors >= 100)
2115N/A throw new RuntimeException("100 errors occurred (there might be more)");
2115N/A }
2115N/A
2115N/A private void mismatch(String s, byte[] expected, byte[] got) {
2115N/A log.println("Encoding mismatch on \""
2115N/A + Util.toString(s)
2115N/A + "\": Expected {"
2115N/A + Util.toString(expected)
2115N/A + "}, got {"
2115N/A + Util.toString(got)
2115N/A + "}");
2115N/A error();
2115N/A }
2115N/A
2115N/A private void mismatch(int i, byte[] ba, String expected, String got) {
2115N/A log.println("Decoding mismatch on \""
2115N/A + Util.toString((char)i)
2115N/A + "\", input {"
2115N/A + Util.toString(ba)
2115N/A + "}: Expected \""
2115N/A + Util.toString(expected)
2115N/A + "\", got \""
2115N/A + Util.toString(got)
2115N/A + "\"");
2115N/A error();
2115N/A }
2115N/A
2115N/A private void check()
2115N/A throws IOException
2115N/A {
2115N/A
2115N/A // String.getBytes(String csn)
2115N/A for (int i = 0; i < UNICODE_SIZE; i++) {
2115N/A if (bytes[i] == null)
2115N/A continue;
2115N/A String s = new String(new char[]{ (char)i });
2115N/A byte[] ba = s.getBytes(csName);
2115N/A if (Util.cmp(ba, bytes[i]) >= 0)
2115N/A mismatch(s, bytes[i], ba);
2115N/A }
2115N/A log.println("String.getBytes(\"" + csName + "\") okay");
2115N/A
2115N/A // String(byte[] ba, String csn)
2115N/A for (int i = 0; i < UNICODE_SIZE; i++) {
2115N/A if (bytes[i] == null)
2115N/A continue;
2115N/A String r = new String(new char[]{ (char)i });
2115N/A String s = new String(bytes[i], csName);
2115N/A if (!r.equals(s))
2115N/A mismatch(i, bytes[i], r, s);
2115N/A }
2115N/A log.println("String(byte[] ba, \"" + csName + "\") okay");
2115N/A
2115N/A // To be really thorough we should test OutputStreamWriter,
2115N/A // InputStreamReader, and Charset{De,En}Coder here also,
2115N/A // but the above will do for now.
2115N/A
2115N/A if (errors > 0) {
2115N/A throw new RuntimeException(errors + " error(s) occurred");
2115N/A }
2115N/A
2115N/A }
2115N/A
2115N/A // Usage: Check charsetName referenceFileName
2115N/A public static void main(String[] args) throws Exception {
2115N/A new Check(args[0], args[1]).load().check();
2115N/A }
2115N/A
2115N/A}