| OLD | NEW |
| 1 /* | 1 /* |
| 2 ******************************************************************************* | 2 ******************************************************************************* |
| 3 * Copyright (C) 2003-2013, International Business Machines Corporation and * | 3 * Copyright (C) 2003-2014, International Business Machines Corporation and |
| 4 * others. All Rights Reserved. * | 4 * others. All Rights Reserved. |
| 5 ******************************************************************************* | 5 ******************************************************************************* |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "unicode/utypes.h" | 8 #include "unicode/utypes.h" |
| 9 | 9 |
| 10 #if !UCONFIG_NO_COLLATION | 10 #if !UCONFIG_NO_COLLATION |
| 11 | 11 |
| 12 #include "svccoll.h" | 12 #include "svccoll.h" |
| 13 #include "unicode/coll.h" | 13 #include "unicode/coll.h" |
| 14 #include "unicode/strenum.h" | 14 #include "unicode/strenum.h" |
| 15 #include "hash.h" | 15 #include "hash.h" |
| 16 #include "uassert.h" | 16 #include "uassert.h" |
| 17 | 17 |
| 18 #include "ucol_imp.h" // internal api needed to test ucollator equality | |
| 19 #include "cstring.h" // internal api used to compare locale strings | 18 #include "cstring.h" // internal api used to compare locale strings |
| 20 | 19 |
| 21 void CollationServiceTest::runIndexedTest(int32_t index, UBool exec, const char*
&name, char* /*par */) | 20 void CollationServiceTest::runIndexedTest(int32_t index, UBool exec, const char*
&name, char* /*par */) |
| 22 { | 21 { |
| 23 if (exec) logln("TestSuite CollationServiceTest: "); | 22 if (exec) logln("TestSuite CollationServiceTest: "); |
| 24 switch (index) { | 23 switch (index) { |
| 25 TESTCASE(0, TestRegister); | 24 TESTCASE(0, TestRegister); |
| 26 TESTCASE(1, TestRegisterFactory); | 25 TESTCASE(1, TestRegisterFactory); |
| 27 TESTCASE(2, TestSeparateTree); | 26 TESTCASE(2, TestSeparateTree); |
| 28 default: name = ""; break; | 27 default: name = ""; break; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 42 Collator* frcol = Collator::createInstance(FR, status); | 41 Collator* frcol = Collator::createInstance(FR, status); |
| 43 Collator* uscol = Collator::createInstance(US, status); | 42 Collator* uscol = Collator::createInstance(US, status); |
| 44 if(U_FAILURE(status)) { | 43 if(U_FAILURE(status)) { |
| 45 errcheckln(status, "Failed to create collators with %s", u_errorName(sta
tus)); | 44 errcheckln(status, "Failed to create collators with %s", u_errorName(sta
tus)); |
| 46 delete frcol; | 45 delete frcol; |
| 47 delete uscol; | 46 delete uscol; |
| 48 return; | 47 return; |
| 49 } | 48 } |
| 50 | 49 |
| 51 { // try override en_US collator | 50 { // try override en_US collator |
| 51 Collator *clone = frcol->clone(); |
| 52 URegistryKey key = Collator::registerInstance(frcol, US, status); | 52 URegistryKey key = Collator::registerInstance(frcol, US, status); |
| 53 // frcol has been adopted. We must not use it any more, nor rely on its
attributes. |
| 54 frcol = NULL; |
| 53 | 55 |
| 54 Collator* ncol = Collator::createInstance(US_FOO, status); | 56 Collator* ncol = Collator::createInstance(US_FOO, status); |
| 55 if (*frcol != *ncol) { | 57 if (*clone != *ncol) { |
| 56 errln("register of french collator for en_US failed on request for e
n_US_FOO"); | 58 errln("register of french collator for en_US failed on request for e
n_US_FOO"); |
| 57 } | 59 } |
| 58 // ensure original collator's params not touched | 60 delete clone; |
| 59 Locale loc = frcol->getLocale(ULOC_REQUESTED_LOCALE, status); | 61 |
| 60 if (loc != FR) { | 62 // The requested locale may be the same as the valid locale, |
| 61 errln(UnicodeString("fr collator's requested locale changed to ") + lo
c.getName()); | 63 // or may not be supported at all. See ticket #10477. |
| 64 Locale loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status); |
| 65 if (U_SUCCESS(status) && loc != US_FOO && loc != US) { |
| 66 errln(UnicodeString("requested locale for en_US_FOO is not en_US_FOO
nor en_US but ") + loc.getName()); |
| 62 } | 67 } |
| 63 loc = frcol->getLocale(ULOC_VALID_LOCALE, status); | 68 status = U_ZERO_ERROR; |
| 64 if (loc != FR) { | |
| 65 errln(UnicodeString("fr collator's valid locale changed to ") + loc.ge
tName()); | |
| 66 } | |
| 67 | |
| 68 loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status); | |
| 69 if (loc != US_FOO) { | |
| 70 errln(UnicodeString("requested locale for en_US_FOO is not en_US_FOO
but ") + loc.getName()); | |
| 71 } | |
| 72 loc = ncol->getLocale(ULOC_VALID_LOCALE, status); | 69 loc = ncol->getLocale(ULOC_VALID_LOCALE, status); |
| 73 if (loc != US) { | 70 if (loc != US) { |
| 74 errln(UnicodeString("valid locale for en_US_FOO is not en_US but ")
+ loc.getName()); | 71 errln(UnicodeString("valid locale for en_US_FOO is not en_US but ")
+ loc.getName()); |
| 75 } | 72 } |
| 76 loc = ncol->getLocale(ULOC_ACTUAL_LOCALE, status); | 73 loc = ncol->getLocale(ULOC_ACTUAL_LOCALE, status); |
| 77 if (loc != US) { | 74 if (loc != US) { |
| 78 errln(UnicodeString("actual locale for en_US_FOO is not en_US but ")
+ loc.getName()); | 75 errln(UnicodeString("actual locale for en_US_FOO is not en_US but ")
+ loc.getName()); |
| 79 } | 76 } |
| 80 delete ncol; ncol = NULL; | 77 delete ncol; ncol = NULL; |
| 81 | 78 |
| 82 if (!Collator::unregister(key, status)) { | 79 if (!Collator::unregister(key, status)) { |
| 83 errln("failed to unregister french collator"); | 80 errln("failed to unregister french collator"); |
| 84 } | 81 } |
| 85 // !!! frcol pointer is now invalid !!! | |
| 86 | 82 |
| 87 ncol = Collator::createInstance(US, status); | 83 ncol = Collator::createInstance(US, status); |
| 88 if (*uscol != *ncol) { | 84 if (*uscol != *ncol) { |
| 89 errln("collator after unregister does not match original"); | 85 errln("collator after unregister does not match original"); |
| 90 } | 86 } |
| 91 delete ncol; ncol = NULL; | 87 delete ncol; ncol = NULL; |
| 92 } | 88 } |
| 93 | 89 |
| 94 // recreate frcol | 90 // recreate frcol |
| 95 frcol = Collator::createInstance(FR, status); | 91 frcol = Collator::createInstance(FR, status); |
| 96 | 92 |
| 97 LocalUCollatorPointer frFR(ucol_open("fr_FR", &status)); | 93 LocalUCollatorPointer frFR(ucol_open("fr_FR", &status)); |
| 98 | 94 |
| 99 { // try create collator for new locale | 95 { // try create collator for new locale |
| 100 Locale fu_FU_FOO("fu", "FU", "FOO"); | 96 Locale fu_FU_FOO("fu", "FU", "FOO"); |
| 101 Locale fu_FU("fu", "FU", ""); | 97 Locale fu_FU("fu", "FU", ""); |
| 102 | 98 |
| 103 Collator* fucol = Collator::createInstance(fu_FU, status); | 99 Collator* fucol = Collator::createInstance(fu_FU, status); |
| 100 Collator *clone = frcol->clone(); |
| 104 URegistryKey key = Collator::registerInstance(frcol, fu_FU, status); | 101 URegistryKey key = Collator::registerInstance(frcol, fu_FU, status); |
| 102 frcol = NULL; // frcol has been adopted. |
| 105 Collator* ncol = Collator::createInstance(fu_FU_FOO, status); | 103 Collator* ncol = Collator::createInstance(fu_FU_FOO, status); |
| 106 if (*frcol != *ncol) { | 104 if (*clone != *ncol) { |
| 107 errln("register of fr collator for fu_FU failed"); | 105 errln("register of fr collator for fu_FU failed"); |
| 108 } | 106 } |
| 107 delete clone; |
| 109 | 108 |
| 110 UnicodeString locName = fu_FU.getName(); | 109 UnicodeString locName = fu_FU.getName(); |
| 111 StringEnumeration* localeEnum = Collator::getAvailableLocales(); | 110 StringEnumeration* localeEnum = Collator::getAvailableLocales(); |
| 112 UBool found = FALSE; | 111 UBool found = FALSE; |
| 113 const UnicodeString* locStr, *ls2; | 112 const UnicodeString* locStr, *ls2; |
| 114 for (locStr = localeEnum->snext(status); | 113 for (locStr = localeEnum->snext(status); |
| 115 !found && locStr != NULL; | 114 !found && locStr != NULL; |
| 116 locStr = localeEnum->snext(status)) { | 115 locStr = localeEnum->snext(status)) { |
| 117 // | 116 // |
| 118 if (locName == *locStr) { | 117 if (locName == *locStr) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 ((defaultLocale == Locale::getKorean() && defaultLocale == Locale::ge
tJapanese()) && displayName == "fu(FU)") && | 166 ((defaultLocale == Locale::getKorean() && defaultLocale == Locale::ge
tJapanese()) && displayName == "fu(FU)") && |
| 168 ((defaultLocale == Locale::getChinese()) && displayName != zh_fuFU))
{ | 167 ((defaultLocale == Locale::getChinese()) && displayName != zh_fuFU))
{ |
| 169 errln(UnicodeString("found ") + displayName + " for fu_FU"); | 168 errln(UnicodeString("found ") + displayName + " for fu_FU"); |
| 170 } | 169 } |
| 171 | 170 |
| 172 // test ucol_open | 171 // test ucol_open |
| 173 LocalUCollatorPointer fufu(ucol_open("fu_FU_FOO", &status)); | 172 LocalUCollatorPointer fufu(ucol_open("fu_FU_FOO", &status)); |
| 174 if (fufu.isNull()) { | 173 if (fufu.isNull()) { |
| 175 errln("could not open fu_FU_FOO with ucol_open"); | 174 errln("could not open fu_FU_FOO with ucol_open"); |
| 176 } else { | 175 } else { |
| 177 if (!ucol_equals(fufu.getAlias(), frFR.getAlias())) { | 176 if (*Collator::fromUCollator(fufu.getAlias()) != |
| 177 *Collator::fromUCollator(frFR.getAlias())) { |
| 178 errln("collator fufu != collator frFR"); | 178 errln("collator fufu != collator frFR"); |
| 179 } | 179 } |
| 180 } | 180 } |
| 181 | 181 |
| 182 if (!Collator::unregister(key, status)) { | 182 if (!Collator::unregister(key, status)) { |
| 183 errln("failed to unregister french collator"); | 183 errln("failed to unregister french collator"); |
| 184 } | 184 } |
| 185 // !!! note frcoll invalid again, but we're no longer using it | 185 // !!! note frcoll invalid again, but we're no longer using it |
| 186 | 186 |
| 187 // other collators should still work ok | 187 // other collators should still work ok |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 CollatorInfo(const Locale& locale, Collator* collatorToAdopt, Hashtable* displ
ayNamesToAdopt); | 220 CollatorInfo(const Locale& locale, Collator* collatorToAdopt, Hashtable* displ
ayNamesToAdopt); |
| 221 ~CollatorInfo(); | 221 ~CollatorInfo(); |
| 222 UnicodeString& getDisplayName(const Locale& displayLocale, UnicodeString& name
) const; | 222 UnicodeString& getDisplayName(const Locale& displayLocale, UnicodeString& name
) const; |
| 223 }; | 223 }; |
| 224 | 224 |
| 225 CollatorInfo::CollatorInfo(const Locale& _locale, Collator* _collator, Hashtable
* _displayNames) | 225 CollatorInfo::CollatorInfo(const Locale& _locale, Collator* _collator, Hashtable
* _displayNames) |
| 226 : locale(_locale) | 226 : locale(_locale) |
| 227 , collator(_collator) | 227 , collator(_collator) |
| 228 , displayNames(_displayNames) | 228 , displayNames(_displayNames) |
| 229 { | 229 { |
| 230 collator->setLocales(locale, locale, locale); |
| 230 } | 231 } |
| 231 | 232 |
| 232 CollatorInfo::~CollatorInfo() { | 233 CollatorInfo::~CollatorInfo() { |
| 233 delete collator; | 234 delete collator; |
| 234 delete displayNames; | 235 delete displayNames; |
| 235 } | 236 } |
| 236 | 237 |
| 237 UnicodeString& | 238 UnicodeString& |
| 238 CollatorInfo::getDisplayName(const Locale& displayLocale, UnicodeString& name) c
onst { | 239 CollatorInfo::getDisplayName(const Locale& displayLocale, UnicodeString& name) c
onst { |
| 239 if (displayNames) { | 240 if (displayNames) { |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 if (*frcol != *ncol) { | 404 if (*frcol != *ncol) { |
| 404 errln("frcoll for en_US failed"); | 405 errln("frcoll for en_US failed"); |
| 405 } | 406 } |
| 406 delete ncol; ncol = NULL; | 407 delete ncol; ncol = NULL; |
| 407 | 408 |
| 408 ncol = Collator::createInstance(fu_FU_FOO, status); | 409 ncol = Collator::createInstance(fu_FU_FOO, status); |
| 409 if (*jpcol != *ncol) { | 410 if (*jpcol != *ncol) { |
| 410 errln("jpcol for fu_FU_FOO failed"); | 411 errln("jpcol for fu_FU_FOO failed"); |
| 411 } | 412 } |
| 412 | 413 |
| 414 // The requested locale may be the same as the valid locale, |
| 415 // or may not be supported at all. See ticket #10477. |
| 413 Locale loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status); | 416 Locale loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status); |
| 414 if (loc != fu_FU_FOO) { | 417 if (U_SUCCESS(status) && loc != fu_FU_FOO && loc != fu_FU) { |
| 415 errln(UnicodeString("requested locale for fu_FU_FOO is not fu_FU_FOO
but ") + loc.getName()); | 418 errln(UnicodeString("requested locale for fu_FU_FOO is not fu_FU_FOO
nor fu_FU but ") + loc.getName()); |
| 416 } | 419 } |
| 420 status = U_ZERO_ERROR; |
| 417 loc = ncol->getLocale(ULOC_VALID_LOCALE, status); | 421 loc = ncol->getLocale(ULOC_VALID_LOCALE, status); |
| 418 if (loc != fu_FU) { | 422 if (loc != fu_FU) { |
| 419 errln(UnicodeString("valid locale for fu_FU_FOO is not fu_FU but ")
+ loc.getName()); | 423 errln(UnicodeString("valid locale for fu_FU_FOO is not fu_FU but ")
+ loc.getName()); |
| 420 } | 424 } |
| 421 delete ncol; ncol = NULL; | 425 delete ncol; ncol = NULL; |
| 422 | 426 |
| 423 UnicodeString locName = fu_FU.getName(); | 427 UnicodeString locName = fu_FU.getName(); |
| 424 StringEnumeration* localeEnum = Collator::getAvailableLocales(); | 428 StringEnumeration* localeEnum = Collator::getAvailableLocales(); |
| 425 UBool found = FALSE; | 429 UBool found = FALSE; |
| 426 const UnicodeString* locStr; | 430 const UnicodeString* locStr; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 assertSuccess("getFunctionalEquivalent", ec); | 582 assertSuccess("getFunctionalEquivalent", ec); |
| 579 assertEquals("getFunctionalEquivalent(de)", "root", equiv.getName()); | 583 assertEquals("getFunctionalEquivalent(de)", "root", equiv.getName()); |
| 580 assertTrue("getFunctionalEquivalent(de).isAvailable==TRUE", | 584 assertTrue("getFunctionalEquivalent(de).isAvailable==TRUE", |
| 581 isAvailable == TRUE); | 585 isAvailable == TRUE); |
| 582 | 586 |
| 583 equiv = Collator::getFunctionalEquivalent("collation", | 587 equiv = Collator::getFunctionalEquivalent("collation", |
| 584 Locale::createFromName("de_DE"), | 588 Locale::createFromName("de_DE"), |
| 585 isAvailable, ec); | 589 isAvailable, ec); |
| 586 assertSuccess("getFunctionalEquivalent", ec); | 590 assertSuccess("getFunctionalEquivalent", ec); |
| 587 assertEquals("getFunctionalEquivalent(de_DE)", "root", equiv.getName()); | 591 assertEquals("getFunctionalEquivalent(de_DE)", "root", equiv.getName()); |
| 588 assertTrue("getFunctionalEquivalent(de_DE).isAvailable==TRUE", | 592 assertTrue("getFunctionalEquivalent(de_DE).isAvailable==FALSE", |
| 589 isAvailable == TRUE); | 593 isAvailable == FALSE); |
| 590 | 594 |
| 591 equiv = Collator::getFunctionalEquivalent("collation", | 595 equiv = Collator::getFunctionalEquivalent("collation", |
| 592 Locale::createFromName("sv"
), | 596 Locale::createFromName("sv"
), |
| 593 isAvailable, ec); | 597 isAvailable, ec); |
| 594 assertSuccess("getFunctionalEquivalent", ec); | 598 assertSuccess("getFunctionalEquivalent", ec); |
| 595 assertEquals("getFunctionalEquivalent(sv)", "sv", equiv.getName()); | 599 assertEquals("getFunctionalEquivalent(sv)", "sv", equiv.getName()); |
| 596 assertTrue("getFunctionalEquivalent(sv).isAvailable==TRUE", | 600 assertTrue("getFunctionalEquivalent(sv).isAvailable==TRUE", |
| 597 isAvailable == TRUE); | 601 isAvailable == TRUE); |
| 598 | 602 |
| 599 equiv = Collator::getFunctionalEquivalent("collation", | 603 equiv = Collator::getFunctionalEquivalent("collation", |
| 600 Locale::createFromName("sv_SE"), | 604 Locale::createFromName("sv_SE"), |
| 601 isAvailable, ec); | 605 isAvailable, ec); |
| 602 assertSuccess("getFunctionalEquivalent", ec); | 606 assertSuccess("getFunctionalEquivalent", ec); |
| 603 assertEquals("getFunctionalEquivalent(sv_SE)", "sv", equiv.getName()); | 607 assertEquals("getFunctionalEquivalent(sv_SE)", "sv", equiv.getName()); |
| 604 assertTrue("getFunctionalEquivalent(sv_SE).isAvailable==TRUE", | 608 assertTrue("getFunctionalEquivalent(sv_SE).isAvailable==FALSE", |
| 605 isAvailable == TRUE); | 609 isAvailable == FALSE); |
| 606 } | 610 } |
| 607 | 611 |
| 608 #endif | 612 #endif |
| OLD | NEW |