1 /********************************************************************
2 * Copyright (c) 1997-2013, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 ********************************************************************/
5 /*****************************************************************************
9 * Modification History:
11 * Madhu Katragadda Ported for C API
12 * Brian Rower Added TestOpenVsOpenRules
13 ******************************************************************************
14 *//* C API TEST For COLLATOR */
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_COLLATION
23 #include "unicode/uloc.h"
24 #include "unicode/ulocdata.h"
25 #include "unicode/ustring.h"
26 #include "unicode/ures.h"
27 #include "unicode/ucoleitr.h"
35 static void TestAttribute(void);
36 static void TestDefault(void);
37 static void TestDefaultKeyword(void);
38 static void TestBengaliSortKey(void);
39 int TestBufferSize(); /* defined in "colutil.c" */
42 static char* U_EXPORT2
ucol_sortKeyToString(const UCollator
*coll
, const uint8_t *sortkey
, char *buffer
, uint32_t len
) {
43 uint32_t position
= 0;
46 if (position
+ 1 < len
)
47 position
+= sprintf(buffer
+ position
, "[");
48 while ((b
= *sortkey
++) != 0) {
49 if (b
== 1 && position
+ 5 < len
) {
50 position
+= sprintf(buffer
+ position
, "%02X . ", b
);
51 } else if (b
!= 1 && position
+ 3 < len
) {
52 position
+= sprintf(buffer
+ position
, "%02X ", b
);
55 if (position
+ 3 < len
)
56 position
+= sprintf(buffer
+ position
, "%02X]", b
);
60 void addCollAPITest(TestNode
** root
)
62 /* WEIVTODO: return tests here */
63 addTest(root
, &TestProperty
, "tscoll/capitst/TestProperty");
64 addTest(root
, &TestRuleBasedColl
, "tscoll/capitst/TestRuleBasedColl");
65 addTest(root
, &TestCompare
, "tscoll/capitst/TestCompare");
66 addTest(root
, &TestSortKey
, "tscoll/capitst/TestSortKey");
67 addTest(root
, &TestHashCode
, "tscoll/capitst/TestHashCode");
68 addTest(root
, &TestElemIter
, "tscoll/capitst/TestElemIter");
69 addTest(root
, &TestGetAll
, "tscoll/capitst/TestGetAll");
70 /*addTest(root, &TestGetDefaultRules, "tscoll/capitst/TestGetDefaultRules");*/
71 addTest(root
, &TestDecomposition
, "tscoll/capitst/TestDecomposition");
72 addTest(root
, &TestSafeClone
, "tscoll/capitst/TestSafeClone");
73 addTest(root
, &TestCloneBinary
, "tscoll/capitst/TestCloneBinary");
74 addTest(root
, &TestGetSetAttr
, "tscoll/capitst/TestGetSetAttr");
75 addTest(root
, &TestBounds
, "tscoll/capitst/TestBounds");
76 addTest(root
, &TestGetLocale
, "tscoll/capitst/TestGetLocale");
77 addTest(root
, &TestSortKeyBufferOverrun
, "tscoll/capitst/TestSortKeyBufferOverrun");
78 addTest(root
, &TestAttribute
, "tscoll/capitst/TestAttribute");
79 addTest(root
, &TestGetTailoredSet
, "tscoll/capitst/TestGetTailoredSet");
80 addTest(root
, &TestMergeSortKeys
, "tscoll/capitst/TestMergeSortKeys");
81 addTest(root
, &TestShortString
, "tscoll/capitst/TestShortString");
82 addTest(root
, &TestGetContractionsAndUnsafes
, "tscoll/capitst/TestGetContractionsAndUnsafes");
83 addTest(root
, &TestOpenBinary
, "tscoll/capitst/TestOpenBinary");
84 addTest(root
, &TestDefault
, "tscoll/capitst/TestDefault");
85 addTest(root
, &TestDefaultKeyword
, "tscoll/capitst/TestDefaultKeyword");
86 addTest(root
, &TestOpenVsOpenRules
, "tscoll/capitst/TestOpenVsOpenRules");
87 addTest(root
, &TestBengaliSortKey
, "tscoll/capitst/TestBengaliSortKey");
88 addTest(root
, &TestGetKeywordValuesForLocale
, "tscoll/capitst/TestGetKeywordValuesForLocale");
91 void TestGetSetAttr(void) {
92 UErrorCode status
= U_ZERO_ERROR
;
93 UCollator
*coll
= ucol_open(NULL
, &status
);
96 UColAttributeValue val
[5];
98 UColAttributeValue nonValue
;
100 {UCOL_FRENCH_COLLATION
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},
101 {UCOL_ALTERNATE_HANDLING
, {UCOL_NON_IGNORABLE
, UCOL_SHIFTED
}, 2, UCOL_OFF
},/* attribute for handling variable elements*/
102 {UCOL_CASE_FIRST
, {UCOL_OFF
, UCOL_LOWER_FIRST
, UCOL_UPPER_FIRST
}, 3, UCOL_SHIFTED
},/* who goes first, lower case or uppercase */
103 {UCOL_CASE_LEVEL
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* do we have an extra case level */
104 {UCOL_NORMALIZATION_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* attribute for normalization */
105 {UCOL_DECOMPOSITION_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},
106 {UCOL_STRENGTH
, {UCOL_PRIMARY
, UCOL_SECONDARY
, UCOL_TERTIARY
, UCOL_QUATERNARY
, UCOL_IDENTICAL
}, 5, UCOL_SHIFTED
},/* attribute for strength */
107 {UCOL_HIRAGANA_QUATERNARY_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* when turned on, this attribute */
109 UColAttribute currAttr
;
110 UColAttributeValue value
;
111 uint32_t i
= 0, j
= 0;
114 log_err_status(status
, "Unable to open collator. %s\n", u_errorName(status
));
117 for(i
= 0; i
<sizeof(attrs
)/sizeof(attrs
[0]); i
++) {
118 currAttr
= attrs
[i
].att
;
119 ucol_setAttribute(coll
, currAttr
, UCOL_DEFAULT
, &status
);
120 if(U_FAILURE(status
)) {
121 log_err_status(status
, "ucol_setAttribute with the default value returned error: %s\n", u_errorName(status
));
124 value
= ucol_getAttribute(coll
, currAttr
, &status
);
125 if(U_FAILURE(status
)) {
126 log_err("ucol_getAttribute returned error: %s\n", u_errorName(status
));
129 for(j
= 0; j
<attrs
[i
].valueSize
; j
++) {
130 ucol_setAttribute(coll
, currAttr
, attrs
[i
].val
[j
], &status
);
131 if(U_FAILURE(status
)) {
132 log_err("ucol_setAttribute with the value %i returned error: %s\n", attrs
[i
].val
[j
], u_errorName(status
));
136 status
= U_ZERO_ERROR
;
137 ucol_setAttribute(coll
, currAttr
, attrs
[i
].nonValue
, &status
);
138 if(U_SUCCESS(status
)) {
139 log_err("ucol_setAttribute with the bad value didn't return an error\n");
142 status
= U_ZERO_ERROR
;
144 ucol_setAttribute(coll
, currAttr
, value
, &status
);
145 if(U_FAILURE(status
)) {
146 log_err("ucol_setAttribute with the default valuereturned error: %s\n", u_errorName(status
));
150 status
= U_ZERO_ERROR
;
151 value
= ucol_getAttribute(coll
, UCOL_ATTRIBUTE_COUNT
, &status
);
152 if(U_SUCCESS(status
)) {
153 log_err("ucol_getAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
155 status
= U_ZERO_ERROR
;
156 ucol_setAttribute(coll
, UCOL_ATTRIBUTE_COUNT
, UCOL_DEFAULT
, &status
);
157 if(U_SUCCESS(status
)) {
158 log_err("ucol_setAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
160 status
= U_ZERO_ERROR
;
165 static void doAssert(int condition
, const char *message
)
168 log_err("ERROR : %s\n", message
);
172 #define UTF8_BUF_SIZE 128
174 static void doStrcoll(const UCollator
* coll
, const UChar
* src
, int32_t srcLen
, const UChar
* tgt
, int32_t tgtLen
,
175 UCollationResult expected
, const char *message
) {
176 UErrorCode err
= U_ZERO_ERROR
;
177 char srcU8
[UTF8_BUF_SIZE
], tgtU8
[UTF8_BUF_SIZE
];
178 int32_t srcU8Len
= -1, tgtU8Len
= -1;
181 if (ucol_strcoll(coll
, src
, srcLen
, tgt
, tgtLen
) != expected
) {
182 log_err("ERROR : %s\n", message
);
185 u_strToUTF8(srcU8
, UTF8_BUF_SIZE
, &len
, src
, srcLen
, &err
);
186 if (U_FAILURE(err
) || len
>= UTF8_BUF_SIZE
) {
187 log_err("ERROR : UTF-8 conversion error\n");
193 u_strToUTF8(tgtU8
, UTF8_BUF_SIZE
, &len
, tgt
, tgtLen
, &err
);
194 if (U_FAILURE(err
) || len
>= UTF8_BUF_SIZE
) {
195 log_err("ERROR : UTF-8 conversion error\n");
202 if (ucol_strcollUTF8(coll
, srcU8
, srcU8Len
, tgtU8
, tgtU8Len
, &err
) != expected
204 log_err("ERROR: %s (strcollUTF8)\n", message
);
209 /* We don't have default rules, at least not in the previous sense */
210 void TestGetDefaultRules(){
212 UErrorCode status
=U_ZERO_ERROR
;
213 UCollator
*coll
=NULL
;
214 int32_t len1
= 0, len2
=0;
215 uint8_t *binColData
= NULL
;
217 UResourceBundle
*res
= NULL
;
218 UResourceBundle
*binColl
= NULL
;
219 uint8_t *binResult
= NULL
;
222 const UChar
* defaultRulesArray
=ucol_getDefaultRulesArray(&size
);
223 log_verbose("Test the function ucol_getDefaultRulesArray()\n");
225 coll
= ucol_openRules(defaultRulesArray
, size
, UCOL_ON
, UCOL_PRIMARY
, &status
);
226 if(U_SUCCESS(status
) && coll
!=NULL
) {
227 binColData
= (uint8_t*)ucol_cloneRuleData(coll
, &len1
, &status
);
233 res
=ures_open(NULL
, "root", &status
);
234 if(U_FAILURE(status
)){
235 log_err("ERROR: Failed to get resource for \"root Locale\" with %s", myErrorName(status
));
238 binColl
=ures_getByKey(res
, "%%Collation", binColl
, &status
);
239 if(U_SUCCESS(status
)){
240 binResult
=(uint8_t*)ures_getBinary(binColl
, &len2
, &status
);
241 if(U_FAILURE(status
)){
242 log_err("ERROR: ures_getBinary() failed\n");
245 log_err("ERROR: ures_getByKey(locale(default), %%Collation) failed");
250 log_err("Error: ucol_getDefaultRulesArray() failed to return the correct length.\n");
252 if(memcmp(binColData
, binResult
, len1
) != 0){
253 log_err("Error: ucol_getDefaultRulesArray() failed\n");
264 /* Collator Properties
265 ucol_open, ucol_strcoll, getStrength/setStrength
266 getDecomposition/setDecomposition, getDisplayName*/
269 UCollator
*col
, *ruled
;
272 UChar source
[12], target
[12];
274 UErrorCode status
= U_ZERO_ERROR
;
276 * Expected version of the English collator.
277 * Currently, the major/minor version numbers change when the builder code
279 * number 2 is from the tailoring data version and
280 * number 3 is the UCA version.
281 * This changes with every UCA version change, and the expected value
282 * needs to be adjusted.
283 * Same in intltest/apicoll.cpp.
285 UVersionInfo currVersionArray
= {0x31, 0xC0, 0x05, 0x2A}; /* from ICU 4.4/UCA 5.2 */
286 UVersionInfo versionArray
= {0, 0, 0, 0};
287 UVersionInfo versionUCAArray
= {0, 0, 0, 0};
288 UVersionInfo versionUCDArray
= {0, 0, 0, 0};
290 log_verbose("The property tests begin : \n");
291 log_verbose("Test ucol_strcoll : \n");
292 col
= ucol_open("en_US", &status
);
293 if (U_FAILURE(status
)) {
294 log_err_status(status
, "Default Collator creation failed.: %s\n", myErrorName(status
));
298 ucol_getVersion(col
, versionArray
);
299 /* Check for a version greater than some value rather than equality
300 * so that we need not update the expected version each time. */
301 if (uprv_memcmp(versionArray
, currVersionArray
, 4)<0) {
302 log_err("Testing ucol_getVersion() - unexpected result: %02x.%02x.%02x.%02x\n",
303 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
305 log_verbose("ucol_getVersion() result: %02x.%02x.%02x.%02x\n",
306 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
309 /* Assume that the UCD and UCA versions are the same,
310 * rather than hardcoding (and updating each time) a particular UCA version. */
311 u_getUnicodeVersion(versionUCDArray
);
312 ucol_getUCAVersion(col
, versionUCAArray
);
313 if (0!=uprv_memcmp(versionUCAArray
, versionUCDArray
, 4)) {
314 log_err("Testing ucol_getUCAVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
315 versionUCAArray
[0], versionUCAArray
[1], versionUCAArray
[2], versionUCAArray
[3]);
318 u_uastrcpy(source
, "ab");
319 u_uastrcpy(target
, "abc");
321 doStrcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
), UCOL_LESS
, "ab < abc comparison failed");
323 u_uastrcpy(source
, "ab");
324 u_uastrcpy(target
, "AB");
326 doStrcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
), UCOL_LESS
, "ab < AB comparison failed");
328 u_uastrcpy(source
, "blackbird");
329 u_uastrcpy(target
, "black-bird");
331 doStrcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
), UCOL_GREATER
, "black-bird > blackbird comparison failed");
333 u_uastrcpy(source
, "black bird");
334 u_uastrcpy(target
, "black-bird");
336 doStrcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
), UCOL_LESS
, "black bird < black-bird comparison failed");
338 u_uastrcpy(source
, "Hello");
339 u_uastrcpy(target
, "hello");
341 doStrcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
), UCOL_GREATER
, "Hello > hello comparison failed");
343 log_verbose("Test ucol_strcoll ends.\n");
345 log_verbose("testing ucol_getStrength() method ...\n");
346 doAssert( (ucol_getStrength(col
) == UCOL_TERTIARY
), "collation object has the wrong strength");
347 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
349 log_verbose("testing ucol_setStrength() method ...\n");
350 ucol_setStrength(col
, UCOL_SECONDARY
);
351 doAssert( (ucol_getStrength(col
) != UCOL_TERTIARY
), "collation object's strength is secondary difference");
352 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
353 doAssert( (ucol_getStrength(col
) == UCOL_SECONDARY
), "collation object has the wrong strength");
356 log_verbose("Get display name for the default collation in German : \n");
358 len
=ucol_getDisplayName("en_US", "de_DE", NULL
, 0, &status
);
359 if(status
==U_BUFFER_OVERFLOW_ERROR
){
361 disName
=(UChar
*)malloc(sizeof(UChar
) * (len
+1));
362 ucol_getDisplayName("en_US", "de_DE", disName
, len
+1, &status
);
363 log_verbose("the display name for default collation in german: %s\n", austrdup(disName
) );
366 if(U_FAILURE(status
)){
367 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status
));
370 log_verbose("Default collation getDisplayName ended.\n");
372 ruled
= ucol_open("da_DK", &status
);
373 log_verbose("ucol_getRules() testing ...\n");
374 ucol_getRules(ruled
, &tempLength
);
375 doAssert( tempLength
!= 0, "getRules() result incorrect" );
376 log_verbose("getRules tests end.\n");
378 UChar
*buffer
= (UChar
*)malloc(200000*sizeof(UChar
));
379 int32_t bufLen
= 200000;
381 log_verbose("ucol_getRulesEx() testing ...\n");
382 tempLength
= ucol_getRulesEx(col
,UCOL_TAILORING_ONLY
,buffer
,bufLen
);
383 doAssert( tempLength
== 0x00, "getRulesEx() result incorrect" );
384 log_verbose("getRules tests end.\n");
386 log_verbose("ucol_getRulesEx() testing ...\n");
387 tempLength
=ucol_getRulesEx(col
,UCOL_FULL_RULES
,buffer
,bufLen
);
388 doAssert( tempLength
!= 0, "getRulesEx() result incorrect" );
389 log_verbose("getRules tests end.\n");
395 log_verbose("open an collator for french locale");
396 col
= ucol_open("fr_FR", &status
);
397 if (U_FAILURE(status
)) {
398 log_err("ERROR: Creating French collation failed.: %s\n", myErrorName(status
));
401 ucol_setStrength(col
, UCOL_PRIMARY
);
402 log_verbose("testing ucol_getStrength() method again ...\n");
403 doAssert( (ucol_getStrength(col
) != UCOL_TERTIARY
), "collation object has the wrong strength");
404 doAssert( (ucol_getStrength(col
) == UCOL_PRIMARY
), "collation object's strength is not primary difference");
406 log_verbose("testing French ucol_setStrength() method ...\n");
407 ucol_setStrength(col
, UCOL_TERTIARY
);
408 doAssert( (ucol_getStrength(col
) == UCOL_TERTIARY
), "collation object's strength is not tertiary difference");
409 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
410 doAssert( (ucol_getStrength(col
) != UCOL_SECONDARY
), "collation object's strength is secondary difference");
413 log_verbose("Get display name for the french collation in english : \n");
414 len
=ucol_getDisplayName("fr_FR", "en_US", NULL
, 0, &status
);
415 if(status
==U_BUFFER_OVERFLOW_ERROR
){
417 disName
=(UChar
*)malloc(sizeof(UChar
) * (len
+1));
418 ucol_getDisplayName("fr_FR", "en_US", disName
, len
+1, &status
);
419 log_verbose("the display name for french collation in english: %s\n", austrdup(disName
) );
422 if(U_FAILURE(status
)){
423 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status
));
426 log_verbose("Default collation getDisplayName ended.\n");
430 /* Test RuleBasedCollator and getRules*/
431 void TestRuleBasedColl()
433 UCollator
*col1
, *col2
, *col3
, *col4
;
434 UCollationElements
*iter1
, *iter2
;
439 const UChar
*rule1
, *rule2
, *rule3
, *rule4
;
441 UErrorCode status
= U_ZERO_ERROR
;
442 u_uastrcpy(ruleset1
, "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
443 u_uastrcpy(ruleset2
, "&9 < a, A < b, B < c, C < d, D, e, E");
446 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
,&status
);
447 if (U_FAILURE(status
)) {
448 log_err_status(status
, "RuleBased Collator creation failed.: %s\n", myErrorName(status
));
452 log_verbose("PASS: RuleBased Collator creation passed\n");
454 status
= U_ZERO_ERROR
;
455 col2
= ucol_openRules(ruleset2
, u_strlen(ruleset2
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
456 if (U_FAILURE(status
)) {
457 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
461 log_verbose("PASS: RuleBased Collator creation passed\n");
464 status
= U_ZERO_ERROR
;
465 col3
= ucol_open(NULL
, &status
);
466 if (U_FAILURE(status
)) {
467 log_err("Default Collator creation failed.: %s\n", myErrorName(status
));
471 log_verbose("PASS: Default Collator creation passed\n");
473 rule1
= ucol_getRules(col1
, &tempLength
);
474 rule2
= ucol_getRules(col2
, &tempLength
);
475 rule3
= ucol_getRules(col3
, &tempLength
);
477 doAssert((u_strcmp(rule1
, rule2
) != 0), "Default collator getRules failed");
478 doAssert((u_strcmp(rule2
, rule3
) != 0), "Default collator getRules failed");
479 doAssert((u_strcmp(rule1
, rule3
) != 0), "Default collator getRules failed");
481 col4
=ucol_openRules(rule2
, u_strlen(rule2
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
482 if (U_FAILURE(status
)) {
483 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
486 rule4
= ucol_getRules(col4
, &tempLength
);
487 doAssert((u_strcmp(rule2
, rule4
) == 0), "Default collator getRules failed");
494 /* tests that modifier ! is always ignored */
495 u_uastrcpy(ruleset1
, "!&a<b");
499 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
500 if (U_FAILURE(status
)) {
501 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
504 col2
= ucol_open("en_US", &status
);
505 if (U_FAILURE(status
)) {
506 log_err("en_US Collator creation failed.: %s\n", myErrorName(status
));
509 iter1
= ucol_openElements(col1
, teststr
, 3, &status
);
510 iter2
= ucol_openElements(col2
, teststr
, 3, &status
);
511 if(U_FAILURE(status
)) {
512 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status
));
516 /* testing with en since thai has its own tailoring */
517 uint32_t ce
= ucol_next(iter1
, &status
);
518 uint32_t ce2
= ucol_next(iter2
, &status
);
519 if(U_FAILURE(status
)) {
520 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status
));
524 log_err("! modifier test failed");
526 if (ce
== UCOL_NULLORDER
) {
530 ucol_closeElements(iter1
);
531 ucol_closeElements(iter2
);
534 /* test that we can start a rule without a & or < */
535 u_uastrcpy(ruleset1
, "< z < a");
536 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
537 if (U_FAILURE(status
)) {
538 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
541 u_uastrcpy(teststr
, "z");
542 u_uastrcpy(teststr2
, "a");
543 if (ucol_greaterOrEqual(col1
, teststr
, 1, teststr2
, 1)) {
544 log_err("Rule \"z < a\" fails");
551 UErrorCode status
= U_ZERO_ERROR
;
556 log_verbose("The compare tests begin : \n");
558 col
= ucol_open("en_US", &status
);
559 if(U_FAILURE(status
)) {
560 log_err_status(status
, "ucal_open() collation creation failed.: %s\n", myErrorName(status
));
563 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
564 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
565 u_uastrcpy(test1
, "Abcda");
566 u_uastrcpy(test2
, "abcda");
568 log_verbose("Use tertiary comparison level testing ....\n");
570 doAssert( (!ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" != \"abcda\" ");
571 doAssert( (ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" >>> \"abcda\" ");
572 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" >>> \"abcda\"");
574 ucol_setStrength(col
, UCOL_SECONDARY
);
575 log_verbose("Use secondary comparison level testing ....\n");
577 doAssert( (ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
) )), "Result should be \"Abcda\" == \"abcda\"");
578 doAssert( (!ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
579 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
) )), "Result should be \"Abcda\" == \"abcda\"");
581 ucol_setStrength(col
, UCOL_PRIMARY
);
582 log_verbose("Use primary comparison level testing ....\n");
584 doAssert( (ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
585 doAssert( (!ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
586 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
589 log_verbose("The compare tests end.\n");
596 ---------------------------------------------
597 tests decomposition setting
599 void TestDecomposition() {
600 UErrorCode status
= U_ZERO_ERROR
;
601 UCollator
*en_US
, *el_GR
, *vi_VN
;
602 en_US
= ucol_open("en_US", &status
);
603 el_GR
= ucol_open("el_GR", &status
);
604 vi_VN
= ucol_open("vi_VN", &status
);
606 if (U_FAILURE(status
)) {
607 log_err_status(status
, "ERROR: collation creation failed.: %s\n", myErrorName(status
));
611 if (ucol_getAttribute(vi_VN
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_ON
||
614 log_err("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
617 status
= U_ZERO_ERROR
;
618 if (ucol_getAttribute(el_GR
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_ON
||
621 log_err("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
624 status
= U_ZERO_ERROR
;
625 if (ucol_getAttribute(en_US
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_OFF
||
628 log_err("ERROR: en_US collation had canonical decomposition for normalization!\n");
636 #define CLONETEST_COLLATOR_COUNT 4
638 void TestSafeClone() {
641 static const UChar umlautUStr
[] = {0x00DC, 0};
642 static const UChar oeStr
[] = {0x0055, 0x0045, 0};
643 UCollator
* someCollators
[CLONETEST_COLLATOR_COUNT
];
644 UCollator
* someClonedCollators
[CLONETEST_COLLATOR_COUNT
];
646 UErrorCode err
= U_ZERO_ERROR
;
647 int8_t idx
= 6; /* Leave this here to test buffer alingment in memory*/
648 uint8_t buffer
[CLONETEST_COLLATOR_COUNT
] [U_COL_SAFECLONE_BUFFERSIZE
];
649 int32_t bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
650 const char sampleRuleChars
[] = "&Z < CH";
651 UChar sampleRule
[sizeof(sampleRuleChars
)];
653 if (TestBufferSize()) {
654 log_err("U_COL_SAFECLONE_BUFFERSIZE should be larger than sizeof(UCollator)\n");
658 u_uastrcpy(test1
, "abCda");
659 u_uastrcpy(test2
, "abcda");
660 u_uastrcpy(sampleRule
, sampleRuleChars
);
662 /* one default collator & two complex ones */
663 someCollators
[0] = ucol_open("en_US", &err
);
664 someCollators
[1] = ucol_open("ko", &err
);
665 someCollators
[2] = ucol_open("ja_JP", &err
);
666 someCollators
[3] = ucol_openRules(sampleRule
, -1, UCOL_ON
, UCOL_TERTIARY
, NULL
, &err
);
668 for (idx
= 0; idx
< CLONETEST_COLLATOR_COUNT
; idx
++) {
669 ucol_close(someCollators
[idx
]);
671 log_data_err("Couldn't open one or more collators\n");
675 /* Check the various error & informational states: */
677 /* Null status - just returns NULL */
678 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, 0))
680 log_err("FAIL: Cloned Collator failed to deal correctly with null status\n");
682 /* error status - should return 0 & keep error the same */
683 err
= U_MEMORY_ALLOCATION_ERROR
;
684 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
) || err
!= U_MEMORY_ALLOCATION_ERROR
)
686 log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
690 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
691 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], 0, &err
) || err
!= U_ILLEGAL_ARGUMENT_ERROR
)
693 log_err("FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer\n");
697 /* buffer size pointer is 0 - fill in pbufferSize with a size */
699 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
) || U_FAILURE(err
) || bufferSize
<= 0)
701 log_err("FAIL: Cloned Collator failed a sizing request ('preflighting')\n");
703 /* Verify our define is large enough */
704 if (U_COL_SAFECLONE_BUFFERSIZE
< bufferSize
)
706 log_err("FAIL: Pre-calculated buffer size is too small\n");
708 /* Verify we can use this run-time calculated size */
709 if (0 == (col
= ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
)) || U_FAILURE(err
))
711 log_err("FAIL: Collator can't be cloned with run-time size\n");
713 if (col
) ucol_close(col
);
714 /* size one byte too small - should allocate & let us know */
716 if (0 == (col
= ucol_safeClone(someCollators
[0], 0, &bufferSize
, &err
)) || err
!= U_SAFECLONE_ALLOCATED_WARNING
)
718 log_err("FAIL: Cloned Collator failed to deal correctly with too-small buffer size\n");
720 if (col
) ucol_close(col
);
722 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
725 /* Null buffer pointer - return Collator & set error to U_SAFECLONE_ALLOCATED_ERROR */
726 if (0 == (col
= ucol_safeClone(someCollators
[0], 0, &bufferSize
, &err
)) || err
!= U_SAFECLONE_ALLOCATED_WARNING
)
728 log_err("FAIL: Cloned Collator failed to deal correctly with null buffer pointer\n");
730 if (col
) ucol_close(col
);
733 /* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
734 if (0 != ucol_safeClone(0, buffer
[0], &bufferSize
, &err
) || err
!= U_ILLEGAL_ARGUMENT_ERROR
)
736 log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
741 /* Test that a cloned collator doesn't accidentally use UCA. */
742 col
=ucol_open("de@collation=phonebook", &err
);
743 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
744 someClonedCollators
[0] = ucol_safeClone(col
, buffer
[0], &bufferSize
, &err
);
745 doAssert( (ucol_greater(col
, umlautUStr
, u_strlen(umlautUStr
), oeStr
, u_strlen(oeStr
))), "Original German phonebook collation sorts differently than expected");
746 doAssert( (ucol_greater(someClonedCollators
[0], umlautUStr
, u_strlen(umlautUStr
), oeStr
, u_strlen(oeStr
))), "Cloned German phonebook collation sorts differently than expected");
747 if (!ucol_equals(someClonedCollators
[0], col
)) {
748 log_err("FAIL: Cloned German phonebook collator is not equal to original.\n");
751 ucol_close(someClonedCollators
[0]);
755 /* change orig & clone & make sure they are independent */
757 for (idx
= 0; idx
< CLONETEST_COLLATOR_COUNT
; idx
++)
759 ucol_setStrength(someCollators
[idx
], UCOL_IDENTICAL
);
762 ucol_close(ucol_safeClone(someCollators
[idx
], buffer
[idx
], &bufferSize
, &err
));
763 if (err
!= U_SAFECLONE_ALLOCATED_WARNING
) {
764 log_err("FAIL: collator number %d was not allocated.\n", idx
);
765 log_err("FAIL: status of Collator[%d] is %d (hex: %x).\n", idx
, err
, err
);
768 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
770 someClonedCollators
[idx
] = ucol_safeClone(someCollators
[idx
], buffer
[idx
], &bufferSize
, &err
);
771 if (someClonedCollators
[idx
] == NULL
772 || someClonedCollators
[idx
] < (UCollator
*)buffer
[idx
]
773 || someClonedCollators
[idx
] > (UCollator
*)(buffer
[idx
]+(U_COL_SAFECLONE_BUFFERSIZE
-1)))
775 /* TODO: The use of U_COL_SAFECLONE_BUFFERSIZE will be deprecated per #9932.
776 In the meantime, just turn the following former error into a log message. */
777 log_verbose("NOTE: Cloned collator did not use provided buffer, index %d, status %s, clone NULL? %d\n",
778 idx
, myErrorName(err
), someClonedCollators
[idx
] == NULL
);
780 if (!ucol_equals(someClonedCollators
[idx
], someCollators
[idx
])) {
781 log_err("FAIL: Cloned collator is not equal to original at index = %d.\n", idx
);
784 /* Check the usability */
785 ucol_setStrength(someCollators
[idx
], UCOL_PRIMARY
);
786 ucol_setAttribute(someCollators
[idx
], UCOL_CASE_LEVEL
, UCOL_OFF
, &err
);
788 doAssert( (ucol_equal(someCollators
[idx
], test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"abcda\" == \"abCda\"");
790 /* Close the original to make sure that the clone is usable. */
791 ucol_close(someCollators
[idx
]);
793 ucol_setStrength(someClonedCollators
[idx
], UCOL_TERTIARY
);
794 ucol_setAttribute(someClonedCollators
[idx
], UCOL_CASE_LEVEL
, UCOL_OFF
, &err
);
795 doAssert( (ucol_greater(someClonedCollators
[idx
], test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"abCda\" >>> \"abcda\" ");
797 ucol_close(someClonedCollators
[idx
]);
801 void TestCloneBinary(){
802 UErrorCode err
= U_ZERO_ERROR
;
803 UCollator
* col
= ucol_open("en_US", &err
);
808 if (U_FAILURE(err
)) {
809 log_data_err("Couldn't open collator. Error: %s\n", u_errorName(err
));
813 size
= ucol_cloneBinary(col
, NULL
, 0, &err
);
814 if(size
==0 || err
!=U_BUFFER_OVERFLOW_ERROR
) {
815 log_err("ucol_cloneBinary - couldn't check size. Error: %s\n", u_errorName(err
));
820 buffer
= (uint8_t *) malloc(size
);
821 ucol_cloneBinary(col
, buffer
, size
, &err
);
823 log_err("ucol_cloneBinary - couldn't clone.. Error: %s\n", u_errorName(err
));
828 /* how to check binary result ? */
830 c
= ucol_openBinary(buffer
, size
, col
, &err
);
832 log_err("ucol_openBinary failed. Error: %s\n", u_errorName(err
));
834 UChar t
[] = {0x41, 0x42, 0x43, 0}; /* ABC */
837 l1
= ucol_getSortKey(col
, t
, -1, NULL
,0);
838 l2
= ucol_getSortKey(c
, t
, -1, NULL
,0);
839 k1
= (uint8_t *) malloc(sizeof(uint8_t) * l1
);
840 k2
= (uint8_t *) malloc(sizeof(uint8_t) * l2
);
841 ucol_getSortKey(col
, t
, -1, k1
, l1
);
842 ucol_getSortKey(col
, t
, -1, k2
, l2
);
843 if (strcmp((char *)k1
,(char *)k2
) != 0){
844 log_err("ucol_openBinary - new collator should equal to old one\n");
855 static void TestBengaliSortKey(void)
857 const char *curLoc
= "bn";
858 UChar str1
[] = { 0x09BE, 0 };
859 UChar str2
[] = { 0x0B70, 0 };
860 UCollator
*c2
= NULL
;
862 int32_t rulesLength
=-1;
864 int32_t sortKeyLen1
= 0;
866 int32_t sortKeyLen2
= 0;
867 UErrorCode status
= U_ZERO_ERROR
;
868 char sortKeyStr1
[2048];
869 uint32_t sortKeyStrLen1
= sizeof(sortKeyStr1
)/sizeof(sortKeyStr1
[0]);
870 char sortKeyStr2
[2048];
871 uint32_t sortKeyStrLen2
= sizeof(sortKeyStr2
)/sizeof(sortKeyStr2
[0]);
872 UCollationResult result
;
874 static UChar preRules
[41] = { 0x26, 0x9fa, 0x3c, 0x98c, 0x3c, 0x9e1, 0x3c, 0x98f, 0x3c, 0x990, 0x3c, 0x993, 0x3c, 0x994, 0x3c, 0x9bc, 0x3c, 0x982, 0x3c, 0x983, 0x3c, 0x981, 0x3c, 0x9b0, 0x3c, 0x9b8, 0x3c, 0x9b9, 0x3c, 0x9bd, 0x3c, 0x9be, 0x3c, 0x9bf, 0x3c, 0x9c8, 0x3c, 0x9cb, 0x3d, 0x9cb , 0};
878 log_verbose("Rules: %s\n", aescstrdup(rules
, rulesLength
));
880 c2
= ucol_openRules(rules
, rulesLength
, UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
881 if (U_FAILURE(status
)) {
882 log_data_err("ERROR: Creating collator from rules failed with locale: %s : %s\n", curLoc
, myErrorName(status
));
886 sortKeyLen1
= ucol_getSortKey(c2
, str1
, -1, NULL
, 0);
887 sortKey1
= (uint8_t*)malloc(sortKeyLen1
+1);
888 ucol_getSortKey(c2
,str1
,-1,sortKey1
, sortKeyLen1
+1);
889 ucol_sortKeyToString(c2
, sortKey1
, sortKeyStr1
, sortKeyStrLen1
);
892 sortKeyLen2
= ucol_getSortKey(c2
, str2
, -1, NULL
, 0);
893 sortKey2
= (uint8_t*)malloc(sortKeyLen2
+1);
894 ucol_getSortKey(c2
,str2
,-1,sortKey2
, sortKeyLen2
+1);
896 ucol_sortKeyToString(c2
, sortKey2
, sortKeyStr2
, sortKeyStrLen2
);
900 result
=ucol_strcoll(c2
, str1
, -1, str2
, -1);
901 if(result
!=UCOL_LESS
) {
902 log_err("Error: %s was not less than %s: result=%d.\n", aescstrdup(str1
,-1), aescstrdup(str2
,-1), result
);
903 log_info("[%s] -> %s (%d, from rule)\n", aescstrdup(str1
,-1), sortKeyStr1
, sortKeyLen1
);
904 log_info("[%s] -> %s (%d, from rule)\n", aescstrdup(str2
,-1), sortKeyStr2
, sortKeyLen2
);
906 log_verbose("OK: %s was less than %s: result=%d.\n", aescstrdup(str1
,-1), aescstrdup(str2
,-1), result
);
907 log_verbose("[%s] -> %s (%d, from rule)\n", aescstrdup(str1
,-1), sortKeyStr1
, sortKeyLen1
);
908 log_verbose("[%s] -> %s (%d, from rule)\n", aescstrdup(str2
,-1), sortKeyStr2
, sortKeyLen2
);
918 TestOpenVsOpenRules ensures that collators from ucol_open and ucol_openRules
919 will generate identical sort keys
921 void TestOpenVsOpenRules(){
923 /* create an array of all the locales */
924 int32_t numLocales
= uloc_countAvailable();
925 int32_t sizeOfStdSet
;
927 UChar str
[41]; /* create an array of UChar of size maximum strSize + 1 */
934 int32_t sortKeyLen1
, sortKeyLen2
;
935 uint8_t *sortKey1
= NULL
, *sortKey2
= NULL
;
936 char sortKeyStr1
[512], sortKeyStr2
[512];
937 uint32_t sortKeyStrLen1
= sizeof(sortKeyStr1
) / sizeof(sortKeyStr1
[0]),
938 sortKeyStrLen2
= sizeof(sortKeyStr2
) / sizeof(sortKeyStr2
[0]);
945 UErrorCode err
= U_ZERO_ERROR
;
947 /* create a set of standard characters that aren't very interesting...
948 and then we can find some interesting ones later */
950 stdSet
= uset_open(0x61, 0x7A);
951 uset_addRange(stdSet
, 0x41, 0x5A);
952 uset_addRange(stdSet
, 0x30, 0x39);
953 sizeOfStdSet
= uset_size(stdSet
);
956 if(getTestOption(QUICK_OPTION
))
961 for(x
= 0; x
< numLocales
; x
+=adder
){
962 curLoc
= (char *)uloc_getAvailable(x
);
963 log_verbose("Processing %s\n", curLoc
);
965 /* create a collator the normal API way */
966 c1
= ucol_open(curLoc
, &err
);
967 if (U_FAILURE(err
)) {
968 log_err("ERROR: Normal collation creation failed with locale: %s : %s\n", curLoc
, myErrorName(err
));
973 rules
= ucol_getRules(c1
, &rulesLength
);
975 /* use those rules to create a collator from rules */
976 c2
= ucol_openRules(rules
, rulesLength
, UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &err
);
977 if (U_FAILURE(err
)) {
978 log_err("ERROR: Creating collator from rules failed with locale: %s : %s\n", curLoc
, myErrorName(err
));
982 uld
= ulocdata_open(curLoc
, &err
);
984 /*now that we have some collators, we get several strings */
986 for(y
= 0; y
< 5; y
++){
988 /* get a set of ALL the characters in this locale */
989 eSet
= ulocdata_getExemplarSet(uld
, NULL
, 0, ULOCDATA_ES_STANDARD
, &err
);
990 eSize
= uset_size(eSet
);
992 /* make a string with these characters in it */
993 strSize
= (rand()%40
) + 1;
995 for(z
= 0; z
< strSize
; z
++){
996 str
[z
] = uset_charAt(eSet
, rand()%eSize
);
999 /* change the set to only include 'abnormal' characters (not A-Z, a-z, 0-9 */
1000 uset_removeAll(eSet
, stdSet
);
1001 eSize
= uset_size(eSet
);
1003 /* if there are some non-normal characters left, put a few into the string, just to make sure we have some */
1005 str
[2%strSize
] = uset_charAt(eSet
, rand()%eSize
);
1006 str
[3%strSize
] = uset_charAt(eSet
, rand()%eSize
);
1007 str
[5%strSize
] = uset_charAt(eSet
, rand()%eSize
);
1008 str
[10%strSize
] = uset_charAt(eSet
, rand()%eSize
);
1009 str
[13%strSize
] = uset_charAt(eSet
, rand()%eSize
);
1011 /* terminate the string */
1012 str
[strSize
-1] = '\0';
1013 log_verbose("String used: %S\n", str
);
1015 /* get sort keys for both of them, and check that the keys are identicle */
1016 sortKeyLen1
= ucol_getSortKey(c1
, str
, u_strlen(str
), NULL
, 0);
1017 sortKey1
= (uint8_t*)malloc(sizeof(uint8_t) * (sortKeyLen1
+ 1));
1018 /*memset(sortKey1, 0xFE, sortKeyLen1);*/
1019 ucol_getSortKey(c1
, str
, u_strlen(str
), sortKey1
, sortKeyLen1
+ 1);
1020 ucol_sortKeyToString(c1
, sortKey1
, sortKeyStr1
, sortKeyStrLen1
);
1022 sortKeyLen2
= ucol_getSortKey(c2
, str
, u_strlen(str
), NULL
, 0);
1023 sortKey2
= (uint8_t*)malloc(sizeof(uint8_t) * (sortKeyLen2
+ 1));
1024 /*memset(sortKey2, 0xFE, sortKeyLen2);*/
1025 ucol_getSortKey(c2
, str
, u_strlen(str
), sortKey2
, sortKeyLen2
+ 1);
1026 ucol_sortKeyToString(c2
, sortKey2
, sortKeyStr2
, sortKeyStrLen2
);
1028 /* Check that the lengths are the same */
1029 if (sortKeyLen1
!= sortKeyLen2
) {
1030 log_err("ERROR : Sort key lengths %d and %d for text '%s' in locale '%s' do not match.\n",
1031 sortKeyLen1
, sortKeyLen2
, str
, curLoc
);
1034 /* check that the keys are the same */
1035 if (memcmp(sortKey1
, sortKey2
, sortKeyLen1
) != 0) {
1036 log_err("ERROR : Sort keys '%s' and '%s' for text '%s' in locale '%s' are not equivalent.\n",
1037 sortKeyStr1
, sortKeyStr2
, str
, curLoc
);
1040 /* clean up after each string */
1045 /* clean up after each locale */
1046 ulocdata_close(uld
);
1050 /* final clean up */
1054 ----------------------------------------------------------------------------
1055 ctor -- Tests the getSortKey
1059 uint8_t *sortk1
= NULL
, *sortk2
= NULL
, *sortk3
= NULL
, *sortkEmpty
= NULL
;
1060 int32_t sortklen
, osortklen
;
1062 UChar
*test1
, *test2
, *test3
;
1063 UErrorCode status
= U_ZERO_ERROR
;
1064 char toStringBuffer
[256], *resultP
;
1065 uint32_t toStringLen
=sizeof(toStringBuffer
)/sizeof(toStringBuffer
[0]);
1068 uint8_t s1
[] = { 0x9f, 0x00 };
1069 uint8_t s2
[] = { 0x61, 0x00 };
1072 strcmpResult
= strcmp((const char *)s1
, (const char *)s2
);
1073 log_verbose("strcmp(0x9f..., 0x61...) = %d\n", strcmpResult
);
1075 if(strcmpResult
<= 0) {
1076 log_err("ERR: expected strcmp(\"9f 00\", \"61 00\") to be >=0 (GREATER).. got %d. Calling strcmp() for sortkeys may not work! \n",
1081 log_verbose("testing SortKey begins...\n");
1082 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
1083 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
1084 /* col = ucol_open(NULL, &status); */
1085 col
= ucol_open("en_US", &status
);
1086 if (U_FAILURE(status
)) {
1087 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1092 if(ucol_getStrength(col
) != UCOL_DEFAULT_STRENGTH
)
1094 log_err("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
1096 /* Need to use identical strength */
1097 ucol_setAttribute(col
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &status
);
1099 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
1100 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
1101 test3
=(UChar
*)malloc(sizeof(UChar
) * 6);
1103 memset(test1
,0xFE, sizeof(UChar
)*6);
1104 memset(test2
,0xFE, sizeof(UChar
)*6);
1105 memset(test3
,0xFE, sizeof(UChar
)*6);
1108 u_uastrcpy(test1
, "Abcda");
1109 u_uastrcpy(test2
, "abcda");
1110 u_uastrcpy(test3
, "abcda");
1112 log_verbose("Use tertiary comparison level testing ....\n");
1114 sortklen
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1115 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1116 memset(sortk1
,0xFE, sortklen
);
1117 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortklen
+1);
1119 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1120 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1121 memset(sortk2
,0xFE, sortklen
);
1122 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortklen
+1);
1124 osortklen
= sortklen
;
1125 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test3
), NULL
, 0);
1126 sortk3
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1127 memset(sortk3
,0xFE, sortklen
);
1128 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk3
, sortklen
+1);
1130 doAssert( (sortklen
== osortklen
), "Sortkey length should be the same (abcda, abcda)");
1132 doAssert( (memcmp(sortk1
, sortk2
, sortklen
) > 0), "Result should be \"Abcda\" > \"abcda\"");
1133 doAssert( (memcmp(sortk2
, sortk1
, sortklen
) < 0), "Result should be \"abcda\" < \"Abcda\"");
1134 doAssert( (memcmp(sortk2
, sortk3
, sortklen
) == 0), "Result should be \"abcda\" == \"abcda\"");
1136 resultP
= ucol_sortKeyToString(col
, sortk3
, toStringBuffer
, toStringLen
);
1137 doAssert( (resultP
!= 0), "sortKeyToString failed!");
1139 #if 1 /* verobse log of sortkeys */
1145 strcpy(junk2
, "abcda[2] ");
1146 strcpy(junk3
, " abcda[3] ");
1148 for(i
=0;i
<sortklen
;i
++)
1150 sprintf(junk2
+strlen(junk2
), "%02X ",(int)( 0xFF & sortk2
[i
]));
1151 sprintf(junk3
+strlen(junk3
), "%02X ",(int)( 0xFF & sortk3
[i
]));
1154 log_verbose("%s\n", junk2
);
1155 log_verbose("%s\n", junk3
);
1163 log_verbose("Use secondary comparision level testing ...\n");
1164 ucol_setStrength(col
, UCOL_SECONDARY
);
1165 sortklen
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1166 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1167 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortklen
+1);
1168 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1169 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1170 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortklen
+1);
1172 doAssert( !(memcmp(sortk1
, sortk2
, sortklen
) > 0), "Result should be \"Abcda\" == \"abcda\"");
1173 doAssert( !(memcmp(sortk2
, sortk1
, sortklen
) < 0), "Result should be \"abcda\" == \"Abcda\"");
1174 doAssert( (memcmp(sortk1
, sortk2
, sortklen
) == 0), "Result should be \"abcda\" == \"abcda\"");
1176 log_verbose("getting sortkey for an empty string\n");
1177 ucol_setAttribute(col
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
1178 sortklen
= ucol_getSortKey(col
, test1
, 0, NULL
, 0);
1179 sortkEmpty
= (uint8_t*)malloc(sizeof(uint8_t) * sortklen
+1);
1180 sortklen
= ucol_getSortKey(col
, test1
, 0, sortkEmpty
, sortklen
+1);
1181 if(sortklen
!= 3 || sortkEmpty
[0] != 1 || sortkEmpty
[0] != 1 || sortkEmpty
[2] != 0) {
1182 log_err("Empty string generated wrong sortkey!\n");
1186 log_verbose("testing passing invalid string\n");
1187 sortklen
= ucol_getSortKey(col
, NULL
, 0, NULL
, 0);
1189 log_err("Invalid string didn't return sortkey size of 0\n");
1193 log_verbose("testing sortkey ends...\n");
1204 uint8_t *sortk1
, *sortk2
, *sortk3
;
1205 int32_t sortk1len
, sortk2len
, sortk3len
;
1207 UChar
*test1
, *test2
, *test3
;
1208 UErrorCode status
= U_ZERO_ERROR
;
1209 log_verbose("testing getHashCode begins...\n");
1210 col
= ucol_open("en_US", &status
);
1211 if (U_FAILURE(status
)) {
1212 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1215 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
1216 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
1217 test3
=(UChar
*)malloc(sizeof(UChar
) * 6);
1218 u_uastrcpy(test1
, "Abcda");
1219 u_uastrcpy(test2
, "abcda");
1220 u_uastrcpy(test3
, "abcda");
1222 log_verbose("Use tertiary comparison level testing ....\n");
1223 sortk1len
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1224 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk1len
+1));
1225 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortk1len
+1);
1226 sortk2len
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1227 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk2len
+1));
1228 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortk2len
+1);
1229 sortk3len
=ucol_getSortKey(col
, test2
, u_strlen(test3
), NULL
, 0);
1230 sortk3
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk3len
+1));
1231 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk3
, sortk3len
+1);
1234 log_verbose("ucol_hashCode() testing ...\n");
1236 doAssert( ucol_keyHashCode(sortk1
, sortk1len
) != ucol_keyHashCode(sortk2
, sortk2len
), "Hash test1 result incorrect" );
1237 doAssert( !(ucol_keyHashCode(sortk1
, sortk1len
) == ucol_keyHashCode(sortk2
, sortk2len
)), "Hash test2 result incorrect" );
1238 doAssert( ucol_keyHashCode(sortk2
, sortk2len
) == ucol_keyHashCode(sortk3
, sortk3len
), "Hash result not equal" );
1240 log_verbose("hashCode tests end.\n");
1252 *----------------------------------------------------------------------------
1253 * Tests the UCollatorElements API.
1259 int32_t order1
, order2
, order3
;
1260 UChar
*testString1
, *testString2
;
1262 UCollationElements
*iterator1
, *iterator2
, *iterator3
;
1263 UErrorCode status
= U_ZERO_ERROR
;
1264 log_verbose("testing UCollatorElements begins...\n");
1265 col
= ucol_open("en_US", &status
);
1266 ucol_setAttribute(col
, UCOL_NORMALIZATION_MODE
, UCOL_OFF
, &status
);
1267 if (U_FAILURE(status
)) {
1268 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1272 testString1
=(UChar
*)malloc(sizeof(UChar
) * 150);
1273 testString2
=(UChar
*)malloc(sizeof(UChar
) * 150);
1274 u_uastrcpy(testString1
, "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
1275 u_uastrcpy(testString2
, "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
1277 log_verbose("Constructors and comparison testing....\n");
1279 iterator1
= ucol_openElements(col
, testString1
, u_strlen(testString1
), &status
);
1280 if(U_FAILURE(status
)) {
1281 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1285 else{ log_verbose("PASS: Default collationElement iterator1 creation passed\n");}
1287 iterator2
= ucol_openElements(col
, testString1
, u_strlen(testString1
), &status
);
1288 if(U_FAILURE(status
)) {
1289 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1293 else{ log_verbose("PASS: Default collationElement iterator2 creation passed\n");}
1295 iterator3
= ucol_openElements(col
, testString2
, u_strlen(testString2
), &status
);
1296 if(U_FAILURE(status
)) {
1297 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1301 else{ log_verbose("PASS: Default collationElement iterator3 creation passed\n");}
1303 offset
=ucol_getOffset(iterator1
);
1304 ucol_setOffset(iterator1
, 6, &status
);
1305 if (U_FAILURE(status
)) {
1306 log_err("Error in setOffset for UCollatorElements iterator.: %s\n", myErrorName(status
));
1309 if(ucol_getOffset(iterator1
)==6)
1310 log_verbose("setOffset and getOffset working fine\n");
1312 log_err("error in set and get Offset got %d instead of 6\n", ucol_getOffset(iterator1
));
1315 ucol_setOffset(iterator1
, 0, &status
);
1316 order1
= ucol_next(iterator1
, &status
);
1317 if (U_FAILURE(status
)) {
1318 log_err("Somehow ran out of memory stepping through the iterator1.: %s\n", myErrorName(status
));
1321 order2
=ucol_getOffset(iterator2
);
1322 doAssert((order1
!= order2
), "The first iterator advance failed");
1323 order2
= ucol_next(iterator2
, &status
);
1324 if (U_FAILURE(status
)) {
1325 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1328 order3
= ucol_next(iterator3
, &status
);
1329 if (U_FAILURE(status
)) {
1330 log_err("Somehow ran out of memory stepping through the iterator3.: %s\n", myErrorName(status
));
1334 doAssert((order1
== order2
), "The second iterator advance failed should be the same as first one");
1336 doAssert( (ucol_primaryOrder(order1
) == ucol_primaryOrder(order3
)), "The primary orders should be identical");
1337 doAssert( (ucol_secondaryOrder(order1
) == ucol_secondaryOrder(order3
)), "The secondary orders should be identical");
1338 doAssert( (ucol_tertiaryOrder(order1
) == ucol_tertiaryOrder(order3
)), "The tertiary orders should be identical");
1340 order1
=ucol_next(iterator1
, &status
);
1341 if (U_FAILURE(status
)) {
1342 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1345 order3
=ucol_next(iterator3
, &status
);
1346 if (U_FAILURE(status
)) {
1347 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1350 doAssert( (ucol_primaryOrder(order1
) == ucol_primaryOrder(order3
)), "The primary orders should be identical");
1351 doAssert( (ucol_tertiaryOrder(order1
) != ucol_tertiaryOrder(order3
)), "The tertiary orders should be different");
1353 order1
=ucol_next(iterator1
, &status
);
1354 if (U_FAILURE(status
)) {
1355 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1358 order3
=ucol_next(iterator3
, &status
);
1359 if (U_FAILURE(status
)) {
1360 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1363 /* this here, my friends, is either pure lunacy or something so obsolete that even it's mother
1364 * doesn't care about it. Essentialy, this test complains if secondary values for 'I' and '_'
1365 * are the same. According to the UCA, this is not true. Therefore, remove the test.
1366 * Besides, if primary strengths for two code points are different, it doesn't matter one bit
1367 * what is the relation between secondary or any other strengths.
1368 * killed by weiv 06/11/2002.
1371 doAssert( ((order1 & UCOL_SECONDARYMASK) != (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be different");
1373 doAssert( (order1
!= UCOL_NULLORDER
), "Unexpected end of iterator reached");
1377 ucol_closeElements(iterator1
);
1378 ucol_closeElements(iterator2
);
1379 ucol_closeElements(iterator3
);
1382 log_verbose("testing CollationElementIterator ends...\n");
1385 void TestGetLocale() {
1386 UErrorCode status
= U_ZERO_ERROR
;
1387 const char *rules
= "&a<x<y<z";
1388 UChar rlz
[256] = {0};
1389 uint32_t rlzLen
= u_unescape(rules
, rlz
, 256);
1391 UCollator
*coll
= NULL
;
1392 const char *locale
= NULL
;
1396 /* Now that the collation tree is separate, actual==valid at all times. [alan] */
1397 static const struct {
1398 const char* requestedLocale
;
1399 const char* validLocale
;
1400 const char* actualLocale
;
1402 { "sr_RS", "sr_Cyrl_RS", "sr" },
1403 { "sh_YU", "sr_Latn_RS", "sr_Latn" }, /* was sh, then aliased to hr, now sr_Latn via import per cldrbug 5647: */
1404 { "en_BE_FOO", "en_BE", "root" },
1405 { "de_DE_NONEXISTANT", "de_DE", "de" }
1408 /* test opening collators for different locales */
1409 for(i
= 0; i
<sizeof(testStruct
)/sizeof(testStruct
[0]); i
++) {
1410 status
= U_ZERO_ERROR
;
1411 coll
= ucol_open(testStruct
[i
].requestedLocale
, &status
);
1412 if(U_FAILURE(status
)) {
1413 log_err_status(status
, "Failed to open collator for %s with %s\n", testStruct
[i
].requestedLocale
, u_errorName(status
));
1417 locale
= ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
);
1418 if(strcmp(locale
, testStruct
[i
].requestedLocale
) != 0) {
1419 log_err("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].requestedLocale
, locale
);
1421 locale
= ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
);
1422 if(strcmp(locale
, testStruct
[i
].validLocale
) != 0) {
1423 log_err("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].validLocale
, locale
);
1425 locale
= ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
);
1426 if(strcmp(locale
, testStruct
[i
].actualLocale
) != 0) {
1427 log_err("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].actualLocale
, locale
);
1432 /* completely non-existant locale for collator should get a default collator */
1434 UCollator
*defaultColl
= ucol_open(NULL
, &status
);
1435 coll
= ucol_open("blahaha", &status
);
1436 if(U_SUCCESS(status
)) {
1437 if(strcmp(ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
), "blahaha")) {
1438 log_err("Nonexisting locale didn't preserve the requested locale\n");
1440 if(strcmp(ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
),
1441 ucol_getLocaleByType(defaultColl
, ULOC_VALID_LOCALE
, &status
))) {
1442 log_err("Valid locale for nonexisting locale locale collator differs "
1443 "from valid locale for default collator\n");
1445 if(strcmp(ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
),
1446 ucol_getLocaleByType(defaultColl
, ULOC_ACTUAL_LOCALE
, &status
))) {
1447 log_err("Actual locale for nonexisting locale locale collator differs "
1448 "from actual locale for default collator\n");
1451 ucol_close(defaultColl
);
1453 log_data_err("Couldn't open collators\n");
1459 /* collator instantiated from rules should have all three locales NULL */
1460 coll
= ucol_openRules(rlz
, rlzLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, NULL
, &status
);
1461 locale
= ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
);
1462 if(locale
!= NULL
) {
1463 log_err("For collator instantiated from rules, requested locale returned %s instead of NULL\n", locale
);
1465 locale
= ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
);
1466 if(locale
!= NULL
) {
1467 log_err("For collator instantiated from rules, valid locale returned %s instead of NULL\n", locale
);
1469 locale
= ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
);
1470 if(locale
!= NULL
) {
1471 log_err("For collator instantiated from rules, actual locale returned %s instead of NULL\n", locale
);
1481 count
=ucol_countAvailable();
1482 /* use something sensible w/o hardcoding the count */
1484 log_err("Error in countAvailable(), it returned %d\n", count
);
1487 log_verbose("PASS: countAvailable() successful, it returned %d\n", count
);
1489 for(i
=0;i
<count
;i
++)
1490 log_verbose("%s\n", ucol_getAvailable(i
));
1497 const char *original
;
1501 static int compare_teststruct(const void *string1
, const void *string2
) {
1502 return(strcmp((const char *)((struct teststruct
*)string1
)->key
, (const char *)((struct teststruct
*)string2
)->key
));
1506 UErrorCode status
= U_ZERO_ERROR
;
1508 UCollator
*coll
= ucol_open("sh", &status
);
1510 uint8_t sortkey
[512], lower
[512], upper
[512];
1513 static const char * const test
[] = {
1517 "j\\u00F6hn sm\\u00EFth",
1518 "J\\u00F6hn Sm\\u00EFth",
1519 "J\\u00D6HN SM\\u00CFTH",
1524 struct teststruct tests
[] = {
1525 {"\\u010CAKI MIHALJ" } ,
1526 {"\\u010CAKI MIHALJ" } ,
1527 {"\\u010CAKI PIRO\\u0160KA" },
1528 {"\\u010CABAI ANDRIJA" } ,
1529 {"\\u010CABAI LAJO\\u0160" } ,
1530 {"\\u010CABAI MARIJA" } ,
1531 {"\\u010CABAI STEVAN" } ,
1532 {"\\u010CABAI STEVAN" } ,
1533 {"\\u010CABARKAPA BRANKO" } ,
1534 {"\\u010CABARKAPA MILENKO" } ,
1535 {"\\u010CABARKAPA MIROSLAV" } ,
1536 {"\\u010CABARKAPA SIMO" } ,
1537 {"\\u010CABARKAPA STANKO" } ,
1538 {"\\u010CABARKAPA TAMARA" } ,
1539 {"\\u010CABARKAPA TOMA\\u0160" } ,
1540 {"\\u010CABDARI\\u0106 NIKOLA" } ,
1541 {"\\u010CABDARI\\u0106 ZORICA" } ,
1542 {"\\u010CABI NANDOR" } ,
1543 {"\\u010CABOVI\\u0106 MILAN" } ,
1544 {"\\u010CABRADI AGNEZIJA" } ,
1545 {"\\u010CABRADI IVAN" } ,
1546 {"\\u010CABRADI JELENA" } ,
1547 {"\\u010CABRADI LJUBICA" } ,
1548 {"\\u010CABRADI STEVAN" } ,
1549 {"\\u010CABRDA MARTIN" } ,
1550 {"\\u010CABRILO BOGDAN" } ,
1551 {"\\u010CABRILO BRANISLAV" } ,
1552 {"\\u010CABRILO LAZAR" } ,
1553 {"\\u010CABRILO LJUBICA" } ,
1554 {"\\u010CABRILO SPASOJA" } ,
1555 {"\\u010CADE\\u0160 ZDENKA" } ,
1556 {"\\u010CADESKI BLAGOJE" } ,
1557 {"\\u010CADOVSKI VLADIMIR" } ,
1558 {"\\u010CAGLJEVI\\u0106 TOMA" } ,
1559 {"\\u010CAGOROVI\\u0106 VLADIMIR" } ,
1560 {"\\u010CAJA VANKA" } ,
1561 {"\\u010CAJI\\u0106 BOGOLJUB" } ,
1562 {"\\u010CAJI\\u0106 BORISLAV" } ,
1563 {"\\u010CAJI\\u0106 RADOSLAV" } ,
1564 {"\\u010CAK\\u0160IRAN MILADIN" } ,
1565 {"\\u010CAKAN EUGEN" } ,
1566 {"\\u010CAKAN EVGENIJE" } ,
1567 {"\\u010CAKAN IVAN" } ,
1568 {"\\u010CAKAN JULIJAN" } ,
1569 {"\\u010CAKAN MIHAJLO" } ,
1570 {"\\u010CAKAN STEVAN" } ,
1571 {"\\u010CAKAN VLADIMIR" } ,
1572 {"\\u010CAKAN VLADIMIR" } ,
1573 {"\\u010CAKAN VLADIMIR" } ,
1574 {"\\u010CAKARA ANA" } ,
1575 {"\\u010CAKAREVI\\u0106 MOMIR" } ,
1576 {"\\u010CAKAREVI\\u0106 NEDELJKO" } ,
1577 {"\\u010CAKI \\u0160ANDOR" } ,
1578 {"\\u010CAKI AMALIJA" } ,
1579 {"\\u010CAKI ANDRA\\u0160" } ,
1580 {"\\u010CAKI LADISLAV" } ,
1581 {"\\u010CAKI LAJO\\u0160" } ,
1582 {"\\u010CAKI LASLO" } ,
1587 int32_t i
= 0, j
= 0, k
= 0, buffSize
= 0, skSize
= 0, lowerSize
= 0, upperSize
= 0;
1588 int32_t arraySize
= sizeof(tests
)/sizeof(tests
[0]);
1590 if(U_SUCCESS(status
) && coll
) {
1591 for(i
= 0; i
<arraySize
; i
++) {
1592 buffSize
= u_unescape(tests
[i
].original
, buffer
, 512);
1593 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, tests
[i
].key
, 512);
1596 qsort(tests
, arraySize
, sizeof(struct teststruct
), compare_teststruct
);
1598 for(i
= 0; i
< arraySize
-1; i
++) {
1599 for(j
= i
+1; j
< arraySize
; j
++) {
1600 lowerSize
= ucol_getBound(tests
[i
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1601 upperSize
= ucol_getBound(tests
[j
].key
, -1, UCOL_BOUND_UPPER
, 1, upper
, 512, &status
);
1602 for(k
= i
; k
<= j
; k
++) {
1603 if(strcmp((const char *)lower
, (const char *)tests
[k
].key
) > 0) {
1604 log_err("Problem with lower! j = %i (%s vs %s)\n", k
, tests
[k
].original
, tests
[i
].original
);
1606 if(strcmp((const char *)upper
, (const char *)tests
[k
].key
) <= 0) {
1607 log_err("Problem with upper! j = %i (%s vs %s)\n", k
, tests
[k
].original
, tests
[j
].original
);
1615 for(i
= 0; i
< 1000; i
++) {
1616 lowerRND
= (rand()/(RAND_MAX
/arraySize
));
1617 upperRND
= lowerRND
+ (rand()/(RAND_MAX
/(arraySize
-lowerRND
)));
1619 lowerSize
= ucol_getBound(tests
[lowerRND
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1620 upperSize
= ucol_getBound(tests
[upperRND
].key
, -1, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1622 for(j
= lowerRND
; j
<=upperRND
; j
++) {
1623 if(strcmp(lower
, tests
[j
].key
) > 0) {
1624 log_err("Problem with lower! j = %i (%s vs %s)\n", j
, tests
[j
].original
, tests
[lowerRND
].original
);
1626 if(strcmp(upper
, tests
[j
].key
) <= 0) {
1627 log_err("Problem with upper! j = %i (%s vs %s)\n", j
, tests
[j
].original
, tests
[upperRND
].original
);
1637 for(i
= 0; i
<sizeof(test
)/sizeof(test
[0]); i
++) {
1638 buffSize
= u_unescape(test
[i
], buffer
, 512);
1639 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, sortkey
, 512);
1640 lowerSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1641 upperSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1642 for(j
= i
+1; j
<sizeof(test
)/sizeof(test
[0]); j
++) {
1643 buffSize
= u_unescape(test
[j
], buffer
, 512);
1644 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, sortkey
, 512);
1645 if(strcmp((const char *)lower
, (const char *)sortkey
) > 0) {
1646 log_err("Problem with lower! i = %i, j = %i (%s vs %s)\n", i
, j
, test
[i
], test
[j
]);
1648 if(strcmp((const char *)upper
, (const char *)sortkey
) <= 0) {
1649 log_err("Problem with upper! i = %i, j = %i (%s vs %s)\n", i
, j
, test
[i
], test
[j
]);
1655 log_data_err("Couldn't open collator\n");
1660 static void doOverrunTest(UCollator
*coll
, const UChar
*uString
, int32_t strLen
) {
1661 int32_t skLen
= 0, skLen2
= 0;
1662 uint8_t sortKey
[256];
1664 uint8_t filler
= 0xFF;
1666 skLen
= ucol_getSortKey(coll
, uString
, strLen
, NULL
, 0);
1668 for(i
= 0; i
< skLen
; i
++) {
1669 memset(sortKey
, filler
, 256);
1670 skLen2
= ucol_getSortKey(coll
, uString
, strLen
, sortKey
, i
);
1671 if(skLen
!= skLen2
) {
1672 log_err("For buffer size %i, got different sortkey length. Expected %i got %i\n", i
, skLen
, skLen2
);
1674 for(j
= i
; j
< 256; j
++) {
1675 if(sortKey
[j
] != filler
) {
1676 log_err("Something run over index %i\n", j
);
1683 /* j1865 reports that if a shorter buffer is passed to
1684 * to get sort key, a buffer overrun happens in some
1685 * cases. This test tries to check this.
1687 void TestSortKeyBufferOverrun(void) {
1688 UErrorCode status
= U_ZERO_ERROR
;
1689 const char* cString
= "A very Merry liTTle-lamB..";
1692 UCollator
*coll
= ucol_open("root", &status
);
1693 strLen
= u_unescape(cString
, uString
, 256);
1695 if(U_SUCCESS(status
)) {
1696 log_verbose("testing non ignorable\n");
1697 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
1698 doOverrunTest(coll
, uString
, strLen
);
1700 log_verbose("testing shifted\n");
1701 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &status
);
1702 doOverrunTest(coll
, uString
, strLen
);
1704 log_verbose("testing shifted quaternary\n");
1705 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &status
);
1706 doOverrunTest(coll
, uString
, strLen
);
1708 log_verbose("testing with french secondaries\n");
1709 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
1710 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
1711 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
1712 doOverrunTest(coll
, uString
, strLen
);
1718 static void TestAttribute()
1720 UErrorCode error
= U_ZERO_ERROR
;
1721 UCollator
*coll
= ucol_open(NULL
, &error
);
1723 if (U_FAILURE(error
)) {
1724 log_err_status(error
, "Creation of default collator failed\n");
1728 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_OFF
, &error
);
1729 if (ucol_getAttribute(coll
, UCOL_FRENCH_COLLATION
, &error
) != UCOL_OFF
||
1731 log_err_status(error
, "Setting and retrieving of the french collation failed\n");
1734 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &error
);
1735 if (ucol_getAttribute(coll
, UCOL_FRENCH_COLLATION
, &error
) != UCOL_ON
||
1737 log_err_status(error
, "Setting and retrieving of the french collation failed\n");
1740 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &error
);
1741 if (ucol_getAttribute(coll
, UCOL_ALTERNATE_HANDLING
, &error
) != UCOL_SHIFTED
||
1743 log_err_status(error
, "Setting and retrieving of the alternate handling failed\n");
1746 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &error
);
1747 if (ucol_getAttribute(coll
, UCOL_ALTERNATE_HANDLING
, &error
) != UCOL_NON_IGNORABLE
||
1749 log_err_status(error
, "Setting and retrieving of the alternate handling failed\n");
1752 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, &error
);
1753 if (ucol_getAttribute(coll
, UCOL_CASE_FIRST
, &error
) != UCOL_LOWER_FIRST
||
1755 log_err_status(error
, "Setting and retrieving of the case first attribute failed\n");
1758 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, &error
);
1759 if (ucol_getAttribute(coll
, UCOL_CASE_FIRST
, &error
) != UCOL_UPPER_FIRST
||
1761 log_err_status(error
, "Setting and retrieving of the case first attribute failed\n");
1764 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_ON
, &error
);
1765 if (ucol_getAttribute(coll
, UCOL_CASE_LEVEL
, &error
) != UCOL_ON
||
1767 log_err_status(error
, "Setting and retrieving of the case level attribute failed\n");
1770 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_OFF
, &error
);
1771 if (ucol_getAttribute(coll
, UCOL_CASE_LEVEL
, &error
) != UCOL_OFF
||
1773 log_err_status(error
, "Setting and retrieving of the case level attribute failed\n");
1776 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &error
);
1777 if (ucol_getAttribute(coll
, UCOL_NORMALIZATION_MODE
, &error
) != UCOL_ON
||
1779 log_err_status(error
, "Setting and retrieving of the normalization on/off attribute failed\n");
1782 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_OFF
, &error
);
1783 if (ucol_getAttribute(coll
, UCOL_NORMALIZATION_MODE
, &error
) != UCOL_OFF
||
1785 log_err_status(error
, "Setting and retrieving of the normalization on/off attribute failed\n");
1788 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_PRIMARY
, &error
);
1789 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_PRIMARY
||
1791 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1794 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_SECONDARY
, &error
);
1795 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_SECONDARY
||
1797 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1800 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &error
);
1801 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_TERTIARY
||
1803 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1806 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &error
);
1807 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_QUATERNARY
||
1809 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1812 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &error
);
1813 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_IDENTICAL
||
1815 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1821 void TestGetTailoredSet() {
1824 const char *tests
[20];
1827 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1828 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1831 int32_t i
= 0, j
= 0;
1832 UErrorCode status
= U_ZERO_ERROR
;
1835 UCollator
*coll
= NULL
;
1837 int32_t buffLen
= 0;
1840 for(i
= 0; i
< sizeof(setTest
)/sizeof(setTest
[0]); i
++) {
1841 buffLen
= u_unescape(setTest
[i
].rules
, buff
, 1024);
1842 coll
= ucol_openRules(buff
, buffLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, &pError
, &status
);
1843 if(U_SUCCESS(status
)) {
1844 set
= ucol_getTailoredSet(coll
, &status
);
1845 if(uset_size(set
) != setTest
[i
].testsize
) {
1846 log_err("Tailored set size different (%d) than expected (%d)\n", uset_size(set
), setTest
[i
].testsize
);
1848 for(j
= 0; j
< setTest
[i
].testsize
; j
++) {
1849 buffLen
= u_unescape(setTest
[i
].tests
[j
], buff
, 1024);
1850 if(!uset_containsString(set
, buff
, buffLen
)) {
1851 log_err("Tailored set doesn't contain %s... It should\n", setTest
[i
].tests
[j
]);
1856 log_err_status(status
, "Couldn't open collator with rules %s\n", setTest
[i
].rules
);
1862 static int tMemCmp(const uint8_t *first
, const uint8_t *second
) {
1863 int32_t firstLen
= (int32_t)strlen((const char *)first
);
1864 int32_t secondLen
= (int32_t)strlen((const char *)second
);
1865 return memcmp(first
, second
, uprv_min(firstLen
, secondLen
));
1867 static const char * strengthsC
[] = {
1875 void TestMergeSortKeys(void) {
1876 UErrorCode status
= U_ZERO_ERROR
;
1877 UCollator
*coll
= ucol_open("en", &status
);
1878 if(U_SUCCESS(status
)) {
1880 const char* cases
[] = {
1885 uint32_t casesSize
= sizeof(cases
)/sizeof(cases
[0]);
1886 const char* prefix
= "foo";
1887 const char* suffix
= "egg";
1888 char outBuff1
[256], outBuff2
[256];
1890 uint8_t **sortkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1891 uint8_t **mergedPrefixkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1892 uint8_t **mergedSuffixkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1893 uint32_t *sortKeysLen
= (uint32_t *)malloc(casesSize
*sizeof(uint32_t));
1894 uint8_t prefixKey
[256], suffixKey
[256];
1895 uint32_t prefixKeyLen
= 0, suffixKeyLen
= 0, i
= 0;
1897 uint32_t unescapedLen
= 0, l1
= 0, l2
= 0;
1898 UColAttributeValue strength
;
1900 log_verbose("ucol_mergeSortkeys test\n");
1901 log_verbose("Testing order of the test cases\n");
1902 genericLocaleStarter("en", cases
, casesSize
);
1904 for(i
= 0; i
<casesSize
; i
++) {
1905 sortkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1906 mergedPrefixkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1907 mergedSuffixkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1910 unescapedLen
= u_unescape(prefix
, buffer
, 256);
1911 prefixKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, prefixKey
, 256);
1913 unescapedLen
= u_unescape(suffix
, buffer
, 256);
1914 suffixKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, suffixKey
, 256);
1916 log_verbose("Massaging data with prefixes and different strengths\n");
1917 strength
= UCOL_PRIMARY
;
1918 while(strength
<= UCOL_IDENTICAL
) {
1919 log_verbose("Strength %s\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1920 ucol_setAttribute(coll
, UCOL_STRENGTH
, strength
, &status
);
1921 for(i
= 0; i
<casesSize
; i
++) {
1922 unescapedLen
= u_unescape(cases
[i
], buffer
, 256);
1923 sortKeysLen
[i
] = ucol_getSortKey(coll
, buffer
, unescapedLen
, sortkeys
[i
], 256);
1924 ucol_mergeSortkeys(prefixKey
, prefixKeyLen
, sortkeys
[i
], sortKeysLen
[i
], mergedPrefixkeys
[i
], 256);
1925 ucol_mergeSortkeys(sortkeys
[i
], sortKeysLen
[i
], suffixKey
, suffixKeyLen
, mergedSuffixkeys
[i
], 256);
1927 if(tMemCmp(mergedPrefixkeys
[i
-1], mergedPrefixkeys
[i
]) >= 0) {
1928 log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1930 ucol_sortKeyToString(coll
, mergedPrefixkeys
[i
-1], outBuff1
, l1
),
1931 ucol_sortKeyToString(coll
, mergedPrefixkeys
[i
], outBuff2
, l2
));
1933 if(tMemCmp(mergedSuffixkeys
[i
-1], mergedSuffixkeys
[i
]) >= 0) {
1934 log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1936 ucol_sortKeyToString(coll
, mergedSuffixkeys
[i
-1], outBuff1
, l1
),
1937 ucol_sortKeyToString(coll
, mergedSuffixkeys
[i
], outBuff2
, l2
));
1941 if(strength
== UCOL_QUATERNARY
) {
1942 strength
= UCOL_IDENTICAL
;
1949 uint8_t smallBuf
[3];
1950 uint32_t reqLen
= 0;
1951 log_verbose("testing buffer overflow\n");
1952 reqLen
= ucol_mergeSortkeys(prefixKey
, prefixKeyLen
, suffixKey
, suffixKeyLen
, smallBuf
, 3);
1953 if(reqLen
!= (prefixKeyLen
+suffixKeyLen
)) {
1954 log_err("Wrong preflight size for merged sortkey\n");
1960 uint8_t emptyKey
[20], abcKey
[50], mergedKey
[100];
1961 int32_t emptyKeyLen
= 0, abcKeyLen
= 0, mergedKeyLen
= 0;
1963 log_verbose("testing merging with sortkeys generated for empty strings\n");
1964 emptyKeyLen
= ucol_getSortKey(coll
, &empty
, 0, emptyKey
, 20);
1965 unescapedLen
= u_unescape(cases
[0], buffer
, 256);
1966 abcKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, abcKey
, 50);
1967 mergedKeyLen
= ucol_mergeSortkeys(emptyKey
, emptyKeyLen
, abcKey
, abcKeyLen
, mergedKey
, 100);
1968 if(mergedKey
[0] != 2) {
1969 log_err("Empty sortkey didn't produce a level separator\n");
1971 /* try with zeros */
1972 mergedKeyLen
= ucol_mergeSortkeys(emptyKey
, 0, abcKey
, abcKeyLen
, mergedKey
, 100);
1973 if(mergedKeyLen
!= 0 || mergedKey
[0] != 0) {
1974 log_err("Empty key didn't produce null mergedKey\n");
1976 mergedKeyLen
= ucol_mergeSortkeys(abcKey
, abcKeyLen
, emptyKey
, 0, mergedKey
, 100);
1977 if(mergedKeyLen
!= 0 || mergedKey
[0] != 0) {
1978 log_err("Empty key didn't produce null mergedKey\n");
1983 for(i
= 0; i
<casesSize
; i
++) {
1985 free(mergedPrefixkeys
[i
]);
1986 free(mergedSuffixkeys
[i
]);
1989 free(mergedPrefixkeys
);
1990 free(mergedSuffixkeys
);
1993 /* need to finish this up */
1995 log_data_err("Couldn't open collator");
1998 static void TestShortString(void)
2002 const char *expectedOutput
;
2004 UErrorCode expectedStatus
;
2005 int32_t expectedOffset
;
2006 uint32_t expectedIdentifier
;
2009 * The following expectedOutput contains a collation weight (2700 from UCA 6.0)
2010 * which is the primary weight for the T character (U+0041) in the input.
2011 * When that character gets a different weight in FractionalUCA.txt,
2012 * the expectedOutput needs to be adjusted.
2013 * That is, when we upgrade to a new UCA version or change collation
2014 * in such a way that the absolute weight for 'A' changes,
2015 * we will get a test failure here and need to adjust the test case.
2017 {"LDE_RDE_KPHONEBOOK_T0041_ZLATN","B2700_KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING
, 0, 0 },
2019 {"LEN_RUS_NO_AS_S4","AS_LROOT_NO_S4", NULL
, U_USING_DEFAULT_WARNING
, 0, 0 },
2020 {"LDE_VPHONEBOOK_EO_SI","EO_KPHONEBOOK_LDE_SI", "de@collation=phonebook", U_ZERO_ERROR
, 0, 0 },
2021 {"LDE_Kphonebook","KPHONEBOOK_LDE", "de@collation=phonebook", U_ZERO_ERROR
, 0, 0 },
2022 {"Xqde_DE@collation=phonebookq_S3_EX","KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING
, 0, 0 },
2023 {"LFR_FO", "FO_LROOT", NULL
, U_USING_DEFAULT_WARNING
, 0, 0 },
2024 {"SO_LX_AS", "", NULL
, U_ILLEGAL_ARGUMENT_ERROR
, 8, 0 },
2025 {"S3_ASS_MMM", "", NULL
, U_ILLEGAL_ARGUMENT_ERROR
, 5, 0 }
2029 UCollator
*coll
= NULL
, *fromNormalized
= NULL
;
2030 UParseError parseError
;
2031 UErrorCode status
= U_ZERO_ERROR
;
2032 char fromShortBuffer
[256], normalizedBuffer
[256], fromNormalizedBuffer
[256];
2033 const char* locale
= NULL
;
2036 for(i
= 0; i
< sizeof(testCases
)/sizeof(testCases
[0]); i
++) {
2037 status
= U_ZERO_ERROR
;
2038 if(testCases
[i
].locale
) {
2039 locale
= testCases
[i
].locale
;
2044 coll
= ucol_openFromShortString(testCases
[i
].input
, FALSE
, &parseError
, &status
);
2045 if(status
!= testCases
[i
].expectedStatus
) {
2046 log_err_status(status
, "Got status '%s' that is different from expected '%s' for '%s'\n",
2047 u_errorName(status
), u_errorName(testCases
[i
].expectedStatus
), testCases
[i
].input
);
2051 if(U_SUCCESS(status
)) {
2052 ucol_getShortDefinitionString(coll
, locale
, fromShortBuffer
, 256, &status
);
2054 if(strcmp(fromShortBuffer
, testCases
[i
].expectedOutput
)) {
2055 log_err("Got short string '%s' from the collator. Expected '%s' for input '%s'\n",
2056 fromShortBuffer
, testCases
[i
].expectedOutput
, testCases
[i
].input
);
2059 ucol_normalizeShortDefinitionString(testCases
[i
].input
, normalizedBuffer
, 256, &parseError
, &status
);
2060 fromNormalized
= ucol_openFromShortString(normalizedBuffer
, FALSE
, &parseError
, &status
);
2061 ucol_getShortDefinitionString(fromNormalized
, locale
, fromNormalizedBuffer
, 256, &status
);
2063 if(strcmp(fromShortBuffer
, fromNormalizedBuffer
)) {
2064 log_err("Strings obtained from collators instantiated by short string ('%s') and from normalized string ('%s') differ\n",
2065 fromShortBuffer
, fromNormalizedBuffer
);
2069 if(!ucol_equals(coll
, fromNormalized
)) {
2070 log_err("Collator from short string ('%s') differs from one obtained through a normalized version ('%s')\n",
2071 testCases
[i
].input
, normalizedBuffer
);
2074 ucol_close(fromNormalized
);
2078 if(parseError
.offset
!= testCases
[i
].expectedOffset
) {
2079 log_err("Got parse error offset %i, but expected %i instead for '%s'\n",
2080 parseError
.offset
, testCases
[i
].expectedOffset
, testCases
[i
].input
);
2088 doSetsTest(const char *locale
, const USet
*ref
, USet
*set
, const char* inSet
, const char* outSet
, UErrorCode
*status
) {
2093 bufLen
= u_unescape(inSet
, buffer
, 512);
2094 uset_applyPattern(set
, buffer
, bufLen
, 0, status
);
2095 if(U_FAILURE(*status
)) {
2096 log_err("%s: Failure setting pattern %s\n", locale
, u_errorName(*status
));
2099 if(!uset_containsAll(ref
, set
)) {
2100 log_err("%s: Some stuff from %s is not present in the set\n", locale
, inSet
);
2104 bufLen
= u_unescape(outSet
, buffer
, 512);
2105 uset_applyPattern(set
, buffer
, bufLen
, 0, status
);
2106 if(U_FAILURE(*status
)) {
2107 log_err("%s: Failure setting pattern %s\n", locale
, u_errorName(*status
));
2110 if(!uset_containsNone(ref
, set
)) {
2111 log_err("%s: Some stuff from %s is present in the set\n", locale
, outSet
);
2119 TestGetContractionsAndUnsafes(void)
2123 const char* inConts
;
2124 const char* outConts
;
2127 const char* unsafeCodeUnits
;
2128 const char* safeCodeUnits
;
2131 "[{\\u0418\\u0306}{\\u0438\\u0306}]",
2136 "[aAbB\\u0430\\u0410\\u0433\\u0413]"
2139 "[{\\u0406\\u0308}{\\u0456\\u0308}{\\u0418\\u0306}{\\u0438\\u0306}]",
2140 "[\\u0407\\u0419\\u0439\\u0457]",
2143 "[\\u0406\\u0456\\u0418\\u0438]",
2147 "[{C\\u0301}{C\\u030C}{C\\u0341}{DZ\\u030C}{Dz\\u030C}{D\\u017D}{D\\u017E}{lj}{nj}]",
2148 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
2155 "[{\\u3053\\u3099\\u309D}{\\u3053\\u3099\\u309D\\u3099}{\\u3053\\u3099\\u309E}{\\u3053\\u3099\\u30FC}{\\u3053\\u309D}{\\u3053\\u309D\\u3099}{\\u3053\\u309E}{\\u3053\\u30FC}{\\u30B3\\u3099\\u30FC}{\\u30B3\\u3099\\u30FD}{\\u30B3\\u3099\\u30FD\\u3099}{\\u30B3\\u3099\\u30FE}{\\u30B3\\u30FC}{\\u30B3\\u30FD}{\\u30B3\\u30FD\\u3099}{\\u30B3\\u30FE}]",
2156 "[{\\u30FD\\u3099}{\\u309D\\u3099}{\\u3053\\u3099}{\\u30B3\\u3099}{lj}{nj}]",
2167 UErrorCode status
= U_ZERO_ERROR
;
2168 UCollator
*coll
= NULL
;
2170 int32_t noConts
= 0;
2171 USet
*conts
= uset_open(0,0);
2172 USet
*exp
= uset_open(0, 0);
2173 USet
*set
= uset_open(0,0);
2174 int32_t setBufferLen
= 65536;
2175 UChar buffer
[65536];
2178 for(i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++) {
2179 log_verbose("Testing locale: %s\n", tests
[i
].locale
);
2180 coll
= ucol_open(tests
[i
].locale
, &status
);
2181 if (coll
== NULL
|| U_FAILURE(status
)) {
2182 log_err_status(status
, "Unable to open collator for locale %s ==> %s\n", tests
[i
].locale
, u_errorName(status
));
2185 ucol_getContractionsAndExpansions(coll
, conts
, exp
, TRUE
, &status
);
2186 doSetsTest(tests
[i
].locale
, conts
, set
, tests
[i
].inConts
, tests
[i
].outConts
, &status
);
2187 setLen
= uset_toPattern(conts
, buffer
, setBufferLen
, TRUE
, &status
);
2188 if(U_SUCCESS(status
)) {
2189 /*log_verbose("Contractions %i: %s\n", uset_getItemCount(conts), aescstrdup(buffer, setLen));*/
2191 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2192 status
= U_ZERO_ERROR
;
2194 doSetsTest(tests
[i
].locale
, exp
, set
, tests
[i
].inExp
, tests
[i
].outExp
, &status
);
2195 setLen
= uset_toPattern(exp
, buffer
, setBufferLen
, TRUE
, &status
);
2196 if(U_SUCCESS(status
)) {
2197 /*log_verbose("Expansions %i: %s\n", uset_getItemCount(exp), aescstrdup(buffer, setLen));*/
2199 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2200 status
= U_ZERO_ERROR
;
2203 noConts
= ucol_getUnsafeSet(coll
, conts
, &status
);
2204 doSetsTest(tests
[i
].locale
, conts
, set
, tests
[i
].unsafeCodeUnits
, tests
[i
].safeCodeUnits
, &status
);
2205 setLen
= uset_toPattern(conts
, buffer
, setBufferLen
, TRUE
, &status
);
2206 if(U_SUCCESS(status
)) {
2207 log_verbose("Unsafe %i: %s\n", uset_getItemCount(exp
), aescstrdup(buffer
, setLen
));
2209 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2210 status
= U_ZERO_ERROR
;
2223 TestOpenBinary(void)
2225 UErrorCode status
= U_ZERO_ERROR
;
2227 char rule[] = "&h < d < c < b";
2228 char *wUCA[] = { "a", "h", "d", "c", "b", "i" };
2229 char *noUCA[] = {"d", "c", "b", "a", "h", "i" };
2231 /* we have to use Cyrillic letters because latin-1 always gets copied */
2232 const char rule
[] = "&\\u0452 < \\u0434 < \\u0433 < \\u0432"; /* &dje < d < g < v */
2233 const char *wUCA
[] = { "\\u0430", "\\u0452", "\\u0434", "\\u0433", "\\u0432", "\\u0435" }; /* a, dje, d, g, v, e */
2234 const char *noUCA
[] = {"\\u0434", "\\u0433", "\\u0432", "\\u0430", "\\u0435", "\\u0452" }; /* d, g, v, a, e, dje */
2237 int32_t uRulesLen
= u_unescape(rule
, uRules
, 256);
2239 UCollator
*coll
= ucol_openRules(uRules
, uRulesLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, NULL
, &status
);
2240 UCollator
*UCA
= NULL
;
2241 UCollator
*cloneNOUCA
= NULL
, *cloneWUCA
= NULL
;
2243 uint8_t imageBuffer
[32768];
2244 uint8_t *image
= imageBuffer
;
2245 int32_t imageBufferCapacity
= 32768;
2249 if((coll
==NULL
)||(U_FAILURE(status
))) {
2250 log_data_err("could not load collators or error occured: %s\n",
2251 u_errorName(status
));
2254 UCA
= ucol_open("root", &status
);
2255 if((UCA
==NULL
)||(U_FAILURE(status
))) {
2256 log_data_err("could not load UCA collator or error occured: %s\n",
2257 u_errorName(status
));
2260 imageSize
= ucol_cloneBinary(coll
, image
, imageBufferCapacity
, &status
);
2261 if(U_FAILURE(status
)) {
2262 image
= (uint8_t *)malloc(imageSize
*sizeof(uint8_t));
2263 status
= U_ZERO_ERROR
;
2264 imageSize
= ucol_cloneBinary(coll
, imageBuffer
, imageSize
, &status
);
2268 cloneWUCA
= ucol_openBinary(image
, imageSize
, UCA
, &status
);
2269 cloneNOUCA
= ucol_openBinary(image
, imageSize
, NULL
, &status
);
2271 genericOrderingTest(coll
, wUCA
, sizeof(wUCA
)/sizeof(wUCA
[0]));
2273 genericOrderingTest(cloneWUCA
, wUCA
, sizeof(wUCA
)/sizeof(wUCA
[0]));
2274 genericOrderingTest(cloneNOUCA
, noUCA
, sizeof(noUCA
)/sizeof(noUCA
[0]));
2276 if(image
!= imageBuffer
) {
2280 ucol_close(cloneNOUCA
);
2281 ucol_close(cloneWUCA
);
2285 static void TestDefault(void) {
2286 /* Tests for code coverage. */
2287 UErrorCode status
= U_ZERO_ERROR
;
2288 UCollator
*coll
= ucol_open("es@collation=pinyin", &status
);
2289 if (coll
== NULL
|| status
== U_FILE_ACCESS_ERROR
) {
2290 log_data_err("Unable to open collator es@collation=pinyin\n");
2293 if (status
!= U_USING_DEFAULT_WARNING
) {
2294 /* What do you mean that you know about using pinyin collation in Spanish!? This should be in the zh locale. */
2295 log_err("es@collation=pinyin should return U_USING_DEFAULT_WARNING, but returned %s\n", u_errorName(status
));
2298 if (ucol_getKeywordValues("funky", &status
) != NULL
) {
2299 log_err("Collators should not know about the funky keyword.\n");
2301 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
2302 log_err("funky keyword didn't fail as expected %s\n", u_errorName(status
));
2304 if (ucol_getKeywordValues("collation", &status
) != NULL
) {
2305 log_err("ucol_getKeywordValues should not work when given a bad status.\n");
2309 static void TestDefaultKeyword(void) {
2310 /* Tests for code coverage. */
2311 UErrorCode status
= U_ZERO_ERROR
;
2312 const char *loc
= "zh_TW@collation=default";
2313 UCollator
*coll
= ucol_open(loc
, &status
);
2314 if(U_FAILURE(status
)) {
2315 log_info("Warning: ucol_open(%s, ...) returned %s, at least it didn't crash.\n", loc
, u_errorName(status
));
2316 } else if (status
!= U_USING_FALLBACK_WARNING
) {
2317 /* Hmm, skip the following test for CLDR 1.9 data and/or ICU 4.6, no longer seems to apply */
2319 log_err("ucol_open(%s, ...) should return an error or some sort of U_USING_FALLBACK_WARNING, but returned %s\n", loc
, u_errorName(status
));
2325 static void TestGetKeywordValuesForLocale(void) {
2326 #define INCLUDE_UNIHAN_COLLATION 1
2327 #define PREFERRED_SIZE 16
2328 #define MAX_NUMBER_OF_KEYWORDS 8
2329 const char *PREFERRED
[PREFERRED_SIZE
][MAX_NUMBER_OF_KEYWORDS
+1] = {
2330 { "und", "standard", "search", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
2331 { "en_US", "standard", "search", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
2332 { "en_029", "standard", "search", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
2333 { "de_DE", "standard", "phonebook", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2334 { "de_Latn_DE", "standard", "phonebook", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2335 #if INCLUDE_UNIHAN_COLLATION
2336 { "zh", "pinyin", "big5han", "gb2312han", "stroke", "unihan", "zhuyin", "search", "standard" },
2337 { "zh_Hans", "pinyin", "big5han", "gb2312han", "stroke", "unihan", "zhuyin", "search", "standard" },
2338 { "zh_CN", "pinyin", "big5han", "gb2312han", "stroke", "unihan", "zhuyin", "search", "standard" },
2339 { "zh_Hant", "stroke", "big5han", "gb2312han", "pinyin", "unihan", "zhuyin", "search", "standard" },
2340 { "zh_TW", "stroke", "big5han", "gb2312han", "pinyin", "unihan", "zhuyin", "search", "standard" },
2341 { "zh__PINYIN", "pinyin", "big5han", "gb2312han", "stroke", "unihan", "zhuyin", "search", "standard" },
2343 { "zh", "pinyin", "big5han", "gb2312han", "stroke", "zhuyin", "search", "standard", NULL
},
2344 { "zh_Hans", "pinyin", "big5han", "gb2312han", "stroke", "zhuyin", "search", "standard", NULL
},
2345 { "zh_CN", "pinyin", "big5han", "gb2312han", "stroke", "zhuyin", "search", "standard", NULL
},
2346 { "zh_Hant", "stroke", "big5han", "gb2312han", "pinyin", "zhuyin", "search", "standard", NULL
},
2347 { "zh_TW", "stroke", "big5han", "gb2312han", "pinyin", "zhuyin", "search", "standard", NULL
},
2348 { "zh__PINYIN", "pinyin", "big5han", "gb2312han", "stroke", "zhuyin", "search", "standard", NULL
},
2350 { "es_ES", "standard", "search", "traditional", NULL
, NULL
, NULL
, NULL
, NULL
},
2351 { "es__TRADITIONAL","traditional", "search", "standard", NULL
, NULL
, NULL
, NULL
, NULL
},
2352 { "und@collation=phonebook", "standard", "search", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
2353 { "de_DE@collation=big5han", "standard", "phonebook", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2354 { "zzz@collation=xxx", "standard", "search", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
}
2356 #if INCLUDE_UNIHAN_COLLATION
2357 const int32_t expectedLength
[PREFERRED_SIZE
] = { 2, 2, 2, 3, 3, 8, 8, 8, 8, 8, 8, 3, 3, 2, 3, 2 };
2359 const int32_t expectedLength
[PREFERRED_SIZE
] = { 2, 2, 2, 3, 3, 7, 7, 7, 7, 7, 7, 3, 3, 2, 3, 2 };
2362 UErrorCode status
= U_ZERO_ERROR
;
2363 UEnumeration
*keywordValues
= NULL
;
2364 int32_t i
, n
, size
, valueLength
;
2365 const char *locale
= NULL
, *value
= NULL
;
2366 UBool errorOccurred
= FALSE
;
2368 for (i
= 0; i
< PREFERRED_SIZE
; i
++) {
2369 locale
= PREFERRED
[i
][0];
2374 keywordValues
= ucol_getKeywordValuesForLocale("collation", locale
, TRUE
, &status
);
2375 if (keywordValues
== NULL
|| U_FAILURE(status
)) {
2376 log_err_status(status
, "Error getting keyword values: %s\n", u_errorName(status
));
2379 size
= uenum_count(keywordValues
, &status
);
2381 if (size
== expectedLength
[i
]) {
2382 for (n
= 0; n
< expectedLength
[i
]; n
++) {
2383 if ((value
= uenum_next(keywordValues
, &valueLength
, &status
)) != NULL
&& U_SUCCESS(status
)) {
2384 if (uprv_strcmp(value
, PREFERRED
[i
][n
+1]) != 0) {
2385 log_err("Keyword values differ: Got [%s] Expected [%s] for locale: %s\n", value
, PREFERRED
[i
][n
+1], locale
);
2386 errorOccurred
= TRUE
;
2391 log_err("While getting keyword value from locale: %s got this error: %s\n", locale
, u_errorName(status
));
2392 errorOccurred
= TRUE
;
2396 if (errorOccurred
) {
2400 log_err("Number of keywords (%d) does not match expected size (%d) for locale: %s\n", size
, expectedLength
[i
], locale
);
2403 uenum_close(keywordValues
);
2404 keywordValues
= NULL
;
2406 if (keywordValues
!= NULL
) {
2407 uenum_close(keywordValues
);
2412 #endif /* #if !UCONFIG_NO_COLLATION */