< prev index next >

test/jdk/java/util/regex/RegExTest.java

Print this page
rev 56177 : [mq]: 8230365-Pattern-for-a-control-char-matches-non-control-characters

@@ -33,11 +33,11 @@
  * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
  * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
  * 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819
  * 8151481 4867170 7080302 6728861 6995635 6736245 4916384 6328855 6192895
  * 6345469 6988218 6693451 7006761 8140212 8143282 8158482 8176029 8184706
- * 8194667 8197462 8184692 8221431 8224789 8228352
+ * 8194667 8197462 8184692 8221431 8224789 8228352 8230365
  *
  * @library /test/lib
  * @library /lib/testlibrary/java/lang
  * @build jdk.test.lib.RandomFactory
  * @run main RegExTest

@@ -55,10 +55,11 @@
 import java.math.BigInteger;
 import java.nio.CharBuffer;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Random;
 import java.util.Scanner;
 import java.util.function.Function;
 import java.util.function.Predicate;

@@ -184,10 +185,11 @@
         grapheme();
         expoBacktracking();
         invalidGroupName();
         illegalRepetitionRange();
         surrogatePairWithCanonEq();
+        controlCharacters();
 
         if (failure) {
             throw new
                 RuntimeException("RegExTest failed, 1st failure: " +
                                  firstFailure);

@@ -4982,6 +4984,77 @@
             failCount++;
             System.out.println("Unexpected exception: " + t);
         }
         report("surrogatePairWithCanonEq");
     }
+
+    private static void controlCharacters() {
+        char[] contolCharsPairs = { '@', 0x00,
+            'A', 0x01, 'B', 0x02, 'C', 0x03, 'D', 0x04, 'E', 0x05, 'F', 0x06,
+            'G', 0x07, 'H', 0x08, 'I', 0x09, 'J', 0x0a, 'K', 0x0b, 'L', 0x0c,
+            'M', 0x0d, 'N', 0x0e, 'O', 0x0f, 'P', 0x10, 'Q', 0x11, 'R', 0x12,
+            'S', 0x13, 'T', 0x14, 'U', 0x15, 'V', 0x16, 'W', 0x17, 'X', 0x18,
+            'Y', 0x19, 'Z', 0x1a,
+            '[', 0x1b, '\\', 0x1c, ']', 0x1d, '^', 0x1e, '_', 0x1f, '?', 0x7f };
+        var contolChars = new HashMap<Character, Integer>();
+        for (int i = 0; i < contolCharsPairs.length; i += 2)
+            contolChars.put(Character.valueOf(contolCharsPairs[i]),
+                            Integer.valueOf(contolCharsPairs[i + 1]));
+
+        for (char chP = 0; chP <= 0xff + 16; ++chP) {
+            String pat = "\\c";
+            if (chP < 0xff) {
+                // \cx with ASCII x
+                pat = "\\c" + Character.toString(chP);
+            } else if (chP == 0xff) {
+                // incomplete \c at the end of pattern
+                pat = "\\c";
+            } else if (chP <= 0xff + 8) {
+                // \cx with a random non-ASCII char x
+                int x = 0xff + generator.nextInt(0xff00 + 1);
+                pat = "\\c" + Character.toString(x);
+            } else {
+                // \cx with a random non-ASCII codepoint x
+                int x = 0xff + generator.nextInt(Character.MAX_CODE_POINT + 1 - 0xff);
+                pat = "\\c" + Character.toString(x);
+            }
+            if (contolChars.containsKey(chP)) {
+                try {
+                    Pattern p = Pattern.compile(pat);
+                    for (int chS = 0; chS < 0xff; ++chS) {
+                        Matcher m = p.matcher(Character.toString(chS));
+                        if (m.matches() && contolChars.get(chP) != chS) {
+                            failCount++;
+                            System.out.println("Control character 0x" + Integer.toHexString(chS) +
+                                               " unexpectedly matched pattern " + pat);
+                        } else if (!m.matches() && contolChars.get(chP) == chS) {
+                            failCount++;
+                            System.out.println("Control character 0x" + Integer.toHexString(chS) +
+                                               " failed to match pattern " + pat);
+                        }
+                        if (m.matches() && Character.getType(chS) != Character.CONTROL) {
+                            failCount++;
+                            System.out.println("Non-control character 0x" + Integer.toHexString(chS) +
+                                               " unexpectedly matched pattern " + pat);
+                        }
+                    }
+                } catch (Throwable t) {
+                    failCount++;
+                    System.out.println("Failed to compile pattern " + pat +
+                                       " due to exception: " + t);
+                }
+            } else {
+                try {
+                    Pattern p = Pattern.compile(pat);
+                    failCount++;
+                    System.out.println("Expected to throw an exception when compiling " + pat);
+                } catch (PatternSyntaxException expected) {
+                } catch (Throwable t) {
+                    failCount++;
+                    System.out.println("Unexpected exception when compiling " + pat +
+                                       " : " + t);
+                }
+            }
+        }
+        report("controlCharacters");
+    }
 }
< prev index next >