blob: 33c3215caccdeef65e5a0161c7fde4f833f3594e [file] [log] [blame]
The Android Open Source Projectb5de22c2012-04-01 00:00:00 -07001/*
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
18package libcore.java.lang;
19
20import java.util.Arrays;
21import java.util.IllegalFormatException;
22import java.util.Locale;
23import java.util.regex.PatternSyntaxException;
24
25public class OldStringTest extends junit.framework.TestCase {
26
27 String hw1 = "HelloWorld";
28
29 String hw2 = "HelloWorld";
30
31 String hwlc = "helloworld";
32
33 String hwuc = "HELLOWORLD";
34
35 String comp11 = "Test String";
36
37 char[] buf = { 'W', 'o', 'r', 'l', 'd' };
38
39 public void test_charAtI() {
40 // Test for method char java.lang.String.charAt(int)
41 assertTrue("Incorrect character returned", hw1.charAt(5) == 'W'
42 && (hw1.charAt(1) != 'Z'));
43
44 String testString = "Test String";
45 try {
46 testString.charAt(testString.length());
47 fail("IndexOutOfBoundsException was not thrown.");
48 } catch(IndexOutOfBoundsException iobe) {
49 //expected
50 }
51
52 try {
53 testString.charAt(Integer.MAX_VALUE);
54 fail("IndexOutOfBoundsException was not thrown.");
55 } catch(IndexOutOfBoundsException iobe) {
56 //expected
57 }
58
59 try {
60 testString.charAt(-1);
61 fail("IndexOutOfBoundsException was not thrown.");
62 } catch(IndexOutOfBoundsException iobe) {
63 //expected
64 }
65 }
66
67 public void test_endsWithLjava_lang_String() {
68 assertFalse("Doesn't return false value.", hw1.endsWith("ld "));
69 assertFalse("Doesn't return false value.", hw1.endsWith(" "));
70 assertTrue("Returned incorrect value for empty string.", hw1.endsWith(""));
71 try {
72 hw1.endsWith(null);
73 fail("NullPointerException is not thrown.");
74 } catch(NullPointerException npe) {
75 //expected
76 }
77 }
78
79 public void test_equalsLjava_lang_Object() {
80 assertTrue("String not equal", hw1.equals(hw2) && !(hw1.equals(comp11)));
81 }
82
83 public void test_equalsIgnoreCaseLjava_lang_String() {
84 assertTrue("Returned false for equals strings.", hwlc
85 .equalsIgnoreCase(hwlc));
86
87 assertFalse("Returned true for different strings.", hwlc
88 .equalsIgnoreCase(hwuc + " "));
89 }
90
91 @SuppressWarnings("deprecation")
92 public void test_getBytesII$BI() {
93 try {
94 "Hello World".getBytes(-1, 1, null, 0);
95 fail("Expected IndexOutOfBoundsException");
96 } catch (IndexOutOfBoundsException expected) {
97 } catch (NullPointerException expected) {
98 }
99
100 try {
101 "Hello World".getBytes(6, 2, null, 0);
102 fail("IndexOutOfBoundsException was not thrown.");
103 } catch (IndexOutOfBoundsException expected) {
104 } catch (NullPointerException expected) {
105 }
106
107 try {
108 "Hello World".getBytes(2, 10, new byte[10], 4);
109 fail("IndexOutOfBoundsException was not thrown.");
110 } catch (IndexOutOfBoundsException expected) {
111 }
112 }
113
114 public void test_getCharsII$CI() {
115 try {
116 "Hello World".getChars(-1, 1, null, 0);
117 fail("Expected IndexOutOfBoundsException");
118 } catch (IndexOutOfBoundsException expected) {
119 } catch (NullPointerException expected) {
120 }
121
122 try {
123 "Hello World".getChars(6, 2, null, 0);
124 fail("IndexOutOfBoundsException was not thrown.");
125 } catch (IndexOutOfBoundsException expected) {
126 } catch (NullPointerException expected) {
127 }
128
129 try {
130 "Hello World".getChars(2, 10, new char[10], 4);
131 fail("IndexOutOfBoundsException was not thrown.");
132 } catch (IndexOutOfBoundsException expected) {
133 }
134 }
135
136 public void test_regionMatchesILjava_lang_StringII() {
137 assertFalse("Returned true for negative offset.", hw1.regionMatches(-1,
138 hw2, 2, 5));
139 assertFalse("Returned true for negative offset.", hw1.regionMatches(2,
140 hw2, -1, 5));
141 assertFalse("Returned true for toffset+len is greater than the length.",
142 hw1.regionMatches(5, hw2, 2, 6));
143 assertFalse("Returned true for ooffset+len is greater than the length.",
144 hw1.regionMatches(2, hw2, 5, 6));
145 }
146
147 public void test_regionMatchesZILjava_lang_StringII() {
148 String bogusString = "xxcedkedkleiorem lvvwr e''' 3r3r 23r";
149
150 assertFalse("Returned true for negative offset.", hw1.regionMatches(true,
151 -1, hw2, 2, 5));
152 assertFalse("Returned true for negative offset.", hw1.regionMatches(false,
153 2, hw2, -1, 5));
154 assertFalse("Returned true for toffset+len is greater than the length.",
155 hw1.regionMatches(true, 5, hw2, 2, 6));
156 assertFalse("Returned true for ooffset+len is greater than the length.",
157 hw1.regionMatches(false, 2, hw2, 5, 6));
158
159 assertTrue("identical regions failed comparison", hwuc.regionMatches(
160 true, 0, hwlc, 0, hwuc.length()));
161 assertFalse("non identical regions failed comparison", hwuc.regionMatches(
162 false, 0, hwlc, 0, hwuc.length()));
163 }
164
165 public void test_replaceCC() {
166 assertEquals("Returned incorrect string.", hw1, hw1.replace("!", "."));
167 }
168
169 public void test_replaceAll() {
170 String str = "!'123123.123HelloWorld!123123helloworld#";
171 String [] patterns = {"[hw\\p{Upper}]", "(o|l){2,}", "([\'\"]?)(\\d+)",
172 "^!.*#$"};
173
174 String [] results = {"!\'123123.123?ello?orld!123123?ello?orld#",
175 "!\'123123.123He?World!123123he?world#",
176 "!?.?HelloWorld!?helloworld#", "?"};
177
178 for(int i = 0; i < patterns.length; i++) {
179 assertEquals("Returned incorrect string",
180 results[i], str.replaceAll(patterns[i], "?"));
181 }
182
183 try {
184 str.replaceAll("[abc*", "?");
185 fail("PatternSyntaxException is not thrown.");
186 } catch(PatternSyntaxException pse) {
187 //expected
188 }
189 }
190
191 public void test_replaceFirst() {
192 String str = "!'123123.123HelloWorld!123123helloworld#";
193 String [] patterns = {"[hw\\p{Upper}]", "(o|l){2,}", "([\'\"]?)(\\d+)",
194 "^!.*#$"};
195
196 String [] results = {"!'123123.123?elloWorld!123123helloworld#",
197 "!'123123.123He?World!123123helloworld#",
198 "!?.123HelloWorld!123123helloworld#", "?"};
199
200 for(int i = 0; i < patterns.length; i++) {
201 assertEquals("Returned incorrect string",
202 results[i], str.replaceFirst(patterns[i], "?"));
203 }
204
205 try {
206 str.replaceFirst("[abc*", "?");
207 fail("PatternSyntaxException is not thrown.");
208 } catch(PatternSyntaxException pse) {
209 //expected
210 }
211 }
212
213 public void test_splitLString() {
214 String str = "!'123123.123HelloWorld!123123helloworld#";
215 String [] patterns = {"[!.1]", "(\\d+).*e(l+)o.*orld"};
216 String [][] results = {{"", "'","23", "23", "", "23HelloWorld", "", "23",
217 "23helloworld#"},
218 {"!'", "#"}};
219
220 for(int i = 0; i < patterns.length; i++) {
221 assertTrue("Returned incorrect string array for pattern: " +
222 patterns[i], Arrays.equals(results[i], str.split(patterns[i])));
223 }
224
225 try {
226 str.split("[a}");
227 fail("PatternSyntaxException is not thrown.");
228 } catch(PatternSyntaxException pse) {
229 //expected
230 }
231 }
232
233 public void test_splitLStringLint() {
234 String str = "!'123123.123HelloWorld!123123helloworld#";
235 String pattern = "[!.1]";
236 String [][] results = {{"", "'","23", "23.123HelloWorld!123123helloworld#"},
237 {"", "'","23", "23", "", "23HelloWorld", "", "23",
238 "23helloworld#"}};
239
240 assertTrue("Returned incorrect string array for limit 4",
241 Arrays.equals(results[0], str.split(pattern, 4)));
242 assertTrue("Returned incorrect string array for limit 9",
243 Arrays.equals(results[1], str.split(pattern, 9)));
244 assertTrue("Returned incorrect string array for limit 0",
245 Arrays.equals(results[1], str.split(pattern, 0)));
246 assertTrue("Returned incorrect string array for limit -1",
247 Arrays.equals(results[1], str.split(pattern, -1)));
248 assertTrue("Returned incorrect string array for limit 10",
249 Arrays.equals(results[1], str.split(pattern, 10)));
250 assertTrue("Returned incorrect string array for limit Integer.MAX_VALUE",
251 Arrays.equals(results[1], str.split(pattern, Integer.MAX_VALUE)));
252
253 try {
254 str.split("[a}", 0);
255 fail("PatternSyntaxException is not thrown.");
256 } catch(PatternSyntaxException pse) {
257 //expected
258 }
259 }
260
261 public void test_replaceLjava_langCharSequenceLjava_langCharSequence() {
262 assertEquals("Failed replace", "aaccdd", "aabbdd".replace(
263 new StringBuffer("bb"), "cc"));
264 assertEquals("Failed replace by bigger seq", "cccbccc", "aba".replace(
265 "a", "ccc"));
266 assertEquals("Failed replace by smaller seq", "$bba^",
267 "$aaaaa^".replace(new StringBuilder("aa"), "b"));
268
269 try {
270 "".replace((CharSequence) null, "123".subSequence(0, 1));
271 fail("NullPointerException is not thrown.");
272 } catch(NullPointerException npe) {
273 //expected
274 }
275
276 try {
277 "".replace("123".subSequence(0, 1), (CharSequence) null);
278 fail("NullPointerException is not thrown.");
279 } catch(NullPointerException npe) {
280 //expected
281 }
282 }
283
284 public void test_substringI() {
285 try {
286 hw1.substring(-1);
287 fail("IndexOutOfBoundsException was not thrown.");
288 } catch(IndexOutOfBoundsException ioobe) {
289 //expected
290 }
291
292 try {
293 hw1.substring(hw1.length() + 1);
294 fail("IndexOutOfBoundsException was not thrown.");
295 } catch(IndexOutOfBoundsException ioobe) {
296 //expected
297 }
298
299 try {
300 hw1.substring(Integer.MAX_VALUE);
301 fail("IndexOutOfBoundsException was not thrown.");
302 } catch(IndexOutOfBoundsException ioobe) {
303 //expected
304 }
305 }
306
307 public void test_substringII() {
308 try {
309 hw1.substring(-1, hw1.length());
310 fail("IndexOutOfBoundsException was not thrown.");
311 } catch(IndexOutOfBoundsException ioobe) {
312 //expected
313 }
314
315 try {
316 hw1.substring(Integer.MAX_VALUE, hw1.length());
317 fail("IndexOutOfBoundsException was not thrown.");
318 } catch(IndexOutOfBoundsException ioobe) {
319 //expected
320 }
321
322 try {
323 hw1.substring(0, Integer.MAX_VALUE);
324 fail("IndexOutOfBoundsException was not thrown.");
325 } catch(IndexOutOfBoundsException ioobe) {
326 //expected
327 }
328 }
329
330 public void test_subSequence() {
331 // Test for method java.lang.String java.lang.String.substring(int, int)
332 assertTrue("Incorrect substring returned", hw1.subSequence(0, 5).equals(
333 "Hello") && (hw1.subSequence(5, 10).equals("World")));
334 assertTrue("not identical", hw1.subSequence(0, hw1.length()) == hw1);
335
336 try {
337 hw1.subSequence(0, Integer.MAX_VALUE);
338 fail("IndexOutOfBoundsException was not thrown.");
339 } catch(IndexOutOfBoundsException ioobe) {
340 //expected
341 }
342
343 try {
344 hw1.subSequence(Integer.MAX_VALUE, hw1.length());
345 fail("IndexOutOfBoundsException was not thrown.");
346 } catch(IndexOutOfBoundsException ioobe) {
347 //expected
348 }
349
350 try {
351 hw1.subSequence(-1, hw1.length());
352 fail("IndexOutOfBoundsException was not thrown.");
353 } catch(IndexOutOfBoundsException ioobe) {
354 //expected
355 }
356 }
357
358 public void test_trim() {
359 assertEquals("Incorrect string returned", hw1, " HelloWorld ".trim());
360 assertTrue("Incorrect string returned", " ".trim().equals(""));
361 }
362
363 public void test_valueOf$C() {
364 assertEquals("Returned incorrect String",
365 "World", String.valueOf(buf));
366 assertEquals("Returned incorrect String",
367 "", String.valueOf(new char[]{}));
368 try {
369 String.valueOf(null);
370 fail("NullPointerException was not thrown.");
371 } catch(NullPointerException npe) {
372 //expected
373 }
374 }
375
376 public void test_valueOf$CII() {
377 char[] t = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
378
379 try {
380 String.valueOf(t, 0, t.length + 1);
381 fail("IndexOutOfBoundsException was not thrown.");
382 } catch(IndexOutOfBoundsException ioobe) {
383 //expected
384 }
385
386 try {
387 String.valueOf(t, 0, -1);
388 fail("IndexOutOfBoundsException was not thrown.");
389 } catch(IndexOutOfBoundsException ioobe) {
390 //expected
391 }
392
393 try {
394 String.valueOf(t, 0, Integer.MAX_VALUE);
395 fail("IndexOutOfBoundsException was not thrown.");
396 } catch(IndexOutOfBoundsException ioobe) {
397 //expected
398 }
399 }
400
401 public void test_valueOfLjava_lang_Object() {
402 assertEquals("Incorrect value was returned for null.",
403 "null", String.valueOf((Object) null));
404 }
405
406 @SuppressWarnings("boxing")
407 public void test_format() {
408 assertEquals("3 2 1 4 3 2 1", String.format(
409 "%3$d %2$d %1$d %4$d %3$d %2$d %1$d", 1, 2, 3, 4));
410
411 assertEquals("empty format", "", String.format("", 123, this));
412 try {
413 String.format(null);
414 fail("NPE is expected on null format");
415 } catch (NullPointerException ok){}
416
417 try {
418 String.format("%d%% of %s is 0x%x", "123");
419 fail("IllegalFormatException was not thrown.");
420 } catch(IllegalFormatException ife) {
421 //expected
422 }
423
424 try {
425 String.format("%tu", "123");
426 fail("IllegalFormatException was not thrown.");
427 } catch(IllegalFormatException ife) {
428 //expected
429 }
430
431 }
432
433 @SuppressWarnings("boxing")
434 public void test_format_Locale() {
435 Locale l = new Locale("UK");
436 assertEquals("13% of sum is 0x11",
437 String.format(l, "%d%% of %s is 0x%x", 13, "sum", 17));
438 assertEquals("empty format", "", String.format("", 123, this));
439 try {
440 String.format(l, null, "");
441 fail("NPE is expected on null format");
442 } catch (NullPointerException ok){}
443
444 try {
445 String.format(l, "%d", "test");
446 fail("IllegalFormatException wasn't thrown.");
447 } catch(IllegalFormatException ife) {
448 //expected
449 }
450 }
451
452 public void test_matches() {
453 String[] patterns = {
454 "(a|b)*abb",
455 "(1*2*3*4*)*567",
456 "(a|b|c|d)*aab",
457 "(1|2|3|4|5|6|7|8|9|0)(1|2|3|4|5|6|7|8|9|0)*",
458 "(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)*",
459 "(a|b)*(a|b)*A(a|b)*lice.*",
460 "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)(a|b|c|d|e|f|g|h|"
461 + "i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)*(1|2|3|4|5|6|7|8|9|0)*|while|for|struct|if|do",
462
463 };
464
465 String[] patternsInc = {
466 "(ac)*bb",
467 "(1)*567",
468 "(c)*ab",
469 "(|8|9|0)(1|2|7|8|9|0)*",
470 "(z)",
471 "(a)*A(b)*lice.",
472 "(a|b|c|d|e)",
473
474 };
475
476 String[][] strings = {
477 { "abb", "ababb", "abababbababb", "abababbababbabababbbbbabb" },
478 { "213567", "12324567", "1234567", "213213567",
479 "21312312312567", "444444567" },
480 { "abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab" },
481 { "213234567", "3458", "0987654", "7689546432", "0398576",
482 "98432", "5" },
483 {
484 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
485 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
486 + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" },
487 { "ababbaAabababblice", "ababbaAliceababab", "ababbAabliceaaa",
488 "abbbAbbbliceaaa", "Alice" },
489 { "a123", "bnxnvgds156", "for", "while", "if", "struct" },
490 { "xy" }, { "xy" }, { "xcy" }
491
492 };
493
494 for (int i = 0; i < patterns.length; i++) {
495 for (int j = 0; j < strings[i].length; j++) {
496 assertTrue("Incorrect match: " + patterns[i] + " vs "
497 + strings[i][j], strings[i][j].matches(patterns[i]));
498 assertFalse("" + i, strings[i][j].matches(patternsInc[i]));
499 }
500 }
501 }
502
503 public void test_indexOfI() {
504 assertEquals("Doesn't return -1 if there is no such character.", -1,
505 hw1.indexOf('q'));
506 }
507
508 public void test_indexOfII() {
509 assertEquals("Doesn't return -1 if there is no such character.", -1,
510 hw1.indexOf('H', 2));
511
512 }
513
514 public void test_indexOfLjava_lang_String() {
515 assertEquals("Doesn't return -1 for unknown string.",
516 -1, hw1.indexOf("Heo"));
517 }
518}