jdk/test/java/util/regex/RegExTest.java
changeset 23734 439905b27f94
parent 22997 7d2259be8e79
child 25523 c751d1010164
equal deleted inserted replaced
23733:b9b80421cfa7 23734:439905b27f94
    30  * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
    30  * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
    31  * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
    31  * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
    32  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
    32  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
    33  * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
    33  * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
    34  * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
    34  * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
    35  * 8027645 8035076
    35  * 8027645 8035076 8039124
    36  */
    36  */
    37 
    37 
    38 import java.util.regex.*;
    38 import java.util.regex.*;
    39 import java.util.Random;
    39 import java.util.Random;
    40 import java.io.*;
    40 import java.io.*;
    73         blankInput();
    73         blankInput();
    74 
    74 
    75         // Substitition tests on randomly generated sequences
    75         // Substitition tests on randomly generated sequences
    76         globalSubstitute();
    76         globalSubstitute();
    77         stringbufferSubstitute();
    77         stringbufferSubstitute();
       
    78         stringbuilderSubstitute();
       
    79 
    78         substitutionBasher();
    80         substitutionBasher();
       
    81         substitutionBasher2();
    79 
    82 
    80         // Canonical Equivalence
    83         // Canonical Equivalence
    81         ceTest();
    84         ceTest();
    82 
    85 
    83         // Anchors
    86         // Anchors
   294         check(new Runnable() { public void run() { Pattern.compile("xyz").split(null);}});
   297         check(new Runnable() { public void run() { Pattern.compile("xyz").split(null);}});
   295         check(new Runnable() { public void run() { Pattern.compile("xyz").matcher(null);}});
   298         check(new Runnable() { public void run() { Pattern.compile("xyz").matcher(null);}});
   296 
   299 
   297         final Matcher m = Pattern.compile("xyz").matcher("xyz");
   300         final Matcher m = Pattern.compile("xyz").matcher("xyz");
   298         m.matches();
   301         m.matches();
   299         check(new Runnable() { public void run() { m.appendTail(null);}});
   302         check(new Runnable() { public void run() { m.appendTail((StringBuffer)null);}});
       
   303         check(new Runnable() { public void run() { m.appendTail((StringBuilder)null);}});
   300         check(new Runnable() { public void run() { m.replaceAll(null);}});
   304         check(new Runnable() { public void run() { m.replaceAll(null);}});
   301         check(new Runnable() { public void run() { m.replaceFirst(null);}});
   305         check(new Runnable() { public void run() { m.replaceFirst(null);}});
   302         check(new Runnable() { public void run() { m.appendReplacement(null, null);}});
   306         check(new Runnable() { public void run() { m.appendReplacement((StringBuffer)null, null);}});
       
   307         check(new Runnable() { public void run() { m.appendReplacement((StringBuilder)null, null);}});
   303         check(new Runnable() { public void run() { m.reset(null);}});
   308         check(new Runnable() { public void run() { m.reset(null);}});
   304         check(new Runnable() { public void run() { Matcher.quoteReplacement(null);}});
   309         check(new Runnable() { public void run() { Matcher.quoteReplacement(null);}});
   305         //check(new Runnable() { public void run() { m.usePattern(null);}});
   310         //check(new Runnable() { public void run() { m.usePattern(null);}});
   306 
   311 
   307         report("Null Argument");
   312         report("Null Argument");
  2971         }
  2976         }
  2972 
  2977 
  2973         report("SB Substitution");
  2978         report("SB Substitution");
  2974     }
  2979     }
  2975 
  2980 
       
  2981     /**
       
  2982      * Tests the usage of Matcher.appendReplacement() with literal
       
  2983      * and group substitutions.
       
  2984      */
       
  2985     private static void stringbuilderSubstitute() throws Exception {
       
  2986         // SB substitution with literal
       
  2987         String blah = "zzzblahzzz";
       
  2988         Pattern p = Pattern.compile("blah");
       
  2989         Matcher m = p.matcher(blah);
       
  2990         StringBuilder result = new StringBuilder();
       
  2991         try {
       
  2992             m.appendReplacement(result, "blech");
       
  2993             failCount++;
       
  2994         } catch (IllegalStateException e) {
       
  2995         }
       
  2996         m.find();
       
  2997         m.appendReplacement(result, "blech");
       
  2998         if (!result.toString().equals("zzzblech"))
       
  2999             failCount++;
       
  3000 
       
  3001         m.appendTail(result);
       
  3002         if (!result.toString().equals("zzzblechzzz"))
       
  3003             failCount++;
       
  3004 
       
  3005         // SB substitution with groups
       
  3006         blah = "zzzabcdzzz";
       
  3007         p = Pattern.compile("(ab)(cd)*");
       
  3008         m = p.matcher(blah);
       
  3009         result = new StringBuilder();
       
  3010         try {
       
  3011             m.appendReplacement(result, "$1");
       
  3012             failCount++;
       
  3013         } catch (IllegalStateException e) {
       
  3014         }
       
  3015         m.find();
       
  3016         m.appendReplacement(result, "$1");
       
  3017         if (!result.toString().equals("zzzab"))
       
  3018             failCount++;
       
  3019 
       
  3020         m.appendTail(result);
       
  3021         if (!result.toString().equals("zzzabzzz"))
       
  3022             failCount++;
       
  3023 
       
  3024         // SB substitution with 3 groups
       
  3025         blah = "zzzabcdcdefzzz";
       
  3026         p = Pattern.compile("(ab)(cd)*(ef)");
       
  3027         m = p.matcher(blah);
       
  3028         result = new StringBuilder();
       
  3029         try {
       
  3030             m.appendReplacement(result, "$1w$2w$3");
       
  3031             failCount++;
       
  3032         } catch (IllegalStateException e) {
       
  3033         }
       
  3034         m.find();
       
  3035         m.appendReplacement(result, "$1w$2w$3");
       
  3036         if (!result.toString().equals("zzzabwcdwef"))
       
  3037             failCount++;
       
  3038 
       
  3039         m.appendTail(result);
       
  3040         if (!result.toString().equals("zzzabwcdwefzzz"))
       
  3041             failCount++;
       
  3042 
       
  3043         // SB substitution with groups and three matches
       
  3044         // skipping middle match
       
  3045         blah = "zzzabcdzzzabcddzzzabcdzzz";
       
  3046         p = Pattern.compile("(ab)(cd*)");
       
  3047         m = p.matcher(blah);
       
  3048         result = new StringBuilder();
       
  3049         try {
       
  3050             m.appendReplacement(result, "$1");
       
  3051             failCount++;
       
  3052         } catch (IllegalStateException e) {
       
  3053         }
       
  3054         m.find();
       
  3055         m.appendReplacement(result, "$1");
       
  3056         if (!result.toString().equals("zzzab"))
       
  3057             failCount++;
       
  3058 
       
  3059         m.find();
       
  3060         m.find();
       
  3061         m.appendReplacement(result, "$2");
       
  3062         if (!result.toString().equals("zzzabzzzabcddzzzcd"))
       
  3063             failCount++;
       
  3064 
       
  3065         m.appendTail(result);
       
  3066         if (!result.toString().equals("zzzabzzzabcddzzzcdzzz"))
       
  3067             failCount++;
       
  3068 
       
  3069         // Check to make sure escaped $ is ignored
       
  3070         blah = "zzzabcdcdefzzz";
       
  3071         p = Pattern.compile("(ab)(cd)*(ef)");
       
  3072         m = p.matcher(blah);
       
  3073         result = new StringBuilder();
       
  3074         m.find();
       
  3075         m.appendReplacement(result, "$1w\\$2w$3");
       
  3076         if (!result.toString().equals("zzzabw$2wef"))
       
  3077             failCount++;
       
  3078 
       
  3079         m.appendTail(result);
       
  3080         if (!result.toString().equals("zzzabw$2wefzzz"))
       
  3081             failCount++;
       
  3082 
       
  3083         // Check to make sure a reference to nonexistent group causes error
       
  3084         blah = "zzzabcdcdefzzz";
       
  3085         p = Pattern.compile("(ab)(cd)*(ef)");
       
  3086         m = p.matcher(blah);
       
  3087         result = new StringBuilder();
       
  3088         m.find();
       
  3089         try {
       
  3090             m.appendReplacement(result, "$1w$5w$3");
       
  3091             failCount++;
       
  3092         } catch (IndexOutOfBoundsException ioobe) {
       
  3093             // Correct result
       
  3094         }
       
  3095 
       
  3096         // Check double digit group references
       
  3097         blah = "zzz123456789101112zzz";
       
  3098         p = Pattern.compile("(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)");
       
  3099         m = p.matcher(blah);
       
  3100         result = new StringBuilder();
       
  3101         m.find();
       
  3102         m.appendReplacement(result, "$1w$11w$3");
       
  3103         if (!result.toString().equals("zzz1w11w3"))
       
  3104             failCount++;
       
  3105 
       
  3106         // Check to make sure it backs off $15 to $1 if only three groups
       
  3107         blah = "zzzabcdcdefzzz";
       
  3108         p = Pattern.compile("(ab)(cd)*(ef)");
       
  3109         m = p.matcher(blah);
       
  3110         result = new StringBuilder();
       
  3111         m.find();
       
  3112         m.appendReplacement(result, "$1w$15w$3");
       
  3113         if (!result.toString().equals("zzzabwab5wef"))
       
  3114             failCount++;
       
  3115 
       
  3116 
       
  3117         // Supplementary character test
       
  3118         // SB substitution with literal
       
  3119         blah = toSupplementaries("zzzblahzzz");
       
  3120         p = Pattern.compile(toSupplementaries("blah"));
       
  3121         m = p.matcher(blah);
       
  3122         result = new StringBuilder();
       
  3123         try {
       
  3124             m.appendReplacement(result, toSupplementaries("blech"));
       
  3125             failCount++;
       
  3126         } catch (IllegalStateException e) {
       
  3127         }
       
  3128         m.find();
       
  3129         m.appendReplacement(result, toSupplementaries("blech"));
       
  3130         if (!result.toString().equals(toSupplementaries("zzzblech")))
       
  3131             failCount++;
       
  3132         m.appendTail(result);
       
  3133         if (!result.toString().equals(toSupplementaries("zzzblechzzz")))
       
  3134             failCount++;
       
  3135 
       
  3136         // SB substitution with groups
       
  3137         blah = toSupplementaries("zzzabcdzzz");
       
  3138         p = Pattern.compile(toSupplementaries("(ab)(cd)*"));
       
  3139         m = p.matcher(blah);
       
  3140         result = new StringBuilder();
       
  3141         try {
       
  3142             m.appendReplacement(result, "$1");
       
  3143             failCount++;
       
  3144         } catch (IllegalStateException e) {
       
  3145         }
       
  3146         m.find();
       
  3147         m.appendReplacement(result, "$1");
       
  3148         if (!result.toString().equals(toSupplementaries("zzzab")))
       
  3149             failCount++;
       
  3150 
       
  3151         m.appendTail(result);
       
  3152         if (!result.toString().equals(toSupplementaries("zzzabzzz")))
       
  3153             failCount++;
       
  3154 
       
  3155         // SB substitution with 3 groups
       
  3156         blah = toSupplementaries("zzzabcdcdefzzz");
       
  3157         p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
       
  3158         m = p.matcher(blah);
       
  3159         result = new StringBuilder();
       
  3160         try {
       
  3161             m.appendReplacement(result, toSupplementaries("$1w$2w$3"));
       
  3162             failCount++;
       
  3163         } catch (IllegalStateException e) {
       
  3164         }
       
  3165         m.find();
       
  3166         m.appendReplacement(result, toSupplementaries("$1w$2w$3"));
       
  3167         if (!result.toString().equals(toSupplementaries("zzzabwcdwef")))
       
  3168             failCount++;
       
  3169 
       
  3170         m.appendTail(result);
       
  3171         if (!result.toString().equals(toSupplementaries("zzzabwcdwefzzz")))
       
  3172             failCount++;
       
  3173 
       
  3174         // SB substitution with groups and three matches
       
  3175         // skipping middle match
       
  3176         blah = toSupplementaries("zzzabcdzzzabcddzzzabcdzzz");
       
  3177         p = Pattern.compile(toSupplementaries("(ab)(cd*)"));
       
  3178         m = p.matcher(blah);
       
  3179         result = new StringBuilder();
       
  3180         try {
       
  3181             m.appendReplacement(result, "$1");
       
  3182             failCount++;
       
  3183         } catch (IllegalStateException e) {
       
  3184         }
       
  3185         m.find();
       
  3186         m.appendReplacement(result, "$1");
       
  3187         if (!result.toString().equals(toSupplementaries("zzzab")))
       
  3188             failCount++;
       
  3189 
       
  3190         m.find();
       
  3191         m.find();
       
  3192         m.appendReplacement(result, "$2");
       
  3193         if (!result.toString().equals(toSupplementaries("zzzabzzzabcddzzzcd")))
       
  3194             failCount++;
       
  3195 
       
  3196         m.appendTail(result);
       
  3197         if (!result.toString().equals(toSupplementaries("zzzabzzzabcddzzzcdzzz")))
       
  3198             failCount++;
       
  3199 
       
  3200         // Check to make sure escaped $ is ignored
       
  3201         blah = toSupplementaries("zzzabcdcdefzzz");
       
  3202         p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
       
  3203         m = p.matcher(blah);
       
  3204         result = new StringBuilder();
       
  3205         m.find();
       
  3206         m.appendReplacement(result, toSupplementaries("$1w\\$2w$3"));
       
  3207         if (!result.toString().equals(toSupplementaries("zzzabw$2wef")))
       
  3208             failCount++;
       
  3209 
       
  3210         m.appendTail(result);
       
  3211         if (!result.toString().equals(toSupplementaries("zzzabw$2wefzzz")))
       
  3212             failCount++;
       
  3213 
       
  3214         // Check to make sure a reference to nonexistent group causes error
       
  3215         blah = toSupplementaries("zzzabcdcdefzzz");
       
  3216         p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
       
  3217         m = p.matcher(blah);
       
  3218         result = new StringBuilder();
       
  3219         m.find();
       
  3220         try {
       
  3221             m.appendReplacement(result, toSupplementaries("$1w$5w$3"));
       
  3222             failCount++;
       
  3223         } catch (IndexOutOfBoundsException ioobe) {
       
  3224             // Correct result
       
  3225         }
       
  3226         // Check double digit group references
       
  3227         blah = toSupplementaries("zzz123456789101112zzz");
       
  3228         p = Pattern.compile("(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)");
       
  3229         m = p.matcher(blah);
       
  3230         result = new StringBuilder();
       
  3231         m.find();
       
  3232         m.appendReplacement(result, toSupplementaries("$1w$11w$3"));
       
  3233         if (!result.toString().equals(toSupplementaries("zzz1w11w3")))
       
  3234             failCount++;
       
  3235 
       
  3236         // Check to make sure it backs off $15 to $1 if only three groups
       
  3237         blah = toSupplementaries("zzzabcdcdefzzz");
       
  3238         p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
       
  3239         m = p.matcher(blah);
       
  3240         result = new StringBuilder();
       
  3241         m.find();
       
  3242         m.appendReplacement(result, toSupplementaries("$1w$15w$3"));
       
  3243         if (!result.toString().equals(toSupplementaries("zzzabwab5wef")))
       
  3244             failCount++;
       
  3245         // Check nothing has been appended into the output buffer if
       
  3246         // the replacement string triggers IllegalArgumentException.
       
  3247         p = Pattern.compile("(abc)");
       
  3248         m = p.matcher("abcd");
       
  3249         result = new StringBuilder();
       
  3250         m.find();
       
  3251         try {
       
  3252             m.appendReplacement(result, ("xyz$g"));
       
  3253             failCount++;
       
  3254         } catch (IllegalArgumentException iae) {
       
  3255             if (result.length() != 0)
       
  3256                 failCount++;
       
  3257         }
       
  3258         report("SB Substitution 2");
       
  3259     }
       
  3260 
  2976     /*
  3261     /*
  2977      * 5 groups of characters are created to make a substitution string.
  3262      * 5 groups of characters are created to make a substitution string.
  2978      * A base string will be created including random lead chars, the
  3263      * A base string will be created including random lead chars, the
  2979      * substitution string, and random trailing chars.
  3264      * substitution string, and random trailing chars.
  2980      * A pattern containing the 5 groups is searched for and replaced with:
  3265      * A pattern containing the 5 groups is searched for and replaced with:
  3055             if (!result.equals(expectedResult))
  3340             if (!result.equals(expectedResult))
  3056                 failCount++;
  3341                 failCount++;
  3057         }
  3342         }
  3058 
  3343 
  3059         report("Substitution Basher");
  3344         report("Substitution Basher");
       
  3345     }
       
  3346 
       
  3347     /*
       
  3348      * 5 groups of characters are created to make a substitution string.
       
  3349      * A base string will be created including random lead chars, the
       
  3350      * substitution string, and random trailing chars.
       
  3351      * A pattern containing the 5 groups is searched for and replaced with:
       
  3352      * random group + random string + random group.
       
  3353      * The results are checked for correctness.
       
  3354      */
       
  3355     private static void substitutionBasher2() {
       
  3356         for (int runs = 0; runs<1000; runs++) {
       
  3357             // Create a base string to work in
       
  3358             int leadingChars = generator.nextInt(10);
       
  3359             StringBuilder baseBuffer = new StringBuilder(100);
       
  3360             String leadingString = getRandomAlphaString(leadingChars);
       
  3361             baseBuffer.append(leadingString);
       
  3362 
       
  3363             // Create 5 groups of random number of random chars
       
  3364             // Create the string to substitute
       
  3365             // Create the pattern string to search for
       
  3366             StringBuilder bufferToSub = new StringBuilder(25);
       
  3367             StringBuilder bufferToPat = new StringBuilder(50);
       
  3368             String[] groups = new String[5];
       
  3369             for(int i=0; i<5; i++) {
       
  3370                 int aGroupSize = generator.nextInt(5)+1;
       
  3371                 groups[i] = getRandomAlphaString(aGroupSize);
       
  3372                 bufferToSub.append(groups[i]);
       
  3373                 bufferToPat.append('(');
       
  3374                 bufferToPat.append(groups[i]);
       
  3375                 bufferToPat.append(')');
       
  3376             }
       
  3377             String stringToSub = bufferToSub.toString();
       
  3378             String pattern = bufferToPat.toString();
       
  3379 
       
  3380             // Place sub string into working string at random index
       
  3381             baseBuffer.append(stringToSub);
       
  3382 
       
  3383             // Append random chars to end
       
  3384             int trailingChars = generator.nextInt(10);
       
  3385             String trailingString = getRandomAlphaString(trailingChars);
       
  3386             baseBuffer.append(trailingString);
       
  3387             String baseString = baseBuffer.toString();
       
  3388 
       
  3389             // Create test pattern and matcher
       
  3390             Pattern p = Pattern.compile(pattern);
       
  3391             Matcher m = p.matcher(baseString);
       
  3392 
       
  3393             // Reject candidate if pattern happens to start early
       
  3394             m.find();
       
  3395             if (m.start() < leadingChars)
       
  3396                 continue;
       
  3397 
       
  3398             // Reject candidate if more than one match
       
  3399             if (m.find())
       
  3400                 continue;
       
  3401 
       
  3402             // Construct a replacement string with :
       
  3403             // random group + random string + random group
       
  3404             StringBuilder bufferToRep = new StringBuilder();
       
  3405             int groupIndex1 = generator.nextInt(5);
       
  3406             bufferToRep.append("$" + (groupIndex1 + 1));
       
  3407             String randomMidString = getRandomAlphaString(5);
       
  3408             bufferToRep.append(randomMidString);
       
  3409             int groupIndex2 = generator.nextInt(5);
       
  3410             bufferToRep.append("$" + (groupIndex2 + 1));
       
  3411             String replacement = bufferToRep.toString();
       
  3412 
       
  3413             // Do the replacement
       
  3414             String result = m.replaceAll(replacement);
       
  3415 
       
  3416             // Construct expected result
       
  3417             StringBuilder bufferToRes = new StringBuilder();
       
  3418             bufferToRes.append(leadingString);
       
  3419             bufferToRes.append(groups[groupIndex1]);
       
  3420             bufferToRes.append(randomMidString);
       
  3421             bufferToRes.append(groups[groupIndex2]);
       
  3422             bufferToRes.append(trailingString);
       
  3423             String expectedResult = bufferToRes.toString();
       
  3424 
       
  3425             // Check results
       
  3426             if (!result.equals(expectedResult)) {
       
  3427                 failCount++;
       
  3428             }
       
  3429         }
       
  3430 
       
  3431         report("Substitution Basher 2");
  3060     }
  3432     }
  3061 
  3433 
  3062     /**
  3434     /**
  3063      * Checks the handling of some escape sequences that the Pattern
  3435      * Checks the handling of some escape sequences that the Pattern
  3064      * class should process instead of the java compiler. These are
  3436      * class should process instead of the java compiler. These are