You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/04/14 09:42:18 UTC
svn commit: r394041 [2/2] - in
/incubator/harmony/enhanced/classlib/trunk/modules/regex: make/common/
src/test/java/tests/api/java/util/regex/
Copied: incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/PatternTest.java (from r394009, incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/PatternTests.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/PatternTest.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/PatternTest.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/PatternTests.java&r1=394009&r2=394041&rev=394041&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/PatternTests.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/PatternTest.java Fri Apr 14 00:42:15 2006
@@ -25,34 +25,26 @@
* Tests simple Pattern compilation and Matcher methods
*
*/
-public class PatternTests extends TestCase {
- public void testSimpleMatch() {
- Pattern p;
-
- try {
- p = Pattern.compile("foo.*");
-
- Matcher m1 = p.matcher("foo123");
- assertTrue(m1.matches());
- assertTrue(m1.find(0));
- assertTrue(m1.lookingAt());
+public class PatternTest extends TestCase {
+ public void testSimpleMatch() throws PatternSyntaxException {
+ Pattern p = Pattern.compile("foo.*");
+
+ Matcher m1 = p.matcher("foo123");
+ assertTrue(m1.matches());
+ assertTrue(m1.find(0));
+ assertTrue(m1.lookingAt());
+
+ Matcher m2 = p.matcher("fox");
+ assertFalse(m2.matches());
+ assertFalse(m2.find(0));
+ assertFalse(m2.lookingAt());
- Matcher m2 = p.matcher("fox");
- assertFalse(m2.matches());
- assertFalse(m2.find(0));
- assertFalse(m2.lookingAt());
+ assertTrue(Pattern.matches("foo.*", "foo123"));
+ assertFalse(Pattern.matches("foo.*", "fox"));
- assertTrue(Pattern.matches("foo.*", "foo123"));
- assertFalse(Pattern.matches("foo.*", "fox"));
+ assertFalse(Pattern.matches("bar", "foobar"));
- assertFalse(Pattern.matches("bar", "foobar"));
-
- assertTrue(Pattern.matches("", ""));
-
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ assertTrue(Pattern.matches("", ""));
}
public void testCursors() {
@@ -110,836 +102,750 @@
}
}
- public void testGroups() {
+ public void testGroups() throws PatternSyntaxException {
Pattern p;
Matcher m;
- try {
- p = Pattern.compile("(p[0-9]*)#?(q[0-9]*)");
-
- m = p.matcher("p1#q3p2q42p5p71p63#q888");
- assertTrue(m.find());
- assertTrue(m.start() == 0);
- assertTrue(m.end() == 5);
- assertTrue(m.groupCount() == 2);
- assertTrue(m.start(0) == 0);
- assertTrue(m.end(0) == 5);
- assertTrue(m.start(1) == 0);
- assertTrue(m.end(1) == 2);
- assertTrue(m.start(2) == 3);
- assertTrue(m.end(2) == 5);
- assertTrue(m.group().equals("p1#q3"));
- assertTrue(m.group(0).equals("p1#q3"));
- assertTrue(m.group(1).equals("p1"));
- assertTrue(m.group(2).equals("q3"));
-
- assertTrue(m.find());
- assertTrue(m.start() == 5);
- assertTrue(m.end() == 10);
- assertTrue(m.groupCount() == 2);
- assertTrue(m.end(0) == 10);
- assertTrue(m.start(1) == 5);
- assertTrue(m.end(1) == 7);
- assertTrue(m.start(2) == 7);
- assertTrue(m.end(2) == 10);
- assertTrue(m.group().equals("p2q42"));
- assertTrue(m.group(0).equals("p2q42"));
- assertTrue(m.group(1).equals("p2"));
- assertTrue(m.group(2).equals("q42"));
+ p = Pattern.compile("(p[0-9]*)#?(q[0-9]*)");
- assertTrue(m.find());
- assertTrue(m.start() == 15);
- assertTrue(m.end() == 23);
- assertTrue(m.groupCount() == 2);
- assertTrue(m.start(0) == 15);
- assertTrue(m.end(0) == 23);
- assertTrue(m.start(1) == 15);
- assertTrue(m.end(1) == 18);
- assertTrue(m.start(2) == 19);
- assertTrue(m.end(2) == 23);
- assertTrue(m.group().equals("p63#q888"));
- assertTrue(m.group(0).equals("p63#q888"));
- assertTrue(m.group(1).equals("p63"));
- assertTrue(m.group(2).equals("q888"));
- assertFalse(m.find());
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ m = p.matcher("p1#q3p2q42p5p71p63#q888");
+ assertTrue(m.find());
+ assertTrue(m.start() == 0);
+ assertTrue(m.end() == 5);
+ assertTrue(m.groupCount() == 2);
+ assertTrue(m.start(0) == 0);
+ assertTrue(m.end(0) == 5);
+ assertTrue(m.start(1) == 0);
+ assertTrue(m.end(1) == 2);
+ assertTrue(m.start(2) == 3);
+ assertTrue(m.end(2) == 5);
+ assertTrue(m.group().equals("p1#q3"));
+ assertTrue(m.group(0).equals("p1#q3"));
+ assertTrue(m.group(1).equals("p1"));
+ assertTrue(m.group(2).equals("q3"));
+
+ assertTrue(m.find());
+ assertTrue(m.start() == 5);
+ assertTrue(m.end() == 10);
+ assertTrue(m.groupCount() == 2);
+ assertTrue(m.end(0) == 10);
+ assertTrue(m.start(1) == 5);
+ assertTrue(m.end(1) == 7);
+ assertTrue(m.start(2) == 7);
+ assertTrue(m.end(2) == 10);
+ assertTrue(m.group().equals("p2q42"));
+ assertTrue(m.group(0).equals("p2q42"));
+ assertTrue(m.group(1).equals("p2"));
+ assertTrue(m.group(2).equals("q42"));
+
+ assertTrue(m.find());
+ assertTrue(m.start() == 15);
+ assertTrue(m.end() == 23);
+ assertTrue(m.groupCount() == 2);
+ assertTrue(m.start(0) == 15);
+ assertTrue(m.end(0) == 23);
+ assertTrue(m.start(1) == 15);
+ assertTrue(m.end(1) == 18);
+ assertTrue(m.start(2) == 19);
+ assertTrue(m.end(2) == 23);
+ assertTrue(m.group().equals("p63#q888"));
+ assertTrue(m.group(0).equals("p63#q888"));
+ assertTrue(m.group(1).equals("p63"));
+ assertTrue(m.group(2).equals("q888"));
+ assertFalse(m.find());
}
- public void testReplace() {
+ public void testReplace() throws PatternSyntaxException {
Pattern p;
Matcher m;
// Note: examples from book,
// Hitchens, Ron, 2002, "Java NIO", O'Reilly, page 171
- try {
- p = Pattern.compile("a*b");
+ p = Pattern.compile("a*b");
- m = p.matcher("aabfooaabfooabfoob");
- assertTrue(m.replaceAll("-").equals("-foo-foo-foo-"));
- assertTrue(m.replaceFirst("-").equals("-fooaabfooabfoob"));
+ m = p.matcher("aabfooaabfooabfoob");
+ assertTrue(m.replaceAll("-").equals("-foo-foo-foo-"));
+ assertTrue(m.replaceFirst("-").equals("-fooaabfooabfoob"));
- /*
- * p = Pattern.compile ("\\p{Blank}");
- *
- * m = p.matcher ("fee fie foe fum"); assertTrue
- * (m.replaceFirst("-").equals ("fee-fie foe fum")); assertTrue
- * (m.replaceAll("-").equals ("fee-fie-foe-fum"));
- */
+ /*
+ * p = Pattern.compile ("\\p{Blank}");
+ *
+ * m = p.matcher ("fee fie foe fum"); assertTrue
+ * (m.replaceFirst("-").equals ("fee-fie foe fum")); assertTrue
+ * (m.replaceAll("-").equals ("fee-fie-foe-fum"));
+ */
- p = Pattern.compile("([bB])yte");
+ p = Pattern.compile("([bB])yte");
- m = p.matcher("Byte for byte");
- assertTrue(m.replaceFirst("$1ite").equals("Bite for byte"));
- assertTrue(m.replaceAll("$1ite").equals("Bite for bite"));
+ m = p.matcher("Byte for byte");
+ assertTrue(m.replaceFirst("$1ite").equals("Bite for byte"));
+ assertTrue(m.replaceAll("$1ite").equals("Bite for bite"));
- p = Pattern.compile("\\d\\d\\d\\d([- ])");
+ p = Pattern.compile("\\d\\d\\d\\d([- ])");
- m = p.matcher("card #1234-5678-1234");
- assertTrue(m.replaceFirst("xxxx$1").equals("card #xxxx-5678-1234"));
- assertTrue(m.replaceAll("xxxx$1").equals("card #xxxx-xxxx-1234"));
+ m = p.matcher("card #1234-5678-1234");
+ assertTrue(m.replaceFirst("xxxx$1").equals("card #xxxx-5678-1234"));
+ assertTrue(m.replaceAll("xxxx$1").equals("card #xxxx-xxxx-1234"));
- p = Pattern.compile("(up|left)( *)(right|down)");
+ p = Pattern.compile("(up|left)( *)(right|down)");
- m = p.matcher("left right, up down");
- assertTrue(m.replaceFirst("$3$2$1").equals("right left, up down"));
- assertTrue(m.replaceAll("$3$2$1").equals("right left, down up"));
+ m = p.matcher("left right, up down");
+ assertTrue(m.replaceFirst("$3$2$1").equals("right left, up down"));
+ assertTrue(m.replaceAll("$3$2$1").equals("right left, down up"));
- p = Pattern.compile("([CcPp][hl]e[ea]se)");
+ p = Pattern.compile("([CcPp][hl]e[ea]se)");
- m = p.matcher("I want cheese. Please.");
- assertTrue(m.replaceFirst("<b> $1 </b>").equals(
- "I want <b> cheese </b>. Please."));
- assertTrue(m.replaceAll("<b> $1 </b>").equals(
- "I want <b> cheese </b>. <b> Please </b>."));
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ m = p.matcher("I want cheese. Please.");
+ assertTrue(m.replaceFirst("<b> $1 </b>").equals(
+ "I want <b> cheese </b>. Please."));
+ assertTrue(m.replaceAll("<b> $1 </b>").equals(
+ "I want <b> cheese </b>. <b> Please </b>."));
}
- public void testEscapes() {
+ public void testEscapes() throws PatternSyntaxException {
Pattern p;
Matcher m;
- boolean triggerError;
+ // Test \\ sequence
+ p = Pattern.compile("([a-z]+)\\\\([a-z]+);");
+ m = p.matcher("fred\\ginger;abbott\\costello;jekell\\hyde;");
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("fred"));
+ assertTrue(m.group(2).equals("ginger"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("abbott"));
+ assertTrue(m.group(2).equals("costello"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("jekell"));
+ assertTrue(m.group(2).equals("hyde"));
+ assertFalse(m.find());
+
+ // Test \n, \t, \r, \f, \e, \a sequences
+ p = Pattern.compile("([a-z]+)[\\n\\t\\r\\f\\e\\a]+([a-z]+)");
+ m = p.matcher("aa\nbb;cc\u0009\rdd;ee\u000C\u001Bff;gg\n\u0007hh");
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("aa"));
+ assertTrue(m.group(2).equals("bb"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("cc"));
+ assertTrue(m.group(2).equals("dd"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("ee"));
+ assertTrue(m.group(2).equals("ff"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("gg"));
+ assertTrue(m.group(2).equals("hh"));
+ assertFalse(m.find());
+
+ // Test \\u and \\x sequences
+ p = Pattern.compile("([0-9]+)[\\u0020:\\x21];");
+ m = p.matcher("11:;22 ;33-;44!;");
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("11"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("22"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("44"));
+ assertFalse(m.find());
+
+ // Test invalid unicode sequences
try {
- // Test \\ sequence
- p = Pattern.compile("([a-z]+)\\\\([a-z]+);");
- m = p.matcher("fred\\ginger;abbott\\costello;jekell\\hyde;");
- assertTrue(m.find());
- assertTrue(m.group(1).equals("fred"));
- assertTrue(m.group(2).equals("ginger"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("abbott"));
- assertTrue(m.group(2).equals("costello"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("jekell"));
- assertTrue(m.group(2).equals("hyde"));
- assertFalse(m.find());
+ p = Pattern.compile("\\u");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \n, \t, \r, \f, \e, \a sequences
- p = Pattern.compile("([a-z]+)[\\n\\t\\r\\f\\e\\a]+([a-z]+)");
- m = p.matcher("aa\nbb;cc\u0009\rdd;ee\u000C\u001Bff;gg\n\u0007hh");
- assertTrue(m.find());
- assertTrue(m.group(1).equals("aa"));
- assertTrue(m.group(2).equals("bb"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("cc"));
- assertTrue(m.group(2).equals("dd"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("ee"));
- assertTrue(m.group(2).equals("ff"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("gg"));
- assertTrue(m.group(2).equals("hh"));
- assertFalse(m.find());
+ try {
+ p = Pattern.compile("\\u;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \\u and \\x sequences
- p = Pattern.compile("([0-9]+)[\\u0020:\\x21];");
- m = p.matcher("11:;22 ;33-;44!;");
- assertTrue(m.find());
- assertTrue(m.group(1).equals("11"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("22"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("44"));
- assertFalse(m.find());
+ try {
+ p = Pattern.compile("\\u002");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test invalid unicode sequences
- triggerError = false;
- try {
- p = Pattern.compile("\\u");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\u;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\u002");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\u002;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
+ try {
+ p = Pattern.compile("\\u002;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test invalid hex sequences
- triggerError = false;
- try {
- p = Pattern.compile("\\x");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\x;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\xa");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\xa;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
+ // Test invalid hex sequences
+ try {
+ p = Pattern.compile("\\x");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \0 (octal) sequences (1, 2 and 3 digit)
- p = Pattern.compile("([0-9]+)[\\07\\040\\0160];");
- m = p.matcher("11\u0007;22:;33 ;44p;");
- assertTrue(m.find());
- assertTrue(m.group(1).equals("11"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("33"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("44"));
- assertFalse(m.find());
+ try {
+ p = Pattern.compile("\\x;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test invalid octal sequences
- triggerError = false;
- try {
- p = Pattern.compile("\\08");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\0477");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- triggerError = false;
- try {
- p = Pattern.compile("\\0");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\0;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
+ try {
+ p = Pattern.compile("\\xa");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \c (control character) sequence
- p = Pattern.compile("([0-9]+)[\\cA\\cB\\cC\\cD];");
- m = p.matcher("11\u0001;22:;33\u0002;44p;55\u0003;66\u0004;");
- assertTrue(m.find());
- assertTrue(m.group(1).equals("11"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("33"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("55"));
- assertTrue(m.find());
- assertTrue(m.group(1).equals("66"));
- assertFalse(m.find());
+ try {
+ p = Pattern.compile("\\xa;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // More thorough control escape test
- // Ensure that each escape matches exactly the corresponding
- // character
- // code and no others (well, from 0-255 at least)
- int i, j;
- for (i = 0; i < 26; i++) {
- p = Pattern.compile("\\c"
- + Character.toString((char) ('A' + i)));
- int match_char = -1;
- for (j = 0; j < 255; j++) {
- m = p.matcher(Character.toString((char) j));
- if (m.matches()) {
- assertTrue(match_char == -1);
- match_char = j;
- }
- }
- assertTrue(match_char == i + 1);
- }
+ // Test \0 (octal) sequences (1, 2 and 3 digit)
+ p = Pattern.compile("([0-9]+)[\\07\\040\\0160];");
+ m = p.matcher("11\u0007;22:;33 ;44p;");
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("11"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("33"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("44"));
+ assertFalse(m.find());
- // Test invalid control escapes
- triggerError = false;
- try {
- p = Pattern.compile("\\c");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\c;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- triggerError = false;
- try {
- p = Pattern.compile("\\ca;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- triggerError = false;
- try {
- p = Pattern.compile("\\c4;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
+ // Test invalid octal sequences
+ try {
+ p = Pattern.compile("\\08");
+ fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
}
- }
- public void testCharacterClasses() {
- Pattern p;
- Matcher m;
- boolean triggerError;
+ //originally contributed test did not check the result
+ //TODO: check what RI does here
+// try {
+// p = Pattern.compile("\\0477");
+// fail("PatternSyntaxException expected");
+// } catch (PatternSyntaxException e) {
+// }
try {
- // Test one character range
- p = Pattern.compile("[p].*[l]");
- m = p.matcher("paul");
- assertTrue(m.matches());
- m = p.matcher("pool");
- assertTrue(m.matches());
- m = p.matcher("pong");
- assertFalse(m.matches());
- m = p.matcher("pl");
- assertTrue(m.matches());
+ p = Pattern.compile("\\0");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test two character range
- p = Pattern.compile("[pm].*[lp]");
- m = p.matcher("prop");
- assertTrue(m.matches());
- m = p.matcher("mall");
- assertTrue(m.matches());
- m = p.matcher("pong");
- assertFalse(m.matches());
- m = p.matcher("pill");
- assertTrue(m.matches());
+ try {
+ p = Pattern.compile("\\0;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test range including [ and ]
- p = Pattern.compile("[<\\[].*[\\]>]");
- m = p.matcher("<foo>");
- assertTrue(m.matches());
- m = p.matcher("[bar]");
- assertTrue(m.matches());
- m = p.matcher("{foobar]");
- assertFalse(m.matches());
- m = p.matcher("<pill]");
- assertTrue(m.matches());
- // Test range using ^
- p = Pattern.compile("[^bc][a-z]+[tr]");
- m = p.matcher("pat");
- assertTrue(m.matches());
- m = p.matcher("liar");
- assertTrue(m.matches());
- m = p.matcher("car");
- assertFalse(m.matches());
- m = p.matcher("gnat");
- assertTrue(m.matches());
+ // Test \c (control character) sequence
+ p = Pattern.compile("([0-9]+)[\\cA\\cB\\cC\\cD];");
+ m = p.matcher("11\u0001;22:;33\u0002;44p;55\u0003;66\u0004;");
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("11"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("33"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("55"));
+ assertTrue(m.find());
+ assertTrue(m.group(1).equals("66"));
+ assertFalse(m.find());
+
+ // More thorough control escape test
+ // Ensure that each escape matches exactly the corresponding
+ // character
+ // code and no others (well, from 0-255 at least)
+ int i, j;
+ for (i = 0; i < 26; i++) {
+ p = Pattern.compile("\\c" + Character.toString((char) ('A' + i)));
+ int match_char = -1;
+ for (j = 0; j < 255; j++) {
+ m = p.matcher(Character.toString((char) j));
+ if (m.matches()) {
+ assertTrue(match_char == -1);
+ match_char = j;
+ }
+ }
+ assertTrue(match_char == i + 1);
+ }
- // Test character range using -
- p = Pattern.compile("[a-z]_+[a-zA-Z]-+[0-9p-z]");
- m = p.matcher("d__F-8");
- assertTrue(m.matches());
- m = p.matcher("c_a-q");
- assertTrue(m.matches());
- m = p.matcher("a__R-a");
- assertFalse(m.matches());
- m = p.matcher("r_____d-----5");
- assertTrue(m.matches());
+ // Test invalid control escapes
+ try {
+ p = Pattern.compile("\\c");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test range using unicode characters and unicode and hex escapes
- p = Pattern.compile("[\\u1234-\\u2345]_+[a-z]-+[\u0001-\\x11]");
- m = p.matcher("\u2000_q-\u0007");
- assertTrue(m.matches());
- m = p.matcher("\u1234_z-\u0001");
- assertTrue(m.matches());
- m = p.matcher("r_p-q");
- assertFalse(m.matches());
- m = p.matcher("\u2345_____d-----\n");
- assertTrue(m.matches());
+ //originally contributed test did not check the result
+ //TODO: check what RI does here
+// try {
+// p = Pattern.compile("\\c;");
+// fail("PatternSyntaxException expected");
+// } catch (PatternSyntaxException e) {
+// }
+//
+// try {
+// p = Pattern.compile("\\ca;");
+// fail("PatternSyntaxException expected");
+// } catch (PatternSyntaxException e) {
+// }
+//
+// try {
+// p = Pattern.compile("\\c4;");
+// fail("PatternSyntaxException expected");
+// } catch (PatternSyntaxException e) {
+// }
+ }
- // Test ranges including the "-" character
- p = Pattern.compile("[\\*-/]_+[---]!+[--AP]");
- m = p.matcher("-_-!!A");
- assertTrue(m.matches());
- m = p.matcher("\u002b_-!!!-");
- assertTrue(m.matches());
- m = p.matcher("!_-!@");
- assertFalse(m.matches());
- m = p.matcher(",______-!!!!!!!P");
- assertTrue(m.matches());
+ public void testCharacterClasses() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
- // Test nested ranges
- p = Pattern.compile("[pm[t]][a-z]+[[r]lp]");
- m = p.matcher("prop");
- assertTrue(m.matches());
- m = p.matcher("tsar");
- assertTrue(m.matches());
- m = p.matcher("pong");
- assertFalse(m.matches());
- m = p.matcher("moor");
- assertTrue(m.matches());
+ // Test one character range
+ p = Pattern.compile("[p].*[l]");
+ m = p.matcher("paul");
+ assertTrue(m.matches());
+ m = p.matcher("pool");
+ assertTrue(m.matches());
+ m = p.matcher("pong");
+ assertFalse(m.matches());
+ m = p.matcher("pl");
+ assertTrue(m.matches());
+
+ // Test two character range
+ p = Pattern.compile("[pm].*[lp]");
+ m = p.matcher("prop");
+ assertTrue(m.matches());
+ m = p.matcher("mall");
+ assertTrue(m.matches());
+ m = p.matcher("pong");
+ assertFalse(m.matches());
+ m = p.matcher("pill");
+ assertTrue(m.matches());
+
+ // Test range including [ and ]
+ p = Pattern.compile("[<\\[].*[\\]>]");
+ m = p.matcher("<foo>");
+ assertTrue(m.matches());
+ m = p.matcher("[bar]");
+ assertTrue(m.matches());
+ m = p.matcher("{foobar]");
+ assertFalse(m.matches());
+ m = p.matcher("<pill]");
+ assertTrue(m.matches());
+
+ // Test range using ^
+ p = Pattern.compile("[^bc][a-z]+[tr]");
+ m = p.matcher("pat");
+ assertTrue(m.matches());
+ m = p.matcher("liar");
+ assertTrue(m.matches());
+ m = p.matcher("car");
+ assertFalse(m.matches());
+ m = p.matcher("gnat");
+ assertTrue(m.matches());
+
+ // Test character range using -
+ p = Pattern.compile("[a-z]_+[a-zA-Z]-+[0-9p-z]");
+ m = p.matcher("d__F-8");
+ assertTrue(m.matches());
+ m = p.matcher("c_a-q");
+ assertTrue(m.matches());
+ m = p.matcher("a__R-a");
+ assertFalse(m.matches());
+ m = p.matcher("r_____d-----5");
+ assertTrue(m.matches());
+
+ // Test range using unicode characters and unicode and hex escapes
+ p = Pattern.compile("[\\u1234-\\u2345]_+[a-z]-+[\u0001-\\x11]");
+ m = p.matcher("\u2000_q-\u0007");
+ assertTrue(m.matches());
+ m = p.matcher("\u1234_z-\u0001");
+ assertTrue(m.matches());
+ m = p.matcher("r_p-q");
+ assertFalse(m.matches());
+ m = p.matcher("\u2345_____d-----\n");
+ assertTrue(m.matches());
+
+ // Test ranges including the "-" character
+ p = Pattern.compile("[\\*-/]_+[---]!+[--AP]");
+ m = p.matcher("-_-!!A");
+ assertTrue(m.matches());
+ m = p.matcher("\u002b_-!!!-");
+ assertTrue(m.matches());
+ m = p.matcher("!_-!@");
+ assertFalse(m.matches());
+ m = p.matcher(",______-!!!!!!!P");
+ assertTrue(m.matches());
+
+ // Test nested ranges
+ p = Pattern.compile("[pm[t]][a-z]+[[r]lp]");
+ m = p.matcher("prop");
+ assertTrue(m.matches());
+ m = p.matcher("tsar");
+ assertTrue(m.matches());
+ m = p.matcher("pong");
+ assertFalse(m.matches());
+ m = p.matcher("moor");
+ assertTrue(m.matches());
+
+ // Test character class intersection with &&
+ // TODO: figure out what x&&y or any class with a null intersection
+ // set (like [[a-c]&&[d-f]]) might mean. It doesn't mean "match
+ // nothing" and doesn't mean "match anything" so I'm stumped.
+ p = Pattern.compile("[[a-p]&&[g-z]]+-+[[a-z]&&q]-+[x&&[a-z]]-+");
+ m = p.matcher("h--q--x--");
+ assertTrue(m.matches());
+ m = p.matcher("hog--q-x-");
+ assertTrue(m.matches());
+ m = p.matcher("ape--q-x-");
+ assertFalse(m.matches());
+ m = p.matcher("mop--q-x----");
+ assertTrue(m.matches());
+
+ // Test error cases with &&
+ p = Pattern.compile("[&&[xyz]]");
+ m = p.matcher("&");
+ // System.out.println(m.matches());
+ m = p.matcher("x");
+ // System.out.println(m.matches());
+ m = p.matcher("y");
+ // System.out.println(m.matches());
+ p = Pattern.compile("[[xyz]&[axy]]");
+ m = p.matcher("x");
+ // System.out.println(m.matches());
+ m = p.matcher("z");
+ // System.out.println(m.matches());
+ m = p.matcher("&");
+ // System.out.println(m.matches());
+ p = Pattern.compile("[abc[123]&&[345]def]");
+ m = p.matcher("a");
+ // System.out.println(m.matches());
- // Test character class intersection with &&
- // TODO: figure out what x&&y or any class with a null intersection
- // set (like [[a-c]&&[d-f]]) might mean. It doesn't mean "match
- // nothing" and doesn't mean "match anything" so I'm stumped.
- p = Pattern.compile("[[a-p]&&[g-z]]+-+[[a-z]&&q]-+[x&&[a-z]]-+");
- m = p.matcher("h--q--x--");
- assertTrue(m.matches());
- m = p.matcher("hog--q-x-");
- assertTrue(m.matches());
- m = p.matcher("ape--q-x-");
- assertFalse(m.matches());
- m = p.matcher("mop--q-x----");
- assertTrue(m.matches());
+ p = Pattern.compile("[[xyz]&&]");
- // Test error cases with &&
- triggerError = false;
- try {
- p = Pattern.compile("[&&[xyz]]");
- m = p.matcher("&");
- // System.out.println(m.matches());
- m = p.matcher("x");
- // System.out.println(m.matches());
- m = p.matcher("y");
- // System.out.println(m.matches());
- p = Pattern.compile("[[xyz]&[axy]]");
- m = p.matcher("x");
- // System.out.println(m.matches());
- m = p.matcher("z");
- // System.out.println(m.matches());
- m = p.matcher("&");
- // System.out.println(m.matches());
- p = Pattern.compile("[abc[123]&&[345]def]");
- m = p.matcher("a");
- // System.out.println(m.matches());
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertFalse(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("[[xyz]&&]");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertFalse(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("[[abc]&]");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertFalse(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("[[abc]&&");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("[[abc]\\&&[xyz]]");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertFalse(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("[[abc]&\\&[xyz]]");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertFalse(triggerError);
+ p = Pattern.compile("[[abc]&]");
- // Test 3-way intersection
- p = Pattern.compile("[[a-p]&&[g-z]&&[d-k]]");
- m = p.matcher("g");
- assertTrue(m.matches());
- m = p.matcher("m");
- assertFalse(m.matches());
+ try {
+ p = Pattern.compile("[[abc]&&");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test nested intersection
- p = Pattern.compile("[[[a-p]&&[g-z]]&&[d-k]]");
- m = p.matcher("g");
- assertTrue(m.matches());
- m = p.matcher("m");
- assertFalse(m.matches());
+ p = Pattern.compile("[[abc]\\&&[xyz]]");
- // Test character class subtraction with && and ^
- p = Pattern.compile("[[a-z]&&[^aeiou]][aeiou][[^xyz]&&[a-z]]");
- m = p.matcher("pop");
- assertTrue(m.matches());
- m = p.matcher("tag");
- assertTrue(m.matches());
- m = p.matcher("eat");
- assertFalse(m.matches());
- m = p.matcher("tax");
- assertFalse(m.matches());
- m = p.matcher("zip");
- assertTrue(m.matches());
+ p = Pattern.compile("[[abc]&\\&[xyz]]");
- // Test . (DOT), with and without DOTALL
- // Note: DOT not allowed in character classes
- p = Pattern.compile(".+/x.z");
- m = p.matcher("!$/xyz");
- assertTrue(m.matches());
- m = p.matcher("%\n\r/x\nz");
- assertFalse(m.matches());
- p = Pattern.compile(".+/x.z", Pattern.DOTALL);
- m = p.matcher("%\n\r/x\nz");
- assertTrue(m.matches());
+ // Test 3-way intersection
+ p = Pattern.compile("[[a-p]&&[g-z]&&[d-k]]");
+ m = p.matcher("g");
+ assertTrue(m.matches());
+ m = p.matcher("m");
+ assertFalse(m.matches());
+
+ // Test nested intersection
+ p = Pattern.compile("[[[a-p]&&[g-z]]&&[d-k]]");
+ m = p.matcher("g");
+ assertTrue(m.matches());
+ m = p.matcher("m");
+ assertFalse(m.matches());
+
+ // Test character class subtraction with && and ^
+ p = Pattern.compile("[[a-z]&&[^aeiou]][aeiou][[^xyz]&&[a-z]]");
+ m = p.matcher("pop");
+ assertTrue(m.matches());
+ m = p.matcher("tag");
+ assertTrue(m.matches());
+ m = p.matcher("eat");
+ assertFalse(m.matches());
+ m = p.matcher("tax");
+ assertFalse(m.matches());
+ m = p.matcher("zip");
+ assertTrue(m.matches());
+
+ // Test . (DOT), with and without DOTALL
+ // Note: DOT not allowed in character classes
+ p = Pattern.compile(".+/x.z");
+ m = p.matcher("!$/xyz");
+ assertTrue(m.matches());
+ m = p.matcher("%\n\r/x\nz");
+ assertFalse(m.matches());
+ p = Pattern.compile(".+/x.z", Pattern.DOTALL);
+ m = p.matcher("%\n\r/x\nz");
+ assertTrue(m.matches());
+
+ // Test \d (digit)
+ p = Pattern.compile("\\d+[a-z][\\dx]");
+ m = p.matcher("42a6");
+ assertTrue(m.matches());
+ m = p.matcher("21zx");
+ assertTrue(m.matches());
+ m = p.matcher("ab6");
+ assertFalse(m.matches());
+ m = p.matcher("56912f9");
+ assertTrue(m.matches());
+
+ // Test \D (not a digit)
+ p = Pattern.compile("\\D+[a-z]-[\\D3]");
+ m = p.matcher("za-p");
+ assertTrue(m.matches());
+ m = p.matcher("%!e-3");
+ assertTrue(m.matches());
+ m = p.matcher("9a-x");
+ assertFalse(m.matches());
+ m = p.matcher("\u1234pp\ny-3");
+ assertTrue(m.matches());
+
+ // Test \s (whitespace)
+ p = Pattern.compile("<[a-zA-Z]+\\s+[0-9]+[\\sx][^\\s]>");
+ m = p.matcher("<cat \t1\fx>");
+ assertTrue(m.matches());
+ m = p.matcher("<cat \t1\f >");
+ assertFalse(m.matches());
+ m = p
+ .matcher("xyz <foo\n\r22 5> <pp \t\n\f\r \u000b41x\u1234><pp \nx7\rc> zzz");
+ assertTrue(m.find());
+ assertTrue(m.find());
+ assertFalse(m.find());
+
+ // Test \S (not whitespace)
+ p = Pattern.compile("<[a-z] \\S[0-9][\\S\n]+[^\\S]221>");
+ m = p.matcher("<f $0**\n** 221>");
+ assertTrue(m.matches());
+ m = p.matcher("<x 441\t221>");
+ assertTrue(m.matches());
+ m = p.matcher("<z \t9\ng 221>");
+ assertFalse(m.matches());
+ m = p.matcher("<z 60\ngg\u1234\f221>");
+ assertTrue(m.matches());
+ p = Pattern.compile("<[a-z] \\S[0-9][\\S\n]+[^\\S]221[\\S&&[^abc]]>");
+ m = p.matcher("<f $0**\n** 221x>");
+ assertTrue(m.matches());
+ m = p.matcher("<x 441\t221z>");
+ assertTrue(m.matches());
+ m = p.matcher("<x 441\t221 >");
+ assertFalse(m.matches());
+ m = p.matcher("<x 441\t221c>");
+ assertFalse(m.matches());
+ m = p.matcher("<z \t9\ng 221x>");
+ assertFalse(m.matches());
+ m = p.matcher("<z 60\ngg\u1234\f221\u0001>");
+ assertTrue(m.matches());
+
+ // Test \w (ascii word)
+ p = Pattern.compile("<\\w+\\s[0-9]+;[^\\w]\\w+/[\\w$]+;");
+ m = p.matcher("<f1 99;!foo5/a$7;");
+ assertTrue(m.matches());
+ m = p.matcher("<f$ 99;!foo5/a$7;");
+ assertFalse(m.matches());
+ m = p
+ .matcher("<abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789 99;!foo5/a$7;");
+ assertTrue(m.matches());
+
+ // Test \W (not an ascii word)
+ p = Pattern.compile("<\\W\\w+\\s[0-9]+;[\\W_][^\\W]+\\s[0-9]+;");
+ m = p.matcher("<$foo3\n99;_bar\t0;");
+ assertTrue(m.matches());
+ m = p.matcher("<hh 99;_g 0;");
+ assertFalse(m.matches());
+ m = p.matcher("<*xx\t00;^zz\f11;");
+ assertTrue(m.matches());
- // Test \d (digit)
- p = Pattern.compile("\\d+[a-z][\\dx]");
- m = p.matcher("42a6");
- assertTrue(m.matches());
- m = p.matcher("21zx");
- assertTrue(m.matches());
- m = p.matcher("ab6");
- assertFalse(m.matches());
- m = p.matcher("56912f9");
- assertTrue(m.matches());
+ // Test x|y pattern
+ // TODO
+ }
- // Test \D (not a digit)
- p = Pattern.compile("\\D+[a-z]-[\\D3]");
- m = p.matcher("za-p");
- assertTrue(m.matches());
- m = p.matcher("%!e-3");
- assertTrue(m.matches());
- m = p.matcher("9a-x");
- assertFalse(m.matches());
- m = p.matcher("\u1234pp\ny-3");
- assertTrue(m.matches());
+ public void testPOSIXGroups() throws PatternSyntaxException {
+ Pattern p;
+ Matcher m;
- // Test \s (whitespace)
- p = Pattern.compile("<[a-zA-Z]+\\s+[0-9]+[\\sx][^\\s]>");
- m = p.matcher("<cat \t1\fx>");
- assertTrue(m.matches());
- m = p.matcher("<cat \t1\f >");
- assertFalse(m.matches());
- m = p
- .matcher("xyz <foo\n\r22 5> <pp \t\n\f\r \u000b41x\u1234><pp \nx7\rc> zzz");
- assertTrue(m.find());
- assertTrue(m.find());
- assertFalse(m.find());
+ // Test POSIX groups using \p and \P (in the group and not in the group)
+ // Groups are Lower, Upper, ASCII, Alpha, Digit, XDigit, Alnum, Punct,
+ // Graph, Print, Blank, Space, Cntrl
+ // Test \p{Lower}
+ /*
+ * FIXME: Requires complex range processing
+ * p = Pattern.compile("<\\p{Lower}\\d\\P{Lower}:[\\p{Lower}Z]\\s[^\\P{Lower}]>");
+ * m = p.matcher("<a4P:g x>"); assertTrue(m.matches()); m =
+ * p.matcher("<p4%:Z\tq>"); assertTrue(m.matches()); m =
+ * p.matcher("<A6#:e e>"); assertFalse(m.matches());
+ */
+ p = Pattern.compile("\\p{Lower}+");
+ m = p.matcher("abcdefghijklmnopqrstuvwxyz");
+ assertTrue(m.matches());
- // Test \S (not whitespace)
- p = Pattern.compile("<[a-z] \\S[0-9][\\S\n]+[^\\S]221>");
- m = p.matcher("<f $0**\n** 221>");
- assertTrue(m.matches());
- m = p.matcher("<x 441\t221>");
- assertTrue(m.matches());
- m = p.matcher("<z \t9\ng 221>");
- assertFalse(m.matches());
- m = p.matcher("<z 60\ngg\u1234\f221>");
- assertTrue(m.matches());
- p = Pattern
- .compile("<[a-z] \\S[0-9][\\S\n]+[^\\S]221[\\S&&[^abc]]>");
- m = p.matcher("<f $0**\n** 221x>");
- assertTrue(m.matches());
- m = p.matcher("<x 441\t221z>");
- assertTrue(m.matches());
- m = p.matcher("<x 441\t221 >");
- assertFalse(m.matches());
- m = p.matcher("<x 441\t221c>");
- assertFalse(m.matches());
- m = p.matcher("<z \t9\ng 221x>");
- assertFalse(m.matches());
- m = p.matcher("<z 60\ngg\u1234\f221\u0001>");
- assertTrue(m.matches());
+ // Invalid uses of \p{Lower}
+ try {
+ p = Pattern.compile("\\p");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \w (ascii word)
- p = Pattern.compile("<\\w+\\s[0-9]+;[^\\w]\\w+/[\\w$]+;");
- m = p.matcher("<f1 99;!foo5/a$7;");
- assertTrue(m.matches());
- m = p.matcher("<f$ 99;!foo5/a$7;");
- assertFalse(m.matches());
- m = p
- .matcher("<abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789 99;!foo5/a$7;");
- assertTrue(m.matches());
+ try {
+ p = Pattern.compile("\\p;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \W (not an ascii word)
- p = Pattern.compile("<\\W\\w+\\s[0-9]+;[\\W_][^\\W]+\\s[0-9]+;");
- m = p.matcher("<$foo3\n99;_bar\t0;");
- assertTrue(m.matches());
- m = p.matcher("<hh 99;_g 0;");
- assertFalse(m.matches());
- m = p.matcher("<*xx\t00;^zz\f11;");
- assertTrue(m.matches());
+ try {
+ p = Pattern.compile("\\p{");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test x|y pattern
- // TODO
+ try {
+ p = Pattern.compile("\\p{;");
+ fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
}
- }
- public void testPOSIXGroups() {
- Pattern p;
- Matcher m;
- boolean triggerError;
+ try {
+ p = Pattern.compile("\\p{Lower");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test POSIX groups using \p and \P (in the group and not in the group)
- // Groups are Lower, Upper, ASCII, Alpha, Digit, XDigit, Alnum, Punct,
- // Graph, Print, Blank, Space, Cntrl
try {
- // Test \p{Lower}
- /*
- * FIXME: Requires complex range processing
- * p = Pattern.compile("<\\p{Lower}\\d\\P{Lower}:[\\p{Lower}Z]\\s[^\\P{Lower}]>");
- * m = p.matcher("<a4P:g x>"); assertTrue(m.matches()); m =
- * p.matcher("<p4%:Z\tq>"); assertTrue(m.matches()); m =
- * p.matcher("<A6#:e e>"); assertFalse(m.matches());
- */
- p = Pattern.compile("\\p{Lower}+");
- m = p.matcher("abcdefghijklmnopqrstuvwxyz");
- assertTrue(m.matches());
+ p = Pattern.compile("\\p{Lower;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Invalid uses of \p{Lower}
- triggerError = false;
- try {
- p = Pattern.compile("\\p");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\p;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\p{");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\p{;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\p{Lower");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\p{Lower;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
+ // Test \p{Upper}
+ /*
+ * FIXME: Requires complex range processing
+ * p = Pattern.compile("<\\p{Upper}\\d\\P{Upper}:[\\p{Upper}z]\\s[^\\P{Upper}]>");
+ * m = p.matcher("<A4p:G X>"); assertTrue(m.matches()); m =
+ * p.matcher("<P4%:z\tQ>"); assertTrue(m.matches()); m =
+ * p.matcher("<a6#:E E>"); assertFalse(m.matches());
+ */
+ p = Pattern.compile("\\p{Upper}+");
+ m = p.matcher("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
+ assertTrue(m.matches());
- // Test \p{Upper}
- /*
- * FIXME: Requires complex range processing
- * p = Pattern.compile("<\\p{Upper}\\d\\P{Upper}:[\\p{Upper}z]\\s[^\\P{Upper}]>");
- * m = p.matcher("<A4p:G X>"); assertTrue(m.matches()); m =
- * p.matcher("<P4%:z\tQ>"); assertTrue(m.matches()); m =
- * p.matcher("<a6#:E E>"); assertFalse(m.matches());
- */
- p = Pattern.compile("\\p{Upper}+");
- m = p.matcher("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- assertTrue(m.matches());
+ // Invalid uses of \p{Upper}
+ try {
+ p = Pattern.compile("\\p{Upper");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Invalid uses of \p{Upper}
- triggerError = false;
- try {
- p = Pattern.compile("\\p{Upper");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\p{Upper;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
+ try {
+ p = Pattern.compile("\\p{Upper;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \p{ASCII}
- /*
- * FIXME: Requires complex range processing p = Pattern.compile("<\\p{ASCII}\\d\\P{ASCII}:[\\p{ASCII}\u1234]\\s[^\\P{ASCII}]>");
- * m = p.matcher("<A4\u0080:G X>"); assertTrue(m.matches()); m =
- * p.matcher("<P4\u00ff:\u1234\t\n>"); assertTrue(m.matches()); m =
- * p.matcher("<\u00846#:E E>"); assertFalse(m.matches())
- */;
- int i;
- p = Pattern.compile("\\p{ASCII}");
- for (i = 0; i < 0x80; i++) {
- m = p.matcher(Character.toString((char) i));
- assertTrue(m.matches());
- }
- for (; i < 0xff; i++) {
- m = p.matcher(Character.toString((char) i));
- assertFalse(m.matches());
- }
+ // Test \p{ASCII}
+ /*
+ * FIXME: Requires complex range processing p = Pattern.compile("<\\p{ASCII}\\d\\P{ASCII}:[\\p{ASCII}\u1234]\\s[^\\P{ASCII}]>");
+ * m = p.matcher("<A4\u0080:G X>"); assertTrue(m.matches()); m =
+ * p.matcher("<P4\u00ff:\u1234\t\n>"); assertTrue(m.matches()); m =
+ * p.matcher("<\u00846#:E E>"); assertFalse(m.matches())
+ */
+ int i;
+ p = Pattern.compile("\\p{ASCII}");
+ for (i = 0; i < 0x80; i++) {
+ m = p.matcher(Character.toString((char) i));
+ assertTrue(m.matches());
+ }
+ for (; i < 0xff; i++) {
+ m = p.matcher(Character.toString((char) i));
+ assertFalse(m.matches());
+ }
- // Invalid uses of \p{ASCII}
- triggerError = false;
- try {
- p = Pattern.compile("\\p{ASCII");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
- triggerError = false;
- try {
- p = Pattern.compile("\\p{ASCII;");
- } catch (PatternSyntaxException e) {
- triggerError = true;
- }
- assertTrue(triggerError);
+ // Invalid uses of \p{ASCII}
+ try {
+ p = Pattern.compile("\\p{ASCII");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \p{Alpha}
- // TODO
+ try {
+ p = Pattern.compile("\\p{ASCII;");
+ fail("PatternSyntaxException expected");
+ } catch (PatternSyntaxException e) {
+ }
- // Test \p{Digit}
- // TODO
+ // Test \p{Alpha}
+ // TODO
- // Test \p{XDigit}
- // TODO
+ // Test \p{Digit}
+ // TODO
- // Test \p{Alnum}
- // TODO
+ // Test \p{XDigit}
+ // TODO
- // Test \p{Punct}
- // TODO
+ // Test \p{Alnum}
+ // TODO
- // Test \p{Graph}
- // TODO
+ // Test \p{Punct}
+ // TODO
- // Test \p{Print}
- // TODO
+ // Test \p{Graph}
+ // TODO
- // Test \p{Blank}
- // TODO
+ // Test \p{Print}
+ // TODO
- // Test \p{Space}
- // TODO
+ // Test \p{Blank}
+ // TODO
- // Test \p{Cntrl}
- // TODO
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ // Test \p{Space}
+ // TODO
+
+ // Test \p{Cntrl}
+ // TODO
}
- public void testUnicodeCategories() {
+ public void testUnicodeCategories() throws PatternSyntaxException {
// Test Unicode categories using \p and \P
// One letter codes: L, M, N, P, S, Z, C
// Two letter codes: Lu, Nd, Sc, Sm, ...
// See java.lang.Character and Unicode standard for complete list
// TODO
- try {
- // Test \p{L}
- // TODO
+ // Test \p{L}
+ // TODO
- // Test \p{N}
- // TODO
+ // Test \p{N}
+ // TODO
- // ... etc
+ // ... etc
- // Test two letter codes:
- // From unicode.org:
- // Lu
- // Ll
- // Lt
- // Lm
- // Lo
- // Mn
- // Mc
- // Me
- // Nd
- // Nl
- // No
- // Pc
- // Pd
- // Ps
- // Pe
- // Pi
- // Pf
- // Po
- // Sm
- // Sc
- // Sk
- // So
- // Zs
- // Zl
- // Zp
- // Cc
- // Cf
- // Cs
- // Co
- // Cn
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ // Test two letter codes:
+ // From unicode.org:
+ // Lu
+ // Ll
+ // Lt
+ // Lm
+ // Lo
+ // Mn
+ // Mc
+ // Me
+ // Nd
+ // Nl
+ // No
+ // Pc
+ // Pd
+ // Ps
+ // Pe
+ // Pi
+ // Pf
+ // Po
+ // Sm
+ // Sc
+ // Sk
+ // So
+ // Zs
+ // Zl
+ // Zp
+ // Cc
+ // Cf
+ // Cs
+ // Co
+ // Cn
}
- public void testUnicodeBlocks() {
+ public void testUnicodeBlocks() throws PatternSyntaxException {
Pattern p;
Matcher m;
int i, j;
@@ -948,339 +854,295 @@
// FIXME:
// Note that LatinExtended-B and ArabicPresentations-B are unrecognized
// by the reference JDK.
- try {
- for (i = 0; i < UBlocks.length; i++) {
- /*
- * p = Pattern.compile("\\p{"+UBlocks[i].name+"}");
- *
- * if (UBlocks[i].low > 0) { m =
- * p.matcher(Character.toString((char)(UBlocks[i].low-1)));
- * assertFalse(m.matches()); } for (j=UBlocks[i].low; j <=
- * UBlocks[i].high; j++) { m =
- * p.matcher(Character.toString((char)j));
- * assertTrue(m.matches()); } if (UBlocks[i].high < 0xFFFF) { m =
- * p.matcher(Character.toString((char)(UBlocks[i].high+1)));
- * assertFalse(m.matches()); }
- *
- * p = Pattern.compile("\\P{"+UBlocks[i].name+"}");
- *
- * if (UBlocks[i].low > 0) { m =
- * p.matcher(Character.toString((char)(UBlocks[i].low-1)));
- * assertTrue(m.matches()); } for (j=UBlocks[i].low; j <
- * UBlocks[i].high; j++) { m =
- * p.matcher(Character.toString((char)j));
- * assertFalse(m.matches()); } if (UBlocks[i].high < 0xFFFF) { m =
- * p.matcher(Character.toString((char)(UBlocks[i].high+1)));
- * assertTrue(m.matches()); }
- */
-
- p = Pattern.compile("\\p{In" + UBlocks[i].name + "}");
-
- if (UBlocks[i].low > 0) {
- m = p.matcher(Character
- .toString((char) (UBlocks[i].low - 1)));
- assertFalse(m.matches());
- }
- for (j = UBlocks[i].low; j <= UBlocks[i].high; j++) {
- m = p.matcher(Character.toString((char) j));
- assertTrue(m.matches());
- }
- if (UBlocks[i].high < 0xFFFF) {
- m = p.matcher(Character
- .toString((char) (UBlocks[i].high + 1)));
- assertFalse(m.matches());
- }
+ for (i = 0; i < UBlocks.length; i++) {
+ /*
+ * p = Pattern.compile("\\p{"+UBlocks[i].name+"}");
+ *
+ * if (UBlocks[i].low > 0) { m =
+ * p.matcher(Character.toString((char)(UBlocks[i].low-1)));
+ * assertFalse(m.matches()); } for (j=UBlocks[i].low; j <=
+ * UBlocks[i].high; j++) { m =
+ * p.matcher(Character.toString((char)j));
+ * assertTrue(m.matches()); } if (UBlocks[i].high < 0xFFFF) { m =
+ * p.matcher(Character.toString((char)(UBlocks[i].high+1)));
+ * assertFalse(m.matches()); }
+ *
+ * p = Pattern.compile("\\P{"+UBlocks[i].name+"}");
+ *
+ * if (UBlocks[i].low > 0) { m =
+ * p.matcher(Character.toString((char)(UBlocks[i].low-1)));
+ * assertTrue(m.matches()); } for (j=UBlocks[i].low; j <
+ * UBlocks[i].high; j++) { m =
+ * p.matcher(Character.toString((char)j));
+ * assertFalse(m.matches()); } if (UBlocks[i].high < 0xFFFF) { m =
+ * p.matcher(Character.toString((char)(UBlocks[i].high+1)));
+ * assertTrue(m.matches()); }
+ */
- p = Pattern.compile("\\P{In" + UBlocks[i].name + "}");
+ p = Pattern.compile("\\p{In" + UBlocks[i].name + "}");
- if (UBlocks[i].low > 0) {
- m = p.matcher(Character
- .toString((char) (UBlocks[i].low - 1)));
- assertTrue(m.matches());
- }
- for (j = UBlocks[i].low; j < UBlocks[i].high; j++) {
- m = p.matcher(Character.toString((char) j));
- assertFalse(m.matches());
- }
- if (UBlocks[i].high < 0xFFFF) {
- m = p.matcher(Character
- .toString((char) (UBlocks[i].high + 1)));
- assertTrue(m.matches());
- }
+ if (UBlocks[i].low > 0) {
+ m = p.matcher(Character.toString((char) (UBlocks[i].low - 1)));
+ assertFalse(m.matches());
+ }
+ for (j = UBlocks[i].low; j <= UBlocks[i].high; j++) {
+ m = p.matcher(Character.toString((char) j));
+ assertTrue(m.matches());
+ }
+ if (UBlocks[i].high < 0xFFFF) {
+ m = p.matcher(Character.toString((char) (UBlocks[i].high + 1)));
+ assertFalse(m.matches());
+ }
+
+ p = Pattern.compile("\\P{In" + UBlocks[i].name + "}");
+
+ if (UBlocks[i].low > 0) {
+ m = p.matcher(Character.toString((char) (UBlocks[i].low - 1)));
+ assertTrue(m.matches());
+ }
+ for (j = UBlocks[i].low; j < UBlocks[i].high; j++) {
+ m = p.matcher(Character.toString((char) j));
+ assertFalse(m.matches());
+ }
+ if (UBlocks[i].high < 0xFFFF) {
+ m = p.matcher(Character.toString((char) (UBlocks[i].high + 1)));
+ assertTrue(m.matches());
}
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
}
}
- public void testCapturingGroups() {
- try {
- // Test simple capturing groups
- // TODO
+ public void testCapturingGroups() throws PatternSyntaxException {
+ // Test simple capturing groups
+ // TODO
- // Test grouping without capture (?:...)
- // TODO
+ // Test grouping without capture (?:...)
+ // TODO
- // Test combination of grouping and capture
- // TODO
+ // Test combination of grouping and capture
+ // TODO
- // Test \<num> sequence with capturing and non-capturing groups
- // TODO
+ // Test \<num> sequence with capturing and non-capturing groups
+ // TODO
- // Test \<num> with <num> out of range
- // TODO
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ // Test \<num> with <num> out of range
+ // TODO
}
public void testRepeats() {
- try {
- // Test ?
- // TODO
+ // Test ?
+ // TODO
- // Test *
- // TODO
+ // Test *
+ // TODO
- // Test +
- // TODO
+ // Test +
+ // TODO
- // Test {<num>}, including 0, 1 and more
- // TODO
+ // Test {<num>}, including 0, 1 and more
+ // TODO
- // Test {<num>,}, including 0, 1 and more
- // TODO
+ // Test {<num>,}, including 0, 1 and more
+ // TODO
- // Test {<n1>,<n2>}, with n1 < n2, n1 = n2 and n1 > n2 (illegal?)
- // TODO
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ // Test {<n1>,<n2>}, with n1 < n2, n1 = n2 and n1 > n2 (illegal?)
+ // TODO
}
- public void testAnchors() {
- try {
- // Test ^, default and MULTILINE
- // TODO
+ public void testAnchors() throws PatternSyntaxException {
+ // Test ^, default and MULTILINE
+ // TODO
- // Test $, default and MULTILINE
- // TODO
+ // Test $, default and MULTILINE
+ // TODO
- // Test \b (word boundary)
- // TODO
+ // Test \b (word boundary)
+ // TODO
- // Test \B (not a word boundary)
- // TODO
+ // Test \B (not a word boundary)
+ // TODO
- // Test \A (beginning of string)
- // TODO
+ // Test \A (beginning of string)
+ // TODO
- // Test \Z (end of string)
- // TODO
+ // Test \Z (end of string)
+ // TODO
- // Test \z (end of string)
- // TODO
+ // Test \z (end of string)
+ // TODO
- // Test \G
- // TODO
+ // Test \G
+ // TODO
- // Test positive lookahead using (?=...)
- // TODO
+ // Test positive lookahead using (?=...)
+ // TODO
- // Test negative lookahead using (?!...)
- // TODO
+ // Test negative lookahead using (?!...)
+ // TODO
- // Test positive lookbehind using (?<=...)
- // TODO
+ // Test positive lookbehind using (?<=...)
+ // TODO
- // Test negative lookbehind using (?<!...)
- // TODO
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ // Test negative lookbehind using (?<!...)
+ // TODO
}
- public void testMisc() {
+ public void testMisc() throws PatternSyntaxException {
Pattern p;
Matcher m;
- try {
- // Test (?>...)
- // TODO
+ // Test (?>...)
+ // TODO
- // Test (?onflags-offflags)
- // Valid flags are i,m,d,s,u,x
- // TODO
-
- // Test (?onflags-offflags:...)
- // TODO
-
- // Test \Q, \E
- p = Pattern.compile("[a-z]+;\\Q[a-z]+;\\Q(foo.*);\\E[0-9]+");
- m = p.matcher("abc;[a-z]+;\\Q(foo.*);411");
- assertTrue(m.matches());
- m = p.matcher("abc;def;foo42;555");
- assertFalse(m.matches());
- m = p.matcher("abc;\\Qdef;\\Qfoo99;\\E123");
- assertFalse(m.matches());
+ // Test (?onflags-offflags)
+ // Valid flags are i,m,d,s,u,x
+ // TODO
- p = Pattern.compile("[a-z]+;(foo[0-9]-\\Q(...)\\E);[0-9]+");
- m = p.matcher("abc;foo5-(...);123");
- assertTrue(m.matches());
- assertTrue(m.group(1).equals("foo5-(...)"));
- m = p.matcher("abc;foo9-(xxx);789");
- assertFalse(m.matches());
+ // Test (?onflags-offflags:...)
+ // TODO
- p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q$-\\E]+);[0-9]+");
- m = p.matcher("abc;bar0-def$-;123");
- assertTrue(m.matches());
+ // Test \Q, \E
+ p = Pattern.compile("[a-z]+;\\Q[a-z]+;\\Q(foo.*);\\E[0-9]+");
+ m = p.matcher("abc;[a-z]+;\\Q(foo.*);411");
+ assertTrue(m.matches());
+ m = p.matcher("abc;def;foo42;555");
+ assertFalse(m.matches());
+ m = p.matcher("abc;\\Qdef;\\Qfoo99;\\E123");
+ assertFalse(m.matches());
+
+ p = Pattern.compile("[a-z]+;(foo[0-9]-\\Q(...)\\E);[0-9]+");
+ m = p.matcher("abc;foo5-(...);123");
+ assertTrue(m.matches());
+ assertTrue(m.group(1).equals("foo5-(...)"));
+ m = p.matcher("abc;foo9-(xxx);789");
+ assertFalse(m.matches());
+
+ p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q$-\\E]+);[0-9]+");
+ m = p.matcher("abc;bar0-def$-;123");
+ assertTrue(m.matches());
- // FIXME:
- // This should work the same as the pattern above but fails with the
- // the reference JDK
- p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q-$\\E]+);[0-9]+");
- m = p.matcher("abc;bar0-def$-;123");
- // assertTrue(m.matches());
-
- // FIXME:
- // This should work too .. it looks as if just about anything that
- // has more
- // than one character between \Q and \E is broken in the the reference JDK
- p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q[0-9]\\E]+);[0-9]+");
- m = p.matcher("abc;bar0-def[99]-]0x[;123");
- // assertTrue(m.matches());
-
- // This is the same as above but with explicit escapes .. and this
- // does work
- // on the the reference JDK
- p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\[0\\-9\\]]+);[0-9]+");
- m = p.matcher("abc;bar0-def[99]-]0x[;123");
- assertTrue(m.matches());
+ // FIXME:
+ // This should work the same as the pattern above but fails with the
+ // the reference JDK
+ p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q-$\\E]+);[0-9]+");
+ m = p.matcher("abc;bar0-def$-;123");
+ // assertTrue(m.matches());
- // Test #<comment text>
- // TODO
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ // FIXME:
+ // This should work too .. it looks as if just about anything that
+ // has more
+ // than one character between \Q and \E is broken in the the reference JDK
+ p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\Q[0-9]\\E]+);[0-9]+");
+ m = p.matcher("abc;bar0-def[99]-]0x[;123");
+ // assertTrue(m.matches());
+
+ // This is the same as above but with explicit escapes .. and this
+ // does work
+ // on the the reference JDK
+ p = Pattern.compile("[a-z]+;(bar[0-9]-[a-z\\[0\\-9\\]]+);[0-9]+");
+ m = p.matcher("abc;bar0-def[99]-]0x[;123");
+ assertTrue(m.matches());
+
+ // Test #<comment text>
+ // TODO
}
- public void testCompile1() {
- try {
- Pattern pattern = Pattern
- .compile("[0-9A-Za-z][0-9A-Za-z\\x2e\\x3a\\x2d\\x5f]*");
- String name = "iso-8859-1";
- assertTrue(pattern.matcher(name).matches());
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ public void testCompile1() throws PatternSyntaxException {
+ Pattern pattern = Pattern
+ .compile("[0-9A-Za-z][0-9A-Za-z\\x2e\\x3a\\x2d\\x5f]*");
+ String name = "iso-8859-1";
+ assertTrue(pattern.matcher(name).matches());
}
- public void testCompile2() {
+ public void testCompile2() throws PatternSyntaxException {
String findString = "\\Qimport\\E";
- try {
- Pattern pattern = Pattern.compile(findString, 0);
- Matcher matcher = pattern.matcher(new String(
- "import a.A;\n\n import b.B;\nclass C {}"));
+ Pattern pattern = Pattern.compile(findString, 0);
+ Matcher matcher = pattern.matcher(new String(
+ "import a.A;\n\n import b.B;\nclass C {}"));
- assertTrue(matcher.find(0));
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ assertTrue(matcher.find(0));
}
- public void testCompile3() {
+ public void testCompile3() throws PatternSyntaxException {
Pattern p;
Matcher m;
- try {
- p = Pattern.compile("a$");
- m = p.matcher("a\n");
- assertTrue(m.find());
- assertTrue(m.group().equals("a"));
- assertFalse(m.find());
-
- p = Pattern.compile("(a$)");
- m = p.matcher("a\n");
- assertTrue(m.find());
- assertTrue(m.group().equals("a"));
- assertTrue(m.group(1).equals("a"));
- assertFalse(m.find());
-
- p = Pattern.compile("^.*$", Pattern.MULTILINE);
-
- m = p.matcher("a\n");
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals("a"));
- assertFalse(m.find());
-
- m = p.matcher("a\nb\n");
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals("a"));
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals("b"));
- assertFalse(m.find());
-
- m = p.matcher("a\nb");
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals("a"));
- assertTrue(m.find());
- assertTrue(m.group().equals("b"));
- assertFalse(m.find());
-
- m = p.matcher("\naa\r\nbb\rcc\n\n");
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals(""));
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals("aa"));
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals("bb"));
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals("cc"));
- assertTrue(m.find());
- // System.out.println("["+m.group()+"]");
- assertTrue(m.group().equals(""));
- assertFalse(m.find());
-
- m = p.matcher("a");
- assertTrue(m.find());
- assertTrue(m.group().equals("a"));
- assertFalse(m.find());
-
- m = p.matcher("");
- // FIXME: This matches the reference behaviour but is
- // inconsistent with matching "a" - ie. the end of the
- // target string should match against $ always but this
- // appears to work with the null string only when not in
- // multiline mode (see below)
- assertFalse(m.find());
-
- p = Pattern.compile("^.*$");
- m = p.matcher("");
- assertTrue(m.find());
- assertTrue(m.group().equals(""));
- assertFalse(m.find());
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- assertTrue(false);
- }
+ p = Pattern.compile("a$");
+ m = p.matcher("a\n");
+ assertTrue(m.find());
+ assertTrue(m.group().equals("a"));
+ assertFalse(m.find());
+
+ p = Pattern.compile("(a$)");
+ m = p.matcher("a\n");
+ assertTrue(m.find());
+ assertTrue(m.group().equals("a"));
+ assertTrue(m.group(1).equals("a"));
+ assertFalse(m.find());
+
+ p = Pattern.compile("^.*$", Pattern.MULTILINE);
+
+ m = p.matcher("a\n");
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals("a"));
+ assertFalse(m.find());
+
+ m = p.matcher("a\nb\n");
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals("a"));
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals("b"));
+ assertFalse(m.find());
+
+ m = p.matcher("a\nb");
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals("a"));
+ assertTrue(m.find());
+ assertTrue(m.group().equals("b"));
+ assertFalse(m.find());
+
+ m = p.matcher("\naa\r\nbb\rcc\n\n");
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals(""));
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals("aa"));
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals("bb"));
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals("cc"));
+ assertTrue(m.find());
+ // System.out.println("["+m.group()+"]");
+ assertTrue(m.group().equals(""));
+ assertFalse(m.find());
+
+ m = p.matcher("a");
+ assertTrue(m.find());
+ assertTrue(m.group().equals("a"));
+ assertFalse(m.find());
+
+ m = p.matcher("");
+ // FIXME: This matches the reference behaviour but is
+ // inconsistent with matching "a" - ie. the end of the
+ // target string should match against $ always but this
+ // appears to work with the null string only when not in
+ // multiline mode (see below)
+ assertFalse(m.find());
+
+ p = Pattern.compile("^.*$");
+ m = p.matcher("");
+ assertTrue(m.find());
+ assertTrue(m.group().equals(""));
+ assertFalse(m.find());
}
- public void testCompile4() {
+ public void testCompile4() throws PatternSyntaxException {
String findString = "\\Qpublic\\E";
StringBuffer text = new StringBuffer(" public class Class {\n"
+ " public class Class {");
@@ -1301,7 +1163,7 @@
assertFalse(found);
}
- public void testCompile5() {
+ public void testCompile5() throws PatternSyntaxException {
Pattern p = Pattern.compile("^[0-9]");
String s[] = p.split("12", -1);
assertEquals("", s[0]);
@@ -1309,19 +1171,17 @@
assertEquals(2, s.length);
}
-
-// public void testCompile6() {
-// String regex = "[\\p{L}[\\p{Mn}[\\p{Pc}[\\p{Nd}[\\p{Nl}[\\p{Sc}]]]]]]+";
-// String regex = "[\\p{L}\\p{Mn}\\p{Pc}\\p{Nd}\\p{Nl}\\p{Sc}]+";
-// try {
-// Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
-// assertTrue(true);
-// } catch (PatternSyntaxException e) {
-// System.out.println(e.getMessage());
-// assertTrue(false);
-// }
-// }
-
+ // public void testCompile6() {
+ // String regex = "[\\p{L}[\\p{Mn}[\\p{Pc}[\\p{Nd}[\\p{Nl}[\\p{Sc}]]]]]]+";
+ // String regex = "[\\p{L}\\p{Mn}\\p{Pc}\\p{Nd}\\p{Nl}\\p{Sc}]+";
+ // try {
+ // Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
+ // assertTrue(true);
+ // } catch (PatternSyntaxException e) {
+ // System.out.println(e.getMessage());
+ // assertTrue(false);
+ // }
+ // }
private static class UBInfo {
public UBInfo(int low, int high, String name) {
@@ -1546,4 +1406,4 @@
/* FFF0; FFFD; Specials */
new UBInfo(0xFFF0, 0xFFFD, "Specials") // Character.UnicodeBlock.SPECIALS
};
-}
+}
\ No newline at end of file
Copied: incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/ReplaceTest.java (from r394009, incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/ReplaceTests.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/ReplaceTest.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/ReplaceTest.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/ReplaceTests.java&r1=394009&r2=394041&rev=394041&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/ReplaceTests.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/ReplaceTest.java Fri Apr 14 00:42:15 2006
@@ -20,27 +20,20 @@
import java.util.regex.Matcher;
import java.util.regex.PatternSyntaxException;
-public class ReplaceTests extends TestCase {
+public class ReplaceTest extends TestCase {
- public void testSimpleReplace() {
- String target, pattern, repl, s;
- Pattern p = null;
- Matcher m;
+ public void testSimpleReplace() throws PatternSyntaxException {
+ String target, pattern, repl;
target = "foobarfobarfoofo1";
pattern = "fo[^o]";
repl = "xxx";
- try {
- p = Pattern.compile(pattern);
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
- m = p.matcher(target);
- s = m.replaceFirst(repl);
- assertTrue(s.equals("foobarxxxarfoofo1"));
- s = m.replaceAll(repl);
- assertTrue(s.equals("foobarxxxarfooxxx"));
+
+ Pattern p = Pattern.compile(pattern);
+ Matcher m = p.matcher(target);
+
+ assertEquals("foobarxxxarfoofo1", m.replaceFirst(repl));
+ assertEquals("foobarxxxarfooxxx", m.replaceAll(repl));
}
public void testCaptureReplace() {
@@ -51,12 +44,8 @@
target = "[31]foo;bar[42];[99]xyz";
pattern = "\\[([0-9]+)\\]([a-z]+)";
repl = "$2[$1]";
- try {
- p = Pattern.compile(pattern);
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+
+ p = Pattern.compile(pattern);
m = p.matcher(target);
s = m.replaceFirst(repl);
assertTrue(s.equals("foo[31];bar[42];[99]xyz"));
@@ -66,12 +55,7 @@
target = "[31]foo(42)bar{63}zoo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;";
pattern = "\\[([0-9]+)\\]([a-z]+)\\(([0-9]+)\\)([a-z]+)\\{([0-9]+)\\}([a-z]+)";
repl = "[$5]$6($3)$4{$1}$2";
- try {
- p = Pattern.compile(pattern);
- } catch (PatternSyntaxException e) {
- System.out.println(e.getMessage());
- fail();
- }
+ p = Pattern.compile(pattern);
m = p.matcher(target);
s = m.replaceFirst(repl);
// System.out.println(s);
Copied: incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/SplitTest.java (from r394009, incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/SplitTests.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/SplitTest.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/SplitTest.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/SplitTests.java&r1=394009&r2=394041&rev=394041&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/SplitTests.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/regex/src/test/java/tests/api/java/util/regex/SplitTest.java Fri Apr 14 00:42:15 2006
@@ -22,7 +22,7 @@
* TODO Type description
*
*/
-public class SplitTests extends TestCase {
+public class SplitTest extends TestCase {
public void testSimple() {
Pattern p = Pattern.compile("/");
@@ -34,15 +34,8 @@
}
}
- public void testSplit1() {
- Pattern p = null;
-
- // This set of tests comes from the O'Reilly NIO book, page 160
- try {
- p = Pattern.compile(" ");
- } catch (PatternSyntaxException e) {
- fail();
- }
+ public void testSplit1() throws PatternSyntaxException {
+ Pattern p = Pattern.compile(" ");
String input = "poodle zoo";
String tokens[];
@@ -71,12 +64,7 @@
assertTrue(tokens[0].equals("poodle"));
assertTrue(tokens[1].equals("zoo"));
- try {
- p = Pattern.compile("d");
- } catch (PatternSyntaxException e) {
- fail();
- return;
- }
+ p = Pattern.compile("d");
tokens = p.split(input, 1);
assertTrue(tokens.length == 1);
@@ -102,11 +90,7 @@
assertTrue(tokens[0].equals("poo"));
assertTrue(tokens[1].equals("le zoo"));
- try {
- p = Pattern.compile("o");
- } catch (PatternSyntaxException e) {
- fail();
- }
+ p = Pattern.compile("o");
tokens = p.split(input, 1);
assertTrue(tokens.length == 1);