]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/util/StringTokenizerTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / util / StringTokenizerTest.java
1 /*\r
2 *******************************************************************************\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and    *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.dev.test.util;\r
9 \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
15 \r
16 /**\r
17 * Testing class for StringTokenizer class\r
18 * @author Syn Wee Quek\r
19 * @since oct 26 2002\r
20 */\r
21 public final class StringTokenizerTest extends TestFmwk\r
22\r
23       // constructor ===================================================\r
24   \r
25       /**\r
26       * Constructor\r
27       */\r
28       public StringTokenizerTest()\r
29       {\r
30       }\r
31   \r
32       // public methods --------------------------------------------------------\r
33     \r
34     /**\r
35      * Testing constructors\r
36      */\r
37     public void TestConstructors()\r
38     {\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
46                                                                 false);\r
47         UnicodeSet delimiterset = new UnicodeSet("[" + delimiter + "]", false);\r
48         StringTokenizer stdelimiterset = new StringTokenizer(str, delimiterset);\r
49         StringTokenizer stdelimitersetreturn = new StringTokenizer(str, \r
50                                                                 delimiterset,\r
51                                                                 false);\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
59             }\r
60         }\r
61         \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
67         \r
68         String expected1[] = {"this", "\t", "is", "\n", "a", "\r", "string", "\f",\r
69                             "testing", "\t", "StringTokenizer", "\n",\r
70                             "constructors!"};\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
77             }\r
78         }\r
79                             \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
85         \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
91         }\r
92         \r
93         delimiter = "";\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
99         \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
105         }\r
106         \r
107         try {\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
112         }\r
113         try {\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
118         }\r
119         try {\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
124         }\r
125         try {\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
130         }\r
131         try {\r
132             stdelimitersetreturn = new StringTokenizer(null, delimiterset,\r
133                                                        false);\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
137         }\r
138     }\r
139     \r
140     /**\r
141      * Testing supplementary\r
142      */\r
143     public void TestSupplementary()\r
144     {\r
145         String str = "bmp string \ud800 with a unmatched surrogate character";\r
146         String delimiter = "\ud800\udc00";\r
147         String expected[] = {str};\r
148             \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
152         }\r
153         if (tokenizer.hasMoreElements()) {\r
154             errln("Number of tokens exceeded expected");\r
155         }\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
160         int i = 0;\r
161         while (tokenizer.hasMoreElements()) {\r
162             if (!tokenizer.nextElement().equals(expected1[i ++])) {\r
163                 errln("Error parsing \"" + Utility.hex(str) + "\"");\r
164             }\r
165         }\r
166         if (tokenizer.hasMoreElements()) {\r
167             errln("Number of tokens exceeded expected");\r
168         }\r
169         \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
176         }\r
177         if (tokenizer.hasMoreElements()) {\r
178             errln("Number of tokens exceeded expected");\r
179         }\r
180   \r
181         delimiter = "\ud800\udc00";\r
182         String expected3[] = {"string ", " with supplementary character"};\r
183         tokenizer = new StringTokenizer(str, delimiter);\r
184         i = 0;\r
185         while (tokenizer.hasMoreElements()) {\r
186             if (!tokenizer.nextElement().equals(expected3[i ++])) {\r
187                 errln("Error parsing \"" + Utility.hex(str) + "\"");\r
188             }\r
189         }\r
190         if (tokenizer.hasMoreElements()) {\r
191             errln("Number of tokens exceeded expected");\r
192         }\r
193         \r
194         str = "\ud800 \ud800\udc00 \ud800 \ud800\udc00";\r
195         delimiter = "\ud800";\r
196         String expected4[] = {" \ud800\udc00 ", " \ud800\udc00"};\r
197         i = 0;\r
198         while (tokenizer.hasMoreElements()) {\r
199             if (!tokenizer.nextElement().equals(expected4[i ++])) {\r
200                 errln("Error parsing \"" + Utility.hex(str) + "\"");\r
201             }\r
202         }\r
203         if (tokenizer.hasMoreElements()) {\r
204             errln("Number of tokens exceeded expected");\r
205         }\r
206         \r
207         delimiter = "\ud800\udc00";\r
208         String expected5[] = {"\ud800 ", " \ud800 "};\r
209         i = 0;\r
210         while (tokenizer.hasMoreElements()) {\r
211             if (!tokenizer.nextElement().equals(expected5[i ++])) {\r
212                 errln("Error parsing \"" + Utility.hex(str) + "\"");\r
213             }\r
214         }\r
215         if (tokenizer.hasMoreElements()) {\r
216             errln("Number of tokens exceeded expected");\r
217         }\r
218     }\r
219   \r
220       /**\r
221       * Testing next api\r
222       */\r
223       public void TestNextNonDelimiterToken()\r
224       {\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
227                              ",", "8\n"};\r
228         String delimiter = " ";\r
229                            \r
230         StringTokenizer tokenizer = new StringTokenizer(str, delimiter);\r
231         int currtoken = 0;\r
232         while (tokenizer.hasMoreElements()) {\r
233             if (!tokenizer.nextElement().equals(expected[currtoken])) {\r
234                 errln("Error token mismatch, expected " + expected[currtoken]);\r
235             }\r
236             currtoken ++;\r
237         }\r
238 \r
239         if (currtoken != expected.length) {\r
240             errln("Didn't get correct number of tokens");\r
241         }\r
242         \r
243         tokenizer = new StringTokenizer("", delimiter);\r
244         if (tokenizer.hasMoreElements()) {\r
245             errln("Empty string should not have any tokens");\r
246         }\r
247         try {\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
252         }\r
253         \r
254         tokenizer = new StringTokenizer(", ,", ", ");\r
255         if (tokenizer.hasMoreElements()) {\r
256             errln("String with only delimiters should not have any tokens");\r
257         }\r
258         try {\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
263         }\r
264 \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
268         }\r
269         if (!tokenizer.nextElement().equals("q")) {\r
270             errln("String that does not begin with delimiters should have some tokens");\r
271         } \r
272         try {\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
277         }\r
278 \r
279         try {\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
284         }\r
285 \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
289         }\r
290     }\r
291 \r
292     /**\r
293      * Test java compatibility, except we support surrogates.\r
294      */\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
298 \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
301         int n = 0;\r
302         while (jt.hasMoreTokens() && it.hasMoreTokens()) {\r
303             assertEquals("[" + String.valueOf(n++) + "]", jt.nextToken(), it.nextToken());\r
304         }\r
305         assertFalse("java tokenizer has no more tokens", jt.hasMoreTokens());\r
306         assertFalse("icu tokenizer has no more tokens", it.hasMoreTokens());\r
307 \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
317     }\r
318 \r
319     /**\r
320     * Testing next api\r
321     */\r
322     public void TestNextDelimiterToken()\r
323     {\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
327                              ",", "        ", "8\n"};\r
328         String delimiter = " ";\r
329                            \r
330         StringTokenizer tokenizer = new StringTokenizer(str, delimiter, true, true);\r
331 \r
332         int currtoken = 0;\r
333         while (tokenizer.hasMoreElements()) {\r
334             if (!tokenizer.nextElement().equals(expected[currtoken])) {\r
335                 errln("Error token mismatch, expected " + expected[currtoken]);\r
336             }\r
337             currtoken ++;\r
338         }\r
339 \r
340         if (currtoken != expected.length) {\r
341             errln("Didn't get correct number of tokens");\r
342         }\r
343         \r
344         tokenizer = new StringTokenizer("", delimiter, true);\r
345         if (tokenizer.hasMoreElements()) {\r
346             errln("Empty string should not have any tokens");\r
347         }\r
348         try {\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
353         }\r
354         \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
358         }\r
359         if (!tokenizer.nextElement().equals(", ,")) {\r
360             errln("String with only delimiters should return itself when delimiter is treated as tokens");\r
361         }\r
362 \r
363         tokenizer = new StringTokenizer("q, ,", ", ", true, true);\r
364         \r
365         if (!tokenizer.hasMoreElements()) {\r
366             errln("String should have some tokens");\r
367         }\r
368         if (!tokenizer.nextElement().equals("q") \r
369             || !tokenizer.nextElement().equals(", ,")) {\r
370             errln("String tokens do not match expected results");\r
371         } \r
372 \r
373         try {\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
378         }\r
379 \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
383         }\r
384     }\r
385     \r
386     /**\r
387      * Testing count tokens\r
388      */\r
389     public void TestCountTokens()\r
390     {\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
402         }\r
403         if (streturn.countTokens() != expectedreturn.length) {\r
404             errln("CountTokens failed for delimiter tokens");\r
405         }\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
410             }\r
411         }\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
416             }\r
417         }    \r
418     }\r
419         \r
420     /**\r
421      * Next token with new delimiters\r
422      */\r
423     public void TestNextNewDelimiters()\r
424     {\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
432                               };\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
439                 }\r
440                 if (st.countTokens() != expected[j].length - i) {            \r
441                     errln("countTokens() after nextToken() with delimiters error"\r
442                           + i + " " + j);\r
443                 }\r
444             }\r
445         }    \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
452                                                             expected1[i])) {\r
453                 errln("nextToken() with delimiters error " + i);\r
454             }\r
455         }\r
456     }\r
457     \r
458     public void TestBug4423()\r
459     {\r
460         // bug 4423:  a bad interaction between countTokens() and hasMoreTokens().\r
461         //\r
462         String s1 = "This is a test";\r
463         StringTokenizer tzr = new StringTokenizer(s1);\r
464         int  tokenCount = 0;\r
465         \r
466         int t = tzr.countTokens();\r
467         if (t!= 4) {\r
468             errln("tzr.countTokens() returned " + t + ".  Expected 4");\r
469         }\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
474             }\r
475             tokenCount++;\r
476         }\r
477         if (tokenCount != 4) {\r
478             errln("Incorrect number of tokens found = " + tokenCount);\r
479         }\r
480         \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
483         String s2 = "";\r
484         for (int i=1; i<250; i++) {\r
485             s2 = s2 + " " + i;\r
486             StringTokenizer tzb = new StringTokenizer(s2);\r
487             int t2 = tzb.countTokens();\r
488             if (t2 != i) {\r
489                 errln("tzb.countTokens() returned " + t + ".  Expected " + i);\r
490                 break;\r
491             }\r
492             int j = 0;\r
493             while (tzb.hasMoreTokens()) {\r
494                 String tok = tzb.nextToken();\r
495                 j++;\r
496                 if (tok.equals(Integer.toString(j)) == false) {\r
497                     errln("Wrong token string.  Expected \"" + j + "\", got \""\r
498                             + tok + "\".");\r
499                     break;\r
500                 }\r
501             }\r
502             if (j != i) {\r
503                 errln("Wrong number of tokens.  Expected " + i + ".  Got " + j\r
504                         + ".");\r
505                 break;\r
506             }\r
507         }\r
508         \r
509     }\r
510 \r
511     public void TestCountTokensNoCoalesce() {\r
512         // jitterbug 5207\r
513         String str = "\"\"";\r
514         String del = "\"";\r
515         StringTokenizer st = new StringTokenizer(str, del, true);\r
516         int count = 0;\r
517         while (st.hasMoreTokens()) {\r
518             String t = st.nextToken();\r
519             logln("[" + count + "] '" + t + "'");\r
520             ++count;\r
521         }\r
522         st = new StringTokenizer(str, del, true);\r
523         int ncount = st.countTokens();\r
524         int xcount = 0;\r
525         while (st.hasMoreTokens()) {\r
526             String t = st.nextToken();\r
527             logln("[" + xcount + "] '" + t + "'");\r
528             ++xcount;\r
529         }\r
530         if (count != ncount || count != xcount) {\r
531             errln("inconsistent counts " + count + ", " + ncount + ", " + xcount);\r
532         }\r
533     }\r
534 \r
535     public static void main(String[] arg)\r
536     {\r
537         try\r
538         {\r
539             StringTokenizerTest test = new StringTokenizerTest();\r
540             test.run(arg);\r
541             // test.TestCaseCompare();\r
542         }\r
543         catch (Exception e)\r
544         {\r
545             e.printStackTrace();\r
546         }\r
547     }\r
548     \r
549     /* Tests the method\r
550      *      public StringBuffer _generatePattern(StringBuffer result, boolean escapeUnprintable)\r
551      */\r
552     public void Test_GeneratePattern(){\r
553         UnicodeSet us = new UnicodeSet();\r
554         StringBuffer sb = new StringBuffer();\r
555         try{\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
563         }\r
564         \r
565         try{\r
566             us._generatePattern(null, true);\r
567             errln("UnicodeSet._generatePattern is suppose to return an exception.");\r
568         } catch(Exception e){}\r
569     }\r
570     \r
571     /* Tests the method\r
572      *      public int matches(Replaceable text, int[] offset, int limit, boolean incremental)\r
573      */\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
578         us.add("dummy");\r
579         \r
580         int[] offset = {0};\r
581         int limit = 0;\r
582         \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
586         }\r
587         \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
591         }\r
592         \r
593         // Tests when "int maxLen = forward ? limit-offset[0] : offset[0]-limit;" is true and false\r
594         try{\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
601         }\r
602         \r
603         // TODO: Tests when "if (forward && length < highWaterLength)" is true\r
604     }\r
605     \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
609      */\r
610     public void TestMatchRest(){\r
611         // TODO: Tests when "if (maxLen > slen) maxLen = slen;" is true and false\r
612     }\r
613     \r
614     /* Tests the method\r
615      *      public int matchesAt(CharSequence text, int offset)\r
616      */\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
621         \r
622         us.matchesAt((CharSequence)"dummy", 0);\r
623         us.add("dummy2");                           // Add another item\r
624         \r
625         us.matchesAt((CharSequence)"yummy", 0);     //charAt(0) >\r
626         us.matchesAt((CharSequence)"amy", 0);       //charAt(0) <\r
627         \r
628         UnicodeSet us1 = new UnicodeSet(0,100000);  // Increase the set\r
629         us1.matchesAt((CharSequence)"dummy", 0);\r
630     }\r
631     \r
632     /* Tests the method\r
633      *      public int indexOf(int c)\r
634      */\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
642         \r
643         for(int i=0; i < invalid.length; i++){\r
644             try{\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
649         }\r
650         \r
651         for(int i=0; i < valid.length; i++){\r
652             try{\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
657             }\r
658         }\r
659     }\r
660     \r
661     /* Tests the method\r
662      *      public int charAt(int index)\r
663      */\r
664     public void TestCharAt(){\r
665         UnicodeSet us = new UnicodeSet();\r
666         \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
673             }\r
674         }\r
675     }\r
676     \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
680      */\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
685          \r
686          // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true\r
687          for(int i=0; i < invalid.length; i++){\r
688              try{\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
692                          + invalid[i]);\r
693              } catch (Exception e){}\r
694          }\r
695          \r
696          // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true\r
697          for(int i=0; i < invalid.length; i++){\r
698              try{\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
702                          + invalid[i]);\r
703              } catch (Exception e){}\r
704          }\r
705          \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
711          \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
716      }\r
717      \r
718      /* Tests the method\r
719       *     private final UnicodeSet add_unchecked(int c)\r
720       * from public final UnicodeSet add(int c)\r
721       */\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
726          \r
727          // Tests when "if (c < MIN_VALUE || c > MAX_VALUE)" is true\r
728          for(int i=0; i < invalid.length; i++){\r
729              try{\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
733                          + invalid[i]);\r
734              } catch (Exception e){}\r
735          }\r
736          \r
737          // Tests when "if (c == MAX_VALUE)" is true\r
738          // TODO: Check comment in UnicodeSet.java\r
739      }\r
740      \r
741      /* Tests the method\r
742       *     private static int getSingleCP(String s)\r
743       * from public final boolean contains(String s)\r
744       */\r
745      public void TestGetSingleCP(){\r
746          UnicodeSet us = new UnicodeSet();\r
747          // Tests when "if (s.length() < 1)" is true\r
748          try{\r
749              us.contains("");\r
750              errln("UnicodeSet.getSingleCP is suppose to give an exception for " +\r
751                      "an empty string.");\r
752          } catch (Exception e){}\r
753          \r
754          try{\r
755              us.contains((String)null);\r
756              errln("UnicodeSet.getSingleCP is suppose to give an exception for " +\r
757              "a null string.");\r
758          } catch (Exception e){}\r
759          \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
763              try{\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
767                      "a null string.");\r
768              }\r
769          }\r
770      }\r
771      \r
772      /* Tests the method\r
773       *     public final UnicodeSet removeAllStrings()\r
774       */\r
775      public void TestRemoveAllString(){\r
776          // Tests when "if (strings.size() != 0)" is false\r
777          UnicodeSet us = new UnicodeSet();\r
778          try{\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
783          }\r
784      }\r
785      \r
786      /* Tests the method\r
787       *     public UnicodeSet retain(int start, int end)\r
788       */\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
793           \r
794           // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true\r
795           for(int i=0; i < invalid.length; i++){\r
796               try{\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
800                           + invalid[i]);\r
801               } catch (Exception e){}\r
802           }\r
803           \r
804           // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true\r
805           for(int i=0; i < invalid.length; i++){\r
806               try{\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
810                           + invalid[i]);\r
811               } catch (Exception e){}\r
812           }\r
813           \r
814           // Tests when "if (start <= end)" is false\r
815           try{\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
820           }\r
821           \r
822           try{\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
827           }\r
828       }\r
829       \r
830       /* Tests the method\r
831        *        public final UnicodeSet retain(String s)\r
832        */\r
833       public void TestRetain_String(){\r
834           // Tests when "if (isIn && size() == 1)" is true\r
835           UnicodeSet us = new UnicodeSet();\r
836           us.add("dummy");\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
840           }\r
841       }\r
842       \r
843       /* Tests the method\r
844        *     public UnicodeSet remove(int start, int end)\r
845        */\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
850            \r
851            // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true\r
852            for(int i=0; i < invalid.length; i++){\r
853                try{\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
857                            + invalid[i]);\r
858                } catch (Exception e){}\r
859            }\r
860            \r
861            // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true\r
862            for(int i=0; i < invalid.length; i++){\r
863                try{\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
867                            + invalid[i]);\r
868                } catch (Exception e){}\r
869            }\r
870            \r
871            // Tests when "if (start <= end)" is false\r
872            try{\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
877            }\r
878            \r
879            try{\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
884            }\r
885        }\r
886        \r
887        /* Tests the method\r
888         *     public UnicodeSet complement(int start, int end)\r
889         */\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
894             \r
895             // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true\r
896             for(int i=0; i < invalid.length; i++){\r
897                 try{\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
901                             + invalid[i]);\r
902                 } catch (Exception e){}\r
903             }\r
904             \r
905             // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true\r
906             for(int i=0; i < invalid.length; i++){\r
907                 try{\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
911                             + invalid[i]);\r
912                 } catch (Exception e){}\r
913             }\r
914             \r
915             // Tests when "if (start <= end)" is false\r
916             try{\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
921             }\r
922             \r
923             try{\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
928             }\r
929         }\r
930         \r
931         /* Tests the method\r
932          *      public final UnicodeSet complement(String s)\r
933          */\r
934         public void TestComplement_String(){\r
935             // Tests when "if (cp < 0)" is false\r
936             UnicodeSet us = new UnicodeSet();\r
937             us.add("dummy");\r
938             try{\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
943             }\r
944             \r
945             // Tests when "if (strings.contains(s))" is true\r
946             us = new UnicodeSet();\r
947             us.add("\uDC11");\r
948             try{\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
953             }\r
954         }\r
955         \r
956         /* Tests the method\r
957          *      public boolean contains(int c)\r
958          */\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
963             \r
964             // Tests when "if (c < MIN_VALUE || c > MAX_VALUE)" is true\r
965             for(int i=0; i < invalid.length; i++){\r
966                 try{\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
970                             + invalid[i]);\r
971                 } catch (Exception e){}\r
972             }\r
973         }\r
974         \r
975         /* Tests the method\r
976          *     public boolean contains(int start, int end)\r
977          */\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
982              \r
983              // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true\r
984              for(int i=0; i < invalid.length; i++){\r
985                  try{\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
989                              + invalid[i]);\r
990                  } catch (Exception e){}\r
991              }\r
992              \r
993              // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true\r
994              for(int i=0; i < invalid.length; i++){\r
995                  try{\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
999                              + invalid[i]);\r
1000                  } catch (Exception e){}\r
1001              }\r
1002          }\r
1003          \r
1004          /* Tests the method\r
1005           *     public String getRegexEquivalent()\r
1006           */\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
1013          }\r
1014          \r
1015          /* Tests the method\r
1016           *     public boolean containsNone(int start, int end)\r
1017           */\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
1022               \r
1023               // Tests when "if (start < MIN_VALUE || start > MAX_VALUE)" is true\r
1024               for(int i=0; i < invalid.length; i++){\r
1025                   try{\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
1029                               + invalid[i]);\r
1030                   } catch (Exception e){}\r
1031               }\r
1032               \r
1033               // Tests when "if (end < MIN_VALUE || end > MAX_VALUE)" is true\r
1034               for(int i=0; i < invalid.length; i++){\r
1035                   try{\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
1039                               + invalid[i]);\r
1040                   } catch (Exception e){}\r
1041               }\r
1042               \r
1043               // Tests when "if (start < list[++i])" is false\r
1044               try{\r
1045                   us.add(0);\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
1049                           "an exception.");\r
1050               }\r
1051           }\r
1052 }