Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: source/test/intltest/apicoll.cpp

Issue 845603002: Update ICU to 54.1 step 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/icu.git@master
Patch Set: remove unusued directories Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « source/test/intltest/apicoll.h ('k') | source/test/intltest/astrotst.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /******************************************************************** 1 /********************************************************************
2 * COPYRIGHT: 2 * COPYRIGHT:
3 * Copyright (c) 1997-2013, International Business Machines Corporation and 3 * Copyright (c) 1997-2014, International Business Machines Corporation and
4 * others. All Rights Reserved. 4 * others. All Rights Reserved.
5 ********************************************************************/ 5 ********************************************************************/
6 //============================================================================== = 6 //============================================================================== =
7 // 7 //
8 // File apitest.cpp 8 // File apicoll.cpp
9 // 9 //
10 // 10 //
11 // 11 //
12 // Created by: Helena Shih 12 // Created by: Helena Shih
13 // 13 //
14 // Modification History: 14 // Modification History:
15 // 15 //
16 // Date Name Description 16 // Date Name Description
17 // 2/5/97 aliu Added streamIn and streamOut methods. Added 17 // 2/5/97 aliu Added streamIn and streamOut methods. Added
18 // constructor which reads RuleBasedCollator object fro m 18 // constructor which reads RuleBasedCollator object fro m
(...skipping 20 matching lines...) Expand all
39 #include "apicoll.h" 39 #include "apicoll.h"
40 #include "unicode/chariter.h" 40 #include "unicode/chariter.h"
41 #include "unicode/schriter.h" 41 #include "unicode/schriter.h"
42 #include "unicode/ustring.h" 42 #include "unicode/ustring.h"
43 #include "unicode/ucol.h" 43 #include "unicode/ucol.h"
44 44
45 #include "sfwdchit.h" 45 #include "sfwdchit.h"
46 #include "cmemory.h" 46 #include "cmemory.h"
47 #include <stdlib.h> 47 #include <stdlib.h>
48 48
49 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
50
51 void 49 void
52 CollationAPITest::doAssert(UBool condition, const char *message) 50 CollationAPITest::doAssert(UBool condition, const char *message)
53 { 51 {
54 if (!condition) { 52 if (!condition) {
55 errln(UnicodeString("ERROR : ") + message); 53 errln(UnicodeString("ERROR : ") + message);
56 } 54 }
57 } 55 }
58 56
59 #ifdef U_USE_COLLATION_OBSOLETE_2_6
60 /*
61 * Test Collator::createInstance(... version...) for some locale. Called by Test Property().
62 */
63 static void
64 TestOpenVersion(IntlTest &test, const Locale &locale) {
65 UVersionInfo version1, version2;
66 Collator *collator1, *collator2;
67 UErrorCode errorCode;
68
69 errorCode=U_ZERO_ERROR;
70 collator1=Collator::createInstance(locale, errorCode);
71 if(U_SUCCESS(errorCode)) {
72 /* get the current version */
73 collator1->getVersion(version1);
74 delete collator1;
75
76 /* try to get that same version again */
77 collator2=Collator::createInstance(locale, version1, errorCode);
78 if(U_SUCCESS(errorCode)) {
79 collator2->getVersion(version2);
80 if(0!=uprv_memcmp(version1, version2, sizeof(UVersionInfo))) {
81 test.errln("error: Collator::createInstance(\"%s\", (%s collator )->getVersion()) returns a different collator\n", locale.getName(), locale.getNa me());
82 }
83 delete collator2;
84 } else {
85 test.errln("error: Collator::createInstance(\"%s\", (%s collator)->g etVersion()) fails: %s\n", locale.getName(), locale.getName(), u_errorName(error Code));
86 }
87 }
88 }
89 #endif
90
91 // Collator Class Properties 57 // Collator Class Properties
92 // ctor, dtor, createInstance, compare, getStrength/setStrength 58 // ctor, dtor, createInstance, compare, getStrength/setStrength
93 // getDecomposition/setDecomposition, getDisplayName 59 // getDecomposition/setDecomposition, getDisplayName
94 void 60 void
95 CollationAPITest::TestProperty(/* char* par */) 61 CollationAPITest::TestProperty(/* char* par */)
96 { 62 {
97 UErrorCode success = U_ZERO_ERROR; 63 UErrorCode success = U_ZERO_ERROR;
98 Collator *col = 0; 64 Collator *col = 0;
99 /* 65 /*
100 * Expected version of the English collator. 66 * Expected version of the English collator.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 #if 0 155 #if 0
190 // weiv : this test is bogus if we're running on any machine that has differ ent default locale than English. 156 // weiv : this test is bogus if we're running on any machine that has differ ent default locale than English.
191 // Therefore, it is banned! 157 // Therefore, it is banned!
192 logln("Get display name for the US English in default locale language : "); 158 logln("Get display name for the US English in default locale language : ");
193 logln(Collator::getDisplayName(Locale::US, name)); 159 logln(Collator::getDisplayName(Locale::US, name));
194 doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine"); 160 doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine");
195 #endif 161 #endif
196 delete col; col = 0; 162 delete col; col = 0;
197 RuleBasedCollator *rcol = (RuleBasedCollator *)Collator::createInstance("da_ DK", 163 RuleBasedCollator *rcol = (RuleBasedCollator *)Collator::createInstance("da_ DK",
198 succ ess); 164 succ ess);
199 doAssert(rcol->getRules().length() != 0, "da_DK rules does not have length 0 "); 165 if (U_FAILURE(success)) {
166 errcheckln(success, "Collator::createInstance(\"da_DK\") failed - %s", u _errorName(success));
167 return;
168 }
169 const UnicodeString &daRules = rcol->getRules();
170 if(daRules.isEmpty()) {
171 dataerrln("missing da_DK tailoring rule string");
172 } else {
173 doAssert(daRules.indexOf("aa") >= 0, "da_DK rules do not contain 'aa'");
174 }
200 delete rcol; 175 delete rcol;
201 176
202 col = Collator::createInstance(Locale::getFrench(), success); 177 col = Collator::createInstance(Locale::getFrench(), success);
203 if (U_FAILURE(success)) 178 if (U_FAILURE(success))
204 { 179 {
205 errln("Creating French collation failed."); 180 errln("Creating French collation failed.");
206 return; 181 return;
207 } 182 }
208 183
209 col->setStrength(Collator::PRIMARY); 184 col->setStrength(Collator::PRIMARY);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 doAssert((*frCol != *junk), "The junk is the same as the fr_CA collator. "); 231 doAssert((*frCol != *junk), "The junk is the same as the fr_CA collator. ");
257 } 232 }
258 Collator *aFrCol = frCol->clone(); 233 Collator *aFrCol = frCol->clone();
259 doAssert((*frCol == *aFrCol), "The cloning of a fr_CA collator failed."); 234 doAssert((*frCol == *aFrCol), "The cloning of a fr_CA collator failed.");
260 logln("Collator property test ended."); 235 logln("Collator property test ended.");
261 236
262 delete col; 237 delete col;
263 delete frCol; 238 delete frCol;
264 delete aFrCol; 239 delete aFrCol;
265 delete junk; 240 delete junk;
266
267 #ifdef U_USE_COLLATION_OBSOLETE_2_6
268 /* test Collator::createInstance(...version...) */
269 TestOpenVersion(*this, "");
270 TestOpenVersion(*this, "da");
271 TestOpenVersion(*this, "fr");
272 TestOpenVersion(*this, "ja");
273
274 /* try some bogus version */
275 versionArray[0]=0;
276 versionArray[1]=0x99;
277 versionArray[2]=0xc7;
278 versionArray[3]=0xfe;
279 col=Collator::createInstance(Locale(), versionArray, success);
280 if(U_SUCCESS(success)) {
281 errln("error: ucol_openVersion(bogus version) succeeded");
282 delete col;
283 }
284 #endif
285 } 241 }
286 242
287 void 243 void
288 CollationAPITest::TestRuleBasedColl() 244 CollationAPITest::TestRuleBasedColl()
289 { 245 {
290 RuleBasedCollator *col1, *col2, *col3, *col4; 246 RuleBasedCollator *col1, *col2, *col3, *col4;
291 UErrorCode status = U_ZERO_ERROR; 247 UErrorCode status = U_ZERO_ERROR;
292 248
293 UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E "); 249 UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E ");
294 UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E"); 250 UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 errcheckln(success, "Default collation creation failed. - %s", u_errorNa me(success)); 499 errcheckln(success, "Default collation creation failed. - %s", u_errorNa me(success));
544 return; 500 return;
545 } 501 }
546 col->setStrength(Collator::TERTIARY); 502 col->setStrength(Collator::TERTIARY);
547 503
548 CollationKey sortk1, sortk2; 504 CollationKey sortk1, sortk2;
549 UnicodeString test1("Abcda"), test2("abcda"); 505 UnicodeString test1("Abcda"), test2("abcda");
550 UErrorCode key1Status = U_ZERO_ERROR, key2Status = U_ZERO_ERROR; 506 UErrorCode key1Status = U_ZERO_ERROR, key2Status = U_ZERO_ERROR;
551 507
552 logln("Testing weird arguments"); 508 logln("Testing weird arguments");
553 col->getCollationKey(NULL, 0, sortk1, key1Status); 509 // No string vs. empty string vs. completely-ignorable string:
510 // See ICU ticket #10495.
511 CollationKey sortkNone;
512 int32_t length;
513 sortkNone.getByteArray(length);
514 doAssert(!sortkNone.isBogus() && length == 0,
515 "Default-constructed collation key should be empty");
516 CollationKey sortkEmpty;
517 col->getCollationKey(NULL, 0, sortkEmpty, key1Status);
554 // key gets reset here 518 // key gets reset here
555 int32_t length; 519 const uint8_t* byteArrayEmpty = sortkEmpty.getByteArray(length);
556 sortk1.getByteArray(length); 520 doAssert(sortkEmpty.isBogus() == FALSE && length == 3 &&
557 doAssert(sortk1.isBogus() == FALSE && length == 0, 521 byteArrayEmpty[0] == 1 && byteArrayEmpty[1] == 1 && byteArrayEmpty[ 2] == 0,
558 "Empty string should return an empty collation key"); 522 "Empty string should return a collation key with empty levels");
523 doAssert(sortkNone.compareTo(sortkEmpty) == Collator::LESS,
524 "Expected no collation key < collation key for empty string");
525 doAssert(sortkEmpty.compareTo(sortkNone) == Collator::GREATER,
526 "Expected collation key for empty string > no collation key");
527
528 CollationKey sortkIgnorable;
529 // Most control codes and CGJ are completely ignorable.
530 // A string with only completely ignorables must compare equal to an empty s tring.
531 col->getCollationKey(UnicodeString((UChar)1).append((UChar)0x34f), sortkIgno rable, key1Status);
532 sortkIgnorable.getByteArray(length);
533 doAssert(!sortkIgnorable.isBogus() && length == 3,
534 "Completely ignorable string should return a collation key with emp ty levels");
535 doAssert(sortkIgnorable.compareTo(sortkEmpty) == Collator::EQUAL,
536 "Completely ignorable string should compare equal to empty string") ;
537
559 // bogus key returned here 538 // bogus key returned here
560 key1Status = U_ILLEGAL_ARGUMENT_ERROR; 539 key1Status = U_ILLEGAL_ARGUMENT_ERROR;
561 col->getCollationKey(NULL, 0, sortk1, key1Status); 540 col->getCollationKey(NULL, 0, sortk1, key1Status);
562 doAssert(sortk1.isBogus() && (sortk1.getByteArray(length), length) == 0, 541 doAssert(sortk1.isBogus() && (sortk1.getByteArray(length), length) == 0,
563 "Error code should return bogus collation key"); 542 "Error code should return bogus collation key");
564 543
565 key1Status = U_ZERO_ERROR; 544 key1Status = U_ZERO_ERROR;
566 logln("Use tertiary comparison level testing ...."); 545 logln("Use tertiary comparison level testing ....");
567 546
568 col->getCollationKey(test1, sortk1, key1Status); 547 col->getCollationKey(test1, sortk1, key1Status);
548 if (U_FAILURE(key1Status)) {
549 errln("getCollationKey(Abcda) failed - %s", u_errorName(key1Status));
550 return;
551 }
569 doAssert((sortk1.compareTo(col->getCollationKey(test2, sortk2, key2Status))) 552 doAssert((sortk1.compareTo(col->getCollationKey(test2, sortk2, key2Status)))
570 == Collator::GREATER, 553 == Collator::GREATER,
571 "Result should be \"Abcda\" >>> \"abcda\""); 554 "Result should be \"Abcda\" >>> \"abcda\"");
572 555
573 CollationKey sortk3(sortk2), sortkNew, sortkEmpty; 556 CollationKey sortk3(sortk2), sortkNew;
574
575 557
576 sortkNew = sortk1; 558 sortkNew = sortk1;
577 doAssert((sortk1 != sortk2), "The sort keys should be different"); 559 doAssert((sortk1 != sortk2), "The sort keys should be different");
578 doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() fail ed"); 560 doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() fail ed");
579 doAssert((sortk2 == sortk3), "The sort keys should be the same"); 561 doAssert((sortk2 == sortk3), "The sort keys should be the same");
580 doAssert((sortk1 == sortkNew), "The sort keys assignment failed"); 562 doAssert((sortk1 == sortkNew), "The sort keys assignment failed");
581 doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() fa iled"); 563 doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() fa iled");
582 doAssert((sortkNew != sortk3), "The sort keys should be different"); 564 doAssert((sortkNew != sortk3), "The sort keys should be different");
583 doAssert(sortk1.compareTo(sortk3) == Collator::GREATER, "Result should be \" Abcda\" >>> \"abcda\""); 565 doAssert(sortk1.compareTo(sortk3) == Collator::GREATER, "Result should be \" Abcda\" >>> \"abcda\"");
584 doAssert(sortk2.compareTo(sortk3) == Collator::EQUAL, "Result should be \"ab cda\" == \"abcda\""); 566 doAssert(sortk2.compareTo(sortk3) == Collator::EQUAL, "Result should be \"ab cda\" == \"abcda\"");
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 813
832 814
833 logln("testing CollationElementIterator ends..."); 815 logln("testing CollationElementIterator ends...");
834 } 816 }
835 817
836 // Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and g etRules 818 // Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and g etRules
837 void 819 void
838 CollationAPITest::TestOperators(/* char* par */) 820 CollationAPITest::TestOperators(/* char* par */)
839 { 821 {
840 UErrorCode success = U_ZERO_ERROR; 822 UErrorCode success = U_ZERO_ERROR;
841 UnicodeString ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E"); 823 UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E ");
842 UnicodeString ruleset2("< a, A < b, B < c, C < d, D, e, E"); 824 UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
843 RuleBasedCollator *col1 = new RuleBasedCollator(ruleset1, success); 825 RuleBasedCollator *col1 = new RuleBasedCollator(ruleset1, success);
844 if (U_FAILURE(success)) { 826 if (U_FAILURE(success)) {
845 errcheckln(success, "RuleBasedCollator creation failed. - %s", u_errorNa me(success)); 827 errcheckln(success, "RuleBasedCollator creation failed. - %s", u_errorNa me(success));
846 return; 828 return;
847 } 829 }
848 success = U_ZERO_ERROR; 830 success = U_ZERO_ERROR;
849 RuleBasedCollator *col2 = new RuleBasedCollator(ruleset2, success); 831 RuleBasedCollator *col2 = new RuleBasedCollator(ruleset2, success);
850 if (U_FAILURE(success)) { 832 if (U_FAILURE(success)) {
851 errln("The RuleBasedCollator constructor failed when building with the 2 nd rule set."); 833 errln("The RuleBasedCollator constructor failed when building with the 2 nd rule set.");
852 return; 834 return;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
918 CollationAPITest::TestDuplicate(/* char* par */) 900 CollationAPITest::TestDuplicate(/* char* par */)
919 { 901 {
920 UErrorCode status = U_ZERO_ERROR; 902 UErrorCode status = U_ZERO_ERROR;
921 Collator *col1 = Collator::createInstance(Locale::getEnglish(), status); 903 Collator *col1 = Collator::createInstance(Locale::getEnglish(), status);
922 if (U_FAILURE(status)) { 904 if (U_FAILURE(status)) {
923 logln("Default collator creation failed."); 905 logln("Default collator creation failed.");
924 return; 906 return;
925 } 907 }
926 Collator *col2 = col1->clone(); 908 Collator *col2 = col1->clone();
927 doAssert((*col1 == *col2), "Cloned object is not equal to the orginal"); 909 doAssert((*col1 == *col2), "Cloned object is not equal to the orginal");
928 UnicodeString *ruleset = new UnicodeString("< a, A < b, B < c, C < d, D, e, E"); 910 UnicodeString ruleset("&9 < a, A < b, B < c, C < d, D, e, E");
929 RuleBasedCollator *col3 = new RuleBasedCollator(*ruleset, status); 911 RuleBasedCollator *col3 = new RuleBasedCollator(ruleset, status);
912 if (U_FAILURE(status)) {
913 logln("Collation tailoring failed.");
914 return;
915 }
930 doAssert((*col1 != *col3), "Cloned object is equal to some dummy"); 916 doAssert((*col1 != *col3), "Cloned object is equal to some dummy");
931 *col3 = *((RuleBasedCollator*)col1); 917 *col3 = *((RuleBasedCollator*)col1);
932 doAssert((*col1 == *col3), "Copied object is not equal to the orginal"); 918 doAssert((*col1 == *col3), "Copied object is not equal to the orginal");
933 919
934 if (U_FAILURE(status)) {
935 logln("Collation tailoring failed.");
936 return;
937 }
938
939 UCollationResult res; 920 UCollationResult res;
940 UnicodeString first((UChar)0x0061); 921 UnicodeString first((UChar)0x0061);
941 UnicodeString second((UChar)0x0062); 922 UnicodeString second((UChar)0x0062);
942 UnicodeString copiedEnglishRules(((RuleBasedCollator*)col1)->getRules()); 923 UnicodeString copiedEnglishRules(((RuleBasedCollator*)col1)->getRules());
943 924
944 delete col1; 925 delete col1;
945 delete ruleset;
946 926
947 // Try using the cloned collators after deleting the original data 927 // Try using the cloned collators after deleting the original data
948 res = col2->compare(first, second, status); 928 res = col2->compare(first, second, status);
949 if(res != UCOL_LESS) { 929 if(res != UCOL_LESS) {
950 errln("a should be less then b after tailoring"); 930 errln("a should be less then b after tailoring");
951 } 931 }
952 if (((RuleBasedCollator*)col2)->getRules() != copiedEnglishRules) { 932 if (((RuleBasedCollator*)col2)->getRules() != copiedEnglishRules) {
953 errln(UnicodeString("English rule difference. ") 933 errln(UnicodeString("English rule difference. ")
954 + copiedEnglishRules + UnicodeString("\ngetRules=") + ((RuleBasedCol lator*)col2)->getRules()); 934 + copiedEnglishRules + UnicodeString("\ngetRules=") + ((RuleBasedCol lator*)col2)->getRules());
955 } 935 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1021 1001
1022 1002
1023 1003
1024 logln("The compare tests end."); 1004 logln("The compare tests end.");
1025 delete col; 1005 delete col;
1026 } 1006 }
1027 1007
1028 void 1008 void
1029 CollationAPITest::TestGetAll(/* char* par */) 1009 CollationAPITest::TestGetAll(/* char* par */)
1030 { 1010 {
1011 if (logKnownIssue("10774","Side effects from utility/LocaleTest/TestGetLocal e")) {
1012 return;
1013 }
1031 int32_t count1, count2; 1014 int32_t count1, count2;
1032 UErrorCode status = U_ZERO_ERROR; 1015 UErrorCode status = U_ZERO_ERROR;
1033 1016
1034 logln("Trying Collator::getAvailableLocales(int&)"); 1017 logln("Trying Collator::getAvailableLocales(int&)");
1035 1018
1036 const Locale* list = Collator::getAvailableLocales(count1); 1019 const Locale* list = Collator::getAvailableLocales(count1);
1037 for (int32_t i = 0; i < count1; ++i) { 1020 for (int32_t i = 0; i < count1; ++i) {
1038 UnicodeString dispName; 1021 UnicodeString dispName;
1039 logln(UnicodeString("Locale name: ") 1022 logln(UnicodeString("Locale name: ")
1040 + UnicodeString(list[i].getName()) 1023 + UnicodeString(list[i].getName())
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
1252 LocalPointer<Collator> col(Collator::createInstance(Locale::getEnglish(), er rorCode)); 1235 LocalPointer<Collator> col(Collator::createInstance(Locale::getEnglish(), er rorCode));
1253 if (errorCode.logDataIfFailureAndReset("Collator::createInstance(English) fa iled")) { 1236 if (errorCode.logDataIfFailureAndReset("Collator::createInstance(English) fa iled")) {
1254 return; 1237 return;
1255 } 1238 }
1256 col->setAttribute(UCOL_STRENGTH, UCOL_PRIMARY, errorCode); 1239 col->setAttribute(UCOL_STRENGTH, UCOL_PRIMARY, errorCode);
1257 UChar i_and_phi[] = { 0x438, 0x3c6 }; // Cyrillic small i & Greek small phi . 1240 UChar i_and_phi[] = { 0x438, 0x3c6 }; // Cyrillic small i & Greek small phi .
1258 // The sort key should be 6 bytes: 1241 // The sort key should be 6 bytes:
1259 // 2 bytes for the Cyrillic i, 1 byte for the primary-compression terminator , 1242 // 2 bytes for the Cyrillic i, 1 byte for the primary-compression terminator ,
1260 // 2 bytes for the Greek phi, and 1 byte for the NUL terminator. 1243 // 2 bytes for the Greek phi, and 1 byte for the NUL terminator.
1261 uint8_t sortKey[12]; 1244 uint8_t sortKey[12];
1262 int32_t length = col->getSortKey(i_and_phi, 2, sortKey, LENGTHOF(sortKey)); 1245 int32_t length = col->getSortKey(i_and_phi, 2, sortKey, UPRV_LENGTHOF(sortKe y));
1263 uint8_t sortKey2[12]; 1246 uint8_t sortKey2[12];
1264 for (int32_t capacity = 0; capacity < length; ++capacity) { 1247 for (int32_t capacity = 0; capacity < length; ++capacity) {
1265 uprv_memset(sortKey2, 2, LENGTHOF(sortKey2)); 1248 uprv_memset(sortKey2, 2, UPRV_LENGTHOF(sortKey2));
1266 int32_t length2 = col->getSortKey(i_and_phi, 2, sortKey2, capacity); 1249 int32_t length2 = col->getSortKey(i_and_phi, 2, sortKey2, capacity);
1267 if (length2 != length || 0 != uprv_memcmp(sortKey, sortKey2, capacity)) { 1250 if (length2 != length || 0 != uprv_memcmp(sortKey, sortKey2, capacity)) {
1268 errln("getSortKey(i_and_phi, capacity=%d) failed to write proper pre fix", capacity); 1251 errln("getSortKey(i_and_phi, capacity=%d) failed to write proper pre fix", capacity);
1269 } else if (sortKey2[capacity] != 2 || sortKey2[capacity + 1] != 2) { 1252 } else if (sortKey2[capacity] != 2 || sortKey2[capacity + 1] != 2) {
1270 errln("getSortKey(i_and_phi, capacity=%d) wrote beyond capacity", ca pacity); 1253 errln("getSortKey(i_and_phi, capacity=%d) wrote beyond capacity", ca pacity);
1271 } 1254 }
1272 } 1255 }
1273 1256
1274 // Now try to break getCollationKey(). 1257 // Now try to break getCollationKey().
1275 // Internally, it always starts with a large stack buffer. 1258 // Internally, it always starts with a large stack buffer.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 ch ++; 1305 ch ++;
1323 1306
1324 str.setCharAt(0, ch); 1307 str.setCharAt(0, ch);
1325 iter->setText(str, status); 1308 iter->setText(str, status);
1326 order = iter->previous(status); 1309 order = iter->previous(status);
1327 1310
1328 /* thai management */ 1311 /* thai management */
1329 if (order == 0) 1312 if (order == 0)
1330 order = iter->previous(status); 1313 order = iter->previous(status);
1331 1314
1332 while (U_SUCCESS(status) && iter->previous(status) != UCOL_NULLORDER) { 1315 while (U_SUCCESS(status) && iter->previous(status) != CollationElementIt erator::NULLORDER) {
1333 count ++; 1316 count ++;
1334 } 1317 }
1335 1318
1336 size = coll.getMaxExpansion(order); 1319 size = coll.getMaxExpansion(order);
1337 if (U_FAILURE(status) || size < count) { 1320 if (U_FAILURE(status) || size < count) {
1338 errln("Failure at codepoint U+%04X, maximum expansion count %d < %d" , 1321 errln("Failure at codepoint U+%04X, maximum expansion count %d < %d" ,
1339 ch, size, count); 1322 ch, size, count);
1340 } 1323 }
1341 } 1324 }
1342 1325
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1538 } 1521 }
1539 1522
1540 delete coll; 1523 delete coll;
1541 } 1524 }
1542 1525
1543 void CollationAPITest::TestVariableTopSetting() { 1526 void CollationAPITest::TestVariableTopSetting() {
1544 UErrorCode status = U_ZERO_ERROR; 1527 UErrorCode status = U_ZERO_ERROR;
1545 1528
1546 UChar vt[256] = { 0 }; 1529 UChar vt[256] = { 0 };
1547 1530
1548 Collator *coll = Collator::createInstance(status); 1531 // Use the root collator, not the default collator.
1532 // This test fails with en_US_POSIX which tailors the dollar sign after 'A'.
1533 Collator *coll = Collator::createInstance(Locale::getRoot(), status);
1549 if(U_FAILURE(status)) { 1534 if(U_FAILURE(status)) {
1550 delete coll; 1535 delete coll;
1551 errcheckln(status, "Collator creation failed with error %s", u_errorName(sta tus)); 1536 errcheckln(status, "Collator creation failed with error %s", u_errorName(sta tus));
1552 return; 1537 return;
1553 } 1538 }
1554 1539
1555 uint32_t oldVarTop = coll->getVariableTop(status); 1540 uint32_t oldVarTop = coll->getVariableTop(status);
1556 1541
1542 // ICU 53+: The character must be in a supported reordering group,
1543 // and the variable top is pinned to the end of that group.
1557 vt[0] = 0x0041; 1544 vt[0] = 0x0041;
1558 1545
1546 (void)coll->setVariableTop(vt, 1, status);
1547 if(status != U_ILLEGAL_ARGUMENT_ERROR) {
1548 errln("setVariableTop(letter) did not detect illegal argument - %s", u_error Name(status));
1549 }
1550
1551 status = U_ZERO_ERROR;
1552 vt[0] = 0x24; // dollar sign (currency symbol)
1559 uint32_t newVarTop = coll->setVariableTop(vt, 1, status); 1553 uint32_t newVarTop = coll->setVariableTop(vt, 1, status);
1560 1554
1561 if((newVarTop & 0xFFFF0000) != (coll->getVariableTop(status) & 0xFFFF0000)) { 1555 if(newVarTop != coll->getVariableTop(status)) {
1562 errln("Didn't set vartop properly\n"); 1556 errln("setVariableTop(dollar sign) != following getVariableTop()");
1563 } 1557 }
1564 1558
1559 UnicodeString dollar((UChar)0x24);
1560 UnicodeString euro((UChar)0x20AC);
1561 uint32_t newVarTop2 = coll->setVariableTop(euro, status);
1562 assertEquals("setVariableTop(Euro sign) == following getVariableTop()",
1563 (int64_t)newVarTop2, (int64_t)coll->getVariableTop(status));
1564 assertEquals("setVariableTop(Euro sign) == setVariableTop(dollar sign) (should pin to top of currency group)",
1565 (int64_t)newVarTop2, (int64_t)newVarTop);
1566
1567 coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status);
1568 assertEquals("empty==dollar", UCOL_EQUAL, coll->compare(UnicodeString(), dolla r));
1569 assertEquals("empty==euro", UCOL_EQUAL, coll->compare(UnicodeString(), euro));
1570 assertEquals("dollar<zero", UCOL_LESS, coll->compare(dollar, UnicodeString((UC har)0x30)));
1571
1565 coll->setVariableTop(oldVarTop, status); 1572 coll->setVariableTop(oldVarTop, status);
1566 1573
1567 uint32_t newerVarTop = coll->setVariableTop(UnicodeString(vt, 1), status); 1574 uint32_t newerVarTop = coll->setVariableTop(UnicodeString(vt, 1), status);
1568 1575
1569 if((newVarTop & 0xFFFF0000) != (newerVarTop & 0xFFFF0000)) { 1576 if(newVarTop != newerVarTop) {
1570 errln("Didn't set vartop properly from UnicodeString!\n"); 1577 errln("Didn't set vartop properly from UnicodeString!\n");
1571 } 1578 }
1572 1579
1573 delete coll; 1580 delete coll;
1574 1581
1575 } 1582 }
1576 1583
1584 void CollationAPITest::TestMaxVariable() {
1585 UErrorCode errorCode = U_ZERO_ERROR;
1586 LocalPointer<Collator> coll(Collator::createInstance(Locale::getRoot(), errorC ode));
1587 if(U_FAILURE(errorCode)) {
1588 errcheckln(errorCode, "Collator creation failed with error %s", u_errorName( errorCode));
1589 return;
1590 }
1591
1592 (void)coll->setMaxVariable(UCOL_REORDER_CODE_OTHERS, errorCode);
1593 if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
1594 errln("setMaxVariable(others) did not detect illegal argument - %s", u_error Name(errorCode));
1595 }
1596
1597 errorCode = U_ZERO_ERROR;
1598 (void)coll->setMaxVariable(UCOL_REORDER_CODE_CURRENCY, errorCode);
1599
1600 if(UCOL_REORDER_CODE_CURRENCY != coll->getMaxVariable()) {
1601 errln("setMaxVariable(currency) != following getMaxVariable()");
1602 }
1603
1604 coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, errorCode);
1605 assertEquals("empty==dollar", UCOL_EQUAL, coll->compare(UnicodeString(), Unico deString((UChar)0x24)));
1606 assertEquals("empty==euro", UCOL_EQUAL, coll->compare(UnicodeString(), Unicode String((UChar)0x20AC)));
1607 assertEquals("dollar<zero", UCOL_LESS, coll->compare(UnicodeString((UChar)0x24 ), UnicodeString((UChar)0x30)));
1608 }
1609
1577 void CollationAPITest::TestGetLocale() { 1610 void CollationAPITest::TestGetLocale() {
1578 UErrorCode status = U_ZERO_ERROR; 1611 UErrorCode status = U_ZERO_ERROR;
1579 const char *rules = "&a<x<y<z"; 1612 const char *rules = "&a<x<y<z";
1580 UChar rlz[256] = {0}; 1613 UChar rlz[256] = {0};
1581 1614
1582 Collator *coll = NULL; 1615 Collator *coll = Collator::createInstance("root", status);
1583 Locale locale; 1616 if(U_FAILURE(status)) {
1617 dataerrln("Failed to open collator for \"root\" with %s", u_errorName(status ));
1618 return;
1619 }
1620 Locale locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
1621 if(locale != Locale::getRoot()) {
1622 errln("Collator::createInstance(\"root\").getLocale(actual) != Locale::getRo ot(); "
1623 "getLocale().getName() = \"%s\"",
1624 locale.getName());
1625 }
1626 delete coll;
1627
1628 coll = Collator::createInstance("", status);
1629 if(U_FAILURE(status)) {
1630 dataerrln("Failed to open collator for \"\" with %s", u_errorName(status));
1631 return;
1632 }
1633 locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
1634 if(locale != Locale::getRoot()) {
1635 errln("Collator::createInstance(\"\").getLocale(actual) != Locale::getRoot() ; "
1636 "getLocale().getName() = \"%s\"",
1637 locale.getName());
1638 }
1639 delete coll;
1584 1640
1585 int32_t i = 0; 1641 int32_t i = 0;
1586 1642
1587 static const struct { 1643 static const struct {
1588 const char* requestedLocale; 1644 const char* requestedLocale;
1589 const char* validLocale; 1645 const char* validLocale;
1590 const char* actualLocale; 1646 const char* actualLocale;
1591 } testStruct[] = { 1647 } testStruct[] = {
1592 { "sr_YU", "sr_YU", "root" }, 1648 // Note: Locale::getRoot().getName() == "" not "root".
1593 { "sh_YU", "sh_YU", "sh" }, 1649 { "de_DE", "de", "" },
1594 { "en_US_CALIFORNIA", "en_US", "root" }, 1650 { "sr_RS", "sr_Cyrl_RS", "sr" },
1595 { "fr_FR_NONEXISTANT", "fr_FR", "fr" } 1651 { "en_US_CALIFORNIA", "en_US", "" },
1652 { "fr_FR_NONEXISTANT", "fr", "" },
1653 // pinyin is the default, therefore suppressed.
1654 { "zh_CN", "zh_Hans_CN", "zh" },
1655 // zh_Hant has default=stroke but the data is in zh.
1656 { "zh_TW", "zh_Hant_TW", "zh@collation=stroke" },
1657 { "zh_TW@collation=pinyin", "zh_Hant_TW@collation=pinyin", "zh" },
1658 { "zh_CN@collation=stroke", "zh_Hans_CN@collation=stroke", "zh@collation=str oke" }
1596 }; 1659 };
1597 1660
1598 u_unescape(rules, rlz, 256); 1661 u_unescape(rules, rlz, 256);
1599 1662
1600 /* test opening collators for different locales */ 1663 /* test opening collators for different locales */
1601 for(i = 0; i<(int32_t)(sizeof(testStruct)/sizeof(testStruct[0])); i++) { 1664 for(i = 0; i<(int32_t)UPRV_LENGTHOF(testStruct); i++) {
1602 status = U_ZERO_ERROR; 1665 status = U_ZERO_ERROR;
1603 coll = Collator::createInstance(testStruct[i].requestedLocale, status); 1666 coll = Collator::createInstance(testStruct[i].requestedLocale, status);
1604 if(U_FAILURE(status)) { 1667 if(U_FAILURE(status)) {
1605 log("Failed to open collator for %s with %s\n", testStruct[i].requestedLoc ale, u_errorName(status)); 1668 errln("Failed to open collator for %s with %s", testStruct[i].requestedLoc ale, u_errorName(status));
1606 delete coll; 1669 delete coll;
1607 continue; 1670 continue;
1608 } 1671 }
1672 // The requested locale may be the same as the valid locale,
1673 // or may not be supported at all. See ticket #10477.
1609 locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status); 1674 locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status);
1610 if(locale != testStruct[i].requestedLocale) { 1675 if(U_SUCCESS(status) &&
1611 log("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStr uct[i].requestedLocale, testStruct[i].requestedLocale, locale.getName()); 1676 locale != testStruct[i].requestedLocale && locale != testStruct[i].valid Locale) {
1677 errln("[Coll %s]: Error in requested locale, expected %s or %s, got %s",
1678 testStruct[i].requestedLocale,
1679 testStruct[i].requestedLocale, testStruct[i].validLocale, locale.get Name());
1612 } 1680 }
1681 status = U_ZERO_ERROR;
1613 locale = coll->getLocale(ULOC_VALID_LOCALE, status); 1682 locale = coll->getLocale(ULOC_VALID_LOCALE, status);
1614 if(locale != testStruct[i].validLocale) { 1683 if(locale != testStruct[i].validLocale) {
1615 log("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct[ i].requestedLocale, testStruct[i].validLocale, locale.getName()); 1684 errln("[Coll %s]: Error in valid locale, expected %s, got %s",
1685 testStruct[i].requestedLocale, testStruct[i].validLocale, locale.get Name());
1616 } 1686 }
1617 locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status); 1687 locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
1618 if(locale != testStruct[i].actualLocale) { 1688 if(locale != testStruct[i].actualLocale) {
1619 log("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct [i].requestedLocale, testStruct[i].actualLocale, locale.getName()); 1689 errln("[Coll %s]: Error in actual locale, expected %s, got %s",
1690 testStruct[i].requestedLocale, testStruct[i].actualLocale, locale.ge tName());
1691 }
1692 // If we open a collator for the actual locale, we should get an equivalent one again.
1693 LocalPointer<Collator> coll2(Collator::createInstance(locale, status));
1694 if(U_FAILURE(status)) {
1695 errln("Failed to open collator for actual locale \"%s\" with %s",
1696 locale.getName(), u_errorName(status));
1697 } else {
1698 Locale actual2 = coll2->getLocale(ULOC_ACTUAL_LOCALE, status);
1699 if(actual2 != locale) {
1700 errln("[Coll actual \"%s\"]: Error in actual locale, got different one: \"%s\"",
1701 locale.getName(), actual2.getName());
1702 }
1703 if(*coll2 != *coll) {
1704 errln("[Coll actual \"%s\"]: Got different collator than before", locale .getName());
1705 }
1620 } 1706 }
1621 delete coll; 1707 delete coll;
1622 } 1708 }
1623 1709
1624 /* completely non-existant locale for collator should get a default collator * / 1710 /* completely non-existant locale for collator should get a default collator * /
1625 { 1711 {
1626 Collator *defaultColl = Collator::createInstance((const Locale)NULL, status) ; 1712 Collator *defaultColl = Collator::createInstance((const Locale)NULL, status) ;
1627 coll = Collator::createInstance("blahaha", status); 1713 coll = Collator::createInstance("blahaha", status);
1628 if(U_FAILURE(status)) { 1714 if(U_FAILURE(status)) {
1629 log("Failed to open collator with %s\n", u_errorName(status)); 1715 errln("Failed to open collator with %s", u_errorName(status));
1630 delete coll; 1716 delete coll;
1631 delete defaultColl; 1717 delete defaultColl;
1632 return; 1718 return;
1633 } 1719 }
1634 if(coll->getLocale(ULOC_REQUESTED_LOCALE, status) != "blahaha") {
1635 log("Nonexisting locale didn't preserve the requested locale\n");
1636 }
1637 if(coll->getLocale(ULOC_VALID_LOCALE, status) != 1720 if(coll->getLocale(ULOC_VALID_LOCALE, status) !=
1638 defaultColl->getLocale(ULOC_VALID_LOCALE, status)) { 1721 defaultColl->getLocale(ULOC_VALID_LOCALE, status)) {
1639 log("Valid locale for nonexisting locale locale collator differs " 1722 errln("Valid locale for nonexisting locale locale collator differs "
1640 "from valid locale for default collator\n"); 1723 "from valid locale for default collator");
1641 } 1724 }
1642 if(coll->getLocale(ULOC_ACTUAL_LOCALE, status) != 1725 if(coll->getLocale(ULOC_ACTUAL_LOCALE, status) !=
1643 defaultColl->getLocale(ULOC_ACTUAL_LOCALE, status)) { 1726 defaultColl->getLocale(ULOC_ACTUAL_LOCALE, status)) {
1644 log("Actual locale for nonexisting locale locale collator differs " 1727 errln("Actual locale for nonexisting locale locale collator differs "
1645 "from actual locale for default collator\n"); 1728 "from actual locale for default collator");
1646 } 1729 }
1647 delete coll; 1730 delete coll;
1648 delete defaultColl; 1731 delete defaultColl;
1649 } 1732 }
1650 1733
1651 1734
1652 1735
1653 /* collator instantiated from rules should have all three locales NULL */ 1736 /* collator instantiated from rules should have all three locales NULL */
1654 coll = new RuleBasedCollator(rlz, status); 1737 coll = new RuleBasedCollator(rlz, status);
1655 locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status); 1738 locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status);
1656 if(!locale.isBogus()) { 1739 if(U_SUCCESS(status) && !locale.isBogus()) {
1657 log("For collator instantiated from rules, requested locale %s is not bogus\ n", locale.getName()); 1740 errln("For collator instantiated from rules, requested locale %s is not bogu s", locale.getName());
1658 } 1741 }
1742 status = U_ZERO_ERROR;
1659 locale = coll->getLocale(ULOC_VALID_LOCALE, status); 1743 locale = coll->getLocale(ULOC_VALID_LOCALE, status);
1660 if(!locale.isBogus()) { 1744 if(!locale.isBogus()) {
1661 log("For collator instantiated from rules, valid locale %s is not bogus\n", locale.getName()); 1745 errln("For collator instantiated from rules, valid locale %s is not bogus", locale.getName());
1662 } 1746 }
1663 locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status); 1747 locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
1664 if(!locale.isBogus()) { 1748 if(!locale.isBogus()) {
1665 log("For collator instantiated from rules, actual locale %s is not bogus\n", locale.getName()); 1749 errln("For collator instantiated from rules, actual locale %s is not bogus", locale.getName());
1666 } 1750 }
1667 delete coll; 1751 delete coll;
1668 } 1752 }
1669 1753
1670 struct teststruct { 1754 struct teststruct {
1671 const char *original; 1755 const char *original;
1672 uint8_t key[256]; 1756 uint8_t key[256];
1673 }; 1757 };
1674 1758
1675 1759
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1822 { 1906 {
1823 struct { 1907 struct {
1824 const char *rules; 1908 const char *rules;
1825 const char *tests[20]; 1909 const char *tests[20];
1826 int32_t testsize; 1910 int32_t testsize;
1827 } setTest[] = { 1911 } setTest[] = {
1828 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3}, 1912 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1829 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C " }, 4} 1913 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C " }, 4}
1830 }; 1914 };
1831 1915
1832 uint32_t i = 0, j = 0; 1916 int32_t i = 0, j = 0;
1833 UErrorCode status = U_ZERO_ERROR; 1917 UErrorCode status = U_ZERO_ERROR;
1834 1918
1835 RuleBasedCollator *coll = NULL;
1836 UnicodeString buff; 1919 UnicodeString buff;
1837 UnicodeSet *set = NULL; 1920 UnicodeSet *set = NULL;
1838 1921
1839 for(i = 0; i < sizeof(setTest)/sizeof(setTest[0]); i++) { 1922 for(i = 0; i < UPRV_LENGTHOF(setTest); i++) {
1840 buff = UnicodeString(setTest[i].rules, "").unescape(); 1923 buff = UnicodeString(setTest[i].rules, -1, US_INV).unescape();
1841 coll = new RuleBasedCollator(buff, status); 1924 RuleBasedCollator coll(buff, status);
1842 if(U_SUCCESS(status)) { 1925 if(U_SUCCESS(status)) {
1843 set = coll->getTailoredSet(status); 1926 set = coll.getTailoredSet(status);
1844 if(set->size() != setTest[i].testsize) { 1927 if(set->size() < setTest[i].testsize) {
1845 errln("Tailored set size different (%d) than expected (%d)", set->size() , setTest[i].testsize); 1928 errln("Tailored set size smaller (%d) than expected (%d)", set->size(), setTest[i].testsize);
1846 } 1929 }
1847 for(j = 0; j < (uint32_t)setTest[i].testsize; j++) { 1930 for(j = 0; j < setTest[i].testsize; j++) {
1848 buff = UnicodeString(setTest[i].tests[j], "").unescape(); 1931 buff = UnicodeString(setTest[i].tests[j], -1, US_INV).unescape();
1849 if(!set->contains(buff)) { 1932 if(!set->contains(buff)) {
1850 errln("Tailored set doesn't contain %s... It should", setTest[i].tests [j]); 1933 errln("Tailored set doesn't contain %s... It should", setTest[i].tests [j]);
1851 } 1934 }
1852 } 1935 }
1853 delete set; 1936 delete set;
1854 } else { 1937 } else {
1855 errcheckln(status, "Couldn't open collator with rules %s - %s", setTest[i] .rules, u_errorName(status)); 1938 errcheckln(status, "Couldn't open collator with rules %s - %s", setTest[i] .rules, u_errorName(status));
1856 } 1939 }
1857 delete coll;
1858 } 1940 }
1859 } 1941 }
1860 1942
1861 void CollationAPITest::TestUClassID() 1943 void CollationAPITest::TestUClassID()
1862 { 1944 {
1863 char id = *((char *)RuleBasedCollator::getStaticClassID()); 1945 char id = *((char *)RuleBasedCollator::getStaticClassID());
1864 if (id != 0) { 1946 if (id != 0) {
1865 errln("Static class id for RuleBasedCollator should be 0"); 1947 errln("Static class id for RuleBasedCollator should be 0");
1866 } 1948 }
1867 UErrorCode status = U_ZERO_ERROR; 1949 UErrorCode status = U_ZERO_ERROR;
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
2263 dump("c1", c1, status); 2345 dump("c1", c1, status);
2264 dump("c2", c2, status); 2346 dump("c2", c2, status);
2265 if(*c1 == *c2){ 2347 if(*c1 == *c2){
2266 errln("The cloned objects refer to same data"); 2348 errln("The cloned objects refer to same data");
2267 } 2349 }
2268 delete c0; 2350 delete c0;
2269 delete c1; 2351 delete c1;
2270 delete c2; 2352 delete c2;
2271 } 2353 }
2272 2354
2355 void CollationAPITest::TestCloneBinary() {
2356 IcuTestErrorCode errorCode(*this, "TestCloneBinary");
2357 LocalPointer<Collator> root(Collator::createInstance(Locale::getRoot(), erro rCode));
2358 LocalPointer<Collator> coll(Collator::createInstance("de@collation=phonebook ", errorCode));
2359 if(errorCode.logDataIfFailureAndReset("Collator::createInstance(de@collation =phonebook)")) {
2360 return;
2361 }
2362 RuleBasedCollator *rbRoot = dynamic_cast<RuleBasedCollator *>(root.getAlias( ));
2363 RuleBasedCollator *rbc = dynamic_cast<RuleBasedCollator *>(coll.getAlias());
2364 if(rbRoot == NULL || rbc == NULL) {
2365 infoln("root or de@collation=phonebook is not a RuleBasedCollator");
2366 return;
2367 }
2368 rbc->setAttribute(UCOL_STRENGTH, UCOL_PRIMARY, errorCode);
2369 UnicodeString uUmlaut((UChar)0xfc);
2370 UnicodeString ue = UNICODE_STRING_SIMPLE("ue");
2371 assertEquals("rbc/primary: u-umlaut==ue", UCOL_EQUAL, rbc->compare(uUmlaut, ue, errorCode));
2372 uint8_t bin[25000];
2373 int32_t binLength = rbc->cloneBinary(bin, UPRV_LENGTHOF(bin), errorCode);
2374 if(errorCode.logDataIfFailureAndReset("rbc->cloneBinary()")) {
2375 return;
2376 }
2377 logln("rbc->cloneBinary() -> %d bytes", (int)binLength);
2378
2379 RuleBasedCollator rbc2(bin, binLength, rbRoot, errorCode);
2380 if(errorCode.logDataIfFailureAndReset("RuleBasedCollator(rbc binary)")) {
2381 return;
2382 }
2383 assertEquals("rbc2.strength==primary", UCOL_PRIMARY, rbc2.getAttribute(UCOL_ STRENGTH, errorCode));
2384 assertEquals("rbc2: u-umlaut==ue", UCOL_EQUAL, rbc2.compare(uUmlaut, ue, err orCode));
2385 assertTrue("rbc==rbc2", *rbc == rbc2);
2386 uint8_t bin2[25000];
2387 int32_t bin2Length = rbc2.cloneBinary(bin2, UPRV_LENGTHOF(bin2), errorCode);
2388 assertEquals("len(rbc binary)==len(rbc2 binary)", binLength, bin2Length);
2389 assertTrue("rbc binary==rbc2 binary", binLength == bin2Length && memcmp(bin, bin2, binLength) == 0);
2390
2391 RuleBasedCollator rbc3(bin, -1, rbRoot, errorCode);
2392 if(errorCode.logDataIfFailureAndReset("RuleBasedCollator(rbc binary, length< 0)")) {
2393 return;
2394 }
2395 assertEquals("rbc3.strength==primary", UCOL_PRIMARY, rbc3.getAttribute(UCOL_ STRENGTH, errorCode));
2396 assertEquals("rbc3: u-umlaut==ue", UCOL_EQUAL, rbc3.compare(uUmlaut, ue, err orCode));
2397 assertTrue("rbc==rbc3", *rbc == rbc3);
2398 }
2399
2273 void CollationAPITest::TestIterNumeric() { 2400 void CollationAPITest::TestIterNumeric() {
2274 // Regression test for ticket #9915. 2401 // Regression test for ticket #9915.
2275 // The collation code sometimes masked the continuation marker away 2402 // The collation code sometimes masked the continuation marker away
2276 // but later tested the result for isContinuation(). 2403 // but later tested the result for isContinuation().
2277 // This test case failed because the third bytes of the computed numeric-col lation primaries 2404 // This test case failed because the third bytes of the computed numeric-col lation primaries
2278 // were permutated with the script reordering table. 2405 // were permutated with the script reordering table.
2279 // It should have been possible to reproduce this with the root collator 2406 // It should have been possible to reproduce this with the root collator
2280 // and characters with appropriate 3-byte primary weights. 2407 // and characters with appropriate 3-byte primary weights.
2281 // The effectiveness of this test depends completely on the collation elemen ts 2408 // The effectiveness of this test depends completely on the collation elemen ts
2282 // and on the implementation code. 2409 // and on the implementation code.
2283 IcuTestErrorCode errorCode(*this, "TestIterNumeric"); 2410 IcuTestErrorCode errorCode(*this, "TestIterNumeric");
2284 RuleBasedCollator coll(UnicodeString("[reorder Hang Hani]"), errorCode); 2411 RuleBasedCollator coll(UnicodeString("[reorder Hang Hani]"), errorCode);
2285 if(errorCode.logDataIfFailureAndReset("RuleBasedCollator constructor")) { 2412 if(errorCode.logDataIfFailureAndReset("RuleBasedCollator constructor")) {
2286 return; 2413 return;
2287 } 2414 }
2288 coll.setAttribute(UCOL_NUMERIC_COLLATION, UCOL_ON, errorCode); 2415 coll.setAttribute(UCOL_NUMERIC_COLLATION, UCOL_ON, errorCode);
2289 UCharIterator iter40, iter72; 2416 UCharIterator iter40, iter72;
2290 uiter_setUTF8(&iter40, "\x34\x30", 2); 2417 uiter_setUTF8(&iter40, "\x34\x30", 2);
2291 uiter_setUTF8(&iter72, "\x37\x32", 2); 2418 uiter_setUTF8(&iter72, "\x37\x32", 2);
2292 UCollationResult result = coll.compare(iter40, iter72, errorCode); 2419 UCollationResult result = coll.compare(iter40, iter72, errorCode);
2293 assertEquals("40<72", (int32_t)UCOL_LESS, (int32_t)result); 2420 assertEquals("40<72", (int32_t)UCOL_LESS, (int32_t)result);
2294 } 2421 }
2295 2422
2423 void CollationAPITest::TestBadKeywords() {
2424 // Test locale IDs with errors.
2425 // Valid locale IDs are tested via data-driven tests.
2426 UErrorCode errorCode = U_ZERO_ERROR;
2427 Locale bogusLocale(Locale::getRoot());
2428 bogusLocale.setToBogus();
2429 LocalPointer<Collator> coll(Collator::createInstance(bogusLocale, errorCode) );
2430 if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
2431 errln("Collator::createInstance(bogus locale) did not fail as expected - %s",
2432 u_errorName(errorCode));
2433 }
2434
2435 // Unknown value.
2436 const char *localeID = "it-u-ks-xyz";
2437 errorCode = U_ZERO_ERROR;
2438 coll.adoptInstead(Collator::createInstance(localeID, errorCode));
2439 if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
2440 dataerrln("Collator::createInstance(%s) did not fail as expected - %s",
2441 localeID, u_errorName(errorCode));
2442 }
2443
2444 // Unsupported attributes.
2445 localeID = "it@colHiraganaQuaternary=true";
2446 errorCode = U_ZERO_ERROR;
2447 coll.adoptInstead(Collator::createInstance(localeID, errorCode));
2448 if(errorCode != U_UNSUPPORTED_ERROR) {
2449 if (errorCode == U_FILE_ACCESS_ERROR) {
2450 dataerrln("Collator::createInstance(it@colHiraganaQuaternary=true) : %s", u_errorName(errorCode));
2451 } else {
2452 errln("Collator::createInstance(%s) did not fail as expected - %s",
2453 localeID, u_errorName(errorCode));
2454 }
2455 }
2456
2457 localeID = "it-u-vt-u24";
2458 errorCode = U_ZERO_ERROR;
2459 coll.adoptInstead(Collator::createInstance(localeID, errorCode));
2460 if(errorCode != U_UNSUPPORTED_ERROR) {
2461 if (errorCode == U_ILLEGAL_ARGUMENT_ERROR || errorCode == U_FILE_ACCESS_ ERROR) {
2462 dataerrln("Collator::createInstance(it-u-vt-u24) : %s", u_errorName( errorCode));
2463 } else {
2464 errln("Collator::createInstance(%s) did not fail as expected - %s",
2465 localeID, u_errorName(errorCode));
2466 }
2467 }
2468 }
2469
2296 void CollationAPITest::dump(UnicodeString msg, RuleBasedCollator* c, UErrorCode & status) { 2470 void CollationAPITest::dump(UnicodeString msg, RuleBasedCollator* c, UErrorCode & status) {
2297 const char* bigone = "One"; 2471 const char* bigone = "One";
2298 const char* littleone = "one"; 2472 const char* littleone = "one";
2299 2473
2300 logln(msg + " " + c->compare(bigone, littleone) + 2474 logln(msg + " " + c->compare(bigone, littleone) +
2301 " s: " + c->getStrength() + 2475 " s: " + c->getStrength() +
2302 " u: " + c->getAttribute(UCOL_CASE_FIRST, status)); 2476 " u: " + c->getAttribute(UCOL_CASE_FIRST, status));
2303 } 2477 }
2304 void CollationAPITest::runIndexedTest( int32_t index, UBool exec, const char* &n ame, char* /*par */) 2478 void CollationAPITest::runIndexedTest( int32_t index, UBool exec, const char* &n ame, char* /*par */)
2305 { 2479 {
2306 if (exec) logln("TestSuite CollationAPITest: "); 2480 if (exec) logln("TestSuite CollationAPITest: ");
2307 TESTCASE_AUTO_BEGIN; 2481 TESTCASE_AUTO_BEGIN;
2308 TESTCASE_AUTO(TestProperty); 2482 TESTCASE_AUTO(TestProperty);
2309 TESTCASE_AUTO(TestOperators); 2483 TESTCASE_AUTO(TestOperators);
2310 TESTCASE_AUTO(TestDuplicate); 2484 TESTCASE_AUTO(TestDuplicate);
2311 TESTCASE_AUTO(TestCompare); 2485 TESTCASE_AUTO(TestCompare);
2312 TESTCASE_AUTO(TestHashCode); 2486 TESTCASE_AUTO(TestHashCode);
2313 TESTCASE_AUTO(TestCollationKey); 2487 TESTCASE_AUTO(TestCollationKey);
2314 TESTCASE_AUTO(TestElemIter); 2488 TESTCASE_AUTO(TestElemIter);
2315 TESTCASE_AUTO(TestGetAll); 2489 TESTCASE_AUTO(TestGetAll);
2316 TESTCASE_AUTO(TestRuleBasedColl); 2490 TESTCASE_AUTO(TestRuleBasedColl);
2317 TESTCASE_AUTO(TestDecomposition); 2491 TESTCASE_AUTO(TestDecomposition);
2318 TESTCASE_AUTO(TestSafeClone); 2492 TESTCASE_AUTO(TestSafeClone);
2319 TESTCASE_AUTO(TestSortKey); 2493 TESTCASE_AUTO(TestSortKey);
2320 TESTCASE_AUTO(TestSortKeyOverflow); 2494 TESTCASE_AUTO(TestSortKeyOverflow);
2321 TESTCASE_AUTO(TestMaxExpansion); 2495 TESTCASE_AUTO(TestMaxExpansion);
2322 TESTCASE_AUTO(TestDisplayName); 2496 TESTCASE_AUTO(TestDisplayName);
2323 TESTCASE_AUTO(TestAttribute); 2497 TESTCASE_AUTO(TestAttribute);
2324 TESTCASE_AUTO(TestVariableTopSetting); 2498 TESTCASE_AUTO(TestVariableTopSetting);
2499 TESTCASE_AUTO(TestMaxVariable);
2325 TESTCASE_AUTO(TestRules); 2500 TESTCASE_AUTO(TestRules);
2326 TESTCASE_AUTO(TestGetLocale); 2501 TESTCASE_AUTO(TestGetLocale);
2327 TESTCASE_AUTO(TestBounds); 2502 TESTCASE_AUTO(TestBounds);
2328 TESTCASE_AUTO(TestGetTailoredSet); 2503 TESTCASE_AUTO(TestGetTailoredSet);
2329 TESTCASE_AUTO(TestUClassID); 2504 TESTCASE_AUTO(TestUClassID);
2330 TESTCASE_AUTO(TestSubclass); 2505 TESTCASE_AUTO(TestSubclass);
2331 TESTCASE_AUTO(TestNULLCharTailoring); 2506 TESTCASE_AUTO(TestNULLCharTailoring);
2332 TESTCASE_AUTO(TestClone); 2507 TESTCASE_AUTO(TestClone);
2508 TESTCASE_AUTO(TestCloneBinary);
2333 TESTCASE_AUTO(TestIterNumeric); 2509 TESTCASE_AUTO(TestIterNumeric);
2510 TESTCASE_AUTO(TestBadKeywords);
2334 TESTCASE_AUTO_END; 2511 TESTCASE_AUTO_END;
2335 } 2512 }
2336 2513
2337 #endif /* #if !UCONFIG_NO_COLLATION */ 2514 #endif /* #if !UCONFIG_NO_COLLATION */
OLDNEW
« no previous file with comments | « source/test/intltest/apicoll.h ('k') | source/test/intltest/astrotst.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698