1 /********************************************************************
2 * Copyright (c) 1997-2012 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
);
173 /* We don't have default rules, at least not in the previous sense */
174 void TestGetDefaultRules(){
176 UErrorCode status
=U_ZERO_ERROR
;
177 UCollator
*coll
=NULL
;
178 int32_t len1
= 0, len2
=0;
179 uint8_t *binColData
= NULL
;
181 UResourceBundle
*res
= NULL
;
182 UResourceBundle
*binColl
= NULL
;
183 uint8_t *binResult
= NULL
;
186 const UChar
* defaultRulesArray
=ucol_getDefaultRulesArray(&size
);
187 log_verbose("Test the function ucol_getDefaultRulesArray()\n");
189 coll
= ucol_openRules(defaultRulesArray
, size
, UCOL_ON
, UCOL_PRIMARY
, &status
);
190 if(U_SUCCESS(status
) && coll
!=NULL
) {
191 binColData
= (uint8_t*)ucol_cloneRuleData(coll
, &len1
, &status
);
197 res
=ures_open(NULL
, "root", &status
);
198 if(U_FAILURE(status
)){
199 log_err("ERROR: Failed to get resource for \"root Locale\" with %s", myErrorName(status
));
202 binColl
=ures_getByKey(res
, "%%Collation", binColl
, &status
);
203 if(U_SUCCESS(status
)){
204 binResult
=(uint8_t*)ures_getBinary(binColl
, &len2
, &status
);
205 if(U_FAILURE(status
)){
206 log_err("ERROR: ures_getBinary() failed\n");
209 log_err("ERROR: ures_getByKey(locale(default), %%Collation) failed");
214 log_err("Error: ucol_getDefaultRulesArray() failed to return the correct length.\n");
216 if(memcmp(binColData
, binResult
, len1
) != 0){
217 log_err("Error: ucol_getDefaultRulesArray() failed\n");
228 /* Collator Properties
229 ucol_open, ucol_strcoll, getStrength/setStrength
230 getDecomposition/setDecomposition, getDisplayName*/
233 UCollator
*col
, *ruled
;
236 UChar
*source
, *target
;
238 UErrorCode status
= U_ZERO_ERROR
;
240 * Expected version of the English collator.
241 * Currently, the major/minor version numbers change when the builder code
243 * number 2 is from the tailoring data version and
244 * number 3 is the UCA version.
245 * This changes with every UCA version change, and the expected value
246 * needs to be adjusted.
247 * Same in intltest/apicoll.cpp.
249 UVersionInfo currVersionArray
= {0x31, 0xC0, 0x05, 0x2A}; /* from ICU 4.4/UCA 5.2 */
250 UVersionInfo versionArray
= {0, 0, 0, 0};
251 UVersionInfo versionUCAArray
= {0, 0, 0, 0};
252 UVersionInfo versionUCDArray
= {0, 0, 0, 0};
254 log_verbose("The property tests begin : \n");
255 log_verbose("Test ucol_strcoll : \n");
256 col
= ucol_open("en_US", &status
);
257 if (U_FAILURE(status
)) {
258 log_err_status(status
, "Default Collator creation failed.: %s\n", myErrorName(status
));
262 ucol_getVersion(col
, versionArray
);
263 /* Check for a version greater than some value rather than equality
264 * so that we need not update the expected version each time. */
265 if (uprv_memcmp(versionArray
, currVersionArray
, 4)<0) {
266 log_err("Testing ucol_getVersion() - unexpected result: %02x.%02x.%02x.%02x\n",
267 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
269 log_verbose("ucol_getVersion() result: %02x.%02x.%02x.%02x\n",
270 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
273 /* Assume that the UCD and UCA versions are the same,
274 * rather than hardcoding (and updating each time) a particular UCA version. */
275 u_getUnicodeVersion(versionUCDArray
);
276 ucol_getUCAVersion(col
, versionUCAArray
);
277 if (0!=uprv_memcmp(versionUCAArray
, versionUCDArray
, 4)) {
278 log_err("Testing ucol_getUCAVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
279 versionUCAArray
[0], versionUCAArray
[1], versionUCAArray
[2], versionUCAArray
[3]);
282 source
=(UChar
*)malloc(sizeof(UChar
) * 12);
283 target
=(UChar
*)malloc(sizeof(UChar
) * 12);
286 u_uastrcpy(source
, "ab");
287 u_uastrcpy(target
, "abc");
289 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
), "ab < abc comparison failed");
291 u_uastrcpy(source
, "ab");
292 u_uastrcpy(target
, "AB");
294 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
), "ab < AB comparison failed");
295 /* u_uastrcpy(source, "black-bird");
296 u_uastrcpy(target, "blackbird"); */
297 u_uastrcpy(target
, "black-bird");
298 u_uastrcpy(source
, "blackbird");
300 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_GREATER
),
301 "black-bird > blackbird comparison failed");
302 u_uastrcpy(source
, "black bird");
303 u_uastrcpy(target
, "black-bird");
304 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
),
305 "black bird < black-bird comparison failed");
306 u_uastrcpy(source
, "Hello");
307 u_uastrcpy(target
, "hello");
309 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_GREATER
),
310 "Hello > hello comparison failed");
313 log_verbose("Test ucol_strcoll ends.\n");
315 log_verbose("testing ucol_getStrength() method ...\n");
316 doAssert( (ucol_getStrength(col
) == UCOL_TERTIARY
), "collation object has the wrong strength");
317 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
319 log_verbose("testing ucol_setStrength() method ...\n");
320 ucol_setStrength(col
, UCOL_SECONDARY
);
321 doAssert( (ucol_getStrength(col
) != UCOL_TERTIARY
), "collation object's strength is secondary difference");
322 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
323 doAssert( (ucol_getStrength(col
) == UCOL_SECONDARY
), "collation object has the wrong strength");
326 log_verbose("Get display name for the default collation in German : \n");
328 len
=ucol_getDisplayName("en_US", "de_DE", NULL
, 0, &status
);
329 if(status
==U_BUFFER_OVERFLOW_ERROR
){
331 disName
=(UChar
*)malloc(sizeof(UChar
) * (len
+1));
332 ucol_getDisplayName("en_US", "de_DE", disName
, len
+1, &status
);
333 log_verbose("the display name for default collation in german: %s\n", austrdup(disName
) );
336 if(U_FAILURE(status
)){
337 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status
));
340 log_verbose("Default collation getDisplayName ended.\n");
342 ruled
= ucol_open("da_DK", &status
);
343 log_verbose("ucol_getRules() testing ...\n");
344 ucol_getRules(ruled
, &tempLength
);
345 doAssert( tempLength
!= 0, "getRules() result incorrect" );
346 log_verbose("getRules tests end.\n");
348 UChar
*buffer
= (UChar
*)malloc(200000*sizeof(UChar
));
349 int32_t bufLen
= 200000;
351 log_verbose("ucol_getRulesEx() testing ...\n");
352 tempLength
= ucol_getRulesEx(col
,UCOL_TAILORING_ONLY
,buffer
,bufLen
);
353 doAssert( tempLength
== 0x00, "getRulesEx() result incorrect" );
354 log_verbose("getRules tests end.\n");
356 log_verbose("ucol_getRulesEx() testing ...\n");
357 tempLength
=ucol_getRulesEx(col
,UCOL_FULL_RULES
,buffer
,bufLen
);
358 doAssert( tempLength
!= 0, "getRulesEx() result incorrect" );
359 log_verbose("getRules tests end.\n");
365 log_verbose("open an collator for french locale");
366 col
= ucol_open("fr_FR", &status
);
367 if (U_FAILURE(status
)) {
368 log_err("ERROR: Creating French collation failed.: %s\n", myErrorName(status
));
371 ucol_setStrength(col
, UCOL_PRIMARY
);
372 log_verbose("testing ucol_getStrength() method again ...\n");
373 doAssert( (ucol_getStrength(col
) != UCOL_TERTIARY
), "collation object has the wrong strength");
374 doAssert( (ucol_getStrength(col
) == UCOL_PRIMARY
), "collation object's strength is not primary difference");
376 log_verbose("testing French ucol_setStrength() method ...\n");
377 ucol_setStrength(col
, UCOL_TERTIARY
);
378 doAssert( (ucol_getStrength(col
) == UCOL_TERTIARY
), "collation object's strength is not tertiary difference");
379 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
380 doAssert( (ucol_getStrength(col
) != UCOL_SECONDARY
), "collation object's strength is secondary difference");
383 log_verbose("Get display name for the french collation in english : \n");
384 len
=ucol_getDisplayName("fr_FR", "en_US", NULL
, 0, &status
);
385 if(status
==U_BUFFER_OVERFLOW_ERROR
){
387 disName
=(UChar
*)malloc(sizeof(UChar
) * (len
+1));
388 ucol_getDisplayName("fr_FR", "en_US", disName
, len
+1, &status
);
389 log_verbose("the display name for french collation in english: %s\n", austrdup(disName
) );
392 if(U_FAILURE(status
)){
393 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status
));
396 log_verbose("Default collation getDisplayName ended.\n");
400 /* Test RuleBasedCollator and getRules*/
401 void TestRuleBasedColl()
403 UCollator
*col1
, *col2
, *col3
, *col4
;
404 UCollationElements
*iter1
, *iter2
;
409 const UChar
*rule1
, *rule2
, *rule3
, *rule4
;
411 UErrorCode status
= U_ZERO_ERROR
;
412 u_uastrcpy(ruleset1
, "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
413 u_uastrcpy(ruleset2
, "&9 < a, A < b, B < c, C < d, D, e, E");
416 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
,&status
);
417 if (U_FAILURE(status
)) {
418 log_err_status(status
, "RuleBased Collator creation failed.: %s\n", myErrorName(status
));
422 log_verbose("PASS: RuleBased Collator creation passed\n");
424 status
= U_ZERO_ERROR
;
425 col2
= ucol_openRules(ruleset2
, u_strlen(ruleset2
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
426 if (U_FAILURE(status
)) {
427 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
431 log_verbose("PASS: RuleBased Collator creation passed\n");
434 status
= U_ZERO_ERROR
;
435 col3
= ucol_open(NULL
, &status
);
436 if (U_FAILURE(status
)) {
437 log_err("Default Collator creation failed.: %s\n", myErrorName(status
));
441 log_verbose("PASS: Default Collator creation passed\n");
443 rule1
= ucol_getRules(col1
, &tempLength
);
444 rule2
= ucol_getRules(col2
, &tempLength
);
445 rule3
= ucol_getRules(col3
, &tempLength
);
447 doAssert((u_strcmp(rule1
, rule2
) != 0), "Default collator getRules failed");
448 doAssert((u_strcmp(rule2
, rule3
) != 0), "Default collator getRules failed");
449 doAssert((u_strcmp(rule1
, rule3
) != 0), "Default collator getRules failed");
451 col4
=ucol_openRules(rule2
, u_strlen(rule2
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
452 if (U_FAILURE(status
)) {
453 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
456 rule4
= ucol_getRules(col4
, &tempLength
);
457 doAssert((u_strcmp(rule2
, rule4
) == 0), "Default collator getRules failed");
464 /* tests that modifier ! is always ignored */
465 u_uastrcpy(ruleset1
, "!&a<b");
469 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
470 if (U_FAILURE(status
)) {
471 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
474 col2
= ucol_open("en_US", &status
);
475 if (U_FAILURE(status
)) {
476 log_err("en_US Collator creation failed.: %s\n", myErrorName(status
));
479 iter1
= ucol_openElements(col1
, teststr
, 3, &status
);
480 iter2
= ucol_openElements(col2
, teststr
, 3, &status
);
481 if(U_FAILURE(status
)) {
482 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status
));
486 /* testing with en since thai has its own tailoring */
487 uint32_t ce
= ucol_next(iter1
, &status
);
488 uint32_t ce2
= ucol_next(iter2
, &status
);
489 if(U_FAILURE(status
)) {
490 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status
));
494 log_err("! modifier test failed");
496 if (ce
== UCOL_NULLORDER
) {
500 ucol_closeElements(iter1
);
501 ucol_closeElements(iter2
);
504 /* test that we can start a rule without a & or < */
505 u_uastrcpy(ruleset1
, "< z < a");
506 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
507 if (U_FAILURE(status
)) {
508 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
511 u_uastrcpy(teststr
, "z");
512 u_uastrcpy(teststr2
, "a");
513 if (ucol_greaterOrEqual(col1
, teststr
, 1, teststr2
, 1)) {
514 log_err("Rule \"z < a\" fails");
521 UErrorCode status
= U_ZERO_ERROR
;
526 log_verbose("The compare tests begin : \n");
528 col
= ucol_open("en_US", &status
);
529 if(U_FAILURE(status
)) {
530 log_err_status(status
, "ucal_open() collation creation failed.: %s\n", myErrorName(status
));
533 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
534 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
535 u_uastrcpy(test1
, "Abcda");
536 u_uastrcpy(test2
, "abcda");
538 log_verbose("Use tertiary comparison level testing ....\n");
540 doAssert( (!ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" != \"abcda\" ");
541 doAssert( (ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" >>> \"abcda\" ");
542 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" >>> \"abcda\"");
544 ucol_setStrength(col
, UCOL_SECONDARY
);
545 log_verbose("Use secondary comparison level testing ....\n");
547 doAssert( (ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
) )), "Result should be \"Abcda\" == \"abcda\"");
548 doAssert( (!ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
549 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
) )), "Result should be \"Abcda\" == \"abcda\"");
551 ucol_setStrength(col
, UCOL_PRIMARY
);
552 log_verbose("Use primary comparison level testing ....\n");
554 doAssert( (ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
555 doAssert( (!ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
556 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
559 log_verbose("The compare tests end.\n");
566 ---------------------------------------------
567 tests decomposition setting
569 void TestDecomposition() {
570 UErrorCode status
= U_ZERO_ERROR
;
571 UCollator
*en_US
, *el_GR
, *vi_VN
;
572 en_US
= ucol_open("en_US", &status
);
573 el_GR
= ucol_open("el_GR", &status
);
574 vi_VN
= ucol_open("vi_VN", &status
);
576 if (U_FAILURE(status
)) {
577 log_err_status(status
, "ERROR: collation creation failed.: %s\n", myErrorName(status
));
581 if (ucol_getAttribute(vi_VN
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_ON
||
584 log_err("ERROR: vi_VN collation did not have cannonical decomposition for normalization!\n");
587 status
= U_ZERO_ERROR
;
588 if (ucol_getAttribute(el_GR
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_ON
||
591 log_err("ERROR: el_GR collation did not have cannonical decomposition for normalization!\n");
594 status
= U_ZERO_ERROR
;
595 if (ucol_getAttribute(en_US
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_OFF
||
598 log_err("ERROR: en_US collation had cannonical decomposition for normalization!\n");
606 #define CLONETEST_COLLATOR_COUNT 4
608 void TestSafeClone() {
611 static const UChar umlautUStr
[] = {0x00DC, 0};
612 static const UChar oeStr
[] = {0x0055, 0x0045, 0};
613 UCollator
* someCollators
[CLONETEST_COLLATOR_COUNT
];
614 UCollator
* someClonedCollators
[CLONETEST_COLLATOR_COUNT
];
616 UErrorCode err
= U_ZERO_ERROR
;
617 int8_t index
= 6; /* Leave this here to test buffer alingment in memory*/
618 uint8_t buffer
[CLONETEST_COLLATOR_COUNT
] [U_COL_SAFECLONE_BUFFERSIZE
];
619 int32_t bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
620 const char sampleRuleChars
[] = "&Z < CH";
621 UChar sampleRule
[sizeof(sampleRuleChars
)];
623 if (TestBufferSize()) {
624 log_err("U_COL_SAFECLONE_BUFFERSIZE should be larger than sizeof(UCollator)\n");
628 u_uastrcpy(test1
, "abCda");
629 u_uastrcpy(test2
, "abcda");
630 u_uastrcpy(sampleRule
, sampleRuleChars
);
632 /* one default collator & two complex ones */
633 someCollators
[0] = ucol_open("en_US", &err
);
634 someCollators
[1] = ucol_open("ko", &err
);
635 someCollators
[2] = ucol_open("ja_JP", &err
);
636 someCollators
[3] = ucol_openRules(sampleRule
, -1, UCOL_ON
, UCOL_TERTIARY
, NULL
, &err
);
638 for (index
= 0; index
< CLONETEST_COLLATOR_COUNT
; index
++) {
639 ucol_close(someCollators
[index
]);
641 log_data_err("Couldn't open one or more collators\n");
645 /* Check the various error & informational states: */
647 /* Null status - just returns NULL */
648 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, 0))
650 log_err("FAIL: Cloned Collator failed to deal correctly with null status\n");
652 /* error status - should return 0 & keep error the same */
653 err
= U_MEMORY_ALLOCATION_ERROR
;
654 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
) || err
!= U_MEMORY_ALLOCATION_ERROR
)
656 log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
660 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
661 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], 0, &err
) || err
!= U_ILLEGAL_ARGUMENT_ERROR
)
663 log_err("FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer\n");
667 /* buffer size pointer is 0 - fill in pbufferSize with a size */
669 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
) || U_FAILURE(err
) || bufferSize
<= 0)
671 log_err("FAIL: Cloned Collator failed a sizing request ('preflighting')\n");
673 /* Verify our define is large enough */
674 if (U_COL_SAFECLONE_BUFFERSIZE
< bufferSize
)
676 log_err("FAIL: Pre-calculated buffer size is too small\n");
678 /* Verify we can use this run-time calculated size */
679 if (0 == (col
= ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
)) || U_FAILURE(err
))
681 log_err("FAIL: Collator can't be cloned with run-time size\n");
683 if (col
) ucol_close(col
);
684 /* size one byte too small - should allocate & let us know */
686 if (0 == (col
= ucol_safeClone(someCollators
[0], 0, &bufferSize
, &err
)) || err
!= U_SAFECLONE_ALLOCATED_WARNING
)
688 log_err("FAIL: Cloned Collator failed to deal correctly with too-small buffer size\n");
690 if (col
) ucol_close(col
);
692 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
695 /* Null buffer pointer - return Collator & set error to U_SAFECLONE_ALLOCATED_ERROR */
696 if (0 == (col
= ucol_safeClone(someCollators
[0], 0, &bufferSize
, &err
)) || err
!= U_SAFECLONE_ALLOCATED_WARNING
)
698 log_err("FAIL: Cloned Collator failed to deal correctly with null buffer pointer\n");
700 if (col
) ucol_close(col
);
703 /* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
704 if (0 != ucol_safeClone(0, buffer
[0], &bufferSize
, &err
) || err
!= U_ILLEGAL_ARGUMENT_ERROR
)
706 log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
711 /* Test that a cloned collator doesn't accidentally use UCA. */
712 col
=ucol_open("de@collation=phonebook", &err
);
713 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
714 someClonedCollators
[0] = ucol_safeClone(col
, buffer
[0], &bufferSize
, &err
);
715 doAssert( (ucol_greater(col
, umlautUStr
, u_strlen(umlautUStr
), oeStr
, u_strlen(oeStr
))), "Original German phonebook collation sorts differently than expected");
716 doAssert( (ucol_greater(someClonedCollators
[0], umlautUStr
, u_strlen(umlautUStr
), oeStr
, u_strlen(oeStr
))), "Cloned German phonebook collation sorts differently than expected");
717 if (!ucol_equals(someClonedCollators
[0], col
)) {
718 log_err("FAIL: Cloned German phonebook collator is not equal to original.\n");
721 ucol_close(someClonedCollators
[0]);
725 /* change orig & clone & make sure they are independent */
727 for (index
= 0; index
< CLONETEST_COLLATOR_COUNT
; index
++)
729 ucol_setStrength(someCollators
[index
], UCOL_IDENTICAL
);
732 ucol_close(ucol_safeClone(someCollators
[index
], buffer
[index
], &bufferSize
, &err
));
733 if (err
!= U_SAFECLONE_ALLOCATED_WARNING
) {
734 log_err("FAIL: collator number %d was not allocated.\n", index
);
735 log_err("FAIL: status of Collator[%d] is %d (hex: %x).\n", index
, err
, err
);
738 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
740 someClonedCollators
[index
] = ucol_safeClone(someCollators
[index
], buffer
[index
], &bufferSize
, &err
);
741 if (someClonedCollators
[index
] == NULL
742 || someClonedCollators
[index
] < (UCollator
*)buffer
[index
]
743 || someClonedCollators
[index
] > (UCollator
*)(buffer
[index
]+(U_COL_SAFECLONE_BUFFERSIZE
-1)))
745 log_err("FAIL: Cloned collator didn't use provided buffer.\n");
748 if (!ucol_equals(someClonedCollators
[index
], someCollators
[index
])) {
749 log_err("FAIL: Cloned collator is not equal to original at index = %d.\n", index
);
752 /* Check the usability */
753 ucol_setStrength(someCollators
[index
], UCOL_PRIMARY
);
754 ucol_setAttribute(someCollators
[index
], UCOL_CASE_LEVEL
, UCOL_OFF
, &err
);
756 doAssert( (ucol_equal(someCollators
[index
], test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"abcda\" == \"abCda\"");
758 /* Close the original to make sure that the clone is usable. */
759 ucol_close(someCollators
[index
]);
761 ucol_setStrength(someClonedCollators
[index
], UCOL_TERTIARY
);
762 ucol_setAttribute(someClonedCollators
[index
], UCOL_CASE_LEVEL
, UCOL_OFF
, &err
);
763 doAssert( (ucol_greater(someClonedCollators
[index
], test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"abCda\" >>> \"abcda\" ");
765 ucol_close(someClonedCollators
[index
]);
769 void TestCloneBinary(){
770 UErrorCode err
= U_ZERO_ERROR
;
771 UCollator
* col
= ucol_open("en_US", &err
);
776 if (U_FAILURE(err
)) {
777 log_data_err("Couldn't open collator. Error: %s\n", u_errorName(err
));
781 size
= ucol_cloneBinary(col
, NULL
, 0, &err
);
782 if(size
==0 || err
!=U_BUFFER_OVERFLOW_ERROR
) {
783 log_err("ucol_cloneBinary - couldn't check size. Error: %s\n", u_errorName(err
));
788 buffer
= (uint8_t *) malloc(size
);
789 ucol_cloneBinary(col
, buffer
, size
, &err
);
791 log_err("ucol_cloneBinary - couldn't clone.. Error: %s\n", u_errorName(err
));
796 /* how to check binary result ? */
798 c
= ucol_openBinary(buffer
, size
, col
, &err
);
800 log_err("ucol_openBinary failed. Error: %s\n", u_errorName(err
));
802 UChar t
[] = {0x41, 0x42, 0x43, 0}; /* ABC */
805 l1
= ucol_getSortKey(col
, t
, -1, NULL
,0);
806 l2
= ucol_getSortKey(c
, t
, -1, NULL
,0);
807 k1
= (uint8_t *) malloc(sizeof(uint8_t) * l1
);
808 k2
= (uint8_t *) malloc(sizeof(uint8_t) * l2
);
809 ucol_getSortKey(col
, t
, -1, k1
, l1
);
810 ucol_getSortKey(col
, t
, -1, k2
, l2
);
811 if (strcmp((char *)k1
,(char *)k2
) != 0){
812 log_err("ucol_openBinary - new collator should equal to old one\n");
823 static void TestBengaliSortKey(void)
825 const char *curLoc
= "bn";
826 UChar str1
[] = { 0x09BE, 0 };
827 UChar str2
[] = { 0x0B70, 0 };
828 UCollator
*c2
= NULL
;
830 int32_t rulesLength
=-1;
832 int32_t sortKeyLen1
= 0;
834 int32_t sortKeyLen2
= 0;
835 UErrorCode status
= U_ZERO_ERROR
;
836 char sortKeyStr1
[2048];
837 uint32_t sortKeyStrLen1
= sizeof(sortKeyStr1
)/sizeof(sortKeyStr1
[0]);
838 char sortKeyStr2
[2048];
839 uint32_t sortKeyStrLen2
= sizeof(sortKeyStr2
)/sizeof(sortKeyStr2
[0]);
840 UCollationResult result
;
842 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};
846 log_verbose("Rules: %s\n", aescstrdup(rules
, rulesLength
));
848 c2
= ucol_openRules(rules
, rulesLength
, UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
849 if (U_FAILURE(status
)) {
850 log_data_err("ERROR: Creating collator from rules failed with locale: %s : %s\n", curLoc
, myErrorName(status
));
854 sortKeyLen1
= ucol_getSortKey(c2
, str1
, -1, NULL
, 0);
855 sortKey1
= (uint8_t*)malloc(sortKeyLen1
+1);
856 ucol_getSortKey(c2
,str1
,-1,sortKey1
, sortKeyLen1
+1);
857 ucol_sortKeyToString(c2
, sortKey1
, sortKeyStr1
, sortKeyStrLen1
);
860 sortKeyLen2
= ucol_getSortKey(c2
, str2
, -1, NULL
, 0);
861 sortKey2
= (uint8_t*)malloc(sortKeyLen2
+1);
862 ucol_getSortKey(c2
,str2
,-1,sortKey2
, sortKeyLen2
+1);
864 ucol_sortKeyToString(c2
, sortKey2
, sortKeyStr2
, sortKeyStrLen2
);
868 result
=ucol_strcoll(c2
, str1
, -1, str2
, -1);
869 if(result
!=UCOL_LESS
) {
870 log_err("Error: %s was not less than %s: result=%d.\n", aescstrdup(str1
,-1), aescstrdup(str2
,-1), result
);
871 log_info("[%s] -> %s (%d, from rule)\n", aescstrdup(str1
,-1), sortKeyStr1
, sortKeyLen1
);
872 log_info("[%s] -> %s (%d, from rule)\n", aescstrdup(str2
,-1), sortKeyStr2
, sortKeyLen2
);
874 log_verbose("OK: %s was less than %s: result=%d.\n", aescstrdup(str1
,-1), aescstrdup(str2
,-1), result
);
875 log_verbose("[%s] -> %s (%d, from rule)\n", aescstrdup(str1
,-1), sortKeyStr1
, sortKeyLen1
);
876 log_verbose("[%s] -> %s (%d, from rule)\n", aescstrdup(str2
,-1), sortKeyStr2
, sortKeyLen2
);
886 TestOpenVsOpenRules ensures that collators from ucol_open and ucol_openRules
887 will generate identical sort keys
889 void TestOpenVsOpenRules(){
891 /* create an array of all the locales */
892 int32_t numLocales
= uloc_countAvailable();
893 int32_t sizeOfStdSet
;
895 UChar str
[41]; /* create an array of UChar of size maximum strSize + 1 */
902 int32_t sortKeyLen1
, sortKeyLen2
;
903 uint8_t *sortKey1
= NULL
, *sortKey2
= NULL
;
904 char sortKeyStr1
[512], sortKeyStr2
[512];
905 uint32_t sortKeyStrLen1
= sizeof(sortKeyStr1
) / sizeof(sortKeyStr1
[0]),
906 sortKeyStrLen2
= sizeof(sortKeyStr2
) / sizeof(sortKeyStr2
[0]);
913 UErrorCode err
= U_ZERO_ERROR
;
915 /* create a set of standard characters that aren't very interesting...
916 and then we can find some interesting ones later */
918 stdSet
= uset_open(0x61, 0x7A);
919 uset_addRange(stdSet
, 0x41, 0x5A);
920 uset_addRange(stdSet
, 0x30, 0x39);
921 sizeOfStdSet
= uset_size(stdSet
);
924 if(getTestOption(QUICK_OPTION
))
929 for(x
= 0; x
< numLocales
; x
+=adder
){
930 curLoc
= (char *)uloc_getAvailable(x
);
931 log_verbose("Processing %s\n", curLoc
);
933 /* create a collator the normal API way */
934 c1
= ucol_open(curLoc
, &err
);
935 if (U_FAILURE(err
)) {
936 log_err("ERROR: Normal collation creation failed with locale: %s : %s\n", curLoc
, myErrorName(err
));
941 rules
= ucol_getRules(c1
, &rulesLength
);
943 /* use those rules to create a collator from rules */
944 c2
= ucol_openRules(rules
, rulesLength
, UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &err
);
945 if (U_FAILURE(err
)) {
946 log_err("ERROR: Creating collator from rules failed with locale: %s : %s\n", curLoc
, myErrorName(err
));
950 uld
= ulocdata_open(curLoc
, &err
);
952 /*now that we have some collators, we get several strings */
954 for(y
= 0; y
< 5; y
++){
956 /* get a set of ALL the characters in this locale */
957 eSet
= ulocdata_getExemplarSet(uld
, NULL
, 0, ULOCDATA_ES_STANDARD
, &err
);
958 eSize
= uset_size(eSet
);
960 /* make a string with these characters in it */
961 strSize
= (rand()%40
) + 1;
963 for(z
= 0; z
< strSize
; z
++){
964 str
[z
] = uset_charAt(eSet
, rand()%eSize
);
967 /* change the set to only include 'abnormal' characters (not A-Z, a-z, 0-9 */
968 uset_removeAll(eSet
, stdSet
);
969 eSize
= uset_size(eSet
);
971 /* if there are some non-normal characters left, put a few into the string, just to make sure we have some */
973 str
[2%strSize
] = uset_charAt(eSet
, rand()%eSize
);
974 str
[3%strSize
] = uset_charAt(eSet
, rand()%eSize
);
975 str
[5%strSize
] = uset_charAt(eSet
, rand()%eSize
);
976 str
[10%strSize
] = uset_charAt(eSet
, rand()%eSize
);
977 str
[13%strSize
] = uset_charAt(eSet
, rand()%eSize
);
979 /* terminate the string */
980 str
[strSize
-1] = '\0';
981 log_verbose("String used: %S\n", str
);
983 /* get sort keys for both of them, and check that the keys are identicle */
984 sortKeyLen1
= ucol_getSortKey(c1
, str
, u_strlen(str
), NULL
, 0);
985 sortKey1
= (uint8_t*)malloc(sizeof(uint8_t) * (sortKeyLen1
+ 1));
986 /*memset(sortKey1, 0xFE, sortKeyLen1);*/
987 ucol_getSortKey(c1
, str
, u_strlen(str
), sortKey1
, sortKeyLen1
+ 1);
988 ucol_sortKeyToString(c1
, sortKey1
, sortKeyStr1
, sortKeyStrLen1
);
990 sortKeyLen2
= ucol_getSortKey(c2
, str
, u_strlen(str
), NULL
, 0);
991 sortKey2
= (uint8_t*)malloc(sizeof(uint8_t) * (sortKeyLen2
+ 1));
992 /*memset(sortKey2, 0xFE, sortKeyLen2);*/
993 ucol_getSortKey(c2
, str
, u_strlen(str
), sortKey2
, sortKeyLen2
+ 1);
994 ucol_sortKeyToString(c2
, sortKey2
, sortKeyStr2
, sortKeyStrLen2
);
996 /* Check that the lengths are the same */
997 if (sortKeyLen1
!= sortKeyLen2
) {
998 log_err("ERROR : Sort key lengths %d and %d for text '%s' in locale '%s' do not match.\n",
999 sortKeyLen1
, sortKeyLen2
, str
, curLoc
);
1002 /* check that the keys are the same */
1003 if (memcmp(sortKey1
, sortKey2
, sortKeyLen1
) != 0) {
1004 log_err("ERROR : Sort keys '%s' and '%s' for text '%s' in locale '%s' are not equivalent.\n",
1005 sortKeyStr1
, sortKeyStr2
, str
, curLoc
);
1008 /* clean up after each string */
1013 /* clean up after each locale */
1014 ulocdata_close(uld
);
1018 /* final clean up */
1022 ----------------------------------------------------------------------------
1023 ctor -- Tests the getSortKey
1027 uint8_t *sortk1
= NULL
, *sortk2
= NULL
, *sortk3
= NULL
, *sortkEmpty
= NULL
;
1028 int32_t sortklen
, osortklen
;
1030 UChar
*test1
, *test2
, *test3
;
1031 UErrorCode status
= U_ZERO_ERROR
;
1032 char toStringBuffer
[256], *resultP
;
1033 uint32_t toStringLen
=sizeof(toStringBuffer
)/sizeof(toStringBuffer
[0]);
1036 uint8_t s1
[] = { 0x9f, 0x00 };
1037 uint8_t s2
[] = { 0x61, 0x00 };
1040 strcmpResult
= strcmp((const char *)s1
, (const char *)s2
);
1041 log_verbose("strcmp(0x9f..., 0x61...) = %d\n", strcmpResult
);
1043 if(strcmpResult
<= 0) {
1044 log_err("ERR: expected strcmp(\"9f 00\", \"61 00\") to be >=0 (GREATER).. got %d. Calling strcmp() for sortkeys may not work! \n",
1049 log_verbose("testing SortKey begins...\n");
1050 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
1051 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
1052 /* col = ucol_open(NULL, &status); */
1053 col
= ucol_open("en_US", &status
);
1054 if (U_FAILURE(status
)) {
1055 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1060 if(ucol_getStrength(col
) != UCOL_DEFAULT_STRENGTH
)
1062 log_err("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
1064 /* Need to use identical strength */
1065 ucol_setAttribute(col
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &status
);
1067 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
1068 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
1069 test3
=(UChar
*)malloc(sizeof(UChar
) * 6);
1071 memset(test1
,0xFE, sizeof(UChar
)*6);
1072 memset(test2
,0xFE, sizeof(UChar
)*6);
1073 memset(test3
,0xFE, sizeof(UChar
)*6);
1076 u_uastrcpy(test1
, "Abcda");
1077 u_uastrcpy(test2
, "abcda");
1078 u_uastrcpy(test3
, "abcda");
1080 log_verbose("Use tertiary comparison level testing ....\n");
1082 sortklen
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1083 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1084 memset(sortk1
,0xFE, sortklen
);
1085 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortklen
+1);
1087 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1088 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1089 memset(sortk2
,0xFE, sortklen
);
1090 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortklen
+1);
1092 osortklen
= sortklen
;
1093 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test3
), NULL
, 0);
1094 sortk3
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1095 memset(sortk3
,0xFE, sortklen
);
1096 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk3
, sortklen
+1);
1098 doAssert( (sortklen
== osortklen
), "Sortkey length should be the same (abcda, abcda)");
1100 doAssert( (memcmp(sortk1
, sortk2
, sortklen
) > 0), "Result should be \"Abcda\" > \"abcda\"");
1101 doAssert( (memcmp(sortk2
, sortk1
, sortklen
) < 0), "Result should be \"abcda\" < \"Abcda\"");
1102 doAssert( (memcmp(sortk2
, sortk3
, sortklen
) == 0), "Result should be \"abcda\" == \"abcda\"");
1104 resultP
= ucol_sortKeyToString(col
, sortk3
, toStringBuffer
, toStringLen
);
1105 doAssert( (resultP
!= 0), "sortKeyToString failed!");
1107 #if 1 /* verobse log of sortkeys */
1113 strcpy(junk2
, "abcda[2] ");
1114 strcpy(junk3
, " abcda[3] ");
1116 for(i
=0;i
<sortklen
;i
++)
1118 sprintf(junk2
+strlen(junk2
), "%02X ",(int)( 0xFF & sortk2
[i
]));
1119 sprintf(junk3
+strlen(junk3
), "%02X ",(int)( 0xFF & sortk3
[i
]));
1122 log_verbose("%s\n", junk2
);
1123 log_verbose("%s\n", junk3
);
1131 log_verbose("Use secondary comparision level testing ...\n");
1132 ucol_setStrength(col
, UCOL_SECONDARY
);
1133 sortklen
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1134 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1135 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortklen
+1);
1136 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1137 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
1138 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortklen
+1);
1140 doAssert( !(memcmp(sortk1
, sortk2
, sortklen
) > 0), "Result should be \"Abcda\" == \"abcda\"");
1141 doAssert( !(memcmp(sortk2
, sortk1
, sortklen
) < 0), "Result should be \"abcda\" == \"Abcda\"");
1142 doAssert( (memcmp(sortk1
, sortk2
, sortklen
) == 0), "Result should be \"abcda\" == \"abcda\"");
1144 log_verbose("getting sortkey for an empty string\n");
1145 ucol_setAttribute(col
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
1146 sortklen
= ucol_getSortKey(col
, test1
, 0, NULL
, 0);
1147 sortkEmpty
= (uint8_t*)malloc(sizeof(uint8_t) * sortklen
+1);
1148 sortklen
= ucol_getSortKey(col
, test1
, 0, sortkEmpty
, sortklen
+1);
1149 if(sortklen
!= 3 || sortkEmpty
[0] != 1 || sortkEmpty
[0] != 1 || sortkEmpty
[2] != 0) {
1150 log_err("Empty string generated wrong sortkey!\n");
1154 log_verbose("testing passing invalid string\n");
1155 sortklen
= ucol_getSortKey(col
, NULL
, 0, NULL
, 0);
1157 log_err("Invalid string didn't return sortkey size of 0\n");
1161 log_verbose("testing sortkey ends...\n");
1172 uint8_t *sortk1
, *sortk2
, *sortk3
;
1173 int32_t sortk1len
, sortk2len
, sortk3len
;
1175 UChar
*test1
, *test2
, *test3
;
1176 UErrorCode status
= U_ZERO_ERROR
;
1177 log_verbose("testing getHashCode begins...\n");
1178 col
= ucol_open("en_US", &status
);
1179 if (U_FAILURE(status
)) {
1180 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1183 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
1184 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
1185 test3
=(UChar
*)malloc(sizeof(UChar
) * 6);
1186 u_uastrcpy(test1
, "Abcda");
1187 u_uastrcpy(test2
, "abcda");
1188 u_uastrcpy(test3
, "abcda");
1190 log_verbose("Use tertiary comparison level testing ....\n");
1191 sortk1len
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
1192 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk1len
+1));
1193 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortk1len
+1);
1194 sortk2len
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
1195 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk2len
+1));
1196 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortk2len
+1);
1197 sortk3len
=ucol_getSortKey(col
, test2
, u_strlen(test3
), NULL
, 0);
1198 sortk3
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk3len
+1));
1199 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk3
, sortk3len
+1);
1202 log_verbose("ucol_hashCode() testing ...\n");
1204 doAssert( ucol_keyHashCode(sortk1
, sortk1len
) != ucol_keyHashCode(sortk2
, sortk2len
), "Hash test1 result incorrect" );
1205 doAssert( !(ucol_keyHashCode(sortk1
, sortk1len
) == ucol_keyHashCode(sortk2
, sortk2len
)), "Hash test2 result incorrect" );
1206 doAssert( ucol_keyHashCode(sortk2
, sortk2len
) == ucol_keyHashCode(sortk3
, sortk3len
), "Hash result not equal" );
1208 log_verbose("hashCode tests end.\n");
1220 *----------------------------------------------------------------------------
1221 * Tests the UCollatorElements API.
1227 int32_t order1
, order2
, order3
;
1228 UChar
*testString1
, *testString2
;
1230 UCollationElements
*iterator1
, *iterator2
, *iterator3
;
1231 UErrorCode status
= U_ZERO_ERROR
;
1232 log_verbose("testing UCollatorElements begins...\n");
1233 col
= ucol_open("en_US", &status
);
1234 ucol_setAttribute(col
, UCOL_NORMALIZATION_MODE
, UCOL_OFF
, &status
);
1235 if (U_FAILURE(status
)) {
1236 log_err_status(status
, "ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
1240 testString1
=(UChar
*)malloc(sizeof(UChar
) * 150);
1241 testString2
=(UChar
*)malloc(sizeof(UChar
) * 150);
1242 u_uastrcpy(testString1
, "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
1243 u_uastrcpy(testString2
, "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
1245 log_verbose("Constructors and comparison testing....\n");
1247 iterator1
= ucol_openElements(col
, testString1
, u_strlen(testString1
), &status
);
1248 if(U_FAILURE(status
)) {
1249 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1253 else{ log_verbose("PASS: Default collationElement iterator1 creation passed\n");}
1255 iterator2
= ucol_openElements(col
, testString1
, u_strlen(testString1
), &status
);
1256 if(U_FAILURE(status
)) {
1257 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1261 else{ log_verbose("PASS: Default collationElement iterator2 creation passed\n");}
1263 iterator3
= ucol_openElements(col
, testString2
, u_strlen(testString2
), &status
);
1264 if(U_FAILURE(status
)) {
1265 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1269 else{ log_verbose("PASS: Default collationElement iterator3 creation passed\n");}
1271 offset
=ucol_getOffset(iterator1
);
1272 ucol_setOffset(iterator1
, 6, &status
);
1273 if (U_FAILURE(status
)) {
1274 log_err("Error in setOffset for UCollatorElements iterator.: %s\n", myErrorName(status
));
1277 if(ucol_getOffset(iterator1
)==6)
1278 log_verbose("setOffset and getOffset working fine\n");
1280 log_err("error in set and get Offset got %d instead of 6\n", ucol_getOffset(iterator1
));
1283 ucol_setOffset(iterator1
, 0, &status
);
1284 order1
= ucol_next(iterator1
, &status
);
1285 if (U_FAILURE(status
)) {
1286 log_err("Somehow ran out of memory stepping through the iterator1.: %s\n", myErrorName(status
));
1289 order2
=ucol_getOffset(iterator2
);
1290 doAssert((order1
!= order2
), "The first iterator advance failed");
1291 order2
= ucol_next(iterator2
, &status
);
1292 if (U_FAILURE(status
)) {
1293 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1296 order3
= ucol_next(iterator3
, &status
);
1297 if (U_FAILURE(status
)) {
1298 log_err("Somehow ran out of memory stepping through the iterator3.: %s\n", myErrorName(status
));
1302 doAssert((order1
== order2
), "The second iterator advance failed should be the same as first one");
1304 doAssert( (ucol_primaryOrder(order1
) == ucol_primaryOrder(order3
)), "The primary orders should be identical");
1305 doAssert( (ucol_secondaryOrder(order1
) == ucol_secondaryOrder(order3
)), "The secondary orders should be identical");
1306 doAssert( (ucol_tertiaryOrder(order1
) == ucol_tertiaryOrder(order3
)), "The tertiary orders should be identical");
1308 order1
=ucol_next(iterator1
, &status
);
1309 if (U_FAILURE(status
)) {
1310 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1313 order3
=ucol_next(iterator3
, &status
);
1314 if (U_FAILURE(status
)) {
1315 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1318 doAssert( (ucol_primaryOrder(order1
) == ucol_primaryOrder(order3
)), "The primary orders should be identical");
1319 doAssert( (ucol_tertiaryOrder(order1
) != ucol_tertiaryOrder(order3
)), "The tertiary orders should be different");
1321 order1
=ucol_next(iterator1
, &status
);
1322 if (U_FAILURE(status
)) {
1323 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1326 order3
=ucol_next(iterator3
, &status
);
1327 if (U_FAILURE(status
)) {
1328 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1331 /* this here, my friends, is either pure lunacy or something so obsolete that even it's mother
1332 * doesn't care about it. Essentialy, this test complains if secondary values for 'I' and '_'
1333 * are the same. According to the UCA, this is not true. Therefore, remove the test.
1334 * Besides, if primary strengths for two code points are different, it doesn't matter one bit
1335 * what is the relation between secondary or any other strengths.
1336 * killed by weiv 06/11/2002.
1339 doAssert( ((order1 & UCOL_SECONDARYMASK) != (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be different");
1341 doAssert( (order1
!= UCOL_NULLORDER
), "Unexpected end of iterator reached");
1345 ucol_closeElements(iterator1
);
1346 ucol_closeElements(iterator2
);
1347 ucol_closeElements(iterator3
);
1350 log_verbose("testing CollationElementIterator ends...\n");
1353 void TestGetLocale() {
1354 UErrorCode status
= U_ZERO_ERROR
;
1355 const char *rules
= "&a<x<y<z";
1356 UChar rlz
[256] = {0};
1357 uint32_t rlzLen
= u_unescape(rules
, rlz
, 256);
1359 UCollator
*coll
= NULL
;
1360 const char *locale
= NULL
;
1364 /* Now that the collation tree is separate, actual==valid at all times. [alan] */
1365 static const struct {
1366 const char* requestedLocale
;
1367 const char* validLocale
;
1368 const char* actualLocale
;
1370 { "sr_RS", "sr_Cyrl_RS", "sr" },
1371 { "sh_YU", "sr_Latn_RS", "hr" }, /* this used to be sh, but now sh collation aliases hr */
1372 { "en_BE_FOO", "en_BE", "root" },
1373 { "de_DE_NONEXISTANT", "de_DE", "de" }
1376 /* test opening collators for different locales */
1377 for(i
= 0; i
<sizeof(testStruct
)/sizeof(testStruct
[0]); i
++) {
1378 status
= U_ZERO_ERROR
;
1379 coll
= ucol_open(testStruct
[i
].requestedLocale
, &status
);
1380 if(U_FAILURE(status
)) {
1381 log_err_status(status
, "Failed to open collator for %s with %s\n", testStruct
[i
].requestedLocale
, u_errorName(status
));
1385 locale
= ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
);
1386 if(strcmp(locale
, testStruct
[i
].requestedLocale
) != 0) {
1387 log_err("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].requestedLocale
, locale
);
1389 locale
= ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
);
1390 if(strcmp(locale
, testStruct
[i
].validLocale
) != 0) {
1391 log_err("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].validLocale
, locale
);
1393 locale
= ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
);
1394 if(strcmp(locale
, testStruct
[i
].actualLocale
) != 0) {
1395 log_err("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].actualLocale
, locale
);
1400 /* completely non-existant locale for collator should get a default collator */
1402 UCollator
*defaultColl
= ucol_open(NULL
, &status
);
1403 coll
= ucol_open("blahaha", &status
);
1404 if(U_SUCCESS(status
)) {
1405 if(strcmp(ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
), "blahaha")) {
1406 log_err("Nonexisting locale didn't preserve the requested locale\n");
1408 if(strcmp(ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
),
1409 ucol_getLocaleByType(defaultColl
, ULOC_VALID_LOCALE
, &status
))) {
1410 log_err("Valid locale for nonexisting locale locale collator differs "
1411 "from valid locale for default collator\n");
1413 if(strcmp(ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
),
1414 ucol_getLocaleByType(defaultColl
, ULOC_ACTUAL_LOCALE
, &status
))) {
1415 log_err("Actual locale for nonexisting locale locale collator differs "
1416 "from actual locale for default collator\n");
1419 ucol_close(defaultColl
);
1421 log_data_err("Couldn't open collators\n");
1427 /* collator instantiated from rules should have all three locales NULL */
1428 coll
= ucol_openRules(rlz
, rlzLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, NULL
, &status
);
1429 locale
= ucol_getLocaleByType(coll
, ULOC_REQUESTED_LOCALE
, &status
);
1430 if(locale
!= NULL
) {
1431 log_err("For collator instantiated from rules, requested locale returned %s instead of NULL\n", locale
);
1433 locale
= ucol_getLocaleByType(coll
, ULOC_VALID_LOCALE
, &status
);
1434 if(locale
!= NULL
) {
1435 log_err("For collator instantiated from rules, valid locale returned %s instead of NULL\n", locale
);
1437 locale
= ucol_getLocaleByType(coll
, ULOC_ACTUAL_LOCALE
, &status
);
1438 if(locale
!= NULL
) {
1439 log_err("For collator instantiated from rules, actual locale returned %s instead of NULL\n", locale
);
1449 count
=ucol_countAvailable();
1450 /* use something sensible w/o hardcoding the count */
1452 log_err("Error in countAvailable(), it returned %d\n", count
);
1455 log_verbose("PASS: countAvailable() successful, it returned %d\n", count
);
1457 for(i
=0;i
<count
;i
++)
1458 log_verbose("%s\n", ucol_getAvailable(i
));
1465 const char *original
;
1469 static int compare_teststruct(const void *string1
, const void *string2
) {
1470 return(strcmp((const char *)((struct teststruct
*)string1
)->key
, (const char *)((struct teststruct
*)string2
)->key
));
1474 UErrorCode status
= U_ZERO_ERROR
;
1476 UCollator
*coll
= ucol_open("sh", &status
);
1478 uint8_t sortkey
[512], lower
[512], upper
[512];
1481 static const char * const test
[] = {
1485 "j\\u00F6hn sm\\u00EFth",
1486 "J\\u00F6hn Sm\\u00EFth",
1487 "J\\u00D6HN SM\\u00CFTH",
1492 struct teststruct tests
[] = {
1493 {"\\u010CAKI MIHALJ" } ,
1494 {"\\u010CAKI MIHALJ" } ,
1495 {"\\u010CAKI PIRO\\u0160KA" },
1496 {"\\u010CABAI ANDRIJA" } ,
1497 {"\\u010CABAI LAJO\\u0160" } ,
1498 {"\\u010CABAI MARIJA" } ,
1499 {"\\u010CABAI STEVAN" } ,
1500 {"\\u010CABAI STEVAN" } ,
1501 {"\\u010CABARKAPA BRANKO" } ,
1502 {"\\u010CABARKAPA MILENKO" } ,
1503 {"\\u010CABARKAPA MIROSLAV" } ,
1504 {"\\u010CABARKAPA SIMO" } ,
1505 {"\\u010CABARKAPA STANKO" } ,
1506 {"\\u010CABARKAPA TAMARA" } ,
1507 {"\\u010CABARKAPA TOMA\\u0160" } ,
1508 {"\\u010CABDARI\\u0106 NIKOLA" } ,
1509 {"\\u010CABDARI\\u0106 ZORICA" } ,
1510 {"\\u010CABI NANDOR" } ,
1511 {"\\u010CABOVI\\u0106 MILAN" } ,
1512 {"\\u010CABRADI AGNEZIJA" } ,
1513 {"\\u010CABRADI IVAN" } ,
1514 {"\\u010CABRADI JELENA" } ,
1515 {"\\u010CABRADI LJUBICA" } ,
1516 {"\\u010CABRADI STEVAN" } ,
1517 {"\\u010CABRDA MARTIN" } ,
1518 {"\\u010CABRILO BOGDAN" } ,
1519 {"\\u010CABRILO BRANISLAV" } ,
1520 {"\\u010CABRILO LAZAR" } ,
1521 {"\\u010CABRILO LJUBICA" } ,
1522 {"\\u010CABRILO SPASOJA" } ,
1523 {"\\u010CADE\\u0160 ZDENKA" } ,
1524 {"\\u010CADESKI BLAGOJE" } ,
1525 {"\\u010CADOVSKI VLADIMIR" } ,
1526 {"\\u010CAGLJEVI\\u0106 TOMA" } ,
1527 {"\\u010CAGOROVI\\u0106 VLADIMIR" } ,
1528 {"\\u010CAJA VANKA" } ,
1529 {"\\u010CAJI\\u0106 BOGOLJUB" } ,
1530 {"\\u010CAJI\\u0106 BORISLAV" } ,
1531 {"\\u010CAJI\\u0106 RADOSLAV" } ,
1532 {"\\u010CAK\\u0160IRAN MILADIN" } ,
1533 {"\\u010CAKAN EUGEN" } ,
1534 {"\\u010CAKAN EVGENIJE" } ,
1535 {"\\u010CAKAN IVAN" } ,
1536 {"\\u010CAKAN JULIJAN" } ,
1537 {"\\u010CAKAN MIHAJLO" } ,
1538 {"\\u010CAKAN STEVAN" } ,
1539 {"\\u010CAKAN VLADIMIR" } ,
1540 {"\\u010CAKAN VLADIMIR" } ,
1541 {"\\u010CAKAN VLADIMIR" } ,
1542 {"\\u010CAKARA ANA" } ,
1543 {"\\u010CAKAREVI\\u0106 MOMIR" } ,
1544 {"\\u010CAKAREVI\\u0106 NEDELJKO" } ,
1545 {"\\u010CAKI \\u0160ANDOR" } ,
1546 {"\\u010CAKI AMALIJA" } ,
1547 {"\\u010CAKI ANDRA\\u0160" } ,
1548 {"\\u010CAKI LADISLAV" } ,
1549 {"\\u010CAKI LAJO\\u0160" } ,
1550 {"\\u010CAKI LASLO" } ,
1555 int32_t i
= 0, j
= 0, k
= 0, buffSize
= 0, skSize
= 0, lowerSize
= 0, upperSize
= 0;
1556 int32_t arraySize
= sizeof(tests
)/sizeof(tests
[0]);
1558 if(U_SUCCESS(status
) && coll
) {
1559 for(i
= 0; i
<arraySize
; i
++) {
1560 buffSize
= u_unescape(tests
[i
].original
, buffer
, 512);
1561 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, tests
[i
].key
, 512);
1564 qsort(tests
, arraySize
, sizeof(struct teststruct
), compare_teststruct
);
1566 for(i
= 0; i
< arraySize
-1; i
++) {
1567 for(j
= i
+1; j
< arraySize
; j
++) {
1568 lowerSize
= ucol_getBound(tests
[i
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1569 upperSize
= ucol_getBound(tests
[j
].key
, -1, UCOL_BOUND_UPPER
, 1, upper
, 512, &status
);
1570 for(k
= i
; k
<= j
; k
++) {
1571 if(strcmp((const char *)lower
, (const char *)tests
[k
].key
) > 0) {
1572 log_err("Problem with lower! j = %i (%s vs %s)\n", k
, tests
[k
].original
, tests
[i
].original
);
1574 if(strcmp((const char *)upper
, (const char *)tests
[k
].key
) <= 0) {
1575 log_err("Problem with upper! j = %i (%s vs %s)\n", k
, tests
[k
].original
, tests
[j
].original
);
1583 for(i
= 0; i
< 1000; i
++) {
1584 lowerRND
= (rand()/(RAND_MAX
/arraySize
));
1585 upperRND
= lowerRND
+ (rand()/(RAND_MAX
/(arraySize
-lowerRND
)));
1587 lowerSize
= ucol_getBound(tests
[lowerRND
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1588 upperSize
= ucol_getBound(tests
[upperRND
].key
, -1, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1590 for(j
= lowerRND
; j
<=upperRND
; j
++) {
1591 if(strcmp(lower
, tests
[j
].key
) > 0) {
1592 log_err("Problem with lower! j = %i (%s vs %s)\n", j
, tests
[j
].original
, tests
[lowerRND
].original
);
1594 if(strcmp(upper
, tests
[j
].key
) <= 0) {
1595 log_err("Problem with upper! j = %i (%s vs %s)\n", j
, tests
[j
].original
, tests
[upperRND
].original
);
1605 for(i
= 0; i
<sizeof(test
)/sizeof(test
[0]); i
++) {
1606 buffSize
= u_unescape(test
[i
], buffer
, 512);
1607 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, sortkey
, 512);
1608 lowerSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1609 upperSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1610 for(j
= i
+1; j
<sizeof(test
)/sizeof(test
[0]); j
++) {
1611 buffSize
= u_unescape(test
[j
], buffer
, 512);
1612 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, sortkey
, 512);
1613 if(strcmp((const char *)lower
, (const char *)sortkey
) > 0) {
1614 log_err("Problem with lower! i = %i, j = %i (%s vs %s)\n", i
, j
, test
[i
], test
[j
]);
1616 if(strcmp((const char *)upper
, (const char *)sortkey
) <= 0) {
1617 log_err("Problem with upper! i = %i, j = %i (%s vs %s)\n", i
, j
, test
[i
], test
[j
]);
1623 log_data_err("Couldn't open collator\n");
1628 static void doOverrunTest(UCollator
*coll
, const UChar
*uString
, int32_t strLen
) {
1629 int32_t skLen
= 0, skLen2
= 0;
1630 uint8_t sortKey
[256];
1632 uint8_t filler
= 0xFF;
1634 skLen
= ucol_getSortKey(coll
, uString
, strLen
, NULL
, 0);
1636 for(i
= 0; i
< skLen
; i
++) {
1637 memset(sortKey
, filler
, 256);
1638 skLen2
= ucol_getSortKey(coll
, uString
, strLen
, sortKey
, i
);
1639 if(skLen
!= skLen2
) {
1640 log_err("For buffer size %i, got different sortkey length. Expected %i got %i\n", i
, skLen
, skLen2
);
1642 for(j
= i
; j
< 256; j
++) {
1643 if(sortKey
[j
] != filler
) {
1644 log_err("Something run over index %i\n", j
);
1651 /* j1865 reports that if a shorter buffer is passed to
1652 * to get sort key, a buffer overrun happens in some
1653 * cases. This test tries to check this.
1655 void TestSortKeyBufferOverrun(void) {
1656 UErrorCode status
= U_ZERO_ERROR
;
1657 const char* cString
= "A very Merry liTTle-lamB..";
1660 UCollator
*coll
= ucol_open("root", &status
);
1661 strLen
= u_unescape(cString
, uString
, 256);
1663 if(U_SUCCESS(status
)) {
1664 log_verbose("testing non ignorable\n");
1665 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
1666 doOverrunTest(coll
, uString
, strLen
);
1668 log_verbose("testing shifted\n");
1669 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &status
);
1670 doOverrunTest(coll
, uString
, strLen
);
1672 log_verbose("testing shifted quaternary\n");
1673 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &status
);
1674 doOverrunTest(coll
, uString
, strLen
);
1676 log_verbose("testing with french secondaries\n");
1677 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
1678 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
1679 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
1680 doOverrunTest(coll
, uString
, strLen
);
1686 static void TestAttribute()
1688 UErrorCode error
= U_ZERO_ERROR
;
1689 UCollator
*coll
= ucol_open(NULL
, &error
);
1691 if (U_FAILURE(error
)) {
1692 log_err_status(error
, "Creation of default collator failed\n");
1696 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_OFF
, &error
);
1697 if (ucol_getAttribute(coll
, UCOL_FRENCH_COLLATION
, &error
) != UCOL_OFF
||
1699 log_err_status(error
, "Setting and retrieving of the french collation failed\n");
1702 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &error
);
1703 if (ucol_getAttribute(coll
, UCOL_FRENCH_COLLATION
, &error
) != UCOL_ON
||
1705 log_err_status(error
, "Setting and retrieving of the french collation failed\n");
1708 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &error
);
1709 if (ucol_getAttribute(coll
, UCOL_ALTERNATE_HANDLING
, &error
) != UCOL_SHIFTED
||
1711 log_err_status(error
, "Setting and retrieving of the alternate handling failed\n");
1714 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &error
);
1715 if (ucol_getAttribute(coll
, UCOL_ALTERNATE_HANDLING
, &error
) != UCOL_NON_IGNORABLE
||
1717 log_err_status(error
, "Setting and retrieving of the alternate handling failed\n");
1720 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, &error
);
1721 if (ucol_getAttribute(coll
, UCOL_CASE_FIRST
, &error
) != UCOL_LOWER_FIRST
||
1723 log_err_status(error
, "Setting and retrieving of the case first attribute failed\n");
1726 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, &error
);
1727 if (ucol_getAttribute(coll
, UCOL_CASE_FIRST
, &error
) != UCOL_UPPER_FIRST
||
1729 log_err_status(error
, "Setting and retrieving of the case first attribute failed\n");
1732 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_ON
, &error
);
1733 if (ucol_getAttribute(coll
, UCOL_CASE_LEVEL
, &error
) != UCOL_ON
||
1735 log_err_status(error
, "Setting and retrieving of the case level attribute failed\n");
1738 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_OFF
, &error
);
1739 if (ucol_getAttribute(coll
, UCOL_CASE_LEVEL
, &error
) != UCOL_OFF
||
1741 log_err_status(error
, "Setting and retrieving of the case level attribute failed\n");
1744 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &error
);
1745 if (ucol_getAttribute(coll
, UCOL_NORMALIZATION_MODE
, &error
) != UCOL_ON
||
1747 log_err_status(error
, "Setting and retrieving of the normalization on/off attribute failed\n");
1750 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_OFF
, &error
);
1751 if (ucol_getAttribute(coll
, UCOL_NORMALIZATION_MODE
, &error
) != UCOL_OFF
||
1753 log_err_status(error
, "Setting and retrieving of the normalization on/off attribute failed\n");
1756 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_PRIMARY
, &error
);
1757 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_PRIMARY
||
1759 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1762 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_SECONDARY
, &error
);
1763 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_SECONDARY
||
1765 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1768 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &error
);
1769 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_TERTIARY
||
1771 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1774 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &error
);
1775 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_QUATERNARY
||
1777 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1780 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &error
);
1781 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_IDENTICAL
||
1783 log_err_status(error
, "Setting and retrieving of the collation strength failed\n");
1789 void TestGetTailoredSet() {
1792 const char *tests
[20];
1795 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1796 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1799 int32_t i
= 0, j
= 0;
1800 UErrorCode status
= U_ZERO_ERROR
;
1803 UCollator
*coll
= NULL
;
1805 int32_t buffLen
= 0;
1808 for(i
= 0; i
< sizeof(setTest
)/sizeof(setTest
[0]); i
++) {
1809 buffLen
= u_unescape(setTest
[i
].rules
, buff
, 1024);
1810 coll
= ucol_openRules(buff
, buffLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, &pError
, &status
);
1811 if(U_SUCCESS(status
)) {
1812 set
= ucol_getTailoredSet(coll
, &status
);
1813 if(uset_size(set
) != setTest
[i
].testsize
) {
1814 log_err("Tailored set size different (%d) than expected (%d)\n", uset_size(set
), setTest
[i
].testsize
);
1816 for(j
= 0; j
< setTest
[i
].testsize
; j
++) {
1817 buffLen
= u_unescape(setTest
[i
].tests
[j
], buff
, 1024);
1818 if(!uset_containsString(set
, buff
, buffLen
)) {
1819 log_err("Tailored set doesn't contain %s... It should\n", setTest
[i
].tests
[j
]);
1824 log_err_status(status
, "Couldn't open collator with rules %s\n", setTest
[i
].rules
);
1830 static int tMemCmp(const uint8_t *first
, const uint8_t *second
) {
1831 int32_t firstLen
= (int32_t)strlen((const char *)first
);
1832 int32_t secondLen
= (int32_t)strlen((const char *)second
);
1833 return memcmp(first
, second
, uprv_min(firstLen
, secondLen
));
1835 static const char * strengthsC
[] = {
1843 void TestMergeSortKeys(void) {
1844 UErrorCode status
= U_ZERO_ERROR
;
1845 UCollator
*coll
= ucol_open("en", &status
);
1846 if(U_SUCCESS(status
)) {
1848 const char* cases
[] = {
1853 uint32_t casesSize
= sizeof(cases
)/sizeof(cases
[0]);
1854 const char* prefix
= "foo";
1855 const char* suffix
= "egg";
1856 char outBuff1
[256], outBuff2
[256];
1858 uint8_t **sortkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1859 uint8_t **mergedPrefixkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1860 uint8_t **mergedSuffixkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1861 uint32_t *sortKeysLen
= (uint32_t *)malloc(casesSize
*sizeof(uint32_t));
1862 uint8_t prefixKey
[256], suffixKey
[256];
1863 uint32_t prefixKeyLen
= 0, suffixKeyLen
= 0, i
= 0;
1865 uint32_t unescapedLen
= 0, l1
= 0, l2
= 0;
1866 UColAttributeValue strength
;
1868 log_verbose("ucol_mergeSortkeys test\n");
1869 log_verbose("Testing order of the test cases\n");
1870 genericLocaleStarter("en", cases
, casesSize
);
1872 for(i
= 0; i
<casesSize
; i
++) {
1873 sortkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1874 mergedPrefixkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1875 mergedSuffixkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1878 unescapedLen
= u_unescape(prefix
, buffer
, 256);
1879 prefixKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, prefixKey
, 256);
1881 unescapedLen
= u_unescape(suffix
, buffer
, 256);
1882 suffixKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, suffixKey
, 256);
1884 log_verbose("Massaging data with prefixes and different strengths\n");
1885 strength
= UCOL_PRIMARY
;
1886 while(strength
<= UCOL_IDENTICAL
) {
1887 log_verbose("Strength %s\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1888 ucol_setAttribute(coll
, UCOL_STRENGTH
, strength
, &status
);
1889 for(i
= 0; i
<casesSize
; i
++) {
1890 unescapedLen
= u_unescape(cases
[i
], buffer
, 256);
1891 sortKeysLen
[i
] = ucol_getSortKey(coll
, buffer
, unescapedLen
, sortkeys
[i
], 256);
1892 ucol_mergeSortkeys(prefixKey
, prefixKeyLen
, sortkeys
[i
], sortKeysLen
[i
], mergedPrefixkeys
[i
], 256);
1893 ucol_mergeSortkeys(sortkeys
[i
], sortKeysLen
[i
], suffixKey
, suffixKeyLen
, mergedSuffixkeys
[i
], 256);
1895 if(tMemCmp(mergedPrefixkeys
[i
-1], mergedPrefixkeys
[i
]) >= 0) {
1896 log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1898 ucol_sortKeyToString(coll
, mergedPrefixkeys
[i
-1], outBuff1
, l1
),
1899 ucol_sortKeyToString(coll
, mergedPrefixkeys
[i
], outBuff2
, l2
));
1901 if(tMemCmp(mergedSuffixkeys
[i
-1], mergedSuffixkeys
[i
]) >= 0) {
1902 log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1904 ucol_sortKeyToString(coll
, mergedSuffixkeys
[i
-1], outBuff1
, l1
),
1905 ucol_sortKeyToString(coll
, mergedSuffixkeys
[i
], outBuff2
, l2
));
1909 if(strength
== UCOL_QUATERNARY
) {
1910 strength
= UCOL_IDENTICAL
;
1917 uint8_t smallBuf
[3];
1918 uint32_t reqLen
= 0;
1919 log_verbose("testing buffer overflow\n");
1920 reqLen
= ucol_mergeSortkeys(prefixKey
, prefixKeyLen
, suffixKey
, suffixKeyLen
, smallBuf
, 3);
1921 if(reqLen
!= (prefixKeyLen
+suffixKeyLen
-1)) {
1922 log_err("Wrong preflight size for merged sortkey\n");
1928 uint8_t emptyKey
[20], abcKey
[50], mergedKey
[100];
1929 int32_t emptyKeyLen
= 0, abcKeyLen
= 0, mergedKeyLen
= 0;
1931 log_verbose("testing merging with sortkeys generated for empty strings\n");
1932 emptyKeyLen
= ucol_getSortKey(coll
, &empty
, 0, emptyKey
, 20);
1933 unescapedLen
= u_unescape(cases
[0], buffer
, 256);
1934 abcKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, abcKey
, 50);
1935 mergedKeyLen
= ucol_mergeSortkeys(emptyKey
, emptyKeyLen
, abcKey
, abcKeyLen
, mergedKey
, 100);
1936 if(mergedKey
[0] != 2) {
1937 log_err("Empty sortkey didn't produce a level separator\n");
1939 /* try with zeros */
1940 mergedKeyLen
= ucol_mergeSortkeys(emptyKey
, 0, abcKey
, abcKeyLen
, mergedKey
, 100);
1941 if(mergedKeyLen
!= 0 || mergedKey
[0] != 0) {
1942 log_err("Empty key didn't produce null mergedKey\n");
1944 mergedKeyLen
= ucol_mergeSortkeys(abcKey
, abcKeyLen
, emptyKey
, 0, mergedKey
, 100);
1945 if(mergedKeyLen
!= 0 || mergedKey
[0] != 0) {
1946 log_err("Empty key didn't produce null mergedKey\n");
1951 for(i
= 0; i
<casesSize
; i
++) {
1953 free(mergedPrefixkeys
[i
]);
1954 free(mergedSuffixkeys
[i
]);
1957 free(mergedPrefixkeys
);
1958 free(mergedSuffixkeys
);
1961 /* need to finish this up */
1963 log_data_err("Couldn't open collator");
1966 static void TestShortString(void)
1970 const char *expectedOutput
;
1972 UErrorCode expectedStatus
;
1973 int32_t expectedOffset
;
1974 uint32_t expectedIdentifier
;
1977 * The following expectedOutput contains a collation weight (2700 from UCA 6.0)
1978 * which is the primary weight for the T character (U+0041) in the input.
1979 * When that character gets a different weight in FractionalUCA.txt,
1980 * the expectedOutput needs to be adjusted.
1981 * That is, when we upgrade to a new UCA version or change collation
1982 * in such a way that the absolute weight for 'A' changes,
1983 * we will get a test failure here and need to adjust the test case.
1985 {"LDE_RDE_KPHONEBOOK_T0041_ZLATN","B2700_KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING
, 0, 0 },
1987 {"LEN_RUS_NO_AS_S4","AS_LROOT_NO_S4", NULL
, U_USING_DEFAULT_WARNING
, 0, 0 },
1988 {"LDE_VPHONEBOOK_EO_SI","EO_KPHONEBOOK_LDE_SI", "de@collation=phonebook", U_ZERO_ERROR
, 0, 0 },
1989 {"LDE_Kphonebook","KPHONEBOOK_LDE", "de@collation=phonebook", U_ZERO_ERROR
, 0, 0 },
1990 {"Xqde_DE@collation=phonebookq_S3_EX","KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING
, 0, 0 },
1991 {"LFR_FO", "FO_LROOT", NULL
, U_USING_DEFAULT_WARNING
, 0, 0 },
1992 {"SO_LX_AS", "", NULL
, U_ILLEGAL_ARGUMENT_ERROR
, 8, 0 },
1993 {"S3_ASS_MMM", "", NULL
, U_ILLEGAL_ARGUMENT_ERROR
, 5, 0 }
1997 UCollator
*coll
= NULL
, *fromNormalized
= NULL
;
1998 UParseError parseError
;
1999 UErrorCode status
= U_ZERO_ERROR
;
2000 char fromShortBuffer
[256], normalizedBuffer
[256], fromNormalizedBuffer
[256];
2001 const char* locale
= NULL
;
2004 for(i
= 0; i
< sizeof(testCases
)/sizeof(testCases
[0]); i
++) {
2005 status
= U_ZERO_ERROR
;
2006 if(testCases
[i
].locale
) {
2007 locale
= testCases
[i
].locale
;
2012 coll
= ucol_openFromShortString(testCases
[i
].input
, FALSE
, &parseError
, &status
);
2013 if(status
!= testCases
[i
].expectedStatus
) {
2014 log_err_status(status
, "Got status '%s' that is different from expected '%s' for '%s'\n",
2015 u_errorName(status
), u_errorName(testCases
[i
].expectedStatus
), testCases
[i
].input
);
2019 if(U_SUCCESS(status
)) {
2020 ucol_getShortDefinitionString(coll
, locale
, fromShortBuffer
, 256, &status
);
2022 if(strcmp(fromShortBuffer
, testCases
[i
].expectedOutput
)) {
2023 log_err("Got short string '%s' from the collator. Expected '%s' for input '%s'\n",
2024 fromShortBuffer
, testCases
[i
].expectedOutput
, testCases
[i
].input
);
2027 ucol_normalizeShortDefinitionString(testCases
[i
].input
, normalizedBuffer
, 256, &parseError
, &status
);
2028 fromNormalized
= ucol_openFromShortString(normalizedBuffer
, FALSE
, &parseError
, &status
);
2029 ucol_getShortDefinitionString(fromNormalized
, locale
, fromNormalizedBuffer
, 256, &status
);
2031 if(strcmp(fromShortBuffer
, fromNormalizedBuffer
)) {
2032 log_err("Strings obtained from collators instantiated by short string ('%s') and from normalized string ('%s') differ\n",
2033 fromShortBuffer
, fromNormalizedBuffer
);
2037 if(!ucol_equals(coll
, fromNormalized
)) {
2038 log_err("Collator from short string ('%s') differs from one obtained through a normalized version ('%s')\n",
2039 testCases
[i
].input
, normalizedBuffer
);
2042 ucol_close(fromNormalized
);
2046 if(parseError
.offset
!= testCases
[i
].expectedOffset
) {
2047 log_err("Got parse error offset %i, but expected %i instead for '%s'\n",
2048 parseError
.offset
, testCases
[i
].expectedOffset
, testCases
[i
].input
);
2056 doSetsTest(const char *locale
, const USet
*ref
, USet
*set
, const char* inSet
, const char* outSet
, UErrorCode
*status
) {
2061 bufLen
= u_unescape(inSet
, buffer
, 512);
2062 uset_applyPattern(set
, buffer
, bufLen
, 0, status
);
2063 if(U_FAILURE(*status
)) {
2064 log_err("%s: Failure setting pattern %s\n", locale
, u_errorName(*status
));
2067 if(!uset_containsAll(ref
, set
)) {
2068 log_err("%s: Some stuff from %s is not present in the set\n", locale
, inSet
);
2072 bufLen
= u_unescape(outSet
, buffer
, 512);
2073 uset_applyPattern(set
, buffer
, bufLen
, 0, status
);
2074 if(U_FAILURE(*status
)) {
2075 log_err("%s: Failure setting pattern %s\n", locale
, u_errorName(*status
));
2078 if(!uset_containsNone(ref
, set
)) {
2079 log_err("%s: Some stuff from %s is present in the set\n", locale
, outSet
);
2087 TestGetContractionsAndUnsafes(void)
2091 const char* inConts
;
2092 const char* outConts
;
2095 const char* unsafeCodeUnits
;
2096 const char* safeCodeUnits
;
2099 "[{\\u0418\\u0306}{\\u0438\\u0306}]",
2104 "[aAbB\\u0430\\u0410\\u0433\\u0413]"
2107 "[{\\u0406\\u0308}{\\u0456\\u0308}{\\u0418\\u0306}{\\u0438\\u0306}]",
2108 "[\\u0407\\u0419\\u0439\\u0457]",
2111 "[\\u0406\\u0456\\u0418\\u0438]",
2115 "[{C\\u0301}{C\\u030C}{C\\u0341}{DZ\\u030C}{Dz\\u030C}{D\\u017D}{D\\u017E}{lj}{nj}]",
2116 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
2123 "[{\\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}]",
2124 "[{\\u30FD\\u3099}{\\u309D\\u3099}{\\u3053\\u3099}{\\u30B3\\u3099}{lj}{nj}]",
2135 UErrorCode status
= U_ZERO_ERROR
;
2136 UCollator
*coll
= NULL
;
2138 int32_t noConts
= 0;
2139 USet
*conts
= uset_open(0,0);
2140 USet
*exp
= uset_open(0, 0);
2141 USet
*set
= uset_open(0,0);
2142 int32_t setBufferLen
= 65536;
2143 UChar buffer
[65536];
2146 for(i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++) {
2147 log_verbose("Testing locale: %s\n", tests
[i
].locale
);
2148 coll
= ucol_open(tests
[i
].locale
, &status
);
2149 if (coll
== NULL
|| U_FAILURE(status
)) {
2150 log_err_status(status
, "Unable to open collator for locale %s ==> %s\n", tests
[i
].locale
, u_errorName(status
));
2153 ucol_getContractionsAndExpansions(coll
, conts
, exp
, TRUE
, &status
);
2154 doSetsTest(tests
[i
].locale
, conts
, set
, tests
[i
].inConts
, tests
[i
].outConts
, &status
);
2155 setLen
= uset_toPattern(conts
, buffer
, setBufferLen
, TRUE
, &status
);
2156 if(U_SUCCESS(status
)) {
2157 /*log_verbose("Contractions %i: %s\n", uset_getItemCount(conts), aescstrdup(buffer, setLen));*/
2159 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2160 status
= U_ZERO_ERROR
;
2162 doSetsTest(tests
[i
].locale
, exp
, set
, tests
[i
].inExp
, tests
[i
].outExp
, &status
);
2163 setLen
= uset_toPattern(exp
, buffer
, setBufferLen
, TRUE
, &status
);
2164 if(U_SUCCESS(status
)) {
2165 /*log_verbose("Expansions %i: %s\n", uset_getItemCount(exp), aescstrdup(buffer, setLen));*/
2167 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2168 status
= U_ZERO_ERROR
;
2171 noConts
= ucol_getUnsafeSet(coll
, conts
, &status
);
2172 doSetsTest(tests
[i
].locale
, conts
, set
, tests
[i
].unsafeCodeUnits
, tests
[i
].safeCodeUnits
, &status
);
2173 setLen
= uset_toPattern(conts
, buffer
, setBufferLen
, TRUE
, &status
);
2174 if(U_SUCCESS(status
)) {
2175 log_verbose("Unsafe %i: %s\n", uset_getItemCount(exp
), aescstrdup(buffer
, setLen
));
2177 log_err("error %s. %i\n", u_errorName(status
), setLen
);
2178 status
= U_ZERO_ERROR
;
2191 TestOpenBinary(void)
2193 UErrorCode status
= U_ZERO_ERROR
;
2195 char rule[] = "&h < d < c < b";
2196 char *wUCA[] = { "a", "h", "d", "c", "b", "i" };
2197 char *noUCA[] = {"d", "c", "b", "a", "h", "i" };
2199 /* we have to use Cyrillic letters because latin-1 always gets copied */
2200 const char rule
[] = "&\\u0452 < \\u0434 < \\u0433 < \\u0432"; /* &dje < d < g < v */
2201 const char *wUCA
[] = { "\\u0430", "\\u0452", "\\u0434", "\\u0433", "\\u0432", "\\u0435" }; /* a, dje, d, g, v, e */
2202 const char *noUCA
[] = {"\\u0434", "\\u0433", "\\u0432", "\\u0430", "\\u0435", "\\u0452" }; /* d, g, v, a, e, dje */
2205 int32_t uRulesLen
= u_unescape(rule
, uRules
, 256);
2207 UCollator
*coll
= ucol_openRules(uRules
, uRulesLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, NULL
, &status
);
2208 UCollator
*UCA
= NULL
;
2209 UCollator
*cloneNOUCA
= NULL
, *cloneWUCA
= NULL
;
2211 uint8_t imageBuffer
[32768];
2212 uint8_t *image
= imageBuffer
;
2213 int32_t imageBufferCapacity
= 32768;
2217 if((coll
==NULL
)||(U_FAILURE(status
))) {
2218 log_data_err("could not load collators or error occured: %s\n",
2219 u_errorName(status
));
2222 UCA
= ucol_open("root", &status
);
2223 if((UCA
==NULL
)||(U_FAILURE(status
))) {
2224 log_data_err("could not load UCA collator or error occured: %s\n",
2225 u_errorName(status
));
2228 imageSize
= ucol_cloneBinary(coll
, image
, imageBufferCapacity
, &status
);
2229 if(U_FAILURE(status
)) {
2230 image
= (uint8_t *)malloc(imageSize
*sizeof(uint8_t));
2231 status
= U_ZERO_ERROR
;
2232 imageSize
= ucol_cloneBinary(coll
, imageBuffer
, imageSize
, &status
);
2236 cloneWUCA
= ucol_openBinary(image
, imageSize
, UCA
, &status
);
2237 cloneNOUCA
= ucol_openBinary(image
, imageSize
, NULL
, &status
);
2239 genericOrderingTest(coll
, wUCA
, sizeof(wUCA
)/sizeof(wUCA
[0]));
2241 genericOrderingTest(cloneWUCA
, wUCA
, sizeof(wUCA
)/sizeof(wUCA
[0]));
2242 genericOrderingTest(cloneNOUCA
, noUCA
, sizeof(noUCA
)/sizeof(noUCA
[0]));
2244 if(image
!= imageBuffer
) {
2248 ucol_close(cloneNOUCA
);
2249 ucol_close(cloneWUCA
);
2253 static void TestDefault(void) {
2254 /* Tests for code coverage. */
2255 UErrorCode status
= U_ZERO_ERROR
;
2256 UCollator
*coll
= ucol_open("es@collation=pinyin", &status
);
2257 if (coll
== NULL
|| status
== U_FILE_ACCESS_ERROR
) {
2258 log_data_err("Unable to open collator es@collation=pinyin\n");
2261 if (status
!= U_USING_DEFAULT_WARNING
) {
2262 /* What do you mean that you know about using pinyin collation in Spanish!? This should be in the zh locale. */
2263 log_err("es@collation=pinyin should return U_USING_DEFAULT_WARNING, but returned %s\n", u_errorName(status
));
2266 if (ucol_getKeywordValues("funky", &status
) != NULL
) {
2267 log_err("Collators should not know about the funky keyword.\n");
2269 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
2270 log_err("funky keyword didn't fail as expected %s\n", u_errorName(status
));
2272 if (ucol_getKeywordValues("collation", &status
) != NULL
) {
2273 log_err("ucol_getKeywordValues should not work when given a bad status.\n");
2277 static void TestDefaultKeyword(void) {
2278 /* Tests for code coverage. */
2279 UErrorCode status
= U_ZERO_ERROR
;
2280 const char *loc
= "zh_TW@collation=default";
2281 UCollator
*coll
= ucol_open(loc
, &status
);
2282 if(U_FAILURE(status
)) {
2283 log_info("Warning: ucol_open(%s, ...) returned %s, at least it didn't crash.\n", loc
, u_errorName(status
));
2284 } else if (status
!= U_USING_FALLBACK_WARNING
) {
2285 /* Hmm, skip the following test for CLDR 1.9 data and/or ICU 4.6, no longer seems to apply */
2287 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
));
2293 static void TestGetKeywordValuesForLocale(void) {
2294 #define INCLUDE_UNIHAN_COLLATION 1
2295 #define PREFERRED_SIZE 16
2296 #define MAX_NUMBER_OF_KEYWORDS 9
2297 const char *PREFERRED
[PREFERRED_SIZE
][MAX_NUMBER_OF_KEYWORDS
+1] = {
2298 { "und", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2299 { "en_US", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2300 { "en_029", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2301 { "de_DE", "standard", "phonebook", "search", "ducet", NULL
, NULL
, NULL
, NULL
},
2302 { "de_Latn_DE", "standard", "phonebook", "search", "ducet", NULL
, NULL
, NULL
, NULL
},
2303 #if INCLUDE_UNIHAN_COLLATION
2304 { "zh", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2305 { "zh_Hans", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2306 { "zh_CN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2307 { "zh_Hant", "stroke", "big5han", "gb2312han", "pinyin", "standard", "unihan", "ducet", "search" },
2308 { "zh_TW", "stroke", "big5han", "gb2312han", "pinyin", "standard", "unihan", "ducet", "search" },
2309 { "zh__PINYIN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2311 { "zh", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL
},
2312 { "zh_Hans", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL
},
2313 { "zh_CN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL
},
2314 { "zh_Hant", "stroke", "big5han", "gb2312han", "pinyin", "standard", "ducet", "search", NULL
},
2315 { "zh_TW", "stroke", "big5han", "gb2312han", "pinyin", "standard", "ducet", "search", NULL
},
2316 { "zh__PINYIN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL
},
2318 { "es_ES", "standard", "search", "traditional", "ducet", NULL
, NULL
, NULL
, NULL
},
2319 { "es__TRADITIONAL","traditional", "search", "standard", "ducet", NULL
, NULL
, NULL
, NULL
},
2320 { "und@collation=phonebook", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
},
2321 { "de_DE@collation=big5han", "standard", "phonebook", "search", "ducet", NULL
, NULL
, NULL
, NULL
},
2322 { "zzz@collation=xxx", "standard", "ducet", "search", NULL
, NULL
, NULL
, NULL
, NULL
}
2324 #if INCLUDE_UNIHAN_COLLATION
2325 const int32_t expectedLength
[PREFERRED_SIZE
] = { 3, 3, 3, 4, 4, 8, 8, 8, 8, 8, 8, 4, 4, 3, 4, 3 };
2327 const int32_t expectedLength
[PREFERRED_SIZE
] = { 3, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 4, 4, 3, 4, 3 };
2330 UErrorCode status
= U_ZERO_ERROR
;
2331 UEnumeration
*keywordValues
= NULL
;
2332 int32_t i
, n
, size
, valueLength
;
2333 const char *locale
= NULL
, *value
= NULL
;
2334 UBool errorOccurred
= FALSE
;
2336 for (i
= 0; i
< PREFERRED_SIZE
; i
++) {
2337 locale
= PREFERRED
[i
][0];
2342 keywordValues
= ucol_getKeywordValuesForLocale("collation", locale
, TRUE
, &status
);
2343 if (keywordValues
== NULL
|| U_FAILURE(status
)) {
2344 log_err_status(status
, "Error getting keyword values: %s\n", u_errorName(status
));
2347 size
= uenum_count(keywordValues
, &status
);
2349 if (size
== expectedLength
[i
]) {
2350 for (n
= 0; n
< expectedLength
[i
]; n
++) {
2351 if ((value
= uenum_next(keywordValues
, &valueLength
, &status
)) != NULL
&& U_SUCCESS(status
)) {
2352 if (uprv_strcmp(value
, PREFERRED
[i
][n
+1]) != 0) {
2353 log_err("Keyword values differ: Got [%s] Expected [%s] for locale: %s\n", value
, PREFERRED
[i
][n
+1], locale
);
2354 errorOccurred
= TRUE
;
2359 log_err("While getting keyword value from locale: %s got this error: %s\n", locale
, u_errorName(status
));
2360 errorOccurred
= TRUE
;
2364 if (errorOccurred
) {
2368 log_err("Number of keywords (%d) does not match expected size (%d) for locale: %s\n", size
, expectedLength
[i
], locale
);
2371 uenum_close(keywordValues
);
2372 keywordValues
= NULL
;
2374 if (keywordValues
!= NULL
) {
2375 uenum_close(keywordValues
);
2380 #endif /* #if !UCONFIG_NO_COLLATION */