1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_COLLATION
13 #include "unicode/coll.h"
14 #include "unicode/localpointer.h"
15 #include "unicode/tblcoll.h"
16 #include "unicode/unistr.h"
17 #include "unicode/sortkey.h"
23 CollationRegressionTest::CollationRegressionTest()
25 UErrorCode status = U_ZERO_ERROR;
27 en_us = (RuleBasedCollator *)Collator::createInstance(Locale::getUS(), status);
28 if(U_FAILURE(status)) {
31 errcheckln(status, "Collator creation failed with %s", u_errorName(status));
36 CollationRegressionTest::~CollationRegressionTest()
44 // CollationElementIterator.reset() doesn't work
46 void CollationRegressionTest::Test4048446(/* char* par */)
48 const UnicodeString test1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";
49 const UnicodeString test2 = "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?";
50 CollationElementIterator *i1 = en_us->createCollationElementIterator(test1);
51 CollationElementIterator *i2 = en_us->createCollationElementIterator(test1);
52 UErrorCode status = U_ZERO_ERROR;
54 if (i1 == NULL|| i2 == NULL)
56 errln("Could not create CollationElementIterator's");
62 while (i1->next(status) != CollationElementIterator::NULLORDER)
64 if (U_FAILURE(status))
66 errln("error calling next()");
76 assertEqual(*i1, *i2);
84 // Collator -> rules -> Collator round-trip broken for expanding characters
86 void CollationRegressionTest::Test4051866(/* char* par */)
89 UErrorCode status = U_ZERO_ERROR;
93 rules += (UChar)0x3080;
95 rules += (UChar)0x1530;
98 rules += (UChar)0x3080;
100 rules += (UChar)0x1520;
103 // Build a collator containing expanding characters
104 LocalPointer<RuleBasedCollator> c1(new RuleBasedCollator(rules, status), status);
105 if (U_FAILURE(status)) {
106 errln("RuleBasedCollator(rule string) failed - %s", u_errorName(status));
110 // Build another using the rules from the first
111 LocalPointer<RuleBasedCollator> c2(new RuleBasedCollator(c1->getRules(), status), status);
112 if (U_FAILURE(status)) {
113 errln("RuleBasedCollator(rule string from other RBC) failed - %s", u_errorName(status));
117 // Make sure they're the same
118 if (!(c1->getRules() == c2->getRules()))
120 errln("Rules are not equal");
126 // Collator thinks "black-bird" == "black"
128 void CollationRegressionTest::Test4053636(/* char* par */)
130 if (en_us->equals("black_bird", "black"))
132 errln("black-bird == black");
138 // CollationElementIterator will not work correctly if the associated
139 // Collator object's mode is changed
141 void CollationRegressionTest::Test4054238(/* char* par */)
143 const UChar chars3[] = {0x61, 0x00FC, 0x62, 0x65, 0x63, 0x6b, 0x20, 0x47, 0x72, 0x00F6, 0x00DF, 0x65, 0x20, 0x4c, 0x00FC, 0x62, 0x63, 0x6b, 0};
144 const UnicodeString test3(chars3);
145 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
147 // NOTE: The Java code uses en_us to create the CollationElementIterators
148 // but I'm pretty sure that's wrong, so I've changed this to use c.
149 UErrorCode status = U_ZERO_ERROR;
150 c->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
151 CollationElementIterator *i1 = c->createCollationElementIterator(test3);
158 // Collator::IDENTICAL documented but not implemented
160 void CollationRegressionTest::Test4054734(/* char* par */)
163 Here's the original Java:
166 "\u0001", "<", "\u0002",
167 "\u0001", "=", "\u0001",
168 "A\u0001", ">", "~\u0002", // Ensure A and ~ are not compared bitwise
169 "\u00C0", "=", "A\u0300" // Decomp should make these equal
172 String[] nodecomp = {
173 "\u00C0", ">", "A\u0300" // A-grave vs. A combining-grave
177 static const UChar decomp[][CollationRegressionTest::MAX_TOKEN_LEN] =
179 {0x0001, 0}, {0x3c, 0}, {0x0002, 0},
180 {0x0001, 0}, {0x3d, 0}, {0x0001, 0},
181 {0x41, 0x0001, 0}, {0x3e, 0}, {0x7e, 0x0002, 0},
182 {0x00c0, 0}, {0x3d, 0}, {0x41, 0x0300, 0}
186 UErrorCode status = U_ZERO_ERROR;
187 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
189 c->setStrength(Collator::IDENTICAL);
191 c->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
192 compareArray(*c, decomp, UPRV_LENGTHOF(decomp));
199 // Full Decomposition mode not implemented
201 void CollationRegressionTest::Test4054736(/* char* par */)
203 UErrorCode status = U_ZERO_ERROR;
204 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
206 c->setStrength(Collator::SECONDARY);
207 c->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
209 static const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN] =
211 {0xFB4F, 0}, {0x3d, 0}, {0x05D0, 0x05DC} // Alef-Lamed vs. Alef, Lamed
214 compareArray(*c, tests, UPRV_LENGTHOF(tests));
221 // Collator::createInstance() causes an ArrayIndexOutofBoundsException for Korean
223 void CollationRegressionTest::Test4058613(/* char* par */)
225 // Creating a default collator doesn't work when Korean is the default
228 Locale oldDefault = Locale::getDefault();
229 UErrorCode status = U_ZERO_ERROR;
231 Locale::setDefault(Locale::getKorean(), status);
233 if (U_FAILURE(status))
235 errln("Could not set default locale to Locale::KOREAN");
241 c = Collator::createInstance("en_US", status);
243 if (c == NULL || U_FAILURE(status))
245 errln("Could not create a Korean collator");
246 Locale::setDefault(oldDefault, status);
251 // Since the fix to this bug was to turn off decomposition for Korean collators,
252 // ensure that's what we got
253 if (c->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_OFF)
255 errln("Decomposition is not set to NO_DECOMPOSITION for Korean collator");
260 Locale::setDefault(oldDefault, status);
265 // RuleBasedCollator.getRules does not return the exact pattern as input
266 // for expanding character sequences
268 void CollationRegressionTest::Test4059820(/* char* par */)
270 UErrorCode status = U_ZERO_ERROR;
272 RuleBasedCollator *c = NULL;
273 UnicodeString rules = "&9 < a < b , c/a < d < z";
275 c = new RuleBasedCollator(rules, status);
277 if (c == NULL || U_FAILURE(status))
279 errln("Failure building a collator.");
284 if ( c->getRules().indexOf("c/a") == -1)
286 errln("returned rules do not contain 'c/a'");
294 // MergeCollation::fixEntry broken for "& H < \u0131, \u0130, i, I"
296 void CollationRegressionTest::Test4060154(/* char* par */)
298 UErrorCode status = U_ZERO_ERROR;
301 rules += "&f < g, G < h, H < i, I < j, J";
303 rules += (UChar)0x0131;
305 rules += (UChar)0x0130;
308 RuleBasedCollator *c = NULL;
310 c = new RuleBasedCollator(rules, status);
312 if (c == NULL || U_FAILURE(status))
314 errln("failure building collator.");
319 c->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
322 String[] tertiary = {
326 "\u0131", "<", "\u0130",
332 static const UChar tertiary[][CollationRegressionTest::MAX_TOKEN_LEN] =
334 {0x41, 0}, {0x3c, 0}, {0x42, 0},
335 {0x48, 0}, {0x3c, 0}, {0x0131, 0},
336 {0x48, 0}, {0x3c, 0}, {0x49, 0},
337 {0x0131, 0}, {0x3c, 0}, {0x0130, 0},
338 {0x0130, 0}, {0x3c, 0}, {0x69, 0},
339 {0x0130, 0}, {0x3e, 0}, {0x48, 0}
342 c->setStrength(Collator::TERTIARY);
343 compareArray(*c, tertiary, UPRV_LENGTHOF(tertiary));
346 String[] secondary = {
348 "\u0131", "=", "\u0130",
351 static const UChar secondary[][CollationRegressionTest::MAX_TOKEN_LEN] =
353 {0x48, 0}, {0x3c, 0}, {0x49, 0},
354 {0x0131, 0}, {0x3d, 0}, {0x0130, 0}
357 c->setStrength(Collator::PRIMARY);
358 compareArray(*c, secondary, UPRV_LENGTHOF(secondary));
365 // Secondary/Tertiary comparison incorrect in French Secondary
367 void CollationRegressionTest::Test4062418(/* char* par */)
369 UErrorCode status = U_ZERO_ERROR;
371 RuleBasedCollator *c = NULL;
373 c = (RuleBasedCollator *) Collator::createInstance(Locale::getCanadaFrench(), status);
375 if (c == NULL || U_FAILURE(status))
377 errln("Failed to create collator for Locale::getCanadaFrench()");
382 c->setStrength(Collator::SECONDARY);
386 "p\u00eache", "<", "p\u00e9ch\u00e9", // Comparing accents from end, p\u00e9ch\u00e9 is greater
389 static const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN] =
391 {0x70, 0x00EA, 0x63, 0x68, 0x65, 0}, {0x3c, 0}, {0x70, 0x00E9, 0x63, 0x68, 0x00E9, 0}
394 compareArray(*c, tests, UPRV_LENGTHOF(tests));
401 // Collator::compare() method broken if either string contains spaces
403 void CollationRegressionTest::Test4065540(/* char* par */)
405 if (en_us->compare("abcd e", "abcd f") == 0)
407 errln("'abcd e' == 'abcd f'");
413 // Unicode characters need to be recursively decomposed to get the
414 // correct result. For example,
415 // u1EB1 -> \u0103 + \u0300 -> a + \u0306 + \u0300.
417 void CollationRegressionTest::Test4066189(/* char* par */)
419 static const UChar chars1[] = {0x1EB1, 0};
420 static const UChar chars2[] = {0x61, 0x0306, 0x0300, 0};
421 const UnicodeString test1(chars1);
422 const UnicodeString test2(chars2);
423 UErrorCode status = U_ZERO_ERROR;
425 // NOTE: The java code used en_us to create the
426 // CollationElementIterator's. I'm pretty sure that
427 // was wrong, so I've change the code to use c1 and c2
428 RuleBasedCollator *c1 = (RuleBasedCollator *) en_us->clone();
429 c1->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
430 CollationElementIterator *i1 = c1->createCollationElementIterator(test1);
432 RuleBasedCollator *c2 = (RuleBasedCollator *) en_us->clone();
433 c2->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_OFF, status);
434 CollationElementIterator *i2 = c2->createCollationElementIterator(test2);
436 assertEqual(*i1, *i2);
446 // French secondary collation checking at the end of compare iteration fails
448 void CollationRegressionTest::Test4066696(/* char* par */)
450 UErrorCode status = U_ZERO_ERROR;
451 RuleBasedCollator *c = NULL;
453 c = (RuleBasedCollator *)Collator::createInstance(Locale::getCanadaFrench(), status);
455 if (c == NULL || U_FAILURE(status))
457 errln("Failure creating collator for Locale::getCanadaFrench()");
462 c->setStrength(Collator::SECONDARY);
466 "\u00e0", "<", "\u01fa", // a-grave < A-ring-acute
472 "\u00e0", ">", "\u01fa", // a-grave < A-ring-acute
477 static const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN] =
479 {0x00E0, 0}, {0x3e, 0}, {0x01FA, 0}
482 compareArray(*c, tests, UPRV_LENGTHOF(tests));
489 // Bad canonicalization of same-class combining characters
491 void CollationRegressionTest::Test4076676(/* char* par */)
493 // These combining characters are all in the same class, so they should not
494 // be reordered, and they should compare as unequal.
495 static const UChar s1[] = {0x41, 0x0301, 0x0302, 0x0300, 0};
496 static const UChar s2[] = {0x41, 0x0302, 0x0300, 0x0301, 0};
498 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
499 c->setStrength(Collator::TERTIARY);
501 if (c->compare(s1,s2) == 0)
503 errln("Same-class combining chars were reordered");
511 // RuleBasedCollator::operator==(NULL) throws NullPointerException
513 void CollationRegressionTest::Test4079231(/* char* par */)
515 // I don't think there's any way to write this test
516 // in C++. The following is equivalent to the Java,
517 // but doesn't compile 'cause NULL can't be converted
520 // if (en_us->operator==(NULL))
522 // errln("en_us->operator==(NULL) returned TRUE");
527 if (en_us->equals(null)) {
528 errln("en_us->equals(null) returned true");
531 catch (Exception e) {
532 errln("en_us->equals(null) threw " + e.toString());
539 // RuleBasedCollator breaks on "< a < bb" rule
541 void CollationRegressionTest::Test4078588(/* char *par */)
543 UErrorCode status = U_ZERO_ERROR;
544 RuleBasedCollator *rbc = new RuleBasedCollator("&9 < a < bb", status);
546 if (rbc == NULL || U_FAILURE(status))
548 errln("Failed to create RuleBasedCollator.");
553 Collator::EComparisonResult result = rbc->compare("a","bb");
555 if (result != Collator::LESS)
557 errln((UnicodeString)"Compare(a,bb) returned " + (int)result
558 + (UnicodeString)"; expected -1");
566 // Combining characters in different classes not reordered properly.
568 void CollationRegressionTest::Test4081866(/* char* par */)
570 // These combining characters are all in different classes,
571 // so they should be reordered and the strings should compare as equal.
572 static const UChar s1[] = {0x41, 0x0300, 0x0316, 0x0327, 0x0315, 0};
573 static const UChar s2[] = {0x41, 0x0327, 0x0316, 0x0315, 0x0300, 0};
575 UErrorCode status = U_ZERO_ERROR;
576 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
577 c->setStrength(Collator::TERTIARY);
579 // Now that the default collators are set to NO_DECOMPOSITION
580 // (as a result of fixing bug 4114077), we must set it explicitly
581 // when we're testing reordering behavior. -- lwerner, 5/5/98
582 c->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
584 if (c->compare(s1,s2) != 0)
586 errln("Combining chars were not reordered");
594 // string comparison errors in Scandinavian collators
596 void CollationRegressionTest::Test4087241(/* char* par */)
598 UErrorCode status = U_ZERO_ERROR;
599 Locale da_DK("da", "DK");
600 RuleBasedCollator *c = NULL;
602 c = (RuleBasedCollator *) Collator::createInstance(da_DK, status);
604 if (c == NULL || U_FAILURE(status))
606 errln("Failed to create collator for da_DK locale");
611 c->setStrength(Collator::SECONDARY);
613 static const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN] =
615 {0x7a, 0}, {0x3c, 0}, {0x00E6, 0}, // z < ae
616 {0x61, 0x0308, 0}, {0x3c, 0}, {0x61, 0x030A, 0}, // a-umlaut < a-ring
617 {0x59, 0}, {0x3c, 0}, {0x75, 0x0308, 0}, // Y < u-umlaut
620 compareArray(*c, tests, UPRV_LENGTHOF(tests));
627 // CollationKey takes ignorable strings into account when it shouldn't
629 void CollationRegressionTest::Test4087243(/* char* par */)
631 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
632 c->setStrength(Collator::TERTIARY);
634 static const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN] =
636 {0x31, 0x32, 0x33, 0}, {0x3d, 0}, {0x31, 0x32, 0x33, 0x0001, 0} // 1 2 3 = 1 2 3 ctrl-A
639 compareArray(*c, tests, UPRV_LENGTHOF(tests));
647 // Micro symbol and greek lowercase letter Mu should sort identically
649 void CollationRegressionTest::Test4092260(/* char* par */)
651 UErrorCode status = U_ZERO_ERROR;
655 c = Collator::createInstance(el, status);
657 if (c == NULL || U_FAILURE(status))
659 errln("Failed to create collator for el locale.");
664 // These now have tertiary differences in UCA
665 c->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, status);
667 static const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN] =
669 {0x00B5, 0}, {0x3d, 0}, {0x03BC, 0}
672 compareArray(*c, tests, UPRV_LENGTHOF(tests));
679 void CollationRegressionTest::Test4095316(/* char* par */)
681 UErrorCode status = U_ZERO_ERROR;
682 Locale el_GR("el", "GR");
683 Collator *c = Collator::createInstance(el_GR, status);
685 if (c == NULL || U_FAILURE(status))
687 errln("Failed to create collator for el_GR locale");
691 // These now have tertiary differences in UCA
692 //c->setStrength(Collator::TERTIARY);
693 c->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, status);
695 static const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN] =
697 {0x03D4, 0}, {0x3d, 0}, {0x03AB, 0}
700 compareArray(*c, tests, UPRV_LENGTHOF(tests));
707 void CollationRegressionTest::Test4101940(/* char* par */)
709 UErrorCode status = U_ZERO_ERROR;
710 RuleBasedCollator *c = NULL;
711 UnicodeString rules = "&9 < a < b";
712 UnicodeString nothing = "";
714 c = new RuleBasedCollator(rules, status);
716 if (c == NULL || U_FAILURE(status))
718 errln("Failed to create RuleBasedCollator");
723 CollationElementIterator *i = c->createCollationElementIterator(nothing);
726 if (i->next(status) != CollationElementIterator::NULLORDER)
728 errln("next did not return NULLORDER");
737 // Collator::compare not handling spaces properly
739 void CollationRegressionTest::Test4103436(/* char* par */)
741 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
742 c->setStrength(Collator::TERTIARY);
744 static const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN] =
746 {0x66, 0x69, 0x6c, 0x65, 0}, {0x3c, 0}, {0x66, 0x69, 0x6c, 0x65, 0x20, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0},
747 {0x66, 0x69, 0x6c, 0x65, 0}, {0x3c, 0}, {0x66, 0x69, 0x6c, 0x65, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0}
750 compareArray(*c, tests, UPRV_LENGTHOF(tests));
757 // Collation not Unicode conformant with Hangul syllables
759 void CollationRegressionTest::Test4114076(/* char* par */)
761 UErrorCode status = U_ZERO_ERROR;
762 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
763 c->setStrength(Collator::TERTIARY);
766 // With Canonical decomposition, Hangul syllables should get decomposed
767 // into Jamo, but Jamo characters should not be decomposed into
770 static const UChar test1[][CollationRegressionTest::MAX_TOKEN_LEN] =
772 {0xd4db, 0}, {0x3d, 0}, {0x1111, 0x1171, 0x11b6, 0}
775 c->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
776 compareArray(*c, test1, UPRV_LENGTHOF(test1));
779 // *In earlier versions of Unicode, jamo characters like ksf
780 // had compatibility mappings to kf + sf. These mappings were
781 // removed in Unicode 2.1.9 to ensure that Hangul syllables are maintained.)
782 // That is, the following test is obsolete as of 2.1.9
784 //obsolete- // With Full decomposition, it should go all the way down to
785 //obsolete- // conjoining Jamo characters.
787 //obsolete- static const UChar test2[][CollationRegressionTest::MAX_TOKEN_LEN] =
789 //obsolete- {0xd4db, 0}, {0x3d, 0}, {0x1111, 0x116e, 0x1175, 0x11af, 0x11c2, 0}
792 //obsolete- c->setDecomposition(Normalizer::DECOMP_COMPAT);
793 //obsolete- compareArray(*c, test2, UPRV_LENGTHOF(test2));
801 // Collator::getCollationKey was hanging on certain character sequences
803 void CollationRegressionTest::Test4124632(/* char* par */)
805 UErrorCode status = U_ZERO_ERROR;
806 Collator *coll = NULL;
808 coll = Collator::createInstance(Locale::getJapan(), status);
810 if (coll == NULL || U_FAILURE(status))
812 errln("Failed to create collator for Locale::JAPAN");
817 static const UChar test[] = {0x41, 0x0308, 0x62, 0x63, 0};
820 coll->getCollationKey(test, key, status);
822 if (key.isBogus() || U_FAILURE(status))
824 errln("CollationKey creation failed.");
832 // sort order of french words with multiple accents has errors
834 void CollationRegressionTest::Test4132736(/* char* par */)
836 UErrorCode status = U_ZERO_ERROR;
840 c = Collator::createInstance(Locale::getCanadaFrench(), status);
841 c->setStrength(Collator::TERTIARY);
843 if (c == NULL || U_FAILURE(status))
845 errln("Failed to create a collator for Locale::getCanadaFrench()");
850 static const UChar test1[][CollationRegressionTest::MAX_TOKEN_LEN] =
852 {0x65, 0x0300, 0x65, 0x0301, 0}, {0x3c, 0}, {0x65, 0x0301, 0x65, 0x0300, 0},
853 {0x65, 0x0300, 0x0301, 0}, {0x3c, 0}, {0x65, 0x0301, 0x0300, 0}
856 compareArray(*c, test1, UPRV_LENGTHOF(test1));
863 // The sorting using java.text.CollationKey is not in the exact order
865 void CollationRegressionTest::Test4133509(/* char* par */)
867 static const UChar test1[][CollationRegressionTest::MAX_TOKEN_LEN] =
869 {0x45, 0x78, 0x63, 0x65, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0}, {0x3c, 0}, {0x45, 0x78, 0x63, 0x65, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x49, 0x6e, 0x49, 0x6e, 0x69, 0x74, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x72, 0x45, 0x72, 0x72, 0x6f, 0x72, 0},
870 {0x47, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0}, {0x3c, 0}, {0x47, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x45, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e, 0x6d, 0x65, 0x6e, 0x74, 0},
871 {0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0}, {0x3c, 0}, {0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x42, 0x75, 0x66, 0x66, 0x65, 0x72, 0}
874 compareArray(*en_us, test1, UPRV_LENGTHOF(test1));
879 // Collation with decomposition off doesn't work for Europe
881 void CollationRegressionTest::Test4114077(/* char* par */)
883 // Ensure that we get the same results with decomposition off
884 // as we do with it on....
886 UErrorCode status = U_ZERO_ERROR;
887 RuleBasedCollator *c = (RuleBasedCollator *) en_us->clone();
888 c->setStrength(Collator::TERTIARY);
890 static const UChar test1[][CollationRegressionTest::MAX_TOKEN_LEN] =
892 {0x00C0, 0}, {0x3d, 0}, {0x41, 0x0300, 0}, // Should be equivalent
893 {0x70, 0x00ea, 0x63, 0x68, 0x65, 0}, {0x3e, 0}, {0x70, 0x00e9, 0x63, 0x68, 0x00e9, 0},
894 {0x0204, 0}, {0x3d, 0}, {0x45, 0x030F, 0},
895 {0x01fa, 0}, {0x3d, 0}, {0x41, 0x030a, 0x0301, 0}, // a-ring-acute -> a-ring, acute
897 {0x41, 0x0300, 0x0316, 0}, {0x3c, 0}, {0x41, 0x0316, 0x0300, 0} // No reordering --> unequal
900 c->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_OFF, status);
901 compareArray(*c, test1, UPRV_LENGTHOF(test1));
903 static const UChar test2[][CollationRegressionTest::MAX_TOKEN_LEN] =
905 {0x41, 0x0300, 0x0316, 0}, {0x3d, 0}, {0x41, 0x0316, 0x0300, 0} // Reordering --> equal
908 c->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
909 compareArray(*c, test2, UPRV_LENGTHOF(test2));
916 // Support for Swedish gone in 1.1.6 (Can't create Swedish collator)
918 void CollationRegressionTest::Test4141640(/* char* par */)
921 // Rather than just creating a Swedish collator, we might as well
922 // try to instantiate one for every locale available on the system
923 // in order to prevent this sort of bug from cropping up in the future
925 UErrorCode status = U_ZERO_ERROR;
926 int32_t i, localeCount;
927 const Locale *locales = Locale::getAvailableLocales(localeCount);
929 for (i = 0; i < localeCount; i += 1)
933 status = U_ZERO_ERROR;
934 c = Collator::createInstance(locales[i], status);
936 if (c == NULL || U_FAILURE(status))
938 UnicodeString msg, localeName;
940 msg += "Could not create collator for locale ";
941 msg += locales[i].getName();
952 // getCollationKey throws exception for spanish text
953 // Cannot reproduce this bug on 1.2, however it DOES fail on 1.1.6
955 void CollationRegressionTest::Test4139572(/* char* par */)
958 // Code pasted straight from the bug report
959 // (and then translated to C++ ;-)
961 // create spanish locale and collator
962 UErrorCode status = U_ZERO_ERROR;
963 Locale l("es", "es");
964 Collator *col = NULL;
966 col = Collator::createInstance(l, status);
968 if (col == NULL || U_FAILURE(status))
970 errln("Failed to create a collator for es_es locale.");
977 // this spanish phrase kills it!
978 col->getCollationKey("Nombre De Objeto", key, status);
980 if (key.isBogus() || U_FAILURE(status))
982 errln("Error creating CollationKey for \"Nombre De Ojbeto\"");
987 /* HSYS : RuleBasedCollator::compare() performance enhancements
988 compare() does not create CollationElementIterator() anymore.*/
990 class My4146160Collator : public RuleBasedCollator
993 My4146160Collator(RuleBasedCollator &rbc, UErrorCode &status);
994 ~My4146160Collator();
996 CollationElementIterator *createCollationElementIterator(const UnicodeString &text) const;
998 CollationElementIterator *createCollationElementIterator(const CharacterIterator &text) const;
1000 static int32_t count;
1003 int32_t My4146160Collator::count = 0;
1005 My4146160Collator::My4146160Collator(RuleBasedCollator &rbc, UErrorCode &status)
1006 : RuleBasedCollator(rbc.getRules(), status)
1010 My4146160Collator::~My4146160Collator()
1014 CollationElementIterator *My4146160Collator::createCollationElementIterator(const UnicodeString &text) const
1017 return RuleBasedCollator::createCollationElementIterator(text);
1020 CollationElementIterator *My4146160Collator::createCollationElementIterator(const CharacterIterator &text) const
1023 return RuleBasedCollator::createCollationElementIterator(text);
1028 // RuleBasedCollator doesn't use createCollationElementIterator internally
1030 void CollationRegressionTest::Test4146160(/* char* par */)
1034 // Use a custom collator class whose createCollationElementIterator
1035 // methods increment a count....
1037 UErrorCode status = U_ZERO_ERROR;
1040 My4146160Collator::count = 0;
1041 My4146160Collator *mc = NULL;
1043 mc = new My4146160Collator(*en_us, status);
1045 if (mc == NULL || U_FAILURE(status))
1047 errln("Failed to create a My4146160Collator.");
1052 mc->getCollationKey("1", key, status);
1054 if (key.isBogus() || U_FAILURE(status))
1056 errln("Failure to get a CollationKey from a My4146160Collator.");
1061 if (My4146160Collator::count < 1)
1063 errln("My4146160Collator::createCollationElementIterator not called for getCollationKey");
1066 My4146160Collator::count = 0;
1067 mc->compare("1", "2");
1069 if (My4146160Collator::count < 1)
1071 errln("My4146160Collator::createtCollationElementIterator not called for compare");
1078 void CollationRegressionTest::Test4179216() {
1079 // you can position a CollationElementIterator in the middle of
1080 // a contracting character sequence, yielding a bogus collation
1082 IcuTestErrorCode errorCode(*this, "Test4179216");
1083 RuleBasedCollator coll(en_us->getRules() + " & C < ch , cH , Ch , CH < cat < crunchy", errorCode);
1084 UnicodeString testText = "church church catcatcher runcrunchynchy";
1085 CollationElementIterator *iter = coll.createCollationElementIterator(testText);
1087 // test that the "ch" combination works properly
1088 iter->setOffset(4, errorCode);
1089 int32_t elt4 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1092 int32_t elt0 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1094 iter->setOffset(5, errorCode);
1095 int32_t elt5 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1097 // Compares and prints only 16-bit primary weights.
1098 if (elt4 != elt0 || elt5 != elt0) {
1099 errln("The collation elements at positions 0 (0x%04x), "
1100 "4 (0x%04x), and 5 (0x%04x) don't match.",
1104 // test that the "cat" combination works properly
1105 iter->setOffset(14, errorCode);
1106 int32_t elt14 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1108 iter->setOffset(15, errorCode);
1109 int32_t elt15 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1111 iter->setOffset(16, errorCode);
1112 int32_t elt16 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1114 iter->setOffset(17, errorCode);
1115 int32_t elt17 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1117 iter->setOffset(18, errorCode);
1118 int32_t elt18 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1120 iter->setOffset(19, errorCode);
1121 int32_t elt19 = CollationElementIterator::primaryOrder(iter->next(errorCode));
1123 // Compares and prints only 16-bit primary weights.
1124 if (elt14 != elt15 || elt14 != elt16 || elt14 != elt17
1125 || elt14 != elt18 || elt14 != elt19) {
1126 errln("\"cat\" elements don't match: elt14 = 0x%04x, "
1127 "elt15 = 0x%04x, elt16 = 0x%04x, elt17 = 0x%04x, "
1128 "elt18 = 0x%04x, elt19 = 0x%04x",
1129 elt14, elt15, elt16, elt17, elt18, elt19);
1132 // now generate a complete list of the collation elements,
1133 // first using next() and then using setOffset(), and
1134 // make sure both interfaces return the same set of elements
1137 int32_t elt = iter->next(errorCode);
1139 while (elt != CollationElementIterator::NULLORDER) {
1141 elt = iter->next(errorCode);
1144 LocalArray<UnicodeString> nextElements(new UnicodeString[count]);
1145 LocalArray<UnicodeString> setOffsetElements(new UnicodeString[count]);
1146 int32_t lastPos = 0;
1149 elt = iter->next(errorCode);
1151 while (elt != CollationElementIterator::NULLORDER) {
1152 nextElements[count++] = testText.tempSubStringBetween(lastPos, iter->getOffset());
1153 lastPos = iter->getOffset();
1154 elt = iter->next(errorCode);
1156 int32_t nextElementsLength = count;
1158 for (int32_t i = 0; i < testText.length(); ) {
1159 iter->setOffset(i, errorCode);
1160 lastPos = iter->getOffset();
1161 elt = iter->next(errorCode);
1162 setOffsetElements[count++] = testText.tempSubStringBetween(lastPos, iter->getOffset());
1163 i = iter->getOffset();
1165 for (int32_t i = 0; i < nextElementsLength; i++) {
1166 if (nextElements[i] == setOffsetElements[i]) {
1167 logln(nextElements[i]);
1169 errln(UnicodeString("Error: next() yielded ") + nextElements[i] +
1170 ", but setOffset() yielded " + setOffsetElements[i]);
1178 // nextSortKeyPart incorrect for EO_S1 collation
1179 static int32_t calcKeyIncremental(UCollator *coll, const UChar* text, int32_t len, uint8_t *keyBuf, int32_t /*keyBufLen*/, UErrorCode& status) {
1180 UCharIterator uiter;
1181 uint32_t state[2] = { 0, 0 };
1185 uiter_setString(&uiter, text, len);
1188 int32_t keyPartLen = ucol_nextSortKeyPart(coll, &uiter, state, &keyBuf[keyLen], count, &status);
1189 if (U_FAILURE(status)) {
1192 if (keyPartLen == 0) {
1195 keyLen += keyPartLen;
1200 void CollationRegressionTest::TestT7189() {
1201 UErrorCode status = U_ZERO_ERROR;
1205 static const UChar text1[][CollationRegressionTest::MAX_TOKEN_LEN] = {
1206 // "Achter De Hoven"
1207 { 0x41, 0x63, 0x68, 0x74, 0x65, 0x72, 0x20, 0x44, 0x65, 0x20, 0x48, 0x6F, 0x76, 0x65, 0x6E, 0x00 },
1209 { 0x41, 0x42, 0x43, 0x00 },
1211 { 0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x00 }
1214 static const UChar text2[][CollationRegressionTest::MAX_TOKEN_LEN] = {
1215 // "Achter de Hoven"
1216 { 0x41, 0x63, 0x68, 0x74, 0x65, 0x72, 0x20, 0x64, 0x65, 0x20, 0x48, 0x6F, 0x76, 0x65, 0x6E, 0x00 },
1218 { 0x61, 0x62, 0x63, 0x00 },
1220 { 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x00 }
1223 // Open the collator
1224 coll = ucol_openFromShortString("EO_S1", FALSE, NULL, &status);
1225 if (U_FAILURE(status)) {
1226 errln("Failed to create a collator for short string EO_S1");
1230 for (i = 0; i < UPRV_LENGTHOF(text1); i++) {
1231 uint8_t key1[100], key2[100];
1234 len1 = calcKeyIncremental(coll, text1[i], -1, key1, sizeof(key1), status);
1235 if (U_FAILURE(status)) {
1236 errln(UnicodeString("Failed to get a partial collation key for ") + text1[i]);
1239 len2 = calcKeyIncremental(coll, text2[i], -1, key2, sizeof(key2), status);
1240 if (U_FAILURE(status)) {
1241 errln(UnicodeString("Failed to get a partial collation key for ") + text2[i]);
1245 if (len1 == len2 && uprv_memcmp(key1, key2, len1) == 0) {
1246 errln(UnicodeString("Failed: Identical key\n") + " text1: " + text1[i] + "\n" + " text2: " + text2[i] + "\n" + " key : " + TestUtility::hex(key1, len1));
1248 logln(UnicodeString("Keys produced -\n") + " text1: " + text1[i] + "\n" + " key1 : " + TestUtility::hex(key1, len1) + "\n" + " text2: " + text2[i] + "\n" + " key2 : "
1249 + TestUtility::hex(key2, len2));
1255 void CollationRegressionTest::TestCaseFirstCompression() {
1256 RuleBasedCollator *col = (RuleBasedCollator *) en_us->clone();
1257 UErrorCode status = U_ZERO_ERROR;
1260 caseFirstCompressionSub(col, "default");
1263 col->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status);
1264 if (U_FAILURE(status)) {
1265 errln("Failed to set UCOL_UPPER_FIRST");
1268 caseFirstCompressionSub(col, "upper first");
1271 col->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status);
1272 if (U_FAILURE(status)) {
1273 errln("Failed to set UCOL_LOWER_FIRST");
1276 caseFirstCompressionSub(col, "lower first");
1281 void CollationRegressionTest::caseFirstCompressionSub(Collator *col, UnicodeString opt) {
1282 const int32_t maxLength = 50;
1284 UChar str1[maxLength];
1285 UChar str2[maxLength];
1287 CollationKey key1, key2;
1289 for (int32_t len = 1; len <= maxLength; len++) {
1291 for (; i < len - 1; i++) {
1292 str1[i] = str2[i] = (UChar)0x61; // 'a'
1294 str1[i] = (UChar)0x41; // 'A'
1295 str2[i] = (UChar)0x61; // 'a'
1297 UErrorCode status = U_ZERO_ERROR;
1298 col->getCollationKey(str1, len, key1, status);
1299 col->getCollationKey(str2, len, key2, status);
1301 UCollationResult cmpKey = key1.compareTo(key2, status);
1302 UCollationResult cmpCol = col->compare(str1, len, str2, len, status);
1304 if (U_FAILURE(status)) {
1305 errln("Error in caseFirstCompressionSub");
1306 } else if (cmpKey != cmpCol) {
1307 errln((UnicodeString)"Inconsistent comparison(" + opt
1308 + "): str1=" + UnicodeString(str1, len) + ", str2=" + UnicodeString(str2, len)
1309 + ", cmpKey=" + cmpKey + ", cmpCol=" + cmpCol);
1314 void CollationRegressionTest::TestTrailingComment() {
1315 // ICU ticket #8070:
1316 // Check that the rule parser handles a comment without terminating end-of-line.
1317 IcuTestErrorCode errorCode(*this, "TestTrailingComment");
1318 RuleBasedCollator coll(UNICODE_STRING_SIMPLE("&c<b#comment1\n<a#comment2"), errorCode);
1319 UnicodeString a((UChar)0x61), b((UChar)0x62), c((UChar)0x63);
1320 assertTrue("c<b", coll.compare(c, b) < 0);
1321 assertTrue("b<a", coll.compare(b, a) < 0);
1324 void CollationRegressionTest::TestBeforeWithTooStrongAfter() {
1325 // ICU ticket #9959:
1326 // Forbid rules with a before-reset followed by a stronger relation.
1327 IcuTestErrorCode errorCode(*this, "TestBeforeWithTooStrongAfter");
1328 RuleBasedCollator before2(UNICODE_STRING_SIMPLE("&[before 2]x<<q<p"), errorCode);
1329 if(errorCode.isSuccess()) {
1330 errln("should forbid before-2-reset followed by primary relation");
1334 RuleBasedCollator before3(UNICODE_STRING_SIMPLE("&[before 3]x<<<q<<s<p"), errorCode);
1335 if(errorCode.isSuccess()) {
1336 errln("should forbid before-3-reset followed by primary or secondary relation");
1342 void CollationRegressionTest::compareArray(Collator &c,
1343 const UChar tests[][CollationRegressionTest::MAX_TOKEN_LEN],
1347 Collator::EComparisonResult expectedResult = Collator::EQUAL;
1349 for (i = 0; i < testCount; i += 3)
1351 UnicodeString source(tests[i]);
1352 UnicodeString comparison(tests[i + 1]);
1353 UnicodeString target(tests[i + 2]);
1355 if (comparison == "<")
1357 expectedResult = Collator::LESS;
1359 else if (comparison == ">")
1361 expectedResult = Collator::GREATER;
1363 else if (comparison == "=")
1365 expectedResult = Collator::EQUAL;
1369 UnicodeString bogus1("Bogus comparison string \"");
1370 UnicodeString bogus2("\"");
1371 errln(bogus1 + comparison + bogus2);
1374 Collator::EComparisonResult compareResult = c.compare(source, target);
1376 CollationKey sourceKey, targetKey;
1377 UErrorCode status = U_ZERO_ERROR;
1379 c.getCollationKey(source, sourceKey, status);
1381 if (U_FAILURE(status))
1383 errln("Couldn't get collationKey for source");
1387 c.getCollationKey(target, targetKey, status);
1389 if (U_FAILURE(status))
1391 errln("Couldn't get collationKey for target");
1395 Collator::EComparisonResult keyResult = sourceKey.compareTo(targetKey);
1397 reportCResult( source, target, sourceKey, targetKey, compareResult, keyResult, compareResult, expectedResult );
1402 void CollationRegressionTest::assertEqual(CollationElementIterator &i1, CollationElementIterator &i2)
1404 int32_t c1, c2, count = 0;
1405 UErrorCode status = U_ZERO_ERROR;
1409 c1 = i1.next(status);
1410 c2 = i2.next(status);
1414 UnicodeString msg, msg1(" ");
1416 msg += msg1 + count;
1417 msg += ": strength(0x";
1418 appendHex(c1, 8, msg);
1419 msg += ") != strength(0x";
1420 appendHex(c2, 8, msg);
1429 while (c1 != CollationElementIterator::NULLORDER);
1432 void CollationRegressionTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /* par */)
1436 logln("Collation Regression Tests: ");
1440 dataerrln("Class collator not instantiated");
1444 TESTCASE_AUTO_BEGIN;
1445 TESTCASE_AUTO(Test4048446);
1446 TESTCASE_AUTO(Test4051866);
1447 TESTCASE_AUTO(Test4053636);
1448 TESTCASE_AUTO(Test4054238);
1449 TESTCASE_AUTO(Test4054734);
1450 TESTCASE_AUTO(Test4054736);
1451 TESTCASE_AUTO(Test4058613);
1452 TESTCASE_AUTO(Test4059820);
1453 TESTCASE_AUTO(Test4060154);
1454 TESTCASE_AUTO(Test4062418);
1455 TESTCASE_AUTO(Test4065540);
1456 TESTCASE_AUTO(Test4066189);
1457 TESTCASE_AUTO(Test4066696);
1458 TESTCASE_AUTO(Test4076676);
1459 TESTCASE_AUTO(Test4078588);
1460 TESTCASE_AUTO(Test4079231);
1461 TESTCASE_AUTO(Test4081866);
1462 TESTCASE_AUTO(Test4087241);
1463 TESTCASE_AUTO(Test4087243);
1464 TESTCASE_AUTO(Test4092260);
1465 TESTCASE_AUTO(Test4095316);
1466 TESTCASE_AUTO(Test4101940);
1467 TESTCASE_AUTO(Test4103436);
1468 TESTCASE_AUTO(Test4114076);
1469 TESTCASE_AUTO(Test4114077);
1470 TESTCASE_AUTO(Test4124632);
1471 TESTCASE_AUTO(Test4132736);
1472 TESTCASE_AUTO(Test4133509);
1473 TESTCASE_AUTO(Test4139572);
1474 TESTCASE_AUTO(Test4141640);
1475 TESTCASE_AUTO(Test4146160);
1476 TESTCASE_AUTO(Test4179216);
1477 TESTCASE_AUTO(TestT7189);
1478 TESTCASE_AUTO(TestCaseFirstCompression);
1479 TESTCASE_AUTO(TestTrailingComment);
1480 TESTCASE_AUTO(TestBeforeWithTooStrongAfter);
1484 #endif /* #if !UCONFIG_NO_COLLATION */