File: CheckBlocks.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (346 lines) | stat: -rw-r--r-- 12,256 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright (c) 2007, 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.
 */

/*
 * @test
 * @bug 4830803 4886934 6565620 6959267 7070436 7198195 8032446 8072600 8202771
 * @summary  Check that the UnicodeBlock forName() method works as expected and block ranges are correct for all Unicode characters.
 * @run main CheckBlocks
 * @author John O'Conner
 */

import java.lang.Character.UnicodeBlock;
import java.lang.reflect.Field;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Locale;

public class CheckBlocks {

    static boolean err = false;
    static Class<?> clazzUnicodeBlock;

    public static void main(String[] args) throws Exception {
        generateBlockList();

        try {
            clazzUnicodeBlock = Class.forName("java.lang.Character$UnicodeBlock");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Class.forName(\"java.lang.Character$UnicodeBlock\") failed.");
        }

        for (Block blk : blocks) {
            test4830803_1(blk);
            test4830803_2();
            test4886934(blk);
        }

        test8202771();

        if (err) {
            throw new RuntimeException("Failed");
        } else {
            System.out.println("Passed");
        }
    }

    /**
     * Check that the UnicodeBlock forName() method works as expected.
     */
    private static void test4830803_1(Block blk) throws Exception {

        /*
         * Try 3 forms of block name in the forName() method. Each form should
         * produce the same expected block.
         */
        String blkName = blk.getName();

        // For backward compatibility
        switch (blkName) {
            case "COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS":
                blkName = "COMBINING_MARKS_FOR_SYMBOLS";
                System.out.println("*** COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS"
                        + " is replaced with COMBINING_MARKS_FOR_SYMBOLS"
                        + " for backward compatibility.");
                break;
            case "GREEK_AND_COPTIC":
                blkName = "GREEK";
                System.out.println("*** GREEK_AND_COPTIC is replaced with GREEK"
                        + " for backward compatibility.");
                break;
            case "CYRILLIC_SUPPLEMENT":
                blkName = "CYRILLIC_SUPPLEMENTARY";
                System.out.println("*** CYRILLIC_SUPPLEMENT is replaced with"
                        + " CYRILLIC_SUPPLEMENTARY for backward compatibility.");
                break;
            default:
                break;
        }

        String expectedBlock = null;
        try {
            expectedBlock = clazzUnicodeBlock.getField(blkName).getName();
        } catch (NoSuchFieldException | SecurityException e) {
            System.err.println("Error: " + blkName + " was not found.");
            err = true;
            return;
        }

        String canonicalBlockName = blk.getOriginalName();
        String idBlockName = expectedBlock;
        String regexBlockName = toRegExString(canonicalBlockName);

        if (regexBlockName == null) {
            System.err.println("Error: Block name which was processed with regex was null.");
            err = true;
            return;
        }

        if (!expectedBlock.equals(UnicodeBlock.forName(canonicalBlockName).toString())) {
            System.err.println("Error #1: UnicodeBlock.forName(\"" +
                    canonicalBlockName + "\") returned wrong value.\n\tGot: " +
                    UnicodeBlock.forName(canonicalBlockName) +
                    "\n\tExpected: " + expectedBlock);
            err = true;
        }

        if (!expectedBlock.equals(UnicodeBlock.forName(idBlockName).toString())) {
            System.err.println("Error #2: UnicodeBlock.forName(\"" +
                    idBlockName + "\") returned wrong value.\n\tGot: " +
                    UnicodeBlock.forName(idBlockName) +
                    "\n\tExpected: " + expectedBlock);
            err = true;
        }

        if (!expectedBlock.equals(UnicodeBlock.forName(regexBlockName).toString())) {
            System.err.println("Error #3: UnicodeBlock.forName(\"" +
                    regexBlockName + "\") returned wrong value.\n\tGot: " +
                    UnicodeBlock.forName(regexBlockName) +
                    "\n\tExpected: " + expectedBlock);
            err = true;
        }
    }

    /**
     * now try a bad block name. This should produce an IAE.
     */
    private static void test4830803_2() {
        boolean threwExpected = false;

        try {
            UnicodeBlock block = UnicodeBlock.forName("notdefined");
        }
        catch(IllegalArgumentException e) {
            threwExpected = true;
        }

        if (threwExpected == false) {
            System.err.println("Error: UnicodeBlock.forName(\"notdefined\") should throw IllegalArgumentException.");
            err = true;
        }
    }

    /**
     * Convert the argument to a block name form used by the regex package.
     * That is, remove all spaces.
     */
    private static String toRegExString(String str) {
        String[] tokens = null;
        StringBuilder retStr = new StringBuilder();
        try {
            tokens = str.split(" ");
        }
        catch(java.util.regex.PatternSyntaxException e) {
            return null;
        }
        for(int x=0; x < tokens.length; ++x) {
            retStr.append(tokens[x]);
        }
        return retStr.toString();
    }

