jdk/test/java/nio/charset/coders/Check.java
author katleman
Thu, 05 Jan 2012 08:42:37 -0800
changeset 11376 075fe3928b7f
parent 5506 202f599c92aa
child 44115 bb4e971bf5d4
permissions -rw-r--r--
Added tag jdk8-b20 for changeset 90e14ec89395
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     4
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     8
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    13
 * accompanied this code).
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    14
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4817
diff changeset
    21
 * questions.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    22
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    23
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    24
/* @test
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    25
 * @bug 4712786
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    26
 * @summary Check charsets against reference files
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    27
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    28
 * @build Util
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    29
 * @run main Check shift_jis ref.shift_jis
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    30
 * @run main/othervm -Dsun.nio.cs.map=Windows-31J/Shift_JIS Check shift_jis ref.windows-31j
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    31
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    32
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    33
import java.io.*;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    34
import java.nio.*;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    35
import java.nio.channels.*;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    36
import java.nio.charset.*;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    37
import java.util.*;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    38
import java.util.regex.*;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    39
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    40
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    41
public class Check {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    42
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    43
    private static PrintStream log = System.err;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    44
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    45
    private static final int UNICODE_SIZE = (1 << 16);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    46
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    47
    private final String csName;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    48
    private final String refName;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    49
    private byte[][] bytes = new byte[UNICODE_SIZE][]; // Indexed by char
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    50
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    51
    private int errors = 0;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    52
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    53
    private Check(String csn, String refn) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    54
        csName = csn;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    55
        refName = refn;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    56
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    57
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    58
    private Check load()
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    59
        throws IOException
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    60
    {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    61
        File fn = new File(System.getProperty("test.src", "."), refName);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    62
        FileChannel fc = new FileInputStream(fn).getChannel();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    63
        ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    64
        CharBuffer cb = Charset.forName("US-ASCII").decode(bb);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    65
        Pattern p = Pattern.compile("^(\\p{XDigit}+) +(\\p{XDigit}+)$",
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    66
                                    Pattern.MULTILINE);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    67
        Matcher m = p.matcher(cb);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    68
        while (m.find()) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    69
            char c = (char)Integer.parseInt(m.group(1), 16);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    70
            String v = m.group(2);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    71
            int nb = v.length() >> 1;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    72
            byte[] ba = new byte[nb];
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    73
            for (int i = 0; i < nb; i++) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    74
                ba[i] = (byte)Integer.parseInt(v.substring(i << 1, (i << 1) + 2),
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    75
                                               16);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    76
            }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    77
            bytes[c] = ba;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    78
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    79
        return this;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    80
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    81
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    82
    private void error() {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    83
        if (++errors >= 100)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    84
            throw new RuntimeException("100 errors occurred (there might be more)");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    85
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    86
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    87
    private void mismatch(String s, byte[] expected, byte[] got) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    88
        log.println("Encoding mismatch on \""
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    89
                    + Util.toString(s)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    90
                    + "\": Expected {"
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    91
                    + Util.toString(expected)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    92
                    + "}, got {"
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    93
                    + Util.toString(got)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    94
                    + "}");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    95
        error();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    96
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    97
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    98
    private void mismatch(int i, byte[] ba, String expected, String got) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    99
        log.println("Decoding mismatch on \""
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   100
                    + Util.toString((char)i)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   101
                    + "\", input {"
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   102
                    + Util.toString(ba)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   103
                    + "}: Expected \""
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   104
                    + Util.toString(expected)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   105
                    + "\", got \""
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   106
                    + Util.toString(got)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   107
                    + "\"");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   108
        error();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   109
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   110
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   111
    private void check()
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   112
        throws IOException
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   113
    {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   114
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   115
        // String.getBytes(String csn)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   116
        for (int i = 0; i < UNICODE_SIZE; i++) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   117
            if (bytes[i] == null)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   118
                continue;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   119
            String s = new String(new char[]{ (char)i });
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   120
            byte[] ba = s.getBytes(csName);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   121
            if (Util.cmp(ba, bytes[i]) >= 0)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   122
                mismatch(s, bytes[i], ba);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   123
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   124
        log.println("String.getBytes(\"" + csName + "\") okay");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   125
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   126
        // String(byte[] ba, String csn)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   127
        for (int i = 0; i < UNICODE_SIZE; i++) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   128
            if (bytes[i] == null)
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   129
                continue;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   130
            String r = new String(new char[]{ (char)i });
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   131
            String s = new String(bytes[i], csName);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   132
            if (!r.equals(s))
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   133
                mismatch(i, bytes[i], r, s);
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   134
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   135
        log.println("String(byte[] ba, \"" + csName + "\") okay");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   136
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   137
        // To be really thorough we should test OutputStreamWriter,
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   138
        // InputStreamReader, and Charset{De,En}Coder here also,
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   139
        // but the above will do for now.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   140
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   141
        if (errors > 0) {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   142
            throw new RuntimeException(errors + " error(s) occurred");
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   143
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   144
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   145
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   146
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   147
    // Usage: Check charsetName referenceFileName
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   148
    public static void main(String[] args) throws Exception {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   149
        new Check(args[0], args[1]).load().check();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   150
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   151
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   152
}