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