    private static void test4886934(Block blk) {
        String blkName = blk.getName();
        String blkOrigName = blk.getOriginalName();
        UnicodeBlock block;
        String blockName;

        // For backward compatibility
        switch (blkName) {
            case "COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS":
                blkName = "COMBINING_MARKS_FOR_SYMBOLS";
                System.out.println("*** COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS"
                        + " is replaced with COMBINING_MARKS_FOR_SYMBOLS"
                        + " for backward compatibility.");
                break;
            case "GREEK_AND_COPTIC":
                blkName = "GREEK";
                System.out.println("*** GREEK_AND_COPTIC is replaced with GREEK"
                        + " for backward compatibility.");
                break;
            case "CYRILLIC_SUPPLEMENT":
                blkName = "CYRILLIC_SUPPLEMENTARY";
                System.out.println("*** CYRILLIC_SUPPLEMENT is replaced with"
                        + " CYRILLIC_SUPPLEMENTARY for backward compatibility.");
                break;
            default:
                break;
        }

        for (int ch = blk.getBegin(); ch <= blk.getEnd(); ch++) {
            block = UnicodeBlock.of(ch);
            if (block == null) {
                System.err.println("Error: The block for " + blkName
                        + " is missing. Please check java.lang.Character.UnicodeBlock.");
                err = true;
                break;
            }
            blockName = block.toString();
            if (!blockName.equals(blkName)) {
                System.err.println("Error: Character(0x"
                        + Integer.toHexString(ch).toUpperCase()
                        + ") should be in \"" + blkName + "\" block "
                        + "(Block name is \"" + blkOrigName + "\")"
                        + " but found in \"" + blockName + "\" block.");
                err = true;
            }
        }
    }

    /**
     * Check if every Field of Character.UnicodeBlock is a valid Unicode Block.
     */
    private static void test8202771() {
        Field[] fields = clazzUnicodeBlock.getFields();

        for (Field f : fields) {
            // Handle Deprecated field "SURROGATES_AREA".
            if (f.getAnnotation(Deprecated.class) != null) {
                continue;
            }

            String blkName = f.getName();
            switch (blkName) {
                case "COMBINING_MARKS_FOR_SYMBOLS":
                    validateBlock("COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS");
                    break;
                case "GREEK":
                    validateBlock("GREEK_AND_COPTIC");
                    break;
                case "CYRILLIC_SUPPLEMENTARY":
                    validateBlock("CYRILLIC_SUPPLEMENT");
                    break;
                default:
                    validateBlock(blkName);
                    break;
            }
        }
    }

    private static void validateBlock(String blkName) {
        for (Block block : blocks) {
            String blockName = block.getName();
            if (blockName.equals(blkName)) {
                return;
            }
        }
        err = true;
        System.err.println(blkName + " is not a valid Unicode Block.");
    }

    // List of all Unicode blocks, their start, and end codepoints.
    public static HashSet<Block> blocks = new HashSet<>();

    private static void generateBlockList() throws Exception {
        File blockData = new File(System.getProperty("test.src", "."),
                "Blocks.txt");
        try (BufferedReader f = new BufferedReader(new FileReader(blockData))) {
            String line;
            while ((line = f.readLine()) != null) {
                if (line.length() == 0 || line.charAt(0) == '#') {
                    continue;
                }

                int index1 = line.indexOf('.');
                int begin = Integer.parseInt(line.substring(0, index1), 16);
                int index2 = line.indexOf(';');
                int end = Integer.parseInt(line.substring(index1 + 2, index2), 16);
                String name = line.substring(index2 + 1).trim();

                System.out.println("  Adding a Block(" + Integer.toHexString(begin) + ", " + Integer.toHexString(end)
                        + ", " + name + ")");
                blocks.add(new Block(begin, end, name));
            }
        }
    }
}

class Block {

    public Block() {
        blockBegin = 0;
        blockEnd = 0;
        blockName = null;
    }

    public Block(int begin, int end, String name) {
        blockBegin = begin;
        blockEnd = end;
        blockName = name.replaceAll("[ -]", "_").toUpperCase(Locale.ENGLISH);
        originalBlockName = name;
    }

    public int getBegin() {
        return blockBegin;
    }

    public int getEnd() {
        return blockEnd;
    }

    public String getName() {
        return blockName;
    }

    public String getOriginalName() {
        return originalBlockName;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (!(obj instanceof Block)) return false;

        Block other = (Block)obj;
        return other.blockBegin == blockBegin &&
                other.blockEnd == blockEnd &&
                other.blockName.equals(blockName) &&
                other.originalBlockName.equals(originalBlockName);
    }
    int blockBegin, blockEnd;
    String blockName, originalBlockName;
}