Path: blob/master/test/jdk/java/lang/Character/CheckScript.java
41149 views
/*1* Copyright (c) 2010, 2019, 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 6945564 6959267 7033561 7070436 7198195 8032446 8072600 822143126* @summary Check that the j.l.Character.UnicodeScript27* @library /lib/testlibrary/java/lang28*/2930import java.io.*;31import java.util.*;32import java.util.regex.*;33import java.lang.Character.UnicodeScript;3435public class CheckScript {3637public static void main(String[] args) throws Exception {38File fScripts;39File fAliases;40if (args.length == 0) {41fScripts = UCDFiles.SCRIPTS.toFile();42fAliases = UCDFiles.PROPERTY_VALUE_ALIASES.toFile();43} else if (args.length == 2) {44fScripts = new File(args[0]);45fAliases = new File(args[1]);46} else {47System.out.println("java CharacterScript Scripts.txt PropertyValueAliases.txt");48throw new RuntimeException("Datafile name should be specified.");49}5051Matcher m = Pattern.compile("(\\p{XDigit}+)(?:\\.{2}(\\p{XDigit}+))?\\s+;\\s+(\\w+)\\s+#.*").matcher("");52String line = null;53HashMap<String,ArrayList<Integer>> scripts = new HashMap<>();54try (BufferedReader sbfr = new BufferedReader(new FileReader(fScripts))) {55while ((line = sbfr.readLine()) != null) {56if (line.length() <= 1 || line.charAt(0) == '#') {57continue;58}59m.reset(line);60if (m.matches()) {61int start = Integer.parseInt(m.group(1), 16);62int end = (m.group(2)==null)?start63:Integer.parseInt(m.group(2), 16);64String name = m.group(3).toLowerCase(Locale.ENGLISH);65ArrayList<Integer> ranges = scripts.get(name);66if (ranges == null) {67ranges = new ArrayList<Integer>();68scripts.put(name, ranges);69}70ranges.add(start);71ranges.add(end);72}73}74}75// check all defined ranges76Integer[] ZEROSIZEARRAY = new Integer[0];77for (String name : scripts.keySet()) {78System.out.println("Checking " + name + "...");79Integer[] ranges = scripts.get(name).toArray(ZEROSIZEARRAY);80Character.UnicodeScript expected =81Character.UnicodeScript.forName(name);8283int off = 0;84while (off < ranges.length) {85int start = ranges[off++];86int end = ranges[off++];87for (int cp = start; cp <= end; cp++) {88Character.UnicodeScript script =89Character.UnicodeScript.of(cp);90if (script != expected) {91throw new RuntimeException(92"UnicodeScript failed: cp=" +93Integer.toHexString(cp) +94", of(cp)=<" + script + "> but <" +95expected + "> is expected");96}97}98}99}100// check all codepoints101for (int cp = 0; cp < Character.MAX_CODE_POINT; cp++) {102Character.UnicodeScript script = Character.UnicodeScript.of(cp);103if (script == Character.UnicodeScript.UNKNOWN) {104if (Character.getType(cp) != Character.UNASSIGNED &&105Character.getType(cp) != Character.SURROGATE &&106Character.getType(cp) != Character.PRIVATE_USE)107throw new RuntimeException(108"UnicodeScript failed: cp=" +109Integer.toHexString(cp) +110", of(cp)=<" + script + "> but UNKNOWN is expected");111} else {112Integer[] ranges =113scripts.get(script.name().toLowerCase(Locale.ENGLISH))114.toArray(ZEROSIZEARRAY);115int off = 0;116boolean found = false;117while (off < ranges.length) {118int start = ranges[off++];119int end = ranges[off++];120if (cp >= start && cp <= end)121found = true;122}123if (!found) {124throw new RuntimeException(125"UnicodeScript failed: cp=" +126Integer.toHexString(cp) +127", of(cp)=<" + script +128"> but NOT in ranges of this script");129130}131}132}133// check all aliases134m = Pattern.compile("sc\\s*;\\s*(\\p{Alpha}{4})\\s*;\\s*([\\p{Alpha}|_]+)\\s*.*").matcher("");135line = null;136try (BufferedReader sbfr = new BufferedReader(new FileReader(fAliases))) {137while ((line = sbfr.readLine()) != null) {138if (line.length() <= 1 || line.charAt(0) == '#') {139continue;140}141m.reset(line);142if (m.matches()) {143String alias = m.group(1);144String name = m.group(2);145// HRKT -> Katakana_Or_Hiragana not supported146if ("HRKT".equals(alias.toUpperCase(Locale.ENGLISH)))147continue;148if (Character.UnicodeScript.forName(alias) !=149Character.UnicodeScript.forName(name)) {150throw new RuntimeException(151"UnicodeScript failed: alias<" + alias +152"> does not map to <" + name + ">");153}154}155}156}157}158}159160161