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