1 /********************************************************************
3 * Copyright (c) 1997-2008, 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"
42 #include "unicode/ucol.h"
49 CollationAPITest::doAssert(UBool condition
, const char *message
)
52 errln(UnicodeString("ERROR : ") + message
);
56 #ifdef U_USE_COLLATION_OBSOLETE_2_6
58 * Test Collator::createInstance(... version...) for some locale. Called by TestProperty().
61 TestOpenVersion(IntlTest
&test
, const Locale
&locale
) {
62 UVersionInfo version1
, version2
;
63 Collator
*collator1
, *collator2
;
66 errorCode
=U_ZERO_ERROR
;
67 collator1
=Collator::createInstance(locale
, errorCode
);
68 if(U_SUCCESS(errorCode
)) {
69 /* get the current version */
70 collator1
->getVersion(version1
);
73 /* try to get that same version again */
74 collator2
=Collator::createInstance(locale
, version1
, errorCode
);
75 if(U_SUCCESS(errorCode
)) {
76 collator2
->getVersion(version2
);
77 if(0!=uprv_memcmp(version1
, version2
, sizeof(UVersionInfo
))) {
78 test
.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) returns a different collator\n", locale
.getName(), locale
.getName());
82 test
.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) fails: %s\n", locale
.getName(), locale
.getName(), u_errorName(errorCode
));
88 // Collator Class Properties
89 // ctor, dtor, createInstance, compare, getStrength/setStrength
90 // getDecomposition/setDecomposition, getDisplayName
92 CollationAPITest::TestProperty(/* char* par */)
94 UErrorCode success
= U_ZERO_ERROR
;
97 All the collations have the same version in an ICU
99 ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};
100 ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};
101 ICU 2.2 currVersionArray = {0x21, 0x40, 0x04, 0x04};
102 ICU 2.4 currVersionArray = {0x21, 0x40, 0x04, 0x04};
103 ICU 2.6 currVersionArray = {0x21, 0x40, 0x03, 0x03};
104 ICU 2.8 currVersionArray = {0x29, 0x80, 0x00, 0x04};
105 ICU 3.4 currVersionArray = {0x31, 0xC0, 0x00, 0x04};
107 UVersionInfo currVersionArray
= {0x31, 0xC0, 0x00, 0x05};
108 UVersionInfo versionArray
;
111 logln("The property tests begin : ");
112 logln("Test ctors : ");
113 col
= Collator::createInstance(Locale::getEnglish(), success
);
115 if (U_FAILURE(success
))
117 errln("Default Collator creation failed.");
121 col
->getVersion(versionArray
);
122 for (i
=0; i
<4; ++i
) {
123 if (versionArray
[i
] != currVersionArray
[i
]) {
124 errln("Testing ucol_getVersion() - unexpected result: %d.%d.%d.%d",
125 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
130 doAssert((col
->compare("ab", "abc") == Collator::LESS
), "ab < abc comparison failed");
131 doAssert((col
->compare("ab", "AB") == Collator::LESS
), "ab < AB comparison failed");
132 doAssert((col
->compare("blackbird", "black-bird") == Collator::GREATER
), "black-bird > blackbird comparison failed");
133 doAssert((col
->compare("black bird", "black-bird") == Collator::LESS
), "black bird > black-bird comparison failed");
134 doAssert((col
->compare("Hello", "hello") == Collator::GREATER
), "Hello > hello comparison failed");
137 /*start of update [Bertrand A. D. 02/10/98]*/
138 doAssert((col
->compare("ab", "abc", 2) == Collator::EQUAL
), "ab = abc with length 2 comparison failed");
139 doAssert((col
->compare("ab", "AB", 2) == Collator::LESS
), "ab < AB with length 2 comparison failed");
140 doAssert((col
->compare("ab", "Aa", 1) == Collator::LESS
), "ab < Aa with length 1 comparison failed");
141 doAssert((col
->compare("ab", "Aa", 2) == Collator::GREATER
), "ab > Aa with length 2 comparison failed");
142 doAssert((col
->compare("black-bird", "blackbird", 5) == Collator::EQUAL
), "black-bird = blackbird with length of 5 comparison failed");
143 doAssert((col
->compare("black bird", "black-bird", 10) == Collator::LESS
), "black bird < black-bird with length 10 comparison failed");
144 doAssert((col
->compare("Hello", "hello", 5) == Collator::GREATER
), "Hello > hello with length 5 comparison failed");
145 /*end of update [Bertrand A. D. 02/10/98]*/
148 logln("Test ctors ends.");
149 logln("testing Collator::getStrength() method ...");
150 doAssert((col
->getStrength() == Collator::TERTIARY
), "collation object has the wrong strength");
151 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
154 logln("testing Collator::setStrength() method ...");
155 col
->setStrength(Collator::SECONDARY
);
156 doAssert((col
->getStrength() != Collator::TERTIARY
), "collation object's strength is secondary difference");
157 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
158 doAssert((col
->getStrength() == Collator::SECONDARY
), "collation object has the wrong strength");
162 logln("Get display name for the US English collation in German : ");
163 logln(Collator::getDisplayName(Locale::getUS(), Locale::getGerman(), name
));
164 doAssert((name
== UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed");
166 logln("Get display name for the US English collation in English : ");
167 logln(Collator::getDisplayName(Locale::getUS(), Locale::getEnglish(), name
));
168 doAssert((name
== UnicodeString("English (United States)")), "getDisplayName failed");
170 // weiv : this test is bogus if we're running on any machine that has different default locale than English.
171 // Therefore, it is banned!
172 logln("Get display name for the US English in default locale language : ");
173 logln(Collator::getDisplayName(Locale::US
, name
));
174 doAssert((name
== UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine");
177 RuleBasedCollator
*rcol
= (RuleBasedCollator
*)Collator::createInstance("da_DK",
179 doAssert(rcol
->getRules().length() != 0, "da_DK rules does not have length 0");
182 col
= Collator::createInstance(Locale::getFrench(), success
);
183 if (U_FAILURE(success
))
185 errln("Creating French collation failed.");
189 col
->setStrength(Collator::PRIMARY
);
190 logln("testing Collator::getStrength() method again ...");
191 doAssert((col
->getStrength() != Collator::TERTIARY
), "collation object has the wrong strength");
192 doAssert((col
->getStrength() == Collator::PRIMARY
), "collation object's strength is not primary difference");
194 logln("testing French Collator::setStrength() method ...");
195 col
->setStrength(Collator::TERTIARY
);
196 doAssert((col
->getStrength() == Collator::TERTIARY
), "collation object's strength is not tertiary difference");
197 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
198 doAssert((col
->getStrength() != Collator::SECONDARY
), "collation object's strength is secondary difference");
200 logln("Create junk collation: ");
201 Locale
abcd("ab", "CD", "");
202 success
= U_ZERO_ERROR
;
204 junk
= Collator::createInstance(abcd
, success
);
206 if (U_FAILURE(success
))
208 errln("Junk collation creation failed, should at least return default.");
214 col
= Collator::createInstance(success
);
215 if (U_FAILURE(success
))
217 errln("Creating default collator failed.");
222 doAssert(((RuleBasedCollator
*)col
)->getRules() == ((RuleBasedCollator
*)junk
)->getRules(),
223 "The default collation should be returned.");
224 Collator
*frCol
= Collator::createInstance(Locale::getFrance(), success
);
225 if (U_FAILURE(success
))
227 errln("Creating French collator failed.");
233 // If the default locale isn't French, the French and non-French collators
234 // should be different
235 if (frCol
->getLocale(ULOC_ACTUAL_LOCALE
, success
) != Locale::getFrench()) {
236 doAssert((*frCol
!= *junk
), "The junk is the same as the French collator.");
238 Collator
*aFrCol
= frCol
->clone();
239 doAssert((*frCol
== *aFrCol
), "The cloning of a French collator failed.");
240 logln("Collator property test ended.");
247 #ifdef U_USE_COLLATION_OBSOLETE_2_6
248 /* test Collator::createInstance(...version...) */
249 TestOpenVersion(*this, "");
250 TestOpenVersion(*this, "da");
251 TestOpenVersion(*this, "fr");
252 TestOpenVersion(*this, "ja");
254 /* try some bogus version */
256 versionArray
[1]=0x99;
257 versionArray
[2]=0xc7;
258 versionArray
[3]=0xfe;
259 col
=Collator::createInstance(Locale(), versionArray
, success
);
260 if(U_SUCCESS(success
)) {
261 errln("error: ucol_openVersion(bogus version) succeeded");
268 CollationAPITest::TestRuleBasedColl()
270 RuleBasedCollator
*col1
, *col2
, *col3
, *col4
;
271 UErrorCode status
= U_ZERO_ERROR
;
273 UnicodeString
ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
274 UnicodeString
ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
276 col1
= new RuleBasedCollator(ruleset1
, status
);
277 if (U_FAILURE(status
)) {
278 errln("RuleBased Collator creation failed.\n");
282 logln("PASS: RuleBased Collator creation passed\n");
285 status
= U_ZERO_ERROR
;
286 col2
= new RuleBasedCollator(ruleset2
, status
);
287 if (U_FAILURE(status
)) {
288 errln("RuleBased Collator creation failed.\n");
292 logln("PASS: RuleBased Collator creation passed\n");
295 status
= U_ZERO_ERROR
;
296 Locale
locale("aa", "AA");
297 col3
= (RuleBasedCollator
*)Collator::createInstance(locale
, status
);
298 if (U_FAILURE(status
)) {
299 errln("Fallback Collator creation failed.: %s\n");
303 logln("PASS: Fallback Collator creation passed\n");
307 status
= U_ZERO_ERROR
;
308 col3
= (RuleBasedCollator
*)Collator::createInstance(status
);
309 if (U_FAILURE(status
)) {
310 errln("Default Collator creation failed.: %s\n");
314 logln("PASS: Default Collator creation passed\n");
317 UnicodeString rule1
= col1
->getRules();
318 UnicodeString rule2
= col2
->getRules();
319 UnicodeString rule3
= col3
->getRules();
321 doAssert(rule1
!= rule2
, "Default collator getRules failed");
322 doAssert(rule2
!= rule3
, "Default collator getRules failed");
323 doAssert(rule1
!= rule3
, "Default collator getRules failed");
325 col4
= new RuleBasedCollator(rule2
, status
);
326 if (U_FAILURE(status
)) {
327 errln("RuleBased Collator creation failed.\n");
331 UnicodeString rule4
= col4
->getRules();
332 doAssert(rule2
== rule4
, "Default collator getRules failed");
334 uint8_t *clonedrule4
= col4
->cloneRuleData(length4
, status
);
335 if (U_FAILURE(status
)) {
336 errln("Cloned rule data failed.\n");
340 // free(clonedrule4); BAD API!!!!
341 uprv_free(clonedrule4
);
351 CollationAPITest::TestRules()
353 RuleBasedCollator
*coll
;
354 UErrorCode status
= U_ZERO_ERROR
;
357 coll
= (RuleBasedCollator
*)Collator::createInstance(Locale::getEnglish(), status
);
358 if (U_FAILURE(status
)) {
359 errln("English Collator creation failed.\n");
363 logln("PASS: RuleBased Collator creation passed\n");
366 coll
->getRules(UCOL_TAILORING_ONLY
, rules
);
367 if (rules
.length() != 0x0a) {
368 errln("English tailored rules failed - length is 0x%x expected 0x%x", rules
.length(), 0x0e);
371 coll
->getRules(UCOL_FULL_RULES
, rules
);
372 if (rules
.length() < 0) {
373 errln("English full rules failed");
379 CollationAPITest::TestDecomposition() {
380 UErrorCode status
= U_ZERO_ERROR
;
381 Collator
*en_US
= Collator::createInstance("en_US", status
),
382 *el_GR
= Collator::createInstance("el_GR", status
),
383 *vi_VN
= Collator::createInstance("vi_VN", status
);
385 if (U_FAILURE(status
)) {
386 errln("ERROR: collation creation failed.\n");
390 /* there is no reason to have canonical decomposition in en_US OR default locale */
391 if (vi_VN
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_ON
)
393 errln("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
396 if (el_GR
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_ON
)
398 errln("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
401 if (en_US
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_OFF
)
403 errln("ERROR: en_US collation had canonical decomposition for normalization!\n");
412 CollationAPITest::TestSafeClone() {
413 static const int CLONETEST_COLLATOR_COUNT
= 3;
414 Collator
*someCollators
[CLONETEST_COLLATOR_COUNT
];
416 UErrorCode err
= U_ZERO_ERROR
;
419 UnicodeString
test1("abCda");
420 UnicodeString
test2("abcda");
422 /* one default collator & two complex ones */
423 someCollators
[0] = Collator::createInstance("en_US", err
);
424 someCollators
[1] = Collator::createInstance("ko", err
);
425 someCollators
[2] = Collator::createInstance("ja_JP", err
);
427 errln("Couldn't instantiate collators. Error: %s", u_errorName(err
));
428 delete someCollators
[0];
429 delete someCollators
[1];
430 delete someCollators
[2];
434 /* change orig & clone & make sure they are independent */
436 for (index
= 0; index
< CLONETEST_COLLATOR_COUNT
; index
++)
438 col
= someCollators
[index
]->safeClone();
440 errln("SafeClone of collator should not return null\n");
443 col
->setStrength(Collator::TERTIARY
);
444 someCollators
[index
]->setStrength(Collator::PRIMARY
);
445 col
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, err
);
446 someCollators
[index
]->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, err
);
448 doAssert(col
->greater(test1
, test2
), "Result should be \"abCda\" >>> \"abcda\" ");
449 doAssert(someCollators
[index
]->equals(test1
, test2
), "Result should be \"abcda\" == \"abCda\"");
451 delete someCollators
[index
];
456 CollationAPITest::TestHashCode(/* char* par */)
458 logln("hashCode tests begin.");
459 UErrorCode success
= U_ZERO_ERROR
;
461 col1
= Collator::createInstance(Locale::getEnglish(), success
);
462 if (U_FAILURE(success
))
464 errln("Default collation creation failed.");
469 Locale
dk("da", "DK", "");
470 col2
= Collator::createInstance(dk
, success
);
471 if (U_FAILURE(success
))
473 errln("Danish collation creation failed.");
478 col3
= Collator::createInstance(Locale::getEnglish(), success
);
479 if (U_FAILURE(success
))
481 errln("2nd default collation creation failed.");
485 logln("Collator::hashCode() testing ...");
487 doAssert(col1
->hashCode() != col2
->hashCode(), "Hash test1 result incorrect" );
488 doAssert(!(col1
->hashCode() == col2
->hashCode()), "Hash test2 result incorrect" );
489 doAssert(col1
->hashCode() == col3
->hashCode(), "Hash result not equal" );
491 logln("hashCode tests end.");
495 UnicodeString
test1("Abcda");
496 UnicodeString
test2("abcda");
498 CollationKey sortk1
, sortk2
, sortk3
;
499 UErrorCode status
= U_ZERO_ERROR
;
501 col3
->getCollationKey(test1
, sortk1
, status
);
502 col3
->getCollationKey(test2
, sortk2
, status
);
503 col3
->getCollationKey(test2
, sortk3
, status
);
505 doAssert(sortk1
.hashCode() != sortk2
.hashCode(), "Hash test1 result incorrect");
506 doAssert(sortk2
.hashCode() == sortk3
.hashCode(), "Hash result not equal" );
511 //----------------------------------------------------------------------------
512 // CollationKey -- Tests the CollationKey methods
515 CollationAPITest::TestCollationKey(/* char* par */)
517 logln("testing CollationKey begins...");
519 UErrorCode success
=U_ZERO_ERROR
;
520 col
= Collator::createInstance(Locale::getEnglish(), success
);
521 if (U_FAILURE(success
))
523 errln("Default collation creation failed.");
526 col
->setStrength(Collator::TERTIARY
);
528 CollationKey sortk1
, sortk2
;
529 UnicodeString
test1("Abcda"), test2("abcda");
530 UErrorCode key1Status
= U_ZERO_ERROR
, key2Status
= U_ZERO_ERROR
;
532 logln("Testing weird arguments");
533 col
->getCollationKey(NULL
, 0, sortk1
, key1Status
);
534 // key gets reset here
536 sortk1
.getByteArray(length
);
537 doAssert(sortk1
.isBogus() == FALSE
&& length
== 0,
538 "Empty string should return an empty collation key");
539 // bogus key returned here
540 key1Status
= U_ILLEGAL_ARGUMENT_ERROR
;
541 col
->getCollationKey(NULL
, 0, sortk1
, key1Status
);
542 doAssert(sortk1
.getByteArray(length
) == NULL
&& length
== 0,
543 "Error code should return bogus collation key");
545 key1Status
= U_ZERO_ERROR
;
546 logln("Use tertiary comparison level testing ....");
548 col
->getCollationKey(test1
, sortk1
, key1Status
);
549 doAssert((sortk1
.compareTo(col
->getCollationKey(test2
, sortk2
, key2Status
)))
550 == Collator::GREATER
,
551 "Result should be \"Abcda\" >>> \"abcda\"");
553 CollationKey
sortk3(sortk2
), sortkNew
, sortkEmpty
;
557 doAssert((sortk1
!= sortk2
), "The sort keys should be different");
558 doAssert((sortk1
.hashCode() != sortk2
.hashCode()), "sort key hashCode() failed");
559 doAssert((sortk2
== sortk3
), "The sort keys should be the same");
560 doAssert((sortk1
== sortkNew
), "The sort keys assignment failed");
561 doAssert((sortk1
.hashCode() == sortkNew
.hashCode()), "sort key hashCode() failed");
562 doAssert((sortkNew
!= sortk3
), "The sort keys should be different");
563 doAssert(sortk1
.compareTo(sortk3
) == Collator::GREATER
, "Result should be \"Abcda\" >>> \"abcda\"");
564 doAssert(sortk2
.compareTo(sortk3
) == Collator::EQUAL
, "Result should be \"abcda\" == \"abcda\"");
565 doAssert(sortkEmpty
.compareTo(sortk1
) == Collator::LESS
, "Result should be (empty key) <<< \"Abcda\"");
566 doAssert(sortk1
.compareTo(sortkEmpty
) == Collator::GREATER
, "Result should be \"Abcda\" >>> (empty key)");
567 doAssert(sortkEmpty
.compareTo(sortkEmpty
) == Collator::EQUAL
, "Result should be (empty key) == (empty key)");
568 doAssert(sortk1
.compareTo(sortk3
, success
) == UCOL_GREATER
, "Result should be \"Abcda\" >>> \"abcda\"");
569 doAssert(sortk2
.compareTo(sortk3
, success
) == UCOL_EQUAL
, "Result should be \"abcda\" == \"abcda\"");
570 doAssert(sortkEmpty
.compareTo(sortk1
, success
) == UCOL_LESS
, "Result should be (empty key) <<< \"Abcda\"");
571 doAssert(sortk1
.compareTo(sortkEmpty
, success
) == UCOL_GREATER
, "Result should be \"Abcda\" >>> (empty key)");
572 doAssert(sortkEmpty
.compareTo(sortkEmpty
, success
) == UCOL_EQUAL
, "Result should be (empty key) == (empty key)");
574 int32_t cnt1
, cnt2
, cnt3
, cnt4
;
576 const uint8_t* byteArray1
= sortk1
.getByteArray(cnt1
);
577 const uint8_t* byteArray2
= sortk2
.getByteArray(cnt2
);
580 this is a bad test since it is dependent on the version of uca data,
583 const char sortk2_compat[] = {
584 // this is a 1.8 sortkey
585 0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00
589 const uint8_t* byteArray3
= 0;
590 byteArray3
= sortk1
.getByteArray(cnt3
);
592 const uint8_t* byteArray4
= 0;
593 byteArray4
= sortk2
.getByteArray(cnt4
);
595 CollationKey
sortk4(byteArray1
, cnt1
), sortk5(byteArray2
, cnt2
);
596 CollationKey
sortk6(byteArray3
, cnt3
), sortk7(byteArray4
, cnt4
);
599 doAssert(memcmp(byteArray2, sortk2_compat, strlen(sortk2_compat)) == 0,
600 "Binary format for 'abcda' sortkey different!");
602 doAssert(sortk1
.compareTo(sortk4
) == Collator::EQUAL
, "CollationKey::toByteArray(sortk1) Failed.");
603 doAssert(sortk2
.compareTo(sortk5
) == Collator::EQUAL
, "CollationKey::toByteArray(sortk2) Failed.");
604 doAssert(sortk4
.compareTo(sortk5
) == Collator::GREATER
, "sortk4 >>> sortk5 Failed");
605 doAssert(sortk1
.compareTo(sortk6
) == Collator::EQUAL
, "CollationKey::getByteArray(sortk1) Failed.");
606 doAssert(sortk2
.compareTo(sortk7
) == Collator::EQUAL
, "CollationKey::getByteArray(sortk2) Failed.");
607 doAssert(sortk6
.compareTo(sortk7
) == Collator::GREATER
, "sortk6 >>> sortk7 Failed");
609 logln("Equality tests : ");
610 doAssert(sortk1
== sortk4
, "sortk1 == sortk4 Failed.");
611 doAssert(sortk2
== sortk5
, "sortk2 == sortk5 Failed.");
612 doAssert(sortk1
!= sortk5
, "sortk1 != sortk5 Failed.");
613 doAssert(sortk1
== sortk6
, "sortk1 == sortk6 Failed.");
614 doAssert(sortk2
== sortk7
, "sortk2 == sortk7 Failed.");
615 doAssert(sortk1
!= sortk7
, "sortk1 != sortk7 Failed.");
621 doAssert(sortk1
== sortk3
, "sortk1 = sortk3 assignment Failed.");
622 doAssert(sortk2
!= sortk3
, "sortk2 != sortk3 Failed.");
623 logln("testing sortkey ends...");
625 col
->setStrength(Collator::SECONDARY
);
626 doAssert(col
->getCollationKey(test1
, sortk1
, key1Status
).compareTo(
627 col
->getCollationKey(test2
, sortk2
, key2Status
))
629 "Result should be \"Abcda\" == \"abcda\"");
633 //----------------------------------------------------------------------------
634 // Tests the CollatorElementIterator class.
635 // ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!=
638 CollationAPITest::TestElemIter(/* char* par */)
640 logln("testing sortkey begins...");
642 UErrorCode success
= U_ZERO_ERROR
;
643 col
= Collator::createInstance(Locale::getEnglish(), success
);
644 if (U_FAILURE(success
))
646 errln("Default collation creation failed.");
650 UnicodeString
testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
651 UnicodeString
testString2("Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
652 logln("Constructors and comparison testing....");
653 CollationElementIterator
*iterator1
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString1
);
655 CharacterIterator
*chariter
=new StringCharacterIterator(testString1
);
656 CollationElementIterator
*coliter
=((RuleBasedCollator
*)col
)->createCollationElementIterator(*chariter
);
659 CollationElementIterator
*iterator2
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString1
);
660 CollationElementIterator
*iterator3
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString2
);
662 int32_t offset
= iterator1
->getOffset();
664 errln("Error in getOffset for collation element iterator\n");
667 iterator1
->setOffset(6, success
);
668 if (U_FAILURE(success
)) {
669 errln("Error in setOffset for collation element iterator\n");
672 iterator1
->setOffset(0, success
);
673 int32_t order1
, order2
, order3
;
674 doAssert((*iterator1
== *iterator2
), "The two iterators should be the same");
675 doAssert((*iterator1
!= *iterator3
), "The two iterators should be different");
677 doAssert((*coliter
== *iterator1
), "The two iterators should be the same");
678 doAssert((*coliter
== *iterator2
), "The two iterators should be the same");
679 doAssert((*coliter
!= *iterator3
), "The two iterators should be different");
681 order1
= iterator1
->next(success
);
682 if (U_FAILURE(success
))
684 errln("Somehow ran out of memory stepping through the iterator.");
688 doAssert((*iterator1
!= *iterator2
), "The first iterator advance failed");
689 order2
= iterator2
->getOffset();
690 doAssert((order1
!= order2
), "The order result should not be the same");
691 order2
= iterator2
->next(success
);
692 if (U_FAILURE(success
))
694 errln("Somehow ran out of memory stepping through the iterator.");
698 doAssert((*iterator1
== *iterator2
), "The second iterator advance failed");
699 doAssert((order1
== order2
), "The order result should be the same");
700 order3
= iterator3
->next(success
);
701 if (U_FAILURE(success
))
703 errln("Somehow ran out of memory stepping through the iterator.");
707 doAssert((CollationElementIterator::primaryOrder(order1
) ==
708 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be the same");
709 doAssert((CollationElementIterator::secondaryOrder(order1
) ==
710 CollationElementIterator::secondaryOrder(order3
)), "The secondary orders should be the same");
711 doAssert((CollationElementIterator::tertiaryOrder(order1
) ==
712 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be the same");
714 order1
= iterator1
->next(success
); order3
= iterator3
->next(success
);
715 if (U_FAILURE(success
))
717 errln("Somehow ran out of memory stepping through the iterator.");
721 doAssert((CollationElementIterator::primaryOrder(order1
) ==
722 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be identical");
723 doAssert((CollationElementIterator::tertiaryOrder(order1
) !=
724 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be different");
726 order1
= iterator1
->next(success
);
727 order3
= iterator3
->next(success
);
728 /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
730 doAssert((CollationElementIterator::secondaryOrder(order1) !=
731 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
733 doAssert((order1
!= CollationElementIterator::NULLORDER
), "Unexpected end of iterator reached");
735 iterator1
->reset(); iterator2
->reset(); iterator3
->reset();
736 order1
= iterator1
->next(success
);
737 if (U_FAILURE(success
))
739 errln("Somehow ran out of memory stepping through the iterator.");
743 doAssert((*iterator1
!= *iterator2
), "The first iterator advance failed");
745 order2
= iterator2
->next(success
);
746 if (U_FAILURE(success
))
748 errln("Somehow ran out of memory stepping through the iterator.");
752 doAssert((*iterator1
== *iterator2
), "The second iterator advance failed");
753 doAssert((order1
== order2
), "The order result should be the same");
755 order3
= iterator3
->next(success
);
756 if (U_FAILURE(success
))
758 errln("Somehow ran out of memory stepping through the iterator.");
762 doAssert((CollationElementIterator::primaryOrder(order1
) ==
763 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be the same");
764 doAssert((CollationElementIterator::secondaryOrder(order1
) ==
765 CollationElementIterator::secondaryOrder(order3
)), "The secondary orders should be the same");
766 doAssert((CollationElementIterator::tertiaryOrder(order1
) ==
767 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be the same");
769 order1
= iterator1
->next(success
); order2
= iterator2
->next(success
); order3
= iterator3
->next(success
);
770 if (U_FAILURE(success
))
772 errln("Somehow ran out of memory stepping through the iterator.");
776 doAssert((CollationElementIterator::primaryOrder(order1
) ==
777 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be identical");
778 doAssert((CollationElementIterator::tertiaryOrder(order1
) !=
779 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be different");
781 order1
= iterator1
->next(success
); order3
= iterator3
->next(success
);
782 if (U_FAILURE(success
))
784 errln("Somehow ran out of memory stepping through the iterator.");
788 /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
790 doAssert((CollationElementIterator::secondaryOrder(order1) !=
791 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
793 doAssert((order1
!= CollationElementIterator::NULLORDER
), "Unexpected end of iterator reached");
794 doAssert((*iterator2
!= *iterator3
), "The iterators should be different");
798 success
=U_UNSUPPORTED_ERROR
;
799 Collator
*colerror
=NULL
;
800 colerror
=Collator::createInstance(Locale::getEnglish(), success
);
801 if (colerror
!= 0 || success
== U_ZERO_ERROR
){
802 errln("Error: createInstance(UErrorCode != U_ZERO_ERROR) should just return and not create an instance\n");
804 int32_t position
=coliter
->previous(success
);
805 if(position
!= CollationElementIterator::NULLORDER
){
806 errln((UnicodeString
)"Expected NULLORDER got" + position
);
809 coliter
->setText(*chariter
, success
);
810 if(!U_FAILURE(success
)){
811 errln("Expeceted error");
813 iterator1
->setText((UnicodeString
)"hello there", success
);
814 if(!U_FAILURE(success
)){
815 errln("Expeceted error");
827 logln("testing CollationElementIterator ends...");
830 // Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules
832 CollationAPITest::TestOperators(/* char* par */)
834 UErrorCode success
= U_ZERO_ERROR
;
835 UnicodeString
ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
836 UnicodeString
ruleset2("< a, A < b, B < c, C < d, D, e, E");
837 RuleBasedCollator
*col1
= new RuleBasedCollator(ruleset1
, success
);
838 if (U_FAILURE(success
)) {
839 errln("RuleBasedCollator creation failed.");
842 success
= U_ZERO_ERROR
;
843 RuleBasedCollator
*col2
= new RuleBasedCollator(ruleset2
, success
);
844 if (U_FAILURE(success
)) {
845 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set.");
848 logln("The operator tests begin : ");
849 logln("testing operator==, operator!=, clone methods ...");
850 doAssert((*col1
!= *col2
), "The two different table collations compared equal");
852 doAssert((*col1
== *col2
), "Collator objects not equal after assignment (operator=)");
854 success
= U_ZERO_ERROR
;
855 Collator
*col3
= Collator::createInstance(Locale::getEnglish(), success
);
856 if (U_FAILURE(success
)) {
857 errln("Default collation creation failed.");
860 doAssert((*col1
!= *col3
), "The two different table collations compared equal");
861 Collator
* col4
= col1
->clone();
862 Collator
* col5
= col3
->clone();
863 doAssert((*col1
== *col4
), "Cloned collation objects not equal");
864 doAssert((*col3
!= *col4
), "Two different table collations compared equal");
865 doAssert((*col3
== *col5
), "Cloned collation objects not equal");
866 doAssert((*col4
!= *col5
), "Two cloned collations compared equal");
868 const UnicodeString
& defRules
= ((RuleBasedCollator
*)col3
)->getRules();
869 RuleBasedCollator
* col6
= new RuleBasedCollator(defRules
, success
);
870 if (U_FAILURE(success
)) {
871 errln("Creating default collation with rules failed.");
874 doAssert((((RuleBasedCollator
*)col3
)->getRules() == col6
->getRules()), "Default collator getRules failed");
876 success
= U_ZERO_ERROR
;
877 RuleBasedCollator
*col7
= new RuleBasedCollator(ruleset2
, Collator::TERTIARY
, success
);
878 if (U_FAILURE(success
)) {
879 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength.");
882 success
= U_ZERO_ERROR
;
883 RuleBasedCollator
*col8
= new RuleBasedCollator(ruleset2
, UCOL_OFF
, success
);
884 if (U_FAILURE(success
)) {
885 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with Normalizer::NO_OP.");
888 success
= U_ZERO_ERROR
;
889 RuleBasedCollator
*col9
= new RuleBasedCollator(ruleset2
, Collator::PRIMARY
, UCOL_ON
, success
);
890 if (U_FAILURE(success
)) {
891 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength and Normalizer::NO_OP.");
894 // doAssert((*col7 == *col8), "The two equal table collations compared different");
895 doAssert((*col7
!= *col9
), "The two different table collations compared equal");
896 doAssert((*col8
!= *col9
), "The two different table collations compared equal");
898 logln("operator tests ended.");
910 // test clone and copy
912 CollationAPITest::TestDuplicate(/* char* par */)
914 UErrorCode status
= U_ZERO_ERROR
;
915 Collator
*col1
= Collator::createInstance(Locale::getEnglish(), status
);
916 if (U_FAILURE(status
)) {
917 logln("Default collator creation failed.");
920 Collator
*col2
= col1
->clone();
921 doAssert((*col1
== *col2
), "Cloned object is not equal to the orginal");
922 UnicodeString
*ruleset
= new UnicodeString("< a, A < b, B < c, C < d, D, e, E");
923 RuleBasedCollator
*col3
= new RuleBasedCollator(*ruleset
, status
);
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");
928 if (U_FAILURE(status
)) {
929 logln("Collation tailoring failed.");
933 UCollationResult res
;
934 UnicodeString
first((UChar
)0x0061);
935 UnicodeString
second((UChar
)0x0062);
936 UnicodeString
copiedEnglishRules(((RuleBasedCollator
*)col1
)->getRules());
941 // Try using the cloned collators after deleting the original data
942 res
= col2
->compare(first
, second
, status
);
943 if(res
!= UCOL_LESS
) {
944 errln("a should be less then b after tailoring");
946 if (((RuleBasedCollator
*)col2
)->getRules() != copiedEnglishRules
) {
947 errln(UnicodeString("English rule difference. ")
948 + copiedEnglishRules
+ UnicodeString("\ngetRules=") + ((RuleBasedCollator
*)col2
)->getRules());
950 res
= col3
->compare(first
, second
, status
);
951 if(res
!= UCOL_LESS
) {
952 errln("a should be less then b after tailoring");
954 if (col3
->getRules() != copiedEnglishRules
) {
955 errln(UnicodeString("English rule difference. ")
956 + copiedEnglishRules
+ UnicodeString("\ngetRules=") + col3
->getRules());
964 CollationAPITest::TestCompare(/* char* par */)
966 logln("The compare tests begin : ");
968 UErrorCode success
= U_ZERO_ERROR
;
969 col
= Collator::createInstance(Locale::getEnglish(), success
);
970 if (U_FAILURE(success
)) {
971 errln("Default collation creation failed.");
974 UnicodeString
test1("Abcda"), test2("abcda");
975 logln("Use tertiary comparison level testing ....");
977 doAssert((!col
->equals(test1
, test2
) ), "Result should be \"Abcda\" != \"abcda\"");
978 doAssert((col
->greater(test1
, test2
) ), "Result should be \"Abcda\" >>> \"abcda\"");
979 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" >>> \"abcda\"");
981 col
->setStrength(Collator::SECONDARY
);
982 logln("Use secondary comparison level testing ....");
984 doAssert((col
->equals(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
985 doAssert((!col
->greater(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
986 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
988 col
->setStrength(Collator::PRIMARY
);
989 logln("Use primary comparison level testing ....");
991 doAssert((col
->equals(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
992 doAssert((!col
->greater(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
993 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
995 // Test different APIs
996 const UChar
* t1
= test1
.getBuffer();
997 int32_t t1Len
= test1
.length();
998 const UChar
* t2
= test2
.getBuffer();
999 int32_t t2Len
= test2
.length();
1001 doAssert((col
->compare(test1
, test2
) == Collator::EQUAL
), "Problem");
1002 doAssert((col
->compare(test1
, test2
, success
) == UCOL_EQUAL
), "Problem");
1003 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
) == Collator::EQUAL
), "Problem");
1004 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
, success
) == UCOL_EQUAL
), "Problem");
1005 doAssert((col
->compare(test1
, test2
, t1Len
) == Collator::EQUAL
), "Problem");
1006 doAssert((col
->compare(test1
, test2
, t1Len
, success
) == UCOL_EQUAL
), "Problem");
1008 col
->setAttribute(UCOL_STRENGTH
, UCOL_TERTIARY
, success
);
1009 doAssert((col
->compare(test1
, test2
) == Collator::GREATER
), "Problem");
1010 doAssert((col
->compare(test1
, test2
, success
) == UCOL_GREATER
), "Problem");
1011 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
) == Collator::GREATER
), "Problem");
1012 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
, success
) == UCOL_GREATER
), "Problem");
1013 doAssert((col
->compare(test1
, test2
, t1Len
) == Collator::GREATER
), "Problem");
1014 doAssert((col
->compare(test1
, test2
, t1Len
, success
) == UCOL_GREATER
), "Problem");
1018 logln("The compare tests end.");
1023 CollationAPITest::TestGetAll(/* char* par */)
1025 int32_t count1
, count2
;
1026 UErrorCode status
= U_ZERO_ERROR
;
1028 logln("Trying Collator::getAvailableLocales(int&)");
1030 const Locale
* list
= Collator::getAvailableLocales(count1
);
1031 for (int32_t i
= 0; i
< count1
; ++i
) {
1032 UnicodeString dispName
;
1033 logln(UnicodeString("Locale name: ")
1034 + UnicodeString(list
[i
].getName())
1035 + UnicodeString(" , the display name is : ")
1036 + UnicodeString(list
[i
].getDisplayName(dispName
)));
1039 if (count1
== 0 || list
== NULL
) {
1040 errln("getAvailableLocales(int&) returned an empty list");
1043 logln("Trying Collator::getAvailableLocales()");
1044 StringEnumeration
* localeEnum
= Collator::getAvailableLocales();
1045 const UnicodeString
* locStr
;
1046 const char *locCStr
;
1049 if (localeEnum
== NULL
) {
1050 errln("getAvailableLocales() returned NULL");
1054 while ((locStr
= localeEnum
->snext(status
)) != NULL
)
1056 logln(UnicodeString("Locale name is: ") + *locStr
);
1059 if (count1
!= count2
) {
1060 errln("getAvailableLocales(int&) returned %d and getAvailableLocales() returned %d", count1
, count2
);
1063 logln("Trying Collator::getAvailableLocales() clone");
1065 StringEnumeration
* localeEnum2
= localeEnum
->clone();
1066 localeEnum2
->reset(status
);
1067 while ((locCStr
= localeEnum2
->next(NULL
, status
)) != NULL
)
1069 logln(UnicodeString("Locale name is: ") + UnicodeString(locCStr
));
1072 if (count1
!= count2
) {
1073 errln("getAvailableLocales(3rd time) returned %d and getAvailableLocales(2nd time) returned %d", count1
, count2
);
1075 if (localeEnum
->count(status
) != count1
) {
1076 errln("localeEnum->count() returned %d and getAvailableLocales() returned %d", localeEnum
->count(status
), count1
);
1082 void CollationAPITest::TestSortKey()
1084 UErrorCode status
= U_ZERO_ERROR
;
1086 this is supposed to open default date format, but later on it treats
1087 it like it is "en_US"
1088 - very bad if you try to run the tests on machine where default
1089 locale is NOT "en_US"
1091 Collator
*col
= Collator::createInstance(Locale::getEnglish(), status
);
1092 if (U_FAILURE(status
)) {
1093 errln("ERROR: Default collation creation failed.: %s\n", u_errorName(status
));
1097 if (col
->getStrength() != Collator::TERTIARY
)
1099 errln("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
1102 /* Need to use identical strength */
1103 col
->setAttribute(UCOL_STRENGTH
, UCOL_IDENTICAL
, status
);
1105 uint8_t key2compat
[] = {
1106 /* 3.9 key, from UCA 5.1 */
1107 0x2c, 0x2e, 0x30, 0x32, 0x2c, 0x01,
1108 0x09, 0x01, 0x09, 0x01, 0x2b, 0x01,
1109 0x92, 0x93, 0x94, 0x95, 0x92, 0x0
1111 /* 3.6 key, from UCA 5.0 */
1113 0x29, 0x2b, 0x2d, 0x2f, 0x29, 0x01,
1114 0x09, 0x01, 0x09, 0x01, 0x28, 0x01,
1115 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
1117 /* 3.4 key, from UCA 4.1 */
1119 0x28, 0x2a, 0x2c, 0x2e, 0x28, 0x01,
1120 0x09, 0x01, 0x09, 0x01, 0x27, 0x01,
1121 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
1125 0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01,
1126 0x09, 0x01, 0x09, 0x01, 0x25, 0x01,
1127 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
1131 0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01,
1132 0x09, 0x01, 0x09, 0x01, 0x1C, 0x01,
1133 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
1137 0x19, 0x1B, 0x1D, 0x1F, 0x19,
1138 0x01, 0x09, 0x01, 0x09, 0x01,
1140 0x92, 0x93, 0x94, 0x95, 0x92,
1145 0x19, 0x1B, 0x1D, 0x1F, 0x19,
1146 0x01, 0x0A, 0x01, 0x0A, 0x01,
1147 0x92, 0x93, 0x94, 0x95, 0x92,
1152 UChar test1
[6] = {0x41, 0x62, 0x63, 0x64, 0x61, 0},
1153 test2
[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0},
1154 test3
[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0};
1156 uint8_t sortkey1
[64];
1157 uint8_t sortkey2
[64];
1158 uint8_t sortkey3
[64];
1160 logln("Use tertiary comparison level testing ....\n");
1163 col
->getCollationKey(test1
, u_strlen(test1
), key1
, status
);
1166 col
->getCollationKey(test2
, u_strlen(test2
), key2
, status
);
1169 col
->getCollationKey(test3
, u_strlen(test3
), key3
, status
);
1171 doAssert(key1
.compareTo(key2
) == Collator::GREATER
,
1172 "Result should be \"Abcda\" > \"abcda\"");
1173 doAssert(key2
.compareTo(key1
) == Collator::LESS
,
1174 "Result should be \"abcda\" < \"Abcda\"");
1175 doAssert(key2
.compareTo(key3
) == Collator::EQUAL
,
1176 "Result should be \"abcda\" == \"abcda\"");
1178 int32_t keylength
= 0;
1179 doAssert(strcmp((const char *)(key2
.getByteArray(keylength
)),
1180 (const char *)key2compat
) == 0,
1181 "Binary format for 'abcda' sortkey different!");
1183 col
->getSortKey(test1
, sortkey1
, 64);
1184 col
->getSortKey(test2
, sortkey2
, 64);
1185 col
->getSortKey(test3
, sortkey3
, 64);
1187 const uint8_t *tempkey
= key1
.getByteArray(keylength
);
1188 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1189 "Test1 string should have the same collation key and sort key");
1190 tempkey
= key2
.getByteArray(keylength
);
1191 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1192 "Test2 string should have the same collation key and sort key");
1193 tempkey
= key3
.getByteArray(keylength
);
1194 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1195 "Test3 string should have the same collation key and sort key");
1197 col
->getSortKey(test1
, 5, sortkey1
, 64);
1198 col
->getSortKey(test2
, 5, sortkey2
, 64);
1199 col
->getSortKey(test3
, 5, sortkey3
, 64);
1201 tempkey
= key1
.getByteArray(keylength
);
1202 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1203 "Test1 string should have the same collation key and sort key");
1204 tempkey
= key2
.getByteArray(keylength
);
1205 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1206 "Test2 string should have the same collation key and sort key");
1207 tempkey
= key3
.getByteArray(keylength
);
1208 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1209 "Test3 string should have the same collation key and sort key");
1211 UnicodeString
strtest1(test1
);
1212 col
->getSortKey(strtest1
, sortkey1
, 64);
1213 UnicodeString
strtest2(test2
);
1214 col
->getSortKey(strtest2
, sortkey2
, 64);
1215 UnicodeString
strtest3(test3
);
1216 col
->getSortKey(strtest3
, sortkey3
, 64);
1218 tempkey
= key1
.getByteArray(keylength
);
1219 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1220 "Test1 string should have the same collation key and sort key");
1221 tempkey
= key2
.getByteArray(keylength
);
1222 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1223 "Test2 string should have the same collation key and sort key");
1224 tempkey
= key3
.getByteArray(keylength
);
1225 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1226 "Test3 string should have the same collation key and sort key");
1228 logln("Use secondary comparision level testing ...\n");
1229 col
->setStrength(Collator::SECONDARY
);
1231 col
->getCollationKey(test1
, u_strlen(test1
), key1
, status
);
1232 col
->getCollationKey(test2
, u_strlen(test2
), key2
, status
);
1233 col
->getCollationKey(test3
, u_strlen(test3
), key3
, status
);
1235 doAssert(key1
.compareTo(key2
) == Collator::EQUAL
,
1236 "Result should be \"Abcda\" == \"abcda\"");
1237 doAssert(key2
.compareTo(key3
) == Collator::EQUAL
,
1238 "Result should be \"abcda\" == \"abcda\"");
1240 tempkey
= key2
.getByteArray(keylength
);
1241 doAssert(memcmp(tempkey
, key2compat
, keylength
- 1) == 0,
1242 "Binary format for 'abcda' sortkey different!");
1244 col
->getSortKey(test1
, sortkey1
, 64);
1245 col
->getSortKey(test2
, sortkey2
, 64);
1246 col
->getSortKey(test3
, sortkey3
, 64);
1248 tempkey
= key1
.getByteArray(keylength
);
1249 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1250 "Test1 string should have the same collation key and sort key");
1251 tempkey
= key2
.getByteArray(keylength
);
1252 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1253 "Test2 string should have the same collation key and sort key");
1254 tempkey
= key3
.getByteArray(keylength
);
1255 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1256 "Test3 string should have the same collation key and sort key");
1258 col
->getSortKey(test1
, 5, sortkey1
, 64);
1259 col
->getSortKey(test2
, 5, sortkey2
, 64);
1260 col
->getSortKey(test3
, 5, sortkey3
, 64);
1262 tempkey
= key1
.getByteArray(keylength
);
1263 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1264 "Test1 string should have the same collation key and sort key");
1265 tempkey
= key2
.getByteArray(keylength
);
1266 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1267 "Test2 string should have the same collation key and sort key");
1268 tempkey
= key3
.getByteArray(keylength
);
1269 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1270 "Test3 string should have the same collation key and sort key");
1272 col
->getSortKey(strtest1
, sortkey1
, 64);
1273 col
->getSortKey(strtest2
, sortkey2
, 64);
1274 col
->getSortKey(strtest3
, sortkey3
, 64);
1276 tempkey
= key1
.getByteArray(keylength
);
1277 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1278 "Test1 string should have the same collation key and sort key");
1279 tempkey
= key2
.getByteArray(keylength
);
1280 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1281 "Test2 string should have the same collation key and sort key");
1282 tempkey
= key3
.getByteArray(keylength
);
1283 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1284 "Test3 string should have the same collation key and sort key");
1286 logln("testing sortkey ends...");
1290 void CollationAPITest::TestMaxExpansion()
1292 UErrorCode status
= U_ZERO_ERROR
;
1294 UChar32 unassigned
= 0xEFFFD;
1295 uint32_t sorder
= 0;
1296 uint32_t temporder
= 0;
1298 UnicodeString
rule("&a < ab < c/aba < d < z < ch");
1299 RuleBasedCollator
coll(rule
, status
);
1300 if(U_FAILURE(status
)) {
1301 errln("Collator creation failed with error %s", u_errorName(status
));
1304 UnicodeString
str(ch
);
1305 CollationElementIterator
*iter
=
1306 coll
.createCollationElementIterator(str
);
1308 while (ch
< 0xFFFF && U_SUCCESS(status
)) {
1315 str
.setCharAt(0, ch
);
1316 iter
->setText(str
, status
);
1317 order
= iter
->previous(status
);
1319 /* thai management */
1321 order
= iter
->previous(status
);
1323 while (U_SUCCESS(status
) && iter
->previous(status
) != UCOL_NULLORDER
) {
1327 size
= coll
.getMaxExpansion(order
);
1328 if (U_FAILURE(status
) || size
< count
) {
1329 errln("Failure at codepoint %d, maximum expansion count < %d\n",
1334 /* testing for exact max expansion */
1339 str
.setCharAt(0, ch
);
1340 iter
->setText(str
, status
);
1341 order
= iter
->previous(status
);
1342 size
= coll
.getMaxExpansion(order
);
1343 if (U_FAILURE(status
) || size
!= 1) {
1344 errln("Failure at codepoint %d, maximum expansion count < %d\n",
1352 iter
->setText(str
, status
);
1353 temporder
= iter
->previous(status
);
1355 if (U_FAILURE(status
) || coll
.getMaxExpansion(temporder
) != 3) {
1356 errln("Failure at codepoint %d, maximum expansion count != %d\n",
1362 iter
->setText(str
, status
);
1363 temporder
= iter
->previous(status
);
1365 if (U_FAILURE(status
) || coll
.getMaxExpansion(temporder
) != 1) {
1366 errln("Failure at codepoint %d, maximum expansion count != %d\n",
1370 str
.setTo(unassigned
);
1371 iter
->setText(str
, status
);
1372 sorder
= iter
->previous(status
);
1374 if (U_FAILURE(status
) || coll
.getMaxExpansion(sorder
) != 2) {
1375 errln("Failure at supplementary codepoints, maximum expansion count < %d\n",
1382 iter
->setText(str
, status
);
1383 temporder
= iter
->previous(status
);
1384 if (U_FAILURE(status
) || coll
.getMaxExpansion(temporder
) > 3) {
1385 errln("Failure at codepoint %d, maximum expansion count > %d\n",
1391 /* testing special jamo &a<\u1160 */
1392 rule
= CharsToUnicodeString("\\u0026\\u0071\\u003c\\u1165\\u002f\\u0071\\u0071\\u0071\\u0071");
1394 RuleBasedCollator
jamocoll(rule
, status
);
1395 iter
= jamocoll
.createCollationElementIterator(str
);
1396 temporder
= iter
->previous(status
);
1397 if (U_FAILURE(status
) || iter
->getMaxExpansion(temporder
) != 6) {
1398 errln("Failure at codepoint %d, maximum expansion count > %d\n",
1405 void CollationAPITest::TestDisplayName()
1407 UErrorCode error
= U_ZERO_ERROR
;
1408 Collator
*coll
= Collator::createInstance("en_US", error
);
1409 if (U_FAILURE(error
)) {
1410 errln("Failure creating english collator");
1414 UnicodeString result
;
1415 coll
->getDisplayName(Locale::getCanadaFrench(), result
);
1416 Locale::getCanadaFrench().getDisplayName(name
);
1417 if (result
.compare(name
)) {
1418 errln("Failure getting the correct name for locale en_US");
1421 coll
->getDisplayName(Locale::getSimplifiedChinese(), result
);
1422 Locale::getSimplifiedChinese().getDisplayName(name
);
1423 if (result
.compare(name
)) {
1424 errln("Failure getting the correct name for locale zh_SG");
1429 void CollationAPITest::TestAttribute()
1431 UErrorCode error
= U_ZERO_ERROR
;
1432 Collator
*coll
= Collator::createInstance(error
);
1434 if (U_FAILURE(error
)) {
1435 errln("Creation of default collator failed");
1439 coll
->setAttribute(UCOL_FRENCH_COLLATION
, UCOL_OFF
, error
);
1440 if (coll
->getAttribute(UCOL_FRENCH_COLLATION
, error
) != UCOL_OFF
||
1442 errln("Setting and retrieving of the french collation failed");
1445 coll
->setAttribute(UCOL_FRENCH_COLLATION
, UCOL_ON
, error
);
1446 if (coll
->getAttribute(UCOL_FRENCH_COLLATION
, error
) != UCOL_ON
||
1448 errln("Setting and retrieving of the french collation failed");
1451 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, error
);
1452 if (coll
->getAttribute(UCOL_ALTERNATE_HANDLING
, error
) != UCOL_SHIFTED
||
1454 errln("Setting and retrieving of the alternate handling failed");
1457 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, error
);
1458 if (coll
->getAttribute(UCOL_ALTERNATE_HANDLING
, error
) != UCOL_NON_IGNORABLE
||
1460 errln("Setting and retrieving of the alternate handling failed");
1463 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, error
);
1464 if (coll
->getAttribute(UCOL_CASE_FIRST
, error
) != UCOL_LOWER_FIRST
||
1466 errln("Setting and retrieving of the case first attribute failed");
1469 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, error
);
1470 if (coll
->getAttribute(UCOL_CASE_FIRST
, error
) != UCOL_UPPER_FIRST
||
1472 errln("Setting and retrieving of the case first attribute failed");
1475 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_ON
, error
);
1476 if (coll
->getAttribute(UCOL_CASE_LEVEL
, error
) != UCOL_ON
||
1478 errln("Setting and retrieving of the case level attribute failed");
1481 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, error
);
1482 if (coll
->getAttribute(UCOL_CASE_LEVEL
, error
) != UCOL_OFF
||
1484 errln("Setting and retrieving of the case level attribute failed");
1487 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, error
);
1488 if (coll
->getAttribute(UCOL_NORMALIZATION_MODE
, error
) != UCOL_ON
||
1490 errln("Setting and retrieving of the normalization on/off attribute failed");
1493 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_OFF
, error
);
1494 if (coll
->getAttribute(UCOL_NORMALIZATION_MODE
, error
) != UCOL_OFF
||
1496 errln("Setting and retrieving of the normalization on/off attribute failed");
1499 coll
->setAttribute(UCOL_STRENGTH
, UCOL_PRIMARY
, error
);
1500 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_PRIMARY
||
1502 errln("Setting and retrieving of the collation strength failed");
1505 coll
->setAttribute(UCOL_STRENGTH
, UCOL_SECONDARY
, error
);
1506 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_SECONDARY
||
1508 errln("Setting and retrieving of the collation strength failed");
1511 coll
->setAttribute(UCOL_STRENGTH
, UCOL_TERTIARY
, error
);
1512 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_TERTIARY
||
1514 errln("Setting and retrieving of the collation strength failed");
1517 coll
->setAttribute(UCOL_STRENGTH
, UCOL_QUATERNARY
, error
);
1518 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_QUATERNARY
||
1520 errln("Setting and retrieving of the collation strength failed");
1523 coll
->setAttribute(UCOL_STRENGTH
, UCOL_IDENTICAL
, error
);
1524 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_IDENTICAL
||
1526 errln("Setting and retrieving of the collation strength failed");
1532 void CollationAPITest::TestVariableTopSetting() {
1533 UErrorCode status
= U_ZERO_ERROR
;
1535 UChar vt
[256] = { 0 };
1537 Collator
*coll
= Collator::createInstance(status
);
1538 if(U_FAILURE(status
)) {
1540 errln("Collator creation failed with error %s", u_errorName(status
));
1544 uint32_t oldVarTop
= coll
->getVariableTop(status
);
1548 uint32_t newVarTop
= coll
->setVariableTop(vt
, 1, status
);
1550 if((newVarTop
& 0xFFFF0000) != (coll
->getVariableTop(status
) & 0xFFFF0000)) {
1551 errln("Didn't set vartop properly\n");
1554 coll
->setVariableTop(oldVarTop
, status
);
1556 uint32_t newerVarTop
= coll
->setVariableTop(UnicodeString(vt
, 1), status
);
1558 if((newVarTop
& 0xFFFF0000) != (newerVarTop
& 0xFFFF0000)) {
1559 errln("Didn't set vartop properly from UnicodeString!\n");
1566 void CollationAPITest::TestGetLocale() {
1567 UErrorCode status
= U_ZERO_ERROR
;
1568 const char *rules
= "&a<x<y<z";
1569 UChar rlz
[256] = {0};
1571 Collator
*coll
= NULL
;
1576 static const struct {
1577 const char* requestedLocale
;
1578 const char* validLocale
;
1579 const char* actualLocale
;
1581 { "sr_YU", "sr_YU", "root" },
1582 { "sh_YU", "sh_YU", "sh" },
1583 { "en_US_CALIFORNIA", "en_US", "root" },
1584 { "fr_FR_NONEXISTANT", "fr_FR", "fr" }
1587 u_unescape(rules
, rlz
, 256);
1589 /* test opening collators for different locales */
1590 for(i
= 0; i
<(int32_t)(sizeof(testStruct
)/sizeof(testStruct
[0])); i
++) {
1591 status
= U_ZERO_ERROR
;
1592 coll
= Collator::createInstance(testStruct
[i
].requestedLocale
, status
);
1593 if(U_FAILURE(status
)) {
1594 log("Failed to open collator for %s with %s\n", testStruct
[i
].requestedLocale
, u_errorName(status
));
1598 locale
= coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
);
1599 if(locale
!= testStruct
[i
].requestedLocale
) {
1600 log("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].requestedLocale
, locale
.getName());
1602 locale
= coll
->getLocale(ULOC_VALID_LOCALE
, status
);
1603 if(locale
!= testStruct
[i
].validLocale
) {
1604 log("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].validLocale
, locale
.getName());
1606 locale
= coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
);
1607 if(locale
!= testStruct
[i
].actualLocale
) {
1608 log("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].actualLocale
, locale
.getName());
1613 /* completely non-existant locale for collator should get a default collator */
1615 Collator
*defaultColl
= Collator::createInstance((const Locale
)NULL
, status
);
1616 coll
= Collator::createInstance("blahaha", status
);
1617 if(U_FAILURE(status
)) {
1618 log("Failed to open collator with %s\n", u_errorName(status
));
1623 if(coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
) != "blahaha") {
1624 log("Nonexisting locale didn't preserve the requested locale\n");
1626 if(coll
->getLocale(ULOC_VALID_LOCALE
, status
) !=
1627 defaultColl
->getLocale(ULOC_VALID_LOCALE
, status
)) {
1628 log("Valid locale for nonexisting locale locale collator differs "
1629 "from valid locale for default collator\n");
1631 if(coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
) !=
1632 defaultColl
->getLocale(ULOC_ACTUAL_LOCALE
, status
)) {
1633 log("Actual locale for nonexisting locale locale collator differs "
1634 "from actual locale for default collator\n");
1642 /* collator instantiated from rules should have all three locales NULL */
1643 coll
= new RuleBasedCollator(rlz
, status
);
1644 locale
= coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
);
1645 if(!locale
.isBogus()) {
1646 log("For collator instantiated from rules, requested locale %s is not bogus\n", locale
.getName());
1648 locale
= coll
->getLocale(ULOC_VALID_LOCALE
, status
);
1649 if(!locale
.isBogus()) {
1650 log("For collator instantiated from rules, valid locale %s is not bogus\n", locale
.getName());
1652 locale
= coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
);
1653 if(!locale
.isBogus()) {
1654 log("For collator instantiated from rules, actual locale %s is not bogus\n", locale
.getName());
1660 const char *original
;
1667 static int U_CALLCONV
1668 compare_teststruct(const void *string1
, const void *string2
) {
1669 return(strcmp((const char *)((struct teststruct
*)string1
)->key
, (const char *)((struct teststruct
*)string2
)->key
));
1674 void CollationAPITest::TestBounds(void) {
1675 UErrorCode status
= U_ZERO_ERROR
;
1677 Collator
*coll
= Collator::createInstance(Locale("sh"), status
);
1678 if(U_FAILURE(status
)) {
1680 errln("Collator creation failed with %s", u_errorName(status
));
1684 uint8_t sortkey
[512], lower
[512], upper
[512];
1687 static const char * const test
[] = {
1691 "j\\u00F6hn sm\\u00EFth",
1692 "J\\u00F6hn Sm\\u00EFth",
1693 "J\\u00D6HN SM\\u00CFTH",
1698 struct teststruct tests
[] = {
1699 {"\\u010CAKI MIHALJ", {0}},
1700 {"\\u010CAKI MIHALJ", {0}},
1701 {"\\u010CAKI PIRO\\u0160KA", {0}},
1702 {"\\u010CABAI ANDRIJA", {0}},
1703 {"\\u010CABAI LAJO\\u0160", {0}},
1704 {"\\u010CABAI MARIJA", {0}},
1705 {"\\u010CABAI STEVAN", {0}},
1706 {"\\u010CABAI STEVAN", {0}},
1707 {"\\u010CABARKAPA BRANKO", {0}},
1708 {"\\u010CABARKAPA MILENKO", {0}},
1709 {"\\u010CABARKAPA MIROSLAV", {0}},
1710 {"\\u010CABARKAPA SIMO", {0}},
1711 {"\\u010CABARKAPA STANKO", {0}},
1712 {"\\u010CABARKAPA TAMARA", {0}},
1713 {"\\u010CABARKAPA TOMA\\u0160", {0}},
1714 {"\\u010CABDARI\\u0106 NIKOLA", {0}},
1715 {"\\u010CABDARI\\u0106 ZORICA", {0}},
1716 {"\\u010CABI NANDOR", {0}},
1717 {"\\u010CABOVI\\u0106 MILAN", {0}},
1718 {"\\u010CABRADI AGNEZIJA", {0}},
1719 {"\\u010CABRADI IVAN", {0}},
1720 {"\\u010CABRADI JELENA", {0}},
1721 {"\\u010CABRADI LJUBICA", {0}},
1722 {"\\u010CABRADI STEVAN", {0}},
1723 {"\\u010CABRDA MARTIN", {0}},
1724 {"\\u010CABRILO BOGDAN", {0}},
1725 {"\\u010CABRILO BRANISLAV", {0}},
1726 {"\\u010CABRILO LAZAR", {0}},
1727 {"\\u010CABRILO LJUBICA", {0}},
1728 {"\\u010CABRILO SPASOJA", {0}},
1729 {"\\u010CADE\\u0160 ZDENKA", {0}},
1730 {"\\u010CADESKI BLAGOJE", {0}},
1731 {"\\u010CADOVSKI VLADIMIR", {0}},
1732 {"\\u010CAGLJEVI\\u0106 TOMA", {0}},
1733 {"\\u010CAGOROVI\\u0106 VLADIMIR", {0}},
1734 {"\\u010CAJA VANKA", {0}},
1735 {"\\u010CAJI\\u0106 BOGOLJUB", {0}},
1736 {"\\u010CAJI\\u0106 BORISLAV", {0}},
1737 {"\\u010CAJI\\u0106 RADOSLAV", {0}},
1738 {"\\u010CAK\\u0160IRAN MILADIN", {0}},
1739 {"\\u010CAKAN EUGEN", {0}},
1740 {"\\u010CAKAN EVGENIJE", {0}},
1741 {"\\u010CAKAN IVAN", {0}},
1742 {"\\u010CAKAN JULIJAN", {0}},
1743 {"\\u010CAKAN MIHAJLO", {0}},
1744 {"\\u010CAKAN STEVAN", {0}},
1745 {"\\u010CAKAN VLADIMIR", {0}},
1746 {"\\u010CAKAN VLADIMIR", {0}},
1747 {"\\u010CAKAN VLADIMIR", {0}},
1748 {"\\u010CAKARA ANA", {0}},
1749 {"\\u010CAKAREVI\\u0106 MOMIR", {0}},
1750 {"\\u010CAKAREVI\\u0106 NEDELJKO", {0}},
1751 {"\\u010CAKI \\u0160ANDOR", {0}},
1752 {"\\u010CAKI AMALIJA", {0}},
1753 {"\\u010CAKI ANDRA\\u0160", {0}},
1754 {"\\u010CAKI LADISLAV", {0}},
1755 {"\\u010CAKI LAJO\\u0160", {0}},
1756 {"\\u010CAKI LASLO", {0}}
1761 int32_t i
= 0, j
= 0, k
= 0, buffSize
= 0, skSize
= 0, lowerSize
= 0, upperSize
= 0;
1762 int32_t arraySize
= sizeof(tests
)/sizeof(tests
[0]);
1764 for(i
= 0; i
<arraySize
; i
++) {
1765 buffSize
= u_unescape(tests
[i
].original
, buffer
, 512);
1766 skSize
= coll
->getSortKey(buffer
, buffSize
, tests
[i
].key
, 512);
1769 qsort(tests
, arraySize
, sizeof(struct teststruct
), compare_teststruct
);
1771 for(i
= 0; i
< arraySize
-1; i
++) {
1772 for(j
= i
+1; j
< arraySize
; j
++) {
1773 lowerSize
= coll
->getBound(tests
[i
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, status
);
1774 upperSize
= coll
->getBound(tests
[j
].key
, -1, UCOL_BOUND_UPPER
, 1, upper
, 512, status
);
1775 for(k
= i
; k
<= j
; k
++) {
1776 if(strcmp((const char *)lower
, (const char *)tests
[k
].key
) > 0) {
1777 errln("Problem with lower! j = %i (%s vs %s)", k
, tests
[k
].original
, tests
[i
].original
);
1779 if(strcmp((const char *)upper
, (const char *)tests
[k
].key
) <= 0) {
1780 errln("Problem with upper! j = %i (%s vs %s)", k
, tests
[k
].original
, tests
[j
].original
);
1787 for(i
= 0; i
<(int32_t)(sizeof(test
)/sizeof(test
[0])); i
++) {
1788 buffSize
= u_unescape(test
[i
], buffer
, 512);
1789 skSize
= coll
->getSortKey(buffer
, buffSize
, sortkey
, 512);
1790 lowerSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1791 upperSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1792 for(j
= i
+1; j
<(int32_t)(sizeof(test
)/sizeof(test
[0])); j
++) {
1793 buffSize
= u_unescape(test
[j
], buffer
, 512);
1794 skSize
= coll
->getSortKey(buffer
, buffSize
, sortkey
, 512);
1795 if(strcmp((const char *)lower
, (const char *)sortkey
) > 0) {
1796 errln("Problem with lower! i = %i, j = %i (%s vs %s)", i
, j
, test
[i
], test
[j
]);
1798 if(strcmp((const char *)upper
, (const char *)sortkey
) <= 0) {
1799 errln("Problem with upper! i = %i, j = %i (%s vs %s)", i
, j
, test
[i
], test
[j
]);
1807 void CollationAPITest::TestGetTailoredSet()
1811 const char *tests
[20];
1814 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1815 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1818 uint32_t i
= 0, j
= 0;
1819 UErrorCode status
= U_ZERO_ERROR
;
1821 RuleBasedCollator
*coll
= NULL
;
1823 UnicodeSet
*set
= NULL
;
1825 for(i
= 0; i
< sizeof(setTest
)/sizeof(setTest
[0]); i
++) {
1826 buff
= UnicodeString(setTest
[i
].rules
, "").unescape();
1827 coll
= new RuleBasedCollator(buff
, status
);
1828 if(U_SUCCESS(status
)) {
1829 set
= coll
->getTailoredSet(status
);
1830 if(set
->size() != setTest
[i
].testsize
) {
1831 errln("Tailored set size different (%d) than expected (%d)", set
->size(), setTest
[i
].testsize
);
1833 for(j
= 0; j
< (uint32_t)setTest
[i
].testsize
; j
++) {
1834 buff
= UnicodeString(setTest
[i
].tests
[j
], "").unescape();
1835 if(!set
->contains(buff
)) {
1836 errln("Tailored set doesn't contain %s... It should", setTest
[i
].tests
[j
]);
1841 errln("Couldn't open collator with rules %s\n", setTest
[i
].rules
);
1847 void CollationAPITest::TestUClassID()
1849 char id
= *((char *)RuleBasedCollator::getStaticClassID());
1851 errln("Static class id for RuleBasedCollator should be 0");
1853 UErrorCode status
= U_ZERO_ERROR
;
1854 RuleBasedCollator
*coll
1855 = (RuleBasedCollator
*)Collator::createInstance(status
);
1856 if(U_FAILURE(status
)) {
1858 errln("Collator creation failed with %s", u_errorName(status
));
1861 id
= *((char *)coll
->getDynamicClassID());
1863 errln("Dynamic class id for RuleBasedCollator should be 0");
1865 id
= *((char *)CollationKey::getStaticClassID());
1867 errln("Static class id for CollationKey should be 0");
1869 CollationKey
*key
= new CollationKey();
1870 id
= *((char *)key
->getDynamicClassID());
1872 errln("Dynamic class id for CollationKey should be 0");
1874 id
= *((char *)CollationElementIterator::getStaticClassID());
1876 errln("Static class id for CollationElementIterator should be 0");
1878 UnicodeString
str("testing");
1879 CollationElementIterator
*iter
= coll
->createCollationElementIterator(str
);
1880 id
= *((char *)iter
->getDynamicClassID());
1882 errln("Dynamic class id for CollationElementIterator should be 0");
1889 class TestCollator
: public Collator
1892 virtual Collator
* clone(void) const;
1894 // dang, markus says we can't use 'using' in ICU. I hate doing this for
1895 // deprecated methods...
1897 // using Collator::compare;
1899 virtual EComparisonResult
compare(const UnicodeString
& source
,
1900 const UnicodeString
& target
) const
1902 return Collator::compare(source
, target
);
1905 virtual EComparisonResult
compare(const UnicodeString
& source
,
1906 const UnicodeString
& target
,
1907 int32_t length
) const
1909 return Collator::compare(source
, target
, length
);
1912 virtual EComparisonResult
compare(const UChar
* source
,
1913 int32_t sourceLength
,
1914 const UChar
* target
,
1915 int32_t targetLength
) const
1917 return Collator::compare(source
, sourceLength
, target
, targetLength
);
1921 virtual UCollationResult
compare(const UnicodeString
& source
,
1922 const UnicodeString
& target
,
1923 UErrorCode
& status
) const;
1924 virtual UCollationResult
compare(const UnicodeString
& source
,
1925 const UnicodeString
& target
,
1927 UErrorCode
& status
) const;
1928 virtual UCollationResult
compare(const UChar
* source
,
1929 int32_t sourceLength
,
1930 const UChar
* target
,
1931 int32_t targetLength
,
1932 UErrorCode
& status
) const;
1933 virtual CollationKey
& getCollationKey(const UnicodeString
& source
,
1935 UErrorCode
& status
) const;
1936 virtual CollationKey
& getCollationKey(const UChar
*source
,
1937 int32_t sourceLength
,
1939 UErrorCode
& status
) const;
1940 virtual int32_t hashCode(void) const;
1941 virtual const Locale
getLocale(ULocDataLocaleType type
,
1942 UErrorCode
& status
) const;
1943 virtual ECollationStrength
getStrength(void) const;
1944 virtual void setStrength(ECollationStrength newStrength
);
1945 virtual UClassID
getDynamicClassID(void) const;
1946 virtual void getVersion(UVersionInfo info
) const;
1947 virtual void setAttribute(UColAttribute attr
, UColAttributeValue value
,
1948 UErrorCode
&status
);
1949 virtual UColAttributeValue
getAttribute(UColAttribute attr
,
1950 UErrorCode
&status
);
1951 virtual uint32_t setVariableTop(const UChar
*varTop
, int32_t len
,
1952 UErrorCode
&status
);
1953 virtual uint32_t setVariableTop(const UnicodeString varTop
,
1954 UErrorCode
&status
);
1955 virtual void setVariableTop(const uint32_t varTop
, UErrorCode
&status
);
1956 virtual uint32_t getVariableTop(UErrorCode
&status
) const;
1957 virtual Collator
* safeClone(void);
1958 virtual int32_t getSortKey(const UnicodeString
& source
,
1960 int32_t resultLength
) const;
1961 virtual int32_t getSortKey(const UChar
*source
, int32_t sourceLength
,
1962 uint8_t*result
, int32_t resultLength
) const;
1963 virtual UnicodeSet
*getTailoredSet(UErrorCode
&status
) const;
1964 virtual UBool
operator!=(const Collator
& other
) const;
1965 virtual void setLocales(const Locale
& requestedLocale
, const Locale
& validLocale
, const Locale
& actualLocale
);
1966 TestCollator() : Collator() {};
1967 TestCollator(UCollationStrength collationStrength
,
1968 UNormalizationMode decompositionMode
) : Collator(collationStrength
, decompositionMode
) {};
1971 inline UBool
TestCollator::operator!=(const Collator
& other
) const {
1972 return Collator::operator!=(other
);
1975 #define returnEComparisonResult(data) \
1976 if (data < 0) return Collator::LESS;\
1977 if (data > 0) return Collator::GREATER;\
1978 return Collator::EQUAL;
1980 Collator
* TestCollator::clone() const
1982 return new TestCollator();
1985 UCollationResult
TestCollator::compare(const UnicodeString
& source
,
1986 const UnicodeString
& target
,
1987 UErrorCode
& status
) const
1989 if(U_SUCCESS(status
)) {
1990 return UCollationResult(source
.compare(target
));
1996 UCollationResult
TestCollator::compare(const UnicodeString
& source
,
1997 const UnicodeString
& target
,
1999 UErrorCode
& status
) const
2001 if(U_SUCCESS(status
)) {
2002 return UCollationResult(source
.compare(0, length
, target
));
2008 UCollationResult
TestCollator::compare(const UChar
* source
,
2009 int32_t sourceLength
,
2010 const UChar
* target
,
2011 int32_t targetLength
,
2012 UErrorCode
& status
) const
2014 UnicodeString
s(source
, sourceLength
);
2015 UnicodeString
t(target
, targetLength
);
2016 return compare(s
, t
, status
);
2019 CollationKey
& TestCollator::getCollationKey(const UnicodeString
& source
,
2021 UErrorCode
& status
) const
2025 length
= source
.extract(temp
, length
, NULL
, status
);
2027 CollationKey
tempkey((uint8_t*)temp
, length
);
2032 CollationKey
& TestCollator::getCollationKey(const UChar
*source
,
2033 int32_t sourceLength
,
2035 UErrorCode
& status
) const
2037 //s tack allocation used since collationkey does not keep the unicodestring
2038 UnicodeString
str(source
, sourceLength
);
2039 return getCollationKey(str
, key
, status
);
2042 int32_t TestCollator::getSortKey(const UnicodeString
& source
, uint8_t* result
,
2043 int32_t resultLength
) const
2045 UErrorCode status
= U_ZERO_ERROR
;
2046 int32_t length
= source
.extract((char *)result
, resultLength
, NULL
,
2052 int32_t TestCollator::getSortKey(const UChar
*source
, int32_t sourceLength
,
2053 uint8_t*result
, int32_t resultLength
) const
2055 UnicodeString
str(source
, sourceLength
);
2056 return getSortKey(str
, result
, resultLength
);
2059 int32_t TestCollator::hashCode() const
2064 const Locale
TestCollator::getLocale(ULocDataLocaleType type
,
2065 UErrorCode
& status
) const
2067 // api not used, this is to make the compiler happy
2068 if (U_FAILURE(status
)) {
2069 type
= ULOC_DATA_LOCALE_TYPE_LIMIT
;
2074 Collator::ECollationStrength
TestCollator::getStrength() const
2079 void TestCollator::setStrength(Collator::ECollationStrength newStrength
)
2081 // api not used, this is to make the compiler happy
2082 newStrength
= TERTIARY
;
2085 UClassID
TestCollator::getDynamicClassID(void) const
2090 void TestCollator::getVersion(UVersionInfo info
) const
2092 // api not used, this is to make the compiler happy
2093 memset(info
, 0, U_MAX_VERSION_LENGTH
);
2096 void TestCollator::setAttribute(UColAttribute attr
, UColAttributeValue value
,
2099 // api not used, this is to make the compiler happy
2100 if (U_FAILURE(status
)) {
2101 attr
= UCOL_ATTRIBUTE_COUNT
;
2106 UColAttributeValue
TestCollator::getAttribute(UColAttribute attr
,
2109 // api not used, this is to make the compiler happy
2110 if (U_FAILURE(status
) || attr
== UCOL_ATTRIBUTE_COUNT
) {
2113 return UCOL_DEFAULT
;
2116 uint32_t TestCollator::setVariableTop(const UChar
*varTop
, int32_t len
,
2119 // api not used, this is to make the compiler happy
2120 if (U_SUCCESS(status
) && (varTop
== 0 || len
< -1)) {
2121 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2126 uint32_t TestCollator::setVariableTop(const UnicodeString varTop
,
2129 // api not used, this is to make the compiler happy
2130 if (U_SUCCESS(status
) && varTop
.length() == 0) {
2131 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2136 void TestCollator::setVariableTop(const uint32_t varTop
, UErrorCode
&status
)
2138 // api not used, this is to make the compiler happy
2139 if (U_SUCCESS(status
) && varTop
== 0) {
2140 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2144 uint32_t TestCollator::getVariableTop(UErrorCode
&status
) const
2147 // api not used, this is to make the compiler happy
2148 if (U_SUCCESS(status
)) {
2151 return (uint32_t)(0xFFFFFFFFu
);
2154 Collator
* TestCollator::safeClone(void)
2156 return new TestCollator();
2159 UnicodeSet
* TestCollator::getTailoredSet(UErrorCode
&status
) const
2161 return Collator::getTailoredSet(status
);
2164 void TestCollator::setLocales(const Locale
& requestedLocale
, const Locale
& validLocale
, const Locale
& actualLocale
)
2166 Collator::setLocales(requestedLocale
, validLocale
, actualLocale
);
2170 void CollationAPITest::TestSubclass()
2174 doAssert(col1
!= col2
, "2 instance of TestCollator should be different");
2175 if (col1
.hashCode() != col2
.hashCode()) {
2176 errln("Every TestCollator has the same hashcode");
2178 UnicodeString
abc("abc", 3);
2179 UnicodeString
bcd("bcd", 3);
2180 if (col1
.compare(abc
, bcd
) != abc
.compare(bcd
)) {
2181 errln("TestCollator compare should be the same as the default "
2182 "string comparison");
2185 UErrorCode status
= U_ZERO_ERROR
;
2186 col1
.getCollationKey(abc
, key
, status
);
2188 const char* bytes
= (const char *)key
.getByteArray(length
);
2189 UnicodeString
keyarray(bytes
, length
, NULL
, status
);
2190 if (abc
!= keyarray
) {
2191 errln("TestCollator collationkey API is returning wrong values");
2194 UnicodeSet
expectedset(0, 0x10FFFF);
2195 UnicodeSet
*defaultset
= col1
.getTailoredSet(status
);
2196 if (!defaultset
->containsAll(expectedset
)
2197 || !expectedset
.containsAll(*defaultset
)) {
2198 errln("Error: expected default tailoring to be 0 to 0x10ffff");
2202 // use base class implementation
2203 Locale loc1
= Locale::getGermany();
2204 Locale loc2
= Locale::getFrance();
2205 col1
.setLocales(loc1
, loc2
, loc2
); // default implementation has no effect
2207 UnicodeString displayName
;
2208 col1
.getDisplayName(loc1
, loc2
, displayName
); // de_DE collator in fr_FR locale
2210 TestCollator
col3(UCOL_TERTIARY
, UNORM_NONE
);
2211 UnicodeString
a("a");
2212 UnicodeString
b("b");
2213 Collator::EComparisonResult result
= Collator::EComparisonResult(a
.compare(b
));
2214 if(col1
.compare(a
, b
) != result
) {
2215 errln("Collator doesn't give default result");
2217 if(col1
.compare(a
, b
, 1) != result
) {
2218 errln("Collator doesn't give default result");
2220 if(col1
.compare(a
.getBuffer(), a
.length(), b
.getBuffer(), b
.length()) != result
) {
2221 errln("Collator doesn't give default result");
2225 void CollationAPITest::TestNULLCharTailoring()
2227 UErrorCode status
= U_ZERO_ERROR
;
2228 UChar buf
[256] = {0};
2229 int32_t len
= u_unescape("&a < '\\u0000'", buf
, 256);
2230 UnicodeString
first((UChar
)0x0061);
2231 UnicodeString
second((UChar
)0);
2232 RuleBasedCollator
*coll
= new RuleBasedCollator(UnicodeString(buf
, len
), status
);
2233 if(U_FAILURE(status
)) {
2234 errln("Failed to open collator");
2236 UCollationResult res
= coll
->compare(first
, second
, status
);
2237 if(res
!= UCOL_LESS
) {
2238 errln("a should be less then NULL after tailoring");
2243 void CollationAPITest::TestClone() {
2245 UErrorCode status
= U_ZERO_ERROR
;
2246 RuleBasedCollator
* c0
= (RuleBasedCollator
*)Collator::createInstance(status
);
2248 if (U_FAILURE(status
)) {
2249 errln("Collator::CreateInstance(status) failed with %s", u_errorName(status
));
2253 c0
->setStrength(Collator::TERTIARY
);
2254 dump("c0", c0
, status
);
2257 RuleBasedCollator
* c1
= (RuleBasedCollator
*)Collator::createInstance(status
);
2258 c1
->setStrength(Collator::TERTIARY
);
2259 UColAttributeValue val
= c1
->getAttribute(UCOL_CASE_FIRST
, status
);
2260 if(val
== UCOL_LOWER_FIRST
){
2261 c1
->setAttribute(UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, status
);
2263 c1
->setAttribute(UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, status
);
2265 dump("c0", c0
, status
);
2266 dump("c1", c1
, status
);
2269 RuleBasedCollator
* c2
= (RuleBasedCollator
*)c1
->clone();
2270 val
= c2
->getAttribute(UCOL_CASE_FIRST
, status
);
2271 if(val
== UCOL_LOWER_FIRST
){
2272 c2
->setAttribute(UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, status
);
2274 c2
->setAttribute(UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, status
);
2276 if(U_FAILURE(status
)){
2277 errln("set and get attributes of collator failed. %s\n", u_errorName(status
));
2280 dump("c0", c0
, status
);
2281 dump("c1", c1
, status
);
2282 dump("c2", c2
, status
);
2284 errln("The cloned objects refer to same data");
2291 void CollationAPITest::dump(UnicodeString msg
, RuleBasedCollator
* c
, UErrorCode
& status
) {
2292 const char* bigone
= "One";
2293 const char* littleone
= "one";
2295 logln(msg
+ " " + c
->compare(bigone
, littleone
) +
2296 " s: " + c
->getStrength() +
2297 " u: " + c
->getAttribute(UCOL_CASE_FIRST
, status
));
2299 void CollationAPITest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par */)
2301 if (exec
) logln("TestSuite CollationAPITest: ");
2303 case 0: name
= "TestProperty"; if (exec
) TestProperty(/* par */); break;
2304 case 1: name
= "TestOperators"; if (exec
) TestOperators(/* par */); break;
2305 case 2: name
= "TestDuplicate"; if (exec
) TestDuplicate(/* par */); break;
2306 case 3: name
= "TestCompare"; if (exec
) TestCompare(/* par */); break;
2307 case 4: name
= "TestHashCode"; if (exec
) TestHashCode(/* par */); break;
2308 case 5: name
= "TestCollationKey"; if (exec
) TestCollationKey(/* par */); break;
2309 case 6: name
= "TestElemIter"; if (exec
) TestElemIter(/* par */); break;
2310 case 7: name
= "TestGetAll"; if (exec
) TestGetAll(/* par */); break;
2311 case 8: name
= "TestRuleBasedColl"; if (exec
) TestRuleBasedColl(/* par */); break;
2312 case 9: name
= "TestDecomposition"; if (exec
) TestDecomposition(/* par */); break;
2313 case 10: name
= "TestSafeClone"; if (exec
) TestSafeClone(/* par */); break;
2314 case 11: name
= "TestSortKey"; if (exec
) TestSortKey(); break;
2315 case 12: name
= "TestMaxExpansion"; if (exec
) TestMaxExpansion(); break;
2316 case 13: name
= "TestDisplayName"; if (exec
) TestDisplayName(); break;
2317 case 14: name
= "TestAttribute"; if (exec
) TestAttribute(); break;
2318 case 15: name
= "TestVariableTopSetting"; if (exec
) TestVariableTopSetting(); break;
2319 case 16: name
= "TestRules"; if (exec
) TestRules(); break;
2320 case 17: name
= "TestGetLocale"; if (exec
) TestGetLocale(); break;
2321 case 18: name
= "TestBounds"; if (exec
) TestBounds(); break;
2322 case 19: name
= "TestGetTailoredSet"; if (exec
) TestGetTailoredSet(); break;
2323 case 20: name
= "TestUClassID"; if (exec
) TestUClassID(); break;
2324 case 21: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
2325 case 22: name
= "TestNULLCharTailoring"; if (exec
) TestNULLCharTailoring(); break;
2326 case 23: name
= "TestClone"; if (exec
) TestClone(); break;
2327 default: name
= ""; break;
2331 #endif /* #if !UCONFIG_NO_COLLATION */