2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.dev.test.util;
\r
10 import com.ibm.icu.dev.test.TestFmwk;
\r
11 import com.ibm.icu.impl.Utility;
\r
12 import com.ibm.icu.text.ReplaceableString;
\r
13 import com.ibm.icu.text.UnicodeSet;
\r
14 import com.ibm.icu.util.StringTokenizer;
\r
17 * Testing class for StringTokenizer class
\r
18 * @author Syn Wee Quek
\r
19 * @since oct 26 2002
\r
21 public final class StringTokenizerTest extends TestFmwk
\r
23 // constructor ===================================================
\r
28 public StringTokenizerTest()
\r
32 // public methods --------------------------------------------------------
\r
35 * Testing constructors
\r
37 public void TestConstructors()
\r
39 String str = "this\tis\na\rstring\ftesting\tStringTokenizer\nconstructors!";
\r
40 String delimiter = " \t\n\r\f";
\r
41 String expected[] = {"this", "is", "a", "string", "testing",
\r
42 "StringTokenizer", "constructors!"};
\r
43 StringTokenizer defaultst = new StringTokenizer(str);
\r
44 StringTokenizer stdelimiter = new StringTokenizer(str, delimiter);
\r
45 StringTokenizer stdelimiterreturn = new StringTokenizer(str, delimiter,
\r
47 UnicodeSet delimiterset = new UnicodeSet("[" + delimiter + "]", false);
\r
48 StringTokenizer stdelimiterset = new StringTokenizer(str, delimiterset);
\r
49 StringTokenizer stdelimitersetreturn = new StringTokenizer(str,
\r
52 for (int i = 0; i < expected.length; i ++) {
\r
53 if (!(defaultst.nextElement().equals(expected[i])
\r
54 && stdelimiter.nextElement().equals(expected[i])
\r
55 && stdelimiterreturn.nextElement().equals(expected[i])
\r
56 && stdelimiterset.nextElement().equals(expected[i])
\r
57 && stdelimitersetreturn.nextElement().equals(expected[i]))) {
\r
58 errln("Constructor with default delimiter gives wrong results");
\r
62 UnicodeSet delimiterset1 = new UnicodeSet("[" + delimiter + "]", true);
\r
63 StringTokenizer stdelimiterset1 = new StringTokenizer(str, delimiterset1);
\r
64 if(!(stdelimiterset1.nextElement().equals(str)))
\r
65 errln("Constructor with a UnicodeSet to ignoreWhiteSpace is " +
\r
66 "to return the same string.");
\r
68 String expected1[] = {"this", "\t", "is", "\n", "a", "\r", "string", "\f",
\r
69 "testing", "\t", "StringTokenizer", "\n",
\r
71 stdelimiterreturn = new StringTokenizer(str, delimiter, true);
\r
72 stdelimitersetreturn = new StringTokenizer(str, delimiterset, true);
\r
73 for (int i = 0; i < expected1.length; i ++) {
\r
74 if (!(stdelimiterreturn.nextElement().equals(expected1[i])
\r
75 && stdelimitersetreturn.nextElement().equals(expected1[i]))) {
\r
76 errln("Constructor with default delimiter and delimiter tokens gives wrong results");
\r
80 stdelimiter = new StringTokenizer(str, (String)null);
\r
81 stdelimiterreturn = new StringTokenizer(str, (String)null, false);
\r
82 delimiterset = null;
\r
83 stdelimiterset = new StringTokenizer(str, delimiterset);
\r
84 stdelimitersetreturn = new StringTokenizer(str, delimiterset, false);
\r
86 if (!(stdelimiter.nextElement().equals(str)
\r
87 && stdelimiterreturn.nextElement().equals(str)
\r
88 && stdelimiterset.nextElement().equals(str)
\r
89 && stdelimitersetreturn.nextElement().equals(str))) {
\r
90 errln("Constructor with null delimiter gives wrong results");
\r
94 stdelimiter = new StringTokenizer(str, delimiter);
\r
95 stdelimiterreturn = new StringTokenizer(str, delimiter, false);
\r
96 delimiterset = new UnicodeSet();
\r
97 stdelimiterset = new StringTokenizer(str, delimiterset);
\r
98 stdelimitersetreturn = new StringTokenizer(str, delimiterset, false);
\r
100 if (!(stdelimiter.nextElement().equals(str)
\r
101 && stdelimiterreturn.nextElement().equals(str)
\r
102 && stdelimiterset.nextElement().equals(str)
\r
103 && stdelimitersetreturn.nextElement().equals(str))) {
\r
104 errln("Constructor with empty delimiter gives wrong results");
\r
108 defaultst = new StringTokenizer(null);
\r
109 errln("null string should throw an exception");
\r
110 } catch (Exception e) {
\r
111 logln("PASS: Constructor with null string failed as expected");
\r
114 stdelimiter = new StringTokenizer(null, delimiter);
\r
115 errln("null string should throw an exception");
\r
116 } catch (Exception e) {
\r
117 logln("PASS: Constructor with null string failed as expected");
\r
120 stdelimiterreturn = new StringTokenizer(null, delimiter, false);
\r
121 errln("null string should throw an exception");
\r
122 } catch (Exception e) {
\r
123 logln("PASS: Constructor with null string failed as expected");
\r
126 stdelimiterset = new StringTokenizer(null, delimiterset);
\r
127 errln("null string should throw an exception");
\r
128 } catch (Exception e) {
\r
129 logln("PASS: Constructor with null string failed as expected");
\r
132 stdelimitersetreturn = new StringTokenizer(null, delimiterset,
\r
134 errln("null string should throw an exception");
\r
135 } catch (Exception e) {
\r
136 logln("PASS: Constructor with null string failed as expected");
\r
141 * Testing supplementary
\r
143 public void TestSupplementary()
\r
145 String str = "bmp string \ud800 with a unmatched surrogate character";
\r
146 String delimiter = "\ud800\udc00";
\r
147 String expected[] = {str};
\r
149 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
\r
150 if (!tokenizer.nextElement().equals(expected[0])) {
\r
151 errln("Error parsing \"" + Utility.hex(str) + "\"");
\r
153 if (tokenizer.hasMoreElements()) {
\r
154 errln("Number of tokens exceeded expected");
\r
156 delimiter = "\ud800";
\r
157 String expected1[] = {"bmp string ",
\r
158 " with a unmatched surrogate character"};
\r
159 tokenizer = new StringTokenizer(str, delimiter);
\r
161 while (tokenizer.hasMoreElements()) {
\r
162 if (!tokenizer.nextElement().equals(expected1[i ++])) {
\r
163 errln("Error parsing \"" + Utility.hex(str) + "\"");
\r
166 if (tokenizer.hasMoreElements()) {
\r
167 errln("Number of tokens exceeded expected");
\r
170 str = "string \ud800\udc00 with supplementary character";
\r
171 delimiter = "\ud800";
\r
172 String expected2[] = {str};
\r
173 tokenizer = new StringTokenizer(str, delimiter);
\r
174 if (!tokenizer.nextElement().equals(expected2[0])) {
\r
175 errln("Error parsing \"" + Utility.hex(str) + "\"");
\r
177 if (tokenizer.hasMoreElements()) {
\r
178 errln("Number of tokens exceeded expected");
\r
181 delimiter = "\ud800\udc00";
\r
182 String expected3[] = {"string ", " with supplementary character"};
\r
183 tokenizer = new StringTokenizer(str, delimiter);
\r
185 while (tokenizer.hasMoreElements()) {
\r
186 if (!tokenizer.nextElement().equals(expected3[i ++])) {
\r
187 errln("Error parsing \"" + Utility.hex(str) + "\"");
\r
190 if (tokenizer.hasMoreElements()) {
\r
191 errln("Number of tokens exceeded expected");
\r
194 str = "\ud800 \ud800\udc00 \ud800 \ud800\udc00";
\r
195 delimiter = "\ud800";
\r
196 String expected4[] = {" \ud800\udc00 ", " \ud800\udc00"};
\r
198 while (tokenizer.hasMoreElements()) {
\r
199 if (!tokenizer.nextElement().equals(expected4[i ++])) {
\r
200 errln("Error parsing \"" + Utility.hex(str) + "\"");
\r
203 if (tokenizer.hasMoreElements()) {
\r
204 errln("Number of tokens exceeded expected");
\r
207 delimiter = "\ud800\udc00";
\r
208 String expected5[] = {"\ud800 ", " \ud800 "};
\r
210 while (tokenizer.hasMoreElements()) {
\r
211 if (!tokenizer.nextElement().equals(expected5[i ++])) {
\r
212 errln("Error parsing \"" + Utility.hex(str) + "\"");
\r
215 if (tokenizer.hasMoreElements()) {
\r
216 errln("Number of tokens exceeded expected");
\r
223 public void TestNextNonDelimiterToken()
\r
225 String str = " , 1 2 3 AHHHHH! 5.5 6 7 , 8\n";
\r
226 String expected[] = {",", "1", "2", "3", "AHHHHH!", "5.5", "6", "7",
\r
228 String delimiter = " ";
\r
230 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
\r
232 while (tokenizer.hasMoreElements()) {
\r
233 if (!tokenizer.nextElement().equals(expected[currtoken])) {
\r
234 errln("Error token mismatch, expected " + expected[currtoken]);
\r
239 if (currtoken != expected.length) {
\r
240 errln("Didn't get correct number of tokens");
\r
243 tokenizer = new StringTokenizer("", delimiter);
\r
244 if (tokenizer.hasMoreElements()) {
\r
245 errln("Empty string should not have any tokens");
\r
248 tokenizer.nextElement();
\r
249 errln("Empty string should not have any tokens");
\r
250 } catch (Exception e) {
\r
251 logln("PASS: empty string failed as expected");
\r
254 tokenizer = new StringTokenizer(", ,", ", ");
\r
255 if (tokenizer.hasMoreElements()) {
\r
256 errln("String with only delimiters should not have any tokens");
\r
259 tokenizer.nextElement();
\r
260 errln("String with only delimiters should not have any tokens");
\r
261 } catch (Exception e) {
\r
262 logln("PASS: String with only delimiters failed as expected");
\r
265 tokenizer = new StringTokenizer("q, ,", ", ");
\r
266 if (!tokenizer.hasMoreElements()) {
\r
267 errln("String that does not begin with delimiters should have some tokens");
\r
269 if (!tokenizer.nextElement().equals("q")) {
\r
270 errln("String that does not begin with delimiters should have some tokens");
\r
273 tokenizer.nextElement();
\r
274 errln("String has only one token");
\r
275 } catch (Exception e) {
\r
276 logln("PASS: String with only one token failed as expected");
\r
280 tokenizer = new StringTokenizer(null, delimiter);
\r
281 errln("StringTokenizer constructed with null source should throw a nullpointerexception");
\r
282 } catch (Exception e) {
\r
283 logln("PASS: StringTokenizer constructed with null source failed as expected");
\r
286 tokenizer = new StringTokenizer(str, "q");
\r
287 if (!tokenizer.nextElement().equals(str)) {
\r
288 errln("Should have received the same string when there are no delimiters");
\r
293 * Test java compatibility, except we support surrogates.
\r
295 public void TestNoCoalesce() {
\r
296 String str = "This is a test\rto see if\nwhitespace is handled \n\r unusually\r\n by our tokenizer\n\n\n!!!plus some other odd ones like \ttab\ttab\ttab\nand form\ffeed\ffoo.\n";
\r
297 String delims = " \t\n\r\f\ud800\udc00";
\r
299 java.util.StringTokenizer jt = new java.util.StringTokenizer(str, delims, true);
\r
300 com.ibm.icu.util.StringTokenizer it = new com.ibm.icu.util.StringTokenizer(str, delims, true);
\r
302 while (jt.hasMoreTokens() && it.hasMoreTokens()) {
\r
303 assertEquals("[" + String.valueOf(n++) + "]", jt.nextToken(), it.nextToken());
\r
305 assertFalse("java tokenizer has no more tokens", jt.hasMoreTokens());
\r
306 assertFalse("icu tokenizer has no more tokens", it.hasMoreTokens());
\r
308 String sur = "Even\ud800\udc00 works.\n\n";
\r
309 it = new com.ibm.icu.util.StringTokenizer(sur, delims, true); // no coalesce
\r
310 assertEquals("sur1", it.nextToken(), "Even");
\r
311 assertEquals("sur2", it.nextToken(), "\ud800\udc00");
\r
312 assertEquals("sur3", it.nextToken(), " ");
\r
313 assertEquals("sur4", it.nextToken(), "works.");
\r
314 assertEquals("sur5", it.nextToken(), "\n");
\r
315 assertEquals("sur6", it.nextToken(), "\n");
\r
316 assertFalse("sur7", it.hasMoreTokens());
\r
322 public void TestNextDelimiterToken()
\r
324 String str = " , 1 2 3 AHHHHH! 5.5 6 7 , 8\n";
\r
325 String expected[] = {" ", ",", " ", "1", " ", "2", " ", "3", " ",
\r
326 "AHHHHH!", " ", "5.5", " ", "6", " ", "7", " ",
\r
328 String delimiter = " ";
\r
330 StringTokenizer tokenizer = new StringTokenizer(str, delimiter, true, true);
\r
333 while (tokenizer.hasMoreElements()) {
\r
334 if (!tokenizer.nextElement().equals(expected[currtoken])) {
\r
335 errln("Error token mismatch, expected " + expected[currtoken]);
\r
340 if (currtoken != expected.length) {
\r
341 errln("Didn't get correct number of tokens");
\r
344 tokenizer = new StringTokenizer("", delimiter, true);
\r
345 if (tokenizer.hasMoreElements()) {
\r
346 errln("Empty string should not have any tokens");
\r
349 tokenizer.nextElement();
\r
350 errln("Empty string should not have any tokens");
\r
351 } catch (Exception e) {
\r
352 logln("PASS: Empty string failed as expected");
\r
355 tokenizer = new StringTokenizer(", ,", ", ", true, true);
\r
356 if (!tokenizer.hasMoreElements()) {
\r
357 errln("String with only delimiters should have tokens when delimiter is treated as tokens");
\r
359 if (!tokenizer.nextElement().equals(", ,")) {
\r
360 errln("String with only delimiters should return itself when delimiter is treated as tokens");
\r
363 tokenizer = new StringTokenizer("q, ,", ", ", true, true);
\r
365 if (!tokenizer.hasMoreElements()) {
\r
366 errln("String should have some tokens");
\r
368 if (!tokenizer.nextElement().equals("q")
\r
369 || !tokenizer.nextElement().equals(", ,")) {
\r
370 errln("String tokens do not match expected results");
\r
374 tokenizer = new StringTokenizer(null, delimiter, true);
\r
375 errln("StringTokenizer constructed with null source should throw a nullpointerexception");
\r
376 } catch (Exception e) {
\r
377 logln("PASS: StringTokenizer constructed with null source failed as expected");
\r
380 tokenizer = new StringTokenizer(str, "q", true);
\r
381 if (!tokenizer.nextElement().equals(str)) {
\r
382 errln("Should have recieved the same string when there are no delimiters");
\r
387 * Testing count tokens
\r
389 public void TestCountTokens()
\r
391 String str = "this\tis\na\rstring\ftesting\tStringTokenizer\nconstructors!";
\r
392 String delimiter = " \t\n\r\f";
\r
393 String expected[] = {"this", "is", "a", "string", "testing",
\r
394 "StringTokenizer", "constructors!"};
\r
395 String expectedreturn[] = {"this", "\t", "is", "\n", "a", "\r",
\r
396 "string", "\f", "testing", "\t",
\r
397 "StringTokenizer", "\n", "constructors!"};
\r
398 StringTokenizer st = new StringTokenizer(str, delimiter);
\r
399 StringTokenizer streturn = new StringTokenizer(str, delimiter, true);
\r
400 if (st.countTokens() != expected.length) {
\r
401 errln("CountTokens failed for non-delimiter tokens");
\r
403 if (streturn.countTokens() != expectedreturn.length) {
\r
404 errln("CountTokens failed for delimiter tokens");
\r
406 for (int i = 0; i < expected.length; i ++) {
\r
407 if (!st.nextElement().equals(expected[i])
\r
408 || st.countTokens() != expected.length - i - 1) {
\r
409 errln("CountTokens default delimiter gives wrong results");
\r
412 for (int i = 0; i < expectedreturn.length; i ++) {
\r
413 if (!streturn.nextElement().equals(expectedreturn[i])
\r
414 || streturn.countTokens() != expectedreturn.length - i - 1) {
\r
415 errln("CountTokens with default delimiter and delimiter tokens gives wrong results");
\r
421 * Next token with new delimiters
\r
423 public void TestNextNewDelimiters()
\r
425 String str = "abc0def1ghi2jkl3mno4pqr0stu1vwx2yza3bcd4efg0hij1klm2nop3qrs4tuv";
\r
426 String delimiter[] = {"0", "1", "2", "3", "4"};
\r
427 String expected[][] = {{"abc", "pqr", "efg"},
\r
428 {"def", "stu", "hij"},
\r
429 {"ghi", "vwx", "klm"},
\r
430 {"jkl", "yza", "nop"},
\r
431 {"mno", "bcd", "qrs"}
\r
433 StringTokenizer st = new StringTokenizer(str);
\r
434 int size = expected[0].length;
\r
435 for (int i = 0; i < size; i ++) {
\r
436 for (int j = 0; j < delimiter.length; j ++) {
\r
437 if (!st.nextToken(delimiter[j]).equals(expected[j][i])) {
\r
438 errln("nextToken() with delimiters error " + i + " " + j);
\r
440 if (st.countTokens() != expected[j].length - i) {
\r
441 errln("countTokens() after nextToken() with delimiters error"
\r
446 st = new StringTokenizer(str);
\r
447 String delimiter1[] = {"0", "2", "4"};
\r
448 String expected1[] = {"abc", "def1ghi", "jkl3mno", "pqr", "stu1vwx",
\r
449 "yza3bcd", "efg", "hij1klm", "nop3qrs", "tuv"};
\r
450 for (int i = 0; i < expected1.length; i ++) {
\r
451 if (!st.nextToken(delimiter1[i % delimiter1.length]).equals(
\r
453 errln("nextToken() with delimiters error " + i);
\r
458 public void TestBug4423()
\r
460 // bug 4423: a bad interaction between countTokens() and hasMoreTokens().
\r
462 String s1 = "This is a test";
\r
463 StringTokenizer tzr = new StringTokenizer(s1);
\r
464 int tokenCount = 0;
\r
466 int t = tzr.countTokens();
\r
468 errln("tzr.countTokens() returned " + t + ". Expected 4");
\r
470 while (tzr.hasMoreTokens()) {
\r
471 String tok = tzr.nextToken();
\r
472 if (tok.length() == 0) {
\r
473 errln("token with length == 0");
\r
477 if (tokenCount != 4) {
\r
478 errln("Incorrect number of tokens found = " + tokenCount);
\r
481 // Precomputed tokens arrays can grow. Check for edge cases around
\r
482 // boundary where growth is forced. Array grows in increments of 100 tokens.
\r
484 for (int i=1; i<250; i++) {
\r
486 StringTokenizer tzb = new StringTokenizer(s2);
\r
487 int t2 = tzb.countTokens();
\r
489 errln("tzb.countTokens() returned " + t + ". Expected " + i);
\r
493 while (tzb.hasMoreTokens()) {
\r
494 String tok = tzb.nextToken();
\r
496 if (tok.equals(Integer.toString(j)) == false) {
\r
497 errln("Wrong token string. Expected \"" + j + "\", got \""
\r
503 errln("Wrong number of tokens. Expected " + i + ". Got " + j
\r
511 public void TestCountTokensNoCoalesce() {
\r
513 String str = "\"\"";
\r
515 StringTokenizer st = new StringTokenizer(str, del, true);
\r
517 while (st.hasMoreTokens()) {
\r
518 String t = st.nextToken();
\r
519 logln("[" + count + "] '" + t + "'");
\r
522 st = new StringTokenizer(str, del, true);
\r
523 int ncount = st.countTokens();
\r
525 while (st.hasMoreTokens()) {
\r
526 String t = st.nextToken();
\r
527 logln("[" + xcount + "] '" + t + "'");
\r
530 if (count != ncount || count != xcount) {
\r
531 errln("inconsistent counts " + count + ", " + ncount + ", " + xcount);
\r
535 public static void main(String[] arg)
\r
539 StringTokenizerTest test = new StringTokenizerTest();
\r
541 // test.TestCaseCompare();
\r
543 catch (Exception e)
\r
545 e.printStackTrace();
\r
549 /* Tests the method
\r
550 * public StringBuffer _generatePattern(StringBuffer result, boolean escapeUnprintable)
\r
552 public void Test_GeneratePattern(){
\r
553 UnicodeSet us = new UnicodeSet();
\r
554 StringBuffer sb = new StringBuffer();
\r
556 us._generatePattern(sb, true);
\r
557 us._generatePattern(sb, false);
\r
558 us._generatePattern(sb.append(1), true);
\r
559 us._generatePattern(sb.append(1.0), true);
\r
560 us._generatePattern(sb.reverse(), true);
\r
561 } catch(Exception e){
\r
562 errln("UnicodeSet._generatePattern is not suppose to return an exception.");
\r
566 us._generatePattern(null, true);
\r
567 errln("UnicodeSet._generatePattern is suppose to return an exception.");
\r
568 } catch(Exception e){}
\r
571 /* Tests the method
\r
572 * public int matches(Replaceable text, int[] offset, int limit, boolean incremental)
\r
574 public void TestMatches(){
\r
575 // Tests when "return incremental ? U_PARTIAL_MATCH : U_MATCH;" is true and false
\r
576 ReplaceableString rs = new ReplaceableString("dummy");
\r
577 UnicodeSet us = new UnicodeSet(0,100000); // Create a large Unicode set
\r
580 int[] offset = {0};
\r
583 if(us.matches(null, offset, limit, true) != UnicodeSet.U_PARTIAL_MATCH){
\r
584 errln("UnicodeSet.matches is suppose to return " + UnicodeSet.U_PARTIAL_MATCH +
\r
585 " but got " + us.matches(null, offset, limit, true));
\r
588 if(us.matches(null, offset, limit, false) != UnicodeSet.U_MATCH){
\r
589 errln("UnicodeSet.matches is suppose to return " + UnicodeSet.U_MATCH +
\r
590 " but got " + us.matches(null, offset, limit, false));
\r
593 // Tests when "int maxLen = forward ? limit-offset[0] : offset[0]-limit;" is true and false
\r
595 offset[0] = 0; // Takes the letter "d"
\r
596 us.matches(rs, offset, 1, true);
\r
597 offset[0] = 4; // Takes the letter "y"
\r
598 us.matches(rs, offset, 1, true);
\r
599 } catch(Exception e) {
\r
600 errln("UnicodeSet.matches is not suppose to return an exception");
\r
603 // TODO: Tests when "if (forward && length < highWaterLength)" is true
\r
606 /* Tests the method
\r
607 * private static int matchRest (Replaceable text, int start, int limit, String s)
\r
608 * from public int matches(Replaceable text, ...
\r
610 public void TestMatchRest(){
\r
611 // TODO: Tests when "if (maxLen > slen) maxLen = slen;" is true and false
\r
614 /* Tests the method
\r
615 * public int matchesAt(CharSequence text, int offset)
\r
617 public void TestMatchesAt(){
\r
618 UnicodeSet us = new UnicodeSet(); // Empty set
\r
619 us.matchesAt((CharSequence)"dummy", 0);
\r
620 us.add("dummy"); // Add an item
\r
622 us.matchesAt((CharSequence)"dummy", 0);
\r
623 us.add("dummy2"); // Add another item
\r
625 us.matchesAt((CharSequence)"yummy", 0); //charAt(0) >
\r
626 us.matchesAt((CharSequence)"amy", 0); //charAt(0) <
\r
628 UnicodeSet us1 = new UnicodeSet(0,100000); // Increase the set
\r
629 us1.matchesAt((CharSequence)"dummy", 0);
\r
632 /* Tests the method
\r
633 * public int indexOf(int c)
\r
635 public void TestIndexOf(){
\r
636 // Tests when "if (c < MIN_VALUE || c > MAX_VALUE)" is true
\r
637 UnicodeSet us = new UnicodeSet();
\r
638 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
639 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
640 int[] valid = {UnicodeSet.MIN_VALUE, UnicodeSet.MIN_VALUE+1,
\r
641 UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1};
\r
643 for(int i=0; i < invalid.length; i++){
\r
645 us.indexOf(invalid[i]);
\r
646 errln("UnicodeSet.indexOf is suppose to return an exception " +
\r
647 "for a value of " + invalid[i]);
\r
648 } catch(Exception e){}
\r
651 for(int i=0; i < valid.length; i++){
\r
653 us.indexOf(valid[i]);
\r
654 } catch(Exception e){
\r
655 errln("UnicodeSet.indexOf is not suppose to return an exception " +
\r
656 "for a value of " + valid[i]);
\r
661 /* Tests the method
\r
662 * public int charAt(int index)
\r
664 public void TestCharAt(){
\r
665 UnicodeSet us = new UnicodeSet();
\r
667 // Test when "if (index >= 0)" is false
\r
668 int[] invalid = {-100,-10,-5,-2,-1};
\r
669 for(int i=0; i < invalid.length; i++){
\r
670 if(us.charAt(invalid[i]) != -1){
\r
671 errln("UnicodeSet.charAt(int index) was suppose to return -1 "
\r
672 + "for an invalid input of " + invalid[i]);
\r
677 /* Tests the method
\r
678 * private UnicodeSet add_unchecked(int start, int end)
\r
679 * from public UnicodeSet add(int start, int end)
\r
681 public void TestAdd_int_int(){
\r
682 UnicodeSet us = new UnicodeSet();
\r
683 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
684 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
686 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true
\r
687 for(int i=0; i < invalid.length; i++){
\r
689 us.add(invalid[i], UnicodeSet.MAX_VALUE);
\r
690 errln("UnicodeSet.add(int start, int end) was suppose to give "
\r
691 + "an exception for an start invalid input of "
\r
693 } catch (Exception e){}
\r
696 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true
\r
697 for(int i=0; i < invalid.length; i++){
\r
699 us.add(UnicodeSet.MIN_VALUE, invalid[i]);
\r
700 errln("UnicodeSet.add(int start, int end) was suppose to give "
\r
701 + "an exception for an end invalid input of "
\r
703 } catch (Exception e){}
\r
706 // Tests when "else if (start == end)" is false
\r
707 if(!(us.add(UnicodeSet.MIN_VALUE+1, UnicodeSet.MIN_VALUE).equals(us)))
\r
708 errln("UnicodeSet.add(int start, int end) was suppose to return "
\r
709 + "the same object because start of value " + (UnicodeSet.MIN_VALUE+1)
\r
710 + " is greater than end of value " + UnicodeSet.MIN_VALUE);
\r
712 if(!(us.add(UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1).equals(us)))
\r
713 errln("UnicodeSet.add(int start, int end) was suppose to return "
\r
714 + "the same object because start of value " + UnicodeSet.MAX_VALUE
\r
715 + " is greater than end of value " + (UnicodeSet.MAX_VALUE-1));
\r
718 /* Tests the method
\r
719 * private final UnicodeSet add_unchecked(int c)
\r
720 * from public final UnicodeSet add(int c)
\r
722 public void TestAdd_int(){
\r
723 UnicodeSet us = new UnicodeSet();
\r
724 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
725 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
727 // Tests when "if (c < MIN_VALUE || c > MAX_VALUE)" is true
\r
728 for(int i=0; i < invalid.length; i++){
\r
730 us.add(invalid[i]);
\r
731 errln("UnicodeSet.add(int c) was suppose to give "
\r
732 + "an exception for an start invalid input of "
\r
734 } catch (Exception e){}
\r
737 // Tests when "if (c == MAX_VALUE)" is true
\r
738 // TODO: Check comment in UnicodeSet.java
\r
741 /* Tests the method
\r
742 * private static int getSingleCP(String s)
\r
743 * from public final boolean contains(String s)
\r
745 public void TestGetSingleCP(){
\r
746 UnicodeSet us = new UnicodeSet();
\r
747 // Tests when "if (s.length() < 1)" is true
\r
750 errln("UnicodeSet.getSingleCP is suppose to give an exception for " +
\r
751 "an empty string.");
\r
752 } catch (Exception e){}
\r
755 us.contains((String)null);
\r
756 errln("UnicodeSet.getSingleCP is suppose to give an exception for " +
\r
758 } catch (Exception e){}
\r
760 // Tests when "if (cp > 0xFFFF)" is true
\r
761 String[] cases = {"\uD811\uDC00","\uD811\uDC11","\uD811\uDC22"};
\r
762 for(int i=0; i<cases.length; i++){
\r
764 us.contains(cases[i]);
\r
765 } catch (Exception e){
\r
766 errln("UnicodeSet.getSingleCP is not suppose to give an exception for " +
\r
772 /* Tests the method
\r
773 * public final UnicodeSet removeAllStrings()
\r
775 public void TestRemoveAllString(){
\r
776 // Tests when "if (strings.size() != 0)" is false
\r
777 UnicodeSet us = new UnicodeSet();
\r
779 us.removeAllStrings();
\r
780 } catch(Exception e){
\r
781 errln("UnicodeSet.removeAllString() was not suppose to given an " +
\r
782 "exception for a strings size of 0");
\r
786 /* Tests the method
\r
787 * public UnicodeSet retain(int start, int end)
\r
789 public void TestRetain_int_int(){
\r
790 UnicodeSet us = new UnicodeSet();
\r
791 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
792 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
794 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true
\r
795 for(int i=0; i < invalid.length; i++){
\r
797 us.retain(invalid[i], UnicodeSet.MAX_VALUE);
\r
798 errln("UnicodeSet.retain(int start, int end) was suppose to give "
\r
799 + "an exception for an start invalid input of "
\r
801 } catch (Exception e){}
\r
804 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true
\r
805 for(int i=0; i < invalid.length; i++){
\r
807 us.retain(UnicodeSet.MIN_VALUE, invalid[i]);
\r
808 errln("UnicodeSet.retain(int start, int end) was suppose to give "
\r
809 + "an exception for an end invalid input of "
\r
811 } catch (Exception e){}
\r
814 // Tests when "if (start <= end)" is false
\r
816 us.retain(UnicodeSet.MIN_VALUE+1, UnicodeSet.MIN_VALUE);
\r
817 } catch(Exception e){
\r
818 errln("UnicodeSet.retain(int start, int end) was not suppose to give "
\r
819 + "an exception.");
\r
823 us.retain(UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1);
\r
824 } catch(Exception e){
\r
825 errln("UnicodeSet.retain(int start, int end) was not suppose to give "
\r
826 + "an exception.");
\r
830 /* Tests the method
\r
831 * public final UnicodeSet retain(String s)
\r
833 public void TestRetain_String(){
\r
834 // Tests when "if (isIn && size() == 1)" is true
\r
835 UnicodeSet us = new UnicodeSet();
\r
837 if(!(us.retain("dummy").equals(us))){
\r
838 errln("UnicodeSet.retain(String s) was suppose to return the " +
\r
839 "same UnicodeSet since the string was found in the original.");
\r
843 /* Tests the method
\r
844 * public UnicodeSet remove(int start, int end)
\r
846 public void TestRemove(){
\r
847 UnicodeSet us = new UnicodeSet();
\r
848 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
849 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
851 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true
\r
852 for(int i=0; i < invalid.length; i++){
\r
854 us.remove(invalid[i], UnicodeSet.MAX_VALUE);
\r
855 errln("UnicodeSet.remove(int start, int end) was suppose to give "
\r
856 + "an exception for an start invalid input of "
\r
858 } catch (Exception e){}
\r
861 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true
\r
862 for(int i=0; i < invalid.length; i++){
\r
864 us.remove(UnicodeSet.MIN_VALUE, invalid[i]);
\r
865 errln("UnicodeSet.remove(int start, int end) was suppose to give "
\r
866 + "an exception for an end invalid input of "
\r
868 } catch (Exception e){}
\r
871 // Tests when "if (start <= end)" is false
\r
873 us.remove(UnicodeSet.MIN_VALUE+1, UnicodeSet.MIN_VALUE);
\r
874 } catch(Exception e){
\r
875 errln("UnicodeSet.remove(int start, int end) was not suppose to give "
\r
876 + "an exception.");
\r
880 us.remove(UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1);
\r
881 } catch(Exception e){
\r
882 errln("UnicodeSet.remove(int start, int end) was not suppose to give "
\r
883 + "an exception.");
\r
887 /* Tests the method
\r
888 * public UnicodeSet complement(int start, int end)
\r
890 public void TestComplement_int_int(){
\r
891 UnicodeSet us = new UnicodeSet();
\r
892 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
893 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
895 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true
\r
896 for(int i=0; i < invalid.length; i++){
\r
898 us.complement(invalid[i], UnicodeSet.MAX_VALUE);
\r
899 errln("UnicodeSet.complement(int start, int end) was suppose to give "
\r
900 + "an exception for an start invalid input of "
\r
902 } catch (Exception e){}
\r
905 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true
\r
906 for(int i=0; i < invalid.length; i++){
\r
908 us.complement(UnicodeSet.MIN_VALUE, invalid[i]);
\r
909 errln("UnicodeSet.complement(int start, int end) was suppose to give "
\r
910 + "an exception for an end invalid input of "
\r
912 } catch (Exception e){}
\r
915 // Tests when "if (start <= end)" is false
\r
917 us.complement(UnicodeSet.MIN_VALUE+1, UnicodeSet.MIN_VALUE);
\r
918 } catch(Exception e){
\r
919 errln("UnicodeSet.complement(int start, int end) was not suppose to give "
\r
920 + "an exception.");
\r
924 us.complement(UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1);
\r
925 } catch(Exception e){
\r
926 errln("UnicodeSet.complement(int start, int end) was not suppose to give "
\r
927 + "an exception.");
\r
931 /* Tests the method
\r
932 * public final UnicodeSet complement(String s)
\r
934 public void TestComplement_String(){
\r
935 // Tests when "if (cp < 0)" is false
\r
936 UnicodeSet us = new UnicodeSet();
\r
939 us.complement("dummy");
\r
940 } catch (Exception e){
\r
941 errln("UnicodeSet.complement(String s) was not suppose to give "
\r
942 + "an exception for 'dummy'.");
\r
945 // Tests when "if (strings.contains(s))" is true
\r
946 us = new UnicodeSet();
\r
949 us.complement("\uDC11");
\r
950 } catch (Exception e){
\r
951 errln("UnicodeSet.complement(String s) was not suppose to give "
\r
952 + "an exception for '\uDC11'.");
\r
956 /* Tests the method
\r
957 * public boolean contains(int c)
\r
959 public void TestContains_int(){
\r
960 UnicodeSet us = new UnicodeSet();
\r
961 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
962 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
964 // Tests when "if (c < MIN_VALUE || c > MAX_VALUE)" is true
\r
965 for(int i=0; i < invalid.length; i++){
\r
967 us.contains(invalid[i]);
\r
968 errln("UnicodeSet.contains(int c) was suppose to give "
\r
969 + "an exception for an start invalid input of "
\r
971 } catch (Exception e){}
\r
975 /* Tests the method
\r
976 * public boolean contains(int start, int end)
\r
978 public void TestContains_int_int(){
\r
979 UnicodeSet us = new UnicodeSet();
\r
980 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
981 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
983 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true
\r
984 for(int i=0; i < invalid.length; i++){
\r
986 us.contains(invalid[i], UnicodeSet.MAX_VALUE);
\r
987 errln("UnicodeSet.contains(int start, int end) was suppose to give "
\r
988 + "an exception for an start invalid input of "
\r
990 } catch (Exception e){}
\r
993 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true
\r
994 for(int i=0; i < invalid.length; i++){
\r
996 us.contains(UnicodeSet.MIN_VALUE, invalid[i]);
\r
997 errln("UnicodeSet.contains(int start, int end) was suppose to give "
\r
998 + "an exception for an end invalid input of "
\r
1000 } catch (Exception e){}
\r
1004 /* Tests the method
\r
1005 * public String getRegexEquivalent()
\r
1007 public void TestGetRegexEquivalent(){
\r
1008 UnicodeSet us = new UnicodeSet();
\r
1009 String res = us.getRegexEquivalent();
\r
1010 if(!(res.equals("[]")))
\r
1011 errln("UnicodeSet.getRegexEquivalent is suppose to return '[]' " +
\r
1012 "but got " + res);
\r
1015 /* Tests the method
\r
1016 * public boolean containsNone(int start, int end)
\r
1018 public void TestContainsNone(){
\r
1019 UnicodeSet us = new UnicodeSet();
\r
1020 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2,
\r
1021 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2};
\r
1023 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true
\r
1024 for(int i=0; i < invalid.length; i++){
\r
1026 us.containsNone(invalid[i], UnicodeSet.MAX_VALUE);
\r
1027 errln("UnicodeSet.containsNoneint start, int end) was suppose to give "
\r
1028 + "an exception for an start invalid input of "
\r
1030 } catch (Exception e){}
\r
1033 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true
\r
1034 for(int i=0; i < invalid.length; i++){
\r
1036 us.containsNone(UnicodeSet.MIN_VALUE, invalid[i]);
\r
1037 errln("UnicodeSet.containsNone(int start, int end) was suppose to give "
\r
1038 + "an exception for an end invalid input of "
\r
1040 } catch (Exception e){}
\r
1043 // Tests when "if (start < list[++i])" is false
\r
1046 us.containsNone(1, 2); // 1 > 0
\r
1047 } catch (Exception e){
\r
1048 errln("UnicodeSet.containsNone(int start, int end) was not suppose to give " +
\r