Home | History | Annotate | Download | only in intltest
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 1997-2009, International Business Machines Corporation and
      4  * others. All Rights Reserved.
      5  ********************************************************************/
      6 //===============================================================================
      7 //
      8 // File apitest.cpp
      9 //
     10 //
     11 //
     12 // Created by: Helena Shih
     13 //
     14 // Modification History:
     15 //
     16 //  Date         Name          Description
     17 //  2/5/97      aliu        Added streamIn and streamOut methods.  Added
     18 //                          constructor which reads RuleBasedCollator object from
     19 //                          a binary file.  Added writeToFile method which streams
     20 //                          RuleBasedCollator out to a binary file.  The streamIn
     21 //                          and streamOut methods use istream and ostream objects
     22 //                          in binary mode.
     23 //  6/30/97     helena      Added tests for CollationElementIterator::setText, getOffset
     24 //                          setOffset and DecompositionIterator::getOffset, setOffset.
     25 //                          DecompositionIterator is made public so add class scope
     26 //                          testing.
     27 //  02/10/98    damiba      Added test for compare(UnicodeString&, UnicodeString&, int32_t)
     28 //===============================================================================
     29 
     30 #include "unicode/utypes.h"
     31 
     32 #if !UCONFIG_NO_COLLATION
     33 
     34 #include "unicode/localpointer.h"
     35 #include "unicode/coll.h"
     36 #include "unicode/tblcoll.h"
     37 #include "unicode/coleitr.h"
     38 #include "unicode/sortkey.h"
     39 #include "apicoll.h"
     40 #include "unicode/chariter.h"
     41 #include "unicode/schriter.h"
     42 #include "unicode/ustring.h"
     43 #include "unicode/ucol.h"
     44 
     45 #include "sfwdchit.h"
     46 #include "cmemory.h"
     47 #include <stdlib.h>
     48 
     49 void
     50 CollationAPITest::doAssert(UBool condition, const char *message)
     51 {
     52     if (!condition) {
     53         errln(UnicodeString("ERROR : ") + message);
     54     }
     55 }
     56 
     57 #ifdef U_USE_COLLATION_OBSOLETE_2_6
     58 /*
     59  * Test Collator::createInstance(... version...) for some locale. Called by TestProperty().
     60  */
     61 static void
     62 TestOpenVersion(IntlTest &test, const Locale &locale) {
     63     UVersionInfo version1, version2;
     64     Collator *collator1, *collator2;
     65     UErrorCode errorCode;
     66 
     67     errorCode=U_ZERO_ERROR;
     68     collator1=Collator::createInstance(locale, errorCode);
     69     if(U_SUCCESS(errorCode)) {
     70         /* get the current version */
     71         collator1->getVersion(version1);
     72         delete collator1;
     73 
     74         /* try to get that same version again */
     75         collator2=Collator::createInstance(locale, version1, errorCode);
     76         if(U_SUCCESS(errorCode)) {
     77             collator2->getVersion(version2);
     78             if(0!=uprv_memcmp(version1, version2, sizeof(UVersionInfo))) {
     79                 test.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) returns a different collator\n", locale.getName(), locale.getName());
     80             }
     81             delete collator2;
     82         } else {
     83             test.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) fails: %s\n", locale.getName(), locale.getName(), u_errorName(errorCode));
     84         }
     85     }
     86 }
     87 #endif
     88 
     89 // Collator Class Properties
     90 // ctor, dtor, createInstance, compare, getStrength/setStrength
     91 // getDecomposition/setDecomposition, getDisplayName
     92 void
     93 CollationAPITest::TestProperty(/* char* par */)
     94 {
     95     UErrorCode success = U_ZERO_ERROR;
     96     Collator *col = 0;
     97     /*
     98      * Expected version of the English collator.
     99      * Currently, the major/minor version numbers change when the builder code
    100      * changes,
    101      * number 2 is from the tailoring data version and
    102      * number 3 is the UCA version.
    103      * This changes with every UCA version change, and the expected value
    104      * needs to be adjusted.
    105      * Same in cintltst/capitst.c.
    106      */
    107     UVersionInfo currVersionArray = {0x31, 0xC0, 0x00, 0x2A};
    108     UVersionInfo versionArray;
    109     int i = 0;
    110 
    111     logln("The property tests begin : ");
    112     logln("Test ctors : ");
    113     col = Collator::createInstance(Locale::getEnglish(), success);
    114     if (U_FAILURE(success)){
    115         errcheckln(success, "Default Collator creation failed. - %s", u_errorName(success));
    116         return;
    117     }
    118 
    119     StringEnumeration* kwEnum = col->getKeywordValuesForLocale("", Locale::getEnglish(),true,success);
    120     if (U_FAILURE(success)){
    121         errcheckln(success, "Get Keyword Values for Locale failed. - %s", u_errorName(success));
    122         return;
    123     }
    124     delete kwEnum;
    125 
    126     col->getVersion(versionArray);
    127     for (i=0; i<4; ++i) {
    128       if (versionArray[i] != currVersionArray[i]) {
    129         errln("Testing Collator::getVersion() - unexpected result: %02x.%02x.%02x.%02x",
    130             versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
    131         break;
    132       }
    133     }
    134 
    135     doAssert((col->compare("ab", "abc") == Collator::LESS), "ab < abc comparison failed");
    136     doAssert((col->compare("ab", "AB") == Collator::LESS), "ab < AB comparison failed");
    137     doAssert((col->compare("blackbird", "black-bird") == Collator::GREATER), "black-bird > blackbird comparison failed");
    138     doAssert((col->compare("black bird", "black-bird") == Collator::LESS), "black bird > black-bird comparison failed");
    139     doAssert((col->compare("Hello", "hello") == Collator::GREATER), "Hello > hello comparison failed");
    140     doAssert((col->compare("","",success) == Collator::EQUAL), "Comparison between empty strings failed");
    141 
    142     doAssert((col->compareUTF8("\x61\x62\xc3\xa4", "\x61\x62\xc3\x9f", success) == UCOL_LESS), "ab a-umlaut < ab sharp-s UTF-8 comparison failed");
    143     success = U_ZERO_ERROR;
    144     {
    145         UnicodeString abau=UNICODE_STRING_SIMPLE("\\x61\\x62\\xe4").unescape();
    146         UnicodeString abss=UNICODE_STRING_SIMPLE("\\x61\\x62\\xdf").unescape();
    147         UCharIterator abauIter, abssIter;
    148         uiter_setReplaceable(&abauIter, &abau);
    149         uiter_setReplaceable(&abssIter, &abss);
    150         doAssert((col->compare(abauIter, abssIter, success) == UCOL_LESS), "ab a-umlaut < ab sharp-s UCharIterator comparison failed");
    151         success = U_ZERO_ERROR;
    152     }
    153 
    154     /*start of update [Bertrand A. D. 02/10/98]*/
    155     doAssert((col->compare("ab", "abc", 2) == Collator::EQUAL), "ab = abc with length 2 comparison failed");
    156     doAssert((col->compare("ab", "AB", 2) == Collator::LESS), "ab < AB  with length 2 comparison failed");
    157     doAssert((col->compare("ab", "Aa", 1) == Collator::LESS), "ab < Aa  with length 1 comparison failed");
    158     doAssert((col->compare("ab", "Aa", 2) == Collator::GREATER), "ab > Aa  with length 2 comparison failed");
    159     doAssert((col->compare("black-bird", "blackbird", 5) == Collator::EQUAL), "black-bird = blackbird with length of 5 comparison failed");
    160     doAssert((col->compare("black bird", "black-bird", 10) == Collator::LESS), "black bird < black-bird with length 10 comparison failed");
    161     doAssert((col->compare("Hello", "hello", 5) == Collator::GREATER), "Hello > hello with length 5 comparison failed");
    162     /*end of update [Bertrand A. D. 02/10/98]*/
    163 
    164 
    165     logln("Test ctors ends.");
    166     logln("testing Collator::getStrength() method ...");
    167     doAssert((col->getStrength() == Collator::TERTIARY), "collation object has the wrong strength");
    168     doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
    169 
    170 
    171     logln("testing Collator::setStrength() method ...");
    172     col->setStrength(Collator::SECONDARY);
    173     doAssert((col->getStrength() != Collator::TERTIARY), "collation object's strength is secondary difference");
    174     doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
    175     doAssert((col->getStrength() == Collator::SECONDARY), "collation object has the wrong strength");
    176 
    177     UnicodeString name;
    178 
    179     logln("Get display name for the US English collation in German : ");
    180     logln(Collator::getDisplayName(Locale::getUS(), Locale::getGerman(), name));
    181     doAssert((name == UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed");
    182 
    183     logln("Get display name for the US English collation in English : ");
    184     logln(Collator::getDisplayName(Locale::getUS(), Locale::getEnglish(), name));
    185     doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed");
    186 #if 0
    187     // weiv : this test is bogus if we're running on any machine that has different default locale than English.
    188     // Therefore, it is banned!
    189     logln("Get display name for the US English in default locale language : ");
    190     logln(Collator::getDisplayName(Locale::US, name));
    191     doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine");
    192 #endif
    193     delete col; col = 0;
    194 
    195 
    196     // BEGIN android-changed
    197     // To save space, Android does not include the collation tailoring rules.
    198     // We skip the tailing tests for collations.
    199     /*
    200     RuleBasedCollator *rcol = (RuleBasedCollator *)Collator::createInstance("da_DK",
    201                                                                             success);
    202     doAssert(rcol->getRules().length() != 0, "da_DK rules does not have length 0");
    203     delete rcol;
    204     */
    205     // END android-changed
    206 
    207     col = Collator::createInstance(Locale::getFrench(), success);
    208     if (U_FAILURE(success))
    209     {
    210         errln("Creating French collation failed.");
    211         return;
    212     }
    213 
    214     col->setStrength(Collator::PRIMARY);
    215     logln("testing Collator::getStrength() method again ...");
    216     doAssert((col->getStrength() != Collator::TERTIARY), "collation object has the wrong strength");
    217     doAssert((col->getStrength() == Collator::PRIMARY), "collation object's strength is not primary difference");
    218 
    219     logln("testing French Collator::setStrength() method ...");
    220     col->setStrength(Collator::TERTIARY);
    221     doAssert((col->getStrength() == Collator::TERTIARY), "collation object's strength is not tertiary difference");
    222     doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
    223     doAssert((col->getStrength() != Collator::SECONDARY), "collation object's strength is secondary difference");
    224 
    225     logln("Create junk collation: ");
    226     Locale abcd("ab", "CD", "");
    227     success = U_ZERO_ERROR;
    228     Collator *junk = 0;
    229     junk = Collator::createInstance(abcd, success);
    230 
    231     if (U_FAILURE(success))
    232     {
    233         errln("Junk collation creation failed, should at least return default.");
    234         delete col;
    235         return;
    236     }
    237 
    238     delete col;
    239     col = Collator::createInstance(success);
    240     if (U_FAILURE(success))
    241     {
    242         errln("Creating default collator failed.");
    243         delete junk;
    244         return;
    245     }
    246 
    247     doAssert(((RuleBasedCollator *)col)->getRules() == ((RuleBasedCollator *)junk)->getRules(),
    248                "The default collation should be returned.");
    249     Collator *frCol = Collator::createInstance(Locale::getFrance(), success);
    250     if (U_FAILURE(success))
    251     {
    252         errln("Creating French collator failed.");
    253         delete col;
    254         delete junk;
    255         return;
    256     }
    257 
    258     // If the default locale isn't French, the French and non-French collators
    259     // should be different
    260     if (frCol->getLocale(ULOC_ACTUAL_LOCALE, success) != Locale::getFrench()) {
    261         doAssert((*frCol != *junk), "The junk is the same as the French collator.");
    262     }
    263     Collator *aFrCol = frCol->clone();
    264     doAssert((*frCol == *aFrCol), "The cloning of a French collator failed.");
    265     logln("Collator property test ended.");
    266 
    267     delete col;
    268     delete frCol;
    269     delete aFrCol;
    270     delete junk;
    271 
    272 #ifdef U_USE_COLLATION_OBSOLETE_2_6
    273     /* test Collator::createInstance(...version...) */
    274     TestOpenVersion(*this, "");
    275     TestOpenVersion(*this, "da");
    276     TestOpenVersion(*this, "fr");
    277     TestOpenVersion(*this, "ja");
    278 
    279     /* try some bogus version */
    280     versionArray[0]=0;
    281     versionArray[1]=0x99;
    282     versionArray[2]=0xc7;
    283     versionArray[3]=0xfe;
    284     col=Collator::createInstance(Locale(), versionArray, success);
    285     if(U_SUCCESS(success)) {
    286         errln("error: ucol_openVersion(bogus version) succeeded");
    287         delete col;
    288     }
    289 #endif
    290 }
    291 
    292 void
    293 CollationAPITest::TestRuleBasedColl()
    294 {
    295     RuleBasedCollator *col1, *col2, *col3, *col4;
    296     UErrorCode status = U_ZERO_ERROR;
    297 
    298     UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
    299     UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
    300 
    301     col1 = new RuleBasedCollator(ruleset1, status);
    302     if (U_FAILURE(status)) {
    303         errcheckln(status, "RuleBased Collator creation failed. - %s", u_errorName(status));
    304         return;
    305     }
    306     else {
    307         logln("PASS: RuleBased Collator creation passed\n");
    308     }
    309 
    310     status = U_ZERO_ERROR;
    311     col2 = new RuleBasedCollator(ruleset2, status);
    312     if (U_FAILURE(status)) {
    313         errln("RuleBased Collator creation failed.\n");
    314         return;
    315     }
    316     else {
    317         logln("PASS: RuleBased Collator creation passed\n");
    318     }
    319 
    320     status = U_ZERO_ERROR;
    321     Locale locale("aa", "AA");
    322     col3 = (RuleBasedCollator *)Collator::createInstance(locale, status);
    323     if (U_FAILURE(status)) {
    324         errln("Fallback Collator creation failed.: %s\n");
    325         return;
    326     }
    327     else {
    328         logln("PASS: Fallback Collator creation passed\n");
    329     }
    330     delete col3;
    331 
    332     status = U_ZERO_ERROR;
    333     col3 = (RuleBasedCollator *)Collator::createInstance(status);
    334     if (U_FAILURE(status)) {
    335         errln("Default Collator creation failed.: %s\n");
    336         return;
    337     }
    338     else {
    339         logln("PASS: Default Collator creation passed\n");
    340     }
    341 
    342     UnicodeString rule1 = col1->getRules();
    343     UnicodeString rule2 = col2->getRules();
    344     UnicodeString rule3 = col3->getRules();
    345 
    346     doAssert(rule1 != rule2, "Default collator getRules failed");
    347     doAssert(rule2 != rule3, "Default collator getRules failed");
    348     doAssert(rule1 != rule3, "Default collator getRules failed");
    349 
    350     col4 = new RuleBasedCollator(rule2, status);
    351     if (U_FAILURE(status)) {
    352         errln("RuleBased Collator creation failed.\n");
    353         return;
    354     }
    355 
    356     UnicodeString rule4 = col4->getRules();
    357     doAssert(rule2 == rule4, "Default collator getRules failed");
    358     int32_t length4 = 0;
    359     uint8_t *clonedrule4 = col4->cloneRuleData(length4, status);
    360     if (U_FAILURE(status)) {
    361         errln("Cloned rule data failed.\n");
    362         return;
    363     }
    364 
    365  //   free(clonedrule4);     BAD API!!!!
    366     uprv_free(clonedrule4);
    367 
    368 
    369     delete col1;
    370     delete col2;
    371     delete col3;
    372     delete col4;
    373 }
    374 
    375 void
    376 CollationAPITest::TestRules()
    377 {
    378     RuleBasedCollator *coll;
    379     UErrorCode status = U_ZERO_ERROR;
    380     UnicodeString rules;
    381 
    382     coll = (RuleBasedCollator *)Collator::createInstance(Locale::getEnglish(), status);
    383     if (U_FAILURE(status)) {
    384         errcheckln(status, "English Collator creation failed. - %s", u_errorName(status));
    385         return;
    386     }
    387     else {
    388         logln("PASS: RuleBased Collator creation passed\n");
    389     }
    390 
    391     coll->getRules(UCOL_TAILORING_ONLY, rules);
    392     if (rules.length() != 0x0a) {
    393       errln("English tailored rules failed - length is 0x%x expected 0x%x", rules.length(), 0x0e);
    394     }
    395 
    396     coll->getRules(UCOL_FULL_RULES, rules);
    397     if (rules.length() < 0) {
    398         errln("English full rules failed");
    399     }
    400     delete coll;
    401 }
    402 
    403 void
    404 CollationAPITest::TestDecomposition() {
    405   UErrorCode status = U_ZERO_ERROR;
    406   Collator *en_US = Collator::createInstance("en_US", status),
    407     *el_GR = Collator::createInstance("el_GR", status),
    408     *vi_VN = Collator::createInstance("vi_VN", status);
    409 
    410   if (U_FAILURE(status)) {
    411     errcheckln(status, "ERROR: collation creation failed. - %s", u_errorName(status));
    412     return;
    413   }
    414 
    415   /* there is no reason to have canonical decomposition in en_US OR default locale */
    416   if (vi_VN->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON)
    417   {
    418     errln("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
    419   }
    420 
    421   if (el_GR->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON)
    422   {
    423     errln("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
    424   }
    425 
    426   if (en_US->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_OFF)
    427   {
    428     errln("ERROR: en_US collation had canonical decomposition for normalization!\n");
    429   }
    430 
    431   delete en_US;
    432   delete el_GR;
    433   delete vi_VN;
    434 }
    435 
    436 void
    437 CollationAPITest::TestSafeClone() {
    438     static const int CLONETEST_COLLATOR_COUNT = 3;
    439     Collator *someCollators [CLONETEST_COLLATOR_COUNT];
    440     Collator *col;
    441     UErrorCode err = U_ZERO_ERROR;
    442     int index;
    443 
    444     UnicodeString test1("abCda");
    445     UnicodeString test2("abcda");
    446 
    447     /* one default collator & two complex ones */
    448     someCollators[0] = Collator::createInstance("en_US", err);
    449     someCollators[1] = Collator::createInstance("ko", err);
    450     someCollators[2] = Collator::createInstance("ja_JP", err);
    451     if(U_FAILURE(err)) {
    452       errcheckln(err, "Couldn't instantiate collators. Error: %s", u_errorName(err));
    453       delete someCollators[0];
    454       delete someCollators[1];
    455       delete someCollators[2];
    456       return;
    457     }
    458 
    459     /* change orig & clone & make sure they are independent */
    460 
    461     for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++)
    462     {
    463         col = someCollators[index]->safeClone();
    464         if (col == 0) {
    465             errln("SafeClone of collator should not return null\n");
    466             break;
    467         }
    468         col->setStrength(Collator::TERTIARY);
    469         someCollators[index]->setStrength(Collator::PRIMARY);
    470         col->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err);
    471         someCollators[index]->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err);
    472 
    473         doAssert(col->greater(test1, test2), "Result should be \"abCda\" >>> \"abcda\" ");
    474         doAssert(someCollators[index]->equals(test1, test2), "Result should be \"abcda\" == \"abCda\"");
    475         delete col;
    476         delete someCollators[index];
    477     }
    478 }
    479 
    480 void
    481 CollationAPITest::TestHashCode(/* char* par */)
    482 {
    483     logln("hashCode tests begin.");
    484     UErrorCode success = U_ZERO_ERROR;
    485     Collator *col1 = 0;
    486     col1 = Collator::createInstance(Locale::getEnglish(), success);
    487     if (U_FAILURE(success))
    488     {
    489         errcheckln(success, "Default collation creation failed. - %s", u_errorName(success));
    490         return;
    491     }
    492 
    493     Collator *col2 = 0;
    494     Locale dk("da", "DK", "");
    495     col2 = Collator::createInstance(dk, success);
    496     if (U_FAILURE(success))
    497     {
    498         errln("Danish collation creation failed.");
    499         return;
    500     }
    501 
    502     Collator *col3 = 0;
    503     col3 = Collator::createInstance(Locale::getEnglish(), success);
    504     if (U_FAILURE(success))
    505     {
    506         errln("2nd default collation creation failed.");
    507         return;
    508     }
    509 
    510     logln("Collator::hashCode() testing ...");
    511 
    512     doAssert(col1->hashCode() != col2->hashCode(), "Hash test1 result incorrect" );
    513     doAssert(!(col1->hashCode() == col2->hashCode()), "Hash test2 result incorrect" );
    514     doAssert(col1->hashCode() == col3->hashCode(), "Hash result not equal" );
    515 
    516     logln("hashCode tests end.");
    517     delete col1;
    518     delete col2;
    519 
    520     UnicodeString test1("Abcda");
    521     UnicodeString test2("abcda");
    522 
    523     CollationKey sortk1, sortk2, sortk3;
    524     UErrorCode status = U_ZERO_ERROR;
    525 
    526     col3->getCollationKey(test1, sortk1, status);
    527     col3->getCollationKey(test2, sortk2, status);
    528     col3->getCollationKey(test2, sortk3, status);
    529 
    530     doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect");
    531     doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal" );
    532 
    533     delete col3;
    534 }
    535 
    536 //----------------------------------------------------------------------------
    537 // CollationKey -- Tests the CollationKey methods
    538 //
    539 void
    540 CollationAPITest::TestCollationKey(/* char* par */)
    541 {
    542     logln("testing CollationKey begins...");
    543     Collator *col = 0;
    544     UErrorCode success=U_ZERO_ERROR;
    545     col = Collator::createInstance(Locale::getEnglish(), success);
    546     if (U_FAILURE(success))
    547     {
    548         errcheckln(success, "Default collation creation failed. - %s", u_errorName(success));
    549         return;
    550     }
    551     col->setStrength(Collator::TERTIARY);
    552 
    553     CollationKey sortk1, sortk2;
    554     UnicodeString test1("Abcda"), test2("abcda");
    555     UErrorCode key1Status = U_ZERO_ERROR, key2Status = U_ZERO_ERROR;
    556 
    557     logln("Testing weird arguments");
    558     col->getCollationKey(NULL, 0, sortk1, key1Status);
    559     // key gets reset here
    560     int32_t length;
    561     sortk1.getByteArray(length);
    562     doAssert(sortk1.isBogus() == FALSE && length == 0,
    563              "Empty string should return an empty collation key");
    564     // bogus key returned here
    565     key1Status = U_ILLEGAL_ARGUMENT_ERROR;
    566     col->getCollationKey(NULL, 0, sortk1, key1Status);
    567     doAssert(sortk1.getByteArray(length) == NULL && length == 0,
    568         "Error code should return bogus collation key");
    569 
    570     key1Status = U_ZERO_ERROR;
    571     logln("Use tertiary comparison level testing ....");
    572 
    573     col->getCollationKey(test1, sortk1, key1Status);
    574     doAssert((sortk1.compareTo(col->getCollationKey(test2, sortk2, key2Status)))
    575                  == Collator::GREATER,
    576                 "Result should be \"Abcda\" >>> \"abcda\"");
    577 
    578     CollationKey sortk3(sortk2), sortkNew, sortkEmpty;
    579 
    580 
    581     sortkNew = sortk1;
    582     doAssert((sortk1 != sortk2), "The sort keys should be different");
    583     doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() failed");
    584     doAssert((sortk2 == sortk3), "The sort keys should be the same");
    585     doAssert((sortk1 == sortkNew), "The sort keys assignment failed");
    586     doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() failed");
    587     doAssert((sortkNew != sortk3), "The sort keys should be different");
    588     doAssert(sortk1.compareTo(sortk3) == Collator::GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
    589     doAssert(sortk2.compareTo(sortk3) == Collator::EQUAL, "Result should be \"abcda\" == \"abcda\"");
    590     doAssert(sortkEmpty.compareTo(sortk1) == Collator::LESS, "Result should be (empty key) <<< \"Abcda\"");
    591     doAssert(sortk1.compareTo(sortkEmpty) == Collator::GREATER, "Result should be \"Abcda\" >>> (empty key)");
    592     doAssert(sortkEmpty.compareTo(sortkEmpty) == Collator::EQUAL, "Result should be (empty key) == (empty key)");
    593     doAssert(sortk1.compareTo(sortk3, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
    594     doAssert(sortk2.compareTo(sortk3, success) == UCOL_EQUAL, "Result should be \"abcda\" == \"abcda\"");
    595     doAssert(sortkEmpty.compareTo(sortk1, success) == UCOL_LESS, "Result should be (empty key) <<< \"Abcda\"");
    596     doAssert(sortk1.compareTo(sortkEmpty, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> (empty key)");
    597     doAssert(sortkEmpty.compareTo(sortkEmpty, success) == UCOL_EQUAL, "Result should be (empty key) == (empty key)");
    598 
    599     int32_t    cnt1, cnt2, cnt3, cnt4;
    600 
    601     const uint8_t* byteArray1 = sortk1.getByteArray(cnt1);
    602     const uint8_t* byteArray2 = sortk2.getByteArray(cnt2);
    603 
    604     const uint8_t* byteArray3 = 0;
    605     byteArray3 = sortk1.getByteArray(cnt3);
    606 
    607     const uint8_t* byteArray4 = 0;
    608     byteArray4 = sortk2.getByteArray(cnt4);
    609 
    610     CollationKey sortk4(byteArray1, cnt1), sortk5(byteArray2, cnt2);
    611     CollationKey sortk6(byteArray3, cnt3), sortk7(byteArray4, cnt4);
    612 
    613     doAssert(sortk1.compareTo(sortk4) == Collator::EQUAL, "CollationKey::toByteArray(sortk1) Failed.");
    614     doAssert(sortk2.compareTo(sortk5) == Collator::EQUAL, "CollationKey::toByteArray(sortk2) Failed.");
    615     doAssert(sortk4.compareTo(sortk5) == Collator::GREATER, "sortk4 >>> sortk5 Failed");
    616     doAssert(sortk1.compareTo(sortk6) == Collator::EQUAL, "CollationKey::getByteArray(sortk1) Failed.");
    617     doAssert(sortk2.compareTo(sortk7) == Collator::EQUAL, "CollationKey::getByteArray(sortk2) Failed.");
    618     doAssert(sortk6.compareTo(sortk7) == Collator::GREATER, "sortk6 >>> sortk7 Failed");
    619 
    620     logln("Equality tests : ");
    621     doAssert(sortk1 == sortk4, "sortk1 == sortk4 Failed.");
    622     doAssert(sortk2 == sortk5, "sortk2 == sortk5 Failed.");
    623     doAssert(sortk1 != sortk5, "sortk1 != sortk5 Failed.");
    624     doAssert(sortk1 == sortk6, "sortk1 == sortk6 Failed.");
    625     doAssert(sortk2 == sortk7, "sortk2 == sortk7 Failed.");
    626     doAssert(sortk1 != sortk7, "sortk1 != sortk7 Failed.");
    627 
    628     byteArray1 = 0;
    629     byteArray2 = 0;
    630 
    631     sortk3 = sortk1;
    632     doAssert(sortk1 == sortk3, "sortk1 = sortk3 assignment Failed.");
    633     doAssert(sortk2 != sortk3, "sortk2 != sortk3 Failed.");
    634     logln("testing sortkey ends...");
    635 
    636     col->setStrength(Collator::SECONDARY);
    637     doAssert(col->getCollationKey(test1, sortk1, key1Status).compareTo(
    638                                   col->getCollationKey(test2, sortk2, key2Status))
    639                                   == Collator::EQUAL,
    640                                   "Result should be \"Abcda\" == \"abcda\"");
    641     delete col;
    642 }
    643 
    644 //----------------------------------------------------------------------------
    645 // Tests the CollatorElementIterator class.
    646 // ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!=
    647 //
    648 void
    649 CollationAPITest::TestElemIter(/* char* par */)
    650 {
    651     logln("testing sortkey begins...");
    652     Collator *col = 0;
    653     UErrorCode success = U_ZERO_ERROR;
    654     col = Collator::createInstance(Locale::getEnglish(), success);
    655     if (U_FAILURE(success))
    656     {
    657         errcheckln(success, "Default collation creation failed. - %s", u_errorName(success));
    658         return;
    659     }
    660 
    661     UnicodeString testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
    662     UnicodeString testString2("Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
    663     logln("Constructors and comparison testing....");
    664     CollationElementIterator *iterator1 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1);
    665 
    666     CharacterIterator *chariter=new StringCharacterIterator(testString1);
    667     CollationElementIterator *coliter=((RuleBasedCollator*)col)->createCollationElementIterator(*chariter);
    668 
    669     // copy ctor
    670     CollationElementIterator *iterator2 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1);
    671     CollationElementIterator *iterator3 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString2);
    672 
    673     int32_t offset = iterator1->getOffset();
    674     if (offset != 0) {
    675         errln("Error in getOffset for collation element iterator\n");
    676         return;
    677     }
    678     iterator1->setOffset(6, success);
    679     if (U_FAILURE(success)) {
    680         errln("Error in setOffset for collation element iterator\n");
    681         return;
    682     }
    683     iterator1->setOffset(0, success);
    684     int32_t order1, order2, order3;
    685     doAssert((*iterator1 == *iterator2), "The two iterators should be the same");
    686     doAssert((*iterator1 != *iterator3), "The two iterators should be different");
    687 
    688     doAssert((*coliter == *iterator1), "The two iterators should be the same");
    689     doAssert((*coliter == *iterator2), "The two iterators should be the same");
    690     doAssert((*coliter != *iterator3), "The two iterators should be different");
    691 
    692     order1 = iterator1->next(success);
    693     if (U_FAILURE(success))
    694     {
    695         errln("Somehow ran out of memory stepping through the iterator.");
    696         return;
    697     }
    698 
    699     doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
    700     order2 = iterator2->getOffset();
    701     doAssert((order1 != order2), "The order result should not be the same");
    702     order2 = iterator2->next(success);
    703     if (U_FAILURE(success))
    704     {
    705         errln("Somehow ran out of memory stepping through the iterator.");
    706         return;
    707     }
    708 
    709     doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
    710     doAssert((order1 == order2), "The order result should be the same");
    711     order3 = iterator3->next(success);
    712     if (U_FAILURE(success))
    713     {
    714         errln("Somehow ran out of memory stepping through the iterator.");
    715         return;
    716     }
    717 
    718     doAssert((CollationElementIterator::primaryOrder(order1) ==
    719         CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
    720     doAssert((CollationElementIterator::secondaryOrder(order1) ==
    721         CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
    722     doAssert((CollationElementIterator::tertiaryOrder(order1) ==
    723         CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
    724 
    725     order1 = iterator1->next(success); order3 = iterator3->next(success);
    726     if (U_FAILURE(success))
    727     {
    728         errln("Somehow ran out of memory stepping through the iterator.");
    729         return;
    730     }
    731 
    732     doAssert((CollationElementIterator::primaryOrder(order1) ==
    733         CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
    734     doAssert((CollationElementIterator::tertiaryOrder(order1) !=
    735         CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
    736 
    737     order1 = iterator1->next(success);
    738     order3 = iterator3->next(success);
    739     /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
    740     /*
    741     doAssert((CollationElementIterator::secondaryOrder(order1) !=
    742         CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
    743     */
    744     doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
    745 
    746     iterator1->reset(); iterator2->reset(); iterator3->reset();
    747     order1 = iterator1->next(success);
    748     if (U_FAILURE(success))
    749     {
    750         errln("Somehow ran out of memory stepping through the iterator.");
    751         return;
    752     }
    753 
    754     doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
    755 
    756     order2 = iterator2->next(success);
    757     if (U_FAILURE(success))
    758     {
    759         errln("Somehow ran out of memory stepping through the iterator.");
    760         return;
    761     }
    762 
    763     doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
    764     doAssert((order1 == order2), "The order result should be the same");
    765 
    766     order3 = iterator3->next(success);
    767     if (U_FAILURE(success))
    768     {
    769         errln("Somehow ran out of memory stepping through the iterator.");
    770         return;
    771     }
    772 
    773     doAssert((CollationElementIterator::primaryOrder(order1) ==
    774         CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
    775     doAssert((CollationElementIterator::secondaryOrder(order1) ==
    776         CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
    777     doAssert((CollationElementIterator::tertiaryOrder(order1) ==
    778         CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
    779 
    780     order1 = iterator1->next(success); order2 = iterator2->next(success); order3 = iterator3->next(success);
    781     if (U_FAILURE(success))
    782     {
    783         errln("Somehow ran out of memory stepping through the iterator.");
    784         return;
    785     }
    786 
    787     doAssert((CollationElementIterator::primaryOrder(order1) ==
    788         CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
    789     doAssert((CollationElementIterator::tertiaryOrder(order1) !=
    790         CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
    791 
    792     order1 = iterator1->next(success); order3 = iterator3->next(success);
    793     if (U_FAILURE(success))
    794     {
    795         errln("Somehow ran out of memory stepping through the iterator.");
    796         return;
    797     }
    798 
    799     /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
    800     /*
    801     doAssert((CollationElementIterator::secondaryOrder(order1) !=
    802         CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
    803     */
    804     doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
    805     doAssert((*iterator2 != *iterator3), "The iterators should be different");
    806 
    807 
    808     //test error values
    809     success=U_UNSUPPORTED_ERROR;
    810     Collator *colerror=NULL;
    811     colerror=Collator::createInstance(Locale::getEnglish(), success);
    812     if (colerror != 0 || success == U_ZERO_ERROR){
    813         errln("Error: createInstance(UErrorCode != U_ZERO_ERROR) should just return and not create an instance\n");
    814     }
    815     int32_t position=coliter->previous(success);
    816     if(position != CollationElementIterator::NULLORDER){
    817         errln((UnicodeString)"Expected NULLORDER got" + position);
    818     }
    819     coliter->reset();
    820     coliter->setText(*chariter, success);
    821     if(!U_FAILURE(success)){
    822         errln("Expeceted error");
    823     }
    824     iterator1->setText((UnicodeString)"hello there", success);
    825     if(!U_FAILURE(success)){
    826         errln("Expeceted error");
    827     }
    828 
    829     delete chariter;
    830     delete coliter;
    831     delete iterator1;
    832     delete iterator2;
    833     delete iterator3;
    834     delete col;
    835 
    836 
    837 
    838     logln("testing CollationElementIterator ends...");
    839 }
    840 
    841 // Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules
    842 void
    843 CollationAPITest::TestOperators(/* char* par */)
    844 {
    845     UErrorCode success = U_ZERO_ERROR;
    846     UnicodeString ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
    847     UnicodeString ruleset2("< a, A < b, B < c, C < d, D, e, E");
    848     RuleBasedCollator *col1 = new RuleBasedCollator(ruleset1, success);
    849     if (U_FAILURE(success)) {
    850         errcheckln(success, "RuleBasedCollator creation failed. - %s", u_errorName(success));
    851         return;
    852     }
    853     success = U_ZERO_ERROR;
    854     RuleBasedCollator *col2 = new RuleBasedCollator(ruleset2, success);
    855     if (U_FAILURE(success)) {
    856         errln("The RuleBasedCollator constructor failed when building with the 2nd rule set.");
    857         return;
    858     }
    859     logln("The operator tests begin : ");
    860     logln("testing operator==, operator!=, clone  methods ...");
    861     doAssert((*col1 != *col2), "The two different table collations compared equal");
    862     *col1 = *col2;
    863     doAssert((*col1 == *col2), "Collator objects not equal after assignment (operator=)");
    864 
    865     success = U_ZERO_ERROR;
    866     Collator *col3 = Collator::createInstance(Locale::getEnglish(), success);
    867     if (U_FAILURE(success)) {
    868         errln("Default collation creation failed.");
    869         return;
    870     }
    871     doAssert((*col1 != *col3), "The two different table collations compared equal");
    872     Collator* col4 = col1->clone();
    873     Collator* col5 = col3->clone();
    874     doAssert((*col1 == *col4), "Cloned collation objects not equal");
    875     doAssert((*col3 != *col4), "Two different table collations compared equal");
    876     doAssert((*col3 == *col5), "Cloned collation objects not equal");
    877     doAssert((*col4 != *col5), "Two cloned collations compared equal");
    878 
    879     const UnicodeString& defRules = ((RuleBasedCollator*)col3)->getRules();
    880     RuleBasedCollator* col6 = new RuleBasedCollator(defRules, success);
    881     if (U_FAILURE(success)) {
    882         errln("Creating default collation with rules failed.");
    883         return;
    884     }
    885     doAssert((((RuleBasedCollator*)col3)->getRules() == col6->getRules()), "Default collator getRules failed");
    886 
    887     success = U_ZERO_ERROR;
    888     RuleBasedCollator *col7 = new RuleBasedCollator(ruleset2, Collator::TERTIARY, success);
    889     if (U_FAILURE(success)) {
    890         errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength.");
    891         return;
    892     }
    893     success = U_ZERO_ERROR;
    894     RuleBasedCollator *col8 = new RuleBasedCollator(ruleset2, UCOL_OFF, success);
    895     if (U_FAILURE(success)) {
    896         errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with Normalizer::NO_OP.");
    897         return;
    898     }
    899     success = U_ZERO_ERROR;
    900     RuleBasedCollator *col9 = new RuleBasedCollator(ruleset2, Collator::PRIMARY, UCOL_ON, success);
    901     if (U_FAILURE(success)) {
    902         errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength and Normalizer::NO_OP.");
    903         return;
    904     }
    905   //  doAssert((*col7 == *col8), "The two equal table collations compared different");
    906     doAssert((*col7 != *col9), "The two different table collations compared equal");
    907     doAssert((*col8 != *col9), "The two different table collations compared equal");
    908 
    909     logln("operator tests ended.");
    910     delete col1;
    911     delete col2;
    912     delete col3;
    913     delete col4;
    914     delete col5;
    915     delete col6;
    916     delete col7;
    917     delete col8;
    918     delete col9;
    919 }
    920 
    921 // test clone and copy
    922 void
    923 CollationAPITest::TestDuplicate(/* char* par */)
    924 {
    925     UErrorCode status = U_ZERO_ERROR;
    926     Collator *col1 = Collator::createInstance(Locale::getEnglish(), status);
    927     if (U_FAILURE(status)) {
    928         logln("Default collator creation failed.");
    929         return;
    930     }
    931     Collator *col2 = col1->clone();
    932     doAssert((*col1 == *col2), "Cloned object is not equal to the orginal");
    933     UnicodeString *ruleset = new UnicodeString("< a, A < b, B < c, C < d, D, e, E");
    934     RuleBasedCollator *col3 = new RuleBasedCollator(*ruleset, status);
    935     doAssert((*col1 != *col3), "Cloned object is equal to some dummy");
    936     *col3 = *((RuleBasedCollator*)col1);
    937     doAssert((*col1 == *col3), "Copied object is not equal to the orginal");
    938 
    939     if (U_FAILURE(status)) {
    940         logln("Collation tailoring failed.");
    941         return;
    942     }
    943 
    944     UCollationResult res;
    945     UnicodeString first((UChar)0x0061);
    946     UnicodeString second((UChar)0x0062);
    947     UnicodeString copiedEnglishRules(((RuleBasedCollator*)col1)->getRules());
    948 
    949     delete col1;
    950     delete ruleset;
    951 
    952     // Try using the cloned collators after deleting the original data
    953     res = col2->compare(first, second, status);
    954     if(res != UCOL_LESS) {
    955         errln("a should be less then b after tailoring");
    956     }
    957     if (((RuleBasedCollator*)col2)->getRules() != copiedEnglishRules) {
    958         errln(UnicodeString("English rule difference. ")
    959             + copiedEnglishRules + UnicodeString("\ngetRules=") + ((RuleBasedCollator*)col2)->getRules());
    960     }
    961     res = col3->compare(first, second, status);
    962     if(res != UCOL_LESS) {
    963         errln("a should be less then b after tailoring");
    964     }
    965     if (col3->getRules() != copiedEnglishRules) {
    966         errln(UnicodeString("English rule difference. ")
    967             + copiedEnglishRules + UnicodeString("\ngetRules=") + col3->getRules());
    968     }
    969 
    970     delete col2;
    971     delete col3;
    972 }
    973 
    974 void
    975 CollationAPITest::TestCompare(/* char* par */)
    976 {
    977     logln("The compare tests begin : ");
    978     Collator *col = 0;
    979     UErrorCode success = U_ZERO_ERROR;
    980     col = Collator::createInstance(Locale::getEnglish(), success);
    981     if (U_FAILURE(success)) {
    982         errcheckln(success, "Default collation creation failed. - %s", u_errorName(success));
    983         return;
    984     }
    985     UnicodeString test1("Abcda"), test2("abcda");
    986     logln("Use tertiary comparison level testing ....");
    987 
    988     doAssert((!col->equals(test1, test2) ), "Result should be \"Abcda\" != \"abcda\"");
    989     doAssert((col->greater(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
    990     doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
    991 
    992     col->setStrength(Collator::SECONDARY);
    993     logln("Use secondary comparison level testing ....");
    994 
    995     doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
    996     doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
    997     doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
    998 
    999     col->setStrength(Collator::PRIMARY);
   1000     logln("Use primary comparison level testing ....");
   1001 
   1002     doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
   1003     doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
   1004     doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
   1005 
   1006     // Test different APIs
   1007     const UChar* t1 = test1.getBuffer();
   1008     int32_t t1Len = test1.length();
   1009     const UChar* t2 = test2.getBuffer();
   1010     int32_t t2Len = test2.length();
   1011 
   1012     doAssert((col->compare(test1, test2) == Collator::EQUAL), "Problem");
   1013     doAssert((col->compare(test1, test2, success) == UCOL_EQUAL), "Problem");
   1014     doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::EQUAL), "Problem");
   1015     doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_EQUAL), "Problem");
   1016     doAssert((col->compare(test1, test2, t1Len) == Collator::EQUAL), "Problem");
   1017     doAssert((col->compare(test1, test2, t1Len, success) == UCOL_EQUAL), "Problem");
   1018 
   1019     col->setAttribute(UCOL_STRENGTH, UCOL_TERTIARY, success);
   1020     doAssert((col->compare(test1, test2) == Collator::GREATER), "Problem");
   1021     doAssert((col->compare(test1, test2, success) == UCOL_GREATER), "Problem");
   1022     doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::GREATER), "Problem");
   1023     doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_GREATER), "Problem");
   1024     doAssert((col->compare(test1, test2, t1Len) == Collator::GREATER), "Problem");
   1025     doAssert((col->compare(test1, test2, t1Len, success) == UCOL_GREATER), "Problem");
   1026 
   1027 
   1028 
   1029     logln("The compare tests end.");
   1030     delete col;
   1031 }
   1032 
   1033 void
   1034 CollationAPITest::TestGetAll(/* char* par */)
   1035 {
   1036     int32_t count1, count2;
   1037     UErrorCode status = U_ZERO_ERROR;
   1038 
   1039     logln("Trying Collator::getAvailableLocales(int&)");
   1040 
   1041     const Locale* list = Collator::getAvailableLocales(count1);
   1042     for (int32_t i = 0; i < count1; ++i) {
   1043         UnicodeString dispName;
   1044         logln(UnicodeString("Locale name: ")
   1045             + UnicodeString(list[i].getName())
   1046             + UnicodeString(" , the display name is : ")
   1047             + UnicodeString(list[i].getDisplayName(dispName)));
   1048     }
   1049 
   1050     if (count1 == 0 || list == NULL) {
   1051         dataerrln("getAvailableLocales(int&) returned an empty list");
   1052     }
   1053 
   1054     logln("Trying Collator::getAvailableLocales()");
   1055     StringEnumeration* localeEnum = Collator::getAvailableLocales();
   1056     const UnicodeString* locStr;
   1057     const char *locCStr;
   1058     count2 = 0;
   1059 
   1060     if (localeEnum == NULL) {
   1061         dataerrln("getAvailableLocales() returned NULL");
   1062         return;
   1063     }
   1064 
   1065     while ((locStr = localeEnum->snext(status)) != NULL)
   1066     {
   1067         logln(UnicodeString("Locale name is: ") + *locStr);
   1068         count2++;
   1069     }
   1070     if (count1 != count2) {
   1071         errln("getAvailableLocales(int&) returned %d and getAvailableLocales() returned %d", count1, count2);
   1072     }
   1073 
   1074     logln("Trying Collator::getAvailableLocales() clone");
   1075     count1 = 0;
   1076     StringEnumeration* localeEnum2 = localeEnum->clone();
   1077     localeEnum2->reset(status);
   1078     while ((locCStr = localeEnum2->next(NULL, status)) != NULL)
   1079     {
   1080         logln(UnicodeString("Locale name is: ") + UnicodeString(locCStr));
   1081         count1++;
   1082     }
   1083     if (count1 != count2) {
   1084         errln("getAvailableLocales(3rd time) returned %d and getAvailableLocales(2nd time) returned %d", count1, count2);
   1085     }
   1086     if (localeEnum->count(status) != count1) {
   1087         errln("localeEnum->count() returned %d and getAvailableLocales() returned %d", localeEnum->count(status), count1);
   1088     }
   1089     delete localeEnum;
   1090     delete localeEnum2;
   1091 }
   1092 
   1093 void CollationAPITest::TestSortKey()
   1094 {
   1095     UErrorCode status = U_ZERO_ERROR;
   1096     /*
   1097     this is supposed to open default date format, but later on it treats
   1098     it like it is "en_US"
   1099     - very bad if you try to run the tests on machine where default
   1100       locale is NOT "en_US"
   1101     */
   1102     Collator *col = Collator::createInstance(Locale::getEnglish(), status);
   1103     if (U_FAILURE(status)) {
   1104         errcheckln(status, "ERROR: Default collation creation failed.: %s\n", u_errorName(status));
   1105         return;
   1106     }
   1107 
   1108     if (col->getStrength() != Collator::TERTIARY)
   1109     {
   1110         errln("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
   1111     }
   1112 
   1113     /* Need to use identical strength */
   1114     col->setAttribute(UCOL_STRENGTH, UCOL_IDENTICAL, status);
   1115 
   1116     UChar test1[6] = {0x41, 0x62, 0x63, 0x64, 0x61, 0},
   1117           test2[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0},
   1118           test3[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0};
   1119 
   1120     uint8_t sortkey1[64];
   1121     uint8_t sortkey2[64];
   1122     uint8_t sortkey3[64];
   1123 
   1124     logln("Use tertiary comparison level testing ....\n");
   1125 
   1126     CollationKey key1;
   1127     col->getCollationKey(test1, u_strlen(test1), key1, status);
   1128 
   1129     CollationKey key2;
   1130     col->getCollationKey(test2, u_strlen(test2), key2, status);
   1131 
   1132     CollationKey key3;
   1133     col->getCollationKey(test3, u_strlen(test3), key3, status);
   1134 
   1135     doAssert(key1.compareTo(key2) == Collator::GREATER,
   1136         "Result should be \"Abcda\" > \"abcda\"");
   1137     doAssert(key2.compareTo(key1) == Collator::LESS,
   1138         "Result should be \"abcda\" < \"Abcda\"");
   1139     doAssert(key2.compareTo(key3) == Collator::EQUAL,
   1140         "Result should be \"abcda\" ==  \"abcda\"");
   1141 
   1142     // Clone the key2 sortkey for later.
   1143     int32_t keylength = 0;
   1144     const uint8_t *key2primary_alias = key2.getByteArray(keylength);
   1145     LocalArray<uint8_t> key2primary(new uint8_t[keylength]);
   1146     memcpy(key2primary.getAlias(), key2primary_alias, keylength);
   1147 
   1148     col->getSortKey(test1, sortkey1, 64);
   1149     col->getSortKey(test2, sortkey2, 64);
   1150     col->getSortKey(test3, sortkey3, 64);
   1151 
   1152     const uint8_t *tempkey = key1.getByteArray(keylength);
   1153     doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
   1154         "Test1 string should have the same collation key and sort key");
   1155     tempkey = key2.getByteArray(keylength);
   1156     doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
   1157         "Test2 string should have the same collation key and sort key");
   1158     tempkey = key3.getByteArray(keylength);
   1159     doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
   1160         "Test3 string should have the same collation key and sort key");
   1161 
   1162     col->getSortKey(test1, 5, sortkey1, 64);
   1163     col->getSortKey(test2, 5, sortkey2, 64);
   1164     col->getSortKey(test3, 5, sortkey3, 64);
   1165 
   1166     tempkey = key1.getByteArray(keylength);
   1167     doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
   1168         "Test1 string should have the same collation key and sort key");
   1169     tempkey = key2.getByteArray(keylength);
   1170     doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
   1171         "Test2 string should have the same collation key and sort key");
   1172     tempkey = key3.getByteArray(keylength);
   1173     doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
   1174         "Test3 string should have the same collation key and sort key");
   1175 
   1176     UnicodeString strtest1(test1);
   1177     col->getSortKey(strtest1, sortkey1, 64);
   1178     UnicodeString strtest2(test2);
   1179     col->getSortKey(strtest2, sortkey2, 64);
   1180     UnicodeString strtest3(test3);
   1181     col->getSortKey(strtest3, sortkey3, 64);
   1182 
   1183     tempkey = key1.getByteArray(keylength);
   1184     doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
   1185         "Test1 string should have the same collation key and sort key");
   1186     tempkey = key2.getByteArray(keylength);
   1187     doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
   1188         "Test2 string should have the same collation key and sort key");
   1189     tempkey = key3.getByteArray(keylength);
   1190     doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
   1191         "Test3 string should have the same collation key and sort key");
   1192 
   1193     logln("Use secondary comparision level testing ...\n");
   1194     col->setStrength(Collator::SECONDARY);
   1195 
   1196     col->getCollationKey(test1, u_strlen(test1), key1, status);
   1197     col->getCollationKey(test2, u_strlen(test2), key2, status);
   1198     col->getCollationKey(test3, u_strlen(test3), key3, status);
   1199 
   1200     doAssert(key1.compareTo(key2) == Collator::EQUAL,
   1201         "Result should be \"Abcda\" == \"abcda\"");
   1202     doAssert(key2.compareTo(key3) == Collator::EQUAL,
   1203         "Result should be \"abcda\" ==  \"abcda\"");
   1204 
   1205     tempkey = key2.getByteArray(keylength);
   1206     doAssert(memcmp(tempkey, key2primary.getAlias(), keylength - 1) == 0,
   1207              "Binary format for 'abcda' sortkey different for secondary strength!");
   1208 
   1209     col->getSortKey(test1, sortkey1, 64);
   1210     col->getSortKey(test2, sortkey2, 64);
   1211     col->getSortKey(test3, sortkey3, 64);
   1212 
   1213     tempkey = key1.getByteArray(keylength);
   1214     doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
   1215         "Test1 string should have the same collation key and sort key");
   1216     tempkey = key2.getByteArray(keylength);
   1217     doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
   1218         "Test2 string should have the same collation key and sort key");
   1219     tempkey = key3.getByteArray(keylength);
   1220     doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
   1221         "Test3 string should have the same collation key and sort key");
   1222 
   1223     col->getSortKey(test1, 5, sortkey1, 64);
   1224     col->getSortKey(test2, 5, sortkey2, 64);
   1225     col->getSortKey(test3, 5, sortkey3, 64);
   1226 
   1227     tempkey = key1.getByteArray(keylength);
   1228     doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
   1229         "Test1 string should have the same collation key and sort key");
   1230     tempkey = key2.getByteArray(keylength);
   1231     doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
   1232         "Test2 string should have the same collation key and sort key");
   1233     tempkey = key3.getByteArray(keylength);
   1234     doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
   1235         "Test3 string should have the same collation key and sort key");
   1236 
   1237     col->getSortKey(strtest1, sortkey1, 64);
   1238     col->getSortKey(strtest2, sortkey2, 64);
   1239     col->getSortKey(strtest3, sortkey3, 64);
   1240 
   1241     tempkey = key1.getByteArray(keylength);
   1242     doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
   1243         "Test1 string should have the same collation key and sort key");
   1244     tempkey = key2.getByteArray(keylength);
   1245     doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
   1246         "Test2 string should have the same collation key and sort key");
   1247     tempkey = key3.getByteArray(keylength);
   1248     doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
   1249         "Test3 string should have the same collation key and sort key");
   1250 
   1251     logln("testing sortkey ends...");
   1252     delete col;
   1253 }
   1254 
   1255 void CollationAPITest::TestMaxExpansion()
   1256 {
   1257     UErrorCode          status = U_ZERO_ERROR;
   1258     UChar               ch     = 0;
   1259     UChar32             unassigned = 0xEFFFD;
   1260     uint32_t            sorder = 0;
   1261     uint32_t            temporder = 0;
   1262 
   1263     UnicodeString rule("&a < ab < c/aba < d < z < ch");
   1264     RuleBasedCollator coll(rule, status);
   1265     if(U_FAILURE(status)) {
   1266       errcheckln(status, "Collator creation failed with error %s", u_errorName(status));
   1267       return;
   1268     }
   1269     UnicodeString str(ch);
   1270     CollationElementIterator *iter =
   1271                                   coll.createCollationElementIterator(str);
   1272 
   1273     while (ch < 0xFFFF && U_SUCCESS(status)) {
   1274         int      count = 1;
   1275         uint32_t order;
   1276         int32_t  size = 0;
   1277 
   1278         ch ++;
   1279 
   1280         str.setCharAt(0, ch);
   1281         iter->setText(str, status);
   1282         order = iter->previous(status);
   1283 
   1284         /* thai management */
   1285         if (order == 0)
   1286             order = iter->previous(status);
   1287 
   1288         while (U_SUCCESS(status) && iter->previous(status) != UCOL_NULLORDER) {
   1289             count ++;
   1290         }
   1291 
   1292         size = coll.getMaxExpansion(order);
   1293         if (U_FAILURE(status) || size < count) {
   1294             errln("Failure at codepoint %d, maximum expansion count < %d\n",
   1295                   ch, count);
   1296         }
   1297     }
   1298 
   1299     /* testing for exact max expansion */
   1300     ch = 0;
   1301     while (ch < 0x61) {
   1302         uint32_t order;
   1303         int32_t  size;
   1304         str.setCharAt(0, ch);
   1305         iter->setText(str, status);
   1306         order = iter->previous(status);
   1307         size  = coll.getMaxExpansion(order);
   1308         if (U_FAILURE(status) || size != 1) {
   1309             errln("Failure at codepoint %d, maximum expansion count < %d\n",
   1310                 ch, 1);
   1311         }
   1312         ch ++;
   1313     }
   1314 
   1315     ch = 0x63;
   1316     str.setTo(ch);
   1317     iter->setText(str, status);
   1318     temporder = iter->previous(status);
   1319 
   1320     if (U_FAILURE(status) || coll.getMaxExpansion(temporder) != 3) {
   1321         errln("Failure at codepoint %d, maximum expansion count != %d\n",
   1322               ch, 3);
   1323     }
   1324 
   1325     ch = 0x64;
   1326     str.setTo(ch);
   1327     iter->setText(str, status);
   1328     temporder = iter->previous(status);
   1329 
   1330     if (U_FAILURE(status) || coll.getMaxExpansion(temporder) != 1) {
   1331         errln("Failure at codepoint %d, maximum expansion count != %d\n",
   1332                 ch, 3);
   1333     }
   1334 
   1335     str.setTo(unassigned);
   1336     iter->setText(str, status);
   1337     sorder = iter->previous(status);
   1338 
   1339     if (U_FAILURE(status) || coll.getMaxExpansion(sorder) != 2) {
   1340         errln("Failure at supplementary codepoints, maximum expansion count < %d\n",
   1341               2);
   1342     }
   1343 
   1344     /* testing jamo */
   1345     ch = 0x1165;
   1346     str.setTo(ch);
   1347     iter->setText(str, status);
   1348     temporder = iter->previous(status);
   1349     if (U_FAILURE(status) || coll.getMaxExpansion(temporder) > 3) {
   1350         errln("Failure at codepoint %d, maximum expansion count > %d\n",
   1351               ch, 3);
   1352     }
   1353 
   1354     delete iter;
   1355 
   1356     /* testing special jamo &a<\u1160 */
   1357     rule = CharsToUnicodeString("\\u0026\\u0071\\u003c\\u1165\\u002f\\u0071\\u0071\\u0071\\u0071");
   1358 
   1359     RuleBasedCollator jamocoll(rule, status);
   1360     iter = jamocoll.createCollationElementIterator(str);
   1361     temporder = iter->previous(status);
   1362     if (U_FAILURE(status) || iter->getMaxExpansion(temporder) != 6) {
   1363         errln("Failure at codepoint %d, maximum expansion count > %d\n",
   1364               ch, 5);
   1365     }
   1366 
   1367     delete iter;
   1368 }
   1369 
   1370 void CollationAPITest::TestDisplayName()
   1371 {
   1372     UErrorCode error = U_ZERO_ERROR;
   1373     Collator *coll = Collator::createInstance("en_US", error);
   1374     if (U_FAILURE(error)) {
   1375         errcheckln(error, "Failure creating english collator - %s", u_errorName(error));
   1376         return;
   1377     }
   1378     UnicodeString name;
   1379     UnicodeString result;
   1380     coll->getDisplayName(Locale::getCanadaFrench(), result);
   1381     Locale::getCanadaFrench().getDisplayName(name);
   1382     if (result.compare(name)) {
   1383         errln("Failure getting the correct name for locale en_US");
   1384     }
   1385 
   1386     coll->getDisplayName(Locale::getSimplifiedChinese(), result);
   1387     Locale::getSimplifiedChinese().getDisplayName(name);
   1388     if (result.compare(name)) {
   1389         errln("Failure getting the correct name for locale zh_SG");
   1390     }
   1391     delete coll;
   1392 }
   1393 
   1394 void CollationAPITest::TestAttribute()
   1395 {
   1396     UErrorCode error = U_ZERO_ERROR;
   1397     Collator *coll = Collator::createInstance(error);
   1398 
   1399     if (U_FAILURE(error)) {
   1400         errcheckln(error, "Creation of default collator failed - %s", u_errorName(error));
   1401         return;
   1402     }
   1403 
   1404     coll->setAttribute(UCOL_FRENCH_COLLATION, UCOL_OFF, error);
   1405     if (coll->getAttribute(UCOL_FRENCH_COLLATION, error) != UCOL_OFF ||
   1406         U_FAILURE(error)) {
   1407         errln("Setting and retrieving of the french collation failed");
   1408     }
   1409 
   1410     coll->setAttribute(UCOL_FRENCH_COLLATION, UCOL_ON, error);
   1411     if (coll->getAttribute(UCOL_FRENCH_COLLATION, error) != UCOL_ON ||
   1412         U_FAILURE(error)) {
   1413         errln("Setting and retrieving of the french collation failed");
   1414     }
   1415 
   1416     coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, error);
   1417     if (coll->getAttribute(UCOL_ALTERNATE_HANDLING, error) != UCOL_SHIFTED ||
   1418         U_FAILURE(error)) {
   1419         errln("Setting and retrieving of the alternate handling failed");
   1420     }
   1421 
   1422     coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, error);
   1423     if (coll->getAttribute(UCOL_ALTERNATE_HANDLING, error) != UCOL_NON_IGNORABLE ||
   1424         U_FAILURE(error)) {
   1425         errln("Setting and retrieving of the alternate handling failed");
   1426     }
   1427 
   1428     coll->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, error);
   1429     if (coll->getAttribute(UCOL_CASE_FIRST, error) != UCOL_LOWER_FIRST ||
   1430         U_FAILURE(error)) {
   1431         errln("Setting and retrieving of the case first attribute failed");
   1432     }
   1433 
   1434     coll->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, error);
   1435     if (coll->getAttribute(UCOL_CASE_FIRST, error) != UCOL_UPPER_FIRST ||
   1436         U_FAILURE(error)) {
   1437         errln("Setting and retrieving of the case first attribute failed");
   1438     }
   1439 
   1440     coll->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, error);
   1441     if (coll->getAttribute(UCOL_CASE_LEVEL, error) != UCOL_ON ||
   1442         U_FAILURE(error)) {
   1443         errln("Setting and retrieving of the case level attribute failed");
   1444     }
   1445 
   1446     coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, error);
   1447     if (coll->getAttribute(UCOL_CASE_LEVEL, error) != UCOL_OFF ||
   1448         U_FAILURE(error)) {
   1449         errln("Setting and retrieving of the case level attribute failed");
   1450     }
   1451 
   1452     coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, error);
   1453     if (coll->getAttribute(UCOL_NORMALIZATION_MODE, error) != UCOL_ON ||
   1454         U_FAILURE(error)) {
   1455         errln("Setting and retrieving of the normalization on/off attribute failed");
   1456     }
   1457 
   1458     coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_OFF, error);
   1459     if (coll->getAttribute(UCOL_NORMALIZATION_MODE, error) != UCOL_OFF ||
   1460         U_FAILURE(error)) {
   1461         errln("Setting and retrieving of the normalization on/off attribute failed");
   1462     }
   1463 
   1464     coll->setAttribute(UCOL_STRENGTH, UCOL_PRIMARY, error);
   1465     if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_PRIMARY ||
   1466         U_FAILURE(error)) {
   1467         errln("Setting and retrieving of the collation strength failed");
   1468     }
   1469 
   1470     coll->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, error);
   1471     if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_SECONDARY ||
   1472         U_FAILURE(error)) {
   1473         errln("Setting and retrieving of the collation strength failed");
   1474     }
   1475 
   1476     coll->setAttribute(UCOL_STRENGTH, UCOL_TERTIARY, error);
   1477     if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_TERTIARY ||
   1478         U_FAILURE(error)) {
   1479         errln("Setting and retrieving of the collation strength failed");
   1480     }
   1481 
   1482     coll->setAttribute(UCOL_STRENGTH, UCOL_QUATERNARY, error);
   1483     if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_QUATERNARY ||
   1484         U_FAILURE(error)) {
   1485         errln("Setting and retrieving of the collation strength failed");
   1486     }
   1487 
   1488     coll->setAttribute(UCOL_STRENGTH, UCOL_IDENTICAL, error);
   1489     if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_IDENTICAL ||
   1490         U_FAILURE(error)) {
   1491         errln("Setting and retrieving of the collation strength failed");
   1492     }
   1493 
   1494     delete coll;
   1495 }
   1496 
   1497 void CollationAPITest::TestVariableTopSetting() {
   1498   UErrorCode status = U_ZERO_ERROR;
   1499 
   1500   UChar vt[256] = { 0 };
   1501 
   1502   Collator *coll = Collator::createInstance(status);
   1503   if(U_FAILURE(status)) {
   1504     delete coll;
   1505     errcheckln(status, "Collator creation failed with error %s", u_errorName(status));
   1506     return;
   1507   }
   1508 
   1509   uint32_t oldVarTop = coll->getVariableTop(status);
   1510 
   1511   vt[0] = 0x0041;
   1512 
   1513   uint32_t newVarTop = coll->setVariableTop(vt, 1, status);
   1514 
   1515   if((newVarTop & 0xFFFF0000) != (coll->getVariableTop(status) & 0xFFFF0000)) {
   1516     errln("Didn't set vartop properly\n");
   1517   }
   1518 
   1519   coll->setVariableTop(oldVarTop, status);
   1520 
   1521   uint32_t newerVarTop = coll->setVariableTop(UnicodeString(vt, 1), status);
   1522 
   1523   if((newVarTop & 0xFFFF0000) != (newerVarTop & 0xFFFF0000)) {
   1524     errln("Didn't set vartop properly from UnicodeString!\n");
   1525   }
   1526 
   1527   delete coll;
   1528 
   1529 }
   1530 
   1531 void CollationAPITest::TestGetLocale() {
   1532   UErrorCode status = U_ZERO_ERROR;
   1533   const char *rules = "&a<x<y<z";
   1534   UChar rlz[256] = {0};
   1535 
   1536   Collator *coll = NULL;
   1537   Locale locale;
   1538 
   1539   int32_t i = 0;
   1540 
   1541   static const struct {
   1542     const char* requestedLocale;
   1543     const char* validLocale;
   1544     const char* actualLocale;
   1545   } testStruct[] = {
   1546     { "sr_YU", "sr_YU", "root" },
   1547     { "sh_YU", "sh_YU", "sh" },
   1548     { "en_US_CALIFORNIA", "en_US", "root" },
   1549     { "fr_FR_NONEXISTANT", "fr_FR", "fr" }
   1550   };
   1551 
   1552   u_unescape(rules, rlz, 256);
   1553 
   1554   /* test opening collators for different locales */
   1555   for(i = 0; i<(int32_t)(sizeof(testStruct)/sizeof(testStruct[0])); i++) {
   1556     status = U_ZERO_ERROR;
   1557     coll = Collator::createInstance(testStruct[i].requestedLocale, status);
   1558     if(U_FAILURE(status)) {
   1559       log("Failed to open collator for %s with %s\n", testStruct[i].requestedLocale, u_errorName(status));
   1560       delete coll;
   1561       continue;
   1562     }
   1563     locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status);
   1564     if(locale != testStruct[i].requestedLocale) {
   1565       log("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].requestedLocale, locale.getName());
   1566     }
   1567     locale = coll->getLocale(ULOC_VALID_LOCALE, status);
   1568     if(locale != testStruct[i].validLocale) {
   1569       log("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].validLocale, locale.getName());
   1570     }
   1571     locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
   1572     if(locale != testStruct[i].actualLocale) {
   1573       log("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].actualLocale, locale.getName());
   1574     }
   1575     delete coll;
   1576   }
   1577 
   1578   /* completely non-existant locale for collator should get a default collator */
   1579   {
   1580     Collator *defaultColl = Collator::createInstance((const Locale)NULL, status);
   1581     coll = Collator::createInstance("blahaha", status);
   1582     if(U_FAILURE(status)) {
   1583       log("Failed to open collator with %s\n", u_errorName(status));
   1584       delete coll;
   1585       delete defaultColl;
   1586       return;
   1587     }
   1588     if(coll->getLocale(ULOC_REQUESTED_LOCALE, status) != "blahaha") {
   1589       log("Nonexisting locale didn't preserve the requested locale\n");
   1590     }
   1591     if(coll->getLocale(ULOC_VALID_LOCALE, status) !=
   1592       defaultColl->getLocale(ULOC_VALID_LOCALE, status)) {
   1593       log("Valid locale for nonexisting locale locale collator differs "
   1594         "from valid locale for default collator\n");
   1595     }
   1596     if(coll->getLocale(ULOC_ACTUAL_LOCALE, status) !=
   1597       defaultColl->getLocale(ULOC_ACTUAL_LOCALE, status)) {
   1598       log("Actual locale for nonexisting locale locale collator differs "
   1599         "from actual locale for default collator\n");
   1600     }
   1601     delete coll;
   1602     delete defaultColl;
   1603   }
   1604 
   1605 
   1606 
   1607   /* collator instantiated from rules should have all three locales NULL */
   1608   coll = new RuleBasedCollator(rlz, status);
   1609   locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status);
   1610   if(!locale.isBogus()) {
   1611     log("For collator instantiated from rules, requested locale %s is not bogus\n", locale.getName());
   1612   }
   1613   locale = coll->getLocale(ULOC_VALID_LOCALE, status);
   1614   if(!locale.isBogus()) {
   1615     log("For collator instantiated from rules, valid locale %s is not bogus\n", locale.getName());
   1616   }
   1617   locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
   1618   if(!locale.isBogus()) {
   1619     log("For collator instantiated from rules, actual locale %s is not bogus\n", locale.getName());
   1620   }
   1621   delete coll;
   1622 }
   1623 
   1624 struct teststruct {
   1625     const char *original;
   1626     uint8_t key[256];
   1627 };
   1628 
   1629 
   1630 
   1631 U_CDECL_BEGIN
   1632 static int U_CALLCONV
   1633 compare_teststruct(const void *string1, const void *string2) {
   1634   return(strcmp((const char *)((struct teststruct *)string1)->key, (const char *)((struct teststruct *)string2)->key));
   1635 }
   1636 U_CDECL_END
   1637 
   1638 
   1639 void CollationAPITest::TestBounds(void) {
   1640     UErrorCode status = U_ZERO_ERROR;
   1641 
   1642     Collator *coll = Collator::createInstance(Locale("sh"), status);
   1643     if(U_FAILURE(status)) {
   1644       delete coll;
   1645       errcheckln(status, "Collator creation failed with %s", u_errorName(status));
   1646       return;
   1647     }
   1648 
   1649     uint8_t sortkey[512], lower[512], upper[512];
   1650     UChar buffer[512];
   1651 
   1652     static const char * const test[] = {
   1653         "John Smith",
   1654         "JOHN SMITH",
   1655         "john SMITH",
   1656         "j\\u00F6hn sm\\u00EFth",
   1657         "J\\u00F6hn Sm\\u00EFth",
   1658         "J\\u00D6HN SM\\u00CFTH",
   1659         "john smithsonian",
   1660         "John Smithsonian"
   1661     };
   1662 
   1663     struct teststruct tests[] = {
   1664         {"\\u010CAKI MIHALJ", {0}},
   1665         {"\\u010CAKI MIHALJ", {0}},
   1666         {"\\u010CAKI PIRO\\u0160KA", {0}},
   1667         {"\\u010CABAI ANDRIJA", {0}},
   1668         {"\\u010CABAI LAJO\\u0160", {0}},
   1669         {"\\u010CABAI MARIJA", {0}},
   1670         {"\\u010CABAI STEVAN", {0}},
   1671         {"\\u010CABAI STEVAN", {0}},
   1672         {"\\u010CABARKAPA BRANKO", {0}},
   1673         {"\\u010CABARKAPA MILENKO", {0}},
   1674         {"\\u010CABARKAPA MIROSLAV", {0}},
   1675         {"\\u010CABARKAPA SIMO", {0}},
   1676         {"\\u010CABARKAPA STANKO", {0}},
   1677         {"\\u010CABARKAPA TAMARA", {0}},
   1678         {"\\u010CABARKAPA TOMA\\u0160", {0}},
   1679         {"\\u010CABDARI\\u0106 NIKOLA", {0}},
   1680         {"\\u010CABDARI\\u0106 ZORICA", {0}},
   1681         {"\\u010CABI NANDOR", {0}},
   1682         {"\\u010CABOVI\\u0106 MILAN", {0}},
   1683         {"\\u010CABRADI AGNEZIJA", {0}},
   1684         {"\\u010CABRADI IVAN", {0}},
   1685         {"\\u010CABRADI JELENA", {0}},
   1686         {"\\u010CABRADI LJUBICA", {0}},
   1687         {"\\u010CABRADI STEVAN", {0}},
   1688         {"\\u010CABRDA MARTIN", {0}},
   1689         {"\\u010CABRILO BOGDAN", {0}},
   1690         {"\\u010CABRILO BRANISLAV", {0}},
   1691         {"\\u010CABRILO LAZAR", {0}},
   1692         {"\\u010CABRILO LJUBICA", {0}},
   1693         {"\\u010CABRILO SPASOJA", {0}},
   1694         {"\\u010CADE\\u0160 ZDENKA", {0}},
   1695         {"\\u010CADESKI BLAGOJE", {0}},
   1696         {"\\u010CADOVSKI VLADIMIR", {0}},
   1697         {"\\u010CAGLJEVI\\u0106 TOMA", {0}},
   1698         {"\\u010CAGOROVI\\u0106 VLADIMIR", {0}},
   1699         {"\\u010CAJA VANKA", {0}},
   1700         {"\\u010CAJI\\u0106 BOGOLJUB", {0}},
   1701         {"\\u010CAJI\\u0106 BORISLAV", {0}},
   1702         {"\\u010CAJI\\u0106 RADOSLAV", {0}},
   1703         {"\\u010CAK\\u0160IRAN MILADIN", {0}},
   1704         {"\\u010CAKAN EUGEN", {0}},
   1705         {"\\u010CAKAN EVGENIJE", {0}},
   1706         {"\\u010CAKAN IVAN", {0}},
   1707         {"\\u010CAKAN JULIJAN", {0}},
   1708         {"\\u010CAKAN MIHAJLO", {0}},
   1709         {"\\u010CAKAN STEVAN", {0}},
   1710         {"\\u010CAKAN VLADIMIR", {0}},
   1711         {"\\u010CAKAN VLADIMIR", {0}},
   1712         {"\\u010CAKAN VLADIMIR", {0}},
   1713         {"\\u010CAKARA ANA", {0}},
   1714         {"\\u010CAKAREVI\\u0106 MOMIR", {0}},
   1715         {"\\u010CAKAREVI\\u0106 NEDELJKO", {0}},
   1716         {"\\u010CAKI \\u0160ANDOR", {0}},
   1717         {"\\u010CAKI AMALIJA", {0}},
   1718         {"\\u010CAKI ANDRA\\u0160", {0}},
   1719         {"\\u010CAKI LADISLAV", {0}},
   1720         {"\\u010CAKI LAJO\\u0160", {0}},
   1721         {"\\u010CAKI LASLO", {0}}
   1722     };
   1723 
   1724 
   1725 
   1726     int32_t i = 0, j = 0, k = 0, buffSize = 0, skSize = 0, lowerSize = 0, upperSize = 0;
   1727     int32_t arraySize = sizeof(tests)/sizeof(tests[0]);
   1728 
   1729     for(i = 0; i<arraySize; i++) {
   1730         buffSize = u_unescape(tests[i].original, buffer, 512);
   1731         skSize = coll->getSortKey(buffer, buffSize, tests[i].key, 512);
   1732     }
   1733 
   1734     qsort(tests, arraySize, sizeof(struct teststruct), compare_teststruct);
   1735 
   1736     for(i = 0; i < arraySize-1; i++) {
   1737         for(j = i+1; j < arraySize; j++) {
   1738             lowerSize = coll->getBound(tests[i].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, status);
   1739             upperSize = coll->getBound(tests[j].key, -1, UCOL_BOUND_UPPER, 1, upper, 512, status);
   1740             for(k = i; k <= j; k++) {
   1741                 if(strcmp((const char *)lower, (const char *)tests[k].key) > 0) {
   1742                     errln("Problem with lower! j = %i (%s vs %s)", k, tests[k].original, tests[i].original);
   1743                 }
   1744                 if(strcmp((const char *)upper, (const char *)tests[k].key) <= 0) {
   1745                     errln("Problem with upper! j = %i (%s vs %s)", k, tests[k].original, tests[j].original);
   1746                 }
   1747             }
   1748         }
   1749     }
   1750 
   1751 
   1752     for(i = 0; i<(int32_t)(sizeof(test)/sizeof(test[0])); i++) {
   1753         buffSize = u_unescape(test[i], buffer, 512);
   1754         skSize = coll->getSortKey(buffer, buffSize, sortkey, 512);
   1755         lowerSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_LOWER, 1, lower, 512, &status);
   1756         upperSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
   1757         for(j = i+1; j<(int32_t)(sizeof(test)/sizeof(test[0])); j++) {
   1758             buffSize = u_unescape(test[j], buffer, 512);
   1759             skSize = coll->getSortKey(buffer, buffSize, sortkey, 512);
   1760             if(strcmp((const char *)lower, (const char *)sortkey) > 0) {
   1761                 errln("Problem with lower! i = %i, j = %i (%s vs %s)", i, j, test[i], test[j]);
   1762             }
   1763             if(strcmp((const char *)upper, (const char *)sortkey) <= 0) {
   1764                 errln("Problem with upper! i = %i, j = %i (%s vs %s)", i, j, test[i], test[j]);
   1765             }
   1766         }
   1767     }
   1768     delete coll;
   1769 }
   1770 
   1771 
   1772 void CollationAPITest::TestGetTailoredSet()
   1773 {
   1774   struct {
   1775     const char *rules;
   1776     const char *tests[20];
   1777     int32_t testsize;
   1778   } setTest[] = {
   1779     { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
   1780     { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
   1781   };
   1782 
   1783   uint32_t i = 0, j = 0;
   1784   UErrorCode status = U_ZERO_ERROR;
   1785 
   1786   RuleBasedCollator *coll = NULL;
   1787   UnicodeString buff;
   1788   UnicodeSet *set = NULL;
   1789 
   1790   for(i = 0; i < sizeof(setTest)/sizeof(setTest[0]); i++) {
   1791     buff = UnicodeString(setTest[i].rules, "").unescape();
   1792     coll = new RuleBasedCollator(buff, status);
   1793     if(U_SUCCESS(status)) {
   1794       set = coll->getTailoredSet(status);
   1795       if(set->size() != setTest[i].testsize) {
   1796         errln("Tailored set size different (%d) than expected (%d)", set->size(), setTest[i].testsize);
   1797       }
   1798       for(j = 0; j < (uint32_t)setTest[i].testsize; j++) {
   1799         buff = UnicodeString(setTest[i].tests[j], "").unescape();
   1800         if(!set->contains(buff)) {
   1801           errln("Tailored set doesn't contain %s... It should", setTest[i].tests[j]);
   1802         }
   1803       }
   1804       delete set;
   1805     } else {
   1806       errcheckln(status, "Couldn't open collator with rules %s - %s", setTest[i].rules, u_errorName(status));
   1807     }
   1808     delete coll;
   1809   }
   1810 }
   1811 
   1812 void CollationAPITest::TestUClassID()
   1813 {
   1814     char id = *((char *)RuleBasedCollator::getStaticClassID());
   1815     if (id != 0) {
   1816         errln("Static class id for RuleBasedCollator should be 0");
   1817     }
   1818     UErrorCode status = U_ZERO_ERROR;
   1819     RuleBasedCollator *coll
   1820         = (RuleBasedCollator *)Collator::createInstance(status);
   1821     if(U_FAILURE(status)) {
   1822       delete coll;
   1823       errcheckln(status, "Collator creation failed with %s", u_errorName(status));
   1824       return;
   1825     }
   1826     id = *((char *)coll->getDynamicClassID());
   1827     if (id != 0) {
   1828         errln("Dynamic class id for RuleBasedCollator should be 0");
   1829     }
   1830     id = *((char *)CollationKey::getStaticClassID());
   1831     if (id != 0) {
   1832         errln("Static class id for CollationKey should be 0");
   1833     }
   1834     CollationKey *key = new CollationKey();
   1835     id = *((char *)key->getDynamicClassID());
   1836     if (id != 0) {
   1837         errln("Dynamic class id for CollationKey should be 0");
   1838     }
   1839     id = *((char *)CollationElementIterator::getStaticClassID());
   1840     if (id != 0) {
   1841         errln("Static class id for CollationElementIterator should be 0");
   1842     }
   1843     UnicodeString str("testing");
   1844     CollationElementIterator *iter = coll->createCollationElementIterator(str);
   1845     id = *((char *)iter->getDynamicClassID());
   1846     if (id != 0) {
   1847         errln("Dynamic class id for CollationElementIterator should be 0");
   1848     }
   1849     delete key;
   1850     delete iter;
   1851     delete coll;
   1852 }
   1853 
   1854 class TestCollator  : public Collator
   1855 {
   1856 public:
   1857     virtual Collator* clone(void) const;
   1858 
   1859     // dang, markus says we can't use 'using' in ICU.  I hate doing this for
   1860     // deprecated methods...
   1861 
   1862     // using Collator::compare;
   1863 
   1864     virtual EComparisonResult compare(const UnicodeString& source,
   1865                                       const UnicodeString& target) const
   1866     {
   1867         return Collator::compare(source, target);
   1868     }
   1869 
   1870     virtual EComparisonResult compare(const UnicodeString& source,
   1871                                       const UnicodeString& target,
   1872                                       int32_t length) const
   1873     {
   1874         return Collator::compare(source, target, length);
   1875     }
   1876 
   1877     virtual EComparisonResult compare(const UChar* source,
   1878                                       int32_t sourceLength,
   1879                                       const UChar* target,
   1880                                       int32_t targetLength) const
   1881     {
   1882         return Collator::compare(source, sourceLength, target, targetLength);
   1883     }
   1884 
   1885 
   1886     virtual UCollationResult compare(const UnicodeString& source,
   1887                                       const UnicodeString& target,
   1888                                       UErrorCode& status) const;
   1889     virtual UCollationResult compare(const UnicodeString& source,
   1890                                       const UnicodeString& target,
   1891                                       int32_t length,
   1892                                       UErrorCode& status) const;
   1893     virtual UCollationResult compare(const UChar* source,
   1894                                       int32_t sourceLength,
   1895                                       const UChar* target,
   1896                                       int32_t targetLength,
   1897                                       UErrorCode& status) const;
   1898     virtual CollationKey& getCollationKey(const UnicodeString&  source,
   1899                                           CollationKey& key,
   1900                                           UErrorCode& status) const;
   1901     virtual CollationKey& getCollationKey(const UChar*source,
   1902                                           int32_t sourceLength,
   1903                                           CollationKey& key,
   1904                                           UErrorCode& status) const;
   1905     virtual int32_t hashCode(void) const;
   1906     virtual const Locale getLocale(ULocDataLocaleType type,
   1907                                    UErrorCode& status) const;
   1908     virtual ECollationStrength getStrength(void) const;
   1909     virtual void setStrength(ECollationStrength newStrength);
   1910     virtual UClassID getDynamicClassID(void) const;
   1911     virtual void getVersion(UVersionInfo info) const;
   1912     virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
   1913                               UErrorCode &status);
   1914     virtual UColAttributeValue getAttribute(UColAttribute attr,
   1915                                             UErrorCode &status);
   1916     virtual uint32_t setVariableTop(const UChar *varTop, int32_t len,
   1917                                     UErrorCode &status);
   1918     virtual uint32_t setVariableTop(const UnicodeString varTop,
   1919                                     UErrorCode &status);
   1920     virtual void setVariableTop(const uint32_t varTop, UErrorCode &status);
   1921     virtual uint32_t getVariableTop(UErrorCode &status) const;
   1922     virtual Collator* safeClone(void);
   1923     virtual int32_t getSortKey(const UnicodeString& source,
   1924                             uint8_t* result,
   1925                             int32_t resultLength) const;
   1926     virtual int32_t getSortKey(const UChar*source, int32_t sourceLength,
   1927                              uint8_t*result, int32_t resultLength) const;
   1928     virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
   1929     virtual UBool operator!=(const Collator& other) const;
   1930     virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
   1931     TestCollator() : Collator() {};
   1932     TestCollator(UCollationStrength collationStrength,
   1933            UNormalizationMode decompositionMode) : Collator(collationStrength, decompositionMode) {};
   1934 };
   1935 
   1936 inline UBool TestCollator::operator!=(const Collator& other) const {
   1937     return Collator::operator!=(other);
   1938 }
   1939 
   1940 #define returnEComparisonResult(data) \
   1941     if (data < 0) return Collator::LESS;\
   1942     if (data > 0) return Collator::GREATER;\
   1943     return Collator::EQUAL;
   1944 
   1945 Collator* TestCollator::clone() const
   1946 {
   1947     return new TestCollator();
   1948 }
   1949 
   1950 UCollationResult TestCollator::compare(const UnicodeString& source,
   1951                                         const UnicodeString& target,
   1952                                         UErrorCode& status) const
   1953 {
   1954   if(U_SUCCESS(status)) {
   1955     return UCollationResult(source.compare(target));
   1956   } else {
   1957     return UCOL_EQUAL;
   1958   }
   1959 }
   1960 
   1961 UCollationResult TestCollator::compare(const UnicodeString& source,
   1962                                         const UnicodeString& target,
   1963                                         int32_t length,
   1964                                         UErrorCode& status) const
   1965 {
   1966   if(U_SUCCESS(status)) {
   1967     return UCollationResult(source.compare(0, length, target));
   1968   } else {
   1969     return UCOL_EQUAL;
   1970   }
   1971 }
   1972 
   1973 UCollationResult TestCollator::compare(const UChar* source,
   1974                                         int32_t sourceLength,
   1975                                         const UChar* target,
   1976                                         int32_t targetLength,
   1977                                         UErrorCode& status) const
   1978 {
   1979     UnicodeString s(source, sourceLength);
   1980     UnicodeString t(target, targetLength);
   1981     return compare(s, t, status);
   1982 }
   1983 
   1984 CollationKey& TestCollator::getCollationKey(const UnicodeString& source,
   1985                                             CollationKey& key,
   1986                                             UErrorCode& status) const
   1987 {
   1988     char temp[100];
   1989     int length = 100;
   1990     length = source.extract(temp, length, NULL, status);
   1991     temp[length] = 0;
   1992     CollationKey tempkey((uint8_t*)temp, length);
   1993     key = tempkey;
   1994     return key;
   1995 }
   1996 
   1997 CollationKey& TestCollator::getCollationKey(const UChar*source,
   1998                                           int32_t sourceLength,
   1999                                           CollationKey& key,
   2000                                           UErrorCode& status) const
   2001 {
   2002     //s tack allocation used since collationkey does not keep the unicodestring
   2003     UnicodeString str(source, sourceLength);
   2004     return getCollationKey(str, key, status);
   2005 }
   2006 
   2007 int32_t TestCollator::getSortKey(const UnicodeString& source, uint8_t* result,
   2008                                  int32_t resultLength) const
   2009 {
   2010     UErrorCode status = U_ZERO_ERROR;
   2011     int32_t length = source.extract((char *)result, resultLength, NULL,
   2012                                     status);
   2013     result[length] = 0;
   2014     return length;
   2015 }
   2016 
   2017 int32_t TestCollator::getSortKey(const UChar*source, int32_t sourceLength,
   2018                                  uint8_t*result, int32_t resultLength) const
   2019 {
   2020     UnicodeString str(source, sourceLength);
   2021     return getSortKey(str, result, resultLength);
   2022 }
   2023 
   2024 int32_t TestCollator::hashCode() const
   2025 {
   2026     return 0;
   2027 }
   2028 
   2029 const Locale TestCollator::getLocale(ULocDataLocaleType type,
   2030                                      UErrorCode& status) const
   2031 {
   2032     // api not used, this is to make the compiler happy
   2033     if (U_FAILURE(status)) {
   2034         type = ULOC_DATA_LOCALE_TYPE_LIMIT;
   2035     }
   2036     return NULL;
   2037 }
   2038 
   2039 Collator::ECollationStrength TestCollator::getStrength() const
   2040 {
   2041     return TERTIARY;
   2042 }
   2043 
   2044 void TestCollator::setStrength(Collator::ECollationStrength newStrength)
   2045 {
   2046     // api not used, this is to make the compiler happy
   2047     newStrength = TERTIARY;
   2048 }
   2049 
   2050 UClassID TestCollator::getDynamicClassID(void) const
   2051 {
   2052     return 0;
   2053 }
   2054 
   2055 void TestCollator::getVersion(UVersionInfo info) const
   2056 {
   2057     // api not used, this is to make the compiler happy
   2058     memset(info, 0, U_MAX_VERSION_LENGTH);
   2059 }
   2060 
   2061 void TestCollator::setAttribute(UColAttribute attr, UColAttributeValue value,
   2062                                 UErrorCode &status)
   2063 {
   2064     // api not used, this is to make the compiler happy
   2065     if (U_FAILURE(status)) {
   2066         attr = UCOL_ATTRIBUTE_COUNT;
   2067         value = UCOL_OFF;
   2068     }
   2069 }
   2070 
   2071 UColAttributeValue TestCollator::getAttribute(UColAttribute attr,
   2072                                               UErrorCode &status)
   2073 {
   2074     // api not used, this is to make the compiler happy
   2075     if (U_FAILURE(status) || attr == UCOL_ATTRIBUTE_COUNT) {
   2076         return UCOL_OFF;
   2077     }
   2078     return UCOL_DEFAULT;
   2079 }
   2080 
   2081 uint32_t TestCollator::setVariableTop(const UChar *varTop, int32_t len,
   2082                                   UErrorCode &status)
   2083 {
   2084     // api not used, this is to make the compiler happy
   2085     if (U_SUCCESS(status) && (varTop == 0 || len < -1)) {
   2086         status = U_ILLEGAL_ARGUMENT_ERROR;
   2087     }
   2088     return 0;
   2089 }
   2090 
   2091 uint32_t TestCollator::setVariableTop(const UnicodeString varTop,
   2092                                   UErrorCode &status)
   2093 {
   2094     // api not used, this is to make the compiler happy
   2095     if (U_SUCCESS(status) && varTop.length() == 0) {
   2096         status = U_ILLEGAL_ARGUMENT_ERROR;
   2097     }
   2098     return 0;
   2099 }
   2100 
   2101 void TestCollator::setVariableTop(const uint32_t varTop, UErrorCode &status)
   2102 {
   2103     // api not used, this is to make the compiler happy
   2104     if (U_SUCCESS(status) && varTop == 0) {
   2105         status = U_ILLEGAL_ARGUMENT_ERROR;
   2106     }
   2107 }
   2108 
   2109 uint32_t TestCollator::getVariableTop(UErrorCode &status) const
   2110 {
   2111 
   2112     // api not used, this is to make the compiler happy
   2113     if (U_SUCCESS(status)) {
   2114         return 0;
   2115     }
   2116     return (uint32_t)(0xFFFFFFFFu);
   2117 }
   2118 
   2119 Collator* TestCollator::safeClone(void)
   2120 {
   2121     return new TestCollator();
   2122 }
   2123 
   2124 UnicodeSet * TestCollator::getTailoredSet(UErrorCode &status) const
   2125 {
   2126     return Collator::getTailoredSet(status);
   2127 }
   2128 
   2129 void TestCollator::setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale)
   2130 {
   2131     Collator::setLocales(requestedLocale, validLocale, actualLocale);
   2132 }
   2133 
   2134 
   2135 void CollationAPITest::TestSubclass()
   2136 {
   2137     TestCollator col1;
   2138     TestCollator col2;
   2139     doAssert(col1 != col2, "2 instance of TestCollator should be different");
   2140     if (col1.hashCode() != col2.hashCode()) {
   2141         errln("Every TestCollator has the same hashcode");
   2142     }
   2143     UnicodeString abc("abc", 3);
   2144     UnicodeString bcd("bcd", 3);
   2145     if (col1.compare(abc, bcd) != abc.compare(bcd)) {
   2146         errln("TestCollator compare should be the same as the default "
   2147               "string comparison");
   2148     }
   2149     CollationKey key;
   2150     UErrorCode status = U_ZERO_ERROR;
   2151     col1.getCollationKey(abc, key, status);
   2152     int32_t length = 0;
   2153     const char* bytes = (const char *)key.getByteArray(length);
   2154     UnicodeString keyarray(bytes, length, NULL, status);
   2155     if (abc != keyarray) {
   2156         errln("TestCollator collationkey API is returning wrong values");
   2157     }
   2158 
   2159     UnicodeSet expectedset(0, 0x10FFFF);
   2160     UnicodeSet *defaultset = col1.getTailoredSet(status);
   2161     if (!defaultset->containsAll(expectedset)
   2162         || !expectedset.containsAll(*defaultset)) {
   2163         errln("Error: expected default tailoring to be 0 to 0x10ffff");
   2164     }
   2165     delete defaultset;
   2166 
   2167     // use base class implementation
   2168     Locale loc1 = Locale::getGermany();
   2169     Locale loc2 = Locale::getFrance();
   2170     col1.setLocales(loc1, loc2, loc2); // default implementation has no effect
   2171 
   2172     UnicodeString displayName;
   2173     col1.getDisplayName(loc1, loc2, displayName); // de_DE collator in fr_FR locale
   2174 
   2175     TestCollator col3(UCOL_TERTIARY, UNORM_NONE);
   2176     UnicodeString a("a");
   2177     UnicodeString b("b");
   2178     Collator::EComparisonResult result = Collator::EComparisonResult(a.compare(b));
   2179     if(col1.compare(a, b) != result) {
   2180       errln("Collator doesn't give default result");
   2181     }
   2182     if(col1.compare(a, b, 1) != result) {
   2183       errln("Collator doesn't give default result");
   2184     }
   2185     if(col1.compare(a.getBuffer(), a.length(), b.getBuffer(), b.length()) != result) {
   2186       errln("Collator doesn't give default result");
   2187     }
   2188 }
   2189 
   2190 void CollationAPITest::TestNULLCharTailoring()
   2191 {
   2192     UErrorCode status = U_ZERO_ERROR;
   2193     UChar buf[256] = {0};
   2194     int32_t len = u_unescape("&a < '\\u0000'", buf, 256);
   2195     UnicodeString first((UChar)0x0061);
   2196     UnicodeString second((UChar)0);
   2197     RuleBasedCollator *coll = new RuleBasedCollator(UnicodeString(buf, len), status);
   2198     if(U_FAILURE(status)) {
   2199         delete coll;
   2200         errcheckln(status, "Failed to open collator - %s", u_errorName(status));
   2201         return;
   2202     }
   2203     UCollationResult res = coll->compare(first, second, status);
   2204     if(res != UCOL_LESS) {
   2205         errln("a should be less then NULL after tailoring");
   2206     }
   2207     delete coll;
   2208 }
   2209 
   2210 void CollationAPITest::TestClone() {
   2211     logln("\ninit c0");
   2212     UErrorCode status = U_ZERO_ERROR;
   2213     RuleBasedCollator* c0 = (RuleBasedCollator*)Collator::createInstance(status);
   2214 
   2215     if (U_FAILURE(status)) {
   2216         errcheckln(status, "Collator::CreateInstance(status) failed with %s", u_errorName(status));
   2217         return;
   2218     }
   2219 
   2220     c0->setStrength(Collator::TERTIARY);
   2221     dump("c0", c0, status);
   2222 
   2223     logln("\ninit c1");
   2224     RuleBasedCollator* c1 = (RuleBasedCollator*)Collator::createInstance(status);
   2225     c1->setStrength(Collator::TERTIARY);
   2226     UColAttributeValue val = c1->getAttribute(UCOL_CASE_FIRST, status);
   2227     if(val == UCOL_LOWER_FIRST){
   2228         c1->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status);
   2229     }else{
   2230         c1->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status);
   2231     }
   2232     dump("c0", c0, status);
   2233     dump("c1", c1, status);
   2234 
   2235     logln("\ninit c2");
   2236     RuleBasedCollator* c2 = (RuleBasedCollator*)c1->clone();
   2237     val = c2->getAttribute(UCOL_CASE_FIRST, status);
   2238     if(val == UCOL_LOWER_FIRST){
   2239         c2->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status);
   2240     }else{
   2241         c2->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status);
   2242     }
   2243     if(U_FAILURE(status)){
   2244         errln("set and get attributes of collator failed. %s\n", u_errorName(status));
   2245         return;
   2246     }
   2247     dump("c0", c0, status);
   2248     dump("c1", c1, status);
   2249     dump("c2", c2, status);
   2250     if(*c1 == *c2){
   2251         errln("The cloned objects refer to same data");
   2252     }
   2253     delete c0;
   2254     delete c1;
   2255     delete c2;
   2256 }
   2257 
   2258  void CollationAPITest::dump(UnicodeString msg, RuleBasedCollator* c, UErrorCode& status) {
   2259     const char* bigone = "One";
   2260     const char* littleone = "one";
   2261 
   2262     logln(msg + " " + c->compare(bigone, littleone) +
   2263                         " s: " + c->getStrength() +
   2264                         " u: " + c->getAttribute(UCOL_CASE_FIRST, status));
   2265 }
   2266 void CollationAPITest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */)
   2267 {
   2268     if (exec) logln("TestSuite CollationAPITest: ");
   2269     switch (index) {
   2270         case 0: name = "TestProperty";  if (exec)   TestProperty(/* par */); break;
   2271         case 1: name = "TestOperators"; if (exec)   TestOperators(/* par */); break;
   2272         case 2: name = "TestDuplicate"; if (exec)   TestDuplicate(/* par */); break;
   2273         case 3: name = "TestCompare";   if (exec)   TestCompare(/* par */); break;
   2274         // BEGIN android-changed
   2275         // To save space, Android does not include the collation tailoring rules.
   2276         // We skip the tailing tests for collations.
   2277         case 4: name = "TestHashCode"; break;
   2278         // END android-changed
   2279         case 5: name = "TestCollationKey";  if (exec)   TestCollationKey(/* par */); break;
   2280         case 6: name = "TestElemIter";  if (exec)   TestElemIter(/* par */); break;
   2281         case 7: name = "TestGetAll";    if (exec)   TestGetAll(/* par */); break;
   2282         case 8: name = "TestRuleBasedColl"; if (exec)   TestRuleBasedColl(/* par */); break;
   2283         case 9: name = "TestDecomposition"; if (exec)   TestDecomposition(/* par */); break;
   2284         case 10: name = "TestSafeClone"; if (exec)   TestSafeClone(/* par */); break;
   2285         case 11: name = "TestSortKey";   if (exec)   TestSortKey(); break;
   2286         case 12: name = "TestMaxExpansion";   if (exec)   TestMaxExpansion(); break;
   2287         case 13: name = "TestDisplayName";   if (exec)   TestDisplayName(); break;
   2288         case 14: name = "TestAttribute";   if (exec)   TestAttribute(); break;
   2289         case 15: name = "TestVariableTopSetting"; if (exec) TestVariableTopSetting(); break;
   2290         // BEGIN android-changed
   2291         // To save space, Android does not include the collation tailoring rules.
   2292         // We skip the tailing tests for collations.
   2293         case 16: name = "TestRules"; break;
   2294         // END android-changed
   2295         case 17: name = "TestGetLocale"; if (exec) TestGetLocale(); break;
   2296         case 18: name = "TestBounds"; if (exec) TestBounds(); break;
   2297         case 19: name = "TestGetTailoredSet"; if (exec) TestGetTailoredSet(); break;
   2298         case 20: name = "TestUClassID"; if (exec) TestUClassID(); break;
   2299         case 21: name = "TestSubclass"; if (exec) TestSubclass(); break;
   2300         case 22: name = "TestNULLCharTailoring"; if (exec) TestNULLCharTailoring(); break;
   2301         case 23: name = "TestClone"; if (exec) TestClone(); break;
   2302         default: name = ""; break;
   2303     }
   2304 }
   2305 
   2306 #endif /* #if !UCONFIG_NO_COLLATION */
   2307