1/* GENERATED SOURCE. DO NOT MODIFY. */ 2/* 3******************************************************************************* 4* Copyright (C) 1996-2010, International Business Machines Corporation and * 5* others. All Rights Reserved. * 6******************************************************************************* 7*/ 8 9package android.icu.dev.test.util; 10 11import android.icu.dev.test.TestFmwk; 12import android.icu.impl.Utility; 13import android.icu.text.ReplaceableString; 14import android.icu.text.UnicodeSet; 15import android.icu.util.StringTokenizer; 16import org.junit.runner.RunWith; 17import android.icu.junit.IcuTestFmwkRunner; 18 19/** 20* Testing class for StringTokenizer class 21* @author Syn Wee Quek 22* @since oct 26 2002 23*/ 24@RunWith(IcuTestFmwkRunner.class) 25public final class StringTokenizerTest extends TestFmwk 26{ 27 // constructor =================================================== 28 29 /** 30 * Constructor 31 */ 32 public StringTokenizerTest() 33 { 34 } 35 36 // public methods -------------------------------------------------------- 37 38 /** 39 * Testing constructors 40 */ 41 public void TestConstructors() 42 { 43 String str = "this\tis\na\rstring\ftesting\tStringTokenizer\nconstructors!"; 44 String delimiter = " \t\n\r\f"; 45 String expected[] = {"this", "is", "a", "string", "testing", 46 "StringTokenizer", "constructors!"}; 47 StringTokenizer defaultst = new StringTokenizer(str); 48 StringTokenizer stdelimiter = new StringTokenizer(str, delimiter); 49 StringTokenizer stdelimiterreturn = new StringTokenizer(str, delimiter, 50 false); 51 UnicodeSet delimiterset = new UnicodeSet("[" + delimiter + "]", false); 52 StringTokenizer stdelimiterset = new StringTokenizer(str, delimiterset); 53 StringTokenizer stdelimitersetreturn = new StringTokenizer(str, 54 delimiterset, 55 false); 56 for (int i = 0; i < expected.length; i ++) { 57 if (!(defaultst.nextElement().equals(expected[i]) 58 && stdelimiter.nextElement().equals(expected[i]) 59 && stdelimiterreturn.nextElement().equals(expected[i]) 60 && stdelimiterset.nextElement().equals(expected[i]) 61 && stdelimitersetreturn.nextElement().equals(expected[i]))) { 62 errln("Constructor with default delimiter gives wrong results"); 63 } 64 } 65 66 UnicodeSet delimiterset1 = new UnicodeSet("[" + delimiter + "]", true); 67 StringTokenizer stdelimiterset1 = new StringTokenizer(str, delimiterset1); 68 if(!(stdelimiterset1.nextElement().equals(str))) 69 errln("Constructor with a UnicodeSet to ignoreWhiteSpace is " + 70 "to return the same string."); 71 72 String expected1[] = {"this", "\t", "is", "\n", "a", "\r", "string", "\f", 73 "testing", "\t", "StringTokenizer", "\n", 74 "constructors!"}; 75 stdelimiterreturn = new StringTokenizer(str, delimiter, true); 76 stdelimitersetreturn = new StringTokenizer(str, delimiterset, true); 77 for (int i = 0; i < expected1.length; i ++) { 78 if (!(stdelimiterreturn.nextElement().equals(expected1[i]) 79 && stdelimitersetreturn.nextElement().equals(expected1[i]))) { 80 errln("Constructor with default delimiter and delimiter tokens gives wrong results"); 81 } 82 } 83 84 stdelimiter = new StringTokenizer(str, (String)null); 85 stdelimiterreturn = new StringTokenizer(str, (String)null, false); 86 delimiterset = null; 87 stdelimiterset = new StringTokenizer(str, delimiterset); 88 stdelimitersetreturn = new StringTokenizer(str, delimiterset, false); 89 90 if (!(stdelimiter.nextElement().equals(str) 91 && stdelimiterreturn.nextElement().equals(str) 92 && stdelimiterset.nextElement().equals(str) 93 && stdelimitersetreturn.nextElement().equals(str))) { 94 errln("Constructor with null delimiter gives wrong results"); 95 } 96 97 delimiter = ""; 98 stdelimiter = new StringTokenizer(str, delimiter); 99 stdelimiterreturn = new StringTokenizer(str, delimiter, false); 100 delimiterset = new UnicodeSet(); 101 stdelimiterset = new StringTokenizer(str, delimiterset); 102 stdelimitersetreturn = new StringTokenizer(str, delimiterset, false); 103 104 if (!(stdelimiter.nextElement().equals(str) 105 && stdelimiterreturn.nextElement().equals(str) 106 && stdelimiterset.nextElement().equals(str) 107 && stdelimitersetreturn.nextElement().equals(str))) { 108 errln("Constructor with empty delimiter gives wrong results"); 109 } 110 111 try { 112 defaultst = new StringTokenizer(null); 113 errln("null string should throw an exception"); 114 } catch (Exception e) { 115 logln("PASS: Constructor with null string failed as expected"); 116 } 117 try { 118 stdelimiter = new StringTokenizer(null, delimiter); 119 errln("null string should throw an exception"); 120 } catch (Exception e) { 121 logln("PASS: Constructor with null string failed as expected"); 122 } 123 try { 124 stdelimiterreturn = new StringTokenizer(null, delimiter, false); 125 errln("null string should throw an exception"); 126 } catch (Exception e) { 127 logln("PASS: Constructor with null string failed as expected"); 128 } 129 try { 130 stdelimiterset = new StringTokenizer(null, delimiterset); 131 errln("null string should throw an exception"); 132 } catch (Exception e) { 133 logln("PASS: Constructor with null string failed as expected"); 134 } 135 try { 136 stdelimitersetreturn = new StringTokenizer(null, delimiterset, 137 false); 138 errln("null string should throw an exception"); 139 } catch (Exception e) { 140 logln("PASS: Constructor with null string failed as expected"); 141 } 142 } 143 144 /** 145 * Testing supplementary 146 */ 147 public void TestSupplementary() 148 { 149 String str = "bmp string \ud800 with a unmatched surrogate character"; 150 String delimiter = "\ud800\udc00"; 151 String expected[] = {str}; 152 153 StringTokenizer tokenizer = new StringTokenizer(str, delimiter); 154 if (!tokenizer.nextElement().equals(expected[0])) { 155 errln("Error parsing \"" + Utility.hex(str) + "\""); 156 } 157 if (tokenizer.hasMoreElements()) { 158 errln("Number of tokens exceeded expected"); 159 } 160 delimiter = "\ud800"; 161 String expected1[] = {"bmp string ", 162 " with a unmatched surrogate character"}; 163 tokenizer = new StringTokenizer(str, delimiter); 164 int i = 0; 165 while (tokenizer.hasMoreElements()) { 166 if (!tokenizer.nextElement().equals(expected1[i ++])) { 167 errln("Error parsing \"" + Utility.hex(str) + "\""); 168 } 169 } 170 if (tokenizer.hasMoreElements()) { 171 errln("Number of tokens exceeded expected"); 172 } 173 174 str = "string \ud800\udc00 with supplementary character"; 175 delimiter = "\ud800"; 176 String expected2[] = {str}; 177 tokenizer = new StringTokenizer(str, delimiter); 178 if (!tokenizer.nextElement().equals(expected2[0])) { 179 errln("Error parsing \"" + Utility.hex(str) + "\""); 180 } 181 if (tokenizer.hasMoreElements()) { 182 errln("Number of tokens exceeded expected"); 183 } 184 185 delimiter = "\ud800\udc00"; 186 String expected3[] = {"string ", " with supplementary character"}; 187 tokenizer = new StringTokenizer(str, delimiter); 188 i = 0; 189 while (tokenizer.hasMoreElements()) { 190 if (!tokenizer.nextElement().equals(expected3[i ++])) { 191 errln("Error parsing \"" + Utility.hex(str) + "\""); 192 } 193 } 194 if (tokenizer.hasMoreElements()) { 195 errln("Number of tokens exceeded expected"); 196 } 197 198 str = "\ud800 \ud800\udc00 \ud800 \ud800\udc00"; 199 delimiter = "\ud800"; 200 String expected4[] = {" \ud800\udc00 ", " \ud800\udc00"}; 201 i = 0; 202 while (tokenizer.hasMoreElements()) { 203 if (!tokenizer.nextElement().equals(expected4[i ++])) { 204 errln("Error parsing \"" + Utility.hex(str) + "\""); 205 } 206 } 207 if (tokenizer.hasMoreElements()) { 208 errln("Number of tokens exceeded expected"); 209 } 210 211 delimiter = "\ud800\udc00"; 212 String expected5[] = {"\ud800 ", " \ud800 "}; 213 i = 0; 214 while (tokenizer.hasMoreElements()) { 215 if (!tokenizer.nextElement().equals(expected5[i ++])) { 216 errln("Error parsing \"" + Utility.hex(str) + "\""); 217 } 218 } 219 if (tokenizer.hasMoreElements()) { 220 errln("Number of tokens exceeded expected"); 221 } 222 } 223 224 /** 225 * Testing next api 226 */ 227 public void TestNextNonDelimiterToken() 228 { 229 String str = " , 1 2 3 AHHHHH! 5.5 6 7 , 8\n"; 230 String expected[] = {",", "1", "2", "3", "AHHHHH!", "5.5", "6", "7", 231 ",", "8\n"}; 232 String delimiter = " "; 233 234 StringTokenizer tokenizer = new StringTokenizer(str, delimiter); 235 int currtoken = 0; 236 while (tokenizer.hasMoreElements()) { 237 if (!tokenizer.nextElement().equals(expected[currtoken])) { 238 errln("Error token mismatch, expected " + expected[currtoken]); 239 } 240 currtoken ++; 241 } 242 243 if (currtoken != expected.length) { 244 errln("Didn't get correct number of tokens"); 245 } 246 247 tokenizer = new StringTokenizer("", delimiter); 248 if (tokenizer.hasMoreElements()) { 249 errln("Empty string should not have any tokens"); 250 } 251 try { 252 tokenizer.nextElement(); 253 errln("Empty string should not have any tokens"); 254 } catch (Exception e) { 255 logln("PASS: empty string failed as expected"); 256 } 257 258 tokenizer = new StringTokenizer(", ,", ", "); 259 if (tokenizer.hasMoreElements()) { 260 errln("String with only delimiters should not have any tokens"); 261 } 262 try { 263 tokenizer.nextElement(); 264 errln("String with only delimiters should not have any tokens"); 265 } catch (Exception e) { 266 logln("PASS: String with only delimiters failed as expected"); 267 } 268 269 tokenizer = new StringTokenizer("q, ,", ", "); 270 if (!tokenizer.hasMoreElements()) { 271 errln("String that does not begin with delimiters should have some tokens"); 272 } 273 if (!tokenizer.nextElement().equals("q")) { 274 errln("String that does not begin with delimiters should have some tokens"); 275 } 276 try { 277 tokenizer.nextElement(); 278 errln("String has only one token"); 279 } catch (Exception e) { 280 logln("PASS: String with only one token failed as expected"); 281 } 282 283 try { 284 tokenizer = new StringTokenizer(null, delimiter); 285 errln("StringTokenizer constructed with null source should throw a nullpointerexception"); 286 } catch (Exception e) { 287 logln("PASS: StringTokenizer constructed with null source failed as expected"); 288 } 289 290 tokenizer = new StringTokenizer(str, "q"); 291 if (!tokenizer.nextElement().equals(str)) { 292 errln("Should have received the same string when there are no delimiters"); 293 } 294 } 295 296 /** 297 * Test java compatibility, except we support surrogates. 298 */ 299 public void TestNoCoalesce() { 300 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"; 301 String delims = " \t\n\r\f\ud800\udc00"; 302 303 java.util.StringTokenizer jt = new java.util.StringTokenizer(str, delims, true); 304 android.icu.util.StringTokenizer it = new android.icu.util.StringTokenizer(str, delims, true); 305 int n = 0; 306 while (jt.hasMoreTokens() && it.hasMoreTokens()) { 307 assertEquals("[" + String.valueOf(n++) + "]", jt.nextToken(), it.nextToken()); 308 } 309 assertFalse("java tokenizer has no more tokens", jt.hasMoreTokens()); 310 assertFalse("icu tokenizer has no more tokens", it.hasMoreTokens()); 311 312 String sur = "Even\ud800\udc00 works.\n\n"; 313 it = new android.icu.util.StringTokenizer(sur, delims, true); // no coalesce 314 assertEquals("sur1", it.nextToken(), "Even"); 315 assertEquals("sur2", it.nextToken(), "\ud800\udc00"); 316 assertEquals("sur3", it.nextToken(), " "); 317 assertEquals("sur4", it.nextToken(), "works."); 318 assertEquals("sur5", it.nextToken(), "\n"); 319 assertEquals("sur6", it.nextToken(), "\n"); 320 assertFalse("sur7", it.hasMoreTokens()); 321 } 322 323 /** 324 * Testing next api 325 */ 326 public void TestNextDelimiterToken() 327 { 328 String str = " , 1 2 3 AHHHHH! 5.5 6 7 , 8\n"; 329 String expected[] = {" ", ",", " ", "1", " ", "2", " ", "3", " ", 330 "AHHHHH!", " ", "5.5", " ", "6", " ", "7", " ", 331 ",", " ", "8\n"}; 332 String delimiter = " "; 333 334 StringTokenizer tokenizer = new StringTokenizer(str, delimiter, true, true); 335 336 int currtoken = 0; 337 while (tokenizer.hasMoreElements()) { 338 if (!tokenizer.nextElement().equals(expected[currtoken])) { 339 errln("Error token mismatch, expected " + expected[currtoken]); 340 } 341 currtoken ++; 342 } 343 344 if (currtoken != expected.length) { 345 errln("Didn't get correct number of tokens"); 346 } 347 348 tokenizer = new StringTokenizer("", delimiter, true); 349 if (tokenizer.hasMoreElements()) { 350 errln("Empty string should not have any tokens"); 351 } 352 try { 353 tokenizer.nextElement(); 354 errln("Empty string should not have any tokens"); 355 } catch (Exception e) { 356 logln("PASS: Empty string failed as expected"); 357 } 358 359 tokenizer = new StringTokenizer(", ,", ", ", true, true); 360 if (!tokenizer.hasMoreElements()) { 361 errln("String with only delimiters should have tokens when delimiter is treated as tokens"); 362 } 363 if (!tokenizer.nextElement().equals(", ,")) { 364 errln("String with only delimiters should return itself when delimiter is treated as tokens"); 365 } 366 367 tokenizer = new StringTokenizer("q, ,", ", ", true, true); 368 369 if (!tokenizer.hasMoreElements()) { 370 errln("String should have some tokens"); 371 } 372 if (!tokenizer.nextElement().equals("q") 373 || !tokenizer.nextElement().equals(", ,")) { 374 errln("String tokens do not match expected results"); 375 } 376 377 try { 378 tokenizer = new StringTokenizer(null, delimiter, true); 379 errln("StringTokenizer constructed with null source should throw a nullpointerexception"); 380 } catch (Exception e) { 381 logln("PASS: StringTokenizer constructed with null source failed as expected"); 382 } 383 384 tokenizer = new StringTokenizer(str, "q", true); 385 if (!tokenizer.nextElement().equals(str)) { 386 errln("Should have recieved the same string when there are no delimiters"); 387 } 388 } 389 390 /** 391 * Testing count tokens 392 */ 393 public void TestCountTokens() 394 { 395 String str = "this\tis\na\rstring\ftesting\tStringTokenizer\nconstructors!"; 396 String delimiter = " \t\n\r\f"; 397 String expected[] = {"this", "is", "a", "string", "testing", 398 "StringTokenizer", "constructors!"}; 399 String expectedreturn[] = {"this", "\t", "is", "\n", "a", "\r", 400 "string", "\f", "testing", "\t", 401 "StringTokenizer", "\n", "constructors!"}; 402 StringTokenizer st = new StringTokenizer(str, delimiter); 403 StringTokenizer streturn = new StringTokenizer(str, delimiter, true); 404 if (st.countTokens() != expected.length) { 405 errln("CountTokens failed for non-delimiter tokens"); 406 } 407 if (streturn.countTokens() != expectedreturn.length) { 408 errln("CountTokens failed for delimiter tokens"); 409 } 410 for (int i = 0; i < expected.length; i ++) { 411 if (!st.nextElement().equals(expected[i]) 412 || st.countTokens() != expected.length - i - 1) { 413 errln("CountTokens default delimiter gives wrong results"); 414 } 415 } 416 for (int i = 0; i < expectedreturn.length; i ++) { 417 if (!streturn.nextElement().equals(expectedreturn[i]) 418 || streturn.countTokens() != expectedreturn.length - i - 1) { 419 errln("CountTokens with default delimiter and delimiter tokens gives wrong results"); 420 } 421 } 422 } 423 424 /** 425 * Next token with new delimiters 426 */ 427 public void TestNextNewDelimiters() 428 { 429 String str = "abc0def1ghi2jkl3mno4pqr0stu1vwx2yza3bcd4efg0hij1klm2nop3qrs4tuv"; 430 String delimiter[] = {"0", "1", "2", "3", "4"}; 431 String expected[][] = {{"abc", "pqr", "efg"}, 432 {"def", "stu", "hij"}, 433 {"ghi", "vwx", "klm"}, 434 {"jkl", "yza", "nop"}, 435 {"mno", "bcd", "qrs"} 436 }; 437 StringTokenizer st = new StringTokenizer(str); 438 int size = expected[0].length; 439 for (int i = 0; i < size; i ++) { 440 for (int j = 0; j < delimiter.length; j ++) { 441 if (!st.nextToken(delimiter[j]).equals(expected[j][i])) { 442 errln("nextToken() with delimiters error " + i + " " + j); 443 } 444 if (st.countTokens() != expected[j].length - i) { 445 errln("countTokens() after nextToken() with delimiters error" 446 + i + " " + j); 447 } 448 } 449 } 450 st = new StringTokenizer(str); 451 String delimiter1[] = {"0", "2", "4"}; 452 String expected1[] = {"abc", "def1ghi", "jkl3mno", "pqr", "stu1vwx", 453 "yza3bcd", "efg", "hij1klm", "nop3qrs", "tuv"}; 454 for (int i = 0; i < expected1.length; i ++) { 455 if (!st.nextToken(delimiter1[i % delimiter1.length]).equals( 456 expected1[i])) { 457 errln("nextToken() with delimiters error " + i); 458 } 459 } 460 } 461 462 public void TestBug4423() 463 { 464 // bug 4423: a bad interaction between countTokens() and hasMoreTokens(). 465 // 466 String s1 = "This is a test"; 467 StringTokenizer tzr = new StringTokenizer(s1); 468 int tokenCount = 0; 469 470 int t = tzr.countTokens(); 471 if (t!= 4) { 472 errln("tzr.countTokens() returned " + t + ". Expected 4"); 473 } 474 while (tzr.hasMoreTokens()) { 475 String tok = tzr.nextToken(); 476 if (tok.length() == 0) { 477 errln("token with length == 0"); 478 } 479 tokenCount++; 480 } 481 if (tokenCount != 4) { 482 errln("Incorrect number of tokens found = " + tokenCount); 483 } 484 485 // Precomputed tokens arrays can grow. Check for edge cases around 486 // boundary where growth is forced. Array grows in increments of 100 tokens. 487 String s2 = ""; 488 for (int i=1; i<250; i++) { 489 s2 = s2 + " " + i; 490 StringTokenizer tzb = new StringTokenizer(s2); 491 int t2 = tzb.countTokens(); 492 if (t2 != i) { 493 errln("tzb.countTokens() returned " + t + ". Expected " + i); 494 break; 495 } 496 int j = 0; 497 while (tzb.hasMoreTokens()) { 498 String tok = tzb.nextToken(); 499 j++; 500 if (tok.equals(Integer.toString(j)) == false) { 501 errln("Wrong token string. Expected \"" + j + "\", got \"" 502 + tok + "\"."); 503 break; 504 } 505 } 506 if (j != i) { 507 errln("Wrong number of tokens. Expected " + i + ". Got " + j 508 + "."); 509 break; 510 } 511 } 512 513 } 514 515 public void TestCountTokensNoCoalesce() { 516 // jitterbug 5207 517 String str = "\"\""; 518 String del = "\""; 519 StringTokenizer st = new StringTokenizer(str, del, true); 520 int count = 0; 521 while (st.hasMoreTokens()) { 522 String t = st.nextToken(); 523 logln("[" + count + "] '" + t + "'"); 524 ++count; 525 } 526 st = new StringTokenizer(str, del, true); 527 int ncount = st.countTokens(); 528 int xcount = 0; 529 while (st.hasMoreTokens()) { 530 String t = st.nextToken(); 531 logln("[" + xcount + "] '" + t + "'"); 532 ++xcount; 533 } 534 if (count != ncount || count != xcount) { 535 errln("inconsistent counts " + count + ", " + ncount + ", " + xcount); 536 } 537 } 538 539 public static void main(String[] arg) 540 { 541 try 542 { 543 StringTokenizerTest test = new StringTokenizerTest(); 544 test.run(arg); 545 // test.TestCaseCompare(); 546 } 547 catch (Exception e) 548 { 549 e.printStackTrace(); 550 } 551 } 552 553 /* Tests the method 554 * public StringBuffer _generatePattern(StringBuffer result, boolean escapeUnprintable) 555 */ 556 public void Test_GeneratePattern(){ 557 UnicodeSet us = new UnicodeSet(); 558 StringBuffer sb = new StringBuffer(); 559 try{ 560 us._generatePattern(sb, true); 561 us._generatePattern(sb, false); 562 us._generatePattern(sb.append(1), true); 563 us._generatePattern(sb.append(1.0), true); 564 us._generatePattern(sb.reverse(), true); 565 } catch(Exception e){ 566 errln("UnicodeSet._generatePattern is not suppose to return an exception."); 567 } 568 569 try{ 570 us._generatePattern(null, true); 571 errln("UnicodeSet._generatePattern is suppose to return an exception."); 572 } catch(Exception e){} 573 } 574 575 /* Tests the method 576 * public int matches(Replaceable text, int[] offset, int limit, boolean incremental) 577 */ 578 public void TestMatches(){ 579 // Tests when "return incremental ? U_PARTIAL_MATCH : U_MATCH;" is true and false 580 ReplaceableString rs = new ReplaceableString("dummy"); 581 UnicodeSet us = new UnicodeSet(0,100000); // Create a large Unicode set 582 us.add("dummy"); 583 584 int[] offset = {0}; 585 int limit = 0; 586 587 if(us.matches(null, offset, limit, true) != UnicodeSet.U_PARTIAL_MATCH){ 588 errln("UnicodeSet.matches is suppose to return " + UnicodeSet.U_PARTIAL_MATCH + 589 " but got " + us.matches(null, offset, limit, true)); 590 } 591 592 if(us.matches(null, offset, limit, false) != UnicodeSet.U_MATCH){ 593 errln("UnicodeSet.matches is suppose to return " + UnicodeSet.U_MATCH + 594 " but got " + us.matches(null, offset, limit, false)); 595 } 596 597 // Tests when "int maxLen = forward ? limit-offset[0] : offset[0]-limit;" is true and false 598 try{ 599 offset[0] = 0; // Takes the letter "d" 600 us.matches(rs, offset, 1, true); 601 offset[0] = 4; // Takes the letter "y" 602 us.matches(rs, offset, 1, true); 603 } catch(Exception e) { 604 errln("UnicodeSet.matches is not suppose to return an exception"); 605 } 606 607 // TODO: Tests when "if (forward && length < highWaterLength)" is true 608 } 609 610 /* Tests the method 611 * private static int matchRest (Replaceable text, int start, int limit, String s) 612 * from public int matches(Replaceable text, ... 613 */ 614 public void TestMatchRest(){ 615 // TODO: Tests when "if (maxLen > slen) maxLen = slen;" is true and false 616 } 617 618 /* Tests the method 619 * public int matchesAt(CharSequence text, int offset) 620 */ 621 public void TestMatchesAt(){ 622 UnicodeSet us = new UnicodeSet(); // Empty set 623 us.matchesAt((CharSequence)"dummy", 0); 624 us.add("dummy"); // Add an item 625 626 us.matchesAt((CharSequence)"dummy", 0); 627 us.add("dummy2"); // Add another item 628 629 us.matchesAt((CharSequence)"yummy", 0); //charAt(0) > 630 us.matchesAt((CharSequence)"amy", 0); //charAt(0) < 631 632 UnicodeSet us1 = new UnicodeSet(0,100000); // Increase the set 633 us1.matchesAt((CharSequence)"dummy", 0); 634 } 635 636 /* Tests the method 637 * public int indexOf(int c) 638 */ 639 public void TestIndexOf(){ 640 // Tests when "if (c < MIN_VALUE || c > MAX_VALUE)" is true 641 UnicodeSet us = new UnicodeSet(); 642 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 643 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 644 int[] valid = {UnicodeSet.MIN_VALUE, UnicodeSet.MIN_VALUE+1, 645 UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1}; 646 647 for(int i=0; i < invalid.length; i++){ 648 try{ 649 us.indexOf(invalid[i]); 650 errln("UnicodeSet.indexOf is suppose to return an exception " + 651 "for a value of " + invalid[i]); 652 } catch(Exception e){} 653 } 654 655 for(int i=0; i < valid.length; i++){ 656 try{ 657 us.indexOf(valid[i]); 658 } catch(Exception e){ 659 errln("UnicodeSet.indexOf is not suppose to return an exception " + 660 "for a value of " + valid[i]); 661 } 662 } 663 } 664 665 /* Tests the method 666 * public int charAt(int index) 667 */ 668 public void TestCharAt(){ 669 UnicodeSet us = new UnicodeSet(); 670 671 // Test when "if (index >= 0)" is false 672 int[] invalid = {-100,-10,-5,-2,-1}; 673 for(int i=0; i < invalid.length; i++){ 674 if(us.charAt(invalid[i]) != -1){ 675 errln("UnicodeSet.charAt(int index) was suppose to return -1 " 676 + "for an invalid input of " + invalid[i]); 677 } 678 } 679 } 680 681 /* Tests the method 682 * private UnicodeSet add_unchecked(int start, int end) 683 * from public UnicodeSet add(int start, int end) 684 */ 685 public void TestAdd_int_int(){ 686 UnicodeSet us = new UnicodeSet(); 687 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 688 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 689 690 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true 691 for(int i=0; i < invalid.length; i++){ 692 try{ 693 us.add(invalid[i], UnicodeSet.MAX_VALUE); 694 errln("UnicodeSet.add(int start, int end) was suppose to give " 695 + "an exception for an start invalid input of " 696 + invalid[i]); 697 } catch (Exception e){} 698 } 699 700 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true 701 for(int i=0; i < invalid.length; i++){ 702 try{ 703 us.add(UnicodeSet.MIN_VALUE, invalid[i]); 704 errln("UnicodeSet.add(int start, int end) was suppose to give " 705 + "an exception for an end invalid input of " 706 + invalid[i]); 707 } catch (Exception e){} 708 } 709 710 // Tests when "else if (start == end)" is false 711 if(!(us.add(UnicodeSet.MIN_VALUE+1, UnicodeSet.MIN_VALUE).equals(us))) 712 errln("UnicodeSet.add(int start, int end) was suppose to return " 713 + "the same object because start of value " + (UnicodeSet.MIN_VALUE+1) 714 + " is greater than end of value " + UnicodeSet.MIN_VALUE); 715 716 if(!(us.add(UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1).equals(us))) 717 errln("UnicodeSet.add(int start, int end) was suppose to return " 718 + "the same object because start of value " + UnicodeSet.MAX_VALUE 719 + " is greater than end of value " + (UnicodeSet.MAX_VALUE-1)); 720 } 721 722 /* Tests the method 723 * private final UnicodeSet add_unchecked(int c) 724 * from public final UnicodeSet add(int c) 725 */ 726 public void TestAdd_int(){ 727 UnicodeSet us = new UnicodeSet(); 728 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 729 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 730 731 // Tests when "if (c < MIN_VALUE || c > MAX_VALUE)" is true 732 for(int i=0; i < invalid.length; i++){ 733 try{ 734 us.add(invalid[i]); 735 errln("UnicodeSet.add(int c) was suppose to give " 736 + "an exception for an start invalid input of " 737 + invalid[i]); 738 } catch (Exception e){} 739 } 740 741 // Tests when "if (c == MAX_VALUE)" is true 742 // TODO: Check comment in UnicodeSet.java 743 } 744 745 /* Tests the method 746 * private static int getSingleCP(String s) 747 * from public final boolean contains(String s) 748 */ 749 public void TestGetSingleCP(){ 750 UnicodeSet us = new UnicodeSet(); 751 // Tests when "if (s.length() < 1)" is true 752 try{ 753 us.contains(""); 754 errln("UnicodeSet.getSingleCP is suppose to give an exception for " + 755 "an empty string."); 756 } catch (Exception e){} 757 758 try{ 759 us.contains((String)null); 760 errln("UnicodeSet.getSingleCP is suppose to give an exception for " + 761 "a null string."); 762 } catch (Exception e){} 763 764 // Tests when "if (cp > 0xFFFF)" is true 765 String[] cases = {"\uD811\uDC00","\uD811\uDC11","\uD811\uDC22"}; 766 for(int i=0; i<cases.length; i++){ 767 try{ 768 us.contains(cases[i]); 769 } catch (Exception e){ 770 errln("UnicodeSet.getSingleCP is not suppose to give an exception for " + 771 "a null string."); 772 } 773 } 774 } 775 776 /* Tests the method 777 * public final UnicodeSet removeAllStrings() 778 */ 779 public void TestRemoveAllString(){ 780 // Tests when "if (strings.size() != 0)" is false 781 UnicodeSet us = new UnicodeSet(); 782 try{ 783 us.removeAllStrings(); 784 } catch(Exception e){ 785 errln("UnicodeSet.removeAllString() was not suppose to given an " + 786 "exception for a strings size of 0"); 787 } 788 } 789 790 /* Tests the method 791 * public UnicodeSet retain(int start, int end) 792 */ 793 public void TestRetain_int_int(){ 794 UnicodeSet us = new UnicodeSet(); 795 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 796 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 797 798 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true 799 for(int i=0; i < invalid.length; i++){ 800 try{ 801 us.retain(invalid[i], UnicodeSet.MAX_VALUE); 802 errln("UnicodeSet.retain(int start, int end) was suppose to give " 803 + "an exception for an start invalid input of " 804 + invalid[i]); 805 } catch (Exception e){} 806 } 807 808 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true 809 for(int i=0; i < invalid.length; i++){ 810 try{ 811 us.retain(UnicodeSet.MIN_VALUE, invalid[i]); 812 errln("UnicodeSet.retain(int start, int end) was suppose to give " 813 + "an exception for an end invalid input of " 814 + invalid[i]); 815 } catch (Exception e){} 816 } 817 818 // Tests when "if (start <= end)" is false 819 try{ 820 us.retain(UnicodeSet.MIN_VALUE+1, UnicodeSet.MIN_VALUE); 821 } catch(Exception e){ 822 errln("UnicodeSet.retain(int start, int end) was not suppose to give " 823 + "an exception."); 824 } 825 826 try{ 827 us.retain(UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1); 828 } catch(Exception e){ 829 errln("UnicodeSet.retain(int start, int end) was not suppose to give " 830 + "an exception."); 831 } 832 } 833 834 /* Tests the method 835 * public final UnicodeSet retain(String s) 836 */ 837 public void TestRetain_String(){ 838 // Tests when "if (isIn && size() == 1)" is true 839 UnicodeSet us = new UnicodeSet(); 840 us.add("dummy"); 841 if(!(us.retain("dummy").equals(us))){ 842 errln("UnicodeSet.retain(String s) was suppose to return the " + 843 "same UnicodeSet since the string was found in the original."); 844 } 845 } 846 847 /* Tests the method 848 * public UnicodeSet remove(int start, int end) 849 */ 850 public void TestRemove(){ 851 UnicodeSet us = new UnicodeSet(); 852 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 853 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 854 855 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true 856 for(int i=0; i < invalid.length; i++){ 857 try{ 858 us.remove(invalid[i], UnicodeSet.MAX_VALUE); 859 errln("UnicodeSet.remove(int start, int end) was suppose to give " 860 + "an exception for an start invalid input of " 861 + invalid[i]); 862 } catch (Exception e){} 863 } 864 865 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true 866 for(int i=0; i < invalid.length; i++){ 867 try{ 868 us.remove(UnicodeSet.MIN_VALUE, invalid[i]); 869 errln("UnicodeSet.remove(int start, int end) was suppose to give " 870 + "an exception for an end invalid input of " 871 + invalid[i]); 872 } catch (Exception e){} 873 } 874 875 // Tests when "if (start <= end)" is false 876 try{ 877 us.remove(UnicodeSet.MIN_VALUE+1, UnicodeSet.MIN_VALUE); 878 } catch(Exception e){ 879 errln("UnicodeSet.remove(int start, int end) was not suppose to give " 880 + "an exception."); 881 } 882 883 try{ 884 us.remove(UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1); 885 } catch(Exception e){ 886 errln("UnicodeSet.remove(int start, int end) was not suppose to give " 887 + "an exception."); 888 } 889 } 890 891 /* Tests the method 892 * public UnicodeSet complement(int start, int end) 893 */ 894 public void TestComplement_int_int(){ 895 UnicodeSet us = new UnicodeSet(); 896 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 897 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 898 899 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true 900 for(int i=0; i < invalid.length; i++){ 901 try{ 902 us.complement(invalid[i], UnicodeSet.MAX_VALUE); 903 errln("UnicodeSet.complement(int start, int end) was suppose to give " 904 + "an exception for an start invalid input of " 905 + invalid[i]); 906 } catch (Exception e){} 907 } 908 909 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true 910 for(int i=0; i < invalid.length; i++){ 911 try{ 912 us.complement(UnicodeSet.MIN_VALUE, invalid[i]); 913 errln("UnicodeSet.complement(int start, int end) was suppose to give " 914 + "an exception for an end invalid input of " 915 + invalid[i]); 916 } catch (Exception e){} 917 } 918 919 // Tests when "if (start <= end)" is false 920 try{ 921 us.complement(UnicodeSet.MIN_VALUE+1, UnicodeSet.MIN_VALUE); 922 } catch(Exception e){ 923 errln("UnicodeSet.complement(int start, int end) was not suppose to give " 924 + "an exception."); 925 } 926 927 try{ 928 us.complement(UnicodeSet.MAX_VALUE, UnicodeSet.MAX_VALUE-1); 929 } catch(Exception e){ 930 errln("UnicodeSet.complement(int start, int end) was not suppose to give " 931 + "an exception."); 932 } 933 } 934 935 /* Tests the method 936 * public final UnicodeSet complement(String s) 937 */ 938 public void TestComplement_String(){ 939 // Tests when "if (cp < 0)" is false 940 UnicodeSet us = new UnicodeSet(); 941 us.add("dummy"); 942 try{ 943 us.complement("dummy"); 944 } catch (Exception e){ 945 errln("UnicodeSet.complement(String s) was not suppose to give " 946 + "an exception for 'dummy'."); 947 } 948 949 // Tests when "if (strings.contains(s))" is true 950 us = new UnicodeSet(); 951 us.add("\uDC11"); 952 try{ 953 us.complement("\uDC11"); 954 } catch (Exception e){ 955 errln("UnicodeSet.complement(String s) was not suppose to give " 956 + "an exception for '\uDC11'."); 957 } 958 } 959 960 /* Tests the method 961 * public boolean contains(int c) 962 */ 963 public void TestContains_int(){ 964 UnicodeSet us = new UnicodeSet(); 965 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 966 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 967 968 // Tests when "if (c < MIN_VALUE || c > MAX_VALUE)" is true 969 for(int i=0; i < invalid.length; i++){ 970 try{ 971 us.contains(invalid[i]); 972 errln("UnicodeSet.contains(int c) was suppose to give " 973 + "an exception for an start invalid input of " 974 + invalid[i]); 975 } catch (Exception e){} 976 } 977 } 978 979 /* Tests the method 980 * public boolean contains(int start, int end) 981 */ 982 public void TestContains_int_int(){ 983 UnicodeSet us = new UnicodeSet(); 984 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 985 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 986 987 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true 988 for(int i=0; i < invalid.length; i++){ 989 try{ 990 us.contains(invalid[i], UnicodeSet.MAX_VALUE); 991 errln("UnicodeSet.contains(int start, int end) was suppose to give " 992 + "an exception for an start invalid input of " 993 + invalid[i]); 994 } catch (Exception e){} 995 } 996 997 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true 998 for(int i=0; i < invalid.length; i++){ 999 try{ 1000 us.contains(UnicodeSet.MIN_VALUE, invalid[i]); 1001 errln("UnicodeSet.contains(int start, int end) was suppose to give " 1002 + "an exception for an end invalid input of " 1003 + invalid[i]); 1004 } catch (Exception e){} 1005 } 1006 } 1007 1008 /* Tests the method 1009 * public String getRegexEquivalent() 1010 */ 1011 public void TestGetRegexEquivalent(){ 1012 UnicodeSet us = new UnicodeSet(); 1013 String res = us.getRegexEquivalent(); 1014 if(!(res.equals("[]"))) 1015 errln("UnicodeSet.getRegexEquivalent is suppose to return '[]' " + 1016 "but got " + res); 1017 } 1018 1019 /* Tests the method 1020 * public boolean containsNone(int start, int end) 1021 */ 1022 public void TestContainsNone(){ 1023 UnicodeSet us = new UnicodeSet(); 1024 int[] invalid = {UnicodeSet.MIN_VALUE-1, UnicodeSet.MIN_VALUE-2, 1025 UnicodeSet.MAX_VALUE+1, UnicodeSet.MAX_VALUE+2}; 1026 1027 // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true 1028 for(int i=0; i < invalid.length; i++){ 1029 try{ 1030 us.containsNone(invalid[i], UnicodeSet.MAX_VALUE); 1031 errln("UnicodeSet.containsNoneint start, int end) was suppose to give " 1032 + "an exception for an start invalid input of " 1033 + invalid[i]); 1034 } catch (Exception e){} 1035 } 1036 1037 // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true 1038 for(int i=0; i < invalid.length; i++){ 1039 try{ 1040 us.containsNone(UnicodeSet.MIN_VALUE, invalid[i]); 1041 errln("UnicodeSet.containsNone(int start, int end) was suppose to give " 1042 + "an exception for an end invalid input of " 1043 + invalid[i]); 1044 } catch (Exception e){} 1045 } 1046 1047 // Tests when "if (start < list[++i])" is false 1048 try{ 1049 us.add(0); 1050 us.containsNone(1, 2); // 1 > 0 1051 } catch (Exception e){ 1052 errln("UnicodeSet.containsNone(int start, int end) was not suppose to give " + 1053 "an exception."); 1054 } 1055 } 1056}