Path: blob/master/test/jdk/java/nio/charset/coders/Check.java
41153 views
/*1* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4712786 822996025* @summary Check charsets against reference files26* @modules jdk.charsets27*28* @build Util29* @run main Check shift_jis ref.shift_jis30*/3132import java.io.*;33import java.nio.*;34import java.nio.channels.*;35import java.nio.charset.*;36import java.util.*;37import java.util.regex.*;383940public class Check {4142private static PrintStream log = System.err;4344private static final int UNICODE_SIZE = (1 << 16);4546private final String csName;47private final String refName;48private byte[][] bytes = new byte[UNICODE_SIZE][]; // Indexed by char4950private int errors = 0;5152private Check(String csn, String refn) {53csName = csn;54refName = refn;55}5657private Check load()58throws IOException59{60File fn = new File(System.getProperty("test.src", "."), refName);61FileChannel fc = new FileInputStream(fn).getChannel();62ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());63CharBuffer cb = Charset.forName("US-ASCII").decode(bb);64Pattern p = Pattern.compile("^(\\p{XDigit}+) +(\\p{XDigit}+)$",65Pattern.MULTILINE);66Matcher m = p.matcher(cb);67while (m.find()) {68char c = (char)Integer.parseInt(m.group(1), 16);69String v = m.group(2);70int nb = v.length() >> 1;71byte[] ba = new byte[nb];72for (int i = 0; i < nb; i++) {73ba[i] = (byte)Integer.parseInt(v.substring(i << 1, (i << 1) + 2),7416);75}76bytes[c] = ba;77}78return this;79}8081private void error() {82if (++errors >= 100)83throw new RuntimeException("100 errors occurred (there might be more)");84}8586private void mismatch(String s, byte[] expected, byte[] got) {87log.println("Encoding mismatch on \""88+ Util.toString(s)89+ "\": Expected {"90+ Util.toString(expected)91+ "}, got {"92+ Util.toString(got)93+ "}");94error();95}9697private void mismatch(int i, byte[] ba, String expected, String got) {98log.println("Decoding mismatch on \""99+ Util.toString((char)i)100+ "\", input {"101+ Util.toString(ba)102+ "}: Expected \""103+ Util.toString(expected)104+ "\", got \""105+ Util.toString(got)106+ "\"");107error();108}109110private void check()111throws IOException112{113114// String.getBytes(String csn)115for (int i = 0; i < UNICODE_SIZE; i++) {116if (bytes[i] == null)117continue;118String s = new String(new char[]{ (char)i });119byte[] ba = s.getBytes(csName);120if (Util.cmp(ba, bytes[i]) >= 0)121mismatch(s, bytes[i], ba);122}123log.println("String.getBytes(\"" + csName + "\") okay");124125// String(byte[] ba, String csn)126for (int i = 0; i < UNICODE_SIZE; i++) {127if (bytes[i] == null)128continue;129String r = new String(new char[]{ (char)i });130String s = new String(bytes[i], csName);131if (!r.equals(s))132mismatch(i, bytes[i], r, s);133}134log.println("String(byte[] ba, \"" + csName + "\") okay");135136// To be really thorough we should test OutputStreamWriter,137// InputStreamReader, and Charset{De,En}Coder here also,138// but the above will do for now.139140if (errors > 0) {141throw new RuntimeException(errors + " error(s) occurred");142}143144}145146// Usage: Check charsetName referenceFileName147public static void main(String[] args) throws Exception {148new Check(args[0], args[1]).load().check();149}150151}152153154