MatcherTest.java revision 3c51cabb4225fd090e7d559c1c991cacbeffd5c5
1/*
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18package org.apache.harmony.tests.java.util.regex;
19
20import java.util.regex.Matcher;
21import java.util.regex.Pattern;
22
23import junit.framework.TestCase;
24
25public class MatcherTest extends TestCase {
26    String[] testPatterns = {
27            "(a|b)*abb",
28            "(1*2*3*4*)*567",
29            "(a|b|c|d)*aab",
30            "(1|2|3|4|5|6|7|8|9|0)(1|2|3|4|5|6|7|8|9|0)*",
31            "(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)*",
32            "(a|b)*(a|b)*A(a|b)*lice.*",
33            "(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|"
34                    + "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" };
35
36    String[] groupPatterns = { "(a|b)*aabb", "((a)|b)*aabb", "((a|b)*)a(abb)",
37            "(((a)|(b))*)aabb", "(((a)|(b))*)aa(b)b", "(((a)|(b))*)a(a(b)b)" };
38
39    public MatcherTest(String name) {
40        super(name);
41    }
42
43    public void testRegionsIntInt() {
44        Pattern p = Pattern.compile("x*");
45        Matcher m = p.matcher("axxxxxa");
46        assertFalse(m.matches());
47
48        m.region(1, 6);
49        assertEquals(1, m.regionStart());
50        assertEquals(6, m.regionEnd());
51        assertTrue(m.matches());
52
53        try {
54            m.region(1, 0);
55            fail("expected an IOOBE");
56        } catch(IndexOutOfBoundsException e) {
57        }
58
59        try {
60            m.region(-1, 2);
61            fail("expected an IOOBE");
62        } catch(IndexOutOfBoundsException e) {
63        }
64
65        try {
66            m.region(10, 11);
67            fail("expected an IOOBE");
68        } catch(IndexOutOfBoundsException e) {
69        }
70
71        try {
72            m.region(1, 10);
73            fail("expected an IOOBE");
74        } catch(IndexOutOfBoundsException e) {
75        }
76    }
77
78    public void testAppendReplacement() {
79        Pattern pat = Pattern.compile("XX");
80        Matcher m = pat.matcher("Today is XX-XX-XX ...");
81        StringBuffer sb = new StringBuffer();
82
83        for (int i = 0; m.find(); i++) {
84            m.appendReplacement(sb, new Integer(i * 10 + i).toString());
85        }
86        m.appendTail(sb);
87        assertEquals("Today is 0-11-22 ...", sb.toString());
88    }
89
90    public void testAppendReplacementRef() {
91        Pattern p = Pattern.compile("xx (rur|\\$)");
92        Matcher m = p.matcher("xx $ equals to xx rur.");
93        StringBuffer sb = new StringBuffer();
94        for (int i = 1; m.find(); i *= 30) {
95            String rep = new Integer(i).toString() + " $1";
96            m.appendReplacement(sb, rep);
97        }
98        m.appendTail(sb);
99        assertEquals("1 $ equals to 30 rur.", sb.toString());
100    }
101
102    public void testReplaceAll() {
103        String input = "aabfooaabfooabfoob";
104        String pattern = "a*b";
105        Pattern pat = Pattern.compile(pattern);
106        Matcher mat = pat.matcher(input);
107
108        assertEquals("-foo-foo-foo-", mat.replaceAll("-"));
109    }
110
111    public void testResetCharSequence() {
112        Pattern p = Pattern.compile("abcd");
113        Matcher m = p.matcher("abcd");
114        assertTrue(m.matches());
115        m.reset("efgh");
116        assertFalse(m.matches());
117
118        try {
119            m.reset(null);
120            fail("expected a NPE");
121        } catch (NullPointerException e) {
122        }
123    }
124
125    public void testAppendSlashes() {
126        Pattern p = Pattern.compile("\\\\");
127        Matcher m = p.matcher("one\\cat\\two\\cats\\in\\the\\yard");
128        StringBuffer sb = new StringBuffer();
129        while (m.find()) {
130            m.appendReplacement(sb, "\\\\");
131        }
132        m.appendTail(sb);
133        assertEquals("one\\cat\\two\\cats\\in\\the\\yard", sb.toString());
134
135    }
136
137    public void testReplaceFirst() {
138        String input = "zzzdogzzzdogzzz";
139        String pattern = "dog";
140        Pattern pat = Pattern.compile(pattern);
141        Matcher mat = pat.matcher(input);
142
143        assertEquals("zzzcatzzzdogzzz", mat.replaceFirst("cat"));
144    }
145
146    public void testPattern() {
147        for (String element : testPatterns) {
148            Pattern test = Pattern.compile(element);
149            assertEquals(test, test.matcher("aaa").pattern());
150        }
151
152        for (String element : testPatterns) {
153            assertEquals(element, Pattern.compile(element).matcher("aaa")
154                    .pattern().toString());
155        }
156    }
157
158    public void testReset() {
159    }
160
161    public void testGroupint() {
162        String positiveTestString = "ababababbaaabb";
163
164        // test IndexOutOfBoundsException
165        // //
166        for (int i = 0; i < groupPatterns.length; i++) {
167            Pattern test = Pattern.compile(groupPatterns[i]);
168            Matcher mat = test.matcher(positiveTestString);
169            mat.matches();
170            try {
171                // groupPattern <index + 1> equals to number of groups
172                // of the specified pattern
173                // //
174                mat.group(i + 2);
175                fail("IndexOutBoundsException expected");
176                mat.group(i + 100);
177                fail("IndexOutBoundsException expected");
178                mat.group(-1);
179                fail("IndexOutBoundsException expected");
180                mat.group(-100);
181                fail("IndexOutBoundsException expected");
182            } catch (IndexOutOfBoundsException iobe) {
183            }
184        }
185
186        String[][] groupResults = { { "a" }, { "a", "a" },
187                { "ababababba", "a", "abb" }, { "ababababba", "a", "a", "b" },
188                { "ababababba", "a", "a", "b", "b" },
189                { "ababababba", "a", "a", "b", "abb", "b" }, };
190
191        for (int i = 0; i < groupPatterns.length; i++) {
192            Pattern test = Pattern.compile(groupPatterns[i]);
193            Matcher mat = test.matcher(positiveTestString);
194            mat.matches();
195            for (int j = 0; j < groupResults[i].length; j++) {
196                assertEquals("i: " + i + " j: " + j, groupResults[i][j], mat
197                        .group(j + 1));
198            }
199
200        }
201
202    }
203
204    public void testGroup() {
205        String positiveTestString = "ababababbaaabb";
206        String negativeTestString = "gjhfgdsjfhgcbv";
207        for (String element : groupPatterns) {
208            Pattern test = Pattern.compile(element);
209            Matcher mat = test.matcher(positiveTestString);
210            mat.matches();
211            // test result
212            assertEquals(positiveTestString, mat.group());
213
214            // test equal to group(0) result
215            assertEquals(mat.group(0), mat.group());
216        }
217
218        for (String element : groupPatterns) {
219            Pattern test = Pattern.compile(element);
220            Matcher mat = test.matcher(negativeTestString);
221            mat.matches();
222            try {
223                mat.group();
224                fail("IllegalStateException expected for <false> matches result");
225            } catch (IllegalStateException ise) {
226            }
227        }
228    }
229
230    public void testGroupPossessive() {
231        Pattern pat = Pattern.compile("((a)|(b))++c");
232        Matcher mat = pat.matcher("aac");
233
234        mat.matches();
235        assertEquals("a", mat.group(1));
236    }
237
238    public void testMatchesMisc() {
239        String[][] posSeq = {
240                { "abb", "ababb", "abababbababb", "abababbababbabababbbbbabb" },
241                { "213567", "12324567", "1234567", "213213567",
242                        "21312312312567", "444444567" },
243                { "abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab" },
244                { "213234567", "3458", "0987654", "7689546432", "0398576",
245                        "98432", "5" },
246                {
247                        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
248                        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
249                                + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" },
250                { "ababbaAabababblice", "ababbaAliceababab", "ababbAabliceaaa",
251                        "abbbAbbbliceaaa", "Alice" },
252                { "a123", "bnxnvgds156", "for", "while", "if", "struct" }
253
254        };
255
256        for (int i = 0; i < testPatterns.length; i++) {
257            Pattern pat = Pattern.compile(testPatterns[i]);
258            for (int j = 0; j < posSeq[i].length; j++) {
259                Matcher mat = pat.matcher(posSeq[i][j]);
260                assertTrue("Incorrect match: " + testPatterns[i] + " vs "
261                        + posSeq[i][j], mat.matches());
262            }
263        }
264    }
265
266    public void testMatchesQuantifiers() {
267        String[] testPatternsSingles = { "a{5}", "a{2,4}", "a{3,}" };
268        String[] testPatternsMultiple = { "((a)|(b)){1,2}abb",
269                "((a)|(b)){2,4}", "((a)|(b)){3,}" };
270
271        String[][] stringSingles = { { "aaaaa", "aaa" },
272                { "aa", "a", "aaa", "aaaaaa", "aaaa", "aaaaa" },
273                { "aaa", "a", "aaaa", "aa" }, };
274
275        String[][] stringMultiples = { { "ababb", "aba" },
276                { "ab", "b", "bab", "ababa", "abba", "abababbb" },
277                { "aba", "b", "abaa", "ba" }, };
278
279        for (int i = 0; i < testPatternsSingles.length; i++) {
280            Pattern pat = Pattern.compile(testPatternsSingles[i]);
281            for (int j = 0; j < stringSingles.length / 2; j++) {
282                assertTrue("Match expected, but failed: " + pat.pattern()
283                        + " : " + stringSingles[i][j], pat.matcher(
284                        stringSingles[i][j * 2]).matches());
285                assertFalse("Match failure expected, but match succeed: "
286                        + pat.pattern() + " : " + stringSingles[i][j * 2 + 1],
287                        pat.matcher(stringSingles[i][j * 2 + 1]).matches());
288            }
289        }
290
291        for (int i = 0; i < testPatternsMultiple.length; i++) {
292            Pattern pat = Pattern.compile(testPatternsMultiple[i]);
293            for (int j = 0; j < stringMultiples.length / 2; j++) {
294                assertTrue("Match expected, but failed: " + pat.pattern()
295                        + " : " + stringMultiples[i][j], pat.matcher(
296                        stringMultiples[i][j * 2]).matches());
297                assertFalse(
298                        "Match failure expected, but match succeed: "
299                                + pat.pattern() + " : "
300                                + stringMultiples[i][j * 2 + 1], pat.matcher(
301                                stringMultiples[i][j * 2 + 1]).matches());
302            }
303        }
304    }
305
306    public void testQuantVsGroup() {
307        String patternString = "(d{1,3})((a|c)*)(d{1,3})((a|c)*)(d{1,3})";
308        String testString = "dacaacaacaaddaaacaacaaddd";
309
310        Pattern pat = Pattern.compile(patternString);
311        Matcher mat = pat.matcher(testString);
312
313        mat.matches();
314        assertEquals("dacaacaacaaddaaacaacaaddd", mat.group());
315        assertEquals("d", mat.group(1));
316        assertEquals("acaacaacaa", mat.group(2));
317        assertEquals("dd", mat.group(4));
318        assertEquals("aaacaacaa", mat.group(5));
319        assertEquals("ddd", mat.group(7));
320    }
321
322    public void testLookingAt() {
323    }
324
325    public void testFind() {
326        String testPattern = "(abb)";
327        String testString = "cccabbabbabbabbabb";
328        Pattern pat = Pattern.compile(testPattern);
329        Matcher mat = pat.matcher(testString);
330        int start = 3;
331        int end = 6;
332        while (mat.find()) {
333            assertEquals(start, mat.start(1));
334            assertEquals(end, mat.end(1));
335
336            start = end;
337            end += 3;
338        }
339
340        testPattern = "(\\d{1,3})";
341        testString = "aaaa123456789045";
342
343        Pattern pat2 = Pattern.compile(testPattern);
344        Matcher mat2 = pat2.matcher(testString);
345        start = 4;
346        int length = 3;
347        while (mat2.find()) {
348            assertEquals(testString.substring(start, start + length), mat2
349                    .group(1));
350            start += length;
351        }
352    }
353
354    public void testSEOLsymbols() {
355        Pattern pat = Pattern.compile("^a\\(bb\\[$");
356        Matcher mat = pat.matcher("a(bb[");
357
358        assertTrue(mat.matches());
359    }
360
361    public void testGroupCount() {
362        for (int i = 0; i < groupPatterns.length; i++) {
363            Pattern test = Pattern.compile(groupPatterns[i]);
364            Matcher mat = test.matcher("ababababbaaabb");
365            mat.matches();
366            assertEquals(i + 1, mat.groupCount());
367
368        }
369    }
370
371    public void testRelactantQuantifiers() {
372        Pattern pat = Pattern.compile("(ab*)*b");
373        Matcher mat = pat.matcher("abbbb");
374
375        if (mat.matches()) {
376            assertEquals("abbb", mat.group(1));
377        } else {
378            fail("Match expected: (ab*)*b vs abbbb");
379        }
380    }
381
382    public void testEnhancedFind() {
383        String input = "foob";
384        String pattern = "a*b";
385        Pattern pat = Pattern.compile(pattern);
386        Matcher mat = pat.matcher(input);
387
388        mat.find();
389        assertEquals("b", mat.group());
390    }
391
392    public void testPosCompositeGroup() {
393        String[] posExamples = { "aabbcc", "aacc", "bbaabbcc" };
394        String[] negExamples = { "aabb", "bb", "bbaabb" };
395        Pattern posPat = Pattern.compile("(aa|bb){1,3}+cc");
396        Pattern negPat = Pattern.compile("(aa|bb){1,3}+bb");
397
398        Matcher mat;
399        for (String element : posExamples) {
400            mat = posPat.matcher(element);
401            assertTrue(mat.matches());
402        }
403
404        for (String element : negExamples) {
405            mat = negPat.matcher(element);
406            assertFalse(mat.matches());
407        }
408
409        assertTrue(Pattern.matches("(aa|bb){1,3}+bb", "aabbaabb"));
410
411    }
412
413    public void testPosAltGroup() {
414        String[] posExamples = { "aacc", "bbcc", "cc" };
415        String[] negExamples = { "bb", "aa" };
416        Pattern posPat = Pattern.compile("(aa|bb)?+cc");
417        Pattern negPat = Pattern.compile("(aa|bb)?+bb");
418
419        Matcher mat;
420        for (String element : posExamples) {
421            mat = posPat.matcher(element);
422            assertTrue(posPat.toString() + " vs: " + element, mat.matches());
423        }
424
425        for (String element : negExamples) {
426            mat = negPat.matcher(element);
427            assertFalse(mat.matches());
428        }
429
430        assertTrue(Pattern.matches("(aa|bb)?+bb", "aabb"));
431    }
432
433    public void testRelCompGroup() {
434
435        Matcher mat;
436        Pattern pat;
437        String res = "";
438        for (int i = 0; i < 4; i++) {
439            pat = Pattern.compile("((aa|bb){" + i + ",3}?).*cc");
440            mat = pat.matcher("aaaaaacc");
441            assertTrue(pat.toString() + " vs: " + "aaaaaacc", mat.matches());
442            assertEquals(res, mat.group(1));
443            res += "aa";
444        }
445    }
446
447    public void testRelAltGroup() {
448
449        Matcher mat;
450        Pattern pat;
451
452        pat = Pattern.compile("((aa|bb)??).*cc");
453        mat = pat.matcher("aacc");
454        assertTrue(pat.toString() + " vs: " + "aacc", mat.matches());
455        assertEquals("", mat.group(1));
456
457        pat = Pattern.compile("((aa|bb)??)cc");
458        mat = pat.matcher("aacc");
459        assertTrue(pat.toString() + " vs: " + "aacc", mat.matches());
460        assertEquals("aa", mat.group(1));
461    }
462
463    public void testIgnoreCase() {
464        Pattern pat = Pattern.compile("(aa|bb)*", Pattern.CASE_INSENSITIVE);
465        Matcher mat = pat.matcher("aAbb");
466
467        assertTrue(mat.matches());
468
469        pat = Pattern.compile("(a|b|c|d|e)*", Pattern.CASE_INSENSITIVE);
470        mat = pat.matcher("aAebbAEaEdebbedEccEdebbedEaedaebEbdCCdbBDcdcdADa");
471        assertTrue(mat.matches());
472
473        pat = Pattern.compile("[a-e]*", Pattern.CASE_INSENSITIVE);
474        mat = pat.matcher("aAebbAEaEdebbedEccEdebbedEaedaebEbdCCdbBDcdcdADa");
475        assertTrue(mat.matches());
476
477    }
478
479    public void testQuoteReplacement() {
480        assertEquals("\\\\aaCC\\$1", Matcher.quoteReplacement("\\aaCC$1"));
481    }
482
483    public void testOverFlow() {
484        Pattern tp = Pattern.compile("(a*)*");
485        Matcher tm = tp.matcher("aaa");
486        assertTrue(tm.matches());
487        assertEquals("", tm.group(1));
488
489        assertTrue(Pattern.matches("(1+)\\1+", "11"));
490        assertTrue(Pattern.matches("(1+)(2*)\\2+", "11"));
491
492        Pattern pat = Pattern.compile("(1+)\\1*");
493        Matcher mat = pat.matcher("11");
494
495        assertTrue(mat.matches());
496        assertEquals("11", mat.group(1));
497
498        pat = Pattern.compile("((1+)|(2+))(\\2+)");
499        mat = pat.matcher("11");
500
501        assertTrue(mat.matches());
502        assertEquals("1", mat.group(2));
503        assertEquals("1", mat.group(1));
504        assertEquals("1", mat.group(4));
505        assertNull(mat.group(3));
506
507    }
508
509    public void testUnicode() {
510
511        assertTrue(Pattern.matches("\\x61a", "aa"));
512        assertTrue(Pattern.matches("\\u0061a", "aa"));
513        assertTrue(Pattern.matches("\\0141a", "aa"));
514        assertTrue(Pattern.matches("\\0777", "?7"));
515
516    }
517
518    public void testUnicodeCategory() {
519        assertTrue(Pattern.matches("\\p{Ll}", "k")); // Unicode lower case
520        assertTrue(Pattern.matches("\\P{Ll}", "K")); // Unicode non-lower
521        // case
522        assertTrue(Pattern.matches("\\p{Lu}", "K")); // Unicode upper case
523        assertTrue(Pattern.matches("\\P{Lu}", "k")); // Unicode non-upper
524        // case
525        // combinations
526        assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Lu}]]", "k"));
527        assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Ll}]]", "K"));
528        assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Lu}]]", "K"));
529        assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Ll}]]", "k"));
530
531        // category/character combinations
532        assertFalse(Pattern.matches("[\\p{L}&&[^a-z]]", "k"));
533        assertTrue(Pattern.matches("[\\p{L}&&[^a-z]]", "K"));
534
535        assertTrue(Pattern.matches("[\\p{Lu}a-z]", "k"));
536        assertTrue(Pattern.matches("[a-z\\p{Lu}]", "k"));
537
538        assertFalse(Pattern.matches("[\\p{Lu}a-d]", "k"));
539        assertTrue(Pattern.matches("[a-d\\p{Lu}]", "K"));
540
541        // assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Lu}&&[^K]]]", "K"));
542        assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Lu}&&[^G]]]", "K"));
543
544    }
545
546    public void testSplitEmpty() {
547
548        Pattern pat = Pattern.compile("");
549        String[] s = pat.split("", -1);
550
551        assertEquals(1, s.length);
552        assertEquals("", s[0]);
553    }
554
555    public void testFindDollar() {
556        Matcher mat = Pattern.compile("a$").matcher("a\n");
557        assertTrue(mat.find());
558        assertEquals("a", mat.group());
559    }
560
561    public void testMatchesRegionChanged() {
562        // Regression for HARMONY-610
563        // Verify if the Matcher can match the input when region is changed
564        String input = " word ";
565        Pattern pattern = Pattern.compile("\\w+");
566        Matcher matcher = pattern.matcher(input);
567        matcher.region(1, 5);
568        assertTrue(matcher.matches());
569    }
570
571    public void testAllCodePoints_p() {
572        // Regression for HARMONY-3145
573        int[] codePoint = new int[1];
574        Pattern p = Pattern.compile("(\\p{all})+");
575        boolean res = true;
576        int cnt = 0;
577        int step = 16; // Ideally 1, but devices are still too slow.
578        for (int i = 0; i < 0x110000; i += step) {
579            codePoint[0] = i;
580            String s = new String(codePoint, 0, 1);
581            if (!s.matches(p.toString())) {
582                cnt++;
583                res = false;
584            }
585        }
586        assertTrue(res);
587        assertEquals(0, cnt);
588    }
589
590    public void testAllCodePoints_P() {
591        // Regression for HARMONY-3145
592        int[] codePoint = new int[1];
593        Pattern p = Pattern.compile("(\\P{all})+");
594        boolean res = true;
595        int cnt = 0;
596        int step = 16; // Ideally 1, but devices are still too slow.
597        for (int i = 0; i < 0x110000; i += step) {
598            codePoint[0] = i;
599            String s = new String(codePoint, 0, 1);
600            if (!s.matches(p.toString())) {
601                cnt++;
602                res = false;
603            }
604        }
605        assertFalse(res);
606        assertEquals(0x110000 / step, cnt);
607    }
608
609    public void testFindRegionChanged() {
610        // Regression for HARMONY-625
611        // Verify if the Matcher behaves correct when region is changed.
612        Pattern pattern = Pattern.compile("(?s).*");
613        Matcher matcher = pattern.matcher("abcde");
614        matcher.find();
615        assertEquals("abcde", matcher.group());
616
617        matcher = pattern.matcher("abcde");
618        matcher.region(0, 2);
619        matcher.find();
620        assertEquals("ab", matcher.group());
621
622    }
623
624    public void testFindRegionChanged2() {
625        // Regression for HARMONY-713
626        // Verify if the Matcher behaves correct with pattern "c" when region is changed.
627        Pattern pattern = Pattern.compile("c");
628
629        String inputStr = "aabb.c";
630        Matcher matcher = pattern.matcher(inputStr);
631        matcher.region(0, 3);
632
633        assertFalse(matcher.find());
634    }
635
636    public void testPatternMatcher() throws Exception {
637        // Regression test for HARMONY-674
638        Pattern pattern = Pattern.compile("(?:\\d+)(?:pt)");
639        assertTrue(pattern.matcher("14pt").matches());
640    }
641
642    public void test3360() {
643        // Inspired by HARMONY-3360
644        String str = "!\"#%&'(),-./";
645        Pattern p = Pattern.compile("\\s");
646        Matcher m = p.matcher(str);
647
648        assertFalse(m.find());
649    }
650
651    public void testGeneralPunctuationCategory() {
652        // Regression test for HARMONY-3360
653        String[] s = { ",", "!", "\"", "#", "%", "&", "'", "(", ")", "-", ".", "/" };
654        String regexp = "\\p{P}";
655
656        for (int i = 0; i < s.length; i++) {
657            Pattern pattern = Pattern.compile(regexp);
658            Matcher matcher = pattern.matcher(s[i]);
659            assertTrue(matcher.find());
660        }
661    }
662
663    public void testHitEndAfterFind() {
664        // Regression test for HARMONY-4396
665        hitEndTest(true, "#01.0", "r((ege)|(geg))x", "regexx", false);
666        hitEndTest(true, "#01.1", "r((ege)|(geg))x", "regex", false);
667        hitEndTest(true, "#01.2", "r((ege)|(geg))x", "rege", true);
668        hitEndTest(true, "#01.2", "r((ege)|(geg))x", "xregexx", false);
669
670        hitEndTest(true, "#02.0", "regex", "rexreger", true);
671        hitEndTest(true, "#02.1", "regex", "raxregexr", false);
672
673        String floatRegex = getHexFloatRegex();
674        hitEndTest(true, "#03.0", floatRegex, Double.toHexString(-1.234d), true);
675        hitEndTest(true, "#03.1", floatRegex, "1 ABC"
676                + Double.toHexString(Double.NaN) + "buhuhu", false);
677        hitEndTest(true, "#03.2", floatRegex, Double.toHexString(-0.0) + "--",
678                false);
679        hitEndTest(true, "#03.3", floatRegex, "--"
680                + Double.toHexString(Double.MIN_VALUE) + "--", false);
681
682        hitEndTest(true, "#04.0", "(\\d+) fish (\\d+) fish (\\w+) fish (\\d+)",
683                "1 fish 2 fish red fish 5", true);
684        hitEndTest(true, "#04.1", "(\\d+) fish (\\d+) fish (\\w+) fish (\\d+)",
685                "----1 fish 2 fish red fish 5----", false);
686    }
687
688    public void testToString() {
689        Matcher m = Pattern.compile("(\\d{1,3})").matcher("aaaa666456789045");
690        assertEquals("java.util.regex.Matcher[pattern=(\\d{1,3}) region=0,16 lastmatch=]", m.toString());
691        assertTrue(m.find());
692        assertEquals("java.util.regex.Matcher[pattern=(\\d{1,3}) region=0,16 lastmatch=666]", m.toString());
693        m.region(4, 8);
694        assertEquals("java.util.regex.Matcher[pattern=(\\d{1,3}) region=4,8 lastmatch=]", m.toString());
695    }
696
697    private void hitEndTest(boolean callFind, String testNo, String regex,
698            String input, boolean hit) {
699        Pattern pattern = Pattern.compile(regex);
700        Matcher matcher = pattern.matcher(input);
701        if (callFind) {
702            matcher.find();
703        } else {
704            matcher.matches();
705        }
706        boolean h = matcher.hitEnd();
707
708        assertTrue(testNo, h == hit);
709    }
710
711    private String getHexFloatRegex() {
712        String hexDecimal = "(-|\\+)?0[xX][0-9a-fA-F]*\\.[0-9a-fA-F]+([pP](-|\\+)?[0-9]+)?";
713        String notANumber = "((-|\\+)?Infinity)|([nN]a[nN])";
714        return new StringBuilder("((").append(hexDecimal).append(")|(").append(
715                notANumber).append("))").toString();
716    }
717}
718