< prev index next >

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

Print this page
rev 57941 : [mq]: 8235812-Unicode-linebreak-with-quantifier-does-not-match-valid-input


  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 tests RegExp framework (use -Dseed=X to set PRNG seed)
  27  * @author Mike McCloskey
  28  * @bug 4481568 4482696 4495089 4504687 4527731 4599621 4631553 4619345
  29  * 4630911 4672616 4711773 4727935 4750573 4792284 4803197 4757029 4808962
  30  * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
  31  * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
  32  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
  33  * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
  34  * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
  35  * 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819
  36  * 8151481 4867170 7080302 6728861 6995635 6736245 4916384 6328855 6192895
  37  * 6345469 6988218 6693451 7006761 8140212 8143282 8158482 8176029 8184706
  38  * 8194667 8197462 8184692 8221431 8224789 8228352 8230829 8236034
  39  *
  40  * @library /test/lib
  41  * @library /lib/testlibrary/java/lang
  42  * @build jdk.test.lib.RandomFactory
  43  * @run main RegExTest
  44  * @key randomness
  45  */
  46 
  47 import java.io.BufferedReader;
  48 import java.io.ByteArrayInputStream;
  49 import java.io.ByteArrayOutputStream;
  50 import java.io.File;
  51 import java.io.FileInputStream;
  52 import java.io.InputStreamReader;
  53 import java.io.ObjectInputStream;
  54 import java.io.ObjectOutputStream;
  55 import java.math.BigInteger;
  56 import java.nio.CharBuffer;
  57 import java.nio.file.Files;
  58 import java.util.ArrayList;
  59 import java.util.Arrays;

  60 import java.util.List;

  61 import java.util.Random;
  62 import java.util.Scanner;
  63 import java.util.function.Function;
  64 import java.util.function.Predicate;
  65 import java.util.regex.Matcher;
  66 import java.util.regex.MatchResult;
  67 import java.util.regex.Pattern;
  68 import java.util.regex.PatternSyntaxException;
  69 import jdk.test.lib.RandomFactory;
  70 
  71 /**
  72  * This is a test class created to check the operation of
  73  * the Pattern and Matcher classes.
  74  */
  75 public class RegExTest {
  76 
  77     private static Random generator = RandomFactory.getRandom();
  78     private static boolean failure = false;
  79     private static int failCount = 0;
  80     private static String firstFailure = null;


 169         namedGroupCaptureTest();
 170         nonBmpClassComplementTest();
 171         unicodePropertiesTest();
 172         unicodeHexNotationTest();
 173         unicodeClassesTest();
 174         unicodeCharacterNameTest();
 175         horizontalAndVerticalWSTest();
 176         linebreakTest();
 177         branchTest();
 178         groupCurlyNotFoundSuppTest();
 179         groupCurlyBackoffTest();
 180         patternAsPredicate();
 181         patternAsMatchPredicate();
 182         invalidFlags();
 183         embeddedFlags();
 184         grapheme();
 185         expoBacktracking();
 186         invalidGroupName();
 187         illegalRepetitionRange();
 188         surrogatePairWithCanonEq();

 189 
 190         if (failure) {
 191             throw new
 192                 RuntimeException("RegExTest failed, 1st failure: " +
 193                                  firstFailure);
 194         } else {
 195             System.err.println("OKAY: All tests passed.");
 196         }
 197     }
 198 
 199     // Utility functions
 200 
 201     private static String getRandomAlphaString(int length) {
 202         StringBuffer buf = new StringBuffer(length);
 203         for (int i=0; i<length; i++) {
 204             char randChar = (char)(97 + generator.nextInt(26));
 205             buf.append(randChar);
 206         }
 207         return buf.toString();
 208     }


