jdk/test/java/io/Unicode.java
author sherman
Tue, 22 Jun 2010 14:04:36 -0400
changeset 5962 0913689fd3a0
parent 5506 202f599c92aa
permissions -rw-r--r--
6963156: TEST_BUG: Several tests under sun/nio/cs failed Summary: Updated the test cases and removed them from ProblemList.txt Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   @bug 4241440 4220470
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Test the various two-byte Unicode encodings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
   @run main Unicode UnicodeLittle little true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
   @run main Unicode UnicodeBig big true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
   @run main Unicode UnicodeLittleUnmarked little false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
   @run main Unicode UnicodeBigUnmarked big false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
   @run main Unicode iso-10646-ucs-2 big false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
   @run main Unicode x-utf-16be big false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
   @run main Unicode x-utf-16le little false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class Unicode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static final int BOM_HIGH = 0xfe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final int BOM_LOW  = 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int BIG = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final int LITTLE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void fail(String enc, String msg, int e0, int e1, int b0, int b1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        throw new Exception(enc + ": " + msg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                            + ": Expected "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                            + Integer.toHexString(e0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                            + " " + Integer.toHexString(e1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                            + ", got "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                            + Integer.toHexString(b0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                            + " " + Integer.toHexString(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* Chars to bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static void encode(String enc, int byteOrder, boolean markExpected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        String s = "abc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        byte[] b = s.getBytes(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (markExpected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            int b0 = b[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            int b1 = b[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            int e0 = 0, e1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (byteOrder == BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                e0 = BOM_HIGH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                e1 = BOM_LOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } else if (byteOrder == LITTLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                e0 = BOM_LOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                e1 = BOM_HIGH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if ((b0 != e0) || (b1 != e1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                fail(enc, "Incorrect or missing byte-order mark",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                     e0, e1, b0, b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        for (int j = 0; j < s.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            char c = s.charAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            int b0 = b[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            int b1 = b[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            int e0 = 0, e1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (byteOrder == BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                e0 = c >> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                e1 = c & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } else if (byteOrder == LITTLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                e0 = c & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                e1 = c >> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if ((b0 != e0) || (b1 != e1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                fail(enc, "Incorrect content at index " + j,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                     e0, e1, b0, b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /* Bytes to chars */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static void decode(String enc, int byteOrder, boolean markit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        String s = "abc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (markit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (byteOrder == BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                bo.write(BOM_HIGH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                bo.write(BOM_LOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } else if (byteOrder == LITTLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                bo.write(BOM_LOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                bo.write(BOM_HIGH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (byteOrder == BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                bo.write(c >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                bo.write(c & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } else if (byteOrder == LITTLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                bo.write(c & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                bo.write(c >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        byte[] b = bo.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        String s2 = new String(b, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (!s.equals(s2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new Exception(enc + ": Decode error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        String enc = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        String bos = args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        boolean markExpected = Boolean.valueOf(args[2]).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int byteOrder = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (bos.equals("big")) byteOrder = BIG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (bos.equals("little")) byteOrder = LITTLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /* We run each test twice in order to check the repeatability of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
           String.getBytes and String(byte[], String) (4220470) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        encode(enc, byteOrder, markExpected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        encode(enc, byteOrder, markExpected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        decode(enc, byteOrder, markExpected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        decode(enc, byteOrder, markExpected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}