Path: blob/master/test/jdk/sun/nio/cs/EuroConverter.java
41149 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4114080 818680326* @summary Make sure the euro converters, which are derived from27* existing converters, only differ from their parents at the expected28* code point.29* @modules jdk.charsets30*/3132import java.text.*;33import java.util.*;34import java.io.*;3536/* Author: Alan Liu37* 7/14/9838*/39public class EuroConverter {40public static void main(String args[]) throws Exception {41boolean pass = true;42char[] map = new char[256]; // map for the encoding43byte[] bytes = new byte[1]; // scratch44char[] chars = new char[1]; // scratch45for (int i=0; i<DATA.length; ) {46String euroEnc = DATA[i++];47String parentEnc = DATA[i++];48System.out.println("Checking encoder " + euroEnc + " against " + parentEnc);49String currentEnc = parentEnc;5051try {52// Fill map with parent values53for (int j=-128; j<128; ++j) {54bytes[0] = (byte)j;55char parentValue = new String(bytes, parentEnc).charAt(0);56// NOTE: 0x25 doesn't round trip on the EBCDIC code pages,57// so we don't check that code point in the sanity check.58if (j != 0x0025) {59chars[0] = parentValue;60int parentRoundTrip = new String(chars).getBytes(parentEnc)[0];61// This is a sanity check -- we aren't really testing the parent62// encoder here.63if (parentRoundTrip != j) {64pass = false;65System.out.println("Error: Encoder " + parentEnc +66" fails round-trip: " + j +67" -> \\u" + Integer.toHexString(parentValue) +68" -> " + parentRoundTrip);69}70}71map[(j+0x100)&0xFF] = parentValue;72}7374// Modify map with new expected values. Each pair has code point, parent value, euro value.75// Terminated by null.76while (DATA[i] != null) {77int codePoint = Integer.valueOf(DATA[i++], 16).intValue();78char expectedParentValue = DATA[i++].charAt(0);79char expectedEuroValue = DATA[i++].charAt(0);80// This is a sanity check -- we aren't really testing the parent81// encoder here.82if (map[codePoint] != expectedParentValue) {83pass = false;84System.out.println("Error: Encoder " + parentEnc +85" " + Integer.toHexString(codePoint) + " -> \\u" +86Integer.toHexString(map[codePoint]) + ", expected \\u" +87Integer.toHexString(expectedParentValue));88}89// Fill in new expected value90map[codePoint] = expectedEuroValue;91}92++i; // Skip over null at end of set9394// Now verify the euro encoder95currentEnc = euroEnc;96for (int j=-128; j<128; ++j) {97bytes[0] = (byte)j;98char euroValue = new String(bytes, euroEnc).charAt(0);99chars[0] = euroValue;100// NOTE: 0x25 doesn't round trip on the EBCDIC code pages,101// so we don't check that code point in the sanity check.102if (j != 0x0025) {103int euroRoundTrip = new String(chars).getBytes(euroEnc)[0];104if (euroRoundTrip != j) {105pass = false;106System.out.println("Error: Encoder " + euroEnc +107" fails round-trip at " + j);108}109}110// Compare against the map111if (euroValue != map[(j+0x100)&0xFF]) {112pass = false;113System.out.println("Error: Encoder " + euroEnc +114" " + Integer.toHexString((j+0x100)&0xFF) + " -> \\u" +115Integer.toHexString(euroValue) + ", expected \\u" +116Integer.toHexString(map[(j+0x100)&0xFF]));117}118}119} catch (UnsupportedEncodingException e) {120System.out.println("Unsupported encoding " + currentEnc);121pass = false;122while (i < DATA.length && DATA[i] != null) ++i;123++i; // Skip over null124}125}126if (!pass) {127throw new RuntimeException("Bug 4114080 - Euro encoder test failed");128}129}130static String[] DATA = {131// New converter, parent converter, [ code point that changed, parent code point value,132// euro code point value ], null133// Any number of changed code points may be specified, including zero.134"ISO8859_15_FDIS", "ISO8859_1",135"A4", "\u00A4", "\u20AC",136"A6", "\u00A6", "\u0160",137"A8", "\u00A8", "\u0161",138"B4", "\u00B4", "\u017D",139"B8", "\u00B8", "\u017E",140"BC", "\u00BC", "\u0152",141"BD", "\u00BD", "\u0153",142"BE", "\u00BE", "\u0178",143null,144// 923 is IBM's name for ISO 8859-15; make sure they're identical145"Cp923", "ISO8859_15_FDIS", null,146"Cp858", "Cp850", "D5", "\u0131", "\u20AC", null,147"Cp1140", "Cp037", "9F", "\u00A4", "\u20AC", null,148"Cp1141", "Cp273", "9F", "\u00A4", "\u20AC", null,149"Cp1142", "Cp277", "5A", "\u00A4", "\u20AC", null,150"Cp1143", "Cp278", "5A", "\u00A4", "\u20AC", null,151"Cp1144", "Cp280", "9F", "\u00A4", "\u20AC", null,152"Cp1145", "Cp284", "9F", "\u00A4", "\u20AC", null,153"Cp1146", "Cp285", "9F", "\u00A4", "\u20AC", null,154"Cp1147", "Cp297", "9F", "\u00A4", "\u20AC", null,155"Cp1148", "Cp500", "9F", "\u00A4", "\u20AC", null,156"Cp1149", "Cp871", "9F", "\u00A4", "\u20AC", null,157};158}159160161