1 /********************************************************************
2 * Copyright (c) 1997-2010 International Business Machines
3 * Corporation and others. All Rights Reserved.
4 ********************************************************************/
5 /*****************************************************************************
6 *
7 * File CAPITEST.C
8 *
9 * Modification History:
10 * Name Description
11 * Madhu Katragadda Ported for C API
12 * Brian Rower Added TestOpenVsOpenRules
13 ******************************************************************************
14 *//* C API TEST For COLLATOR */
15
16 #include "unicode/utypes.h"
17
18 #if !UCONFIG_NO_COLLATION
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
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"
28 #include "cintltst.h"
29 #include "capitst.h"
30 #include "ccolltst.h"
31 #include "putilimp.h"
32 #include "cmemory.h"
33 #include "cstring.h"
34
35 static void TestAttribute(void);
36 static void TestDefault(void);
37 static void TestDefaultKeyword(void);
38 int TestBufferSize(); /* defined in "colutil.c" */
39
40
41
42
43 /* next two function is modified from "i18n/ucol.cpp" to avoid include "ucol_imp.h" */
uprv_appendByteToHexString(char * dst,uint8_t val)44 static void uprv_appendByteToHexString(char *dst, uint8_t val) {
45 uint32_t len = (uint32_t)strlen(dst);
46 sprintf(dst+len, "%02X", val);
47 }
48
ucol_sortKeyToString(const UCollator * coll,const uint8_t * sortkey,char * buffer,uint32_t * len)49 static char* U_EXPORT2 ucol_sortKeyToString(const UCollator *coll, const uint8_t *sortkey, char *buffer, uint32_t *len) {
50 int32_t strength = UCOL_PRIMARY;
51 uint32_t res_size = 0;
52 UBool doneCase = FALSE;
53
54 char *current = buffer;
55 const uint8_t *currentSk = sortkey;
56
57 UErrorCode error_code = U_ZERO_ERROR;
58
59 strcpy(current, "[");
60
61 while(strength <= UCOL_QUATERNARY && strength <= ucol_getAttribute(coll,UCOL_STRENGTH, &error_code)) {
62 if(U_FAILURE(error_code)) {
63 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code));
64 }
65 if(strength > UCOL_PRIMARY) {
66 strcat(current, " . ");
67 }
68 while(*currentSk != 0x01 && *currentSk != 0x00) { /* print a level */
69 uprv_appendByteToHexString(current, *currentSk++);
70 strcat(current, " ");
71 }
72 if(ucol_getAttribute(coll,UCOL_CASE_LEVEL, &error_code) == UCOL_ON && strength == UCOL_SECONDARY && doneCase == FALSE) {
73 doneCase = TRUE;
74 } else if(ucol_getAttribute(coll,UCOL_CASE_LEVEL, &error_code) == UCOL_OFF || doneCase == TRUE || strength != UCOL_SECONDARY) {
75 strength ++;
76 }
77 if(U_FAILURE(error_code)) {
78 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code));
79 }
80 uprv_appendByteToHexString(current, *currentSk++); /* This should print '01' */
81 if(strength == UCOL_QUATERNARY && ucol_getAttribute(coll,UCOL_ALTERNATE_HANDLING, &error_code) == UCOL_NON_IGNORABLE) {
82 break;
83 }
84 }
85
86 if(ucol_getAttribute(coll,UCOL_STRENGTH, &error_code) == UCOL_IDENTICAL) {
87 strcat(current, " . ");
88 while(*currentSk != 0) {
89 uprv_appendByteToHexString(current, *currentSk++);
90 strcat(current, " ");
91 }
92
93 uprv_appendByteToHexString(current, *currentSk++);
94 }
95 if(U_FAILURE(error_code)) {
96 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code));
97 }
98 strcat(current, "]");
99
100 if(res_size > *len) {
101 return NULL;
102 }
103
104 return buffer;
105 }
106 /* end of avoid include "ucol_imp.h" */
107
108
addCollAPITest(TestNode ** root)109 void addCollAPITest(TestNode** root)
110 {
111 /* WEIVTODO: return tests here */
112 addTest(root, &TestProperty, "tscoll/capitst/TestProperty");
113 addTest(root, &TestRuleBasedColl, "tscoll/capitst/TestRuleBasedColl");
114 addTest(root, &TestCompare, "tscoll/capitst/TestCompare");
115 addTest(root, &TestSortKey, "tscoll/capitst/TestSortKey");
116 addTest(root, &TestHashCode, "tscoll/capitst/TestHashCode");
117 addTest(root, &TestElemIter, "tscoll/capitst/TestElemIter");
118 addTest(root, &TestGetAll, "tscoll/capitst/TestGetAll");
119 /*addTest(root, &TestGetDefaultRules, "tscoll/capitst/TestGetDefaultRules");*/
120 addTest(root, &TestDecomposition, "tscoll/capitst/TestDecomposition");
121 addTest(root, &TestSafeClone, "tscoll/capitst/TestSafeClone");
122 addTest(root, &TestCloneBinary, "tscoll/capitst/TestCloneBinary");
123 addTest(root, &TestGetSetAttr, "tscoll/capitst/TestGetSetAttr");
124 addTest(root, &TestBounds, "tscoll/capitst/TestBounds");
125 addTest(root, &TestGetLocale, "tscoll/capitst/TestGetLocale");
126 addTest(root, &TestSortKeyBufferOverrun, "tscoll/capitst/TestSortKeyBufferOverrun");
127 addTest(root, &TestAttribute, "tscoll/capitst/TestAttribute");
128 addTest(root, &TestGetTailoredSet, "tscoll/capitst/TestGetTailoredSet");
129 addTest(root, &TestMergeSortKeys, "tscoll/capitst/TestMergeSortKeys");
130 addTest(root, &TestShortString, "tscoll/capitst/TestShortString");
131 /* BEGIN android-removed
132 To save space, Android does not include the collation tailoring rules.
133 We skip the tailing tests for collations. */
134 /* addTest(root, &TestGetContractionsAndUnsafes, "tscoll/capitst/TestGetContractionsAndUnsafes"); */
135 /* END android-removed */
136 addTest(root, &TestOpenBinary, "tscoll/capitst/TestOpenBinary");
137 addTest(root, &TestDefault, "tscoll/capitst/TestDefault");
138 addTest(root, &TestDefaultKeyword, "tscoll/capitst/TestDefaultKeyword");
139 /* BEGIN android-removed
140 To save space, Android does not build full collation tables and tailing rules.
141 We skip the related tests. */
142 /* addTest(root, &TestOpenVsOpenRules, "tscoll/capitst/TestOpenVsOpenRules"); */
143 /* addTest(root, &TestGetKeywordValuesForLocale, "tscoll/capitst/TestGetKeywordValuesForLocale"); */
144 /* END android-removed */
145 }
146
TestGetSetAttr(void)147 void TestGetSetAttr(void) {
148 UErrorCode status = U_ZERO_ERROR;
149 UCollator *coll = ucol_open(NULL, &status);
150 struct attrTest {
151 UColAttribute att;
152 UColAttributeValue val[5];
153 uint32_t valueSize;
154 UColAttributeValue nonValue;
155 } attrs[] = {
156 {UCOL_FRENCH_COLLATION, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},
157 {UCOL_ALTERNATE_HANDLING, {UCOL_NON_IGNORABLE, UCOL_SHIFTED}, 2, UCOL_OFF},/* attribute for handling variable elements*/
158 {UCOL_CASE_FIRST, {UCOL_OFF, UCOL_LOWER_FIRST, UCOL_UPPER_FIRST}, 3, UCOL_SHIFTED},/* who goes first, lower case or uppercase */
159 {UCOL_CASE_LEVEL, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* do we have an extra case level */
160 {UCOL_NORMALIZATION_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* attribute for normalization */
161 {UCOL_DECOMPOSITION_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},
162 {UCOL_STRENGTH, {UCOL_PRIMARY, UCOL_SECONDARY, UCOL_TERTIARY, UCOL_QUATERNARY, UCOL_IDENTICAL}, 5, UCOL_SHIFTED},/* attribute for strength */
163 {UCOL_HIRAGANA_QUATERNARY_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* when turned on, this attribute */
164 };
165 UColAttribute currAttr;
166 UColAttributeValue value;
167 uint32_t i = 0, j = 0;
168
169 if (coll == NULL) {
170 log_err_status(status, "Unable to open collator. %s\n", u_errorName(status));
171 return;
172 }
173 for(i = 0; i<sizeof(attrs)/sizeof(attrs[0]); i++) {
174 currAttr = attrs[i].att;
175 ucol_setAttribute(coll, currAttr, UCOL_DEFAULT, &status);
176 if(U_FAILURE(status)) {
177 log_err_status(status, "ucol_setAttribute with the default value returned error: %s\n", u_errorName(status));
178 break;
179 }
180 value = ucol_getAttribute(coll, currAttr, &status);
181 if(U_FAILURE(status)) {
182 log_err("ucol_getAttribute returned error: %s\n", u_errorName(status));
183 break;
184 }
185 for(j = 0; j<attrs[i].valueSize; j++) {
186 ucol_setAttribute(coll, currAttr, attrs[i].val[j], &status);
187 if(U_FAILURE(status)) {
188 log_err("ucol_setAttribute with the value %i returned error: %s\n", attrs[i].val[j], u_errorName(status));
189 break;
190 }
191 }
192 status = U_ZERO_ERROR;
193 ucol_setAttribute(coll, currAttr, attrs[i].nonValue, &status);
194 if(U_SUCCESS(status)) {
195 log_err("ucol_setAttribute with the bad value didn't return an error\n");
196 break;
197 }
198 status = U_ZERO_ERROR;
199
200 ucol_setAttribute(coll, currAttr, value, &status);
201 if(U_FAILURE(status)) {
202 log_err("ucol_setAttribute with the default valuereturned error: %s\n", u_errorName(status));
203 break;
204 }
205 }
206 status = U_ZERO_ERROR;
207 value = ucol_getAttribute(coll, UCOL_ATTRIBUTE_COUNT, &status);
208 if(U_SUCCESS(status)) {
209 log_err("ucol_getAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
210 }
211 status = U_ZERO_ERROR;
212 ucol_setAttribute(coll, UCOL_ATTRIBUTE_COUNT, UCOL_DEFAULT, &status);
213 if(U_SUCCESS(status)) {
214 log_err("ucol_setAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
215 }
216 status = U_ZERO_ERROR;
217 ucol_close(coll);
218 }
219
220
doAssert(int condition,const char * message)221 static void doAssert(int condition, const char *message)
222 {
223 if (condition==0) {
224 log_err("ERROR : %s\n", message);
225 }
226 }
227
228 #if 0
229 /* We don't have default rules, at least not in the previous sense */
230 void TestGetDefaultRules(){
231 uint32_t size=0;
232 UErrorCode status=U_ZERO_ERROR;
233 UCollator *coll=NULL;
234 int32_t len1 = 0, len2=0;
235 uint8_t *binColData = NULL;
236
237 UResourceBundle *res = NULL;
238 UResourceBundle *binColl = NULL;
239 uint8_t *binResult = NULL;
240
241
242 const UChar * defaultRulesArray=ucol_getDefaultRulesArray(&size);
243 log_verbose("Test the function ucol_getDefaultRulesArray()\n");
244
245 coll = ucol_openRules(defaultRulesArray, size, UCOL_ON, UCOL_PRIMARY, &status);
246 if(U_SUCCESS(status) && coll !=NULL) {
247 binColData = (uint8_t*)ucol_cloneRuleData(coll, &len1, &status);
248
249 }
250
251
252 status=U_ZERO_ERROR;
253 res=ures_open(NULL, "root", &status);
254 if(U_FAILURE(status)){
255 log_err("ERROR: Failed to get resource for \"root Locale\" with %s", myErrorName(status));
256 return;
257 }
258 binColl=ures_getByKey(res, "%%Collation", binColl, &status);
259 if(U_SUCCESS(status)){
260 binResult=(uint8_t*)ures_getBinary(binColl, &len2, &status);
261 if(U_FAILURE(status)){
262 log_err("ERROR: ures_getBinary() failed\n");
263 }
264 }else{
265 log_err("ERROR: ures_getByKey(locale(default), %%Collation) failed");
266 }
267
268
269 if(len1 != len2){
270 log_err("Error: ucol_getDefaultRulesArray() failed to return the correct length.\n");
271 }
272 if(memcmp(binColData, binResult, len1) != 0){
273 log_err("Error: ucol_getDefaultRulesArray() failed\n");
274 }
275
276 free(binColData);
277 ures_close(binColl);
278 ures_close(res);
279 ucol_close(coll);
280
281 }
282 #endif
283
284 /* Collator Properties
285 ucol_open, ucol_strcoll, getStrength/setStrength
286 getDecomposition/setDecomposition, getDisplayName*/
TestProperty()287 void TestProperty()
288 {
289 UCollator *col, *ruled;
290 UChar *disName;
291 int32_t len = 0;
292 UChar *source, *target;
293 int32_t tempLength;
294 UErrorCode status = U_ZERO_ERROR;
295 /*
296 * Expected version of the English collator.
297 * Currently, the major/minor version numbers change when the builder code
298 * changes,
299 * number 2 is from the tailoring data version and
300 * number 3 is the UCA version.
301 * This changes with every UCA version change, and the expected value
302 * needs to be adjusted.
303 * Same in intltest/apicoll.cpp.
304 */
305 UVersionInfo currVersionArray = {0x31, 0xC0, 0x05, 0x2A}; /* from ICU 4.4/UCA 5.2 */
306 UVersionInfo versionArray = {0, 0, 0, 0};
307 UVersionInfo versionUCAArray = {0, 0, 0, 0};
308 UVersionInfo versionUCDArray = {0, 0, 0, 0};
309
310 log_verbose("The property tests begin : \n");
311 log_verbose("Test ucol_strcoll : \n");
312 col = ucol_open("en_US", &status);
313 if (U_FAILURE(status)) {
314 log_err_status(status, "Default Collator creation failed.: %s\n", myErrorName(status));
315 return;
316 }
317
318 ucol_getVersion(col, versionArray);
319 /* Check for a version greater than some value rather than equality
320 * so that we need not update the expected version each time. */
321 if (uprv_memcmp(versionArray, currVersionArray, 4)<0) {
322 log_err("Testing ucol_getVersion() - unexpected result: %02x.%02x.%02x.%02x\n",
323 versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
324 } else {
325 log_verbose("ucol_getVersion() result: %02x.%02x.%02x.%02x\n",
326 versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
327 }
328
329 /* Assume that the UCD and UCA versions are the same,
330 * rather than hardcoding (and updating each time) a particular UCA version. */
331 u_getUnicodeVersion(versionUCDArray);
332 ucol_getUCAVersion(col, versionUCAArray);
333 if (0!=uprv_memcmp(versionUCAArray, versionUCDArray, 4)) {
334 log_err("Testing ucol_getUCAVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
335 versionUCAArray[0], versionUCAArray[1], versionUCAArray[2], versionUCAArray[3]);
336 }
337
338 source=(UChar*)malloc(sizeof(UChar) * 12);
339 target=(UChar*)malloc(sizeof(UChar) * 12);
340
341
342 u_uastrcpy(source, "ab");
343 u_uastrcpy(target, "abc");
344
345 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS), "ab < abc comparison failed");
346
347 u_uastrcpy(source, "ab");
348 u_uastrcpy(target, "AB");
349
350 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS), "ab < AB comparison failed");
351 /* u_uastrcpy(source, "black-bird");
352 u_uastrcpy(target, "blackbird"); */
353 u_uastrcpy(target, "black-bird");
354 u_uastrcpy(source, "blackbird");
355
356 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_GREATER),
357 "black-bird > blackbird comparison failed");
358 u_uastrcpy(source, "black bird");
359 u_uastrcpy(target, "black-bird");
360 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS),
361 "black bird < black-bird comparison failed");
362 u_uastrcpy(source, "Hello");
363 u_uastrcpy(target, "hello");
364
365 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_GREATER),
366 "Hello > hello comparison failed");
367 free(source);
368 free(target);
369 log_verbose("Test ucol_strcoll ends.\n");
370
371 log_verbose("testing ucol_getStrength() method ...\n");
372 doAssert( (ucol_getStrength(col) == UCOL_TERTIARY), "collation object has the wrong strength");
373 doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
374
375 log_verbose("testing ucol_setStrength() method ...\n");
376 ucol_setStrength(col, UCOL_SECONDARY);
377 doAssert( (ucol_getStrength(col) != UCOL_TERTIARY), "collation object's strength is secondary difference");
378 doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
379 doAssert( (ucol_getStrength(col) == UCOL_SECONDARY), "collation object has the wrong strength");
380
381
382 log_verbose("Get display name for the default collation in German : \n");
383
384 len=ucol_getDisplayName("en_US", "de_DE", NULL, 0, &status);
385 if(status==U_BUFFER_OVERFLOW_ERROR){
386 status=U_ZERO_ERROR;
387 disName=(UChar*)malloc(sizeof(UChar) * (len+1));
388 ucol_getDisplayName("en_US", "de_DE", disName, len+1, &status);
389 log_verbose("the display name for default collation in german: %s\n", austrdup(disName) );
390 free(disName);
391 }
392 if(U_FAILURE(status)){
393 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status));
394 return;
395 }
396 log_verbose("Default collation getDisplayName ended.\n");
397
398
399 /* BEGIN android-removed
400 To save space, Android does not include the collation tailoring rules.
401 Skip the related tests.
402 ruled = ucol_open("da_DK", &status);
403 log_verbose("ucol_getRules() testing ...\n");
404 ucol_getRules(ruled, &tempLength);
405 doAssert( tempLength != 0, "getRules() result incorrect" );
406 log_verbose("getRules tests end.\n");
407 {
408 UChar *buffer = (UChar *)malloc(200000*sizeof(UChar));
409 int32_t bufLen = 200000;
410 buffer[0] = '\0';
411 log_verbose("ucol_getRulesEx() testing ...\n");
412 tempLength = ucol_getRulesEx(col,UCOL_TAILORING_ONLY,buffer,bufLen );
413 doAssert( tempLength == 0x0a, "getRulesEx() result incorrect" );
414 log_verbose("getRules tests end.\n");
415
416 log_verbose("ucol_getRulesEx() testing ...\n");
417 tempLength=ucol_getRulesEx(col,UCOL_FULL_RULES,buffer,bufLen );
418 doAssert( tempLength != 0, "getRulesEx() result incorrect" );
419 log_verbose("getRules tests end.\n");
420 free(buffer);
421 }
422 ucol_close(ruled);
423 ucol_close(col);
424
425 END android-removed */
426
427 log_verbose("open an collator for french locale");
428 col = ucol_open("fr_FR", &status);
429 if (U_FAILURE(status)) {
430 log_err("ERROR: Creating French collation failed.: %s\n", myErrorName(status));
431 return;
432 }
433 ucol_setStrength(col, UCOL_PRIMARY);
434 log_verbose("testing ucol_getStrength() method again ...\n");
435 doAssert( (ucol_getStrength(col) != UCOL_TERTIARY), "collation object has the wrong strength");
436 doAssert( (ucol_getStrength(col) == UCOL_PRIMARY), "collation object's strength is not primary difference");
437
438 log_verbose("testing French ucol_setStrength() method ...\n");
439 ucol_setStrength(col, UCOL_TERTIARY);
440 doAssert( (ucol_getStrength(col) == UCOL_TERTIARY), "collation object's strength is not tertiary difference");
441 doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
442 doAssert( (ucol_getStrength(col) != UCOL_SECONDARY), "collation object's strength is secondary difference");
443 ucol_close(col);
444
445 log_verbose("Get display name for the french collation in english : \n");
446 len=ucol_getDisplayName("fr_FR", "en_US", NULL, 0, &status);
447 if(status==U_BUFFER_OVERFLOW_ERROR){
448 status=U_ZERO_ERROR;
449 disName=(UChar*)malloc(sizeof(UChar) * (len+1));
450 ucol_getDisplayName("fr_FR", "en_US", disName, len+1, &status);
451 log_verbose("the display name for french collation in english: %s\n", austrdup(disName) );
452 free(disName);
453 }
454 if(U_FAILURE(status)){
455 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status));
456 return;
457 }
458 log_verbose("Default collation getDisplayName ended.\n");
459
460 }
461
462 /* Test RuleBasedCollator and getRules*/
TestRuleBasedColl()463 void TestRuleBasedColl()
464 {
465 UCollator *col1, *col2, *col3, *col4;
466 UCollationElements *iter1, *iter2;
467 UChar ruleset1[60];
468 UChar ruleset2[50];
469 UChar teststr[10];
470 UChar teststr2[10];
471 const UChar *rule1, *rule2, *rule3, *rule4;
472 int32_t tempLength;
473 UErrorCode status = U_ZERO_ERROR;
474 u_uastrcpy(ruleset1, "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
475 u_uastrcpy(ruleset2, "&9 < a, A < b, B < c, C < d, D, e, E");
476
477
478 col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL,&status);
479 if (U_FAILURE(status)) {
480 log_err_status(status, "RuleBased Collator creation failed.: %s\n", myErrorName(status));
481 return;
482 }
483 else
484 log_verbose("PASS: RuleBased Collator creation passed\n");
485
486 status = U_ZERO_ERROR;
487 col2 = ucol_openRules(ruleset2, u_strlen(ruleset2), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
488 if (U_FAILURE(status)) {
489 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
490 return;
491 }
492 else
493 log_verbose("PASS: RuleBased Collator creation passed\n");
494
495
496 status = U_ZERO_ERROR;
497 col3= ucol_open(NULL, &status);
498 if (U_FAILURE(status)) {
499 log_err("Default Collator creation failed.: %s\n", myErrorName(status));
500 return;
501 }
502 else
503 log_verbose("PASS: Default Collator creation passed\n");
504
505 rule1 = ucol_getRules(col1, &tempLength);
506 rule2 = ucol_getRules(col2, &tempLength);
507 rule3 = ucol_getRules(col3, &tempLength);
508
509 doAssert((u_strcmp(rule1, rule2) != 0), "Default collator getRules failed");
510 doAssert((u_strcmp(rule2, rule3) != 0), "Default collator getRules failed");
511 doAssert((u_strcmp(rule1, rule3) != 0), "Default collator getRules failed");
512
513 col4=ucol_openRules(rule2, u_strlen(rule2), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
514 if (U_FAILURE(status)) {
515 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
516 return;
517 }
518 rule4= ucol_getRules(col4, &tempLength);
519 doAssert((u_strcmp(rule2, rule4) == 0), "Default collator getRules failed");
520
521 ucol_close(col1);
522 ucol_close(col2);
523 ucol_close(col3);
524 ucol_close(col4);
525
526 /* tests that modifier ! is always ignored */
527 u_uastrcpy(ruleset1, "!&a<b");
528 teststr[0] = 0x0e40;
529 teststr[1] = 0x0e01;
530 teststr[2] = 0x0e2d;
531 col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
532 if (U_FAILURE(status)) {
533 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
534 return;
535 }
536 col2 = ucol_open("en_US", &status);
537 if (U_FAILURE(status)) {
538 log_err("en_US Collator creation failed.: %s\n", myErrorName(status));
539 return;
540 }
541 iter1 = ucol_openElements(col1, teststr, 3, &status);
542 iter2 = ucol_openElements(col2, teststr, 3, &status);
543 if(U_FAILURE(status)) {
544 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status));
545 return;
546 }
547 while (TRUE) {
548 /* testing with en since thai has its own tailoring */
549 uint32_t ce = ucol_next(iter1, &status);
550 uint32_t ce2 = ucol_next(iter2, &status);
551 if(U_FAILURE(status)) {
552 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status));
553 return;
554 }
555 if (ce2 != ce) {
556 log_err("! modifier test failed");
557 }
558 if (ce == UCOL_NULLORDER) {
559 break;
560 }
561 }
562 ucol_closeElements(iter1);
563 ucol_closeElements(iter2);
564 ucol_close(col1);
565 ucol_close(col2);
566 /* test that we can start a rule without a & or < */
567 u_uastrcpy(ruleset1, "< z < a");
568 col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
569 if (U_FAILURE(status)) {
570 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
571 return;
572 }
573 u_uastrcpy(teststr, "z");
574 u_uastrcpy(teststr2, "a");
575 if (ucol_greaterOrEqual(col1, teststr, 1, teststr2, 1)) {
576 log_err("Rule \"z < a\" fails");
577 }
578 ucol_close(col1);
579 }
580
TestCompare()581 void TestCompare()
582 {
583 UErrorCode status = U_ZERO_ERROR;
584 UCollator *col;
585 UChar* test1;
586 UChar* test2;
587
588 log_verbose("The compare tests begin : \n");
589 status=U_ZERO_ERROR;
590 col = ucol_open("en_US", &status);
591 if(U_FAILURE(status)) {
592 log_err_status(status, "ucal_open() collation creation failed.: %s\n", myErrorName(status));
593 return;
594 }
595 test1=(UChar*)malloc(sizeof(UChar) * 6);
596 test2=(UChar*)malloc(sizeof(UChar) * 6);
597 u_uastrcpy(test1, "Abcda");
598 u_uastrcpy(test2, "abcda");
599
600 log_verbose("Use tertiary comparison level testing ....\n");
601
602 doAssert( (!ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" != \"abcda\" ");
603 doAssert( (ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\" ");
604 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\"");
605
606 ucol_setStrength(col, UCOL_SECONDARY);
607 log_verbose("Use secondary comparison level testing ....\n");
608
609 doAssert( (ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\"");
610 doAssert( (!ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
611 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\"");
612
613 ucol_setStrength(col, UCOL_PRIMARY);
614 log_verbose("Use primary comparison level testing ....\n");
615
616 doAssert( (ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
617 doAssert( (!ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
618 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
619
620
621 log_verbose("The compare tests end.\n");
622 ucol_close(col);
623 free(test1);
624 free(test2);
625
626 }
627 /*
628 ---------------------------------------------
629 tests decomposition setting
630 */
TestDecomposition()631 void TestDecomposition() {
632 UErrorCode status = U_ZERO_ERROR;
633 UCollator *en_US, *el_GR, *vi_VN;
634 en_US = ucol_open("en_US", &status);
635 el_GR = ucol_open("el_GR", &status);
636 vi_VN = ucol_open("vi_VN", &status);
637
638 if (U_FAILURE(status)) {
639 log_err_status(status, "ERROR: collation creation failed.: %s\n", myErrorName(status));
640 return;
641 }
642
643 if (ucol_getAttribute(vi_VN, UCOL_NORMALIZATION_MODE, &status) != UCOL_ON ||
644 U_FAILURE(status))
645 {
646 log_err("ERROR: vi_VN collation did not have cannonical decomposition for normalization!\n");
647 }
648
649 status = U_ZERO_ERROR;
650 if (ucol_getAttribute(el_GR, UCOL_NORMALIZATION_MODE, &status) != UCOL_ON ||
651 U_FAILURE(status))
652 {
653 log_err("ERROR: el_GR collation did not have cannonical decomposition for normalization!\n");
654 }
655
656 status = U_ZERO_ERROR;
657 if (ucol_getAttribute(en_US, UCOL_NORMALIZATION_MODE, &status) != UCOL_OFF ||
658 U_FAILURE(status))
659 {
660 log_err("ERROR: en_US collation had cannonical decomposition for normalization!\n");
661 }
662
663 ucol_close(en_US);
664 ucol_close(el_GR);
665 ucol_close(vi_VN);
666 }
667
668 #define CLONETEST_COLLATOR_COUNT 4
669
TestSafeClone()670 void TestSafeClone() {
671 UChar test1[6];
672 UChar test2[6];
673 static const UChar umlautUStr[] = {0x00DC, 0};
674 static const UChar oeStr[] = {0x0055, 0x0045, 0};
675 UCollator * someCollators [CLONETEST_COLLATOR_COUNT];
676 UCollator * someClonedCollators [CLONETEST_COLLATOR_COUNT];
677 UCollator * col;
678 UErrorCode err = U_ZERO_ERROR;
679 int8_t index = 6; /* Leave this here to test buffer alingment in memory*/
680 uint8_t buffer [CLONETEST_COLLATOR_COUNT] [U_COL_SAFECLONE_BUFFERSIZE];
681 int32_t bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
682 const char sampleRuleChars[] = "&Z < CH";
683 UChar sampleRule[sizeof(sampleRuleChars)];
684
685 if (TestBufferSize()) {
686 log_err("U_COL_SAFECLONE_BUFFERSIZE should be larger than sizeof(UCollator)\n");
687 return;
688 }
689
690 u_uastrcpy(test1, "abCda");
691 u_uastrcpy(test2, "abcda");
692 u_uastrcpy(sampleRule, sampleRuleChars);
693
694 /* one default collator & two complex ones */
695 someCollators[0] = ucol_open("en_US", &err);
696 someCollators[1] = ucol_open("ko", &err);
697 someCollators[2] = ucol_open("ja_JP", &err);
698 someCollators[3] = ucol_openRules(sampleRule, -1, UCOL_ON, UCOL_TERTIARY, NULL, &err);
699 if(U_FAILURE(err)) {
700 for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++) {
701 ucol_close(someCollators[index]);
702 }
703 log_data_err("Couldn't open one or more collators\n");
704 return;
705 }
706
707 /* Check the various error & informational states: */
708
709 /* Null status - just returns NULL */
710 if (0 != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, 0))
711 {
712 log_err("FAIL: Cloned Collator failed to deal correctly with null status\n");
713 }
714 /* error status - should return 0 & keep error the same */
715 err = U_MEMORY_ALLOCATION_ERROR;
716 if (0 != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err) || err != U_MEMORY_ALLOCATION_ERROR)
717 {
718 log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
719 }
720 err = U_ZERO_ERROR;
721
722 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
723 if (0 != ucol_safeClone(someCollators[0], buffer[0], 0, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
724 {
725 log_err("FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer\n");
726 }
727 err = U_ZERO_ERROR;
728
729 /* buffer size pointer is 0 - fill in pbufferSize with a size */
730 bufferSize = 0;
731 if (0 != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err) || U_FAILURE(err) || bufferSize <= 0)
732 {
733 log_err("FAIL: Cloned Collator failed a sizing request ('preflighting')\n");
734 }
735 /* Verify our define is large enough */
736 if (U_COL_SAFECLONE_BUFFERSIZE < bufferSize)
737 {
738 log_err("FAIL: Pre-calculated buffer size is too small\n");
739 }
740 /* Verify we can use this run-time calculated size */
741 if (0 == (col = ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err)) || U_FAILURE(err))
742 {
743 log_err("FAIL: Collator can't be cloned with run-time size\n");
744 }
745 if (col) ucol_close(col);
746 /* size one byte too small - should allocate & let us know */
747 --bufferSize;
748 if (0 == (col = ucol_safeClone(someCollators[0], 0, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
749 {
750 log_err("FAIL: Cloned Collator failed to deal correctly with too-small buffer size\n");
751 }
752 if (col) ucol_close(col);
753 err = U_ZERO_ERROR;
754 bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
755
756
757 /* Null buffer pointer - return Collator & set error to U_SAFECLONE_ALLOCATED_ERROR */
758 if (0 == (col = ucol_safeClone(someCollators[0], 0, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
759 {
760 log_err("FAIL: Cloned Collator failed to deal correctly with null buffer pointer\n");
761 }
762 if (col) ucol_close(col);
763 err = U_ZERO_ERROR;
764
765 /* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
766 if (0 != ucol_safeClone(0, buffer[0], &bufferSize, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
767 {
768 log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
769 }
770
771 err = U_ZERO_ERROR;
772
773 /* Test that a cloned collator doesn't accidentally use UCA. */
774 col=ucol_open("de@collation=phonebook", &err);
775 bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
776 someClonedCollators[0] = ucol_safeClone(col, buffer[0], &bufferSize, &err);
777 doAssert( (ucol_greater(col, umlautUStr, u_strlen(umlautUStr), oeStr, u_strlen(oeStr))), "Original German phonebook collation sorts differently than expected");
778 doAssert( (ucol_greater(someClonedCollators[0], umlautUStr, u_strlen(umlautUStr), oeStr, u_strlen(oeStr))), "Cloned German phonebook collation sorts differently than expected");
779 if (!ucol_equals(someClonedCollators[0], col)) {
780 log_err("FAIL: Cloned German phonebook collator is not equal to original.\n");
781 }
782 ucol_close(col);
783 ucol_close(someClonedCollators[0]);
784
785 err = U_ZERO_ERROR;
786
787 /* change orig & clone & make sure they are independent */
788
789 for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++)
790 {
791 ucol_setStrength(someCollators[index], UCOL_IDENTICAL);
792 bufferSize = 1;
793 err = U_ZERO_ERROR;
794 ucol_close(ucol_safeClone(someCollators[index], buffer[index], &bufferSize, &err));
795 if (err != U_SAFECLONE_ALLOCATED_WARNING) {
796 log_err("FAIL: collator number %d was not allocated.\n", index);
797 log_err("FAIL: status of Collator[%d] is %d (hex: %x).\n", index, err, err);
798 }
799
800 bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
801 err = U_ZERO_ERROR;
802 someClonedCollators[index] = ucol_safeClone(someCollators[index], buffer[index], &bufferSize, &err);
803 if (someClonedCollators[index] == NULL
804 || someClonedCollators[index] < (UCollator *)buffer[index]
805 || someClonedCollators[index] > (UCollator *)(buffer[index]+(U_COL_SAFECLONE_BUFFERSIZE-1)))
806 {
807 log_err("FAIL: Cloned collator didn't use provided buffer.\n");
808 return;
809 }
810 if (!ucol_equals(someClonedCollators[index], someCollators[index])) {
811 log_err("FAIL: Cloned collator is not equal to original at index = %d.\n", index);
812 }
813
814 /* Check the usability */
815 ucol_setStrength(someCollators[index], UCOL_PRIMARY);
816 ucol_setAttribute(someCollators[index], UCOL_CASE_LEVEL, UCOL_OFF, &err);
817
818 doAssert( (ucol_equal(someCollators[index], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abcda\" == \"abCda\"");
819
820 /* Close the original to make sure that the clone is usable. */
821 ucol_close(someCollators[index]);
822
823 ucol_setStrength(someClonedCollators[index], UCOL_TERTIARY);
824 ucol_setAttribute(someClonedCollators[index], UCOL_CASE_LEVEL, UCOL_OFF, &err);
825 doAssert( (ucol_greater(someClonedCollators[index], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abCda\" >>> \"abcda\" ");
826
827 ucol_close(someClonedCollators[index]);
828 }
829 }
830
TestCloneBinary()831 void TestCloneBinary(){
832 UErrorCode err = U_ZERO_ERROR;
833 UCollator * col = ucol_open("en_US", &err);
834 UCollator * c;
835 int32_t size;
836 uint8_t * buffer;
837
838 if (U_FAILURE(err)) {
839 log_data_err("Couldn't open collator. Error: %s\n", u_errorName(err));
840 return;
841 }
842
843 size = ucol_cloneBinary(col, NULL, 0, &err);
844 if(size==0 || err!=U_BUFFER_OVERFLOW_ERROR) {
845 log_err("ucol_cloneBinary - couldn't check size. Error: %s\n", u_errorName(err));
846 return;
847 }
848 err = U_ZERO_ERROR;
849
850 buffer = (uint8_t *) malloc(size);
851 ucol_cloneBinary(col, buffer, size, &err);
852 if(U_FAILURE(err)) {
853 log_err("ucol_cloneBinary - couldn't clone.. Error: %s\n", u_errorName(err));
854 free(buffer);
855 return;
856 }
857
858 /* how to check binary result ? */
859
860 c = ucol_openBinary(buffer, size, col, &err);
861 if(U_FAILURE(err)) {
862 log_err("ucol_openBinary failed. Error: %s\n", u_errorName(err));
863 } else {
864 UChar t[] = {0x41, 0x42, 0x43, 0}; /* ABC */
865 uint8_t *k1, *k2;
866 int l1, l2;
867 l1 = ucol_getSortKey(col, t, -1, NULL,0);
868 l2 = ucol_getSortKey(c, t, -1, NULL,0);
869 k1 = (uint8_t *) malloc(sizeof(uint8_t) * l1);
870 k2 = (uint8_t *) malloc(sizeof(uint8_t) * l2);
871 ucol_getSortKey(col, t, -1, k1, l1);
872 ucol_getSortKey(col, t, -1, k2, l2);
873 if (strcmp((char *)k1,(char *)k2) != 0){
874 log_err("ucol_openBinary - new collator should equal to old one\n");
875 };
876 free(k1);
877 free(k2);
878 }
879 free(buffer);
880 ucol_close(c);
881 ucol_close(col);
882 }
883 /*
884 TestOpenVsOpenRules ensures that collators from ucol_open and ucol_openRules
885 will generate identical sort keys
886 */
TestOpenVsOpenRules()887 void TestOpenVsOpenRules(){
888
889 /* create an array of all the locales */
890 int32_t numLocales = uloc_countAvailable();
891 int32_t sizeOfStdSet;
892 uint32_t adder;
893 UChar str[41]; /* create an array of UChar of size maximum strSize + 1 */
894 USet *stdSet;
895 char* curLoc;
896 UCollator * c1;
897 UCollator * c2;
898 const UChar* rules;
899 int32_t rulesLength;
900 int32_t sortKeyLen1, sortKeyLen2;
901 uint8_t *sortKey1 = NULL, *sortKey2 = NULL;
902 ULocaleData *uld;
903 int32_t x, y, z;
904 USet *eSet;
905 int32_t eSize;
906 int strSize;
907
908 UErrorCode err = U_ZERO_ERROR;
909
910 /* create a set of standard characters that aren't very interesting...
911 and then we can find some interesting ones later */
912
913 stdSet = uset_open(0x61, 0x7A);
914 uset_addRange(stdSet, 0x41, 0x5A);
915 uset_addRange(stdSet, 0x30, 0x39);
916 sizeOfStdSet = uset_size(stdSet);
917
918 adder = 1;
919 if(getTestOption(QUICK_OPTION))
920 {
921 adder = 10;
922 }
923
924 for(x = 0; x < numLocales; x+=adder){
925 curLoc = (char *)uloc_getAvailable(x);
926 log_verbose("Processing %s\n", curLoc);
927
928 /* create a collator the normal API way */
929 c1 = ucol_open(curLoc, &err);
930 if (U_FAILURE(err)) {
931 log_err("ERROR: Normal collation creation failed with locale: %s : %s\n", curLoc, myErrorName(err));
932 return;
933 }
934
935 /* grab the rules */
936 rules = ucol_getRules(c1, &rulesLength);
937
938 /* use those rules to create a collator from rules */
939 c2 = ucol_openRules(rules, rulesLength, UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &err);
940 if (U_FAILURE(err)) {
941 log_err("ERROR: Creating collator from rules failed with locale: %s : %s\n", curLoc, myErrorName(err));
942 return;
943 }
944
945 uld = ulocdata_open(curLoc, &err);
946
947 /*now that we have some collators, we get several strings */
948
949 for(y = 0; y < 5; y++){
950
951 /* get a set of ALL the characters in this locale */
952 eSet = ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &err);
953 eSize = uset_size(eSet);
954
955 /* make a string with these characters in it */
956 strSize = (rand()%40) + 1;
957
958 for(z = 0; z < strSize; z++){
959 str[z] = uset_charAt(eSet, rand()%eSize);
960 }
961
962 /* change the set to only include 'abnormal' characters (not A-Z, a-z, 0-9 */
963 uset_removeAll(eSet, stdSet);
964 eSize = uset_size(eSet);
965
966 /* if there are some non-normal characters left, put a few into the string, just to make sure we have some */
967 if(eSize > 0){
968 str[2%strSize] = uset_charAt(eSet, rand()%eSize);
969 str[3%strSize] = uset_charAt(eSet, rand()%eSize);
970 str[5%strSize] = uset_charAt(eSet, rand()%eSize);
971 str[10%strSize] = uset_charAt(eSet, rand()%eSize);
972 str[13%strSize] = uset_charAt(eSet, rand()%eSize);
973 }
974 /* terminate the string */
975 str[strSize-1] = '\0';
976 log_verbose("String used: %S\n", str);
977
978 /* get sort keys for both of them, and check that the keys are identicle */
979 sortKeyLen1 = ucol_getSortKey(c1, str, u_strlen(str), NULL, 0);
980 sortKey1 = (uint8_t*)malloc(sizeof(uint8_t) * (sortKeyLen1 + 1));
981 /*memset(sortKey1, 0xFE, sortKeyLen1);*/
982 ucol_getSortKey(c1, str, u_strlen(str), sortKey1, sortKeyLen1 + 1);
983
984 sortKeyLen2 = ucol_getSortKey(c2, str, u_strlen(str), NULL, 0);
985 sortKey2 = (uint8_t*)malloc(sizeof(uint8_t) * (sortKeyLen2 + 1));
986 /*memset(sortKey2, 0xFE, sortKeyLen2);*/
987 ucol_getSortKey(c2, str, u_strlen(str), sortKey2, sortKeyLen2 + 1);
988
989 /* Check that the lengths are the same */
990 doAssert((sortKeyLen1 == sortKeyLen2), "Sort key lengths do not match.");
991
992 /* check that the keys are the same */
993 doAssert((memcmp(sortKey1, sortKey2, sortKeyLen1) == 0), "Keys are not equivalent");
994
995 /* clean up after each string */
996 free(sortKey1);
997 free(sortKey2);
998 uset_close(eSet);
999 }
1000 /* clean up after each locale */
1001 ulocdata_close(uld);
1002 ucol_close(c1);
1003 ucol_close(c2);
1004 }
1005 /* final clean up */
1006 uset_close(stdSet);
1007 }
1008 /*
1009 ----------------------------------------------------------------------------
1010 ctor -- Tests the getSortKey
1011 */
TestSortKey()1012 void TestSortKey()
1013 {
1014 uint8_t *sortk1 = NULL, *sortk2 = NULL, *sortk3 = NULL, *sortkEmpty = NULL;
1015 int32_t sortklen, osortklen;
1016 uint32_t toStringLen=0;
1017 UCollator *col;
1018 UChar *test1, *test2, *test3;
1019 UErrorCode status = U_ZERO_ERROR;
1020 char toStringBuffer[256], *resultP;
1021
1022
1023 uint8_t s1[] = { 0x9f, 0x00 };
1024 uint8_t s2[] = { 0x61, 0x00 };
1025 int strcmpResult;
1026
1027 strcmpResult = strcmp((const char *)s1, (const char *)s2);
1028 log_verbose("strcmp(0x9f..., 0x61...) = %d\n", strcmpResult);
1029
1030 if(strcmpResult <= 0) {
1031 log_err("ERR: expected strcmp(\"9f 00\", \"61 00\") to be >=0 (GREATER).. got %d. Calling strcmp() for sortkeys may not work! \n",
1032 strcmpResult);
1033 }
1034
1035
1036 log_verbose("testing SortKey begins...\n");
1037 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
1038 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
1039 /* col = ucol_open(NULL, &status); */
1040 col = ucol_open("en_US", &status);
1041 if (U_FAILURE(status)) {
1042 log_err_status(status, "ERROR: Default collation creation failed.: %s\n", myErrorName(status));
1043 return;
1044 }
1045
1046
1047 if(ucol_getStrength(col) != UCOL_DEFAULT_STRENGTH)
1048 {
1049 log_err("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
1050 }
1051 /* Need to use identical strength */
1052 ucol_setAttribute(col, UCOL_STRENGTH, UCOL_IDENTICAL, &status);
1053
1054 test1=(UChar*)malloc(sizeof(UChar) * 6);
1055 test2=(UChar*)malloc(sizeof(UChar) * 6);
1056 test3=(UChar*)malloc(sizeof(UChar) * 6);
1057
1058 memset(test1,0xFE, sizeof(UChar)*6);
1059 memset(test2,0xFE, sizeof(UChar)*6);
1060 memset(test3,0xFE, sizeof(UChar)*6);
1061
1062
1063 u_uastrcpy(test1, "Abcda");
1064 u_uastrcpy(test2, "abcda");
1065 u_uastrcpy(test3, "abcda");
1066
1067 log_verbose("Use tertiary comparison level testing ....\n");
1068
1069 sortklen=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
1070 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
1071 memset(sortk1,0xFE, sortklen);
1072 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortklen+1);
1073
1074 sortklen=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
1075 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
1076 memset(sortk2,0xFE, sortklen);
1077 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortklen+1);
1078
1079 osortklen = sortklen;
1080 sortklen=ucol_getSortKey(col, test2, u_strlen(test3), NULL, 0);
1081 sortk3=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
1082 memset(sortk3,0xFE, sortklen);
1083 ucol_getSortKey(col, test2, u_strlen(test2), sortk3, sortklen+1);
1084
1085 doAssert( (sortklen == osortklen), "Sortkey length should be the same (abcda, abcda)");
1086
1087 doAssert( (memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" > \"abcda\"");
1088 doAssert( (memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" < \"Abcda\"");
1089 doAssert( (memcmp(sortk2, sortk3, sortklen) == 0), "Result should be \"abcda\" == \"abcda\"");
1090
1091 resultP = ucol_sortKeyToString(col, sortk3, toStringBuffer, &toStringLen);
1092 doAssert( (resultP != 0), "sortKeyToString failed!");
1093
1094 #if 1 /* verobse log of sortkeys */
1095 {
1096 char junk2[1000];
1097 char junk3[1000];
1098 int i;
1099
1100 strcpy(junk2, "abcda[2] ");
1101 strcpy(junk3, " abcda[3] ");
1102
1103 for(i=0;i<sortklen;i++)
1104 {
1105 sprintf(junk2+strlen(junk2), "%02X ",(int)( 0xFF & sortk2[i]));
1106 sprintf(junk3+strlen(junk3), "%02X ",(int)( 0xFF & sortk3[i]));
1107 }
1108
1109 log_verbose("%s\n", junk2);
1110 log_verbose("%s\n", junk3);
1111 }
1112 #endif
1113
1114 free(sortk1);
1115 free(sortk2);
1116 free(sortk3);
1117
1118 log_verbose("Use secondary comparision level testing ...\n");
1119 ucol_setStrength(col, UCOL_SECONDARY);
1120 sortklen=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
1121 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
1122 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortklen+1);
1123 sortklen=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
1124 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
1125 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortklen+1);
1126
1127 doAssert( !(memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" == \"abcda\"");
1128 doAssert( !(memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" == \"Abcda\"");
1129 doAssert( (memcmp(sortk1, sortk2, sortklen) == 0), "Result should be \"abcda\" == \"abcda\"");
1130
1131 log_verbose("getting sortkey for an empty string\n");
1132 ucol_setAttribute(col, UCOL_STRENGTH, UCOL_TERTIARY, &status);
1133 sortklen = ucol_getSortKey(col, test1, 0, NULL, 0);
1134 sortkEmpty = (uint8_t*)malloc(sizeof(uint8_t) * sortklen+1);
1135 sortklen = ucol_getSortKey(col, test1, 0, sortkEmpty, sortklen+1);
1136 if(sortklen != 3 || sortkEmpty[0] != 1 || sortkEmpty[0] != 1 || sortkEmpty[2] != 0) {
1137 log_err("Empty string generated wrong sortkey!\n");
1138 }
1139 free(sortkEmpty);
1140
1141 log_verbose("testing passing invalid string\n");
1142 sortklen = ucol_getSortKey(col, NULL, 0, NULL, 0);
1143 if(sortklen != 0) {
1144 log_err("Invalid string didn't return sortkey size of 0\n");
1145 }
1146
1147
1148 log_verbose("testing sortkey ends...\n");
1149 ucol_close(col);
1150 free(test1);
1151 free(test2);
1152 free(test3);
1153 free(sortk1);
1154 free(sortk2);
1155
1156 }
TestHashCode()1157 void TestHashCode()
1158 {
1159 uint8_t *sortk1, *sortk2, *sortk3;
1160 int32_t sortk1len, sortk2len, sortk3len;
1161 UCollator *col;
1162 UChar *test1, *test2, *test3;
1163 UErrorCode status = U_ZERO_ERROR;
1164 log_verbose("testing getHashCode begins...\n");
1165 col = ucol_open("en_US", &status);
1166 if (U_FAILURE(status)) {
1167 log_err_status(status, "ERROR: Default collation creation failed.: %s\n", myErrorName(status));
1168 return;
1169 }
1170 test1=(UChar*)malloc(sizeof(UChar) * 6);
1171 test2=(UChar*)malloc(sizeof(UChar) * 6);
1172 test3=(UChar*)malloc(sizeof(UChar) * 6);
1173 u_uastrcpy(test1, "Abcda");
1174 u_uastrcpy(test2, "abcda");
1175 u_uastrcpy(test3, "abcda");
1176
1177 log_verbose("Use tertiary comparison level testing ....\n");
1178 sortk1len=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
1179 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortk1len+1));
1180 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortk1len+1);
1181 sortk2len=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
1182 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortk2len+1));
1183 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortk2len+1);
1184 sortk3len=ucol_getSortKey(col, test2, u_strlen(test3), NULL, 0);
1185 sortk3=(uint8_t*)malloc(sizeof(uint8_t) * (sortk3len+1));
1186 ucol_getSortKey(col, test2, u_strlen(test2), sortk3, sortk3len+1);
1187
1188
1189 log_verbose("ucol_hashCode() testing ...\n");
1190
1191 doAssert( ucol_keyHashCode(sortk1, sortk1len) != ucol_keyHashCode(sortk2, sortk2len), "Hash test1 result incorrect" );
1192 doAssert( !(ucol_keyHashCode(sortk1, sortk1len) == ucol_keyHashCode(sortk2, sortk2len)), "Hash test2 result incorrect" );
1193 doAssert( ucol_keyHashCode(sortk2, sortk2len) == ucol_keyHashCode(sortk3, sortk3len), "Hash result not equal" );
1194
1195 log_verbose("hashCode tests end.\n");
1196 ucol_close(col);
1197 free(sortk1);
1198 free(sortk2);
1199 free(sortk3);
1200 free(test1);
1201 free(test2);
1202 free(test3);
1203
1204
1205 }
1206 /*
1207 *----------------------------------------------------------------------------
1208 * Tests the UCollatorElements API.
1209 *
1210 */
TestElemIter()1211 void TestElemIter()
1212 {
1213 int32_t offset;
1214 int32_t order1, order2, order3;
1215 UChar *testString1, *testString2;
1216 UCollator *col;
1217 UCollationElements *iterator1, *iterator2, *iterator3;
1218 UErrorCode status = U_ZERO_ERROR;
1219 log_verbose("testing UCollatorElements begins...\n");
1220 col = ucol_open("en_US", &status);
1221 ucol_setAttribute(col, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
1222 if (U_FAILURE(status)) {
1223 log_err_status(status, "ERROR: Default collation creation failed.: %s\n", myErrorName(status));
1224 return;
1225 }
1226
1227 testString1=(UChar*)malloc(sizeof(UChar) * 150);
1228 testString2=(UChar*)malloc(sizeof(UChar) * 150);
1229 u_uastrcpy(testString1, "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
1230 u_uastrcpy(testString2, "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
1231
1232 log_verbose("Constructors and comparison testing....\n");
1233
1234 iterator1 = ucol_openElements(col, testString1, u_strlen(testString1), &status);
1235 if(U_FAILURE(status)) {
1236 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1237 ucol_close(col);
1238 return;
1239 }
1240 else{ log_verbose("PASS: Default collationElement iterator1 creation passed\n");}
1241
1242 iterator2 = ucol_openElements(col, testString1, u_strlen(testString1), &status);
1243 if(U_FAILURE(status)) {
1244 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1245 ucol_close(col);
1246 return;
1247 }
1248 else{ log_verbose("PASS: Default collationElement iterator2 creation passed\n");}
1249
1250 iterator3 = ucol_openElements(col, testString2, u_strlen(testString2), &status);
1251 if(U_FAILURE(status)) {
1252 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1253 ucol_close(col);
1254 return;
1255 }
1256 else{ log_verbose("PASS: Default collationElement iterator3 creation passed\n");}
1257
1258 offset=ucol_getOffset(iterator1);
1259 ucol_setOffset(iterator1, 6, &status);
1260 if (U_FAILURE(status)) {
1261 log_err("Error in setOffset for UCollatorElements iterator.: %s\n", myErrorName(status));
1262 return;
1263 }
1264 if(ucol_getOffset(iterator1)==6)
1265 log_verbose("setOffset and getOffset working fine\n");
1266 else{
1267 log_err("error in set and get Offset got %d instead of 6\n", ucol_getOffset(iterator1));
1268 }
1269
1270 ucol_setOffset(iterator1, 0, &status);
1271 order1 = ucol_next(iterator1, &status);
1272 if (U_FAILURE(status)) {
1273 log_err("Somehow ran out of memory stepping through the iterator1.: %s\n", myErrorName(status));
1274 return;
1275 }
1276 order2=ucol_getOffset(iterator2);
1277 doAssert((order1 != order2), "The first iterator advance failed");
1278 order2 = ucol_next(iterator2, &status);
1279 if (U_FAILURE(status)) {
1280 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1281 return;
1282 }
1283 order3 = ucol_next(iterator3, &status);
1284 if (U_FAILURE(status)) {
1285 log_err("Somehow ran out of memory stepping through the iterator3.: %s\n", myErrorName(status));
1286 return;
1287 }
1288
1289 doAssert((order1 == order2), "The second iterator advance failed should be the same as first one");
1290
1291 doAssert( (ucol_primaryOrder(order1) == ucol_primaryOrder(order3)), "The primary orders should be identical");
1292 doAssert( (ucol_secondaryOrder(order1) == ucol_secondaryOrder(order3)), "The secondary orders should be identical");
1293 doAssert( (ucol_tertiaryOrder(order1) == ucol_tertiaryOrder(order3)), "The tertiary orders should be identical");
1294
1295 order1=ucol_next(iterator1, &status);
1296 if (U_FAILURE(status)) {
1297 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1298 return;
1299 }
1300 order3=ucol_next(iterator3, &status);
1301 if (U_FAILURE(status)) {
1302 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1303 return;
1304 }
1305 doAssert( (ucol_primaryOrder(order1) == ucol_primaryOrder(order3)), "The primary orders should be identical");
1306 doAssert( (ucol_tertiaryOrder(order1) != ucol_tertiaryOrder(order3)), "The tertiary orders should be different");
1307
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));
1311 return;
1312 }
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));
1316 return;
1317 }
1318 /* this here, my friends, is either pure lunacy or something so obsolete that even it's mother
1319 * doesn't care about it. Essentialy, this test complains if secondary values for 'I' and '_'
1320 * are the same. According to the UCA, this is not true. Therefore, remove the test.
1321 * Besides, if primary strengths for two code points are different, it doesn't matter one bit
1322 * what is the relation between secondary or any other strengths.
1323 * killed by weiv 06/11/2002.
1324 */
1325 /*
1326 doAssert( ((order1 & UCOL_SECONDARYMASK) != (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be different");
1327 */
1328 doAssert( (order1 != UCOL_NULLORDER), "Unexpected end of iterator reached");
1329
1330 free(testString1);
1331 free(testString2);
1332 ucol_closeElements(iterator1);
1333 ucol_closeElements(iterator2);
1334 ucol_closeElements(iterator3);
1335 ucol_close(col);
1336
1337 log_verbose("testing CollationElementIterator ends...\n");
1338 }
1339
TestGetLocale()1340 void TestGetLocale() {
1341 UErrorCode status = U_ZERO_ERROR;
1342 const char *rules = "&a<x<y<z";
1343 UChar rlz[256] = {0};
1344 uint32_t rlzLen = u_unescape(rules, rlz, 256);
1345
1346 UCollator *coll = NULL;
1347 const char *locale = NULL;
1348
1349 int32_t i = 0;
1350
1351 /* Now that the collation tree is separate, actual==valid at all times. [alan] */
1352 static const struct {
1353 const char* requestedLocale;
1354 const char* validLocale;
1355 const char* actualLocale;
1356 } testStruct[] = {
1357 { "sr_RS", "sr_Cyrl_RS", "sr" },
1358 { "sh_YU", "sr_Latn_RS", "hr" }, /* this used to be sh, but now sh collation aliases hr */
1359 { "en_BE_FOO", "en_BE", "root" },
1360 { "de_DE_NONEXISTANT", "de_DE", "de" }
1361 };
1362
1363 /* test opening collators for different locales */
1364 for(i = 0; i<sizeof(testStruct)/sizeof(testStruct[0]); i++) {
1365 status = U_ZERO_ERROR;
1366 coll = ucol_open(testStruct[i].requestedLocale, &status);
1367 if(U_FAILURE(status)) {
1368 log_err_status(status, "Failed to open collator for %s with %s\n", testStruct[i].requestedLocale, u_errorName(status));
1369 ucol_close(coll);
1370 continue;
1371 }
1372 locale = ucol_getLocaleByType(coll, ULOC_REQUESTED_LOCALE, &status);
1373 if(strcmp(locale, testStruct[i].requestedLocale) != 0) {
1374 log_err("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].requestedLocale, locale);
1375 }
1376 locale = ucol_getLocaleByType(coll, ULOC_VALID_LOCALE, &status);
1377 if(strcmp(locale, testStruct[i].validLocale) != 0) {
1378 log_err("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].validLocale, locale);
1379 }
1380 locale = ucol_getLocaleByType(coll, ULOC_ACTUAL_LOCALE, &status);
1381 if(strcmp(locale, testStruct[i].actualLocale) != 0) {
1382 log_err("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].actualLocale, locale);
1383 }
1384 ucol_close(coll);
1385 }
1386
1387 /* completely non-existant locale for collator should get a default collator */
1388 {
1389 UCollator *defaultColl = ucol_open(NULL, &status);
1390 coll = ucol_open("blahaha", &status);
1391 if(U_SUCCESS(status)) {
1392 if(strcmp(ucol_getLocaleByType(coll, ULOC_REQUESTED_LOCALE, &status), "blahaha")) {
1393 log_err("Nonexisting locale didn't preserve the requested locale\n");
1394 }
1395 if(strcmp(ucol_getLocaleByType(coll, ULOC_VALID_LOCALE, &status),
1396 ucol_getLocaleByType(defaultColl, ULOC_VALID_LOCALE, &status))) {
1397 log_err("Valid locale for nonexisting locale locale collator differs "
1398 "from valid locale for default collator\n");
1399 }
1400 if(strcmp(ucol_getLocaleByType(coll, ULOC_ACTUAL_LOCALE, &status),
1401 ucol_getLocaleByType(defaultColl, ULOC_ACTUAL_LOCALE, &status))) {
1402 log_err("Actual locale for nonexisting locale locale collator differs "
1403 "from actual locale for default collator\n");
1404 }
1405 ucol_close(coll);
1406 ucol_close(defaultColl);
1407 } else {
1408 log_data_err("Couldn't open collators\n");
1409 }
1410 }
1411
1412
1413
1414 /* collator instantiated from rules should have all three locales NULL */
1415 coll = ucol_openRules(rlz, rlzLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
1416 locale = ucol_getLocaleByType(coll, ULOC_REQUESTED_LOCALE, &status);
1417 if(locale != NULL) {
1418 log_err("For collator instantiated from rules, requested locale returned %s instead of NULL\n", locale);
1419 }
1420 locale = ucol_getLocaleByType(coll, ULOC_VALID_LOCALE, &status);
1421 if(locale != NULL) {
1422 log_err("For collator instantiated from rules, valid locale returned %s instead of NULL\n", locale);
1423 }
1424 locale = ucol_getLocaleByType(coll, ULOC_ACTUAL_LOCALE, &status);
1425 if(locale != NULL) {
1426 log_err("For collator instantiated from rules, actual locale returned %s instead of NULL\n", locale);
1427 }
1428 ucol_close(coll);
1429
1430 }
1431
1432
TestGetAll()1433 void TestGetAll()
1434 {
1435 int32_t i, count;
1436 count=ucol_countAvailable();
1437 /* use something sensible w/o hardcoding the count */
1438 if(count < 0){
1439 log_err("Error in countAvailable(), it returned %d\n", count);
1440 }
1441 else{
1442 log_verbose("PASS: countAvailable() successful, it returned %d\n", count);
1443 }
1444 for(i=0;i<count;i++)
1445 log_verbose("%s\n", ucol_getAvailable(i));
1446
1447
1448 }
1449
1450
1451 struct teststruct {
1452 const char *original;
1453 uint8_t key[256];
1454 } ;
1455
compare_teststruct(const void * string1,const void * string2)1456 static int compare_teststruct(const void *string1, const void *string2) {
1457 return(strcmp((const char *)((struct teststruct *)string1)->key, (const char *)((struct teststruct *)string2)->key));
1458 }
1459
TestBounds()1460 void TestBounds() {
1461 UErrorCode status = U_ZERO_ERROR;
1462
1463 UCollator *coll = ucol_open("sh", &status);
1464
1465 uint8_t sortkey[512], lower[512], upper[512];
1466 UChar buffer[512];
1467
1468 static const char * const test[] = {
1469 "John Smith",
1470 "JOHN SMITH",
1471 "john SMITH",
1472 "j\\u00F6hn sm\\u00EFth",
1473 "J\\u00F6hn Sm\\u00EFth",
1474 "J\\u00D6HN SM\\u00CFTH",
1475 "john smithsonian",
1476 "John Smithsonian",
1477 };
1478
1479 struct teststruct tests[] = {
1480 {"\\u010CAKI MIHALJ" } ,
1481 {"\\u010CAKI MIHALJ" } ,
1482 {"\\u010CAKI PIRO\\u0160KA" },
1483 {"\\u010CABAI ANDRIJA" } ,
1484 {"\\u010CABAI LAJO\\u0160" } ,
1485 {"\\u010CABAI MARIJA" } ,
1486 {"\\u010CABAI STEVAN" } ,
1487 {"\\u010CABAI STEVAN" } ,
1488 {"\\u010CABARKAPA BRANKO" } ,
1489 {"\\u010CABARKAPA MILENKO" } ,
1490 {"\\u010CABARKAPA MIROSLAV" } ,
1491 {"\\u010CABARKAPA SIMO" } ,
1492 {"\\u010CABARKAPA STANKO" } ,
1493 {"\\u010CABARKAPA TAMARA" } ,
1494 {"\\u010CABARKAPA TOMA\\u0160" } ,
1495 {"\\u010CABDARI\\u0106 NIKOLA" } ,
1496 {"\\u010CABDARI\\u0106 ZORICA" } ,
1497 {"\\u010CABI NANDOR" } ,
1498 {"\\u010CABOVI\\u0106 MILAN" } ,
1499 {"\\u010CABRADI AGNEZIJA" } ,
1500 {"\\u010CABRADI IVAN" } ,
1501 {"\\u010CABRADI JELENA" } ,
1502 {"\\u010CABRADI LJUBICA" } ,
1503 {"\\u010CABRADI STEVAN" } ,
1504 {"\\u010CABRDA MARTIN" } ,
1505 {"\\u010CABRILO BOGDAN" } ,
1506 {"\\u010CABRILO BRANISLAV" } ,
1507 {"\\u010CABRILO LAZAR" } ,
1508 {"\\u010CABRILO LJUBICA" } ,
1509 {"\\u010CABRILO SPASOJA" } ,
1510 {"\\u010CADE\\u0160 ZDENKA" } ,
1511 {"\\u010CADESKI BLAGOJE" } ,
1512 {"\\u010CADOVSKI VLADIMIR" } ,
1513 {"\\u010CAGLJEVI\\u0106 TOMA" } ,
1514 {"\\u010CAGOROVI\\u0106 VLADIMIR" } ,
1515 {"\\u010CAJA VANKA" } ,
1516 {"\\u010CAJI\\u0106 BOGOLJUB" } ,
1517 {"\\u010CAJI\\u0106 BORISLAV" } ,
1518 {"\\u010CAJI\\u0106 RADOSLAV" } ,
1519 {"\\u010CAK\\u0160IRAN MILADIN" } ,
1520 {"\\u010CAKAN EUGEN" } ,
1521 {"\\u010CAKAN EVGENIJE" } ,
1522 {"\\u010CAKAN IVAN" } ,
1523 {"\\u010CAKAN JULIJAN" } ,
1524 {"\\u010CAKAN MIHAJLO" } ,
1525 {"\\u010CAKAN STEVAN" } ,
1526 {"\\u010CAKAN VLADIMIR" } ,
1527 {"\\u010CAKAN VLADIMIR" } ,
1528 {"\\u010CAKAN VLADIMIR" } ,
1529 {"\\u010CAKARA ANA" } ,
1530 {"\\u010CAKAREVI\\u0106 MOMIR" } ,
1531 {"\\u010CAKAREVI\\u0106 NEDELJKO" } ,
1532 {"\\u010CAKI \\u0160ANDOR" } ,
1533 {"\\u010CAKI AMALIJA" } ,
1534 {"\\u010CAKI ANDRA\\u0160" } ,
1535 {"\\u010CAKI LADISLAV" } ,
1536 {"\\u010CAKI LAJO\\u0160" } ,
1537 {"\\u010CAKI LASLO" } ,
1538 };
1539
1540
1541
1542 int32_t i = 0, j = 0, k = 0, buffSize = 0, skSize = 0, lowerSize = 0, upperSize = 0;
1543 int32_t arraySize = sizeof(tests)/sizeof(tests[0]);
1544
1545 if(U_SUCCESS(status) && coll) {
1546 for(i = 0; i<arraySize; i++) {
1547 buffSize = u_unescape(tests[i].original, buffer, 512);
1548 skSize = ucol_getSortKey(coll, buffer, buffSize, tests[i].key, 512);
1549 }
1550
1551 qsort(tests, arraySize, sizeof(struct teststruct), compare_teststruct);
1552
1553 for(i = 0; i < arraySize-1; i++) {
1554 for(j = i+1; j < arraySize; j++) {
1555 lowerSize = ucol_getBound(tests[i].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1556 upperSize = ucol_getBound(tests[j].key, -1, UCOL_BOUND_UPPER, 1, upper, 512, &status);
1557 for(k = i; k <= j; k++) {
1558 if(strcmp((const char *)lower, (const char *)tests[k].key) > 0) {
1559 log_err("Problem with lower! j = %i (%s vs %s)\n", k, tests[k].original, tests[i].original);
1560 }
1561 if(strcmp((const char *)upper, (const char *)tests[k].key) <= 0) {
1562 log_err("Problem with upper! j = %i (%s vs %s)\n", k, tests[k].original, tests[j].original);
1563 }
1564 }
1565 }
1566 }
1567
1568
1569 #if 0
1570 for(i = 0; i < 1000; i++) {
1571 lowerRND = (rand()/(RAND_MAX/arraySize));
1572 upperRND = lowerRND + (rand()/(RAND_MAX/(arraySize-lowerRND)));
1573
1574 lowerSize = ucol_getBound(tests[lowerRND].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1575 upperSize = ucol_getBound(tests[upperRND].key, -1, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
1576
1577 for(j = lowerRND; j<=upperRND; j++) {
1578 if(strcmp(lower, tests[j].key) > 0) {
1579 log_err("Problem with lower! j = %i (%s vs %s)\n", j, tests[j].original, tests[lowerRND].original);
1580 }
1581 if(strcmp(upper, tests[j].key) <= 0) {
1582 log_err("Problem with upper! j = %i (%s vs %s)\n", j, tests[j].original, tests[upperRND].original);
1583 }
1584 }
1585 }
1586 #endif
1587
1588
1589
1590
1591
1592 for(i = 0; i<sizeof(test)/sizeof(test[0]); i++) {
1593 buffSize = u_unescape(test[i], buffer, 512);
1594 skSize = ucol_getSortKey(coll, buffer, buffSize, sortkey, 512);
1595 lowerSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1596 upperSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
1597 for(j = i+1; j<sizeof(test)/sizeof(test[0]); j++) {
1598 buffSize = u_unescape(test[j], buffer, 512);
1599 skSize = ucol_getSortKey(coll, buffer, buffSize, sortkey, 512);
1600 if(strcmp((const char *)lower, (const char *)sortkey) > 0) {
1601 log_err("Problem with lower! i = %i, j = %i (%s vs %s)\n", i, j, test[i], test[j]);
1602 }
1603 if(strcmp((const char *)upper, (const char *)sortkey) <= 0) {
1604 log_err("Problem with upper! i = %i, j = %i (%s vs %s)\n", i, j, test[i], test[j]);
1605 }
1606 }
1607 }
1608 ucol_close(coll);
1609 } else {
1610 log_data_err("Couldn't open collator\n");
1611 }
1612
1613 }
1614
doOverrunTest(UCollator * coll,const UChar * uString,int32_t strLen)1615 static void doOverrunTest(UCollator *coll, const UChar *uString, int32_t strLen) {
1616 int32_t skLen = 0, skLen2 = 0;
1617 uint8_t sortKey[256];
1618 int32_t i, j;
1619 uint8_t filler = 0xFF;
1620
1621 skLen = ucol_getSortKey(coll, uString, strLen, NULL, 0);
1622
1623 for(i = 0; i < skLen; i++) {
1624 memset(sortKey, filler, 256);
1625 skLen2 = ucol_getSortKey(coll, uString, strLen, sortKey, i);
1626 if(skLen != skLen2) {
1627 log_err("For buffer size %i, got different sortkey length. Expected %i got %i\n", i, skLen, skLen2);
1628 }
1629 for(j = i; j < 256; j++) {
1630 if(sortKey[j] != filler) {
1631 log_err("Something run over index %i\n", j);
1632 break;
1633 }
1634 }
1635 }
1636 }
1637
1638 /* j1865 reports that if a shorter buffer is passed to
1639 * to get sort key, a buffer overrun happens in some
1640 * cases. This test tries to check this.
1641 */
TestSortKeyBufferOverrun(void)1642 void TestSortKeyBufferOverrun(void) {
1643 UErrorCode status = U_ZERO_ERROR;
1644 const char* cString = "A very Merry liTTle-lamB..";
1645 UChar uString[256];
1646 int32_t strLen = 0;
1647 UCollator *coll = ucol_open("root", &status);
1648 strLen = u_unescape(cString, uString, 256);
1649
1650 if(U_SUCCESS(status)) {
1651 log_verbose("testing non ignorable\n");
1652 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
1653 doOverrunTest(coll, uString, strLen);
1654
1655 log_verbose("testing shifted\n");
1656 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
1657 doOverrunTest(coll, uString, strLen);
1658
1659 log_verbose("testing shifted quaternary\n");
1660 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
1661 doOverrunTest(coll, uString, strLen);
1662
1663 log_verbose("testing with french secondaries\n");
1664 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
1665 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &status);
1666 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
1667 doOverrunTest(coll, uString, strLen);
1668
1669 }
1670 ucol_close(coll);
1671 }
1672
TestAttribute()1673 static void TestAttribute()
1674 {
1675 UErrorCode error = U_ZERO_ERROR;
1676 UCollator *coll = ucol_open(NULL, &error);
1677
1678 if (U_FAILURE(error)) {
1679 log_err_status(error, "Creation of default collator failed\n");
1680 return;
1681 }
1682
1683 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_OFF, &error);
1684 if (ucol_getAttribute(coll, UCOL_FRENCH_COLLATION, &error) != UCOL_OFF ||
1685 U_FAILURE(error)) {
1686 log_err_status(error, "Setting and retrieving of the french collation failed\n");
1687 }
1688
1689 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_ON, &error);
1690 if (ucol_getAttribute(coll, UCOL_FRENCH_COLLATION, &error) != UCOL_ON ||
1691 U_FAILURE(error)) {
1692 log_err_status(error, "Setting and retrieving of the french collation failed\n");
1693 }
1694
1695 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &error);
1696 if (ucol_getAttribute(coll, UCOL_ALTERNATE_HANDLING, &error) != UCOL_SHIFTED ||
1697 U_FAILURE(error)) {
1698 log_err_status(error, "Setting and retrieving of the alternate handling failed\n");
1699 }
1700
1701 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &error);
1702 if (ucol_getAttribute(coll, UCOL_ALTERNATE_HANDLING, &error) != UCOL_NON_IGNORABLE ||
1703 U_FAILURE(error)) {
1704 log_err_status(error, "Setting and retrieving of the alternate handling failed\n");
1705 }
1706
1707 ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_LOWER_FIRST, &error);
1708 if (ucol_getAttribute(coll, UCOL_CASE_FIRST, &error) != UCOL_LOWER_FIRST ||
1709 U_FAILURE(error)) {
1710 log_err_status(error, "Setting and retrieving of the case first attribute failed\n");
1711 }
1712
1713 ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &error);
1714 if (ucol_getAttribute(coll, UCOL_CASE_FIRST, &error) != UCOL_UPPER_FIRST ||
1715 U_FAILURE(error)) {
1716 log_err_status(error, "Setting and retrieving of the case first attribute failed\n");
1717 }
1718
1719 ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_ON, &error);
1720 if (ucol_getAttribute(coll, UCOL_CASE_LEVEL, &error) != UCOL_ON ||
1721 U_FAILURE(error)) {
1722 log_err_status(error, "Setting and retrieving of the case level attribute failed\n");
1723 }
1724
1725 ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_OFF, &error);
1726 if (ucol_getAttribute(coll, UCOL_CASE_LEVEL, &error) != UCOL_OFF ||
1727 U_FAILURE(error)) {
1728 log_err_status(error, "Setting and retrieving of the case level attribute failed\n");
1729 }
1730
1731 ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &error);
1732 if (ucol_getAttribute(coll, UCOL_NORMALIZATION_MODE, &error) != UCOL_ON ||
1733 U_FAILURE(error)) {
1734 log_err_status(error, "Setting and retrieving of the normalization on/off attribute failed\n");
1735 }
1736
1737 ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &error);
1738 if (ucol_getAttribute(coll, UCOL_NORMALIZATION_MODE, &error) != UCOL_OFF ||
1739 U_FAILURE(error)) {
1740 log_err_status(error, "Setting and retrieving of the normalization on/off attribute failed\n");
1741 }
1742
1743 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &error);
1744 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_PRIMARY ||
1745 U_FAILURE(error)) {
1746 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
1747 }
1748
1749 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_SECONDARY, &error);
1750 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_SECONDARY ||
1751 U_FAILURE(error)) {
1752 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
1753 }
1754
1755 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &error);
1756 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_TERTIARY ||
1757 U_FAILURE(error)) {
1758 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
1759 }
1760
1761 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &error);
1762 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_QUATERNARY ||
1763 U_FAILURE(error)) {
1764 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
1765 }
1766
1767 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_IDENTICAL, &error);
1768 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_IDENTICAL ||
1769 U_FAILURE(error)) {
1770 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
1771 }
1772
1773 ucol_close(coll);
1774 }
1775
TestGetTailoredSet()1776 void TestGetTailoredSet() {
1777 struct {
1778 const char *rules;
1779 const char *tests[20];
1780 int32_t testsize;
1781 } setTest[] = {
1782 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1783 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1784 };
1785
1786 int32_t i = 0, j = 0;
1787 UErrorCode status = U_ZERO_ERROR;
1788 UParseError pError;
1789
1790 UCollator *coll = NULL;
1791 UChar buff[1024];
1792 int32_t buffLen = 0;
1793 USet *set = NULL;
1794
1795 for(i = 0; i < sizeof(setTest)/sizeof(setTest[0]); i++) {
1796 buffLen = u_unescape(setTest[i].rules, buff, 1024);
1797 coll = ucol_openRules(buff, buffLen, UCOL_DEFAULT, UCOL_DEFAULT, &pError, &status);
1798 if(U_SUCCESS(status)) {
1799 set = ucol_getTailoredSet(coll, &status);
1800 if(uset_size(set) != setTest[i].testsize) {
1801 log_err("Tailored set size different (%d) than expected (%d)\n", uset_size(set), setTest[i].testsize);
1802 }
1803 for(j = 0; j < setTest[i].testsize; j++) {
1804 buffLen = u_unescape(setTest[i].tests[j], buff, 1024);
1805 if(!uset_containsString(set, buff, buffLen)) {
1806 log_err("Tailored set doesn't contain %s... It should\n", setTest[i].tests[j]);
1807 }
1808 }
1809 uset_close(set);
1810 } else {
1811 log_err_status(status, "Couldn't open collator with rules %s\n", setTest[i].rules);
1812 }
1813 ucol_close(coll);
1814 }
1815 }
1816
tMemCmp(const uint8_t * first,const uint8_t * second)1817 static int tMemCmp(const uint8_t *first, const uint8_t *second) {
1818 int32_t firstLen = (int32_t)strlen((const char *)first);
1819 int32_t secondLen = (int32_t)strlen((const char *)second);
1820 return memcmp(first, second, uprv_min(firstLen, secondLen));
1821 }
1822 static const char * strengthsC[] = {
1823 "UCOL_PRIMARY",
1824 "UCOL_SECONDARY",
1825 "UCOL_TERTIARY",
1826 "UCOL_QUATERNARY",
1827 "UCOL_IDENTICAL"
1828 };
1829
TestMergeSortKeys(void)1830 void TestMergeSortKeys(void) {
1831 UErrorCode status = U_ZERO_ERROR;
1832 UCollator *coll = ucol_open("en", &status);
1833 if(U_SUCCESS(status)) {
1834
1835 const char* cases[] = {
1836 "abc",
1837 "abcd",
1838 "abcde"
1839 };
1840 uint32_t casesSize = sizeof(cases)/sizeof(cases[0]);
1841 const char* prefix = "foo";
1842 const char* suffix = "egg";
1843 char outBuff1[256], outBuff2[256];
1844
1845 uint8_t **sortkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1846 uint8_t **mergedPrefixkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1847 uint8_t **mergedSuffixkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1848 uint32_t *sortKeysLen = (uint32_t *)malloc(casesSize*sizeof(uint32_t));
1849 uint8_t prefixKey[256], suffixKey[256];
1850 uint32_t prefixKeyLen = 0, suffixKeyLen = 0, i = 0;
1851 UChar buffer[256];
1852 uint32_t unescapedLen = 0, l1 = 0, l2 = 0;
1853 UColAttributeValue strength;
1854
1855 log_verbose("ucol_mergeSortkeys test\n");
1856 log_verbose("Testing order of the test cases\n");
1857 genericLocaleStarter("en", cases, casesSize);
1858
1859 for(i = 0; i<casesSize; i++) {
1860 sortkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1861 mergedPrefixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1862 mergedSuffixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1863 }
1864
1865 unescapedLen = u_unescape(prefix, buffer, 256);
1866 prefixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, prefixKey, 256);
1867
1868 unescapedLen = u_unescape(suffix, buffer, 256);
1869 suffixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, suffixKey, 256);
1870
1871 log_verbose("Massaging data with prefixes and different strengths\n");
1872 strength = UCOL_PRIMARY;
1873 while(strength <= UCOL_IDENTICAL) {
1874 log_verbose("Strength %s\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1875 ucol_setAttribute(coll, UCOL_STRENGTH, strength, &status);
1876 for(i = 0; i<casesSize; i++) {
1877 unescapedLen = u_unescape(cases[i], buffer, 256);
1878 sortKeysLen[i] = ucol_getSortKey(coll, buffer, unescapedLen, sortkeys[i], 256);
1879 ucol_mergeSortkeys(prefixKey, prefixKeyLen, sortkeys[i], sortKeysLen[i], mergedPrefixkeys[i], 256);
1880 ucol_mergeSortkeys(sortkeys[i], sortKeysLen[i], suffixKey, suffixKeyLen, mergedSuffixkeys[i], 256);
1881 if(i>0) {
1882 if(tMemCmp(mergedPrefixkeys[i-1], mergedPrefixkeys[i]) >= 0) {
1883 log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1884 log_err("%s\n%s\n",
1885 ucol_sortKeyToString(coll, mergedPrefixkeys[i-1], outBuff1, &l1),
1886 ucol_sortKeyToString(coll, mergedPrefixkeys[i], outBuff2, &l2));
1887 }
1888 if(tMemCmp(mergedSuffixkeys[i-1], mergedSuffixkeys[i]) >= 0) {
1889 log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1890 log_err("%s\n%s\n",
1891 ucol_sortKeyToString(coll, mergedSuffixkeys[i-1], outBuff1, &l1),
1892 ucol_sortKeyToString(coll, mergedSuffixkeys[i], outBuff2, &l2));
1893 }
1894 }
1895 }
1896 if(strength == UCOL_QUATERNARY) {
1897 strength = UCOL_IDENTICAL;
1898 } else {
1899 strength++;
1900 }
1901 }
1902
1903 {
1904 uint8_t smallBuf[3];
1905 uint32_t reqLen = 0;
1906 log_verbose("testing buffer overflow\n");
1907 reqLen = ucol_mergeSortkeys(prefixKey, prefixKeyLen, suffixKey, suffixKeyLen, smallBuf, 3);
1908 if(reqLen != (prefixKeyLen+suffixKeyLen-1)) {
1909 log_err("Wrong preflight size for merged sortkey\n");
1910 }
1911 }
1912
1913 {
1914 UChar empty = 0;
1915 uint8_t emptyKey[20], abcKey[50], mergedKey[100];
1916 int32_t emptyKeyLen = 0, abcKeyLen = 0, mergedKeyLen = 0;
1917
1918 log_verbose("testing merging with sortkeys generated for empty strings\n");
1919 emptyKeyLen = ucol_getSortKey(coll, &empty, 0, emptyKey, 20);
1920 unescapedLen = u_unescape(cases[0], buffer, 256);
1921 abcKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, abcKey, 50);
1922 mergedKeyLen = ucol_mergeSortkeys(emptyKey, emptyKeyLen, abcKey, abcKeyLen, mergedKey, 100);
1923 if(mergedKey[0] != 2) {
1924 log_err("Empty sortkey didn't produce a level separator\n");
1925 }
1926 /* try with zeros */
1927 mergedKeyLen = ucol_mergeSortkeys(emptyKey, 0, abcKey, abcKeyLen, mergedKey, 100);
1928 if(mergedKeyLen != 0 || mergedKey[0] != 0) {
1929 log_err("Empty key didn't produce null mergedKey\n");
1930 }
1931 mergedKeyLen = ucol_mergeSortkeys(abcKey, abcKeyLen, emptyKey, 0, mergedKey, 100);
1932 if(mergedKeyLen != 0 || mergedKey[0] != 0) {
1933 log_err("Empty key didn't produce null mergedKey\n");
1934 }
1935
1936 }
1937
1938 for(i = 0; i<casesSize; i++) {
1939 free(sortkeys[i]);
1940 free(mergedPrefixkeys[i]);
1941 free(mergedSuffixkeys[i]);
1942 }
1943 free(sortkeys);
1944 free(mergedPrefixkeys);
1945 free(mergedSuffixkeys);
1946 free(sortKeysLen);
1947 ucol_close(coll);
1948 /* need to finish this up */
1949 } else {
1950 log_data_err("Couldn't open collator");
1951 }
1952 }
TestShortString(void)1953 static void TestShortString(void)
1954 {
1955 struct {
1956 const char *input;
1957 const char *expectedOutput;
1958 const char *locale;
1959 UErrorCode expectedStatus;
1960 int32_t expectedOffset;
1961 uint32_t expectedIdentifier;
1962 } testCases[] = {
1963 /*
1964 * The following expectedOutput contains a collation weight (2700 from UCA 6.0)
1965 * which is the primary weight for the T character (U+0041) in the input.
1966 * When that character gets a different weight in FractionalUCA.txt,
1967 * the expectedOutput needs to be adjusted.
1968 * That is, when we upgrade to a new UCA version or change collation
1969 * in such a way that the absolute weight for 'A' changes,
1970 * we will get a test failure here and need to adjust the test case.
1971 */
1972 {"LDE_RDE_KPHONEBOOK_T0041_ZLATN","B2700_KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING, 0, 0 },
1973
1974 {"LEN_RUS_NO_AS_S4","AS_LROOT_NO_S4", NULL, U_USING_DEFAULT_WARNING, 0, 0 },
1975 {"LDE_VPHONEBOOK_EO_SI","EO_KPHONEBOOK_LDE_SI", "de@collation=phonebook", U_ZERO_ERROR, 0, 0 },
1976 {"LDE_Kphonebook","KPHONEBOOK_LDE", "de@collation=phonebook", U_ZERO_ERROR, 0, 0 },
1977 {"Xqde_DE@collation=phonebookq_S3_EX","KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING, 0, 0 },
1978 {"LFR_FO", "FO_LROOT", NULL, U_USING_DEFAULT_WARNING, 0, 0 },
1979 {"SO_LX_AS", "", NULL, U_ILLEGAL_ARGUMENT_ERROR, 8, 0 },
1980 {"S3_ASS_MMM", "", NULL, U_ILLEGAL_ARGUMENT_ERROR, 5, 0 }
1981 };
1982
1983 int32_t i = 0;
1984 UCollator *coll = NULL, *fromNormalized = NULL;
1985 UParseError parseError;
1986 UErrorCode status = U_ZERO_ERROR;
1987 char fromShortBuffer[256], normalizedBuffer[256], fromNormalizedBuffer[256];
1988 const char* locale = NULL;
1989
1990
1991 for(i = 0; i < sizeof(testCases)/sizeof(testCases[0]); i++) {
1992 status = U_ZERO_ERROR;
1993 if(testCases[i].locale) {
1994 locale = testCases[i].locale;
1995 } else {
1996 locale = NULL;
1997 }
1998
1999 coll = ucol_openFromShortString(testCases[i].input, FALSE, &parseError, &status);
2000 if(status != testCases[i].expectedStatus) {
2001 log_err_status(status, "Got status '%s' that is different from expected '%s' for '%s'\n",
2002 u_errorName(status), u_errorName(testCases[i].expectedStatus), testCases[i].input);
2003 continue;
2004 }
2005
2006 if(U_SUCCESS(status)) {
2007 ucol_getShortDefinitionString(coll, locale, fromShortBuffer, 256, &status);
2008
2009 if(strcmp(fromShortBuffer, testCases[i].expectedOutput)) {
2010 log_err("Got short string '%s' from the collator. Expected '%s' for input '%s'\n",
2011 fromShortBuffer, testCases[i].expectedOutput, testCases[i].input);
2012 }
2013
2014 ucol_normalizeShortDefinitionString(testCases[i].input, normalizedBuffer, 256, &parseError, &status);
2015 fromNormalized = ucol_openFromShortString(normalizedBuffer, FALSE, &parseError, &status);
2016 ucol_getShortDefinitionString(fromNormalized, locale, fromNormalizedBuffer, 256, &status);
2017
2018 if(strcmp(fromShortBuffer, fromNormalizedBuffer)) {
2019 log_err("Strings obtained from collators instantiated by short string ('%s') and from normalized string ('%s') differ\n",
2020 fromShortBuffer, fromNormalizedBuffer);
2021 }
2022
2023
2024 if(!ucol_equals(coll, fromNormalized)) {
2025 log_err("Collator from short string ('%s') differs from one obtained through a normalized version ('%s')\n",
2026 testCases[i].input, normalizedBuffer);
2027 }
2028
2029 ucol_close(fromNormalized);
2030 ucol_close(coll);
2031
2032 } else {
2033 if(parseError.offset != testCases[i].expectedOffset) {
2034 log_err("Got parse error offset %i, but expected %i instead for '%s'\n",
2035 parseError.offset, testCases[i].expectedOffset, testCases[i].input);
2036 }
2037 }
2038 }
2039
2040 }
2041
2042 static void
doSetsTest(const char * locale,const USet * ref,USet * set,const char * inSet,const char * outSet,UErrorCode * status)2043 doSetsTest(const char *locale, const USet *ref, USet *set, const char* inSet, const char* outSet, UErrorCode *status) {
2044 UChar buffer[512];
2045 int32_t bufLen;
2046
2047 uset_clear(set);
2048 bufLen = u_unescape(inSet, buffer, 512);
2049 uset_applyPattern(set, buffer, bufLen, 0, status);
2050 if(U_FAILURE(*status)) {
2051 log_err("%s: Failure setting pattern %s\n", locale, u_errorName(*status));
2052 }
2053
2054 if(!uset_containsAll(ref, set)) {
2055 log_err("%s: Some stuff from %s is not present in the set\n", locale, inSet);
2056 }
2057
2058 uset_clear(set);
2059 bufLen = u_unescape(outSet, buffer, 512);
2060 uset_applyPattern(set, buffer, bufLen, 0, status);
2061 if(U_FAILURE(*status)) {
2062 log_err("%s: Failure setting pattern %s\n", locale, u_errorName(*status));
2063 }
2064
2065 if(!uset_containsNone(ref, set)) {
2066 log_err("%s: Some stuff from %s is present in the set\n", locale, outSet);
2067 }
2068 }
2069
2070
2071
2072
2073 static void
TestGetContractionsAndUnsafes(void)2074 TestGetContractionsAndUnsafes(void)
2075 {
2076 static struct {
2077 const char* locale;
2078 const char* inConts;
2079 const char* outConts;
2080 const char* inExp;
2081 const char* outExp;
2082 const char* unsafeCodeUnits;
2083 const char* safeCodeUnits;
2084 } tests[] = {
2085 { "ru",
2086 "[{\\u0418\\u0306}{\\u0438\\u0306}]",
2087 "[\\u0439\\u0457]",
2088 "[\\u00e6]",
2089 "[ae]",
2090 "[\\u0418\\u0438]",
2091 "[aAbB\\u0430\\u0410\\u0433\\u0413]"
2092 },
2093 { "uk",
2094 "[{\\u0406\\u0308}{\\u0456\\u0308}{\\u0418\\u0306}{\\u0438\\u0306}]",
2095 "[\\u0407\\u0419\\u0439\\u0457]",
2096 "[\\u00e6]",
2097 "[ae]",
2098 "[\\u0406\\u0456\\u0418\\u0438]",
2099 "[aAbBxv]",
2100 },
2101 { "sh",
2102 "[{C\\u0301}{C\\u030C}{C\\u0341}{DZ\\u030C}{Dz\\u030C}{D\\u017D}{D\\u017E}{lj}{nj}]",
2103 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
2104 "[\\u00e6]",
2105 "[a]",
2106 "[nlcdzNLCDZ]",
2107 "[jabv]"
2108 },
2109 { "ja",
2110 "[{\\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}]",
2111 "[{\\u30FD\\u3099}{\\u309D\\u3099}{\\u3053\\u3099}{\\u30B3\\u3099}{lj}{nj}]",
2112 "[\\u30FE\\u00e6]",
2113 "[a]",
2114 "[\\u3099]",
2115 "[]"
2116 }
2117 };
2118
2119
2120
2121
2122 UErrorCode status = U_ZERO_ERROR;
2123 UCollator *coll = NULL;
2124 int32_t i = 0;
2125 int32_t noConts = 0;
2126 USet *conts = uset_open(0,0);
2127 USet *exp = uset_open(0, 0);
2128 USet *set = uset_open(0,0);
2129 int32_t setBufferLen = 65536;
2130 UChar buffer[65536];
2131 int32_t setLen = 0;
2132
2133 for(i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
2134 log_verbose("Testing locale: %s\n", tests[i].locale);
2135 coll = ucol_open(tests[i].locale, &status);
2136 if (coll == NULL || U_FAILURE(status)) {
2137 log_err_status(status, "Unable to open collator for locale %s ==> %s\n", tests[i].locale, u_errorName(status));
2138 continue;
2139 }
2140 ucol_getContractionsAndExpansions(coll, conts, exp, TRUE, &status);
2141 doSetsTest(tests[i].locale, conts, set, tests[i].inConts, tests[i].outConts, &status);
2142 setLen = uset_toPattern(conts, buffer, setBufferLen, TRUE, &status);
2143 if(U_SUCCESS(status)) {
2144 /*log_verbose("Contractions %i: %s\n", uset_getItemCount(conts), aescstrdup(buffer, setLen));*/
2145 } else {
2146 log_err("error %s. %i\n", u_errorName(status), setLen);
2147 status = U_ZERO_ERROR;
2148 }
2149 doSetsTest(tests[i].locale, exp, set, tests[i].inExp, tests[i].outExp, &status);
2150 setLen = uset_toPattern(exp, buffer, setBufferLen, TRUE, &status);
2151 if(U_SUCCESS(status)) {
2152 /*log_verbose("Expansions %i: %s\n", uset_getItemCount(exp), aescstrdup(buffer, setLen));*/
2153 } else {
2154 log_err("error %s. %i\n", u_errorName(status), setLen);
2155 status = U_ZERO_ERROR;
2156 }
2157
2158 noConts = ucol_getUnsafeSet(coll, conts, &status);
2159 doSetsTest(tests[i].locale, conts, set, tests[i].unsafeCodeUnits, tests[i].safeCodeUnits, &status);
2160 setLen = uset_toPattern(conts, buffer, setBufferLen, TRUE, &status);
2161 if(U_SUCCESS(status)) {
2162 log_verbose("Unsafe %i: %s\n", uset_getItemCount(exp), aescstrdup(buffer, setLen));
2163 } else {
2164 log_err("error %s. %i\n", u_errorName(status), setLen);
2165 status = U_ZERO_ERROR;
2166 }
2167
2168 ucol_close(coll);
2169 }
2170
2171
2172 uset_close(conts);
2173 uset_close(exp);
2174 uset_close(set);
2175 }
2176
2177 static void
TestOpenBinary(void)2178 TestOpenBinary(void)
2179 {
2180 UErrorCode status = U_ZERO_ERROR;
2181 /*
2182 char rule[] = "&h < d < c < b";
2183 char *wUCA[] = { "a", "h", "d", "c", "b", "i" };
2184 char *noUCA[] = {"d", "c", "b", "a", "h", "i" };
2185 */
2186 /* we have to use Cyrillic letters because latin-1 always gets copied */
2187 const char rule[] = "&\\u0452 < \\u0434 < \\u0433 < \\u0432"; /* &dje < d < g < v */
2188 const char *wUCA[] = { "\\u0430", "\\u0452", "\\u0434", "\\u0433", "\\u0432", "\\u0435" }; /* a, dje, d, g, v, e */
2189 const char *noUCA[] = {"\\u0434", "\\u0433", "\\u0432", "\\u0430", "\\u0435", "\\u0452" }; /* d, g, v, a, e, dje */
2190
2191 UChar uRules[256];
2192 int32_t uRulesLen = u_unescape(rule, uRules, 256);
2193
2194 UCollator *coll = ucol_openRules(uRules, uRulesLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
2195 UCollator *UCA = NULL;
2196 UCollator *cloneNOUCA = NULL, *cloneWUCA = NULL;
2197
2198 uint8_t imageBuffer[32768];
2199 uint8_t *image = imageBuffer;
2200 int32_t imageBufferCapacity = 32768;
2201
2202 int32_t imageSize;
2203
2204 if((coll==NULL)||(U_FAILURE(status))) {
2205 log_data_err("could not load collators or error occured: %s\n",
2206 u_errorName(status));
2207 return;
2208 }
2209 UCA = ucol_open("root", &status);
2210 if((UCA==NULL)||(U_FAILURE(status))) {
2211 log_data_err("could not load UCA collator or error occured: %s\n",
2212 u_errorName(status));
2213 return;
2214 }
2215 imageSize = ucol_cloneBinary(coll, image, imageBufferCapacity, &status);
2216 if(U_FAILURE(status)) {
2217 image = (uint8_t *)malloc(imageSize*sizeof(uint8_t));
2218 status = U_ZERO_ERROR;
2219 imageSize = ucol_cloneBinary(coll, imageBuffer, imageSize, &status);
2220 }
2221
2222
2223 cloneWUCA = ucol_openBinary(image, imageSize, UCA, &status);
2224 cloneNOUCA = ucol_openBinary(image, imageSize, NULL, &status);
2225
2226 genericOrderingTest(coll, wUCA, sizeof(wUCA)/sizeof(wUCA[0]));
2227
2228 genericOrderingTest(cloneWUCA, wUCA, sizeof(wUCA)/sizeof(wUCA[0]));
2229 genericOrderingTest(cloneNOUCA, noUCA, sizeof(noUCA)/sizeof(noUCA[0]));
2230
2231 if(image != imageBuffer) {
2232 free(image);
2233 }
2234 ucol_close(coll);
2235 ucol_close(cloneNOUCA);
2236 ucol_close(cloneWUCA);
2237 ucol_close(UCA);
2238 }
2239
TestDefault(void)2240 static void TestDefault(void) {
2241 /* Tests for code coverage. */
2242 UErrorCode status = U_ZERO_ERROR;
2243 UCollator *coll = ucol_open("es@collation=pinyin", &status);
2244 if (coll == NULL || status == U_FILE_ACCESS_ERROR) {
2245 log_data_err("Unable to open collator es@collation=pinyin\n");
2246 return;
2247 }
2248 if (status != U_USING_DEFAULT_WARNING) {
2249 /* What do you mean that you know about using pinyin collation in Spanish!? This should be in the zh locale. */
2250 log_err("es@collation=pinyin should return U_USING_DEFAULT_WARNING, but returned %s\n", u_errorName(status));
2251 }
2252 ucol_close(coll);
2253 if (ucol_getKeywordValues("funky", &status) != NULL) {
2254 log_err("Collators should not know about the funky keyword.\n");
2255 }
2256 if (status != U_ILLEGAL_ARGUMENT_ERROR) {
2257 log_err("funky keyword didn't fail as expected %s\n", u_errorName(status));
2258 }
2259 if (ucol_getKeywordValues("collation", &status) != NULL) {
2260 log_err("ucol_getKeywordValues should not work when given a bad status.\n");
2261 }
2262 }
2263
TestDefaultKeyword(void)2264 static void TestDefaultKeyword(void) {
2265 /* Tests for code coverage. */
2266 UErrorCode status = U_ZERO_ERROR;
2267 const char *loc = "zh_TW@collation=default";
2268 UCollator *coll = ucol_open(loc, &status);
2269 if(U_FAILURE(status)) {
2270 log_info("Warning: ucol_open(%s, ...) returned %s, at least it didn't crash.\n", loc, u_errorName(status));
2271 } else if (status != U_USING_FALLBACK_WARNING) {
2272 /* Hmm, skip the following test for CLDR 1.9 data and/or ICU 4.6, no longer seems to apply */
2273 #if 0
2274 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));
2275 #endif
2276 }
2277 ucol_close(coll);
2278 }
2279
TestGetKeywordValuesForLocale(void)2280 static void TestGetKeywordValuesForLocale(void) {
2281 #define INCLUDE_UNIHAN_COLLATION 0
2282 #define PREFERRED_SIZE 16
2283 #define MAX_NUMBER_OF_KEYWORDS 8
2284 const char *PREFERRED[PREFERRED_SIZE][MAX_NUMBER_OF_KEYWORDS+1] = {
2285 { "und", "standard", "ducet", "search", NULL, NULL, NULL, NULL, NULL },
2286 { "en_US", "standard", "ducet", "search", NULL, NULL, NULL, NULL, NULL },
2287 { "en_029", "standard", "ducet", "search", NULL, NULL, NULL, NULL, NULL },
2288 { "de_DE", "standard", "phonebook", "search", "ducet", NULL, NULL, NULL, NULL },
2289 { "de_Latn_DE", "standard", "phonebook", "search", "ducet", NULL, NULL, NULL, NULL },
2290 #if INCLUDE_UNIHAN_COLLATION
2291 { "zh", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2292 { "zh_Hans", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2293 { "zh_CN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2294 { "zh_Hant", "stroke", "big5han", "gb2312han", "pinyin", "standard", "unihan", "ducet", "search" },
2295 { "zh_TW", "stroke", "big5han", "gb2312han", "pinyin", "standard", "unihan", "ducet", "search" },
2296 { "zh__PINYIN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "unihan", "ducet", "search" },
2297 #else
2298 { "zh", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL },
2299 { "zh_Hans", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL },
2300 { "zh_CN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL },
2301 { "zh_Hant", "stroke", "big5han", "gb2312han", "pinyin", "standard", "ducet", "search", NULL },
2302 { "zh_TW", "stroke", "big5han", "gb2312han", "pinyin", "standard", "ducet", "search", NULL },
2303 { "zh__PINYIN", "pinyin", "big5han", "gb2312han", "standard", "stroke", "ducet", "search", NULL },
2304 #endif
2305 { "es_ES", "standard", "search", "traditional", "ducet", NULL, NULL, NULL, NULL },
2306 { "es__TRADITIONAL","traditional", "search", "standard", "ducet", NULL, NULL, NULL, NULL },
2307 { "und@collation=phonebook", "standard", "ducet", "search", NULL, NULL, NULL, NULL, NULL },
2308 { "de_DE@collation=big5han", "standard", "phonebook", "search", "ducet", NULL, NULL, NULL, NULL },
2309 { "zzz@collation=xxx", "standard", "ducet", "search", NULL, NULL, NULL, NULL, NULL }
2310 };
2311 #if INCLUDE_UNIHAN_COLLATION
2312 const int32_t expectedLength[PREFERRED_SIZE] = { 3, 3, 3, 4, 4, 8, 8, 8, 8, 8, 8, 4, 4, 3, 4, 3 };
2313 #else
2314 const int32_t expectedLength[PREFERRED_SIZE] = { 3, 3, 3, 4, 4, 7, 7, 7, 7, 7, 7, 4, 4, 3, 4, 3 };
2315 #endif
2316
2317 UErrorCode status = U_ZERO_ERROR;
2318 UEnumeration *keywordValues = NULL;
2319 int32_t i, n, size, valueLength;
2320 const char *locale = NULL, *value = NULL;
2321 UBool errorOccurred = FALSE;
2322
2323 for (i = 0; i < PREFERRED_SIZE; i++) {
2324 locale = PREFERRED[i][0];
2325 value = NULL;
2326 valueLength = 0;
2327 size = 0;
2328
2329 keywordValues = ucol_getKeywordValuesForLocale("collation", locale, TRUE, &status);
2330 if (keywordValues == NULL || U_FAILURE(status)) {
2331 log_err_status(status, "Error getting keyword values: %s\n", u_errorName(status));
2332 break;
2333 }
2334 size = uenum_count(keywordValues, &status);
2335
2336 if (size == expectedLength[i]) {
2337 for (n = 0; n < expectedLength[i]; n++) {
2338 if ((value = uenum_next(keywordValues, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
2339 if (uprv_strcmp(value, PREFERRED[i][n+1]) != 0) {
2340 log_err("Keyword values differ: Got [%s] Expected [%s] for locale: %s\n", value, PREFERRED[i][n+1], locale);
2341 errorOccurred = TRUE;
2342 break;
2343 }
2344
2345 } else {
2346 log_err("While getting keyword value from locale: %s got this error: %s\n", locale, u_errorName(status));
2347 errorOccurred = TRUE;
2348 break;
2349 }
2350 }
2351 if (errorOccurred) {
2352 break;
2353 }
2354 } else {
2355 log_err("Number of keywords (%d) does not match expected size (%d) for locale: %s\n", size, expectedLength[i], locale);
2356 break;
2357 }
2358 uenum_close(keywordValues);
2359 keywordValues = NULL;
2360 }
2361 if (keywordValues != NULL) {
2362 uenum_close(keywordValues);
2363 }
2364 }
2365
2366
2367 #endif /* #if !UCONFIG_NO_COLLATION */
2368