1 /********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 //===============================================================================
12 // Created by: Helena Shih
14 // Modification History:
16 // Date Name Description
17 // 2/5/97 aliu Added streamIn and streamOut methods. Added
18 // constructor which reads RuleBasedCollator object from
19 // a binary file. Added writeToFile method which streams
20 // RuleBasedCollator out to a binary file. The streamIn
21 // and streamOut methods use istream and ostream objects
23 // 6/30/97 helena Added tests for CollationElementIterator::setText, getOffset
24 // setOffset and DecompositionIterator::getOffset, setOffset.
25 // DecompositionIterator is made public so add class scope
27 // 02/10/98 damiba Added test for compare(UnicodeString&, UnicodeString&, int32_t)
28 //===============================================================================
30 #include "unicode/utypes.h"
32 #if !UCONFIG_NO_COLLATION
34 #include "unicode/coll.h"
35 #include "unicode/tblcoll.h"
36 #include "unicode/coleitr.h"
37 #include "unicode/sortkey.h"
39 #include "unicode/chariter.h"
40 #include "unicode/schriter.h"
41 #include "unicode/ustring.h"
47 CollationAPITest::doAssert(UBool condition
, const char *message
)
50 errln(UnicodeString("ERROR : ") + message
);
54 #ifdef U_USE_COLLATION_OBSOLETE_2_6
56 * Test Collator::createInstance(... version...) for some locale. Called by TestProperty().
59 TestOpenVersion(IntlTest
&test
, const Locale
&locale
) {
60 UVersionInfo version1
, version2
;
61 Collator
*collator1
, *collator2
;
64 errorCode
=U_ZERO_ERROR
;
65 collator1
=Collator::createInstance(locale
, errorCode
);
66 if(U_SUCCESS(errorCode
)) {
67 /* get the current version */
68 collator1
->getVersion(version1
);
71 /* try to get that same version again */
72 collator2
=Collator::createInstance(locale
, version1
, errorCode
);
73 if(U_SUCCESS(errorCode
)) {
74 collator2
->getVersion(version2
);
75 if(0!=uprv_memcmp(version1
, version2
, sizeof(UVersionInfo
))) {
76 test
.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) returns a different collator\n", locale
.getName(), locale
.getName());
80 test
.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) fails: %s\n", locale
.getName(), locale
.getName(), u_errorName(errorCode
));
86 // Collator Class Properties
87 // ctor, dtor, createInstance, compare, getStrength/setStrength
88 // getDecomposition/setDecomposition, getDisplayName
90 CollationAPITest::TestProperty(/* char* par */)
92 UErrorCode success
= U_ZERO_ERROR
;
95 All the collations have the same version in an ICU
97 ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};
98 ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};
99 ICU 2.2 currVersionArray = {0x21, 0x40, 0x04, 0x04};
100 ICU 2.4 currVersionArray = {0x21, 0x40, 0x04, 0x04};
101 ICU 2.6 currVersionArray = {0x21, 0x40, 0x03, 0x03};
103 UVersionInfo currVersionArray
= {0x29, 0x80, 0x00, 0x04};
104 UVersionInfo versionArray
;
107 logln("The property tests begin : ");
108 logln("Test ctors : ");
109 col
= Collator::createInstance(Locale::getEnglish(), success
);
111 if (U_FAILURE(success
))
113 errln("Default Collator creation failed.");
117 col
->getVersion(versionArray
);
118 for (i
=0; i
<4; ++i
) {
119 if (versionArray
[i
] != currVersionArray
[i
]) {
120 errln("Testing ucol_getVersion() - unexpected result: %d.%d.%d.%d",
121 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
126 doAssert((col
->compare("ab", "abc") == Collator::LESS
), "ab < abc comparison failed");
127 doAssert((col
->compare("ab", "AB") == Collator::LESS
), "ab < AB comparison failed");
128 doAssert((col
->compare("blackbird", "black-bird") == Collator::GREATER
), "black-bird > blackbird comparison failed");
129 doAssert((col
->compare("black bird", "black-bird") == Collator::LESS
), "black bird > black-bird comparison failed");
130 doAssert((col
->compare("Hello", "hello") == Collator::GREATER
), "Hello > hello comparison failed");
133 /*start of update [Bertrand A. D. 02/10/98]*/
134 doAssert((col
->compare("ab", "abc", 2) == Collator::EQUAL
), "ab = abc with length 2 comparison failed");
135 doAssert((col
->compare("ab", "AB", 2) == Collator::LESS
), "ab < AB with length 2 comparison failed");
136 doAssert((col
->compare("ab", "Aa", 1) == Collator::LESS
), "ab < Aa with length 1 comparison failed");
137 doAssert((col
->compare("ab", "Aa", 2) == Collator::GREATER
), "ab > Aa with length 2 comparison failed");
138 doAssert((col
->compare("black-bird", "blackbird", 5) == Collator::EQUAL
), "black-bird = blackbird with length of 5 comparison failed");
139 doAssert((col
->compare("black bird", "black-bird", 10) == Collator::LESS
), "black bird < black-bird with length 10 comparison failed");
140 doAssert((col
->compare("Hello", "hello", 5) == Collator::GREATER
), "Hello > hello with length 5 comparison failed");
141 /*end of update [Bertrand A. D. 02/10/98]*/
144 logln("Test ctors ends.");
145 logln("testing Collator::getStrength() method ...");
146 doAssert((col
->getStrength() == Collator::TERTIARY
), "collation object has the wrong strength");
147 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
150 logln("testing Collator::setStrength() method ...");
151 col
->setStrength(Collator::SECONDARY
);
152 doAssert((col
->getStrength() != Collator::TERTIARY
), "collation object's strength is secondary difference");
153 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
154 doAssert((col
->getStrength() == Collator::SECONDARY
), "collation object has the wrong strength");
158 logln("Get display name for the US English collation in German : ");
159 logln(Collator::getDisplayName(Locale::getUS(), Locale::getGerman(), name
));
160 doAssert((name
== UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed");
162 logln("Get display name for the US English collation in English : ");
163 logln(Collator::getDisplayName(Locale::getUS(), Locale::getEnglish(), name
));
164 doAssert((name
== UnicodeString("English (United States)")), "getDisplayName failed");
166 // weiv : this test is bogus if we're running on any machine that has different default locale than English.
167 // Therefore, it is banned!
168 logln("Get display name for the US English in default locale language : ");
169 logln(Collator::getDisplayName(Locale::US
, name
));
170 doAssert((name
== UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine");
173 RuleBasedCollator
*rcol
= (RuleBasedCollator
*)Collator::createInstance("da_DK",
175 doAssert(rcol
->getRules().length() != 0, "da_DK rules does not have length 0");
178 col
= Collator::createInstance(Locale::getFrench(), success
);
179 if (U_FAILURE(success
))
181 errln("Creating French collation failed.");
185 col
->setStrength(Collator::PRIMARY
);
186 logln("testing Collator::getStrength() method again ...");
187 doAssert((col
->getStrength() != Collator::TERTIARY
), "collation object has the wrong strength");
188 doAssert((col
->getStrength() == Collator::PRIMARY
), "collation object's strength is not primary difference");
190 logln("testing French Collator::setStrength() method ...");
191 col
->setStrength(Collator::TERTIARY
);
192 doAssert((col
->getStrength() == Collator::TERTIARY
), "collation object's strength is not tertiary difference");
193 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
194 doAssert((col
->getStrength() != Collator::SECONDARY
), "collation object's strength is secondary difference");
196 logln("Create junk collation: ");
197 Locale
abcd("ab", "CD", "");
198 success
= U_ZERO_ERROR
;
200 junk
= Collator::createInstance(abcd
, success
);
202 if (U_FAILURE(success
))
204 errln("Junk collation creation failed, should at least return default.");
210 col
= Collator::createInstance(success
);
211 if (U_FAILURE(success
))
213 errln("Creating default collator failed.");
218 doAssert(((RuleBasedCollator
*)col
)->getRules() == ((RuleBasedCollator
*)junk
)->getRules(),
219 "The default collation should be returned.");
220 Collator
*frCol
= Collator::createInstance(Locale::getFrance(), success
);
221 if (U_FAILURE(success
))
223 errln("Creating French collator failed.");
224 delete col
; delete junk
;
228 doAssert((*frCol
!= *junk
), "The junk is the same as the French collator.");
229 Collator
*aFrCol
= frCol
->clone();
230 doAssert((*frCol
== *aFrCol
), "The cloning of a French collator failed.");
231 logln("Collator property test ended.");
238 #ifdef U_USE_COLLATION_OBSOLETE_2_6
239 /* test Collator::createInstance(...version...) */
240 TestOpenVersion(*this, "");
241 TestOpenVersion(*this, "da");
242 TestOpenVersion(*this, "fr");
243 TestOpenVersion(*this, "ja");
245 /* try some bogus version */
247 versionArray
[1]=0x99;
248 versionArray
[2]=0xc7;
249 versionArray
[3]=0xfe;
250 col
=Collator::createInstance(Locale(), versionArray
, success
);
251 if(U_SUCCESS(success
)) {
252 errln("error: ucol_openVersion(bogus version) succeeded");
259 CollationAPITest::TestRuleBasedColl()
261 RuleBasedCollator
*col1
, *col2
, *col3
, *col4
;
262 UErrorCode status
= U_ZERO_ERROR
;
264 UnicodeString
ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
265 UnicodeString
ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
267 col1
= new RuleBasedCollator(ruleset1
, status
);
268 if (U_FAILURE(status
)) {
269 errln("RuleBased Collator creation failed.\n");
273 logln("PASS: RuleBased Collator creation passed\n");
276 status
= U_ZERO_ERROR
;
277 col2
= new RuleBasedCollator(ruleset2
, status
);
278 if (U_FAILURE(status
)) {
279 errln("RuleBased Collator creation failed.\n");
283 logln("PASS: RuleBased Collator creation passed\n");
286 status
= U_ZERO_ERROR
;
287 Locale
locale("aa", "AA");
288 col3
= (RuleBasedCollator
*)Collator::createInstance(locale
, status
);
289 if (U_FAILURE(status
)) {
290 errln("Fallback Collator creation failed.: %s\n");
294 logln("PASS: Fallback Collator creation passed\n");
298 status
= U_ZERO_ERROR
;
299 col3
= (RuleBasedCollator
*)Collator::createInstance(status
);
300 if (U_FAILURE(status
)) {
301 errln("Default Collator creation failed.: %s\n");
305 logln("PASS: Default Collator creation passed\n");
308 UnicodeString rule1
= col1
->getRules();
309 UnicodeString rule2
= col2
->getRules();
310 UnicodeString rule3
= col3
->getRules();
312 doAssert(rule1
!= rule2
, "Default collator getRules failed");
313 doAssert(rule2
!= rule3
, "Default collator getRules failed");
314 doAssert(rule1
!= rule3
, "Default collator getRules failed");
316 col4
= new RuleBasedCollator(rule2
, status
);
317 if (U_FAILURE(status
)) {
318 errln("RuleBased Collator creation failed.\n");
322 UnicodeString rule4
= col4
->getRules();
323 doAssert(rule2
== rule4
, "Default collator getRules failed");
325 uint8_t *clonedrule4
= col4
->cloneRuleData(length4
, status
);
326 if (U_FAILURE(status
)) {
327 errln("Cloned rule data failed.\n");
331 // free(clonedrule4); BAD API!!!!
332 uprv_free(clonedrule4
);
342 CollationAPITest::TestRules()
344 RuleBasedCollator
*coll
;
345 UErrorCode status
= U_ZERO_ERROR
;
348 coll
= (RuleBasedCollator
*)Collator::createInstance(Locale::getEnglish(), status
);
349 if (U_FAILURE(status
)) {
350 errln("English Collator creation failed.\n");
354 logln("PASS: RuleBased Collator creation passed\n");
357 coll
->getRules(UCOL_TAILORING_ONLY
, rules
);
358 if (rules
.length() != 0x0a) {
359 errln("English tailored rules failed - length is 0x%x expected 0x%x", rules
.length(), 0x0e);
362 coll
->getRules(UCOL_FULL_RULES
, rules
);
363 if (rules
.length() < 0) {
364 errln("English full rules failed");
370 CollationAPITest::TestDecomposition() {
371 UErrorCode status
= U_ZERO_ERROR
;
372 Collator
*en_US
= Collator::createInstance("en_US", status
),
373 *el_GR
= Collator::createInstance("el_GR", status
),
374 *vi_VN
= Collator::createInstance("vi_VN", status
);
376 if (U_FAILURE(status
)) {
377 errln("ERROR: collation creation failed.\n");
381 /* there is no reason to have canonical decomposition in en_US OR default locale */
382 if (vi_VN
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_ON
)
384 errln("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
387 if (el_GR
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_ON
)
389 errln("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
392 if (en_US
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_OFF
)
394 errln("ERROR: en_US collation had canonical decomposition for normalization!\n");
403 CollationAPITest::TestSafeClone() {
404 static const int CLONETEST_COLLATOR_COUNT
= 3;
405 Collator
*someCollators
[CLONETEST_COLLATOR_COUNT
];
407 UErrorCode err
= U_ZERO_ERROR
;
410 UnicodeString
test1("abCda");
411 UnicodeString
test2("abcda");
413 /* one default collator & two complex ones */
414 someCollators
[0] = Collator::createInstance("en_US", err
);
415 someCollators
[1] = Collator::createInstance("ko", err
);
416 someCollators
[2] = Collator::createInstance("ja_JP", err
);
418 errln("Couldn't instantiate collators. Error: %s", u_errorName(err
));
419 delete someCollators
[0];
420 delete someCollators
[1];
421 delete someCollators
[2];
425 /* change orig & clone & make sure they are independent */
427 for (index
= 0; index
< CLONETEST_COLLATOR_COUNT
; index
++)
429 col
= someCollators
[index
]->safeClone();
431 errln("SafeClone of collator should not return null\n");
434 col
->setStrength(Collator::TERTIARY
);
435 someCollators
[index
]->setStrength(Collator::PRIMARY
);
436 col
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, err
);
437 someCollators
[index
]->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, err
);
439 doAssert(col
->greater(test1
, test2
), "Result should be \"abCda\" >>> \"abcda\" ");
440 doAssert(someCollators
[index
]->equals(test1
, test2
), "Result should be \"abcda\" == \"abCda\"");
442 delete someCollators
[index
];
447 CollationAPITest::TestHashCode(/* char* par */)
449 logln("hashCode tests begin.");
450 UErrorCode success
= U_ZERO_ERROR
;
452 col1
= Collator::createInstance(Locale::getEnglish(), success
);
453 if (U_FAILURE(success
))
455 errln("Default collation creation failed.");
460 Locale
dk("da", "DK", "");
461 col2
= Collator::createInstance(dk
, success
);
462 if (U_FAILURE(success
))
464 errln("Danish collation creation failed.");
469 col3
= Collator::createInstance(Locale::getEnglish(), success
);
470 if (U_FAILURE(success
))
472 errln("2nd default collation creation failed.");
476 logln("Collator::hashCode() testing ...");
478 doAssert(col1
->hashCode() != col2
->hashCode(), "Hash test1 result incorrect" );
479 doAssert(!(col1
->hashCode() == col2
->hashCode()), "Hash test2 result incorrect" );
480 doAssert(col1
->hashCode() == col3
->hashCode(), "Hash result not equal" );
482 logln("hashCode tests end.");
486 UnicodeString
test1("Abcda");
487 UnicodeString
test2("abcda");
489 CollationKey sortk1
, sortk2
, sortk3
;
490 UErrorCode status
= U_ZERO_ERROR
;
492 col3
->getCollationKey(test1
, sortk1
, status
);
493 col3
->getCollationKey(test2
, sortk2
, status
);
494 col3
->getCollationKey(test2
, sortk3
, status
);
496 doAssert(sortk1
.hashCode() != sortk2
.hashCode(), "Hash test1 result incorrect");
497 doAssert(sortk2
.hashCode() == sortk3
.hashCode(), "Hash result not equal" );
502 //----------------------------------------------------------------------------
503 // CollationKey -- Tests the CollationKey methods
506 CollationAPITest::TestCollationKey(/* char* par */)
508 logln("testing CollationKey begins...");
510 UErrorCode success
=U_ZERO_ERROR
;
511 col
= Collator::createInstance(Locale::getEnglish(), success
);
512 if (U_FAILURE(success
))
514 errln("Default collation creation failed.");
517 col
->setStrength(Collator::TERTIARY
);
519 CollationKey sortk1
, sortk2
;
520 UnicodeString
test1("Abcda"), test2("abcda");
521 UErrorCode key1Status
= U_ZERO_ERROR
, key2Status
= U_ZERO_ERROR
;
523 logln("Testing weird arguments");
524 col
->getCollationKey(NULL
, 0, sortk1
, key1Status
);
525 // key gets reset here
527 sortk1
.getByteArray(length
);
528 doAssert(sortk1
.isBogus() == FALSE
&& length
== 0,
529 "Empty string should return an empty collation key");
530 // bogus key returned here
531 key1Status
= U_ILLEGAL_ARGUMENT_ERROR
;
532 col
->getCollationKey(NULL
, 0, sortk1
, key1Status
);
533 doAssert(sortk1
.getByteArray(length
) == NULL
&& length
== 0,
534 "Error code should return bogus collation key");
536 key1Status
= U_ZERO_ERROR
;
537 logln("Use tertiary comparison level testing ....");
539 col
->getCollationKey(test1
, sortk1
, key1Status
);
540 doAssert((sortk1
.compareTo(col
->getCollationKey(test2
, sortk2
, key2Status
)))
541 == Collator::GREATER
,
542 "Result should be \"Abcda\" >>> \"abcda\"");
544 CollationKey
sortk3(sortk2
), sortkNew
, sortkEmpty
;
548 doAssert((sortk1
!= sortk2
), "The sort keys should be different");
549 doAssert((sortk1
.hashCode() != sortk2
.hashCode()), "sort key hashCode() failed");
550 doAssert((sortk2
== sortk3
), "The sort keys should be the same");
551 doAssert((sortk1
== sortkNew
), "The sort keys assignment failed");
552 doAssert((sortk1
.hashCode() == sortkNew
.hashCode()), "sort key hashCode() failed");
553 doAssert((sortkNew
!= sortk3
), "The sort keys should be different");
554 doAssert(sortk1
.compareTo(sortk3
) == Collator::GREATER
, "Result should be \"Abcda\" >>> \"abcda\"");
555 doAssert(sortk2
.compareTo(sortk3
) == Collator::EQUAL
, "Result should be \"abcda\" == \"abcda\"");
556 doAssert(sortkEmpty
.compareTo(sortk1
) == Collator::LESS
, "Result should be (empty key) <<< \"Abcda\"");
557 doAssert(sortk1
.compareTo(sortkEmpty
) == Collator::GREATER
, "Result should be \"Abcda\" >>> (empty key)");
558 doAssert(sortkEmpty
.compareTo(sortkEmpty
) == Collator::EQUAL
, "Result should be (empty key) == (empty key)");
559 doAssert(sortk1
.compareTo(sortk3
, success
) == UCOL_GREATER
, "Result should be \"Abcda\" >>> \"abcda\"");
560 doAssert(sortk2
.compareTo(sortk3
, success
) == UCOL_EQUAL
, "Result should be \"abcda\" == \"abcda\"");
561 doAssert(sortkEmpty
.compareTo(sortk1
, success
) == UCOL_LESS
, "Result should be (empty key) <<< \"Abcda\"");
562 doAssert(sortk1
.compareTo(sortkEmpty
, success
) == UCOL_GREATER
, "Result should be \"Abcda\" >>> (empty key)");
563 doAssert(sortkEmpty
.compareTo(sortkEmpty
, success
) == UCOL_EQUAL
, "Result should be (empty key) == (empty key)");
565 int32_t cnt1
, cnt2
, cnt3
, cnt4
;
567 const uint8_t* byteArray1
= sortk1
.getByteArray(cnt1
);
568 const uint8_t* byteArray2
= sortk2
.getByteArray(cnt2
);
571 this is a bad test since it is dependent on the version of uca data,
574 const char sortk2_compat[] = {
575 // this is a 1.8 sortkey
576 0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00
580 const uint8_t* byteArray3
= 0;
581 byteArray3
= sortk1
.getByteArray(cnt3
);
583 const uint8_t* byteArray4
= 0;
584 byteArray4
= sortk2
.getByteArray(cnt4
);
586 CollationKey
sortk4(byteArray1
, cnt1
), sortk5(byteArray2
, cnt2
);
587 CollationKey
sortk6(byteArray3
, cnt3
), sortk7(byteArray4
, cnt4
);
590 doAssert(memcmp(byteArray2, sortk2_compat, strlen(sortk2_compat)) == 0,
591 "Binary format for 'abcda' sortkey different!");
593 doAssert(sortk1
.compareTo(sortk4
) == Collator::EQUAL
, "CollationKey::toByteArray(sortk1) Failed.");
594 doAssert(sortk2
.compareTo(sortk5
) == Collator::EQUAL
, "CollationKey::toByteArray(sortk2) Failed.");
595 doAssert(sortk4
.compareTo(sortk5
) == Collator::GREATER
, "sortk4 >>> sortk5 Failed");
596 doAssert(sortk1
.compareTo(sortk6
) == Collator::EQUAL
, "CollationKey::getByteArray(sortk1) Failed.");
597 doAssert(sortk2
.compareTo(sortk7
) == Collator::EQUAL
, "CollationKey::getByteArray(sortk2) Failed.");
598 doAssert(sortk6
.compareTo(sortk7
) == Collator::GREATER
, "sortk6 >>> sortk7 Failed");
600 logln("Equality tests : ");
601 doAssert(sortk1
== sortk4
, "sortk1 == sortk4 Failed.");
602 doAssert(sortk2
== sortk5
, "sortk2 == sortk5 Failed.");
603 doAssert(sortk1
!= sortk5
, "sortk1 != sortk5 Failed.");
604 doAssert(sortk1
== sortk6
, "sortk1 == sortk6 Failed.");
605 doAssert(sortk2
== sortk7
, "sortk2 == sortk7 Failed.");
606 doAssert(sortk1
!= sortk7
, "sortk1 != sortk7 Failed.");
612 doAssert(sortk1
== sortk3
, "sortk1 = sortk3 assignment Failed.");
613 doAssert(sortk2
!= sortk3
, "sortk2 != sortk3 Failed.");
614 logln("testing sortkey ends...");
616 col
->setStrength(Collator::SECONDARY
);
617 doAssert(col
->getCollationKey(test1
, sortk1
, key1Status
).compareTo(
618 col
->getCollationKey(test2
, sortk2
, key2Status
))
620 "Result should be \"Abcda\" == \"abcda\"");
624 //----------------------------------------------------------------------------
625 // Tests the CollatorElementIterator class.
626 // ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!=
629 CollationAPITest::TestElemIter(/* char* par */)
631 logln("testing sortkey begins...");
633 UErrorCode success
= U_ZERO_ERROR
;
634 col
= Collator::createInstance(Locale::getEnglish(), success
);
635 if (U_FAILURE(success
))
637 errln("Default collation creation failed.");
641 UnicodeString
testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
642 UnicodeString
testString2("Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
643 logln("Constructors and comparison testing....");
644 CollationElementIterator
*iterator1
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString1
);
646 CharacterIterator
*chariter
=new StringCharacterIterator(testString1
);
647 CollationElementIterator
*coliter
=((RuleBasedCollator
*)col
)->createCollationElementIterator(*chariter
);
650 CollationElementIterator
*iterator2
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString1
);
651 CollationElementIterator
*iterator3
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString2
);
653 int32_t offset
= iterator1
->getOffset();
655 errln("Error in getOffset for collation element iterator\n");
658 iterator1
->setOffset(6, success
);
659 if (U_FAILURE(success
)) {
660 errln("Error in setOffset for collation element iterator\n");
663 iterator1
->setOffset(0, success
);
664 int32_t order1
, order2
, order3
;
665 doAssert((*iterator1
== *iterator2
), "The two iterators should be the same");
666 doAssert((*iterator1
!= *iterator3
), "The two iterators should be different");
668 doAssert((*coliter
== *iterator1
), "The two iterators should be the same");
669 doAssert((*coliter
== *iterator2
), "The two iterators should be the same");
670 doAssert((*coliter
!= *iterator3
), "The two iterators should be different");
672 order1
= iterator1
->next(success
);
673 if (U_FAILURE(success
))
675 errln("Somehow ran out of memory stepping through the iterator.");
679 doAssert((*iterator1
!= *iterator2
), "The first iterator advance failed");
680 order2
= iterator2
->getOffset();
681 doAssert((order1
!= order2
), "The order result should not be the same");
682 order2
= iterator2
->next(success
);
683 if (U_FAILURE(success
))
685 errln("Somehow ran out of memory stepping through the iterator.");
689 doAssert((*iterator1
== *iterator2
), "The second iterator advance failed");
690 doAssert((order1
== order2
), "The order result should be the same");
691 order3
= iterator3
->next(success
);
692 if (U_FAILURE(success
))
694 errln("Somehow ran out of memory stepping through the iterator.");
698 doAssert((CollationElementIterator::primaryOrder(order1
) ==
699 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be the same");
700 doAssert((CollationElementIterator::secondaryOrder(order1
) ==
701 CollationElementIterator::secondaryOrder(order3
)), "The secondary orders should be the same");
702 doAssert((CollationElementIterator::tertiaryOrder(order1
) ==
703 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be the same");
705 order1
= iterator1
->next(success
); order3
= iterator3
->next(success
);
706 if (U_FAILURE(success
))
708 errln("Somehow ran out of memory stepping through the iterator.");
712 doAssert((CollationElementIterator::primaryOrder(order1
) ==
713 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be identical");
714 doAssert((CollationElementIterator::tertiaryOrder(order1
) !=
715 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be different");
717 order1
= iterator1
->next(success
);
718 order3
= iterator3
->next(success
);
719 /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
721 doAssert((CollationElementIterator::secondaryOrder(order1) !=
722 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
724 doAssert((order1
!= CollationElementIterator::NULLORDER
), "Unexpected end of iterator reached");
726 iterator1
->reset(); iterator2
->reset(); iterator3
->reset();
727 order1
= iterator1
->next(success
);
728 if (U_FAILURE(success
))
730 errln("Somehow ran out of memory stepping through the iterator.");
734 doAssert((*iterator1
!= *iterator2
), "The first iterator advance failed");
736 order2
= iterator2
->next(success
);
737 if (U_FAILURE(success
))
739 errln("Somehow ran out of memory stepping through the iterator.");
743 doAssert((*iterator1
== *iterator2
), "The second iterator advance failed");
744 doAssert((order1
== order2
), "The order result should be the same");
746 order3
= iterator3
->next(success
);
747 if (U_FAILURE(success
))
749 errln("Somehow ran out of memory stepping through the iterator.");
753 doAssert((CollationElementIterator::primaryOrder(order1
) ==
754 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be the same");
755 doAssert((CollationElementIterator::secondaryOrder(order1
) ==
756 CollationElementIterator::secondaryOrder(order3
)), "The secondary orders should be the same");
757 doAssert((CollationElementIterator::tertiaryOrder(order1
) ==
758 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be the same");
760 order1
= iterator1
->next(success
); order2
= iterator2
->next(success
); order3
= iterator3
->next(success
);
761 if (U_FAILURE(success
))
763 errln("Somehow ran out of memory stepping through the iterator.");
767 doAssert((CollationElementIterator::primaryOrder(order1
) ==
768 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be identical");
769 doAssert((CollationElementIterator::tertiaryOrder(order1
) !=
770 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be different");
772 order1
= iterator1
->next(success
); order3
= iterator3
->next(success
);
773 if (U_FAILURE(success
))
775 errln("Somehow ran out of memory stepping through the iterator.");
779 /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
781 doAssert((CollationElementIterator::secondaryOrder(order1) !=
782 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
784 doAssert((order1
!= CollationElementIterator::NULLORDER
), "Unexpected end of iterator reached");
785 doAssert((*iterator2
!= *iterator3
), "The iterators should be different");
789 success
=U_UNSUPPORTED_ERROR
;
790 Collator
*colerror
=NULL
;
791 colerror
=Collator::createInstance(Locale::getEnglish(), success
);
792 if (colerror
!= 0 || success
== U_ZERO_ERROR
){
793 errln("Error: createInstance(UErrorCode != U_ZERO_ERROR) should just return and not create an instance\n");
795 int32_t position
=coliter
->previous(success
);
796 if(position
!= CollationElementIterator::NULLORDER
){
797 errln((UnicodeString
)"Expected NULLORDER got" + position
);
800 coliter
->setText(*chariter
, success
);
801 if(!U_FAILURE(success
)){
802 errln("Expeceted error");
804 iterator1
->setText((UnicodeString
)"hello there", success
);
805 if(!U_FAILURE(success
)){
806 errln("Expeceted error");
818 logln("testing CollationElementIterator ends...");
821 // Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules
823 CollationAPITest::TestOperators(/* char* par */)
825 UErrorCode success
= U_ZERO_ERROR
;
826 UnicodeString
ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
827 UnicodeString
ruleset2("< a, A < b, B < c, C < d, D, e, E");
828 RuleBasedCollator
*col1
= new RuleBasedCollator(ruleset1
, success
);
829 if (U_FAILURE(success
)) {
830 errln("RuleBasedCollator creation failed.");
833 success
= U_ZERO_ERROR
;
834 RuleBasedCollator
*col2
= new RuleBasedCollator(ruleset2
, success
);
835 if (U_FAILURE(success
)) {
836 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set.");
839 logln("The operator tests begin : ");
840 logln("testing operator==, operator!=, clone methods ...");
841 doAssert((*col1
!= *col2
), "The two different table collations compared equal");
843 doAssert((*col1
== *col2
), "Collator objects not equal after assignment (operator=)");
845 success
= U_ZERO_ERROR
;
846 Collator
*col3
= Collator::createInstance(Locale::getEnglish(), success
);
847 if (U_FAILURE(success
)) {
848 errln("Default collation creation failed.");
851 doAssert((*col1
!= *col3
), "The two different table collations compared equal");
852 Collator
* col4
= col1
->clone();
853 Collator
* col5
= col3
->clone();
854 doAssert((*col1
== *col4
), "Cloned collation objects not equal");
855 doAssert((*col3
!= *col4
), "Two different table collations compared equal");
856 doAssert((*col3
== *col5
), "Cloned collation objects not equal");
857 doAssert((*col4
!= *col5
), "Two cloned collations compared equal");
859 const UnicodeString
& defRules
= ((RuleBasedCollator
*)col3
)->getRules();
860 RuleBasedCollator
* col6
= new RuleBasedCollator(defRules
, success
);
861 if (U_FAILURE(success
)) {
862 errln("Creating default collation with rules failed.");
865 doAssert((((RuleBasedCollator
*)col3
)->getRules() == col6
->getRules()), "Default collator getRules failed");
867 success
= U_ZERO_ERROR
;
868 RuleBasedCollator
*col7
= new RuleBasedCollator(ruleset2
, Collator::TERTIARY
, success
);
869 if (U_FAILURE(success
)) {
870 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength.");
873 success
= U_ZERO_ERROR
;
874 RuleBasedCollator
*col8
= new RuleBasedCollator(ruleset2
, UCOL_OFF
, success
);
875 if (U_FAILURE(success
)) {
876 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with Normalizer::NO_OP.");
879 success
= U_ZERO_ERROR
;
880 RuleBasedCollator
*col9
= new RuleBasedCollator(ruleset2
, Collator::PRIMARY
, UCOL_ON
, success
);
881 if (U_FAILURE(success
)) {
882 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength and Normalizer::NO_OP.");
885 // doAssert((*col7 == *col8), "The two equal table collations compared different");
886 doAssert((*col7
!= *col9
), "The two different table collations compared equal");
887 doAssert((*col8
!= *col9
), "The two different table collations compared equal");
889 logln("operator tests ended.");
901 // test clone and copy
903 CollationAPITest::TestDuplicate(/* char* par */)
905 UErrorCode status
= U_ZERO_ERROR
;
906 Collator
*col1
= Collator::createInstance(Locale::getEnglish(), status
);
907 if (U_FAILURE(status
)) {
908 logln("Default collator creation failed.");
911 Collator
*col2
= col1
->clone();
912 doAssert((*col1
== *col2
), "Cloned object is not equal to the orginal");
913 UnicodeString
ruleset("< a, A < b, B < c, C < d, D, e, E");
914 RuleBasedCollator
*col3
= new RuleBasedCollator(ruleset
, status
);
915 doAssert((*col1
!= *col3
), "Cloned object is equal to some dummy");
916 *col3
= *((RuleBasedCollator
*)col1
);
917 doAssert((*col1
== *col3
), "Copied object is not equal to the orginal");
924 CollationAPITest::TestCompare(/* char* par */)
926 logln("The compare tests begin : ");
928 UErrorCode success
= U_ZERO_ERROR
;
929 col
= Collator::createInstance(Locale::getEnglish(), success
);
930 if (U_FAILURE(success
)) {
931 errln("Default collation creation failed.");
934 UnicodeString
test1("Abcda"), test2("abcda");
935 logln("Use tertiary comparison level testing ....");
937 doAssert((!col
->equals(test1
, test2
) ), "Result should be \"Abcda\" != \"abcda\"");
938 doAssert((col
->greater(test1
, test2
) ), "Result should be \"Abcda\" >>> \"abcda\"");
939 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" >>> \"abcda\"");
941 col
->setStrength(Collator::SECONDARY
);
942 logln("Use secondary comparison level testing ....");
944 doAssert((col
->equals(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
945 doAssert((!col
->greater(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
946 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
948 col
->setStrength(Collator::PRIMARY
);
949 logln("Use primary comparison level testing ....");
951 doAssert((col
->equals(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
952 doAssert((!col
->greater(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
953 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
955 // Test different APIs
956 const UChar
* t1
= test1
.getBuffer();
957 int32_t t1Len
= test1
.length();
958 const UChar
* t2
= test2
.getBuffer();
959 int32_t t2Len
= test2
.length();
961 doAssert((col
->compare(test1
, test2
) == Collator::EQUAL
), "Problem");
962 doAssert((col
->compare(test1
, test2
, success
) == UCOL_EQUAL
), "Problem");
963 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
) == Collator::EQUAL
), "Problem");
964 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
, success
) == UCOL_EQUAL
), "Problem");
965 doAssert((col
->compare(test1
, test2
, t1Len
) == Collator::EQUAL
), "Problem");
966 doAssert((col
->compare(test1
, test2
, t1Len
, success
) == UCOL_EQUAL
), "Problem");
968 col
->setAttribute(UCOL_STRENGTH
, UCOL_TERTIARY
, success
);
969 doAssert((col
->compare(test1
, test2
) == Collator::GREATER
), "Problem");
970 doAssert((col
->compare(test1
, test2
, success
) == UCOL_GREATER
), "Problem");
971 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
) == Collator::GREATER
), "Problem");
972 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
, success
) == UCOL_GREATER
), "Problem");
973 doAssert((col
->compare(test1
, test2
, t1Len
) == Collator::GREATER
), "Problem");
974 doAssert((col
->compare(test1
, test2
, t1Len
, success
) == UCOL_GREATER
), "Problem");
978 logln("The compare tests end.");
983 CollationAPITest::TestGetAll(/* char* par */)
986 const Locale
* list
= Collator::getAvailableLocales(count
);
987 for (int32_t i
= 0; i
< count
; ++i
) {
988 UnicodeString locName
, dispName
;
989 log("Locale name: ");
990 log(list
[i
].getName());
991 log(" , the display name is : ");
992 logln(list
[i
].getDisplayName(dispName
));
996 void CollationAPITest::TestSortKey()
998 UErrorCode status
= U_ZERO_ERROR
;
1000 this is supposed to open default date format, but later on it treats
1001 it like it is "en_US"
1002 - very bad if you try to run the tests on machine where default
1003 locale is NOT "en_US"
1005 Collator
*col
= Collator::createInstance(Locale::getEnglish(), status
);
1006 if (U_FAILURE(status
)) {
1007 errln("ERROR: Default collation creation failed.: %s\n", u_errorName(status
));
1011 if (col
->getStrength() != Collator::TERTIARY
)
1013 errln("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
1016 /* Need to use identical strength */
1017 col
->setAttribute(UCOL_STRENGTH
, UCOL_IDENTICAL
, status
);
1019 uint8_t key2compat
[] = {
1021 0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01,
1022 0x09, 0x01, 0x09, 0x01, 0x25, 0x01,
1023 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
1027 0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01,
1028 0x09, 0x01, 0x09, 0x01, 0x1C, 0x01,
1029 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
1033 0x19, 0x1B, 0x1D, 0x1F, 0x19,
1034 0x01, 0x09, 0x01, 0x09, 0x01,
1036 0x92, 0x93, 0x94, 0x95, 0x92,
1041 0x19, 0x1B, 0x1D, 0x1F, 0x19,
1042 0x01, 0x0A, 0x01, 0x0A, 0x01,
1043 0x92, 0x93, 0x94, 0x95, 0x92,
1048 UChar test1
[6] = {0x41, 0x62, 0x63, 0x64, 0x61, 0},
1049 test2
[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0},
1050 test3
[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0};
1052 uint8_t sortkey1
[64];
1053 uint8_t sortkey2
[64];
1054 uint8_t sortkey3
[64];
1056 logln("Use tertiary comparison level testing ....\n");
1059 col
->getCollationKey(test1
, u_strlen(test1
), key1
, status
);
1062 col
->getCollationKey(test2
, u_strlen(test2
), key2
, status
);
1065 col
->getCollationKey(test3
, u_strlen(test3
), key3
, status
);
1067 doAssert(key1
.compareTo(key2
) == Collator::GREATER
,
1068 "Result should be \"Abcda\" > \"abcda\"");
1069 doAssert(key2
.compareTo(key1
) == Collator::LESS
,
1070 "Result should be \"abcda\" < \"Abcda\"");
1071 doAssert(key2
.compareTo(key3
) == Collator::EQUAL
,
1072 "Result should be \"abcda\" == \"abcda\"");
1074 int32_t keylength
= 0;
1075 doAssert(strcmp((const char *)(key2
.getByteArray(keylength
)),
1076 (const char *)key2compat
) == 0,
1077 "Binary format for 'abcda' sortkey different!");
1079 col
->getSortKey(test1
, sortkey1
, 64);
1080 col
->getSortKey(test2
, sortkey2
, 64);
1081 col
->getSortKey(test3
, sortkey3
, 64);
1083 const uint8_t *tempkey
= key1
.getByteArray(keylength
);
1084 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1085 "Test1 string should have the same collation key and sort key");
1086 tempkey
= key2
.getByteArray(keylength
);
1087 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1088 "Test2 string should have the same collation key and sort key");
1089 tempkey
= key3
.getByteArray(keylength
);
1090 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1091 "Test3 string should have the same collation key and sort key");
1093 col
->getSortKey(test1
, 5, sortkey1
, 64);
1094 col
->getSortKey(test2
, 5, sortkey2
, 64);
1095 col
->getSortKey(test3
, 5, sortkey3
, 64);
1097 tempkey
= key1
.getByteArray(keylength
);
1098 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1099 "Test1 string should have the same collation key and sort key");
1100 tempkey
= key2
.getByteArray(keylength
);
1101 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1102 "Test2 string should have the same collation key and sort key");
1103 tempkey
= key3
.getByteArray(keylength
);
1104 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1105 "Test3 string should have the same collation key and sort key");
1107 UnicodeString
strtest1(test1
);
1108 col
->getSortKey(strtest1
, sortkey1
, 64);
1109 UnicodeString
strtest2(test2
);
1110 col
->getSortKey(strtest2
, sortkey2
, 64);
1111 UnicodeString
strtest3(test3
);
1112 col
->getSortKey(strtest3
, sortkey3
, 64);
1114 tempkey
= key1
.getByteArray(keylength
);
1115 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1116 "Test1 string should have the same collation key and sort key");
1117 tempkey
= key2
.getByteArray(keylength
);
1118 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1119 "Test2 string should have the same collation key and sort key");
1120 tempkey
= key3
.getByteArray(keylength
);
1121 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1122 "Test3 string should have the same collation key and sort key");
1124 logln("Use secondary comparision level testing ...\n");
1125 col
->setStrength(Collator::SECONDARY
);
1127 col
->getCollationKey(test1
, u_strlen(test1
), key1
, status
);
1128 col
->getCollationKey(test2
, u_strlen(test2
), key2
, status
);
1129 col
->getCollationKey(test3
, u_strlen(test3
), key3
, status
);
1131 doAssert(key1
.compareTo(key2
) == Collator::EQUAL
,
1132 "Result should be \"Abcda\" == \"abcda\"");
1133 doAssert(key2
.compareTo(key3
) == Collator::EQUAL
,
1134 "Result should be \"abcda\" == \"abcda\"");
1136 tempkey
= key2
.getByteArray(keylength
);
1137 doAssert(memcmp(tempkey
, key2compat
, keylength
- 1) == 0,
1138 "Binary format for 'abcda' sortkey different!");
1140 col
->getSortKey(test1
, sortkey1
, 64);
1141 col
->getSortKey(test2
, sortkey2
, 64);
1142 col
->getSortKey(test3
, sortkey3
, 64);
1144 tempkey
= key1
.getByteArray(keylength
);
1145 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1146 "Test1 string should have the same collation key and sort key");
1147 tempkey
= key2
.getByteArray(keylength
);
1148 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1149 "Test2 string should have the same collation key and sort key");
1150 tempkey
= key3
.getByteArray(keylength
);
1151 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1152 "Test3 string should have the same collation key and sort key");
1154 col
->getSortKey(test1
, 5, sortkey1
, 64);
1155 col
->getSortKey(test2
, 5, sortkey2
, 64);
1156 col
->getSortKey(test3
, 5, sortkey3
, 64);
1158 tempkey
= key1
.getByteArray(keylength
);
1159 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1160 "Test1 string should have the same collation key and sort key");
1161 tempkey
= key2
.getByteArray(keylength
);
1162 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1163 "Test2 string should have the same collation key and sort key");
1164 tempkey
= key3
.getByteArray(keylength
);
1165 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1166 "Test3 string should have the same collation key and sort key");
1168 col
->getSortKey(strtest1
, sortkey1
, 64);
1169 col
->getSortKey(strtest2
, sortkey2
, 64);
1170 col
->getSortKey(strtest3
, sortkey3
, 64);
1172 tempkey
= key1
.getByteArray(keylength
);
1173 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1174 "Test1 string should have the same collation key and sort key");
1175 tempkey
= key2
.getByteArray(keylength
);
1176 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1177 "Test2 string should have the same collation key and sort key");
1178 tempkey
= key3
.getByteArray(keylength
);
1179 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1180 "Test3 string should have the same collation key and sort key");
1182 logln("testing sortkey ends...");
1186 void CollationAPITest::TestMaxExpansion()
1188 UErrorCode status
= U_ZERO_ERROR
;
1190 UChar32 unassigned
= 0xEFFFD;
1191 uint32_t sorder
= 0;
1192 uint32_t temporder
= 0;
1194 UnicodeString
rule("&a < ab < c/aba < d < z < ch");
1195 RuleBasedCollator
coll(rule
, status
);
1196 if(U_FAILURE(status
)) {
1197 errln("Collator creation failed with error %s", u_errorName(status
));
1200 UnicodeString
str(ch
);
1201 CollationElementIterator
*iter
=
1202 coll
.createCollationElementIterator(str
);
1204 while (ch
< 0xFFFF && U_SUCCESS(status
)) {
1211 str
.setCharAt(0, ch
);
1212 iter
->setText(str
, status
);
1213 order
= iter
->previous(status
);
1215 /* thai management */
1217 order
= iter
->previous(status
);
1219 while (U_SUCCESS(status
) && iter
->previous(status
) != UCOL_NULLORDER
) {
1223 size
= coll
.getMaxExpansion(order
);
1224 if (U_FAILURE(status
) || size
< count
) {
1225 errln("Failure at codepoint %d, maximum expansion count < %d\n",
1230 /* testing for exact max expansion */
1235 str
.setCharAt(0, ch
);
1236 iter
->setText(str
, status
);
1237 order
= iter
->previous(status
);
1238 size
= coll
.getMaxExpansion(order
);
1239 if (U_FAILURE(status
) || size
!= 1) {
1240 errln("Failure at codepoint %d, maximum expansion count < %d\n",
1248 iter
->setText(str
, status
);
1249 temporder
= iter
->previous(status
);
1251 if (U_FAILURE(status
) || coll
.getMaxExpansion(temporder
) != 3) {
1252 errln("Failure at codepoint %d, maximum expansion count != %d\n",
1258 iter
->setText(str
, status
);
1259 temporder
= iter
->previous(status
);
1261 if (U_FAILURE(status
) || coll
.getMaxExpansion(temporder
) != 1) {
1262 errln("Failure at codepoint %d, maximum expansion count != %d\n",
1266 str
.setTo(unassigned
);
1267 iter
->setText(str
, status
);
1268 sorder
= iter
->previous(status
);
1270 if (U_FAILURE(status
) || coll
.getMaxExpansion(sorder
) != 2) {
1271 errln("Failure at supplementary codepoints, maximum expansion count < %d\n",
1278 iter
->setText(str
, status
);
1279 temporder
= iter
->previous(status
);
1280 if (U_FAILURE(status
) || coll
.getMaxExpansion(temporder
) > 3) {
1281 errln("Failure at codepoint %d, maximum expansion count > %d\n",
1287 /* testing special jamo &a<\u1160 */
1288 rule
= CharsToUnicodeString("\\u0026\\u0071\\u003c\\u1165\\u002f\\u0071\\u0071\\u0071\\u0071");
1290 RuleBasedCollator
jamocoll(rule
, status
);
1291 iter
= jamocoll
.createCollationElementIterator(str
);
1292 temporder
= iter
->previous(status
);
1293 if (U_FAILURE(status
) || iter
->getMaxExpansion(temporder
) != 6) {
1294 errln("Failure at codepoint %d, maximum expansion count > %d\n",
1301 void CollationAPITest::TestDisplayName()
1303 UErrorCode error
= U_ZERO_ERROR
;
1304 Collator
*coll
= Collator::createInstance("en_US", error
);
1305 if (U_FAILURE(error
)) {
1306 errln("Failure creating english collator");
1310 UnicodeString result
;
1311 coll
->getDisplayName(Locale::getCanadaFrench(), result
);
1312 Locale::getCanadaFrench().getDisplayName(name
);
1313 if (result
.compare(name
)) {
1314 errln("Failure getting the correct name for locale en_US");
1317 coll
->getDisplayName(Locale::getSimplifiedChinese(), result
);
1318 Locale::getSimplifiedChinese().getDisplayName(name
);
1319 if (result
.compare(name
)) {
1320 errln("Failure getting the correct name for locale zh_SG");
1325 void CollationAPITest::TestAttribute()
1327 UErrorCode error
= U_ZERO_ERROR
;
1328 Collator
*coll
= Collator::createInstance(error
);
1330 if (U_FAILURE(error
)) {
1331 errln("Creation of default collator failed");
1335 coll
->setAttribute(UCOL_FRENCH_COLLATION
, UCOL_OFF
, error
);
1336 if (coll
->getAttribute(UCOL_FRENCH_COLLATION
, error
) != UCOL_OFF
||
1338 errln("Setting and retrieving of the french collation failed");
1341 coll
->setAttribute(UCOL_FRENCH_COLLATION
, UCOL_ON
, error
);
1342 if (coll
->getAttribute(UCOL_FRENCH_COLLATION
, error
) != UCOL_ON
||
1344 errln("Setting and retrieving of the french collation failed");
1347 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, error
);
1348 if (coll
->getAttribute(UCOL_ALTERNATE_HANDLING
, error
) != UCOL_SHIFTED
||
1350 errln("Setting and retrieving of the alternate handling failed");
1353 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, error
);
1354 if (coll
->getAttribute(UCOL_ALTERNATE_HANDLING
, error
) != UCOL_NON_IGNORABLE
||
1356 errln("Setting and retrieving of the alternate handling failed");
1359 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, error
);
1360 if (coll
->getAttribute(UCOL_CASE_FIRST
, error
) != UCOL_LOWER_FIRST
||
1362 errln("Setting and retrieving of the case first attribute failed");
1365 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, error
);
1366 if (coll
->getAttribute(UCOL_CASE_FIRST
, error
) != UCOL_UPPER_FIRST
||
1368 errln("Setting and retrieving of the case first attribute failed");
1371 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_ON
, error
);
1372 if (coll
->getAttribute(UCOL_CASE_LEVEL
, error
) != UCOL_ON
||
1374 errln("Setting and retrieving of the case level attribute failed");
1377 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, error
);
1378 if (coll
->getAttribute(UCOL_CASE_LEVEL
, error
) != UCOL_OFF
||
1380 errln("Setting and retrieving of the case level attribute failed");
1383 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, error
);
1384 if (coll
->getAttribute(UCOL_NORMALIZATION_MODE
, error
) != UCOL_ON
||
1386 errln("Setting and retrieving of the normalization on/off attribute failed");
1389 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_OFF
, error
);
1390 if (coll
->getAttribute(UCOL_NORMALIZATION_MODE
, error
) != UCOL_OFF
||
1392 errln("Setting and retrieving of the normalization on/off attribute failed");
1395 coll
->setAttribute(UCOL_STRENGTH
, UCOL_PRIMARY
, error
);
1396 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_PRIMARY
||
1398 errln("Setting and retrieving of the collation strength failed");
1401 coll
->setAttribute(UCOL_STRENGTH
, UCOL_SECONDARY
, error
);
1402 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_SECONDARY
||
1404 errln("Setting and retrieving of the collation strength failed");
1407 coll
->setAttribute(UCOL_STRENGTH
, UCOL_TERTIARY
, error
);
1408 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_TERTIARY
||
1410 errln("Setting and retrieving of the collation strength failed");
1413 coll
->setAttribute(UCOL_STRENGTH
, UCOL_QUATERNARY
, error
);
1414 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_QUATERNARY
||
1416 errln("Setting and retrieving of the collation strength failed");
1419 coll
->setAttribute(UCOL_STRENGTH
, UCOL_IDENTICAL
, error
);
1420 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_IDENTICAL
||
1422 errln("Setting and retrieving of the collation strength failed");
1428 void CollationAPITest::TestVariableTopSetting() {
1429 UErrorCode status
= U_ZERO_ERROR
;
1431 UChar vt
[256] = { 0 };
1433 Collator
*coll
= Collator::createInstance(status
);
1434 if(U_FAILURE(status
)) {
1436 errln("Collator creation failed with error %s", u_errorName(status
));
1440 uint32_t oldVarTop
= coll
->getVariableTop(status
);
1444 uint32_t newVarTop
= coll
->setVariableTop(vt
, 1, status
);
1446 if((newVarTop
& 0xFFFF0000) != (coll
->getVariableTop(status
) & 0xFFFF0000)) {
1447 errln("Didn't set vartop properly\n");
1450 coll
->setVariableTop(oldVarTop
, status
);
1452 uint32_t newerVarTop
= coll
->setVariableTop(UnicodeString(vt
, 1), status
);
1454 if((newVarTop
& 0xFFFF0000) != (newerVarTop
& 0xFFFF0000)) {
1455 errln("Didn't set vartop properly from UnicodeString!\n");
1462 void CollationAPITest::TestGetLocale() {
1463 UErrorCode status
= U_ZERO_ERROR
;
1464 const char *rules
= "&a<x<y<z";
1465 UChar rlz
[256] = {0};
1467 Collator
*coll
= NULL
;
1472 static const struct {
1473 const char* requestedLocale
;
1474 const char* validLocale
;
1475 const char* actualLocale
;
1477 { "sr_YU", "sr_YU", "root" },
1478 { "sh_YU", "sh_YU", "sh" },
1479 { "en_US_CALIFORNIA", "en_US", "root" },
1480 { "fr_FR_NONEXISTANT", "fr_FR", "fr" }
1483 u_unescape(rules
, rlz
, 256);
1485 /* test opening collators for different locales */
1486 for(i
= 0; i
<(int32_t)(sizeof(testStruct
)/sizeof(testStruct
[0])); i
++) {
1487 status
= U_ZERO_ERROR
;
1488 coll
= Collator::createInstance(testStruct
[i
].requestedLocale
, status
);
1489 if(U_FAILURE(status
)) {
1490 log("Failed to open collator for %s with %s\n", testStruct
[i
].requestedLocale
, u_errorName(status
));
1494 locale
= coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
);
1495 if(locale
!= testStruct
[i
].requestedLocale
) {
1496 log("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].requestedLocale
, locale
.getName());
1498 locale
= coll
->getLocale(ULOC_VALID_LOCALE
, status
);
1499 if(locale
!= testStruct
[i
].validLocale
) {
1500 log("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].validLocale
, locale
.getName());
1502 locale
= coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
);
1503 if(locale
!= testStruct
[i
].actualLocale
) {
1504 log("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].actualLocale
, locale
.getName());
1509 /* completely non-existant locale for collator should get a default collator */
1511 Collator
*defaultColl
= Collator::createInstance((const Locale
)NULL
, status
);
1512 coll
= Collator::createInstance("blahaha", status
);
1513 if(U_FAILURE(status
)) {
1514 log("Failed to open collator with %s\n", u_errorName(status
));
1519 if(coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
) != "blahaha") {
1520 log("Nonexisting locale didn't preserve the requested locale\n");
1522 if(coll
->getLocale(ULOC_VALID_LOCALE
, status
) !=
1523 defaultColl
->getLocale(ULOC_VALID_LOCALE
, status
)) {
1524 log("Valid locale for nonexisting locale locale collator differs "
1525 "from valid locale for default collator\n");
1527 if(coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
) !=
1528 defaultColl
->getLocale(ULOC_ACTUAL_LOCALE
, status
)) {
1529 log("Actual locale for nonexisting locale locale collator differs "
1530 "from actual locale for default collator\n");
1538 /* collator instantiated from rules should have all three locales NULL */
1539 coll
= new RuleBasedCollator(rlz
, status
);
1540 locale
= coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
);
1541 if(!locale
.isBogus()) {
1542 log("For collator instantiated from rules, requested locale %s is not bogus\n", locale
.getName());
1544 locale
= coll
->getLocale(ULOC_VALID_LOCALE
, status
);
1545 if(!locale
.isBogus()) {
1546 log("For collator instantiated from rules, valid locale %s is not bogus\n", locale
.getName());
1548 locale
= coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
);
1549 if(!locale
.isBogus()) {
1550 log("For collator instantiated from rules, actual locale %s is not bogus\n", locale
.getName());
1556 const char *original
;
1563 static int U_CALLCONV
1564 compare_teststruct(const void *string1
, const void *string2
) {
1565 return(strcmp((const char *)((struct teststruct
*)string1
)->key
, (const char *)((struct teststruct
*)string2
)->key
));
1570 void CollationAPITest::TestBounds(void) {
1571 UErrorCode status
= U_ZERO_ERROR
;
1573 Collator
*coll
= Collator::createInstance(Locale("sh"), status
);
1574 if(U_FAILURE(status
)) {
1576 errln("Collator creation failed with %s", u_errorName(status
));
1580 uint8_t sortkey
[512], lower
[512], upper
[512];
1583 const char *test
[] = {
1587 "j\\u00F6hn sm\\u00EFth",
1588 "J\\u00F6hn Sm\\u00EFth",
1589 "J\\u00D6HN SM\\u00CFTH",
1594 static struct teststruct tests
[] = {
1595 {"\\u010CAKI MIHALJ", {0}},
1596 {"\\u010CAKI MIHALJ", {0}},
1597 {"\\u010CAKI PIRO\\u0160KA", {0}},
1598 {"\\u010CABAI ANDRIJA", {0}},
1599 {"\\u010CABAI LAJO\\u0160", {0}},
1600 {"\\u010CABAI MARIJA", {0}},
1601 {"\\u010CABAI STEVAN", {0}},
1602 {"\\u010CABAI STEVAN", {0}},
1603 {"\\u010CABARKAPA BRANKO", {0}},
1604 {"\\u010CABARKAPA MILENKO", {0}},
1605 {"\\u010CABARKAPA MIROSLAV", {0}},
1606 {"\\u010CABARKAPA SIMO", {0}},
1607 {"\\u010CABARKAPA STANKO", {0}},
1608 {"\\u010CABARKAPA TAMARA", {0}},
1609 {"\\u010CABARKAPA TOMA\\u0160", {0}},
1610 {"\\u010CABDARI\\u0106 NIKOLA", {0}},
1611 {"\\u010CABDARI\\u0106 ZORICA", {0}},
1612 {"\\u010CABI NANDOR", {0}},
1613 {"\\u010CABOVI\\u0106 MILAN", {0}},
1614 {"\\u010CABRADI AGNEZIJA", {0}},
1615 {"\\u010CABRADI IVAN", {0}},
1616 {"\\u010CABRADI JELENA", {0}},
1617 {"\\u010CABRADI LJUBICA", {0}},
1618 {"\\u010CABRADI STEVAN", {0}},
1619 {"\\u010CABRDA MARTIN", {0}},
1620 {"\\u010CABRILO BOGDAN", {0}},
1621 {"\\u010CABRILO BRANISLAV", {0}},
1622 {"\\u010CABRILO LAZAR", {0}},
1623 {"\\u010CABRILO LJUBICA", {0}},
1624 {"\\u010CABRILO SPASOJA", {0}},
1625 {"\\u010CADE\\u0160 ZDENKA", {0}},
1626 {"\\u010CADESKI BLAGOJE", {0}},
1627 {"\\u010CADOVSKI VLADIMIR", {0}},
1628 {"\\u010CAGLJEVI\\u0106 TOMA", {0}},
1629 {"\\u010CAGOROVI\\u0106 VLADIMIR", {0}},
1630 {"\\u010CAJA VANKA", {0}},
1631 {"\\u010CAJI\\u0106 BOGOLJUB", {0}},
1632 {"\\u010CAJI\\u0106 BORISLAV", {0}},
1633 {"\\u010CAJI\\u0106 RADOSLAV", {0}},
1634 {"\\u010CAK\\u0160IRAN MILADIN", {0}},
1635 {"\\u010CAKAN EUGEN", {0}},
1636 {"\\u010CAKAN EVGENIJE", {0}},
1637 {"\\u010CAKAN IVAN", {0}},
1638 {"\\u010CAKAN JULIJAN", {0}},
1639 {"\\u010CAKAN MIHAJLO", {0}},
1640 {"\\u010CAKAN STEVAN", {0}},
1641 {"\\u010CAKAN VLADIMIR", {0}},
1642 {"\\u010CAKAN VLADIMIR", {0}},
1643 {"\\u010CAKAN VLADIMIR", {0}},
1644 {"\\u010CAKARA ANA", {0}},
1645 {"\\u010CAKAREVI\\u0106 MOMIR", {0}},
1646 {"\\u010CAKAREVI\\u0106 NEDELJKO", {0}},
1647 {"\\u010CAKI \\u0160ANDOR", {0}},
1648 {"\\u010CAKI AMALIJA", {0}},
1649 {"\\u010CAKI ANDRA\\u0160", {0}},
1650 {"\\u010CAKI LADISLAV", {0}},
1651 {"\\u010CAKI LAJO\\u0160", {0}},
1652 {"\\u010CAKI LASLO", {0}}
1657 int32_t i
= 0, j
= 0, k
= 0, buffSize
= 0, skSize
= 0, lowerSize
= 0, upperSize
= 0;
1658 int32_t arraySize
= sizeof(tests
)/sizeof(tests
[0]);
1660 for(i
= 0; i
<arraySize
; i
++) {
1661 buffSize
= u_unescape(tests
[i
].original
, buffer
, 512);
1662 skSize
= coll
->getSortKey(buffer
, buffSize
, tests
[i
].key
, 512);
1665 qsort(tests
, arraySize
, sizeof(struct teststruct
), compare_teststruct
);
1667 for(i
= 0; i
< arraySize
-1; i
++) {
1668 for(j
= i
+1; j
< arraySize
; j
++) {
1669 lowerSize
= coll
->getBound(tests
[i
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, status
);
1670 upperSize
= coll
->getBound(tests
[j
].key
, -1, UCOL_BOUND_UPPER
, 1, upper
, 512, status
);
1671 for(k
= i
; k
<= j
; k
++) {
1672 if(strcmp((const char *)lower
, (const char *)tests
[k
].key
) > 0) {
1673 errln("Problem with lower! j = %i (%s vs %s)", k
, tests
[k
].original
, tests
[i
].original
);
1675 if(strcmp((const char *)upper
, (const char *)tests
[k
].key
) <= 0) {
1676 errln("Problem with upper! j = %i (%s vs %s)", k
, tests
[k
].original
, tests
[j
].original
);
1683 for(i
= 0; i
<(int32_t)(sizeof(test
)/sizeof(test
[0])); i
++) {
1684 buffSize
= u_unescape(test
[i
], buffer
, 512);
1685 skSize
= coll
->getSortKey(buffer
, buffSize
, sortkey
, 512);
1686 lowerSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1687 upperSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1688 for(j
= i
+1; j
<(int32_t)(sizeof(test
)/sizeof(test
[0])); j
++) {
1689 buffSize
= u_unescape(test
[j
], buffer
, 512);
1690 skSize
= coll
->getSortKey(buffer
, buffSize
, sortkey
, 512);
1691 if(strcmp((const char *)lower
, (const char *)sortkey
) > 0) {
1692 errln("Problem with lower! i = %i, j = %i (%s vs %s)", i
, j
, test
[i
], test
[j
]);
1694 if(strcmp((const char *)upper
, (const char *)sortkey
) <= 0) {
1695 errln("Problem with upper! i = %i, j = %i (%s vs %s)", i
, j
, test
[i
], test
[j
]);
1703 void CollationAPITest::TestGetTailoredSet()
1707 const char *tests
[20];
1710 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1711 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1714 uint32_t i
= 0, j
= 0;
1715 UErrorCode status
= U_ZERO_ERROR
;
1717 RuleBasedCollator
*coll
= NULL
;
1719 UnicodeSet
*set
= NULL
;
1721 for(i
= 0; i
< sizeof(setTest
)/sizeof(setTest
[0]); i
++) {
1722 buff
= UnicodeString(setTest
[i
].rules
, "").unescape();
1723 coll
= new RuleBasedCollator(buff
, status
);
1724 if(U_SUCCESS(status
)) {
1725 set
= coll
->getTailoredSet(status
);
1726 if(set
->size() != setTest
[i
].testsize
) {
1727 errln("Tailored set size different (%d) than expected (%d)", set
->size(), setTest
[i
].testsize
);
1729 for(j
= 0; j
< (uint32_t)setTest
[i
].testsize
; j
++) {
1730 buff
= UnicodeString(setTest
[i
].tests
[j
], "").unescape();
1731 if(!set
->contains(buff
)) {
1732 errln("Tailored set doesn't contain %s... It should", setTest
[i
].tests
[j
]);
1737 errln("Couldn't open collator with rules %s\n", setTest
[i
].rules
);
1743 void CollationAPITest::TestUClassID()
1745 char id
= *((char *)RuleBasedCollator::getStaticClassID());
1747 errln("Static class id for RuleBasedCollator should be 0");
1749 UErrorCode status
= U_ZERO_ERROR
;
1750 RuleBasedCollator
*coll
1751 = (RuleBasedCollator
*)Collator::createInstance(status
);
1752 if(U_FAILURE(status
)) {
1754 errln("Collator creation failed with %s", u_errorName(status
));
1757 id
= *((char *)coll
->getDynamicClassID());
1759 errln("Dynamic class id for RuleBasedCollator should be 0");
1761 id
= *((char *)CollationKey::getStaticClassID());
1763 errln("Static class id for CollationKey should be 0");
1765 CollationKey
*key
= new CollationKey();
1766 id
= *((char *)key
->getDynamicClassID());
1768 errln("Dynamic class id for CollationKey should be 0");
1770 id
= *((char *)CollationElementIterator::getStaticClassID());
1772 errln("Static class id for CollationElementIterator should be 0");
1774 UnicodeString
str("testing");
1775 CollationElementIterator
*iter
= coll
->createCollationElementIterator(str
);
1776 id
= *((char *)iter
->getDynamicClassID());
1778 errln("Dynamic class id for CollationElementIterator should be 0");
1785 class TestCollator
: public Collator
1788 virtual Collator
* clone(void) const;
1790 // dang, markus says we can't use 'using' in ICU. I hate doing this for
1791 // deprecated methods...
1793 // using Collator::compare;
1795 virtual EComparisonResult
compare(const UnicodeString
& source
,
1796 const UnicodeString
& target
) const
1798 return Collator::compare(source
, target
);
1801 virtual EComparisonResult
compare(const UnicodeString
& source
,
1802 const UnicodeString
& target
,
1803 int32_t length
) const
1805 return Collator::compare(source
, target
, length
);
1808 virtual EComparisonResult
compare(const UChar
* source
,
1809 int32_t sourceLength
,
1810 const UChar
* target
,
1811 int32_t targetLength
) const
1813 return Collator::compare(source
, sourceLength
, target
, targetLength
);
1817 virtual UCollationResult
compare(const UnicodeString
& source
,
1818 const UnicodeString
& target
,
1819 UErrorCode
& status
) const;
1820 virtual UCollationResult
compare(const UnicodeString
& source
,
1821 const UnicodeString
& target
,
1823 UErrorCode
& status
) const;
1824 virtual UCollationResult
compare(const UChar
* source
,
1825 int32_t sourceLength
,
1826 const UChar
* target
,
1827 int32_t targetLength
,
1828 UErrorCode
& status
) const;
1829 virtual CollationKey
& getCollationKey(const UnicodeString
& source
,
1831 UErrorCode
& status
) const;
1832 virtual CollationKey
& getCollationKey(const UChar
*source
,
1833 int32_t sourceLength
,
1835 UErrorCode
& status
) const;
1836 virtual int32_t hashCode(void) const;
1837 virtual const Locale
getLocale(ULocDataLocaleType type
,
1838 UErrorCode
& status
) const;
1839 virtual ECollationStrength
getStrength(void) const;
1840 virtual void setStrength(ECollationStrength newStrength
);
1841 virtual UClassID
getDynamicClassID(void) const;
1842 virtual void getVersion(UVersionInfo info
) const;
1843 virtual void setAttribute(UColAttribute attr
, UColAttributeValue value
,
1844 UErrorCode
&status
);
1845 virtual UColAttributeValue
getAttribute(UColAttribute attr
,
1846 UErrorCode
&status
);
1847 virtual uint32_t setVariableTop(const UChar
*varTop
, int32_t len
,
1848 UErrorCode
&status
);
1849 virtual uint32_t setVariableTop(const UnicodeString varTop
,
1850 UErrorCode
&status
);
1851 virtual void setVariableTop(const uint32_t varTop
, UErrorCode
&status
);
1852 virtual uint32_t getVariableTop(UErrorCode
&status
) const;
1853 virtual Collator
* safeClone(void);
1854 virtual int32_t getSortKey(const UnicodeString
& source
,
1856 int32_t resultLength
) const;
1857 virtual int32_t getSortKey(const UChar
*source
, int32_t sourceLength
,
1858 uint8_t*result
, int32_t resultLength
) const;
1859 virtual UnicodeSet
*getTailoredSet(UErrorCode
&status
) const;
1860 virtual UBool
operator!=(const Collator
& other
) const;
1861 virtual void setLocales(const Locale
& requestedLocale
, const Locale
& validLocale
);
1862 TestCollator() : Collator() {};
1863 TestCollator(UCollationStrength collationStrength
,
1864 UNormalizationMode decompositionMode
) : Collator(collationStrength
, decompositionMode
) {};
1867 inline UBool
TestCollator::operator!=(const Collator
& other
) const {
1868 return Collator::operator!=(other
);
1871 #define returnEComparisonResult(data) \
1872 if (data < 0) return Collator::LESS;\
1873 if (data > 0) return Collator::GREATER;\
1874 return Collator::EQUAL;
1876 Collator
* TestCollator::clone() const
1878 return new TestCollator();
1881 UCollationResult
TestCollator::compare(const UnicodeString
& source
,
1882 const UnicodeString
& target
,
1883 UErrorCode
& status
) const
1885 if(U_SUCCESS(status
)) {
1886 return UCollationResult(source
.compare(target
));
1892 UCollationResult
TestCollator::compare(const UnicodeString
& source
,
1893 const UnicodeString
& target
,
1895 UErrorCode
& status
) const
1897 if(U_SUCCESS(status
)) {
1898 return UCollationResult(source
.compare(0, length
, target
));
1904 UCollationResult
TestCollator::compare(const UChar
* source
,
1905 int32_t sourceLength
,
1906 const UChar
* target
,
1907 int32_t targetLength
,
1908 UErrorCode
& status
) const
1910 UnicodeString
s(source
, sourceLength
);
1911 UnicodeString
t(target
, targetLength
);
1912 return compare(s
, t
, status
);
1915 CollationKey
& TestCollator::getCollationKey(const UnicodeString
& source
,
1917 UErrorCode
& status
) const
1921 length
= source
.extract(temp
, length
, NULL
, status
);
1923 CollationKey
tempkey((uint8_t*)temp
, length
);
1928 CollationKey
& TestCollator::getCollationKey(const UChar
*source
,
1929 int32_t sourceLength
,
1931 UErrorCode
& status
) const
1933 //s tack allocation used since collationkey does not keep the unicodestring
1934 UnicodeString
str(source
, sourceLength
);
1935 return getCollationKey(str
, key
, status
);
1938 int32_t TestCollator::getSortKey(const UnicodeString
& source
, uint8_t* result
,
1939 int32_t resultLength
) const
1941 UErrorCode status
= U_ZERO_ERROR
;
1942 int32_t length
= source
.extract((char *)result
, resultLength
, NULL
,
1948 int32_t TestCollator::getSortKey(const UChar
*source
, int32_t sourceLength
,
1949 uint8_t*result
, int32_t resultLength
) const
1951 UnicodeString
str(source
, sourceLength
);
1952 return getSortKey(str
, result
, resultLength
);
1955 int32_t TestCollator::hashCode() const
1960 const Locale
TestCollator::getLocale(ULocDataLocaleType type
,
1961 UErrorCode
& status
) const
1963 // api not used, this is to make the compiler happy
1964 if (U_FAILURE(status
)) {
1965 type
= ULOC_DATA_LOCALE_TYPE_LIMIT
;
1970 Collator::ECollationStrength
TestCollator::getStrength() const
1975 void TestCollator::setStrength(Collator::ECollationStrength newStrength
)
1977 // api not used, this is to make the compiler happy
1978 newStrength
= TERTIARY
;
1981 UClassID
TestCollator::getDynamicClassID(void) const
1986 void TestCollator::getVersion(UVersionInfo info
) const
1988 // api not used, this is to make the compiler happy
1989 memset(info
, 0, U_MAX_VERSION_LENGTH
);
1992 void TestCollator::setAttribute(UColAttribute attr
, UColAttributeValue value
,
1995 // api not used, this is to make the compiler happy
1996 if (U_FAILURE(status
)) {
1997 attr
= UCOL_ATTRIBUTE_COUNT
;
2002 UColAttributeValue
TestCollator::getAttribute(UColAttribute attr
,
2005 // api not used, this is to make the compiler happy
2006 if (U_FAILURE(status
) || attr
== UCOL_ATTRIBUTE_COUNT
) {
2009 return UCOL_DEFAULT
;
2012 uint32_t TestCollator::setVariableTop(const UChar
*varTop
, int32_t len
,
2015 // api not used, this is to make the compiler happy
2016 if (U_SUCCESS(status
) && (varTop
== 0 || len
< -1)) {
2017 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2022 uint32_t TestCollator::setVariableTop(const UnicodeString varTop
,
2025 // api not used, this is to make the compiler happy
2026 if (U_SUCCESS(status
) && varTop
.length() == 0) {
2027 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2032 void TestCollator::setVariableTop(const uint32_t varTop
, UErrorCode
&status
)
2034 // api not used, this is to make the compiler happy
2035 if (U_SUCCESS(status
) && varTop
== 0) {
2036 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2040 uint32_t TestCollator::getVariableTop(UErrorCode
&status
) const
2043 // api not used, this is to make the compiler happy
2044 if (U_SUCCESS(status
)) {
2047 return (uint32_t)(0xFFFFFFFFu
);
2050 Collator
* TestCollator::safeClone(void)
2052 return new TestCollator();
2055 UnicodeSet
* TestCollator::getTailoredSet(UErrorCode
&status
) const
2057 return Collator::getTailoredSet(status
);
2060 void TestCollator::setLocales(const Locale
& requestedLocale
, const Locale
& validLocale
)
2062 Collator::setLocales(requestedLocale
, validLocale
);
2066 void CollationAPITest::TestSubclass()
2070 doAssert(col1
!= col2
, "2 instance of TestCollator should be different");
2071 if (col1
.hashCode() != col2
.hashCode()) {
2072 errln("Every TestCollator has the same hashcode");
2074 UnicodeString
abc("abc", 3);
2075 UnicodeString
bcd("bcd", 3);
2076 if (col1
.compare(abc
, bcd
) != abc
.compare(bcd
)) {
2077 errln("TestCollator compare should be the same as the default "
2078 "string comparison");
2081 UErrorCode status
= U_ZERO_ERROR
;
2082 col1
.getCollationKey(abc
, key
, status
);
2084 const char* bytes
= (const char *)key
.getByteArray(length
);
2085 UnicodeString
keyarray(bytes
, length
, NULL
, status
);
2086 if (abc
!= keyarray
) {
2087 errln("TestCollator collationkey API is returning wrong values");
2090 UnicodeSet
expectedset(0, 0x10FFFF);
2091 UnicodeSet
*defaultset
= col1
.getTailoredSet(status
);
2092 if (!defaultset
->containsAll(expectedset
)
2093 || !expectedset
.containsAll(*defaultset
)) {
2094 errln("Error: expected default tailoring to be 0 to 0x10ffff");
2098 // use base class implementation
2099 Locale loc1
= Locale::getGermany();
2100 Locale loc2
= Locale::getFrance();
2101 col1
.setLocales(loc1
, loc2
); // default implementation has no effect
2103 UnicodeString displayName
;
2104 col1
.getDisplayName(loc1
, loc2
, displayName
); // de_DE collator in fr_FR locale
2106 TestCollator
col3(UCOL_TERTIARY
, UNORM_NONE
);
2107 UnicodeString
a("a");
2108 UnicodeString
b("b");
2109 Collator::EComparisonResult result
= Collator::EComparisonResult(a
.compare(b
));
2110 if(col1
.compare(a
, b
) != result
) {
2111 errln("Collator doesn't give default result");
2113 if(col1
.compare(a
, b
, 1) != result
) {
2114 errln("Collator doesn't give default result");
2116 if(col1
.compare(a
.getBuffer(), a
.length(), b
.getBuffer(), b
.length()) != result
) {
2117 errln("Collator doesn't give default result");
2121 void CollationAPITest::TestNULLCharTailoring()
2123 UErrorCode status
= U_ZERO_ERROR
;
2124 UChar buf
[256] = {0};
2125 int32_t len
= u_unescape("&a < '\\u0000'", buf
, 256);
2126 UnicodeString
first((UChar
)0x0061);
2127 UnicodeString
second((UChar
)0);
2128 RuleBasedCollator
*coll
= new RuleBasedCollator(UnicodeString(buf
, len
), status
);
2129 if(U_FAILURE(status
)) {
2130 errln("Failed to open collator");
2132 UCollationResult res
= coll
->compare(first
, second
, status
);
2133 if(res
!= UCOL_LESS
) {
2134 errln("a should be less then NULL after tailoring");
2139 void CollationAPITest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par */)
2141 if (exec
) logln("TestSuite CollationAPITest: ");
2143 case 0: name
= "TestProperty"; if (exec
) TestProperty(/* par */); break;
2144 case 1: name
= "TestOperators"; if (exec
) TestOperators(/* par */); break;
2145 case 2: name
= "TestDuplicate"; if (exec
) TestDuplicate(/* par */); break;
2146 case 3: name
= "TestCompare"; if (exec
) TestCompare(/* par */); break;
2147 case 4: name
= "TestHashCode"; if (exec
) TestHashCode(/* par */); break;
2148 case 5: name
= "TestCollationKey"; if (exec
) TestCollationKey(/* par */); break;
2149 case 6: name
= "TestElemIter"; if (exec
) TestElemIter(/* par */); break;
2150 case 7: name
= "TestGetAll"; if (exec
) TestGetAll(/* par */); break;
2151 case 8: name
= "TestRuleBasedColl"; if (exec
) TestRuleBasedColl(/* par */); break;
2152 case 9: name
= "TestDecomposition"; if (exec
) TestDecomposition(/* par */); break;
2153 case 10: name
= "TestSafeClone"; if (exec
) TestSafeClone(/* par */); break;
2154 case 11: name
= "TestSortKey"; if (exec
) TestSortKey(); break;
2155 case 12: name
= "TestMaxExpansion"; if (exec
) TestMaxExpansion(); break;
2156 case 13: name
= "TestDisplayName"; if (exec
) TestDisplayName(); break;
2157 case 14: name
= "TestAttribute"; if (exec
) TestAttribute(); break;
2158 case 15: name
= "TestVariableTopSetting"; if (exec
) TestVariableTopSetting(); break;
2159 case 16: name
= "TestRules"; if (exec
) TestRules(); break;
2160 case 17: name
= "TestGetLocale"; if (exec
) TestGetLocale(); break;
2161 case 18: name
= "TestBounds"; if (exec
) TestBounds(); break;
2162 case 19: name
= "TestGetTailoredSet"; if (exec
) TestGetTailoredSet(); break;
2163 case 20: name
= "TestUClassID"; if (exec
) TestUClassID(); break;
2164 case 21: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
2165 case 22: name
= "TestNULLCharTailoring"; if (exec
) TestNULLCharTailoring(); break;
2166 default: name
= ""; break;
2170 #endif /* #if !UCONFIG_NO_COLLATION */