2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.harmony.regex.tests.java.util.regex;
20 import java.io.Serializable;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 import java.util.regex.PatternSyntaxException;
25 import junit.framework.TestCase;
27 import org.apache.harmony.testframework.serialization.SerializationTest;
28 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
30 import dalvik.annotation.TestLevel;
31 import dalvik.annotation.TestTargetClass;
32 import dalvik.annotation.TestTargetNew;
33 import dalvik.annotation.TestTargets;
36 value = Pattern.class,
39 level = TestLevel.NOT_FEASIBLE,
40 notes = "finalize is hard to test since the implementation only calls a native function",
46 public class PatternTest extends TestCase {
47 String[] testPatterns = {
51 "(1|2|3|4|5|6|7|8|9|0)(1|2|3|4|5|6|7|8|9|0)*",
52 "(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)*",
53 "(a|b)*(a|b)*A(a|b)*lice.*",
54 "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)(a|b|c|d|e|f|g|h|"
55 + "i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)*(1|2|3|4|5|6|7|8|9|0)*|while|for|struct|if|do",
56 // BEGIN android-changed
57 // We don't have canonical equivalence.
58 // "x(?c)y", "x(?cc)y"
60 // END android-changed
64 String[] testPatternsAlt = {
66 * According to JavaDoc 2 and 3 oct digit sequences like \\o70\\o347
67 * should be OK, but test is failed for them
69 "[ab]\\b\\\\o5\\xF9\\u1E7B\\t\\n\\f\\r\\a\\e[yz]",
70 "^\\p{Lower}*\\p{Upper}*\\p{ASCII}?\\p{Alpha}?\\p{Digit}*\\p{Alnum}\\p{Punct}\\p{Graph}\\p{Print}\\p{Blank}\\p{Cntrl}\\p{XDigit}\\p{Space}",
71 "$\\p{javaLowerCase}\\p{javaUpperCase}\\p{javaWhitespace}\\p{javaMirrored}",
72 "\\p{InGreek}\\p{Lu}\\p{Sc}\\P{InGreek}[\\p{L}&&[^\\p{Lu}]]" };
74 String[] wrongTestPatterns = { "\\o9A", "\\p{Lawer}", "\\xG0" };
76 final static int[] flagsSet = { Pattern.CASE_INSENSITIVE,
77 Pattern.MULTILINE, Pattern.DOTALL, Pattern.UNICODE_CASE
78 /* , Pattern.CANON_EQ */ };
81 * Based on RI implenetation documents. Need to check this set regarding
82 * actual implementation.
84 final static int[] wrongFlagsSet = { 256, 512, 1024 };
86 final static int DEFAULT_FLAGS = 0;
89 level = TestLevel.ADDITIONAL,
94 public void testMatcher() {
95 // some very simple test
96 Pattern p = Pattern.compile("a");
97 assertNotNull(p.matcher("bcde"));
98 assertNotSame(p.matcher("a"), p.matcher("a"));
102 * Class under test for String[] split(CharSequence, int)
105 level = TestLevel.PARTIAL_COMPLETE,
106 notes = "Verifies the functionality of splitsplit(java.lang.String, int) method.",
108 args = {CharSequence.class, int.class}
110 public void testSplitCharSequenceint() {
111 // splitting CharSequence which ends with pattern
113 assertEquals(",,".split(",", 3).length, 3);
114 assertEquals(",,".split(",", 4).length, 3);
117 assertEquals(Pattern.compile("o").split("boo:and:foo", 5).length, 5);
118 assertEquals(Pattern.compile("b").split("ab", -1).length, 2);
121 Pattern pat = Pattern.compile("x");
122 s = pat.split("zxx:zzz:zxx", 10);
123 assertEquals(s.length, 5);
124 s = pat.split("zxx:zzz:zxx", 3);
125 assertEquals(s.length, 3);
126 s = pat.split("zxx:zzz:zxx", -1);
127 assertEquals(s.length, 5);
128 s = pat.split("zxx:zzz:zxx", 0);
129 assertEquals(s.length, 3);
132 pat = Pattern.compile("b");
133 s = pat.split("abccbadfebb", -1);
134 assertEquals(s.length, 5);
135 s = pat.split("", -1);
136 assertEquals(s.length, 1);
137 pat = Pattern.compile("");
138 s = pat.split("", -1);
139 assertEquals(s.length, 1);
140 s = pat.split("abccbadfe", -1);
141 assertEquals(s.length, 11);
143 pat = Pattern.compile("b");
144 s = pat.split("abccbadfebb", 0);
145 assertEquals(s.length, 3);
146 s = pat.split("", 0);
147 assertEquals(s.length, 1);
148 pat = Pattern.compile("");
149 s = pat.split("", 0);
150 assertEquals(s.length, 1);
151 s = pat.split("abccbadfe", 0);
152 assertEquals(s.length, 10);
154 pat = Pattern.compile("b");
155 s = pat.split("abccbadfebb", 12);
156 assertEquals(s.length, 5);
157 s = pat.split("", 6);
158 assertEquals(s.length, 1);
159 pat = Pattern.compile("");
160 s = pat.split("", 11);
161 assertEquals(s.length, 1);
162 s = pat.split("abccbadfe", 15);
163 assertEquals(s.length, 11);
165 pat = Pattern.compile("b");
166 s = pat.split("abccbadfebb", 5);
167 assertEquals(s.length, 5);
168 s = pat.split("", 1);
169 assertEquals(s.length, 1);
170 pat = Pattern.compile("");
171 s = pat.split("", 1);
172 assertEquals(s.length, 1);
173 s = pat.split("abccbadfe", 11);
174 assertEquals(s.length, 11);
176 pat = Pattern.compile("b");
177 s = pat.split("abccbadfebb", 3);
178 assertEquals(s.length, 3);
179 pat = Pattern.compile("");
180 s = pat.split("abccbadfe", 5);
181 assertEquals(s.length, 5);
185 * Class under test for String[] split(CharSequence)
188 level = TestLevel.PARTIAL_COMPLETE,
189 notes = "Verifies the functionality of splitsplit(java.lang.String) method.",
191 args = {CharSequence.class}
193 public void testSplitCharSequence() {
195 Pattern pat = Pattern.compile("b");
196 s = pat.split("abccbadfebb");
197 assertEquals(s.length, 3);
199 assertEquals(s.length, 1);
200 pat = Pattern.compile("");
202 assertEquals(s.length, 1);
203 s = pat.split("abccbadfe");
204 assertEquals(s.length, 10);
207 String[] arr = s1.split(":");
208 assertEquals(arr.length, 1);
213 level = TestLevel.COMPLETE,
214 notes = "Verifies the functionality of pattern() method.",
218 public void testPattern() {
219 /* Positive assertion test. */
220 for (String aPattern : testPatterns) {
221 Pattern p = Pattern.compile(aPattern);
223 assertTrue(p.pattern().equals(aPattern));
224 } catch (Exception e) {
225 fail("Unexpected exception: " + e);
231 level = TestLevel.PARTIAL_COMPLETE,
232 notes = "Verifies the functionality of compile(java.lang.String) method.",
234 args = {java.lang.String.class}
236 public void testCompile() {
237 /* Positive assertion test. */
238 for (String aPattern : testPatterns) {
240 Pattern p = Pattern.compile(aPattern);
241 } catch (Exception e) {
242 fail("Unexpected exception: " + e);
246 /* Positive assertion test with alternative templates. */
247 for (String aPattern : testPatternsAlt) {
249 Pattern p = Pattern.compile(aPattern);
250 } catch (Exception e) {
251 fail("Unexpected exception: " + e);
255 /* Negative assertion test. */
256 for (String aPattern : wrongTestPatterns) {
258 Pattern p = Pattern.compile(aPattern);
259 fail("PatternSyntaxException is expected");
260 } catch (PatternSyntaxException pse) {
262 } catch (Exception e) {
263 fail("Unexpected exception: " + e);
269 level = TestLevel.PARTIAL_COMPLETE,
270 notes = "Verifies the functionality of compile(java.lang.String) method for different flags.",
272 args = {java.lang.String.class}
274 public void testFlags() {
280 baseString = "((?i)|b)a";
282 pat = Pattern.compile(baseString);
283 mat = pat.matcher(testString);
284 assertFalse(mat.matches());
286 baseString = "(?i)a|b";
288 pat = Pattern.compile(baseString);
289 mat = pat.matcher(testString);
290 assertTrue(mat.matches());
292 baseString = "(?i)a|b";
294 pat = Pattern.compile(baseString);
295 mat = pat.matcher(testString);
296 assertTrue(mat.matches());
298 baseString = "c|(?i)a|b";
300 pat = Pattern.compile(baseString);
301 mat = pat.matcher(testString);
302 assertTrue(mat.matches());
304 baseString = "(?i)a|(?s)b";
306 pat = Pattern.compile(baseString);
307 mat = pat.matcher(testString);
308 assertTrue(mat.matches());
310 baseString = "(?i)a|(?-i)b";
312 pat = Pattern.compile(baseString);
313 mat = pat.matcher(testString);
314 assertFalse(mat.matches());
316 baseString = "(?i)a|(?-i)c|b";
318 pat = Pattern.compile(baseString);
319 mat = pat.matcher(testString);
320 assertFalse(mat.matches());
322 baseString = "(?i)a|(?-i)c|(?i)b";
324 pat = Pattern.compile(baseString);
325 mat = pat.matcher(testString);
326 assertTrue(mat.matches());
328 baseString = "(?i)a|(?-i)b";
330 pat = Pattern.compile(baseString);
331 mat = pat.matcher(testString);
332 assertTrue(mat.matches());
334 baseString = "((?i))a";
336 pat = Pattern.compile(baseString);
337 mat = pat.matcher(testString);
338 assertFalse(mat.matches());
340 baseString = "|(?i)|a";
342 pat = Pattern.compile(baseString);
343 mat = pat.matcher(testString);
344 assertTrue(mat.matches());
346 baseString = "(?i)((?s)a.)";
348 pat = Pattern.compile(baseString);
349 mat = pat.matcher(testString);
350 assertTrue(mat.matches());
352 baseString = "(?i)((?-i)a)";
354 pat = Pattern.compile(baseString);
355 mat = pat.matcher(testString);
356 assertFalse(mat.matches());
358 baseString = "(?i)(?s:a.)";
360 pat = Pattern.compile(baseString);
361 mat = pat.matcher(testString);
362 assertTrue(mat.matches());
364 baseString = "(?i)fgh(?s:aa)";
365 testString = "fghAA";
366 pat = Pattern.compile(baseString);
367 mat = pat.matcher(testString);
368 assertTrue(mat.matches());
370 baseString = "(?i)((?-i))a";
372 pat = Pattern.compile(baseString);
373 mat = pat.matcher(testString);
374 assertTrue(mat.matches());
376 baseString = "abc(?i)d";
378 pat = Pattern.compile(baseString);
379 mat = pat.matcher(testString);
380 assertFalse(mat.matches());
383 mat = pat.matcher(testString);
384 assertTrue(mat.matches());
386 baseString = "a(?i)a(?-i)a(?i)a(?-i)a";
387 testString = "aAaAa";
388 pat = Pattern.compile(baseString);
389 mat = pat.matcher(testString);
390 assertTrue(mat.matches());
392 testString = "aAAAa";
393 mat = pat.matcher(testString);
394 assertFalse(mat.matches());
397 // BEGIN android-removed
398 // The flags() method should only return those flags that were explicitly
399 // passed during the compilation. The JDK also accepts the ones implicitly
400 // contained in the pattern, but ICU doesn't do this.
402 // public void testFlagsMethod() {
403 // String baseString;
407 // * These tests are for compatibility with RI only. Logically we have to
408 // * return only flags specified during the compilation. For example
409 // * pat.flags() == 0 when we compile Pattern pat =
410 // * Pattern.compile("(?i)abc(?-i)"); but the whole expression is compiled
411 // * in a case insensitive manner. So there is little sense to do calls to
414 // baseString = "(?-i)";
415 // pat = Pattern.compile(baseString);
417 // baseString = "(?idmsux)abc(?-i)vg(?-dmu)";
418 // pat = Pattern.compile(baseString);
419 // assertEquals(pat.flags(), Pattern.DOTALL | Pattern.COMMENTS);
421 // baseString = "(?idmsux)abc|(?-i)vg|(?-dmu)";
422 // pat = Pattern.compile(baseString);
423 // assertEquals(pat.flags(), Pattern.DOTALL | Pattern.COMMENTS);
425 // baseString = "(?is)a((?x)b.)";
426 // pat = Pattern.compile(baseString);
427 // assertEquals(pat.flags(), Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
429 // baseString = "(?i)a((?-i))";
430 // pat = Pattern.compile(baseString);
431 // assertEquals(pat.flags(), Pattern.CASE_INSENSITIVE);
433 // baseString = "((?i)a)";
434 // pat = Pattern.compile(baseString);
435 // assertEquals(pat.flags(), 0);
437 // pat = Pattern.compile("(?is)abc");
438 // assertEquals(pat.flags(), Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
440 //END android-removed
443 * Check default flags when they are not specified in pattern. Based on RI
444 * since could not find that info
448 level = TestLevel.PARTIAL_COMPLETE,
449 notes = "Verifies the functionality of flags() method for default flags.",
453 public void testFlagsCompileDefault() {
454 for (String pat : testPatternsAlt) {
456 Pattern p = Pattern.compile(pat);
457 assertEquals(p.flags(), DEFAULT_FLAGS);
458 } catch (Exception e) {
459 fail("Unexpected exception: " + e);
465 * Check that flags specified during compile are set properly This is a
466 * simple implementation that does not use flags combinations. Need to
472 level = TestLevel.PARTIAL_COMPLETE,
473 notes = "Verifies the functionality of compile(java.lang.String, int) & flags() methods. Checks that compilation was corect.",
478 level = TestLevel.PARTIAL_COMPLETE,
479 notes = "Verifies the functionality of compile(java.lang.String, int) & flags() methods. Checks that compilation was corect.",
481 args = {java.lang.String.class, int.class}
484 public void testFlagsCompileValid() {
485 for (String pat : testPatternsAlt) {
486 for (int flags : flagsSet) {
488 Pattern p = Pattern.compile(pat, flags);
489 assertEquals(p.flags(), flags);
490 } catch (Exception e) {
491 fail("Unexpected exception: " + e);
498 * Class under test for Pattern compile(String, int)
502 level = TestLevel.PARTIAL_COMPLETE,
503 notes = "Verifies the functionality of compile(java.lang.String) method.Checks that correct exceptions were thrown.",
505 args = {java.lang.String.class}
507 public void testCompileStringint() {
509 * these tests are needed to verify that appropriate exceptions are
512 String pattern = "b)a";
514 Pattern.compile(pattern);
515 fail("Expected a PatternSyntaxException when compiling pattern: "
517 } catch (PatternSyntaxException e) {
522 Pattern.compile(pattern);
523 fail("Expected a PatternSyntaxException when compiling pattern: "
525 } catch (PatternSyntaxException e) {
530 Pattern pat = Pattern.compile(pattern);
531 fail("Expected a PatternSyntaxException when compiling pattern: "
533 } catch (PatternSyntaxException e) {
537 pattern = "cdb(?i))a";
539 Pattern pat = Pattern.compile(pattern);
540 fail("Expected a PatternSyntaxException when compiling pattern: "
542 } catch (PatternSyntaxException e) {
547 * This pattern should compile - HARMONY-2127
549 // pattern = "x(?c)y";
550 // Pattern.compile(pattern);
553 * this pattern doesn't match any string, but should be compiled anyway
556 Pattern.compile(pattern);
560 * Class under test for Pattern compile(String)
563 level = TestLevel.PARTIAL_COMPLETE,
564 notes = "Verifies the functionality of compile(java.lang.String) method.Checks that correct exceptions were thrown.",
566 args = {java.lang.String.class}
568 public void testQuantCompileNeg() {
569 String[] patterns = { "5{,2}", "{5asd", "{hgdhg", "{5,hjkh", "{,5hdsh",
571 for (String element : patterns) {
573 Pattern.compile(element);
574 fail("PatternSyntaxException was expected, but compilation succeeds");
575 } catch (PatternSyntaxException pse) {
579 // Regression for HARMONY-1365
580 // BEGIN android-changed
581 // Original regex contained some illegal stuff. Changed it slightly,
582 // while maintaining the wicked character of this "mother of all
584 // String pattern = "(?![^\\<C\\f\\0146\\0270\\}&&[|\\02-\\x3E\\}|X-\\|]]{7,}+)[|\\\\\\x98\\<\\?\\u4FCFr\\,\\0025\\}\\004|\\0025-\\052\061]|(?<![|\\01-\\u829E])|(?<!\\p{Alpha})|^|(?-s:[^\\x15\\\\\\x24F\\a\\,\\a\\u97D8[\\x38\\a[\\0224-\\0306[^\\0020-\\u6A57]]]]??)(?uxix:[^|\\{\\[\\0367\\t\\e\\x8C\\{\\[\\074c\\]V[|b\\fu\\r\\0175\\<\\07f\\066s[^D-\\x5D]]])(?xx:^{5,}+)(?uuu)(?=^\\D)|(?!\\G)(?>\\G*?)(?![^|\\]\\070\\ne\\{\\t\\[\\053\\?\\\\\\x51\\a\\075\\0023-\\[&&[|\\022-\\xEA\\00-\\u41C2&&[^|a-\\xCC&&[^\\037\\uECB3\\u3D9A\\x31\\|\\<b\\0206\\uF2EC\\01m\\,\\ak\\a\\03&&\\p{Punct}]]]])(?-dxs:[|\\06-\\07|\\e-\\x63&&[|Tp\\u18A3\\00\\|\\xE4\\05\\061\\015\\0116C|\\r\\{\\}\\006\\xEA\\0367\\xC4\\01\\0042\\0267\\xBB\\01T\\}\\0100\\?[|\\[-\\u459B|\\x23\\x91\\rF\\0376[|\\?-\\x94\\0113-\\\\\\s]]]]{6}?)(?<=[^\\t-\\x42H\\04\\f\\03\\0172\\?i\\u97B6\\e\\f\\uDAC2])(?=\\B*+)(?>[^\\016\\r\\{\\,\\uA29D\\034\\02[\\02-\\[|\\t\\056\\uF599\\x62\\e\\<\\032\\uF0AC\\0026\\0205Q\\|\\\\\\06\\0164[|\\057-\\u7A98&&[\\061-g|\\|\\0276\\n\\042\\011\\e\\xE8\\x64B\\04\\u6D0EDW^\\p{Lower}]]]]?)(?<=[^\\n\\\\\\t\\u8E13\\,\\0114\\u656E\\xA5\\]&&[\\03-\\026|\\uF39D\\01\\{i\\u3BC2\\u14FE]])(?<=[^|\\uAE62\\054H\\|\\}&&^\\p{Space}])(?sxx)(?<=[\\f\\006\\a\\r\\xB4]*+)|(?x-xd:^{5}+)()";
585 String pattern = "(?![^\\<C\\f\\0146\\0270\\}&&[|\\02-\\x3E\\}|X-\\|]]{7,}+)[|\\\\\\x98\\<\\?\\u4FCFr\\,\\0025\\}\\004|\\0025-\\052\061]|(?<![|\\01-\\u829E])|(?<!\\p{Alpha})|^|(?-s:[^\\x15\\\\\\x24F\\a\\,\\a\\u97D8[\\x38\\a[\\0224-\\0306[^\\0020-\\u6A57]]]]??)(?uxix:[^|\\{\\[\\0367\\t\\e\\x8C\\{\\[\\074c\\]V[|b\\fu\\r\\0175\\<\\07f\\066s[^D-\\x5D]]])(?xx:^{5,}+)(?uuu)(?=^\\D)|(?!\\G)(?>\\.*?)(?![^|\\]\\070\\ne\\{\\t\\[\\053\\?\\\\\\x51\\a\\075\\0023-\\[&&[|\\022-\\xEA\\00-\\u41C2&&[^|a-\\xCC&&[^\\037\\uECB3\\u3D9A\\x31\\|\\<b\\0206\\uF2EC\\01m\\,\\ak\\a\\03&&\\p{Punct}]]]])(?-dxs:[|\\06-\\07|\\e-\\x63&&[|Tp\\u18A3\\00\\|\\xE4\\05\\061\\015\\0116C|\\r\\{\\}\\006\\xEA\\0367\\xC4\\01\\0042\\0267\\xBB\\01T\\}\\0100\\?[|\\[-\\u459B|\\x23\\x91\\rF\\0376[|\\?-\\x94\\0113-\\\\\\s]]]]{6}?)(?<=[^\\t-\\x42H\\04\\f\\03\\0172\\?i\\u97B6\\e\\f\\uDAC2])(?=\\.*+)(?>[^\\016\\r\\{\\,\\uA29D\\034\\02[\\02-\\[|\\t\\056\\uF599\\x62\\e\\<\\032\\uF0AC\\0026\\0205Q\\|\\\\\\06\\0164[|\\057-\\u7A98&&[\\061-g|\\|\\0276\\n\\042\\011\\e\\xE8\\x64B\\04\\u6D0EDW^\\p{Lower}]]]]?)(?<=[^\\n\\\\\\t\\u8E13\\,\\0114\\u656E\\xA5\\]&&[\\03-\\026|\\uF39D\\01\\{i\\u3BC2\\u14FE]])(?<=[^|\\uAE62\\054H\\|\\}&&^\\p{Space}])(?sxx)(?<=[\\f\\006\\a\\r\\xB4]{1,5})|(?x-xd:^{5}+)()";
586 // END android-changed
587 assertNotNull(Pattern.compile(pattern));
591 level = TestLevel.PARTIAL_COMPLETE,
592 notes = "Verifies the functionality of compile(java.lang.String) method.",
594 args = {java.lang.String.class}
596 public void testQuantCompilePos() {
597 String[] patterns = {/* "(abc){1,3}", */"abc{2,}", "abc{5}" };
598 for (String element : patterns) {
599 Pattern.compile(element);
604 level = TestLevel.PARTIAL_COMPLETE,
605 notes = "Verifies the functionality of compile() method. Also tested methods from matcher: matches(), start(int), group(int)",
607 args = {java.lang.String.class}
609 public void testQuantComposition() {
610 String pattern = "(a{1,3})aab";
611 java.util.regex.Pattern pat = java.util.regex.Pattern.compile(pattern);
612 java.util.regex.Matcher mat = pat.matcher("aaab");
619 level = TestLevel.PARTIAL_COMPLETE,
622 args = {java.lang.String.class, java.lang.CharSequence.class}
624 public void testMatches() {
625 String[][] posSeq = {
626 { "abb", "ababb", "abababbababb", "abababbababbabababbbbbabb" },
627 { "213567", "12324567", "1234567", "213213567",
628 "21312312312567", "444444567" },
629 { "abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab" },
630 { "213234567", "3458", "0987654", "7689546432", "0398576",
633 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
634 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
635 + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" },
636 { "ababbaAabababblice", "ababbaAliceababab", "ababbAabliceaaa",
637 "abbbAbbbliceaaa", "Alice" },
638 { "a123", "bnxnvgds156", "for", "while", "if", "struct" },
639 { "xy" }, { "xy" }, { "xcy" }
643 for (int i = 0; i < testPatterns.length; i++) {
644 for (int j = 0; j < posSeq[i].length; j++) {
645 assertTrue("Incorrect match: " + testPatterns[i] + " vs "
646 + posSeq[i][j], Pattern.matches(testPatterns[i],
653 level = TestLevel.PARTIAL_COMPLETE,
654 notes = "Verifies exception",
656 args = {java.lang.String.class, java.lang.CharSequence.class}
658 public void testMatchesException() {
659 /* Negative assertion test. */
660 for (String aPattern : wrongTestPatterns) {
662 Pattern.matches(aPattern, "Foo");
663 fail("PatternSyntaxException is expected");
664 } catch (PatternSyntaxException pse) {
666 } catch (Exception e) {
667 fail("Unexpected exception: " + e);
673 level = TestLevel.PARTIAL_COMPLETE,
674 notes = "The test verifies the functionality of matches(java.lang.String,java.lang.CharSequence) method.",
676 args = {java.lang.String.class, java.lang.CharSequence.class}
678 public void testTimeZoneIssue() {
679 Pattern p = Pattern.compile("GMT(\\+|\\-)(\\d+)(:(\\d+))?");
680 Matcher m = p.matcher("GMT-9:45");
681 assertTrue(m.matches());
682 assertEquals("-", m.group(1));
683 assertEquals("9", m.group(2));
684 assertEquals(":45", m.group(3));
685 assertEquals("45", m.group(4));
688 // BEGIN android-changed
689 // Removed one pattern that is buggy on the JDK. We don't want to duplicate that.
691 level = TestLevel.PARTIAL_COMPLETE,
692 notes = "Verifies the functionality of matches(java.lang.String,java.lang.CharSequence) method.",
694 args = {java.lang.String.class, java.lang.CharSequence.class}
696 public void testCompileRanges() {
697 String[] correctTestPatterns = { "[^]*abb]*", /* "[^a-d[^m-p]]*abb", */
698 "[a-d\\d]*abb", "[abc]*abb", "[a-e&&[de]]*abb", "[^abc]*abb",
699 "[a-e&&[^de]]*abb", "[a-z&&[^m-p]]*abb", "[a-d[m-p]]*abb",
700 "[a-zA-Z]*abb", "[+*?]*abb", "[^+*?]*abb" };
702 String[] inputSecuence = { "kkkk", /* "admpabb", */ "abcabcd124654abb",
703 "abcabccbacababb", "dededededededeedabb", "gfdhfghgdfghabb",
704 "accabacbcbaabb", "acbvfgtyabb", "adbcacdbmopabcoabb",
705 "jhfkjhaSDFGHJkdfhHNJMjkhfabb", "+*??+*abb", "sdfghjkabb" };
709 for (int i = 0; i < correctTestPatterns.length; i++) {
710 assertTrue("pattern: " + correctTestPatterns[i] + " input: "
711 + inputSecuence[i], Pattern.matches(correctTestPatterns[i],
716 String[] wrongInputSecuence = { "]", /* "admpkk", */ "abcabcd124k654abb",
717 "abwcabccbacababb", "abababdeababdeabb", "abcabcacbacbabb",
718 "acdcbecbaabb", "acbotyabb", "adbcaecdbmopabcoabb",
719 "jhfkjhaSDFGHJk;dfhHNJMjkhfabb", "+*?a?+*abb", "sdf+ghjkabb" };
721 for (int i = 0; i < correctTestPatterns.length; i++) {
722 assertFalse("pattern: " + correctTestPatterns[i] + " input: "
723 + wrongInputSecuence[i], Pattern.matches(
724 correctTestPatterns[i], wrongInputSecuence[i]));
730 level = TestLevel.PARTIAL_COMPLETE,
731 notes = "Verifies the functionality of matches(java.lang.String,java.lang.CharSequence) method for ranged patterns.",
733 args = {java.lang.String.class, java.lang.CharSequence.class}
735 public void testRangesSpecialCases() {
736 String neg_patterns[] = { "[a-&&[b-c]]", "[a-\\w]", "[b-a]", "[]" };
738 for (String element : neg_patterns) {
740 Pattern.compile(element);
741 fail("PatternSyntaxException was expected: " + element);
742 } catch (PatternSyntaxException pse) {
746 String pos_patterns[] = { "[-]+", "----", "[a-]+", "a-a-a-a-aa--",
747 "[\\w-a]+", "123-2312--aaa-213", "[a-]]+", "-]]]]]]]]]]]]]]]" };
749 for (int i = 0; i < pos_patterns.length; i++) {
750 String pat = pos_patterns[i++];
751 String inp = pos_patterns[i];
752 assertTrue("pattern: " + pat + " input: " + inp, Pattern.matches(
756 // END android-changed
759 level = TestLevel.PARTIAL_COMPLETE,
760 notes = "The test verifies the functionality of matches(java.lang.String,java.lang.CharSequence) method.",
762 args = {java.lang.String.class, java.lang.CharSequence.class}
764 public void testZeroSymbols() {
765 assertTrue(Pattern.matches("[\0]*abb", "\0\0\0\0\0\0abb"));
769 level = TestLevel.PARTIAL_COMPLETE,
770 notes = "Verifies the functionality of matcher(java.lang.String) method.",
772 args = {CharSequence.class}
774 public void testEscapes() {
775 Pattern pat = Pattern.compile("\\Q{]()*?");
776 Matcher mat = pat.matcher("{]()*?");
778 assertTrue(mat.matches());
782 level = TestLevel.PARTIAL_COMPLETE,
783 notes = "Verifies the functionality of compile(java.lang.String) method.",
785 args = {java.lang.String.class}
787 public void testBug181() {
788 Pattern.compile("[\\t-\\r]");
792 level = TestLevel.PARTIAL_COMPLETE,
793 notes = "Verifies the functionality of compile(java.lang.String) method.",
795 args = {java.lang.String.class}
797 public void testOrphanQuantifiers() {
799 Pattern.compile("+++++");
800 fail("PatternSyntaxException expected");
801 } catch (PatternSyntaxException pse) {
806 level = TestLevel.PARTIAL_COMPLETE,
807 notes = "Verifies the functionality of compile(java.lang.String) method.",
809 args = {java.lang.String.class}
811 public void testOrphanQuantifiers2() {
813 Pattern pat = Pattern.compile("\\d+*");
814 fail("PatternSyntaxException expected");
815 } catch (PatternSyntaxException pse) {
822 level = TestLevel.PARTIAL_COMPLETE,
823 notes = "Verifies the functionality of compile(java.lang.String) method.",
825 args = {java.lang.String.class}
828 level = TestLevel.PARTIAL_COMPLETE,
829 notes = "Verifies the functionality of compile(java.lang.String) method.",
831 args = {java.lang.String.class}
834 public void testBug197() {
835 Object[] vals = { ":", new Integer(2),
836 new String[] { "boo", "and:foo" }, ":", new Integer(5),
837 new String[] { "boo", "and", "foo" }, ":", new Integer(-2),
838 new String[] { "boo", "and", "foo" }, ":", new Integer(3),
839 new String[] { "boo", "and", "foo" }, ":", new Integer(1),
840 new String[] { "boo:and:foo" }, "o", new Integer(5),
841 new String[] { "b", "", ":and:f", "", "" }, "o",
842 new Integer(4), new String[] { "b", "", ":and:f", "o" }, "o",
843 new Integer(-2), new String[] { "b", "", ":and:f", "", "" },
844 "o", new Integer(0), new String[] { "b", "", ":and:f" } };
846 for (int i = 0; i < vals.length / 3;) {
847 String[] res = Pattern.compile(vals[i++].toString()).split(
848 "boo:and:foo", ((Integer) vals[i++]).intValue());
849 String[] expectedRes = (String[]) vals[i++];
851 assertEquals(expectedRes.length, res.length);
853 for (int j = 0; j < expectedRes.length; j++) {
854 assertEquals(expectedRes[j], res[j]);
861 level = TestLevel.PARTIAL_COMPLETE,
862 notes = "The test verifies the functionality of compile(java.lang.String) method.",
864 args = {java.lang.String.class}
866 public void testURIPatterns() {
867 String URI_REGEXP_STR = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
868 String SCHEME_REGEXP_STR = "^[a-zA-Z]{1}[\\w+-.]+$";
869 String REL_URI_REGEXP_STR = "^(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
870 String IPV6_REGEXP_STR = "^[0-9a-fA-F\\:\\.]+(\\%\\w+)?$";
871 String IPV6_REGEXP_STR2 = "^\\[[0-9a-fA-F\\:\\.]+(\\%\\w+)?\\]$";
872 String IPV4_REGEXP_STR = "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$";
873 String HOSTNAME_REGEXP_STR = "\\w+[\\w\\-\\.]*";
875 Pattern URI_REGEXP = Pattern.compile(URI_REGEXP_STR);
876 Pattern REL_URI_REGEXP = Pattern.compile(REL_URI_REGEXP_STR);
877 Pattern SCHEME_REGEXP = Pattern.compile(SCHEME_REGEXP_STR);
878 Pattern IPV4_REGEXP = Pattern.compile(IPV4_REGEXP_STR);
879 Pattern IPV6_REGEXP = Pattern.compile(IPV6_REGEXP_STR);
880 Pattern IPV6_REGEXP2 = Pattern.compile(IPV6_REGEXP_STR2);
881 Pattern HOSTNAME_REGEXP = Pattern.compile(HOSTNAME_REGEXP_STR);
885 level = TestLevel.PARTIAL_COMPLETE,
886 notes = "Verifies the functionality of compile(java.lang.String) method.",
888 args = {java.lang.String.class}
890 public void testFindBoundaryCases1() {
891 Pattern pat = Pattern.compile(".*\n");
892 Matcher mat = pat.matcher("a\n");
895 assertEquals("a\n", mat.group());
899 level = TestLevel.PARTIAL_COMPLETE,
900 notes = "Verifies the functionality of compile(java.lang.String) method.",
902 args = {java.lang.String.class}
904 public void testFindBoundaryCases2() {
905 Pattern pat = Pattern.compile(".*A");
906 Matcher mat = pat.matcher("aAa");
909 assertEquals("aA", mat.group());
913 level = TestLevel.PARTIAL_COMPLETE,
914 notes = "Verifies the functionality of compile(java.lang.String) method.",
916 args = {java.lang.String.class}
918 public void testFindBoundaryCases3() {
919 Pattern pat = Pattern.compile(".*A");
920 Matcher mat = pat.matcher("a\naA\n");
923 assertEquals("aA", mat.group());
927 level = TestLevel.PARTIAL_COMPLETE,
928 notes = "Verifies the functionality of compile(java.lang.String) method.",
930 args = {java.lang.String.class}
932 public void testFindBoundaryCases4() {
933 Pattern pat = Pattern.compile("A.*");
934 Matcher mat = pat.matcher("A\n");
937 assertEquals("A", mat.group());
941 level = TestLevel.PARTIAL_COMPLETE,
942 notes = "Verifies the functionality of compile(java.lang.String) method.",
944 args = {java.lang.String.class}
946 public void testFindBoundaryCases5() {
947 Pattern pat = Pattern.compile(".*A.*");
948 Matcher mat = pat.matcher("\nA\naaa\nA\naaAaa\naaaA\n");
949 // Matcher mat = pat.matcher("\nA\n");
950 String[] res = { "A", "A", "aaAaa", "aaaA" };
952 for (; mat.find(); k++) {
953 assertEquals(res[k], mat.group());
958 level = TestLevel.PARTIAL_COMPLETE,
959 notes = "Verifies the functionality of compile(java.lang.String) method.",
961 args = {java.lang.String.class}
963 public void testFindBoundaryCases6() {
964 String[] res = { "", "a", "", "" };
965 Pattern pat = Pattern.compile(".*");
966 Matcher mat = pat.matcher("\na\n");
969 for (; mat.find(); k++) {
970 assertEquals(res[k], mat.group());
976 level = TestLevel.PARTIAL_COMPLETE,
977 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
979 args = {java.lang.String.class}
982 level = TestLevel.PARTIAL_COMPLETE,
983 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
985 args = {java.lang.CharSequence.class}
988 public void testBackReferences() {
989 Pattern pat = Pattern.compile("(\\((\\w*):(.*):(\\2)\\))");
991 .matcher("(start1: word :start1)(start2: word :start2)");
993 for (; mat.find(); k++) {
994 assertEquals("start" + k, mat.group(2));
995 assertEquals(" word ", mat.group(3));
996 assertEquals("start" + k, mat.group(4));
1000 pat = Pattern.compile(".*(.)\\1");
1001 mat = pat.matcher("saa");
1002 assertTrue(mat.matches());
1007 level = TestLevel.PARTIAL_COMPLETE,
1008 notes = "Verifies the functionality of compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
1010 args = {java.lang.String.class, int.class}
1013 level = TestLevel.PARTIAL_COMPLETE,
1014 notes = "Verifies the functionality of compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
1016 args = {java.lang.CharSequence.class}
1019 public void testNewLine() {
1020 Pattern pat = Pattern.compile("(^$)*\n", Pattern.MULTILINE);
1021 Matcher mat = pat.matcher("\r\n\n");
1023 while (mat.find()) {
1026 assertEquals(2, counter);
1031 level = TestLevel.PARTIAL_COMPLETE,
1032 notes = "Verifies the functionality of compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
1034 args = {java.lang.String.class, int.class}
1037 level = TestLevel.PARTIAL_COMPLETE,
1038 notes = "Verifies the functionality of compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
1040 args = {java.lang.CharSequence.class}
1043 public void testFindGreedy() {
1044 Pattern pat = Pattern.compile(".*aaa", Pattern.DOTALL);
1045 Matcher mat = pat.matcher("aaaa\naaa\naaaaaa");
1047 assertEquals(15, mat.end());
1051 level = TestLevel.PARTIAL_COMPLETE,
1052 notes = "Verifies serialization/deserialization.",
1053 method = "!SerializationSelf",
1057 level = TestLevel.PARTIAL_COMPLETE,
1058 notes = "Verifies serialization/deserialization.",
1059 method = "!SerializationGolden",
1063 public void testSerialization() throws Exception {
1064 Pattern pat = Pattern.compile("a*bc");
1065 SerializableAssert comparator = new SerializableAssert() {
1066 public void assertDeserialized(Serializable initial,
1067 Serializable deserialized) {
1068 assertEquals(((Pattern) initial).toString(),
1069 ((Pattern) deserialized).toString());
1072 SerializationTest.verifyGolden(this, pat, comparator);
1073 SerializationTest.verifySelf(pat, comparator);
1078 level = TestLevel.PARTIAL_COMPLETE,
1079 notes = "Verifies the functionality of compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
1081 args = {java.lang.String.class, int.class}
1084 level = TestLevel.PARTIAL_COMPLETE,
1085 notes = "Verifies the functionality of compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
1087 args = {java.lang.CharSequence.class}
1090 public void testSOLQuant() {
1091 Pattern pat = Pattern.compile("$*", Pattern.MULTILINE);
1092 Matcher mat = pat.matcher("\n\n");
1094 while (mat.find()) {
1098 assertEquals(3, counter);
1102 level = TestLevel.PARTIAL_COMPLETE,
1103 notes = "Verifies the functionality of compile(java.lang.String) method.",
1105 args = {java.lang.String.class}
1107 public void testIllegalEscape() {
1109 Pattern.compile("\\y");
1110 fail("PatternSyntaxException expected");
1111 } catch (PatternSyntaxException pse) {
1116 level = TestLevel.PARTIAL_COMPLETE,
1117 notes = "Verifies the functionality of compile(java.lang.String) method.",
1119 args = {java.lang.String.class}
1121 public void testEmptyFamily() {
1122 Pattern.compile("\\p{Lower}");
1128 level = TestLevel.PARTIAL_COMPLETE,
1129 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1131 args = {java.lang.String.class}
1134 level = TestLevel.PARTIAL_COMPLETE,
1135 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1137 args = {java.lang.CharSequence.class}
1140 public void testNonCaptConstr() {
1142 Pattern pat = Pattern.compile("(?i)b*(?-i)a*");
1143 assertTrue(pat.matcher("bBbBaaaa").matches());
1144 assertFalse(pat.matcher("bBbBAaAa").matches());
1146 // Non-capturing groups
1147 pat = Pattern.compile("(?i:b*)a*");
1148 assertTrue(pat.matcher("bBbBaaaa").matches());
1149 assertFalse(pat.matcher("bBbBAaAa").matches());
1152 // 1 2 3 4 5 6 7 8 9 10 11
1153 .compile("(?:-|(-?\\d+\\d\\d\\d))?(?:-|-(\\d\\d))?(?:-|-(\\d\\d))?(T)?(?:(\\d\\d):(\\d\\d):(\\d\\d)(\\.\\d+)?)?(?:(?:((?:\\+|\\-)\\d\\d):(\\d\\d))|(Z))?");
1154 Matcher mat = pat.matcher("-1234-21-31T41:51:61.789+71:81");
1155 assertTrue(mat.matches());
1156 assertEquals("-1234", mat.group(1));
1157 assertEquals("21", mat.group(2));
1158 assertEquals("31", mat.group(3));
1159 assertEquals("T", mat.group(4));
1160 assertEquals("41", mat.group(5));
1161 assertEquals("51", mat.group(6));
1162 assertEquals("61", mat.group(7));
1163 assertEquals(".789", mat.group(8));
1164 assertEquals("+71", mat.group(9));
1165 assertEquals("81", mat.group(10));
1167 // positive lookahead
1168 pat = Pattern.compile(".*\\.(?=log$).*$");
1169 assertTrue(pat.matcher("a.b.c.log").matches());
1170 assertFalse(pat.matcher("a.b.c.log.").matches());
1172 // negative lookahead
1173 pat = Pattern.compile(".*\\.(?!log$).*$");
1174 assertFalse(pat.matcher("abc.log").matches());
1175 assertTrue(pat.matcher("abc.logg").matches());
1177 // positive lookbehind
1178 pat = Pattern.compile(".*(?<=abc)\\.log$");
1179 assertFalse(pat.matcher("cde.log").matches());
1180 assertTrue(pat.matcher("abc.log").matches());
1182 // negative lookbehind
1183 pat = Pattern.compile(".*(?<!abc)\\.log$");
1184 assertTrue(pat.matcher("cde.log").matches());
1185 assertFalse(pat.matcher("abc.log").matches());
1188 pat = Pattern.compile("(?>a*)abb");
1189 assertFalse(pat.matcher("aaabb").matches());
1190 pat = Pattern.compile("(?>a*)bb");
1191 assertTrue(pat.matcher("aaabb").matches());
1193 pat = Pattern.compile("(?>a|aa)aabb");
1194 assertTrue(pat.matcher("aaabb").matches());
1195 pat = Pattern.compile("(?>aa|a)aabb");
1196 assertFalse(pat.matcher("aaabb").matches());
1198 // BEGIN android-removed
1199 // Questionable constructs that ICU doesn't support.
1200 // // quantifiers over look ahead
1201 // pat = Pattern.compile(".*(?<=abc)*\\.log$");
1202 // assertTrue(pat.matcher("cde.log").matches());
1203 // pat = Pattern.compile(".*(?<=abc)+\\.log$");
1204 // assertFalse(pat.matcher("cde.log").matches());
1205 // END android-removed
1210 level = TestLevel.PARTIAL_COMPLETE,
1211 notes = "Verifies the functionality of compile(java.lang.String) method.",
1213 args = {java.lang.String.class}
1215 public void testCorrectReplacementBackreferencedJointSet() {
1216 Pattern pat = Pattern.compile("ab(a)*\\1");
1217 pat = Pattern.compile("abc(cd)fg");
1218 pat = Pattern.compile("aba*cd");
1219 pat = Pattern.compile("ab(a)*+cd");
1220 pat = Pattern.compile("ab(a)*?cd");
1221 pat = Pattern.compile("ab(a)+cd");
1222 pat = Pattern.compile(".*(.)\\1");
1223 pat = Pattern.compile("ab((a)|c|d)e");
1224 pat = Pattern.compile("abc((a(b))cd)");
1225 pat = Pattern.compile("ab(a)++cd");
1226 pat = Pattern.compile("ab(a)?(c)d");
1227 pat = Pattern.compile("ab(a)?+cd");
1228 pat = Pattern.compile("ab(a)??cd");
1229 pat = Pattern.compile("ab(a)??cd");
1230 pat = Pattern.compile("ab(a){1,3}?(c)d");
1235 level = TestLevel.PARTIAL_COMPLETE,
1236 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1238 args = {java.lang.String.class}
1241 level = TestLevel.PARTIAL_COMPLETE,
1242 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1244 args = {java.lang.CharSequence.class}
1247 public void testCompilePatternWithTerminatorMark() {
1248 Pattern pat = Pattern.compile("a\u0000\u0000cd");
1249 Matcher mat = pat.matcher("a\u0000\u0000cd");
1250 assertTrue(mat.matches());
1255 level = TestLevel.PARTIAL_COMPLETE,
1256 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1258 args = {java.lang.String.class}
1261 level = TestLevel.PARTIAL_COMPLETE,
1262 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1264 args = {java.lang.CharSequence.class}
1267 public void testAlternations() {
1268 String baseString = "|a|bc";
1269 Pattern pat = Pattern.compile(baseString);
1270 Matcher mat = pat.matcher("");
1272 assertTrue(mat.matches());
1274 baseString = "a||bc";
1275 pat = Pattern.compile(baseString);
1276 mat = pat.matcher("");
1277 assertTrue(mat.matches());
1279 baseString = "a|bc|";
1280 pat = Pattern.compile(baseString);
1281 mat = pat.matcher("");
1282 assertTrue(mat.matches());
1284 baseString = "a|b|";
1285 pat = Pattern.compile(baseString);
1286 mat = pat.matcher("");
1287 assertTrue(mat.matches());
1289 baseString = "a(|b|cd)e";
1290 pat = Pattern.compile(baseString);
1291 mat = pat.matcher("ae");
1292 assertTrue(mat.matches());
1294 baseString = "a(b||cd)e";
1295 pat = Pattern.compile(baseString);
1296 mat = pat.matcher("ae");
1297 assertTrue(mat.matches());
1299 baseString = "a(b|cd|)e";
1300 pat = Pattern.compile(baseString);
1301 mat = pat.matcher("ae");
1302 assertTrue(mat.matches());
1304 baseString = "a(b|c|)e";
1305 pat = Pattern.compile(baseString);
1306 mat = pat.matcher("ae");
1307 assertTrue(mat.matches());
1309 baseString = "a(|)e";
1310 pat = Pattern.compile(baseString);
1311 mat = pat.matcher("ae");
1312 assertTrue(mat.matches());
1315 pat = Pattern.compile(baseString);
1316 mat = pat.matcher("");
1317 assertTrue(mat.matches());
1319 baseString = "a(?:|)e";
1320 pat = Pattern.compile(baseString);
1321 mat = pat.matcher("ae");
1322 assertTrue(mat.matches());
1324 baseString = "a||||bc";
1325 pat = Pattern.compile(baseString);
1326 mat = pat.matcher("");
1327 assertTrue(mat.matches());
1329 baseString = "(?i-is)|a";
1330 pat = Pattern.compile(baseString);
1331 mat = pat.matcher("a");
1332 assertTrue(mat.matches());
1337 level = TestLevel.PARTIAL_COMPLETE,
1338 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1340 args = {java.lang.String.class}
1343 level = TestLevel.PARTIAL_COMPLETE,
1344 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1346 args = {java.lang.CharSequence.class}
1349 public void testMatchWithGroups() {
1350 String baseString = "jwkerhjwehrkwjehrkwjhrwkjehrjwkehrjkwhrkwehrkwhrkwrhwkhrwkjehr";
1351 String pattern = ".*(..).*\\1.*";
1352 assertTrue(Pattern.compile(pattern).matcher(baseString).matches());
1355 pattern = ".*(.)\\1";
1356 assertTrue(Pattern.compile(pattern).matcher(baseString).matches());
1357 assertTrue(Pattern.compile(pattern).matcher(baseString).find());
1361 level = TestLevel.PARTIAL_COMPLETE,
1362 notes = "Verifies split method for empty string.",
1364 args = {java.lang.CharSequence.class}
1366 public void testSplitEmptyCharSequence() {
1368 String[] arr = s1.split(":");
1369 assertEquals(arr.length, 1);
1374 level = TestLevel.PARTIAL_COMPLETE,
1375 notes = "The test verifies the functionality of compile(java.lang.String) & split(java.lang.CharSequence, int) methods.",
1377 args = {java.lang.String.class}
1380 level = TestLevel.PARTIAL_COMPLETE,
1381 notes = "The test verifies the functionality of compile(java.lang.String) & split(java.lang.CharSequence, int) methods.",
1383 args = {java.lang.CharSequence.class, int.class}
1386 public void testSplitEndsWithPattern() {
1387 assertEquals(",,".split(",", 3).length, 3);
1388 assertEquals(",,".split(",", 4).length, 3);
1390 assertEquals(Pattern.compile("o").split("boo:and:foo", 5).length, 5);
1391 assertEquals(Pattern.compile("b").split("ab", -1).length, 2);
1395 level = TestLevel.PARTIAL_COMPLETE,
1396 notes = "The test verifies the functionality of matches(java.lang.String), java.lang.CharSequence) method for case insensitive flags.",
1398 args = {java.lang.String.class, java.lang.CharSequence.class}
1400 public void testCaseInsensitiveFlag() {
1401 assertTrue(Pattern.matches("(?i-:AbC)", "ABC"));
1406 level = TestLevel.PARTIAL_COMPLETE,
1407 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1409 args = {java.lang.String.class}
1412 level = TestLevel.PARTIAL_COMPLETE,
1413 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1415 args = {java.lang.CharSequence.class}
1418 public void testEmptyGroups() {
1419 Pattern pat = Pattern.compile("ab(?>)cda");
1420 Matcher mat = pat.matcher("abcda");
1421 assertTrue(mat.matches());
1423 pat = Pattern.compile("ab()");
1424 mat = pat.matcher("ab");
1425 assertTrue(mat.matches());
1427 pat = Pattern.compile("abc(?:)(..)");
1428 mat = pat.matcher("abcgf");
1429 assertTrue(mat.matches());
1434 level = TestLevel.PARTIAL_COMPLETE,
1435 notes = "Verifies the functionality of compile(java.lang.String) & compile(java.lang.String, int) methods.",
1437 args = {java.lang.String.class}
1440 level = TestLevel.PARTIAL_COMPLETE,
1441 notes = "Verifies the functionality of compile(java.lang.String) & compile(java.lang.String, int) methods.",
1443 args = {java.lang.String.class, int.class}
1446 public void testCompileNonCaptGroup() {
1447 boolean isCompiled = false;
1450 // BEGIN android-change
1451 // We don't have canonical equivalence.
1452 Pattern pat = Pattern.compile("(?:)");
1453 pat = Pattern.compile("(?:)", Pattern.DOTALL);
1454 pat = Pattern.compile("(?:)", Pattern.CASE_INSENSITIVE);
1455 pat = Pattern.compile("(?:)", Pattern.COMMENTS | Pattern.UNIX_LINES);
1456 // END android-change
1458 } catch (PatternSyntaxException e) {
1459 System.out.println(e);
1461 assertTrue(isCompiled);
1466 level = TestLevel.PARTIAL_COMPLETE,
1467 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1469 args = {java.lang.String.class}
1472 level = TestLevel.PARTIAL_COMPLETE,
1473 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1475 args = {java.lang.CharSequence.class}
1478 public void testEmbeddedFlags() {
1479 String baseString = "(?i)((?s)a)";
1480 String testString = "A";
1481 Pattern pat = Pattern.compile(baseString);
1482 Matcher mat = pat.matcher(testString);
1483 assertTrue(mat.matches());
1485 baseString = "(?x)(?i)(?s)(?d)a";
1487 pat = Pattern.compile(baseString);
1488 mat = pat.matcher(testString);
1489 assertTrue(mat.matches());
1491 baseString = "(?x)(?i)(?s)(?d)a.";
1493 pat = Pattern.compile(baseString);
1494 mat = pat.matcher(testString);
1495 assertTrue(mat.matches());
1497 baseString = "abc(?x:(?i)(?s)(?d)a.)";
1498 testString = "abcA\n";
1499 pat = Pattern.compile(baseString);
1500 mat = pat.matcher(testString);
1501 assertTrue(mat.matches());
1503 baseString = "abc((?x)d)(?i)(?s)a";
1504 testString = "abcdA";
1505 pat = Pattern.compile(baseString);
1506 mat = pat.matcher(testString);
1507 assertTrue(mat.matches());
1511 level = TestLevel.PARTIAL_COMPLETE,
1512 notes = "Verifies the functionality of compile(java.lang.String) method.",
1514 args = {java.lang.String.class}
1516 public void testAltWithFlags() {
1517 boolean isCompiled = false;
1520 Pattern pat = Pattern.compile("|(?i-xi)|()");
1522 } catch (PatternSyntaxException e) {
1523 System.out.println(e);
1525 assertTrue(isCompiled);
1530 level = TestLevel.PARTIAL_COMPLETE,
1531 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1533 args = {java.lang.String.class}
1536 level = TestLevel.PARTIAL_COMPLETE,
1537 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1539 args = {java.lang.CharSequence.class}
1542 public void testRestoreFlagsAfterGroup() {
1543 String baseString = "abc((?x)d) a";
1544 String testString = "abcd a";
1545 Pattern pat = Pattern.compile(baseString);
1546 Matcher mat = pat.matcher(testString);
1548 assertTrue(mat.matches());
1552 * Verify if the Pattern support the following character classes:
1553 * \p{javaLowerCase} \p{javaUpperCase} \p{javaWhitespace} \p{javaMirrored}
1556 level = TestLevel.PARTIAL_COMPLETE,
1557 notes = "Verifies the functionality of compile(java.lang.String) method.",
1559 args = {java.lang.String.class}
1561 public void testCompileCharacterClass() {
1562 // Regression for HARMONY-606, 696
1563 Pattern pattern = Pattern.compile("\\p{javaLowerCase}");
1564 assertNotNull(pattern);
1566 pattern = Pattern.compile("\\p{javaUpperCase}");
1567 assertNotNull(pattern);
1569 pattern = Pattern.compile("\\p{javaWhitespace}");
1570 assertNotNull(pattern);
1572 pattern = Pattern.compile("\\p{javaMirrored}");
1573 assertNotNull(pattern);
1575 pattern = Pattern.compile("\\p{javaDefined}");
1576 assertNotNull(pattern);
1578 pattern = Pattern.compile("\\p{javaDigit}");
1579 assertNotNull(pattern);
1581 pattern = Pattern.compile("\\p{javaIdentifierIgnorable}");
1582 assertNotNull(pattern);
1584 pattern = Pattern.compile("\\p{javaISOControl}");
1585 assertNotNull(pattern);
1587 pattern = Pattern.compile("\\p{javaJavaIdentifierPart}");
1588 assertNotNull(pattern);
1590 pattern = Pattern.compile("\\p{javaJavaIdentifierStart}");
1591 assertNotNull(pattern);
1593 pattern = Pattern.compile("\\p{javaLetter}");
1594 assertNotNull(pattern);
1596 pattern = Pattern.compile("\\p{javaLetterOrDigit}");
1597 assertNotNull(pattern);
1599 pattern = Pattern.compile("\\p{javaSpaceChar}");
1600 assertNotNull(pattern);
1602 pattern = Pattern.compile("\\p{javaTitleCase}");
1603 assertNotNull(pattern);
1605 pattern = Pattern.compile("\\p{javaUnicodeIdentifierPart}");
1606 assertNotNull(pattern);
1608 pattern = Pattern.compile("\\p{javaUnicodeIdentifierStart}");
1609 assertNotNull(pattern);
1613 * s original test was fixed to pass on RI
1616 // BEGIN android-removed
1617 // We don't have canonical equivalence.
1618 // public void testCanonEqFlag() {
1621 // * for decompositions see
1622 // * http://www.unicode.org/Public/4.0-Update/UnicodeData-4.0.0.txt
1623 // * http://www.unicode.org/reports/tr15/#Decomposition
1625 // String baseString;
1626 // String testString;
1630 // baseString = "ab(a*)\\1";
1631 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1633 // baseString = "a(abcdf)d";
1634 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1636 // baseString = "aabcdfd";
1637 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1639 // // \u01E0 -> \u0226\u0304 ->\u0041\u0307\u0304
1640 // // \u00CC -> \u0049\u0300
1643 // * baseString = "\u01E0\u00CCcdb(ac)"; testString =
1644 // * "\u0226\u0304\u0049\u0300cdbac"; pat = Pattern.compile(baseString,
1645 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1646 // * assertTrue(mat.matches());
1648 // baseString = "\u01E0cdb(a\u00CCc)";
1649 // testString = "\u0041\u0307\u0304cdba\u0049\u0300c";
1650 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1651 // mat = pat.matcher(testString);
1652 // assertTrue(mat.matches());
1654 // baseString = "a\u00CC";
1655 // testString = "a\u0049\u0300";
1656 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1657 // mat = pat.matcher(testString);
1658 // assertTrue(mat.matches());
1661 // * baseString = "\u0226\u0304cdb(ac\u0049\u0300)"; testString =
1662 // * "\u01E0cdbac\u00CC"; pat = Pattern.compile(baseString,
1663 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1664 // * assertTrue(mat.matches());
1666 // * baseString = "cdb(?:\u0041\u0307\u0304\u00CC)"; testString =
1667 // * "cdb\u0226\u0304\u0049\u0300"; pat = Pattern.compile(baseString,
1668 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1669 // * assertTrue(mat.matches());
1671 // * baseString = "\u01E0[a-c]\u0049\u0300cdb(ac)"; testString =
1672 // * "\u01E0b\u00CCcdbac"; pat = Pattern.compile(baseString,
1673 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1674 // * assertTrue(mat.matches());
1676 // * baseString = "\u01E0|\u00CCcdb(ac)"; testString =
1677 // * "\u0041\u0307\u0304"; pat = Pattern.compile(baseString,
1678 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1679 // * assertTrue(mat.matches());
1681 // * baseString = "\u00CC?cdb(ac)*(\u01E0)*[a-c]"; testString =
1682 // * "cdb\u0041\u0307\u0304b"; pat = Pattern.compile(baseString,
1683 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1684 // * assertTrue(mat.matches());
1686 // baseString = "a\u0300";
1687 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1688 // mat = pat.matcher("a\u00E0a");
1689 // assertTrue(mat.find());
1692 // * baseString = "\u7B20\uF9F8abc"; pat = Pattern.compile(baseString,
1693 // * Pattern.CANON_EQ); mat = pat.matcher("\uF9F8\uF9F8abc");
1694 // * assertTrue(mat.matches());
1696 // * //\u01F9 -> \u006E\u0300 //\u00C3 -> \u0041\u0303
1698 // * baseString = "cdb(?:\u00C3\u006E\u0300)"; testString =
1699 // * "cdb\u0041\u0303\u01F9"; pat = Pattern.compile(baseString,
1700 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1701 // * assertTrue(mat.matches());
1703 // * //\u014C -> \u004F\u0304 //\u0163 -> \u0074\u0327
1705 // * baseString = "cdb(?:\u0163\u004F\u0304)"; testString =
1706 // * "cdb\u0074\u0327\u014C"; pat = Pattern.compile(baseString,
1707 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1708 // * assertTrue(mat.matches());
1710 // // \u00E1->a\u0301
1711 // // canonical ordering takes place \u0301\u0327 -> \u0327\u0301
1712 // baseString = "c\u0327\u0301";
1713 // testString = "c\u0301\u0327";
1714 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1715 // mat = pat.matcher(testString);
1716 // assertTrue(mat.matches());
1719 // * Hangul decompositions
1721 // // \uD4DB->\u1111\u1171\u11B6
1722 // // \uD21E->\u1110\u116D\u11B5
1723 // // \uD264->\u1110\u1170
1724 // // not Hangul:\u0453->\u0433\u0301
1725 // baseString = "a\uD4DB\u1111\u1171\u11B6\uD264";
1726 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1728 // baseString = "\u0453c\uD4DB";
1729 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1731 // baseString = "a\u1110\u116D\u11B5b\uD21Ebc";
1732 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1735 // * baseString = "\uD4DB\uD21E\u1110\u1170cdb(ac)"; testString =
1736 // * "\u1111\u1171\u11B6\u1110\u116D\u11B5\uD264cdbac"; pat =
1737 // * Pattern.compile(baseString, Pattern.CANON_EQ); mat =
1738 // * pat.matcher(testString); assertTrue(mat.matches());
1740 // baseString = "\uD4DB\uD264cdb(a\uD21Ec)";
1741 // testString = "\u1111\u1171\u11B6\u1110\u1170cdba\u1110\u116D\u11B5c";
1742 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1743 // mat = pat.matcher(testString);
1744 // assertTrue(mat.matches());
1746 // baseString = "a\uD4DB";
1747 // testString = "a\u1111\u1171\u11B6";
1748 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1749 // mat = pat.matcher(testString);
1750 // assertTrue(mat.matches());
1752 // baseString = "a\uD21E";
1753 // testString = "a\u1110\u116D\u11B5";
1754 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1755 // mat = pat.matcher(testString);
1756 // assertTrue(mat.matches());
1759 // * baseString = "\u1111\u1171\u11B6cdb(ac\u1110\u116D\u11B5)";
1760 // * testString = "\uD4DBcdbac\uD21E"; pat = Pattern.compile(baseString,
1761 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1762 // * assertTrue(mat.matches());
1764 // * baseString = "cdb(?:\u1111\u1171\u11B6\uD21E)"; testString =
1765 // * "cdb\uD4DB\u1110\u116D\u11B5"; pat = Pattern.compile(baseString,
1766 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1767 // * assertTrue(mat.matches());
1769 // * baseString = "\uD4DB[a-c]\u1110\u116D\u11B5cdb(ac)"; testString =
1770 // * "\uD4DBb\uD21Ecdbac"; pat = Pattern.compile(baseString,
1771 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1772 // * assertTrue(mat.matches());
1774 // baseString = "\uD4DB|\u00CCcdb(ac)";
1775 // testString = "\u1111\u1171\u11B6";
1776 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1777 // mat = pat.matcher(testString);
1778 // assertTrue(mat.matches());
1780 // baseString = "\uD4DB|\u00CCcdb(ac)";
1781 // testString = "\u1111\u1171";
1782 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1783 // mat = pat.matcher(testString);
1784 // assertFalse(mat.matches());
1786 // baseString = "\u00CC?cdb(ac)*(\uD4DB)*[a-c]";
1787 // testString = "cdb\u1111\u1171\u11B6b";
1788 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1789 // mat = pat.matcher(testString);
1790 // assertTrue(mat.matches());
1792 // baseString = "\uD4DB";
1793 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1794 // mat = pat.matcher("a\u1111\u1171\u11B6a");
1795 // assertTrue(mat.find());
1797 // baseString = "\u1111";
1798 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1799 // mat = pat.matcher("bcda\uD4DBr");
1800 // assertFalse(mat.find());
1804 // * s original test was fixed to pass on RI
1807 // public void testIndexesCanonicalEq() {
1808 // String baseString;
1809 // String testString;
1813 // baseString = "\uD4DB";
1814 // pat = Pattern.compile(baseString, Pattern.CANON_EQ);
1815 // mat = pat.matcher("bcda\u1111\u1171\u11B6awr");
1816 // assertTrue(mat.find());
1817 // assertEquals(mat.start(), 4);
1818 // assertEquals(mat.end(), 7);
1821 // * baseString = "\uD4DB\u1111\u1171\u11B6"; pat =
1822 // * Pattern.compile(baseString, Pattern.CANON_EQ); mat =
1823 // * pat.matcher("bcda\u1111\u1171\u11B6\uD4DBawr");
1824 // * assertTrue(mat.find()); assertEquals(mat.start(), 4);
1825 // * assertEquals(mat.end(), 8);
1827 // * baseString = "\uD4DB\uD21E\u1110\u1170"; testString =
1828 // * "abcabc\u1111\u1171\u11B6\u1110\u116D\u11B5\uD264cdbac"; pat =
1829 // * Pattern.compile(baseString, Pattern.CANON_EQ); mat =
1830 // * pat.matcher(testString); assertTrue(mat.find());
1831 // * assertEquals(mat.start(), 6); assertEquals(mat.end(), 13);
1835 // * s original test was fixed to pass on RI
1838 // public void testCanonEqFlagWithSupplementaryCharacters() {
1841 // * \u1D1BF->\u1D1BB\u1D16F->\u1D1B9\u1D165\u1D16F in UTF32
1842 // * \uD834\uDDBF->\uD834\uDDBB\uD834\uDD6F
1843 // * ->\uD834\uDDB9\uD834\uDD65\uD834\uDD6F in UTF16
1845 // String patString = "abc\uD834\uDDBFef";
1846 // String testString = "abc\uD834\uDDB9\uD834\uDD65\uD834\uDD6Fef";
1847 // Pattern pat = Pattern.compile(patString, Pattern.CANON_EQ);
1848 // Matcher mat = pat.matcher(testString);
1849 // assertTrue(mat.matches());
1851 // * testString = "abc\uD834\uDDBB\uD834\uDD6Fef"; mat =
1852 // * pat.matcher(testString); assertTrue(mat.matches());
1854 // * patString = "abc\uD834\uDDBB\uD834\uDD6Fef"; testString =
1855 // * "abc\uD834\uDDBFef"; pat = Pattern.compile(patString,
1856 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1857 // * assertTrue(mat.matches());
1859 // testString = "abc\uD834\uDDB9\uD834\uDD65\uD834\uDD6Fef";
1860 // mat = pat.matcher(testString);
1861 // assertTrue(mat.matches());
1863 // * patString = "abc\uD834\uDDB9\uD834\uDD65\uD834\uDD6Fef"; testString =
1864 // * "abc\uD834\uDDBFef"; pat = Pattern.compile(patString,
1865 // * Pattern.CANON_EQ); mat = pat.matcher(testString);
1866 // * assertTrue(mat.matches());
1868 // * testString = "abc\uD834\uDDBB\uD834\uDD6Fef"; mat =
1869 // * pat.matcher(testString); assertTrue(mat.matches());
1872 // * testSupplementary characters with no decomposition
1875 // * patString = "a\uD9A0\uDE8Ebc\uD834\uDDBB\uD834\uDD6Fe\uDE8Ef";
1876 // * testString = "a\uD9A0\uDE8Ebc\uD834\uDDBFe\uDE8Ef"; pat =
1877 // * Pattern.compile(patString, Pattern.CANON_EQ); mat =
1878 // * pat.matcher(testString); assertTrue(mat.matches());
1880 // END android-removed
1884 level = TestLevel.PARTIAL_COMPLETE,
1885 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1887 args = {java.lang.String.class}
1890 level = TestLevel.PARTIAL_COMPLETE,
1891 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1893 args = {java.lang.CharSequence.class}
1896 public void testRangesWithSurrogatesSupplementary() {
1897 String patString = "[abc\uD8D2]";
1898 String testString = "\uD8D2";
1899 Pattern pat = Pattern.compile(patString);
1900 Matcher mat = pat.matcher(testString);
1901 assertTrue(mat.matches());
1904 mat = pat.matcher(testString);
1905 assertTrue(mat.matches());
1907 testString = "ef\uD8D2\uDD71gh";
1908 mat = pat.matcher(testString);
1909 assertFalse(mat.find());
1911 testString = "ef\uD8D2gh";
1912 mat = pat.matcher(testString);
1913 assertTrue(mat.find());
1915 patString = "[abc\uD8D3&&[c\uD8D3]]";
1917 pat = Pattern.compile(patString);
1918 mat = pat.matcher(testString);
1919 assertTrue(mat.matches());
1922 mat = pat.matcher(testString);
1923 assertFalse(mat.matches());
1925 testString = "ef\uD8D3\uDD71gh";
1926 mat = pat.matcher(testString);
1927 assertFalse(mat.find());
1929 testString = "ef\uD8D3gh";
1930 mat = pat.matcher(testString);
1931 assertTrue(mat.find());
1933 patString = "[abc\uD8D3\uDBEE\uDF0C&&[c\uD8D3\uDBEE\uDF0C]]";
1935 pat = Pattern.compile(patString);
1936 mat = pat.matcher(testString);
1937 assertTrue(mat.matches());
1939 testString = "\uDBEE\uDF0C";
1940 mat = pat.matcher(testString);
1941 assertTrue(mat.matches());
1943 testString = "ef\uD8D3\uDD71gh";
1944 mat = pat.matcher(testString);
1945 assertFalse(mat.find());
1947 testString = "ef\uD8D3gh";
1948 mat = pat.matcher(testString);
1949 assertTrue(mat.find());
1951 patString = "[abc\uDBFC]\uDDC2cd";
1952 testString = "\uDBFC\uDDC2cd";
1953 pat = Pattern.compile(patString);
1954 mat = pat.matcher(testString);
1955 assertFalse(mat.matches());
1957 testString = "a\uDDC2cd";
1958 mat = pat.matcher(testString);
1959 assertTrue(mat.matches());
1964 level = TestLevel.PARTIAL_COMPLETE,
1965 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1967 args = {java.lang.String.class}
1970 level = TestLevel.PARTIAL_COMPLETE,
1971 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
1973 args = {java.lang.CharSequence.class}
1976 public void testSequencesWithSurrogatesSupplementary() {
1977 String patString = "abcd\uD8D3";
1978 String testString = "abcd\uD8D3\uDFFC";
1979 Pattern pat = Pattern.compile(patString);
1980 Matcher mat = pat.matcher(testString);
1981 // BEGIN android-changed
1982 // This one really doesn't make sense, as the above is a corrupt surrogate.
1983 // Even if it's matched by the JDK, it's more of a bug than of a behavior one
1984 // might want to duplicate.
1985 // assertFalse(mat.find());
1986 // END android-changed
1987 testString = "abcd\uD8D3abc";
1988 mat = pat.matcher(testString);
1989 assertTrue(mat.find());
1991 patString = "ab\uDBEFcd";
1992 testString = "ab\uDBEFcd";
1993 pat = Pattern.compile(patString);
1994 mat = pat.matcher(testString);
1995 assertTrue(mat.matches());
1997 patString = "\uDFFCabcd";
1998 testString = "\uD8D3\uDFFCabcd";
1999 pat = Pattern.compile(patString);
2000 mat = pat.matcher(testString);
2001 assertFalse(mat.find());
2003 testString = "abc\uDFFCabcdecd";
2004 mat = pat.matcher(testString);
2005 assertTrue(mat.find());
2007 patString = "\uD8D3\uDFFCabcd";
2008 testString = "abc\uD8D3\uD8D3\uDFFCabcd";
2009 pat = Pattern.compile(patString);
2010 mat = pat.matcher(testString);
2011 assertTrue(mat.find());
2015 * s original test was fixed to pass on RI
2020 level = TestLevel.PARTIAL_COMPLETE,
2021 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2023 args = {java.lang.String.class}
2026 level = TestLevel.PARTIAL_COMPLETE,
2027 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2029 args = {java.lang.CharSequence.class}
2032 public void testPredefinedClassesWithSurrogatesSupplementary() {
2033 String patString = "[123\\D]";
2034 String testString = "a";
2035 Pattern pat = Pattern.compile(patString);
2036 Matcher mat = pat.matcher(testString);
2037 assertTrue(mat.find());
2040 mat = pat.matcher(testString);
2041 assertFalse(mat.find());
2044 mat = pat.matcher(testString);
2045 assertTrue(mat.find());
2048 testString = "\uDFC4";
2049 mat = pat.matcher(testString);
2050 assertTrue(mat.find());
2053 testString = "\uDADA";
2054 mat = pat.matcher(testString);
2055 assertTrue(mat.find());
2057 testString = "\uDADA\uDFC4";
2058 mat = pat.matcher(testString);
2059 assertTrue(mat.find());
2061 patString = "[123[^\\p{javaDigit}]]";
2063 pat = Pattern.compile(patString);
2064 mat = pat.matcher(testString);
2065 assertTrue(mat.find());
2068 mat = pat.matcher(testString);
2069 assertFalse(mat.find());
2072 mat = pat.matcher(testString);
2073 assertTrue(mat.find());
2076 testString = "\uDFC4";
2077 mat = pat.matcher(testString);
2078 assertTrue(mat.find());
2081 testString = "\uDADA";
2082 mat = pat.matcher(testString);
2083 assertTrue(mat.find());
2085 testString = "\uDADA\uDFC4";
2086 mat = pat.matcher(testString);
2087 assertTrue(mat.find());
2089 // surrogate characters
2090 patString = "\\p{Cs}";
2091 testString = "\uD916\uDE27";
2092 pat = Pattern.compile(patString);
2093 mat = pat.matcher(testString);
2096 * see http://www.unicode.org/reports/tr18/#Supplementary_Characters we
2097 * have to treat text as code points not code units. \\p{Cs} matches any
2098 * surrogate character but here testString is a one code point
2099 * consisting of two code units (two surrogate characters) so we find
2102 // assertFalse(mat.find());
2103 // swap low and high surrogates
2104 testString = "\uDE27\uD916";
2105 mat = pat.matcher(testString);
2106 assertTrue(mat.find());
2108 patString = "[\uD916\uDE271\uD91623&&[^\\p{Cs}]]";
2110 pat = Pattern.compile(patString);
2111 mat = pat.matcher(testString);
2112 assertTrue(mat.find());
2114 testString = "\uD916";
2115 pat = Pattern.compile(patString);
2116 mat = pat.matcher(testString);
2117 assertFalse(mat.find());
2119 testString = "\uD916\uDE27";
2120 pat = Pattern.compile(patString);
2121 mat = pat.matcher(testString);
2122 assertTrue(mat.find());
2124 // \uD9A0\uDE8E=\u7828E
2125 // \u78281=\uD9A0\uDE81
2126 patString = "[a-\uD9A0\uDE8E]";
2127 testString = "\uD9A0\uDE81";
2128 pat = Pattern.compile(patString);
2129 mat = pat.matcher(testString);
2130 assertTrue(mat.matches());
2135 level = TestLevel.PARTIAL_COMPLETE,
2136 notes = "Verifies the functionality of compile(java.lang.String) & compile(java.lang.String, int) matcher(java.lang.CharSequence) methods.",
2138 args = {java.lang.String.class}
2141 level = TestLevel.PARTIAL_COMPLETE,
2142 notes = "Verifies the functionality of compile(java.lang.String) & compile(java.lang.String, int) matcher(java.lang.CharSequence) methods.",
2144 args = {java.lang.String.class, int.class}
2147 level = TestLevel.PARTIAL_COMPLETE,
2148 notes = "Verifies the functionality of compile(java.lang.String) & compile(java.lang.String, int) matcher(java.lang.CharSequence) methods.",
2150 args = {java.lang.CharSequence.class}
2153 public void testDotConstructionWithSurrogatesSupplementary() {
2154 String patString = ".";
2155 String testString = "\uD9A0\uDE81";
2156 Pattern pat = Pattern.compile(patString);
2157 Matcher mat = pat.matcher(testString);
2158 assertTrue(mat.matches());
2160 testString = "\uDE81";
2161 mat = pat.matcher(testString);
2162 assertTrue(mat.matches());
2164 testString = "\uD9A0";
2165 mat = pat.matcher(testString);
2166 assertTrue(mat.matches());
2169 mat = pat.matcher(testString);
2170 assertFalse(mat.matches());
2172 patString = ".*\uDE81";
2173 testString = "\uD9A0\uDE81\uD9A0\uDE81\uD9A0\uDE81";
2174 pat = Pattern.compile(patString);
2175 mat = pat.matcher(testString);
2176 assertFalse(mat.matches());
2178 testString = "\uD9A0\uDE81\uD9A0\uDE81\uDE81";
2179 mat = pat.matcher(testString);
2180 assertTrue(mat.matches());
2183 testString = "\uD9A0\uDE81\n\uD9A0\uDE81\uD9A0\n\uDE81";
2184 pat = Pattern.compile(patString, Pattern.DOTALL);
2185 mat = pat.matcher(testString);
2186 assertTrue(mat.matches());
2190 * s java.util.regex.Pattern.quote(String)
2193 level = TestLevel.COMPLETE,
2196 args = {java.lang.String.class}
2198 public void test_quoteLjava_lang_String() {
2199 for (String aPattern : testPatterns) {
2200 Pattern p = Pattern.compile(aPattern);
2202 assertEquals("quote was wrong for plain text", "\\Qtest\\E", p
2204 assertEquals("quote was wrong for text with quote sign",
2205 "\\Q\\Qtest\\E", p.quote("\\Qtest"));
2206 assertEquals("quote was wrong for quotted text",
2207 "\\Q\\Qtest\\E\\\\E\\Q\\E", p.quote("\\Qtest\\E"));
2208 } catch (Exception e) {
2209 fail("Unexpected exception: " + e);
2215 * s java.util.regex.Pattern.matcher(String, CharSequence)
2216 * coped from test for matches method
2221 level = TestLevel.PARTIAL_COMPLETE,
2222 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2224 args = {java.lang.String.class}
2227 level = TestLevel.PARTIAL_COMPLETE,
2228 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2230 args = {java.lang.CharSequence.class}
2233 public void test_matcherLjava_lang_StringLjava_lang_CharSequence() {
2234 String[][] posSeq = {
2235 { "abb", "ababb", "abababbababb", "abababbababbabababbbbbabb" },
2236 { "213567", "12324567", "1234567", "213213567",
2237 "21312312312567", "444444567" },
2238 { "abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab" },
2239 { "213234567", "3458", "0987654", "7689546432", "0398576",
2242 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
2243 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2244 + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" },
2245 { "ababbaAabababblice", "ababbaAliceababab", "ababbAabliceaaa",
2246 "abbbAbbbliceaaa", "Alice" },
2247 { "a123", "bnxnvgds156", "for", "while", "if", "struct" },
2248 { "xy" }, { "xy" }, { "xcy" }
2252 for (int i = 0; i < testPatterns.length; i++) {
2253 for (int j = 0; j < posSeq[i].length; j++) {
2254 assertTrue("Incorrect match: " + testPatterns[i] + " vs "
2255 + posSeq[i][j], Pattern.compile(testPatterns[i])
2256 .matcher(posSeq[i][j]).matches());
2263 level = TestLevel.PARTIAL_COMPLETE,
2264 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2266 args = {java.lang.String.class}
2269 level = TestLevel.PARTIAL_COMPLETE,
2270 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2272 args = {java.lang.CharSequence.class}
2275 public void testQuantifiersWithSurrogatesSupplementary() {
2276 String patString = "\uD9A0\uDE81*abc";
2277 String testString = "\uD9A0\uDE81\uD9A0\uDE81abc";
2278 Pattern pat = Pattern.compile(patString);
2279 Matcher mat = pat.matcher(testString);
2280 assertTrue(mat.matches());
2283 mat = pat.matcher(testString);
2284 assertTrue(mat.matches());
2289 level = TestLevel.PARTIAL_COMPLETE,
2290 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2292 args = {java.lang.String.class}
2295 level = TestLevel.PARTIAL_COMPLETE,
2296 notes = "Verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2298 args = {java.lang.CharSequence.class}
2301 public void testAlternationsWithSurrogatesSupplementary() {
2302 String patString = "\uDE81|\uD9A0\uDE81|\uD9A0";
2303 String testString = "\uD9A0";
2304 Pattern pat = Pattern.compile(patString);
2305 Matcher mat = pat.matcher(testString);
2306 assertTrue(mat.matches());
2308 testString = "\uDE81";
2309 mat = pat.matcher(testString);
2310 assertTrue(mat.matches());
2312 testString = "\uD9A0\uDE81";
2313 mat = pat.matcher(testString);
2314 assertTrue(mat.matches());
2316 testString = "\uDE81\uD9A0";
2317 mat = pat.matcher(testString);
2318 assertFalse(mat.matches());
2323 level = TestLevel.PARTIAL_COMPLETE,
2324 notes = "Verifies the functionality of compile(java.lang.String) & compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
2326 args = {java.lang.String.class}
2329 level = TestLevel.PARTIAL_COMPLETE,
2330 notes = "Verifies the functionality of compile(java.lang.String) & compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
2332 args = {java.lang.String.class, int.class}
2335 level = TestLevel.PARTIAL_COMPLETE,
2336 notes = "Verifies the functionality of compile(java.lang.String) & compile(java.lang.String, int) & matcher(java.lang.CharSequence) methods.",
2338 args = {java.lang.CharSequence.class}
2341 public void testGroupsWithSurrogatesSupplementary() {
2343 //this pattern matches nothing
2344 String patString = "(\uD9A0)\uDE81";
2345 String testString = "\uD9A0\uDE81";
2346 Pattern pat = Pattern.compile(patString);
2347 Matcher mat = pat.matcher(testString);
2348 assertFalse(mat.matches());
2350 patString = "(\uD9A0)";
2351 testString = "\uD9A0\uDE81";
2352 pat = Pattern.compile(patString, Pattern.DOTALL);
2353 mat = pat.matcher(testString);
2354 assertFalse(mat.find());
2358 * Regression test for HARMONY-688
2362 level = TestLevel.PARTIAL_COMPLETE,
2363 notes = "The test verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2365 args = {java.lang.String.class}
2368 level = TestLevel.PARTIAL_COMPLETE,
2369 notes = "The test verifies the functionality of compile(java.lang.String) & matcher(java.lang.CharSequence) methods.",
2371 args = {java.lang.CharSequence.class}
2374 public void testUnicodeCategoryWithSurrogatesSupplementary() {
2375 Pattern p = Pattern.compile("\\p{javaLowerCase}");
2376 Matcher matcher = p.matcher("\uD801\uDC28");
2377 assertTrue(matcher.find());
2381 level = TestLevel.PARTIAL_COMPLETE,
2382 notes = "The test doesn't verify Matcher and should be moved to PatterTest.",
2384 args = {java.lang.CharSequence.class, int.class}
2386 public void testSplitEmpty() {
2388 Pattern pat = Pattern.compile("");
2389 String[] s = pat.split("", -1);
2391 assertEquals(1, s.length);
2392 assertEquals("", s[0]);
2396 level = TestLevel.COMPLETE,
2398 method = "toString",
2401 public void testToString() {
2402 for (int i = 0; i < testPatterns.length; i++) {
2403 Pattern p = Pattern.compile(testPatterns[i]);
2404 assertEquals(testPatterns[i], p.toString());