1 /********************************************************************
3 * Copyright (c) 1997-2010, 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/localpointer.h"
35 #include "unicode/coll.h"
36 #include "unicode/tblcoll.h"
37 #include "unicode/coleitr.h"
38 #include "unicode/sortkey.h"
40 #include "unicode/chariter.h"
41 #include "unicode/schriter.h"
42 #include "unicode/ustring.h"
43 #include "unicode/ucol.h"
50 CollationAPITest::doAssert(UBool condition
, const char *message
)
53 errln(UnicodeString("ERROR : ") + message
);
57 #ifdef U_USE_COLLATION_OBSOLETE_2_6
59 * Test Collator::createInstance(... version...) for some locale. Called by TestProperty().
62 TestOpenVersion(IntlTest
&test
, const Locale
&locale
) {
63 UVersionInfo version1
, version2
;
64 Collator
*collator1
, *collator2
;
67 errorCode
=U_ZERO_ERROR
;
68 collator1
=Collator::createInstance(locale
, errorCode
);
69 if(U_SUCCESS(errorCode
)) {
70 /* get the current version */
71 collator1
->getVersion(version1
);
74 /* try to get that same version again */
75 collator2
=Collator::createInstance(locale
, version1
, errorCode
);
76 if(U_SUCCESS(errorCode
)) {
77 collator2
->getVersion(version2
);
78 if(0!=uprv_memcmp(version1
, version2
, sizeof(UVersionInfo
))) {
79 test
.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) returns a different collator\n", locale
.getName(), locale
.getName());
83 test
.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) fails: %s\n", locale
.getName(), locale
.getName(), u_errorName(errorCode
));
89 // Collator Class Properties
90 // ctor, dtor, createInstance, compare, getStrength/setStrength
91 // getDecomposition/setDecomposition, getDisplayName
93 CollationAPITest::TestProperty(/* char* par */)
95 UErrorCode success
= U_ZERO_ERROR
;
98 * Expected version of the English collator.
99 * Currently, the major/minor version numbers change when the builder code
101 * number 2 is from the tailoring data version and
102 * number 3 is the UCA version.
103 * This changes with every UCA version change, and the expected value
104 * needs to be adjusted.
105 * Same in cintltst/capitst.c.
107 UVersionInfo currVersionArray
= {0x31, 0xC0, 0x05, 0x2A}; // from ICU 4.4/UCA 5.2
108 UVersionInfo versionArray
;
110 logln("The property tests begin : ");
111 logln("Test ctors : ");
112 col
= Collator::createInstance(Locale::getEnglish(), success
);
113 if (U_FAILURE(success
)){
114 errcheckln(success
, "Default Collator creation failed. - %s", u_errorName(success
));
118 StringEnumeration
* kwEnum
= col
->getKeywordValuesForLocale("", Locale::getEnglish(),true,success
);
119 if (U_FAILURE(success
)){
120 errcheckln(success
, "Get Keyword Values for Locale failed. - %s", u_errorName(success
));
125 col
->getVersion(versionArray
);
126 // Check for a version greater than some value rather than equality
127 // so that we need not update the expected version each time.
128 if (uprv_memcmp(versionArray
, currVersionArray
, 4)<0) {
129 errln("Testing Collator::getVersion() - unexpected result: %02x.%02x.%02x.%02x",
130 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
132 logln("Collator::getVersion() result: %02x.%02x.%02x.%02x",
133 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
136 doAssert((col
->compare("ab", "abc") == Collator::LESS
), "ab < abc comparison failed");
137 doAssert((col
->compare("ab", "AB") == Collator::LESS
), "ab < AB comparison failed");
138 doAssert((col
->compare("blackbird", "black-bird") == Collator::GREATER
), "black-bird > blackbird comparison failed");
139 doAssert((col
->compare("black bird", "black-bird") == Collator::LESS
), "black bird > black-bird comparison failed");
140 doAssert((col
->compare("Hello", "hello") == Collator::GREATER
), "Hello > hello comparison failed");
141 doAssert((col
->compare("","",success
) == UCOL_EQUAL
), "Comparison between empty strings failed");
143 doAssert((col
->compareUTF8("\x61\x62\xc3\xa4", "\x61\x62\xc3\x9f", success
) == UCOL_LESS
), "ab a-umlaut < ab sharp-s UTF-8 comparison failed");
144 success
= U_ZERO_ERROR
;
146 UnicodeString abau
=UNICODE_STRING_SIMPLE("\\x61\\x62\\xe4").unescape();
147 UnicodeString abss
=UNICODE_STRING_SIMPLE("\\x61\\x62\\xdf").unescape();
148 UCharIterator abauIter
, abssIter
;
149 uiter_setReplaceable(&abauIter
, &abau
);
150 uiter_setReplaceable(&abssIter
, &abss
);
151 doAssert((col
->compare(abauIter
, abssIter
, success
) == UCOL_LESS
), "ab a-umlaut < ab sharp-s UCharIterator comparison failed");
152 success
= U_ZERO_ERROR
;
155 /*start of update [Bertrand A. D. 02/10/98]*/
156 doAssert((col
->compare("ab", "abc", 2) == Collator::EQUAL
), "ab = abc with length 2 comparison failed");
157 doAssert((col
->compare("ab", "AB", 2) == Collator::LESS
), "ab < AB with length 2 comparison failed");
158 doAssert((col
->compare("ab", "Aa", 1) == Collator::LESS
), "ab < Aa with length 1 comparison failed");
159 doAssert((col
->compare("ab", "Aa", 2) == Collator::GREATER
), "ab > Aa with length 2 comparison failed");
160 doAssert((col
->compare("black-bird", "blackbird", 5) == Collator::EQUAL
), "black-bird = blackbird with length of 5 comparison failed");
161 doAssert((col
->compare("black bird", "black-bird", 10) == Collator::LESS
), "black bird < black-bird with length 10 comparison failed");
162 doAssert((col
->compare("Hello", "hello", 5) == Collator::GREATER
), "Hello > hello with length 5 comparison failed");
163 /*end of update [Bertrand A. D. 02/10/98]*/
166 logln("Test ctors ends.");
167 logln("testing Collator::getStrength() method ...");
168 doAssert((col
->getStrength() == Collator::TERTIARY
), "collation object has the wrong strength");
169 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
172 logln("testing Collator::setStrength() method ...");
173 col
->setStrength(Collator::SECONDARY
);
174 doAssert((col
->getStrength() != Collator::TERTIARY
), "collation object's strength is secondary difference");
175 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
176 doAssert((col
->getStrength() == Collator::SECONDARY
), "collation object has the wrong strength");
180 logln("Get display name for the US English collation in German : ");
181 logln(Collator::getDisplayName(Locale::getUS(), Locale::getGerman(), name
));
182 doAssert((name
== UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed");
184 logln("Get display name for the US English collation in English : ");
185 logln(Collator::getDisplayName(Locale::getUS(), Locale::getEnglish(), name
));
186 doAssert((name
== UnicodeString("English (United States)")), "getDisplayName failed");
188 // weiv : this test is bogus if we're running on any machine that has different default locale than English.
189 // Therefore, it is banned!
190 logln("Get display name for the US English in default locale language : ");
191 logln(Collator::getDisplayName(Locale::US
, name
));
192 doAssert((name
== UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine");
195 RuleBasedCollator
*rcol
= (RuleBasedCollator
*)Collator::createInstance("da_DK",
197 doAssert(rcol
->getRules().length() != 0, "da_DK rules does not have length 0");
200 col
= Collator::createInstance(Locale::getFrench(), success
);
201 if (U_FAILURE(success
))
203 errln("Creating French collation failed.");
207 col
->setStrength(Collator::PRIMARY
);
208 logln("testing Collator::getStrength() method again ...");
209 doAssert((col
->getStrength() != Collator::TERTIARY
), "collation object has the wrong strength");
210 doAssert((col
->getStrength() == Collator::PRIMARY
), "collation object's strength is not primary difference");
212 logln("testing French Collator::setStrength() method ...");
213 col
->setStrength(Collator::TERTIARY
);
214 doAssert((col
->getStrength() == Collator::TERTIARY
), "collation object's strength is not tertiary difference");
215 doAssert((col
->getStrength() != Collator::PRIMARY
), "collation object's strength is primary difference");
216 doAssert((col
->getStrength() != Collator::SECONDARY
), "collation object's strength is secondary difference");
218 logln("Create junk collation: ");
219 Locale
abcd("ab", "CD", "");
220 success
= U_ZERO_ERROR
;
222 junk
= Collator::createInstance(abcd
, success
);
224 if (U_FAILURE(success
))
226 errln("Junk collation creation failed, should at least return default.");
232 col
= Collator::createInstance(success
);
233 if (U_FAILURE(success
))
235 errln("Creating default collator failed.");
240 doAssert(((RuleBasedCollator
*)col
)->getRules() == ((RuleBasedCollator
*)junk
)->getRules(),
241 "The default collation should be returned.");
242 Collator
*frCol
= Collator::createInstance(Locale::getCanadaFrench(), success
);
243 if (U_FAILURE(success
))
245 errln("Creating fr_CA collator failed.");
251 // If the default locale isn't French, the French and non-French collators
252 // should be different
253 if (frCol
->getLocale(ULOC_ACTUAL_LOCALE
, success
) != Locale::getCanadaFrench()) {
254 doAssert((*frCol
!= *junk
), "The junk is the same as the fr_CA collator.");
256 Collator
*aFrCol
= frCol
->clone();
257 doAssert((*frCol
== *aFrCol
), "The cloning of a fr_CA collator failed.");
258 logln("Collator property test ended.");
265 #ifdef U_USE_COLLATION_OBSOLETE_2_6
266 /* test Collator::createInstance(...version...) */
267 TestOpenVersion(*this, "");
268 TestOpenVersion(*this, "da");
269 TestOpenVersion(*this, "fr");
270 TestOpenVersion(*this, "ja");
272 /* try some bogus version */
274 versionArray
[1]=0x99;
275 versionArray
[2]=0xc7;
276 versionArray
[3]=0xfe;
277 col
=Collator::createInstance(Locale(), versionArray
, success
);
278 if(U_SUCCESS(success
)) {
279 errln("error: ucol_openVersion(bogus version) succeeded");
286 CollationAPITest::TestRuleBasedColl()
288 RuleBasedCollator
*col1
, *col2
, *col3
, *col4
;
289 UErrorCode status
= U_ZERO_ERROR
;
291 UnicodeString
ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
292 UnicodeString
ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
294 col1
= new RuleBasedCollator(ruleset1
, status
);
295 if (U_FAILURE(status
)) {
296 errcheckln(status
, "RuleBased Collator creation failed. - %s", u_errorName(status
));
300 logln("PASS: RuleBased Collator creation passed\n");
303 status
= U_ZERO_ERROR
;
304 col2
= new RuleBasedCollator(ruleset2
, status
);
305 if (U_FAILURE(status
)) {
306 errln("RuleBased Collator creation failed.\n");
310 logln("PASS: RuleBased Collator creation passed\n");
313 status
= U_ZERO_ERROR
;
314 Locale
locale("aa", "AA");
315 col3
= (RuleBasedCollator
*)Collator::createInstance(locale
, status
);
316 if (U_FAILURE(status
)) {
317 errln("Fallback Collator creation failed.: %s\n");
321 logln("PASS: Fallback Collator creation passed\n");
325 status
= U_ZERO_ERROR
;
326 col3
= (RuleBasedCollator
*)Collator::createInstance(status
);
327 if (U_FAILURE(status
)) {
328 errln("Default Collator creation failed.: %s\n");
332 logln("PASS: Default Collator creation passed\n");
335 UnicodeString rule1
= col1
->getRules();
336 UnicodeString rule2
= col2
->getRules();
337 UnicodeString rule3
= col3
->getRules();
339 doAssert(rule1
!= rule2
, "Default collator getRules failed");
340 doAssert(rule2
!= rule3
, "Default collator getRules failed");
341 doAssert(rule1
!= rule3
, "Default collator getRules failed");
343 col4
= new RuleBasedCollator(rule2
, status
);
344 if (U_FAILURE(status
)) {
345 errln("RuleBased Collator creation failed.\n");
349 UnicodeString rule4
= col4
->getRules();
350 doAssert(rule2
== rule4
, "Default collator getRules failed");
352 uint8_t *clonedrule4
= col4
->cloneRuleData(length4
, status
);
353 if (U_FAILURE(status
)) {
354 errln("Cloned rule data failed.\n");
358 // free(clonedrule4); BAD API!!!!
359 uprv_free(clonedrule4
);
369 CollationAPITest::TestRules()
371 RuleBasedCollator
*coll
;
372 UErrorCode status
= U_ZERO_ERROR
;
375 coll
= (RuleBasedCollator
*)Collator::createInstance(Locale::getEnglish(), status
);
376 if (U_FAILURE(status
)) {
377 errcheckln(status
, "English Collator creation failed. - %s", u_errorName(status
));
381 logln("PASS: RuleBased Collator creation passed\n");
384 coll
->getRules(UCOL_TAILORING_ONLY
, rules
);
385 if (rules
.length() != 0x00) {
386 errln("English tailored rules failed - length is 0x%x expected 0x%x", rules
.length(), 0x00);
389 coll
->getRules(UCOL_FULL_RULES
, rules
);
390 if (rules
.length() < 0) {
391 errln("English full rules failed");
397 CollationAPITest::TestDecomposition() {
398 UErrorCode status
= U_ZERO_ERROR
;
399 Collator
*en_US
= Collator::createInstance("en_US", status
),
400 *el_GR
= Collator::createInstance("el_GR", status
),
401 *vi_VN
= Collator::createInstance("vi_VN", status
);
403 if (U_FAILURE(status
)) {
404 errcheckln(status
, "ERROR: collation creation failed. - %s", u_errorName(status
));
408 /* there is no reason to have canonical decomposition in en_US OR default locale */
409 if (vi_VN
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_ON
)
411 errln("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
414 if (el_GR
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_ON
)
416 errln("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
419 if (en_US
->getAttribute(UCOL_NORMALIZATION_MODE
, status
) != UCOL_OFF
)
421 errln("ERROR: en_US collation had canonical decomposition for normalization!\n");
430 CollationAPITest::TestSafeClone() {
431 static const int CLONETEST_COLLATOR_COUNT
= 3;
432 Collator
*someCollators
[CLONETEST_COLLATOR_COUNT
];
434 UErrorCode err
= U_ZERO_ERROR
;
437 UnicodeString
test1("abCda");
438 UnicodeString
test2("abcda");
440 /* one default collator & two complex ones */
441 someCollators
[0] = Collator::createInstance("en_US", err
);
442 someCollators
[1] = Collator::createInstance("ko", err
);
443 someCollators
[2] = Collator::createInstance("ja_JP", err
);
445 errcheckln(err
, "Couldn't instantiate collators. Error: %s", u_errorName(err
));
446 delete someCollators
[0];
447 delete someCollators
[1];
448 delete someCollators
[2];
452 /* change orig & clone & make sure they are independent */
454 for (index
= 0; index
< CLONETEST_COLLATOR_COUNT
; index
++)
456 col
= someCollators
[index
]->safeClone();
458 errln("SafeClone of collator should not return null\n");
461 col
->setStrength(Collator::TERTIARY
);
462 someCollators
[index
]->setStrength(Collator::PRIMARY
);
463 col
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, err
);
464 someCollators
[index
]->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, err
);
466 doAssert(col
->greater(test1
, test2
), "Result should be \"abCda\" >>> \"abcda\" ");
467 doAssert(someCollators
[index
]->equals(test1
, test2
), "Result should be \"abcda\" == \"abCda\"");
469 delete someCollators
[index
];
474 CollationAPITest::TestHashCode(/* char* par */)
476 logln("hashCode tests begin.");
477 UErrorCode success
= U_ZERO_ERROR
;
479 col1
= Collator::createInstance(Locale::getEnglish(), success
);
480 if (U_FAILURE(success
))
482 errcheckln(success
, "Default collation creation failed. - %s", u_errorName(success
));
487 Locale
dk("da", "DK", "");
488 col2
= Collator::createInstance(dk
, success
);
489 if (U_FAILURE(success
))
491 errln("Danish collation creation failed.");
496 col3
= Collator::createInstance(Locale::getEnglish(), success
);
497 if (U_FAILURE(success
))
499 errln("2nd default collation creation failed.");
503 logln("Collator::hashCode() testing ...");
505 doAssert(col1
->hashCode() != col2
->hashCode(), "Hash test1 result incorrect" );
506 doAssert(!(col1
->hashCode() == col2
->hashCode()), "Hash test2 result incorrect" );
507 doAssert(col1
->hashCode() == col3
->hashCode(), "Hash result not equal" );
509 logln("hashCode tests end.");
513 UnicodeString
test1("Abcda");
514 UnicodeString
test2("abcda");
516 CollationKey sortk1
, sortk2
, sortk3
;
517 UErrorCode status
= U_ZERO_ERROR
;
519 col3
->getCollationKey(test1
, sortk1
, status
);
520 col3
->getCollationKey(test2
, sortk2
, status
);
521 col3
->getCollationKey(test2
, sortk3
, status
);
523 doAssert(sortk1
.hashCode() != sortk2
.hashCode(), "Hash test1 result incorrect");
524 doAssert(sortk2
.hashCode() == sortk3
.hashCode(), "Hash result not equal" );
529 //----------------------------------------------------------------------------
530 // CollationKey -- Tests the CollationKey methods
533 CollationAPITest::TestCollationKey(/* char* par */)
535 logln("testing CollationKey begins...");
537 UErrorCode success
=U_ZERO_ERROR
;
538 col
= Collator::createInstance(Locale::getEnglish(), success
);
539 if (U_FAILURE(success
))
541 errcheckln(success
, "Default collation creation failed. - %s", u_errorName(success
));
544 col
->setStrength(Collator::TERTIARY
);
546 CollationKey sortk1
, sortk2
;
547 UnicodeString
test1("Abcda"), test2("abcda");
548 UErrorCode key1Status
= U_ZERO_ERROR
, key2Status
= U_ZERO_ERROR
;
550 logln("Testing weird arguments");
551 col
->getCollationKey(NULL
, 0, sortk1
, key1Status
);
552 // key gets reset here
554 sortk1
.getByteArray(length
);
555 doAssert(sortk1
.isBogus() == FALSE
&& length
== 0,
556 "Empty string should return an empty collation key");
557 // bogus key returned here
558 key1Status
= U_ILLEGAL_ARGUMENT_ERROR
;
559 col
->getCollationKey(NULL
, 0, sortk1
, key1Status
);
560 doAssert(sortk1
.getByteArray(length
) == NULL
&& length
== 0,
561 "Error code should return bogus collation key");
563 key1Status
= U_ZERO_ERROR
;
564 logln("Use tertiary comparison level testing ....");
566 col
->getCollationKey(test1
, sortk1
, key1Status
);
567 doAssert((sortk1
.compareTo(col
->getCollationKey(test2
, sortk2
, key2Status
)))
568 == Collator::GREATER
,
569 "Result should be \"Abcda\" >>> \"abcda\"");
571 CollationKey
sortk3(sortk2
), sortkNew
, sortkEmpty
;
575 doAssert((sortk1
!= sortk2
), "The sort keys should be different");
576 doAssert((sortk1
.hashCode() != sortk2
.hashCode()), "sort key hashCode() failed");
577 doAssert((sortk2
== sortk3
), "The sort keys should be the same");
578 doAssert((sortk1
== sortkNew
), "The sort keys assignment failed");
579 doAssert((sortk1
.hashCode() == sortkNew
.hashCode()), "sort key hashCode() failed");
580 doAssert((sortkNew
!= sortk3
), "The sort keys should be different");
581 doAssert(sortk1
.compareTo(sortk3
) == Collator::GREATER
, "Result should be \"Abcda\" >>> \"abcda\"");
582 doAssert(sortk2
.compareTo(sortk3
) == Collator::EQUAL
, "Result should be \"abcda\" == \"abcda\"");
583 doAssert(sortkEmpty
.compareTo(sortk1
) == Collator::LESS
, "Result should be (empty key) <<< \"Abcda\"");
584 doAssert(sortk1
.compareTo(sortkEmpty
) == Collator::GREATER
, "Result should be \"Abcda\" >>> (empty key)");
585 doAssert(sortkEmpty
.compareTo(sortkEmpty
) == Collator::EQUAL
, "Result should be (empty key) == (empty key)");
586 doAssert(sortk1
.compareTo(sortk3
, success
) == UCOL_GREATER
, "Result should be \"Abcda\" >>> \"abcda\"");
587 doAssert(sortk2
.compareTo(sortk3
, success
) == UCOL_EQUAL
, "Result should be \"abcda\" == \"abcda\"");
588 doAssert(sortkEmpty
.compareTo(sortk1
, success
) == UCOL_LESS
, "Result should be (empty key) <<< \"Abcda\"");
589 doAssert(sortk1
.compareTo(sortkEmpty
, success
) == UCOL_GREATER
, "Result should be \"Abcda\" >>> (empty key)");
590 doAssert(sortkEmpty
.compareTo(sortkEmpty
, success
) == UCOL_EQUAL
, "Result should be (empty key) == (empty key)");
592 int32_t cnt1
, cnt2
, cnt3
, cnt4
;
594 const uint8_t* byteArray1
= sortk1
.getByteArray(cnt1
);
595 const uint8_t* byteArray2
= sortk2
.getByteArray(cnt2
);
597 const uint8_t* byteArray3
= 0;
598 byteArray3
= sortk1
.getByteArray(cnt3
);
600 const uint8_t* byteArray4
= 0;
601 byteArray4
= sortk2
.getByteArray(cnt4
);
603 CollationKey
sortk4(byteArray1
, cnt1
), sortk5(byteArray2
, cnt2
);
604 CollationKey
sortk6(byteArray3
, cnt3
), sortk7(byteArray4
, cnt4
);
606 doAssert(sortk1
.compareTo(sortk4
) == Collator::EQUAL
, "CollationKey::toByteArray(sortk1) Failed.");
607 doAssert(sortk2
.compareTo(sortk5
) == Collator::EQUAL
, "CollationKey::toByteArray(sortk2) Failed.");
608 doAssert(sortk4
.compareTo(sortk5
) == Collator::GREATER
, "sortk4 >>> sortk5 Failed");
609 doAssert(sortk1
.compareTo(sortk6
) == Collator::EQUAL
, "CollationKey::getByteArray(sortk1) Failed.");
610 doAssert(sortk2
.compareTo(sortk7
) == Collator::EQUAL
, "CollationKey::getByteArray(sortk2) Failed.");
611 doAssert(sortk6
.compareTo(sortk7
) == Collator::GREATER
, "sortk6 >>> sortk7 Failed");
613 logln("Equality tests : ");
614 doAssert(sortk1
== sortk4
, "sortk1 == sortk4 Failed.");
615 doAssert(sortk2
== sortk5
, "sortk2 == sortk5 Failed.");
616 doAssert(sortk1
!= sortk5
, "sortk1 != sortk5 Failed.");
617 doAssert(sortk1
== sortk6
, "sortk1 == sortk6 Failed.");
618 doAssert(sortk2
== sortk7
, "sortk2 == sortk7 Failed.");
619 doAssert(sortk1
!= sortk7
, "sortk1 != sortk7 Failed.");
625 doAssert(sortk1
== sortk3
, "sortk1 = sortk3 assignment Failed.");
626 doAssert(sortk2
!= sortk3
, "sortk2 != sortk3 Failed.");
627 logln("testing sortkey ends...");
629 col
->setStrength(Collator::SECONDARY
);
630 doAssert(col
->getCollationKey(test1
, sortk1
, key1Status
).compareTo(
631 col
->getCollationKey(test2
, sortk2
, key2Status
))
633 "Result should be \"Abcda\" == \"abcda\"");
637 //----------------------------------------------------------------------------
638 // Tests the CollatorElementIterator class.
639 // ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!=
642 CollationAPITest::TestElemIter(/* char* par */)
644 logln("testing sortkey begins...");
646 UErrorCode success
= U_ZERO_ERROR
;
647 col
= Collator::createInstance(Locale::getEnglish(), success
);
648 if (U_FAILURE(success
))
650 errcheckln(success
, "Default collation creation failed. - %s", u_errorName(success
));
654 UnicodeString
testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
655 UnicodeString
testString2("Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
656 logln("Constructors and comparison testing....");
657 CollationElementIterator
*iterator1
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString1
);
659 CharacterIterator
*chariter
=new StringCharacterIterator(testString1
);
660 CollationElementIterator
*coliter
=((RuleBasedCollator
*)col
)->createCollationElementIterator(*chariter
);
663 CollationElementIterator
*iterator2
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString1
);
664 CollationElementIterator
*iterator3
= ((RuleBasedCollator
*)col
)->createCollationElementIterator(testString2
);
666 int32_t offset
= iterator1
->getOffset();
668 errln("Error in getOffset for collation element iterator\n");
671 iterator1
->setOffset(6, success
);
672 if (U_FAILURE(success
)) {
673 errln("Error in setOffset for collation element iterator\n");
676 iterator1
->setOffset(0, success
);
677 int32_t order1
, order2
, order3
;
678 doAssert((*iterator1
== *iterator2
), "The two iterators should be the same");
679 doAssert((*iterator1
!= *iterator3
), "The two iterators should be different");
681 doAssert((*coliter
== *iterator1
), "The two iterators should be the same");
682 doAssert((*coliter
== *iterator2
), "The two iterators should be the same");
683 doAssert((*coliter
!= *iterator3
), "The two iterators should be different");
685 order1
= iterator1
->next(success
);
686 if (U_FAILURE(success
))
688 errln("Somehow ran out of memory stepping through the iterator.");
692 doAssert((*iterator1
!= *iterator2
), "The first iterator advance failed");
693 order2
= iterator2
->getOffset();
694 doAssert((order1
!= order2
), "The order result should not be the same");
695 order2
= iterator2
->next(success
);
696 if (U_FAILURE(success
))
698 errln("Somehow ran out of memory stepping through the iterator.");
702 doAssert((*iterator1
== *iterator2
), "The second iterator advance failed");
703 doAssert((order1
== order2
), "The order result should be the same");
704 order3
= iterator3
->next(success
);
705 if (U_FAILURE(success
))
707 errln("Somehow ran out of memory stepping through the iterator.");
711 doAssert((CollationElementIterator::primaryOrder(order1
) ==
712 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be the same");
713 doAssert((CollationElementIterator::secondaryOrder(order1
) ==
714 CollationElementIterator::secondaryOrder(order3
)), "The secondary orders should be the same");
715 doAssert((CollationElementIterator::tertiaryOrder(order1
) ==
716 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be the same");
718 order1
= iterator1
->next(success
); order3
= iterator3
->next(success
);
719 if (U_FAILURE(success
))
721 errln("Somehow ran out of memory stepping through the iterator.");
725 doAssert((CollationElementIterator::primaryOrder(order1
) ==
726 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be identical");
727 doAssert((CollationElementIterator::tertiaryOrder(order1
) !=
728 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be different");
730 order1
= iterator1
->next(success
);
731 order3
= iterator3
->next(success
);
732 /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
734 doAssert((CollationElementIterator::secondaryOrder(order1) !=
735 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
737 doAssert((order1
!= CollationElementIterator::NULLORDER
), "Unexpected end of iterator reached");
739 iterator1
->reset(); iterator2
->reset(); iterator3
->reset();
740 order1
= iterator1
->next(success
);
741 if (U_FAILURE(success
))
743 errln("Somehow ran out of memory stepping through the iterator.");
747 doAssert((*iterator1
!= *iterator2
), "The first iterator advance failed");
749 order2
= iterator2
->next(success
);
750 if (U_FAILURE(success
))
752 errln("Somehow ran out of memory stepping through the iterator.");
756 doAssert((*iterator1
== *iterator2
), "The second iterator advance failed");
757 doAssert((order1
== order2
), "The order result should be the same");
759 order3
= iterator3
->next(success
);
760 if (U_FAILURE(success
))
762 errln("Somehow ran out of memory stepping through the iterator.");
766 doAssert((CollationElementIterator::primaryOrder(order1
) ==
767 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be the same");
768 doAssert((CollationElementIterator::secondaryOrder(order1
) ==
769 CollationElementIterator::secondaryOrder(order3
)), "The secondary orders should be the same");
770 doAssert((CollationElementIterator::tertiaryOrder(order1
) ==
771 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be the same");
773 order1
= iterator1
->next(success
); order2
= iterator2
->next(success
); order3
= iterator3
->next(success
);
774 if (U_FAILURE(success
))
776 errln("Somehow ran out of memory stepping through the iterator.");
780 doAssert((CollationElementIterator::primaryOrder(order1
) ==
781 CollationElementIterator::primaryOrder(order3
)), "The primary orders should be identical");
782 doAssert((CollationElementIterator::tertiaryOrder(order1
) !=
783 CollationElementIterator::tertiaryOrder(order3
)), "The tertiary orders should be different");
785 order1
= iterator1
->next(success
); order3
= iterator3
->next(success
);
786 if (U_FAILURE(success
))
788 errln("Somehow ran out of memory stepping through the iterator.");
792 /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
794 doAssert((CollationElementIterator::secondaryOrder(order1) !=
795 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
797 doAssert((order1
!= CollationElementIterator::NULLORDER
), "Unexpected end of iterator reached");
798 doAssert((*iterator2
!= *iterator3
), "The iterators should be different");
802 success
=U_UNSUPPORTED_ERROR
;
803 Collator
*colerror
=NULL
;
804 colerror
=Collator::createInstance(Locale::getEnglish(), success
);
805 if (colerror
!= 0 || success
== U_ZERO_ERROR
){
806 errln("Error: createInstance(UErrorCode != U_ZERO_ERROR) should just return and not create an instance\n");
808 int32_t position
=coliter
->previous(success
);
809 if(position
!= CollationElementIterator::NULLORDER
){
810 errln((UnicodeString
)"Expected NULLORDER got" + position
);
813 coliter
->setText(*chariter
, success
);
814 if(!U_FAILURE(success
)){
815 errln("Expeceted error");
817 iterator1
->setText((UnicodeString
)"hello there", success
);
818 if(!U_FAILURE(success
)){
819 errln("Expeceted error");
831 logln("testing CollationElementIterator ends...");
834 // Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules
836 CollationAPITest::TestOperators(/* char* par */)
838 UErrorCode success
= U_ZERO_ERROR
;
839 UnicodeString
ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
840 UnicodeString
ruleset2("< a, A < b, B < c, C < d, D, e, E");
841 RuleBasedCollator
*col1
= new RuleBasedCollator(ruleset1
, success
);
842 if (U_FAILURE(success
)) {
843 errcheckln(success
, "RuleBasedCollator creation failed. - %s", u_errorName(success
));
846 success
= U_ZERO_ERROR
;
847 RuleBasedCollator
*col2
= new RuleBasedCollator(ruleset2
, success
);
848 if (U_FAILURE(success
)) {
849 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set.");
852 logln("The operator tests begin : ");
853 logln("testing operator==, operator!=, clone methods ...");
854 doAssert((*col1
!= *col2
), "The two different table collations compared equal");
856 doAssert((*col1
== *col2
), "Collator objects not equal after assignment (operator=)");
858 success
= U_ZERO_ERROR
;
859 Collator
*col3
= Collator::createInstance(Locale::getEnglish(), success
);
860 if (U_FAILURE(success
)) {
861 errln("Default collation creation failed.");
864 doAssert((*col1
!= *col3
), "The two different table collations compared equal");
865 Collator
* col4
= col1
->clone();
866 Collator
* col5
= col3
->clone();
867 doAssert((*col1
== *col4
), "Cloned collation objects not equal");
868 doAssert((*col3
!= *col4
), "Two different table collations compared equal");
869 doAssert((*col3
== *col5
), "Cloned collation objects not equal");
870 doAssert((*col4
!= *col5
), "Two cloned collations compared equal");
872 const UnicodeString
& defRules
= ((RuleBasedCollator
*)col3
)->getRules();
873 RuleBasedCollator
* col6
= new RuleBasedCollator(defRules
, success
);
874 if (U_FAILURE(success
)) {
875 errln("Creating default collation with rules failed.");
878 doAssert((((RuleBasedCollator
*)col3
)->getRules() == col6
->getRules()), "Default collator getRules failed");
880 success
= U_ZERO_ERROR
;
881 RuleBasedCollator
*col7
= new RuleBasedCollator(ruleset2
, Collator::TERTIARY
, success
);
882 if (U_FAILURE(success
)) {
883 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength.");
886 success
= U_ZERO_ERROR
;
887 RuleBasedCollator
*col8
= new RuleBasedCollator(ruleset2
, UCOL_OFF
, success
);
888 if (U_FAILURE(success
)) {
889 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with Normalizer::NO_OP.");
892 success
= U_ZERO_ERROR
;
893 RuleBasedCollator
*col9
= new RuleBasedCollator(ruleset2
, Collator::PRIMARY
, UCOL_ON
, success
);
894 if (U_FAILURE(success
)) {
895 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength and Normalizer::NO_OP.");
898 // doAssert((*col7 == *col8), "The two equal table collations compared different");
899 doAssert((*col7
!= *col9
), "The two different table collations compared equal");
900 doAssert((*col8
!= *col9
), "The two different table collations compared equal");
902 logln("operator tests ended.");
914 // test clone and copy
916 CollationAPITest::TestDuplicate(/* char* par */)
918 UErrorCode status
= U_ZERO_ERROR
;
919 Collator
*col1
= Collator::createInstance(Locale::getEnglish(), status
);
920 if (U_FAILURE(status
)) {
921 logln("Default collator creation failed.");
924 Collator
*col2
= col1
->clone();
925 doAssert((*col1
== *col2
), "Cloned object is not equal to the orginal");
926 UnicodeString
*ruleset
= new UnicodeString("< a, A < b, B < c, C < d, D, e, E");
927 RuleBasedCollator
*col3
= new RuleBasedCollator(*ruleset
, status
);
928 doAssert((*col1
!= *col3
), "Cloned object is equal to some dummy");
929 *col3
= *((RuleBasedCollator
*)col1
);
930 doAssert((*col1
== *col3
), "Copied object is not equal to the orginal");
932 if (U_FAILURE(status
)) {
933 logln("Collation tailoring failed.");
937 UCollationResult res
;
938 UnicodeString
first((UChar
)0x0061);
939 UnicodeString
second((UChar
)0x0062);
940 UnicodeString
copiedEnglishRules(((RuleBasedCollator
*)col1
)->getRules());
945 // Try using the cloned collators after deleting the original data
946 res
= col2
->compare(first
, second
, status
);
947 if(res
!= UCOL_LESS
) {
948 errln("a should be less then b after tailoring");
950 if (((RuleBasedCollator
*)col2
)->getRules() != copiedEnglishRules
) {
951 errln(UnicodeString("English rule difference. ")
952 + copiedEnglishRules
+ UnicodeString("\ngetRules=") + ((RuleBasedCollator
*)col2
)->getRules());
954 res
= col3
->compare(first
, second
, status
);
955 if(res
!= UCOL_LESS
) {
956 errln("a should be less then b after tailoring");
958 if (col3
->getRules() != copiedEnglishRules
) {
959 errln(UnicodeString("English rule difference. ")
960 + copiedEnglishRules
+ UnicodeString("\ngetRules=") + col3
->getRules());
968 CollationAPITest::TestCompare(/* char* par */)
970 logln("The compare tests begin : ");
972 UErrorCode success
= U_ZERO_ERROR
;
973 col
= Collator::createInstance(Locale::getEnglish(), success
);
974 if (U_FAILURE(success
)) {
975 errcheckln(success
, "Default collation creation failed. - %s", u_errorName(success
));
978 UnicodeString
test1("Abcda"), test2("abcda");
979 logln("Use tertiary comparison level testing ....");
981 doAssert((!col
->equals(test1
, test2
) ), "Result should be \"Abcda\" != \"abcda\"");
982 doAssert((col
->greater(test1
, test2
) ), "Result should be \"Abcda\" >>> \"abcda\"");
983 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" >>> \"abcda\"");
985 col
->setStrength(Collator::SECONDARY
);
986 logln("Use secondary comparison level testing ....");
988 doAssert((col
->equals(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
989 doAssert((!col
->greater(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
990 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
992 col
->setStrength(Collator::PRIMARY
);
993 logln("Use primary comparison level testing ....");
995 doAssert((col
->equals(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
996 doAssert((!col
->greater(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
997 doAssert((col
->greaterOrEqual(test1
, test2
) ), "Result should be \"Abcda\" == \"abcda\"");
999 // Test different APIs
1000 const UChar
* t1
= test1
.getBuffer();
1001 int32_t t1Len
= test1
.length();
1002 const UChar
* t2
= test2
.getBuffer();
1003 int32_t t2Len
= test2
.length();
1005 doAssert((col
->compare(test1
, test2
) == Collator::EQUAL
), "Problem");
1006 doAssert((col
->compare(test1
, test2
, success
) == UCOL_EQUAL
), "Problem");
1007 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
) == Collator::EQUAL
), "Problem");
1008 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
, success
) == UCOL_EQUAL
), "Problem");
1009 doAssert((col
->compare(test1
, test2
, t1Len
) == Collator::EQUAL
), "Problem");
1010 doAssert((col
->compare(test1
, test2
, t1Len
, success
) == UCOL_EQUAL
), "Problem");
1012 col
->setAttribute(UCOL_STRENGTH
, UCOL_TERTIARY
, success
);
1013 doAssert((col
->compare(test1
, test2
) == Collator::GREATER
), "Problem");
1014 doAssert((col
->compare(test1
, test2
, success
) == UCOL_GREATER
), "Problem");
1015 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
) == Collator::GREATER
), "Problem");
1016 doAssert((col
->compare(t1
, t1Len
, t2
, t2Len
, success
) == UCOL_GREATER
), "Problem");
1017 doAssert((col
->compare(test1
, test2
, t1Len
) == Collator::GREATER
), "Problem");
1018 doAssert((col
->compare(test1
, test2
, t1Len
, success
) == UCOL_GREATER
), "Problem");
1022 logln("The compare tests end.");
1027 CollationAPITest::TestGetAll(/* char* par */)
1029 int32_t count1
, count2
;
1030 UErrorCode status
= U_ZERO_ERROR
;
1032 logln("Trying Collator::getAvailableLocales(int&)");
1034 const Locale
* list
= Collator::getAvailableLocales(count1
);
1035 for (int32_t i
= 0; i
< count1
; ++i
) {
1036 UnicodeString dispName
;
1037 logln(UnicodeString("Locale name: ")
1038 + UnicodeString(list
[i
].getName())
1039 + UnicodeString(" , the display name is : ")
1040 + UnicodeString(list
[i
].getDisplayName(dispName
)));
1043 if (count1
== 0 || list
== NULL
) {
1044 dataerrln("getAvailableLocales(int&) returned an empty list");
1047 logln("Trying Collator::getAvailableLocales()");
1048 StringEnumeration
* localeEnum
= Collator::getAvailableLocales();
1049 const UnicodeString
* locStr
;
1050 const char *locCStr
;
1053 if (localeEnum
== NULL
) {
1054 dataerrln("getAvailableLocales() returned NULL");
1058 while ((locStr
= localeEnum
->snext(status
)) != NULL
)
1060 logln(UnicodeString("Locale name is: ") + *locStr
);
1063 if (count1
!= count2
) {
1064 errln("getAvailableLocales(int&) returned %d and getAvailableLocales() returned %d", count1
, count2
);
1067 logln("Trying Collator::getAvailableLocales() clone");
1069 StringEnumeration
* localeEnum2
= localeEnum
->clone();
1070 localeEnum2
->reset(status
);
1071 while ((locCStr
= localeEnum2
->next(NULL
, status
)) != NULL
)
1073 logln(UnicodeString("Locale name is: ") + UnicodeString(locCStr
));
1076 if (count1
!= count2
) {
1077 errln("getAvailableLocales(3rd time) returned %d and getAvailableLocales(2nd time) returned %d", count1
, count2
);
1079 if (localeEnum
->count(status
) != count1
) {
1080 errln("localeEnum->count() returned %d and getAvailableLocales() returned %d", localeEnum
->count(status
), count1
);
1086 void CollationAPITest::TestSortKey()
1088 UErrorCode status
= U_ZERO_ERROR
;
1090 this is supposed to open default date format, but later on it treats
1091 it like it is "en_US"
1092 - very bad if you try to run the tests on machine where default
1093 locale is NOT "en_US"
1095 Collator
*col
= Collator::createInstance(Locale::getEnglish(), status
);
1096 if (U_FAILURE(status
)) {
1097 errcheckln(status
, "ERROR: Default collation creation failed.: %s\n", u_errorName(status
));
1101 if (col
->getStrength() != Collator::TERTIARY
)
1103 errln("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
1106 /* Need to use identical strength */
1107 col
->setAttribute(UCOL_STRENGTH
, UCOL_IDENTICAL
, status
);
1109 UChar test1
[6] = {0x41, 0x62, 0x63, 0x64, 0x61, 0},
1110 test2
[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0},
1111 test3
[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0};
1113 uint8_t sortkey1
[64];
1114 uint8_t sortkey2
[64];
1115 uint8_t sortkey3
[64];
1117 logln("Use tertiary comparison level testing ....\n");
1120 col
->getCollationKey(test1
, u_strlen(test1
), key1
, status
);
1123 col
->getCollationKey(test2
, u_strlen(test2
), key2
, status
);
1126 col
->getCollationKey(test3
, u_strlen(test3
), key3
, status
);
1128 doAssert(key1
.compareTo(key2
) == Collator::GREATER
,
1129 "Result should be \"Abcda\" > \"abcda\"");
1130 doAssert(key2
.compareTo(key1
) == Collator::LESS
,
1131 "Result should be \"abcda\" < \"Abcda\"");
1132 doAssert(key2
.compareTo(key3
) == Collator::EQUAL
,
1133 "Result should be \"abcda\" == \"abcda\"");
1135 // Clone the key2 sortkey for later.
1136 int32_t keylength
= 0;
1137 const uint8_t *key2primary_alias
= key2
.getByteArray(keylength
);
1138 LocalArray
<uint8_t> key2primary(new uint8_t[keylength
]);
1139 memcpy(key2primary
.getAlias(), key2primary_alias
, keylength
);
1141 col
->getSortKey(test1
, sortkey1
, 64);
1142 col
->getSortKey(test2
, sortkey2
, 64);
1143 col
->getSortKey(test3
, sortkey3
, 64);
1145 const uint8_t *tempkey
= key1
.getByteArray(keylength
);
1146 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1147 "Test1 string should have the same collation key and sort key");
1148 tempkey
= key2
.getByteArray(keylength
);
1149 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1150 "Test2 string should have the same collation key and sort key");
1151 tempkey
= key3
.getByteArray(keylength
);
1152 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1153 "Test3 string should have the same collation key and sort key");
1155 col
->getSortKey(test1
, 5, sortkey1
, 64);
1156 col
->getSortKey(test2
, 5, sortkey2
, 64);
1157 col
->getSortKey(test3
, 5, sortkey3
, 64);
1159 tempkey
= key1
.getByteArray(keylength
);
1160 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1161 "Test1 string should have the same collation key and sort key");
1162 tempkey
= key2
.getByteArray(keylength
);
1163 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1164 "Test2 string should have the same collation key and sort key");
1165 tempkey
= key3
.getByteArray(keylength
);
1166 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1167 "Test3 string should have the same collation key and sort key");
1169 UnicodeString
strtest1(test1
);
1170 col
->getSortKey(strtest1
, sortkey1
, 64);
1171 UnicodeString
strtest2(test2
);
1172 col
->getSortKey(strtest2
, sortkey2
, 64);
1173 UnicodeString
strtest3(test3
);
1174 col
->getSortKey(strtest3
, sortkey3
, 64);
1176 tempkey
= key1
.getByteArray(keylength
);
1177 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1178 "Test1 string should have the same collation key and sort key");
1179 tempkey
= key2
.getByteArray(keylength
);
1180 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1181 "Test2 string should have the same collation key and sort key");
1182 tempkey
= key3
.getByteArray(keylength
);
1183 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1184 "Test3 string should have the same collation key and sort key");
1186 logln("Use secondary comparision level testing ...\n");
1187 col
->setStrength(Collator::SECONDARY
);
1189 col
->getCollationKey(test1
, u_strlen(test1
), key1
, status
);
1190 col
->getCollationKey(test2
, u_strlen(test2
), key2
, status
);
1191 col
->getCollationKey(test3
, u_strlen(test3
), key3
, status
);
1193 doAssert(key1
.compareTo(key2
) == Collator::EQUAL
,
1194 "Result should be \"Abcda\" == \"abcda\"");
1195 doAssert(key2
.compareTo(key3
) == Collator::EQUAL
,
1196 "Result should be \"abcda\" == \"abcda\"");
1198 tempkey
= key2
.getByteArray(keylength
);
1199 doAssert(memcmp(tempkey
, key2primary
.getAlias(), keylength
- 1) == 0,
1200 "Binary format for 'abcda' sortkey different for secondary strength!");
1202 col
->getSortKey(test1
, sortkey1
, 64);
1203 col
->getSortKey(test2
, sortkey2
, 64);
1204 col
->getSortKey(test3
, sortkey3
, 64);
1206 tempkey
= key1
.getByteArray(keylength
);
1207 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1208 "Test1 string should have the same collation key and sort key");
1209 tempkey
= key2
.getByteArray(keylength
);
1210 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1211 "Test2 string should have the same collation key and sort key");
1212 tempkey
= key3
.getByteArray(keylength
);
1213 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1214 "Test3 string should have the same collation key and sort key");
1216 col
->getSortKey(test1
, 5, sortkey1
, 64);
1217 col
->getSortKey(test2
, 5, sortkey2
, 64);
1218 col
->getSortKey(test3
, 5, sortkey3
, 64);
1220 tempkey
= key1
.getByteArray(keylength
);
1221 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1222 "Test1 string should have the same collation key and sort key");
1223 tempkey
= key2
.getByteArray(keylength
);
1224 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1225 "Test2 string should have the same collation key and sort key");
1226 tempkey
= key3
.getByteArray(keylength
);
1227 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1228 "Test3 string should have the same collation key and sort key");
1230 col
->getSortKey(strtest1
, sortkey1
, 64);
1231 col
->getSortKey(strtest2
, sortkey2
, 64);
1232 col
->getSortKey(strtest3
, sortkey3
, 64);
1234 tempkey
= key1
.getByteArray(keylength
);
1235 doAssert(memcmp(tempkey
, sortkey1
, keylength
) == 0,
1236 "Test1 string should have the same collation key and sort key");
1237 tempkey
= key2
.getByteArray(keylength
);
1238 doAssert(memcmp(tempkey
, sortkey2
, keylength
) == 0,
1239 "Test2 string should have the same collation key and sort key");
1240 tempkey
= key3
.getByteArray(keylength
);
1241 doAssert(memcmp(tempkey
, sortkey3
, keylength
) == 0,
1242 "Test3 string should have the same collation key and sort key");
1244 logln("testing sortkey ends...");
1248 void CollationAPITest::TestMaxExpansion()
1250 UErrorCode status
= U_ZERO_ERROR
;
1252 UChar32 unassigned
= 0xEFFFD;
1253 uint32_t sorder
= 0;
1254 uint32_t temporder
= 0;
1256 UnicodeString
rule("&a < ab < c/aba < d < z < ch");
1257 RuleBasedCollator
coll(rule
, status
);
1258 if(U_FAILURE(status
)) {
1259 errcheckln(status
, "Collator creation failed with error %s", u_errorName(status
));
1262 UnicodeString
str(ch
);
1263 CollationElementIterator
*iter
=
1264 coll
.createCollationElementIterator(str
);
1266 while (ch
< 0xFFFF && U_SUCCESS(status
)) {
1273 str
.setCharAt(0, ch
);
1274 iter
->setText(str
, status
);
1275 order
= iter
->previous(status
);
1277 /* thai management */
1279 order
= iter
->previous(status
);
1281 while (U_SUCCESS(status
) && iter
->previous(status
) != UCOL_NULLORDER
) {
1285 size
= coll
.getMaxExpansion(order
);
1286 if (U_FAILURE(status
) || size
< count
) {
1287 errln("Failure at codepoint U+%04X, maximum expansion count %d < %d",
1292 /* testing for exact max expansion */
1297 str
.setCharAt(0, ch
);
1298 iter
->setText(str
, status
);
1299 order
= iter
->previous(status
);
1300 size
= coll
.getMaxExpansion(order
);
1301 if (U_FAILURE(status
) || size
!= 1) {
1302 errln("Failure at codepoint U+%04X, maximum expansion count %d < %d",
1310 iter
->setText(str
, status
);
1311 temporder
= iter
->previous(status
);
1312 size
= coll
.getMaxExpansion(temporder
);
1313 if (U_FAILURE(status
) || size
!= 3) {
1314 errln("Failure at codepoint U+%04X, CE %08x, maximum expansion count %d != %d",
1315 ch
, temporder
, size
, 3);
1320 iter
->setText(str
, status
);
1321 temporder
= iter
->previous(status
);
1322 size
= coll
.getMaxExpansion(temporder
);
1323 if (U_FAILURE(status
) || size
!= 1) {
1324 errln("Failure at codepoint U+%04X, CE %08x, maximum expansion count %d != %d",
1325 ch
, temporder
, size
, 1);
1328 str
.setTo(unassigned
);
1329 iter
->setText(str
, status
);
1330 sorder
= iter
->previous(status
);
1331 size
= coll
.getMaxExpansion(sorder
);
1332 if (U_FAILURE(status
) || size
!= 2) {
1333 errln("Failure at supplementary codepoints, maximum expansion count %d < %d",
1340 iter
->setText(str
, status
);
1341 temporder
= iter
->previous(status
);
1342 size
= coll
.getMaxExpansion(temporder
);
1343 if (U_FAILURE(status
) || size
> 3) {
1344 errln("Failure at codepoint U+%04X, maximum expansion count %d > %d",
1350 /* testing special jamo &a<\u1160 */
1351 rule
= CharsToUnicodeString("\\u0026\\u0071\\u003c\\u1165\\u002f\\u0071\\u0071\\u0071\\u0071");
1353 RuleBasedCollator
jamocoll(rule
, status
);
1354 iter
= jamocoll
.createCollationElementIterator(str
);
1355 temporder
= iter
->previous(status
);
1356 size
= iter
->getMaxExpansion(temporder
);
1357 if (U_FAILURE(status
) || size
!= 6) {
1358 errln("Failure at codepoint U+%04X, maximum expansion count %d > %d",
1365 void CollationAPITest::TestDisplayName()
1367 UErrorCode error
= U_ZERO_ERROR
;
1368 Collator
*coll
= Collator::createInstance("en_US", error
);
1369 if (U_FAILURE(error
)) {
1370 errcheckln(error
, "Failure creating english collator - %s", u_errorName(error
));
1374 UnicodeString result
;
1375 coll
->getDisplayName(Locale::getCanadaFrench(), result
);
1376 Locale::getCanadaFrench().getDisplayName(name
);
1377 if (result
.compare(name
)) {
1378 errln("Failure getting the correct name for locale en_US");
1381 coll
->getDisplayName(Locale::getSimplifiedChinese(), result
);
1382 Locale::getSimplifiedChinese().getDisplayName(name
);
1383 if (result
.compare(name
)) {
1384 errln("Failure getting the correct name for locale zh_SG");
1389 void CollationAPITest::TestAttribute()
1391 UErrorCode error
= U_ZERO_ERROR
;
1392 Collator
*coll
= Collator::createInstance(error
);
1394 if (U_FAILURE(error
)) {
1395 errcheckln(error
, "Creation of default collator failed - %s", u_errorName(error
));
1399 coll
->setAttribute(UCOL_FRENCH_COLLATION
, UCOL_OFF
, error
);
1400 if (coll
->getAttribute(UCOL_FRENCH_COLLATION
, error
) != UCOL_OFF
||
1402 errln("Setting and retrieving of the french collation failed");
1405 coll
->setAttribute(UCOL_FRENCH_COLLATION
, UCOL_ON
, error
);
1406 if (coll
->getAttribute(UCOL_FRENCH_COLLATION
, error
) != UCOL_ON
||
1408 errln("Setting and retrieving of the french collation failed");
1411 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, error
);
1412 if (coll
->getAttribute(UCOL_ALTERNATE_HANDLING
, error
) != UCOL_SHIFTED
||
1414 errln("Setting and retrieving of the alternate handling failed");
1417 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, error
);
1418 if (coll
->getAttribute(UCOL_ALTERNATE_HANDLING
, error
) != UCOL_NON_IGNORABLE
||
1420 errln("Setting and retrieving of the alternate handling failed");
1423 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, error
);
1424 if (coll
->getAttribute(UCOL_CASE_FIRST
, error
) != UCOL_LOWER_FIRST
||
1426 errln("Setting and retrieving of the case first attribute failed");
1429 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, error
);
1430 if (coll
->getAttribute(UCOL_CASE_FIRST
, error
) != UCOL_UPPER_FIRST
||
1432 errln("Setting and retrieving of the case first attribute failed");
1435 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_ON
, error
);
1436 if (coll
->getAttribute(UCOL_CASE_LEVEL
, error
) != UCOL_ON
||
1438 errln("Setting and retrieving of the case level attribute failed");
1441 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, error
);
1442 if (coll
->getAttribute(UCOL_CASE_LEVEL
, error
) != UCOL_OFF
||
1444 errln("Setting and retrieving of the case level attribute failed");
1447 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, error
);
1448 if (coll
->getAttribute(UCOL_NORMALIZATION_MODE
, error
) != UCOL_ON
||
1450 errln("Setting and retrieving of the normalization on/off attribute failed");
1453 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_OFF
, error
);
1454 if (coll
->getAttribute(UCOL_NORMALIZATION_MODE
, error
) != UCOL_OFF
||
1456 errln("Setting and retrieving of the normalization on/off attribute failed");
1459 coll
->setAttribute(UCOL_STRENGTH
, UCOL_PRIMARY
, error
);
1460 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_PRIMARY
||
1462 errln("Setting and retrieving of the collation strength failed");
1465 coll
->setAttribute(UCOL_STRENGTH
, UCOL_SECONDARY
, error
);
1466 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_SECONDARY
||
1468 errln("Setting and retrieving of the collation strength failed");
1471 coll
->setAttribute(UCOL_STRENGTH
, UCOL_TERTIARY
, error
);
1472 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_TERTIARY
||
1474 errln("Setting and retrieving of the collation strength failed");
1477 coll
->setAttribute(UCOL_STRENGTH
, UCOL_QUATERNARY
, error
);
1478 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_QUATERNARY
||
1480 errln("Setting and retrieving of the collation strength failed");
1483 coll
->setAttribute(UCOL_STRENGTH
, UCOL_IDENTICAL
, error
);
1484 if (coll
->getAttribute(UCOL_STRENGTH
, error
) != UCOL_IDENTICAL
||
1486 errln("Setting and retrieving of the collation strength failed");
1492 void CollationAPITest::TestVariableTopSetting() {
1493 UErrorCode status
= U_ZERO_ERROR
;
1495 UChar vt
[256] = { 0 };
1497 Collator
*coll
= Collator::createInstance(status
);
1498 if(U_FAILURE(status
)) {
1500 errcheckln(status
, "Collator creation failed with error %s", u_errorName(status
));
1504 uint32_t oldVarTop
= coll
->getVariableTop(status
);
1508 uint32_t newVarTop
= coll
->setVariableTop(vt
, 1, status
);
1510 if((newVarTop
& 0xFFFF0000) != (coll
->getVariableTop(status
) & 0xFFFF0000)) {
1511 errln("Didn't set vartop properly\n");
1514 coll
->setVariableTop(oldVarTop
, status
);
1516 uint32_t newerVarTop
= coll
->setVariableTop(UnicodeString(vt
, 1), status
);
1518 if((newVarTop
& 0xFFFF0000) != (newerVarTop
& 0xFFFF0000)) {
1519 errln("Didn't set vartop properly from UnicodeString!\n");
1526 void CollationAPITest::TestGetLocale() {
1527 UErrorCode status
= U_ZERO_ERROR
;
1528 const char *rules
= "&a<x<y<z";
1529 UChar rlz
[256] = {0};
1531 Collator
*coll
= NULL
;
1536 static const struct {
1537 const char* requestedLocale
;
1538 const char* validLocale
;
1539 const char* actualLocale
;
1541 { "sr_YU", "sr_YU", "root" },
1542 { "sh_YU", "sh_YU", "sh" },
1543 { "en_US_CALIFORNIA", "en_US", "root" },
1544 { "fr_FR_NONEXISTANT", "fr_FR", "fr" }
1547 u_unescape(rules
, rlz
, 256);
1549 /* test opening collators for different locales */
1550 for(i
= 0; i
<(int32_t)(sizeof(testStruct
)/sizeof(testStruct
[0])); i
++) {
1551 status
= U_ZERO_ERROR
;
1552 coll
= Collator::createInstance(testStruct
[i
].requestedLocale
, status
);
1553 if(U_FAILURE(status
)) {
1554 log("Failed to open collator for %s with %s\n", testStruct
[i
].requestedLocale
, u_errorName(status
));
1558 locale
= coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
);
1559 if(locale
!= testStruct
[i
].requestedLocale
) {
1560 log("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].requestedLocale
, locale
.getName());
1562 locale
= coll
->getLocale(ULOC_VALID_LOCALE
, status
);
1563 if(locale
!= testStruct
[i
].validLocale
) {
1564 log("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].validLocale
, locale
.getName());
1566 locale
= coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
);
1567 if(locale
!= testStruct
[i
].actualLocale
) {
1568 log("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].actualLocale
, locale
.getName());
1573 /* completely non-existant locale for collator should get a default collator */
1575 Collator
*defaultColl
= Collator::createInstance((const Locale
)NULL
, status
);
1576 coll
= Collator::createInstance("blahaha", status
);
1577 if(U_FAILURE(status
)) {
1578 log("Failed to open collator with %s\n", u_errorName(status
));
1583 if(coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
) != "blahaha") {
1584 log("Nonexisting locale didn't preserve the requested locale\n");
1586 if(coll
->getLocale(ULOC_VALID_LOCALE
, status
) !=
1587 defaultColl
->getLocale(ULOC_VALID_LOCALE
, status
)) {
1588 log("Valid locale for nonexisting locale locale collator differs "
1589 "from valid locale for default collator\n");
1591 if(coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
) !=
1592 defaultColl
->getLocale(ULOC_ACTUAL_LOCALE
, status
)) {
1593 log("Actual locale for nonexisting locale locale collator differs "
1594 "from actual locale for default collator\n");
1602 /* collator instantiated from rules should have all three locales NULL */
1603 coll
= new RuleBasedCollator(rlz
, status
);
1604 locale
= coll
->getLocale(ULOC_REQUESTED_LOCALE
, status
);
1605 if(!locale
.isBogus()) {
1606 log("For collator instantiated from rules, requested locale %s is not bogus\n", locale
.getName());
1608 locale
= coll
->getLocale(ULOC_VALID_LOCALE
, status
);
1609 if(!locale
.isBogus()) {
1610 log("For collator instantiated from rules, valid locale %s is not bogus\n", locale
.getName());
1612 locale
= coll
->getLocale(ULOC_ACTUAL_LOCALE
, status
);
1613 if(!locale
.isBogus()) {
1614 log("For collator instantiated from rules, actual locale %s is not bogus\n", locale
.getName());
1620 const char *original
;
1627 static int U_CALLCONV
1628 compare_teststruct(const void *string1
, const void *string2
) {
1629 return(strcmp((const char *)((struct teststruct
*)string1
)->key
, (const char *)((struct teststruct
*)string2
)->key
));
1634 void CollationAPITest::TestBounds(void) {
1635 UErrorCode status
= U_ZERO_ERROR
;
1637 Collator
*coll
= Collator::createInstance(Locale("sh"), status
);
1638 if(U_FAILURE(status
)) {
1640 errcheckln(status
, "Collator creation failed with %s", u_errorName(status
));
1644 uint8_t sortkey
[512], lower
[512], upper
[512];
1647 static const char * const test
[] = {
1651 "j\\u00F6hn sm\\u00EFth",
1652 "J\\u00F6hn Sm\\u00EFth",
1653 "J\\u00D6HN SM\\u00CFTH",
1658 struct teststruct tests
[] = {
1659 {"\\u010CAKI MIHALJ", {0}},
1660 {"\\u010CAKI MIHALJ", {0}},
1661 {"\\u010CAKI PIRO\\u0160KA", {0}},
1662 {"\\u010CABAI ANDRIJA", {0}},
1663 {"\\u010CABAI LAJO\\u0160", {0}},
1664 {"\\u010CABAI MARIJA", {0}},
1665 {"\\u010CABAI STEVAN", {0}},
1666 {"\\u010CABAI STEVAN", {0}},
1667 {"\\u010CABARKAPA BRANKO", {0}},
1668 {"\\u010CABARKAPA MILENKO", {0}},
1669 {"\\u010CABARKAPA MIROSLAV", {0}},
1670 {"\\u010CABARKAPA SIMO", {0}},
1671 {"\\u010CABARKAPA STANKO", {0}},
1672 {"\\u010CABARKAPA TAMARA", {0}},
1673 {"\\u010CABARKAPA TOMA\\u0160", {0}},
1674 {"\\u010CABDARI\\u0106 NIKOLA", {0}},
1675 {"\\u010CABDARI\\u0106 ZORICA", {0}},
1676 {"\\u010CABI NANDOR", {0}},
1677 {"\\u010CABOVI\\u0106 MILAN", {0}},
1678 {"\\u010CABRADI AGNEZIJA", {0}},
1679 {"\\u010CABRADI IVAN", {0}},
1680 {"\\u010CABRADI JELENA", {0}},
1681 {"\\u010CABRADI LJUBICA", {0}},
1682 {"\\u010CABRADI STEVAN", {0}},
1683 {"\\u010CABRDA MARTIN", {0}},
1684 {"\\u010CABRILO BOGDAN", {0}},
1685 {"\\u010CABRILO BRANISLAV", {0}},
1686 {"\\u010CABRILO LAZAR", {0}},
1687 {"\\u010CABRILO LJUBICA", {0}},
1688 {"\\u010CABRILO SPASOJA", {0}},
1689 {"\\u010CADE\\u0160 ZDENKA", {0}},
1690 {"\\u010CADESKI BLAGOJE", {0}},
1691 {"\\u010CADOVSKI VLADIMIR", {0}},
1692 {"\\u010CAGLJEVI\\u0106 TOMA", {0}},
1693 {"\\u010CAGOROVI\\u0106 VLADIMIR", {0}},
1694 {"\\u010CAJA VANKA", {0}},
1695 {"\\u010CAJI\\u0106 BOGOLJUB", {0}},
1696 {"\\u010CAJI\\u0106 BORISLAV", {0}},
1697 {"\\u010CAJI\\u0106 RADOSLAV", {0}},
1698 {"\\u010CAK\\u0160IRAN MILADIN", {0}},
1699 {"\\u010CAKAN EUGEN", {0}},
1700 {"\\u010CAKAN EVGENIJE", {0}},
1701 {"\\u010CAKAN IVAN", {0}},
1702 {"\\u010CAKAN JULIJAN", {0}},
1703 {"\\u010CAKAN MIHAJLO", {0}},
1704 {"\\u010CAKAN STEVAN", {0}},
1705 {"\\u010CAKAN VLADIMIR", {0}},
1706 {"\\u010CAKAN VLADIMIR", {0}},
1707 {"\\u010CAKAN VLADIMIR", {0}},
1708 {"\\u010CAKARA ANA", {0}},
1709 {"\\u010CAKAREVI\\u0106 MOMIR", {0}},
1710 {"\\u010CAKAREVI\\u0106 NEDELJKO", {0}},
1711 {"\\u010CAKI \\u0160ANDOR", {0}},
1712 {"\\u010CAKI AMALIJA", {0}},
1713 {"\\u010CAKI ANDRA\\u0160", {0}},
1714 {"\\u010CAKI LADISLAV", {0}},
1715 {"\\u010CAKI LAJO\\u0160", {0}},
1716 {"\\u010CAKI LASLO", {0}}
1721 int32_t i
= 0, j
= 0, k
= 0, buffSize
= 0, skSize
= 0, lowerSize
= 0, upperSize
= 0;
1722 int32_t arraySize
= sizeof(tests
)/sizeof(tests
[0]);
1724 for(i
= 0; i
<arraySize
; i
++) {
1725 buffSize
= u_unescape(tests
[i
].original
, buffer
, 512);
1726 skSize
= coll
->getSortKey(buffer
, buffSize
, tests
[i
].key
, 512);
1729 qsort(tests
, arraySize
, sizeof(struct teststruct
), compare_teststruct
);
1731 for(i
= 0; i
< arraySize
-1; i
++) {
1732 for(j
= i
+1; j
< arraySize
; j
++) {
1733 lowerSize
= coll
->getBound(tests
[i
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, status
);
1734 upperSize
= coll
->getBound(tests
[j
].key
, -1, UCOL_BOUND_UPPER
, 1, upper
, 512, status
);
1735 for(k
= i
; k
<= j
; k
++) {
1736 if(strcmp((const char *)lower
, (const char *)tests
[k
].key
) > 0) {
1737 errln("Problem with lower! j = %i (%s vs %s)", k
, tests
[k
].original
, tests
[i
].original
);
1739 if(strcmp((const char *)upper
, (const char *)tests
[k
].key
) <= 0) {
1740 errln("Problem with upper! j = %i (%s vs %s)", k
, tests
[k
].original
, tests
[j
].original
);
1747 for(i
= 0; i
<(int32_t)(sizeof(test
)/sizeof(test
[0])); i
++) {
1748 buffSize
= u_unescape(test
[i
], buffer
, 512);
1749 skSize
= coll
->getSortKey(buffer
, buffSize
, sortkey
, 512);
1750 lowerSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1751 upperSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1752 for(j
= i
+1; j
<(int32_t)(sizeof(test
)/sizeof(test
[0])); j
++) {
1753 buffSize
= u_unescape(test
[j
], buffer
, 512);
1754 skSize
= coll
->getSortKey(buffer
, buffSize
, sortkey
, 512);
1755 if(strcmp((const char *)lower
, (const char *)sortkey
) > 0) {
1756 errln("Problem with lower! i = %i, j = %i (%s vs %s)", i
, j
, test
[i
], test
[j
]);
1758 if(strcmp((const char *)upper
, (const char *)sortkey
) <= 0) {
1759 errln("Problem with upper! i = %i, j = %i (%s vs %s)", i
, j
, test
[i
], test
[j
]);
1767 void CollationAPITest::TestGetTailoredSet()
1771 const char *tests
[20];
1774 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1775 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1778 uint32_t i
= 0, j
= 0;
1779 UErrorCode status
= U_ZERO_ERROR
;
1781 RuleBasedCollator
*coll
= NULL
;
1783 UnicodeSet
*set
= NULL
;
1785 for(i
= 0; i
< sizeof(setTest
)/sizeof(setTest
[0]); i
++) {
1786 buff
= UnicodeString(setTest
[i
].rules
, "").unescape();
1787 coll
= new RuleBasedCollator(buff
, status
);
1788 if(U_SUCCESS(status
)) {
1789 set
= coll
->getTailoredSet(status
);
1790 if(set
->size() != setTest
[i
].testsize
) {
1791 errln("Tailored set size different (%d) than expected (%d)", set
->size(), setTest
[i
].testsize
);
1793 for(j
= 0; j
< (uint32_t)setTest
[i
].testsize
; j
++) {
1794 buff
= UnicodeString(setTest
[i
].tests
[j
], "").unescape();
1795 if(!set
->contains(buff
)) {
1796 errln("Tailored set doesn't contain %s... It should", setTest
[i
].tests
[j
]);
1801 errcheckln(status
, "Couldn't open collator with rules %s - %s", setTest
[i
].rules
, u_errorName(status
));
1807 void CollationAPITest::TestUClassID()
1809 char id
= *((char *)RuleBasedCollator::getStaticClassID());
1811 errln("Static class id for RuleBasedCollator should be 0");
1813 UErrorCode status
= U_ZERO_ERROR
;
1814 RuleBasedCollator
*coll
1815 = (RuleBasedCollator
*)Collator::createInstance(status
);
1816 if(U_FAILURE(status
)) {
1818 errcheckln(status
, "Collator creation failed with %s", u_errorName(status
));
1821 id
= *((char *)coll
->getDynamicClassID());
1823 errln("Dynamic class id for RuleBasedCollator should be 0");
1825 id
= *((char *)CollationKey::getStaticClassID());
1827 errln("Static class id for CollationKey should be 0");
1829 CollationKey
*key
= new CollationKey();
1830 id
= *((char *)key
->getDynamicClassID());
1832 errln("Dynamic class id for CollationKey should be 0");
1834 id
= *((char *)CollationElementIterator::getStaticClassID());
1836 errln("Static class id for CollationElementIterator should be 0");
1838 UnicodeString
str("testing");
1839 CollationElementIterator
*iter
= coll
->createCollationElementIterator(str
);
1840 id
= *((char *)iter
->getDynamicClassID());
1842 errln("Dynamic class id for CollationElementIterator should be 0");
1849 class TestCollator
: public Collator
1852 virtual Collator
* clone(void) const;
1854 // dang, markus says we can't use 'using' in ICU. I hate doing this for
1855 // deprecated methods...
1857 // using Collator::compare;
1859 virtual EComparisonResult
compare(const UnicodeString
& source
,
1860 const UnicodeString
& target
) const
1862 return Collator::compare(source
, target
);
1865 virtual EComparisonResult
compare(const UnicodeString
& source
,
1866 const UnicodeString
& target
,
1867 int32_t length
) const
1869 return Collator::compare(source
, target
, length
);
1872 virtual EComparisonResult
compare(const UChar
* source
,
1873 int32_t sourceLength
,
1874 const UChar
* target
,
1875 int32_t targetLength
) const
1877 return Collator::compare(source
, sourceLength
, target
, targetLength
);
1881 virtual UCollationResult
compare(const UnicodeString
& source
,
1882 const UnicodeString
& target
,
1883 UErrorCode
& status
) const;
1884 virtual UCollationResult
compare(const UnicodeString
& source
,
1885 const UnicodeString
& target
,
1887 UErrorCode
& status
) const;
1888 virtual UCollationResult
compare(const UChar
* source
,
1889 int32_t sourceLength
,
1890 const UChar
* target
,
1891 int32_t targetLength
,
1892 UErrorCode
& status
) const;
1893 virtual CollationKey
& getCollationKey(const UnicodeString
& source
,
1895 UErrorCode
& status
) const;
1896 virtual CollationKey
& getCollationKey(const UChar
*source
,
1897 int32_t sourceLength
,
1899 UErrorCode
& status
) const;
1900 virtual int32_t hashCode(void) const;
1901 virtual const Locale
getLocale(ULocDataLocaleType type
,
1902 UErrorCode
& status
) const;
1903 virtual ECollationStrength
getStrength(void) const;
1904 virtual void setStrength(ECollationStrength newStrength
);
1905 virtual UClassID
getDynamicClassID(void) const;
1906 virtual void getVersion(UVersionInfo info
) const;
1907 virtual void setAttribute(UColAttribute attr
, UColAttributeValue value
,
1908 UErrorCode
&status
);
1909 virtual UColAttributeValue
getAttribute(UColAttribute attr
,
1910 UErrorCode
&status
);
1911 virtual uint32_t setVariableTop(const UChar
*varTop
, int32_t len
,
1912 UErrorCode
&status
);
1913 virtual uint32_t setVariableTop(const UnicodeString varTop
,
1914 UErrorCode
&status
);
1915 virtual void setVariableTop(const uint32_t varTop
, UErrorCode
&status
);
1916 virtual uint32_t getVariableTop(UErrorCode
&status
) const;
1917 virtual Collator
* safeClone(void);
1918 virtual int32_t getSortKey(const UnicodeString
& source
,
1920 int32_t resultLength
) const;
1921 virtual int32_t getSortKey(const UChar
*source
, int32_t sourceLength
,
1922 uint8_t*result
, int32_t resultLength
) const;
1923 virtual UnicodeSet
*getTailoredSet(UErrorCode
&status
) const;
1924 virtual UBool
operator!=(const Collator
& other
) const;
1925 virtual void setLocales(const Locale
& requestedLocale
, const Locale
& validLocale
, const Locale
& actualLocale
);
1926 TestCollator() : Collator() {};
1927 TestCollator(UCollationStrength collationStrength
,
1928 UNormalizationMode decompositionMode
) : Collator(collationStrength
, decompositionMode
) {};
1931 inline UBool
TestCollator::operator!=(const Collator
& other
) const {
1932 return Collator::operator!=(other
);
1935 #define returnEComparisonResult(data) \
1936 if (data < 0) return Collator::LESS;\
1937 if (data > 0) return Collator::GREATER;\
1938 return Collator::EQUAL;
1940 Collator
* TestCollator::clone() const
1942 return new TestCollator();
1945 UCollationResult
TestCollator::compare(const UnicodeString
& source
,
1946 const UnicodeString
& target
,
1947 UErrorCode
& status
) const
1949 if(U_SUCCESS(status
)) {
1950 return UCollationResult(source
.compare(target
));
1956 UCollationResult
TestCollator::compare(const UnicodeString
& source
,
1957 const UnicodeString
& target
,
1959 UErrorCode
& status
) const
1961 if(U_SUCCESS(status
)) {
1962 return UCollationResult(source
.compare(0, length
, target
));
1968 UCollationResult
TestCollator::compare(const UChar
* source
,
1969 int32_t sourceLength
,
1970 const UChar
* target
,
1971 int32_t targetLength
,
1972 UErrorCode
& status
) const
1974 UnicodeString
s(source
, sourceLength
);
1975 UnicodeString
t(target
, targetLength
);
1976 return compare(s
, t
, status
);
1979 CollationKey
& TestCollator::getCollationKey(const UnicodeString
& source
,
1981 UErrorCode
& status
) const
1985 length
= source
.extract(temp
, length
, NULL
, status
);
1987 CollationKey
tempkey((uint8_t*)temp
, length
);
1992 CollationKey
& TestCollator::getCollationKey(const UChar
*source
,
1993 int32_t sourceLength
,
1995 UErrorCode
& status
) const
1997 //s tack allocation used since collationkey does not keep the unicodestring
1998 UnicodeString
str(source
, sourceLength
);
1999 return getCollationKey(str
, key
, status
);
2002 int32_t TestCollator::getSortKey(const UnicodeString
& source
, uint8_t* result
,
2003 int32_t resultLength
) const
2005 UErrorCode status
= U_ZERO_ERROR
;
2006 int32_t length
= source
.extract((char *)result
, resultLength
, NULL
,
2012 int32_t TestCollator::getSortKey(const UChar
*source
, int32_t sourceLength
,
2013 uint8_t*result
, int32_t resultLength
) const
2015 UnicodeString
str(source
, sourceLength
);
2016 return getSortKey(str
, result
, resultLength
);
2019 int32_t TestCollator::hashCode() const
2024 const Locale
TestCollator::getLocale(ULocDataLocaleType type
,
2025 UErrorCode
& status
) const
2027 // api not used, this is to make the compiler happy
2028 if (U_FAILURE(status
)) {
2029 type
= ULOC_DATA_LOCALE_TYPE_LIMIT
;
2034 Collator::ECollationStrength
TestCollator::getStrength() const
2039 void TestCollator::setStrength(Collator::ECollationStrength newStrength
)
2041 // api not used, this is to make the compiler happy
2042 newStrength
= TERTIARY
;
2045 UClassID
TestCollator::getDynamicClassID(void) const
2050 void TestCollator::getVersion(UVersionInfo info
) const
2052 // api not used, this is to make the compiler happy
2053 memset(info
, 0, U_MAX_VERSION_LENGTH
);
2056 void TestCollator::setAttribute(UColAttribute attr
, UColAttributeValue value
,
2059 // api not used, this is to make the compiler happy
2060 if (U_FAILURE(status
)) {
2061 attr
= UCOL_ATTRIBUTE_COUNT
;
2066 UColAttributeValue
TestCollator::getAttribute(UColAttribute attr
,
2069 // api not used, this is to make the compiler happy
2070 if (U_FAILURE(status
) || attr
== UCOL_ATTRIBUTE_COUNT
) {
2073 return UCOL_DEFAULT
;
2076 uint32_t TestCollator::setVariableTop(const UChar
*varTop
, int32_t len
,
2079 // api not used, this is to make the compiler happy
2080 if (U_SUCCESS(status
) && (varTop
== 0 || len
< -1)) {
2081 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2086 uint32_t TestCollator::setVariableTop(const UnicodeString varTop
,
2089 // api not used, this is to make the compiler happy
2090 if (U_SUCCESS(status
) && varTop
.length() == 0) {
2091 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2096 void TestCollator::setVariableTop(const uint32_t varTop
, UErrorCode
&status
)
2098 // api not used, this is to make the compiler happy
2099 if (U_SUCCESS(status
) && varTop
== 0) {
2100 status
= U_ILLEGAL_ARGUMENT_ERROR
;
2104 uint32_t TestCollator::getVariableTop(UErrorCode
&status
) const
2107 // api not used, this is to make the compiler happy
2108 if (U_SUCCESS(status
)) {
2111 return (uint32_t)(0xFFFFFFFFu
);
2114 Collator
* TestCollator::safeClone(void)
2116 return new TestCollator();
2119 UnicodeSet
* TestCollator::getTailoredSet(UErrorCode
&status
) const
2121 return Collator::getTailoredSet(status
);
2124 void TestCollator::setLocales(const Locale
& requestedLocale
, const Locale
& validLocale
, const Locale
& actualLocale
)
2126 Collator::setLocales(requestedLocale
, validLocale
, actualLocale
);
2130 void CollationAPITest::TestSubclass()
2134 doAssert(col1
!= col2
, "2 instance of TestCollator should be different");
2135 if (col1
.hashCode() != col2
.hashCode()) {
2136 errln("Every TestCollator has the same hashcode");
2138 UnicodeString
abc("abc", 3);
2139 UnicodeString
bcd("bcd", 3);
2140 if (col1
.compare(abc
, bcd
) != abc
.compare(bcd
)) {
2141 errln("TestCollator compare should be the same as the default "
2142 "string comparison");
2145 UErrorCode status
= U_ZERO_ERROR
;
2146 col1
.getCollationKey(abc
, key
, status
);
2148 const char* bytes
= (const char *)key
.getByteArray(length
);
2149 UnicodeString
keyarray(bytes
, length
, NULL
, status
);
2150 if (abc
!= keyarray
) {
2151 errln("TestCollator collationkey API is returning wrong values");
2154 UnicodeSet
expectedset(0, 0x10FFFF);
2155 UnicodeSet
*defaultset
= col1
.getTailoredSet(status
);
2156 if (!defaultset
->containsAll(expectedset
)
2157 || !expectedset
.containsAll(*defaultset
)) {
2158 errln("Error: expected default tailoring to be 0 to 0x10ffff");
2162 // use base class implementation
2163 Locale loc1
= Locale::getGermany();
2164 Locale loc2
= Locale::getFrance();
2165 col1
.setLocales(loc1
, loc2
, loc2
); // default implementation has no effect
2167 UnicodeString displayName
;
2168 col1
.getDisplayName(loc1
, loc2
, displayName
); // de_DE collator in fr_FR locale
2170 TestCollator
col3(UCOL_TERTIARY
, UNORM_NONE
);
2171 UnicodeString
a("a");
2172 UnicodeString
b("b");
2173 Collator::EComparisonResult result
= Collator::EComparisonResult(a
.compare(b
));
2174 if(col1
.compare(a
, b
) != result
) {
2175 errln("Collator doesn't give default result");
2177 if(col1
.compare(a
, b
, 1) != result
) {
2178 errln("Collator doesn't give default result");
2180 if(col1
.compare(a
.getBuffer(), a
.length(), b
.getBuffer(), b
.length()) != result
) {
2181 errln("Collator doesn't give default result");
2185 void CollationAPITest::TestNULLCharTailoring()
2187 UErrorCode status
= U_ZERO_ERROR
;
2188 UChar buf
[256] = {0};
2189 int32_t len
= u_unescape("&a < '\\u0000'", buf
, 256);
2190 UnicodeString
first((UChar
)0x0061);
2191 UnicodeString
second((UChar
)0);
2192 RuleBasedCollator
*coll
= new RuleBasedCollator(UnicodeString(buf
, len
), status
);
2193 if(U_FAILURE(status
)) {
2195 errcheckln(status
, "Failed to open collator - %s", u_errorName(status
));
2198 UCollationResult res
= coll
->compare(first
, second
, status
);
2199 if(res
!= UCOL_LESS
) {
2200 errln("a should be less then NULL after tailoring");
2205 void CollationAPITest::TestClone() {
2207 UErrorCode status
= U_ZERO_ERROR
;
2208 RuleBasedCollator
* c0
= (RuleBasedCollator
*)Collator::createInstance(status
);
2210 if (U_FAILURE(status
)) {
2211 errcheckln(status
, "Collator::CreateInstance(status) failed with %s", u_errorName(status
));
2215 c0
->setStrength(Collator::TERTIARY
);
2216 dump("c0", c0
, status
);
2219 RuleBasedCollator
* c1
= (RuleBasedCollator
*)Collator::createInstance(status
);
2220 c1
->setStrength(Collator::TERTIARY
);
2221 UColAttributeValue val
= c1
->getAttribute(UCOL_CASE_FIRST
, status
);
2222 if(val
== UCOL_LOWER_FIRST
){
2223 c1
->setAttribute(UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, status
);
2225 c1
->setAttribute(UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, status
);
2227 dump("c0", c0
, status
);
2228 dump("c1", c1
, status
);
2231 RuleBasedCollator
* c2
= (RuleBasedCollator
*)c1
->clone();
2232 val
= c2
->getAttribute(UCOL_CASE_FIRST
, status
);
2233 if(val
== UCOL_LOWER_FIRST
){
2234 c2
->setAttribute(UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, status
);
2236 c2
->setAttribute(UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, status
);
2238 if(U_FAILURE(status
)){
2239 errln("set and get attributes of collator failed. %s\n", u_errorName(status
));
2242 dump("c0", c0
, status
);
2243 dump("c1", c1
, status
);
2244 dump("c2", c2
, status
);
2246 errln("The cloned objects refer to same data");
2253 void CollationAPITest::dump(UnicodeString msg
, RuleBasedCollator
* c
, UErrorCode
& status
) {
2254 const char* bigone
= "One";
2255 const char* littleone
= "one";
2257 logln(msg
+ " " + c
->compare(bigone
, littleone
) +
2258 " s: " + c
->getStrength() +
2259 " u: " + c
->getAttribute(UCOL_CASE_FIRST
, status
));
2261 void CollationAPITest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par */)
2263 if (exec
) logln("TestSuite CollationAPITest: ");
2265 case 0: name
= "TestProperty"; if (exec
) TestProperty(/* par */); break;
2266 case 1: name
= "TestOperators"; if (exec
) TestOperators(/* par */); break;
2267 case 2: name
= "TestDuplicate"; if (exec
) TestDuplicate(/* par */); break;
2268 case 3: name
= "TestCompare"; if (exec
) TestCompare(/* par */); break;
2269 case 4: name
= "TestHashCode"; if (exec
) TestHashCode(/* par */); break;
2270 case 5: name
= "TestCollationKey"; if (exec
) TestCollationKey(/* par */); break;
2271 case 6: name
= "TestElemIter"; if (exec
) TestElemIter(/* par */); break;
2272 case 7: name
= "TestGetAll"; if (exec
) TestGetAll(/* par */); break;
2273 case 8: name
= "TestRuleBasedColl"; if (exec
) TestRuleBasedColl(/* par */); break;
2274 case 9: name
= "TestDecomposition"; if (exec
) TestDecomposition(/* par */); break;
2275 case 10: name
= "TestSafeClone"; if (exec
) TestSafeClone(/* par */); break;
2276 case 11: name
= "TestSortKey"; if (exec
) TestSortKey(); break;
2277 case 12: name
= "TestMaxExpansion"; if (exec
) TestMaxExpansion(); break;
2278 case 13: name
= "TestDisplayName"; if (exec
) TestDisplayName(); break;
2279 case 14: name
= "TestAttribute"; if (exec
) TestAttribute(); break;
2280 case 15: name
= "TestVariableTopSetting"; if (exec
) TestVariableTopSetting(); break;
2281 case 16: name
= "TestRules"; if (exec
) TestRules(); break;
2282 case 17: name
= "TestGetLocale"; if (exec
) TestGetLocale(); break;
2283 case 18: name
= "TestBounds"; if (exec
) TestBounds(); break;
2284 case 19: name
= "TestGetTailoredSet"; if (exec
) TestGetTailoredSet(); break;
2285 case 20: name
= "TestUClassID"; if (exec
) TestUClassID(); break;
2286 case 21: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
2287 case 22: name
= "TestNULLCharTailoring"; if (exec
) TestNULLCharTailoring(); break;
2288 case 23: name
= "TestClone"; if (exec
) TestClone(); break;
2289 default: name
= ""; break;
2293 #endif /* #if !UCONFIG_NO_COLLATION */