jdk/test/sun/nio/cs/EuroConverter.java
author sherman
Tue, 19 May 2009 16:03:02 -0700
changeset 2914 1375969fd02e
parent 796 2c3db720c065
child 5506 202f599c92aa
permissions -rw-r--r--
6843079: Putback for the new EUC_TW is not complete Summary: Putback the files missed in last putback Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     1
/*
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    21
 * have any questions.
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
 * @test
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    26
 * @bug      4114080
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    27
 * @summary  Make sure the euro converters, which are derived from
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    28
 * existing converters, only differ from their parents at the expected
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    29
 * code point.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    30
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    31
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    32
import java.text.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    33
import java.util.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    34
import java.io.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    35
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    36
/* Author: Alan Liu
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    37
 * 7/14/98
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    38
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    39
public class EuroConverter {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    40
    public static void main(String args[]) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    41
        boolean pass = true;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
        char[] map = new char[256]; // map for the encoding
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
        byte[] bytes = new byte[1]; // scratch
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
        char[] chars = new char[1]; // scratch
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
        for (int i=0; i<DATA.length; ) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
            String euroEnc = DATA[i++];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
            String parentEnc = DATA[i++];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
            System.out.println("Checking encoder " + euroEnc + " against " + parentEnc);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
            String currentEnc = parentEnc;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
            try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    52
                // Fill map with parent values
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    53
                for (int j=-128; j<128; ++j) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    54
                    bytes[0] = (byte)j;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
                    char parentValue = new String(bytes, parentEnc).charAt(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
                    // NOTE: 0x25 doesn't round trip on the EBCDIC code pages,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
                    // so we don't check that code point in the sanity check.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
                    if (j != 0x0025) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
                        chars[0] = parentValue;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
                        int parentRoundTrip = new String(chars).getBytes(parentEnc)[0];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
                        // This is a sanity check -- we aren't really testing the parent
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
                        // encoder here.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
                        if (parentRoundTrip != j) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
                            pass = false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    65
                            System.out.println("Error: Encoder " + parentEnc +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
                                           " fails round-trip: " + j +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
                                           " -> \\u" + Integer.toHexString(parentValue) +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    68
                                           " -> " + parentRoundTrip);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
                        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
                    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
                    map[(j+0x100)&0xFF] = parentValue;
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
                // Modify map with new expected values.  Each pair has code point, parent value, euro value.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
                // Terminated by null.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
                while (DATA[i] != null) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    77
                    int codePoint = Integer.valueOf(DATA[i++], 16).intValue();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    78
                    char expectedParentValue = DATA[i++].charAt(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    79
                    char expectedEuroValue = DATA[i++].charAt(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    80
                    // This is a sanity check -- we aren't really testing the parent
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    81
                    // encoder here.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    82
                    if (map[codePoint] != expectedParentValue) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    83
                        pass = false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    84
                        System.out.println("Error: Encoder " + parentEnc +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    85
                                           " " + Integer.toHexString(codePoint) + " -> \\u" +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    86
                                           Integer.toHexString(map[codePoint]) + ", expected \\u" +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    87
                                           Integer.toHexString(expectedParentValue));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    88
                    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    89
                    // Fill in new expected value
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    90
                    map[codePoint] = expectedEuroValue;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    91
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    92
                ++i; // Skip over null at end of set
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    93
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
                // Now verify the euro encoder
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
                currentEnc = euroEnc;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    96
                for (int j=-128; j<128; ++j) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    97
                    bytes[0] = (byte)j;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    98
                    char euroValue = new String(bytes, euroEnc).charAt(0);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    99
                    chars[0] = euroValue;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   100
                    // NOTE: 0x15 doesn't round trip on the EBCDIC code pages,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   101
                    // so we don't check that code point in the sanity check.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   102
                    if (j != 0x0015) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   103
                        int euroRoundTrip = new String(chars).getBytes(euroEnc)[0];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   104
                        if (euroRoundTrip != j) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   105
                            pass = false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   106
                            System.out.println("Error: Encoder " + euroEnc +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   107
                                           " fails round-trip at " + j);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   108
                        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   109
                    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   110
                    // Compare against the map
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   111
                    if (euroValue != map[(j+0x100)&0xFF]) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   112
                        pass = false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   113
                        System.out.println("Error: Encoder " + euroEnc +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   114
                                           " " + Integer.toHexString((j+0x100)&0xFF) + " -> \\u" +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   115
                                           Integer.toHexString(euroValue) + ", expected \\u" +
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   116
                                           Integer.toHexString(map[(j+0x100)&0xFF]));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   117
                    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   118
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   119
            } catch (UnsupportedEncodingException e) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   120
                System.out.println("Unsupported encoding " + currentEnc);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   121
                pass = false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   122
                while (i < DATA.length && DATA[i] != null) ++i;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   123
                ++i; // Skip over null
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   124
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   125
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   126
        if (!pass) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   127
            throw new RuntimeException("Bug 4114080 - Euro encoder test failed");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   128
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   129
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   130
    static String[] DATA = {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   131
        // New converter, parent converter, [ code point that changed, parent code point value,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   132
        // euro code point value ], null
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   133
        // Any number of changed code points may be specified, including zero.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   134
        "ISO8859_15_FDIS", "ISO8859_1",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   135
            "A4", "\u00A4", "\u20AC",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   136
            "A6", "\u00A6", "\u0160",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   137
            "A8", "\u00A8", "\u0161",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   138
            "B4", "\u00B4", "\u017D",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   139
            "B8", "\u00B8", "\u017E",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   140
            "BC", "\u00BC", "\u0152",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   141
            "BD", "\u00BD", "\u0153",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   142
            "BE", "\u00BE", "\u0178",
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   143
            null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   144
        // 923 is IBM's name for ISO 8859-15; make sure they're identical
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   145
        "Cp923", "ISO8859_15_FDIS", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   146
        "Cp858", "Cp850", "D5", "\u0131", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   147
        "Cp1140", "Cp037", "9F", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   148
        "Cp1141", "Cp273", "9F", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   149
        "Cp1142", "Cp277", "5A", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   150
        "Cp1143", "Cp278", "5A", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   151
        "Cp1144", "Cp280", "9F", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   152
        "Cp1145", "Cp284", "9F", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   153
        "Cp1146", "Cp285", "9F", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   154
        "Cp1147", "Cp297", "9F", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   155
        "Cp1148", "Cp500", "9F", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   156
        "Cp1149", "Cp871", "9F", "\u00A4", "\u20AC", null,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   157
    };
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   158
}