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