1package org.apache.harmony.regex.tests.java.util.regex;
2
3import dalvik.annotation.TestTargetClass;
4import dalvik.annotation.TestTargets;
5import dalvik.annotation.TestTargetNew;
6import dalvik.annotation.TestLevel;
7
8import junit.framework.TestCase;
9import java.util.regex.*;
10
11@TestTargetClass(java.util.regex.Pattern.class)
12/**
13 * TODO Type description
14 *
15 */
16public class SplitTest extends TestCase {
17    @TestTargets({
18        @TestTargetNew(
19            level = TestLevel.PARTIAL_COMPLETE,
20            notes = "Verifies the basic functionality of split(java.lang.CharSequence) & compile(java.lang.String)methods.",
21            method = "split",
22            args = {java.lang.CharSequence.class}
23        ),
24        @TestTargetNew(
25            level = TestLevel.PARTIAL_COMPLETE,
26            notes = "Verifies the basic functionality of split(java.lang.CharSequence) & compile(java.lang.String)methods.",
27            method = "compile",
28            args = {java.lang.String.class}
29        )
30    })
31    public void testSimple() {
32        Pattern p = Pattern.compile("/");
33        String[] results = p.split("have/you/done/it/right");
34        String[] expected = new String[] { "have", "you", "done", "it", "right" };
35        assertArraysEqual(expected, results);
36    }
37
38    @TestTargets({
39        @TestTargetNew(
40            level = TestLevel.PARTIAL_COMPLETE,
41            notes = "Verifies the basic functionality of split with empty matches.",
42            method = "split",
43            args = {java.lang.CharSequence.class}
44        )
45    })
46    public void testEmptySplits() {
47        // Trailing empty matches are removed.
48        assertArraysEqual(new String[0], "hello".split("."));
49        assertArraysEqual(new String[] { "1", "2" }, "1:2:".split(":"));
50        // ...including when that results in an empty result.
51        assertArraysEqual(new String[0], ":".split(":"));
52        // ...but not when limit < 0.
53        assertArraysEqual(new String[] { "1", "2", "" }, "1:2:".split(":", -1));
54
55        // Leading empty matches are retained.
56        assertArraysEqual(new String[] { "", "", "o" }, "hello".split(".."));
57
58        // A separator that doesn't occur in the input gets you the input.
59        assertArraysEqual(new String[] { "hello" }, "hello".split("not-present-in-test"));
60        // ...including when the input is the empty string.
61        // (Perl returns an empty list instead.)
62        assertArraysEqual(new String[] { "" }, "".split("not-present-in-test"));
63        assertArraysEqual(new String[] { "" }, "".split("A?"));
64
65        // The limit argument controls the size of the result.
66        // If l == 0, the result is as long as needed, except trailing empty matches are dropped.
67        // If l < 0, the result is as long as needed, and trailing empty matches are retained.
68        // If l > 0, the result contains the first l matches, plus one string containing the remaining input.
69        // Examples without a trailing separator (and hence without a trailing empty match):
70        assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", 0));
71        assertArraysEqual(new String[] { "a,b,c" }, "a,b,c".split(",", 1));
72        assertArraysEqual(new String[] { "a", "b,c" }, "a,b,c".split(",", 2));
73        assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", 3));
74        assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", Integer.MAX_VALUE));
75        // Examples with a trailing separator (and hence possibly with a trailing empty match):
76        assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c,".split(",", 0));
77        assertArraysEqual(new String[] { "a,b,c," }, "a,b,c,".split(",", 1));
78        assertArraysEqual(new String[] { "a", "b,c," }, "a,b,c,".split(",", 2));
79        assertArraysEqual(new String[] { "a", "b", "c," }, "a,b,c,".split(",", 3));
80        assertArraysEqual(new String[] { "a", "b", "c", "" }, "a,b,c,".split(",", Integer.MAX_VALUE));
81        assertArraysEqual(new String[] { "a", "b", "c", "" }, "a,b,c,".split(",", -1));
82    }
83
84    private void assertArraysEqual(String[] expected, String[] actual) {
85        assertEquals(expected.length, actual.length);
86        for (int i = 0; i < expected.length; i++) {
87            assertEquals(Integer.toString(i), expected[i], actual[i]);
88        }
89    }
90
91    @TestTargetNew(
92        level = TestLevel.PARTIAL_COMPLETE,
93        notes = "Verifies the functionality of split(java.lang.CharSequence). Test uses not empty pattern.",
94        method = "split",
95        args = {java.lang.CharSequence.class, int.class}
96    )
97    public void testSplit1() throws PatternSyntaxException {
98        Pattern p = Pattern.compile(" ");
99
100        String input = "poodle zoo";
101        String tokens[];
102
103        tokens = p.split(input, 1);
104        assertEquals(1, tokens.length);
105        assertTrue(tokens[0].equals(input));
106        tokens = p.split(input, 2);
107        assertEquals(2, tokens.length);
108        assertEquals("poodle", tokens[0]);
109        assertEquals("zoo", tokens[1]);
110        tokens = p.split(input, 5);
111        assertEquals(2, tokens.length);
112        assertEquals("poodle", tokens[0]);
113        assertEquals("zoo", tokens[1]);
114        tokens = p.split(input, -2);
115        assertEquals(2, tokens.length);
116        assertEquals("poodle", tokens[0]);
117        assertEquals("zoo", tokens[1]);
118        tokens = p.split(input, 0);
119        assertEquals(2, tokens.length);
120        assertEquals("poodle", tokens[0]);
121        assertEquals("zoo", tokens[1]);
122        tokens = p.split(input);
123        assertEquals(2, tokens.length);
124        assertEquals("poodle", tokens[0]);
125        assertEquals("zoo", tokens[1]);
126
127        p = Pattern.compile("d");
128
129        tokens = p.split(input, 1);
130        assertEquals(1, tokens.length);
131        assertTrue(tokens[0].equals(input));
132        tokens = p.split(input, 2);
133        assertEquals(2, tokens.length);
134        assertEquals("poo", tokens[0]);
135        assertEquals("le zoo", tokens[1]);
136        tokens = p.split(input, 5);
137        assertEquals(2, tokens.length);
138        assertEquals("poo", tokens[0]);
139        assertEquals("le zoo", tokens[1]);
140        tokens = p.split(input, -2);
141        assertEquals(2, tokens.length);
142        assertEquals("poo", tokens[0]);
143        assertEquals("le zoo", tokens[1]);
144        tokens = p.split(input, 0);
145        assertEquals(2, tokens.length);
146        assertEquals("poo", tokens[0]);
147        assertEquals("le zoo", tokens[1]);
148        tokens = p.split(input);
149        assertEquals(2, tokens.length);
150        assertEquals("poo", tokens[0]);
151        assertEquals("le zoo", tokens[1]);
152
153        p = Pattern.compile("o");
154
155        tokens = p.split(input, 1);
156        assertEquals(1, tokens.length);
157        assertTrue(tokens[0].equals(input));
158        tokens = p.split(input, 2);
159        assertEquals(2, tokens.length);
160        assertEquals("p", tokens[0]);
161        assertEquals("odle zoo", tokens[1]);
162        tokens = p.split(input, 5);
163        assertEquals(5, tokens.length);
164        assertEquals("p", tokens[0]);
165        assertTrue(tokens[1].equals(""));
166        assertEquals("dle z", tokens[2]);
167        assertTrue(tokens[3].equals(""));
168        assertTrue(tokens[4].equals(""));
169        tokens = p.split(input, -2);
170        assertEquals(5, tokens.length);
171        assertEquals("p", tokens[0]);
172        assertTrue(tokens[1].equals(""));
173        assertEquals("dle z", tokens[2]);
174        assertTrue(tokens[3].equals(""));
175        assertTrue(tokens[4].equals(""));
176        tokens = p.split(input, 0);
177        assertEquals(3, tokens.length);
178        assertEquals("p", tokens[0]);
179        assertTrue(tokens[1].equals(""));
180        assertEquals("dle z", tokens[2]);
181        tokens = p.split(input);
182        assertEquals(3, tokens.length);
183        assertEquals("p", tokens[0]);
184        assertTrue(tokens[1].equals(""));
185        assertEquals("dle z", tokens[2]);
186    }
187
188    @TestTargetNew(
189        level = TestLevel.PARTIAL_COMPLETE,
190        notes = "Verifies the functionality of split(java.lang.CharSequence). Test uses empty pattern.",
191        method = "split",
192        args = {java.lang.CharSequence.class, int.class}
193    )
194    public void testSplit2() {
195        Pattern p = Pattern.compile("");
196        String s[];
197        s = p.split("a", -1);
198        assertEquals(3, s.length);
199        assertEquals("", s[0]);
200        assertEquals("a", s[1]);
201        assertEquals("", s[2]);
202
203        s = p.split("", -1);
204        assertEquals(1, s.length);
205        assertEquals("", s[0]);
206
207        s = p.split("abcd", -1);
208        assertEquals(6, s.length);
209        assertEquals("", s[0]);
210        assertEquals("a", s[1]);
211        assertEquals("b", s[2]);
212        assertEquals("c", s[3]);
213        assertEquals("d", s[4]);
214        assertEquals("", s[5]);
215
216        // Regression test for Android
217        assertEquals("GOOG,23,500".split("|").length, 12);
218    }
219
220
221    @TestTargets({
222        @TestTargetNew(
223            level = TestLevel.PARTIAL_COMPLETE,
224            notes = "Verifies the functionality of split(java.lang.CharSequence) & compile(java.lang.String, int) methods. Test uses empty pattern and supplementary chars.",
225            method = "split",
226            args = {java.lang.CharSequence.class, int.class}
227        ),
228        @TestTargetNew(
229            level = TestLevel.PARTIAL_COMPLETE,
230            notes = "Verifies the functionality of split(java.lang.CharSequence) & compile(java.lang.String, int) methods. Test uses empty pattern and supplementary chars.",
231            method = "compile",
232            args = {java.lang.String.class}
233        )
234    })
235    public void testSplitSupplementaryWithEmptyString() {
236
237        /*
238         * See http://www.unicode.org/reports/tr18/#Supplementary_Characters
239         * We have to treat text as code points not code units.
240         */
241        Pattern p = Pattern.compile("");
242        String s[];
243        s = p.split("a\ud869\uded6b", -1);
244        assertEquals(5, s.length);
245        assertEquals("", s[0]);
246        assertEquals("a", s[1]);
247        assertEquals("\ud869\uded6", s[2]);
248        assertEquals("b", s[3]);
249        assertEquals("", s[4]);
250    }
251}
252