Fredrik Roubert | 0596fae | 2017-04-18 21:34:02 +0200 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Fredrik Roubert | 64339d3 | 2016-10-21 19:43:16 +0200 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 3 | /* |
| 4 | ******************************************************************************* |
Fredrik Roubert | 8de051c | 2016-03-10 13:13:27 +0100 | [diff] [blame] | 5 | * Copyright (C) 2003-2016, International Business Machines Corporation and |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 6 | * others. All Rights Reserved. |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 7 | ******************************************************************************* |
| 8 | */ |
| 9 | |
| 10 | #include "unicode/utypes.h" |
| 11 | |
| 12 | #if !UCONFIG_NO_COLLATION |
| 13 | |
| 14 | #include "svccoll.h" |
| 15 | #include "unicode/coll.h" |
| 16 | #include "unicode/strenum.h" |
Fredrik Roubert | 8de051c | 2016-03-10 13:13:27 +0100 | [diff] [blame] | 17 | #include "cmemory.h" |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 18 | #include "hash.h" |
| 19 | #include "uassert.h" |
| 20 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 21 | #include "cstring.h" // internal api used to compare locale strings |
| 22 | |
| 23 | void CollationServiceTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par */) |
| 24 | { |
| 25 | if (exec) logln("TestSuite CollationServiceTest: "); |
| 26 | switch (index) { |
| 27 | TESTCASE(0, TestRegister); |
| 28 | TESTCASE(1, TestRegisterFactory); |
| 29 | TESTCASE(2, TestSeparateTree); |
| 30 | default: name = ""; break; |
| 31 | } |
| 32 | } |
| 33 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 34 | void CollationServiceTest::TestRegister() |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 35 | { |
| 36 | #if !UCONFIG_NO_SERVICE |
| 37 | // register a singleton |
| 38 | const Locale& FR = Locale::getFrance(); |
| 39 | const Locale& US = Locale::getUS(); |
| 40 | const Locale US_FOO("en", "US", "FOO"); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 41 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 42 | UErrorCode status = U_ZERO_ERROR; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 43 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 44 | Collator* frcol = Collator::createInstance(FR, status); |
| 45 | Collator* uscol = Collator::createInstance(US, status); |
| 46 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | 6d5deb1 | 2009-07-20 15:14:11 -0700 | [diff] [blame] | 47 | errcheckln(status, "Failed to create collators with %s", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 48 | delete frcol; |
| 49 | delete uscol; |
| 50 | return; |
| 51 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 52 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 53 | { // try override en_US collator |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 54 | Collator *clone = frcol->clone(); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 55 | URegistryKey key = Collator::registerInstance(frcol, US, status); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 56 | // frcol has been adopted. We must not use it any more, nor rely on its attributes. |
| 57 | frcol = NULL; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 58 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 59 | Collator* ncol = Collator::createInstance(US_FOO, status); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 60 | if (*clone != *ncol) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 61 | errln("register of french collator for en_US failed on request for en_US_FOO"); |
| 62 | } |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 63 | delete clone; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 64 | |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 65 | // The requested locale may be the same as the valid locale, |
| 66 | // or may not be supported at all. See ticket #10477. |
| 67 | Locale loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status); |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 68 | if (U_SUCCESS(status) && loc != US_FOO && loc != US) { |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 69 | errln(UnicodeString("requested locale for en_US_FOO is not en_US_FOO nor en_US but ") + loc.getName()); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 70 | } |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 71 | status = U_ZERO_ERROR; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 72 | loc = ncol->getLocale(ULOC_VALID_LOCALE, status); |
| 73 | if (loc != US) { |
| 74 | errln(UnicodeString("valid locale for en_US_FOO is not en_US but ") + loc.getName()); |
| 75 | } |
| 76 | loc = ncol->getLocale(ULOC_ACTUAL_LOCALE, status); |
| 77 | if (loc != US) { |
| 78 | errln(UnicodeString("actual locale for en_US_FOO is not en_US but ") + loc.getName()); |
| 79 | } |
| 80 | delete ncol; ncol = NULL; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 81 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 82 | if (!Collator::unregister(key, status)) { |
| 83 | errln("failed to unregister french collator"); |
| 84 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 85 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 86 | ncol = Collator::createInstance(US, status); |
| 87 | if (*uscol != *ncol) { |
| 88 | errln("collator after unregister does not match original"); |
| 89 | } |
| 90 | delete ncol; ncol = NULL; |
| 91 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 92 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 93 | // recreate frcol |
| 94 | frcol = Collator::createInstance(FR, status); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 95 | |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 96 | LocalUCollatorPointer frFR(ucol_open("fr_FR", &status)); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 97 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 98 | { // try create collator for new locale |
| 99 | Locale fu_FU_FOO("fu", "FU", "FOO"); |
| 100 | Locale fu_FU("fu", "FU", ""); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 101 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 102 | Collator* fucol = Collator::createInstance(fu_FU, status); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 103 | Collator *clone = frcol->clone(); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 104 | URegistryKey key = Collator::registerInstance(frcol, fu_FU, status); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 105 | frcol = NULL; // frcol has been adopted. |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 106 | Collator* ncol = Collator::createInstance(fu_FU_FOO, status); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 107 | if (*clone != *ncol) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 108 | errln("register of fr collator for fu_FU failed"); |
| 109 | } |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 110 | delete clone; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 111 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 112 | UnicodeString locName = fu_FU.getName(); |
| 113 | StringEnumeration* localeEnum = Collator::getAvailableLocales(); |
| 114 | UBool found = FALSE; |
| 115 | const UnicodeString* locStr, *ls2; |
| 116 | for (locStr = localeEnum->snext(status); |
| 117 | !found && locStr != NULL; |
| 118 | locStr = localeEnum->snext(status)) { |
| 119 | // |
| 120 | if (locName == *locStr) { |
| 121 | found = TRUE; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | StringEnumeration *le2 = NULL; |
| 126 | localeEnum->reset(status); |
| 127 | int32_t i, count; |
| 128 | count = localeEnum->count(status); |
| 129 | for(i = 0; i < count; ++i) { |
| 130 | if(i == count / 2) { |
| 131 | le2 = localeEnum->clone(); |
| 132 | if(le2 == NULL || count != le2->count(status)) { |
| 133 | errln("ServiceEnumeration.clone() failed"); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | if(i >= count / 2) { |
| 138 | locStr = localeEnum->snext(status); |
| 139 | ls2 = le2->snext(status); |
| 140 | if(*locStr != *ls2) { |
| 141 | errln("ServiceEnumeration.clone() failed for item %d", i); |
| 142 | } |
| 143 | } else { |
| 144 | localeEnum->snext(status); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | delete localeEnum; |
| 149 | delete le2; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 150 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 151 | if (!found) { |
| 152 | errln("new locale fu_FU not reported as supported locale"); |
| 153 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 154 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 155 | UnicodeString displayName; |
| 156 | Collator::getDisplayName(fu_FU, displayName); |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 157 | /* The locale display pattern for the locale ja, ko, and zh are different. */ |
| 158 | const UChar zh_fuFU_Array[] = { 0x0066, 0x0075, 0xff08, 0x0046, 0x0055, 0xff09, 0 }; |
| 159 | const UnicodeString zh_fuFU(zh_fuFU_Array); |
| 160 | const Locale& defaultLocale = Locale::getDefault(); |
| 161 | if (displayName != "fu (FU)" && |
| 162 | ((defaultLocale == Locale::getKorean() && defaultLocale == Locale::getJapanese()) && displayName == "fu(FU)") && |
| 163 | ((defaultLocale == Locale::getChinese()) && displayName != zh_fuFU)) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 164 | errln(UnicodeString("found ") + displayName + " for fu_FU"); |
| 165 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 166 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 167 | Collator::getDisplayName(fu_FU, fu_FU, displayName); |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 168 | if (displayName != "fu (FU)" && |
| 169 | ((defaultLocale == Locale::getKorean() && defaultLocale == Locale::getJapanese()) && displayName == "fu(FU)") && |
| 170 | ((defaultLocale == Locale::getChinese()) && displayName != zh_fuFU)) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 171 | errln(UnicodeString("found ") + displayName + " for fu_FU"); |
| 172 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 173 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 174 | // test ucol_open |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 175 | LocalUCollatorPointer fufu(ucol_open("fu_FU_FOO", &status)); |
| 176 | if (fufu.isNull()) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 177 | errln("could not open fu_FU_FOO with ucol_open"); |
| 178 | } else { |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 179 | if (*Collator::fromUCollator(fufu.getAlias()) != |
| 180 | *Collator::fromUCollator(frFR.getAlias())) { |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 181 | errln("collator fufu != collator frFR"); |
| 182 | } |
| 183 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 184 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 185 | if (!Collator::unregister(key, status)) { |
| 186 | errln("failed to unregister french collator"); |
| 187 | } |
| 188 | // !!! note frcoll invalid again, but we're no longer using it |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 189 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 190 | // other collators should still work ok |
| 191 | Locale nloc = ncol->getLocale(ULOC_VALID_LOCALE, status); |
| 192 | if (nloc != fu_FU) { |
| 193 | errln(UnicodeString("asked for nloc valid locale after close and got") + nloc.getName()); |
| 194 | } |
| 195 | delete ncol; ncol = NULL; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 196 | |
claireho | 50294ea | 2010-05-03 15:44:48 -0700 | [diff] [blame] | 197 | if (fufu.isValid()) { |
| 198 | const char* nlocstr = ucol_getLocaleByType(fufu.getAlias(), ULOC_VALID_LOCALE, &status); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 199 | if (uprv_strcmp(nlocstr, "fu_FU") != 0) { |
| 200 | errln(UnicodeString("asked for uloc valid locale after close and got ") + nlocstr); |
| 201 | } |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 202 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 203 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 204 | ncol = Collator::createInstance(fu_FU, status); |
| 205 | if (*fucol != *ncol) { |
| 206 | errln("collator after unregister does not match original fu_FU"); |
| 207 | } |
| 208 | delete uscol; uscol = NULL; |
| 209 | delete ncol; ncol = NULL; |
| 210 | delete fucol; fucol = NULL; |
| 211 | } |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | // ------------------ |
| 216 | |
| 217 | #if !UCONFIG_NO_SERVICE |
| 218 | struct CollatorInfo { |
| 219 | Locale locale; |
| 220 | Collator* collator; |
| 221 | Hashtable* displayNames; // locale name -> string |
| 222 | |
| 223 | CollatorInfo(const Locale& locale, Collator* collatorToAdopt, Hashtable* displayNamesToAdopt); |
| 224 | ~CollatorInfo(); |
| 225 | UnicodeString& getDisplayName(const Locale& displayLocale, UnicodeString& name) const; |
| 226 | }; |
| 227 | |
| 228 | CollatorInfo::CollatorInfo(const Locale& _locale, Collator* _collator, Hashtable* _displayNames) |
| 229 | : locale(_locale) |
| 230 | , collator(_collator) |
| 231 | , displayNames(_displayNames) |
| 232 | { |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 233 | collator->setLocales(locale, locale, locale); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | CollatorInfo::~CollatorInfo() { |
| 237 | delete collator; |
| 238 | delete displayNames; |
| 239 | } |
| 240 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 241 | UnicodeString& |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 242 | CollatorInfo::getDisplayName(const Locale& displayLocale, UnicodeString& name) const { |
| 243 | if (displayNames) { |
| 244 | UnicodeString* val = (UnicodeString*)displayNames->get(displayLocale.getName()); |
| 245 | if (val) { |
| 246 | name = *val; |
| 247 | return name; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return locale.getDisplayName(displayLocale, name); |
| 252 | } |
| 253 | |
| 254 | // --------------- |
| 255 | |
| 256 | class TestFactory : public CollatorFactory { |
| 257 | CollatorInfo** info; |
| 258 | int32_t count; |
| 259 | UnicodeString* ids; |
| 260 | |
| 261 | const CollatorInfo* getInfo(const Locale& loc) const { |
| 262 | for (CollatorInfo** p = info; *p; ++p) { |
| 263 | if (loc == (**p).locale) { |
| 264 | return *p; |
| 265 | } |
| 266 | } |
| 267 | return NULL; |
| 268 | } |
| 269 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 270 | public: |
| 271 | TestFactory(CollatorInfo** _info) |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 272 | : info(_info) |
| 273 | , count(0) |
| 274 | , ids(NULL) |
| 275 | { |
| 276 | CollatorInfo** p; |
| 277 | for (p = info; *p; ++p) {} |
| 278 | count = (int32_t)(p - info); |
| 279 | } |
| 280 | |
| 281 | ~TestFactory() { |
| 282 | for (CollatorInfo** p = info; *p; ++p) { |
| 283 | delete *p; |
| 284 | } |
| 285 | delete[] info; |
| 286 | delete[] ids; |
| 287 | } |
| 288 | |
| 289 | virtual Collator* createCollator(const Locale& loc) { |
| 290 | const CollatorInfo* ci = getInfo(loc); |
| 291 | if (ci) { |
| 292 | return ci->collator->clone(); |
| 293 | } |
| 294 | return NULL; |
| 295 | } |
| 296 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 297 | virtual UnicodeString& getDisplayName(const Locale& objectLocale, |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 298 | const Locale& displayLocale, |
| 299 | UnicodeString& result) |
| 300 | { |
| 301 | const CollatorInfo* ci = getInfo(objectLocale); |
| 302 | if (ci) { |
| 303 | ci->getDisplayName(displayLocale, result); |
| 304 | } else { |
| 305 | result.setToBogus(); |
| 306 | } |
| 307 | return result; |
| 308 | } |
| 309 | |
| 310 | const UnicodeString* getSupportedIDs(int32_t& _count, UErrorCode& status) { |
| 311 | if (U_SUCCESS(status)) { |
| 312 | if (!ids) { |
| 313 | ids = new UnicodeString[count]; |
| 314 | if (!ids) { |
| 315 | status = U_MEMORY_ALLOCATION_ERROR; |
| 316 | _count = 0; |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | for (int i = 0; i < count; ++i) { |
| 321 | ids[i] = info[i]->locale.getName(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | _count = count; |
| 326 | return ids; |
| 327 | } |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | virtual inline UClassID getDynamicClassID() const { |
| 332 | return (UClassID)&gClassID; |
| 333 | } |
| 334 | |
| 335 | static UClassID getStaticClassID() { |
| 336 | return (UClassID)&gClassID; |
| 337 | } |
| 338 | |
| 339 | private: |
| 340 | static char gClassID; |
| 341 | }; |
| 342 | |
| 343 | char TestFactory::gClassID = 0; |
| 344 | #endif |
| 345 | |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 346 | void CollationServiceTest::TestRegisterFactory(void) |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 347 | { |
| 348 | #if !UCONFIG_NO_SERVICE |
| 349 | int32_t n1, n2, n3; |
| 350 | Locale fu_FU("fu", "FU", ""); |
| 351 | Locale fu_FU_FOO("fu", "FU", "FOO"); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 352 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 353 | UErrorCode status = U_ZERO_ERROR; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 354 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 355 | Hashtable* fuFUNames = new Hashtable(FALSE, status); |
| 356 | if (!fuFUNames) { |
| 357 | errln("memory allocation error"); |
| 358 | return; |
| 359 | } |
Craig Cornelius | 83a171d | 2012-10-09 17:03:29 -0700 | [diff] [blame] | 360 | fuFUNames->setValueDeleter(uprv_deleteUObject); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 361 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 362 | fuFUNames->put(fu_FU.getName(), new UnicodeString("ze leetle bunny Fu-Fu"), status); |
| 363 | fuFUNames->put(fu_FU_FOO.getName(), new UnicodeString("zee leetel bunny Foo-Foo"), status); |
| 364 | fuFUNames->put(Locale::getDefault().getName(), new UnicodeString("little bunny Foo Foo"), status); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 365 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 366 | Collator* frcol = Collator::createInstance(Locale::getFrance(), status); |
| 367 | Collator* gecol = Collator::createInstance(Locale::getGermany(), status); |
| 368 | Collator* jpcol = Collator::createInstance(Locale::getJapan(), status); |
| 369 | if(U_FAILURE(status)) { |
Jean-Baptiste Queru | 6d5deb1 | 2009-07-20 15:14:11 -0700 | [diff] [blame] | 370 | errcheckln(status, "Failed to create collators with %s", u_errorName(status)); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 371 | delete frcol; |
| 372 | delete gecol; |
| 373 | delete jpcol; |
| 374 | delete fuFUNames; |
| 375 | return; |
| 376 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 377 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 378 | CollatorInfo** info = new CollatorInfo*[4]; |
| 379 | if (!info) { |
| 380 | errln("memory allocation error"); |
| 381 | return; |
| 382 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 383 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 384 | info[0] = new CollatorInfo(Locale::getUS(), frcol, NULL); |
| 385 | info[1] = new CollatorInfo(Locale::getFrance(), gecol, NULL); |
| 386 | info[2] = new CollatorInfo(fu_FU, jpcol, fuFUNames); |
| 387 | info[3] = NULL; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 388 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 389 | TestFactory* factory = new TestFactory(info); |
| 390 | if (!factory) { |
| 391 | errln("memory allocation error"); |
| 392 | return; |
| 393 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 394 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 395 | Collator* uscol = Collator::createInstance(Locale::getUS(), status); |
| 396 | Collator* fucol = Collator::createInstance(fu_FU, status); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 397 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 398 | { |
| 399 | n1 = checkAvailable("before registerFactory"); |
| 400 | |
| 401 | URegistryKey key = Collator::registerFactory(factory, status); |
| 402 | |
| 403 | n2 = checkAvailable("after registerFactory"); |
| 404 | assertTrue("count after > count before", n2 > n1); |
| 405 | |
| 406 | Collator* ncol = Collator::createInstance(Locale::getUS(), status); |
| 407 | if (*frcol != *ncol) { |
| 408 | errln("frcoll for en_US failed"); |
| 409 | } |
| 410 | delete ncol; ncol = NULL; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 411 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 412 | ncol = Collator::createInstance(fu_FU_FOO, status); |
| 413 | if (*jpcol != *ncol) { |
| 414 | errln("jpcol for fu_FU_FOO failed"); |
| 415 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 416 | |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 417 | // The requested locale may be the same as the valid locale, |
| 418 | // or may not be supported at all. See ticket #10477. |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 419 | Locale loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status); |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 420 | if (U_SUCCESS(status) && loc != fu_FU_FOO && loc != fu_FU) { |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 421 | errln(UnicodeString("requested locale for fu_FU_FOO is not fu_FU_FOO nor fu_FU but ") + loc.getName()); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 422 | } |
ccornelius | f9878a2 | 2014-11-20 18:09:39 -0800 | [diff] [blame] | 423 | status = U_ZERO_ERROR; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 424 | loc = ncol->getLocale(ULOC_VALID_LOCALE, status); |
| 425 | if (loc != fu_FU) { |
| 426 | errln(UnicodeString("valid locale for fu_FU_FOO is not fu_FU but ") + loc.getName()); |
| 427 | } |
| 428 | delete ncol; ncol = NULL; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 429 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 430 | UnicodeString locName = fu_FU.getName(); |
| 431 | StringEnumeration* localeEnum = Collator::getAvailableLocales(); |
| 432 | UBool found = FALSE; |
| 433 | const UnicodeString* locStr; |
| 434 | for (locStr = localeEnum->snext(status); |
| 435 | !found && locStr != NULL; |
| 436 | locStr = localeEnum->snext(status)) |
| 437 | { |
| 438 | if (locName == *locStr) { |
| 439 | found = TRUE; |
| 440 | } |
| 441 | } |
| 442 | delete localeEnum; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 443 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 444 | if (!found) { |
| 445 | errln("new locale fu_FU not reported as supported locale"); |
| 446 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 447 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 448 | UnicodeString name; |
| 449 | Collator::getDisplayName(fu_FU, name); |
| 450 | if (name != "little bunny Foo Foo") { |
| 451 | errln(UnicodeString("found ") + name + " for fu_FU"); |
| 452 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 453 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 454 | Collator::getDisplayName(fu_FU, fu_FU_FOO, name); |
| 455 | if (name != "zee leetel bunny Foo-Foo") { |
| 456 | errln(UnicodeString("found ") + name + " for fu_FU in fu_FU_FOO"); |
| 457 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 458 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 459 | if (!Collator::unregister(key, status)) { |
| 460 | errln("failed to unregister factory"); |
| 461 | } |
| 462 | // ja, fr, ge collators no longer valid |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 463 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 464 | ncol = Collator::createInstance(fu_FU, status); |
| 465 | if (*fucol != *ncol) { |
| 466 | errln("collator after unregister does not match original fu_FU"); |
| 467 | } |
| 468 | delete ncol; |
| 469 | |
| 470 | n3 = checkAvailable("after unregister"); |
| 471 | assertTrue("count after unregister == count before register", n3 == n1); |
| 472 | } |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 473 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 474 | delete fucol; |
| 475 | delete uscol; |
| 476 | #endif |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Iterate through the given iterator, checking to see that all the strings |
| 481 | * in the expected array are present. |
| 482 | * @param expected array of strings we expect to see, or NULL |
| 483 | * @param expectedCount number of elements of expected, or 0 |
| 484 | */ |
| 485 | int32_t CollationServiceTest::checkStringEnumeration(const char* msg, |
| 486 | StringEnumeration& iter, |
| 487 | const char** expected, |
| 488 | int32_t expectedCount) { |
| 489 | UErrorCode ec = U_ZERO_ERROR; |
| 490 | U_ASSERT(expectedCount >= 0 && expectedCount < 31); // [sic] 31 not 32 |
| 491 | int32_t i = 0, idxAfterReset = 0, n = iter.count(ec); |
| 492 | assertSuccess("count", ec); |
| 493 | UnicodeString buf, buffAfterReset; |
| 494 | int32_t seenMask = 0; |
| 495 | for (;; ++i) { |
| 496 | const UnicodeString* s = iter.snext(ec); |
| 497 | if (!assertSuccess("snext", ec) || s == NULL) |
| 498 | break; |
| 499 | if (i != 0) |
| 500 | buf.append(UNICODE_STRING_SIMPLE(", ")); |
| 501 | buf.append(*s); |
| 502 | // check expected list |
| 503 | for (int32_t j=0, bit=1; j<expectedCount; ++j, bit<<=1) { |
| 504 | if ((seenMask&bit)==0) { |
| 505 | UnicodeString exp(expected[j], (char*)NULL); |
| 506 | if (*s == exp) { |
| 507 | seenMask |= bit; |
| 508 | logln((UnicodeString)"Ok: \"" + exp + "\" seen"); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | // can't get pesky operator+(const US&, foo) to cooperate; use toString |
| 514 | #if !UCONFIG_NO_FORMATTING |
| 515 | logln(UnicodeString() + msg + " = [" + buf + "] (" + toString(i) + ")"); |
| 516 | #else |
| 517 | logln(UnicodeString() + msg + " = [" + buf + "] (??? NO_FORMATTING)"); |
| 518 | #endif |
| 519 | assertTrue("count verified", i==n); |
| 520 | iter.reset(ec); |
| 521 | for (;; ++idxAfterReset) { |
| 522 | const UChar *s = iter.unext(NULL, ec); |
| 523 | if (!assertSuccess("unext", ec) || s == NULL) |
| 524 | break; |
| 525 | if (idxAfterReset != 0) |
| 526 | buffAfterReset.append(UNICODE_STRING_SIMPLE(", ")); |
| 527 | buffAfterReset.append(s); |
| 528 | } |
| 529 | assertTrue("idxAfterReset verified", idxAfterReset==n); |
| 530 | assertTrue("buffAfterReset verified", buffAfterReset==buf); |
| 531 | // did we see all expected strings? |
| 532 | if (((1<<expectedCount)-1) != seenMask) { |
| 533 | for (int32_t j=0, bit=1; j<expectedCount; ++j, bit<<=1) { |
| 534 | if ((seenMask&bit)==0) { |
| 535 | errln((UnicodeString)"FAIL: \"" + expected[j] + "\" not seen"); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | return n; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Check the integrity of the results of Collator::getAvailableLocales(). |
| 544 | * Return the number of items returned. |
| 545 | */ |
| 546 | #if !UCONFIG_NO_SERVICE |
| 547 | int32_t CollationServiceTest::checkAvailable(const char* msg) { |
| 548 | StringEnumeration *iter = Collator::getAvailableLocales(); |
| 549 | if (!assertTrue("getAvailableLocales != NULL", iter!=NULL)) return -1; |
| 550 | int32_t n = checkStringEnumeration(msg, *iter, NULL, 0); |
| 551 | delete iter; |
| 552 | return n; |
| 553 | } |
| 554 | #endif |
| 555 | |
| 556 | static const char* KW[] = { |
| 557 | "collation" |
| 558 | }; |
Fredrik Roubert | 8de051c | 2016-03-10 13:13:27 +0100 | [diff] [blame] | 559 | static const int32_t KW_COUNT = UPRV_LENGTHOF(KW); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 560 | |
| 561 | static const char* KWVAL[] = { |
| 562 | "phonebook", |
| 563 | "stroke" |
| 564 | }; |
Fredrik Roubert | 8de051c | 2016-03-10 13:13:27 +0100 | [diff] [blame] | 565 | static const int32_t KWVAL_COUNT = UPRV_LENGTHOF(KWVAL); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 566 | |
| 567 | void CollationServiceTest::TestSeparateTree() { |
| 568 | UErrorCode ec = U_ZERO_ERROR; |
| 569 | StringEnumeration *iter = Collator::getKeywords(ec); |
| 570 | if (!assertTrue("getKeywords != NULL", iter!=NULL)) return; |
| 571 | if (!assertSuccess("getKeywords", ec)) return; |
| 572 | checkStringEnumeration("getKeywords", *iter, KW, KW_COUNT); |
| 573 | delete iter; |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 574 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 575 | iter = Collator::getKeywordValues(KW[0], ec); |
Jean-Baptiste Queru | 6d5deb1 | 2009-07-20 15:14:11 -0700 | [diff] [blame] | 576 | if (!assertTrue("getKeywordValues != NULL", iter!=NULL, FALSE, TRUE)) return; |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 577 | if (!assertSuccess("getKeywordValues", ec)) return; |
| 578 | checkStringEnumeration("getKeywordValues", *iter, KWVAL, KWVAL_COUNT); |
| 579 | delete iter; |
| 580 | |
| 581 | UBool isAvailable; |
| 582 | Locale equiv = Collator::getFunctionalEquivalent("collation", |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 583 | Locale::createFromName("de"), |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 584 | isAvailable, ec); |
| 585 | assertSuccess("getFunctionalEquivalent", ec); |
Nikita Iashchenko | da0990f | 2019-06-13 19:36:45 +0100 | [diff] [blame] | 586 | assertEquals("getFunctionalEquivalent(de)", "", equiv.getName()); |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 587 | assertTrue("getFunctionalEquivalent(de).isAvailable==TRUE", |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 588 | isAvailable == TRUE); |
Jean-Baptiste Queru | b0ac937 | 2009-07-20 15:09:32 -0700 | [diff] [blame] | 589 | |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 590 | equiv = Collator::getFunctionalEquivalent("collation", |
claireho | 27f6547 | 2011-06-09 11:11:49 -0700 | [diff] [blame] | 591 | Locale::createFromName("de_DE"), |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 592 | isAvailable, ec); |
| 593 | assertSuccess("getFunctionalEquivalent", ec); |
Nikita Iashchenko | da0990f | 2019-06-13 19:36:45 +0100 | [diff] [blame] | 594 | assertEquals("getFunctionalEquivalent(de_DE)", "", equiv.getName()); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 595 | assertTrue("getFunctionalEquivalent(de_DE).isAvailable==FALSE", |
| 596 | isAvailable == FALSE); |
ccornelius | 59d709d | 2014-02-20 10:29:46 -0800 | [diff] [blame] | 597 | |
| 598 | equiv = Collator::getFunctionalEquivalent("collation", |
| 599 | Locale::createFromName("sv"), |
| 600 | isAvailable, ec); |
| 601 | assertSuccess("getFunctionalEquivalent", ec); |
| 602 | assertEquals("getFunctionalEquivalent(sv)", "sv", equiv.getName()); |
| 603 | assertTrue("getFunctionalEquivalent(sv).isAvailable==TRUE", |
| 604 | isAvailable == TRUE); |
| 605 | |
| 606 | equiv = Collator::getFunctionalEquivalent("collation", |
| 607 | Locale::createFromName("sv_SE"), |
| 608 | isAvailable, ec); |
| 609 | assertSuccess("getFunctionalEquivalent", ec); |
| 610 | assertEquals("getFunctionalEquivalent(sv_SE)", "sv", equiv.getName()); |
ccornelius | fceb398 | 2014-04-16 12:27:14 -0700 | [diff] [blame] | 611 | assertTrue("getFunctionalEquivalent(sv_SE).isAvailable==FALSE", |
| 612 | isAvailable == FALSE); |
Jean-Baptiste Queru | b13da9d | 2009-07-17 17:53:22 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | #endif |