1 /*
   2  * Copyright (c) 2019, 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  * @summary Test behavior of isJavaIdentifierXX, testIsJavaLetter, and
  27  *  testIsJavaLetterOrDigit methods for all code points.
  28  * @bug 8218915
  29  */
  30 
  31 import java.util.List;
  32 import java.util.ArrayList;
  33  
  34 public class TestIsJavaIdentifierMethods {
  35     
  36     //List of new code points are not present in Unicode 6.2.
  37     private static final List<Integer> newCodePoints = new ArrayList<Integer>()
  38                                        {{
  39                                          add(0x20BB); //NORDIC MARK SIGN
  40                                          add(0x20BC); //MANAT SIGN
  41                                          add(0x20BD); //RUBLE SIGN
  42                                          add(0x20BE); //LARI SIGN
  43                                          add(0x20BF); //BITCOIN SIGN 
  44                                          add(0x32FF); //SQUARE ERA NAME NEWERA
  45                                        }};
  46 
  47     public static void main(String[] args) {
  48         testIsJavaIdentifierPart_int();
  49         testIsJavaIdentifierPart_char();
  50         testIsJavaIdentifierStart_int();
  51         testIsJavaIdentifierStart_char();
  52         testIsJavaLetter();
  53         testIsJavaLetterOrDigit();
  54     }
  55 
  56     /**
  57      * Assertion testing for public static boolean isJavaIdentifierPart(int
  58      * codePoint), A character may be part of a Java identifier if any of the
  59      * following are true:
  60      * <ul>
  61      * <li>it is a letter</li>
  62      * <li>it is a currency symbol (such as <code>'$'</code>)</li>
  63      * <li>it is a connecting punctuation character (such as <code>'_'</code>)</li>
  64      * <li>it is a digit</li>
  65      * <li>it is a numeric letter (such as a Roman numeral character)</li>
  66      * <li>it is a combining mark</li>
  67      * <li>it is a non-spacing mark</li>
  68      * <li><code>isIdentifierIgnorable</code> returns <code>true</code> for the
  69      * character</li>
  70      * </ul>
  71      * All code points from (0x0000..0x10FFFF) are tested.
  72      */
  73     public static void testIsJavaIdentifierPart_int() {
  74         for (int cp = 0; cp <= Character.MAX_CODE_POINT; cp++) {
  75             boolean expected = false;
  76 
  77             //Since Character.isJavaIdentifierPart(int) strictly conforms to
  78             //character information from version 6.2 of the Unicode Standard,
  79             //check if code point is new code point. If the code point is new
  80             //code point, value of variable expected is considered false.
  81             if (!newCodePoints.contains(cp)) {
  82                 byte type = (byte) Character.getType(cp);
  83                 expected = Character.isLetter(cp)
  84                         || type == Character.CURRENCY_SYMBOL
  85                         || type == Character.CONNECTOR_PUNCTUATION
  86                         || Character.isDigit(cp)
  87                         || type == Character.LETTER_NUMBER
  88                         || type == Character.COMBINING_SPACING_MARK
  89                         || type == Character.NON_SPACING_MARK
  90                         || Character.isIdentifierIgnorable(cp);
  91             }
  92 
  93             if (Character.isJavaIdentifierPart(cp) != expected) {
  94                 throw new RuntimeException(
  95                         "Character.isJavaIdentifierPart(int) failed for codepoint "
  96                                 + Integer.toHexString(cp));
  97             }
  98         }
  99     }
 100 
 101     /**
 102      * Assertion testing for public static boolean isJavaIdentifierPart(char
 103      * ch), A character may be part of a Java identifier if any of the following
 104      * are true:
 105      * <ul>
 106      * <li>it is a letter;
 107      * <li>it is a currency symbol (such as "$");
 108      * <li>it is a connecting punctuation character (such as "_");
 109      * <li>it is a digit;
 110      * <li>it is a numeric letter (such as a Roman numeral character);
 111      * <li>it is a combining mark;
 112      * <li>it is a non-spacing mark;
 113      * <li>isIdentifierIgnorable returns true for the character.
 114      * </ul>
 115      * All Unicode chars (0x0000..0xFFFF) are tested.. <br>
 116      * <b>Expected results</b>: true if the character may be part of a Java
 117      * identifier; false otherwise
 118      */
 119     public static void testIsJavaIdentifierPart_char() {
 120         for (int i = 0; i <= Character.MAX_VALUE; ++i) {
 121             char ch = (char) i;
 122             boolean expected = false;
 123             //Since Character.isJavaIdentifierPart(char) strictly conforms to
 124             //character information from version 6.2 of the Unicode Standard,
 125             //check if code point is new code point. If the code point is new
 126             //code point, value of variable expected is considered false.
 127             if (!newCodePoints.contains(i)) {
 128                 byte type = (byte) Character.getType(ch);
 129                 expected = Character.isLetter(ch)
 130                         || type == Character.CURRENCY_SYMBOL
 131                         || type == Character.CONNECTOR_PUNCTUATION
 132                         || Character.isDigit(ch)
 133                         || type == Character.LETTER_NUMBER
 134                         || type == Character.COMBINING_SPACING_MARK
 135                         || type == Character.NON_SPACING_MARK
 136                         || Character.isIdentifierIgnorable(ch);
 137             }
 138 
 139             if (Character.isJavaIdentifierPart((char) i) != expected) {
 140                 throw new RuntimeException(
 141                         "Character.isJavaIdentifierPart(char) failed for codepoint "
 142                                 + Integer.toHexString(i));
 143             }
 144         }
 145     }
 146 
 147     /**
 148      * Assertion testing for public static boolean isJavaIdentifierStart(int
 149      * codePoint), A character may start a Java identifier if and only if it is
 150      * one of the following:
 151      * <ul>
 152      * <li>it is a letter;</li>
 153      * <li>it is a currency symbol (such as "$");</li>
 154      * <li>it is a connecting punctuation character (such as "_");</li>
 155      * </ul>
 156      * All Code points from (0x0000..0x10FFFF) are tested..
 157      */
 158     public static void testIsJavaIdentifierStart_int() {
 159         for (int cp = 0; cp <= Character.MAX_CODE_POINT; cp++) {
 160             boolean expected = false;
 161             //Since Character.isJavaIdentifierStart(int) strictly conforms to
 162             //character information from version 6.2 of the Unicode Standard,
 163             //check if code point is new code point. If the code point is new
 164             //code point, value of variable expected is considered false.
 165             if (!newCodePoints.contains(cp)) {
 166                 byte type = (byte) Character.getType(cp);
 167                 expected = Character.isLetter(cp)
 168                         || type == Character.LETTER_NUMBER
 169                         || type == Character.CURRENCY_SYMBOL
 170                         || type == Character.CONNECTOR_PUNCTUATION;
 171             }
 172 
 173             if (Character.isJavaIdentifierStart(cp) != expected) {
 174                 throw new RuntimeException(
 175                         "Character.isLetter(int) failed for codepoint "
 176                                 + Integer.toHexString(cp));
 177             }
 178         }
 179     }
 180 
 181     /**
 182      * Assertion testing for public static boolean isJavaIdentifierStart(char),
 183      * A character may start a Java identifier if and only if it is
 184      * one of the following:
 185      * <ul>
 186      * <li>it is a letter;</li>
 187      * <li>it is a currency symbol (such as "$");</li>
 188      * <li>it is a connecting punctuation character (such as "_");</li>
 189      * </ul>
 190      * All Unicode chars (0x0000..0xFFFF) are tested..
 191      */
 192     public static void testIsJavaIdentifierStart_char() {
 193         for (int i = 0; i <= Character.MAX_VALUE; i++) {
 194             char ch = (char) i;
 195             boolean expected = false;
 196             //Since Character.isJavaIdentifierStart(char) strictly conforms to
 197             //character information from version 6.2 of the Unicode Standard,
 198             //check if code point is new code point. If the code point is new
 199             //code point, value of variable expected is considered false.
 200             if (!newCodePoints.contains(i)) {
 201                 byte type = (byte) Character.getType(ch);
 202                 expected = Character.isLetter(ch)
 203                         || type == Character.LETTER_NUMBER
 204                         || type == Character.CURRENCY_SYMBOL
 205                         || type == Character.CONNECTOR_PUNCTUATION;
 206             }
 207             
 208             if (Character.isJavaIdentifierStart(ch) != expected) {
 209                 throw new RuntimeException(
 210                         "Character.isLetter(char) failed for codepoint "
 211                                 + Integer.toHexString(i));
 212             }
 213         }
 214     }
 215     
 216     /**
 217      * Assertion testing for public static boolean isJavaLetter(char ch), A
 218      * character may start a Java identifier if and only if one of the following
 219      * is true:
 220      * <ul>
 221      * <li>isLetter(ch) returns true
 222      * <li>getType(ch) returns LETTER_NUMBER
 223      * <li>ch is a currency symbol (such as "$")
 224      * <li>ch is a connecting punctuation character (such as "_").
 225      * </ul>
 226      * All Unicode chars (0x0000..0xFFFF) are tested.. <br>
 227      */
 228     public static void testIsJavaLetter() {
 229         for (int i = 0; i <= Character.MAX_VALUE; ++i) {
 230             char ch = (char) i;
 231             boolean expected = false;
 232             //Since Character.isJavaLetter(char) strictly conforms to
 233             //character information from version 6.2 of the Unicode Standard,
 234             //check if code point is new code point. If the code point is new
 235             //code point, value of variable expected is considered false.
 236             if (!newCodePoints.contains(i)) {
 237                 byte type = (byte) Character.getType(ch);
 238                 expected = Character.isLetter(ch)
 239                         || type == Character.LETTER_NUMBER
 240                         || type == Character.CURRENCY_SYMBOL
 241                         || type == Character.CONNECTOR_PUNCTUATION;
 242             }
 243 
 244             if (Character.isJavaLetter(ch) != expected) {
 245                 throw new RuntimeException(
 246                         "Character.isJavaLetter(ch) failed for codepoint "
 247                                 + Integer.toHexString(i));
 248             }
 249         }
 250     }
 251 
 252     /**
 253      * Assertion testing for public static boolean isJavaLetterOrDigit(char ch),
 254      * A character may be part of a Java identifier if and only if any of the
 255      * following are true:
 256      * <ul>
 257      * <li>it is a letter
 258      * <li>it is a currency symbol (such as '$')
 259      * <li>it is a connecting punctuation character (such as '_')
 260      * <li>it is a digit
 261      * <li>it is a numeric letter (such as a Roman numeral character)
 262      * <li>it is a combining mark
 263      * <li>it is a non-spacing mark
 264      * <li>isIdentifierIgnorable returns true for the character.
 265      * </ul>
 266      * All Unicode chars (0x0000..0xFFFF) are tested.. <br>
 267      */
 268     public static void testIsJavaLetterOrDigit() {
 269         for (int i = 0; i <= Character.MAX_VALUE; ++i) {
 270             char ch = (char) i;
 271             boolean expected = false;
 272             //Since Character.isIdentifierIgnorable(char) strictly conforms to
 273             //character information from version 6.2 of the Unicode Standard,
 274             //check if code point is new code point. If the code point is new
 275             //code point, value of variable expected is considered false.
 276             if (!newCodePoints.contains(i)) {
 277                 byte type = (byte) Character.getType(ch);
 278                 expected = Character.isLetter(ch)
 279                         || type == Character.CURRENCY_SYMBOL
 280                         || type == Character.CONNECTOR_PUNCTUATION
 281                         || Character.isDigit(ch)
 282                         || type == Character.LETTER_NUMBER
 283                         || type == Character.COMBINING_SPACING_MARK
 284                         || type == Character.NON_SPACING_MARK
 285                         || Character.isIdentifierIgnorable(ch);
 286             }
 287 
 288             if (Character.isJavaLetterOrDigit(ch) != expected) {
 289                 throw new RuntimeException(
 290                         "Character.isJavaLetterOrDigit(ch) failed for codepoint "
 291                                 + Integer.toHexString(i));
 292             }
 293         }
 294     }
 295 }