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