blob: 8d46c68f37f41f7fa70f204a44b9404e352873e1 [file] [log] [blame]
Fredrik Roubert0596fae2017-04-18 21:34:02 +02001// © 2016 and later: Unicode, Inc. and others.
Fredrik Roubert64339d32016-10-21 19:43:16 +02002// License & terms of use: http://www.unicode.org/copyright.html
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07003/*
4 *******************************************************************************
Fredrik Roubert8de051c2016-03-10 13:13:27 +01005 * Copyright (C) 2003-2016, International Business Machines Corporation and
ccorneliusfceb3982014-04-16 12:27:14 -07006 * others. All Rights Reserved.
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07007 *******************************************************************************
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 Roubert8de051c2016-03-10 13:13:27 +010017#include "cmemory.h"
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070018#include "hash.h"
19#include "uassert.h"
20
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070021#include "cstring.h" // internal api used to compare locale strings
22
23void 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 Querub0ac9372009-07-20 15:09:32 -070034void CollationServiceTest::TestRegister()
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070035{
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 Querub0ac9372009-07-20 15:09:32 -070041
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070042 UErrorCode status = U_ZERO_ERROR;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070043
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070044 Collator* frcol = Collator::createInstance(FR, status);
45 Collator* uscol = Collator::createInstance(US, status);
46 if(U_FAILURE(status)) {
Jean-Baptiste Queru6d5deb12009-07-20 15:14:11 -070047 errcheckln(status, "Failed to create collators with %s", u_errorName(status));
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070048 delete frcol;
49 delete uscol;
50 return;
51 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070052
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070053 { // try override en_US collator
ccorneliusfceb3982014-04-16 12:27:14 -070054 Collator *clone = frcol->clone();
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070055 URegistryKey key = Collator::registerInstance(frcol, US, status);
ccorneliusfceb3982014-04-16 12:27:14 -070056 // frcol has been adopted. We must not use it any more, nor rely on its attributes.
57 frcol = NULL;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070058
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070059 Collator* ncol = Collator::createInstance(US_FOO, status);
ccorneliusfceb3982014-04-16 12:27:14 -070060 if (*clone != *ncol) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070061 errln("register of french collator for en_US failed on request for en_US_FOO");
62 }
ccorneliusfceb3982014-04-16 12:27:14 -070063 delete clone;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070064
ccorneliusfceb3982014-04-16 12:27:14 -070065 // 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);
ccorneliusf9878a22014-11-20 18:09:39 -080068 if (U_SUCCESS(status) && loc != US_FOO && loc != US) {
ccorneliusfceb3982014-04-16 12:27:14 -070069 errln(UnicodeString("requested locale for en_US_FOO is not en_US_FOO nor en_US but ") + loc.getName());
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070070 }
ccorneliusf9878a22014-11-20 18:09:39 -080071 status = U_ZERO_ERROR;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070072 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 Querub0ac9372009-07-20 15:09:32 -070081
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070082 if (!Collator::unregister(key, status)) {
83 errln("failed to unregister french collator");
84 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070085
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070086 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 Querub0ac9372009-07-20 15:09:32 -070092
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070093 // recreate frcol
94 frcol = Collator::createInstance(FR, status);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070095
claireho50294ea2010-05-03 15:44:48 -070096 LocalUCollatorPointer frFR(ucol_open("fr_FR", &status));
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070097
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070098 { // try create collator for new locale
99 Locale fu_FU_FOO("fu", "FU", "FOO");
100 Locale fu_FU("fu", "FU", "");
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700101
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700102 Collator* fucol = Collator::createInstance(fu_FU, status);
ccorneliusfceb3982014-04-16 12:27:14 -0700103 Collator *clone = frcol->clone();
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700104 URegistryKey key = Collator::registerInstance(frcol, fu_FU, status);
ccorneliusfceb3982014-04-16 12:27:14 -0700105 frcol = NULL; // frcol has been adopted.
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700106 Collator* ncol = Collator::createInstance(fu_FU_FOO, status);
ccorneliusfceb3982014-04-16 12:27:14 -0700107 if (*clone != *ncol) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700108 errln("register of fr collator for fu_FU failed");
109 }
ccorneliusfceb3982014-04-16 12:27:14 -0700110 delete clone;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700111
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700112 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 Querub0ac9372009-07-20 15:09:32 -0700150
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700151 if (!found) {
152 errln("new locale fu_FU not reported as supported locale");
153 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700154
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700155 UnicodeString displayName;
156 Collator::getDisplayName(fu_FU, displayName);
claireho27f65472011-06-09 11:11:49 -0700157 /* 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 Querub13da9d2009-07-17 17:53:22 -0700164 errln(UnicodeString("found ") + displayName + " for fu_FU");
165 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700166
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700167 Collator::getDisplayName(fu_FU, fu_FU, displayName);
claireho27f65472011-06-09 11:11:49 -0700168 if (displayName != "fu (FU)" &&
169 ((defaultLocale == Locale::getKorean() && defaultLocale == Locale::getJapanese()) && displayName == "fu(FU)") &&
170 ((defaultLocale == Locale::getChinese()) && displayName != zh_fuFU)) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700171 errln(UnicodeString("found ") + displayName + " for fu_FU");
172 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700173
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700174 // test ucol_open
claireho50294ea2010-05-03 15:44:48 -0700175 LocalUCollatorPointer fufu(ucol_open("fu_FU_FOO", &status));
176 if (fufu.isNull()) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700177 errln("could not open fu_FU_FOO with ucol_open");
178 } else {
ccorneliusfceb3982014-04-16 12:27:14 -0700179 if (*Collator::fromUCollator(fufu.getAlias()) !=
180 *Collator::fromUCollator(frFR.getAlias())) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700181 errln("collator fufu != collator frFR");
182 }
183 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700184
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700185 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 Querub0ac9372009-07-20 15:09:32 -0700189
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700190 // 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 Querub0ac9372009-07-20 15:09:32 -0700196
claireho50294ea2010-05-03 15:44:48 -0700197 if (fufu.isValid()) {
198 const char* nlocstr = ucol_getLocaleByType(fufu.getAlias(), ULOC_VALID_LOCALE, &status);
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700199 if (uprv_strcmp(nlocstr, "fu_FU") != 0) {
200 errln(UnicodeString("asked for uloc valid locale after close and got ") + nlocstr);
201 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700202 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700203
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700204 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
218struct 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
228CollatorInfo::CollatorInfo(const Locale& _locale, Collator* _collator, Hashtable* _displayNames)
229 : locale(_locale)
230 , collator(_collator)
231 , displayNames(_displayNames)
232{
ccorneliusfceb3982014-04-16 12:27:14 -0700233 collator->setLocales(locale, locale, locale);
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700234}
235
236CollatorInfo::~CollatorInfo() {
237 delete collator;
238 delete displayNames;
239}
240
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700241UnicodeString&
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700242CollatorInfo::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
256class 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 Querub0ac9372009-07-20 15:09:32 -0700270public:
271 TestFactory(CollatorInfo** _info)
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700272 : 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 Querub0ac9372009-07-20 15:09:32 -0700297 virtual UnicodeString& getDisplayName(const Locale& objectLocale,
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700298 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
339private:
340 static char gClassID;
341};
342
343char TestFactory::gClassID = 0;
344#endif
345
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700346void CollationServiceTest::TestRegisterFactory(void)
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700347{
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 Querub0ac9372009-07-20 15:09:32 -0700352
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700353 UErrorCode status = U_ZERO_ERROR;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700354
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700355 Hashtable* fuFUNames = new Hashtable(FALSE, status);
356 if (!fuFUNames) {
357 errln("memory allocation error");
358 return;
359 }
Craig Cornelius83a171d2012-10-09 17:03:29 -0700360 fuFUNames->setValueDeleter(uprv_deleteUObject);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700361
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700362 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 Querub0ac9372009-07-20 15:09:32 -0700365
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700366 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 Queru6d5deb12009-07-20 15:14:11 -0700370 errcheckln(status, "Failed to create collators with %s", u_errorName(status));
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700371 delete frcol;
372 delete gecol;
373 delete jpcol;
374 delete fuFUNames;
375 return;
376 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700377
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700378 CollatorInfo** info = new CollatorInfo*[4];
379 if (!info) {
380 errln("memory allocation error");
381 return;
382 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700383
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700384 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 Querub0ac9372009-07-20 15:09:32 -0700388
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700389 TestFactory* factory = new TestFactory(info);
390 if (!factory) {
391 errln("memory allocation error");
392 return;
393 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700394
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700395 Collator* uscol = Collator::createInstance(Locale::getUS(), status);
396 Collator* fucol = Collator::createInstance(fu_FU, status);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700397
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700398 {
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 Querub0ac9372009-07-20 15:09:32 -0700411
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700412 ncol = Collator::createInstance(fu_FU_FOO, status);
413 if (*jpcol != *ncol) {
414 errln("jpcol for fu_FU_FOO failed");
415 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700416
ccorneliusfceb3982014-04-16 12:27:14 -0700417 // The requested locale may be the same as the valid locale,
418 // or may not be supported at all. See ticket #10477.
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700419 Locale loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status);
ccorneliusf9878a22014-11-20 18:09:39 -0800420 if (U_SUCCESS(status) && loc != fu_FU_FOO && loc != fu_FU) {
ccorneliusfceb3982014-04-16 12:27:14 -0700421 errln(UnicodeString("requested locale for fu_FU_FOO is not fu_FU_FOO nor fu_FU but ") + loc.getName());
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700422 }
ccorneliusf9878a22014-11-20 18:09:39 -0800423 status = U_ZERO_ERROR;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700424 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 Querub0ac9372009-07-20 15:09:32 -0700429
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700430 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 Querub0ac9372009-07-20 15:09:32 -0700443
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700444 if (!found) {
445 errln("new locale fu_FU not reported as supported locale");
446 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700447
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700448 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 Querub0ac9372009-07-20 15:09:32 -0700453
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700454 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 Querub0ac9372009-07-20 15:09:32 -0700458
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700459 if (!Collator::unregister(key, status)) {
460 errln("failed to unregister factory");
461 }
462 // ja, fr, ge collators no longer valid
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700463
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700464 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 Querub0ac9372009-07-20 15:09:32 -0700473
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700474 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 */
485int32_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
547int32_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
556static const char* KW[] = {
557 "collation"
558};
Fredrik Roubert8de051c2016-03-10 13:13:27 +0100559static const int32_t KW_COUNT = UPRV_LENGTHOF(KW);
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700560
561static const char* KWVAL[] = {
562 "phonebook",
563 "stroke"
564};
Fredrik Roubert8de051c2016-03-10 13:13:27 +0100565static const int32_t KWVAL_COUNT = UPRV_LENGTHOF(KWVAL);
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700566
567void 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 Querub0ac9372009-07-20 15:09:32 -0700574
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700575 iter = Collator::getKeywordValues(KW[0], ec);
Jean-Baptiste Queru6d5deb12009-07-20 15:14:11 -0700576 if (!assertTrue("getKeywordValues != NULL", iter!=NULL, FALSE, TRUE)) return;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700577 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",
claireho27f65472011-06-09 11:11:49 -0700583 Locale::createFromName("de"),
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700584 isAvailable, ec);
585 assertSuccess("getFunctionalEquivalent", ec);
Nikita Iashchenkoda0990f2019-06-13 19:36:45 +0100586 assertEquals("getFunctionalEquivalent(de)", "", equiv.getName());
claireho27f65472011-06-09 11:11:49 -0700587 assertTrue("getFunctionalEquivalent(de).isAvailable==TRUE",
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700588 isAvailable == TRUE);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700589
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700590 equiv = Collator::getFunctionalEquivalent("collation",
claireho27f65472011-06-09 11:11:49 -0700591 Locale::createFromName("de_DE"),
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700592 isAvailable, ec);
593 assertSuccess("getFunctionalEquivalent", ec);
Nikita Iashchenkoda0990f2019-06-13 19:36:45 +0100594 assertEquals("getFunctionalEquivalent(de_DE)", "", equiv.getName());
ccorneliusfceb3982014-04-16 12:27:14 -0700595 assertTrue("getFunctionalEquivalent(de_DE).isAvailable==FALSE",
596 isAvailable == FALSE);
ccornelius59d709d2014-02-20 10:29:46 -0800597
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());
ccorneliusfceb3982014-04-16 12:27:14 -0700611 assertTrue("getFunctionalEquivalent(sv_SE).isAvailable==FALSE",
612 isAvailable == FALSE);
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700613}
614
615#endif