3992N/A
3992N/A/*
3992N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3992N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3992N/A *
3992N/A * This code is free software; you can redistribute it and/or modify it
3992N/A * under the terms of the GNU General Public License version 2 only, as
3992N/A * published by the Free Software Foundation.
3992N/A *
3992N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3992N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3992N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3992N/A * version 2 for more details (a copy is included in the LICENSE file that
3992N/A * accompanied this code).
3992N/A *
3992N/A * You should have received a copy of the GNU General Public License version
3992N/A * 2 along with this work; if not, write to the Free Software Foundation,
3992N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3992N/A *
3992N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3992N/A * or visit www.oracle.com if you need additional information or have any
3992N/A * questions.
3992N/A */
3992N/A
2401N/A/**
2401N/A * @test
3992N/A * @bug 6945564 6959267 7033561
2401N/A * @summary Check that the j.l.Character.UnicodeScript
2401N/A */
2401N/A
2401N/Aimport java.io.*;
2401N/Aimport java.util.*;
2401N/Aimport java.util.regex.*;
2401N/Aimport java.lang.Character.UnicodeScript;
2401N/A
2401N/Apublic class CheckScript {
2401N/A
3992N/A public static void main(String[] args) throws Exception {
3992N/A File fScripts;
3992N/A File fAliases;
3081N/A if (args.length == 0) {
3992N/A fScripts = new File(System.getProperty("test.src", "."), "Scripts.txt");
3992N/A fAliases = new File(System.getProperty("test.src", "."), "PropertyValueAliases.txt");
3992N/A } else if (args.length == 2) {
3992N/A fScripts = new File(args[0]);
3992N/A fAliases = new File(args[1]);
3081N/A } else {
3992N/A System.out.println("java CharacterScript Scripts.txt PropertyValueAliases.txt");
3081N/A throw new RuntimeException("Datafile name should be specified.");
2401N/A }
3646N/A
2401N/A Matcher m = Pattern.compile("(\\p{XDigit}+)(?:\\.{2}(\\p{XDigit}+))?\\s+;\\s+(\\w+)\\s+#.*").matcher("");
2401N/A String line = null;
2401N/A HashMap<String,ArrayList<Integer>> scripts = new HashMap<>();
3992N/A try (BufferedReader sbfr = new BufferedReader(new FileReader(fScripts))) {
3646N/A while ((line = sbfr.readLine()) != null) {
3646N/A if (line.length() <= 1 || line.charAt(0) == '#') {
3646N/A continue;
2401N/A }
3646N/A m.reset(line);
3646N/A if (m.matches()) {
3646N/A int start = Integer.parseInt(m.group(1), 16);
3646N/A int end = (m.group(2)==null)?start
3646N/A :Integer.parseInt(m.group(2), 16);
3646N/A String name = m.group(3).toLowerCase(Locale.ENGLISH);
3646N/A ArrayList<Integer> ranges = scripts.get(name);
3646N/A if (ranges == null) {
3646N/A ranges = new ArrayList<Integer>();
3646N/A scripts.put(name, ranges);
3646N/A }
3646N/A ranges.add(start);
3646N/A ranges.add(end);
3646N/A }
2401N/A }
2401N/A }
2401N/A // check all defined ranges
2401N/A Integer[] ZEROSIZEARRAY = new Integer[0];
2401N/A for (String name : scripts.keySet()) {
2401N/A System.out.println("Checking " + name + "...");
2401N/A Integer[] ranges = scripts.get(name).toArray(ZEROSIZEARRAY);
2401N/A Character.UnicodeScript expected =
2401N/A Character.UnicodeScript.forName(name);
2401N/A
2401N/A int off = 0;
2401N/A while (off < ranges.length) {
2401N/A int start = ranges[off++];
2401N/A int end = ranges[off++];
2401N/A for (int cp = start; cp <= end; cp++) {
2401N/A Character.UnicodeScript script =
2401N/A Character.UnicodeScript.of(cp);
2401N/A if (script != expected) {
2401N/A throw new RuntimeException(
2401N/A "UnicodeScript failed: cp=" +
2401N/A Integer.toHexString(cp) +
2401N/A ", of(cp)=<" + script + "> but <" +
2401N/A expected + "> is expected");
2401N/A }
2401N/A }
2401N/A }
2401N/A }
2401N/A // check all codepoints
2401N/A for (int cp = 0; cp < Character.MAX_CODE_POINT; cp++) {
2401N/A Character.UnicodeScript script = Character.UnicodeScript.of(cp);
2401N/A if (script == Character.UnicodeScript.UNKNOWN) {
2401N/A if (Character.getType(cp) != Character.UNASSIGNED &&
2401N/A Character.getType(cp) != Character.SURROGATE &&
2401N/A Character.getType(cp) != Character.PRIVATE_USE)
2401N/A throw new RuntimeException(
2401N/A "UnicodeScript failed: cp=" +
2401N/A Integer.toHexString(cp) +
2401N/A ", of(cp)=<" + script + "> but UNKNOWN is expected");
2401N/A } else {
2401N/A Integer[] ranges =
2401N/A scripts.get(script.name().toLowerCase(Locale.ENGLISH))
2401N/A .toArray(ZEROSIZEARRAY);
2401N/A int off = 0;
2401N/A boolean found = false;
2401N/A while (off < ranges.length) {
2401N/A int start = ranges[off++];
2401N/A int end = ranges[off++];
2401N/A if (cp >= start && cp <= end)
2401N/A found = true;
2401N/A }
2401N/A if (!found) {
2401N/A throw new RuntimeException(
2401N/A "UnicodeScript failed: cp=" +
2401N/A Integer.toHexString(cp) +
2401N/A ", of(cp)=<" + script +
2401N/A "> but NOT in ranges of this script");
2401N/A
2401N/A }
2401N/A }
2401N/A }
3992N/A // check all aliases
3992N/A m = Pattern.compile("sc\\s*;\\s*(\\p{Alpha}{4})\\s*;\\s*([\\p{Alpha}|_]+)\\s*.*").matcher("");
3992N/A line = null;
3992N/A try (BufferedReader sbfr = new BufferedReader(new FileReader(fAliases))) {
3992N/A while ((line = sbfr.readLine()) != null) {
3992N/A if (line.length() <= 1 || line.charAt(0) == '#') {
3992N/A continue;
3992N/A }
3992N/A m.reset(line);
3992N/A if (m.matches()) {
3992N/A String alias = m.group(1);
3992N/A String name = m.group(2);
3992N/A // HRKT -> Katakana_Or_Hiragana not supported
3992N/A if ("HRKT".equals(alias.toUpperCase(Locale.ENGLISH)))
3992N/A continue;
3992N/A if (Character.UnicodeScript.forName(alias) !=
3992N/A Character.UnicodeScript.forName(name)) {
3992N/A throw new RuntimeException(
3992N/A "UnicodeScript failed: alias<" + alias +
3992N/A "> does not map to <" + name + ">");
3992N/A }
3992N/A }
3992N/A }
3992N/A }
2401N/A }
2401N/A}