diff options
author | Tobias Thierer <tobiast@google.com> | 2018-06-12 10:42:01 +0100 |
---|---|---|
committer | Tobias Thierer <tobiast@google.com> | 2018-06-12 11:30:52 +0100 |
commit | 53c667dbc9f27149a08fd79953841f0a31c24182 (patch) | |
tree | 31c25316a1ac0379801bc55b2915aa3b6a4309f5 /harmony-tests | |
parent | ede798d23c864cff55eae0d5fea7c68af689e033 (diff) |
Unify duplicate copies of harmony {Matcher2,Mode,Replace,Split}Test.
Android is carrying near-duplicate copies of these tests introduced in:
1.) luni/: commit fdb2704414a9ed92394ada0d1395e4db86889465
(2008-10-21, "Initial Contribution")
2.) harmony-tests/: commit bfd68b1dd4409f61fbc6800ba61f4605ad57945b
(2013-05-03, "Add the harmony regex tests.")
The initial copies already differed, eg.
This CL integrates differences between the two copies. After this CL,
the files are pairwise identical except:
- they are in different packages
- luni's SplitTest.java is missing the file header
Most of the changes are integrated luni/ -> harmony-tests, keeping
tests that were previously only present in one copy, eg.:
- slightly more verbose test case in ReplaceTest.testSimpleReplace()
- SplitTest.testEmptySplits() (added in commit
51809b9c7995d8b813f68712b096d23179de3af0 )
- Matcher2Test tests added by http://r.android.com/695546
In cases where the "better" version is not clear, an arbitrary judgement
call was made. Examples:
- @SuppressWarnings("nls"): Differed in the original imports.
Might be Eclipse specific, but kept for now.
- minor whitespace changes
- formatting of ReplaceTest
This CL does not touch the following files / differences:
- luni/ has PatternTest but harmony-tests/ has MatcherTest
- luni/ has PatternSyntaxExceptionTest but harmony-tests/ has
PatternErrorTest
Bug: 109727025
Test: Treehugger
Test: Ran CtsLibcoreTestCases at a later CL.
Change-Id: Ib1c1c25c2010c082e119a0b8b21dadf58576e3f7
Diffstat (limited to 'harmony-tests')
4 files changed, 67 insertions, 13 deletions
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java index 9d4ee70d83..57cc65fd63 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/Matcher2Test.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ModeTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ModeTest.java index c34cebea11..f696dff829 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ModeTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ModeTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -27,6 +27,7 @@ import junit.framework.TestCase; */ @SuppressWarnings("nls") public class ModeTest extends TestCase { + public void testCase() throws PatternSyntaxException { Pattern p; Matcher m; @@ -37,6 +38,7 @@ public class ModeTest extends TestCase { assertEquals("dog", m.group(1)); assertFalse(m.find()); + p = Pattern.compile("([a-z]+)[0-9]+", Pattern.CASE_INSENSITIVE); m = p.matcher("cAt123#doG345"); assertTrue(m.find()); @@ -45,6 +47,7 @@ public class ModeTest extends TestCase { assertEquals("doG", m.group(1)); assertFalse(m.find()); + p = Pattern.compile("(?i)([a-z]+)[0-9]+"); m = p.matcher("cAt123#doG345"); assertTrue(m.find()); @@ -67,6 +70,7 @@ public class ModeTest extends TestCase { m = p.matcher("barfoo"); assertFalse(m.find()); + p = Pattern.compile("foo$"); m = p.matcher("foobar"); assertFalse(m.find()); @@ -76,6 +80,7 @@ public class ModeTest extends TestCase { assertTrue(m.start() == 3 && m.end() == 6); assertFalse(m.find()); + p = Pattern.compile("^foo([0-9]*)", Pattern.MULTILINE); m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4"); assertTrue(m.find()); @@ -84,6 +89,7 @@ public class ModeTest extends TestCase { assertEquals("2", m.group(1)); assertFalse(m.find()); + p = Pattern.compile("foo([0-9]*)$", Pattern.MULTILINE); m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4"); assertTrue(m.find()); @@ -92,6 +98,7 @@ public class ModeTest extends TestCase { assertEquals("4", m.group(1)); assertFalse(m.find()); + p = Pattern.compile("(?m)^foo([0-9]*)"); m = p.matcher("foo1bar\nfoo2foo3\nbarfoo4"); assertTrue(m.find()); diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ReplaceTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ReplaceTest.java index 1eac3f3013..2226df87eb 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ReplaceTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/ReplaceTest.java @@ -4,9 +4,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -28,15 +28,15 @@ public class ReplaceTest extends TestCase { public void testSimpleReplace() throws PatternSyntaxException { String target, pattern, repl; - target = "foobarfobarfoofo1"; + target = "foobarfobarfoofo1barfort"; pattern = "fo[^o]"; repl = "xxx"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(target); - assertEquals("foobarxxxarfoofo1", m.replaceFirst(repl)); - assertEquals("foobarxxxarfooxxx", m.replaceAll(repl)); + assertEquals("foobarxxxarfoofo1barfort", m.replaceFirst(repl)); + assertEquals("foobarxxxarfooxxxbarxxxt", m.replaceAll(repl)); } public void testCaptureReplace() { diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/SplitTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/SplitTest.java index 5a5bc2ba6e..c40ef7c6f1 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/SplitTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/regex/SplitTest.java @@ -32,9 +32,52 @@ public class SplitTest extends TestCase { Pattern p = Pattern.compile("/"); String[] results = p.split("have/you/done/it/right"); String[] expected = new String[] { "have", "you", "done", "it", "right" }; - assertEquals(expected.length, results.length); + assertArraysEqual(expected, results); + } + + @SuppressWarnings("InvalidPatternSyntax") + public void testEmptySplits() { + // Trailing empty matches are removed. + assertArraysEqual(new String[0], "hello".split(".")); + assertArraysEqual(new String[] { "1", "2" }, "1:2:".split(":")); + // ...including when that results in an empty result. + assertArraysEqual(new String[0], ":".split(":")); + // ...but not when limit < 0. + assertArraysEqual(new String[] { "1", "2", "" }, "1:2:".split(":", -1)); + + // Leading empty matches are retained. + assertArraysEqual(new String[] { "", "", "o" }, "hello".split("..")); + + // A separator that doesn't occur in the input gets you the input. + assertArraysEqual(new String[] { "hello" }, "hello".split("not-present-in-test")); + // ...including when the input is the empty string. + // (Perl returns an empty list instead.) + assertArraysEqual(new String[] { "" }, "".split("not-present-in-test")); + assertArraysEqual(new String[] { "" }, "".split("A?")); + + // The limit argument controls the size of the result. + // If l == 0, the result is as long as needed, except trailing empty matches are dropped. + // If l < 0, the result is as long as needed, and trailing empty matches are retained. + // If l > 0, the result contains the first l matches, plus one string containing the remaining input. + // Examples without a trailing separator (and hence without a trailing empty match): + assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", 0)); + assertArraysEqual(new String[] { "a,b,c" }, "a,b,c".split(",", 1)); + assertArraysEqual(new String[] { "a", "b,c" }, "a,b,c".split(",", 2)); + assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", 3)); + assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", Integer.MAX_VALUE)); + // Examples with a trailing separator (and hence possibly with a trailing empty match): + assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c,".split(",", 0)); + assertArraysEqual(new String[] { "a,b,c," }, "a,b,c,".split(",", 1)); + assertArraysEqual(new String[] { "a", "b,c," }, "a,b,c,".split(",", 2)); + assertArraysEqual(new String[] { "a", "b", "c," }, "a,b,c,".split(",", 3)); + assertArraysEqual(new String[] { "a", "b", "c", "" }, "a,b,c,".split(",", Integer.MAX_VALUE)); + assertArraysEqual(new String[] { "a", "b", "c", "" }, "a,b,c,".split(",", -1)); + } + + private void assertArraysEqual(String[] expected, String[] actual) { + assertEquals(expected.length, actual.length); for (int i = 0; i < expected.length; i++) { - assertEquals(results[i], expected[i]); + assertEquals(Integer.toString(i), expected[i], actual[i]); } } @@ -150,13 +193,17 @@ public class SplitTest extends TestCase { assertEquals("c", s[3]); assertEquals("d", s[4]); assertEquals("", s[5]); + + // Regression test for Android + assertEquals("GOOG,23,500".split("|").length, 12); } + public void testSplitSupplementaryWithEmptyString() { /* - * See http://www.unicode.org/reports/tr18/#Supplementary_Characters We - * have to treat text as code points not code units. + * See http://www.unicode.org/reports/tr18/#Supplementary_Characters + * We have to treat text as code points not code units. */ Pattern p = Pattern.compile(""); String s[]; |