]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/alphaindextst.cpp
f1b9b13878e94ec1efb45289d7f14ce4724c59a8
[apple/icu.git] / icuSources / test / intltest / alphaindextst.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2012-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ********************************************************************/
8 //
9 // file: alphaindextst.cpp
10 // Alphabetic Index Tests.
11 //
12 // Note: please... no character literals cast to UChars.. use (UChar)0xZZZZ
13
14 #include <stdio.h> // for sprintf
15
16 #include "intltest.h"
17 #include "alphaindextst.h"
18 #include "cmemory.h"
19
20 #include "unicode/alphaindex.h"
21 #include "unicode/coll.h"
22 #include "unicode/localpointer.h"
23 #include "unicode/tblcoll.h"
24 #include "unicode/uniset.h"
25 #include "unicode/uscript.h"
26
27 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_NORMALIZATION
28
29 // #include <string>
30 // #include <iostream>
31
32 namespace {
33
34 UnicodeString joinLabelsAndAppend(AlphabeticIndex::ImmutableIndex &index, UnicodeString &dest) {
35 int32_t oldLength = dest.length();
36 const AlphabeticIndex::Bucket *bucket;
37 for (int32_t i = 0; (bucket = index.getBucket(i)) != NULL; ++i) {
38 if (dest.length() > oldLength) {
39 dest.append((UChar)0x3A); // ':'
40 }
41 dest.append(bucket->getLabel());
42 }
43 return dest;
44 }
45
46 } // namespace
47
48 AlphabeticIndexTest::AlphabeticIndexTest() {
49 }
50
51 AlphabeticIndexTest::~AlphabeticIndexTest() {
52 }
53
54 void AlphabeticIndexTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
55 {
56 if (exec) logln("TestSuite AlphabeticIndex: ");
57 TESTCASE_AUTO_BEGIN;
58 TESTCASE_AUTO(APITest);
59 TESTCASE_AUTO(ManyLocalesTest);
60 TESTCASE_AUTO(HackPinyinTest);
61 TESTCASE_AUTO(TestBug9009);
62 TESTCASE_AUTO(TestIndexCharactersList);
63 TESTCASE_AUTO(TestHaniFirst);
64 TESTCASE_AUTO(TestPinyinFirst);
65 TESTCASE_AUTO(TestSchSt);
66 TESTCASE_AUTO(TestNoLabels);
67 TESTCASE_AUTO(TestChineseZhuyin);
68 TESTCASE_AUTO(TestJapaneseKanji);
69 TESTCASE_AUTO(TestChineseUnihan);
70 TESTCASE_AUTO(testHasBuckets);
71 TESTCASE_AUTO_END;
72 }
73
74 #define TEST_CHECK_STATUS UPRV_BLOCK_MACRO_BEGIN { \
75 if (U_FAILURE(status)) { \
76 dataerrln("%s:%d: Test failure. status=%s", \
77 __FILE__, __LINE__, u_errorName(status)); \
78 return; \
79 } \
80 } UPRV_BLOCK_MACRO_END
81
82 #define TEST_ASSERT(expr) UPRV_BLOCK_MACRO_BEGIN { \
83 if ((expr)==FALSE) { \
84 errln("%s:%d: Test failure \n", __FILE__, __LINE__); \
85 } \
86 } UPRV_BLOCK_MACRO_END
87
88 //
89 // APITest. Invoke every function at least once, and check that it does something.
90 // Does not attempt to check complete functionality.
91 //
92 void AlphabeticIndexTest::APITest() {
93 //
94 // Simple constructor and destructor, getBucketCount()
95 //
96 UErrorCode status = U_ZERO_ERROR;
97 int32_t lc = 0;
98 int32_t i = 0;
99 LocalPointer<AlphabeticIndex> index(new AlphabeticIndex(Locale::getEnglish(), status));
100 TEST_CHECK_STATUS;
101 lc = index->getBucketCount(status);
102 TEST_CHECK_STATUS;
103 TEST_ASSERT(28 == lc); // 26 letters plus two under/overflow labels.
104 //printf("getBucketCount() == %d\n", lc);
105 index.adoptInstead(nullptr);
106
107 // Constructor from a Collator
108 //
109 status = U_ZERO_ERROR;
110 LocalPointer<RuleBasedCollator> coll(dynamic_cast<RuleBasedCollator *>(
111 Collator::createInstance(Locale::getGerman(), status)), status);
112 TEST_CHECK_STATUS;
113 TEST_ASSERT(coll.isValid());
114 RuleBasedCollator *originalColl = coll.getAlias();
115 index.adoptInstead(new AlphabeticIndex(coll.orphan(), status));
116 TEST_CHECK_STATUS;
117 TEST_ASSERT(originalColl == &index->getCollator());
118 assertEquals("only the underflow label in an index built from a collator",
119 1, index->getBucketCount(status));
120 TEST_CHECK_STATUS;
121 index.adoptInstead(nullptr);
122
123
124 // addLabels()
125
126 status = U_ZERO_ERROR;
127 index.adoptInstead(new AlphabeticIndex(Locale::getEnglish(), status));
128 TEST_CHECK_STATUS;
129 UnicodeSet additions;
130 additions.add((UChar32)0x410).add((UChar32)0x415); // A couple of Cyrillic letters
131 index->addLabels(additions, status);
132 TEST_CHECK_STATUS;
133 lc = index->getBucketCount(status);
134 TEST_CHECK_STATUS;
135 assertEquals("underflow, A-Z, inflow, 2 Cyrillic, overflow",
136 31, index->getBucketCount(status));
137 // std::cout << lc << std::endl;
138 index.adoptInstead(nullptr);
139
140
141 // addLabels(Locale)
142
143 status = U_ZERO_ERROR;
144 index.adoptInstead(new AlphabeticIndex(Locale::getEnglish(), status));
145 TEST_CHECK_STATUS;
146 AlphabeticIndex &aip = index->addLabels(Locale::getJapanese(), status);
147 TEST_ASSERT(&aip == index.getAlias());
148 TEST_CHECK_STATUS;
149 lc = index->getBucketCount(status);
150 TEST_CHECK_STATUS;
151 TEST_ASSERT(35 < lc); // Japanese should add a bunch. Don't rely on the exact value.
152 index.adoptInstead(nullptr);
153
154 // GetCollator(), Get under/in/over flow labels
155
156 status = U_ZERO_ERROR;
157 index.adoptInstead(new AlphabeticIndex(Locale::getGerman(), status));
158 TEST_CHECK_STATUS;
159 LocalPointer<Collator> germanCol(Collator::createInstance(Locale::getGerman(), status));
160 TEST_CHECK_STATUS;
161 const RuleBasedCollator &indexCol = index->getCollator();
162 TEST_ASSERT(*germanCol == indexCol);
163
164 UnicodeString ELLIPSIS; ELLIPSIS.append((UChar32)0x2026);
165 UnicodeString s = index->getUnderflowLabel();
166 TEST_ASSERT(ELLIPSIS == s);
167 s = index->getOverflowLabel();
168 TEST_ASSERT(ELLIPSIS == s);
169 s = index->getInflowLabel();
170 TEST_ASSERT(ELLIPSIS == s);
171 index->setOverflowLabel(UNICODE_STRING_SIMPLE("O"), status);
172 index->setUnderflowLabel(UNICODE_STRING_SIMPLE("U"), status).setInflowLabel(UNICODE_STRING_SIMPLE("I"), status);
173 s = index->getUnderflowLabel();
174 TEST_ASSERT(UNICODE_STRING_SIMPLE("U") == s);
175 s = index->getOverflowLabel();
176 TEST_ASSERT(UNICODE_STRING_SIMPLE("O") == s);
177 s = index->getInflowLabel();
178 TEST_ASSERT(UNICODE_STRING_SIMPLE("I") == s);
179
180
181
182
183 index.adoptInstead(nullptr);
184
185
186
187 const UnicodeString adam = UNICODE_STRING_SIMPLE("Adam");
188 const UnicodeString baker = UNICODE_STRING_SIMPLE("Baker");
189 const UnicodeString charlie = UNICODE_STRING_SIMPLE("Charlie");
190 const UnicodeString chad = UNICODE_STRING_SIMPLE("Chad");
191 const UnicodeString zed = UNICODE_STRING_SIMPLE("Zed");
192 const UnicodeString Cyrillic = UNICODE_STRING_SIMPLE("\\u0410\\u0443\\u0435").unescape();
193
194 // addRecord(), verify that it comes back out.
195 //
196 status = U_ZERO_ERROR;
197 index.adoptInstead(new AlphabeticIndex(Locale::getEnglish(), status));
198 TEST_CHECK_STATUS;
199 index->addRecord(UnicodeString("Adam"), this, status);
200 UBool b;
201 TEST_CHECK_STATUS;
202 index->resetBucketIterator(status);
203 TEST_CHECK_STATUS;
204 index->nextBucket(status); // Move to underflow label
205 index->nextBucket(status); // Move to "A"
206 TEST_CHECK_STATUS;
207 const UnicodeString &label2 = index->getBucketLabel();
208 UnicodeString A_STR = UNICODE_STRING_SIMPLE("A");
209 TEST_ASSERT(A_STR == label2);
210
211 b = index->nextRecord(status);
212 TEST_CHECK_STATUS;
213 TEST_ASSERT(b);
214 const UnicodeString &itemName = index->getRecordName();
215 TEST_ASSERT(adam == itemName);
216
217 const void *itemContext = index->getRecordData();
218 TEST_ASSERT(itemContext == this);
219
220 index.adoptInstead(nullptr);
221
222 // clearRecords, addRecord(), Iteration
223
224 status = U_ZERO_ERROR;
225 index.adoptInstead(new AlphabeticIndex(Locale::getEnglish(), status));
226 TEST_CHECK_STATUS;
227 while (index->nextBucket(status)) {
228 TEST_CHECK_STATUS;
229 while (index->nextRecord(status)) {
230 TEST_CHECK_STATUS;
231 TEST_ASSERT(FALSE); // No items have been added.
232 }
233 TEST_CHECK_STATUS;
234 }
235
236 index->addRecord(adam, NULL, status);
237 index->addRecord(baker, NULL, status);
238 index->addRecord(charlie, NULL, status);
239 index->addRecord(chad, NULL, status);
240 TEST_CHECK_STATUS;
241 int itemCount = 0;
242 index->resetBucketIterator(status);
243 while (index->nextBucket(status)) {
244 TEST_CHECK_STATUS;
245 while (index->nextRecord(status)) {
246 TEST_CHECK_STATUS;
247 ++itemCount;
248 }
249 }
250 TEST_CHECK_STATUS;
251 TEST_ASSERT(itemCount == 4);
252
253 TEST_ASSERT(index->nextBucket(status) == FALSE);
254 index->resetBucketIterator(status);
255 TEST_CHECK_STATUS;
256 TEST_ASSERT(index->nextBucket(status) == TRUE);
257
258 index->clearRecords(status);
259 TEST_CHECK_STATUS;
260 index->resetBucketIterator(status);
261 while (index->nextBucket(status)) {
262 TEST_CHECK_STATUS;
263 while (index->nextRecord(status)) {
264 TEST_ASSERT(FALSE); // No items have been added.
265 }
266 }
267 TEST_CHECK_STATUS;
268 index.adoptInstead(nullptr);
269
270 // getBucketLabel(), getBucketType()
271
272 status = U_ZERO_ERROR;
273 index.adoptInstead(new AlphabeticIndex(Locale::getEnglish(), status));
274 TEST_CHECK_STATUS;
275 index->setUnderflowLabel(adam, status).setOverflowLabel(charlie, status);
276 TEST_CHECK_STATUS;
277 for (i=0; index->nextBucket(status); i++) {
278 TEST_CHECK_STATUS;
279 UnicodeString label = index->getBucketLabel();
280 UAlphabeticIndexLabelType type = index->getBucketLabelType();
281 if (i == 0) {
282 TEST_ASSERT(type == U_ALPHAINDEX_UNDERFLOW);
283 TEST_ASSERT(label == adam);
284 } else if (i <= 26) {
285 // Labels A - Z for English locale
286 TEST_ASSERT(type == U_ALPHAINDEX_NORMAL);
287 UnicodeString expectedLabel((UChar)(0x40 + i));
288 TEST_ASSERT(expectedLabel == label);
289 } else if (i == 27) {
290 TEST_ASSERT(type == U_ALPHAINDEX_OVERFLOW);
291 TEST_ASSERT(label == charlie);
292 } else {
293 TEST_ASSERT(FALSE);
294 }
295 }
296 TEST_ASSERT(i==28);
297 index.adoptInstead(nullptr);
298
299 // getBucketIndex()
300
301 status = U_ZERO_ERROR;
302 index.adoptInstead(new AlphabeticIndex(Locale::getEnglish(), status));
303 TEST_CHECK_STATUS;
304 int32_t n = index->getBucketIndex(adam, status);
305 TEST_CHECK_STATUS;
306 TEST_ASSERT(n == 1); /* Label #0 is underflow, 1 is A, etc. */
307 n = index->getBucketIndex(baker, status);
308 TEST_ASSERT(n == 2);
309 n = index->getBucketIndex(Cyrillic, status);
310 TEST_ASSERT(n == 27); // Overflow label
311 n = index->getBucketIndex(zed, status);
312 TEST_ASSERT(n == 26);
313
314 for (i=0; index->nextBucket(status); i++) {
315 n = index->getBucketIndex();
316 TEST_ASSERT(n == i);
317 UnicodeString label = index->getBucketLabel();
318 TEST_ASSERT(n == i);
319 }
320 TEST_ASSERT(i == 28);
321
322 index.adoptInstead(nullptr);
323 index.adoptInstead(new AlphabeticIndex(Locale::createFromName("ru"), status));
324 TEST_CHECK_STATUS;
325 assertEquals("Russian index.getBucketCount()", 32, index->getBucketCount(status));
326 // Latin-script names should go into the underflow label (0)
327 // if the Russian collation does not use script reordering,
328 // but into the overflow label (getBucketCount()-1)
329 // if Russian sorts Cyrillic first.
330 int32_t reorderCodes[20];
331 int32_t expectedLatinIndex = 0;
332 if (index->getCollator().getReorderCodes(reorderCodes, UPRV_LENGTHOF(reorderCodes), status) > 0) {
333 expectedLatinIndex = index->getBucketCount(status) - 1;
334 }
335 n = index->getBucketIndex(adam, status);
336 TEST_CHECK_STATUS;
337 assertEquals("Russian index.getBucketIndex(adam)", expectedLatinIndex, n);
338 n = index->getBucketIndex(baker, status);
339 assertEquals("Russian index.getBucketIndex(baker)", expectedLatinIndex, n);
340 n = index->getBucketIndex(Cyrillic, status);
341 assertEquals("Russian index.getBucketIndex(Cyrillic)", 1, n);
342 n = index->getBucketIndex(zed, status);
343 assertEquals("Russian index.getBucketIndex(zed)", expectedLatinIndex, n);
344
345 }
346
347
348 static const char * KEY_LOCALES[] = {
349 "en", "es", "de", "fr", "ja", "it", "tr", "pt", "zh", "nl",
350 "pl", "ar", "ru", "zh_Hant", "ko", "th", "sv", "fi", "da",
351 "he", "nb", "el", "hr", "bg", "sk", "lt", "vi", "lv", "sr",
352 "pt_PT", "ro", "hu", "cs", "id", "sl", "fil", "fa", "uk",
353 "ca", "hi", "et", "eu", "is", "sw", "ms", "bn", "am", "ta",
354 "te", "mr", "ur", "ml", "kn", "gu", "or", ""};
355
356
357 void AlphabeticIndexTest::ManyLocalesTest() {
358 UErrorCode status = U_ZERO_ERROR;
359 int32_t lc = 0;
360
361 for (int i=0; ; ++i) {
362 status = U_ZERO_ERROR;
363 const char *localeName = KEY_LOCALES[i];
364 if (localeName[0] == 0) {
365 break;
366 }
367 // std::cout << localeName << " ";
368 Locale loc = Locale::createFromName(localeName);
369 AlphabeticIndex index(loc, status);
370 TEST_CHECK_STATUS;
371 lc = index.getBucketCount(status);
372 TEST_CHECK_STATUS;
373 // std::cout << "getBucketCount() == " << lc << std::endl;
374
375 LocalPointer<AlphabeticIndex::ImmutableIndex> immIndex(index.buildImmutableIndex(status));
376 TEST_CHECK_STATUS;
377 TEST_ASSERT(lc == immIndex->getBucketCount());
378
379 assertEquals("initial bucket index", -1, index.getBucketIndex());
380 int32_t bucketIndex = 0;
381 while (index.nextBucket(status)) {
382 TEST_CHECK_STATUS;
383 assertEquals("bucket index", bucketIndex, index.getBucketIndex());
384 const UnicodeString &label = index.getBucketLabel();
385 TEST_ASSERT(label.length()>0);
386 // std::string ss;
387 // std::cout << ":" << label.toUTF8String(ss);
388 const AlphabeticIndex::Bucket *bucket = immIndex->getBucket(bucketIndex);
389 TEST_ASSERT(bucket != NULL);
390 assertEquals("bucket label vs. immutable: locale=" + UnicodeString(localeName) +
391 " index=" + bucketIndex,
392 label, bucket->getLabel());
393 TEST_ASSERT(&label != &bucket->getLabel()); // not the same pointers
394 UAlphabeticIndexLabelType labelType = index.getBucketLabelType();
395 TEST_ASSERT(labelType == bucket->getLabelType());
396 ++bucketIndex;
397 }
398 // std::cout << ":" << std::endl;
399
400 TEST_ASSERT(immIndex->getBucketCount() == bucketIndex);
401 TEST_ASSERT(immIndex->getBucket(-1) == NULL);
402 TEST_ASSERT(immIndex->getBucket(bucketIndex) == NULL);
403 }
404 }
405
406
407 // Test data for Pinyin based indexes.
408 // The Chinese characters should be distributed under latin labels in
409 // an index.
410
411 static const char *pinyinTestData[] = {
412 "\\u0101", "\\u5416", "\\u58ba", //
413 "b", "\\u516b", "\\u62d4", "\\u8500", //
414 "c", "\\u5693", "\\u7938", "\\u9e7e", //
415 "d", "\\u5491", "\\u8fcf", "\\u964a", //
416 "\\u0113","\\u59b8", "\\u92e8", "\\u834b", //
417 "f", "\\u53d1", "\\u9197", "\\u99a5", //
418 "g", "\\u7324", "\\u91d3", "\\u8142", //
419 "h", "\\u598e", "\\u927f", "\\u593b", //
420 "j", "\\u4e0c", "\\u6785", "\\u9d58", //
421 "k", "\\u5494", "\\u958b", "\\u7a52", //
422 "l", "\\u5783", "\\u62c9", "\\u9ba5", //
423 "m", "\\u5638", "\\u9ebb", "\\u65c0", //
424 "n", "\\u62ff", "\\u80ad", "\\u685b", //
425 "\\u014D", "\\u5662", "\\u6bee", "\\u8bb4", //
426 "p", "\\u5991", "\\u8019", "\\u8c31", //
427 "q", "\\u4e03", "\\u6053", "\\u7f56", //
428 "r", "\\u5465", "\\u72aa", "\\u6e03", //
429 "s", "\\u4ee8", "\\u9491", "\\u93c1", //
430 "t", "\\u4ed6", "\\u9248", "\\u67dd", //
431 "w", "\\u5c72", "\\u5558", "\\u5a7a", //
432 "x", "\\u5915", "\\u5438", "\\u6bbe", //
433 "y", "\\u4e2b", "\\u82bd", "\\u8574", //
434 "z", "\\u5e00", "\\u707d", "\\u5c0a",
435 NULL
436 };
437
438 void AlphabeticIndexTest::HackPinyinTest() {
439 UErrorCode status = U_ZERO_ERROR;
440 AlphabeticIndex aindex(Locale::createFromName("zh"), status);
441 TEST_CHECK_STATUS;
442
443 UnicodeString names[UPRV_LENGTHOF(pinyinTestData)];
444 int32_t nameCount;
445 for (nameCount=0; pinyinTestData[nameCount] != NULL; nameCount++) {
446 names[nameCount] = UnicodeString(pinyinTestData[nameCount], -1, UnicodeString::kInvariant).unescape();
447 aindex.addRecord(names[nameCount], &names[nameCount], status);
448 TEST_CHECK_STATUS;
449 if (U_FAILURE(status)) {
450 return;
451 }
452 }
453 TEST_ASSERT(nameCount == aindex.getRecordCount(status));
454
455 // Weak checking: make sure that none of the Chinese names landed in the overflow bucket
456 // of the index, and that the names are distributed among several buckets.
457 // (Exact expected data would be subject to change with evolution of the collation rules.)
458
459 int32_t bucketCount = 0;
460 int32_t filledBucketCount = 0;
461 while (aindex.nextBucket(status)) {
462 bucketCount++;
463 UnicodeString label = aindex.getBucketLabel();
464 // std::string s;
465 // std::cout << label.toUTF8String(s) << ": ";
466
467 UBool bucketHasContents = FALSE;
468 while (aindex.nextRecord(status)) {
469 bucketHasContents = TRUE;
470 UnicodeString name = aindex.getRecordName();
471 if (aindex.getBucketLabelType() != U_ALPHAINDEX_NORMAL) {
472 errln("File %s, Line %d, Name \"\\u%x\" is in an under or overflow bucket.",
473 __FILE__, __LINE__, name.char32At(0));
474 }
475 // s.clear();
476 // std::cout << aindex.getRecordName().toUTF8String(s) << " ";
477 }
478 if (bucketHasContents) {
479 filledBucketCount++;
480 }
481 // std::cout << std::endl;
482 }
483 TEST_ASSERT(bucketCount > 25);
484 TEST_ASSERT(filledBucketCount > 15);
485 }
486
487
488 void AlphabeticIndexTest::TestBug9009() {
489 UErrorCode status = U_ZERO_ERROR;
490 Locale loc("root");
491 AlphabeticIndex aindex(loc, status);
492 TEST_CHECK_STATUS;
493 aindex.nextBucket(status); // Crash here before bug was fixed.
494 TEST_CHECK_STATUS;
495 }
496
497 static const char *localeAndIndexCharactersLists[][2] = {
498 /* Arabic*/ {"ar", "\\u0627:\\u0628:\\u062A:\\u062B:\\u062C:\\u062D:\\u062E:\\u062F:\\u0630:\\u0631:\\u0632:\\u0633:\\u0634:\\u0635:\\u0636:\\u0637:\\u0638:\\u0639:\\u063A:\\u0641:\\u0642:\\u0643:\\u0644:\\u0645:\\u0646:\\u0647:\\u0648:\\u064A"},
499 /* Bulgarian*/ {"bg", "\\u0410:\\u0411:\\u0412:\\u0413:\\u0414:\\u0415:\\u0416:\\u0417:\\u0418:\\u0419:\\u041A:\\u041B:\\u041C:\\u041D:\\u041E:\\u041F:\\u0420:\\u0421:\\u0422:\\u0423:\\u0424:\\u0425:\\u0426:\\u0427:\\u0428:\\u0429:\\u042E:\\u042F"},
500 /* Catalan*/ {"ca", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
501 /* Czech*/ {"cs", "A:B:C:\\u010C:D:E:F:G:H:CH:I:J:K:L:M:N:O:P:Q:R:\\u0158:S:\\u0160:T:U:V:W:X:Y:Z:\\u017D"},
502 /* Danish*/ {"da", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z:\\u00C6:\\u00D8:\\u00C5"},
503 /* German*/ {"de", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
504 /* Greek*/ {"el", "\\u0391:\\u0392:\\u0393:\\u0394:\\u0395:\\u0396:\\u0397:\\u0398:\\u0399:\\u039A:\\u039B:\\u039C:\\u039D:\\u039E:\\u039F:\\u03A0:\\u03A1:\\u03A3:\\u03A4:\\u03A5:\\u03A6:\\u03A7:\\u03A8:\\u03A9"},
505 /* English*/ {"en", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
506 /* Spanish*/ {"es", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:\\u00D1:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
507 /* Estonian*/ {"et", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:\\u0160:Z:\\u017D:T:U:V:W:\\u00D5:\\u00C4:\\u00D6:\\u00DC:X:Y"},
508 /* Basque*/ {"eu", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
509 /* Finnish*/ {"fi", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z:\\u00C5:\\u00C4:\\u00D6"},
510 /* Filipino*/ {"fil", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:\\u00D1:Ng:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
511 /* French*/ {"fr", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
512 /* Hebrew*/ {"he", "\\u05D0:\\u05D1:\\u05D2:\\u05D3:\\u05D4:\\u05D5:\\u05D6:\\u05D7:\\u05D8:\\u05D9:\\u05DB:\\u05DC:\\u05DE:\\u05E0:\\u05E1:\\u05E2:\\u05E4:\\u05E6:\\u05E7:\\u05E8:\\u05E9:\\u05EA"},
513 /* Icelandic*/ {"is", "A:\\u00C1:B:C:D:\\u00D0:E:\\u00C9:F:G:H:I:\\u00CD:J:K:L:M:N:O:\\u00D3:P:Q:R:S:T:U:\\u00DA:V:W:X:Y:\\u00DD:Z:\\u00DE:\\u00C6:\\u00D6"},
514 /* Italian*/ {"it", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
515 /* Japanese*/ {"ja", "\\u3042:\\u304B:\\u3055:\\u305F:\\u306A:\\u306F:\\u307E:\\u3084:\\u3089:\\u308F"},
516 /* Korean*/ {"ko", "\\u3131:\\u3134:\\u3137:\\u3139:\\u3141:\\u3142:\\u3145:\\u3147:\\u3148:\\u314A:\\u314B:\\u314C:\\u314D:\\u314E"},
517 /* Lithuanian*/ {"lt", "A:B:C:\\u010C:D:E:F:G:H:I:J:K:L:M:N:O:P:R:S:\\u0160:T:U:V:Z:\\u017D"},
518 /* Latvian*/ {"lv", "A:B:C:\\u010C:D:E:F:G:\\u0122:H:I:J:K:\\u0136:L:\\u013B:M:N:\\u0145:O:P:Q:R:S:\\u0160:T:U:V:W:X:Z:\\u017D"},
519 /* Norwegian Bokm\\u00E5l*/ {"nb", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z:\\u00C6:\\u00D8:\\u00C5"},
520 /* Dutch*/ {"nl", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
521 /* Polish*/ {"pl", "A:\\u0104:B:C:\\u0106:D:E:\\u0118:F:G:H:I:J:K:L:\\u0141:M:N:\\u0143:O:\\u00D3:P:Q:R:S:\\u015A:T:U:V:W:X:Y:Z:\\u0179:\\u017B"},
522 /* Portuguese*/ {"pt", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
523 /* Romanian*/ {"ro", "A:\\u0102:\\u00C2:B:C:D:E:F:G:H:I:\\u00CE:J:K:L:M:N:O:P:Q:R:S:\\u0218:T:\\u021A:U:V:W:X:Y:Z"},
524 /* Russian*/ {"ru", "\\u0410:\\u0411:\\u0412:\\u0413:\\u0414:\\u0415:\\u0416:\\u0417:\\u0418:\\u0419:\\u041A:\\u041B:\\u041C:\\u041D:\\u041E:\\u041F:\\u0420:\\u0421:\\u0422:\\u0423:\\u0424:\\u0425:\\u0426:\\u0427:\\u0428:\\u0429:\\u042B:\\u042D:\\u042E:\\u042F"},
525 /* Slovak*/ {"sk", "A:\\u00C4:B:C:\\u010C:D:E:F:G:H:CH:I:J:K:L:M:N:O:\\u00D4:P:Q:R:S:\\u0160:T:U:V:W:X:Y:Z:\\u017D"},
526 /* Slovenian*/ {"sl", "A:B:C:\\u010C:\\u0106:D:\\u0110:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:\\u0160:T:U:V:W:X:Y:Z:\\u017D"},
527 /* Serbian*/ {"sr", "\\u0410:\\u0411:\\u0412:\\u0413:\\u0414:\\u0402:\\u0415:\\u0416:\\u0417:\\u0418:\\u0408:\\u041A:\\u041B:\\u0409:\\u041C:\\u041D:\\u040A:\\u041E:\\u041F:\\u0420:\\u0421:\\u0422:\\u040B:\\u0423:\\u0424:\\u0425:\\u0426:\\u0427:\\u040F:\\u0428"},
528 /* Swedish*/ {"sv", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z:\\u00C5:\\u00C4:\\u00D6"},
529 /* Turkish*/ {"tr", "A:B:C:\\u00C7:D:E:F:G:H:I:\\u0130:J:K:L:M:N:O:\\u00D6:P:Q:R:S:\\u015E:T:U:\\u00DC:V:W:X:Y:Z"},
530 /* Ukrainian*/ {"uk", "\\u0410:\\u0411:\\u0412:\\u0413:\\u0490:\\u0414:\\u0415:\\u0404:\\u0416:\\u0417:\\u0418:\\u0406:\\u0407:\\u0419:\\u041A:\\u041B:\\u041C:\\u041D:\\u041E:\\u041F:\\u0420:\\u0421:\\u0422:\\u0423:\\u0424:\\u0425:\\u0426:\\u0427:\\u0428:\\u0429:\\u042E:\\u042F"},
531 /* Vietnamese*/ {"vi", "A:\\u0102:\\u00C2:B:C:D:\\u0110:E:\\u00CA:F:G:H:I:J:K:L:M:N:O:\\u00D4:\\u01A0:P:Q:R:S:T:U:\\u01AF:V:W:X:Y:Z"},
532 /* Chinese*/ {"zh", "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z"},
533 /* Chinese (Traditional Han)*/ {"zh_Hant", "1\\u5283:2\\u5283:3\\u5283:4\\u5283:5\\u5283:6\\u5283:7\\u5283:8\\u5283:9\\u5283:10\\u5283:11\\u5283:12\\u5283:13\\u5283:14\\u5283:15\\u5283:16\\u5283:17\\u5283:18\\u5283:19\\u5283:20\\u5283:21\\u5283:22\\u5283:23\\u5283:24\\u5283:25\\u5283:26\\u5283:27\\u5283:28\\u5283:29\\u5283:30\\u5283:31\\u5283:32\\u5283:33\\u5283:35\\u5283:36\\u5283:39\\u5283:48\\u5283"},
534 };
535
536 void AlphabeticIndexTest::TestIndexCharactersList() {
537 UErrorCode status = U_ZERO_ERROR;
538 for (int32_t i = 0; i < UPRV_LENGTHOF(localeAndIndexCharactersLists); ++i) {
539 const char *(&localeAndIndexCharacters)[2] = localeAndIndexCharactersLists[i];
540 const char *locale = localeAndIndexCharacters[0];
541 UnicodeString expectedIndexCharacters
542 = (UnicodeString("\\u2026:") + localeAndIndexCharacters[1] + ":\\u2026").unescape();
543 AlphabeticIndex index(locale, status);
544 TEST_CHECK_STATUS;
545 LocalPointer<AlphabeticIndex::ImmutableIndex> immIndex(index.buildImmutableIndex(status));
546 TEST_CHECK_STATUS;
547
548 // Join the elements of the list to a string with delimiter ":"
549 UnicodeString actualIndexCharacters;
550 assertEquals(locale,
551 expectedIndexCharacters,
552 joinLabelsAndAppend(*immIndex, actualIndexCharacters));
553 logln(locale + UnicodeString(": ") + actualIndexCharacters);
554 }
555 }
556
557 void AlphabeticIndexTest::TestHaniFirst() {
558 UErrorCode status = U_ZERO_ERROR;
559 LocalPointer<RuleBasedCollator> coll(
560 static_cast<RuleBasedCollator *>(Collator::createInstance(Locale::getRoot(), status)));
561
562 if (U_FAILURE(status)) {
563 dataerrln("Failed Collator::createInstance call - %s", u_errorName(status));
564 return;
565 }
566 int32_t reorderCodes[] = { USCRIPT_HAN };
567 coll->setReorderCodes(reorderCodes, UPRV_LENGTHOF(reorderCodes), status);
568 TEST_CHECK_STATUS;
569 AlphabeticIndex index(coll.orphan(), status);
570 TEST_CHECK_STATUS;
571 assertEquals("getBucketCount()", 1, index.getBucketCount(status)); // ... (underflow only)
572 index.addLabels(Locale::getEnglish(), status);
573 assertEquals("getBucketCount()", 28, index.getBucketCount(status)); // ... A-Z ...
574 int32_t bucketIndex = index.getBucketIndex(UnicodeString((UChar)0x897f), status);
575 assertEquals("getBucketIndex(U+897F)", 0, bucketIndex); // underflow bucket
576 bucketIndex = index.getBucketIndex("i", status);
577 assertEquals("getBucketIndex(i)", 9, bucketIndex);
578 bucketIndex = index.getBucketIndex(UnicodeString((UChar)0x03B1), status);
579 assertEquals("getBucketIndex(Greek alpha)", 27, bucketIndex);
580 // U+50005 is an unassigned code point which sorts at the end, independent of the Hani group.
581 bucketIndex = index.getBucketIndex(UnicodeString((UChar32)0x50005), status);
582 assertEquals("getBucketIndex(U+50005)", 27, bucketIndex);
583 bucketIndex = index.getBucketIndex(UnicodeString((UChar)0xFFFF), status);
584 assertEquals("getBucketIndex(U+FFFF)", 27, bucketIndex);
585 }
586
587 void AlphabeticIndexTest::TestPinyinFirst() {
588 UErrorCode status = U_ZERO_ERROR;
589 LocalPointer<RuleBasedCollator> coll(
590 static_cast<RuleBasedCollator *>(Collator::createInstance(Locale::getChinese(), status)));
591 if (U_FAILURE(status)) {
592 dataerrln("Failed Collator::createInstance call - %s", u_errorName(status));
593 return;
594 }
595 int32_t reorderCodes[] = { USCRIPT_HAN };
596 coll->setReorderCodes(reorderCodes, UPRV_LENGTHOF(reorderCodes), status);
597 TEST_CHECK_STATUS;
598 AlphabeticIndex index(coll.orphan(), status);
599 TEST_CHECK_STATUS;
600 assertEquals("getBucketCount()", 28, index.getBucketCount(status)); // ... A-Z ...
601 index.addLabels(Locale::getChinese(), status);
602 assertEquals("getBucketCount()", 28, index.getBucketCount(status)); // ... A-Z ...
603 int32_t bucketIndex = index.getBucketIndex(UnicodeString((UChar)0x897f), status);
604 assertEquals("getBucketIndex(U+897F)", (int32_t)((UChar)0x0058/*X*/ - (UChar)0x0041/*A*/ + 1), bucketIndex);
605 bucketIndex = index.getBucketIndex("i", status);
606 assertEquals("getBucketIndex(i)", 9, bucketIndex);
607 bucketIndex = index.getBucketIndex(UnicodeString((UChar)0x03B1), status);
608 assertEquals("getBucketIndex(Greek alpha)", (int32_t)27, bucketIndex);
609 // U+50005 is an unassigned code point which sorts at the end, independent of the Hani group.
610 bucketIndex = index.getBucketIndex(UnicodeString((UChar32)0x50005), status);
611 assertEquals("getBucketIndex(U+50005)", 27, bucketIndex);
612 bucketIndex = index.getBucketIndex(UnicodeString((UChar)0xFFFF), status);
613 assertEquals("getBucketIndex(U+FFFF)", 27, bucketIndex);
614 }
615
616 void AlphabeticIndexTest::TestSchSt() {
617 UErrorCode status = U_ZERO_ERROR;
618 AlphabeticIndex index(Locale::getGerman(), status);
619 index.addLabels(UnicodeSet("[\\u00C6{Sch*}{St*}]", status), status);
620 TEST_CHECK_STATUS;
621 // ... A AE-ligature B-R S Sch St T-Z ...
622 LocalPointer<AlphabeticIndex::ImmutableIndex> immIndex(index.buildImmutableIndex(status));
623 TEST_CHECK_STATUS;
624 assertEquals("getBucketCount()", 31, index.getBucketCount(status));
625 assertEquals("immutable getBucketCount()", 31, immIndex->getBucketCount());
626 static const struct TestCase {
627 const char *name;
628 int32_t bucketIndex;
629 const char *bucketLabel;
630 } testCases[] = {
631 // name, bucket index, bucket label
632 { "Adelbert", 1, "A" },
633 { "Afrika", 1, "A" },
634 { "\\u00C6sculap", 2, "\\u00C6" },
635 { "Aesthet", 2, "\\u00C6" },
636 { "Berlin", 3, "B" },
637 { "Rilke", 19, "R" },
638 { "Sacher", 20, "S" },
639 { "Seiler", 20, "S" },
640 { "Sultan", 20, "S" },
641 { "Schiller", 21, "Sch" },
642 { "Steiff", 22, "St" },
643 { "Thomas", 23, "T" }
644 };
645 for (int32_t i = 0; i < UPRV_LENGTHOF(testCases); ++i) {
646 const TestCase &testCase = testCases[i];
647 UnicodeString name = UnicodeString(testCase.name).unescape();
648 UnicodeString label = UnicodeString(testCase.bucketLabel).unescape();
649 char msg[100];
650 sprintf(msg, "getBucketIndex(%s)", testCase.name);
651 assertEquals(msg, testCase.bucketIndex, index.getBucketIndex(name, status));
652 sprintf(msg, "immutable getBucketIndex(%s)", testCase.name);
653 assertEquals(msg, testCase.bucketIndex, immIndex->getBucketIndex(name, status));
654 sprintf(msg, "immutable bucket label (%s)", testCase.name);
655 assertEquals(msg, label, immIndex->getBucket(testCase.bucketIndex)->getLabel());
656 }
657 }
658
659 void AlphabeticIndexTest::TestNoLabels() {
660 UErrorCode status = U_ZERO_ERROR;
661 LocalPointer<RuleBasedCollator> coll(
662 static_cast<RuleBasedCollator *>(Collator::createInstance(Locale::getRoot(), status)));
663 TEST_CHECK_STATUS;
664 AlphabeticIndex index(coll.orphan(), status);
665 TEST_CHECK_STATUS;
666 index.addRecord(UnicodeString((UChar)0x897f), NULL, status);
667 index.addRecord("i", NULL, status);
668 index.addRecord(UnicodeString((UChar)0x03B1), NULL, status);
669 assertEquals("getBucketCount()", 1, index.getBucketCount(status)); // ...
670 TEST_ASSERT(index.nextBucket(status));
671 assertEquals("underflow label type", (int32_t)U_ALPHAINDEX_UNDERFLOW, index.getBucketLabelType());
672 assertEquals("all records in the underflow bucket", (int32_t)3, index.getBucketRecordCount());
673 }
674
675 void AlphabeticIndexTest::TestChineseZhuyin() {
676 UErrorCode status = U_ZERO_ERROR;
677 char loc[100];
678 uloc_forLanguageTag("zh-u-co-zhuyin", loc, UPRV_LENGTHOF(loc), NULL, &status);
679 AlphabeticIndex index(loc, status);
680 LocalPointer<AlphabeticIndex::ImmutableIndex> immIndex(index.buildImmutableIndex(status));
681 TEST_CHECK_STATUS;
682 assertEquals("getBucketCount()", 38, immIndex->getBucketCount());
683 assertEquals("label 1", UnicodeString((UChar)0x3105), immIndex->getBucket(1)->getLabel());
684 assertEquals("label 2", UnicodeString((UChar)0x3106), immIndex->getBucket(2)->getLabel());
685 assertEquals("label 3", UnicodeString((UChar)0x3107), immIndex->getBucket(3)->getLabel());
686 assertEquals("label 4", UnicodeString((UChar)0x3108), immIndex->getBucket(4)->getLabel());
687 assertEquals("label 5", UnicodeString((UChar)0x3109), immIndex->getBucket(5)->getLabel());
688 }
689
690 void AlphabeticIndexTest::TestJapaneseKanji() {
691 UErrorCode status = U_ZERO_ERROR;
692 AlphabeticIndex index(Locale::getJapanese(), status);
693 LocalPointer<AlphabeticIndex::ImmutableIndex> immIndex(index.buildImmutableIndex(status));
694 TEST_CHECK_STATUS;
695 // There are no index characters for Kanji in the Japanese standard collator.
696 // They should all go into the overflow bucket.
697 static const UChar32 kanji[] = { 0x4E9C, 0x95C7, 0x4E00, 0x58F1 };
698 int32_t overflowIndex = immIndex->getBucketCount() - 1;
699 for(int32_t i = 0; i < UPRV_LENGTHOF(kanji); ++i) {
700 char msg[40];
701 sprintf(msg, "kanji[%d]=U+%04lX in overflow bucket", (int)i, (long)kanji[i]);
702 assertEquals(msg, overflowIndex, immIndex->getBucketIndex(UnicodeString(kanji[i]), status));
703 TEST_CHECK_STATUS;
704 }
705 }
706
707 void AlphabeticIndexTest::TestChineseUnihan() {
708 UErrorCode status = U_ZERO_ERROR;
709 AlphabeticIndex index("zh-u-co-unihan", status);
710 if(U_FAILURE(status)) {
711 dataerrln("unable create an AlphabeticIndex for Chinese/unihan: %s", u_errorName(status));
712 return;
713 }
714 index.setMaxLabelCount(500, status); // ICU 54 default is 99.
715 LocalPointer<AlphabeticIndex::ImmutableIndex> immIndex(index.buildImmutableIndex(status));
716 TEST_CHECK_STATUS;
717 int32_t bucketCount = immIndex->getBucketCount();
718 if(bucketCount < 216) {
719 // There should be at least an underflow and overflow label,
720 // and one for each of 214 radicals,
721 // and maybe additional labels for simplified radicals.
722 dataerrln("too few buckets/labels for Chinese/unihan: %d (is zh/unihan data available?)",
723 bucketCount);
724 return;
725 } else {
726 logln("Chinese/unihan has %d buckets/labels", bucketCount);
727 }
728 // bucketIndex = radical number, adjusted for simplified radicals in lower buckets.
729 int32_t bucketIndex = index.getBucketIndex(UnicodeString((UChar)0x4e5d), status);
730 assertEquals("getBucketIndex(U+4E5D)", 5, bucketIndex);
731 // radical 100, and there is a 90' since Unicode 8
732 bucketIndex = index.getBucketIndex(UnicodeString((UChar)0x7527), status);
733 assertEquals("getBucketIndex(U+7527)", 101, bucketIndex);
734 }
735
736 void AlphabeticIndexTest::testHasBuckets() {
737 checkHasBuckets(Locale("am"), USCRIPT_ETHIOPIC);
738 checkHasBuckets(Locale("haw"), USCRIPT_LATIN);
739 checkHasBuckets(Locale("hy"), USCRIPT_ARMENIAN);
740 checkHasBuckets(Locale("vai"), USCRIPT_VAI);
741 }
742
743 void AlphabeticIndexTest::checkHasBuckets(const Locale &locale, UScriptCode script) {
744 IcuTestErrorCode errorCode(*this, "checkHasBuckets");
745 AlphabeticIndex aindex(locale, errorCode);
746 LocalPointer<AlphabeticIndex::ImmutableIndex> index(aindex.buildImmutableIndex(errorCode), errorCode);
747 if (U_FAILURE(errorCode)) {
748 dataerrln("%s %d Error in index creation", __FILE__, __LINE__);
749 return;
750 }
751 UnicodeString loc = locale.getName();
752 assertTrue(loc + u" at least 3 buckets", index->getBucketCount() >= 3);
753 const AlphabeticIndex::Bucket *bucket = index->getBucket(1);
754 assertEquals(loc + u" real bucket", U_ALPHAINDEX_NORMAL, bucket->getLabelType());
755 assertEquals(loc + u" expected script", script,
756 uscript_getScript(bucket->getLabel().char32At(0), errorCode));
757 }
758
759 #endif