blob: f583769ecb274a75026b0d6901a18dcfeca45631 [file] [log] [blame]
sherman0b4d42d2009-02-23 21:06:15 -08001/*
ohairbf91ea12011-04-06 22:06:11 -07002 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
sherman0b4d42d2009-02-23 21:06:15 -08003 * 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
ohair2283b9d2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
sherman0b4d42d2009-02-23 21:06:15 -08008 * particular file as subject to the "Classpath" exception as provided
ohair2283b9d2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
sherman0b4d42d2009-02-23 21:06:15 -080010 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
ohair2283b9d2010-05-25 15:58:33 -070021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
sherman0b4d42d2009-02-23 21:06:15 -080024 */
25
26/**
27 * @test
28 * @summary tests RegExp framework
29 * @author Mike McCloskey
30 * @bug 4481568 4482696 4495089 4504687 4527731 4599621 4631553 4619345
31 * 4630911 4672616 4711773 4727935 4750573 4792284 4803197 4757029 4808962
32 * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
33 * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
34 * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
shermanb16229d2011-12-19 14:14:14 -080035 * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
sherman5c8f3492012-04-12 15:01:41 -070036 * 7067045
sherman0b4d42d2009-02-23 21:06:15 -080037 */
38
39import java.util.regex.*;
40import java.util.Random;
41import java.io.*;
42import java.util.*;
43import java.nio.CharBuffer;
44
45/**
46 * This is a test class created to check the operation of
47 * the Pattern and Matcher classes.
48 */
49public class RegExTest {
50
51 private static Random generator = new Random();
52 private static boolean failure = false;
53 private static int failCount = 0;
shermanb16229d2011-12-19 14:14:14 -080054 private static String firstFailure = null;
sherman0b4d42d2009-02-23 21:06:15 -080055
56 /**
57 * Main to interpret arguments and run several tests.
58 *
59 */
60 public static void main(String[] args) throws Exception {
61 // Most of the tests are in a file
62 processFile("TestCases.txt");
63 //processFile("PerlCases.txt");
64 processFile("BMPTestCases.txt");
65 processFile("SupplementaryTestCases.txt");
66
67 // These test many randomly generated char patterns
68 bm();
69 slice();
70
71 // These are hard to put into the file
72 escapes();
73 blankInput();
74
75 // Substitition tests on randomly generated sequences
76 globalSubstitute();
77 stringbufferSubstitute();
78 substitutionBasher();
79
80 // Canonical Equivalence
81 ceTest();
82
83 // Anchors
84 anchorTest();
85
86 // boolean match calls
87 matchesTest();
88 lookingAtTest();
89
90 // Pattern API
91 patternMatchesTest();
92
93 // Misc
94 lookbehindTest();
95 nullArgumentTest();
96 backRefTest();
97 groupCaptureTest();
98 caretTest();
99 charClassTest();
100 emptyPatternTest();
101 findIntTest();
102 group0Test();
103 longPatternTest();
104 octalTest();
105 ampersandTest();
106 negationTest();
107 splitTest();
108 appendTest();
109 caseFoldingTest();
110 commentsTest();
111 unixLinesTest();
112 replaceFirstTest();
113 gTest();
114 zTest();
115 serializeTest();
116 reluctantRepetitionTest();
117 multilineDollarTest();
118 dollarAtEndTest();
119 caretBetweenTerminatorsTest();
120 // This RFE rejected in Tiger numOccurrencesTest();
121 javaCharClassTest();
122 nonCaptureRepetitionTest();
123 notCapturedGroupCurlyMatchTest();
124 escapedSegmentTest();
125 literalPatternTest();
126 literalReplacementTest();
127 regionTest();
128 toStringTest();
129 negatedCharClassTest();
130 findFromTest();
131 boundsTest();
132 unicodeWordBoundsTest();
133 caretAtEndTest();
134 wordSearchTest();
135 hitEndTest();
136 toMatchResultTest();
137 surrogatesInClassTest();
shermanb16229d2011-12-19 14:14:14 -0800138 removeQEQuotingTest();
sherman0b4d42d2009-02-23 21:06:15 -0800139 namedGroupCaptureTest();
sherman6782c962010-02-05 00:10:42 -0800140 nonBmpClassComplementTest();
shermancc01ef52010-05-18 15:36:47 -0700141 unicodePropertiesTest();
shermanf03c78b2011-02-03 13:49:25 -0800142 unicodeHexNotationTest();
sherman85bbd8b2011-04-28 20:48:36 -0700143 unicodeClassesTest();
shermanb16229d2011-12-19 14:14:14 -0800144 if (failure) {
145 throw new
146 RuntimeException("RegExTest failed, 1st failure: " +
147 firstFailure);
148 } else {
sherman0b4d42d2009-02-23 21:06:15 -0800149 System.err.println("OKAY: All tests passed.");
shermanb16229d2011-12-19 14:14:14 -0800150 }
sherman0b4d42d2009-02-23 21:06:15 -0800151 }
152
153 // Utility functions
154
155 private static String getRandomAlphaString(int length) {
156 StringBuffer buf = new StringBuffer(length);
157 for (int i=0; i<length; i++) {
158 char randChar = (char)(97 + generator.nextInt(26));
159 buf.append(randChar);
160 }
161 return buf.toString();
162 }
163
164 private static void check(Matcher m, String expected) {
165 m.find();
166 if (!m.group().equals(expected))
167 failCount++;
168 }
169
170 private static void check(Matcher m, String result, boolean expected) {
171 m.find();
shermanf03c78b2011-02-03 13:49:25 -0800172 if (m.group().equals(result) != expected)
173 failCount++;
sherman0b4d42d2009-02-23 21:06:15 -0800174 }
175
176 private static void check(Pattern p, String s, boolean expected) {
shermanf03c78b2011-02-03 13:49:25 -0800177 if (p.matcher(s).find() != expected)
178 failCount++;
179 }
180
181 private static void check(String p, String s, boolean expected) {
182 Matcher matcher = Pattern.compile(p).matcher(s);
183 if (matcher.find() != expected)
184 failCount++;
sherman0b4d42d2009-02-23 21:06:15 -0800185 }
186
187 private static void check(String p, char c, boolean expected) {
188 String propertyPattern = expected ? "\\p" + p : "\\P" + p;
189 Pattern pattern = Pattern.compile(propertyPattern);
190 char[] ca = new char[1]; ca[0] = c;
191 Matcher matcher = pattern.matcher(new String(ca));
192 if (!matcher.find())
193 failCount++;
194 }
195
196 private static void check(String p, int codePoint, boolean expected) {
197 String propertyPattern = expected ? "\\p" + p : "\\P" + p;
198 Pattern pattern = Pattern.compile(propertyPattern);
199 char[] ca = Character.toChars(codePoint);
200 Matcher matcher = pattern.matcher(new String(ca));
201 if (!matcher.find())
202 failCount++;
203 }
204
205 private static void check(String p, int flag, String input, String s,
206 boolean expected)
207 {
208 Pattern pattern = Pattern.compile(p, flag);
209 Matcher matcher = pattern.matcher(input);
210 if (expected)
211 check(matcher, s, expected);
212 else
213 check(pattern, input, false);
214 }
215
216 private static void report(String testName) {
217 int spacesToAdd = 30 - testName.length();
218 StringBuffer paddedNameBuffer = new StringBuffer(testName);
219 for (int i=0; i<spacesToAdd; i++)
220 paddedNameBuffer.append(" ");
221 String paddedName = paddedNameBuffer.toString();
222 System.err.println(paddedName + ": " +
223 (failCount==0 ? "Passed":"Failed("+failCount+")"));
shermanb16229d2011-12-19 14:14:14 -0800224 if (failCount > 0) {
sherman0b4d42d2009-02-23 21:06:15 -0800225 failure = true;
shermanb16229d2011-12-19 14:14:14 -0800226
227 if (firstFailure == null) {
228 firstFailure = testName;
229 }
230 }
231
sherman0b4d42d2009-02-23 21:06:15 -0800232 failCount = 0;
233 }
234
235 /**
236 * Converts ASCII alphabet characters [A-Za-z] in the given 's' to
237 * supplementary characters. This method does NOT fully take care
238 * of the regex syntax.
239 */
240 private static String toSupplementaries(String s) {
241 int length = s.length();
242 StringBuffer sb = new StringBuffer(length * 2);
243
244 for (int i = 0; i < length; ) {
245 char c = s.charAt(i++);
246 if (c == '\\') {
247 sb.append(c);
248 if (i < length) {
249 c = s.charAt(i++);
250 sb.append(c);
251 if (c == 'u') {
252 // assume no syntax error
253 sb.append(s.charAt(i++));
254 sb.append(s.charAt(i++));
255 sb.append(s.charAt(i++));
256 sb.append(s.charAt(i++));
257 }
258 }
259 } else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
260 sb.append('\ud800').append((char)('\udc00'+c));
261 } else {
262 sb.append(c);
263 }
264 }
265 return sb.toString();
266 }
267
268 // Regular expression tests
269
270 // This is for bug 6178785
271 // Test if an expected NPE gets thrown when passing in a null argument
272 private static boolean check(Runnable test) {
273 try {
274 test.run();
275 failCount++;
276 return false;
277 } catch (NullPointerException npe) {
278 return true;
279 }
280 }
281
282 private static void nullArgumentTest() {
283 check(new Runnable() { public void run() { Pattern.compile(null); }});
284 check(new Runnable() { public void run() { Pattern.matches(null, null); }});
285 check(new Runnable() { public void run() { Pattern.matches("xyz", null);}});
286 check(new Runnable() { public void run() { Pattern.quote(null);}});
287 check(new Runnable() { public void run() { Pattern.compile("xyz").split(null);}});
288 check(new Runnable() { public void run() { Pattern.compile("xyz").matcher(null);}});
289
290 final Matcher m = Pattern.compile("xyz").matcher("xyz");
291 m.matches();
292 check(new Runnable() { public void run() { m.appendTail(null);}});
293 check(new Runnable() { public void run() { m.replaceAll(null);}});
294 check(new Runnable() { public void run() { m.replaceFirst(null);}});
295 check(new Runnable() { public void run() { m.appendReplacement(null, null);}});
296 check(new Runnable() { public void run() { m.reset(null);}});
297 check(new Runnable() { public void run() { Matcher.quoteReplacement(null);}});
298 //check(new Runnable() { public void run() { m.usePattern(null);}});
299
300 report("Null Argument");
301 }
302
303 // This is for bug6635133
304 // Test if surrogate pair in Unicode escapes can be handled correctly.
305 private static void surrogatesInClassTest() throws Exception {
306 Pattern pattern = Pattern.compile("[\\ud834\\udd21-\\ud834\\udd24]");
307 Matcher matcher = pattern.matcher("\ud834\udd22");
308 if (!matcher.find())
309 failCount++;
shermanb16229d2011-12-19 14:14:14 -0800310
311 report("Surrogate pair in Unicode escape");
312 }
313
314 // This is for bug6990617
315 // Test if Pattern.RemoveQEQuoting works correctly if the octal unicode
316 // char encoding is only 2 or 3 digits instead of 4 and the first quoted
317 // char is an octal digit.
318 private static void removeQEQuotingTest() throws Exception {
319 Pattern pattern =
320 Pattern.compile("\\011\\Q1sometext\\E\\011\\Q2sometext\\E");
321 Matcher matcher = pattern.matcher("\t1sometext\t2sometext");
322 if (!matcher.find())
323 failCount++;
324
325 report("Remove Q/E Quoting");
sherman0b4d42d2009-02-23 21:06:15 -0800326 }
327
328 // This is for bug 4988891
329 // Test toMatchResult to see that it is a copy of the Matcher
330 // that is not affected by subsequent operations on the original
331 private static void toMatchResultTest() throws Exception {
332 Pattern pattern = Pattern.compile("squid");
333 Matcher matcher = pattern.matcher(
334 "agiantsquidofdestinyasmallsquidoffate");
335 matcher.find();
336 int matcherStart1 = matcher.start();
337 MatchResult mr = matcher.toMatchResult();
338 if (mr == matcher)
339 failCount++;
340 int resultStart1 = mr.start();
341 if (matcherStart1 != resultStart1)
342 failCount++;
343 matcher.find();
344 int matcherStart2 = matcher.start();
345 int resultStart2 = mr.start();
346 if (matcherStart2 == resultStart2)
347 failCount++;
348 if (resultStart1 != resultStart2)
349 failCount++;
350 MatchResult mr2 = matcher.toMatchResult();
351 if (mr == mr2)
352 failCount++;
353 if (mr2.start() != matcherStart2)
354 failCount++;
355 report("toMatchResult is a copy");
356 }
357
358 // This is for bug 5013885
359 // Must test a slice to see if it reports hitEnd correctly
360 private static void hitEndTest() throws Exception {
361 // Basic test of Slice node
362 Pattern p = Pattern.compile("^squidattack");
363 Matcher m = p.matcher("squack");
364 m.find();
365 if (m.hitEnd())
366 failCount++;
367 m.reset("squid");
368 m.find();
369 if (!m.hitEnd())
370 failCount++;
371
372 // Test Slice, SliceA and SliceU nodes
373 for (int i=0; i<3; i++) {
374 int flags = 0;
375 if (i==1) flags = Pattern.CASE_INSENSITIVE;
376 if (i==2) flags = Pattern.UNICODE_CASE;
377 p = Pattern.compile("^abc", flags);
378 m = p.matcher("ad");
379 m.find();
380 if (m.hitEnd())
381 failCount++;
382 m.reset("ab");
383 m.find();
384 if (!m.hitEnd())
385 failCount++;
386 }
387
388 // Test Boyer-Moore node
389 p = Pattern.compile("catattack");
390 m = p.matcher("attack");
391 m.find();
392 if (!m.hitEnd())
393 failCount++;
394
395 p = Pattern.compile("catattack");
396 m = p.matcher("attackattackattackcatatta");
397 m.find();
398 if (!m.hitEnd())
399 failCount++;
sherman0b4d42d2009-02-23 21:06:15 -0800400 report("hitEnd from a Slice");
401 }
402
403 // This is for bug 4997476
404 // It is weird code submitted by customer demonstrating a regression
405 private static void wordSearchTest() throws Exception {
406 String testString = new String("word1 word2 word3");
407 Pattern p = Pattern.compile("\\b");
408 Matcher m = p.matcher(testString);
409 int position = 0;
410 int start = 0;
411 while (m.find(position)) {
412 start = m.start();
413 if (start == testString.length())
414 break;
415 if (m.find(start+1)) {
416 position = m.start();
417 } else {
418 position = testString.length();
419 }
420 if (testString.substring(start, position).equals(" "))
421 continue;
422 if (!testString.substring(start, position-1).startsWith("word"))
423 failCount++;
424 }
425 report("Customer word search");
426 }
427
428 // This is for bug 4994840
429 private static void caretAtEndTest() throws Exception {
430 // Problem only occurs with multiline patterns
431 // containing a beginning-of-line caret "^" followed
432 // by an expression that also matches the empty string.
433 Pattern pattern = Pattern.compile("^x?", Pattern.MULTILINE);
434 Matcher matcher = pattern.matcher("\r");
435 matcher.find();
436 matcher.find();
437 report("Caret at end");
438 }
439
440 // This test is for 4979006
441 // Check to see if word boundary construct properly handles unicode
442 // non spacing marks
443 private static void unicodeWordBoundsTest() throws Exception {
444 String spaces = " ";
445 String wordChar = "a";
446 String nsm = "\u030a";
447
448 assert (Character.getType('\u030a') == Character.NON_SPACING_MARK);
449
450 Pattern pattern = Pattern.compile("\\b");
451 Matcher matcher = pattern.matcher("");
452 // S=other B=word character N=non spacing mark .=word boundary
453 // SS.BB.SS
454 String input = spaces + wordChar + wordChar + spaces;
455 twoFindIndexes(input, matcher, 2, 4);
456 // SS.BBN.SS
457 input = spaces + wordChar +wordChar + nsm + spaces;
458 twoFindIndexes(input, matcher, 2, 5);
459 // SS.BN.SS
460 input = spaces + wordChar + nsm + spaces;
461 twoFindIndexes(input, matcher, 2, 4);
462 // SS.BNN.SS
463 input = spaces + wordChar + nsm + nsm + spaces;
464 twoFindIndexes(input, matcher, 2, 5);
465 // SSN.BB.SS
466 input = spaces + nsm + wordChar + wordChar + spaces;
467 twoFindIndexes(input, matcher, 3, 5);
468 // SS.BNB.SS
469 input = spaces + wordChar + nsm + wordChar + spaces;
470 twoFindIndexes(input, matcher, 2, 5);
471 // SSNNSS
472 input = spaces + nsm + nsm + spaces;
473 matcher.reset(input);
474 if (matcher.find())
475 failCount++;
476 // SSN.BBN.SS
477 input = spaces + nsm + wordChar + wordChar + nsm + spaces;
478 twoFindIndexes(input, matcher, 3, 6);
479
480 report("Unicode word boundary");
481 }
482
483 private static void twoFindIndexes(String input, Matcher matcher, int a,
484 int b) throws Exception
485 {
486 matcher.reset(input);
487 matcher.find();
488 if (matcher.start() != a)
489 failCount++;
490 matcher.find();
491 if (matcher.start() != b)
492 failCount++;
493 }
494
495 // This test is for 6284152
496 static void check(String regex, String input, String[] expected) {
497 List<String> result = new ArrayList<String>();
498 Pattern p = Pattern.compile(regex);
499 Matcher m = p.matcher(input);
500 while (m.find()) {
501 result.add(m.group());
502 }
503 if (!Arrays.asList(expected).equals(result))
504 failCount++;
505 }
506
507 private static void lookbehindTest() throws Exception {
508 //Positive
509 check("(?<=%.{0,5})foo\\d",
510 "%foo1\n%bar foo2\n%bar foo3\n%blahblah foo4\nfoo5",
511 new String[]{"foo1", "foo2", "foo3"});
512
513 //boundary at end of the lookbehind sub-regex should work consistently
514 //with the boundary just after the lookbehind sub-regex
515 check("(?<=.*\\b)foo", "abcd foo", new String[]{"foo"});
516 check("(?<=.*)\\bfoo", "abcd foo", new String[]{"foo"});
517 check("(?<!abc )\\bfoo", "abc foo", new String[0]);
518 check("(?<!abc \\b)foo", "abc foo", new String[0]);
519
520 //Negative
521 check("(?<!%.{0,5})foo\\d",
522 "%foo1\n%bar foo2\n%bar foo3\n%blahblah foo4\nfoo5",
523 new String[] {"foo4", "foo5"});
524
525 //Positive greedy
526 check("(?<=%b{1,4})foo", "%bbbbfoo", new String[] {"foo"});
527
528 //Positive reluctant
529 check("(?<=%b{1,4}?)foo", "%bbbbfoo", new String[] {"foo"});
530
531 //supplementary
532 check("(?<=%b{1,4})fo\ud800\udc00o", "%bbbbfo\ud800\udc00o",
533 new String[] {"fo\ud800\udc00o"});
534 check("(?<=%b{1,4}?)fo\ud800\udc00o", "%bbbbfo\ud800\udc00o",
535 new String[] {"fo\ud800\udc00o"});
536 check("(?<!%b{1,4})fo\ud800\udc00o", "%afo\ud800\udc00o",
537 new String[] {"fo\ud800\udc00o"});
538 check("(?<!%b{1,4}?)fo\ud800\udc00o", "%afo\ud800\udc00o",
539 new String[] {"fo\ud800\udc00o"});
540 report("Lookbehind");
541 }
542
543 // This test is for 4938995
544 // Check to see if weak region boundaries are transparent to
545 // lookahead and lookbehind constructs
546 private static void boundsTest() throws Exception {
547 String fullMessage = "catdogcat";
548 Pattern pattern = Pattern.compile("(?<=cat)dog(?=cat)");
549 Matcher matcher = pattern.matcher("catdogca");
550 matcher.useTransparentBounds(true);
551 if (matcher.find())
552 failCount++;
553 matcher.reset("atdogcat");
554 if (matcher.find())
555 failCount++;
556 matcher.reset(fullMessage);
557 if (!matcher.find())
558 failCount++;
559 matcher.reset(fullMessage);
560 matcher.region(0,9);
561 if (!matcher.find())
562 failCount++;
563 matcher.reset(fullMessage);
564 matcher.region(0,6);
565 if (!matcher.find())
566 failCount++;
567 matcher.reset(fullMessage);
568 matcher.region(3,6);
569 if (!matcher.find())
570 failCount++;
571 matcher.useTransparentBounds(false);
572 if (matcher.find())
573 failCount++;
574
575 // Negative lookahead/lookbehind
576 pattern = Pattern.compile("(?<!cat)dog(?!cat)");
577 matcher = pattern.matcher("dogcat");
578 matcher.useTransparentBounds(true);
579 matcher.region(0,3);
580 if (matcher.find())
581 failCount++;
582 matcher.reset("catdog");
583 matcher.region(3,6);
584 if (matcher.find())
585 failCount++;
586 matcher.useTransparentBounds(false);
587 matcher.reset("dogcat");
588 matcher.region(0,3);
589 if (!matcher.find())
590 failCount++;
591 matcher.reset("catdog");
592 matcher.region(3,6);
593 if (!matcher.find())
594 failCount++;
595
596 report("Region bounds transparency");
597 }
598
599 // This test is for 4945394
600 private static void findFromTest() throws Exception {
601 String message = "This is 40 $0 message.";
602 Pattern pat = Pattern.compile("\\$0");
603 Matcher match = pat.matcher(message);
604 if (!match.find())
605 failCount++;
606 if (match.find())
607 failCount++;
608 if (match.find())
609 failCount++;
610 report("Check for alternating find");
611 }
612
613 // This test is for 4872664 and 4892980
614 private static void negatedCharClassTest() throws Exception {
615 Pattern pattern = Pattern.compile("[^>]");
616 Matcher matcher = pattern.matcher("\u203A");
617 if (!matcher.matches())
618 failCount++;
619 pattern = Pattern.compile("[^fr]");
620 matcher = pattern.matcher("a");
621 if (!matcher.find())
622 failCount++;
623 matcher.reset("\u203A");
624 if (!matcher.find())
625 failCount++;
626 String s = "for";
627 String result[] = s.split("[^fr]");
628 if (!result[0].equals("f"))
629 failCount++;
630 if (!result[1].equals("r"))
631 failCount++;
632 s = "f\u203Ar";
633 result = s.split("[^fr]");
634 if (!result[0].equals("f"))
635 failCount++;
636 if (!result[1].equals("r"))
637 failCount++;
638
639 // Test adding to bits, subtracting a node, then adding to bits again
640 pattern = Pattern.compile("[^f\u203Ar]");
641 matcher = pattern.matcher("a");
642 if (!matcher.find())
643 failCount++;
644 matcher.reset("f");
645 if (matcher.find())
646 failCount++;
647 matcher.reset("\u203A");
648 if (matcher.find())
649 failCount++;
650 matcher.reset("r");
651 if (matcher.find())
652 failCount++;
653 matcher.reset("\u203B");
654 if (!matcher.find())
655 failCount++;
656
657 // Test subtracting a node, adding to bits, subtracting again
658 pattern = Pattern.compile("[^\u203Ar\u203B]");
659 matcher = pattern.matcher("a");
660 if (!matcher.find())
661 failCount++;
662 matcher.reset("\u203A");
663 if (matcher.find())
664 failCount++;
665 matcher.reset("r");
666 if (matcher.find())
667 failCount++;
668 matcher.reset("\u203B");
669 if (matcher.find())
670 failCount++;
671 matcher.reset("\u203C");
672 if (!matcher.find())
673 failCount++;
674
675 report("Negated Character Class");
676 }
677
678 // This test is for 4628291
679 private static void toStringTest() throws Exception {
680 Pattern pattern = Pattern.compile("b+");
681 if (pattern.toString() != "b+")
682 failCount++;
683 Matcher matcher = pattern.matcher("aaabbbccc");
684 String matcherString = matcher.toString(); // unspecified
685 matcher.find();
686 matcherString = matcher.toString(); // unspecified
687 matcher.region(0,3);
688 matcherString = matcher.toString(); // unspecified
689 matcher.reset();
690 matcherString = matcher.toString(); // unspecified
691 report("toString");
692 }
693
694 // This test is for 4808962
695 private static void literalPatternTest() throws Exception {
696 int flags = Pattern.LITERAL;
697
698 Pattern pattern = Pattern.compile("abc\\t$^", flags);
699 check(pattern, "abc\\t$^", true);
700
701 pattern = Pattern.compile(Pattern.quote("abc\\t$^"));
702 check(pattern, "abc\\t$^", true);
703
704 pattern = Pattern.compile("\\Qa^$bcabc\\E", flags);
705 check(pattern, "\\Qa^$bcabc\\E", true);
706 check(pattern, "a^$bcabc", false);
707
708 pattern = Pattern.compile("\\\\Q\\\\E");
709 check(pattern, "\\Q\\E", true);
710
711 pattern = Pattern.compile("\\Qabc\\Eefg\\\\Q\\\\Ehij");
712 check(pattern, "abcefg\\Q\\Ehij", true);
713
714 pattern = Pattern.compile("\\\\\\Q\\\\E");
715 check(pattern, "\\\\\\\\", true);
716
717 pattern = Pattern.compile(Pattern.quote("\\Qa^$bcabc\\E"));
718 check(pattern, "\\Qa^$bcabc\\E", true);
719 check(pattern, "a^$bcabc", false);
720
721 pattern = Pattern.compile(Pattern.quote("\\Qabc\\Edef"));
722 check(pattern, "\\Qabc\\Edef", true);
723 check(pattern, "abcdef", false);
724
725 pattern = Pattern.compile(Pattern.quote("abc\\Edef"));
726 check(pattern, "abc\\Edef", true);
727 check(pattern, "abcdef", false);
728
729 pattern = Pattern.compile(Pattern.quote("\\E"));
730 check(pattern, "\\E", true);
731
732 pattern = Pattern.compile("((((abc.+?:)", flags);
733 check(pattern, "((((abc.+?:)", true);
734
735 flags |= Pattern.MULTILINE;
736
737 pattern = Pattern.compile("^cat$", flags);
738 check(pattern, "abc^cat$def", true);
739 check(pattern, "cat", false);
740
741 flags |= Pattern.CASE_INSENSITIVE;
742
743 pattern = Pattern.compile("abcdef", flags);
744 check(pattern, "ABCDEF", true);
745 check(pattern, "AbCdEf", true);
746
747 flags |= Pattern.DOTALL;
748
749 pattern = Pattern.compile("a...b", flags);
750 check(pattern, "A...b", true);
751 check(pattern, "Axxxb", false);
752
753 flags |= Pattern.CANON_EQ;
754
755 Pattern p = Pattern.compile("testa\u030a", flags);
756 check(pattern, "testa\u030a", false);
757 check(pattern, "test\u00e5", false);
758
759 // Supplementary character test
760 flags = Pattern.LITERAL;
761
762 pattern = Pattern.compile(toSupplementaries("abc\\t$^"), flags);
763 check(pattern, toSupplementaries("abc\\t$^"), true);
764
765 pattern = Pattern.compile(Pattern.quote(toSupplementaries("abc\\t$^")));
766 check(pattern, toSupplementaries("abc\\t$^"), true);
767
768 pattern = Pattern.compile(toSupplementaries("\\Qa^$bcabc\\E"), flags);
769 check(pattern, toSupplementaries("\\Qa^$bcabc\\E"), true);
770 check(pattern, toSupplementaries("a^$bcabc"), false);
771
772 pattern = Pattern.compile(Pattern.quote(toSupplementaries("\\Qa^$bcabc\\E")));
773 check(pattern, toSupplementaries("\\Qa^$bcabc\\E"), true);
774 check(pattern, toSupplementaries("a^$bcabc"), false);
775
776 pattern = Pattern.compile(Pattern.quote(toSupplementaries("\\Qabc\\Edef")));
777 check(pattern, toSupplementaries("\\Qabc\\Edef"), true);
778 check(pattern, toSupplementaries("abcdef"), false);
779
780 pattern = Pattern.compile(Pattern.quote(toSupplementaries("abc\\Edef")));
781 check(pattern, toSupplementaries("abc\\Edef"), true);
782 check(pattern, toSupplementaries("abcdef"), false);
783
784 pattern = Pattern.compile(toSupplementaries("((((abc.+?:)"), flags);
785 check(pattern, toSupplementaries("((((abc.+?:)"), true);
786
787 flags |= Pattern.MULTILINE;
788
789 pattern = Pattern.compile(toSupplementaries("^cat$"), flags);
790 check(pattern, toSupplementaries("abc^cat$def"), true);
791 check(pattern, toSupplementaries("cat"), false);
792
793 flags |= Pattern.DOTALL;
794
795 // note: this is case-sensitive.
796 pattern = Pattern.compile(toSupplementaries("a...b"), flags);
797 check(pattern, toSupplementaries("a...b"), true);
798 check(pattern, toSupplementaries("axxxb"), false);
799
800 flags |= Pattern.CANON_EQ;
801
802 String t = toSupplementaries("test");
803 p = Pattern.compile(t + "a\u030a", flags);
804 check(pattern, t + "a\u030a", false);
805 check(pattern, t + "\u00e5", false);
806
807 report("Literal pattern");
808 }
809
810 // This test is for 4803179
811 // This test is also for 4808962, replacement parts
812 private static void literalReplacementTest() throws Exception {
813 int flags = Pattern.LITERAL;
814
815 Pattern pattern = Pattern.compile("abc", flags);
816 Matcher matcher = pattern.matcher("zzzabczzz");
817 String replaceTest = "$0";
818 String result = matcher.replaceAll(replaceTest);
819 if (!result.equals("zzzabczzz"))
820 failCount++;
821
822 matcher.reset();
823 String literalReplacement = matcher.quoteReplacement(replaceTest);
824 result = matcher.replaceAll(literalReplacement);
825 if (!result.equals("zzz$0zzz"))
826 failCount++;
827
828 matcher.reset();
829 replaceTest = "\\t$\\$";
830 literalReplacement = matcher.quoteReplacement(replaceTest);
831 result = matcher.replaceAll(literalReplacement);
832 if (!result.equals("zzz\\t$\\$zzz"))
833 failCount++;
834
835 // Supplementary character test
836 pattern = Pattern.compile(toSupplementaries("abc"), flags);
837 matcher = pattern.matcher(toSupplementaries("zzzabczzz"));
838 replaceTest = "$0";
839 result = matcher.replaceAll(replaceTest);
840 if (!result.equals(toSupplementaries("zzzabczzz")))
841 failCount++;
842
843 matcher.reset();
844 literalReplacement = matcher.quoteReplacement(replaceTest);
845 result = matcher.replaceAll(literalReplacement);
846 if (!result.equals(toSupplementaries("zzz$0zzz")))
847 failCount++;
848
849 matcher.reset();
850 replaceTest = "\\t$\\$";
851 literalReplacement = matcher.quoteReplacement(replaceTest);
852 result = matcher.replaceAll(literalReplacement);
853 if (!result.equals(toSupplementaries("zzz\\t$\\$zzz")))
854 failCount++;
855
sherman5c8f3492012-04-12 15:01:41 -0700856 // IAE should be thrown if backslash or '$' is the last character
857 // in replacement string
858 try {
859 "\uac00".replaceAll("\uac00", "$");
860 "\uac00".replaceAll("\uac00", "\\");
861 failCount++;
862 } catch (IllegalArgumentException iie) {
863 } catch (Exception e) {
864 failCount++;
865 }
866
sherman0b4d42d2009-02-23 21:06:15 -0800867 report("Literal replacement");
868 }
869
870 // This test is for 4757029
871 private static void regionTest() throws Exception {
872 Pattern pattern = Pattern.compile("abc");
873 Matcher matcher = pattern.matcher("abcdefabc");
874
875 matcher.region(0,9);
876 if (!matcher.find())
877 failCount++;
878 if (!matcher.find())
879 failCount++;
880 matcher.region(0,3);
881 if (!matcher.find())
882 failCount++;
883 matcher.region(3,6);
884 if (matcher.find())
885 failCount++;
886 matcher.region(0,2);
887 if (matcher.find())
888 failCount++;
889
890 expectRegionFail(matcher, 1, -1);
891 expectRegionFail(matcher, -1, -1);
892 expectRegionFail(matcher, -1, 1);
893 expectRegionFail(matcher, 5, 3);
894 expectRegionFail(matcher, 5, 12);
895 expectRegionFail(matcher, 12, 12);
896
897 pattern = Pattern.compile("^abc$");
898 matcher = pattern.matcher("zzzabczzz");
899 matcher.region(0,9);
900 if (matcher.find())
901 failCount++;
902 matcher.region(3,6);
903 if (!matcher.find())
904 failCount++;
905 matcher.region(3,6);
906 matcher.useAnchoringBounds(false);
907 if (matcher.find())
908 failCount++;
909
910 // Supplementary character test
911 pattern = Pattern.compile(toSupplementaries("abc"));
912 matcher = pattern.matcher(toSupplementaries("abcdefabc"));
913 matcher.region(0,9*2);
914 if (!matcher.find())
915 failCount++;
916 if (!matcher.find())
917 failCount++;
918 matcher.region(0,3*2);
919 if (!matcher.find())
920 failCount++;
921 matcher.region(1,3*2);
922 if (matcher.find())
923 failCount++;
924 matcher.region(3*2,6*2);
925 if (matcher.find())
926 failCount++;
927 matcher.region(0,2*2);
928 if (matcher.find())
929 failCount++;
930 matcher.region(0,2*2+1);
931 if (matcher.find())
932 failCount++;
933
934 expectRegionFail(matcher, 1*2, -1);
935 expectRegionFail(matcher, -1, -1);
936 expectRegionFail(matcher, -1, 1*2);
937 expectRegionFail(matcher, 5*2, 3*2);
938 expectRegionFail(matcher, 5*2, 12*2);
939 expectRegionFail(matcher, 12*2, 12*2);
940
941 pattern = Pattern.compile(toSupplementaries("^abc$"));
942 matcher = pattern.matcher(toSupplementaries("zzzabczzz"));
943 matcher.region(0,9*2);
944 if (matcher.find())
945 failCount++;
946 matcher.region(3*2,6*2);
947 if (!matcher.find())
948 failCount++;
949 matcher.region(3*2+1,6*2);
950 if (matcher.find())
951 failCount++;
952 matcher.region(3*2,6*2-1);
953 if (matcher.find())
954 failCount++;
955 matcher.region(3*2,6*2);
956 matcher.useAnchoringBounds(false);
957 if (matcher.find())
958 failCount++;
959 report("Regions");
960 }
961
962 private static void expectRegionFail(Matcher matcher, int index1,
963 int index2)
964 {
965 try {
966 matcher.region(index1, index2);
967 failCount++;
968 } catch (IndexOutOfBoundsException ioobe) {
969 // Correct result
970 } catch (IllegalStateException ise) {
971 // Correct result
972 }
973 }
974
975 // This test is for 4803197
976 private static void escapedSegmentTest() throws Exception {
977
978 Pattern pattern = Pattern.compile("\\Qdir1\\dir2\\E");
979 check(pattern, "dir1\\dir2", true);
980
981 pattern = Pattern.compile("\\Qdir1\\dir2\\\\E");
982 check(pattern, "dir1\\dir2\\", true);
983
984 pattern = Pattern.compile("(\\Qdir1\\dir2\\\\E)");
985 check(pattern, "dir1\\dir2\\", true);
986
987 // Supplementary character test
988 pattern = Pattern.compile(toSupplementaries("\\Qdir1\\dir2\\E"));
989 check(pattern, toSupplementaries("dir1\\dir2"), true);
990
991 pattern = Pattern.compile(toSupplementaries("\\Qdir1\\dir2")+"\\\\E");
992 check(pattern, toSupplementaries("dir1\\dir2\\"), true);
993
994 pattern = Pattern.compile(toSupplementaries("(\\Qdir1\\dir2")+"\\\\E)");
995 check(pattern, toSupplementaries("dir1\\dir2\\"), true);
996
997 report("Escaped segment");
998 }
999
1000 // This test is for 4792284
1001 private static void nonCaptureRepetitionTest() throws Exception {
1002 String input = "abcdefgh;";
1003
1004 String[] patterns = new String[] {
1005 "(?:\\w{4})+;",
1006 "(?:\\w{8})*;",
1007 "(?:\\w{2}){2,4};",
1008 "(?:\\w{4}){2,};", // only matches the
1009 ".*?(?:\\w{5})+;", // specified minimum
1010 ".*?(?:\\w{9})*;", // number of reps - OK
1011 "(?:\\w{4})+?;", // lazy repetition - OK
1012 "(?:\\w{4})++;", // possessive repetition - OK
1013 "(?:\\w{2,}?)+;", // non-deterministic - OK
1014 "(\\w{4})+;", // capturing group - OK
1015 };
1016
1017 for (int i = 0; i < patterns.length; i++) {
1018 // Check find()
1019 check(patterns[i], 0, input, input, true);
1020 // Check matches()
1021 Pattern p = Pattern.compile(patterns[i]);
1022 Matcher m = p.matcher(input);
1023
1024 if (m.matches()) {
1025 if (!m.group(0).equals(input))
1026 failCount++;
1027 } else {
1028 failCount++;
1029 }
1030 }
1031
1032 report("Non capturing repetition");
1033 }
1034
1035 // This test is for 6358731
1036 private static void notCapturedGroupCurlyMatchTest() throws Exception {
1037 Pattern pattern = Pattern.compile("(abc)+|(abcd)+");
1038 Matcher matcher = pattern.matcher("abcd");
1039 if (!matcher.matches() ||
1040 matcher.group(1) != null ||
1041 !matcher.group(2).equals("abcd")) {
1042 failCount++;
1043 }
1044 report("Not captured GroupCurly");
1045 }
1046
1047 // This test is for 4706545
1048 private static void javaCharClassTest() throws Exception {
1049 for (int i=0; i<1000; i++) {
1050 char c = (char)generator.nextInt();
1051 check("{javaLowerCase}", c, Character.isLowerCase(c));
1052 check("{javaUpperCase}", c, Character.isUpperCase(c));
1053 check("{javaUpperCase}+", c, Character.isUpperCase(c));
1054 check("{javaTitleCase}", c, Character.isTitleCase(c));
1055 check("{javaDigit}", c, Character.isDigit(c));
1056 check("{javaDefined}", c, Character.isDefined(c));
1057 check("{javaLetter}", c, Character.isLetter(c));
1058 check("{javaLetterOrDigit}", c, Character.isLetterOrDigit(c));
1059 check("{javaJavaIdentifierStart}", c,
1060 Character.isJavaIdentifierStart(c));
1061 check("{javaJavaIdentifierPart}", c,
1062 Character.isJavaIdentifierPart(c));
1063 check("{javaUnicodeIdentifierStart}", c,
1064 Character.isUnicodeIdentifierStart(c));
1065 check("{javaUnicodeIdentifierPart}", c,
1066 Character.isUnicodeIdentifierPart(c));
1067 check("{javaIdentifierIgnorable}", c,
1068 Character.isIdentifierIgnorable(c));
1069 check("{javaSpaceChar}", c, Character.isSpaceChar(c));
1070 check("{javaWhitespace}", c, Character.isWhitespace(c));
1071 check("{javaISOControl}", c, Character.isISOControl(c));
1072 check("{javaMirrored}", c, Character.isMirrored(c));
1073
1074 }
1075
1076 // Supplementary character test
1077 for (int i=0; i<1000; i++) {
1078 int c = generator.nextInt(Character.MAX_CODE_POINT
1079 - Character.MIN_SUPPLEMENTARY_CODE_POINT)
1080 + Character.MIN_SUPPLEMENTARY_CODE_POINT;
1081 check("{javaLowerCase}", c, Character.isLowerCase(c));
1082 check("{javaUpperCase}", c, Character.isUpperCase(c));
1083 check("{javaUpperCase}+", c, Character.isUpperCase(c));
1084 check("{javaTitleCase}", c, Character.isTitleCase(c));
1085 check("{javaDigit}", c, Character.isDigit(c));
1086 check("{javaDefined}", c, Character.isDefined(c));
1087 check("{javaLetter}", c, Character.isLetter(c));
1088 check("{javaLetterOrDigit}", c, Character.isLetterOrDigit(c));
1089 check("{javaJavaIdentifierStart}", c,
1090 Character.isJavaIdentifierStart(c));
1091 check("{javaJavaIdentifierPart}", c,
1092 Character.isJavaIdentifierPart(c));
1093 check("{javaUnicodeIdentifierStart}", c,
1094 Character.isUnicodeIdentifierStart(c));
1095 check("{javaUnicodeIdentifierPart}", c,
1096 Character.isUnicodeIdentifierPart(c));
1097 check("{javaIdentifierIgnorable}", c,
1098 Character.isIdentifierIgnorable(c));
1099 check("{javaSpaceChar}", c, Character.isSpaceChar(c));
1100 check("{javaWhitespace}", c, Character.isWhitespace(c));
1101 check("{javaISOControl}", c, Character.isISOControl(c));
1102 check("{javaMirrored}", c, Character.isMirrored(c));
1103 }
1104
1105 report("Java character classes");
1106 }
1107
1108 // This test is for 4523620
1109 /*
1110 private static void numOccurrencesTest() throws Exception {
1111 Pattern pattern = Pattern.compile("aaa");
1112
1113 if (pattern.numOccurrences("aaaaaa", false) != 2)
1114 failCount++;
1115 if (pattern.numOccurrences("aaaaaa", true) != 4)
1116 failCount++;
1117
1118 pattern = Pattern.compile("^");
1119 if (pattern.numOccurrences("aaaaaa", false) != 1)
1120 failCount++;
1121 if (pattern.numOccurrences("aaaaaa", true) != 1)
1122 failCount++;
1123
1124 report("Number of Occurrences");
1125 }
1126 */
1127
1128 // This test is for 4776374
1129 private static void caretBetweenTerminatorsTest() throws Exception {
1130 int flags1 = Pattern.DOTALL;
1131 int flags2 = Pattern.DOTALL | Pattern.UNIX_LINES;
1132 int flags3 = Pattern.DOTALL | Pattern.UNIX_LINES | Pattern.MULTILINE;
1133 int flags4 = Pattern.DOTALL | Pattern.MULTILINE;
1134
1135 check("^....", flags1, "test\ntest", "test", true);
1136 check(".....^", flags1, "test\ntest", "test", false);
1137 check(".....^", flags1, "test\n", "test", false);
1138 check("....^", flags1, "test\r\n", "test", false);
1139
1140 check("^....", flags2, "test\ntest", "test", true);
1141 check("....^", flags2, "test\ntest", "test", false);
1142 check(".....^", flags2, "test\n", "test", false);
1143 check("....^", flags2, "test\r\n", "test", false);
1144
1145 check("^....", flags3, "test\ntest", "test", true);
1146 check(".....^", flags3, "test\ntest", "test\n", true);
1147 check(".....^", flags3, "test\u0085test", "test\u0085", false);
1148 check(".....^", flags3, "test\n", "test", false);
1149 check(".....^", flags3, "test\r\n", "test", false);
1150 check("......^", flags3, "test\r\ntest", "test\r\n", true);
1151
1152 check("^....", flags4, "test\ntest", "test", true);
1153 check(".....^", flags3, "test\ntest", "test\n", true);
1154 check(".....^", flags4, "test\u0085test", "test\u0085", true);
1155 check(".....^", flags4, "test\n", "test\n", false);
1156 check(".....^", flags4, "test\r\n", "test\r", false);
1157
1158 // Supplementary character test
1159 String t = toSupplementaries("test");
1160 check("^....", flags1, t+"\n"+t, t, true);
1161 check(".....^", flags1, t+"\n"+t, t, false);
1162 check(".....^", flags1, t+"\n", t, false);
1163 check("....^", flags1, t+"\r\n", t, false);
1164
1165 check("^....", flags2, t+"\n"+t, t, true);
1166 check("....^", flags2, t+"\n"+t, t, false);
1167 check(".....^", flags2, t+"\n", t, false);
1168 check("....^", flags2, t+"\r\n", t, false);
1169
1170 check("^....", flags3, t+"\n"+t, t, true);
1171 check(".....^", flags3, t+"\n"+t, t+"\n", true);
1172 check(".....^", flags3, t+"\u0085"+t, t+"\u0085", false);
1173 check(".....^", flags3, t+"\n", t, false);
1174 check(".....^", flags3, t+"\r\n", t, false);
1175 check("......^", flags3, t+"\r\n"+t, t+"\r\n", true);
1176
1177 check("^....", flags4, t+"\n"+t, t, true);
1178 check(".....^", flags3, t+"\n"+t, t+"\n", true);
1179 check(".....^", flags4, t+"\u0085"+t, t+"\u0085", true);
1180 check(".....^", flags4, t+"\n", t+"\n", false);
1181 check(".....^", flags4, t+"\r\n", t+"\r", false);
1182
1183 report("Caret between terminators");
1184 }
1185
1186 // This test is for 4727935
1187 private static void dollarAtEndTest() throws Exception {
1188 int flags1 = Pattern.DOTALL;
1189 int flags2 = Pattern.DOTALL | Pattern.UNIX_LINES;
1190 int flags3 = Pattern.DOTALL | Pattern.MULTILINE;
1191
1192 check("....$", flags1, "test\n", "test", true);
1193 check("....$", flags1, "test\r\n", "test", true);
1194 check(".....$", flags1, "test\n", "test\n", true);
1195 check(".....$", flags1, "test\u0085", "test\u0085", true);
1196 check("....$", flags1, "test\u0085", "test", true);
1197
1198 check("....$", flags2, "test\n", "test", true);
1199 check(".....$", flags2, "test\n", "test\n", true);
1200 check(".....$", flags2, "test\u0085", "test\u0085", true);
1201 check("....$", flags2, "test\u0085", "est\u0085", true);
1202
1203 check("....$.blah", flags3, "test\nblah", "test\nblah", true);
1204 check(".....$.blah", flags3, "test\n\nblah", "test\n\nblah", true);
1205 check("....$blah", flags3, "test\nblah", "!!!!", false);
1206 check(".....$blah", flags3, "test\nblah", "!!!!", false);
1207
1208 // Supplementary character test
1209 String t = toSupplementaries("test");
1210 String b = toSupplementaries("blah");
1211 check("....$", flags1, t+"\n", t, true);
1212 check("....$", flags1, t+"\r\n", t, true);
1213 check(".....$", flags1, t+"\n", t+"\n", true);
1214 check(".....$", flags1, t+"\u0085", t+"\u0085", true);
1215 check("....$", flags1, t+"\u0085", t, true);
1216
1217 check("....$", flags2, t+"\n", t, true);
1218 check(".....$", flags2, t+"\n", t+"\n", true);
1219 check(".....$", flags2, t+"\u0085", t+"\u0085", true);
1220 check("....$", flags2, t+"\u0085", toSupplementaries("est\u0085"), true);
1221
1222 check("....$."+b, flags3, t+"\n"+b, t+"\n"+b, true);
1223 check(".....$."+b, flags3, t+"\n\n"+b, t+"\n\n"+b, true);
1224 check("....$"+b, flags3, t+"\n"+b, "!!!!", false);
1225 check(".....$"+b, flags3, t+"\n"+b, "!!!!", false);
1226
1227 report("Dollar at End");
1228 }
1229
1230 // This test is for 4711773
1231 private static void multilineDollarTest() throws Exception {
1232 Pattern findCR = Pattern.compile("$", Pattern.MULTILINE);
1233 Matcher matcher = findCR.matcher("first bit\nsecond bit");
1234 matcher.find();
1235 if (matcher.start(0) != 9)
1236 failCount++;
1237 matcher.find();
1238 if (matcher.start(0) != 20)
1239 failCount++;
1240
1241 // Supplementary character test
1242 matcher = findCR.matcher(toSupplementaries("first bit\n second bit")); // double BMP chars
1243 matcher.find();
1244 if (matcher.start(0) != 9*2)
1245 failCount++;
1246 matcher.find();
1247 if (matcher.start(0) != 20*2)
1248 failCount++;
1249
1250 report("Multiline Dollar");
1251 }
1252
1253 private static void reluctantRepetitionTest() throws Exception {
1254 Pattern p = Pattern.compile("1(\\s\\S+?){1,3}?[\\s,]2");
1255 check(p, "1 word word word 2", true);
1256 check(p, "1 wor wo w 2", true);
1257 check(p, "1 word word 2", true);
1258 check(p, "1 word 2", true);
1259 check(p, "1 wo w w 2", true);
1260 check(p, "1 wo w 2", true);
1261 check(p, "1 wor w 2", true);
1262
1263 p = Pattern.compile("([a-z])+?c");
1264 Matcher m = p.matcher("ababcdefdec");
1265 check(m, "ababc");
1266
1267 // Supplementary character test
1268 p = Pattern.compile(toSupplementaries("([a-z])+?c"));
1269 m = p.matcher(toSupplementaries("ababcdefdec"));
1270 check(m, toSupplementaries("ababc"));
1271
1272 report("Reluctant Repetition");
1273 }
1274
1275 private static void serializeTest() throws Exception {
1276 String patternStr = "(b)";
1277 String matchStr = "b";
1278 Pattern pattern = Pattern.compile(patternStr);
1279 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1280 ObjectOutputStream oos = new ObjectOutputStream(baos);
1281 oos.writeObject(pattern);
1282 oos.close();
1283 ObjectInputStream ois = new ObjectInputStream(
1284 new ByteArrayInputStream(baos.toByteArray()));
1285 Pattern serializedPattern = (Pattern)ois.readObject();
1286 ois.close();
1287 Matcher matcher = serializedPattern.matcher(matchStr);
1288 if (!matcher.matches())
1289 failCount++;
1290 if (matcher.groupCount() != 1)
1291 failCount++;
1292
1293 report("Serialization");
1294 }
1295
1296 private static void gTest() {
1297 Pattern pattern = Pattern.compile("\\G\\w");
1298 Matcher matcher = pattern.matcher("abc#x#x");
1299 matcher.find();
1300 matcher.find();
1301 matcher.find();
1302 if (matcher.find())
1303 failCount++;
1304
1305 pattern = Pattern.compile("\\GA*");
1306 matcher = pattern.matcher("1A2AA3");
1307 matcher.find();
1308 if (matcher.find())
1309 failCount++;
1310
1311 pattern = Pattern.compile("\\GA*");
1312 matcher = pattern.matcher("1A2AA3");
1313 if (!matcher.find(1))
1314 failCount++;
1315 matcher.find();
1316 if (matcher.find())
1317 failCount++;
1318
1319 report("\\G");
1320 }
1321
1322 private static void zTest() {
1323 Pattern pattern = Pattern.compile("foo\\Z");
1324 // Positives
1325 check(pattern, "foo\u0085", true);
1326 check(pattern, "foo\u2028", true);
1327 check(pattern, "foo\u2029", true);
1328 check(pattern, "foo\n", true);
1329 check(pattern, "foo\r", true);
1330 check(pattern, "foo\r\n", true);
1331 // Negatives
1332 check(pattern, "fooo", false);
1333 check(pattern, "foo\n\r", false);
1334
1335 pattern = Pattern.compile("foo\\Z", Pattern.UNIX_LINES);
1336 // Positives
1337 check(pattern, "foo", true);
1338 check(pattern, "foo\n", true);
1339 // Negatives
1340 check(pattern, "foo\r", false);
1341 check(pattern, "foo\u0085", false);
1342 check(pattern, "foo\u2028", false);
1343 check(pattern, "foo\u2029", false);
1344
1345 report("\\Z");
1346 }
1347
1348 private static void replaceFirstTest() {
1349 Pattern pattern = Pattern.compile("(ab)(c*)");
1350 Matcher matcher = pattern.matcher("abccczzzabcczzzabccc");
1351 if (!matcher.replaceFirst("test").equals("testzzzabcczzzabccc"))
1352 failCount++;
1353
1354 matcher.reset("zzzabccczzzabcczzzabccczzz");
1355 if (!matcher.replaceFirst("test").equals("zzztestzzzabcczzzabccczzz"))
1356 failCount++;
1357
1358 matcher.reset("zzzabccczzzabcczzzabccczzz");
1359 String result = matcher.replaceFirst("$1");
1360 if (!result.equals("zzzabzzzabcczzzabccczzz"))
1361 failCount++;
1362
1363 matcher.reset("zzzabccczzzabcczzzabccczzz");
1364 result = matcher.replaceFirst("$2");
1365 if (!result.equals("zzzccczzzabcczzzabccczzz"))
1366 failCount++;
1367
1368 pattern = Pattern.compile("a*");
1369 matcher = pattern.matcher("aaaaaaaaaa");
1370 if (!matcher.replaceFirst("test").equals("test"))
1371 failCount++;
1372
1373 pattern = Pattern.compile("a+");
1374 matcher = pattern.matcher("zzzaaaaaaaaaa");
1375 if (!matcher.replaceFirst("test").equals("zzztest"))
1376 failCount++;
1377
1378 // Supplementary character test
1379 pattern = Pattern.compile(toSupplementaries("(ab)(c*)"));
1380 matcher = pattern.matcher(toSupplementaries("abccczzzabcczzzabccc"));
1381 if (!matcher.replaceFirst(toSupplementaries("test"))
1382 .equals(toSupplementaries("testzzzabcczzzabccc")))
1383 failCount++;
1384
1385 matcher.reset(toSupplementaries("zzzabccczzzabcczzzabccczzz"));
1386 if (!matcher.replaceFirst(toSupplementaries("test")).
1387 equals(toSupplementaries("zzztestzzzabcczzzabccczzz")))
1388 failCount++;
1389
1390 matcher.reset(toSupplementaries("zzzabccczzzabcczzzabccczzz"));
1391 result = matcher.replaceFirst("$1");
1392 if (!result.equals(toSupplementaries("zzzabzzzabcczzzabccczzz")))
1393 failCount++;
1394
1395 matcher.reset(toSupplementaries("zzzabccczzzabcczzzabccczzz"));
1396 result = matcher.replaceFirst("$2");
1397 if (!result.equals(toSupplementaries("zzzccczzzabcczzzabccczzz")))
1398 failCount++;
1399
1400 pattern = Pattern.compile(toSupplementaries("a*"));
1401 matcher = pattern.matcher(toSupplementaries("aaaaaaaaaa"));
1402 if (!matcher.replaceFirst(toSupplementaries("test")).equals(toSupplementaries("test")))
1403 failCount++;
1404
1405 pattern = Pattern.compile(toSupplementaries("a+"));
1406 matcher = pattern.matcher(toSupplementaries("zzzaaaaaaaaaa"));
1407 if (!matcher.replaceFirst(toSupplementaries("test")).equals(toSupplementaries("zzztest")))
1408 failCount++;
1409
1410 report("Replace First");
1411 }
1412
1413 private static void unixLinesTest() {
1414 Pattern pattern = Pattern.compile(".*");
1415 Matcher matcher = pattern.matcher("aa\u2028blah");
1416 matcher.find();
1417 if (!matcher.group(0).equals("aa"))
1418 failCount++;
1419
1420 pattern = Pattern.compile(".*", Pattern.UNIX_LINES);
1421 matcher = pattern.matcher("aa\u2028blah");
1422 matcher.find();
1423 if (!matcher.group(0).equals("aa\u2028blah"))
1424 failCount++;
1425
1426 pattern = Pattern.compile("[az]$",
1427 Pattern.MULTILINE | Pattern.UNIX_LINES);
1428 matcher = pattern.matcher("aa\u2028zz");
1429 check(matcher, "a\u2028", false);
1430
1431 // Supplementary character test
1432 pattern = Pattern.compile(".*");
1433 matcher = pattern.matcher(toSupplementaries("aa\u2028blah"));
1434 matcher.find();
1435 if (!matcher.group(0).equals(toSupplementaries("aa")))
1436 failCount++;
1437
1438 pattern = Pattern.compile(".*", Pattern.UNIX_LINES);
1439 matcher = pattern.matcher(toSupplementaries("aa\u2028blah"));
1440 matcher.find();
1441 if (!matcher.group(0).equals(toSupplementaries("aa\u2028blah")))
1442 failCount++;
1443
1444 pattern = Pattern.compile(toSupplementaries("[az]$"),
1445 Pattern.MULTILINE | Pattern.UNIX_LINES);
1446 matcher = pattern.matcher(toSupplementaries("aa\u2028zz"));
1447 check(matcher, toSupplementaries("a\u2028"), false);
1448
1449 report("Unix Lines");
1450 }
1451
1452 private static void commentsTest() {
1453 int flags = Pattern.COMMENTS;
1454
1455 Pattern pattern = Pattern.compile("aa \\# aa", flags);
1456 Matcher matcher = pattern.matcher("aa#aa");
1457 if (!matcher.matches())
1458 failCount++;
1459
1460 pattern = Pattern.compile("aa # blah", flags);
1461 matcher = pattern.matcher("aa");
1462 if (!matcher.matches())
1463 failCount++;
1464
1465 pattern = Pattern.compile("aa blah", flags);
1466 matcher = pattern.matcher("aablah");
1467 if (!matcher.matches())
1468 failCount++;
1469
1470 pattern = Pattern.compile("aa # blah blech ", flags);
1471 matcher = pattern.matcher("aa");
1472 if (!matcher.matches())
1473 failCount++;
1474
1475 pattern = Pattern.compile("aa # blah\n ", flags);
1476 matcher = pattern.matcher("aa");
1477 if (!matcher.matches())
1478 failCount++;
1479
1480 pattern = Pattern.compile("aa # blah\nbc # blech", flags);
1481 matcher = pattern.matcher("aabc");
1482 if (!matcher.matches())
1483 failCount++;
1484
1485 pattern = Pattern.compile("aa # blah\nbc# blech", flags);
1486 matcher = pattern.matcher("aabc");
1487 if (!matcher.matches())
1488 failCount++;
1489
1490 pattern = Pattern.compile("aa # blah\nbc\\# blech", flags);
1491 matcher = pattern.matcher("aabc#blech");
1492 if (!matcher.matches())
1493 failCount++;
1494
1495 // Supplementary character test
1496 pattern = Pattern.compile(toSupplementaries("aa \\# aa"), flags);
1497 matcher = pattern.matcher(toSupplementaries("aa#aa"));
1498 if (!matcher.matches())
1499 failCount++;
1500
1501 pattern = Pattern.compile(toSupplementaries("aa # blah"), flags);
1502 matcher = pattern.matcher(toSupplementaries("aa"));
1503 if (!matcher.matches())
1504 failCount++;
1505
1506 pattern = Pattern.compile(toSupplementaries("aa blah"), flags);
1507 matcher = pattern.matcher(toSupplementaries("aablah"));
1508 if (!matcher.matches())
1509 failCount++;
1510
1511 pattern = Pattern.compile(toSupplementaries("aa # blah blech "), flags);
1512 matcher = pattern.matcher(toSupplementaries("aa"));
1513 if (!matcher.matches())
1514 failCount++;
1515
1516 pattern = Pattern.compile(toSupplementaries("aa # blah\n "), flags);
1517 matcher = pattern.matcher(toSupplementaries("aa"));
1518 if (!matcher.matches())
1519 failCount++;
1520
1521 pattern = Pattern.compile(toSupplementaries("aa # blah\nbc # blech"), flags);
1522 matcher = pattern.matcher(toSupplementaries("aabc"));
1523 if (!matcher.matches())
1524 failCount++;
1525
1526 pattern = Pattern.compile(toSupplementaries("aa # blah\nbc# blech"), flags);
1527 matcher = pattern.matcher(toSupplementaries("aabc"));
1528 if (!matcher.matches())
1529 failCount++;
1530
1531 pattern = Pattern.compile(toSupplementaries("aa # blah\nbc\\# blech"), flags);
1532 matcher = pattern.matcher(toSupplementaries("aabc#blech"));
1533 if (!matcher.matches())
1534 failCount++;
1535
1536 report("Comments");
1537 }
1538
1539 private static void caseFoldingTest() { // bug 4504687
1540 int flags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
1541 Pattern pattern = Pattern.compile("aa", flags);
1542 Matcher matcher = pattern.matcher("ab");
1543 if (matcher.matches())
1544 failCount++;
1545
1546 pattern = Pattern.compile("aA", flags);
1547 matcher = pattern.matcher("ab");
1548 if (matcher.matches())
1549 failCount++;
1550
1551 pattern = Pattern.compile("aa", flags);
1552 matcher = pattern.matcher("aB");
1553 if (matcher.matches())
1554 failCount++;
1555 matcher = pattern.matcher("Ab");
1556 if (matcher.matches())
1557 failCount++;
1558
1559 // ASCII "a"
1560 // Latin-1 Supplement "a" + grave
1561 // Cyrillic "a"
1562 String[] patterns = new String[] {
1563 //single
1564 "a", "\u00e0", "\u0430",
1565 //slice
1566 "ab", "\u00e0\u00e1", "\u0430\u0431",
1567 //class single
1568 "[a]", "[\u00e0]", "[\u0430]",
1569 //class range
1570 "[a-b]", "[\u00e0-\u00e5]", "[\u0430-\u0431]",
1571 //back reference
1572 "(a)\\1", "(\u00e0)\\1", "(\u0430)\\1"
1573 };
1574
1575 String[] texts = new String[] {
1576 "A", "\u00c0", "\u0410",
1577 "AB", "\u00c0\u00c1", "\u0410\u0411",
1578 "A", "\u00c0", "\u0410",
1579 "B", "\u00c2", "\u0411",
1580 "aA", "\u00e0\u00c0", "\u0430\u0410"
1581 };
1582
1583 boolean[] expected = new boolean[] {
1584 true, false, false,
1585 true, false, false,
1586 true, false, false,
1587 true, false, false,
1588 true, false, false
1589 };
1590
1591 flags = Pattern.CASE_INSENSITIVE;
1592 for (int i = 0; i < patterns.length; i++) {
1593 pattern = Pattern.compile(patterns[i], flags);
1594 matcher = pattern.matcher(texts[i]);
1595 if (matcher.matches() != expected[i]) {
1596 System.out.println("<1> Failed at " + i);
1597 failCount++;
1598 }
1599 }
1600
1601 flags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
1602 for (int i = 0; i < patterns.length; i++) {
1603 pattern = Pattern.compile(patterns[i], flags);
1604 matcher = pattern.matcher(texts[i]);
1605 if (!matcher.matches()) {
1606 System.out.println("<2> Failed at " + i);
1607 failCount++;
1608 }
1609 }
1610 // flag unicode_case alone should do nothing
1611 flags = Pattern.UNICODE_CASE;
1612 for (int i = 0; i < patterns.length; i++) {
1613 pattern = Pattern.compile(patterns[i], flags);
1614 matcher = pattern.matcher(texts[i]);
1615 if (matcher.matches()) {
1616 System.out.println("<3> Failed at " + i);
1617 failCount++;
1618 }
1619 }
1620
1621 // Special cases: i, I, u+0131 and u+0130
1622 flags = Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE;
1623 pattern = Pattern.compile("[h-j]+", flags);
1624 if (!pattern.matcher("\u0131\u0130").matches())
1625 failCount++;
1626 report("Case Folding");
1627 }
1628
1629 private static void appendTest() {
1630 Pattern pattern = Pattern.compile("(ab)(cd)");
1631 Matcher matcher = pattern.matcher("abcd");
1632 String result = matcher.replaceAll("$2$1");
1633 if (!result.equals("cdab"))
1634 failCount++;
1635
1636 String s1 = "Swap all: first = 123, second = 456";
1637 String s2 = "Swap one: first = 123, second = 456";
1638 String r = "$3$2$1";
1639 pattern = Pattern.compile("([a-z]+)( *= *)([0-9]+)");
1640 matcher = pattern.matcher(s1);
1641
1642 result = matcher.replaceAll(r);
1643 if (!result.equals("Swap all: 123 = first, 456 = second"))
1644 failCount++;
1645
1646 matcher = pattern.matcher(s2);
1647
1648 if (matcher.find()) {
1649 StringBuffer sb = new StringBuffer();
1650 matcher.appendReplacement(sb, r);
1651 matcher.appendTail(sb);
1652 result = sb.toString();
1653 if (!result.equals("Swap one: 123 = first, second = 456"))
1654 failCount++;
1655 }
1656
1657 // Supplementary character test
1658 pattern = Pattern.compile(toSupplementaries("(ab)(cd)"));
1659 matcher = pattern.matcher(toSupplementaries("abcd"));
1660 result = matcher.replaceAll("$2$1");
1661 if (!result.equals(toSupplementaries("cdab")))
1662 failCount++;
1663
1664 s1 = toSupplementaries("Swap all: first = 123, second = 456");
1665 s2 = toSupplementaries("Swap one: first = 123, second = 456");
1666 r = toSupplementaries("$3$2$1");
1667 pattern = Pattern.compile(toSupplementaries("([a-z]+)( *= *)([0-9]+)"));
1668 matcher = pattern.matcher(s1);
1669
1670 result = matcher.replaceAll(r);
1671 if (!result.equals(toSupplementaries("Swap all: 123 = first, 456 = second")))
1672 failCount++;
1673
1674 matcher = pattern.matcher(s2);
1675
1676 if (matcher.find()) {
1677 StringBuffer sb = new StringBuffer();
1678 matcher.appendReplacement(sb, r);
1679 matcher.appendTail(sb);
1680 result = sb.toString();
1681 if (!result.equals(toSupplementaries("Swap one: 123 = first, second = 456")))
1682 failCount++;
1683 }
1684 report("Append");
1685 }
1686
1687 private static void splitTest() {
1688 Pattern pattern = Pattern.compile(":");
1689 String[] result = pattern.split("foo:and:boo", 2);
1690 if (!result[0].equals("foo"))
1691 failCount++;
1692 if (!result[1].equals("and:boo"))
1693 failCount++;
1694 // Supplementary character test
1695 Pattern patternX = Pattern.compile(toSupplementaries("X"));
1696 result = patternX.split(toSupplementaries("fooXandXboo"), 2);
1697 if (!result[0].equals(toSupplementaries("foo")))
1698 failCount++;
1699 if (!result[1].equals(toSupplementaries("andXboo")))
1700 failCount++;
1701
1702 CharBuffer cb = CharBuffer.allocate(100);
1703 cb.put("foo:and:boo");
1704 cb.flip();
1705 result = pattern.split(cb);
1706 if (!result[0].equals("foo"))
1707 failCount++;
1708 if (!result[1].equals("and"))
1709 failCount++;
1710 if (!result[2].equals("boo"))
1711 failCount++;
1712
1713 // Supplementary character test
1714 CharBuffer cbs = CharBuffer.allocate(100);
1715 cbs.put(toSupplementaries("fooXandXboo"));
1716 cbs.flip();
1717 result = patternX.split(cbs);
1718 if (!result[0].equals(toSupplementaries("foo")))
1719 failCount++;
1720 if (!result[1].equals(toSupplementaries("and")))
1721 failCount++;
1722 if (!result[2].equals(toSupplementaries("boo")))
1723 failCount++;
1724
1725 String source = "0123456789";
1726 for (int limit=-2; limit<3; limit++) {
1727 for (int x=0; x<10; x++) {
1728 result = source.split(Integer.toString(x), limit);
1729 int expectedLength = limit < 1 ? 2 : limit;
1730
1731 if ((limit == 0) && (x == 9)) {
1732 // expected dropping of ""
1733 if (result.length != 1)
1734 failCount++;
1735 if (!result[0].equals("012345678")) {
1736 failCount++;
1737 }
1738 } else {
1739 if (result.length != expectedLength) {
1740 failCount++;
1741 }
1742 if (!result[0].equals(source.substring(0,x))) {
1743 if (limit != 1) {
1744 failCount++;
1745 } else {
1746 if (!result[0].equals(source.substring(0,10))) {
1747 failCount++;
1748 }
1749 }
1750 }
1751 if (expectedLength > 1) { // Check segment 2
1752 if (!result[1].equals(source.substring(x+1,10)))
1753 failCount++;
1754 }
1755 }
1756 }
1757 }
1758 // Check the case for no match found
1759 for (int limit=-2; limit<3; limit++) {
1760 result = source.split("e", limit);
1761 if (result.length != 1)
1762 failCount++;
1763 if (!result[0].equals(source))
1764 failCount++;
1765 }
1766 // Check the case for limit == 0, source = "";
1767 source = "";
1768 result = source.split("e", 0);
1769 if (result.length != 1)
1770 failCount++;
1771 if (!result[0].equals(source))
1772 failCount++;
1773
1774 report("Split");
1775 }
1776
1777 private static void negationTest() {
1778 Pattern pattern = Pattern.compile("[\\[@^]+");
1779 Matcher matcher = pattern.matcher("@@@@[[[[^^^^");
1780 if (!matcher.find())
1781 failCount++;
1782 if (!matcher.group(0).equals("@@@@[[[[^^^^"))
1783 failCount++;
1784 pattern = Pattern.compile("[@\\[^]+");
1785 matcher = pattern.matcher("@@@@[[[[^^^^");
1786 if (!matcher.find())
1787 failCount++;
1788 if (!matcher.group(0).equals("@@@@[[[[^^^^"))
1789 failCount++;
1790 pattern = Pattern.compile("[@\\[^@]+");
1791 matcher = pattern.matcher("@@@@[[[[^^^^");
1792 if (!matcher.find())
1793 failCount++;
1794 if (!matcher.group(0).equals("@@@@[[[[^^^^"))
1795 failCount++;
1796
1797 pattern = Pattern.compile("\\)");
1798 matcher = pattern.matcher("xxx)xxx");
1799 if (!matcher.find())
1800 failCount++;
1801
1802 report("Negation");
1803 }
1804
1805 private static void ampersandTest() {
1806 Pattern pattern = Pattern.compile("[&@]+");
1807 check(pattern, "@@@@&&&&", true);
1808
1809 pattern = Pattern.compile("[@&]+");
1810 check(pattern, "@@@@&&&&", true);
1811
1812 pattern = Pattern.compile("[@\\&]+");
1813 check(pattern, "@@@@&&&&", true);
1814
1815 report("Ampersand");
1816 }
1817
1818 private static void octalTest() throws Exception {
1819 Pattern pattern = Pattern.compile("\\u0007");
1820 Matcher matcher = pattern.matcher("\u0007");
1821 if (!matcher.matches())
1822 failCount++;
1823 pattern = Pattern.compile("\\07");
1824 matcher = pattern.matcher("\u0007");
1825 if (!matcher.matches())
1826 failCount++;
1827 pattern = Pattern.compile("\\007");
1828 matcher = pattern.matcher("\u0007");
1829 if (!matcher.matches())
1830 failCount++;
1831 pattern = Pattern.compile("\\0007");
1832 matcher = pattern.matcher("\u0007");
1833 if (!matcher.matches())
1834 failCount++;
1835 pattern = Pattern.compile("\\040");
1836 matcher = pattern.matcher("\u0020");
1837 if (!matcher.matches())
1838 failCount++;
1839 pattern = Pattern.compile("\\0403");
1840 matcher = pattern.matcher("\u00203");
1841 if (!matcher.matches())
1842 failCount++;
1843 pattern = Pattern.compile("\\0103");
1844 matcher = pattern.matcher("\u0043");
1845 if (!matcher.matches())
1846 failCount++;
1847
1848 report("Octal");
1849 }
1850
1851 private static void longPatternTest() throws Exception {
1852 try {
1853 Pattern pattern = Pattern.compile(
1854 "a 32-character-long pattern xxxx");
1855 pattern = Pattern.compile("a 33-character-long pattern xxxxx");
1856 pattern = Pattern.compile("a thirty four character long regex");
1857 StringBuffer patternToBe = new StringBuffer(101);
1858 for (int i=0; i<100; i++)
1859 patternToBe.append((char)(97 + i%26));
1860 pattern = Pattern.compile(patternToBe.toString());
1861 } catch (PatternSyntaxException e) {
1862 failCount++;
1863 }
1864
1865 // Supplementary character test
1866 try {
1867 Pattern pattern = Pattern.compile(
1868 toSupplementaries("a 32-character-long pattern xxxx"));
1869 pattern = Pattern.compile(toSupplementaries("a 33-character-long pattern xxxxx"));
1870 pattern = Pattern.compile(toSupplementaries("a thirty four character long regex"));
1871 StringBuffer patternToBe = new StringBuffer(101*2);
1872 for (int i=0; i<100; i++)
1873 patternToBe.append(Character.toChars(Character.MIN_SUPPLEMENTARY_CODE_POINT
1874 + 97 + i%26));
1875 pattern = Pattern.compile(patternToBe.toString());
1876 } catch (PatternSyntaxException e) {
1877 failCount++;
1878 }
1879 report("LongPattern");
1880 }
1881
1882 private static void group0Test() throws Exception {
1883 Pattern pattern = Pattern.compile("(tes)ting");
1884 Matcher matcher = pattern.matcher("testing");
1885 check(matcher, "testing");
1886
1887 matcher.reset("testing");
1888 if (matcher.lookingAt()) {
1889 if (!matcher.group(0).equals("testing"))
1890 failCount++;
1891 } else {
1892 failCount++;
1893 }
1894
1895 matcher.reset("testing");
1896 if (matcher.matches()) {
1897 if (!matcher.group(0).equals("testing"))
1898 failCount++;
1899 } else {
1900 failCount++;
1901 }
1902
1903 pattern = Pattern.compile("(tes)ting");
1904 matcher = pattern.matcher("testing");
1905 if (matcher.lookingAt()) {
1906 if (!matcher.group(0).equals("testing"))
1907 failCount++;
1908 } else {
1909 failCount++;
1910 }
1911
1912 pattern = Pattern.compile("^(tes)ting");
1913 matcher = pattern.matcher("testing");
1914 if (matcher.matches()) {
1915 if (!matcher.group(0).equals("testing"))
1916 failCount++;
1917 } else {
1918 failCount++;
1919 }
1920
1921 // Supplementary character test
1922 pattern = Pattern.compile(toSupplementaries("(tes)ting"));
1923 matcher = pattern.matcher(toSupplementaries("testing"));
1924 check(matcher, toSupplementaries("testing"));
1925
1926 matcher.reset(toSupplementaries("testing"));
1927 if (matcher.lookingAt()) {
1928 if (!matcher.group(0).equals(toSupplementaries("testing")))
1929 failCount++;
1930 } else {
1931 failCount++;
1932 }
1933
1934 matcher.reset(toSupplementaries("testing"));
1935 if (matcher.matches()) {
1936 if (!matcher.group(0).equals(toSupplementaries("testing")))
1937 failCount++;
1938 } else {
1939 failCount++;
1940 }
1941
1942 pattern = Pattern.compile(toSupplementaries("(tes)ting"));
1943 matcher = pattern.matcher(toSupplementaries("testing"));
1944 if (matcher.lookingAt()) {
1945 if (!matcher.group(0).equals(toSupplementaries("testing")))
1946 failCount++;
1947 } else {
1948 failCount++;
1949 }
1950
1951 pattern = Pattern.compile(toSupplementaries("^(tes)ting"));
1952 matcher = pattern.matcher(toSupplementaries("testing"));
1953 if (matcher.matches()) {
1954 if (!matcher.group(0).equals(toSupplementaries("testing")))
1955 failCount++;
1956 } else {
1957 failCount++;
1958 }
1959
1960 report("Group0");
1961 }
1962
1963 private static void findIntTest() throws Exception {
1964 Pattern p = Pattern.compile("blah");
1965 Matcher m = p.matcher("zzzzblahzzzzzblah");
1966 boolean result = m.find(2);
1967 if (!result)
1968 failCount++;
1969
1970 p = Pattern.compile("$");
1971 m = p.matcher("1234567890");
1972 result = m.find(10);
1973 if (!result)
1974 failCount++;
1975 try {
1976 result = m.find(11);
1977 failCount++;
1978 } catch (IndexOutOfBoundsException e) {
1979 // correct result
1980 }
1981
1982 // Supplementary character test
1983 p = Pattern.compile(toSupplementaries("blah"));
1984 m = p.matcher(toSupplementaries("zzzzblahzzzzzblah"));
1985 result = m.find(2);
1986 if (!result)
1987 failCount++;
1988
1989 report("FindInt");
1990 }
1991
1992 private static void emptyPatternTest() throws Exception {
1993 Pattern p = Pattern.compile("");
1994 Matcher m = p.matcher("foo");
1995
1996 // Should find empty pattern at beginning of input
1997 boolean result = m.find();
1998 if (result != true)
1999 failCount++;
2000 if (m.start() != 0)
2001 failCount++;
2002
2003 // Should not match entire input if input is not empty
2004 m.reset();
2005 result = m.matches();
2006 if (result == true)
2007 failCount++;
2008
2009 try {
2010 m.start(0);
2011 failCount++;
2012 } catch (IllegalStateException e) {
2013 // Correct result
2014 }
2015
2016 // Should match entire input if input is empty
2017 m.reset("");
2018 result = m.matches();
2019 if (result != true)
2020 failCount++;
2021
2022 result = Pattern.matches("", "");
2023 if (result != true)
2024 failCount++;
2025
2026 result = Pattern.matches("", "foo");
2027 if (result == true)
2028 failCount++;
2029 report("EmptyPattern");
2030 }
2031
2032 private static void charClassTest() throws Exception {
2033 Pattern pattern = Pattern.compile("blah[ab]]blech");
2034 check(pattern, "blahb]blech", true);
2035
2036 pattern = Pattern.compile("[abc[def]]");
2037 check(pattern, "b", true);
2038
2039 // Supplementary character tests
2040 pattern = Pattern.compile(toSupplementaries("blah[ab]]blech"));
2041 check(pattern, toSupplementaries("blahb]blech"), true);
2042
2043 pattern = Pattern.compile(toSupplementaries("[abc[def]]"));
2044 check(pattern, toSupplementaries("b"), true);
2045
2046 try {
2047 // u00ff when UNICODE_CASE
2048 pattern = Pattern.compile("[ab\u00ffcd]",
2049 Pattern.CASE_INSENSITIVE|
2050 Pattern.UNICODE_CASE);
2051 check(pattern, "ab\u00ffcd", true);
2052 check(pattern, "Ab\u0178Cd", true);
2053
2054 // u00b5 when UNICODE_CASE
2055 pattern = Pattern.compile("[ab\u00b5cd]",
2056 Pattern.CASE_INSENSITIVE|
2057 Pattern.UNICODE_CASE);
2058 check(pattern, "ab\u00b5cd", true);
2059 check(pattern, "Ab\u039cCd", true);
2060 } catch (Exception e) { failCount++; }
2061
2062 /* Special cases
2063 (1)LatinSmallLetterLongS u+017f
2064 (2)LatinSmallLetterDotlessI u+0131
2065 (3)LatineCapitalLetterIWithDotAbove u+0130
2066 (4)KelvinSign u+212a
2067 (5)AngstromSign u+212b
2068 */
2069 int flags = Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE;
2070 pattern = Pattern.compile("[sik\u00c5]+", flags);
2071 if (!pattern.matcher("\u017f\u0130\u0131\u212a\u212b").matches())
2072 failCount++;
2073
2074 report("CharClass");
2075 }
2076
2077 private static void caretTest() throws Exception {
2078 Pattern pattern = Pattern.compile("\\w*");
2079 Matcher matcher = pattern.matcher("a#bc#def##g");
2080 check(matcher, "a");
2081 check(matcher, "");
2082 check(matcher, "bc");
2083 check(matcher, "");
2084 check(matcher, "def");
2085 check(matcher, "");
2086 check(matcher, "");
2087 check(matcher, "g");
2088 check(matcher, "");
2089 if (matcher.find())
2090 failCount++;
2091
2092 pattern = Pattern.compile("^\\w*");
2093 matcher = pattern.matcher("a#bc#def##g");
2094 check(matcher, "a");
2095 if (matcher.find())
2096 failCount++;
2097
2098 pattern = Pattern.compile("\\w");
2099 matcher = pattern.matcher("abc##x");
2100 check(matcher, "a");
2101 check(matcher, "b");
2102 check(matcher, "c");
2103 check(matcher, "x");
2104 if (matcher.find())
2105 failCount++;
2106
2107 pattern = Pattern.compile("^\\w");
2108 matcher = pattern.matcher("abc##x");
2109 check(matcher, "a");
2110 if (matcher.find())
2111 failCount++;
2112
2113 pattern = Pattern.compile("\\A\\p{Alpha}{3}");
2114 matcher = pattern.matcher("abcdef-ghi\njklmno");
2115 check(matcher, "abc");
2116 if (matcher.find())
2117 failCount++;
2118
2119 pattern = Pattern.compile("^\\p{Alpha}{3}", Pattern.MULTILINE);
2120 matcher = pattern.matcher("abcdef-ghi\njklmno");
2121 check(matcher, "abc");
2122 check(matcher, "jkl");
2123 if (matcher.find())
2124 failCount++;
2125
2126 pattern = Pattern.compile("^", Pattern.MULTILINE);
2127 matcher = pattern.matcher("this is some text");
2128 String result = matcher.replaceAll("X");
2129 if (!result.equals("Xthis is some text"))
2130 failCount++;
2131
2132 pattern = Pattern.compile("^");
2133 matcher = pattern.matcher("this is some text");
2134 result = matcher.replaceAll("X");
2135 if (!result.equals("Xthis is some text"))
2136 failCount++;
2137
2138 pattern = Pattern.compile("^", Pattern.MULTILINE | Pattern.UNIX_LINES);
2139 matcher = pattern.matcher("this is some text\n");
2140 result = matcher.replaceAll("X");
2141 if (!result.equals("Xthis is some text\n"))
2142 failCount++;
2143
2144 report("Caret");
2145 }
2146
2147 private static void groupCaptureTest() throws Exception {
2148 // Independent group
2149 Pattern pattern = Pattern.compile("x+(?>y+)z+");
2150 Matcher matcher = pattern.matcher("xxxyyyzzz");
2151 matcher.find();
2152 try {
2153 String blah = matcher.group(1);
2154 failCount++;
2155 } catch (IndexOutOfBoundsException ioobe) {
2156 // Good result
2157 }
2158 // Pure group
2159 pattern = Pattern.compile("x+(?:y+)z+");
2160 matcher = pattern.matcher("xxxyyyzzz");
2161 matcher.find();
2162 try {
2163 String blah = matcher.group(1);
2164 failCount++;
2165 } catch (IndexOutOfBoundsException ioobe) {
2166 // Good result
2167 }
2168
2169 // Supplementary character tests
2170 // Independent group
2171 pattern = Pattern.compile(toSupplementaries("x+(?>y+)z+"));
2172 matcher = pattern.matcher(toSupplementaries("xxxyyyzzz"));
2173 matcher.find();
2174 try {
2175 String blah = matcher.group(1);
2176 failCount++;
2177 } catch (IndexOutOfBoundsException ioobe) {
2178 // Good result
2179 }
2180 // Pure group
2181 pattern = Pattern.compile(toSupplementaries("x+(?:y+)z+"));
2182 matcher = pattern.matcher(toSupplementaries("xxxyyyzzz"));
2183 matcher.find();
2184 try {
2185 String blah = matcher.group(1);
2186 failCount++;
2187 } catch (IndexOutOfBoundsException ioobe) {
2188 // Good result
2189 }
2190
2191 report("GroupCapture");
2192 }
2193
2194 private static void backRefTest() throws Exception {
2195 Pattern pattern = Pattern.compile("(a*)bc\\1");
2196 check(pattern, "zzzaabcazzz", true);
2197
2198 pattern = Pattern.compile("(a*)bc\\1");
2199 check(pattern, "zzzaabcaazzz", true);
2200
2201 pattern = Pattern.compile("(abc)(def)\\1");
2202 check(pattern, "abcdefabc", true);
2203
2204 pattern = Pattern.compile("(abc)(def)\\3");
2205 check(pattern, "abcdefabc", false);
2206
2207 try {
2208 for (int i = 1; i < 10; i++) {
2209 // Make sure backref 1-9 are always accepted
2210 pattern = Pattern.compile("abcdef\\" + i);
2211 // and fail to match if the target group does not exit
2212 check(pattern, "abcdef", false);
2213 }
2214 } catch(PatternSyntaxException e) {
2215 failCount++;
2216 }
2217
2218 pattern = Pattern.compile("(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)\\11");
2219 check(pattern, "abcdefghija", false);
2220 check(pattern, "abcdefghija1", true);
2221
2222 pattern = Pattern.compile("(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)\\11");
2223 check(pattern, "abcdefghijkk", true);
2224
2225 pattern = Pattern.compile("(a)bcdefghij\\11");
2226 check(pattern, "abcdefghija1", true);
2227
2228 // Supplementary character tests
2229 pattern = Pattern.compile(toSupplementaries("(a*)bc\\1"));
2230 check(pattern, toSupplementaries("zzzaabcazzz"), true);
2231
2232 pattern = Pattern.compile(toSupplementaries("(a*)bc\\1"));
2233 check(pattern, toSupplementaries("zzzaabcaazzz"), true);
2234
2235 pattern = Pattern.compile(toSupplementaries("(abc)(def)\\1"));
2236 check(pattern, toSupplementaries("abcdefabc"), true);
2237
2238 pattern = Pattern.compile(toSupplementaries("(abc)(def)\\3"));
2239 check(pattern, toSupplementaries("abcdefabc"), false);
2240
2241 pattern = Pattern.compile(toSupplementaries("(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)\\11"));
2242 check(pattern, toSupplementaries("abcdefghija"), false);
2243 check(pattern, toSupplementaries("abcdefghija1"), true);
2244
2245 pattern = Pattern.compile(toSupplementaries("(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)\\11"));
2246 check(pattern, toSupplementaries("abcdefghijkk"), true);
2247
2248 report("BackRef");
2249 }
2250
2251 /**
2252 * Unicode Technical Report #18, section 2.6 End of Line
2253 * There is no empty line to be matched in the sequence \u000D\u000A
2254 * but there is an empty line in the sequence \u000A\u000D.
2255 */
2256 private static void anchorTest() throws Exception {
2257 Pattern p = Pattern.compile("^.*$", Pattern.MULTILINE);
2258 Matcher m = p.matcher("blah1\r\nblah2");
2259 m.find();
2260 m.find();
2261 if (!m.group().equals("blah2"))
2262 failCount++;
2263
2264 m.reset("blah1\n\rblah2");
2265 m.find();
2266 m.find();
2267 m.find();
2268 if (!m.group().equals("blah2"))
2269 failCount++;
2270
2271 // Test behavior of $ with \r\n at end of input
2272 p = Pattern.compile(".+$");
2273 m = p.matcher("blah1\r\n");
2274 if (!m.find())
2275 failCount++;
2276 if (!m.group().equals("blah1"))
2277 failCount++;
2278 if (m.find())
2279 failCount++;
2280
2281 // Test behavior of $ with \r\n at end of input in multiline
2282 p = Pattern.compile(".+$", Pattern.MULTILINE);
2283 m = p.matcher("blah1\r\n");
2284 if (!m.find())
2285 failCount++;
2286 if (m.find())
2287 failCount++;
2288
2289 // Test for $ recognition of \u0085 for bug 4527731
2290 p = Pattern.compile(".+$", Pattern.MULTILINE);
2291 m = p.matcher("blah1\u0085");
2292 if (!m.find())
2293 failCount++;
2294
2295 // Supplementary character test
2296 p = Pattern.compile("^.*$", Pattern.MULTILINE);
2297 m = p.matcher(toSupplementaries("blah1\r\nblah2"));
2298 m.find();
2299 m.find();
2300 if (!m.group().equals(toSupplementaries("blah2")))
2301 failCount++;
2302
2303 m.reset(toSupplementaries("blah1\n\rblah2"));
2304 m.find();
2305 m.find();
2306 m.find();
2307 if (!m.group().equals(toSupplementaries("blah2")))
2308 failCount++;
2309
2310 // Test behavior of $ with \r\n at end of input
2311 p = Pattern.compile(".+$");
2312 m = p.matcher(toSupplementaries("blah1\r\n"));
2313 if (!m.find())
2314 failCount++;
2315 if (!m.group().equals(toSupplementaries("blah1")))
2316 failCount++;
2317 if (m.find())
2318 failCount++;
2319
2320 // Test behavior of $ with \r\n at end of input in multiline
2321 p = Pattern.compile(".+$", Pattern.MULTILINE);
2322 m = p.matcher(toSupplementaries("blah1\r\n"));
2323 if (!m.find())
2324 failCount++;
2325 if (m.find())
2326 failCount++;
2327
2328 // Test for $ recognition of \u0085 for bug 4527731
2329 p = Pattern.compile(".+$", Pattern.MULTILINE);
2330 m = p.matcher(toSupplementaries("blah1\u0085"));
2331 if (!m.find())
2332 failCount++;
2333
2334 report("Anchors");
2335 }
2336
2337 /**
2338 * A basic sanity test of Matcher.lookingAt().
2339 */
2340 private static void lookingAtTest() throws Exception {
2341 Pattern p = Pattern.compile("(ab)(c*)");
2342 Matcher m = p.matcher("abccczzzabcczzzabccc");
2343
2344 if (!m.lookingAt())
2345 failCount++;
2346
2347 if (!m.group().equals(m.group(0)))
2348 failCount++;
2349
2350 m = p.matcher("zzzabccczzzabcczzzabccczzz");
2351 if (m.lookingAt())
2352 failCount++;
2353
2354 // Supplementary character test
2355 p = Pattern.compile(toSupplementaries("(ab)(c*)"));
2356 m = p.matcher(toSupplementaries("abccczzzabcczzzabccc"));
2357
2358 if (!m.lookingAt())
2359 failCount++;
2360
2361 if (!m.group().equals(m.group(0)))
2362 failCount++;
2363
2364 m = p.matcher(toSupplementaries("zzzabccczzzabcczzzabccczzz"));
2365 if (m.lookingAt())
2366 failCount++;
2367
2368 report("Looking At");
2369 }
2370
2371 /**
2372 * A basic sanity test of Matcher.matches().
2373 */
2374 private static void matchesTest() throws Exception {
2375 // matches()
2376 Pattern p = Pattern.compile("ulb(c*)");
2377 Matcher m = p.matcher("ulbcccccc");
2378 if (!m.matches())
2379 failCount++;
2380
2381 // find() but not matches()
2382 m.reset("zzzulbcccccc");
2383 if (m.matches())
2384 failCount++;
2385
2386 // lookingAt() but not matches()
2387 m.reset("ulbccccccdef");
2388 if (m.matches())
2389 failCount++;
2390
2391 // matches()
2392 p = Pattern.compile("a|ad");
2393 m = p.matcher("ad");
2394 if (!m.matches())
2395 failCount++;
2396
2397 // Supplementary character test
2398 // matches()
2399 p = Pattern.compile(toSupplementaries("ulb(c*)"));
2400 m = p.matcher(toSupplementaries("ulbcccccc"));
2401 if (!m.matches())
2402 failCount++;
2403
2404 // find() but not matches()
2405 m.reset(toSupplementaries("zzzulbcccccc"));
2406 if (m.matches())
2407 failCount++;
2408
2409 // lookingAt() but not matches()
2410 m.reset(toSupplementaries("ulbccccccdef"));
2411 if (m.matches())
2412 failCount++;
2413
2414 // matches()
2415 p = Pattern.compile(toSupplementaries("a|ad"));
2416 m = p.matcher(toSupplementaries("ad"));
2417 if (!m.matches())
2418 failCount++;
2419
2420 report("Matches");
2421 }
2422
2423 /**
2424 * A basic sanity test of Pattern.matches().
2425 */
2426 private static void patternMatchesTest() throws Exception {
2427 // matches()
2428 if (!Pattern.matches(toSupplementaries("ulb(c*)"),
2429 toSupplementaries("ulbcccccc")))
2430 failCount++;
2431
2432 // find() but not matches()
2433 if (Pattern.matches(toSupplementaries("ulb(c*)"),
2434 toSupplementaries("zzzulbcccccc")))
2435 failCount++;
2436
2437 // lookingAt() but not matches()
2438 if (Pattern.matches(toSupplementaries("ulb(c*)"),
2439 toSupplementaries("ulbccccccdef")))
2440 failCount++;
2441
2442 // Supplementary character test
2443 // matches()
2444 if (!Pattern.matches(toSupplementaries("ulb(c*)"),
2445 toSupplementaries("ulbcccccc")))
2446 failCount++;
2447
2448 // find() but not matches()
2449 if (Pattern.matches(toSupplementaries("ulb(c*)"),
2450 toSupplementaries("zzzulbcccccc")))
2451 failCount++;
2452
2453 // lookingAt() but not matches()
2454 if (Pattern.matches(toSupplementaries("ulb(c*)"),
2455 toSupplementaries("ulbccccccdef")))
2456 failCount++;
2457
2458 report("Pattern Matches");
2459 }
2460
2461 /**
2462 * Canonical equivalence testing. Tests the ability of the engine
2463 * to match sequences that are not explicitly specified in the
2464 * pattern when they are considered equivalent by the Unicode Standard.
2465 */
2466 private static void ceTest() throws Exception {
2467 // Decomposed char outside char classes
2468 Pattern p = Pattern.compile("testa\u030a", Pattern.CANON_EQ);
2469 Matcher m = p.matcher("test\u00e5");
2470 if (!m.matches())
2471 failCount++;
2472
2473 m.reset("testa\u030a");
2474 if (!m.matches())
2475 failCount++;
2476
2477 // Composed char outside char classes
2478 p = Pattern.compile("test\u00e5", Pattern.CANON_EQ);
2479 m = p.matcher("test\u00e5");
2480 if (!m.matches())
2481 failCount++;
2482
2483 m.reset("testa\u030a");
2484 if (!m.find())
2485 failCount++;
2486
2487 // Decomposed char inside a char class
2488 p = Pattern.compile("test[abca\u030a]", Pattern.CANON_EQ);
2489 m = p.matcher("test\u00e5");
2490 if (!m.find())
2491 failCount++;
2492
2493 m.reset("testa\u030a");
2494 if (!m.find())
2495 failCount++;
2496
2497 // Composed char inside a char class
2498 p = Pattern.compile("test[abc\u00e5def\u00e0]", Pattern.CANON_EQ);
2499 m = p.matcher("test\u00e5");
2500 if (!m.find())
2501 failCount++;
2502
2503 m.reset("testa\u0300");
2504 if (!m.find())
2505 failCount++;
2506
2507 m.reset("testa\u030a");
2508 if (!m.find())
2509 failCount++;
2510
2511 // Marks that cannot legally change order and be equivalent
2512 p = Pattern.compile("testa\u0308\u0300", Pattern.CANON_EQ);
2513 check(p, "testa\u0308\u0300", true);
2514 check(p, "testa\u0300\u0308", false);
2515
2516 // Marks that can legally change order and be equivalent
2517 p = Pattern.compile("testa\u0308\u0323", Pattern.CANON_EQ);
2518 check(p, "testa\u0308\u0323", true);
2519 check(p, "testa\u0323\u0308", true);
2520
2521 // Test all equivalences of the sequence a\u0308\u0323\u0300
2522 p = Pattern.compile("testa\u0308\u0323\u0300", Pattern.CANON_EQ);
2523 check(p, "testa\u0308\u0323\u0300", true);
2524 check(p, "testa\u0323\u0308\u0300", true);
2525 check(p, "testa\u0308\u0300\u0323", true);
2526 check(p, "test\u00e4\u0323\u0300", true);
2527 check(p, "test\u00e4\u0300\u0323", true);
2528
2529 /*
2530 * The following canonical equivalence tests don't work. Bug id: 4916384.
2531 *
2532 // Decomposed hangul (jamos)
2533 p = Pattern.compile("\u1100\u1161", Pattern.CANON_EQ);
2534 m = p.matcher("\u1100\u1161");
2535 if (!m.matches())
2536 failCount++;
2537
2538 m.reset("\uac00");
2539 if (!m.matches())
2540 failCount++;
2541
2542 // Composed hangul
2543 p = Pattern.compile("\uac00", Pattern.CANON_EQ);
2544 m = p.matcher("\u1100\u1161");
2545 if (!m.matches())
2546 failCount++;
2547
2548 m.reset("\uac00");
2549 if (!m.matches())
2550 failCount++;
2551
2552 // Decomposed supplementary outside char classes
2553 p = Pattern.compile("test\ud834\uddbc\ud834\udd6f", Pattern.CANON_EQ);
2554 m = p.matcher("test\ud834\uddc0");
2555 if (!m.matches())
2556 failCount++;
2557
2558 m.reset("test\ud834\uddbc\ud834\udd6f");
2559 if (!m.matches())
2560 failCount++;
2561
2562 // Composed supplementary outside char classes
2563 p = Pattern.compile("test\ud834\uddc0", Pattern.CANON_EQ);
2564 m.reset("test\ud834\uddbc\ud834\udd6f");
2565 if (!m.matches())
2566 failCount++;
2567
2568 m = p.matcher("test\ud834\uddc0");
2569 if (!m.matches())
2570 failCount++;
2571
2572 */
2573
2574 report("Canonical Equivalence");
2575 }
2576
2577 /**
2578 * A basic sanity test of Matcher.replaceAll().
2579 */
2580 private static void globalSubstitute() throws Exception {
2581 // Global substitution with a literal
2582 Pattern p = Pattern.compile("(ab)(c*)");
2583 Matcher m = p.matcher("abccczzzabcczzzabccc");
2584 if (!m.replaceAll("test").equals("testzzztestzzztest"))
2585 failCount++;
2586
2587 m.reset("zzzabccczzzabcczzzabccczzz");
2588 if (!m.replaceAll("test").equals("zzztestzzztestzzztestzzz"))
2589 failCount++;
2590
2591 // Global substitution with groups
2592 m.reset("zzzabccczzzabcczzzabccczzz");
2593 String result = m.replaceAll("$1");
2594 if (!result.equals("zzzabzzzabzzzabzzz"))
2595 failCount++;
2596
2597 // Supplementary character test
2598 // Global substitution with a literal
2599 p = Pattern.compile(toSupplementaries("(ab)(c*)"));
2600 m = p.matcher(toSupplementaries("abccczzzabcczzzabccc"));
2601 if (!m.replaceAll(toSupplementaries("test")).
2602 equals(toSupplementaries("testzzztestzzztest")))
2603 failCount++;
2604
2605 m.reset(toSupplementaries("zzzabccczzzabcczzzabccczzz"));
2606 if (!m.replaceAll(toSupplementaries("test")).
2607 equals(toSupplementaries("zzztestzzztestzzztestzzz")))
2608 failCount++;
2609
2610 // Global substitution with groups
2611 m.reset(toSupplementaries("zzzabccczzzabcczzzabccczzz"));
2612 result = m.replaceAll("$1");
2613 if (!result.equals(toSupplementaries("zzzabzzzabzzzabzzz")))
2614 failCount++;
2615
2616 report("Global Substitution");
2617 }
2618
2619 /**
2620 * Tests the usage of Matcher.appendReplacement() with literal
2621 * and group substitutions.
2622 */
2623 private static void stringbufferSubstitute() throws Exception {
2624 // SB substitution with literal
2625 String blah = "zzzblahzzz";
2626 Pattern p = Pattern.compile("blah");
2627 Matcher m = p.matcher(blah);
2628 StringBuffer result = new StringBuffer();
2629 try {
2630 m.appendReplacement(result, "blech");
2631 failCount++;
2632 } catch (IllegalStateException e) {
2633 }
2634 m.find();
2635 m.appendReplacement(result, "blech");
2636 if (!result.toString().equals("zzzblech"))
2637 failCount++;
2638
2639 m.appendTail(result);
2640 if (!result.toString().equals("zzzblechzzz"))
2641 failCount++;
2642
2643 // SB substitution with groups
2644 blah = "zzzabcdzzz";
2645 p = Pattern.compile("(ab)(cd)*");
2646 m = p.matcher(blah);
2647 result = new StringBuffer();
2648 try {
2649 m.appendReplacement(result, "$1");
2650 failCount++;
2651 } catch (IllegalStateException e) {
2652 }
2653 m.find();
2654 m.appendReplacement(result, "$1");
2655 if (!result.toString().equals("zzzab"))
2656 failCount++;
2657
2658 m.appendTail(result);
2659 if (!result.toString().equals("zzzabzzz"))
2660 failCount++;
2661
2662 // SB substitution with 3 groups
2663 blah = "zzzabcdcdefzzz";
2664 p = Pattern.compile("(ab)(cd)*(ef)");
2665 m = p.matcher(blah);
2666 result = new StringBuffer();
2667 try {
2668 m.appendReplacement(result, "$1w$2w$3");
2669 failCount++;
2670 } catch (IllegalStateException e) {
2671 }
2672 m.find();
2673 m.appendReplacement(result, "$1w$2w$3");
2674 if (!result.toString().equals("zzzabwcdwef"))
2675 failCount++;
2676
2677 m.appendTail(result);
2678 if (!result.toString().equals("zzzabwcdwefzzz"))
2679 failCount++;
2680
2681 // SB substitution with groups and three matches
2682 // skipping middle match
2683 blah = "zzzabcdzzzabcddzzzabcdzzz";
2684 p = Pattern.compile("(ab)(cd*)");
2685 m = p.matcher(blah);
2686 result = new StringBuffer();
2687 try {
2688 m.appendReplacement(result, "$1");
2689 failCount++;
2690 } catch (IllegalStateException e) {
2691 }
2692 m.find();
2693 m.appendReplacement(result, "$1");
2694 if (!result.toString().equals("zzzab"))
2695 failCount++;
2696
2697 m.find();
2698 m.find();
2699 m.appendReplacement(result, "$2");
2700 if (!result.toString().equals("zzzabzzzabcddzzzcd"))
2701 failCount++;
2702
2703 m.appendTail(result);
2704 if (!result.toString().equals("zzzabzzzabcddzzzcdzzz"))
2705 failCount++;
2706
2707 // Check to make sure escaped $ is ignored
2708 blah = "zzzabcdcdefzzz";
2709 p = Pattern.compile("(ab)(cd)*(ef)");
2710 m = p.matcher(blah);
2711 result = new StringBuffer();
2712 m.find();
2713 m.appendReplacement(result, "$1w\\$2w$3");
2714 if (!result.toString().equals("zzzabw$2wef"))
2715 failCount++;
2716
2717 m.appendTail(result);
2718 if (!result.toString().equals("zzzabw$2wefzzz"))
2719 failCount++;
2720
2721 // Check to make sure a reference to nonexistent group causes error
2722 blah = "zzzabcdcdefzzz";
2723 p = Pattern.compile("(ab)(cd)*(ef)");
2724 m = p.matcher(blah);
2725 result = new StringBuffer();
2726 m.find();
2727 try {
2728 m.appendReplacement(result, "$1w$5w$3");
2729 failCount++;
2730 } catch (IndexOutOfBoundsException ioobe) {
2731 // Correct result
2732 }
2733
2734 // Check double digit group references
2735 blah = "zzz123456789101112zzz";
2736 p = Pattern.compile("(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)");
2737 m = p.matcher(blah);
2738 result = new StringBuffer();
2739 m.find();
2740 m.appendReplacement(result, "$1w$11w$3");
2741 if (!result.toString().equals("zzz1w11w3"))
2742 failCount++;
2743
2744 // Check to make sure it backs off $15 to $1 if only three groups
2745 blah = "zzzabcdcdefzzz";
2746 p = Pattern.compile("(ab)(cd)*(ef)");
2747 m = p.matcher(blah);
2748 result = new StringBuffer();
2749 m.find();
2750 m.appendReplacement(result, "$1w$15w$3");
2751 if (!result.toString().equals("zzzabwab5wef"))
2752 failCount++;
2753
2754
2755 // Supplementary character test
2756 // SB substitution with literal
2757 blah = toSupplementaries("zzzblahzzz");
2758 p = Pattern.compile(toSupplementaries("blah"));
2759 m = p.matcher(blah);
2760 result = new StringBuffer();
2761 try {
2762 m.appendReplacement(result, toSupplementaries("blech"));
2763 failCount++;
2764 } catch (IllegalStateException e) {
2765 }
2766 m.find();
2767 m.appendReplacement(result, toSupplementaries("blech"));
2768 if (!result.toString().equals(toSupplementaries("zzzblech")))
2769 failCount++;
2770
2771 m.appendTail(result);
2772 if (!result.toString().equals(toSupplementaries("zzzblechzzz")))
2773 failCount++;
2774
2775 // SB substitution with groups
2776 blah = toSupplementaries("zzzabcdzzz");
2777 p = Pattern.compile(toSupplementaries("(ab)(cd)*"));
2778 m = p.matcher(blah);
2779 result = new StringBuffer();
2780 try {
2781 m.appendReplacement(result, "$1");
2782 failCount++;
2783 } catch (IllegalStateException e) {
2784 }
2785 m.find();
2786 m.appendReplacement(result, "$1");
2787 if (!result.toString().equals(toSupplementaries("zzzab")))
2788 failCount++;
2789
2790 m.appendTail(result);
2791 if (!result.toString().equals(toSupplementaries("zzzabzzz")))
2792 failCount++;
2793
2794 // SB substitution with 3 groups
2795 blah = toSupplementaries("zzzabcdcdefzzz");
2796 p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
2797 m = p.matcher(blah);
2798 result = new StringBuffer();
2799 try {
2800 m.appendReplacement(result, toSupplementaries("$1w$2w$3"));
2801 failCount++;
2802 } catch (IllegalStateException e) {
2803 }
2804 m.find();
2805 m.appendReplacement(result, toSupplementaries("$1w$2w$3"));
2806 if (!result.toString().equals(toSupplementaries("zzzabwcdwef")))
2807 failCount++;
2808
2809 m.appendTail(result);
2810 if (!result.toString().equals(toSupplementaries("zzzabwcdwefzzz")))
2811 failCount++;
2812
2813 // SB substitution with groups and three matches
2814 // skipping middle match
2815 blah = toSupplementaries("zzzabcdzzzabcddzzzabcdzzz");
2816 p = Pattern.compile(toSupplementaries("(ab)(cd*)"));
2817 m = p.matcher(blah);
2818 result = new StringBuffer();
2819 try {
2820 m.appendReplacement(result, "$1");
2821 failCount++;
2822 } catch (IllegalStateException e) {
2823 }
2824 m.find();
2825 m.appendReplacement(result, "$1");
2826 if (!result.toString().equals(toSupplementaries("zzzab")))
2827 failCount++;
2828
2829 m.find();
2830 m.find();
2831 m.appendReplacement(result, "$2");
2832 if (!result.toString().equals(toSupplementaries("zzzabzzzabcddzzzcd")))
2833 failCount++;
2834
2835 m.appendTail(result);
2836 if (!result.toString().equals(toSupplementaries("zzzabzzzabcddzzzcdzzz")))
2837 failCount++;
2838
2839 // Check to make sure escaped $ is ignored
2840 blah = toSupplementaries("zzzabcdcdefzzz");
2841 p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
2842 m = p.matcher(blah);
2843 result = new StringBuffer();
2844 m.find();
2845 m.appendReplacement(result, toSupplementaries("$1w\\$2w$3"));
2846 if (!result.toString().equals(toSupplementaries("zzzabw$2wef")))
2847 failCount++;
2848
2849 m.appendTail(result);
2850 if (!result.toString().equals(toSupplementaries("zzzabw$2wefzzz")))
2851 failCount++;
2852
2853 // Check to make sure a reference to nonexistent group causes error
2854 blah = toSupplementaries("zzzabcdcdefzzz");
2855 p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
2856 m = p.matcher(blah);
2857 result = new StringBuffer();
2858 m.find();
2859 try {
2860 m.appendReplacement(result, toSupplementaries("$1w$5w$3"));
2861 failCount++;
2862 } catch (IndexOutOfBoundsException ioobe) {
2863 // Correct result
2864 }
2865
2866 // Check double digit group references
2867 blah = toSupplementaries("zzz123456789101112zzz");
2868 p = Pattern.compile("(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)");
2869 m = p.matcher(blah);
2870 result = new StringBuffer();
2871 m.find();
2872 m.appendReplacement(result, toSupplementaries("$1w$11w$3"));
2873 if (!result.toString().equals(toSupplementaries("zzz1w11w3")))
2874 failCount++;
2875
2876 // Check to make sure it backs off $15 to $1 if only three groups
2877 blah = toSupplementaries("zzzabcdcdefzzz");
2878 p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
2879 m = p.matcher(blah);
2880 result = new StringBuffer();
2881 m.find();
2882 m.appendReplacement(result, toSupplementaries("$1w$15w$3"));
2883 if (!result.toString().equals(toSupplementaries("zzzabwab5wef")))
2884 failCount++;
2885
2886 // Check nothing has been appended into the output buffer if
2887 // the replacement string triggers IllegalArgumentException.
2888 p = Pattern.compile("(abc)");
2889 m = p.matcher("abcd");
2890 result = new StringBuffer();
2891 m.find();
2892 try {
2893 m.appendReplacement(result, ("xyz$g"));
2894 failCount++;
2895 } catch (IllegalArgumentException iae) {
2896 if (result.length() != 0)
2897 failCount++;
2898 }
2899
2900 report("SB Substitution");
2901 }
2902
2903 /*
2904 * 5 groups of characters are created to make a substitution string.
2905 * A base string will be created including random lead chars, the
2906 * substitution string, and random trailing chars.
2907 * A pattern containing the 5 groups is searched for and replaced with:
2908 * random group + random string + random group.
2909 * The results are checked for correctness.
2910 */
2911 private static void substitutionBasher() {
2912 for (int runs = 0; runs<1000; runs++) {
2913 // Create a base string to work in
2914 int leadingChars = generator.nextInt(10);
2915 StringBuffer baseBuffer = new StringBuffer(100);
2916 String leadingString = getRandomAlphaString(leadingChars);
2917 baseBuffer.append(leadingString);
2918
2919 // Create 5 groups of random number of random chars
2920 // Create the string to substitute
2921 // Create the pattern string to search for
2922 StringBuffer bufferToSub = new StringBuffer(25);
2923 StringBuffer bufferToPat = new StringBuffer(50);
2924 String[] groups = new String[5];
2925 for(int i=0; i<5; i++) {
2926 int aGroupSize = generator.nextInt(5)+1;
2927 groups[i] = getRandomAlphaString(aGroupSize);
2928 bufferToSub.append(groups[i]);
2929 bufferToPat.append('(');
2930 bufferToPat.append(groups[i]);
2931 bufferToPat.append(')');
2932 }
2933 String stringToSub = bufferToSub.toString();
2934 String pattern = bufferToPat.toString();
2935
2936 // Place sub string into working string at random index
2937 baseBuffer.append(stringToSub);
2938
2939 // Append random chars to end
2940 int trailingChars = generator.nextInt(10);
2941 String trailingString = getRandomAlphaString(trailingChars);
2942 baseBuffer.append(trailingString);
2943 String baseString = baseBuffer.toString();
2944
2945 // Create test pattern and matcher
2946 Pattern p = Pattern.compile(pattern);
2947 Matcher m = p.matcher(baseString);
2948
2949 // Reject candidate if pattern happens to start early
2950 m.find();
2951 if (m.start() < leadingChars)
2952 continue;
2953
2954 // Reject candidate if more than one match
2955 if (m.find())
2956 continue;
2957
2958 // Construct a replacement string with :
2959 // random group + random string + random group
2960 StringBuffer bufferToRep = new StringBuffer();
2961 int groupIndex1 = generator.nextInt(5);
2962 bufferToRep.append("$" + (groupIndex1 + 1));
2963 String randomMidString = getRandomAlphaString(5);
2964 bufferToRep.append(randomMidString);
2965 int groupIndex2 = generator.nextInt(5);
2966 bufferToRep.append("$" + (groupIndex2 + 1));
2967 String replacement = bufferToRep.toString();
2968
2969 // Do the replacement
2970 String result = m.replaceAll(replacement);
2971
2972 // Construct expected result
2973 StringBuffer bufferToRes = new StringBuffer();
2974 bufferToRes.append(leadingString);
2975 bufferToRes.append(groups[groupIndex1]);
2976 bufferToRes.append(randomMidString);
2977 bufferToRes.append(groups[groupIndex2]);
2978 bufferToRes.append(trailingString);
2979 String expectedResult = bufferToRes.toString();
2980
2981 // Check results
2982 if (!result.equals(expectedResult))
2983 failCount++;
2984 }
2985
2986 report("Substitution Basher");
2987 }
2988
2989 /**
2990 * Checks the handling of some escape sequences that the Pattern
2991 * class should process instead of the java compiler. These are
2992 * not in the file because the escapes should be be processed
2993 * by the Pattern class when the regex is compiled.
2994 */
2995 private static void escapes() throws Exception {
2996 Pattern p = Pattern.compile("\\043");
2997 Matcher m = p.matcher("#");
2998 if (!m.find())
2999 failCount++;
3000
3001 p = Pattern.compile("\\x23");
3002 m = p.matcher("#");
3003 if (!m.find())
3004 failCount++;
3005
3006 p = Pattern.compile("\\u0023");
3007 m = p.matcher("#");
3008 if (!m.find())
3009 failCount++;
3010
3011 report("Escape sequences");
3012 }
3013
3014 /**
3015 * Checks the handling of blank input situations. These
3016 * tests are incompatible with my test file format.
3017 */
3018 private static void blankInput() throws Exception {
3019 Pattern p = Pattern.compile("abc", Pattern.CASE_INSENSITIVE);
3020 Matcher m = p.matcher("");
3021 if (m.find())
3022 failCount++;
3023
3024 p = Pattern.compile("a*", Pattern.CASE_INSENSITIVE);
3025 m = p.matcher("");
3026 if (!m.find())
3027 failCount++;
3028
3029 p = Pattern.compile("abc");
3030 m = p.matcher("");
3031 if (m.find())
3032 failCount++;
3033
3034 p = Pattern.compile("a*");
3035 m = p.matcher("");
3036 if (!m.find())
3037 failCount++;
3038
3039 report("Blank input");
3040 }
3041
3042 /**
3043 * Tests the Boyer-Moore pattern matching of a character sequence
3044 * on randomly generated patterns.
3045 */
3046 private static void bm() throws Exception {
3047 doBnM('a');
3048 report("Boyer Moore (ASCII)");
3049
3050 doBnM(Character.MIN_SUPPLEMENTARY_CODE_POINT - 10);
3051 report("Boyer Moore (Supplementary)");
3052 }
3053
3054 private static void doBnM(int baseCharacter) throws Exception {
3055 int achar=0;
3056
3057 for (int i=0; i<100; i++) {
3058 // Create a short pattern to search for
3059 int patternLength = generator.nextInt(7) + 4;
3060 StringBuffer patternBuffer = new StringBuffer(patternLength);
3061 for (int x=0; x<patternLength; x++) {
3062 int ch = baseCharacter + generator.nextInt(26);
3063 if (Character.isSupplementaryCodePoint(ch)) {
3064 patternBuffer.append(Character.toChars(ch));
3065 } else {
3066 patternBuffer.append((char)ch);
3067 }
3068 }
3069 String pattern = patternBuffer.toString();
3070 Pattern p = Pattern.compile(pattern);
3071
3072 // Create a buffer with random ASCII chars that does
3073 // not match the sample
3074 String toSearch = null;
3075 StringBuffer s = null;
3076 Matcher m = p.matcher("");
3077 do {
3078 s = new StringBuffer(100);
3079 for (int x=0; x<100; x++) {
3080 int ch = baseCharacter + generator.nextInt(26);
3081 if (Character.isSupplementaryCodePoint(ch)) {
3082 s.append(Character.toChars(ch));
3083 } else {
3084 s.append((char)ch);
3085 }
3086 }
3087 toSearch = s.toString();
3088 m.reset(toSearch);
3089 } while (m.find());
3090
3091 // Insert the pattern at a random spot
3092 int insertIndex = generator.nextInt(99);
3093 if (Character.isLowSurrogate(s.charAt(insertIndex)))
3094 insertIndex++;
3095 s = s.insert(insertIndex, pattern);
3096 toSearch = s.toString();
3097
3098 // Make sure that the pattern is found
3099 m.reset(toSearch);
3100 if (!m.find())
3101 failCount++;
3102
3103 // Make sure that the match text is the pattern
3104 if (!m.group().equals(pattern))
3105 failCount++;
3106
3107 // Make sure match occured at insertion point
3108 if (m.start() != insertIndex)
3109 failCount++;
3110 }
3111 }
3112
3113 /**
3114 * Tests the matching of slices on randomly generated patterns.
3115 * The Boyer-Moore optimization is not done on these patterns
3116 * because it uses unicode case folding.
3117 */
3118 private static void slice() throws Exception {
3119 doSlice(Character.MAX_VALUE);
3120 report("Slice");
3121
3122 doSlice(Character.MAX_CODE_POINT);
3123 report("Slice (Supplementary)");
3124 }
3125
3126 private static void doSlice(int maxCharacter) throws Exception {
3127 Random generator = new Random();
3128 int achar=0;
3129
3130 for (int i=0; i<100; i++) {
3131 // Create a short pattern to search for
3132 int patternLength = generator.nextInt(7) + 4;
3133 StringBuffer patternBuffer = new StringBuffer(patternLength);
3134 for (int x=0; x<patternLength; x++) {
3135 int randomChar = 0;
3136 while (!Character.isLetterOrDigit(randomChar))
3137 randomChar = generator.nextInt(maxCharacter);
3138 if (Character.isSupplementaryCodePoint(randomChar)) {
3139 patternBuffer.append(Character.toChars(randomChar));
3140 } else {
3141 patternBuffer.append((char) randomChar);
3142 }
3143 }
3144 String pattern = patternBuffer.toString();
3145 Pattern p = Pattern.compile(pattern, Pattern.UNICODE_CASE);
3146
3147 // Create a buffer with random chars that does not match the sample
3148 String toSearch = null;
3149 StringBuffer s = null;
3150 Matcher m = p.matcher("");
3151 do {
3152 s = new StringBuffer(100);
3153 for (int x=0; x<100; x++) {
3154 int randomChar = 0;
3155 while (!Character.isLetterOrDigit(randomChar))
3156 randomChar = generator.nextInt(maxCharacter);
3157 if (Character.isSupplementaryCodePoint(randomChar)) {
3158 s.append(Character.toChars(randomChar));
3159 } else {
3160 s.append((char) randomChar);
3161 }
3162 }
3163 toSearch = s.toString();
3164 m.reset(toSearch);
3165 } while (m.find());
3166
3167 // Insert the pattern at a random spot
3168 int insertIndex = generator.nextInt(99);
3169 if (Character.isLowSurrogate(s.charAt(insertIndex)))
3170 insertIndex++;
3171 s = s.insert(insertIndex, pattern);
3172 toSearch = s.toString();
3173
3174 // Make sure that the pattern is found
3175 m.reset(toSearch);
3176 if (!m.find())
3177 failCount++;
3178
3179 // Make sure that the match text is the pattern
3180 if (!m.group().equals(pattern))
3181 failCount++;
3182
3183 // Make sure match occured at insertion point
3184 if (m.start() != insertIndex)
3185 failCount++;
3186 }
3187 }
3188
3189 private static void explainFailure(String pattern, String data,
3190 String expected, String actual) {
3191 System.err.println("----------------------------------------");
3192 System.err.println("Pattern = "+pattern);
3193 System.err.println("Data = "+data);
3194 System.err.println("Expected = " + expected);
3195 System.err.println("Actual = " + actual);
3196 }
3197
3198 private static void explainFailure(String pattern, String data,
3199 Throwable t) {
3200 System.err.println("----------------------------------------");
3201 System.err.println("Pattern = "+pattern);
3202 System.err.println("Data = "+data);
3203 t.printStackTrace(System.err);
3204 }
3205
3206 // Testing examples from a file
3207
3208 /**
3209 * Goes through the file "TestCases.txt" and creates many patterns
3210 * described in the file, matching the patterns against input lines in
3211 * the file, and comparing the results against the correct results
3212 * also found in the file. The file format is described in comments
3213 * at the head of the file.
3214 */
3215 private static void processFile(String fileName) throws Exception {
3216 File testCases = new File(System.getProperty("test.src", "."),
3217 fileName);
3218 FileInputStream in = new FileInputStream(testCases);
3219 BufferedReader r = new BufferedReader(new InputStreamReader(in));
3220
3221 // Process next test case.
3222 String aLine;
3223 while((aLine = r.readLine()) != null) {
3224 // Read a line for pattern
3225 String patternString = grabLine(r);
3226 Pattern p = null;
3227 try {
3228 p = compileTestPattern(patternString);
3229 } catch (PatternSyntaxException e) {
3230 String dataString = grabLine(r);
3231 String expectedResult = grabLine(r);
3232 if (expectedResult.startsWith("error"))
3233 continue;
3234 explainFailure(patternString, dataString, e);
3235 failCount++;
3236 continue;
3237 }
3238
3239 // Read a line for input string
3240 String dataString = grabLine(r);
3241 Matcher m = p.matcher(dataString);
3242 StringBuffer result = new StringBuffer();
3243
3244 // Check for IllegalStateExceptions before a match
3245 failCount += preMatchInvariants(m);
3246
3247 boolean found = m.find();
3248
3249 if (found)
3250 failCount += postTrueMatchInvariants(m);
3251 else
3252 failCount += postFalseMatchInvariants(m);
3253
3254 if (found) {
3255 result.append("true ");
3256 result.append(m.group(0) + " ");
3257 } else {
3258 result.append("false ");
3259 }
3260
3261 result.append(m.groupCount());
3262
3263 if (found) {
3264 for (int i=1; i<m.groupCount()+1; i++)
3265 if (m.group(i) != null)
3266 result.append(" " +m.group(i));
3267 }
3268
3269 // Read a line for the expected result
3270 String expectedResult = grabLine(r);
3271
3272 if (!result.toString().equals(expectedResult)) {
3273 explainFailure(patternString, dataString, expectedResult, result.toString());
3274 failCount++;
3275 }
3276 }
3277
3278 report(fileName);
3279 }
3280
3281 private static int preMatchInvariants(Matcher m) {
3282 int failCount = 0;
3283 try {
3284 m.start();
3285 failCount++;
3286 } catch (IllegalStateException ise) {}
3287 try {
3288 m.end();
3289 failCount++;
3290 } catch (IllegalStateException ise) {}
3291 try {
3292 m.group();
3293 failCount++;
3294 } catch (IllegalStateException ise) {}
3295 return failCount;
3296 }
3297
3298 private static int postFalseMatchInvariants(Matcher m) {
3299 int failCount = 0;
3300 try {
3301 m.group();
3302 failCount++;
3303 } catch (IllegalStateException ise) {}
3304 try {
3305 m.start();
3306 failCount++;
3307 } catch (IllegalStateException ise) {}
3308 try {
3309 m.end();
3310 failCount++;
3311 } catch (IllegalStateException ise) {}
3312 return failCount;
3313 }
3314
3315 private static int postTrueMatchInvariants(Matcher m) {
3316 int failCount = 0;
3317 //assert(m.start() = m.start(0);
3318 if (m.start() != m.start(0))
3319 failCount++;
3320 //assert(m.end() = m.end(0);
3321 if (m.start() != m.start(0))
3322 failCount++;
3323 //assert(m.group() = m.group(0);
3324 if (!m.group().equals(m.group(0)))
3325 failCount++;
3326 try {
3327 m.group(50);
3328 failCount++;
3329 } catch (IndexOutOfBoundsException ise) {}
3330
3331 return failCount;
3332 }
3333
3334 private static Pattern compileTestPattern(String patternString) {
3335 if (!patternString.startsWith("'")) {
3336 return Pattern.compile(patternString);
3337 }
3338
3339 int break1 = patternString.lastIndexOf("'");
3340 String flagString = patternString.substring(
3341 break1+1, patternString.length());
3342 patternString = patternString.substring(1, break1);
3343
3344 if (flagString.equals("i"))
3345 return Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
3346
3347 if (flagString.equals("m"))
3348 return Pattern.compile(patternString, Pattern.MULTILINE);
3349
3350 return Pattern.compile(patternString);
3351 }
3352
3353 /**
3354 * Reads a line from the input file. Keeps reading lines until a non
3355 * empty non comment line is read. If the line contains a \n then
3356 * these two characters are replaced by a newline char. If a \\uxxxx
3357 * sequence is read then the sequence is replaced by the unicode char.
3358 */
3359 private static String grabLine(BufferedReader r) throws Exception {
3360 int index = 0;
3361 String line = r.readLine();
3362 while (line.startsWith("//") || line.length() < 1)
3363 line = r.readLine();
3364 while ((index = line.indexOf("\\n")) != -1) {
3365 StringBuffer temp = new StringBuffer(line);
3366 temp.replace(index, index+2, "\n");
3367 line = temp.toString();
3368 }
3369 while ((index = line.indexOf("\\u")) != -1) {
3370 StringBuffer temp = new StringBuffer(line);
3371 String value = temp.substring(index+2, index+6);
3372 char aChar = (char)Integer.parseInt(value, 16);
3373 String unicodeChar = "" + aChar;
3374 temp.replace(index, index+6, unicodeChar);
3375 line = temp.toString();
3376 }
3377
3378 return line;
3379 }
3380
3381 private static void check(Pattern p, String s, String g, String expected) {
3382 Matcher m = p.matcher(s);
3383 m.find();
3384 if (!m.group(g).equals(expected))
3385 failCount++;
3386 }
3387
3388 private static void checkReplaceFirst(String p, String s, String r, String expected)
3389 {
3390 if (!expected.equals(Pattern.compile(p)
3391 .matcher(s)
3392 .replaceFirst(r)))
3393 failCount++;
3394 }
3395
3396 private static void checkReplaceAll(String p, String s, String r, String expected)
3397 {
3398 if (!expected.equals(Pattern.compile(p)
3399 .matcher(s)
3400 .replaceAll(r)))
3401 failCount++;
3402 }
3403
3404 private static void checkExpectedFail(String p) {
3405 try {
3406 Pattern.compile(p);
3407 } catch (PatternSyntaxException pse) {
3408 //pse.printStackTrace();
3409 return;
3410 }
3411 failCount++;
3412 }
3413
3414 private static void checkExpectedFail(Matcher m, String g) {
3415 m.find();
3416 try {
3417 m.group(g);
3418 } catch (IllegalArgumentException iae) {
3419 //iae.printStackTrace();
3420 return;
3421 } catch (NullPointerException npe) {
3422 return;
3423 }
3424 failCount++;
3425 }
3426
3427
3428 private static void namedGroupCaptureTest() throws Exception {
3429 check(Pattern.compile("x+(?<gname>y+)z+"),
3430 "xxxyyyzzz",
3431 "gname",
3432 "yyy");
3433
shermand9337e02009-10-21 11:40:40 -07003434 check(Pattern.compile("x+(?<gname8>y+)z+"),
shermanffaf4922009-03-20 16:22:59 -07003435 "xxxyyyzzz",
shermand9337e02009-10-21 11:40:40 -07003436 "gname8",
shermanffaf4922009-03-20 16:22:59 -07003437 "yyy");
3438
sherman0b4d42d2009-02-23 21:06:15 -08003439 //backref
3440 Pattern pattern = Pattern.compile("(a*)bc\\1");
3441 check(pattern, "zzzaabcazzz", true); // found "abca"
3442
3443 check(Pattern.compile("(?<gname>a*)bc\\k<gname>"),
3444 "zzzaabcaazzz", true);
3445
3446 check(Pattern.compile("(?<gname>abc)(def)\\k<gname>"),
3447 "abcdefabc", true);
3448
3449 check(Pattern.compile("(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(?<gname>k)\\k<gname>"),
3450 "abcdefghijkk", true);
3451
3452 // Supplementary character tests
3453 check(Pattern.compile("(?<gname>" + toSupplementaries("a*)bc") + "\\k<gname>"),
3454 toSupplementaries("zzzaabcazzz"), true);
3455
3456 check(Pattern.compile("(?<gname>" + toSupplementaries("a*)bc") + "\\k<gname>"),
3457 toSupplementaries("zzzaabcaazzz"), true);
3458
3459 check(Pattern.compile("(?<gname>" + toSupplementaries("abc)(def)") + "\\k<gname>"),
3460 toSupplementaries("abcdefabc"), true);
3461
3462 check(Pattern.compile(toSupplementaries("(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)") +
3463 "(?<gname>" +
3464 toSupplementaries("k)") + "\\k<gname>"),
3465 toSupplementaries("abcdefghijkk"), true);
3466
3467 check(Pattern.compile("x+(?<gname>y+)z+\\k<gname>"),
3468 "xxxyyyzzzyyy",
3469 "gname",
3470 "yyy");
3471
3472 //replaceFirst/All
3473 checkReplaceFirst("(?<gn>ab)(c*)",
3474 "abccczzzabcczzzabccc",
shermand9337e02009-10-21 11:40:40 -07003475 "${gn}",
sherman0b4d42d2009-02-23 21:06:15 -08003476 "abzzzabcczzzabccc");
3477
3478 checkReplaceAll("(?<gn>ab)(c*)",
3479 "abccczzzabcczzzabccc",
shermand9337e02009-10-21 11:40:40 -07003480 "${gn}",
sherman0b4d42d2009-02-23 21:06:15 -08003481 "abzzzabzzzab");
3482
3483
3484 checkReplaceFirst("(?<gn>ab)(c*)",
3485 "zzzabccczzzabcczzzabccczzz",
shermand9337e02009-10-21 11:40:40 -07003486 "${gn}",
sherman0b4d42d2009-02-23 21:06:15 -08003487 "zzzabzzzabcczzzabccczzz");
3488
3489 checkReplaceAll("(?<gn>ab)(c*)",
3490 "zzzabccczzzabcczzzabccczzz",
shermand9337e02009-10-21 11:40:40 -07003491 "${gn}",
sherman0b4d42d2009-02-23 21:06:15 -08003492 "zzzabzzzabzzzabzzz");
3493
3494 checkReplaceFirst("(?<gn1>ab)(?<gn2>c*)",
3495 "zzzabccczzzabcczzzabccczzz",
shermand9337e02009-10-21 11:40:40 -07003496 "${gn2}",
sherman0b4d42d2009-02-23 21:06:15 -08003497 "zzzccczzzabcczzzabccczzz");
3498
3499 checkReplaceAll("(?<gn1>ab)(?<gn2>c*)",
3500 "zzzabccczzzabcczzzabccczzz",
shermand9337e02009-10-21 11:40:40 -07003501 "${gn2}",
sherman0b4d42d2009-02-23 21:06:15 -08003502 "zzzccczzzcczzzccczzz");
3503
3504 //toSupplementaries("(ab)(c*)"));
3505 checkReplaceFirst("(?<gn1>" + toSupplementaries("ab") +
3506 ")(?<gn2>" + toSupplementaries("c") + "*)",
3507 toSupplementaries("abccczzzabcczzzabccc"),
shermand9337e02009-10-21 11:40:40 -07003508 "${gn1}",
sherman0b4d42d2009-02-23 21:06:15 -08003509 toSupplementaries("abzzzabcczzzabccc"));
3510
3511
3512 checkReplaceAll("(?<gn1>" + toSupplementaries("ab") +
3513 ")(?<gn2>" + toSupplementaries("c") + "*)",
3514 toSupplementaries("abccczzzabcczzzabccc"),
shermand9337e02009-10-21 11:40:40 -07003515 "${gn1}",
sherman0b4d42d2009-02-23 21:06:15 -08003516 toSupplementaries("abzzzabzzzab"));
3517
3518 checkReplaceFirst("(?<gn1>" + toSupplementaries("ab") +
3519 ")(?<gn2>" + toSupplementaries("c") + "*)",
3520 toSupplementaries("abccczzzabcczzzabccc"),
shermand9337e02009-10-21 11:40:40 -07003521 "${gn2}",
sherman0b4d42d2009-02-23 21:06:15 -08003522 toSupplementaries("ccczzzabcczzzabccc"));
3523
3524
3525 checkReplaceAll("(?<gn1>" + toSupplementaries("ab") +
3526 ")(?<gn2>" + toSupplementaries("c") + "*)",
3527 toSupplementaries("abccczzzabcczzzabccc"),
shermand9337e02009-10-21 11:40:40 -07003528 "${gn2}",
sherman0b4d42d2009-02-23 21:06:15 -08003529 toSupplementaries("ccczzzcczzzccc"));
3530
3531 checkReplaceFirst("(?<dog>Dog)AndCat",
3532 "zzzDogAndCatzzzDogAndCatzzz",
shermand9337e02009-10-21 11:40:40 -07003533 "${dog}",
sherman0b4d42d2009-02-23 21:06:15 -08003534 "zzzDogzzzDogAndCatzzz");
3535
3536
3537 checkReplaceAll("(?<dog>Dog)AndCat",
3538 "zzzDogAndCatzzzDogAndCatzzz",
shermand9337e02009-10-21 11:40:40 -07003539 "${dog}",
sherman0b4d42d2009-02-23 21:06:15 -08003540 "zzzDogzzzDogzzz");
3541
3542 // backref in Matcher & String
shermand9337e02009-10-21 11:40:40 -07003543 if (!"abcdefghij".replaceFirst("cd(?<gn>ef)gh", "${gn}").equals("abefij") ||
3544 !"abbbcbdbefgh".replaceAll("(?<gn>[a-e])b", "${gn}").equals("abcdefgh"))
sherman0b4d42d2009-02-23 21:06:15 -08003545 failCount++;
3546
3547 // negative
3548 checkExpectedFail("(?<groupnamehasnoascii.in>abc)(def)");
3549 checkExpectedFail("(?<groupnamehasnoascii_in>abc)(def)");
shermand9337e02009-10-21 11:40:40 -07003550 checkExpectedFail("(?<6groupnamestartswithdigit>abc)(def)");
sherman0b4d42d2009-02-23 21:06:15 -08003551 checkExpectedFail("(?<gname>abc)(def)\\k<gnameX>");
3552 checkExpectedFail("(?<gname>abc)(?<gname>def)\\k<gnameX>");
3553 checkExpectedFail(Pattern.compile("(?<gname>abc)(def)").matcher("abcdef"),
3554 "gnameX");
3555 checkExpectedFail(Pattern.compile("(?<gname>abc)(def)").matcher("abcdef"),
3556 null);
3557 report("NamedGroupCapture");
3558 }
sherman6782c962010-02-05 00:10:42 -08003559
shermancc01ef52010-05-18 15:36:47 -07003560 // This is for bug 6969132
sherman6782c962010-02-05 00:10:42 -08003561 private static void nonBmpClassComplementTest() throws Exception {
3562 Pattern p = Pattern.compile("\\P{Lu}");
3563 Matcher m = p.matcher(new String(new int[] {0x1d400}, 0, 1));
3564 if (m.find() && m.start() == 1)
3565 failCount++;
3566
3567 // from a unicode category
3568 p = Pattern.compile("\\P{Lu}");
3569 m = p.matcher(new String(new int[] {0x1d400}, 0, 1));
3570 if (m.find())
3571 failCount++;
3572 if (!m.hitEnd())
3573 failCount++;
3574
3575 // block
3576 p = Pattern.compile("\\P{InMathematicalAlphanumericSymbols}");
3577 m = p.matcher(new String(new int[] {0x1d400}, 0, 1));
3578 if (m.find() && m.start() == 1)
3579 failCount++;
3580
3581 report("NonBmpClassComplement");
3582 }
3583
shermancc01ef52010-05-18 15:36:47 -07003584 private static void unicodePropertiesTest() throws Exception {
3585 // different forms
3586 if (!Pattern.compile("\\p{IsLu}").matcher("A").matches() ||
3587 !Pattern.compile("\\p{Lu}").matcher("A").matches() ||
3588 !Pattern.compile("\\p{gc=Lu}").matcher("A").matches() ||
3589 !Pattern.compile("\\p{general_category=Lu}").matcher("A").matches() ||
3590 !Pattern.compile("\\p{IsLatin}").matcher("B").matches() ||
3591 !Pattern.compile("\\p{sc=Latin}").matcher("B").matches() ||
3592 !Pattern.compile("\\p{script=Latin}").matcher("B").matches() ||
3593 !Pattern.compile("\\p{InBasicLatin}").matcher("c").matches() ||
3594 !Pattern.compile("\\p{blk=BasicLatin}").matcher("c").matches() ||
3595 !Pattern.compile("\\p{block=BasicLatin}").matcher("c").matches())
3596 failCount++;
3597
3598 Matcher common = Pattern.compile("\\p{script=Common}").matcher("");
3599 Matcher unknown = Pattern.compile("\\p{IsUnknown}").matcher("");
3600 Matcher lastSM = common;
3601 Character.UnicodeScript lastScript = Character.UnicodeScript.of(0);
3602
3603 Matcher latin = Pattern.compile("\\p{block=basic_latin}").matcher("");
3604 Matcher greek = Pattern.compile("\\p{InGreek}").matcher("");
3605 Matcher lastBM = latin;
3606 Character.UnicodeBlock lastBlock = Character.UnicodeBlock.of(0);
3607
3608 for (int cp = 1; cp < Character.MAX_CODE_POINT; cp++) {
3609 if (cp >= 0x30000 && (cp & 0x70) == 0){
3610 continue; // only pick couple code points, they are the same
3611 }
3612
3613 // Unicode Script
3614 Character.UnicodeScript script = Character.UnicodeScript.of(cp);
3615 Matcher m;
3616 String str = new String(Character.toChars(cp));
3617 if (script == lastScript) {
3618 m = lastSM;
3619 m.reset(str);
3620 } else {
3621 m = Pattern.compile("\\p{Is" + script.name() + "}").matcher(str);
3622 }
3623 if (!m.matches()) {
3624 failCount++;
3625 }
3626 Matcher other = (script == Character.UnicodeScript.COMMON)? unknown : common;
3627 other.reset(str);
3628 if (other.matches()) {
3629 failCount++;
3630 }
3631 lastSM = m;
3632 lastScript = script;
3633
3634 // Unicode Block
3635 Character.UnicodeBlock block = Character.UnicodeBlock.of(cp);
3636 if (block == null) {
3637 //System.out.printf("Not a Block: cp=%x%n", cp);
3638 continue;
3639 }
3640 if (block == lastBlock) {
3641 m = lastBM;
3642 m.reset(str);
3643 } else {
3644 m = Pattern.compile("\\p{block=" + block.toString() + "}").matcher(str);
3645 }
3646 if (!m.matches()) {
3647 failCount++;
3648 }
3649 other = (block == Character.UnicodeBlock.BASIC_LATIN)? greek : latin;
3650 other.reset(str);
3651 if (other.matches()) {
3652 failCount++;
3653 }
3654 lastBM = m;
3655 lastBlock = block;
3656 }
3657 report("unicodeProperties");
3658 }
shermanf03c78b2011-02-03 13:49:25 -08003659
3660 private static void unicodeHexNotationTest() throws Exception {
3661
3662 // negative
3663 checkExpectedFail("\\x{-23}");
3664 checkExpectedFail("\\x{110000}");
3665 checkExpectedFail("\\x{}");
3666 checkExpectedFail("\\x{AB[ef]");
3667
3668 // codepoint
3669 check("^\\x{1033c}$", "\uD800\uDF3C", true);
3670 check("^\\xF0\\x90\\x8C\\xBC$", "\uD800\uDF3C", false);
3671 check("^\\x{D800}\\x{DF3c}+$", "\uD800\uDF3C", false);
3672 check("^\\xF0\\x90\\x8C\\xBC$", "\uD800\uDF3C", false);
3673
3674 // in class
3675 check("^[\\x{D800}\\x{DF3c}]+$", "\uD800\uDF3C", false);
3676 check("^[\\xF0\\x90\\x8C\\xBC]+$", "\uD800\uDF3C", false);
3677 check("^[\\x{D800}\\x{DF3C}]+$", "\uD800\uDF3C", false);
3678 check("^[\\x{DF3C}\\x{D800}]+$", "\uD800\uDF3C", false);
3679 check("^[\\x{D800}\\x{DF3C}]+$", "\uDF3C\uD800", true);
3680 check("^[\\x{DF3C}\\x{D800}]+$", "\uDF3C\uD800", true);
3681
3682 for (int cp = 0; cp <= 0x10FFFF; cp++) {
3683 String s = "A" + new String(Character.toChars(cp)) + "B";
3684 String hexUTF16 = (cp <= 0xFFFF)? String.format("\\u%04x", cp)
3685 : String.format("\\u%04x\\u%04x",
3686 (int) Character.toChars(cp)[0],
3687 (int) Character.toChars(cp)[1]);
3688 String hexCodePoint = "\\x{" + Integer.toHexString(cp) + "}";
3689 if (!Pattern.matches("A" + hexUTF16 + "B", s))
3690 failCount++;
3691 if (!Pattern.matches("A[" + hexUTF16 + "]B", s))
3692 failCount++;
3693 if (!Pattern.matches("A" + hexCodePoint + "B", s))
3694 failCount++;
3695 if (!Pattern.matches("A[" + hexCodePoint + "]B", s))
3696 failCount++;
3697 }
3698 report("unicodeHexNotation");
sherman85bbd8b2011-04-28 20:48:36 -07003699 }
3700
3701 private static void unicodeClassesTest() throws Exception {
3702
3703 Matcher lower = Pattern.compile("\\p{Lower}").matcher("");
3704 Matcher upper = Pattern.compile("\\p{Upper}").matcher("");
3705 Matcher ASCII = Pattern.compile("\\p{ASCII}").matcher("");
3706 Matcher alpha = Pattern.compile("\\p{Alpha}").matcher("");
3707 Matcher digit = Pattern.compile("\\p{Digit}").matcher("");
3708 Matcher alnum = Pattern.compile("\\p{Alnum}").matcher("");
3709 Matcher punct = Pattern.compile("\\p{Punct}").matcher("");
3710 Matcher graph = Pattern.compile("\\p{Graph}").matcher("");
3711 Matcher print = Pattern.compile("\\p{Print}").matcher("");
3712 Matcher blank = Pattern.compile("\\p{Blank}").matcher("");
3713 Matcher cntrl = Pattern.compile("\\p{Cntrl}").matcher("");
3714 Matcher xdigit = Pattern.compile("\\p{XDigit}").matcher("");
3715 Matcher space = Pattern.compile("\\p{Space}").matcher("");
3716 Matcher bound = Pattern.compile("\\b").matcher("");
3717 Matcher word = Pattern.compile("\\w++").matcher("");
3718 // UNICODE_CHARACTER_CLASS
3719 Matcher lowerU = Pattern.compile("\\p{Lower}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3720 Matcher upperU = Pattern.compile("\\p{Upper}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3721 Matcher ASCIIU = Pattern.compile("\\p{ASCII}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3722 Matcher alphaU = Pattern.compile("\\p{Alpha}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3723 Matcher digitU = Pattern.compile("\\p{Digit}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3724 Matcher alnumU = Pattern.compile("\\p{Alnum}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3725 Matcher punctU = Pattern.compile("\\p{Punct}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3726 Matcher graphU = Pattern.compile("\\p{Graph}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3727 Matcher printU = Pattern.compile("\\p{Print}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3728 Matcher blankU = Pattern.compile("\\p{Blank}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3729 Matcher cntrlU = Pattern.compile("\\p{Cntrl}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3730 Matcher xdigitU = Pattern.compile("\\p{XDigit}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3731 Matcher spaceU = Pattern.compile("\\p{Space}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3732 Matcher boundU = Pattern.compile("\\b", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3733 Matcher wordU = Pattern.compile("\\w", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3734 // embedded flag (?U)
3735 Matcher lowerEU = Pattern.compile("(?U)\\p{Lower}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3736 Matcher graphEU = Pattern.compile("(?U)\\p{Graph}", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3737 Matcher wordEU = Pattern.compile("(?U)\\w", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3738
3739 Matcher bwb = Pattern.compile("\\b\\w\\b").matcher("");
3740 Matcher bwbU = Pattern.compile("\\b\\w++\\b", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3741 Matcher bwbEU = Pattern.compile("(?U)\\b\\w++\\b", Pattern.UNICODE_CHARACTER_CLASS).matcher("");
3742 // properties
3743 Matcher lowerP = Pattern.compile("\\p{IsLowerCase}").matcher("");
3744 Matcher upperP = Pattern.compile("\\p{IsUpperCase}").matcher("");
3745 Matcher titleP = Pattern.compile("\\p{IsTitleCase}").matcher("");
3746 Matcher letterP = Pattern.compile("\\p{IsLetter}").matcher("");
3747 Matcher alphaP = Pattern.compile("\\p{IsAlphabetic}").matcher("");
3748 Matcher ideogP = Pattern.compile("\\p{IsIdeographic}").matcher("");
3749 Matcher cntrlP = Pattern.compile("\\p{IsControl}").matcher("");
3750 Matcher spaceP = Pattern.compile("\\p{IsWhiteSpace}").matcher("");
3751 Matcher definedP = Pattern.compile("\\p{IsAssigned}").matcher("");
3752 Matcher nonCCPP = Pattern.compile("\\p{IsNoncharacterCodePoint}").matcher("");
3753
3754 // javaMethod
3755 Matcher lowerJ = Pattern.compile("\\p{javaLowerCase}").matcher("");
3756 Matcher upperJ = Pattern.compile("\\p{javaUpperCase}").matcher("");
3757 Matcher alphaJ = Pattern.compile("\\p{javaAlphabetic}").matcher("");
3758 Matcher ideogJ = Pattern.compile("\\p{javaIdeographic}").matcher("");
3759
3760 for (int cp = 1; cp < 0x30000; cp++) {
3761 String str = new String(Character.toChars(cp));
3762 int type = Character.getType(cp);
3763 if (// lower
3764 POSIX_ASCII.isLower(cp) != lower.reset(str).matches() ||
3765 Character.isLowerCase(cp) != lowerU.reset(str).matches() ||
3766 Character.isLowerCase(cp) != lowerP.reset(str).matches() ||
3767 Character.isLowerCase(cp) != lowerEU.reset(str).matches()||
3768 Character.isLowerCase(cp) != lowerJ.reset(str).matches()||
3769 // upper
3770 POSIX_ASCII.isUpper(cp) != upper.reset(str).matches() ||
3771 POSIX_Unicode.isUpper(cp) != upperU.reset(str).matches() ||
3772 Character.isUpperCase(cp) != upperP.reset(str).matches() ||
3773 Character.isUpperCase(cp) != upperJ.reset(str).matches() ||
3774 // alpha
3775 POSIX_ASCII.isAlpha(cp) != alpha.reset(str).matches() ||
3776 POSIX_Unicode.isAlpha(cp) != alphaU.reset(str).matches() ||
3777 Character.isAlphabetic(cp)!= alphaP.reset(str).matches() ||
3778 Character.isAlphabetic(cp)!= alphaJ.reset(str).matches() ||
3779 // digit
3780 POSIX_ASCII.isDigit(cp) != digit.reset(str).matches() ||
3781 Character.isDigit(cp) != digitU.reset(str).matches() ||
3782 // alnum
3783 POSIX_ASCII.isAlnum(cp) != alnum.reset(str).matches() ||
3784 POSIX_Unicode.isAlnum(cp) != alnumU.reset(str).matches() ||
3785 // punct
3786 POSIX_ASCII.isPunct(cp) != punct.reset(str).matches() ||
3787 POSIX_Unicode.isPunct(cp) != punctU.reset(str).matches() ||
3788 // graph
3789 POSIX_ASCII.isGraph(cp) != graph.reset(str).matches() ||
3790 POSIX_Unicode.isGraph(cp) != graphU.reset(str).matches() ||
3791 POSIX_Unicode.isGraph(cp) != graphEU.reset(str).matches()||
3792 // blank
3793 POSIX_ASCII.isType(cp, POSIX_ASCII.BLANK)
3794 != blank.reset(str).matches() ||
3795 POSIX_Unicode.isBlank(cp) != blankU.reset(str).matches() ||
3796 // print
3797 POSIX_ASCII.isPrint(cp) != print.reset(str).matches() ||
3798 POSIX_Unicode.isPrint(cp) != printU.reset(str).matches() ||
3799 // cntrl
3800 POSIX_ASCII.isCntrl(cp) != cntrl.reset(str).matches() ||
3801 POSIX_Unicode.isCntrl(cp) != cntrlU.reset(str).matches() ||
3802 (Character.CONTROL == type) != cntrlP.reset(str).matches() ||
3803 // hexdigit
3804 POSIX_ASCII.isHexDigit(cp) != xdigit.reset(str).matches() ||
3805 POSIX_Unicode.isHexDigit(cp) != xdigitU.reset(str).matches() ||
3806 // space
3807 POSIX_ASCII.isSpace(cp) != space.reset(str).matches() ||
3808 POSIX_Unicode.isSpace(cp) != spaceU.reset(str).matches() ||
3809 POSIX_Unicode.isSpace(cp) != spaceP.reset(str).matches() ||
3810 // word
3811 POSIX_ASCII.isWord(cp) != word.reset(str).matches() ||
3812 POSIX_Unicode.isWord(cp) != wordU.reset(str).matches() ||
3813 POSIX_Unicode.isWord(cp) != wordEU.reset(str).matches()||
3814 // bwordb
3815 POSIX_ASCII.isWord(cp) != bwb.reset(str).matches() ||
3816 POSIX_Unicode.isWord(cp) != bwbU.reset(str).matches() ||
3817 // properties
3818 Character.isTitleCase(cp) != titleP.reset(str).matches() ||
3819 Character.isLetter(cp) != letterP.reset(str).matches()||
3820 Character.isIdeographic(cp) != ideogP.reset(str).matches() ||
3821 Character.isIdeographic(cp) != ideogJ.reset(str).matches() ||
3822 (Character.UNASSIGNED == type) == definedP.reset(str).matches() ||
3823 POSIX_Unicode.isNoncharacterCodePoint(cp) != nonCCPP.reset(str).matches())
3824 failCount++;
3825 }
3826
3827 // bounds/word align
3828 twoFindIndexes(" \u0180sherman\u0400 ", bound, 1, 10);
3829 if (!bwbU.reset("\u0180sherman\u0400").matches())
3830 failCount++;
3831 twoFindIndexes(" \u0180sh\u0345erman\u0400 ", bound, 1, 11);
3832 if (!bwbU.reset("\u0180sh\u0345erman\u0400").matches())
3833 failCount++;
3834 twoFindIndexes(" \u0724\u0739\u0724 ", bound, 1, 4);
3835 if (!bwbU.reset("\u0724\u0739\u0724").matches())
3836 failCount++;
3837 if (!bwbEU.reset("\u0724\u0739\u0724").matches())
3838 failCount++;
3839 report("unicodePredefinedClasses");
3840 }
sherman0b4d42d2009-02-23 21:06:15 -08003841}