001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.tests.java.util.regex;
019:
020: import java.util.regex.Pattern;
021: import java.util.regex.PatternSyntaxException;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * TODO Type description
027: */
028: @SuppressWarnings("nls")
029: public class SplitTest extends TestCase {
030:
031: public void testSimple() {
032: Pattern p = Pattern.compile("/");
033: String[] results = p.split("have/you/done/it/right");
034: String[] expected = new String[] { "have", "you", "done", "it",
035: "right" };
036: assertEquals(expected.length, results.length);
037: for (int i = 0; i < expected.length; i++) {
038: assertEquals(results[i], expected[i]);
039: }
040: }
041:
042: public void testSplit1() throws PatternSyntaxException {
043: Pattern p = Pattern.compile(" ");
044:
045: String input = "poodle zoo";
046: String tokens[];
047:
048: tokens = p.split(input, 1);
049: assertEquals(1, tokens.length);
050: assertTrue(tokens[0].equals(input));
051: tokens = p.split(input, 2);
052: assertEquals(2, tokens.length);
053: assertEquals("poodle", tokens[0]);
054: assertEquals("zoo", tokens[1]);
055: tokens = p.split(input, 5);
056: assertEquals(2, tokens.length);
057: assertEquals("poodle", tokens[0]);
058: assertEquals("zoo", tokens[1]);
059: tokens = p.split(input, -2);
060: assertEquals(2, tokens.length);
061: assertEquals("poodle", tokens[0]);
062: assertEquals("zoo", tokens[1]);
063: tokens = p.split(input, 0);
064: assertEquals(2, tokens.length);
065: assertEquals("poodle", tokens[0]);
066: assertEquals("zoo", tokens[1]);
067: tokens = p.split(input);
068: assertEquals(2, tokens.length);
069: assertEquals("poodle", tokens[0]);
070: assertEquals("zoo", tokens[1]);
071:
072: p = Pattern.compile("d");
073:
074: tokens = p.split(input, 1);
075: assertEquals(1, tokens.length);
076: assertTrue(tokens[0].equals(input));
077: tokens = p.split(input, 2);
078: assertEquals(2, tokens.length);
079: assertEquals("poo", tokens[0]);
080: assertEquals("le zoo", tokens[1]);
081: tokens = p.split(input, 5);
082: assertEquals(2, tokens.length);
083: assertEquals("poo", tokens[0]);
084: assertEquals("le zoo", tokens[1]);
085: tokens = p.split(input, -2);
086: assertEquals(2, tokens.length);
087: assertEquals("poo", tokens[0]);
088: assertEquals("le zoo", tokens[1]);
089: tokens = p.split(input, 0);
090: assertEquals(2, tokens.length);
091: assertEquals("poo", tokens[0]);
092: assertEquals("le zoo", tokens[1]);
093: tokens = p.split(input);
094: assertEquals(2, tokens.length);
095: assertEquals("poo", tokens[0]);
096: assertEquals("le zoo", tokens[1]);
097:
098: p = Pattern.compile("o");
099:
100: tokens = p.split(input, 1);
101: assertEquals(1, tokens.length);
102: assertTrue(tokens[0].equals(input));
103: tokens = p.split(input, 2);
104: assertEquals(2, tokens.length);
105: assertEquals("p", tokens[0]);
106: assertEquals("odle zoo", tokens[1]);
107: tokens = p.split(input, 5);
108: assertEquals(5, tokens.length);
109: assertEquals("p", tokens[0]);
110: assertTrue(tokens[1].equals(""));
111: assertEquals("dle z", tokens[2]);
112: assertTrue(tokens[3].equals(""));
113: assertTrue(tokens[4].equals(""));
114: tokens = p.split(input, -2);
115: assertEquals(5, tokens.length);
116: assertEquals("p", tokens[0]);
117: assertTrue(tokens[1].equals(""));
118: assertEquals("dle z", tokens[2]);
119: assertTrue(tokens[3].equals(""));
120: assertTrue(tokens[4].equals(""));
121: tokens = p.split(input, 0);
122: assertEquals(3, tokens.length);
123: assertEquals("p", tokens[0]);
124: assertTrue(tokens[1].equals(""));
125: assertEquals("dle z", tokens[2]);
126: tokens = p.split(input);
127: assertEquals(3, tokens.length);
128: assertEquals("p", tokens[0]);
129: assertTrue(tokens[1].equals(""));
130: assertEquals("dle z", tokens[2]);
131: }
132:
133: public void testSplit2() {
134: Pattern p = Pattern.compile("");
135: String s[];
136: s = p.split("a", -1);
137: assertEquals(3, s.length);
138: assertEquals("", s[0]);
139: assertEquals("a", s[1]);
140: assertEquals("", s[2]);
141:
142: s = p.split("", -1);
143: assertEquals(1, s.length);
144: assertEquals("", s[0]);
145:
146: s = p.split("abcd", -1);
147: assertEquals(6, s.length);
148: assertEquals("", s[0]);
149: assertEquals("a", s[1]);
150: assertEquals("b", s[2]);
151: assertEquals("c", s[3]);
152: assertEquals("d", s[4]);
153: assertEquals("", s[5]);
154: }
155:
156: public void testSplitSupplementaryWithEmptyString() {
157:
158: /*
159: * See http://www.unicode.org/reports/tr18/#Supplementary_Characters We
160: * have to treat text as code points not code units.
161: */
162: Pattern p = Pattern.compile("");
163: String s[];
164: s = p.split("a\ud869\uded6b", -1);
165: assertEquals(5, s.length);
166: assertEquals("", s[0]);
167: assertEquals("a", s[1]);
168: assertEquals("\ud869\uded6", s[2]);
169: assertEquals("b", s[3]);
170: assertEquals("", s[4]);
171: }
172: }
|