4982                 if (!e.getMessage().startsWith("Illegal repetition")) {
4983                     failCount++;
4984                     System.out.println("Unexpected error message: " + e.getMessage());
4985                 }
4986             } catch (Throwable t) {
4987                 failCount++;
4988                 System.out.println("Unexpected exception: " + t);
4989             }
4990         }
4991         report("illegalRepetitionRange");
4992     }
4993 
4994     private static void surrogatePairWithCanonEq() {
4995         try {
4996             Pattern.compile("\ud834\udd21", Pattern.CANON_EQ);
4997         } catch (Throwable t) {
4998             failCount++;
4999             System.out.println("Unexpected exception: " + t);
5000         }
5001         report("surrogatePairWithCanonEq");













































































5002     }
5003 }


  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 tests RegExp framework (use -Dseed=X to set PRNG seed)
  27  * @author Mike McCloskey
  28  * @bug 4481568 4482696 4495089 4504687 4527731 4599621 4631553 4619345
  29  * 4630911 4672616 4711773 4727935 4750573 4792284 4803197 4757029 4808962
  30  * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
  31  * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
  32  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
  33  * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
  34  * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
  35  * 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819
  36  * 8151481 4867170 7080302 6728861 6995635 6736245 4916384 6328855 6192895
  37  * 6345469 6988218 6693451 7006761 8140212 8143282 8158482 8176029 8184706
  38  * 8194667 8197462 8184692 8221431 8224789 8228352 8230829 8236034 8235812
  39  *
  40  * @library /test/lib
  41  * @library /lib/testlibrary/java/lang
  42  * @build jdk.test.lib.RandomFactory
  43  * @run main RegExTest
  44  * @key randomness
  45  */
  46 
  47 import java.io.BufferedReader;
  48 import java.io.ByteArrayInputStream;
  49 import java.io.ByteArrayOutputStream;
  50 import java.io.File;
  51 import java.io.FileInputStream;
  52 import java.io.InputStreamReader;
  53 import java.io.ObjectInputStream;
  54 import java.io.ObjectOutputStream;
  55 import java.math.BigInteger;
  56 import java.nio.CharBuffer;
  57 import java.nio.file.Files;
  58 import java.util.ArrayList;
  59 import java.util.Arrays;
  60 import java.util.HashMap;
  61 import java.util.List;
  62 import java.util.Map;
  63 import java.util.Random;
  64 import java.util.Scanner;
  65 import java.util.function.Function;
  66 import java.util.function.Predicate;
  67 import java.util.regex.Matcher;
  68 import java.util.regex.MatchResult;
  69 import java.util.regex.Pattern;
  70 import java.util.regex.PatternSyntaxException;
  71 import jdk.test.lib.RandomFactory;
  72 
  73 /**
  74  * This is a test class created to check the operation of
  75  * the Pattern and Matcher classes.
  76  */
  77 public class RegExTest {
  78 
  79     private static Random generator = RandomFactory.getRandom();
  80     private static boolean failure = false;
  81     private static int failCount = 0;
  82     private static String firstFailure = null;


 171         namedGroupCaptureTest();
 172         nonBmpClassComplementTest();
 173         unicodePropertiesTest();
 174         unicodeHexNotationTest();
 175         unicodeClassesTest();
 176         unicodeCharacterNameTest();
 177         horizontalAndVerticalWSTest();
 178         linebreakTest();
 179         branchTest();
 180         groupCurlyNotFoundSuppTest();
 181         groupCurlyBackoffTest();
 182         patternAsPredicate();
 183         patternAsMatchPredicate();
 184         invalidFlags();
 185         embeddedFlags();
 186         grapheme();
 187         expoBacktracking();
 188         invalidGroupName();
 189         illegalRepetitionRange();
 190         surrogatePairWithCanonEq();
 191         lineBreakWithQuantifier();
 192 
 193         if (failure) {
 194             throw new
 195                 RuntimeException("RegExTest failed, 1st failure: " +
 196                                  firstFailure);
 197         } else {
 198             System.err.println("OKAY: All tests passed.");
 199         }
 200     }
 201 
 202     // Utility functions
 203 
 204     private static String getRandomAlphaString(int length) {
 205         StringBuffer buf = new StringBuffer(length);
 206         for (int i=0; i<length; i++) {
 207             char randChar = (char)(97 + generator.nextInt(26));
 208             buf.append(randChar);
 209         }
 210         return buf.toString();
 211     }


