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