• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package org.apache.harmony.tests.java.util.regex;
19 
20 import java.util.Arrays;
21 import java.util.regex.Pattern;
22 import java.util.regex.PatternSyntaxException;
23 
24 import junit.framework.TestCase;
25 import java.util.regex.*;
26 
27 /**
28  * TODO Type description
29  *
30  */
31 @SuppressWarnings("nls")
32 public class SplitTest extends TestCase {
33 
testSimple()34     public void testSimple() {
35         Pattern p = Pattern.compile("/");
36         String[] results = p.split("have/you/done/it/right");
37         String[] expected = new String[] { "have", "you", "done", "it", "right" };
38         assertArraysEqual(expected, results);
39     }
40 
41     @SuppressWarnings("InvalidPatternSyntax")
testEmptySplits()42     public void testEmptySplits() {
43         // Trailing empty matches are removed.
44         assertArraysEqual(new String[0], "hello".split("."));
45         assertArraysEqual(new String[] { "1", "2" }, "1:2:".split(":"));
46         // ...including when that results in an empty result.
47         assertArraysEqual(new String[0], ":".split(":"));
48         // ...but not when limit < 0.
49         assertArraysEqual(new String[] { "1", "2", "" }, "1:2:".split(":", -1));
50 
51         // Leading empty matches are retained.
52         assertArraysEqual(new String[] { "", "", "o" }, "hello".split(".."));
53 
54         // A separator that doesn't occur in the input gets you the input.
55         assertArraysEqual(new String[] { "hello" }, "hello".split("not-present-in-test"));
56         // ...including when the input is the empty string.
57         // (Perl returns an empty list instead.)
58         assertArraysEqual(new String[] { "" }, "".split("not-present-in-test"));
59         assertArraysEqual(new String[] { "" }, "".split("A?"));
60 
61         // The limit argument controls the size of the result.
62         // If l == 0, the result is as long as needed, except trailing empty matches are dropped.
63         // If l < 0, the result is as long as needed, and trailing empty matches are retained.
64         // If l > 0, the result contains the first l matches, plus one string containing the remaining input.
65         // Examples without a trailing separator (and hence without a trailing empty match):
66         assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", 0));
67         assertArraysEqual(new String[] { "a,b,c" }, "a,b,c".split(",", 1));
68         assertArraysEqual(new String[] { "a", "b,c" }, "a,b,c".split(",", 2));
69         assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", 3));
70         assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c".split(",", Integer.MAX_VALUE));
71         // Examples with a trailing separator (and hence possibly with a trailing empty match):
72         assertArraysEqual(new String[] { "a", "b", "c" }, "a,b,c,".split(",", 0));
73         assertArraysEqual(new String[] { "a,b,c," }, "a,b,c,".split(",", 1));
74         assertArraysEqual(new String[] { "a", "b,c," }, "a,b,c,".split(",", 2));
75         assertArraysEqual(new String[] { "a", "b", "c," }, "a,b,c,".split(",", 3));
76         assertArraysEqual(new String[] { "a", "b", "c", "" }, "a,b,c,".split(",", Integer.MAX_VALUE));
77         assertArraysEqual(new String[] { "a", "b", "c", "" }, "a,b,c,".split(",", -1));
78     }
79 
assertArraysEqual(String[] expected, String[] actual)80     private void assertArraysEqual(String[] expected, String[] actual) {
81         assertEquals(expected.length, actual.length);
82         for (int i = 0; i < expected.length; i++) {
83             assertEquals(Integer.toString(i), expected[i], actual[i]);
84         }
85     }
86 
testSplit1()87     public void testSplit1() throws PatternSyntaxException {
88         Pattern p = Pattern.compile(" ");
89 
90         String input = "poodle zoo";
91         String tokens[];
92 
93         tokens = p.split(input, 1);
94         assertEquals(1, tokens.length);
95         assertTrue(tokens[0].equals(input));
96         tokens = p.split(input, 2);
97         assertEquals(2, tokens.length);
98         assertEquals("poodle", tokens[0]);
99         assertEquals("zoo", tokens[1]);
100         tokens = p.split(input, 5);
101         assertEquals(2, tokens.length);
102         assertEquals("poodle", tokens[0]);
103         assertEquals("zoo", tokens[1]);
104         tokens = p.split(input, -2);
105         assertEquals(2, tokens.length);
106         assertEquals("poodle", tokens[0]);
107         assertEquals("zoo", tokens[1]);
108         tokens = p.split(input, 0);
109         assertEquals(2, tokens.length);
110         assertEquals("poodle", tokens[0]);
111         assertEquals("zoo", tokens[1]);
112         tokens = p.split(input);
113         assertEquals(2, tokens.length);
114         assertEquals("poodle", tokens[0]);
115         assertEquals("zoo", tokens[1]);
116 
117         p = Pattern.compile("d");
118 
119         tokens = p.split(input, 1);
120         assertEquals(1, tokens.length);
121         assertTrue(tokens[0].equals(input));
122         tokens = p.split(input, 2);
123         assertEquals(2, tokens.length);
124         assertEquals("poo", tokens[0]);
125         assertEquals("le zoo", tokens[1]);
126         tokens = p.split(input, 5);
127         assertEquals(2, tokens.length);
128         assertEquals("poo", tokens[0]);
129         assertEquals("le zoo", tokens[1]);
130         tokens = p.split(input, -2);
131         assertEquals(2, tokens.length);
132         assertEquals("poo", tokens[0]);
133         assertEquals("le zoo", tokens[1]);
134         tokens = p.split(input, 0);
135         assertEquals(2, tokens.length);
136         assertEquals("poo", tokens[0]);
137         assertEquals("le zoo", tokens[1]);
138         tokens = p.split(input);
139         assertEquals(2, tokens.length);
140         assertEquals("poo", tokens[0]);
141         assertEquals("le zoo", tokens[1]);
142 
143         p = Pattern.compile("o");
144 
145         tokens = p.split(input, 1);
146         assertEquals(1, tokens.length);
147         assertTrue(tokens[0].equals(input));
148         tokens = p.split(input, 2);
149         assertEquals(2, tokens.length);
150         assertEquals("p", tokens[0]);
151         assertEquals("odle zoo", tokens[1]);
152         tokens = p.split(input, 5);
153         assertEquals(5, tokens.length);
154         assertEquals("p", tokens[0]);
155         assertTrue(tokens[1].equals(""));
156         assertEquals("dle z", tokens[2]);
157         assertTrue(tokens[3].equals(""));
158         assertTrue(tokens[4].equals(""));
159         tokens = p.split(input, -2);
160         assertEquals(5, tokens.length);
161         assertEquals("p", tokens[0]);
162         assertTrue(tokens[1].equals(""));
163         assertEquals("dle z", tokens[2]);
164         assertTrue(tokens[3].equals(""));
165         assertTrue(tokens[4].equals(""));
166         tokens = p.split(input, 0);
167         assertEquals(3, tokens.length);
168         assertEquals("p", tokens[0]);
169         assertTrue(tokens[1].equals(""));
170         assertEquals("dle z", tokens[2]);
171         tokens = p.split(input);
172         assertEquals(3, tokens.length);
173         assertEquals("p", tokens[0]);
174         assertTrue(tokens[1].equals(""));
175         assertEquals("dle z", tokens[2]);
176     }
177 
testSplit2()178     public void testSplit2() {
179         Pattern p = Pattern.compile("");
180         assertEquals(Arrays.asList("a", ""), Arrays.asList(p.split("a", -1)));
181         assertEquals(Arrays.asList(""), Arrays.asList(p.split("", -1)));
182         assertEquals(Arrays.asList("a", "b", "c", "d", ""), Arrays.asList(p.split("abcd", -1)));
183 
184         // Regression test for Android
185         assertEquals("GOOG,23,500".split("|").length, 11);
186     }
187 
188 
testSplitSupplementaryWithEmptyString()189     public void testSplitSupplementaryWithEmptyString() {
190         /*
191          * See http://www.unicode.org/reports/tr18/#Supplementary_Characters
192          * We have to treat text as code points not code units.
193          */
194         Pattern p = Pattern.compile("");
195         String[] s = p.split("a\ud869\uded6b", -1);
196         assertEquals(Arrays.asList("a", "\ud869\uded6", "b", ""), Arrays.asList(s));
197     }
198 
199 }
200