|
1 /* |
|
2 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @bug 6945564 6959267 7033561 7070436 7198195 8032446 8072600 |
|
27 * @summary Check that the j.l.Character.UnicodeScript |
|
28 */ |
|
29 |
|
30 import java.io.*; |
|
31 import java.util.*; |
|
32 import java.util.regex.*; |
|
33 import java.lang.Character.UnicodeScript; |
|
34 |
|
35 public class CheckScript { |
|
36 |
|
37 public static void main(String[] args) throws Exception { |
|
38 File fScripts; |
|
39 File fAliases; |
|
40 if (args.length == 0) { |
|
41 fScripts = new File(System.getProperty("test.src", "."), "Scripts.txt"); |
|
42 fAliases = new File(System.getProperty("test.src", "."), "PropertyValueAliases.txt"); |
|
43 } else if (args.length == 2) { |
|
44 fScripts = new File(args[0]); |
|
45 fAliases = new File(args[1]); |
|
46 } else { |
|
47 System.out.println("java CharacterScript Scripts.txt PropertyValueAliases.txt"); |
|
48 throw new RuntimeException("Datafile name should be specified."); |
|
49 } |
|
50 |
|
51 Matcher m = Pattern.compile("(\\p{XDigit}+)(?:\\.{2}(\\p{XDigit}+))?\\s+;\\s+(\\w+)\\s+#.*").matcher(""); |
|
52 String line = null; |
|
53 HashMap<String,ArrayList<Integer>> scripts = new HashMap<>(); |
|
54 try (BufferedReader sbfr = new BufferedReader(new FileReader(fScripts))) { |
|
55 while ((line = sbfr.readLine()) != null) { |
|
56 if (line.length() <= 1 || line.charAt(0) == '#') { |
|
57 continue; |
|
58 } |
|
59 m.reset(line); |
|
60 if (m.matches()) { |
|
61 int start = Integer.parseInt(m.group(1), 16); |
|
62 int end = (m.group(2)==null)?start |
|
63 :Integer.parseInt(m.group(2), 16); |
|
64 String name = m.group(3).toLowerCase(Locale.ENGLISH); |
|
65 ArrayList<Integer> ranges = scripts.get(name); |
|
66 if (ranges == null) { |
|
67 ranges = new ArrayList<Integer>(); |
|
68 scripts.put(name, ranges); |
|
69 } |
|
70 ranges.add(start); |
|
71 ranges.add(end); |
|
72 } |
|
73 } |
|
74 } |
|
75 // check all defined ranges |
|
76 Integer[] ZEROSIZEARRAY = new Integer[0]; |
|
77 for (String name : scripts.keySet()) { |
|
78 System.out.println("Checking " + name + "..."); |
|
79 Integer[] ranges = scripts.get(name).toArray(ZEROSIZEARRAY); |
|
80 Character.UnicodeScript expected = |
|
81 Character.UnicodeScript.forName(name); |
|
82 |
|
83 int off = 0; |
|
84 while (off < ranges.length) { |
|
85 int start = ranges[off++]; |
|
86 int end = ranges[off++]; |
|
87 for (int cp = start; cp <= end; cp++) { |
|
88 Character.UnicodeScript script = |
|
89 Character.UnicodeScript.of(cp); |
|
90 if (script != expected) { |
|
91 throw new RuntimeException( |
|
92 "UnicodeScript failed: cp=" + |
|
93 Integer.toHexString(cp) + |
|
94 ", of(cp)=<" + script + "> but <" + |
|
95 expected + "> is expected"); |
|
96 } |
|
97 } |
|
98 } |
|
99 } |
|
100 // check all codepoints |
|
101 for (int cp = 0; cp < Character.MAX_CODE_POINT; cp++) { |
|
102 Character.UnicodeScript script = Character.UnicodeScript.of(cp); |
|
103 if (script == Character.UnicodeScript.UNKNOWN) { |
|
104 if (Character.getType(cp) != Character.UNASSIGNED && |
|
105 Character.getType(cp) != Character.SURROGATE && |
|
106 Character.getType(cp) != Character.PRIVATE_USE) |
|
107 throw new RuntimeException( |
|
108 "UnicodeScript failed: cp=" + |
|
109 Integer.toHexString(cp) + |
|
110 ", of(cp)=<" + script + "> but UNKNOWN is expected"); |
|
111 } else { |
|
112 Integer[] ranges = |
|
113 scripts.get(script.name().toLowerCase(Locale.ENGLISH)) |
|
114 .toArray(ZEROSIZEARRAY); |
|
115 int off = 0; |
|
116 boolean found = false; |
|
117 while (off < ranges.length) { |
|
118 int start = ranges[off++]; |
|
119 int end = ranges[off++]; |
|
120 if (cp >= start && cp <= end) |
|
121 found = true; |
|
122 } |
|
123 if (!found) { |
|
124 throw new RuntimeException( |
|
125 "UnicodeScript failed: cp=" + |
|
126 Integer.toHexString(cp) + |
|
127 ", of(cp)=<" + script + |
|
128 "> but NOT in ranges of this script"); |
|
129 |
|
130 } |
|
131 } |
|
132 } |
|
133 // check all aliases |
|
134 m = Pattern.compile("sc\\s*;\\s*(\\p{Alpha}{4})\\s*;\\s*([\\p{Alpha}|_]+)\\s*.*").matcher(""); |
|
135 line = null; |
|
136 try (BufferedReader sbfr = new BufferedReader(new FileReader(fAliases))) { |
|
137 while ((line = sbfr.readLine()) != null) { |
|
138 if (line.length() <= 1 || line.charAt(0) == '#') { |
|
139 continue; |
|
140 } |
|
141 m.reset(line); |
|
142 if (m.matches()) { |
|
143 String alias = m.group(1); |
|
144 String name = m.group(2); |
|
145 // HRKT -> Katakana_Or_Hiragana not supported |
|
146 if ("HRKT".equals(alias.toUpperCase(Locale.ENGLISH))) |
|
147 continue; |
|
148 if (Character.UnicodeScript.forName(alias) != |
|
149 Character.UnicodeScript.forName(name)) { |
|
150 throw new RuntimeException( |
|
151 "UnicodeScript failed: alias<" + alias + |
|
152 "> does not map to <" + name + ">"); |
|
153 } |
|
154 } |
|
155 } |
|
156 } |
|
157 } |
|
158 } |