1 /********************************************************************
2 * Copyright (c) 1997-2010 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 int TestBufferSize(); /* defined in "colutil.c" */
43 /* next two function is modified from "i18n/ucol.cpp" to avoid include "ucol_imp.h" */
44 static void uprv_appendByteToHexString(char *dst
, uint8_t val
) {
45 uint32_t len
= (uint32_t)strlen(dst
);
46 sprintf(dst
+len
, "%02X", val
);
49 static char* U_EXPORT2
ucol_sortKeyToString(const UCollator
*coll
, const uint8_t *sortkey
, char *buffer
, uint32_t *len
) {
50 int32_t strength
= UCOL_PRIMARY
;
51 uint32_t res_size
= 0;
52 UBool doneCase
= FALSE
;
54 char *current
= buffer
;
55 const uint8_t *currentSk
= sortkey
;
57 UErrorCode error_code
= U_ZERO_ERROR
;
61 while(strength
<= UCOL_QUATERNARY
&& strength
<= ucol_getAttribute(coll
,UCOL_STRENGTH
, &error_code
)) {
62 if(U_FAILURE(error_code
)) {
63 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code
));
65 if(strength
> UCOL_PRIMARY
) {
66 strcat(current
, " . ");
68 while(*currentSk
!= 0x01 && *currentSk
!= 0x00) { /* print a level */
69 uprv_appendByteToHexString(current
, *currentSk
++);
72 if(ucol_getAttribute(coll
,UCOL_CASE_LEVEL
, &error_code
) == UCOL_ON
&& strength
== UCOL_SECONDARY
&& doneCase
== FALSE
) {
74 } else if(ucol_getAttribute(coll
,UCOL_CASE_LEVEL
, &error_code
) == UCOL_OFF
|| doneCase
== TRUE
|| strength
!= UCOL_SECONDARY
) {
77 if(U_FAILURE(error_code
)) {
78 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code
));
80 uprv_appendByteToHexString(current
, *currentSk
++); /* This should print '01' */
81 if(strength
== UCOL_QUATERNARY
&& ucol_getAttribute(coll
,UCOL_ALTERNATE_HANDLING
, &error_code
) == UCOL_NON_IGNORABLE
) {
86 if(ucol_getAttribute(coll
,UCOL_STRENGTH
, &error_code
) == UCOL_IDENTICAL
) {
87 strcat(current
, " . ");
88 while(*currentSk
!= 0) {
89 uprv_appendByteToHexString(current
, *currentSk
++);
93 uprv_appendByteToHexString(current
, *currentSk
++);
95 if(U_FAILURE(error_code
)) {
96 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code
));
100 if(res_size
> *len
) {
106 /* end of avoid include "ucol_imp.h" */
109 void addCollAPITest(TestNode
** root
)
111 /* WEIVTODO: return tests here */
112 addTest(root
, &TestProperty
, "tscoll/capitst/TestProperty");
113 addTest(root
, &TestRuleBasedColl
, "tscoll/capitst/TestRuleBasedColl");
114 addTest(root
, &TestCompare
, "tscoll/capitst/TestCompare");
115 addTest(root
, &TestSortKey
, "tscoll/capitst/TestSortKey");
116 addTest(root
, &TestHashCode
, "tscoll/capitst/TestHashCode");
117 addTest(root
, &TestElemIter
, "tscoll/capitst/TestElemIter");
118 addTest(root
, &TestGetAll
, "tscoll/capitst/TestGetAll");
119 /*addTest(root, &TestGetDefaultRules, "tscoll/capitst/TestGetDefaultRules");*/
120 addTest(root
, &TestDecomposition
, "tscoll/capitst/TestDecomposition");
121 addTest(root
, &TestSafeClone
, "tscoll/capitst/TestSafeClone");
122 addTest(root
, &TestCloneBinary
, "tscoll/capitst/TestCloneBinary");
123 addTest(root
, &TestGetSetAttr
, "tscoll/capitst/TestGetSetAttr");
124 addTest(root
, &TestBounds
, "tscoll/capitst/TestBounds");
125 addTest(root
, &TestGetLocale
, "tscoll/capitst/TestGetLocale");
126 addTest(root
, &TestSortKeyBufferOverrun
, "tscoll/capitst/TestSortKeyBufferOverrun");
127 addTest(root
, &TestAttribute
, "tscoll/capitst/TestAttribute");
128 addTest(root
, &TestGetTailoredSet
, "tscoll/capitst/TestGetTailoredSet");
129 addTest(root
, &TestMergeSortKeys
, "tscoll/capitst/TestMergeSortKeys");
130 addTest(root
, &TestShortString
, "tscoll/capitst/TestShortString");
131 addTest(root
, &TestGetContractionsAndUnsafes
, "tscoll/capitst/TestGetContractionsAndUnsafes");
132 addTest(root
, &TestOpenBinary
, "tscoll/capitst/TestOpenBinary");
133 addTest(root
, &TestDefault
, "tscoll/capitst/TestDefault");
134 addTest(root
, &TestDefaultKeyword
, "tscoll/capitst/TestDefaultKeyword");
135 addTest(root
, &TestOpenVsOpenRules
, "tscoll/capitst/TestOpenVsOpenRules");
136 addTest(root
, &TestGetKeywordValuesForLocale
, "tscoll/capitst/TestGetKeywordValuesForLocale");
139 void TestGetSetAttr(void) {
140 UErrorCode status
= U_ZERO_ERROR
;
141 UCollator
*coll
= ucol_open(NULL
, &status
);
144 UColAttributeValue val
[5];
146 UColAttributeValue nonValue
;
148 {UCOL_FRENCH_COLLATION
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},
149 {UCOL_ALTERNATE_HANDLING
, {UCOL_NON_IGNORABLE
, UCOL_SHIFTED
}, 2, UCOL_OFF
},/* attribute for handling variable elements*/
150 {UCOL_CASE_FIRST
, {UCOL_OFF
, UCOL_LOWER_FIRST
, UCOL_UPPER_FIRST
}, 3, UCOL_SHIFTED
},/* who goes first, lower case or uppercase */
151 {UCOL_CASE_LEVEL
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* do we have an extra case level */
152 {UCOL_NORMALIZATION_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* attribute for normalization */
153 {UCOL_DECOMPOSITION_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},
154 {UCOL_STRENGTH
, {UCOL_PRIMARY
, UCOL_SECONDARY
, UCOL_TERTIARY
, UCOL_QUATERNARY
, UCOL_IDENTICAL
}, 5, UCOL_SHIFTED
},/* attribute for strength */
155 {UCOL_HIRAGANA_QUATERNARY_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* when turned on, this attribute */
157 UColAttribute currAttr
;
158 UColAttributeValue value
;
159 uint32_t i
= 0, j
= 0;
162 log_err_status(status
, "Unable to open collator. %s\n", u_errorName(status
));
165 for(i
= 0; i
<sizeof(attrs
)/sizeof(attrs
[0]); i
++) {
166 currAttr
= attrs
[i
].att
;
167 ucol_setAttribute(coll
, currAttr
, UCOL_DEFAULT
, &status
);
168 if(U_FAILURE(status
)) {
169 log_err_status(status
, "ucol_setAttribute with the default value returned error: %s\n", u_errorName(status
));
172 value
= ucol_getAttribute(coll
, currAttr
, &status
);
173 if(U_FAILURE(status
)) {
174 log_err("ucol_getAttribute returned error: %s\n", u_errorName(status
));
177 for(j
= 0; j
<attrs
[i
].valueSize
; j
++) {
178 ucol_setAttribute(coll
, currAttr
, attrs
[i
].val
[j
], &status
);
179 if(U_FAILURE(status
)) {
180 log_err("ucol_setAttribute with the value %i returned error: %s\n", attrs
[i
].val
[j
], u_errorName(status
));
184 status
= U_ZERO_ERROR
;
185 ucol_setAttribute(coll
, currAttr
, attrs
[i
].nonValue
, &status
);
186 if(U_SUCCESS(status
)) {
187 log_err("ucol_setAttribute with the bad value didn't return an error\n");
190 status
= U_ZERO_ERROR
;
192 ucol_setAttribute(coll
, currAttr
, value
, &status
);
193 if(U_FAILURE(status
)) {
194 log_err("ucol_setAttribute with the default valuereturned error: %s\n", u_errorName(status
));
198 status
= U_ZERO_ERROR
;
199 value
= ucol_getAttribute(coll
, UCOL_ATTRIBUTE_COUNT
, &status
);
200 if(U_SUCCESS(status
)) {
201 log_err("ucol_getAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
203 status
= U_ZERO_ERROR
;
204 ucol_setAttribute(coll
, UCOL_ATTRIBUTE_COUNT
, UCOL_DEFAULT
, &status
);
205 if(U_SUCCESS(status
)) {
206 log_err("ucol_setAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
208 status
= U_ZERO_ERROR
;
213 static void doAssert(int condition
, const char *message
)
216 log_err("ERROR : %s\n", message
);
221 /* We don't have default rules, at least not in the previous sense */
222 void TestGetDefaultRules(){
224 UErrorCode status
=U_ZERO_ERROR
;
225 UCollator
*coll
=NULL
;
226 int32_t len1
= 0, len2
=0;
227 uint8_t *binColData
= NULL
;
229 UResourceBundle
*res
= NULL
;
230 UResourceBundle
*binColl
= NULL
;
231 uint8_t *binResult
= NULL
;
234 const UChar
* defaultRulesArray
=ucol_getDefaultRulesArray(&size
);
235 log_verbose("Test the function ucol_getDefaultRulesArray()\n");
237 coll
= ucol_openRules(defaultRulesArray
, size
, UCOL_ON
, UCOL_PRIMARY
, &status
);
238 if(U_SUCCESS(status
) && coll
!=NULL
) {
239 binColData
= (uint8_t*)ucol_cloneRuleData(coll
, &len1
, &status
);
245 res
=ures_open(NULL
, "root", &status
);
246 if(U_FAILURE(status
)){
247 log_err("ERROR: Failed to get resource for \"root Locale\" with %s", myErrorName(status
));
250 binColl
=ures_getByKey(res
, "%%Collation", binColl
, &status
);
251 if(U_SUCCESS(status
)){
252 binResult
=(uint8_t*)ures_getBinary(binColl
, &len2
, &status
);
253 if(U_FAILURE(status
)){
254 log_err("ERROR: ures_getBinary() failed\n");
257 log_err("ERROR: ures_getByKey(locale(default), %%Collation) failed");
262 log_err("Error: ucol_getDefaultRulesArray() failed to return the correct length.\n");
264 if(memcmp(binColData
, binResult
, len1
) != 0){
265 log_err("Error: ucol_getDefaultRulesArray() failed\n");
276 /* Collator Properties
277 ucol_open, ucol_strcoll, getStrength/setStrength
278 getDecomposition/setDecomposition, getDisplayName*/
281 UCollator
*col
, *ruled
;
284 UChar
*source
, *target
;
286 UErrorCode status
= U_ZERO_ERROR
;
288 * Expected version of the English collator.
289 * Currently, the major/minor version numbers change when the builder code
291 * number 2 is from the tailoring data version and
292 * number 3 is the UCA version.
293 * This changes with every UCA version change, and the expected value
294 * needs to be adjusted.
295 * Same in intltest/apicoll.cpp.
297 UVersionInfo currVersionArray
= {0x31, 0xC0, 0x05, 0x2A}; /* from ICU 4.4/UCA 5.2 */
298 UVersionInfo versionArray
= {0, 0, 0, 0};
299 UVersionInfo versionUCAArray
= {0, 0, 0, 0};
300 UVersionInfo versionUCDArray
= {0, 0, 0, 0};
302 log_verbose("The property tests begin : \n");
303 log_verbose("Test ucol_strcoll : \n");
304 col
= ucol_open("en_US", &status
);
305 if (U_FAILURE(status
)) {
306 log_err_status(status
, "Default Collator creation failed.: %s\n", myErrorName(status
));
310 ucol_getVersion(col
, versionArray
);
311 /* Check for a version greater than some value rather than equality
312 * so that we need not update the expected version each time. */
313 if (uprv_memcmp(versionArray
, currVersionArray
, 4)<0) {
314 log_err("Testing ucol_getVersion() - unexpected result: %02x.%02x.%02x.%02x\n",
315 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
317 log_verbose("ucol_getVersion() result: %02x.%02x.%02x.%02x\n",
318 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
321 /* Assume that the UCD and UCA versions are the same,
322 * rather than hardcoding (and updating each time) a particular UCA version. */
323 u_getUnicodeVersion(versionUCDArray
);
324 ucol_getUCAVersion(col
, versionUCAArray
);
325 if (0!=uprv_memcmp(versionUCAArray
, versionUCDArray
, 4)) {
326 log_err("Testing ucol_getUCAVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
327 versionUCAArray
[0], versionUCAArray
[1], versionUCAArray
[2], versionUCAArray
[3]);
330 source
=(UChar
*)malloc(sizeof(UChar
) * 12);
331 target
=(UChar
*)malloc(sizeof(UChar
) * 12);
334 u_uastrcpy(source
, "ab");
335 u_uastrcpy(target
, "abc");
337 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
), "ab < abc comparison failed");
339 u_uastrcpy(source
, "ab");
340 u_uastrcpy(target
, "AB");
342 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
), "ab < AB comparison failed");
343 /* u_uastrcpy(source, "black-bird");
344 u_uastrcpy(target, "blackbird"); */
345 u_uastrcpy(target
, "black-bird");
346 u_uastrcpy(source
, "blackbird");
348 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_GREATER
),
349 "black-bird > blackbird comparison failed");
350 u_uastrcpy(source
, "black bird");
351 u_uastrcpy(target
, "black-bird");
352 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
),
353 "black bird < black-bird comparison failed");
354 u_uastrcpy(source
, "Hello");
355 u_uastrcpy(target
, "hello");
357 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_GREATER
),
358 "Hello > hello comparison failed");
361 log_verbose("Test ucol_strcoll ends.\n");
363 log_verbose("testing ucol_getStrength() method ...\n");
364 doAssert( (ucol_getStrength(col
) == UCOL_TERTIARY
), "collation object has the wrong strength");
365 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
367 log_verbose("testing ucol_setStrength() method ...\n");
368 ucol_setStrength(col
, UCOL_SECONDARY
);
369 doAssert( (ucol_getStrength(col
) != UCOL_TERTIARY
), "collation object's strength is secondary difference");
370 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
371 doAssert( (ucol_getStrength(col
) == UCOL_SECONDARY
), "collation object has the wrong strength");
374 log_verbose("Get display name for the default collation in German : \n");
376 len
=ucol_getDisplayName("en_US", "de_DE", NULL
, 0, &status
);
377 if(status
==U_BUFFER_OVERFLOW_ERROR
){
379 disName
=(UChar
*)malloc(sizeof(UChar
) * (len
+1));
380 ucol_getDisplayName("en_US", "de_DE", disName
, len
+1, &status
);
381 log_verbose("the display name for default collation in german: %s\n", austrdup(disName
) );
384 if(U_FAILURE(status
)){
385 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status
));
388 log_verbose("Default collation getDisplayName ended.\n");
390 ruled
= ucol_open("da_DK", &status
);
391 log_verbose("ucol_getRules() testing ...\n");
392 ucol_getRules(ruled
, &tempLength
);
393 doAssert( tempLength
!= 0, "getRules() result incorrect" );
394 log_verbose("getRules tests end.\n");
396 UChar
*buffer
= (UChar
*)malloc(200000*sizeof(UChar
));
397 int32_t bufLen
= 200000;
399 log_verbose("ucol_getRulesEx() testing ...\n");
400 tempLength
= ucol_getRulesEx(col
,UCOL_TAILORING_ONLY
,buffer
,bufLen
);
401 doAssert( tempLength
== 0x00, "getRulesEx() result incorrect" );
402 log_verbose("getRules tests end.\n");
404 log_verbose("ucol_getRulesEx() testing ...\n");
405 tempLength
=ucol_getRulesEx(col
,UCOL_FULL_RULES
,buffer
,bufLen
);
406 doAssert( tempLength
!= 0, "getRulesEx() result incorrect" );
407 log_verbose("getRules tests end.\n");
413 log_verbose("open an collator for french locale");
414 col
= ucol_open("fr_FR", &status
);
415 if (U_FAILURE(status
)) {
416 log_err("ERROR: Creating French collation failed.: %s\n", myErrorName(status
));
419 ucol_setStrength(col
, UCOL_PRIMARY
);
420 log_verbose("testing ucol_getStrength() method again ...\n");
421 doAssert( (ucol_getStrength(col
) != UCOL_TERTIARY
), "collation object has the wrong strength");
422 doAssert( (ucol_getStrength(col
) == UCOL_PRIMARY
), "collation object's strength is not primary difference");
424 log_verbose("testing French ucol_setStrength() method ...\n");
425 ucol_setStrength(col
, UCOL_TERTIARY
);
426 doAssert( (ucol_getStrength(col
) == UCOL_TERTIARY
), "collation object's strength is not tertiary difference");
427 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
428 doAssert( (ucol_getStrength(col
) != UCOL_SECONDARY
), "collation object's strength is secondary difference");
431 log_verbose("Get display name for the french collation in english : \n");
432 len
=ucol_getDisplayName("fr_FR", "en_US", NULL
, 0, &status
);
433 if(status
==U_BUFFER_OVERFLOW_ERROR
){
435 disName
=(UChar
*)malloc(sizeof(UChar
) * (len
+1));
436 ucol_getDisplayName("fr_FR", "en_US", disName
, len
+1, &status
);
437 log_verbose("the display name for french collation in english: %s\n", austrdup(disName
) );
440 if(U_FAILURE(status
)){
441 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status
));
444 log_verbose("Default collation getDisplayName ended.\n");
448 /* Test RuleBasedCollator and getRules*/
449 void TestRuleBasedColl()
451 UCollator
*col1
, *col2
, *col3
, *col4
;
452 UCollationElements
*iter1
, *iter2
;
457 const UChar
*rule1
, *rule2
, *rule3
, *rule4
;
459 UErrorCode status
= U_ZERO_ERROR
;
460 u_uastrcpy(ruleset1
, "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
461 u_uastrcpy(ruleset2
, "&9 < a, A < b, B < c, C < d, D, e, E");
464 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
,&status
);
465 if (U_FAILURE(status
)) {
466 log_err_status(status
, "RuleBased Collator creation failed.: %s\n", myErrorName(status
));
470 log_verbose("PASS: RuleBased Collator creation passed\n");
472 status
= U_ZERO_ERROR
;
473 col2
= ucol_openRules(ruleset2
, u_strlen(ruleset2
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
474 if (U_FAILURE(status
)) {
475 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
479 log_verbose("PASS: RuleBased Collator creation passed\n");
482 status
= U_ZERO_ERROR
;
483 col3
= ucol_open(NULL
, &status
);
484 if (U_FAILURE(status
)) {
485 log_err("Default Collator creation failed.: %s\n", myErrorName(status
));
489 log_verbose("PASS: Default Collator creation passed\n");
491 rule1
= ucol_getRules(col1
, &tempLength
);
492 rule2
= ucol_getRules(col2
, &tempLength
);
493 rule3
= ucol_getRules(col3
, &tempLength
);
495 doAssert((u_strcmp(rule1
, rule2
) != 0), "Default collator getRules failed");
496 doAssert((u_strcmp(rule2
, rule3
) != 0), "Default collator getRules failed");
497 doAssert((u_strcmp(rule1
, rule3
) != 0), "Default collator getRules failed");
499 col4
=ucol_openRules(rule2
, u_strlen(rule2
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
500 if (U_FAILURE(status
)) {
501 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
504 rule4
= ucol_getRules(col4
, &tempLength
);
505 doAssert((u_strcmp(rule2
, rule4
) == 0), "Default collator getRules failed");
512 /* tests that modifier ! is always ignored */
513 u_uastrcpy(ruleset1
, "!&a<b");
517 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
518 if (U_FAILURE(status
)) {
519 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
522 col2
= ucol_open("en_US", &status
);
523 if (U_FAILURE(status
)) {
524 log_err("en_US Collator creation failed.: %s\n", myErrorName(status
));
527 iter1
= ucol_openElements(col1
, teststr
, 3, &status
);
528 iter2
= ucol_openElements(col2
, teststr
, 3, &status
);
529 if(U_FAILURE(status
)) {
530 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status
));
534 /* testing with en since thai has its own tailoring */
535 uint32_t ce
= ucol_next(iter1
, &status
);
536 uint32_t ce2
= ucol_next(iter2
, &status
);
537 if(U_FAILURE(status
)) {
538 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status
));
542 log_err("! modifier test failed");
544 if (ce
== UCOL_NULLORDER
) {
548 ucol_closeElements(iter1
);
549 ucol_closeElements(iter2
);
552 /* test that we can start a rule without a & or < */
553 u_uastrcpy(ruleset1
, "< z < a");
554 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
555 if (U_FAILURE(status
)) {
556 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
559 u_uastrcpy(teststr
, "z");
560 u_uastrcpy(teststr2
, "a");
561 if (ucol_greaterOrEqual(col1
, teststr
, 1, teststr2
, 1)) {
562 log_err("Rule \"z < a\" fails");
569 UErrorCode status
= U_ZERO_ERROR
;
574 log_verbose("The compare tests begin : \n");
576 col
= ucol_open("en_US", &status
);
577 if(U_FAILURE(status
)) {
578 log_err_status(status
, "ucal_open() collation creation failed.: %s\n", myErrorName(status
));
581 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
582 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
583 u_uastrcpy(test1
, "Abcda");
584 u_uastrcpy(test2
, "abcda");
586 log_verbose("Use tertiary comparison level testing ....\n");
588 doAssert( (!ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" != \"abcda\" ");
589 doAssert( (ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" >>> \"abcda\" ");
590 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" >>> \"abcda\"");
592 ucol_setStrength(col
, UCOL_SECONDARY
);
593 log_verbose("Use secondary comparison level testing ....\n");
595 doAssert( (ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
) )), "Result should be \"Abcda\" == \"abcda\"");
596 doAssert( (!ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
597 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
) )), "Result should be \"Abcda\" == \"abcda\"");
599 ucol_setStrength(col
, UCOL_PRIMARY
);
600 log_verbose("Use primary comparison level testing ....\n");
602 doAssert( (ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
603 doAssert( (!ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
604 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
607 log_verbose("The compare tests end.\n");
614 ---------------------------------------------
615 tests decomposition setting
617 void TestDecomposition() {
618 UErrorCode status
= U_ZERO_ERROR
;
619 UCollator
*en_US
, *el_GR
, *vi_VN
;
620 en_US
= ucol_open("en_US", &status
);
621 el_GR
= ucol_open("el_GR", &status
);
622 vi_VN
= ucol_open("vi_VN", &status
);
624 if (U_FAILURE(status
)) {
625 log_err_status(status
, "ERROR: collation creation failed.: %s\n", myErrorName(status
));
629 if (ucol_getAttribute(vi_VN
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_ON
||
632 log_err("ERROR: vi_VN collation did not have cannonical decomposition for normalization!\n");
635 status
= U_ZERO_ERROR
;
636 if (ucol_getAttribute(el_GR
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_ON
||
639 log_err("ERROR: el_GR collation did not have cannonical decomposition for normalization!\n");
642 status
= U_ZERO_ERROR
;
643 if (ucol_getAttribute(en_US
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_OFF
||
646 log_err("ERROR: en_US collation had cannonical decomposition for normalization!\n");
654 #define CLONETEST_COLLATOR_COUNT 4
656 void TestSafeClone() {
659 static const UChar umlautUStr
[] = {0x00DC, 0};
660 static const UChar oeStr
[] = {0x0055, 0x0045, 0};
661 UCollator
* someCollators
[CLONETEST_COLLATOR_COUNT
];
662 UCollator
* someClonedCollators
[CLONETEST_COLLATOR_COUNT
];
664 UErrorCode err
= U_ZERO_ERROR
;
665 int8_t index
= 6; /* Leave this here to test buffer alingment in memory*/
666 uint8_t buffer
[CLONETEST_COLLATOR_COUNT
] [U_COL_SAFECLONE_BUFFERSIZE
];
667 int32_t bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
668 const char sampleRuleChars
[] = "&Z < CH";
669 UChar sampleRule
[sizeof(sampleRuleChars
)];
671 if (TestBufferSize()) {
672 log_err("U_COL_SAFECLONE_BUFFERSIZE should be larger than sizeof(UCollator)\n");
676 u_uastrcpy(test1
, "abCda");
677 u_uastrcpy(test2
, "abcda");
678 u_uastrcpy(sampleRule
, sampleRuleChars
);
680 /* one default collator & two complex ones */
681 someCollators
[0] = ucol_open("en_US", &err
);
682 someCollators
[1] = ucol_open("ko", &err
);
683 someCollators
[2] = ucol_open("ja_JP", &err
);
684 someCollators
[3] = ucol_openRules(sampleRule
, -1, UCOL_ON
, UCOL_TERTIARY
, NULL
, &err
);
686 for (index
= 0; index
< CLONETEST_COLLATOR_COUNT
; index
++) {
687 ucol_close(someCollators
[index
]);
689 log_data_err("Couldn't open one or more collators\n");
693 /* Check the various error & informational states: */
695 /* Null status - just returns NULL */
696 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, 0))
698 log_err("FAIL: Cloned Collator failed to deal correctly with null status\n");
700 /* error status - should return 0 & keep error the same */
701 err
= U_MEMORY_ALLOCATION_ERROR
;
702 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
) || err
!= U_MEMORY_ALLOCATION_ERROR
)
704 log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
708 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
709 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], 0, &err
) || err
!= U_ILLEGAL_ARGUMENT_ERROR
)
711 log_err("FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer\n");
715 /* buffer size pointer is 0 - fill in pbufferSize with a size */
717 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
) || U_FAILURE(err
) || bufferSize
<= 0)
719 log_err("FAIL: Cloned Collator failed a sizing request ('preflighting')\n");
721 /* Verify our define is large enough */
722 if (U_COL_SAFECLONE_BUFFERSIZE
< bufferSize
)
724 log_err("FAIL: Pre-calculated buffer size is too small\n");
726 /* Verify we can use this run-time calculated size */
727 if (0 == (col
= ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
)) || U_FAILURE(err
))
729 log_err("FAIL: Collator can't be cloned with run-time size\n");
731 if (col
) ucol_close(col
);
732 /* size one byte too small - should allocate & let us know */
734 if (0 == (col
= ucol_safeClone(someCollators
[0], 0, &bufferSize
, &err
)) || err
!= U_SAFECLONE_ALLOCATED_WARNING
)
736 log_err("FAIL: Cloned Collator failed to deal correctly with too-small buffer size\n");
738 if (col
) ucol_close(col
);
740 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
743 /* Null buffer pointer - return Collator & set error to U_SAFECLONE_ALLOCATED_ERROR */
744 if (0 == (col
= ucol_safeClone(someCollators
[0], 0, &bufferSize
, &err
)) || err
!= U_SAFECLONE_ALLOCATED_WARNING
)
746 log_err("FAIL: Cloned Collator failed to deal correctly with null buffer pointer\n");
748 if (col
) ucol_close(col
);
751 /* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
752 if (0 != ucol_safeClone(0, buffer
[0], &bufferSize
, &err
) || err
!= U_ILLEGAL_ARGUMENT_ERROR
)
754 log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
759 /* Test that a cloned collator doesn't accidentally use UCA. */
760 col
=ucol_open("de@collation=phonebook", &err
);
761 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
762 someClonedCollators
[0] = ucol_safeClone(col
, buffer
[0], &bufferSize
, &err
);
763 doAssert( (ucol_greater(col
, umlautUStr
, u_strlen(umlautUStr
), oeStr
, u_strlen(oeStr
))), "Original German phonebook collation sorts differently than expected");
764 doAssert( (ucol_greater(someClonedCollators
[0], umlautUStr
, u_strlen(umlautUStr
), oeStr
, u_strlen(oeStr
))), "Cloned German phonebook collation sorts differently than expected");
765 if (!ucol_equals(someClonedCollators
[0], col
)) {
766 log_err("FAIL: Cloned German phonebook collator is not equal to original.\n");
769 ucol_close(someClonedCollators
[0]);
773 /* change orig & clone & make sure they are independent */
775 for (index
= 0; index
< CLONETEST_COLLATOR_COUNT
; index
++)
777 ucol_setStrength(someCollators
[index
], UCOL_IDENTICAL
);
780 ucol_close(ucol_safeClone(someCollators
[index
], buffer
[index
], &bufferSize
, &err
));
781 if (err
!= U_SAFECLONE_ALLOCATED_WARNING
) {
782 log_err("FAIL: collator number %d was not allocated.\n", index
);
783 log_err("FAIL: status of Collator[%d] is %d (hex: %x).\n", index
, err
, err
);
786 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
788 someClonedCollators
[index
] = ucol_safeClone(someCollators
[index
], buffer
[index
], &bufferSize
, &err
);
789 if (someClonedCollators
[index
] == NULL
790 || someClonedCollators
[index
] < (UCollator
*)buffer
[index
]
791 || someClonedCollators
[index
] > (UCollator
*)(buffer
[index
]+(U_COL_SAFECLONE_BUFFERSIZE
-1)))
793 log_err("FAIL: Cloned collator didn't use provided buffer.\n");
796 if (!ucol_equals(someClonedCollators
[index
], someCollators
[index
])) {
797 log_err("FAIL: Cloned collator is not equal to original at index = %d.\n", index
);
800 /* Check the usability */
801 ucol_setStrength(someCollators
[index
], UCOL_PRIMARY
);
802 ucol_setAttribute(someCollators
[index
], UCOL_CASE_LEVEL
, UCOL_OFF
, &err
);
804 doAssert( (ucol_equal(someCollators
[index
], test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"abcda\" == \"abCda\"");
806 /* Close the original to make sure that the clone is usable. */
807 ucol_close(someCollators
[index
]);
809 ucol_setStrength(someClonedCollators
[index
], UCOL_TERTIARY
);
810 ucol_setAttribute(someClonedCollators
[index
], UCOL_CASE_LEVEL
, UCOL_OFF
, &err
);
811 doAssert( (ucol_greater(someClonedCollators
[index
], test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"abCda\" >>> \"abcda\" ");
813 ucol_close(someClonedCollators
[index
]);
817 void TestCloneBinary(){
818 UErrorCode err
= U_ZERO_ERROR
;
819 UCollator
* col
= ucol_open("en_US", &err
);
824 if (U_FAILURE(err
)) {
825 log_data_err("Couldn't open collator. Error: %s\n", u_errorName(err
));
829 size
= ucol_cloneBinary(col
, NULL
, 0, &err
);
830 if(size
==0 || err
!=U_BUFFER_OVERFLOW_ERROR
) {
831 log_err("ucol_cloneBinary - couldn't check size. Error: %s\n", u_errorName(err
));
836 buffer
= (uint8_t *) malloc(size
);
837 ucol_cloneBinary(col
, buffer
, size
, &err
);
839 log_err("ucol_cloneBinary - couldn't clone.. Error: %s\n", u_errorName(err
));
844 /* how to check binary result ? */
846 c
= ucol_openBinary(buffer
, size
, col
, &err
);
848 log_err("ucol_openBinary failed. Error: %s\n", u_errorName(err
));
850 UChar t
[] = {0x41, 0x42, 0x43, 0}; /* ABC */
853 l1
= ucol_getSortKey(col
, t
, -1, NULL
,0);
854 l2
= ucol_getSortKey(c
, t
, -1, NULL
,0);
855 k1
= (uint8_t *) malloc(sizeof(uint8_t) * l1
);
856 k2
= (uint8_t *) malloc(sizeof(uint8_t) * l2
);
857 ucol_getSortKey(col
, t
, -1, k1
, l1
);
858 ucol_getSortKey(col
, t
, -1, k2
, l2
);
859 if (strcmp((char *)k1
,(char *)k2
) != 0){
860 log_err("ucol_openBinary - new collator should equal to old one\n");
870 TestOpenVsOpenRules ensures that collators from ucol_open and ucol_openRules
871 will generate identical sort keys
873 void TestOpenVsOpenRules(){
875 /* create an array of all the locales */
876 int32_t numLocales
= uloc_countAvailable();
877 int32_t sizeOfStdSet
;
879 UChar str
[41]; /* create an array of UChar of size maximum strSize + 1 */
886 int32_t sortKeyLen1
, sortKeyLen2
;
887 uint8_t *sortKey1
= NULL
, *sortKey2
= NULL
;
894 UErrorCode err
= U_ZERO_ERROR
;
896 /* create a set of standard characters that aren't very interesting...
897 and then we can find some interesting ones later */
899 stdSet
= uset_open(0x61, 0x7A);
900 uset_addRange(stdSet
, 0x41, 0x5A);
901 uset_addRange(stdSet
, 0x30, 0x39);
902 sizeOfStdSet
= uset_size(stdSet
);
905 if(getTestOption(QUICK_OPTION
))
910 for(x
= 0; x
< numLocales
; x
+=adder
){
911 curLoc
= (char *)uloc_getAvailable(x
);
912 log_verbose("Processing %s\n", curLoc
);
914 /* create a collator the normal API way */
915 c1
= ucol_open(curLoc
, &err
);
916 if (U_FAILURE(err
)) {
917 log_err("ERROR: Normal collation creation failed with locale: %s : %s\n", curLoc
, myErrorName(err
));
922 rules
= ucol_getRules(c1
, &rulesLength
);
924 /* use those rules to create a collator from rules */
925 c2
= ucol_openRules(rules
, rulesLength
, UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &err
);
926 if (U_FAILURE(err
)) {
927 log_err("ERROR: Creating collator from rules failed with locale: %s : %s\n", curLoc
, myErrorName(err
));
931 uld
= ulocdata_open(curLoc
, &err
);
933 /*now that we have some collators, we get several strings */
935 for(y
= 0; y
< 5; y
++){
937 /* get a set of ALL the characters in this locale */
938 eSet
= ulocdata_getExemplarSet(uld
, NULL
, 0, ULOCDATA_ES_STANDARD
, &err
);
939 eSize
= uset_size(eSet
);
941 /* make a string with these characters in it */
942 strSize
= (rand()%40
) + 1;
944 for(z
= 0; z
< strSize
; z
++){
945 str
[z
] = uset_charAt(eSet
, rand()%eSize
);
948 /* change the set to only include 'abnormal' characters (not A-Z, a-z, 0-9 */
949 uset_removeAll(eSet
, stdSet
);
950 eSize
= uset_size(eSet
);
952 /* if there are some non-normal characters left, put a few into the string, just to make sure we have some */
954 str
[2%strSize
] = uset_charAt(eSet
, rand()%eSize
);
955 str
[3%strSize
] = uset_charAt(eSet
, rand()%eSize
);
956 str
[5%strSize
] = uset_charAt(eSet
, rand()%eSize
);
957 str
[10%strSize
] = uset_charAt(eSet
, rand()%eSize
);
958 str
[13%strSize
] = uset_charAt(eSet
, rand()%eSize
);
960 /* terminate the string */
961 str
[strSize
-1] = '\0';
962 log_verbose("String used: %S\n", str
);
964 /* get sort keys for both of them, and check that the keys are identicle */
965 sortKeyLen1
= ucol_getSortKey(c1
, str
, u_strlen(str
), NULL
, 0);
966 sortKey1
= (uint8_t*)malloc(sizeof(uint8_t) * (sortKeyLen1
+ 1));
967 /*memset(sortKey1, 0xFE, sortKeyLen1);*/
968 ucol_getSortKey(c1
, str
, u_strlen(str
), sortKey1
, sortKeyLen1
+ 1);
970 sortKeyLen2
= ucol_getSortKey(c2
, str
, u_strlen(str
), NULL
, 0);
971 sortKey2
= (uint8_t*)malloc(sizeof(uint8_t) * (sortKeyLen2
+ 1));
972 /*memset(sortKey2, 0xFE, sortKeyLen2);*/
973 ucol_getSortKey(c2
, str
, u_strlen(str
), sortKey2
, sortKeyLen2
+ 1);
975 /* Check that the lengths are the same */
976 doAssert((sortKeyLen1
== sortKeyLen2
), "Sort key lengths do not match.");
978 /* check that the keys are the same */
979 doAssert((memcmp(sortKey1
, sortKey2
, sortKeyLen1
) == 0), "Keys are not equivalent");
981 /* clean up after each string */
986 /* clean up after each locale */
995 ----------------------------------------------------------------------------
996 ctor -- Tests the getSortKey
1000 uint8_t *sortk1
= NULL
, *sortk2
= NULL
, *sortk3
= NULL
, *sortkEmpty
= NULL
;
1001 int32_t sortklen
, osortklen
;
1002 uint32_t toStringLen
=0;
1004 UChar
*test1
, *test2
, *test3
;
1005 UErrorCode status
= U_ZERO_ERROR
;
1006 char toStringBuffer
[256], *resultP
;
1009 uint8_t s1
[] = { 0x9f, 0x00 };
1010 uint8_t s2
[] = { 0x61, 0x00 };
1013 strcmpResult
= strcmp((const char *)s1
, (const char *)s2
);
1014 log_verbose("strcmp(0x9f..., 0x61...) = %d\n", strcmpResult
);
1016 if(strcmpResult
<= 0) {
1017 log_err("ERR: expected strcmp(\"9f 00\", \"61 00\") to be >=0 (GREATER).. got %d. Calling strcmp() for sortkeys may not work! \n",
1022 log_verbose("testing SortKey begins...\n");
1023 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
1024 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
1025 /* col = ucol_open(NULL, &status); */
1026 col
= ucol_open("en_US", &status
);
1027 if (U_FAILURE(status
)) {
1028 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1033 if(ucol_getStrength(col
) != UCOL_DEFAULT_STRENGTH
)
1035 log_err("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
1037 /* Need to use identical strength */
1038 ucol_setAttribute(col
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &status
);
1040 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
1041 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
1042 test3
=(UChar
*)malloc(sizeof(UChar
) * 6);
1044 memset(test1
,0xFE, sizeof(UChar
)*6);
1045 memset(test2
,0xFE, sizeof(UChar
)*6);
1046 memset(test3
,0xFE, sizeof(UChar
)*6);
1049 u_uastrcpy(test1
, "Abcda");
1050 u_uastrcpy(test2
, "abcda");
1051 u_uastrcpy(test3
, "abcda");
1053 log_verbose("Use tertiary comparison level testing ....\n");
1055 sortklen
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1056 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1057 memset(sortk1
,0xFE, sortklen
);
1058 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortklen
+1);
1060 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1061 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1062 memset(sortk2
,0xFE, sortklen
);
1063 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortklen
+1);
1065 osortklen
= sortklen
;
1066 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test3
), NULL
, 0);
1067 sortk3
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1068 memset(sortk3
,0xFE, sortklen
);
1069 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk3
, sortklen
+1);
1071 doAssert( (sortklen
== osortklen
), "Sortkey length should be the same (abcda, abcda)");
1073 doAssert( (memcmp(sortk1
, sortk2
, sortklen
) > 0), "Result should be \"Abcda\" > \"abcda\"");
1074 doAssert( (memcmp(sortk2
, sortk1
, sortklen
) < 0), "Result should be \"abcda\" < \"Abcda\"");
1075 doAssert( (memcmp(sortk2
, sortk3
, sortklen
) == 0), "Result should be \"abcda\" == \"abcda\"");
1077 resultP
= ucol_sortKeyToString(col
, sortk3
, toStringBuffer
, &toStringLen
);
1078 doAssert( (resultP
!= 0), "sortKeyToString failed!");
1080 #if 1 /* verobse log of sortkeys */
1086 strcpy(junk2
, "abcda[2] ");
1087 strcpy(junk3
, " abcda[3] ");
1089 for(i
=0;i
<sortklen
;i
++)
1091 sprintf(junk2
+strlen(junk2
), "%02X ",(int)( 0xFF & sortk2
[i
]));
1092 sprintf(junk3
+strlen(junk3
), "%02X ",(int)( 0xFF & sortk3
[i
]));
1095 log_verbose("%s\n", junk2
);
1096 log_verbose("%s\n", junk3
);
1104 log_verbose("Use secondary comparision level testing ...\n");
1105 ucol_setStrength(col
, UCOL_SECONDARY
);
1106 sortklen
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1107 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1108 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortklen
+1);
1109 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1110 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1111 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortklen
+1);
1113 doAssert( !(memcmp(sortk1
, sortk2
, sortklen
) > 0), "Result should be \"Abcda\" == \"abcda\"");
1114 doAssert( !(memcmp(sortk2
, sortk1
, sortklen
) < 0), "Result should be \"abcda\" == \"Abcda\"");
1115 doAssert( (memcmp(sortk1
, sortk2
, sortklen
) == 0), "Result should be \"abcda\" == \"abcda\"");
1117 log_verbose("getting sortkey for an empty string\n");
1118 ucol_setAttribute(col
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
1119 sortklen
= ucol_getSortKey(col
, test1
, 0, NULL
, 0);
1120 sortkEmpty
= (uint8_t*)malloc(sizeof(uint8_t) * sortklen
+1);
1121 sortklen
= ucol_getSortKey(col
, test1
, 0, sortkEmpty
, sortklen
+1);
1122 if(sortklen
!= 3 || sortkEmpty
[0] != 1 || sortkEmpty
[0] != 1 || sortkEmpty
[2] != 0) {
1123 log_err("Empty string generated wrong sortkey!\n");
1127 log_verbose("testing passing invalid string\n");
1128 sortklen
= ucol_getSortKey(col
, NULL
, 0, NULL
, 0);
1130 log_err("Invalid string didn't return sortkey size of 0\n");
1134 log_verbose("testing sortkey ends...\n");
1145 uint8_t *sortk1
, *sortk2
, *sortk3
;
1146 int32_t sortk1len
, sortk2len
, sortk3len
;
1148 UChar
*test1
, *test2
, *test3
;
1149 UErrorCode status
= U_ZERO_ERROR
;
1150 log_verbose("testing getHashCode begins...\n");
1151 col
= ucol_open("en_US", &status
);
1152 if (U_FAILURE(status
)) {
1153 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1156 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
1157 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
1158 test3
=(UChar
*)malloc(sizeof(UChar
) * 6);
1159 u_uastrcpy(test1
, "Abcda");
1160 u_uastrcpy(test2
, "abcda");
1161 u_uastrcpy(test3
, "abcda");
1163 log_verbose("Use tertiary comparison level testing ....\n");
1164 sortk1len
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1165 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk1len
+1));
1166 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortk1len
+1);
1167 sortk2len
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1168 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk2len
+1));
1169 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortk2len
+1);
1170 sortk3len
=ucol_getSortKey(col
, test2
, u_strlen(test3
), NULL
, 0);
1171 sortk3
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk3len
+1));
1172 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk3
, sortk3len
+1);
1175 log_verbose("ucol_hashCode() testing ...\n");
1177 doAssert( ucol_keyHashCode(sortk1
, sortk1len
) != ucol_keyHashCode(sortk2
, sortk2len
), "Hash test1 result incorrect" );
1178 doAssert( !(ucol_keyHashCode(sortk1
, sortk1len
) == ucol_keyHashCode(sortk2
, sortk2len
)), "Hash test2 result incorrect" );
1179 doAssert( ucol_keyHashCode(sortk2
, sortk2len
) == ucol_keyHashCode(sortk3
, sortk3len
), "Hash result not equal" );
1181 log_verbose("hashCode tests end.\n");
1193 *----------------------------------------------------------------------------
1194 * Tests the UCollatorElements API.
1200 int32_t order1
, order2
, order3
;
1201 UChar
*testString1
, *testString2
;
1203 UCollationElements
*iterator1
, *iterator2
, *iterator3
;
1204 UErrorCode status
= U_ZERO_ERROR
;
1205 log_verbose("testing UCollatorElements begins...\n");
1206 col
= ucol_open("en_US", &status
);
1207 ucol_setAttribute(col
, UCOL_NORMALIZATION_MODE
, UCOL_OFF
, &status
);
1208 if (U_FAILURE(status
)) {
1209 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1213 testString1
=(UChar
*)malloc(sizeof(UChar
) * 150);
1214 testString2
=(UChar
*)malloc(sizeof(UChar
) * 150);
1215 u_uastrcpy(testString1
, "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
1216 u_uastrcpy(testString2
, "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
1218 log_verbose("Constructors and comparison testing....\n");
1220 iterator1
= ucol_openElements(col
, testString1
, u_strlen(testString1
), &status
);
1221 if(U_FAILURE(status
)) {
1222 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1226 else{ log_verbose("PASS: Default collationElement iterator1 creation passed\n");}
1228 iterator2
= ucol_openElements(col
, testString1
, u_strlen(testString1
), &status
);
1229 if(U_FAILURE(status
)) {
1230 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1234 else{ log_verbose("PASS: Default collationElement iterator2 creation passed\n");}
1236 iterator3
= ucol_openElements(col
, testString2
, u_strlen(testString2
), &status
);
1237 if(U_FAILURE(status
)) {
1238 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1242 else{ log_verbose("PASS: Default collationElement iterator3 creation passed\n");}
1244 offset
=ucol_getOffset(iterator1
);
1245 ucol_setOffset(iterator1
, 6, &status
);
1246 if (U_FAILURE(status
)) {
1247 log_err("Error in setOffset for UCollatorElements iterator.: %s\n", myErrorName(status
));
1250 if(ucol_getOffset(iterator1
)==6)
1251 log_verbose("setOffset and getOffset working fine\n");
1253 log_err("error in set and get Offset got %d instead of 6\n", ucol_getOffset(iterator1
));
1256 ucol_setOffset(iterator1
, 0, &status
);
1257 order1
= ucol_next(iterator1
, &status
);
1258 if (U_FAILURE(status
)) {
1259 log_err("Somehow ran out of memory stepping through the iterator1.: %s\n", myErrorName(status
));
1262 order2
=ucol_getOffset(iterator2
);
1263 doAssert((order1
!= order2
), "The first iterator advance failed");
1264 order2
= ucol_next(iterator2
, &status
);
1265 if (U_FAILURE(status
)) {
1266 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1269 order3
= ucol_next(iterator3
, &status
);
1270 if (U_FAILURE(status
)) {
1271 log_err("Somehow ran out of memory stepping through the iterator3.: %s\n", myErrorName(status
));
1275 doAssert((order1
== order2
), "The second iterator advance failed should be the same as first one");
1277 doAssert( (ucol_primaryOrder(order1
) == ucol_primaryOrder(order3
)), "The primary orders should be identical");
1278 doAssert( (ucol_secondaryOrder(order1
) == ucol_secondaryOrder(order3
)), "The secondary orders should be identical");
1279 doAssert( (ucol_tertiaryOrder(order1
) == ucol_tertiaryOrder(order3
)), "The tertiary orders should be identical");
1281 order1
=ucol_next(iterator1
, &status
);
1282 if (U_FAILURE(status
)) {
1283 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1286 order3
=ucol_next(iterator3
, &status
);
1287 if (U_FAILURE(status
)) {
1288 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1291 doAssert( (ucol_primaryOrder(order1
) == ucol_primaryOrder(order3
)), "The primary orders should be identical");
1292 doAssert( (ucol_tertiaryOrder(order1
) != ucol_tertiaryOrder(order3
)), "The tertiary orders should be different");
1294 order1
=ucol_next(iterator1
, &status
);
1295 if (U_FAILURE(status
)) {
1296 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1299 order3
=ucol_next(iterator3
, &status
);
1300 if (U_FAILURE(status
)) {
1301 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1304 /* this here, my friends, is either pure lunacy or something so obsolete that even it's mother
1305 * doesn't care about it. Essentialy, this test complains if secondary values for 'I' and '_'
1306 * are the same. According to the UCA, this is not true. Therefore, remove the test.
1307 * Besides, if primary strengths for two code points are different, it doesn't matter one bit
1308 * what is the relation between secondary or any other strengths.
1309 * killed by weiv 06/11/2002.
1312 doAssert( ((order1 & UCOL_SECONDARYMASK) != (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be different");
1314 doAssert( (order1
!= UCOL_NULLORDER
), "Unexpected end of iterator reached");
1318 ucol_closeElements(iterator1
);
1319 ucol_closeElements(iterator2
);
1320 ucol_closeElements(iterator3
);
1323 log_verbose("testing CollationElementIterator ends...\n");
1326 void TestGetLocale() {
1327 UErrorCode status
= U_ZERO_ERROR
;
1328 const char *rules
= "&a<x<y<z";
1329 UChar rlz
[256] = {0};
1330 uint32_t rlzLen
= u_unescape(rules
, rlz
, 256);
1332 UCollator
*coll
= NULL
;
1333 const char *locale
= NULL
;
1337 /* Now that the collation tree is separate, actual==valid at all times. [alan] */
1338 static const struct {
1339 const char* requestedLocale
;
1340 const char* validLocale
;
1341 const char* actualLocale
;
1343 { "sr_RS", "sr_Cyrl_RS", "sr" },
1344 { "sh_YU", "sr_Latn_RS", "hr" }, /* this used to be sh, but now sh collation aliases hr */
1345 { "en_BE_FOO", "en_BE", "root" },
1346 { "de_DE_NONEXISTANT", "de_DE", "de" }
1349 /* test opening collators for different locales */
1350 for(i
= 0; i
<sizeof(testStruct
)/sizeof(testStruct
[0]); i
++) {
1351 status
= U_ZERO_ERROR
;
1352 coll
= ucol_open(testStruct
[i
].requestedLocale
, &status
);
1353 if(U_FAILURE(status
)) {
1354 log_err_status(status
, "Failed to open collator for %s with %s\n", testStruct
[i
].requestedLocale
, u_errorName(status
));
1358 locale
= ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
);
1359 if(strcmp(locale
, testStruct
[i
].requestedLocale
) != 0) {
1360 log_err("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].requestedLocale
, locale
);
1362 locale
= ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
);
1363 if(strcmp(locale
, testStruct
[i
].validLocale
) != 0) {
1364 log_err("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].validLocale
, locale
);
1366 locale
= ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
);
1367 if(strcmp(locale
, testStruct
[i
].actualLocale
) != 0) {
1368 log_err("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].actualLocale
, locale
);
1373 /* completely non-existant locale for collator should get a default collator */
1375 UCollator
*defaultColl
= ucol_open(NULL
, &status
);
1376 coll
= ucol_open("blahaha", &status
);
1377 if(U_SUCCESS(status
)) {
1378 if(strcmp(ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
), "blahaha")) {
1379 log_err("Nonexisting locale didn't preserve the requested locale\n");
1381 if(strcmp(ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
),
1382 ucol_getLocaleByType(defaultColl
, ULOC_VALID_LOCALE
, &status
))) {
1383 log_err("Valid locale for nonexisting locale locale collator differs "
1384 "from valid locale for default collator\n");
1386 if(strcmp(ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
),
1387 ucol_getLocaleByType(defaultColl
, ULOC_ACTUAL_LOCALE
, &status
))) {
1388 log_err("Actual locale for nonexisting locale locale collator differs "
1389 "from actual locale for default collator\n");
1392 ucol_close(defaultColl
);
1394 log_data_err("Couldn't open collators\n");
1400 /* collator instantiated from rules should have all three locales NULL */
1401 coll
= ucol_openRules(rlz
, rlzLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, NULL
, &status
);
1402 locale
= ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
);
1403 if(locale
!= NULL
) {
1404 log_err("For collator instantiated from rules, requested locale returned %s instead of NULL\n", locale
);
1406 locale
= ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
);
1407 if(locale
!= NULL
) {
1408 log_err("For collator instantiated from rules, valid locale returned %s instead of NULL\n", locale
);
1410 locale
= ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
);
1411 if(locale
!= NULL
) {
1412 log_err("For collator instantiated from rules, actual locale returned %s instead of NULL\n", locale
);
1422 count
=ucol_countAvailable();
1423 /* use something sensible w/o hardcoding the count */
1425 log_err("Error in countAvailable(), it returned %d\n", count
);
1428 log_verbose("PASS: countAvailable() successful, it returned %d\n", count
);
1430 for(i
=0;i
<count
;i
++)
1431 log_verbose("%s\n", ucol_getAvailable(i
));
1438 const char *original
;
1442 static int compare_teststruct(const void *string1
, const void *string2
) {
1443 return(strcmp((const char *)((struct teststruct
*)string1
)->key
, (const char *)((struct teststruct
*)string2
)->key
));
1447 UErrorCode status
= U_ZERO_ERROR
;
1449 UCollator
*coll
= ucol_open("sh", &status
);
1451 uint8_t sortkey
[512], lower
[512], upper
[512];
1454 static const char * const test
[] = {
1458 "j\\u00F6hn sm\\u00EFth",
1459 "J\\u00F6hn Sm\\u00EFth",
1460 "J\\u00D6HN SM\\u00CFTH",
1465 struct teststruct tests
[] = {
1466 {"\\u010CAKI MIHALJ" } ,
1467 {"\\u010CAKI MIHALJ" } ,
1468 {"\\u010CAKI PIRO\\u0160KA" },
1469 {"\\u010CABAI ANDRIJA" } ,
1470 {"\\u010CABAI LAJO\\u0160" } ,
1471 {"\\u010CABAI MARIJA" } ,
1472 {"\\u010CABAI STEVAN" } ,
1473 {"\\u010CABAI STEVAN" } ,
1474 {"\\u010CABARKAPA BRANKO" } ,
1475 {"\\u010CABARKAPA MILENKO" } ,
1476 {"\\u010CABARKAPA MIROSLAV" } ,
1477 {"\\u010CABARKAPA SIMO" } ,
1478 {"\\u010CABARKAPA STANKO" } ,
1479 {"\\u010CABARKAPA TAMARA" } ,
1480 {"\\u010CABARKAPA TOMA\\u0160" } ,
1481 {"\\u010CABDARI\\u0106 NIKOLA" } ,
1482 {"\\u010CABDARI\\u0106 ZORICA" } ,
1483 {"\\u010CABI NANDOR" } ,
1484 {"\\u010CABOVI\\u0106 MILAN" } ,
1485 {"\\u010CABRADI AGNEZIJA" } ,
1486 {"\\u010CABRADI IVAN" } ,
1487 {"\\u010CABRADI JELENA" } ,
1488 {"\\u010CABRADI LJUBICA" } ,
1489 {"\\u010CABRADI STEVAN" } ,
1490 {"\\u010CABRDA MARTIN" } ,
1491 {"\\u010CABRILO BOGDAN" } ,
1492 {"\\u010CABRILO BRANISLAV" } ,
1493 {"\\u010CABRILO LAZAR" } ,
1494 {"\\u010CABRILO LJUBICA" } ,
1495 {"\\u010CABRILO SPASOJA" } ,
1496 {"\\u010CADE\\u0160 ZDENKA" } ,
1497 {"\\u010CADESKI BLAGOJE" } ,
1498 {"\\u010CADOVSKI VLADIMIR" } ,
1499 {"\\u010CAGLJEVI\\u0106 TOMA" } ,
1500 {"\\u010CAGOROVI\\u0106 VLADIMIR" } ,
1501 {"\\u010CAJA VANKA" } ,
1502 {"\\u010CAJI\\u0106 BOGOLJUB" } ,
1503 {"\\u010CAJI\\u0106 BORISLAV" } ,
1504 {"\\u010CAJI\\u0106 RADOSLAV" } ,
1505 {"\\u010CAK\\u0160IRAN MILADIN" } ,
1506 {"\\u010CAKAN EUGEN" } ,
1507 {"\\u010CAKAN EVGENIJE" } ,
1508 {"\\u010CAKAN IVAN" } ,
1509 {"\\u010CAKAN JULIJAN" } ,
1510 {"\\u010CAKAN MIHAJLO" } ,
1511 {"\\u010CAKAN STEVAN" } ,
1512 {"\\u010CAKAN VLADIMIR" } ,
1513 {"\\u010CAKAN VLADIMIR" } ,
1514 {"\\u010CAKAN VLADIMIR" } ,
1515 {"\\u010CAKARA ANA" } ,
1516 {"\\u010CAKAREVI\\u0106 MOMIR" } ,
1517 {"\\u010CAKAREVI\\u0106 NEDELJKO" } ,
1518 {"\\u010CAKI \\u0160ANDOR" } ,
1519 {"\\u010CAKI AMALIJA" } ,
1520 {"\\u010CAKI ANDRA\\u0160" } ,
1521 {"\\u010CAKI LADISLAV" } ,
1522 {"\\u010CAKI LAJO\\u0160" } ,
1523 {"\\u010CAKI LASLO" } ,
1528 int32_t i
= 0, j
= 0, k
= 0, buffSize
= 0, skSize
= 0, lowerSize
= 0, upperSize
= 0;
1529 int32_t arraySize
= sizeof(tests
)/sizeof(tests
[0]);
1531 if(U_SUCCESS(status
) && coll
) {
1532 for(i
= 0; i
<arraySize
; i
++) {
1533 buffSize
= u_unescape(tests
[i
].original
, buffer
, 512);
1534 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, tests
[i
].key
, 512);
1537 qsort(tests
, arraySize
, sizeof(struct teststruct
), compare_teststruct
);
1539 for(i
= 0; i
< arraySize
-1; i
++) {
1540 for(j
= i
+1; j
< arraySize
; j
++) {
1541 lowerSize
= ucol_getBound(tests
[i
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1542 upperSize
= ucol_getBound(tests
[j
].key
, -1, UCOL_BOUND_UPPER
, 1, upper
, 512, &status
);
1543 for(k
= i
; k
<= j
; k
++) {
1544 if(strcmp((const char *)lower
, (const char *)tests
[k
].key
) > 0) {
1545 log_err("Problem with lower! j = %i (%s vs %s)\n", k
, tests
[k
].original
, tests
[i
].original
);
1547 if(strcmp((const char *)upper
, (const char *)tests
[k
].key
) <= 0) {
1548 log_err("Problem with upper! j = %i (%s vs %s)\n", k
, tests
[k
].original
, tests
[j
].original
);
1556 for(i
= 0; i
< 1000; i
++) {
1557 lowerRND
= (rand()/(RAND_MAX
/arraySize
));
1558 upperRND
= lowerRND
+ (rand()/(RAND_MAX
/(arraySize
-lowerRND
)));
1560 lowerSize
= ucol_getBound(tests
[lowerRND
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1561 upperSize
= ucol_getBound(tests
[upperRND
].key
, -1, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1563 for(j
= lowerRND
; j
<=upperRND
; j
++) {
1564 if(strcmp(lower
, tests
[j
].key
) > 0) {
1565 log_err("Problem with lower! j = %i (%s vs %s)\n", j
, tests
[j
].original
, tests
[lowerRND
].original
);
1567 if(strcmp(upper
, tests
[j
].key
) <= 0) {
1568 log_err("Problem with upper! j = %i (%s vs %s)\n", j
, tests
[j
].original
, tests
[upperRND
].original
);
1578 for(i
= 0; i
<sizeof(test
)/sizeof(test
[0]); i
++) {
1579 buffSize
= u_unescape(test
[i
], buffer
, 512);
1580 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, sortkey
, 512);
1581 lowerSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1582 upperSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1583 for(j
= i
+1; j
<sizeof(test
)/sizeof(test
[0]); j
++) {
1584 buffSize
= u_unescape(test
[j
], buffer
, 512);
1585 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, sortkey
, 512);
1586 if(strcmp((const char *)lower
, (const char *)sortkey
) > 0) {
1587 log_err("Problem with lower! i = %i, j = %i (%s vs %s)\n", i
, j
, test
[i
], test
[j
]);
1589 if(strcmp((const char *)upper
, (const char *)sortkey
) <= 0) {
1590 log_err("Problem with upper! i = %i, j = %i (%s vs %s)\n", i
, j
, test
[i
], test
[j
]);
1596 log_data_err("Couldn't open collator\n");
1601 static void doOverrunTest(UCollator
*coll
, const UChar
*uString
, int32_t strLen
) {
1602 int32_t skLen
= 0, skLen2
= 0;
1603 uint8_t sortKey
[256];
1605 uint8_t filler
= 0xFF;
1607 skLen
= ucol_getSortKey(coll
, uString
, strLen
, NULL
, 0);
1609 for(i
= 0; i
< skLen
; i
++) {
1610 memset(sortKey
, filler
, 256);
1611 skLen2
= ucol_getSortKey(coll
, uString
, strLen
, sortKey
, i
);
1612 if(skLen
!= skLen2
) {
1613 log_err("For buffer size %i, got different sortkey length. Expected %i got %i\n", i
, skLen
, skLen2
);
1615 for(j
= i
; j
< 256; j
++) {
1616 if(sortKey
[j
] != filler
) {
1617 log_err("Something run over index %i\n", j
);
1624 /* j1865 reports that if a shorter buffer is passed to
1625 * to get sort key, a buffer overrun happens in some
1626 * cases. This test tries to check this.
1628 void TestSortKeyBufferOverrun(void) {
1629 UErrorCode status
= U_ZERO_ERROR
;
1630 const char* cString
= "A very Merry liTTle-lamB..";
1633 UCollator
*coll
= ucol_open("root", &status
);
1634 strLen
= u_unescape(cString
, uString
, 256);
1636 if(U_SUCCESS(status
)) {
1637 log_verbose("testing non ignorable\n");
1638 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
1639 doOverrunTest(coll
, uString
, strLen
);
1641 log_verbose("testing shifted\n");
1642 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &status
);
1643 doOverrunTest(coll
, uString
, strLen
);
1645 log_verbose("testing shifted quaternary\n");
1646 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &status
);
1647 doOverrunTest(coll
, uString
, strLen
);
1649 log_verbose("testing with french secondaries\n");
1650 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
1651 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
1652 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
1653 doOverrunTest(coll
, uString
, strLen
);
1659 static void TestAttribute()
1661 UErrorCode error
= U_ZERO_ERROR
;
1662 UCollator
*coll
= ucol_open(NULL
, &error
);
1664 if (U_FAILURE(error
)) {
1665 log_err_status(error
, "Creation of default collator failed\n");
1669 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_OFF
, &error
);
1670 if (ucol_getAttribute(coll
, UCOL_FRENCH_COLLATION
, &error
) != UCOL_OFF
||
1672 log_err_status(error
, "Setting and retrieving of the french collation failed\n");
1675 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &error
);
1676 if (ucol_getAttribute(coll
, UCOL_FRENCH_COLLATION
, &error
) != UCOL_ON
||
1678 log_err_status(error
, "Setting and retrieving of the french collation failed\n");
1681 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &error
);
1682 if (ucol_getAttribute(coll
, UCOL_ALTERNATE_HANDLING
, &error
) != UCOL_SHIFTED
||
1684 log_err_status(error
, "Setting and retrieving of the alternate handling failed\n");
1687 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &error
);
1688 if (ucol_getAttribute(coll
, UCOL_ALTERNATE_HANDLING
, &error
) != UCOL_NON_IGNORABLE
||
1690 log_err_status(error
, "Setting and retrieving of the alternate handling failed\n");
1693 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, &error
);
1694 if (ucol_getAttribute(coll
, UCOL_CASE_FIRST
, &error
) != UCOL_LOWER_FIRST
||
1696 log_err_status(error
, "Setting and retrieving of the case first attribute failed\n");
1699 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, &error
);
1700 if (ucol_getAttribute(coll
, UCOL_CASE_FIRST
, &error
) != UCOL_UPPER_FIRST
||
1702 log_err_status(error
, "Setting and retrieving of the case first attribute failed\n");
1705 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_ON
, &error
);
1706 if (ucol_getAttribute(coll
, UCOL_CASE_LEVEL
, &error
) != UCOL_ON
||
1708 log_err_status(error
, "Setting and retrieving of the case level attribute failed\n");
1711 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_OFF
, &error
);
1712 if (ucol_getAttribute(coll
, UCOL_CASE_LEVEL
, &error
) != UCOL_OFF
||
1714 log_err_status(error
, "Setting and retrieving of the case level attribute failed\n");
1717 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &error
);
1718 if (ucol_getAttribute(coll
, UCOL_NORMALIZATION_MODE
, &error
) != UCOL_ON
||
1720 log_err_status(error
, "Setting and retrieving of the normalization on/off attribute failed\n");
1723 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_OFF
, &error
);
1724 if (ucol_getAttribute(coll
, UCOL_NORMALIZATION_MODE
, &error
) != UCOL_OFF
||
1726 log_err_status(error
, "Setting and retrieving of the normalization on/off attribute failed\n");
1729 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_PRIMARY
, &error
);
1730 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_PRIMARY
||
1732 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1735 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_SECONDARY
, &error
);
1736 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_SECONDARY
||
1738 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1741 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &error
);
1742 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_TERTIARY
||
1744 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1747 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &error
);
1748 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_QUATERNARY
||
1750 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1753 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &error
);
1754 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_IDENTICAL
||
1756 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1762 void TestGetTailoredSet() {
1765 const char *tests
[20];
1768 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1769 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1772 int32_t i
= 0, j
= 0;
1773 UErrorCode status
= U_ZERO_ERROR
;
1776 UCollator
*coll
= NULL
;
1778 int32_t buffLen
= 0;
1781 for(i
= 0; i
< sizeof(setTest
)/sizeof(setTest
[0]); i
++) {
1782 buffLen
= u_unescape(setTest
[i
].rules
, buff
, 1024);
1783 coll
= ucol_openRules(buff
, buffLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, &pError
, &status
);
1784 if(U_SUCCESS(status
)) {
1785 set
= ucol_getTailoredSet(coll
, &status
);
1786 if(uset_size(set
) != setTest
[i
].testsize
) {
1787 log_err("Tailored set size different (%d) than expected (%d)\n", uset_size(set
), setTest
[i
].testsize
);
1789 for(j
= 0; j
< setTest
[i
].testsize
; j
++) {
1790 buffLen
= u_unescape(setTest
[i
].tests
[j
], buff
, 1024);
1791 if(!uset_containsString(set
, buff
, buffLen
)) {
1792 log_err("Tailored set doesn't contain %s... It should\n", setTest
[i
].tests
[j
]);
1797 log_err_status(status
, "Couldn't open collator with rules %s\n", setTest
[i
].rules
);
1803 static int tMemCmp(const uint8_t *first
, const uint8_t *second
) {
1804 int32_t firstLen
= (int32_t)strlen((const char *)first
);
1805 int32_t secondLen
= (int32_t)strlen((const char *)second
);
1806 return memcmp(first
, second
, uprv_min(firstLen
, secondLen
));
1808 static const char * strengthsC
[] = {
1816 void TestMergeSortKeys(void) {
1817 UErrorCode status
= U_ZERO_ERROR
;
1818 UCollator
*coll
= ucol_open("en", &status
);
1819 if(U_SUCCESS(status
)) {
1821 const char* cases
[] = {
1826 uint32_t casesSize
= sizeof(cases
)/sizeof(cases
[0]);
1827 const char* prefix
= "foo";
1828 const char* suffix
= "egg";
1829 char outBuff1
[256], outBuff2
[256];
1831 uint8_t **sortkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1832 uint8_t **mergedPrefixkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1833 uint8_t **mergedSuffixkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1834 uint32_t *sortKeysLen
= (uint32_t *)malloc(casesSize
*sizeof(uint32_t));
1835 uint8_t prefixKey
[256], suffixKey
[256];
1836 uint32_t prefixKeyLen
= 0, suffixKeyLen
= 0, i
= 0;
1838 uint32_t unescapedLen
= 0, l1
= 0, l2
= 0;
1839 UColAttributeValue strength
;
1841 log_verbose("ucol_mergeSortkeys test\n");
1842 log_verbose("Testing order of the test cases\n");
1843 genericLocaleStarter("en", cases
, casesSize
);
1845 for(i
= 0; i
<casesSize
; i
++) {
1846 sortkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1847 mergedPrefixkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1848 mergedSuffixkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1851 unescapedLen
= u_unescape(prefix
, buffer
, 256);
1852 prefixKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, prefixKey
, 256);
1854 unescapedLen
= u_unescape(suffix
, buffer
, 256);
1855 suffixKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, suffixKey
, 256);
1857 log_verbose("Massaging data with prefixes and different strengths\n");
1858 strength
= UCOL_PRIMARY
;
1859 while(strength
<= UCOL_IDENTICAL
) {
1860 log_verbose("Strength %s\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1861 ucol_setAttribute(coll
, UCOL_STRENGTH
, strength
, &status
);
1862 for(i
= 0; i
<casesSize
; i
++) {
1863 unescapedLen
= u_unescape(cases
[i
], buffer
, 256);
1864 sortKeysLen
[i
] = ucol_getSortKey(coll
, buffer
, unescapedLen
, sortkeys
[i
], 256);
1865 ucol_mergeSortkeys(prefixKey
, prefixKeyLen
, sortkeys
[i
], sortKeysLen
[i
], mergedPrefixkeys
[i
], 256);
1866 ucol_mergeSortkeys(sortkeys
[i
], sortKeysLen
[i
], suffixKey
, suffixKeyLen
, mergedSuffixkeys
[i
], 256);
1868 if(tMemCmp(mergedPrefixkeys
[i
-1], mergedPrefixkeys
[i
]) >= 0) {
1869 log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1871 ucol_sortKeyToString(coll
, mergedPrefixkeys
[i
-1], outBuff1
, &l1
),
1872 ucol_sortKeyToString(coll
, mergedPrefixkeys
[i
], outBuff2
, &l2
));
1874 if(tMemCmp(mergedSuffixkeys
[i
-1], mergedSuffixkeys
[i
]) >= 0) {
1875 log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1877 ucol_sortKeyToString(coll
, mergedSuffixkeys
[i
-1], outBuff1
, &l1
),
1878 ucol_sortKeyToString(coll
, mergedSuffixkeys
[i
], outBuff2
, &l2
));
1882 if(strength
== UCOL_QUATERNARY
) {
1883 strength
= UCOL_IDENTICAL
;
1890 uint8_t smallBuf
[3];
1891 uint32_t reqLen
= 0;
1892 log_verbose("testing buffer overflow\n");
1893 reqLen
= ucol_mergeSortkeys(prefixKey
, prefixKeyLen
, suffixKey
, suffixKeyLen
, smallBuf
, 3);
1894 if(reqLen
!= (prefixKeyLen
+suffixKeyLen
-1)) {
1895 log_err("Wrong preflight size for merged sortkey\n");
1901 uint8_t emptyKey
[20], abcKey
[50], mergedKey
[100];
1902 int32_t emptyKeyLen
= 0, abcKeyLen
= 0, mergedKeyLen
= 0;
1904 log_verbose("testing merging with sortkeys generated for empty strings\n");
1905 emptyKeyLen
= ucol_getSortKey(coll
, &empty
, 0, emptyKey
, 20);
1906 unescapedLen
= u_unescape(cases
[0], buffer
, 256);
1907 abcKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, abcKey
, 50);
1908 mergedKeyLen
= ucol_mergeSortkeys(emptyKey
, emptyKeyLen
, abcKey
, abcKeyLen
, mergedKey
, 100);
1909 if(mergedKey
[0] != 2) {
1910 log_err("Empty sortkey didn't produce a level separator\n");
1912 /* try with zeros */
1913 mergedKeyLen
= ucol_mergeSortkeys(emptyKey
, 0, abcKey
, abcKeyLen
, mergedKey
, 100);
1914 if(mergedKeyLen
!= 0 || mergedKey
[0] != 0) {
1915 log_err("Empty key didn't produce null mergedKey\n");
1917 mergedKeyLen
= ucol_mergeSortkeys(abcKey
, abcKeyLen
, emptyKey
, 0, mergedKey
, 100);
1918 if(mergedKeyLen
!= 0 || mergedKey
[0] != 0) {
1919 log_err("Empty key didn't produce null mergedKey\n");
1924 for(i
= 0; i
<casesSize
; i
++) {
1926 free(mergedPrefixkeys
[i
]);
1927 free(mergedSuffixkeys
[i
]);
1930 free(mergedPrefixkeys
);
1931 free(mergedSuffixkeys
);
1934 /* need to finish this up */
1936 log_data_err("Couldn't open collator");
1939 static void TestShortString(void)
1943 const char *expectedOutput
;
1945 UErrorCode expectedStatus
;
1946 int32_t expectedOffset
;
1947 uint32_t expectedIdentifier
;
1950 * The following expectedOutput contains a collation weight (2700 from UCA 6.0)
1951 * which is the primary weight for the T character (U+0041) in the input.
1952 * When that character gets a different weight in FractionalUCA.txt,
1953 * the expectedOutput needs to be adjusted.
1954 * That is, when we upgrade to a new UCA version or change collation
1955 * in such a way that the absolute weight for 'A' changes,
1956 * we will get a test failure here and need to adjust the test case.
1958 {"LDE_RDE_KPHONEBOOK_T0041_ZLATN","B2700_KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING
, 0, 0 },
1960 {"LEN_RUS_NO_AS_S4","AS_LROOT_NO_S4", NULL
, U_USING_DEFAULT_WARNING
, 0, 0 },
1961 {"LDE_VPHONEBOOK_EO_SI","EO_KPHONEBOOK_LDE_SI", "de@collation=phonebook", U_ZERO_ERROR
, 0, 0 },
1962 {"LDE_Kphonebook","KPHONEBOOK_LDE", "de@collation=phonebook", U_ZERO_ERROR
, 0, 0 },
1963 {"Xqde_DE@collation=phonebookq_S3_EX","KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING
, 0, 0 },
1964 {"LFR_FO", "FO_LROOT", NULL
, U_USING_DEFAULT_WARNING
, 0, 0 },
1965 {"SO_LX_AS", "", NULL
, U_ILLEGAL_ARGUMENT_ERROR
, 8, 0 },
1966 {"S3_ASS_MMM", "", NULL
, U_ILLEGAL_ARGUMENT_ERROR
, 5, 0 }
1970 UCollator
*coll
= NULL
, *fromNormalized
= NULL
;
1971 UParseError parseError
;
1972 UErrorCode status
= U_ZERO_ERROR
;
1973 char fromShortBuffer
[256], normalizedBuffer
[256], fromNormalizedBuffer
[256];
1974 const char* locale
= NULL
;
1977 for(i
= 0; i
< sizeof(testCases
)/sizeof(testCases
[0]); i
++) {
1978 status
= U_ZERO_ERROR
;
1979 if(testCases
[i
].locale
) {
1980 locale
= testCases
[i
].locale
;
1985 coll
= ucol_openFromShortString(testCases
[i
].input
, FALSE
, &parseError
, &status
);
1986 if(status
!= testCases
[i
].expectedStatus
) {
1987 log_err_status(status
, "Got status '%s' that is different from expected '%s' for '%s'\n",
1988 u_errorName(status
), u_errorName(testCases
[i
].expectedStatus
), testCases
[i
].input
);
1992 if(U_SUCCESS(status
)) {
1993 ucol_getShortDefinitionString(coll
, locale
, fromShortBuffer
, 256, &status
);
1995 if(strcmp(fromShortBuffer
, testCases
[i
].expectedOutput
)) {
1996 log_err("Got short string '%s' from the collator. Expected '%s' for input '%s'\n",
1997 fromShortBuffer
, testCases
[i
].expectedOutput
, testCases
[i
].input
);
2000 ucol_normalizeShortDefinitionString(testCases
[i
].input
, normalizedBuffer
, 256, &parseError
, &status
);
2001 fromNormalized
= ucol_openFromShortString(normalizedBuffer
, FALSE
, &parseError
, &status
);
2002 ucol_getShortDefinitionString(fromNormalized
, locale
, fromNormalizedBuffer
, 256, &status
);
2004 if(strcmp(fromShortBuffer
, fromNormalizedBuffer
)) {
2005 log_err("Strings obtained from collators instantiated by short string ('%s') and from normalized string ('%s') differ\n",
2006 fromShortBuffer
, fromNormalizedBuffer
);
2010 if(!ucol_equals(coll
, fromNormalized
)) {
2011 log_err("Collator from short string ('%s') differs from one obtained through a normalized version ('%s')\n",
2012 testCases
[i
].input
, normalizedBuffer
);
2015 ucol_close(fromNormalized
);
2019 if(parseError
.offset
!= testCases
[i
].expectedOffset
) {
2020 log_err("Got parse error offset %i, but expected %i instead for '%s'\n",
2021 parseError
.offset
, testCases
[i
].expectedOffset
, testCases
[i
].input
);
2029 doSetsTest(const char *locale
, const USet
*ref
, USet
*set
, const char* inSet
, const char* outSet
, UErrorCode
*status
) {
2034 bufLen
= u_unescape(inSet
, buffer
, 512);
2035 uset_applyPattern(set
, buffer
, bufLen
, 0, status
);
2036 if(U_FAILURE(*status
)) {
2037 log_err("%s: Failure setting pattern %s\n", locale
, u_errorName(*status
));
2040 if(!uset_containsAll(ref
, set
)) {
2041 log_err("%s: Some stuff from %s is not present in the set\n", locale
, inSet
);
2045 bufLen
= u_unescape(outSet
, buffer
, 512);
2046 uset_applyPattern(set
, buffer
, bufLen
, 0, status
);
2047 if(U_FAILURE(*status
)) {
2048 log_err("%s: Failure setting pattern %s\n", locale
, u_errorName(*status
));
2051 if(!uset_containsNone(ref
, set
)) {
2052 log_err("%s: Some stuff from %s is present in the set\n", locale
, outSet
);
2060 TestGetContractionsAndUnsafes(void)
2064 const char* inConts
;
2065 const char* outConts
;
2068 const char* unsafeCodeUnits
;
2069 const char* safeCodeUnits
;
2072 "[{\\u0418\\u0306}{\\u0438\\u0306}]",
2077 "[aAbB\\u0430\\u0410\\u0433\\u0413]"
2080 "[{\\u0406\\u0308}{\\u0456\\u0308}{\\u0418\\u0306}{\\u0438\\u0306}]",
2081 "[\\u0407\\u0419\\u0439\\u0457]",
2084 "[\\u0406\\u0456\\u0418\\u0438]",
2088 "[{C\\u0301}{C\\u030C}{C\\u0341}{DZ\\u030C}{Dz\\u030C}{D\\u017D}{D\\u017E}{lj}{nj}]",
2089 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
2096 "[{\\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}]",
2097 "[{\\u30FD\\u3099}{\\u309D\\u3099}{\\u3053\\u3099}{\\u30B3\\u3099}{lj}{nj}]",
2108 UErrorCode status
= U_ZERO_ERROR
;
2109 UCollator
*coll
= NULL
;
2111 int32_t noConts
= 0;
2112 USet
*conts
= uset_open(0,0);
2113 USet
*exp
= uset_open(0, 0);
2114 USet
*set
= uset_open(0,0);
2115 int32_t setBufferLen
= 65536;
2116 UChar buffer
[65536];
2119 for(i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++) {
2120 log_verbose("Testing locale: %s\n", tests
[i
].locale
);
2121 coll
= ucol_open(tests
[i
].locale
, &status
);
2122 if (coll
== NULL
|| U_FAILURE(status
)) {
2123 log_err_status(status
, "Unable to open collator for locale %s ==> %s\n", tests
[i
].locale
, u_errorName(status
));
2126 ucol_getContractionsAndExpansions(coll
, conts
, exp
, TRUE
, &status
);
2127 doSetsTest(tests
[i
].locale
, conts
, set
, tests
[i
].inConts
, tests
[i
].outConts
, &status
);
2128 setLen
= uset_toPattern(conts
, buffer
, setBufferLen
, TRUE
, &status
);
2129 if(U_SUCCESS(status
)) {
2130 /*log_verbose("Contractions %i: %s\n", uset_getItemCount(conts), aescstrdup(buffer, setLen));*/
2132 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2133 status
= U_ZERO_ERROR
;
2135 doSetsTest(tests
[i
].locale
, exp
, set
, tests
[i
].inExp
, tests
[i
].outExp
, &status
);
2136 setLen
= uset_toPattern(exp
, buffer
, setBufferLen
, TRUE
, &status
);
2137 if(U_SUCCESS(status
)) {
2138 /*log_verbose("Expansions %i: %s\n", uset_getItemCount(exp), aescstrdup(buffer, setLen));*/
2140 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2141 status
= U_ZERO_ERROR
;
2144 noConts
= ucol_getUnsafeSet(coll
, conts
, &status
);
2145 doSetsTest(tests
[i
].locale
, conts
, set
, tests
[i
].unsafeCodeUnits
, tests
[i
].safeCodeUnits
, &status
);
2146 setLen
= uset_toPattern(conts
, buffer
, setBufferLen
, TRUE
, &status
);
2147 if(U_SUCCESS(status
)) {
2148 log_verbose("Unsafe %i: %s\n", uset_getItemCount(exp
), aescstrdup(buffer
, setLen
));
2150 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2151 status
= U_ZERO_ERROR
;
2164 TestOpenBinary(void)
2166 UErrorCode status
= U_ZERO_ERROR
;
2168 char rule[] = "&h < d < c < b";
2169 char *wUCA[] = { "a", "h", "d", "c", "b", "i" };
2170 char *noUCA[] = {"d", "c", "b", "a", "h", "i" };
2172 /* we have to use Cyrillic letters because latin-1 always gets copied */
2173 const char rule
[] = "&\\u0452 < \\u0434 < \\u0433 < \\u0432"; /* &dje < d < g < v */
2174 const char *wUCA
[] = { "\\u0430", "\\u0452", "\\u0434", "\\u0433", "\\u0432", "\\u0435" }; /* a, dje, d, g, v, e */
2175 const char *noUCA
[] = {"\\u0434", "\\u0433", "\\u0432", "\\u0430", "\\u0435", "\\u0452" }; /* d, g, v, a, e, dje */
2178 int32_t uRulesLen
= u_unescape(rule
, uRules
, 256);
2180 UCollator
*coll
= ucol_openRules(uRules
, uRulesLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, NULL
, &status
);
2181 UCollator
*UCA
= NULL
;
2182 UCollator
*cloneNOUCA
= NULL
, *cloneWUCA
= NULL
;
2184 uint8_t imageBuffer
[32768];
2185 uint8_t *image
= imageBuffer
;
2186 int32_t imageBufferCapacity
= 32768;
2190 if((coll
==NULL
)||(U_FAILURE(status
))) {
2191 log_data_err("could not load collators or error occured: %s\n",
2192 u_errorName(status
));
2195 UCA
= ucol_open("root", &status
);
2196 if((UCA
==NULL
)||(U_FAILURE(status
))) {
2197 log_data_err("could not load UCA collator or error occured: %s\n",
2198 u_errorName(status
));
2201 imageSize
= ucol_cloneBinary(coll
, image
, imageBufferCapacity
, &status
);
2202 if(U_FAILURE(status
)) {
2203 image
= (uint8_t *)malloc(imageSize
*sizeof(uint8_t));
2204 status
= U_ZERO_ERROR
;
2205 imageSize
= ucol_cloneBinary(coll
, imageBuffer
, imageSize
, &status
);
2209 cloneWUCA
= ucol_openBinary(image
, imageSize
, UCA
, &status
);
2210 cloneNOUCA
= ucol_openBinary(image
, imageSize
, NULL
, &status
);
2212 genericOrderingTest(coll
, wUCA
, sizeof(wUCA
)/sizeof(wUCA
[0]));
2214 genericOrderingTest(cloneWUCA
, wUCA
, sizeof(wUCA
)/sizeof(wUCA
[0]));
2215 genericOrderingTest(cloneNOUCA
, noUCA
, sizeof(noUCA
)/sizeof(noUCA
[0]));
2217 if(image
!= imageBuffer
) {
2221 ucol_close(cloneNOUCA
);
2222 ucol_close(cloneWUCA
);
2226 static void TestDefault(void) {
2227 /* Tests for code coverage. */
2228 UErrorCode status
= U_ZERO_ERROR
;
2229 UCollator
*coll
= ucol_open("es@collation=pinyin", &status
);
2230 if (coll
== NULL
|| status
== U_FILE_ACCESS_ERROR
) {
2231 log_data_err("Unable to open collator es@collation=pinyin\n");
2234 if (status
!= U_USING_DEFAULT_WARNING
) {
2235 /* What do you mean that you know about using pinyin collation in Spanish!? This should be in the zh locale. */
2236 log_err("es@collation=pinyin should return U_USING_DEFAULT_WARNING, but returned %s\n", u_errorName(status
));
2239 if (ucol_getKeywordValues("funky", &status
) != NULL
) {
2240 log_err("Collators should not know about the funky keyword.\n");
2242 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
2243 log_err("funky keyword didn't fail as expected %s\n", u_errorName(status
));
2245 if (ucol_getKeywordValues("collation", &status
) != NULL
) {
2246 log_err("ucol_getKeywordValues should not work when given a bad status.\n");
2250 static void TestDefaultKeyword(void) {
2251 /* Tests for code coverage. */
2252 UErrorCode status
= U_ZERO_ERROR
;
2253 const char *loc
= "zh_TW@collation=default";
2254 UCollator
*coll
= ucol_open(loc
, &status
);
2255 if(U_FAILURE(status
)) {
2256 log_info("Warning: ucol_open(%s, ...) returned %s, at least it didn't crash.\n", loc
, u_errorName(status
));
2257 } else if (status
!= U_USING_FALLBACK_WARNING
) {
2258 /* Hmm, skip the following test for CLDR 1.9 data and/or ICU 4.6, no longer seems to apply */
2260 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
));
2266 static void TestGetKeywordValuesForLocale(void) {
2267 #define INCLUDE_UNIHAN_COLLATION 1
2268 #define PREFERRED_SIZE 16
2269 #define MAX_NUMBER_OF_KEYWORDS 8
2270 const char *PREFERRED
[PREFERRED_SIZE
][MAX_NUMBER_OF_KEYWORDS
+1] = {
2271 { "und", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2272 { "en_US", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2273 { "en_029", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2274 { "de_DE", "standard", "phonebook", "search", "ducet", NULL
, NULL
, NULL
, NULL
},
2275 { "de_Latn_DE", "standard", "phonebook", "search", "ducet", NULL
, NULL
, NULL
, NULL
},
2276 #if INCLUDE_UNIHAN_COLLATION
2277 { "zh", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2278 { "zh_Hans", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2279 { "zh_CN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2280 { "zh_Hant", "stroke", "big5han", "gb2312han", "pinyin", "standard", "unihan", "ducet", "search" },
2281 { "zh_TW", "stroke", "big5han", "gb2312han", "pinyin", "standard", "unihan", "ducet", "search" },
2282 { "zh__PINYIN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2284 { "zh", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL
},
2285 { "zh_Hans", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL
},
2286 { "zh_CN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL
},
2287 { "zh_Hant", "stroke", "big5han", "gb2312han", "pinyin", "standard", "ducet", "search", NULL
},
2288 { "zh_TW", "stroke", "big5han", "gb2312han", "pinyin", "standard", "ducet", "search", NULL
},
2289 { "zh__PINYIN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL
},
2291 { "es_ES", "standard", "search", "traditional", "ducet", NULL
, NULL
, NULL
, NULL
},
2292 { "es__TRADITIONAL","traditional", "search", "standard", "ducet", NULL
, NULL
, NULL
, NULL
},
2293 { "und@collation=phonebook", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2294 { "de_DE@collation=big5han", "standard", "phonebook", "search", "ducet", NULL
, NULL
, NULL
, NULL
},
2295 { "zzz@collation=xxx", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
}
2297 #if INCLUDE_UNIHAN_COLLATION
2298 const int32_t expectedLength
[PREFERRED_SIZE
] = { 3, 3, 3, 4, 4, 8, 8, 8, 8, 8, 8, 4, 4, 3, 4, 3 };
2300 const int32_t expectedLength
[PREFERRED_SIZE
] = { 3, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 4, 4, 3, 4, 3 };
2303 UErrorCode status
= U_ZERO_ERROR
;
2304 UEnumeration
*keywordValues
= NULL
;
2305 int32_t i
, n
, size
, valueLength
;
2306 const char *locale
= NULL
, *value
= NULL
;
2307 UBool errorOccurred
= FALSE
;
2309 for (i
= 0; i
< PREFERRED_SIZE
; i
++) {
2310 locale
= PREFERRED
[i
][0];
2315 keywordValues
= ucol_getKeywordValuesForLocale("collation", locale
, TRUE
, &status
);
2316 if (keywordValues
== NULL
|| U_FAILURE(status
)) {
2317 log_err_status(status
, "Error getting keyword values: %s\n", u_errorName(status
));
2320 size
= uenum_count(keywordValues
, &status
);
2322 if (size
== expectedLength
[i
]) {
2323 for (n
= 0; n
< expectedLength
[i
]; n
++) {
2324 if ((value
= uenum_next(keywordValues
, &valueLength
, &status
)) != NULL
&& U_SUCCESS(status
)) {
2325 if (uprv_strcmp(value
, PREFERRED
[i
][n
+1]) != 0) {
2326 log_err("Keyword values differ: Got [%s] Expected [%s] for locale: %s\n", value
, PREFERRED
[i
][n
+1], locale
);
2327 errorOccurred
= TRUE
;
2332 log_err("While getting keyword value from locale: %s got this error: %s\n", locale
, u_errorName(status
));
2333 errorOccurred
= TRUE
;
2337 if (errorOccurred
) {
2341 log_err("Number of keywords (%d) does not match expected size (%d) for locale: %s\n", size
, expectedLength
[i
], locale
);
2344 uenum_close(keywordValues
);
2345 keywordValues
= NULL
;
2347 if (keywordValues
!= NULL
) {
2348 uenum_close(keywordValues
);
2353 #endif /* #if !UCONFIG_NO_COLLATION */