Path: blob/master/test/jdk/java/lang/Character/CharPropTest.java
41149 views
/*1* Copyright (c) 2018, 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 8202771 8221431 822983126* @summary Check j.l.Character.isDigit/isLetter/isLetterOrDigit/isSpaceChar27* /isWhitespace/isTitleCase/isISOControl/isIdentifierIgnorable28* /isJavaIdentifierStart/isJavaIdentifierPart/isUnicodeIdentifierStart29* /isUnicodeIdentifierPart30* @library /lib/testlibrary/java/lang31* @run main CharPropTest32*/3334import java.nio.file.Files;35import java.util.stream.Stream;3637public class CharPropTest {38private static int diffs = 0;39private static int rangeStart = 0x0000;40private static boolean isRange = false;4142public static void main(String[] args) throws Exception {43try (Stream<String> lines = Files.lines(UCDFiles.UNICODE_DATA)) {44lines.map(String::trim)45.filter(line -> line.length() != 0 && line.charAt(0) != '#')46.forEach(line -> handleOneLine(line));4748if (diffs != 0) {49throw new RuntimeException("Total differences: " + diffs);50}51}52}5354private static void handleOneLine(String line) {55String[] fields = line.split(";");56int currentCp = Integer.parseInt(fields[0], 16);57String name = fields[1];58String category = fields[2];5960// Except single code point, also handle ranges like the following:61// 3400;<CJK Ideograph Extension A, First>;Lo;0;L;;;;;N;;;;;62// 4DB5;<CJK Ideograph Extension A, Last>;Lo;0;L;;;;;N;;;;63if (isRange) {64if (name.endsWith("Last>")) {65for (int cp = rangeStart; cp <= currentCp; cp++) {66testCodePoint(cp, category);67}68} else {69throw new RuntimeException("Not a valid range, first range <"70+ Integer.toHexString(rangeStart) + "> without last.");71}72isRange = false;73} else {74if (name.endsWith("First>")) {75rangeStart = currentCp;76isRange = true;77} else {78testCodePoint(currentCp, category);79}80}81}8283private static void testCodePoint(int codePoint, String category) {84isDigitTest(codePoint, category);85isLetterTest(codePoint, category);86isLetterOrDigitTest(codePoint, category);8788isSpaceCharTest(codePoint, category);89isWhitespaceTest(codePoint, category);9091isTitleCaseTest(codePoint, category);9293isISOControlTest(codePoint);9495isIdentifierIgnorableTest(codePoint, category);96isJavaIdentifierStartTest(codePoint, category);97isJavaIdentifierPartTest(codePoint, category);98isUnicodeIdentifierStartTest(codePoint, category);99isUnicodeIdentifierPartTest(codePoint, category);100}101102private static void isDigitTest(int codePoint, String category) {103boolean actual = Character.isDigit(codePoint);104boolean expected = category.equals("Nd");105if (actual != expected) {106printDiff(codePoint, "isDigit", actual, expected);107}108}109110private static void isLetterTest(int codePoint, String category) {111boolean actual = Character.isLetter(codePoint);112boolean expected = isLetter(category);113if (actual != expected) {114printDiff(codePoint, "isLetter", actual, expected);115}116}117118private static void isLetterOrDigitTest(int codePoint, String category) {119boolean actual = Character.isLetterOrDigit(codePoint);120boolean expected = isLetter(category) || category.equals("Nd");121if (actual != expected) {122printDiff(codePoint, "isLetterOrDigit", actual, expected);123}124}125126private static void isSpaceCharTest(int codePoint, String category) {127boolean actual = Character.isSpaceChar(codePoint);128boolean expected = isSpaceChar(category);129if (actual != expected) {130printDiff(codePoint, "isSpaceChar", actual, expected);131}132}133134private static void isWhitespaceTest(int codePoint, String category) {135boolean actual = Character.isWhitespace(codePoint);136boolean expected = isWhitespace(codePoint, category);137if (actual != expected) {138printDiff(codePoint, "isWhitespace", actual, expected);139}140}141142private static void isTitleCaseTest(int codePoint, String category) {143boolean actual = Character.isTitleCase(codePoint);144boolean expected = category.equals("Lt");145if (actual != expected) {146printDiff(codePoint, "isTitleCase", actual, expected);147}148}149150private static void isISOControlTest(int codePoint) {151boolean actual = Character.isISOControl(codePoint);152boolean expected = isISOControl(codePoint);153if (actual != expected) {154printDiff(codePoint, "isISOControl", actual, expected);155}156}157158private static void isIdentifierIgnorableTest(int codePoint, String category) {159boolean actual = Character.isIdentifierIgnorable(codePoint);160boolean expected = isIdentifierIgnorable(codePoint, category);161if (actual != expected) {162printDiff(codePoint, "isIdentifierIgnorable", actual, expected);163}164}165166private static void isJavaIdentifierStartTest(int codePoint, String category) {167boolean actual = Character.isJavaIdentifierStart(codePoint);168boolean expected = isJavaIdentifierStart(category);169if (actual != expected) {170printDiff(codePoint, "isJavaIdentifierStart", actual, expected);171}172}173174private static void isJavaIdentifierPartTest(int codePoint, String category) {175boolean actual = Character.isJavaIdentifierPart(codePoint);176boolean expected = isJavaIdentifierPart(codePoint, category);177if (actual != expected) {178printDiff(codePoint, "isJavaIdentifierPart", actual, expected);179}180}181182private static void isUnicodeIdentifierStartTest(int codePoint, String category) {183boolean actual = Character.isUnicodeIdentifierStart(codePoint);184boolean expected = isUnicodeIdentifierStart(codePoint, category);185if (actual != expected) {186printDiff(codePoint, "isUnicodeIdentifierStart", actual, expected);187}188}189190private static void isUnicodeIdentifierPartTest(int codePoint, String category) {191boolean actual = Character.isUnicodeIdentifierPart(codePoint);192boolean expected = isUnicodeIdentifierPart(codePoint, category);193if (actual != expected) {194printDiff(codePoint, "isUnicodeIdentifierPart", actual, expected);195}196}197198private static boolean isLetter(String category) {199return category.equals("Lu") || category.equals("Ll")200|| category.equals("Lt") || category.equals("Lm")201|| category.equals("Lo");202}203204private static boolean isSpaceChar(String category) {205return category.equals("Zs") || category.equals("Zl")206|| category.equals("Zp");207}208209private static boolean isWhitespace(int codePoint, String category) {210if (isSpaceChar(category) && codePoint != Integer.parseInt("00A0", 16)211&& codePoint != Integer.parseInt("2007", 16)212&& codePoint != Integer.parseInt("202F", 16)) {213return true;214} else {215if (codePoint == Integer.parseInt("0009", 16)216|| codePoint == Integer.parseInt("000A", 16)217|| codePoint == Integer.parseInt("000B", 16)218|| codePoint == Integer.parseInt("000C", 16)219|| codePoint == Integer.parseInt("000D", 16)220|| codePoint == Integer.parseInt("001C", 16)221|| codePoint == Integer.parseInt("001D", 16)222|| codePoint == Integer.parseInt("001E", 16)223|| codePoint == Integer.parseInt("001F", 16)) {224return true;225}226}227return false;228}229230private static boolean isISOControl(int codePoint) {231return (codePoint > 0x00 && codePoint < 0x1f)232|| (codePoint > 0x7f && codePoint < 0x9f)233|| (codePoint == 0x00 || codePoint == 0x1f || codePoint == 0x7f || codePoint == 0x9f);234}235236private static boolean isIdentifierIgnorable(int codePoint, String category) {237if (category.equals("Cf")) {238return true;239} else {240int a1 = Integer.parseInt("0000", 16);241int a2 = Integer.parseInt("0008", 16);242int b1 = Integer.parseInt("000E", 16);243int b2 = Integer.parseInt("001B", 16);244int c1 = Integer.parseInt("007F", 16);245int c2 = Integer.parseInt("009F", 16);246247if ((codePoint > a1 && codePoint < a2) || (codePoint > b1 && codePoint < b2)248|| (codePoint > c1 && codePoint < c2) || (codePoint == a1 || codePoint == a2249|| codePoint == b1 || codePoint == b2 || codePoint == c1 || codePoint == c2)) {250return true;251}252}253return false;254}255256private static boolean isJavaIdentifierStart(String category) {257return isLetter(category) || category.equals("Nl") || category.equals("Sc")258|| category.equals("Pc");259}260261private static boolean isJavaIdentifierPart(int codePoint, String category) {262return isLetter(category) || category.equals("Sc") || category.equals("Pc")263|| category.equals("Nd") || category.equals("Nl")264|| category.equals("Mc") || category.equals("Mn")265|| isIdentifierIgnorable(codePoint, category);266}267268private static boolean isUnicodeIdentifierStart(int codePoint, String category) {269return isLetter(category) || category.equals("Nl")270|| isOtherIDStart(codePoint);271}272273private static boolean isUnicodeIdentifierPart(int codePoint, String category) {274return isLetter(category) || category.equals("Pc") || category.equals("Nd")275|| category.equals("Nl") || category.equals("Mc") || category.equals("Mn")276|| isIdentifierIgnorable(codePoint, category)277|| isOtherIDStart(codePoint)278|| isOtherIDContinue(codePoint);279}280281private static boolean isOtherIDStart(int codePoint) {282return codePoint == 0x1885 ||283codePoint == 0x1886 ||284codePoint == 0x2118 ||285codePoint == 0x212E ||286codePoint == 0x309B ||287codePoint == 0x309C;288}289290private static boolean isOtherIDContinue(int codePoint) {291return codePoint == 0x00B7 ||292codePoint == 0x0387 ||293(codePoint >= 0x1369 && codePoint <= 0x1371) ||294codePoint == 0x19DA;295}296297private static void printDiff(int codePoint, String method, boolean actual, boolean expected) {298System.out.println("Not equal at codePoint <" + Integer.toHexString(codePoint)299+ ">, method: " + method300+ ", actual: " + actual + ", expected: " + expected);301diffs++;302}303}304305306