1 /********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda Ported for C API
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/ustring.h"
25 #include "unicode/ures.h"
26 #include "unicode/ucoleitr.h"
32 static void TestAttribute(void);
33 int TestBufferSize(); /* defined in "colutil.c" */
38 /* next two function is modified from "i18n/ucol.cpp" to avoid include "ucol_imp.h" */
39 static void uprv_appendByteToHexString(char *dst
, uint8_t val
) {
40 uint32_t len
= (uint32_t)strlen(dst
);
41 sprintf(dst
+len
, "%02X", val
);
44 static char* U_EXPORT2
ucol_sortKeyToString(const UCollator
*coll
, const uint8_t *sortkey
, char *buffer
, uint32_t *len
) {
45 int32_t strength
= UCOL_PRIMARY
;
46 uint32_t res_size
= 0;
47 UBool doneCase
= FALSE
;
49 char *current
= buffer
;
50 const uint8_t *currentSk
= sortkey
;
52 UErrorCode error_code
= U_ZERO_ERROR
;
56 while(strength
<= UCOL_QUATERNARY
&& strength
<= ucol_getAttribute(coll
,UCOL_STRENGTH
, &error_code
)) {
57 if(U_FAILURE(error_code
)) {
58 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code
));
60 if(strength
> UCOL_PRIMARY
) {
61 strcat(current
, " . ");
63 while(*currentSk
!= 0x01 && *currentSk
!= 0x00) { /* print a level */
64 uprv_appendByteToHexString(current
, *currentSk
++);
67 if(ucol_getAttribute(coll
,UCOL_CASE_LEVEL
, &error_code
) == UCOL_ON
&& strength
== UCOL_SECONDARY
&& doneCase
== FALSE
) {
69 } else if(ucol_getAttribute(coll
,UCOL_CASE_LEVEL
, &error_code
) == UCOL_OFF
|| doneCase
== TRUE
|| strength
!= UCOL_SECONDARY
) {
72 if(U_FAILURE(error_code
)) {
73 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code
));
75 uprv_appendByteToHexString(current
, *currentSk
++); /* This should print '01' */
76 if(strength
== UCOL_QUATERNARY
&& ucol_getAttribute(coll
,UCOL_ALTERNATE_HANDLING
, &error_code
) == UCOL_NON_IGNORABLE
) {
81 if(ucol_getAttribute(coll
,UCOL_STRENGTH
, &error_code
) == UCOL_IDENTICAL
) {
82 strcat(current
, " . ");
83 while(*currentSk
!= 0) {
84 uprv_appendByteToHexString(current
, *currentSk
++);
88 uprv_appendByteToHexString(current
, *currentSk
++);
90 if(U_FAILURE(error_code
)) {
91 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code
));
101 /* end of avoid include "ucol_imp.h" */
104 void addCollAPITest(TestNode
** root
)
106 /* WEIVTODO: return tests here */
107 addTest(root
, &TestProperty
, "tscoll/capitst/TestProperty");
108 addTest(root
, &TestRuleBasedColl
, "tscoll/capitst/TestRuleBasedColl");
109 addTest(root
, &TestCompare
, "tscoll/capitst/TestCompare");
110 addTest(root
, &TestSortKey
, "tscoll/capitst/TestSortKey");
111 addTest(root
, &TestHashCode
, "tscoll/capitst/TestHashCode");
112 addTest(root
, &TestElemIter
, "tscoll/capitst/TestElemIter");
113 addTest(root
, &TestGetAll
, "tscoll/capitst/TestGetAll");
114 /*addTest(root, &TestGetDefaultRules, "tscoll/capitst/TestGetDefaultRules");*/
115 addTest(root
, &TestDecomposition
, "tscoll/capitst/TestDecomposition");
116 addTest(root
, &TestSafeClone
, "tscoll/capitst/TestSafeClone");
117 addTest(root
, &TestGetSetAttr
, "tscoll/capitst/TestGetSetAttr");
118 addTest(root
, &TestBounds
, "tscoll/capitst/TestBounds");
119 addTest(root
, &TestGetLocale
, "tscoll/capitst/TestGetLocale");
120 addTest(root
, &TestSortKeyBufferOverrun
, "tscoll/capitst/TestSortKeyBufferOverrun");
121 addTest(root
, &TestAttribute
, "tscoll/capitst/TestAttribute");
122 addTest(root
, &TestGetTailoredSet
, "tscoll/capitst/TestGetTailoredSet");
123 addTest(root
, &TestMergeSortKeys
, "tscoll/capitst/TestMergeSortKeys");
124 addTest(root
, &TestShortString
, "tscoll/capitst/TestShortString");
125 addTest(root
, &TestGetContractionsAndUnsafes
, "tscoll/capitst/TestGetContractionsAndUnsafes");
128 void TestGetSetAttr(void) {
129 UErrorCode status
= U_ZERO_ERROR
;
130 UCollator
*coll
= ucol_open(NULL
, &status
);
133 UColAttributeValue val
[5];
135 UColAttributeValue nonValue
;
137 {UCOL_FRENCH_COLLATION
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},
138 {UCOL_ALTERNATE_HANDLING
, {UCOL_NON_IGNORABLE
, UCOL_SHIFTED
}, 2, UCOL_OFF
},/* attribute for handling variable elements*/
139 {UCOL_CASE_FIRST
, {UCOL_OFF
, UCOL_LOWER_FIRST
, UCOL_UPPER_FIRST
}, 3, UCOL_SHIFTED
},/* who goes first, lower case or uppercase */
140 {UCOL_CASE_LEVEL
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* do we have an extra case level */
141 {UCOL_NORMALIZATION_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* attribute for normalization */
142 {UCOL_DECOMPOSITION_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},
143 {UCOL_STRENGTH
, {UCOL_PRIMARY
, UCOL_SECONDARY
, UCOL_TERTIARY
, UCOL_QUATERNARY
, UCOL_IDENTICAL
}, 5, UCOL_SHIFTED
},/* attribute for strength */
144 {UCOL_HIRAGANA_QUATERNARY_MODE
, {UCOL_ON
, UCOL_OFF
}, 2, UCOL_SHIFTED
},/* when turned on, this attribute */
146 UColAttribute currAttr
;
147 UColAttributeValue value
;
148 uint32_t i
= 0, j
= 0;
150 for(i
= 0; i
<sizeof(attrs
)/sizeof(attrs
[0]); i
++) {
151 currAttr
= attrs
[i
].att
;
152 ucol_setAttribute(coll
, currAttr
, UCOL_DEFAULT
, &status
);
153 if(U_FAILURE(status
)) {
154 log_err("ucol_setAttribute with the default value returned error: %s\n", u_errorName(status
));
157 value
= ucol_getAttribute(coll
, currAttr
, &status
);
158 if(U_FAILURE(status
)) {
159 log_err("ucol_getAttribute returned error: %s\n", u_errorName(status
));
162 for(j
= 0; j
<attrs
[i
].valueSize
; j
++) {
163 ucol_setAttribute(coll
, currAttr
, attrs
[i
].val
[j
], &status
);
164 if(U_FAILURE(status
)) {
165 log_err("ucol_setAttribute with the value %i returned error: %s\n", attrs
[i
].val
[j
], u_errorName(status
));
169 status
= U_ZERO_ERROR
;
170 ucol_setAttribute(coll
, currAttr
, attrs
[i
].nonValue
, &status
);
171 if(U_SUCCESS(status
)) {
172 log_err("ucol_setAttribute with the bad value didn't return an error\n");
175 status
= U_ZERO_ERROR
;
177 ucol_setAttribute(coll
, currAttr
, value
, &status
);
178 if(U_FAILURE(status
)) {
179 log_err("ucol_setAttribute with the default valuereturned error: %s\n", u_errorName(status
));
184 status
= U_ZERO_ERROR
;
185 value
= ucol_getAttribute(coll
, UCOL_ATTRIBUTE_COUNT
, &status
);
186 if(U_SUCCESS(status
)) {
187 log_err("ucol_getAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
189 status
= U_ZERO_ERROR
;
190 ucol_setAttribute(coll
, UCOL_ATTRIBUTE_COUNT
, UCOL_DEFAULT
, &status
);
191 if(U_SUCCESS(status
)) {
192 log_err("ucol_setAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
194 status
= U_ZERO_ERROR
;
199 static void doAssert(int condition
, const char *message
)
202 log_err("ERROR : %s\n", message
);
207 /* We don't have default rules, at least not in the previous sense */
208 void TestGetDefaultRules(){
210 UErrorCode status
=U_ZERO_ERROR
;
211 UCollator
*coll
=NULL
;
212 int32_t len1
= 0, len2
=0;
213 uint8_t *binColData
= NULL
;
215 UResourceBundle
*res
= NULL
;
216 UResourceBundle
*binColl
= NULL
;
217 uint8_t *binResult
= NULL
;
220 const UChar
* defaultRulesArray
=ucol_getDefaultRulesArray(&size
);
221 log_verbose("Test the function ucol_getDefaultRulesArray()\n");
223 coll
= ucol_openRules(defaultRulesArray
, size
, UCOL_ON
, UCOL_PRIMARY
, &status
);
224 if(U_SUCCESS(status
) && coll
!=NULL
) {
225 binColData
= (uint8_t*)ucol_cloneRuleData(coll
, &len1
, &status
);
231 res
=ures_open(NULL
, "root", &status
);
232 if(U_FAILURE(status
)){
233 log_err("ERROR: Failed to get resource for \"root Locale\" with %s", myErrorName(status
));
236 binColl
=ures_getByKey(res
, "%%Collation", binColl
, &status
);
237 if(U_SUCCESS(status
)){
238 binResult
=(uint8_t*)ures_getBinary(binColl
, &len2
, &status
);
239 if(U_FAILURE(status
)){
240 log_err("ERROR: ures_getBinary() failed\n");
243 log_err("ERROR: ures_getByKey(locale(default), %%Collation) failed");
248 log_err("Error: ucol_getDefaultRulesArray() failed to return the correct length.\n");
250 if(memcmp(binColData
, binResult
, len1
) != 0){
251 log_err("Error: ucol_getDefaultRulesArray() failed\n");
262 /* Collator Properties
263 ucol_open, ucol_strcoll, getStrength/setStrength
264 getDecomposition/setDecomposition, getDisplayName*/
267 UCollator
*col
, *ruled
;
269 int32_t len
= 0, i
= 0;
270 UChar
*source
, *target
;
272 UErrorCode status
= U_ZERO_ERROR
;
274 All the collations have the same version in an ICU
276 ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};
277 ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};
278 ICU 2.2 currVersionArray = {0x21, 0x40, 0x04, 0x04};
279 ICU 2.4 currVersionArray = {0x21, 0x40, 0x04, 0x04};
280 ICU 2.6 currVersionArray = {0x21, 0x40, 0x03, 0x03};
282 UVersionInfo currVersionArray
= {0x29, 0x80, 0x00, 0x04};
283 UVersionInfo currUCAVersionArray
= {4, 0, 0, 0};
284 UVersionInfo versionArray
= {0, 0, 0, 0};
285 UVersionInfo versionUCAArray
= {0, 0, 0, 0};
287 log_verbose("The property tests begin : \n");
288 log_verbose("Test ucol_strcoll : \n");
289 col
= ucol_open("en_US", &status
);
290 if (U_FAILURE(status
)) {
291 log_err("Default Collator creation failed.: %s\n", myErrorName(status
));
295 ucol_getVersion(col
, versionArray
);
296 for (i
=0; i
<4; ++i
) {
297 if (versionArray
[i
] != currVersionArray
[i
]) {
298 log_err("Testing ucol_getVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
299 versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
304 ucol_getUCAVersion(col
, versionUCAArray
);
305 for (i
=0; i
<4; ++i
) {
306 if (versionUCAArray
[i
] != currUCAVersionArray
[i
]) {
307 log_err("Testing ucol_getUCAVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
308 versionUCAArray
[0], versionUCAArray
[1], versionUCAArray
[2], versionUCAArray
[3]);
313 source
=(UChar
*)malloc(sizeof(UChar
) * 12);
314 target
=(UChar
*)malloc(sizeof(UChar
) * 12);
317 u_uastrcpy(source
, "ab");
318 u_uastrcpy(target
, "abc");
320 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
), "ab < abc comparison failed");
322 u_uastrcpy(source
, "ab");
323 u_uastrcpy(target
, "AB");
325 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
), "ab < AB comparison failed");
326 /* u_uastrcpy(source, "black-bird");
327 u_uastrcpy(target, "blackbird"); */
328 u_uastrcpy(target
, "black-bird");
329 u_uastrcpy(source
, "blackbird");
331 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_GREATER
),
332 "black-bird > blackbird comparison failed");
333 u_uastrcpy(source
, "black bird");
334 u_uastrcpy(target
, "black-bird");
335 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_LESS
),
336 "black bird < black-bird comparison failed");
337 u_uastrcpy(source
, "Hello");
338 u_uastrcpy(target
, "hello");
340 doAssert((ucol_strcoll(col
, source
, u_strlen(source
), target
, u_strlen(target
)) == UCOL_GREATER
),
341 "Hello > hello comparison failed");
344 log_verbose("Test ucol_strcoll ends.\n");
346 log_verbose("testing ucol_getStrength() method ...\n");
347 doAssert( (ucol_getStrength(col
) == UCOL_TERTIARY
), "collation object has the wrong strength");
348 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
350 log_verbose("testing ucol_setStrength() method ...\n");
351 ucol_setStrength(col
, UCOL_SECONDARY
);
352 doAssert( (ucol_getStrength(col
) != UCOL_TERTIARY
), "collation object's strength is secondary difference");
353 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
354 doAssert( (ucol_getStrength(col
) == UCOL_SECONDARY
), "collation object has the wrong strength");
357 log_verbose("Get display name for the default collation in German : \n");
359 len
=ucol_getDisplayName("en_US", "de_DE", NULL
, 0, &status
);
360 if(status
==U_BUFFER_OVERFLOW_ERROR
){
362 disName
=(UChar
*)malloc(sizeof(UChar
) * (len
+1));
363 ucol_getDisplayName("en_US", "de_DE", disName
, len
+1, &status
);
364 log_verbose("the display name for default collation in german: %s\n", austrdup(disName
) );
367 if(U_FAILURE(status
)){
368 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status
));
371 log_verbose("Default collation getDisplayName ended.\n");
373 ruled
= ucol_open("da_DK", &status
);
374 log_verbose("ucol_getRules() testing ...\n");
375 ucol_getRules(ruled
, &tempLength
);
376 doAssert( tempLength
!= 0, "getRules() result incorrect" );
377 log_verbose("getRules tests end.\n");
379 UChar
*buffer
= (UChar
*)malloc(200000*sizeof(UChar
));
380 int32_t bufLen
= 200000;
382 log_verbose("ucol_getRulesEx() testing ...\n");
383 tempLength
= ucol_getRulesEx(col
,UCOL_TAILORING_ONLY
,buffer
,bufLen
);
384 doAssert( tempLength
== 0x0a, "getRulesEx() result incorrect" );
385 log_verbose("getRules tests end.\n");
387 log_verbose("ucol_getRulesEx() testing ...\n");
388 tempLength
=ucol_getRulesEx(col
,UCOL_FULL_RULES
,buffer
,bufLen
);
389 doAssert( tempLength
!= 0, "getRulesEx() result incorrect" );
390 log_verbose("getRules tests end.\n");
396 log_verbose("open an collator for french locale");
397 col
= ucol_open("fr_FR", &status
);
398 if (U_FAILURE(status
)) {
399 log_err("ERROR: Creating French collation failed.: %s\n", myErrorName(status
));
402 ucol_setStrength(col
, UCOL_PRIMARY
);
403 log_verbose("testing ucol_getStrength() method again ...\n");
404 doAssert( (ucol_getStrength(col
) != UCOL_TERTIARY
), "collation object has the wrong strength");
405 doAssert( (ucol_getStrength(col
) == UCOL_PRIMARY
), "collation object's strength is not primary difference");
407 log_verbose("testing French ucol_setStrength() method ...\n");
408 ucol_setStrength(col
, UCOL_TERTIARY
);
409 doAssert( (ucol_getStrength(col
) == UCOL_TERTIARY
), "collation object's strength is not tertiary difference");
410 doAssert( (ucol_getStrength(col
) != UCOL_PRIMARY
), "collation object's strength is primary difference");
411 doAssert( (ucol_getStrength(col
) != UCOL_SECONDARY
), "collation object's strength is secondary difference");
414 log_verbose("Get display name for the french collation in english : \n");
415 len
=ucol_getDisplayName("fr_FR", "en_US", NULL
, 0, &status
);
416 if(status
==U_BUFFER_OVERFLOW_ERROR
){
418 disName
=(UChar
*)malloc(sizeof(UChar
) * (len
+1));
419 ucol_getDisplayName("fr_FR", "en_US", disName
, len
+1, &status
);
420 log_verbose("the display name for french collation in english: %s\n", austrdup(disName
) );
423 if(U_FAILURE(status
)){
424 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status
));
427 log_verbose("Default collation getDisplayName ended.\n");
431 /* Test RuleBasedCollator and getRules*/
432 void TestRuleBasedColl()
434 UCollator
*col1
, *col2
, *col3
, *col4
;
435 UCollationElements
*iter1
, *iter2
;
440 const UChar
*rule1
, *rule2
, *rule3
, *rule4
;
442 UErrorCode status
= U_ZERO_ERROR
;
443 u_uastrcpy(ruleset1
, "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
444 u_uastrcpy(ruleset2
, "&9 < a, A < b, B < c, C < d, D, e, E");
447 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
,&status
);
448 if (U_FAILURE(status
)) {
449 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
453 log_verbose("PASS: RuleBased Collator creation passed\n");
455 status
= U_ZERO_ERROR
;
456 col2
= ucol_openRules(ruleset2
, u_strlen(ruleset2
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
457 if (U_FAILURE(status
)) {
458 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
462 log_verbose("PASS: RuleBased Collator creation passed\n");
465 status
= U_ZERO_ERROR
;
466 col3
= ucol_open(NULL
, &status
);
467 if (U_FAILURE(status
)) {
468 log_err("Default Collator creation failed.: %s\n", myErrorName(status
));
472 log_verbose("PASS: Default Collator creation passed\n");
474 rule1
= ucol_getRules(col1
, &tempLength
);
475 rule2
= ucol_getRules(col2
, &tempLength
);
476 rule3
= ucol_getRules(col3
, &tempLength
);
478 doAssert((u_strcmp(rule1
, rule2
) != 0), "Default collator getRules failed");
479 doAssert((u_strcmp(rule2
, rule3
) != 0), "Default collator getRules failed");
480 doAssert((u_strcmp(rule1
, rule3
) != 0), "Default collator getRules failed");
482 col4
=ucol_openRules(rule2
, u_strlen(rule2
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
483 if (U_FAILURE(status
)) {
484 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
487 rule4
= ucol_getRules(col4
, &tempLength
);
488 doAssert((u_strcmp(rule2
, rule4
) == 0), "Default collator getRules failed");
495 /* tests that modifier ! is always ignored */
496 u_uastrcpy(ruleset1
, "!&a<b");
500 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
501 if (U_FAILURE(status
)) {
502 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
505 col2
= ucol_open("en_US", &status
);
506 if (U_FAILURE(status
)) {
507 log_err("en_US Collator creation failed.: %s\n", myErrorName(status
));
510 iter1
= ucol_openElements(col1
, teststr
, 3, &status
);
511 iter2
= ucol_openElements(col2
, teststr
, 3, &status
);
512 if(U_FAILURE(status
)) {
513 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status
));
517 /* testing with en since thai has its own tailoring */
518 uint32_t ce
= ucol_next(iter1
, &status
);
519 uint32_t ce2
= ucol_next(iter2
, &status
);
520 if(U_FAILURE(status
)) {
521 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status
));
525 log_err("! modifier test failed");
527 if (ce
== UCOL_NULLORDER
) {
531 ucol_closeElements(iter1
);
532 ucol_closeElements(iter2
);
535 /* test that we can start a rule without a & or < */
536 u_uastrcpy(ruleset1
, "< z < a");
537 col1
= ucol_openRules(ruleset1
, u_strlen(ruleset1
), UCOL_DEFAULT
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
538 if (U_FAILURE(status
)) {
539 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status
));
542 u_uastrcpy(teststr
, "z");
543 u_uastrcpy(teststr2
, "a");
544 if (ucol_greaterOrEqual(col1
, teststr
, 1, teststr2
, 1)) {
545 log_err("Rule \"z < a\" fails");
552 UErrorCode status
= U_ZERO_ERROR
;
557 log_verbose("The compare tests begin : \n");
559 col
= ucol_open("en_US", &status
);
560 if(U_FAILURE(status
)) {
561 log_err("ucal_open() collation creation failed.: %s\n", myErrorName(status
));
564 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
565 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
566 u_uastrcpy(test1
, "Abcda");
567 u_uastrcpy(test2
, "abcda");
569 log_verbose("Use tertiary comparison level testing ....\n");
571 doAssert( (!ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" != \"abcda\" ");
572 doAssert( (ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" >>> \"abcda\" ");
573 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" >>> \"abcda\"");
575 ucol_setStrength(col
, UCOL_SECONDARY
);
576 log_verbose("Use secondary comparison level testing ....\n");
578 doAssert( (ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
) )), "Result should be \"Abcda\" == \"abcda\"");
579 doAssert( (!ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
580 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
) )), "Result should be \"Abcda\" == \"abcda\"");
582 ucol_setStrength(col
, UCOL_PRIMARY
);
583 log_verbose("Use primary comparison level testing ....\n");
585 doAssert( (ucol_equal(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
586 doAssert( (!ucol_greater(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
587 doAssert( (ucol_greaterOrEqual(col
, test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"Abcda\" == \"abcda\"");
590 log_verbose("The compare tests end.\n");
597 ---------------------------------------------
598 tests decomposition setting
600 void TestDecomposition() {
601 UErrorCode status
= U_ZERO_ERROR
;
602 UCollator
*en_US
, *el_GR
, *vi_VN
;
603 en_US
= ucol_open("en_US", &status
);
604 el_GR
= ucol_open("el_GR", &status
);
605 vi_VN
= ucol_open("vi_VN", &status
);
607 if (U_FAILURE(status
)) {
608 log_err("ERROR: collation creation failed.: %s\n", myErrorName(status
));
612 if (ucol_getAttribute(vi_VN
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_ON
||
615 log_err("ERROR: vi_VN collation did not have cannonical decomposition for normalization!\n");
618 status
= U_ZERO_ERROR
;
619 if (ucol_getAttribute(el_GR
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_ON
||
622 log_err("ERROR: el_GR collation did not have cannonical decomposition for normalization!\n");
625 status
= U_ZERO_ERROR
;
626 if (ucol_getAttribute(en_US
, UCOL_NORMALIZATION_MODE
, &status
) != UCOL_OFF
||
629 log_err("ERROR: en_US collation had cannonical decomposition for normalization!\n");
637 #define CLONETEST_COLLATOR_COUNT 3
639 void TestSafeClone() {
642 UCollator
* someCollators
[CLONETEST_COLLATOR_COUNT
];
643 UCollator
* someClonedCollators
[CLONETEST_COLLATOR_COUNT
];
645 UErrorCode err
= U_ZERO_ERROR
;
646 int8_t testSize
= 6; /* Leave this here to test buffer alingment in memory*/
647 uint8_t buffer
[CLONETEST_COLLATOR_COUNT
] [U_COL_SAFECLONE_BUFFERSIZE
];
648 int32_t bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
651 if (TestBufferSize()) {
652 log_err("U_COL_SAFECLONE_BUFFERSIZE should be larger than sizeof(UCollator)\n");
656 test1
=(UChar
*)malloc(sizeof(UChar
) * testSize
);
657 test2
=(UChar
*)malloc(sizeof(UChar
) * testSize
);
658 u_uastrcpy(test1
, "abCda");
659 u_uastrcpy(test2
, "abcda");
661 /* one default collator & two complex ones */
662 someCollators
[0] = ucol_open("en_US", &err
);
663 someCollators
[1] = ucol_open("ko", &err
);
664 someCollators
[2] = ucol_open("ja_JP", &err
);
666 log_data_err("Couldn't open one or more collators\n");
670 /* Check the various error & informational states: */
672 /* Null status - just returns NULL */
673 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, 0))
675 log_err("FAIL: Cloned Collator failed to deal correctly with null status\n");
677 /* error status - should return 0 & keep error the same */
678 err
= U_MEMORY_ALLOCATION_ERROR
;
679 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
) || err
!= U_MEMORY_ALLOCATION_ERROR
)
681 log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
685 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
686 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], 0, &err
) || err
!= U_ILLEGAL_ARGUMENT_ERROR
)
688 log_err("FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer\n");
692 /* buffer size pointer is 0 - fill in pbufferSize with a size */
694 if (0 != ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
) || U_FAILURE(err
) || bufferSize
<= 0)
696 log_err("FAIL: Cloned Collator failed a sizing request ('preflighting')\n");
698 /* Verify our define is large enough */
699 if (U_COL_SAFECLONE_BUFFERSIZE
< bufferSize
)
701 log_err("FAIL: Pre-calculated buffer size is too small\n");
703 /* Verify we can use this run-time calculated size */
704 if (0 == (col
= ucol_safeClone(someCollators
[0], buffer
[0], &bufferSize
, &err
)) || U_FAILURE(err
))
706 log_err("FAIL: Collator can't be cloned with run-time size\n");
708 if (col
) ucol_close(col
);
709 /* size one byte too small - should allocate & let us know */
711 if (0 == (col
= ucol_safeClone(someCollators
[0], 0, &bufferSize
, &err
)) || err
!= U_SAFECLONE_ALLOCATED_WARNING
)
713 log_err("FAIL: Cloned Collator failed to deal correctly with too-small buffer size\n");
715 if (col
) ucol_close(col
);
717 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
720 /* Null buffer pointer - return Collator & set error to U_SAFECLONE_ALLOCATED_ERROR */
721 if (0 == (col
= ucol_safeClone(someCollators
[0], 0, &bufferSize
, &err
)) || err
!= U_SAFECLONE_ALLOCATED_WARNING
)
723 log_err("FAIL: Cloned Collator failed to deal correctly with null buffer pointer\n");
725 if (col
) ucol_close(col
);
728 /* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
729 if (0 != ucol_safeClone(0, buffer
[0], &bufferSize
, &err
) || err
!= U_ILLEGAL_ARGUMENT_ERROR
)
731 log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
736 /* change orig & clone & make sure they are independent */
738 for (index
= 0; index
< CLONETEST_COLLATOR_COUNT
; index
++)
740 bufferSize
= U_COL_SAFECLONE_BUFFERSIZE
;
741 someClonedCollators
[index
] = ucol_safeClone(someCollators
[index
], buffer
[index
], &bufferSize
, &err
);
743 ucol_setStrength(someClonedCollators
[index
], UCOL_TERTIARY
);
744 ucol_setStrength(someCollators
[index
], UCOL_PRIMARY
);
745 ucol_setAttribute(someClonedCollators
[index
], UCOL_CASE_LEVEL
, UCOL_OFF
, &err
);
746 ucol_setAttribute(someCollators
[index
], UCOL_CASE_LEVEL
, UCOL_OFF
, &err
);
748 doAssert( (ucol_greater(someClonedCollators
[index
], test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"abCda\" >>> \"abcda\" ");
749 doAssert( (ucol_equal(someCollators
[index
], test1
, u_strlen(test1
), test2
, u_strlen(test2
))), "Result should be \"abcda\" == \"abCda\"");
751 ucol_close(someClonedCollators
[index
]);
752 ucol_close(someCollators
[index
]);
759 ----------------------------------------------------------------------------
760 ctor -- Tests the getSortKey
764 uint8_t *sortk1
= NULL
, *sortk2
= NULL
, *sortk3
= NULL
, *sortkEmpty
= NULL
;
765 uint8_t sortk2_compat
[] = {
767 0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01,
768 0x09, 0x01, 0x09, 0x01, 0x25, 0x01,
769 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
771 /*0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01, 0x09, 0x01, 0x09, 0x01, 0x1C, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
773 /*0x19, 0x1B, 0x1D, 0x1F, 0x19, 0x01, 0x09, 0x01, 0x09, 0x01, 0x18, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
775 /*0x19, 0x1B, 0x1D, 0x1F, 0x19, 0x01, 0x0A, 0x01, 0x0A, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
776 /*this is a 1.8 sortkey */
777 /*0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00*/
778 /*this is a 1.7 sortkey */
779 /*0x02, 0x54, 0x02, 0x55, 0x02, 0x56, 0x02, 0x57, 0x02, 0x54, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00*/
780 /* this is a 1.6 sortkey */
781 /*0x00, 0x53, 0x00, 0x54, 0x00, 0x55, 0x00, 0x56, 0x00, 0x53, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00*/
784 int32_t sortklen
, osortklen
;
785 uint32_t toStringLen
=0;
787 UChar
*test1
, *test2
, *test3
;
788 UErrorCode status
= U_ZERO_ERROR
;
789 char toStringBuffer
[256], *resultP
;
792 uint8_t s1
[] = { 0x9f, 0x00 };
793 uint8_t s2
[] = { 0x61, 0x00 };
796 strcmpResult
= strcmp((const char *)s1
, (const char *)s2
);
797 log_verbose("strcmp(0x9f..., 0x61...) = %d\n", strcmpResult
);
799 if(strcmpResult
<= 0) {
800 log_err("ERR: expected strcmp(\"9f 00\", \"61 00\") to be >=0 (GREATER).. got %d. Calling strcmp() for sortkeys may not work! \n",
805 log_verbose("testing SortKey begins...\n");
806 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
807 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
808 /* col = ucol_open(NULL, &status); */
809 col
= ucol_open("en_US", &status
);
810 if (U_FAILURE(status
)) {
811 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
816 if(ucol_getStrength(col
) != UCOL_DEFAULT_STRENGTH
)
818 log_err("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
820 /* Need to use identical strength */
821 ucol_setAttribute(col
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &status
);
823 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
824 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
825 test3
=(UChar
*)malloc(sizeof(UChar
) * 6);
827 memset(test1
,0xFE, sizeof(UChar
)*6);
828 memset(test2
,0xFE, sizeof(UChar
)*6);
829 memset(test3
,0xFE, sizeof(UChar
)*6);
832 u_uastrcpy(test1
, "Abcda");
833 u_uastrcpy(test2
, "abcda");
834 u_uastrcpy(test3
, "abcda");
836 log_verbose("Use tertiary comparison level testing ....\n");
838 sortklen
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
839 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
840 memset(sortk1
,0xFE, sortklen
);
841 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortklen
+1);
843 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
844 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
845 memset(sortk2
,0xFE, sortklen
);
846 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortklen
+1);
848 osortklen
= sortklen
;
849 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test3
), NULL
, 0);
850 sortk3
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
851 memset(sortk3
,0xFE, sortklen
);
852 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk3
, sortklen
+1);
854 doAssert( (sortklen
== osortklen
), "Sortkey length should be the same (abcda, abcda)");
856 doAssert( (memcmp(sortk1
, sortk2
, sortklen
) > 0), "Result should be \"Abcda\" > \"abcda\"");
857 doAssert( (memcmp(sortk2
, sortk1
, sortklen
) < 0), "Result should be \"abcda\" < \"Abcda\"");
858 doAssert( (memcmp(sortk2
, sortk3
, sortklen
) == 0), "Result should be \"abcda\" == \"abcda\"");
860 doAssert( (memcmp(sortk2
, sortk2_compat
, sortklen
) == 0), "Binary format for 'abcda' sortkey different!");
862 resultP
= ucol_sortKeyToString(col
, sortk2_compat
, toStringBuffer
, &toStringLen
);
863 doAssert( (resultP
!= 0), "sortKeyToString failed!");
865 #if 1 /* verobse log of sortkeys */
871 strcpy(junk2
, "abcda[2] ");
872 strcpy(junk3
, " abcda[3] ");
874 for(i
=0;i
<sortklen
;i
++)
876 sprintf(junk2
+strlen(junk2
), "%02X ",(int)( 0xFF & sortk2
[i
]));
877 sprintf(junk3
+strlen(junk3
), "%02X ",(int)( 0xFF & sortk3
[i
]));
880 log_verbose("%s\n", junk2
);
881 log_verbose("%s\n", junk3
);
889 log_verbose("Use secondary comparision level testing ...\n");
890 ucol_setStrength(col
, UCOL_SECONDARY
);
891 sortklen
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
892 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
893 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortklen
+1);
894 sortklen
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
895 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen
+1));
896 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortklen
+1);
898 doAssert( !(memcmp(sortk1
, sortk2
, sortklen
) > 0), "Result should be \"Abcda\" == \"abcda\"");
899 doAssert( !(memcmp(sortk2
, sortk1
, sortklen
) < 0), "Result should be \"abcda\" == \"Abcda\"");
900 doAssert( (memcmp(sortk1
, sortk2
, sortklen
) == 0), "Result should be \"abcda\" == \"abcda\"");
902 log_verbose("getting sortkey for an empty string\n");
903 ucol_setAttribute(col
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
904 sortklen
= ucol_getSortKey(col
, test1
, 0, NULL
, 0);
905 sortkEmpty
= (uint8_t*)malloc(sizeof(uint8_t) * sortklen
+1);
906 sortklen
= ucol_getSortKey(col
, test1
, 0, sortkEmpty
, sortklen
+1);
907 if(sortklen
!= 3 || sortkEmpty
[0] != 1 || sortkEmpty
[0] != 1 || sortkEmpty
[2] != 0) {
908 log_err("Empty string generated wrong sortkey!\n");
912 log_verbose("testing passing invalid string\n");
913 sortklen
= ucol_getSortKey(col
, NULL
, 0, NULL
, 0);
915 log_err("Invalid string didn't return sortkey size of 0\n");
919 log_verbose("testing sortkey ends...\n");
930 uint8_t *sortk1
, *sortk2
, *sortk3
;
931 int32_t sortk1len
, sortk2len
, sortk3len
;
933 UChar
*test1
, *test2
, *test3
;
934 UErrorCode status
= U_ZERO_ERROR
;
935 log_verbose("testing getHashCode begins...\n");
936 col
= ucol_open("en_US", &status
);
937 if (U_FAILURE(status
)) {
938 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
941 test1
=(UChar
*)malloc(sizeof(UChar
) * 6);
942 test2
=(UChar
*)malloc(sizeof(UChar
) * 6);
943 test3
=(UChar
*)malloc(sizeof(UChar
) * 6);
944 u_uastrcpy(test1
, "Abcda");
945 u_uastrcpy(test2
, "abcda");
946 u_uastrcpy(test3
, "abcda");
948 log_verbose("Use tertiary comparison level testing ....\n");
949 sortk1len
=ucol_getSortKey(col
, test1
, u_strlen(test1
), NULL
, 0);
950 sortk1
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk1len
+1));
951 ucol_getSortKey(col
, test1
, u_strlen(test1
), sortk1
, sortk1len
+1);
952 sortk2len
=ucol_getSortKey(col
, test2
, u_strlen(test2
), NULL
, 0);
953 sortk2
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk2len
+1));
954 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk2
, sortk2len
+1);
955 sortk3len
=ucol_getSortKey(col
, test2
, u_strlen(test3
), NULL
, 0);
956 sortk3
=(uint8_t*)malloc(sizeof(uint8_t) * (sortk3len
+1));
957 ucol_getSortKey(col
, test2
, u_strlen(test2
), sortk3
, sortk3len
+1);
960 log_verbose("ucol_hashCode() testing ...\n");
962 doAssert( ucol_keyHashCode(sortk1
, sortk1len
) != ucol_keyHashCode(sortk2
, sortk2len
), "Hash test1 result incorrect" );
963 doAssert( !(ucol_keyHashCode(sortk1
, sortk1len
) == ucol_keyHashCode(sortk2
, sortk2len
)), "Hash test2 result incorrect" );
964 doAssert( ucol_keyHashCode(sortk2
, sortk2len
) == ucol_keyHashCode(sortk3
, sortk3len
), "Hash result not equal" );
966 log_verbose("hashCode tests end.\n");
978 *----------------------------------------------------------------------------
979 * Tests the UCollatorElements API.
985 int32_t order1
, order2
, order3
;
986 UChar
*testString1
, *testString2
;
988 UCollationElements
*iterator1
, *iterator2
, *iterator3
;
989 UErrorCode status
= U_ZERO_ERROR
;
990 log_verbose("testing UCollatorElements begins...\n");
991 col
= ucol_open("en_US", &status
);
992 ucol_setAttribute(col
, UCOL_NORMALIZATION_MODE
, UCOL_OFF
, &status
);
993 if (U_FAILURE(status
)) {
994 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status
));
998 testString1
=(UChar
*)malloc(sizeof(UChar
) * 150);
999 testString2
=(UChar
*)malloc(sizeof(UChar
) * 150);
1000 u_uastrcpy(testString1
, "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
1001 u_uastrcpy(testString2
, "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
1003 log_verbose("Constructors and comparison testing....\n");
1005 iterator1
= ucol_openElements(col
, testString1
, u_strlen(testString1
), &status
);
1006 if(U_FAILURE(status
)) {
1007 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1011 else{ log_verbose("PASS: Default collationElement iterator1 creation passed\n");}
1013 iterator2
= ucol_openElements(col
, testString1
, u_strlen(testString1
), &status
);
1014 if(U_FAILURE(status
)) {
1015 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1019 else{ log_verbose("PASS: Default collationElement iterator2 creation passed\n");}
1021 iterator3
= ucol_openElements(col
, testString2
, u_strlen(testString2
), &status
);
1022 if(U_FAILURE(status
)) {
1023 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status
));
1027 else{ log_verbose("PASS: Default collationElement iterator3 creation passed\n");}
1029 offset
=ucol_getOffset(iterator1
);
1030 ucol_setOffset(iterator1
, 6, &status
);
1031 if (U_FAILURE(status
)) {
1032 log_err("Error in setOffset for UCollatorElements iterator.: %s\n", myErrorName(status
));
1035 if(ucol_getOffset(iterator1
)==6)
1036 log_verbose("setOffset and getOffset working fine\n");
1038 log_err("error in set and get Offset got %d instead of 6\n", ucol_getOffset(iterator1
));
1041 ucol_setOffset(iterator1
, 0, &status
);
1042 order1
= ucol_next(iterator1
, &status
);
1043 if (U_FAILURE(status
)) {
1044 log_err("Somehow ran out of memory stepping through the iterator1.: %s\n", myErrorName(status
));
1047 order2
=ucol_getOffset(iterator2
);
1048 doAssert((order1
!= order2
), "The first iterator advance failed");
1049 order2
= ucol_next(iterator2
, &status
);
1050 if (U_FAILURE(status
)) {
1051 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1054 order3
= ucol_next(iterator3
, &status
);
1055 if (U_FAILURE(status
)) {
1056 log_err("Somehow ran out of memory stepping through the iterator3.: %s\n", myErrorName(status
));
1060 doAssert((order1
== order2
), "The second iterator advance failed should be the same as first one");
1062 doAssert( (ucol_primaryOrder(order1
) == ucol_primaryOrder(order3
)), "The primary orders should be identical");
1063 doAssert( (ucol_secondaryOrder(order1
) == ucol_secondaryOrder(order3
)), "The secondary orders should be identical");
1064 doAssert( (ucol_tertiaryOrder(order1
) == ucol_tertiaryOrder(order3
)), "The tertiary orders should be identical");
1066 order1
=ucol_next(iterator1
, &status
);
1067 if (U_FAILURE(status
)) {
1068 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1071 order3
=ucol_next(iterator3
, &status
);
1072 if (U_FAILURE(status
)) {
1073 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1076 doAssert( (ucol_primaryOrder(order1
) == ucol_primaryOrder(order3
)), "The primary orders should be identical");
1077 doAssert( (ucol_tertiaryOrder(order1
) != ucol_tertiaryOrder(order3
)), "The tertiary orders should be different");
1079 order1
=ucol_next(iterator1
, &status
);
1080 if (U_FAILURE(status
)) {
1081 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1084 order3
=ucol_next(iterator3
, &status
);
1085 if (U_FAILURE(status
)) {
1086 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status
));
1089 /* this here, my friends, is either pure lunacy or something so obsolete that even it's mother
1090 * doesn't care about it. Essentialy, this test complains if secondary values for 'I' and '_'
1091 * are the same. According to the UCA, this is not true. Therefore, remove the test.
1092 * Besides, if primary strengths for two code points are different, it doesn't matter one bit
1093 * what is the relation between secondary or any other strengths.
1094 * killed by weiv 06/11/2002.
1097 doAssert( ((order1 & UCOL_SECONDARYMASK) != (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be different");
1099 doAssert( (order1
!= UCOL_NULLORDER
), "Unexpected end of iterator reached");
1103 ucol_closeElements(iterator1
);
1104 ucol_closeElements(iterator2
);
1105 ucol_closeElements(iterator3
);
1108 log_verbose("testing CollationElementIterator ends...\n");
1111 void TestGetLocale() {
1112 UErrorCode status
= U_ZERO_ERROR
;
1113 const char *rules
= "&a<x<y<z";
1114 UChar rlz
[256] = {0};
1115 uint32_t rlzLen
= u_unescape(rules
, rlz
, 256);
1117 UCollator
*coll
= NULL
;
1118 const char *locale
= NULL
;
1122 /* Now that the collation tree is separate, actual==valid at all times. [alan] */
1123 static const struct {
1124 const char* requestedLocale
;
1125 const char* validLocale
;
1126 const char* actualLocale
;
1128 { "sr_YU", "sr_YU", "ru" },
1129 { "sh_YU", "sh_YU", "hr" }, /* this used to be sh, but now sh collation aliases hr */
1130 { "en_BE_FOO", "en_BE", "en_BE" },
1131 { "fr_FR_NONEXISTANT", "fr_FR", "fr" }
1134 /* test opening collators for different locales */
1135 for(i
= 0; i
<sizeof(testStruct
)/sizeof(testStruct
[0]); i
++) {
1136 status
= U_ZERO_ERROR
;
1137 coll
= ucol_open(testStruct
[i
].requestedLocale
, &status
);
1138 if(U_FAILURE(status
)) {
1139 log_err("Failed to open collator for %s with %s\n", testStruct
[i
].requestedLocale
, u_errorName(status
));
1143 locale
= ucol_getLocale(coll
, ULOC_REQUESTED_LOCALE
, &status
);
1144 if(strcmp(locale
, testStruct
[i
].requestedLocale
) != 0) {
1145 log_err("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].requestedLocale
, locale
);
1147 locale
= ucol_getLocale(coll
, ULOC_VALID_LOCALE
, &status
);
1148 if(strcmp(locale
, testStruct
[i
].validLocale
) != 0) {
1149 log_err("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].validLocale
, locale
);
1151 locale
= ucol_getLocale(coll
, ULOC_ACTUAL_LOCALE
, &status
);
1152 if(strcmp(locale
, testStruct
[i
].actualLocale
) != 0) {
1153 log_err("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct
[i
].requestedLocale
, testStruct
[i
].actualLocale
, locale
);
1158 /* completely non-existant locale for collator should get a default collator */
1160 UCollator
*defaultColl
= ucol_open(NULL
, &status
);
1161 coll
= ucol_open("blahaha", &status
);
1162 if(U_SUCCESS(status
)) {
1163 if(strcmp(ucol_getLocale(coll
, ULOC_REQUESTED_LOCALE
, &status
), "blahaha")) {
1164 log_err("Nonexisting locale didn't preserve the requested locale\n");
1166 if(strcmp(ucol_getLocale(coll
, ULOC_VALID_LOCALE
, &status
),
1167 ucol_getLocale(defaultColl
, ULOC_VALID_LOCALE
, &status
))) {
1168 log_err("Valid locale for nonexisting locale locale collator differs "
1169 "from valid locale for default collator\n");
1171 if(strcmp(ucol_getLocale(coll
, ULOC_ACTUAL_LOCALE
, &status
),
1172 ucol_getLocale(defaultColl
, ULOC_ACTUAL_LOCALE
, &status
))) {
1173 log_err("Actual locale for nonexisting locale locale collator differs "
1174 "from actual locale for default collator\n");
1177 ucol_close(defaultColl
);
1179 log_data_err("Couldn't open collators\n");
1185 /* collator instantiated from rules should have all three locales NULL */
1186 coll
= ucol_openRules(rlz
, rlzLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, NULL
, &status
);
1187 locale
= ucol_getLocale(coll
, ULOC_REQUESTED_LOCALE
, &status
);
1188 if(locale
!= NULL
) {
1189 log_err("For collator instantiated from rules, requested locale returned %s instead of NULL\n", locale
);
1191 locale
= ucol_getLocale(coll
, ULOC_VALID_LOCALE
, &status
);
1192 if(locale
!= NULL
) {
1193 log_err("For collator instantiated from rules, valid locale returned %s instead of NULL\n", locale
);
1195 locale
= ucol_getLocale(coll
, ULOC_ACTUAL_LOCALE
, &status
);
1196 if(locale
!= NULL
) {
1197 log_err("For collator instantiated from rules, actual locale returned %s instead of NULL\n", locale
);
1207 count
=ucol_countAvailable();
1208 /* use something sensible w/o hardcoding the count */
1210 log_err("Error in countAvailable(), it returned %d\n", count
);
1213 log_verbose("PASS: countAvailable() successful, it returned %d\n", count
);
1215 for(i
=0;i
<count
;i
++)
1216 log_verbose("%s\n", ucol_getAvailable(i
));
1223 const char *original
;
1227 static int compare_teststruct(const void *string1
, const void *string2
) {
1228 return(strcmp((const char *)((struct teststruct
*)string1
)->key
, (const char *)((struct teststruct
*)string2
)->key
));
1232 UErrorCode status
= U_ZERO_ERROR
;
1234 UCollator
*coll
= ucol_open("sh", &status
);
1236 uint8_t sortkey
[512], lower
[512], upper
[512];
1239 const char *test
[] = {
1243 "j\\u00F6hn sm\\u00EFth",
1244 "J\\u00F6hn Sm\\u00EFth",
1245 "J\\u00D6HN SM\\u00CFTH",
1250 static struct teststruct tests
[] = {
1251 {"\\u010CAKI MIHALJ" } ,
1252 {"\\u010CAKI MIHALJ" } ,
1253 {"\\u010CAKI PIRO\\u0160KA" },
1254 { "\\u010CABAI ANDRIJA" } ,
1255 {"\\u010CABAI LAJO\\u0160" } ,
1256 {"\\u010CABAI MARIJA" } ,
1257 {"\\u010CABAI STEVAN" } ,
1258 {"\\u010CABAI STEVAN" } ,
1259 {"\\u010CABARKAPA BRANKO" } ,
1260 {"\\u010CABARKAPA MILENKO" } ,
1261 {"\\u010CABARKAPA MIROSLAV" } ,
1262 {"\\u010CABARKAPA SIMO" } ,
1263 {"\\u010CABARKAPA STANKO" } ,
1264 {"\\u010CABARKAPA TAMARA" } ,
1265 {"\\u010CABARKAPA TOMA\\u0160" } ,
1266 {"\\u010CABDARI\\u0106 NIKOLA" } ,
1267 {"\\u010CABDARI\\u0106 ZORICA" } ,
1268 {"\\u010CABI NANDOR" } ,
1269 {"\\u010CABOVI\\u0106 MILAN" } ,
1270 {"\\u010CABRADI AGNEZIJA" } ,
1271 {"\\u010CABRADI IVAN" } ,
1272 {"\\u010CABRADI JELENA" } ,
1273 {"\\u010CABRADI LJUBICA" } ,
1274 {"\\u010CABRADI STEVAN" } ,
1275 {"\\u010CABRDA MARTIN" } ,
1276 {"\\u010CABRILO BOGDAN" } ,
1277 {"\\u010CABRILO BRANISLAV" } ,
1278 {"\\u010CABRILO LAZAR" } ,
1279 {"\\u010CABRILO LJUBICA" } ,
1280 {"\\u010CABRILO SPASOJA" } ,
1281 {"\\u010CADE\\u0160 ZDENKA" } ,
1282 {"\\u010CADESKI BLAGOJE" } ,
1283 {"\\u010CADOVSKI VLADIMIR" } ,
1284 {"\\u010CAGLJEVI\\u0106 TOMA" } ,
1285 {"\\u010CAGOROVI\\u0106 VLADIMIR" } ,
1286 {"\\u010CAJA VANKA" } ,
1287 {"\\u010CAJI\\u0106 BOGOLJUB" } ,
1288 {"\\u010CAJI\\u0106 BORISLAV" } ,
1289 {"\\u010CAJI\\u0106 RADOSLAV" } ,
1290 {"\\u010CAK\\u0160IRAN MILADIN" } ,
1291 {"\\u010CAKAN EUGEN" } ,
1292 {"\\u010CAKAN EVGENIJE" } ,
1293 {"\\u010CAKAN IVAN" } ,
1294 {"\\u010CAKAN JULIJAN" } ,
1295 {"\\u010CAKAN MIHAJLO" } ,
1296 {"\\u010CAKAN STEVAN" } ,
1297 {"\\u010CAKAN VLADIMIR" } ,
1298 {"\\u010CAKAN VLADIMIR" } ,
1299 {"\\u010CAKAN VLADIMIR" } ,
1300 {"\\u010CAKARA ANA" } ,
1301 {"\\u010CAKAREVI\\u0106 MOMIR" } ,
1302 {"\\u010CAKAREVI\\u0106 NEDELJKO" } ,
1303 {"\\u010CAKI \\u0160ANDOR" } ,
1304 {"\\u010CAKI AMALIJA" } ,
1305 {"\\u010CAKI ANDRA\\u0160" } ,
1306 {"\\u010CAKI LADISLAV" } ,
1307 {"\\u010CAKI LAJO\\u0160" } ,
1308 {"\\u010CAKI LASLO" } ,
1313 int32_t i
= 0, j
= 0, k
= 0, buffSize
= 0, skSize
= 0, lowerSize
= 0, upperSize
= 0;
1314 int32_t arraySize
= sizeof(tests
)/sizeof(tests
[0]);
1316 if(U_SUCCESS(status
) && coll
) {
1317 for(i
= 0; i
<arraySize
; i
++) {
1318 buffSize
= u_unescape(tests
[i
].original
, buffer
, 512);
1319 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, tests
[i
].key
, 512);
1322 qsort(tests
, arraySize
, sizeof(struct teststruct
), compare_teststruct
);
1324 for(i
= 0; i
< arraySize
-1; i
++) {
1325 for(j
= i
+1; j
< arraySize
; j
++) {
1326 lowerSize
= ucol_getBound(tests
[i
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1327 upperSize
= ucol_getBound(tests
[j
].key
, -1, UCOL_BOUND_UPPER
, 1, upper
, 512, &status
);
1328 for(k
= i
; k
<= j
; k
++) {
1329 if(strcmp((const char *)lower
, (const char *)tests
[k
].key
) > 0) {
1330 log_err("Problem with lower! j = %i (%s vs %s)\n", k
, tests
[k
].original
, tests
[i
].original
);
1332 if(strcmp((const char *)upper
, (const char *)tests
[k
].key
) <= 0) {
1333 log_err("Problem with upper! j = %i (%s vs %s)\n", k
, tests
[k
].original
, tests
[j
].original
);
1341 for(i
= 0; i
< 1000; i
++) {
1342 lowerRND
= (rand()/(RAND_MAX
/arraySize
));
1343 upperRND
= lowerRND
+ (rand()/(RAND_MAX
/(arraySize
-lowerRND
)));
1345 lowerSize
= ucol_getBound(tests
[lowerRND
].key
, -1, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1346 upperSize
= ucol_getBound(tests
[upperRND
].key
, -1, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1348 for(j
= lowerRND
; j
<=upperRND
; j
++) {
1349 if(strcmp(lower
, tests
[j
].key
) > 0) {
1350 log_err("Problem with lower! j = %i (%s vs %s)\n", j
, tests
[j
].original
, tests
[lowerRND
].original
);
1352 if(strcmp(upper
, tests
[j
].key
) <= 0) {
1353 log_err("Problem with upper! j = %i (%s vs %s)\n", j
, tests
[j
].original
, tests
[upperRND
].original
);
1363 for(i
= 0; i
<sizeof(test
)/sizeof(test
[0]); i
++) {
1364 buffSize
= u_unescape(test
[i
], buffer
, 512);
1365 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, sortkey
, 512);
1366 lowerSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_LOWER
, 1, lower
, 512, &status
);
1367 upperSize
= ucol_getBound(sortkey
, skSize
, UCOL_BOUND_UPPER_LONG
, 1, upper
, 512, &status
);
1368 for(j
= i
+1; j
<sizeof(test
)/sizeof(test
[0]); j
++) {
1369 buffSize
= u_unescape(test
[j
], buffer
, 512);
1370 skSize
= ucol_getSortKey(coll
, buffer
, buffSize
, sortkey
, 512);
1371 if(strcmp((const char *)lower
, (const char *)sortkey
) > 0) {
1372 log_err("Problem with lower! i = %i, j = %i (%s vs %s)\n", i
, j
, test
[i
], test
[j
]);
1374 if(strcmp((const char *)upper
, (const char *)sortkey
) <= 0) {
1375 log_err("Problem with upper! i = %i, j = %i (%s vs %s)\n", i
, j
, test
[i
], test
[j
]);
1381 log_data_err("Couldn't open collator\n");
1386 static void doOverrunTest(UCollator
*coll
, const UChar
*uString
, int32_t strLen
) {
1387 int32_t skLen
= 0, skLen2
= 0;
1388 uint8_t sortKey
[256];
1390 uint8_t filler
= 0xFF;
1392 skLen
= ucol_getSortKey(coll
, uString
, strLen
, NULL
, 0);
1394 for(i
= 0; i
< skLen
; i
++) {
1395 memset(sortKey
, filler
, 256);
1396 skLen2
= ucol_getSortKey(coll
, uString
, strLen
, sortKey
, i
);
1397 if(skLen
!= skLen2
) {
1398 log_err("For buffer size %i, got different sortkey length. Expected %i got %i\n", i
, skLen
, skLen2
);
1400 for(j
= i
; j
< 256; j
++) {
1401 if(sortKey
[j
] != filler
) {
1402 log_err("Something run over index %i\n", j
);
1409 /* j1865 reports that if a shorter buffer is passed to
1410 * to get sort key, a buffer overrun happens in some
1411 * cases. This test tries to check this.
1413 void TestSortKeyBufferOverrun(void) {
1414 UErrorCode status
= U_ZERO_ERROR
;
1415 const char* cString
= "A very Merry liTTle-lamB..";
1418 UCollator
*coll
= ucol_open("root", &status
);
1419 strLen
= u_unescape(cString
, uString
, 256);
1421 if(U_SUCCESS(status
)) {
1422 log_verbose("testing non ignorable\n");
1423 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
1424 doOverrunTest(coll
, uString
, strLen
);
1426 log_verbose("testing shifted\n");
1427 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &status
);
1428 doOverrunTest(coll
, uString
, strLen
);
1430 log_verbose("testing shifted quaternary\n");
1431 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &status
);
1432 doOverrunTest(coll
, uString
, strLen
);
1434 log_verbose("testing with french secondaries\n");
1435 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
1436 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
1437 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
1438 doOverrunTest(coll
, uString
, strLen
);
1444 static void TestAttribute()
1446 UErrorCode error
= U_ZERO_ERROR
;
1447 UCollator
*coll
= ucol_open(NULL
, &error
);
1449 if (U_FAILURE(error
)) {
1450 log_err("Creation of default collator failed");
1454 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_OFF
, &error
);
1455 if (ucol_getAttribute(coll
, UCOL_FRENCH_COLLATION
, &error
) != UCOL_OFF
||
1457 log_err("Setting and retrieving of the french collation failed");
1460 ucol_setAttribute(coll
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &error
);
1461 if (ucol_getAttribute(coll
, UCOL_FRENCH_COLLATION
, &error
) != UCOL_ON
||
1463 log_err("Setting and retrieving of the french collation failed");
1466 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &error
);
1467 if (ucol_getAttribute(coll
, UCOL_ALTERNATE_HANDLING
, &error
) != UCOL_SHIFTED
||
1469 log_err("Setting and retrieving of the alternate handling failed");
1472 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &error
);
1473 if (ucol_getAttribute(coll
, UCOL_ALTERNATE_HANDLING
, &error
) != UCOL_NON_IGNORABLE
||
1475 log_err("Setting and retrieving of the alternate handling failed");
1478 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, &error
);
1479 if (ucol_getAttribute(coll
, UCOL_CASE_FIRST
, &error
) != UCOL_LOWER_FIRST
||
1481 log_err("Setting and retrieving of the case first attribute failed");
1484 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, &error
);
1485 if (ucol_getAttribute(coll
, UCOL_CASE_FIRST
, &error
) != UCOL_UPPER_FIRST
||
1487 log_err("Setting and retrieving of the case first attribute failed");
1490 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_ON
, &error
);
1491 if (ucol_getAttribute(coll
, UCOL_CASE_LEVEL
, &error
) != UCOL_ON
||
1493 log_err("Setting and retrieving of the case level attribute failed");
1496 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_OFF
, &error
);
1497 if (ucol_getAttribute(coll
, UCOL_CASE_LEVEL
, &error
) != UCOL_OFF
||
1499 log_err("Setting and retrieving of the case level attribute failed");
1502 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &error
);
1503 if (ucol_getAttribute(coll
, UCOL_NORMALIZATION_MODE
, &error
) != UCOL_ON
||
1505 log_err("Setting and retrieving of the normalization on/off attribute failed");
1508 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_OFF
, &error
);
1509 if (ucol_getAttribute(coll
, UCOL_NORMALIZATION_MODE
, &error
) != UCOL_OFF
||
1511 log_err("Setting and retrieving of the normalization on/off attribute failed");
1514 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_PRIMARY
, &error
);
1515 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_PRIMARY
||
1517 log_err("Setting and retrieving of the collation strength failed");
1520 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_SECONDARY
, &error
);
1521 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_SECONDARY
||
1523 log_err("Setting and retrieving of the collation strength failed");
1526 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &error
);
1527 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_TERTIARY
||
1529 log_err("Setting and retrieving of the collation strength failed");
1532 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &error
);
1533 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_QUATERNARY
||
1535 log_err("Setting and retrieving of the collation strength failed");
1538 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &error
);
1539 if (ucol_getAttribute(coll
, UCOL_STRENGTH
, &error
) != UCOL_IDENTICAL
||
1541 log_err("Setting and retrieving of the collation strength failed");
1547 void TestGetTailoredSet() {
1550 const char *tests
[20];
1553 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1554 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1557 int32_t i
= 0, j
= 0;
1558 UErrorCode status
= U_ZERO_ERROR
;
1561 UCollator
*coll
= NULL
;
1563 int32_t buffLen
= 0;
1566 for(i
= 0; i
< sizeof(setTest
)/sizeof(setTest
[0]); i
++) {
1567 buffLen
= u_unescape(setTest
[i
].rules
, buff
, 1024);
1568 coll
= ucol_openRules(buff
, buffLen
, UCOL_DEFAULT
, UCOL_DEFAULT
, &pError
, &status
);
1569 if(U_SUCCESS(status
)) {
1570 set
= ucol_getTailoredSet(coll
, &status
);
1571 if(uset_size(set
) != setTest
[i
].testsize
) {
1572 log_err("Tailored set size different (%d) than expected (%d)\n", uset_size(set
), setTest
[i
].testsize
);
1574 for(j
= 0; j
< setTest
[i
].testsize
; j
++) {
1575 buffLen
= u_unescape(setTest
[i
].tests
[j
], buff
, 1024);
1576 if(!uset_containsString(set
, buff
, buffLen
)) {
1577 log_err("Tailored set doesn't contain %s... It should\n", setTest
[i
].tests
[j
]);
1582 log_err("Couldn't open collator with rules %s\n", setTest
[i
].rules
);
1588 static int tMemCmp(const uint8_t *first
, const uint8_t *second
) {
1589 int32_t firstLen
= (int32_t)strlen((const char *)first
);
1590 int32_t secondLen
= (int32_t)strlen((const char *)second
);
1591 return memcmp(first
, second
, uprv_min(firstLen
, secondLen
));
1593 static const char * strengthsC
[] = {
1601 void TestMergeSortKeys(void) {
1602 UErrorCode status
= U_ZERO_ERROR
;
1603 UCollator
*coll
= ucol_open("en", &status
);
1604 if(U_SUCCESS(status
)) {
1606 const char* cases
[] = {
1611 uint32_t casesSize
= sizeof(cases
)/sizeof(cases
[0]);
1612 const char* prefix
= "foo";
1613 const char* suffix
= "egg";
1614 char outBuff1
[256], outBuff2
[256];
1616 uint8_t **sortkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1617 uint8_t **mergedPrefixkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1618 uint8_t **mergedSuffixkeys
= (uint8_t **)malloc(casesSize
*sizeof(uint8_t *));
1619 uint32_t *sortKeysLen
= (uint32_t *)malloc(casesSize
*sizeof(uint32_t));
1620 uint8_t prefixKey
[256], suffixKey
[256];
1621 uint32_t prefixKeyLen
= 0, suffixKeyLen
= 0, i
= 0;
1623 uint32_t unescapedLen
= 0, l1
= 0, l2
= 0;
1624 UColAttributeValue strength
;
1626 log_verbose("ucol_mergeSortkeys test\n");
1627 log_verbose("Testing order of the test cases\n");
1628 genericLocaleStarter("en", cases
, casesSize
);
1630 for(i
= 0; i
<casesSize
; i
++) {
1631 sortkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1632 mergedPrefixkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1633 mergedSuffixkeys
[i
] = (uint8_t *)malloc(256*sizeof(uint8_t));
1636 unescapedLen
= u_unescape(prefix
, buffer
, 256);
1637 prefixKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, prefixKey
, 256);
1639 unescapedLen
= u_unescape(suffix
, buffer
, 256);
1640 suffixKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, suffixKey
, 256);
1642 log_verbose("Massaging data with prefixes and different strengths\n");
1643 strength
= UCOL_PRIMARY
;
1644 while(strength
<= UCOL_IDENTICAL
) {
1645 log_verbose("Strength %s\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1646 ucol_setAttribute(coll
, UCOL_STRENGTH
, strength
, &status
);
1647 for(i
= 0; i
<casesSize
; i
++) {
1648 unescapedLen
= u_unescape(cases
[i
], buffer
, 256);
1649 sortKeysLen
[i
] = ucol_getSortKey(coll
, buffer
, unescapedLen
, sortkeys
[i
], 256);
1650 ucol_mergeSortkeys(prefixKey
, prefixKeyLen
, sortkeys
[i
], sortKeysLen
[i
], mergedPrefixkeys
[i
], 256);
1651 ucol_mergeSortkeys(sortkeys
[i
], sortKeysLen
[i
], suffixKey
, suffixKeyLen
, mergedSuffixkeys
[i
], 256);
1653 if(tMemCmp(mergedPrefixkeys
[i
-1], mergedPrefixkeys
[i
]) >= 0) {
1654 log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1656 ucol_sortKeyToString(coll
, mergedPrefixkeys
[i
-1], outBuff1
, &l1
),
1657 ucol_sortKeyToString(coll
, mergedPrefixkeys
[i
], outBuff2
, &l2
));
1659 if(tMemCmp(mergedSuffixkeys
[i
-1], mergedSuffixkeys
[i
]) >= 0) {
1660 log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC
[strength
<=UCOL_QUATERNARY
?strength
:4]);
1662 ucol_sortKeyToString(coll
, mergedSuffixkeys
[i
-1], outBuff1
, &l1
),
1663 ucol_sortKeyToString(coll
, mergedSuffixkeys
[i
], outBuff2
, &l2
));
1667 if(strength
== UCOL_QUATERNARY
) {
1668 strength
= UCOL_IDENTICAL
;
1675 uint8_t smallBuf
[3];
1676 uint32_t reqLen
= 0;
1677 log_verbose("testing buffer overflow\n");
1678 reqLen
= ucol_mergeSortkeys(prefixKey
, prefixKeyLen
, suffixKey
, suffixKeyLen
, smallBuf
, 3);
1679 if(reqLen
!= (prefixKeyLen
+suffixKeyLen
-1)) {
1680 log_err("Wrong preflight size for merged sortkey\n");
1686 uint8_t emptyKey
[20], abcKey
[50], mergedKey
[100];
1687 int32_t emptyKeyLen
= 0, abcKeyLen
= 0, mergedKeyLen
= 0;
1689 log_verbose("testing merging with sortkeys generated for empty strings\n");
1690 emptyKeyLen
= ucol_getSortKey(coll
, &empty
, 0, emptyKey
, 20);
1691 unescapedLen
= u_unescape(cases
[0], buffer
, 256);
1692 abcKeyLen
= ucol_getSortKey(coll
, buffer
, unescapedLen
, abcKey
, 50);
1693 mergedKeyLen
= ucol_mergeSortkeys(emptyKey
, emptyKeyLen
, abcKey
, abcKeyLen
, mergedKey
, 100);
1694 if(mergedKey
[0] != 2) {
1695 log_err("Empty sortkey didn't produce a level separator\n");
1697 /* try with zeros */
1698 mergedKeyLen
= ucol_mergeSortkeys(emptyKey
, 0, abcKey
, abcKeyLen
, mergedKey
, 100);
1699 if(mergedKeyLen
!= 0 || mergedKey
[0] != 0) {
1700 log_err("Empty key didn't produce null mergedKey\n");
1702 mergedKeyLen
= ucol_mergeSortkeys(abcKey
, abcKeyLen
, emptyKey
, 0, mergedKey
, 100);
1703 if(mergedKeyLen
!= 0 || mergedKey
[0] != 0) {
1704 log_err("Empty key didn't produce null mergedKey\n");
1709 for(i
= 0; i
<casesSize
; i
++) {
1711 free(mergedPrefixkeys
[i
]);
1712 free(mergedSuffixkeys
[i
]);
1715 free(mergedPrefixkeys
);
1716 free(mergedSuffixkeys
);
1719 /* need to finish this up */
1721 log_data_err("Couldn't open collator");
1724 static void TestShortString(void)
1728 const char *expectedOutput
;
1730 UErrorCode expectedStatus
;
1731 int32_t expectedOffset
;
1732 uint32_t expectedIdentifier
;
1734 {"LDE_RDE_KPHONEBOOK_T0041_ZLATN","B2600_KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING
, 0, 0 },
1735 {"LEN_RUS_NO_AS_S4","AS_LEN_NO_S4", NULL
, U_USING_FALLBACK_WARNING
, 0, 0 },
1736 {"LDE_VPHONEBOOK_EO_SI","EO_KPHONEBOOK_LDE_SI", "de@collation=phonebook", U_ZERO_ERROR
, 0, 0 },
1737 {"LDE_Kphonebook","KPHONEBOOK_LDE", "de@collation=phonebook", U_ZERO_ERROR
, 0, 0 },
1738 {"Xqde_DE@collation=phonebookq_S3_EX","KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING
, 0, 0 },
1739 {"LFR_FO", "LFR", NULL
, U_ZERO_ERROR
, 0, 0 },
1740 {"SO_LX_AS", "", NULL
, U_ILLEGAL_ARGUMENT_ERROR
, 8, 0 },
1741 {"S3_ASS_MMM", "", NULL
, U_ILLEGAL_ARGUMENT_ERROR
, 5, 0 }
1744 int32_t i
= 0, j
= 0;
1745 UCollator
*coll
= NULL
, *fromID
= NULL
, *fromNormalized
= NULL
;
1746 UParseError parseError
;
1747 UErrorCode status
= U_ZERO_ERROR
;
1748 char fromShortBuffer
[256], fromIDBuffer
[256], fromIDRoundtrip
[256], normalizedBuffer
[256], fromNormalizedBuffer
[256];
1749 uint32_t identifier
= 0, idFromSS
= 0;
1750 const char* locale
= NULL
;
1753 for(i
= 0; i
< sizeof(testCases
)/sizeof(testCases
[0]); i
++) {
1754 status
= U_ZERO_ERROR
;
1755 if(testCases
[i
].locale
) {
1756 locale
= testCases
[i
].locale
;
1761 coll
= ucol_openFromShortString(testCases
[i
].input
, FALSE
, &parseError
, &status
);
1762 if(status
!= testCases
[i
].expectedStatus
) {
1763 log_err("Got status '%s' that is different from expected '%s' for '%s'\n",
1764 u_errorName(status
), u_errorName(testCases
[i
].expectedStatus
), testCases
[i
].input
);
1767 if(U_SUCCESS(status
)) {
1768 ucol_getShortDefinitionString(coll
, locale
, fromShortBuffer
, 256, &status
);
1770 if(strcmp(fromShortBuffer
, testCases
[i
].expectedOutput
)) {
1771 log_err("Got short string '%s' from the collator. Expected '%s' for input '%s'\n",
1772 fromShortBuffer
, testCases
[i
].expectedOutput
, testCases
[i
].input
);
1775 ucol_normalizeShortDefinitionString(testCases
[i
].input
, normalizedBuffer
, 256, &parseError
, &status
);
1776 fromNormalized
= ucol_openFromShortString(normalizedBuffer
, FALSE
, &parseError
, &status
);
1777 ucol_getShortDefinitionString(fromNormalized
, locale
, fromNormalizedBuffer
, 256, &status
);
1779 if(strcmp(fromShortBuffer
, fromNormalizedBuffer
)) {
1780 log_err("Strings obtained from collators instantiated by short string ('%s') and from normalized string ('%s') differ\n",
1781 fromShortBuffer
, fromNormalizedBuffer
);
1785 if(!ucol_equals(coll
, fromNormalized
)) {
1786 log_err("Collator from short string ('%s') differs from one obtained through a normalized version ('%s')\n",
1787 testCases
[i
].input
, normalizedBuffer
);
1790 /* test identifiers */
1791 identifier
= ucol_collatorToIdentifier(coll
, locale
, &status
);
1792 if(identifier
< UCOL_SIT_COLLATOR_NOT_ENCODABLE
) {
1793 ucol_identifierToShortString(identifier
, fromIDBuffer
, 256, FALSE
, &status
);
1794 fromID
= ucol_openFromIdentifier(identifier
, FALSE
, &status
);
1795 if(!ucol_equals(coll
, fromID
)) {
1796 log_err("Collator from short string ('%s') differs from one obtained through an identifier ('%s')\n",
1797 testCases
[i
].input
, fromIDBuffer
);
1802 /* round-trip short string - identifier */
1803 for(j
= 1; j
< 2; j
++) {
1804 idFromSS
= ucol_shortStringToIdentifier(testCases
[i
].input
, (UBool
)j
, &status
);
1805 ucol_identifierToShortString(idFromSS
, fromIDBuffer
, 256, (UBool
)j
, &status
);
1806 identifier
= ucol_shortStringToIdentifier(fromIDBuffer
, (UBool
)j
, &status
);
1807 ucol_identifierToShortString(identifier
, fromIDRoundtrip
, 256, (UBool
)j
, &status
);
1809 if(idFromSS
!= identifier
) {
1810 log_err("FD = %i, id didn't round trip. %08X vs %08X (%s)\n",
1811 j
, idFromSS
, identifier
, testCases
[i
]);
1813 if(strcmp(fromIDBuffer
, fromIDRoundtrip
)) {
1814 log_err("FD = %i, SS didn't round trip. %s vs %s (%s)\n",
1815 j
, fromIDBuffer
, fromIDRoundtrip
, testCases
[i
]);
1819 ucol_close(fromNormalized
);
1823 if(parseError
.offset
!= testCases
[i
].expectedOffset
) {
1824 log_err("Got parse error offset %i, but expected %i instead for '%s'\n",
1825 parseError
.offset
, testCases
[i
].expectedOffset
, testCases
[i
].input
);
1833 doSetsTest(const USet
*ref
, USet
*set
, const char* inSet
, const char* outSet
, UErrorCode
*status
) {
1838 bufLen
= u_unescape(inSet
, buffer
, 512);
1839 uset_applyPattern(set
, buffer
, bufLen
, 0, status
);
1840 if(U_FAILURE(*status
)) {
1841 log_err("Failure setting pattern %s\n", u_errorName(*status
));
1844 if(!uset_containsAll(ref
, set
)) {
1845 log_err("Some stuff from %s is not present in the set\n", inSet
);
1849 bufLen
= u_unescape(outSet
, buffer
, 512);
1850 uset_applyPattern(set
, buffer
, bufLen
, 0, status
);
1851 if(U_FAILURE(*status
)) {
1852 log_err("Failure setting pattern %s\n", u_errorName(*status
));
1855 if(!uset_containsNone(ref
, set
)) {
1856 log_err("Some stuff from %s is present in the set\n", outSet
);
1864 TestGetContractionsAndUnsafes(void)
1868 const char* inConts
;
1869 const char* outConts
;
1870 const char* unsafeCodeUnits
;
1871 const char* safeCodeUnits
;
1874 "[{\\u0474\\u030F}{\\u0475\\u030F}{\\u04D8\\u0308}{\\u04D9\\u0308}{\\u04E8\\u0308}{\\u04E9\\u0308}]",
1875 "[{\\u0430\\u0306}{\\u0410\\u0306}{\\u0430\\u0308}{\\u0410\\u0306}{\\u0433\\u0301}{\\u0413\\u0301}]",
1876 "[\\u0474\\u0475\\u04d8\\u04d9\\u04e8\\u04e9]",
1877 "[aAbB\\u0430\\u0410\\u0433\\u0413]"
1880 "[{\\u0474\\u030F}{\\u0475\\u030F}{\\u04D8\\u0308}{\\u04D9\\u0308}{\\u04E8\\u0308}{\\u04E9\\u0308}"
1881 "{\\u0430\\u0306}{\\u0410\\u0306}{\\u0430\\u0308}{\\u0410\\u0306}{\\u0433\\u0301}{\\u0413\\u0301}]",
1883 "[\\u0474\\u0475\\u04D8\\u04D9\\u04E8\\u04E9\\u0430\\u0410\\u0433\\u0413]",
1887 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
1889 "[\\u3099\\u309d\\u30fd]",
1890 "[\\u30a6\\u3044\\uff73]"
1893 "[{C\\u0301}{C\\u030C}{C\\u0341}{DZ\\u030C}{Dz\\u030C}{D\\u017D}{D\\u017E}{lj}{nj}]",
1894 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
1903 UErrorCode status
= U_ZERO_ERROR
;
1904 UCollator
*coll
= NULL
;
1906 int32_t noConts
= 0;
1907 USet
*conts
= uset_open(0,0);
1908 USet
*set
= uset_open(0,0);
1909 UChar buffer
[32768];
1912 for(i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++) {
1913 log_verbose("Testing locale: %s\n", tests
[i
].locale
);
1914 coll
= ucol_open(tests
[i
].locale
, &status
);
1915 noConts
= ucol_getContractions(coll
, conts
, &status
);
1916 doSetsTest(conts
, set
, tests
[i
].inConts
, tests
[i
].outConts
, &status
);
1917 setLen
= uset_toPattern(conts
, buffer
, 32768, TRUE
, &status
);
1918 if(U_SUCCESS(status
)) {
1919 log_verbose("%i: %s\n", noConts
, aescstrdup(buffer
, setLen
));
1921 log_err("error %s. %i\n", u_errorName(status
), setLen
);
1924 noConts
= ucol_getUnsafeSet(coll
, conts
, &status
);
1925 doSetsTest(conts
, set
, tests
[i
].unsafeCodeUnits
, tests
[i
].safeCodeUnits
, &status
);
1935 #endif /* #if !UCONFIG_NO_COLLATION */