test/jdk/sun/nio/cs/TestUTF_16.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 27292 jdk/test/sun/nio/cs/TestUTF_16.java@7ff4b24b33ce
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
/* @test
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    25
   @bug 4403848 6348426 6407730
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    26
   @summary Check correctness of the UTF-16 converter in all its flavors
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.IOException;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    30
import java.nio.BufferOverflowException;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    31
import java.nio.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    32
import java.nio.charset.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    33
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    34
public class TestUTF_16 {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    35
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    36
    private static void testDecode(String charset,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    37
                                   String expected,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    38
                                   byte[] input)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    39
    throws Exception
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    40
    {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    41
        String out = new String(input, charset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
        if (!out.equals(expected)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
            failureReport (out, expected);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
            throw new Exception("UTF_16 Decoding test failed " + charset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
    private static void testEncode(String charset,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
                                   String input,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
                                   byte[] expected)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
    throws Exception
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    52
    {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    53
        byte[] testBytes = input.getBytes(charset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    54
        for (int i = 0; i< expected.length; i++)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
            if (testBytes[i] != expected[i])
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
                throw new Exception("UTF_16 Encoding test failed " + charset);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
    private static void warn(String s) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
        System.err.println("FAILED Test 4403848 UTF-16" +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
                            s) ;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    65
    private static void failureReport(String testStr,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
                                      String expected) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    68
        System.err.println ("Expected Characters:");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
        for (int i = 0; i < expected.length() ; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
            warn("expected char[" + i + "] : " +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
                  Integer.toHexString((int)expected.charAt(i)) +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    72
                 "obtained char[" + i + "] : " +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    73
                  Integer.toHexString((int)testStr.charAt(i)));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    74
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    77
    /*
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    78
    private static void checkResult(char[] expected,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    79
                                    String testStr,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    80
                                    String testName)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    81
    throws Exception
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    82
    {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    83
        if (testStr.length() != expected.length)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    84
            failureReport(testStr, expected);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    85
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    86
        for (int i = 0; i < testStr.length(); i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    87
            if (testStr.charAt(i) != expected[i]) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    88
                failureReport(testStr, expected);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    89
                throw new Exception ("REGTEST TestUTF16 failed: "
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    90
                                        + testName);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    91
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    92
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    93
        System.err.println ("Test " + testName + " PASSED");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
        return;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    96
    */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    97
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    98
    private static void test() throws Exception  {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    99
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   100
            // Tests: Check decoding of UTF-16.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   101
            //        Ensures correct endian polarity
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   102
            //        of the decoders and appropriate
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   103
            //        interpretation of BOM bytes where
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   104
            //        they are required.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   105
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   106
            // Test 1: BigEndian UTF-16 Decoding
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   107
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   108
            testDecode("UTF_16BE", "\u0092\u0093",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   109
                        new byte[] { (byte) 0x00, (byte) 0x92,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   110
                                     (byte) 0x00, (byte) 0x93 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   111
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   112
            // Test 1a: BigEndian UTF-16 Decoding. BOM bytes provided.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   113
            testDecode("UTF_16BE", "\ufeff\u0092\u0093",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   114
                        new byte[] { (byte) 0xfe, (byte) 0xff,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   115
                                     (byte) 0x00, (byte) 0x92,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   116
                                     (byte) 0x00, (byte) 0x93 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   117
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   118
            testDecode("UTF_16LE", "\u9200\u9300",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   119
                        new byte[] { (byte) 0x00, (byte) 0x92,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   120
                                     (byte) 0x00, (byte) 0x93 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   121
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   122
            // Test 2a: LittleEndian  UTF-16 Decoding, BOM bytes provided.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   123
            testDecode("UTF_16LE", "\ufeff\u9200\u9300",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   124
                        new byte[] { (byte) 0xff, (byte) 0xfe,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   125
                                     (byte) 0x00, (byte) 0x92,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   126
                                     (byte) 0x00, (byte) 0x93 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   127
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   128
            // Test 3: UTF-16 (with mandatory byte order mark) Decoding
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   129
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   130
            testDecode("UTF-16", "\u9200\u9300",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   131
                        new byte[] { (byte) 0xfe, (byte) 0xff,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   132
                                     (byte) 0x92, (byte) 0x00,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   133
                                     (byte) 0x93, (byte) 0x00 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   134
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   135
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   136
            // Test 3a: UTF-16 BOM omitted. This should decode OK.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   137
            testDecode("UTF-16", "\u9200\u9300",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   138
                        new byte[] { (byte) 0x92, (byte) 0x00,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   139
                                     (byte) 0x93, (byte) 0x00 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   140
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   141
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   142
            // Test 4: encoding using UTF-16
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   143
            // BOM must be emitted when encoding and must be BigEndian.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   144
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   145
            testEncode("UTF-16", "\u0123",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   146
                        new byte[] { (byte) 0xfe, (byte) 0xff,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   147
                                     (byte) 0x01, (byte) 0x23 });
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   148
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   149
            // Test 5:
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   150
            if (CoderResult.OVERFLOW !=
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   151
                Charset.forName("UTF_16")
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   152
                .newDecoder()
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 5506
diff changeset
   153
                .decode((ByteBuffer.allocate(4)
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   154
                                     .put(new byte[]
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   155
                                          {(byte)0xd8,(byte)0x00,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   156
                                           (byte)0xdc,(byte)0x01})
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   157
                                     .flip()),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   158
                        CharBuffer.allocate(1),
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   159
                        true)) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   160
                throw new Exception ("REGTEST TestUTF16 Overflow test failed");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   161
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   162
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   163
            // Test 6: decoding using UTF_16LE_BOM/UnicodeLittle
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   164
            // UnicodeLittle should accept non-BOM byte sequence
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   165
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   166
            testDecode("UnicodeLittle", "Arial",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   167
                        new byte[] { 'A', 0, 'r', 0, 'i', 0, 'a', 0, 'l', 0});
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   168
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   169
            System.err.println ("\nPASSED UTF-16 encoder test");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   170
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   171
        // Reversed BOM in middle of stream Negative test.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   172
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   173
        /*
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   174
        boolean caughtException = false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   175
        try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   176
                String out = new String(new byte[] {(byte)0x00,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   177
                                            (byte)0x92,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   178
                                            (byte)0xff,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   179
                                            (byte)0xfe},
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   180
                                            "UTF-16");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   181
        } catch (IOException e) { caughtException = true; }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   182
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   183
        if (caughtException == false)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   184
           throw new Exception ("Incorrectly parsed BOM in middle of input");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   185
        */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   186
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   187
            // Fixed included with bug 4403848 fixes buffer sizing
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   188
            // issue due to non provision of additional 2 bytes
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   189
            // headroom for initial BOM bytes for UTF-16 encoding.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   190
          System.err.println ("OVERALL PASS OF UTF-16 Test");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   191
   }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   192
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   193
   public static void main (String[] args) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   194
     test();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   195
   }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   196
}