1 /********************************************************************
3 * Copyright (c) 2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 // file: alphaindex.cpp
8 // Alphabetic Index Tests.
11 #include "alphaindextst.h"
13 #include "unicode/alphaindex.h"
14 #include "unicode/coll.h"
15 #include "unicode/tblcoll.h"
16 #include "unicode/uniset.h"
18 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_NORMALIZATION
21 // #include <iostream>
23 AlphabeticIndexTest::AlphabeticIndexTest() {
26 AlphabeticIndexTest::~AlphabeticIndexTest() {
29 void AlphabeticIndexTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
31 if (exec
) logln("TestSuite AlphabeticIndex: ");
34 case 0: name
= "APITest";
38 case 1: name
= "ManyLocales";
39 if (exec
) ManyLocalesTest();
42 case 2: name
= "HackPinyinTest";
43 if (exec
) HackPinyinTest();
46 case 3: name
= "TestBug9009";
47 if (exec
) TestBug9009();
51 break; //needed to end loop
55 #define TEST_CHECK_STATUS {if (U_FAILURE(status)) {dataerrln("%s:%d: Test failure. status=%s", \
56 __FILE__, __LINE__, u_errorName(status)); return;}}
58 #define TEST_ASSERT(expr) {if ((expr)==FALSE) {errln("%s:%d: Test failure \n", __FILE__, __LINE__);};}
61 // APITest. Invoke every function at least once, and check that it does something.
62 // Does not attempt to check complete functionality.
64 void AlphabeticIndexTest::APITest() {
66 // Simple constructor and destructor, getBucketCount()
68 UErrorCode status
= U_ZERO_ERROR
;
71 AlphabeticIndex
*index
= new AlphabeticIndex(Locale::getEnglish(), status
);
73 lc
= index
->getBucketCount(status
);
75 TEST_ASSERT(28 == lc
); // 26 letters plus two under/overflow labels.
76 //printf("getBucketCount() == %d\n", lc);
81 status
= U_ZERO_ERROR
;
82 index
= new AlphabeticIndex(Locale::getEnglish(), status
);
85 additions
.add((UChar32
)0x410).add((UChar32
)0x415); // A couple of Cyrillic letters
86 index
->addLabels(additions
, status
);
88 lc
= index
->getBucketCount(status
);
90 // TODO: should get 31. Java also gives 30. Needs fixing
91 TEST_ASSERT(30 == lc
); // 26 Latin letters plus
92 // TEST_ASSERT(31 == lc); // 26 Latin letters plus
93 // 2 Cyrillic letters plus
94 // 1 inflow label plus
95 // two under/overflow labels.
96 // std::cout << lc << std::endl;
102 status
= U_ZERO_ERROR
;
103 index
= new AlphabeticIndex(Locale::getEnglish(), status
);
105 AlphabeticIndex
&aip
= index
->addLabels(Locale::getJapanese(), status
);
106 TEST_ASSERT(&aip
== index
);
108 lc
= index
->getBucketCount(status
);
110 TEST_ASSERT(35 < lc
); // Japanese should add a bunch. Don't rely on the exact value.
113 // GetCollator(), Get under/in/over flow labels
115 status
= U_ZERO_ERROR
;
116 index
= new AlphabeticIndex(Locale::getGerman(), status
);
118 Collator
*germanCol
= Collator::createInstance(Locale::getGerman(), status
);
120 const RuleBasedCollator
&indexCol
= index
->getCollator();
121 TEST_ASSERT(*germanCol
== indexCol
);
124 UnicodeString ELLIPSIS
; ELLIPSIS
.append((UChar32
)0x2026);
125 UnicodeString s
= index
->getUnderflowLabel();
126 TEST_ASSERT(ELLIPSIS
== s
);
127 s
= index
->getOverflowLabel();
128 TEST_ASSERT(ELLIPSIS
== s
);
129 s
= index
->getInflowLabel();
130 TEST_ASSERT(ELLIPSIS
== s
);
131 index
->setOverflowLabel(UNICODE_STRING_SIMPLE("O"), status
);
132 index
->setUnderflowLabel(UNICODE_STRING_SIMPLE("U"), status
).setInflowLabel(UNICODE_STRING_SIMPLE("I"), status
);
133 s
= index
->getUnderflowLabel();
134 TEST_ASSERT(UNICODE_STRING_SIMPLE("U") == s
);
135 s
= index
->getOverflowLabel();
136 TEST_ASSERT(UNICODE_STRING_SIMPLE("O") == s
);
137 s
= index
->getInflowLabel();
138 TEST_ASSERT(UNICODE_STRING_SIMPLE("I") == s
);
147 const UnicodeString adam
= UNICODE_STRING_SIMPLE("Adam");
148 const UnicodeString baker
= UNICODE_STRING_SIMPLE("Baker");
149 const UnicodeString charlie
= UNICODE_STRING_SIMPLE("Charlie");
150 const UnicodeString chad
= UNICODE_STRING_SIMPLE("Chad");
151 const UnicodeString zed
= UNICODE_STRING_SIMPLE("Zed");
152 const UnicodeString Cyrillic
= UNICODE_STRING_SIMPLE("\\u0410\\u0443\\u0435").unescape();
154 // addRecord(), verify that it comes back out.
156 status
= U_ZERO_ERROR
;
157 index
= new AlphabeticIndex(Locale::getEnglish(), status
);
159 index
->addRecord(UnicodeString("Adam"), this, status
);
162 index
->resetBucketIterator(status
);
164 index
->nextBucket(status
); // Move to underflow label
165 index
->nextBucket(status
); // Move to "A"
167 const UnicodeString
&label2
= index
->getBucketLabel();
168 UnicodeString A_STR
= UNICODE_STRING_SIMPLE("A");
169 TEST_ASSERT(A_STR
== label2
);
171 b
= index
->nextRecord(status
);
174 const UnicodeString
&itemName
= index
->getRecordName();
175 TEST_ASSERT(adam
== itemName
);
177 const void *itemContext
= index
->getRecordData();
178 TEST_ASSERT(itemContext
== this);
182 // clearRecords, addRecord(), Iteration
184 status
= U_ZERO_ERROR
;
185 index
= new AlphabeticIndex(Locale::getEnglish(), status
);
187 while (index
->nextBucket(status
)) {
189 while (index
->nextRecord(status
)) {
191 TEST_ASSERT(FALSE
); // No items have been added.
196 index
->addRecord(adam
, NULL
, status
);
197 index
->addRecord(baker
, NULL
, status
);
198 index
->addRecord(charlie
, NULL
, status
);
199 index
->addRecord(chad
, NULL
, status
);
202 index
->resetBucketIterator(status
);
203 while (index
->nextBucket(status
)) {
205 while (index
->nextRecord(status
)) {
211 TEST_ASSERT(itemCount
== 4);
213 TEST_ASSERT(index
->nextBucket(status
) == FALSE
);
214 index
->resetBucketIterator(status
);
216 TEST_ASSERT(index
->nextBucket(status
) == TRUE
);
218 index
->clearRecords(status
);
220 index
->resetBucketIterator(status
);
221 while (index
->nextBucket(status
)) {
223 while (index
->nextRecord(status
)) {
224 TEST_ASSERT(FALSE
); // No items have been added.
230 // getBucketLabel(), getBucketType()
232 status
= U_ZERO_ERROR
;
233 index
= new AlphabeticIndex(Locale::getEnglish(), status
);
235 index
->setUnderflowLabel(adam
, status
).setOverflowLabel(charlie
, status
);
237 for (i
=0; index
->nextBucket(status
); i
++) {
239 UnicodeString label
= index
->getBucketLabel();
240 UAlphabeticIndexLabelType type
= index
->getBucketLabelType();
242 TEST_ASSERT(type
== U_ALPHAINDEX_UNDERFLOW
);
243 TEST_ASSERT(label
== adam
);
244 } else if (i
<= 26) {
245 // Labels A - Z for English locale
246 TEST_ASSERT(type
== U_ALPHAINDEX_NORMAL
);
247 UnicodeString
expectedLabel((UChar
)(0x40 + i
));
248 TEST_ASSERT(expectedLabel
== label
);
249 } else if (i
== 27) {
250 TEST_ASSERT(type
== U_ALPHAINDEX_OVERFLOW
);
251 TEST_ASSERT(label
== charlie
);
261 status
= U_ZERO_ERROR
;
262 index
= new AlphabeticIndex(Locale::getEnglish(), status
);
264 int32_t n
= index
->getBucketIndex(adam
, status
);
266 TEST_ASSERT(n
== 1); /* Label #0 is underflow, 1 is A, etc. */
267 n
= index
->getBucketIndex(baker
, status
);
269 n
= index
->getBucketIndex(Cyrillic
, status
);
270 TEST_ASSERT(n
== 27); // Overflow label
271 n
= index
->getBucketIndex(zed
, status
);
272 TEST_ASSERT(n
== 26);
274 for (i
=0; index
->nextBucket(status
); i
++) {
275 n
= index
->getBucketIndex();
277 UnicodeString label
= index
->getBucketLabel();
280 TEST_ASSERT(i
== 28);
283 index
= new AlphabeticIndex(Locale::createFromName("ru"), status
);
284 //Locale loc = Locale::createFromName(localeName);
286 n
= index
->getBucketIndex(adam
, status
);
288 TEST_ASSERT(n
== 0); // Label #0 is underflow
289 n
= index
->getBucketIndex(baker
, status
);
291 n
= index
->getBucketIndex(Cyrillic
, status
);
292 TEST_ASSERT(n
== 1); // Overflow label
293 n
= index
->getBucketIndex(zed
, status
);
301 static const char * KEY_LOCALES
[] = {
302 "en", "es", "de", "fr", "ja", "it", "tr", "pt", "zh", "nl",
303 "pl", "ar", "ru", "zh_Hant", "ko", "th", "sv", "fi", "da",
304 "he", "nb", "el", "hr", "bg", "sk", "lt", "vi", "lv", "sr",
305 "pt_PT", "ro", "hu", "cs", "id", "sl", "fil", "fa", "uk",
306 "ca", "hi", "et", "eu", "is", "sw", "ms", "bn", "am", "ta",
307 "te", "mr", "ur", "ml", "kn", "gu", "or", ""};
310 void AlphabeticIndexTest::ManyLocalesTest() {
311 UErrorCode status
= U_ZERO_ERROR
;
313 AlphabeticIndex
*index
= NULL
;
315 for (int i
=0; ; ++i
) {
316 status
= U_ZERO_ERROR
;
317 const char *localeName
= KEY_LOCALES
[i
];
318 if (localeName
[0] == 0) {
321 // std::cout << localeName << " ";
322 Locale loc
= Locale::createFromName(localeName
);
323 index
= new AlphabeticIndex(loc
, status
);
325 lc
= index
->getBucketCount(status
);
327 // std::cout << "getBucketCount() == " << lc << std::endl;
329 while (index
->nextBucket(status
)) {
331 const UnicodeString
&label
= index
->getBucketLabel();
332 TEST_ASSERT(label
.length()>0);
334 // std::cout << ":" << label.toUTF8String(ss);
336 // std::cout << ":" << std::endl;
344 // Test data for Pinyin based indexes.
345 // The Chinese characters should be distributed under latin labels in
348 static const char *pinyinTestData
[] = {
349 "\\u0101", "\\u5416", "\\u58ba", //
350 "b", "\\u516b", "\\u62d4", "\\u8500", //
351 "c", "\\u5693", "\\u7938", "\\u9e7e", //
352 "d", "\\u5491", "\\u8fcf", "\\u964a", //
353 "\\u0113","\\u59b8", "\\u92e8", "\\u834b", //
354 "f", "\\u53d1", "\\u9197", "\\u99a5", //
355 "g", "\\u7324", "\\u91d3", "\\u8142", //
356 "h", "\\u598e", "\\u927f", "\\u593b", //
357 "j", "\\u4e0c", "\\u6785", "\\u9d58", //
358 "k", "\\u5494", "\\u958b", "\\u7a52", //
359 "l", "\\u5783", "\\u62c9", "\\u9ba5", //
360 "m", "\\u5638", "\\u9ebb", "\\u65c0", //
361 "n", "\\u62ff", "\\u80ad", "\\u685b", //
362 "\\u014D", "\\u5662", "\\u6bee", "\\u8bb4", //
363 "p", "\\u5991", "\\u8019", "\\u8c31", //
364 "q", "\\u4e03", "\\u6053", "\\u7f56", //
365 "r", "\\u5465", "\\u72aa", "\\u6e03", //
366 "s", "\\u4ee8", "\\u9491", "\\u93c1", //
367 "t", "\\u4ed6", "\\u9248", "\\u67dd", //
368 "w", "\\u5c72", "\\u5558", "\\u5a7a", //
369 "x", "\\u5915", "\\u5438", "\\u6bbe", //
370 "y", "\\u4e2b", "\\u82bd", "\\u8574", //
371 "z", "\\u5e00", "\\u707d", "\\u5c0a",
375 void AlphabeticIndexTest::HackPinyinTest() {
376 UErrorCode status
= U_ZERO_ERROR
;
377 AlphabeticIndex
aindex(Locale::createFromName("zh"), status
);
380 UnicodeString names
[sizeof(pinyinTestData
) / sizeof(pinyinTestData
[0])];
382 for (nameCount
=0; pinyinTestData
[nameCount
] != NULL
; nameCount
++) {
383 names
[nameCount
] = UnicodeString(pinyinTestData
[nameCount
], -1, UnicodeString::kInvariant
).unescape();
384 aindex
.addRecord(names
[nameCount
], &names
[nameCount
], status
);
386 if (U_FAILURE(status
)) {
390 TEST_ASSERT(nameCount
== aindex
.getRecordCount(status
));
392 // Weak checking: make sure that none of the Chinese names landed in the overflow bucket
393 // of the index, and that the names are distributed among several buckets.
394 // (Exact expected data would be subject to change with evolution of the collation rules.)
396 int32_t bucketCount
= 0;
397 int32_t filledBucketCount
= 0;
398 while (aindex
.nextBucket(status
)) {
400 UnicodeString label
= aindex
.getBucketLabel();
402 // std::cout << label.toUTF8String(s) << ": ";
404 UBool bucketHasContents
= FALSE
;
405 while (aindex
.nextRecord(status
)) {
406 bucketHasContents
= TRUE
;
407 UnicodeString name
= aindex
.getRecordName();
408 if (aindex
.getBucketLabelType() != U_ALPHAINDEX_NORMAL
) {
409 errln("File %s, Line %d, Name \"\\u%x\" is in an under or overflow bucket.",
410 __FILE__
, __LINE__
, name
.char32At(0));
413 // std::cout << aindex.getRecordName().toUTF8String(s) << " ";
415 if (bucketHasContents
) {
418 // std::cout << std::endl;
420 TEST_ASSERT(bucketCount
> 25);
421 TEST_ASSERT(filledBucketCount
> 15);
425 void AlphabeticIndexTest::TestBug9009() {
426 UErrorCode status
= U_ZERO_ERROR
;
428 AlphabeticIndex
aindex(loc
, status
);
430 aindex
.nextBucket(status
); // Crash here before bug was fixed.