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