--- /dev/null 2018-03-12 15:28:26.000000000 +0530 +++ new/test/jdk/java/lang/Character/CharCheck.java 2018-03-12 15:28:25.000000000 +0530 @@ -0,0 +1,560 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * + * @author Alan Liu + * @author John O'Conner + */ + +import java.io.*; + + +/** + * This class either loads or dumps the character properties of all Unicode + * characters out to a file. When loading, it compares the loaded data with + * that obtained through the java.lang.Character API. This allows detection of + * changes to the character properties between versions of the Java VM. A + * typical usage would be to dump the properties under an early VM, and load + * them under a later VM. + * + * Also: Check the current VM's character properties against those in a + * Unicode database. The database should be of the format + * available on ftp.unicode.org/Public/UNIDATA. + * + */ +public class CharCheck { + static int differences = 0; + + public static void main(String args[]) throws Exception { + + if (args.length != 2 && args.length != 3) usage(); + if (args[0].equals("dump")) + dump(Integer.parseInt(args[1], 16), new ObjectOutputStream(new FileOutputStream(args[2]))); + else if (args[0].equals("load")) + load(Integer.parseInt(args[1], 16), new ObjectInputStream(new FileInputStream(args[2]))); + else if (args[0].equals("check")) + check(Integer.parseInt(args[1], 16), new File(args[2])); + else if (args[0].equals("char")) + showChar(Integer.parseInt(args[1],16)); + else if (args[0].equals("fchar")) + showFileChar(args[1], Integer.parseInt(args[2],16)); + else usage(); + if (differences != 0) { + throw new RuntimeException("There are differences between Character properties and the specification."); + } + } + + static void usage() { + System.err.println("Usage: java CharCheck "); + System.err.println("where is one of the following:"); + System.err.println("dump - dumps the character properties of the given plane,"); + System.err.println(" read from the current VM, to the given file."); + System.err.println("load - loads the character properties from the given"); + System.err.println(" file and compares them to those of the given character plane"); + System.err.println(" in the current VM."); + System.err.println("check - compare the current VM's character properties"); + System.err.println(" in the given plane to those listed in the given file, "); + System.err.println(" which should be in the format available on "); + System.err.println(" ftp.unicode.org/Public/2.0-Update."); + System.err.println("char - show current VM properties of the given Unicode char."); + System.err.println("fchar - show file properties of the given Unicode char."); + System.exit(0); + } + + static String getTypeName(int type) { + return (type >= 0 && type < UnicodeSpec.generalCategoryList.length) ? + (UnicodeSpec.generalCategoryList[type][UnicodeSpec.LONG] + '(' + type + ')') : + (""); + } + + static int check(int plane, File specFile) throws Exception { + + String version = System.getProperty("java.version"); + System.out.println("Current VM version " + version); + int rangeLimit = (plane << 16) | 0xFFFF; + String record; + UnicodeSpec[] spec = UnicodeSpec.readSpecFile(specFile, plane); + int rangeStart = 0x0000; + boolean isRange = false; + + lastCheck = (plane << 16) - 1; + + for (int currentSpec = 0; currentSpec < spec.length; currentSpec++) { + int c = spec[currentSpec].getCodePoint(); + if (isRange) { + // Must see end of range now + if (spec[currentSpec].getName().endsWith("Last>")) { + for (int d=rangeStart; d<=c; d++) { + checkOneChar(d, spec[currentSpec]); + } + } + else { + // No good -- First without Last + System.out.println("BAD FILE: First without last at '" + escape(rangeStart) + "'"); + } + isRange = false; + } + else { + // Look for a First, Last pair: This is a pair of entries like the following: + // 4E00;;Lo;0;L;;;;;N;;;;; + // 9FA5;;Lo;0;L;;;;;N;;;;; + if (spec[currentSpec].getName().endsWith("First>")) { + rangeStart = c; + isRange = true; + } + else { + checkOneChar(c, spec[currentSpec]); + } + } + } + + // Check undefined chars at the end of the range + + while (lastCheck < rangeLimit) checkOneCharDefined(++lastCheck, "?", false); + + System.out.println("Total differences: "+differences); + return differences; + } + + static int lastCheck = -1; + + static final void checkOneCharDefined(int c, String name, boolean fileDefined) { + if (Character.isDefined(c) != fileDefined) + showDifference(c, name, "isDefined", ""+(!fileDefined), ""+fileDefined); + } + + // In GenerateCharacter, the following ranges are handled specially. + // Each is the start of a 26-character range with values 10..35. + static final char NUMERIC_EXCEPTION[] = { '\u0041', '\u0061', '\uFF21', '\uFF41' }; + + static void checkOneChar(int c, UnicodeSpec charSpec) { + // Handle intervening ranges -- we assume that we will be called in monotonically + // increasing order. If the last char we checked is more than one before this + // char, then check the intervening range -- it should all be undefined. + int lowerLimit = (c & 0xFF0000); + if (lastCheck >= lowerLimit && (lastCheck+1) != c) { + for (int i=lastCheck+1; i= NUMERIC_EXCEPTION[k] && c < (char)(NUMERIC_EXCEPTION[k]+26)) { + numericValueDB = c - NUMERIC_EXCEPTION[k] + 10; + break; + } + } + } + else { + String strValue = charSpec.getNumericValue(); + int parsedNumericValue; + if (strValue.equals("10000000000") + || strValue.equals("1000000000000")) { + System.out.println("Skipping strValue: " + strValue + + " for " + charSpec.getName() + + "(0x" + Integer.toHexString(c) + ")"); + parsedNumericValue = -2; + } else { + parsedNumericValue = strValue.indexOf('/') < 0 ? + Integer.parseInt(strValue) : -2; + } + numericValueDB = parsedNumericValue < 0 ? -2 : parsedNumericValue; + } + if (numericValue != numericValueDB) + showDifference(c, charSpec.getName(), "numeric value", ""+numericValue, ""+numericValueDB); + } + + static void showDifference(int c, String name, String property, String vmValue, String dbValue) { + System.out.println(escape("Mismatch at '" + hex6(c) + "' (" + name+ "): " + + property + "=" + vmValue + ", db=" + dbValue)); + ++differences; + } + + /** + * Given a record containing ';'-separated fields, return the fieldno-th + * field. The first field is field 0. + */ + static String getField(String record, int fieldno) { + int i=0; + int j=record.indexOf(';'); + while (fieldno > 0) { + i=j+1; + j=record.indexOf(';', i); + } + return record.substring(i, j); + } + + static final int FIELD_COUNT = 15; + + /** + * Given a record containing ';'-separated fields, return an array of + * the fields. It is assumed that there are FIELD_COUNT fields per record. + */ + static void getFields(String record, String[] fields) { + int i=0; + int j=record.indexOf(';'); + fields[0] = record.substring(i, j); + for (int n=1; n= 0x20 && c <= 0x7F) buf.append(c); + else { + buf.append("\\u"); + String h = "000" + Integer.toHexString(c); + if (h.length() > 4) h = h.substring(h.length() - 4); + buf.append(h); + } + } + return buf.toString(); + } + + static String escape(int c) { + StringBuffer buf = new StringBuffer(); + if (c >= 0x20 && c <= 0x7F) buf.append(c); + else { + buf.append("\\u"); + String h = "000" + Integer.toHexString(c); + if (h.length() > 4) h = h.substring(h.length() - 4); + buf.append(h); + } + return buf.toString(); + } +} + + +//eof