jdk/test/sun/nio/cs/TestUTF_32.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 27292 7ff4b24b33ce
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     4
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     8
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    13
 * accompanied this code).
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    14
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    21
 * questions.
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    22
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    23
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    24
/*
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    25
   @bug 6346419
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    26
   @summary Check correctness of the UTF-32 and its variant charsets
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    27
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    28
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    29
import java.io.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    30
import java.nio.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    31
import java.nio.charset.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    32
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    33
public class TestUTF_32 {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    34
    private static void testDecode(String charset,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    35
                                   String expected,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    36
                                   byte[] input)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    37
        throws Exception
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    38
    {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    39
        String out = new String(input, charset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    40
        if (!out.equals(expected)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    41
            failureReport (out, expected);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
            throw new Exception("UTF_32 Decoding test failed: " + charset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
    private static void testEncode(String charset,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
                                   String input,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
                                   byte[] expected)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
        throws Exception
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
    {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
        byte[] testBytes = input.getBytes(charset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    52
        for (int i = 0; i< expected.length; i++)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    53
            if (testBytes[i] != expected[i])
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    54
                throw new Exception("UTF_32 Encoding test failed: [" + i + "]"+ charset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
    private static void warn(String s) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
        System.err.println("FAILED Test UTF-32:" +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
                            s) ;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
    private static void failureReport(String testStr,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
                                      String expected) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    65
        System.err.println ("Expected Characters:");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
        for (int i = 0; i < expected.length() ; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
            warn("expected char[" + i + "] : " +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    68
                  Integer.toHexString((int)expected.charAt(i)) +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
                 "obtained char[" + i + "] : " +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
                  Integer.toHexString((int)testStr.charAt(i)));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    72
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    73
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    74
    private static void writeInt(OutputStream os, int i, boolean isBig)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
        throws Exception
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
    {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    77
        if (isBig) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    78
            os.write((i>>24) & 0xff);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    79
            os.write((i>>16) & 0xff);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    80
            os.write((i>>8) & 0xff);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    81
            os.write(i & 0xff);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    82
        } else {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    83
            os.write(i & 0xff);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    84
            os.write((i>>8) & 0xff);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    85
            os.write((i>>16) & 0xff);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    86
            os.write((i>>24) & 0xff);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    87
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    88
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    89
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    90
    private static byte[] getBytes(boolean doBOM, boolean isBig)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    91
        throws Exception
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    92
    {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    93
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024*1024);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
        if (doBOM)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
           writeInt(baos, 0xfeff, isBig);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    96
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    97
        for (int i = 0; i < 0xffff; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    98
            if (i < Character.MIN_SURROGATE ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    99
                i > Character.MAX_SURROGATE)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   100
            writeInt(baos, i, isBig);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   101
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   102
        for (int i = 0x10000; i < 0x1ffff; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   103
            writeInt(baos, i, isBig);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   104
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   105
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   106
        for (int i = 0x100000; i < 0x10ffff; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   107
            writeInt(baos, i, isBig);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   108
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   109
        byte[] bb = baos.toByteArray();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   110
        baos.close();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   111
        return bb;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   112
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   113
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   114
    public static void main (String[] args) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   115
        byte[] bb;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   116
        String s;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   117
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   118
        // Test 1: UTF_32 BigEndian
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   119
        bb = getBytes(false, true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   120
        s = new String(bb, "UTF-32");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   121
        testDecode("UTF_32", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   122
        testEncode("UTF_32", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   123
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   124
        // Test 2: UTF_32 LittleEndian Decoding With BOM and
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   125
        //         BigEndian Encoding
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   126
        bb = getBytes(true, false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   127
        s = new String(bb, "UTF-32");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   128
        bb = getBytes(false, true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   129
        testDecode("UTF_32", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   130
        testEncode("UTF_32", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   131
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   132
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   133
        // Test 3: UTF_32BE
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   134
        bb = getBytes(false, true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   135
        s = new String(bb, "UTF-32BE");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   136
        testDecode("UTF_32BE", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   137
        testEncode("UTF_32BE", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   138
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   139
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   140
        // Test 4: UTF_32LE
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   141
        bb = getBytes(false, false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   142
        s = new String(bb, "UTF-32LE");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   143
        testDecode("UTF_32LE", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   144
        testEncode("UTF_32LE", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   145
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   146
        // Test 5: UTF_32BE_BOM
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   147
        bb = getBytes(true, true);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   148
        s = new String(bb, "UTF-32BE-BOM");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   149
        testDecode("UTF_32BE_BOM", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   150
        testEncode("UTF_32BE_BOM", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   151
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   152
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   153
        // Test 6: UTF_32LE_BOM
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   154
        bb = getBytes(true, false);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   155
        s = new String(bb, "UTF-32LE-BOM");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   156
        testDecode("UTF_32LE_BOM", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   157
        testEncode("UTF_32LE_BOM", s, bb);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   158
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   159
        s = "\u4e00\ufffd\u4e01";
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   160
        // Test 7: BigEndian with reverse BOM in middle
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   161
        bb = new byte[] {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   162
            (byte)0x00,(byte)0x00,(byte)0x4e,(byte)0x00,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   163
            (byte)0xff,(byte)0xfe,(byte)0x00,(byte)0x00,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   164
            (byte)0x00,(byte)0x00,(byte)0x4e,(byte)0x01
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   165
        };
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   166
        if (!s.equals(new String(bb, "UTF_32")) ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   167
            !s.equals(new String(bb, "UTF_32BE")) ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   168
            !s.equals(new String(bb, "UTF_32BE_BOM")))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   169
            throw new Exception("UTF_32 Decoding test failed: ");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   170
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   171
        // Test 7: LittleEndian with reverse BOM in middle
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   172
        bb = new byte[] {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   173
            (byte)0xff,(byte)0xfe,(byte)0x00,(byte)0x00,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   174
            (byte)0x00,(byte)0x4e,(byte)0x00,(byte)0x00,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   175
            (byte)0x00,(byte)0x00,(byte)0xfe,(byte)0xff,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   176
            (byte)0x01,(byte)0x4e,(byte)0x00,(byte)0x00
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   177
        };
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   178
        if (!s.equals(new String(bb, "UTF_32")) ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   179
            !s.equals(new String(bb, "UTF_32LE")) ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   180
            !s.equals(new String(bb, "UTF_32LE_BOM")))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   181
            throw new Exception("UTF_32 Decoding test failed: ");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   182
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   183
        // Test 8: Overflow
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   184
        if (CoderResult.OVERFLOW !=
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   185
            Charset.forName("UTF_32")
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   186
            .newDecoder()
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   187
            .decode((ByteBuffer)(ByteBuffer.allocate(4)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   188
                                 .put(new byte[]
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   189
                                      {(byte)0,(byte)1, (byte)0,(byte)01})
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   190
                                 .flip()),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   191
                    CharBuffer.allocate(1),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   192
                    true)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   193
            throw new Exception ("Test UTF-32 Overflow test failed");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   194
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   195
        System.err.println ("OVERALL PASS OF UTF-32 Test");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   196
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   197
}