]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
46f4442e | 3 | * Copyright (c) 1997-2008, International Business Machines Corporation and |
b75a7d8f A |
4 | * others. All Rights Reserved. |
5 | ********************************************************************/ | |
6 | //=============================================================================== | |
7 | // | |
8 | // File apitest.cpp | |
9 | // | |
10 | // | |
11 | // | |
12 | // Created by: Helena Shih | |
13 | // | |
14 | // Modification History: | |
15 | // | |
16 | // Date Name Description | |
17 | // 2/5/97 aliu Added streamIn and streamOut methods. Added | |
18 | // constructor which reads RuleBasedCollator object from | |
19 | // a binary file. Added writeToFile method which streams | |
20 | // RuleBasedCollator out to a binary file. The streamIn | |
21 | // and streamOut methods use istream and ostream objects | |
22 | // in binary mode. | |
23 | // 6/30/97 helena Added tests for CollationElementIterator::setText, getOffset | |
24 | // setOffset and DecompositionIterator::getOffset, setOffset. | |
25 | // DecompositionIterator is made public so add class scope | |
26 | // testing. | |
27 | // 02/10/98 damiba Added test for compare(UnicodeString&, UnicodeString&, int32_t) | |
28 | //=============================================================================== | |
29 | ||
30 | #include "unicode/utypes.h" | |
31 | ||
32 | #if !UCONFIG_NO_COLLATION | |
33 | ||
34 | #include "unicode/coll.h" | |
35 | #include "unicode/tblcoll.h" | |
36 | #include "unicode/coleitr.h" | |
37 | #include "unicode/sortkey.h" | |
38 | #include "apicoll.h" | |
39 | #include "unicode/chariter.h" | |
40 | #include "unicode/schriter.h" | |
41 | #include "unicode/ustring.h" | |
73c04bcf | 42 | #include "unicode/ucol.h" |
b75a7d8f A |
43 | |
44 | #include "sfwdchit.h" | |
45 | #include "cmemory.h" | |
73c04bcf | 46 | #include <stdlib.h> |
b75a7d8f A |
47 | |
48 | void | |
49 | CollationAPITest::doAssert(UBool condition, const char *message) | |
50 | { | |
51 | if (!condition) { | |
52 | errln(UnicodeString("ERROR : ") + message); | |
53 | } | |
54 | } | |
55 | ||
374ca955 | 56 | #ifdef U_USE_COLLATION_OBSOLETE_2_6 |
b75a7d8f A |
57 | /* |
58 | * Test Collator::createInstance(... version...) for some locale. Called by TestProperty(). | |
59 | */ | |
60 | static void | |
61 | TestOpenVersion(IntlTest &test, const Locale &locale) { | |
62 | UVersionInfo version1, version2; | |
63 | Collator *collator1, *collator2; | |
64 | UErrorCode errorCode; | |
65 | ||
66 | errorCode=U_ZERO_ERROR; | |
67 | collator1=Collator::createInstance(locale, errorCode); | |
68 | if(U_SUCCESS(errorCode)) { | |
69 | /* get the current version */ | |
70 | collator1->getVersion(version1); | |
71 | delete collator1; | |
72 | ||
73 | /* try to get that same version again */ | |
74 | collator2=Collator::createInstance(locale, version1, errorCode); | |
75 | if(U_SUCCESS(errorCode)) { | |
76 | collator2->getVersion(version2); | |
77 | if(0!=uprv_memcmp(version1, version2, sizeof(UVersionInfo))) { | |
78 | test.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) returns a different collator\n", locale.getName(), locale.getName()); | |
79 | } | |
80 | delete collator2; | |
81 | } else { | |
82 | test.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) fails: %s\n", locale.getName(), locale.getName(), u_errorName(errorCode)); | |
83 | } | |
84 | } | |
85 | } | |
374ca955 | 86 | #endif |
b75a7d8f A |
87 | |
88 | // Collator Class Properties | |
89 | // ctor, dtor, createInstance, compare, getStrength/setStrength | |
90 | // getDecomposition/setDecomposition, getDisplayName | |
91 | void | |
92 | CollationAPITest::TestProperty(/* char* par */) | |
93 | { | |
94 | UErrorCode success = U_ZERO_ERROR; | |
95 | Collator *col = 0; | |
96 | /* | |
97 | All the collations have the same version in an ICU | |
98 | version. | |
99 | ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02}; | |
100 | ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03}; | |
101 | ICU 2.2 currVersionArray = {0x21, 0x40, 0x04, 0x04}; | |
102 | ICU 2.4 currVersionArray = {0x21, 0x40, 0x04, 0x04}; | |
374ca955 | 103 | ICU 2.6 currVersionArray = {0x21, 0x40, 0x03, 0x03}; |
73c04bcf A |
104 | ICU 2.8 currVersionArray = {0x29, 0x80, 0x00, 0x04}; |
105 | ICU 3.4 currVersionArray = {0x31, 0xC0, 0x00, 0x04}; | |
b75a7d8f | 106 | */ |
73c04bcf | 107 | UVersionInfo currVersionArray = {0x31, 0xC0, 0x00, 0x05}; |
b75a7d8f A |
108 | UVersionInfo versionArray; |
109 | int i = 0; | |
110 | ||
111 | logln("The property tests begin : "); | |
112 | logln("Test ctors : "); | |
113 | col = Collator::createInstance(Locale::getEnglish(), success); | |
114 | ||
115 | if (U_FAILURE(success)) | |
116 | { | |
117 | errln("Default Collator creation failed."); | |
118 | return; | |
119 | } | |
120 | ||
121 | col->getVersion(versionArray); | |
122 | for (i=0; i<4; ++i) { | |
123 | if (versionArray[i] != currVersionArray[i]) { | |
124 | errln("Testing ucol_getVersion() - unexpected result: %d.%d.%d.%d", | |
125 | versionArray[0], versionArray[1], versionArray[2], versionArray[3]); | |
126 | break; | |
127 | } | |
128 | } | |
129 | ||
130 | doAssert((col->compare("ab", "abc") == Collator::LESS), "ab < abc comparison failed"); | |
131 | doAssert((col->compare("ab", "AB") == Collator::LESS), "ab < AB comparison failed"); | |
132 | doAssert((col->compare("blackbird", "black-bird") == Collator::GREATER), "black-bird > blackbird comparison failed"); | |
133 | doAssert((col->compare("black bird", "black-bird") == Collator::LESS), "black bird > black-bird comparison failed"); | |
134 | doAssert((col->compare("Hello", "hello") == Collator::GREATER), "Hello > hello comparison failed"); | |
135 | ||
136 | ||
137 | /*start of update [Bertrand A. D. 02/10/98]*/ | |
138 | doAssert((col->compare("ab", "abc", 2) == Collator::EQUAL), "ab = abc with length 2 comparison failed"); | |
139 | doAssert((col->compare("ab", "AB", 2) == Collator::LESS), "ab < AB with length 2 comparison failed"); | |
140 | doAssert((col->compare("ab", "Aa", 1) == Collator::LESS), "ab < Aa with length 1 comparison failed"); | |
141 | doAssert((col->compare("ab", "Aa", 2) == Collator::GREATER), "ab > Aa with length 2 comparison failed"); | |
142 | doAssert((col->compare("black-bird", "blackbird", 5) == Collator::EQUAL), "black-bird = blackbird with length of 5 comparison failed"); | |
143 | doAssert((col->compare("black bird", "black-bird", 10) == Collator::LESS), "black bird < black-bird with length 10 comparison failed"); | |
144 | doAssert((col->compare("Hello", "hello", 5) == Collator::GREATER), "Hello > hello with length 5 comparison failed"); | |
145 | /*end of update [Bertrand A. D. 02/10/98]*/ | |
146 | ||
147 | ||
148 | logln("Test ctors ends."); | |
149 | logln("testing Collator::getStrength() method ..."); | |
150 | doAssert((col->getStrength() == Collator::TERTIARY), "collation object has the wrong strength"); | |
151 | doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference"); | |
152 | ||
153 | ||
154 | logln("testing Collator::setStrength() method ..."); | |
155 | col->setStrength(Collator::SECONDARY); | |
156 | doAssert((col->getStrength() != Collator::TERTIARY), "collation object's strength is secondary difference"); | |
157 | doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference"); | |
158 | doAssert((col->getStrength() == Collator::SECONDARY), "collation object has the wrong strength"); | |
159 | ||
160 | UnicodeString name; | |
161 | ||
162 | logln("Get display name for the US English collation in German : "); | |
163 | logln(Collator::getDisplayName(Locale::getUS(), Locale::getGerman(), name)); | |
164 | doAssert((name == UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed"); | |
165 | ||
166 | logln("Get display name for the US English collation in English : "); | |
167 | logln(Collator::getDisplayName(Locale::getUS(), Locale::getEnglish(), name)); | |
168 | doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed"); | |
169 | #if 0 | |
170 | // weiv : this test is bogus if we're running on any machine that has different default locale than English. | |
171 | // Therefore, it is banned! | |
172 | logln("Get display name for the US English in default locale language : "); | |
173 | logln(Collator::getDisplayName(Locale::US, name)); | |
174 | doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine"); | |
175 | #endif | |
176 | delete col; col = 0; | |
177 | RuleBasedCollator *rcol = (RuleBasedCollator *)Collator::createInstance("da_DK", | |
178 | success); | |
179 | doAssert(rcol->getRules().length() != 0, "da_DK rules does not have length 0"); | |
180 | delete rcol; | |
181 | ||
182 | col = Collator::createInstance(Locale::getFrench(), success); | |
183 | if (U_FAILURE(success)) | |
184 | { | |
185 | errln("Creating French collation failed."); | |
186 | return; | |
187 | } | |
188 | ||
189 | col->setStrength(Collator::PRIMARY); | |
190 | logln("testing Collator::getStrength() method again ..."); | |
191 | doAssert((col->getStrength() != Collator::TERTIARY), "collation object has the wrong strength"); | |
192 | doAssert((col->getStrength() == Collator::PRIMARY), "collation object's strength is not primary difference"); | |
193 | ||
194 | logln("testing French Collator::setStrength() method ..."); | |
195 | col->setStrength(Collator::TERTIARY); | |
196 | doAssert((col->getStrength() == Collator::TERTIARY), "collation object's strength is not tertiary difference"); | |
197 | doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference"); | |
198 | doAssert((col->getStrength() != Collator::SECONDARY), "collation object's strength is secondary difference"); | |
199 | ||
200 | logln("Create junk collation: "); | |
201 | Locale abcd("ab", "CD", ""); | |
202 | success = U_ZERO_ERROR; | |
203 | Collator *junk = 0; | |
204 | junk = Collator::createInstance(abcd, success); | |
205 | ||
206 | if (U_FAILURE(success)) | |
207 | { | |
208 | errln("Junk collation creation failed, should at least return default."); | |
209 | delete col; | |
210 | return; | |
211 | } | |
212 | ||
213 | delete col; | |
214 | col = Collator::createInstance(success); | |
215 | if (U_FAILURE(success)) | |
216 | { | |
217 | errln("Creating default collator failed."); | |
218 | delete junk; | |
219 | return; | |
220 | } | |
221 | ||
222 | doAssert(((RuleBasedCollator *)col)->getRules() == ((RuleBasedCollator *)junk)->getRules(), | |
223 | "The default collation should be returned."); | |
224 | Collator *frCol = Collator::createInstance(Locale::getFrance(), success); | |
225 | if (U_FAILURE(success)) | |
226 | { | |
227 | errln("Creating French collator failed."); | |
46f4442e A |
228 | delete col; |
229 | delete junk; | |
b75a7d8f A |
230 | return; |
231 | } | |
232 | ||
46f4442e A |
233 | // If the default locale isn't French, the French and non-French collators |
234 | // should be different | |
235 | if (frCol->getLocale(ULOC_ACTUAL_LOCALE, success) != Locale::getFrench()) { | |
236 | doAssert((*frCol != *junk), "The junk is the same as the French collator."); | |
237 | } | |
b75a7d8f A |
238 | Collator *aFrCol = frCol->clone(); |
239 | doAssert((*frCol == *aFrCol), "The cloning of a French collator failed."); | |
240 | logln("Collator property test ended."); | |
241 | ||
242 | delete col; | |
243 | delete frCol; | |
244 | delete aFrCol; | |
245 | delete junk; | |
246 | ||
374ca955 | 247 | #ifdef U_USE_COLLATION_OBSOLETE_2_6 |
b75a7d8f A |
248 | /* test Collator::createInstance(...version...) */ |
249 | TestOpenVersion(*this, ""); | |
250 | TestOpenVersion(*this, "da"); | |
251 | TestOpenVersion(*this, "fr"); | |
252 | TestOpenVersion(*this, "ja"); | |
253 | ||
254 | /* try some bogus version */ | |
255 | versionArray[0]=0; | |
256 | versionArray[1]=0x99; | |
257 | versionArray[2]=0xc7; | |
258 | versionArray[3]=0xfe; | |
259 | col=Collator::createInstance(Locale(), versionArray, success); | |
260 | if(U_SUCCESS(success)) { | |
261 | errln("error: ucol_openVersion(bogus version) succeeded"); | |
262 | delete col; | |
263 | } | |
374ca955 | 264 | #endif |
b75a7d8f A |
265 | } |
266 | ||
267 | void | |
268 | CollationAPITest::TestRuleBasedColl() | |
269 | { | |
270 | RuleBasedCollator *col1, *col2, *col3, *col4; | |
271 | UErrorCode status = U_ZERO_ERROR; | |
272 | ||
273 | UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E"); | |
274 | UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E"); | |
275 | ||
276 | col1 = new RuleBasedCollator(ruleset1, status); | |
277 | if (U_FAILURE(status)) { | |
278 | errln("RuleBased Collator creation failed.\n"); | |
279 | return; | |
280 | } | |
281 | else { | |
282 | logln("PASS: RuleBased Collator creation passed\n"); | |
283 | } | |
284 | ||
285 | status = U_ZERO_ERROR; | |
286 | col2 = new RuleBasedCollator(ruleset2, status); | |
287 | if (U_FAILURE(status)) { | |
288 | errln("RuleBased Collator creation failed.\n"); | |
289 | return; | |
290 | } | |
291 | else { | |
292 | logln("PASS: RuleBased Collator creation passed\n"); | |
293 | } | |
294 | ||
295 | status = U_ZERO_ERROR; | |
296 | Locale locale("aa", "AA"); | |
297 | col3 = (RuleBasedCollator *)Collator::createInstance(locale, status); | |
298 | if (U_FAILURE(status)) { | |
299 | errln("Fallback Collator creation failed.: %s\n"); | |
300 | return; | |
301 | } | |
302 | else { | |
303 | logln("PASS: Fallback Collator creation passed\n"); | |
304 | } | |
305 | delete col3; | |
306 | ||
307 | status = U_ZERO_ERROR; | |
308 | col3 = (RuleBasedCollator *)Collator::createInstance(status); | |
309 | if (U_FAILURE(status)) { | |
310 | errln("Default Collator creation failed.: %s\n"); | |
311 | return; | |
312 | } | |
313 | else { | |
314 | logln("PASS: Default Collator creation passed\n"); | |
315 | } | |
316 | ||
317 | UnicodeString rule1 = col1->getRules(); | |
318 | UnicodeString rule2 = col2->getRules(); | |
319 | UnicodeString rule3 = col3->getRules(); | |
320 | ||
321 | doAssert(rule1 != rule2, "Default collator getRules failed"); | |
322 | doAssert(rule2 != rule3, "Default collator getRules failed"); | |
323 | doAssert(rule1 != rule3, "Default collator getRules failed"); | |
324 | ||
325 | col4 = new RuleBasedCollator(rule2, status); | |
326 | if (U_FAILURE(status)) { | |
327 | errln("RuleBased Collator creation failed.\n"); | |
328 | return; | |
329 | } | |
330 | ||
331 | UnicodeString rule4 = col4->getRules(); | |
332 | doAssert(rule2 == rule4, "Default collator getRules failed"); | |
333 | int32_t length4 = 0; | |
334 | uint8_t *clonedrule4 = col4->cloneRuleData(length4, status); | |
335 | if (U_FAILURE(status)) { | |
336 | errln("Cloned rule data failed.\n"); | |
337 | return; | |
338 | } | |
339 | ||
340 | // free(clonedrule4); BAD API!!!! | |
341 | uprv_free(clonedrule4); | |
342 | ||
343 | ||
344 | delete col1; | |
345 | delete col2; | |
346 | delete col3; | |
347 | delete col4; | |
348 | } | |
349 | ||
350 | void | |
351 | CollationAPITest::TestRules() | |
352 | { | |
353 | RuleBasedCollator *coll; | |
354 | UErrorCode status = U_ZERO_ERROR; | |
355 | UnicodeString rules; | |
356 | ||
357 | coll = (RuleBasedCollator *)Collator::createInstance(Locale::getEnglish(), status); | |
358 | if (U_FAILURE(status)) { | |
359 | errln("English Collator creation failed.\n"); | |
360 | return; | |
361 | } | |
362 | else { | |
363 | logln("PASS: RuleBased Collator creation passed\n"); | |
364 | } | |
365 | ||
366 | coll->getRules(UCOL_TAILORING_ONLY, rules); | |
374ca955 A |
367 | if (rules.length() != 0x0a) { |
368 | errln("English tailored rules failed - length is 0x%x expected 0x%x", rules.length(), 0x0e); | |
b75a7d8f A |
369 | } |
370 | ||
371 | coll->getRules(UCOL_FULL_RULES, rules); | |
372 | if (rules.length() < 0) { | |
373 | errln("English full rules failed"); | |
374 | } | |
375 | delete coll; | |
376 | } | |
377 | ||
378 | void | |
379 | CollationAPITest::TestDecomposition() { | |
380 | UErrorCode status = U_ZERO_ERROR; | |
381 | Collator *en_US = Collator::createInstance("en_US", status), | |
382 | *el_GR = Collator::createInstance("el_GR", status), | |
383 | *vi_VN = Collator::createInstance("vi_VN", status); | |
384 | ||
385 | if (U_FAILURE(status)) { | |
386 | errln("ERROR: collation creation failed.\n"); | |
387 | return; | |
388 | } | |
389 | ||
390 | /* there is no reason to have canonical decomposition in en_US OR default locale */ | |
391 | if (vi_VN->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON) | |
392 | { | |
374ca955 | 393 | errln("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n"); |
b75a7d8f A |
394 | } |
395 | ||
396 | if (el_GR->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON) | |
397 | { | |
374ca955 | 398 | errln("ERROR: el_GR collation did not have canonical decomposition for normalization!\n"); |
b75a7d8f A |
399 | } |
400 | ||
401 | if (en_US->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_OFF) | |
402 | { | |
374ca955 | 403 | errln("ERROR: en_US collation had canonical decomposition for normalization!\n"); |
b75a7d8f A |
404 | } |
405 | ||
406 | delete en_US; | |
407 | delete el_GR; | |
408 | delete vi_VN; | |
409 | } | |
410 | ||
411 | void | |
412 | CollationAPITest::TestSafeClone() { | |
413 | static const int CLONETEST_COLLATOR_COUNT = 3; | |
414 | Collator *someCollators [CLONETEST_COLLATOR_COUNT]; | |
415 | Collator *col; | |
416 | UErrorCode err = U_ZERO_ERROR; | |
417 | int index; | |
418 | ||
419 | UnicodeString test1("abCda"); | |
420 | UnicodeString test2("abcda"); | |
421 | ||
422 | /* one default collator & two complex ones */ | |
423 | someCollators[0] = Collator::createInstance("en_US", err); | |
424 | someCollators[1] = Collator::createInstance("ko", err); | |
425 | someCollators[2] = Collator::createInstance("ja_JP", err); | |
426 | if(U_FAILURE(err)) { | |
427 | errln("Couldn't instantiate collators. Error: %s", u_errorName(err)); | |
428 | delete someCollators[0]; | |
429 | delete someCollators[1]; | |
430 | delete someCollators[2]; | |
431 | return; | |
432 | } | |
433 | ||
434 | /* change orig & clone & make sure they are independent */ | |
435 | ||
436 | for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++) | |
437 | { | |
438 | col = someCollators[index]->safeClone(); | |
439 | if (col == 0) { | |
440 | errln("SafeClone of collator should not return null\n"); | |
441 | break; | |
442 | } | |
443 | col->setStrength(Collator::TERTIARY); | |
444 | someCollators[index]->setStrength(Collator::PRIMARY); | |
445 | col->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err); | |
446 | someCollators[index]->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err); | |
447 | ||
448 | doAssert(col->greater(test1, test2), "Result should be \"abCda\" >>> \"abcda\" "); | |
449 | doAssert(someCollators[index]->equals(test1, test2), "Result should be \"abcda\" == \"abCda\""); | |
450 | delete col; | |
451 | delete someCollators[index]; | |
452 | } | |
453 | } | |
454 | ||
455 | void | |
456 | CollationAPITest::TestHashCode(/* char* par */) | |
457 | { | |
458 | logln("hashCode tests begin."); | |
459 | UErrorCode success = U_ZERO_ERROR; | |
460 | Collator *col1 = 0; | |
461 | col1 = Collator::createInstance(Locale::getEnglish(), success); | |
462 | if (U_FAILURE(success)) | |
463 | { | |
464 | errln("Default collation creation failed."); | |
465 | return; | |
466 | } | |
467 | ||
468 | Collator *col2 = 0; | |
469 | Locale dk("da", "DK", ""); | |
470 | col2 = Collator::createInstance(dk, success); | |
471 | if (U_FAILURE(success)) | |
472 | { | |
473 | errln("Danish collation creation failed."); | |
474 | return; | |
475 | } | |
476 | ||
477 | Collator *col3 = 0; | |
478 | col3 = Collator::createInstance(Locale::getEnglish(), success); | |
479 | if (U_FAILURE(success)) | |
480 | { | |
481 | errln("2nd default collation creation failed."); | |
482 | return; | |
483 | } | |
484 | ||
485 | logln("Collator::hashCode() testing ..."); | |
486 | ||
487 | doAssert(col1->hashCode() != col2->hashCode(), "Hash test1 result incorrect" ); | |
488 | doAssert(!(col1->hashCode() == col2->hashCode()), "Hash test2 result incorrect" ); | |
489 | doAssert(col1->hashCode() == col3->hashCode(), "Hash result not equal" ); | |
490 | ||
491 | logln("hashCode tests end."); | |
492 | delete col1; | |
493 | delete col2; | |
494 | ||
495 | UnicodeString test1("Abcda"); | |
496 | UnicodeString test2("abcda"); | |
497 | ||
498 | CollationKey sortk1, sortk2, sortk3; | |
499 | UErrorCode status = U_ZERO_ERROR; | |
500 | ||
501 | col3->getCollationKey(test1, sortk1, status); | |
502 | col3->getCollationKey(test2, sortk2, status); | |
503 | col3->getCollationKey(test2, sortk3, status); | |
504 | ||
505 | doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect"); | |
506 | doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal" ); | |
507 | ||
508 | delete col3; | |
509 | } | |
510 | ||
511 | //---------------------------------------------------------------------------- | |
512 | // CollationKey -- Tests the CollationKey methods | |
513 | // | |
514 | void | |
515 | CollationAPITest::TestCollationKey(/* char* par */) | |
516 | { | |
517 | logln("testing CollationKey begins..."); | |
518 | Collator *col = 0; | |
519 | UErrorCode success=U_ZERO_ERROR; | |
520 | col = Collator::createInstance(Locale::getEnglish(), success); | |
521 | if (U_FAILURE(success)) | |
522 | { | |
523 | errln("Default collation creation failed."); | |
524 | return; | |
525 | } | |
526 | col->setStrength(Collator::TERTIARY); | |
527 | ||
528 | CollationKey sortk1, sortk2; | |
529 | UnicodeString test1("Abcda"), test2("abcda"); | |
530 | UErrorCode key1Status = U_ZERO_ERROR, key2Status = U_ZERO_ERROR; | |
531 | ||
532 | logln("Testing weird arguments"); | |
533 | col->getCollationKey(NULL, 0, sortk1, key1Status); | |
534 | // key gets reset here | |
535 | int32_t length; | |
536 | sortk1.getByteArray(length); | |
537 | doAssert(sortk1.isBogus() == FALSE && length == 0, | |
538 | "Empty string should return an empty collation key"); | |
539 | // bogus key returned here | |
540 | key1Status = U_ILLEGAL_ARGUMENT_ERROR; | |
541 | col->getCollationKey(NULL, 0, sortk1, key1Status); | |
542 | doAssert(sortk1.getByteArray(length) == NULL && length == 0, | |
543 | "Error code should return bogus collation key"); | |
544 | ||
545 | key1Status = U_ZERO_ERROR; | |
546 | logln("Use tertiary comparison level testing ...."); | |
547 | ||
548 | col->getCollationKey(test1, sortk1, key1Status); | |
549 | doAssert((sortk1.compareTo(col->getCollationKey(test2, sortk2, key2Status))) | |
550 | == Collator::GREATER, | |
551 | "Result should be \"Abcda\" >>> \"abcda\""); | |
552 | ||
553 | CollationKey sortk3(sortk2), sortkNew, sortkEmpty; | |
554 | ||
555 | ||
556 | sortkNew = sortk1; | |
557 | doAssert((sortk1 != sortk2), "The sort keys should be different"); | |
558 | doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() failed"); | |
559 | doAssert((sortk2 == sortk3), "The sort keys should be the same"); | |
560 | doAssert((sortk1 == sortkNew), "The sort keys assignment failed"); | |
561 | doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() failed"); | |
562 | doAssert((sortkNew != sortk3), "The sort keys should be different"); | |
563 | doAssert(sortk1.compareTo(sortk3) == Collator::GREATER, "Result should be \"Abcda\" >>> \"abcda\""); | |
564 | doAssert(sortk2.compareTo(sortk3) == Collator::EQUAL, "Result should be \"abcda\" == \"abcda\""); | |
565 | doAssert(sortkEmpty.compareTo(sortk1) == Collator::LESS, "Result should be (empty key) <<< \"Abcda\""); | |
566 | doAssert(sortk1.compareTo(sortkEmpty) == Collator::GREATER, "Result should be \"Abcda\" >>> (empty key)"); | |
567 | doAssert(sortkEmpty.compareTo(sortkEmpty) == Collator::EQUAL, "Result should be (empty key) == (empty key)"); | |
568 | doAssert(sortk1.compareTo(sortk3, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> \"abcda\""); | |
569 | doAssert(sortk2.compareTo(sortk3, success) == UCOL_EQUAL, "Result should be \"abcda\" == \"abcda\""); | |
570 | doAssert(sortkEmpty.compareTo(sortk1, success) == UCOL_LESS, "Result should be (empty key) <<< \"Abcda\""); | |
571 | doAssert(sortk1.compareTo(sortkEmpty, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> (empty key)"); | |
572 | doAssert(sortkEmpty.compareTo(sortkEmpty, success) == UCOL_EQUAL, "Result should be (empty key) == (empty key)"); | |
573 | ||
574 | int32_t cnt1, cnt2, cnt3, cnt4; | |
575 | ||
576 | const uint8_t* byteArray1 = sortk1.getByteArray(cnt1); | |
577 | const uint8_t* byteArray2 = sortk2.getByteArray(cnt2); | |
578 | ||
579 | /* | |
580 | this is a bad test since it is dependent on the version of uca data, | |
581 | which changes | |
582 | will remove it. | |
583 | const char sortk2_compat[] = { | |
584 | // this is a 1.8 sortkey | |
585 | 0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00 | |
586 | }; | |
587 | */ | |
588 | ||
589 | const uint8_t* byteArray3 = 0; | |
590 | byteArray3 = sortk1.getByteArray(cnt3); | |
591 | ||
592 | const uint8_t* byteArray4 = 0; | |
593 | byteArray4 = sortk2.getByteArray(cnt4); | |
594 | ||
595 | CollationKey sortk4(byteArray1, cnt1), sortk5(byteArray2, cnt2); | |
596 | CollationKey sortk6(byteArray3, cnt3), sortk7(byteArray4, cnt4); | |
597 | ||
598 | /* | |
599 | doAssert(memcmp(byteArray2, sortk2_compat, strlen(sortk2_compat)) == 0, | |
600 | "Binary format for 'abcda' sortkey different!"); | |
601 | */ | |
602 | doAssert(sortk1.compareTo(sortk4) == Collator::EQUAL, "CollationKey::toByteArray(sortk1) Failed."); | |
603 | doAssert(sortk2.compareTo(sortk5) == Collator::EQUAL, "CollationKey::toByteArray(sortk2) Failed."); | |
604 | doAssert(sortk4.compareTo(sortk5) == Collator::GREATER, "sortk4 >>> sortk5 Failed"); | |
605 | doAssert(sortk1.compareTo(sortk6) == Collator::EQUAL, "CollationKey::getByteArray(sortk1) Failed."); | |
606 | doAssert(sortk2.compareTo(sortk7) == Collator::EQUAL, "CollationKey::getByteArray(sortk2) Failed."); | |
607 | doAssert(sortk6.compareTo(sortk7) == Collator::GREATER, "sortk6 >>> sortk7 Failed"); | |
608 | ||
609 | logln("Equality tests : "); | |
610 | doAssert(sortk1 == sortk4, "sortk1 == sortk4 Failed."); | |
611 | doAssert(sortk2 == sortk5, "sortk2 == sortk5 Failed."); | |
612 | doAssert(sortk1 != sortk5, "sortk1 != sortk5 Failed."); | |
613 | doAssert(sortk1 == sortk6, "sortk1 == sortk6 Failed."); | |
614 | doAssert(sortk2 == sortk7, "sortk2 == sortk7 Failed."); | |
615 | doAssert(sortk1 != sortk7, "sortk1 != sortk7 Failed."); | |
616 | ||
617 | byteArray1 = 0; | |
618 | byteArray2 = 0; | |
619 | ||
620 | sortk3 = sortk1; | |
621 | doAssert(sortk1 == sortk3, "sortk1 = sortk3 assignment Failed."); | |
622 | doAssert(sortk2 != sortk3, "sortk2 != sortk3 Failed."); | |
623 | logln("testing sortkey ends..."); | |
624 | ||
625 | col->setStrength(Collator::SECONDARY); | |
626 | doAssert(col->getCollationKey(test1, sortk1, key1Status).compareTo( | |
627 | col->getCollationKey(test2, sortk2, key2Status)) | |
628 | == Collator::EQUAL, | |
629 | "Result should be \"Abcda\" == \"abcda\""); | |
630 | delete col; | |
631 | } | |
632 | ||
633 | //---------------------------------------------------------------------------- | |
634 | // Tests the CollatorElementIterator class. | |
635 | // ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!= | |
636 | // | |
637 | void | |
638 | CollationAPITest::TestElemIter(/* char* par */) | |
639 | { | |
640 | logln("testing sortkey begins..."); | |
641 | Collator *col = 0; | |
642 | UErrorCode success = U_ZERO_ERROR; | |
643 | col = Collator::createInstance(Locale::getEnglish(), success); | |
644 | if (U_FAILURE(success)) | |
645 | { | |
646 | errln("Default collation creation failed."); | |
647 | return; | |
648 | } | |
649 | ||
650 | UnicodeString testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?"); | |
651 | UnicodeString testString2("Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?"); | |
652 | logln("Constructors and comparison testing...."); | |
653 | CollationElementIterator *iterator1 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1); | |
654 | ||
655 | CharacterIterator *chariter=new StringCharacterIterator(testString1); | |
656 | CollationElementIterator *coliter=((RuleBasedCollator*)col)->createCollationElementIterator(*chariter); | |
657 | ||
658 | // copy ctor | |
659 | CollationElementIterator *iterator2 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1); | |
660 | CollationElementIterator *iterator3 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString2); | |
661 | ||
662 | int32_t offset = iterator1->getOffset(); | |
663 | if (offset != 0) { | |
664 | errln("Error in getOffset for collation element iterator\n"); | |
665 | return; | |
666 | } | |
667 | iterator1->setOffset(6, success); | |
668 | if (U_FAILURE(success)) { | |
669 | errln("Error in setOffset for collation element iterator\n"); | |
670 | return; | |
671 | } | |
672 | iterator1->setOffset(0, success); | |
673 | int32_t order1, order2, order3; | |
674 | doAssert((*iterator1 == *iterator2), "The two iterators should be the same"); | |
675 | doAssert((*iterator1 != *iterator3), "The two iterators should be different"); | |
676 | ||
677 | doAssert((*coliter == *iterator1), "The two iterators should be the same"); | |
678 | doAssert((*coliter == *iterator2), "The two iterators should be the same"); | |
679 | doAssert((*coliter != *iterator3), "The two iterators should be different"); | |
680 | ||
681 | order1 = iterator1->next(success); | |
682 | if (U_FAILURE(success)) | |
683 | { | |
684 | errln("Somehow ran out of memory stepping through the iterator."); | |
685 | return; | |
686 | } | |
687 | ||
688 | doAssert((*iterator1 != *iterator2), "The first iterator advance failed"); | |
689 | order2 = iterator2->getOffset(); | |
690 | doAssert((order1 != order2), "The order result should not be the same"); | |
691 | order2 = iterator2->next(success); | |
692 | if (U_FAILURE(success)) | |
693 | { | |
694 | errln("Somehow ran out of memory stepping through the iterator."); | |
695 | return; | |
696 | } | |
697 | ||
698 | doAssert((*iterator1 == *iterator2), "The second iterator advance failed"); | |
699 | doAssert((order1 == order2), "The order result should be the same"); | |
700 | order3 = iterator3->next(success); | |
701 | if (U_FAILURE(success)) | |
702 | { | |
703 | errln("Somehow ran out of memory stepping through the iterator."); | |
704 | return; | |
705 | } | |
706 | ||
707 | doAssert((CollationElementIterator::primaryOrder(order1) == | |
708 | CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same"); | |
709 | doAssert((CollationElementIterator::secondaryOrder(order1) == | |
710 | CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same"); | |
711 | doAssert((CollationElementIterator::tertiaryOrder(order1) == | |
712 | CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same"); | |
713 | ||
714 | order1 = iterator1->next(success); order3 = iterator3->next(success); | |
715 | if (U_FAILURE(success)) | |
716 | { | |
717 | errln("Somehow ran out of memory stepping through the iterator."); | |
718 | return; | |
719 | } | |
720 | ||
721 | doAssert((CollationElementIterator::primaryOrder(order1) == | |
722 | CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical"); | |
723 | doAssert((CollationElementIterator::tertiaryOrder(order1) != | |
724 | CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different"); | |
725 | ||
726 | order1 = iterator1->next(success); | |
727 | order3 = iterator3->next(success); | |
728 | /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */ | |
729 | /* | |
730 | doAssert((CollationElementIterator::secondaryOrder(order1) != | |
731 | CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same"); | |
732 | */ | |
733 | doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached"); | |
734 | ||
735 | iterator1->reset(); iterator2->reset(); iterator3->reset(); | |
736 | order1 = iterator1->next(success); | |
737 | if (U_FAILURE(success)) | |
738 | { | |
739 | errln("Somehow ran out of memory stepping through the iterator."); | |
740 | return; | |
741 | } | |
742 | ||
743 | doAssert((*iterator1 != *iterator2), "The first iterator advance failed"); | |
744 | ||
745 | order2 = iterator2->next(success); | |
746 | if (U_FAILURE(success)) | |
747 | { | |
748 | errln("Somehow ran out of memory stepping through the iterator."); | |
749 | return; | |
750 | } | |
751 | ||
752 | doAssert((*iterator1 == *iterator2), "The second iterator advance failed"); | |
753 | doAssert((order1 == order2), "The order result should be the same"); | |
754 | ||
755 | order3 = iterator3->next(success); | |
756 | if (U_FAILURE(success)) | |
757 | { | |
758 | errln("Somehow ran out of memory stepping through the iterator."); | |
759 | return; | |
760 | } | |
761 | ||
762 | doAssert((CollationElementIterator::primaryOrder(order1) == | |
763 | CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same"); | |
764 | doAssert((CollationElementIterator::secondaryOrder(order1) == | |
765 | CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same"); | |
766 | doAssert((CollationElementIterator::tertiaryOrder(order1) == | |
767 | CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same"); | |
768 | ||
769 | order1 = iterator1->next(success); order2 = iterator2->next(success); order3 = iterator3->next(success); | |
770 | if (U_FAILURE(success)) | |
771 | { | |
772 | errln("Somehow ran out of memory stepping through the iterator."); | |
773 | return; | |
774 | } | |
775 | ||
776 | doAssert((CollationElementIterator::primaryOrder(order1) == | |
777 | CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical"); | |
778 | doAssert((CollationElementIterator::tertiaryOrder(order1) != | |
779 | CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different"); | |
780 | ||
781 | order1 = iterator1->next(success); order3 = iterator3->next(success); | |
782 | if (U_FAILURE(success)) | |
783 | { | |
784 | errln("Somehow ran out of memory stepping through the iterator."); | |
785 | return; | |
786 | } | |
787 | ||
788 | /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */ | |
789 | /* | |
790 | doAssert((CollationElementIterator::secondaryOrder(order1) != | |
791 | CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same"); | |
792 | */ | |
793 | doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached"); | |
794 | doAssert((*iterator2 != *iterator3), "The iterators should be different"); | |
795 | ||
796 | ||
797 | //test error values | |
798 | success=U_UNSUPPORTED_ERROR; | |
799 | Collator *colerror=NULL; | |
800 | colerror=Collator::createInstance(Locale::getEnglish(), success); | |
801 | if (colerror != 0 || success == U_ZERO_ERROR){ | |
802 | errln("Error: createInstance(UErrorCode != U_ZERO_ERROR) should just return and not create an instance\n"); | |
803 | } | |
804 | int32_t position=coliter->previous(success); | |
805 | if(position != CollationElementIterator::NULLORDER){ | |
806 | errln((UnicodeString)"Expected NULLORDER got" + position); | |
807 | } | |
808 | coliter->reset(); | |
809 | coliter->setText(*chariter, success); | |
810 | if(!U_FAILURE(success)){ | |
811 | errln("Expeceted error"); | |
812 | } | |
813 | iterator1->setText((UnicodeString)"hello there", success); | |
814 | if(!U_FAILURE(success)){ | |
815 | errln("Expeceted error"); | |
816 | } | |
817 | ||
818 | delete chariter; | |
819 | delete coliter; | |
820 | delete iterator1; | |
821 | delete iterator2; | |
822 | delete iterator3; | |
823 | delete col; | |
824 | ||
825 | ||
826 | ||
827 | logln("testing CollationElementIterator ends..."); | |
828 | } | |
829 | ||
830 | // Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules | |
831 | void | |
832 | CollationAPITest::TestOperators(/* char* par */) | |
833 | { | |
834 | UErrorCode success = U_ZERO_ERROR; | |
835 | UnicodeString ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E"); | |
836 | UnicodeString ruleset2("< a, A < b, B < c, C < d, D, e, E"); | |
837 | RuleBasedCollator *col1 = new RuleBasedCollator(ruleset1, success); | |
838 | if (U_FAILURE(success)) { | |
839 | errln("RuleBasedCollator creation failed."); | |
840 | return; | |
841 | } | |
842 | success = U_ZERO_ERROR; | |
843 | RuleBasedCollator *col2 = new RuleBasedCollator(ruleset2, success); | |
844 | if (U_FAILURE(success)) { | |
845 | errln("The RuleBasedCollator constructor failed when building with the 2nd rule set."); | |
846 | return; | |
847 | } | |
848 | logln("The operator tests begin : "); | |
849 | logln("testing operator==, operator!=, clone methods ..."); | |
850 | doAssert((*col1 != *col2), "The two different table collations compared equal"); | |
851 | *col1 = *col2; | |
852 | doAssert((*col1 == *col2), "Collator objects not equal after assignment (operator=)"); | |
853 | ||
854 | success = U_ZERO_ERROR; | |
855 | Collator *col3 = Collator::createInstance(Locale::getEnglish(), success); | |
856 | if (U_FAILURE(success)) { | |
857 | errln("Default collation creation failed."); | |
858 | return; | |
859 | } | |
860 | doAssert((*col1 != *col3), "The two different table collations compared equal"); | |
861 | Collator* col4 = col1->clone(); | |
862 | Collator* col5 = col3->clone(); | |
863 | doAssert((*col1 == *col4), "Cloned collation objects not equal"); | |
864 | doAssert((*col3 != *col4), "Two different table collations compared equal"); | |
865 | doAssert((*col3 == *col5), "Cloned collation objects not equal"); | |
866 | doAssert((*col4 != *col5), "Two cloned collations compared equal"); | |
867 | ||
868 | const UnicodeString& defRules = ((RuleBasedCollator*)col3)->getRules(); | |
869 | RuleBasedCollator* col6 = new RuleBasedCollator(defRules, success); | |
870 | if (U_FAILURE(success)) { | |
871 | errln("Creating default collation with rules failed."); | |
872 | return; | |
873 | } | |
874 | doAssert((((RuleBasedCollator*)col3)->getRules() == col6->getRules()), "Default collator getRules failed"); | |
875 | ||
876 | success = U_ZERO_ERROR; | |
877 | RuleBasedCollator *col7 = new RuleBasedCollator(ruleset2, Collator::TERTIARY, success); | |
878 | if (U_FAILURE(success)) { | |
879 | errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength."); | |
880 | return; | |
881 | } | |
882 | success = U_ZERO_ERROR; | |
883 | RuleBasedCollator *col8 = new RuleBasedCollator(ruleset2, UCOL_OFF, success); | |
884 | if (U_FAILURE(success)) { | |
885 | errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with Normalizer::NO_OP."); | |
886 | return; | |
887 | } | |
888 | success = U_ZERO_ERROR; | |
889 | RuleBasedCollator *col9 = new RuleBasedCollator(ruleset2, Collator::PRIMARY, UCOL_ON, success); | |
890 | if (U_FAILURE(success)) { | |
891 | errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength and Normalizer::NO_OP."); | |
892 | return; | |
893 | } | |
894 | // doAssert((*col7 == *col8), "The two equal table collations compared different"); | |
895 | doAssert((*col7 != *col9), "The two different table collations compared equal"); | |
896 | doAssert((*col8 != *col9), "The two different table collations compared equal"); | |
897 | ||
898 | logln("operator tests ended."); | |
899 | delete col1; | |
900 | delete col2; | |
901 | delete col3; | |
902 | delete col4; | |
903 | delete col5; | |
904 | delete col6; | |
905 | delete col7; | |
906 | delete col8; | |
907 | delete col9; | |
908 | } | |
909 | ||
910 | // test clone and copy | |
911 | void | |
912 | CollationAPITest::TestDuplicate(/* char* par */) | |
913 | { | |
914 | UErrorCode status = U_ZERO_ERROR; | |
915 | Collator *col1 = Collator::createInstance(Locale::getEnglish(), status); | |
916 | if (U_FAILURE(status)) { | |
917 | logln("Default collator creation failed."); | |
918 | return; | |
919 | } | |
920 | Collator *col2 = col1->clone(); | |
921 | doAssert((*col1 == *col2), "Cloned object is not equal to the orginal"); | |
73c04bcf A |
922 | UnicodeString *ruleset = new UnicodeString("< a, A < b, B < c, C < d, D, e, E"); |
923 | RuleBasedCollator *col3 = new RuleBasedCollator(*ruleset, status); | |
b75a7d8f A |
924 | doAssert((*col1 != *col3), "Cloned object is equal to some dummy"); |
925 | *col3 = *((RuleBasedCollator*)col1); | |
926 | doAssert((*col1 == *col3), "Copied object is not equal to the orginal"); | |
73c04bcf A |
927 | |
928 | if (U_FAILURE(status)) { | |
929 | logln("Collation tailoring failed."); | |
930 | return; | |
931 | } | |
932 | ||
933 | UCollationResult res; | |
934 | UnicodeString first((UChar)0x0061); | |
935 | UnicodeString second((UChar)0x0062); | |
936 | UnicodeString copiedEnglishRules(((RuleBasedCollator*)col1)->getRules()); | |
937 | ||
b75a7d8f | 938 | delete col1; |
73c04bcf A |
939 | delete ruleset; |
940 | ||
941 | // Try using the cloned collators after deleting the original data | |
942 | res = col2->compare(first, second, status); | |
943 | if(res != UCOL_LESS) { | |
944 | errln("a should be less then b after tailoring"); | |
945 | } | |
946 | if (((RuleBasedCollator*)col2)->getRules() != copiedEnglishRules) { | |
947 | errln(UnicodeString("English rule difference. ") | |
948 | + copiedEnglishRules + UnicodeString("\ngetRules=") + ((RuleBasedCollator*)col2)->getRules()); | |
949 | } | |
950 | res = col3->compare(first, second, status); | |
951 | if(res != UCOL_LESS) { | |
952 | errln("a should be less then b after tailoring"); | |
953 | } | |
954 | if (col3->getRules() != copiedEnglishRules) { | |
955 | errln(UnicodeString("English rule difference. ") | |
956 | + copiedEnglishRules + UnicodeString("\ngetRules=") + col3->getRules()); | |
957 | } | |
958 | ||
b75a7d8f A |
959 | delete col2; |
960 | delete col3; | |
961 | } | |
962 | ||
963 | void | |
964 | CollationAPITest::TestCompare(/* char* par */) | |
965 | { | |
966 | logln("The compare tests begin : "); | |
967 | Collator *col = 0; | |
968 | UErrorCode success = U_ZERO_ERROR; | |
969 | col = Collator::createInstance(Locale::getEnglish(), success); | |
970 | if (U_FAILURE(success)) { | |
971 | errln("Default collation creation failed."); | |
972 | return; | |
973 | } | |
974 | UnicodeString test1("Abcda"), test2("abcda"); | |
975 | logln("Use tertiary comparison level testing ...."); | |
976 | ||
977 | doAssert((!col->equals(test1, test2) ), "Result should be \"Abcda\" != \"abcda\""); | |
978 | doAssert((col->greater(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\""); | |
979 | doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\""); | |
980 | ||
981 | col->setStrength(Collator::SECONDARY); | |
982 | logln("Use secondary comparison level testing ...."); | |
983 | ||
984 | doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\""); | |
985 | doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\""); | |
986 | doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\""); | |
987 | ||
988 | col->setStrength(Collator::PRIMARY); | |
989 | logln("Use primary comparison level testing ...."); | |
990 | ||
991 | doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\""); | |
992 | doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\""); | |
993 | doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\""); | |
994 | ||
995 | // Test different APIs | |
996 | const UChar* t1 = test1.getBuffer(); | |
997 | int32_t t1Len = test1.length(); | |
998 | const UChar* t2 = test2.getBuffer(); | |
999 | int32_t t2Len = test2.length(); | |
1000 | ||
1001 | doAssert((col->compare(test1, test2) == Collator::EQUAL), "Problem"); | |
1002 | doAssert((col->compare(test1, test2, success) == UCOL_EQUAL), "Problem"); | |
1003 | doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::EQUAL), "Problem"); | |
1004 | doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_EQUAL), "Problem"); | |
1005 | doAssert((col->compare(test1, test2, t1Len) == Collator::EQUAL), "Problem"); | |
1006 | doAssert((col->compare(test1, test2, t1Len, success) == UCOL_EQUAL), "Problem"); | |
1007 | ||
1008 | col->setAttribute(UCOL_STRENGTH, UCOL_TERTIARY, success); | |
1009 | doAssert((col->compare(test1, test2) == Collator::GREATER), "Problem"); | |
1010 | doAssert((col->compare(test1, test2, success) == UCOL_GREATER), "Problem"); | |
1011 | doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::GREATER), "Problem"); | |
1012 | doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_GREATER), "Problem"); | |
1013 | doAssert((col->compare(test1, test2, t1Len) == Collator::GREATER), "Problem"); | |
1014 | doAssert((col->compare(test1, test2, t1Len, success) == UCOL_GREATER), "Problem"); | |
1015 | ||
1016 | ||
1017 | ||
1018 | logln("The compare tests end."); | |
1019 | delete col; | |
1020 | } | |
1021 | ||
1022 | void | |
1023 | CollationAPITest::TestGetAll(/* char* par */) | |
1024 | { | |
46f4442e A |
1025 | int32_t count1, count2; |
1026 | UErrorCode status = U_ZERO_ERROR; | |
1027 | ||
1028 | logln("Trying Collator::getAvailableLocales(int&)"); | |
1029 | ||
1030 | const Locale* list = Collator::getAvailableLocales(count1); | |
1031 | for (int32_t i = 0; i < count1; ++i) { | |
1032 | UnicodeString dispName; | |
1033 | logln(UnicodeString("Locale name: ") | |
1034 | + UnicodeString(list[i].getName()) | |
1035 | + UnicodeString(" , the display name is : ") | |
1036 | + UnicodeString(list[i].getDisplayName(dispName))); | |
1037 | } | |
1038 | ||
1039 | if (count1 == 0 || list == NULL) { | |
1040 | errln("getAvailableLocales(int&) returned an empty list"); | |
1041 | } | |
1042 | ||
1043 | logln("Trying Collator::getAvailableLocales()"); | |
1044 | StringEnumeration* localeEnum = Collator::getAvailableLocales(); | |
1045 | const UnicodeString* locStr; | |
1046 | const char *locCStr; | |
1047 | count2 = 0; | |
1048 | ||
1049 | if (localeEnum == NULL) { | |
1050 | errln("getAvailableLocales() returned NULL"); | |
1051 | return; | |
1052 | } | |
1053 | ||
1054 | while ((locStr = localeEnum->snext(status)) != NULL) | |
1055 | { | |
1056 | logln(UnicodeString("Locale name is: ") + *locStr); | |
1057 | count2++; | |
1058 | } | |
1059 | if (count1 != count2) { | |
1060 | errln("getAvailableLocales(int&) returned %d and getAvailableLocales() returned %d", count1, count2); | |
1061 | } | |
1062 | ||
1063 | logln("Trying Collator::getAvailableLocales() clone"); | |
1064 | count1 = 0; | |
1065 | StringEnumeration* localeEnum2 = localeEnum->clone(); | |
1066 | localeEnum2->reset(status); | |
1067 | while ((locCStr = localeEnum2->next(NULL, status)) != NULL) | |
1068 | { | |
1069 | logln(UnicodeString("Locale name is: ") + UnicodeString(locCStr)); | |
1070 | count1++; | |
1071 | } | |
1072 | if (count1 != count2) { | |
1073 | errln("getAvailableLocales(3rd time) returned %d and getAvailableLocales(2nd time) returned %d", count1, count2); | |
1074 | } | |
1075 | if (localeEnum->count(status) != count1) { | |
1076 | errln("localeEnum->count() returned %d and getAvailableLocales() returned %d", localeEnum->count(status), count1); | |
b75a7d8f | 1077 | } |
46f4442e A |
1078 | delete localeEnum; |
1079 | delete localeEnum2; | |
b75a7d8f A |
1080 | } |
1081 | ||
1082 | void CollationAPITest::TestSortKey() | |
1083 | { | |
1084 | UErrorCode status = U_ZERO_ERROR; | |
1085 | /* | |
1086 | this is supposed to open default date format, but later on it treats | |
1087 | it like it is "en_US" | |
1088 | - very bad if you try to run the tests on machine where default | |
1089 | locale is NOT "en_US" | |
1090 | */ | |
1091 | Collator *col = Collator::createInstance(Locale::getEnglish(), status); | |
1092 | if (U_FAILURE(status)) { | |
1093 | errln("ERROR: Default collation creation failed.: %s\n", u_errorName(status)); | |
1094 | return; | |
1095 | } | |
1096 | ||
1097 | if (col->getStrength() != Collator::TERTIARY) | |
1098 | { | |
1099 | errln("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n"); | |
1100 | } | |
1101 | ||
1102 | /* Need to use identical strength */ | |
1103 | col->setAttribute(UCOL_STRENGTH, UCOL_IDENTICAL, status); | |
1104 | ||
1105 | uint8_t key2compat[] = { | |
46f4442e A |
1106 | /* 3.9 key, from UCA 5.1 */ |
1107 | 0x2c, 0x2e, 0x30, 0x32, 0x2c, 0x01, | |
1108 | 0x09, 0x01, 0x09, 0x01, 0x2b, 0x01, | |
1109 | 0x92, 0x93, 0x94, 0x95, 0x92, 0x0 | |
1110 | ||
73c04bcf | 1111 | /* 3.6 key, from UCA 5.0 */ |
46f4442e | 1112 | /* |
73c04bcf A |
1113 | 0x29, 0x2b, 0x2d, 0x2f, 0x29, 0x01, |
1114 | 0x09, 0x01, 0x09, 0x01, 0x28, 0x01, | |
1115 | 0x92, 0x93, 0x94, 0x95, 0x92, 0x00 | |
46f4442e | 1116 | */ |
73c04bcf A |
1117 | /* 3.4 key, from UCA 4.1 */ |
1118 | /* | |
1119 | 0x28, 0x2a, 0x2c, 0x2e, 0x28, 0x01, | |
1120 | 0x09, 0x01, 0x09, 0x01, 0x27, 0x01, | |
1121 | 0x92, 0x93, 0x94, 0x95, 0x92, 0x00 | |
1122 | */ | |
1123 | /* 2.6.1 key */ | |
1124 | /* | |
374ca955 A |
1125 | 0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01, |
1126 | 0x09, 0x01, 0x09, 0x01, 0x25, 0x01, | |
1127 | 0x92, 0x93, 0x94, 0x95, 0x92, 0x00 | |
73c04bcf | 1128 | */ |
b75a7d8f | 1129 | /* 2.2 key */ |
374ca955 | 1130 | /* |
b75a7d8f A |
1131 | 0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01, |
1132 | 0x09, 0x01, 0x09, 0x01, 0x1C, 0x01, | |
1133 | 0x92, 0x93, 0x94, 0x95, 0x92, 0x00 | |
374ca955 | 1134 | */ |
b75a7d8f A |
1135 | /* 2.0 key */ |
1136 | /* | |
1137 | 0x19, 0x1B, 0x1D, 0x1F, 0x19, | |
1138 | 0x01, 0x09, 0x01, 0x09, 0x01, | |
1139 | 0x18, 0x01, | |
1140 | 0x92, 0x93, 0x94, 0x95, 0x92, | |
1141 | 0x00 | |
1142 | */ | |
1143 | /* 1.8.1 key.*/ | |
1144 | /* | |
1145 | 0x19, 0x1B, 0x1D, 0x1F, 0x19, | |
1146 | 0x01, 0x0A, 0x01, 0x0A, 0x01, | |
1147 | 0x92, 0x93, 0x94, 0x95, 0x92, | |
1148 | 0x00 | |
1149 | */ | |
1150 | }; | |
1151 | ||
1152 | UChar test1[6] = {0x41, 0x62, 0x63, 0x64, 0x61, 0}, | |
1153 | test2[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0}, | |
1154 | test3[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0}; | |
1155 | ||
1156 | uint8_t sortkey1[64]; | |
1157 | uint8_t sortkey2[64]; | |
1158 | uint8_t sortkey3[64]; | |
1159 | ||
1160 | logln("Use tertiary comparison level testing ....\n"); | |
1161 | ||
1162 | CollationKey key1; | |
1163 | col->getCollationKey(test1, u_strlen(test1), key1, status); | |
1164 | ||
1165 | CollationKey key2; | |
1166 | col->getCollationKey(test2, u_strlen(test2), key2, status); | |
1167 | ||
1168 | CollationKey key3; | |
1169 | col->getCollationKey(test3, u_strlen(test3), key3, status); | |
1170 | ||
1171 | doAssert(key1.compareTo(key2) == Collator::GREATER, | |
1172 | "Result should be \"Abcda\" > \"abcda\""); | |
1173 | doAssert(key2.compareTo(key1) == Collator::LESS, | |
1174 | "Result should be \"abcda\" < \"Abcda\""); | |
1175 | doAssert(key2.compareTo(key3) == Collator::EQUAL, | |
1176 | "Result should be \"abcda\" == \"abcda\""); | |
1177 | ||
1178 | int32_t keylength = 0; | |
1179 | doAssert(strcmp((const char *)(key2.getByteArray(keylength)), | |
1180 | (const char *)key2compat) == 0, | |
1181 | "Binary format for 'abcda' sortkey different!"); | |
1182 | ||
1183 | col->getSortKey(test1, sortkey1, 64); | |
1184 | col->getSortKey(test2, sortkey2, 64); | |
1185 | col->getSortKey(test3, sortkey3, 64); | |
1186 | ||
1187 | const uint8_t *tempkey = key1.getByteArray(keylength); | |
1188 | doAssert(memcmp(tempkey, sortkey1, keylength) == 0, | |
1189 | "Test1 string should have the same collation key and sort key"); | |
1190 | tempkey = key2.getByteArray(keylength); | |
1191 | doAssert(memcmp(tempkey, sortkey2, keylength) == 0, | |
1192 | "Test2 string should have the same collation key and sort key"); | |
1193 | tempkey = key3.getByteArray(keylength); | |
1194 | doAssert(memcmp(tempkey, sortkey3, keylength) == 0, | |
1195 | "Test3 string should have the same collation key and sort key"); | |
1196 | ||
1197 | col->getSortKey(test1, 5, sortkey1, 64); | |
1198 | col->getSortKey(test2, 5, sortkey2, 64); | |
1199 | col->getSortKey(test3, 5, sortkey3, 64); | |
1200 | ||
1201 | tempkey = key1.getByteArray(keylength); | |
1202 | doAssert(memcmp(tempkey, sortkey1, keylength) == 0, | |
1203 | "Test1 string should have the same collation key and sort key"); | |
1204 | tempkey = key2.getByteArray(keylength); | |
1205 | doAssert(memcmp(tempkey, sortkey2, keylength) == 0, | |
1206 | "Test2 string should have the same collation key and sort key"); | |
1207 | tempkey = key3.getByteArray(keylength); | |
1208 | doAssert(memcmp(tempkey, sortkey3, keylength) == 0, | |
1209 | "Test3 string should have the same collation key and sort key"); | |
1210 | ||
1211 | UnicodeString strtest1(test1); | |
1212 | col->getSortKey(strtest1, sortkey1, 64); | |
1213 | UnicodeString strtest2(test2); | |
1214 | col->getSortKey(strtest2, sortkey2, 64); | |
1215 | UnicodeString strtest3(test3); | |
1216 | col->getSortKey(strtest3, sortkey3, 64); | |
1217 | ||
1218 | tempkey = key1.getByteArray(keylength); | |
1219 | doAssert(memcmp(tempkey, sortkey1, keylength) == 0, | |
1220 | "Test1 string should have the same collation key and sort key"); | |
1221 | tempkey = key2.getByteArray(keylength); | |
1222 | doAssert(memcmp(tempkey, sortkey2, keylength) == 0, | |
1223 | "Test2 string should have the same collation key and sort key"); | |
1224 | tempkey = key3.getByteArray(keylength); | |
1225 | doAssert(memcmp(tempkey, sortkey3, keylength) == 0, | |
1226 | "Test3 string should have the same collation key and sort key"); | |
1227 | ||
1228 | logln("Use secondary comparision level testing ...\n"); | |
1229 | col->setStrength(Collator::SECONDARY); | |
1230 | ||
1231 | col->getCollationKey(test1, u_strlen(test1), key1, status); | |
1232 | col->getCollationKey(test2, u_strlen(test2), key2, status); | |
1233 | col->getCollationKey(test3, u_strlen(test3), key3, status); | |
1234 | ||
1235 | doAssert(key1.compareTo(key2) == Collator::EQUAL, | |
1236 | "Result should be \"Abcda\" == \"abcda\""); | |
1237 | doAssert(key2.compareTo(key3) == Collator::EQUAL, | |
1238 | "Result should be \"abcda\" == \"abcda\""); | |
1239 | ||
1240 | tempkey = key2.getByteArray(keylength); | |
1241 | doAssert(memcmp(tempkey, key2compat, keylength - 1) == 0, | |
1242 | "Binary format for 'abcda' sortkey different!"); | |
1243 | ||
1244 | col->getSortKey(test1, sortkey1, 64); | |
1245 | col->getSortKey(test2, sortkey2, 64); | |
1246 | col->getSortKey(test3, sortkey3, 64); | |
1247 | ||
1248 | tempkey = key1.getByteArray(keylength); | |
1249 | doAssert(memcmp(tempkey, sortkey1, keylength) == 0, | |
1250 | "Test1 string should have the same collation key and sort key"); | |
1251 | tempkey = key2.getByteArray(keylength); | |
1252 | doAssert(memcmp(tempkey, sortkey2, keylength) == 0, | |
1253 | "Test2 string should have the same collation key and sort key"); | |
1254 | tempkey = key3.getByteArray(keylength); | |
1255 | doAssert(memcmp(tempkey, sortkey3, keylength) == 0, | |
1256 | "Test3 string should have the same collation key and sort key"); | |
1257 | ||
1258 | col->getSortKey(test1, 5, sortkey1, 64); | |
1259 | col->getSortKey(test2, 5, sortkey2, 64); | |
1260 | col->getSortKey(test3, 5, sortkey3, 64); | |
1261 | ||
1262 | tempkey = key1.getByteArray(keylength); | |
1263 | doAssert(memcmp(tempkey, sortkey1, keylength) == 0, | |
1264 | "Test1 string should have the same collation key and sort key"); | |
1265 | tempkey = key2.getByteArray(keylength); | |
1266 | doAssert(memcmp(tempkey, sortkey2, keylength) == 0, | |
1267 | "Test2 string should have the same collation key and sort key"); | |
1268 | tempkey = key3.getByteArray(keylength); | |
1269 | doAssert(memcmp(tempkey, sortkey3, keylength) == 0, | |
1270 | "Test3 string should have the same collation key and sort key"); | |
1271 | ||
1272 | col->getSortKey(strtest1, sortkey1, 64); | |
1273 | col->getSortKey(strtest2, sortkey2, 64); | |
1274 | col->getSortKey(strtest3, sortkey3, 64); | |
1275 | ||
1276 | tempkey = key1.getByteArray(keylength); | |
1277 | doAssert(memcmp(tempkey, sortkey1, keylength) == 0, | |
1278 | "Test1 string should have the same collation key and sort key"); | |
1279 | tempkey = key2.getByteArray(keylength); | |
1280 | doAssert(memcmp(tempkey, sortkey2, keylength) == 0, | |
1281 | "Test2 string should have the same collation key and sort key"); | |
1282 | tempkey = key3.getByteArray(keylength); | |
1283 | doAssert(memcmp(tempkey, sortkey3, keylength) == 0, | |
1284 | "Test3 string should have the same collation key and sort key"); | |
1285 | ||
1286 | logln("testing sortkey ends..."); | |
1287 | delete col; | |
1288 | } | |
1289 | ||
1290 | void CollationAPITest::TestMaxExpansion() | |
1291 | { | |
1292 | UErrorCode status = U_ZERO_ERROR; | |
1293 | UChar ch = 0; | |
374ca955 | 1294 | UChar32 unassigned = 0xEFFFD; |
b75a7d8f A |
1295 | uint32_t sorder = 0; |
1296 | uint32_t temporder = 0; | |
1297 | ||
1298 | UnicodeString rule("&a < ab < c/aba < d < z < ch"); | |
1299 | RuleBasedCollator coll(rule, status); | |
1300 | if(U_FAILURE(status)) { | |
1301 | errln("Collator creation failed with error %s", u_errorName(status)); | |
1302 | return; | |
1303 | } | |
1304 | UnicodeString str(ch); | |
1305 | CollationElementIterator *iter = | |
1306 | coll.createCollationElementIterator(str); | |
1307 | ||
1308 | while (ch < 0xFFFF && U_SUCCESS(status)) { | |
1309 | int count = 1; | |
1310 | uint32_t order; | |
1311 | int32_t size = 0; | |
1312 | ||
1313 | ch ++; | |
1314 | ||
1315 | str.setCharAt(0, ch); | |
1316 | iter->setText(str, status); | |
1317 | order = iter->previous(status); | |
1318 | ||
1319 | /* thai management */ | |
1320 | if (order == 0) | |
1321 | order = iter->previous(status); | |
1322 | ||
374ca955 | 1323 | while (U_SUCCESS(status) && iter->previous(status) != UCOL_NULLORDER) { |
b75a7d8f A |
1324 | count ++; |
1325 | } | |
1326 | ||
1327 | size = coll.getMaxExpansion(order); | |
1328 | if (U_FAILURE(status) || size < count) { | |
1329 | errln("Failure at codepoint %d, maximum expansion count < %d\n", | |
1330 | ch, count); | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | /* testing for exact max expansion */ | |
1335 | ch = 0; | |
1336 | while (ch < 0x61) { | |
1337 | uint32_t order; | |
1338 | int32_t size; | |
1339 | str.setCharAt(0, ch); | |
1340 | iter->setText(str, status); | |
1341 | order = iter->previous(status); | |
1342 | size = coll.getMaxExpansion(order); | |
1343 | if (U_FAILURE(status) || size != 1) { | |
1344 | errln("Failure at codepoint %d, maximum expansion count < %d\n", | |
1345 | ch, 1); | |
1346 | } | |
1347 | ch ++; | |
1348 | } | |
1349 | ||
1350 | ch = 0x63; | |
1351 | str.setTo(ch); | |
1352 | iter->setText(str, status); | |
1353 | temporder = iter->previous(status); | |
1354 | ||
1355 | if (U_FAILURE(status) || coll.getMaxExpansion(temporder) != 3) { | |
1356 | errln("Failure at codepoint %d, maximum expansion count != %d\n", | |
1357 | ch, 3); | |
1358 | } | |
1359 | ||
1360 | ch = 0x64; | |
1361 | str.setTo(ch); | |
1362 | iter->setText(str, status); | |
1363 | temporder = iter->previous(status); | |
1364 | ||
1365 | if (U_FAILURE(status) || coll.getMaxExpansion(temporder) != 1) { | |
1366 | errln("Failure at codepoint %d, maximum expansion count != %d\n", | |
1367 | ch, 3); | |
1368 | } | |
1369 | ||
374ca955 | 1370 | str.setTo(unassigned); |
b75a7d8f A |
1371 | iter->setText(str, status); |
1372 | sorder = iter->previous(status); | |
1373 | ||
1374 | if (U_FAILURE(status) || coll.getMaxExpansion(sorder) != 2) { | |
1375 | errln("Failure at supplementary codepoints, maximum expansion count < %d\n", | |
1376 | 2); | |
1377 | } | |
1378 | ||
1379 | /* testing jamo */ | |
1380 | ch = 0x1165; | |
1381 | str.setTo(ch); | |
1382 | iter->setText(str, status); | |
1383 | temporder = iter->previous(status); | |
1384 | if (U_FAILURE(status) || coll.getMaxExpansion(temporder) > 3) { | |
1385 | errln("Failure at codepoint %d, maximum expansion count > %d\n", | |
1386 | ch, 3); | |
1387 | } | |
1388 | ||
1389 | delete iter; | |
1390 | ||
1391 | /* testing special jamo &a<\u1160 */ | |
1392 | rule = CharsToUnicodeString("\\u0026\\u0071\\u003c\\u1165\\u002f\\u0071\\u0071\\u0071\\u0071"); | |
1393 | ||
1394 | RuleBasedCollator jamocoll(rule, status); | |
1395 | iter = jamocoll.createCollationElementIterator(str); | |
1396 | temporder = iter->previous(status); | |
1397 | if (U_FAILURE(status) || iter->getMaxExpansion(temporder) != 6) { | |
1398 | errln("Failure at codepoint %d, maximum expansion count > %d\n", | |
1399 | ch, 5); | |
1400 | } | |
1401 | ||
1402 | delete iter; | |
1403 | } | |
1404 | ||
1405 | void CollationAPITest::TestDisplayName() | |
1406 | { | |
1407 | UErrorCode error = U_ZERO_ERROR; | |
1408 | Collator *coll = Collator::createInstance("en_US", error); | |
1409 | if (U_FAILURE(error)) { | |
1410 | errln("Failure creating english collator"); | |
1411 | return; | |
1412 | } | |
1413 | UnicodeString name; | |
1414 | UnicodeString result; | |
1415 | coll->getDisplayName(Locale::getCanadaFrench(), result); | |
1416 | Locale::getCanadaFrench().getDisplayName(name); | |
1417 | if (result.compare(name)) { | |
1418 | errln("Failure getting the correct name for locale en_US"); | |
1419 | } | |
1420 | ||
1421 | coll->getDisplayName(Locale::getSimplifiedChinese(), result); | |
1422 | Locale::getSimplifiedChinese().getDisplayName(name); | |
1423 | if (result.compare(name)) { | |
1424 | errln("Failure getting the correct name for locale zh_SG"); | |
1425 | } | |
1426 | delete coll; | |
1427 | } | |
1428 | ||
1429 | void CollationAPITest::TestAttribute() | |
1430 | { | |
1431 | UErrorCode error = U_ZERO_ERROR; | |
1432 | Collator *coll = Collator::createInstance(error); | |
1433 | ||
1434 | if (U_FAILURE(error)) { | |
1435 | errln("Creation of default collator failed"); | |
1436 | return; | |
1437 | } | |
1438 | ||
1439 | coll->setAttribute(UCOL_FRENCH_COLLATION, UCOL_OFF, error); | |
1440 | if (coll->getAttribute(UCOL_FRENCH_COLLATION, error) != UCOL_OFF || | |
1441 | U_FAILURE(error)) { | |
1442 | errln("Setting and retrieving of the french collation failed"); | |
1443 | } | |
1444 | ||
1445 | coll->setAttribute(UCOL_FRENCH_COLLATION, UCOL_ON, error); | |
1446 | if (coll->getAttribute(UCOL_FRENCH_COLLATION, error) != UCOL_ON || | |
1447 | U_FAILURE(error)) { | |
1448 | errln("Setting and retrieving of the french collation failed"); | |
1449 | } | |
1450 | ||
1451 | coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, error); | |
1452 | if (coll->getAttribute(UCOL_ALTERNATE_HANDLING, error) != UCOL_SHIFTED || | |
1453 | U_FAILURE(error)) { | |
1454 | errln("Setting and retrieving of the alternate handling failed"); | |
1455 | } | |
1456 | ||
1457 | coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, error); | |
1458 | if (coll->getAttribute(UCOL_ALTERNATE_HANDLING, error) != UCOL_NON_IGNORABLE || | |
1459 | U_FAILURE(error)) { | |
1460 | errln("Setting and retrieving of the alternate handling failed"); | |
1461 | } | |
1462 | ||
1463 | coll->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, error); | |
1464 | if (coll->getAttribute(UCOL_CASE_FIRST, error) != UCOL_LOWER_FIRST || | |
1465 | U_FAILURE(error)) { | |
1466 | errln("Setting and retrieving of the case first attribute failed"); | |
1467 | } | |
1468 | ||
1469 | coll->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, error); | |
1470 | if (coll->getAttribute(UCOL_CASE_FIRST, error) != UCOL_UPPER_FIRST || | |
1471 | U_FAILURE(error)) { | |
1472 | errln("Setting and retrieving of the case first attribute failed"); | |
1473 | } | |
1474 | ||
1475 | coll->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, error); | |
1476 | if (coll->getAttribute(UCOL_CASE_LEVEL, error) != UCOL_ON || | |
1477 | U_FAILURE(error)) { | |
1478 | errln("Setting and retrieving of the case level attribute failed"); | |
1479 | } | |
1480 | ||
1481 | coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, error); | |
1482 | if (coll->getAttribute(UCOL_CASE_LEVEL, error) != UCOL_OFF || | |
1483 | U_FAILURE(error)) { | |
1484 | errln("Setting and retrieving of the case level attribute failed"); | |
1485 | } | |
1486 | ||
1487 | coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, error); | |
1488 | if (coll->getAttribute(UCOL_NORMALIZATION_MODE, error) != UCOL_ON || | |
1489 | U_FAILURE(error)) { | |
1490 | errln("Setting and retrieving of the normalization on/off attribute failed"); | |
1491 | } | |
1492 | ||
1493 | coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_OFF, error); | |
1494 | if (coll->getAttribute(UCOL_NORMALIZATION_MODE, error) != UCOL_OFF || | |
1495 | U_FAILURE(error)) { | |
1496 | errln("Setting and retrieving of the normalization on/off attribute failed"); | |
1497 | } | |
1498 | ||
1499 | coll->setAttribute(UCOL_STRENGTH, UCOL_PRIMARY, error); | |
1500 | if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_PRIMARY || | |
1501 | U_FAILURE(error)) { | |
1502 | errln("Setting and retrieving of the collation strength failed"); | |
1503 | } | |
1504 | ||
1505 | coll->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, error); | |
1506 | if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_SECONDARY || | |
1507 | U_FAILURE(error)) { | |
1508 | errln("Setting and retrieving of the collation strength failed"); | |
1509 | } | |
1510 | ||
1511 | coll->setAttribute(UCOL_STRENGTH, UCOL_TERTIARY, error); | |
1512 | if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_TERTIARY || | |
1513 | U_FAILURE(error)) { | |
1514 | errln("Setting and retrieving of the collation strength failed"); | |
1515 | } | |
1516 | ||
1517 | coll->setAttribute(UCOL_STRENGTH, UCOL_QUATERNARY, error); | |
1518 | if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_QUATERNARY || | |
1519 | U_FAILURE(error)) { | |
1520 | errln("Setting and retrieving of the collation strength failed"); | |
1521 | } | |
1522 | ||
1523 | coll->setAttribute(UCOL_STRENGTH, UCOL_IDENTICAL, error); | |
1524 | if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_IDENTICAL || | |
1525 | U_FAILURE(error)) { | |
1526 | errln("Setting and retrieving of the collation strength failed"); | |
1527 | } | |
1528 | ||
1529 | delete coll; | |
1530 | } | |
1531 | ||
1532 | void CollationAPITest::TestVariableTopSetting() { | |
1533 | UErrorCode status = U_ZERO_ERROR; | |
1534 | ||
1535 | UChar vt[256] = { 0 }; | |
1536 | ||
1537 | Collator *coll = Collator::createInstance(status); | |
1538 | if(U_FAILURE(status)) { | |
1539 | delete coll; | |
1540 | errln("Collator creation failed with error %s", u_errorName(status)); | |
1541 | return; | |
1542 | } | |
1543 | ||
1544 | uint32_t oldVarTop = coll->getVariableTop(status); | |
1545 | ||
1546 | vt[0] = 0x0041; | |
1547 | ||
1548 | uint32_t newVarTop = coll->setVariableTop(vt, 1, status); | |
1549 | ||
1550 | if((newVarTop & 0xFFFF0000) != (coll->getVariableTop(status) & 0xFFFF0000)) { | |
1551 | errln("Didn't set vartop properly\n"); | |
1552 | } | |
1553 | ||
1554 | coll->setVariableTop(oldVarTop, status); | |
1555 | ||
1556 | uint32_t newerVarTop = coll->setVariableTop(UnicodeString(vt, 1), status); | |
1557 | ||
1558 | if((newVarTop & 0xFFFF0000) != (newerVarTop & 0xFFFF0000)) { | |
1559 | errln("Didn't set vartop properly from UnicodeString!\n"); | |
1560 | } | |
1561 | ||
1562 | delete coll; | |
1563 | ||
1564 | } | |
1565 | ||
1566 | void CollationAPITest::TestGetLocale() { | |
1567 | UErrorCode status = U_ZERO_ERROR; | |
1568 | const char *rules = "&a<x<y<z"; | |
1569 | UChar rlz[256] = {0}; | |
1570 | ||
1571 | Collator *coll = NULL; | |
1572 | Locale locale; | |
1573 | ||
1574 | int32_t i = 0; | |
1575 | ||
1576 | static const struct { | |
1577 | const char* requestedLocale; | |
1578 | const char* validLocale; | |
1579 | const char* actualLocale; | |
1580 | } testStruct[] = { | |
1581 | { "sr_YU", "sr_YU", "root" }, | |
1582 | { "sh_YU", "sh_YU", "sh" }, | |
1583 | { "en_US_CALIFORNIA", "en_US", "root" }, | |
1584 | { "fr_FR_NONEXISTANT", "fr_FR", "fr" } | |
1585 | }; | |
1586 | ||
1587 | u_unescape(rules, rlz, 256); | |
1588 | ||
1589 | /* test opening collators for different locales */ | |
1590 | for(i = 0; i<(int32_t)(sizeof(testStruct)/sizeof(testStruct[0])); i++) { | |
1591 | status = U_ZERO_ERROR; | |
1592 | coll = Collator::createInstance(testStruct[i].requestedLocale, status); | |
1593 | if(U_FAILURE(status)) { | |
1594 | log("Failed to open collator for %s with %s\n", testStruct[i].requestedLocale, u_errorName(status)); | |
1595 | delete coll; | |
1596 | continue; | |
1597 | } | |
1598 | locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status); | |
1599 | if(locale != testStruct[i].requestedLocale) { | |
1600 | log("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].requestedLocale, locale.getName()); | |
1601 | } | |
1602 | locale = coll->getLocale(ULOC_VALID_LOCALE, status); | |
1603 | if(locale != testStruct[i].validLocale) { | |
1604 | log("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].validLocale, locale.getName()); | |
1605 | } | |
1606 | locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status); | |
1607 | if(locale != testStruct[i].actualLocale) { | |
1608 | log("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].actualLocale, locale.getName()); | |
1609 | } | |
1610 | delete coll; | |
1611 | } | |
1612 | ||
1613 | /* completely non-existant locale for collator should get a default collator */ | |
1614 | { | |
374ca955 | 1615 | Collator *defaultColl = Collator::createInstance((const Locale)NULL, status); |
b75a7d8f A |
1616 | coll = Collator::createInstance("blahaha", status); |
1617 | if(U_FAILURE(status)) { | |
1618 | log("Failed to open collator with %s\n", u_errorName(status)); | |
1619 | delete coll; | |
1620 | delete defaultColl; | |
1621 | return; | |
1622 | } | |
1623 | if(coll->getLocale(ULOC_REQUESTED_LOCALE, status) != "blahaha") { | |
1624 | log("Nonexisting locale didn't preserve the requested locale\n"); | |
1625 | } | |
1626 | if(coll->getLocale(ULOC_VALID_LOCALE, status) != | |
1627 | defaultColl->getLocale(ULOC_VALID_LOCALE, status)) { | |
1628 | log("Valid locale for nonexisting locale locale collator differs " | |
1629 | "from valid locale for default collator\n"); | |
1630 | } | |
1631 | if(coll->getLocale(ULOC_ACTUAL_LOCALE, status) != | |
1632 | defaultColl->getLocale(ULOC_ACTUAL_LOCALE, status)) { | |
1633 | log("Actual locale for nonexisting locale locale collator differs " | |
1634 | "from actual locale for default collator\n"); | |
1635 | } | |
1636 | delete coll; | |
1637 | delete defaultColl; | |
1638 | } | |
1639 | ||
1640 | ||
1641 | ||
1642 | /* collator instantiated from rules should have all three locales NULL */ | |
1643 | coll = new RuleBasedCollator(rlz, status); | |
1644 | locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status); | |
1645 | if(!locale.isBogus()) { | |
1646 | log("For collator instantiated from rules, requested locale %s is not bogus\n", locale.getName()); | |
1647 | } | |
1648 | locale = coll->getLocale(ULOC_VALID_LOCALE, status); | |
1649 | if(!locale.isBogus()) { | |
1650 | log("For collator instantiated from rules, valid locale %s is not bogus\n", locale.getName()); | |
1651 | } | |
1652 | locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status); | |
1653 | if(!locale.isBogus()) { | |
1654 | log("For collator instantiated from rules, actual locale %s is not bogus\n", locale.getName()); | |
1655 | } | |
1656 | delete coll; | |
1657 | } | |
1658 | ||
1659 | struct teststruct { | |
1660 | const char *original; | |
1661 | uint8_t key[256]; | |
1662 | }; | |
1663 | ||
1664 | ||
1665 | ||
1666 | U_CDECL_BEGIN | |
1667 | static int U_CALLCONV | |
1668 | compare_teststruct(const void *string1, const void *string2) { | |
1669 | return(strcmp((const char *)((struct teststruct *)string1)->key, (const char *)((struct teststruct *)string2)->key)); | |
1670 | } | |
1671 | U_CDECL_END | |
1672 | ||
1673 | ||
1674 | void CollationAPITest::TestBounds(void) { | |
1675 | UErrorCode status = U_ZERO_ERROR; | |
1676 | ||
1677 | Collator *coll = Collator::createInstance(Locale("sh"), status); | |
1678 | if(U_FAILURE(status)) { | |
1679 | delete coll; | |
1680 | errln("Collator creation failed with %s", u_errorName(status)); | |
1681 | return; | |
1682 | } | |
1683 | ||
1684 | uint8_t sortkey[512], lower[512], upper[512]; | |
1685 | UChar buffer[512]; | |
1686 | ||
46f4442e | 1687 | static const char * const test[] = { |
b75a7d8f A |
1688 | "John Smith", |
1689 | "JOHN SMITH", | |
1690 | "john SMITH", | |
1691 | "j\\u00F6hn sm\\u00EFth", | |
1692 | "J\\u00F6hn Sm\\u00EFth", | |
1693 | "J\\u00D6HN SM\\u00CFTH", | |
1694 | "john smithsonian", | |
1695 | "John Smithsonian" | |
1696 | }; | |
1697 | ||
46f4442e | 1698 | struct teststruct tests[] = { |
b75a7d8f A |
1699 | {"\\u010CAKI MIHALJ", {0}}, |
1700 | {"\\u010CAKI MIHALJ", {0}}, | |
1701 | {"\\u010CAKI PIRO\\u0160KA", {0}}, | |
1702 | {"\\u010CABAI ANDRIJA", {0}}, | |
1703 | {"\\u010CABAI LAJO\\u0160", {0}}, | |
1704 | {"\\u010CABAI MARIJA", {0}}, | |
1705 | {"\\u010CABAI STEVAN", {0}}, | |
1706 | {"\\u010CABAI STEVAN", {0}}, | |
1707 | {"\\u010CABARKAPA BRANKO", {0}}, | |
1708 | {"\\u010CABARKAPA MILENKO", {0}}, | |
1709 | {"\\u010CABARKAPA MIROSLAV", {0}}, | |
1710 | {"\\u010CABARKAPA SIMO", {0}}, | |
1711 | {"\\u010CABARKAPA STANKO", {0}}, | |
1712 | {"\\u010CABARKAPA TAMARA", {0}}, | |
1713 | {"\\u010CABARKAPA TOMA\\u0160", {0}}, | |
1714 | {"\\u010CABDARI\\u0106 NIKOLA", {0}}, | |
1715 | {"\\u010CABDARI\\u0106 ZORICA", {0}}, | |
1716 | {"\\u010CABI NANDOR", {0}}, | |
1717 | {"\\u010CABOVI\\u0106 MILAN", {0}}, | |
1718 | {"\\u010CABRADI AGNEZIJA", {0}}, | |
1719 | {"\\u010CABRADI IVAN", {0}}, | |
1720 | {"\\u010CABRADI JELENA", {0}}, | |
1721 | {"\\u010CABRADI LJUBICA", {0}}, | |
1722 | {"\\u010CABRADI STEVAN", {0}}, | |
1723 | {"\\u010CABRDA MARTIN", {0}}, | |
1724 | {"\\u010CABRILO BOGDAN", {0}}, | |
1725 | {"\\u010CABRILO BRANISLAV", {0}}, | |
1726 | {"\\u010CABRILO LAZAR", {0}}, | |
1727 | {"\\u010CABRILO LJUBICA", {0}}, | |
1728 | {"\\u010CABRILO SPASOJA", {0}}, | |
1729 | {"\\u010CADE\\u0160 ZDENKA", {0}}, | |
1730 | {"\\u010CADESKI BLAGOJE", {0}}, | |
1731 | {"\\u010CADOVSKI VLADIMIR", {0}}, | |
1732 | {"\\u010CAGLJEVI\\u0106 TOMA", {0}}, | |
1733 | {"\\u010CAGOROVI\\u0106 VLADIMIR", {0}}, | |
1734 | {"\\u010CAJA VANKA", {0}}, | |
1735 | {"\\u010CAJI\\u0106 BOGOLJUB", {0}}, | |
1736 | {"\\u010CAJI\\u0106 BORISLAV", {0}}, | |
1737 | {"\\u010CAJI\\u0106 RADOSLAV", {0}}, | |
1738 | {"\\u010CAK\\u0160IRAN MILADIN", {0}}, | |
1739 | {"\\u010CAKAN EUGEN", {0}}, | |
1740 | {"\\u010CAKAN EVGENIJE", {0}}, | |
1741 | {"\\u010CAKAN IVAN", {0}}, | |
1742 | {"\\u010CAKAN JULIJAN", {0}}, | |
1743 | {"\\u010CAKAN MIHAJLO", {0}}, | |
1744 | {"\\u010CAKAN STEVAN", {0}}, | |
1745 | {"\\u010CAKAN VLADIMIR", {0}}, | |
1746 | {"\\u010CAKAN VLADIMIR", {0}}, | |
1747 | {"\\u010CAKAN VLADIMIR", {0}}, | |
1748 | {"\\u010CAKARA ANA", {0}}, | |
1749 | {"\\u010CAKAREVI\\u0106 MOMIR", {0}}, | |
1750 | {"\\u010CAKAREVI\\u0106 NEDELJKO", {0}}, | |
1751 | {"\\u010CAKI \\u0160ANDOR", {0}}, | |
1752 | {"\\u010CAKI AMALIJA", {0}}, | |
1753 | {"\\u010CAKI ANDRA\\u0160", {0}}, | |
1754 | {"\\u010CAKI LADISLAV", {0}}, | |
1755 | {"\\u010CAKI LAJO\\u0160", {0}}, | |
1756 | {"\\u010CAKI LASLO", {0}} | |
1757 | }; | |
1758 | ||
1759 | ||
1760 | ||
1761 | int32_t i = 0, j = 0, k = 0, buffSize = 0, skSize = 0, lowerSize = 0, upperSize = 0; | |
1762 | int32_t arraySize = sizeof(tests)/sizeof(tests[0]); | |
1763 | ||
1764 | for(i = 0; i<arraySize; i++) { | |
1765 | buffSize = u_unescape(tests[i].original, buffer, 512); | |
1766 | skSize = coll->getSortKey(buffer, buffSize, tests[i].key, 512); | |
1767 | } | |
1768 | ||
1769 | qsort(tests, arraySize, sizeof(struct teststruct), compare_teststruct); | |
1770 | ||
1771 | for(i = 0; i < arraySize-1; i++) { | |
1772 | for(j = i+1; j < arraySize; j++) { | |
1773 | lowerSize = coll->getBound(tests[i].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, status); | |
1774 | upperSize = coll->getBound(tests[j].key, -1, UCOL_BOUND_UPPER, 1, upper, 512, status); | |
1775 | for(k = i; k <= j; k++) { | |
1776 | if(strcmp((const char *)lower, (const char *)tests[k].key) > 0) { | |
1777 | errln("Problem with lower! j = %i (%s vs %s)", k, tests[k].original, tests[i].original); | |
1778 | } | |
1779 | if(strcmp((const char *)upper, (const char *)tests[k].key) <= 0) { | |
1780 | errln("Problem with upper! j = %i (%s vs %s)", k, tests[k].original, tests[j].original); | |
1781 | } | |
1782 | } | |
1783 | } | |
1784 | } | |
1785 | ||
1786 | ||
1787 | for(i = 0; i<(int32_t)(sizeof(test)/sizeof(test[0])); i++) { | |
1788 | buffSize = u_unescape(test[i], buffer, 512); | |
1789 | skSize = coll->getSortKey(buffer, buffSize, sortkey, 512); | |
1790 | lowerSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_LOWER, 1, lower, 512, &status); | |
1791 | upperSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status); | |
1792 | for(j = i+1; j<(int32_t)(sizeof(test)/sizeof(test[0])); j++) { | |
1793 | buffSize = u_unescape(test[j], buffer, 512); | |
1794 | skSize = coll->getSortKey(buffer, buffSize, sortkey, 512); | |
1795 | if(strcmp((const char *)lower, (const char *)sortkey) > 0) { | |
1796 | errln("Problem with lower! i = %i, j = %i (%s vs %s)", i, j, test[i], test[j]); | |
1797 | } | |
1798 | if(strcmp((const char *)upper, (const char *)sortkey) <= 0) { | |
1799 | errln("Problem with upper! i = %i, j = %i (%s vs %s)", i, j, test[i], test[j]); | |
1800 | } | |
1801 | } | |
1802 | } | |
1803 | delete coll; | |
1804 | } | |
1805 | ||
1806 | ||
1807 | void CollationAPITest::TestGetTailoredSet() | |
1808 | { | |
1809 | struct { | |
1810 | const char *rules; | |
1811 | const char *tests[20]; | |
1812 | int32_t testsize; | |
1813 | } setTest[] = { | |
1814 | { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3}, | |
1815 | { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4} | |
1816 | }; | |
1817 | ||
1818 | uint32_t i = 0, j = 0; | |
1819 | UErrorCode status = U_ZERO_ERROR; | |
1820 | ||
1821 | RuleBasedCollator *coll = NULL; | |
1822 | UnicodeString buff; | |
1823 | UnicodeSet *set = NULL; | |
1824 | ||
1825 | for(i = 0; i < sizeof(setTest)/sizeof(setTest[0]); i++) { | |
1826 | buff = UnicodeString(setTest[i].rules, "").unescape(); | |
1827 | coll = new RuleBasedCollator(buff, status); | |
1828 | if(U_SUCCESS(status)) { | |
1829 | set = coll->getTailoredSet(status); | |
1830 | if(set->size() != setTest[i].testsize) { | |
1831 | errln("Tailored set size different (%d) than expected (%d)", set->size(), setTest[i].testsize); | |
1832 | } | |
1833 | for(j = 0; j < (uint32_t)setTest[i].testsize; j++) { | |
1834 | buff = UnicodeString(setTest[i].tests[j], "").unescape(); | |
1835 | if(!set->contains(buff)) { | |
1836 | errln("Tailored set doesn't contain %s... It should", setTest[i].tests[j]); | |
1837 | } | |
1838 | } | |
1839 | delete set; | |
1840 | } else { | |
1841 | errln("Couldn't open collator with rules %s\n", setTest[i].rules); | |
1842 | } | |
1843 | delete coll; | |
1844 | } | |
1845 | } | |
1846 | ||
1847 | void CollationAPITest::TestUClassID() | |
1848 | { | |
1849 | char id = *((char *)RuleBasedCollator::getStaticClassID()); | |
1850 | if (id != 0) { | |
1851 | errln("Static class id for RuleBasedCollator should be 0"); | |
1852 | } | |
1853 | UErrorCode status = U_ZERO_ERROR; | |
1854 | RuleBasedCollator *coll | |
1855 | = (RuleBasedCollator *)Collator::createInstance(status); | |
1856 | if(U_FAILURE(status)) { | |
1857 | delete coll; | |
1858 | errln("Collator creation failed with %s", u_errorName(status)); | |
1859 | return; | |
1860 | } | |
1861 | id = *((char *)coll->getDynamicClassID()); | |
1862 | if (id != 0) { | |
1863 | errln("Dynamic class id for RuleBasedCollator should be 0"); | |
1864 | } | |
1865 | id = *((char *)CollationKey::getStaticClassID()); | |
1866 | if (id != 0) { | |
1867 | errln("Static class id for CollationKey should be 0"); | |
1868 | } | |
1869 | CollationKey *key = new CollationKey(); | |
1870 | id = *((char *)key->getDynamicClassID()); | |
1871 | if (id != 0) { | |
1872 | errln("Dynamic class id for CollationKey should be 0"); | |
1873 | } | |
1874 | id = *((char *)CollationElementIterator::getStaticClassID()); | |
1875 | if (id != 0) { | |
1876 | errln("Static class id for CollationElementIterator should be 0"); | |
1877 | } | |
1878 | UnicodeString str("testing"); | |
1879 | CollationElementIterator *iter = coll->createCollationElementIterator(str); | |
1880 | id = *((char *)iter->getDynamicClassID()); | |
1881 | if (id != 0) { | |
1882 | errln("Dynamic class id for CollationElementIterator should be 0"); | |
1883 | } | |
1884 | delete key; | |
1885 | delete iter; | |
1886 | delete coll; | |
1887 | } | |
1888 | ||
1889 | class TestCollator : public Collator | |
1890 | { | |
1891 | public: | |
1892 | virtual Collator* clone(void) const; | |
1893 | ||
1894 | // dang, markus says we can't use 'using' in ICU. I hate doing this for | |
1895 | // deprecated methods... | |
1896 | ||
1897 | // using Collator::compare; | |
1898 | ||
1899 | virtual EComparisonResult compare(const UnicodeString& source, | |
1900 | const UnicodeString& target) const | |
1901 | { | |
1902 | return Collator::compare(source, target); | |
1903 | } | |
1904 | ||
1905 | virtual EComparisonResult compare(const UnicodeString& source, | |
1906 | const UnicodeString& target, | |
1907 | int32_t length) const | |
1908 | { | |
1909 | return Collator::compare(source, target, length); | |
1910 | } | |
1911 | ||
1912 | virtual EComparisonResult compare(const UChar* source, | |
1913 | int32_t sourceLength, | |
1914 | const UChar* target, | |
1915 | int32_t targetLength) const | |
1916 | { | |
1917 | return Collator::compare(source, sourceLength, target, targetLength); | |
1918 | } | |
1919 | ||
1920 | ||
1921 | virtual UCollationResult compare(const UnicodeString& source, | |
1922 | const UnicodeString& target, | |
1923 | UErrorCode& status) const; | |
1924 | virtual UCollationResult compare(const UnicodeString& source, | |
1925 | const UnicodeString& target, | |
1926 | int32_t length, | |
1927 | UErrorCode& status) const; | |
1928 | virtual UCollationResult compare(const UChar* source, | |
1929 | int32_t sourceLength, | |
1930 | const UChar* target, | |
1931 | int32_t targetLength, | |
1932 | UErrorCode& status) const; | |
1933 | virtual CollationKey& getCollationKey(const UnicodeString& source, | |
1934 | CollationKey& key, | |
1935 | UErrorCode& status) const; | |
1936 | virtual CollationKey& getCollationKey(const UChar*source, | |
1937 | int32_t sourceLength, | |
1938 | CollationKey& key, | |
1939 | UErrorCode& status) const; | |
1940 | virtual int32_t hashCode(void) const; | |
1941 | virtual const Locale getLocale(ULocDataLocaleType type, | |
1942 | UErrorCode& status) const; | |
1943 | virtual ECollationStrength getStrength(void) const; | |
1944 | virtual void setStrength(ECollationStrength newStrength); | |
1945 | virtual UClassID getDynamicClassID(void) const; | |
1946 | virtual void getVersion(UVersionInfo info) const; | |
1947 | virtual void setAttribute(UColAttribute attr, UColAttributeValue value, | |
1948 | UErrorCode &status); | |
1949 | virtual UColAttributeValue getAttribute(UColAttribute attr, | |
1950 | UErrorCode &status); | |
1951 | virtual uint32_t setVariableTop(const UChar *varTop, int32_t len, | |
1952 | UErrorCode &status); | |
1953 | virtual uint32_t setVariableTop(const UnicodeString varTop, | |
1954 | UErrorCode &status); | |
1955 | virtual void setVariableTop(const uint32_t varTop, UErrorCode &status); | |
1956 | virtual uint32_t getVariableTop(UErrorCode &status) const; | |
1957 | virtual Collator* safeClone(void); | |
1958 | virtual int32_t getSortKey(const UnicodeString& source, | |
1959 | uint8_t* result, | |
1960 | int32_t resultLength) const; | |
1961 | virtual int32_t getSortKey(const UChar*source, int32_t sourceLength, | |
1962 | uint8_t*result, int32_t resultLength) const; | |
1963 | virtual UnicodeSet *getTailoredSet(UErrorCode &status) const; | |
1964 | virtual UBool operator!=(const Collator& other) const; | |
46f4442e | 1965 | virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale); |
b75a7d8f A |
1966 | TestCollator() : Collator() {}; |
1967 | TestCollator(UCollationStrength collationStrength, | |
1968 | UNormalizationMode decompositionMode) : Collator(collationStrength, decompositionMode) {}; | |
1969 | }; | |
1970 | ||
1971 | inline UBool TestCollator::operator!=(const Collator& other) const { | |
1972 | return Collator::operator!=(other); | |
1973 | } | |
1974 | ||
1975 | #define returnEComparisonResult(data) \ | |
1976 | if (data < 0) return Collator::LESS;\ | |
1977 | if (data > 0) return Collator::GREATER;\ | |
1978 | return Collator::EQUAL; | |
1979 | ||
1980 | Collator* TestCollator::clone() const | |
1981 | { | |
1982 | return new TestCollator(); | |
1983 | } | |
1984 | ||
1985 | UCollationResult TestCollator::compare(const UnicodeString& source, | |
1986 | const UnicodeString& target, | |
1987 | UErrorCode& status) const | |
1988 | { | |
1989 | if(U_SUCCESS(status)) { | |
1990 | return UCollationResult(source.compare(target)); | |
1991 | } else { | |
1992 | return UCOL_EQUAL; | |
1993 | } | |
1994 | } | |
1995 | ||
1996 | UCollationResult TestCollator::compare(const UnicodeString& source, | |
1997 | const UnicodeString& target, | |
1998 | int32_t length, | |
1999 | UErrorCode& status) const | |
2000 | { | |
2001 | if(U_SUCCESS(status)) { | |
2002 | return UCollationResult(source.compare(0, length, target)); | |
2003 | } else { | |
2004 | return UCOL_EQUAL; | |
2005 | } | |
2006 | } | |
2007 | ||
2008 | UCollationResult TestCollator::compare(const UChar* source, | |
2009 | int32_t sourceLength, | |
2010 | const UChar* target, | |
2011 | int32_t targetLength, | |
2012 | UErrorCode& status) const | |
2013 | { | |
2014 | UnicodeString s(source, sourceLength); | |
2015 | UnicodeString t(target, targetLength); | |
2016 | return compare(s, t, status); | |
2017 | } | |
2018 | ||
2019 | CollationKey& TestCollator::getCollationKey(const UnicodeString& source, | |
2020 | CollationKey& key, | |
2021 | UErrorCode& status) const | |
2022 | { | |
2023 | char temp[100]; | |
2024 | int length = 100; | |
2025 | length = source.extract(temp, length, NULL, status); | |
2026 | temp[length] = 0; | |
2027 | CollationKey tempkey((uint8_t*)temp, length); | |
2028 | key = tempkey; | |
2029 | return key; | |
2030 | } | |
2031 | ||
2032 | CollationKey& TestCollator::getCollationKey(const UChar*source, | |
2033 | int32_t sourceLength, | |
2034 | CollationKey& key, | |
2035 | UErrorCode& status) const | |
2036 | { | |
2037 | //s tack allocation used since collationkey does not keep the unicodestring | |
2038 | UnicodeString str(source, sourceLength); | |
2039 | return getCollationKey(str, key, status); | |
2040 | } | |
2041 | ||
2042 | int32_t TestCollator::getSortKey(const UnicodeString& source, uint8_t* result, | |
2043 | int32_t resultLength) const | |
2044 | { | |
2045 | UErrorCode status = U_ZERO_ERROR; | |
2046 | int32_t length = source.extract((char *)result, resultLength, NULL, | |
2047 | status); | |
2048 | result[length] = 0; | |
2049 | return length; | |
2050 | } | |
2051 | ||
2052 | int32_t TestCollator::getSortKey(const UChar*source, int32_t sourceLength, | |
2053 | uint8_t*result, int32_t resultLength) const | |
2054 | { | |
2055 | UnicodeString str(source, sourceLength); | |
2056 | return getSortKey(str, result, resultLength); | |
2057 | } | |
2058 | ||
2059 | int32_t TestCollator::hashCode() const | |
2060 | { | |
2061 | return 0; | |
2062 | } | |
2063 | ||
2064 | const Locale TestCollator::getLocale(ULocDataLocaleType type, | |
2065 | UErrorCode& status) const | |
2066 | { | |
2067 | // api not used, this is to make the compiler happy | |
2068 | if (U_FAILURE(status)) { | |
2069 | type = ULOC_DATA_LOCALE_TYPE_LIMIT; | |
2070 | } | |
2071 | return NULL; | |
2072 | } | |
2073 | ||
2074 | Collator::ECollationStrength TestCollator::getStrength() const | |
2075 | { | |
2076 | return TERTIARY; | |
2077 | } | |
2078 | ||
2079 | void TestCollator::setStrength(Collator::ECollationStrength newStrength) | |
2080 | { | |
2081 | // api not used, this is to make the compiler happy | |
2082 | newStrength = TERTIARY; | |
2083 | } | |
2084 | ||
2085 | UClassID TestCollator::getDynamicClassID(void) const | |
2086 | { | |
2087 | return 0; | |
2088 | } | |
2089 | ||
2090 | void TestCollator::getVersion(UVersionInfo info) const | |
2091 | { | |
2092 | // api not used, this is to make the compiler happy | |
2093 | memset(info, 0, U_MAX_VERSION_LENGTH); | |
2094 | } | |
2095 | ||
2096 | void TestCollator::setAttribute(UColAttribute attr, UColAttributeValue value, | |
2097 | UErrorCode &status) | |
2098 | { | |
2099 | // api not used, this is to make the compiler happy | |
2100 | if (U_FAILURE(status)) { | |
2101 | attr = UCOL_ATTRIBUTE_COUNT; | |
2102 | value = UCOL_OFF; | |
2103 | } | |
2104 | } | |
2105 | ||
2106 | UColAttributeValue TestCollator::getAttribute(UColAttribute attr, | |
2107 | UErrorCode &status) | |
2108 | { | |
2109 | // api not used, this is to make the compiler happy | |
2110 | if (U_FAILURE(status) || attr == UCOL_ATTRIBUTE_COUNT) { | |
2111 | return UCOL_OFF; | |
2112 | } | |
2113 | return UCOL_DEFAULT; | |
2114 | } | |
2115 | ||
2116 | uint32_t TestCollator::setVariableTop(const UChar *varTop, int32_t len, | |
2117 | UErrorCode &status) | |
2118 | { | |
2119 | // api not used, this is to make the compiler happy | |
2120 | if (U_SUCCESS(status) && (varTop == 0 || len < -1)) { | |
2121 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2122 | } | |
2123 | return 0; | |
2124 | } | |
2125 | ||
2126 | uint32_t TestCollator::setVariableTop(const UnicodeString varTop, | |
2127 | UErrorCode &status) | |
2128 | { | |
2129 | // api not used, this is to make the compiler happy | |
2130 | if (U_SUCCESS(status) && varTop.length() == 0) { | |
2131 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2132 | } | |
2133 | return 0; | |
2134 | } | |
2135 | ||
2136 | void TestCollator::setVariableTop(const uint32_t varTop, UErrorCode &status) | |
2137 | { | |
2138 | // api not used, this is to make the compiler happy | |
2139 | if (U_SUCCESS(status) && varTop == 0) { | |
2140 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
2141 | } | |
2142 | } | |
2143 | ||
2144 | uint32_t TestCollator::getVariableTop(UErrorCode &status) const | |
2145 | { | |
2146 | ||
2147 | // api not used, this is to make the compiler happy | |
2148 | if (U_SUCCESS(status)) { | |
2149 | return 0; | |
2150 | } | |
2151 | return (uint32_t)(0xFFFFFFFFu); | |
2152 | } | |
2153 | ||
2154 | Collator* TestCollator::safeClone(void) | |
2155 | { | |
2156 | return new TestCollator(); | |
2157 | } | |
2158 | ||
2159 | UnicodeSet * TestCollator::getTailoredSet(UErrorCode &status) const | |
2160 | { | |
2161 | return Collator::getTailoredSet(status); | |
2162 | } | |
2163 | ||
46f4442e | 2164 | void TestCollator::setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale) |
b75a7d8f | 2165 | { |
46f4442e | 2166 | Collator::setLocales(requestedLocale, validLocale, actualLocale); |
b75a7d8f A |
2167 | } |
2168 | ||
2169 | ||
2170 | void CollationAPITest::TestSubclass() | |
2171 | { | |
2172 | TestCollator col1; | |
2173 | TestCollator col2; | |
2174 | doAssert(col1 != col2, "2 instance of TestCollator should be different"); | |
2175 | if (col1.hashCode() != col2.hashCode()) { | |
2176 | errln("Every TestCollator has the same hashcode"); | |
2177 | } | |
2178 | UnicodeString abc("abc", 3); | |
2179 | UnicodeString bcd("bcd", 3); | |
2180 | if (col1.compare(abc, bcd) != abc.compare(bcd)) { | |
2181 | errln("TestCollator compare should be the same as the default " | |
2182 | "string comparison"); | |
2183 | } | |
2184 | CollationKey key; | |
2185 | UErrorCode status = U_ZERO_ERROR; | |
2186 | col1.getCollationKey(abc, key, status); | |
2187 | int32_t length = 0; | |
2188 | const char* bytes = (const char *)key.getByteArray(length); | |
2189 | UnicodeString keyarray(bytes, length, NULL, status); | |
2190 | if (abc != keyarray) { | |
2191 | errln("TestCollator collationkey API is returning wrong values"); | |
2192 | } | |
2193 | ||
2194 | UnicodeSet expectedset(0, 0x10FFFF); | |
2195 | UnicodeSet *defaultset = col1.getTailoredSet(status); | |
2196 | if (!defaultset->containsAll(expectedset) | |
2197 | || !expectedset.containsAll(*defaultset)) { | |
2198 | errln("Error: expected default tailoring to be 0 to 0x10ffff"); | |
2199 | } | |
2200 | delete defaultset; | |
2201 | ||
2202 | // use base class implementation | |
2203 | Locale loc1 = Locale::getGermany(); | |
2204 | Locale loc2 = Locale::getFrance(); | |
46f4442e | 2205 | col1.setLocales(loc1, loc2, loc2); // default implementation has no effect |
b75a7d8f A |
2206 | |
2207 | UnicodeString displayName; | |
2208 | col1.getDisplayName(loc1, loc2, displayName); // de_DE collator in fr_FR locale | |
2209 | ||
2210 | TestCollator col3(UCOL_TERTIARY, UNORM_NONE); | |
2211 | UnicodeString a("a"); | |
2212 | UnicodeString b("b"); | |
2213 | Collator::EComparisonResult result = Collator::EComparisonResult(a.compare(b)); | |
2214 | if(col1.compare(a, b) != result) { | |
2215 | errln("Collator doesn't give default result"); | |
2216 | } | |
2217 | if(col1.compare(a, b, 1) != result) { | |
2218 | errln("Collator doesn't give default result"); | |
2219 | } | |
2220 | if(col1.compare(a.getBuffer(), a.length(), b.getBuffer(), b.length()) != result) { | |
2221 | errln("Collator doesn't give default result"); | |
2222 | } | |
2223 | } | |
2224 | ||
374ca955 A |
2225 | void CollationAPITest::TestNULLCharTailoring() |
2226 | { | |
2227 | UErrorCode status = U_ZERO_ERROR; | |
2228 | UChar buf[256] = {0}; | |
2229 | int32_t len = u_unescape("&a < '\\u0000'", buf, 256); | |
2230 | UnicodeString first((UChar)0x0061); | |
2231 | UnicodeString second((UChar)0); | |
2232 | RuleBasedCollator *coll = new RuleBasedCollator(UnicodeString(buf, len), status); | |
2233 | if(U_FAILURE(status)) { | |
2234 | errln("Failed to open collator"); | |
2235 | } | |
2236 | UCollationResult res = coll->compare(first, second, status); | |
2237 | if(res != UCOL_LESS) { | |
2238 | errln("a should be less then NULL after tailoring"); | |
2239 | } | |
2240 | delete coll; | |
2241 | } | |
2242 | ||
73c04bcf A |
2243 | void CollationAPITest::TestClone() { |
2244 | logln("\ninit c0"); | |
2245 | UErrorCode status = U_ZERO_ERROR; | |
2246 | RuleBasedCollator* c0 = (RuleBasedCollator*)Collator::createInstance(status); | |
46f4442e A |
2247 | |
2248 | if (U_FAILURE(status)) { | |
2249 | errln("Collator::CreateInstance(status) failed with %s", u_errorName(status)); | |
2250 | return; | |
2251 | } | |
2252 | ||
73c04bcf A |
2253 | c0->setStrength(Collator::TERTIARY); |
2254 | dump("c0", c0, status); | |
2255 | ||
2256 | logln("\ninit c1"); | |
2257 | RuleBasedCollator* c1 = (RuleBasedCollator*)Collator::createInstance(status); | |
2258 | c1->setStrength(Collator::TERTIARY); | |
2259 | UColAttributeValue val = c1->getAttribute(UCOL_CASE_FIRST, status); | |
2260 | if(val == UCOL_LOWER_FIRST){ | |
2261 | c1->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status); | |
2262 | }else{ | |
2263 | c1->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status); | |
2264 | } | |
2265 | dump("c0", c0, status); | |
2266 | dump("c1", c1, status); | |
2267 | ||
2268 | logln("\ninit c2"); | |
2269 | RuleBasedCollator* c2 = (RuleBasedCollator*)c1->clone(); | |
2270 | val = c2->getAttribute(UCOL_CASE_FIRST, status); | |
2271 | if(val == UCOL_LOWER_FIRST){ | |
2272 | c2->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status); | |
2273 | }else{ | |
2274 | c2->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status); | |
2275 | } | |
2276 | if(U_FAILURE(status)){ | |
2277 | errln("set and get attributes of collator failed. %s\n", u_errorName(status)); | |
2278 | return; | |
2279 | } | |
2280 | dump("c0", c0, status); | |
2281 | dump("c1", c1, status); | |
2282 | dump("c2", c2, status); | |
2283 | if(*c1 == *c2){ | |
2284 | errln("The cloned objects refer to same data"); | |
2285 | } | |
2286 | delete c0; | |
2287 | delete c1; | |
2288 | delete c2; | |
2289 | } | |
2290 | ||
2291 | void CollationAPITest::dump(UnicodeString msg, RuleBasedCollator* c, UErrorCode& status) { | |
2292 | const char* bigone = "One"; | |
2293 | const char* littleone = "one"; | |
2294 | ||
2295 | logln(msg + " " + c->compare(bigone, littleone) + | |
2296 | " s: " + c->getStrength() + | |
2297 | " u: " + c->getAttribute(UCOL_CASE_FIRST, status)); | |
2298 | } | |
b75a7d8f A |
2299 | void CollationAPITest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */) |
2300 | { | |
2301 | if (exec) logln("TestSuite CollationAPITest: "); | |
2302 | switch (index) { | |
2303 | case 0: name = "TestProperty"; if (exec) TestProperty(/* par */); break; | |
2304 | case 1: name = "TestOperators"; if (exec) TestOperators(/* par */); break; | |
2305 | case 2: name = "TestDuplicate"; if (exec) TestDuplicate(/* par */); break; | |
2306 | case 3: name = "TestCompare"; if (exec) TestCompare(/* par */); break; | |
2307 | case 4: name = "TestHashCode"; if (exec) TestHashCode(/* par */); break; | |
2308 | case 5: name = "TestCollationKey"; if (exec) TestCollationKey(/* par */); break; | |
2309 | case 6: name = "TestElemIter"; if (exec) TestElemIter(/* par */); break; | |
2310 | case 7: name = "TestGetAll"; if (exec) TestGetAll(/* par */); break; | |
2311 | case 8: name = "TestRuleBasedColl"; if (exec) TestRuleBasedColl(/* par */); break; | |
2312 | case 9: name = "TestDecomposition"; if (exec) TestDecomposition(/* par */); break; | |
2313 | case 10: name = "TestSafeClone"; if (exec) TestSafeClone(/* par */); break; | |
2314 | case 11: name = "TestSortKey"; if (exec) TestSortKey(); break; | |
2315 | case 12: name = "TestMaxExpansion"; if (exec) TestMaxExpansion(); break; | |
2316 | case 13: name = "TestDisplayName"; if (exec) TestDisplayName(); break; | |
2317 | case 14: name = "TestAttribute"; if (exec) TestAttribute(); break; | |
2318 | case 15: name = "TestVariableTopSetting"; if (exec) TestVariableTopSetting(); break; | |
2319 | case 16: name = "TestRules"; if (exec) TestRules(); break; | |
2320 | case 17: name = "TestGetLocale"; if (exec) TestGetLocale(); break; | |
2321 | case 18: name = "TestBounds"; if (exec) TestBounds(); break; | |
2322 | case 19: name = "TestGetTailoredSet"; if (exec) TestGetTailoredSet(); break; | |
2323 | case 20: name = "TestUClassID"; if (exec) TestUClassID(); break; | |
2324 | case 21: name = "TestSubclass"; if (exec) TestSubclass(); break; | |
374ca955 | 2325 | case 22: name = "TestNULLCharTailoring"; if (exec) TestNULLCharTailoring(); break; |
73c04bcf | 2326 | case 23: name = "TestClone"; if (exec) TestClone(); break; |
b75a7d8f A |
2327 | default: name = ""; break; |
2328 | } | |
2329 | } | |
2330 | ||
2331 | #endif /* #if !UCONFIG_NO_COLLATION */ |