4985                 if (!e.getMessage().startsWith("Illegal repetition")) {
4986                     failCount++;
4987                     System.out.println("Unexpected error message: " + e.getMessage());
4988                 }
4989             } catch (Throwable t) {
4990                 failCount++;
4991                 System.out.println("Unexpected exception: " + t);
4992             }
4993         }
4994         report("illegalRepetitionRange");
4995     }
4996 
4997     private static void surrogatePairWithCanonEq() {
4998         try {
4999             Pattern.compile("\ud834\udd21", Pattern.CANON_EQ);
5000         } catch (Throwable t) {
5001             failCount++;
5002             System.out.println("Unexpected exception: " + t);
5003         }
5004         report("surrogatePairWithCanonEq");
5005     }
5006 
5007     // This test is for 8235812
5008     private static void lineBreakWithQuantifier() {
5009         // key:    pattern
5010         // value:  lengths of input that must match the pattern
5011         Map<String, List<Integer>> cases = Map.ofEntries(
5012             Map.entry("\\R?",      List.of(0, 1)),
5013             Map.entry("\\R*",      List.of(0, 1, 2, 3)),
5014             Map.entry("\\R+",      List.of(1, 2, 3)),
5015             Map.entry("\\R{0}",    List.of(0)),
5016             Map.entry("\\R{1}",    List.of(1)),
5017             Map.entry("\\R{2}",    List.of(2)),
5018             Map.entry("\\R{3}",    List.of(3)),
5019             Map.entry("\\R{0,}",   List.of(0, 1, 2, 3)),
5020             Map.entry("\\R{1,}",   List.of(1, 2, 3)),
5021             Map.entry("\\R{2,}",   List.of(2, 3)),
5022             Map.entry("\\R{3,}",   List.of(3)),
5023             Map.entry("\\R{0,0}",  List.of(0)),
5024             Map.entry("\\R{0,1}",  List.of(0, 1)),
5025             Map.entry("\\R{0,2}",  List.of(0, 1, 2)),
5026             Map.entry("\\R{0,3}",  List.of(0, 1, 2, 3)),
5027             Map.entry("\\R{1,1}",  List.of(1)),
5028             Map.entry("\\R{1,2}",  List.of(1, 2)),
5029             Map.entry("\\R{1,3}",  List.of(1, 2, 3)),
5030             Map.entry("\\R{2,2}",  List.of(2)),
5031             Map.entry("\\R{2,3}",  List.of(2, 3)),
5032             Map.entry("\\R{3,3}",  List.of(3)),
5033             Map.entry("\\R",       List.of(1)),
5034             Map.entry("\\R\\R",    List.of(2)),
5035             Map.entry("\\R\\R\\R", List.of(3))
5036         );
5037 
5038         // key:    length of input
5039         // value:  all possible inputs of given length
5040         Map<Integer, List<String>> inputs = new HashMap<>();
5041         String[] Rs = { "\r\n", "\r", "\n",
5042                         "\u000B", "\u000C", "\u0085", "\u2028", "\u2029" };
5043         StringBuilder sb = new StringBuilder();
5044         for (int len = 0; len <= 3; ++len) {
5045             int[] idx = new int[len + 1];
5046             do {
5047                 sb.setLength(0);
5048                 for (int j = 0; j < len; ++j)
5049                     sb.append(Rs[idx[j]]);
5050                 inputs.computeIfAbsent(len, ArrayList::new).add(sb.toString());
5051                 idx[0]++;
5052                 for (int j = 0; j < len; ++j) {
5053                     if (idx[j] < Rs.length)
5054                         break;
5055                     idx[j] = 0;
5056                     idx[j+1]++;
5057                 }
5058             } while (idx[len] == 0);
5059         }
5060 
5061         // exhaustive testing
5062         for (String patStr : cases.keySet()) {
5063             Pattern[] pats = patStr.endsWith("R")
5064                 ? new Pattern[] { Pattern.compile(patStr) }  // no quantifiers
5065                 : new Pattern[] { Pattern.compile(patStr),          // greedy
5066                                   Pattern.compile(patStr + "?") };  // reluctant
5067             Matcher m = pats[0].matcher("");
5068             for (Pattern p : pats) {
5069                 m.usePattern(p);
5070                 for (int len : cases.get(patStr)) {
5071                     for (String in : inputs.get(len)) {
5072                         if (!m.reset(in).matches()) {
5073                             failCount++;
5074                             System.out.println("Expected to match '" +
5075                                     in + "' =~ /" + p + "/");
5076                         }
5077                     }
5078                 }
5079             }
5080         }
5081         report("lineBreakWithQuantifier");
5082     }
5083 }
< prev index next >