]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/capitst.c
ICU-400.39.tar.gz
[apple/icu.git] / icuSources / test / cintltst / capitst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2008, 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" */
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
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
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
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
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*/
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, 1, 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*/
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
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 */
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
647 void TestSafeClone() {
648 UChar test1[6];
649 UChar test2[6];
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 index = 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
662 if (TestBufferSize()) {
663 log_err("U_COL_SAFECLONE_BUFFERSIZE should be larger than sizeof(UCollator)\n");
664 return;
665 }
666
667 u_uastrcpy(test1, "abCda");
668 u_uastrcpy(test2, "abcda");
669 u_uastrcpy(sampleRule, sampleRuleChars);
670
671 /* one default collator & two complex ones */
672 someCollators[0] = ucol_open("en_US", &err);
673 someCollators[1] = ucol_open("ko", &err);
674 someCollators[2] = ucol_open("ja_JP", &err);
675 someCollators[3] = ucol_openRules(sampleRule, -1, UCOL_ON, UCOL_TERTIARY, NULL, &err);
676 if(U_FAILURE(err)) {
677 for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++) {
678 ucol_close(someCollators[index]);
679 }
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 log_err("FAIL: status of Collator[%d] is %d (hex: %x).\n", index, err, err);
775 }
776
777 bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
778 err = U_ZERO_ERROR;
779 someClonedCollators[index] = ucol_safeClone(someCollators[index], buffer[index], &bufferSize, &err);
780 if (someClonedCollators[index] == NULL
781 || someClonedCollators[index] < (UCollator *)buffer[index]
782 || someClonedCollators[index] > (UCollator *)(buffer[index]+(U_COL_SAFECLONE_BUFFERSIZE-1)))
783 {
784 log_err("FAIL: Cloned collator didn't use provided buffer.\n");
785 return;
786 }
787 if (!ucol_equals(someClonedCollators[index], someCollators[index])) {
788 log_err("FAIL: Cloned collator is not equal to original at index = %d.\n", index);
789 }
790
791 /* Check the usability */
792 ucol_setStrength(someCollators[index], UCOL_PRIMARY);
793 ucol_setAttribute(someCollators[index], UCOL_CASE_LEVEL, UCOL_OFF, &err);
794
795 doAssert( (ucol_equal(someCollators[index], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abcda\" == \"abCda\"");
796
797 /* Close the original to make sure that the clone is usable. */
798 ucol_close(someCollators[index]);
799
800 ucol_setStrength(someClonedCollators[index], UCOL_TERTIARY);
801 ucol_setAttribute(someClonedCollators[index], UCOL_CASE_LEVEL, UCOL_OFF, &err);
802 doAssert( (ucol_greater(someClonedCollators[index], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abCda\" >>> \"abcda\" ");
803
804 ucol_close(someClonedCollators[index]);
805 }
806 }
807
808 void TestCloneBinary(){
809 UErrorCode err = U_ZERO_ERROR;
810 UCollator * col = ucol_open("en_US", &err);
811 UCollator * c;
812 int32_t size;
813 uint8_t * buffer;
814
815 if (U_FAILURE(err)) {
816 log_data_err("Couldn't open collator. Error: %s\n", u_errorName(err));
817 return;
818 }
819
820 size = ucol_cloneBinary(col, NULL, 0, &err);
821 if(size==0 || err!=U_BUFFER_OVERFLOW_ERROR) {
822 log_err("ucol_cloneBinary - couldn't check size. Error: %s\n", u_errorName(err));
823 return;
824 }
825 err = U_ZERO_ERROR;
826
827 buffer = (uint8_t *) malloc(size);
828 ucol_cloneBinary(col, buffer, size, &err);
829 if(U_FAILURE(err)) {
830 log_err("ucol_cloneBinary - couldn't clone.. Error: %s\n", u_errorName(err));
831 free(buffer);
832 return;
833 }
834
835 /* how to check binary result ? */
836
837 c = ucol_openBinary(buffer, size, col, &err);
838 if(U_FAILURE(err)) {
839 log_err("ucol_openBinary failed. Error: %s\n", u_errorName(err));
840 } else {
841 UChar t[] = {0x41, 0x42, 0x43, 0}; /* ABC */
842 uint8_t *k1, *k2;
843 int l1, l2;
844 l1 = ucol_getSortKey(col, t, -1, NULL,0);
845 l2 = ucol_getSortKey(c, t, -1, NULL,0);
846 k1 = (uint8_t *) malloc(sizeof(uint8_t) * l1);
847 k2 = (uint8_t *) malloc(sizeof(uint8_t) * l2);
848 ucol_getSortKey(col, t, -1, k1, l1);
849 ucol_getSortKey(col, t, -1, k2, l2);
850 if (strcmp((char *)k1,(char *)k2) != 0){
851 log_err("ucol_openBinary - new collator should equal to old one\n");
852 };
853 free(k1);
854 free(k2);
855 }
856 free(buffer);
857 ucol_close(c);
858 ucol_close(col);
859 }
860 /*
861 ----------------------------------------------------------------------------
862 ctor -- Tests the getSortKey
863 */
864 void TestSortKey()
865 {
866 uint8_t *sortk1 = NULL, *sortk2 = NULL, *sortk3 = NULL, *sortkEmpty = NULL;
867 uint8_t sortk2_compat[] = {
868 /* 3.9 key, from UCA 5.1 */
869 0x2c, 0x2e, 0x30, 0x32, 0x2c, 0x01,
870 0x09, 0x01, 0x09, 0x01, 0x2b, 0x01,
871 0x92, 0x93, 0x94, 0x95, 0x92, 0x0
872
873 /* 3.6 key, from UCA 5.0 */
874 /*
875 0x29, 0x2b, 0x2d, 0x2f, 0x29, 0x01,
876 0x09, 0x01, 0x09, 0x01, 0x28, 0x01,
877 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
878 */
879 /* 3.4 key, from UCA 4.1 */
880 /* 0x28, 0x2a, 0x2c, 0x2e, 0x28, 0x01, 0x09, 0x01, 0x09, 0x01, 0x27, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00 */
881 /* 2.6.1 key */
882 /* 0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01, 0x09, 0x01, 0x09, 0x01, 0x25, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00 */
883 /* 2.2 key */
884 /*0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01, 0x09, 0x01, 0x09, 0x01, 0x1C, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
885 /* 2.0 key */
886 /*0x19, 0x1B, 0x1D, 0x1F, 0x19, 0x01, 0x09, 0x01, 0x09, 0x01, 0x18, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
887 /* 1.8.1 key.*/
888 /*0x19, 0x1B, 0x1D, 0x1F, 0x19, 0x01, 0x0A, 0x01, 0x0A, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
889 /*this is a 1.8 sortkey */
890 /*0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00*/
891 /*this is a 1.7 sortkey */
892 /*0x02, 0x54, 0x02, 0x55, 0x02, 0x56, 0x02, 0x57, 0x02, 0x54, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00*/
893 /* this is a 1.6 sortkey */
894 /*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*/
895 };
896
897 int32_t sortklen, osortklen;
898 uint32_t toStringLen=0;
899 UCollator *col;
900 UChar *test1, *test2, *test3;
901 UErrorCode status = U_ZERO_ERROR;
902 char toStringBuffer[256], *resultP;
903
904
905 uint8_t s1[] = { 0x9f, 0x00 };
906 uint8_t s2[] = { 0x61, 0x00 };
907 int strcmpResult;
908
909 strcmpResult = strcmp((const char *)s1, (const char *)s2);
910 log_verbose("strcmp(0x9f..., 0x61...) = %d\n", strcmpResult);
911
912 if(strcmpResult <= 0) {
913 log_err("ERR: expected strcmp(\"9f 00\", \"61 00\") to be >=0 (GREATER).. got %d. Calling strcmp() for sortkeys may not work! \n",
914 strcmpResult);
915 }
916
917
918 log_verbose("testing SortKey begins...\n");
919 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
920 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
921 /* col = ucol_open(NULL, &status); */
922 col = ucol_open("en_US", &status);
923 if (U_FAILURE(status)) {
924 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status));
925 return;
926 }
927
928
929 if(ucol_getStrength(col) != UCOL_DEFAULT_STRENGTH)
930 {
931 log_err("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
932 }
933 /* Need to use identical strength */
934 ucol_setAttribute(col, UCOL_STRENGTH, UCOL_IDENTICAL, &status);
935
936 test1=(UChar*)malloc(sizeof(UChar) * 6);
937 test2=(UChar*)malloc(sizeof(UChar) * 6);
938 test3=(UChar*)malloc(sizeof(UChar) * 6);
939
940 memset(test1,0xFE, sizeof(UChar)*6);
941 memset(test2,0xFE, sizeof(UChar)*6);
942 memset(test3,0xFE, sizeof(UChar)*6);
943
944
945 u_uastrcpy(test1, "Abcda");
946 u_uastrcpy(test2, "abcda");
947 u_uastrcpy(test3, "abcda");
948
949 log_verbose("Use tertiary comparison level testing ....\n");
950
951 sortklen=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
952 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
953 memset(sortk1,0xFE, sortklen);
954 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortklen+1);
955
956 sortklen=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
957 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
958 memset(sortk2,0xFE, sortklen);
959 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortklen+1);
960
961 osortklen = sortklen;
962 sortklen=ucol_getSortKey(col, test2, u_strlen(test3), NULL, 0);
963 sortk3=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
964 memset(sortk3,0xFE, sortklen);
965 ucol_getSortKey(col, test2, u_strlen(test2), sortk3, sortklen+1);
966
967 doAssert( (sortklen == osortklen), "Sortkey length should be the same (abcda, abcda)");
968
969 doAssert( (memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" > \"abcda\"");
970 doAssert( (memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" < \"Abcda\"");
971 doAssert( (memcmp(sortk2, sortk3, sortklen) == 0), "Result should be \"abcda\" == \"abcda\"");
972
973 doAssert( (memcmp(sortk2, sortk2_compat, sortklen) == 0), "Binary format for 'abcda' sortkey different!");
974
975 resultP = ucol_sortKeyToString(col, sortk2_compat, toStringBuffer, &toStringLen);
976 doAssert( (resultP != 0), "sortKeyToString failed!");
977
978 #if 1 /* verobse log of sortkeys */
979 {
980 char junk2[1000];
981 char junk3[1000];
982 int i;
983
984 strcpy(junk2, "abcda[2] ");
985 strcpy(junk3, " abcda[3] ");
986
987 for(i=0;i<sortklen;i++)
988 {
989 sprintf(junk2+strlen(junk2), "%02X ",(int)( 0xFF & sortk2[i]));
990 sprintf(junk3+strlen(junk3), "%02X ",(int)( 0xFF & sortk3[i]));
991 }
992
993 log_verbose("%s\n", junk2);
994 log_verbose("%s\n", junk3);
995 }
996 #endif
997
998 free(sortk1);
999 free(sortk2);
1000 free(sortk3);
1001
1002 log_verbose("Use secondary comparision level testing ...\n");
1003 ucol_setStrength(col, UCOL_SECONDARY);
1004 sortklen=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
1005 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
1006 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortklen+1);
1007 sortklen=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
1008 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
1009 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortklen+1);
1010
1011 doAssert( !(memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" == \"abcda\"");
1012 doAssert( !(memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" == \"Abcda\"");
1013 doAssert( (memcmp(sortk1, sortk2, sortklen) == 0), "Result should be \"abcda\" == \"abcda\"");
1014
1015 log_verbose("getting sortkey for an empty string\n");
1016 ucol_setAttribute(col, UCOL_STRENGTH, UCOL_TERTIARY, &status);
1017 sortklen = ucol_getSortKey(col, test1, 0, NULL, 0);
1018 sortkEmpty = (uint8_t*)malloc(sizeof(uint8_t) * sortklen+1);
1019 sortklen = ucol_getSortKey(col, test1, 0, sortkEmpty, sortklen+1);
1020 if(sortklen != 3 || sortkEmpty[0] != 1 || sortkEmpty[0] != 1 || sortkEmpty[2] != 0) {
1021 log_err("Empty string generated wrong sortkey!\n");
1022 }
1023 free(sortkEmpty);
1024
1025 log_verbose("testing passing invalid string\n");
1026 sortklen = ucol_getSortKey(col, NULL, 0, NULL, 0);
1027 if(sortklen != 0) {
1028 log_err("Invalid string didn't return sortkey size of 0\n");
1029 }
1030
1031
1032 log_verbose("testing sortkey ends...\n");
1033 ucol_close(col);
1034 free(test1);
1035 free(test2);
1036 free(test3);
1037 free(sortk1);
1038 free(sortk2);
1039
1040 }
1041 void TestHashCode()
1042 {
1043 uint8_t *sortk1, *sortk2, *sortk3;
1044 int32_t sortk1len, sortk2len, sortk3len;
1045 UCollator *col;
1046 UChar *test1, *test2, *test3;
1047 UErrorCode status = U_ZERO_ERROR;
1048 log_verbose("testing getHashCode begins...\n");
1049 col = ucol_open("en_US", &status);
1050 if (U_FAILURE(status)) {
1051 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status));
1052 return;
1053 }
1054 test1=(UChar*)malloc(sizeof(UChar) * 6);
1055 test2=(UChar*)malloc(sizeof(UChar) * 6);
1056 test3=(UChar*)malloc(sizeof(UChar) * 6);
1057 u_uastrcpy(test1, "Abcda");
1058 u_uastrcpy(test2, "abcda");
1059 u_uastrcpy(test3, "abcda");
1060
1061 log_verbose("Use tertiary comparison level testing ....\n");
1062 sortk1len=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
1063 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortk1len+1));
1064 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortk1len+1);
1065 sortk2len=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
1066 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortk2len+1));
1067 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortk2len+1);
1068 sortk3len=ucol_getSortKey(col, test2, u_strlen(test3), NULL, 0);
1069 sortk3=(uint8_t*)malloc(sizeof(uint8_t) * (sortk3len+1));
1070 ucol_getSortKey(col, test2, u_strlen(test2), sortk3, sortk3len+1);
1071
1072
1073 log_verbose("ucol_hashCode() testing ...\n");
1074
1075 doAssert( ucol_keyHashCode(sortk1, sortk1len) != ucol_keyHashCode(sortk2, sortk2len), "Hash test1 result incorrect" );
1076 doAssert( !(ucol_keyHashCode(sortk1, sortk1len) == ucol_keyHashCode(sortk2, sortk2len)), "Hash test2 result incorrect" );
1077 doAssert( ucol_keyHashCode(sortk2, sortk2len) == ucol_keyHashCode(sortk3, sortk3len), "Hash result not equal" );
1078
1079 log_verbose("hashCode tests end.\n");
1080 ucol_close(col);
1081 free(sortk1);
1082 free(sortk2);
1083 free(sortk3);
1084 free(test1);
1085 free(test2);
1086 free(test3);
1087
1088
1089 }
1090 /*
1091 *----------------------------------------------------------------------------
1092 * Tests the UCollatorElements API.
1093 *
1094 */
1095 void TestElemIter()
1096 {
1097 int32_t offset;
1098 int32_t order1, order2, order3;
1099 UChar *testString1, *testString2;
1100 UCollator *col;
1101 UCollationElements *iterator1, *iterator2, *iterator3;
1102 UErrorCode status = U_ZERO_ERROR;
1103 log_verbose("testing UCollatorElements begins...\n");
1104 col = ucol_open("en_US", &status);
1105 ucol_setAttribute(col, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
1106 if (U_FAILURE(status)) {
1107 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status));
1108 return;
1109 }
1110
1111 testString1=(UChar*)malloc(sizeof(UChar) * 150);
1112 testString2=(UChar*)malloc(sizeof(UChar) * 150);
1113 u_uastrcpy(testString1, "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
1114 u_uastrcpy(testString2, "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
1115
1116 log_verbose("Constructors and comparison testing....\n");
1117
1118 iterator1 = ucol_openElements(col, testString1, u_strlen(testString1), &status);
1119 if(U_FAILURE(status)) {
1120 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1121 ucol_close(col);
1122 return;
1123 }
1124 else{ log_verbose("PASS: Default collationElement iterator1 creation passed\n");}
1125
1126 iterator2 = ucol_openElements(col, testString1, u_strlen(testString1), &status);
1127 if(U_FAILURE(status)) {
1128 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1129 ucol_close(col);
1130 return;
1131 }
1132 else{ log_verbose("PASS: Default collationElement iterator2 creation passed\n");}
1133
1134 iterator3 = ucol_openElements(col, testString2, u_strlen(testString2), &status);
1135 if(U_FAILURE(status)) {
1136 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1137 ucol_close(col);
1138 return;
1139 }
1140 else{ log_verbose("PASS: Default collationElement iterator3 creation passed\n");}
1141
1142 offset=ucol_getOffset(iterator1);
1143 ucol_setOffset(iterator1, 6, &status);
1144 if (U_FAILURE(status)) {
1145 log_err("Error in setOffset for UCollatorElements iterator.: %s\n", myErrorName(status));
1146 return;
1147 }
1148 if(ucol_getOffset(iterator1)==6)
1149 log_verbose("setOffset and getOffset working fine\n");
1150 else{
1151 log_err("error in set and get Offset got %d instead of 6\n", ucol_getOffset(iterator1));
1152 }
1153
1154 ucol_setOffset(iterator1, 0, &status);
1155 order1 = ucol_next(iterator1, &status);
1156 if (U_FAILURE(status)) {
1157 log_err("Somehow ran out of memory stepping through the iterator1.: %s\n", myErrorName(status));
1158 return;
1159 }
1160 order2=ucol_getOffset(iterator2);
1161 doAssert((order1 != order2), "The first iterator advance failed");
1162 order2 = ucol_next(iterator2, &status);
1163 if (U_FAILURE(status)) {
1164 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1165 return;
1166 }
1167 order3 = ucol_next(iterator3, &status);
1168 if (U_FAILURE(status)) {
1169 log_err("Somehow ran out of memory stepping through the iterator3.: %s\n", myErrorName(status));
1170 return;
1171 }
1172
1173 doAssert((order1 == order2), "The second iterator advance failed should be the same as first one");
1174
1175 doAssert( (ucol_primaryOrder(order1) == ucol_primaryOrder(order3)), "The primary orders should be identical");
1176 doAssert( (ucol_secondaryOrder(order1) == ucol_secondaryOrder(order3)), "The secondary orders should be identical");
1177 doAssert( (ucol_tertiaryOrder(order1) == ucol_tertiaryOrder(order3)), "The tertiary orders should be identical");
1178
1179 order1=ucol_next(iterator1, &status);
1180 if (U_FAILURE(status)) {
1181 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1182 return;
1183 }
1184 order3=ucol_next(iterator3, &status);
1185 if (U_FAILURE(status)) {
1186 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1187 return;
1188 }
1189 doAssert( (ucol_primaryOrder(order1) == ucol_primaryOrder(order3)), "The primary orders should be identical");
1190 doAssert( (ucol_tertiaryOrder(order1) != ucol_tertiaryOrder(order3)), "The tertiary orders should be different");
1191
1192 order1=ucol_next(iterator1, &status);
1193 if (U_FAILURE(status)) {
1194 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1195 return;
1196 }
1197 order3=ucol_next(iterator3, &status);
1198 if (U_FAILURE(status)) {
1199 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1200 return;
1201 }
1202 /* this here, my friends, is either pure lunacy or something so obsolete that even it's mother
1203 * doesn't care about it. Essentialy, this test complains if secondary values for 'I' and '_'
1204 * are the same. According to the UCA, this is not true. Therefore, remove the test.
1205 * Besides, if primary strengths for two code points are different, it doesn't matter one bit
1206 * what is the relation between secondary or any other strengths.
1207 * killed by weiv 06/11/2002.
1208 */
1209 /*
1210 doAssert( ((order1 & UCOL_SECONDARYMASK) != (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be different");
1211 */
1212 doAssert( (order1 != UCOL_NULLORDER), "Unexpected end of iterator reached");
1213
1214 free(testString1);
1215 free(testString2);
1216 ucol_closeElements(iterator1);
1217 ucol_closeElements(iterator2);
1218 ucol_closeElements(iterator3);
1219 ucol_close(col);
1220
1221 log_verbose("testing CollationElementIterator ends...\n");
1222 }
1223
1224 void TestGetLocale() {
1225 UErrorCode status = U_ZERO_ERROR;
1226 const char *rules = "&a<x<y<z";
1227 UChar rlz[256] = {0};
1228 uint32_t rlzLen = u_unescape(rules, rlz, 256);
1229
1230 UCollator *coll = NULL;
1231 const char *locale = NULL;
1232
1233 int32_t i = 0;
1234
1235 /* Now that the collation tree is separate, actual==valid at all times. [alan] */
1236 static const struct {
1237 const char* requestedLocale;
1238 const char* validLocale;
1239 const char* actualLocale;
1240 } testStruct[] = {
1241 { "sr_RS", "sr_Cyrl_RS", "ru" },
1242 { "sh_YU", "sr_Latn_RS", "hr" }, /* this used to be sh, but now sh collation aliases hr */
1243 { "en_BE_FOO", "en_BE", "en_BE" },
1244 { "fr_FR_NONEXISTANT", "fr_FR", "fr" }
1245 };
1246
1247 /* test opening collators for different locales */
1248 for(i = 0; i<sizeof(testStruct)/sizeof(testStruct[0]); i++) {
1249 status = U_ZERO_ERROR;
1250 coll = ucol_open(testStruct[i].requestedLocale, &status);
1251 if(U_FAILURE(status)) {
1252 log_err("Failed to open collator for %s with %s\n", testStruct[i].requestedLocale, u_errorName(status));
1253 ucol_close(coll);
1254 continue;
1255 }
1256 locale = ucol_getLocale(coll, ULOC_REQUESTED_LOCALE, &status);
1257 if(strcmp(locale, testStruct[i].requestedLocale) != 0) {
1258 log_err("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].requestedLocale, locale);
1259 }
1260 locale = ucol_getLocale(coll, ULOC_VALID_LOCALE, &status);
1261 if(strcmp(locale, testStruct[i].validLocale) != 0) {
1262 log_err("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].validLocale, locale);
1263 }
1264 locale = ucol_getLocale(coll, ULOC_ACTUAL_LOCALE, &status);
1265 if(strcmp(locale, testStruct[i].actualLocale) != 0) {
1266 log_err("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].actualLocale, locale);
1267 }
1268 ucol_close(coll);
1269 }
1270
1271 /* completely non-existant locale for collator should get a default collator */
1272 {
1273 UCollator *defaultColl = ucol_open(NULL, &status);
1274 coll = ucol_open("blahaha", &status);
1275 if(U_SUCCESS(status)) {
1276 if(strcmp(ucol_getLocale(coll, ULOC_REQUESTED_LOCALE, &status), "blahaha")) {
1277 log_err("Nonexisting locale didn't preserve the requested locale\n");
1278 }
1279 if(strcmp(ucol_getLocale(coll, ULOC_VALID_LOCALE, &status),
1280 ucol_getLocale(defaultColl, ULOC_VALID_LOCALE, &status))) {
1281 log_err("Valid locale for nonexisting locale locale collator differs "
1282 "from valid locale for default collator\n");
1283 }
1284 if(strcmp(ucol_getLocale(coll, ULOC_ACTUAL_LOCALE, &status),
1285 ucol_getLocale(defaultColl, ULOC_ACTUAL_LOCALE, &status))) {
1286 log_err("Actual locale for nonexisting locale locale collator differs "
1287 "from actual locale for default collator\n");
1288 }
1289 ucol_close(coll);
1290 ucol_close(defaultColl);
1291 } else {
1292 log_data_err("Couldn't open collators\n");
1293 }
1294 }
1295
1296
1297
1298 /* collator instantiated from rules should have all three locales NULL */
1299 coll = ucol_openRules(rlz, rlzLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
1300 locale = ucol_getLocale(coll, ULOC_REQUESTED_LOCALE, &status);
1301 if(locale != NULL) {
1302 log_err("For collator instantiated from rules, requested locale returned %s instead of NULL\n", locale);
1303 }
1304 locale = ucol_getLocale(coll, ULOC_VALID_LOCALE, &status);
1305 if(locale != NULL) {
1306 log_err("For collator instantiated from rules, valid locale returned %s instead of NULL\n", locale);
1307 }
1308 locale = ucol_getLocale(coll, ULOC_ACTUAL_LOCALE, &status);
1309 if(locale != NULL) {
1310 log_err("For collator instantiated from rules, actual locale returned %s instead of NULL\n", locale);
1311 }
1312 ucol_close(coll);
1313
1314 }
1315
1316
1317 void TestGetAll()
1318 {
1319 int32_t i, count;
1320 count=ucol_countAvailable();
1321 /* use something sensible w/o hardcoding the count */
1322 if(count < 0){
1323 log_err("Error in countAvailable(), it returned %d\n", count);
1324 }
1325 else{
1326 log_verbose("PASS: countAvailable() successful, it returned %d\n", count);
1327 }
1328 for(i=0;i<count;i++)
1329 log_verbose("%s\n", ucol_getAvailable(i));
1330
1331
1332 }
1333
1334
1335 struct teststruct {
1336 const char *original;
1337 uint8_t key[256];
1338 } ;
1339
1340 static int compare_teststruct(const void *string1, const void *string2) {
1341 return(strcmp((const char *)((struct teststruct *)string1)->key, (const char *)((struct teststruct *)string2)->key));
1342 }
1343
1344 void TestBounds() {
1345 UErrorCode status = U_ZERO_ERROR;
1346
1347 UCollator *coll = ucol_open("sh", &status);
1348
1349 uint8_t sortkey[512], lower[512], upper[512];
1350 UChar buffer[512];
1351
1352 static const char * const test[] = {
1353 "John Smith",
1354 "JOHN SMITH",
1355 "john SMITH",
1356 "j\\u00F6hn sm\\u00EFth",
1357 "J\\u00F6hn Sm\\u00EFth",
1358 "J\\u00D6HN SM\\u00CFTH",
1359 "john smithsonian",
1360 "John Smithsonian",
1361 };
1362
1363 struct teststruct tests[] = {
1364 {"\\u010CAKI MIHALJ" } ,
1365 {"\\u010CAKI MIHALJ" } ,
1366 {"\\u010CAKI PIRO\\u0160KA" },
1367 {"\\u010CABAI ANDRIJA" } ,
1368 {"\\u010CABAI LAJO\\u0160" } ,
1369 {"\\u010CABAI MARIJA" } ,
1370 {"\\u010CABAI STEVAN" } ,
1371 {"\\u010CABAI STEVAN" } ,
1372 {"\\u010CABARKAPA BRANKO" } ,
1373 {"\\u010CABARKAPA MILENKO" } ,
1374 {"\\u010CABARKAPA MIROSLAV" } ,
1375 {"\\u010CABARKAPA SIMO" } ,
1376 {"\\u010CABARKAPA STANKO" } ,
1377 {"\\u010CABARKAPA TAMARA" } ,
1378 {"\\u010CABARKAPA TOMA\\u0160" } ,
1379 {"\\u010CABDARI\\u0106 NIKOLA" } ,
1380 {"\\u010CABDARI\\u0106 ZORICA" } ,
1381 {"\\u010CABI NANDOR" } ,
1382 {"\\u010CABOVI\\u0106 MILAN" } ,
1383 {"\\u010CABRADI AGNEZIJA" } ,
1384 {"\\u010CABRADI IVAN" } ,
1385 {"\\u010CABRADI JELENA" } ,
1386 {"\\u010CABRADI LJUBICA" } ,
1387 {"\\u010CABRADI STEVAN" } ,
1388 {"\\u010CABRDA MARTIN" } ,
1389 {"\\u010CABRILO BOGDAN" } ,
1390 {"\\u010CABRILO BRANISLAV" } ,
1391 {"\\u010CABRILO LAZAR" } ,
1392 {"\\u010CABRILO LJUBICA" } ,
1393 {"\\u010CABRILO SPASOJA" } ,
1394 {"\\u010CADE\\u0160 ZDENKA" } ,
1395 {"\\u010CADESKI BLAGOJE" } ,
1396 {"\\u010CADOVSKI VLADIMIR" } ,
1397 {"\\u010CAGLJEVI\\u0106 TOMA" } ,
1398 {"\\u010CAGOROVI\\u0106 VLADIMIR" } ,
1399 {"\\u010CAJA VANKA" } ,
1400 {"\\u010CAJI\\u0106 BOGOLJUB" } ,
1401 {"\\u010CAJI\\u0106 BORISLAV" } ,
1402 {"\\u010CAJI\\u0106 RADOSLAV" } ,
1403 {"\\u010CAK\\u0160IRAN MILADIN" } ,
1404 {"\\u010CAKAN EUGEN" } ,
1405 {"\\u010CAKAN EVGENIJE" } ,
1406 {"\\u010CAKAN IVAN" } ,
1407 {"\\u010CAKAN JULIJAN" } ,
1408 {"\\u010CAKAN MIHAJLO" } ,
1409 {"\\u010CAKAN STEVAN" } ,
1410 {"\\u010CAKAN VLADIMIR" } ,
1411 {"\\u010CAKAN VLADIMIR" } ,
1412 {"\\u010CAKAN VLADIMIR" } ,
1413 {"\\u010CAKARA ANA" } ,
1414 {"\\u010CAKAREVI\\u0106 MOMIR" } ,
1415 {"\\u010CAKAREVI\\u0106 NEDELJKO" } ,
1416 {"\\u010CAKI \\u0160ANDOR" } ,
1417 {"\\u010CAKI AMALIJA" } ,
1418 {"\\u010CAKI ANDRA\\u0160" } ,
1419 {"\\u010CAKI LADISLAV" } ,
1420 {"\\u010CAKI LAJO\\u0160" } ,
1421 {"\\u010CAKI LASLO" } ,
1422 };
1423
1424
1425
1426 int32_t i = 0, j = 0, k = 0, buffSize = 0, skSize = 0, lowerSize = 0, upperSize = 0;
1427 int32_t arraySize = sizeof(tests)/sizeof(tests[0]);
1428
1429 if(U_SUCCESS(status) && coll) {
1430 for(i = 0; i<arraySize; i++) {
1431 buffSize = u_unescape(tests[i].original, buffer, 512);
1432 skSize = ucol_getSortKey(coll, buffer, buffSize, tests[i].key, 512);
1433 }
1434
1435 qsort(tests, arraySize, sizeof(struct teststruct), compare_teststruct);
1436
1437 for(i = 0; i < arraySize-1; i++) {
1438 for(j = i+1; j < arraySize; j++) {
1439 lowerSize = ucol_getBound(tests[i].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1440 upperSize = ucol_getBound(tests[j].key, -1, UCOL_BOUND_UPPER, 1, upper, 512, &status);
1441 for(k = i; k <= j; k++) {
1442 if(strcmp((const char *)lower, (const char *)tests[k].key) > 0) {
1443 log_err("Problem with lower! j = %i (%s vs %s)\n", k, tests[k].original, tests[i].original);
1444 }
1445 if(strcmp((const char *)upper, (const char *)tests[k].key) <= 0) {
1446 log_err("Problem with upper! j = %i (%s vs %s)\n", k, tests[k].original, tests[j].original);
1447 }
1448 }
1449 }
1450 }
1451
1452
1453 #if 0
1454 for(i = 0; i < 1000; i++) {
1455 lowerRND = (rand()/(RAND_MAX/arraySize));
1456 upperRND = lowerRND + (rand()/(RAND_MAX/(arraySize-lowerRND)));
1457
1458 lowerSize = ucol_getBound(tests[lowerRND].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1459 upperSize = ucol_getBound(tests[upperRND].key, -1, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
1460
1461 for(j = lowerRND; j<=upperRND; j++) {
1462 if(strcmp(lower, tests[j].key) > 0) {
1463 log_err("Problem with lower! j = %i (%s vs %s)\n", j, tests[j].original, tests[lowerRND].original);
1464 }
1465 if(strcmp(upper, tests[j].key) <= 0) {
1466 log_err("Problem with upper! j = %i (%s vs %s)\n", j, tests[j].original, tests[upperRND].original);
1467 }
1468 }
1469 }
1470 #endif
1471
1472
1473
1474
1475
1476 for(i = 0; i<sizeof(test)/sizeof(test[0]); i++) {
1477 buffSize = u_unescape(test[i], buffer, 512);
1478 skSize = ucol_getSortKey(coll, buffer, buffSize, sortkey, 512);
1479 lowerSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1480 upperSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
1481 for(j = i+1; j<sizeof(test)/sizeof(test[0]); j++) {
1482 buffSize = u_unescape(test[j], buffer, 512);
1483 skSize = ucol_getSortKey(coll, buffer, buffSize, sortkey, 512);
1484 if(strcmp((const char *)lower, (const char *)sortkey) > 0) {
1485 log_err("Problem with lower! i = %i, j = %i (%s vs %s)\n", i, j, test[i], test[j]);
1486 }
1487 if(strcmp((const char *)upper, (const char *)sortkey) <= 0) {
1488 log_err("Problem with upper! i = %i, j = %i (%s vs %s)\n", i, j, test[i], test[j]);
1489 }
1490 }
1491 }
1492 ucol_close(coll);
1493 } else {
1494 log_data_err("Couldn't open collator\n");
1495 }
1496
1497 }
1498
1499 static void doOverrunTest(UCollator *coll, const UChar *uString, int32_t strLen) {
1500 int32_t skLen = 0, skLen2 = 0;
1501 uint8_t sortKey[256];
1502 int32_t i, j;
1503 uint8_t filler = 0xFF;
1504
1505 skLen = ucol_getSortKey(coll, uString, strLen, NULL, 0);
1506
1507 for(i = 0; i < skLen; i++) {
1508 memset(sortKey, filler, 256);
1509 skLen2 = ucol_getSortKey(coll, uString, strLen, sortKey, i);
1510 if(skLen != skLen2) {
1511 log_err("For buffer size %i, got different sortkey length. Expected %i got %i\n", i, skLen, skLen2);
1512 }
1513 for(j = i; j < 256; j++) {
1514 if(sortKey[j] != filler) {
1515 log_err("Something run over index %i\n", j);
1516 break;
1517 }
1518 }
1519 }
1520 }
1521
1522 /* j1865 reports that if a shorter buffer is passed to
1523 * to get sort key, a buffer overrun happens in some
1524 * cases. This test tries to check this.
1525 */
1526 void TestSortKeyBufferOverrun(void) {
1527 UErrorCode status = U_ZERO_ERROR;
1528 const char* cString = "A very Merry liTTle-lamB..";
1529 UChar uString[256];
1530 int32_t strLen = 0;
1531 UCollator *coll = ucol_open("root", &status);
1532 strLen = u_unescape(cString, uString, 256);
1533
1534 if(U_SUCCESS(status)) {
1535 log_verbose("testing non ignorable\n");
1536 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
1537 doOverrunTest(coll, uString, strLen);
1538
1539 log_verbose("testing shifted\n");
1540 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
1541 doOverrunTest(coll, uString, strLen);
1542
1543 log_verbose("testing shifted quaternary\n");
1544 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
1545 doOverrunTest(coll, uString, strLen);
1546
1547 log_verbose("testing with french secondaries\n");
1548 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
1549 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &status);
1550 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
1551 doOverrunTest(coll, uString, strLen);
1552
1553 }
1554 ucol_close(coll);
1555 }
1556
1557 static void TestAttribute()
1558 {
1559 UErrorCode error = U_ZERO_ERROR;
1560 UCollator *coll = ucol_open(NULL, &error);
1561
1562 if (U_FAILURE(error)) {
1563 log_err("Creation of default collator failed");
1564 return;
1565 }
1566
1567 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_OFF, &error);
1568 if (ucol_getAttribute(coll, UCOL_FRENCH_COLLATION, &error) != UCOL_OFF ||
1569 U_FAILURE(error)) {
1570 log_err("Setting and retrieving of the french collation failed");
1571 }
1572
1573 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_ON, &error);
1574 if (ucol_getAttribute(coll, UCOL_FRENCH_COLLATION, &error) != UCOL_ON ||
1575 U_FAILURE(error)) {
1576 log_err("Setting and retrieving of the french collation failed");
1577 }
1578
1579 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &error);
1580 if (ucol_getAttribute(coll, UCOL_ALTERNATE_HANDLING, &error) != UCOL_SHIFTED ||
1581 U_FAILURE(error)) {
1582 log_err("Setting and retrieving of the alternate handling failed");
1583 }
1584
1585 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &error);
1586 if (ucol_getAttribute(coll, UCOL_ALTERNATE_HANDLING, &error) != UCOL_NON_IGNORABLE ||
1587 U_FAILURE(error)) {
1588 log_err("Setting and retrieving of the alternate handling failed");
1589 }
1590
1591 ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_LOWER_FIRST, &error);
1592 if (ucol_getAttribute(coll, UCOL_CASE_FIRST, &error) != UCOL_LOWER_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_FIRST, UCOL_UPPER_FIRST, &error);
1598 if (ucol_getAttribute(coll, UCOL_CASE_FIRST, &error) != UCOL_UPPER_FIRST ||
1599 U_FAILURE(error)) {
1600 log_err("Setting and retrieving of the case first attribute failed");
1601 }
1602
1603 ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_ON, &error);
1604 if (ucol_getAttribute(coll, UCOL_CASE_LEVEL, &error) != UCOL_ON ||
1605 U_FAILURE(error)) {
1606 log_err("Setting and retrieving of the case level attribute failed");
1607 }
1608
1609 ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_OFF, &error);
1610 if (ucol_getAttribute(coll, UCOL_CASE_LEVEL, &error) != UCOL_OFF ||
1611 U_FAILURE(error)) {
1612 log_err("Setting and retrieving of the case level attribute failed");
1613 }
1614
1615 ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &error);
1616 if (ucol_getAttribute(coll, UCOL_NORMALIZATION_MODE, &error) != UCOL_ON ||
1617 U_FAILURE(error)) {
1618 log_err("Setting and retrieving of the normalization on/off attribute failed");
1619 }
1620
1621 ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &error);
1622 if (ucol_getAttribute(coll, UCOL_NORMALIZATION_MODE, &error) != UCOL_OFF ||
1623 U_FAILURE(error)) {
1624 log_err("Setting and retrieving of the normalization on/off attribute failed");
1625 }
1626
1627 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &error);
1628 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_PRIMARY ||
1629 U_FAILURE(error)) {
1630 log_err("Setting and retrieving of the collation strength failed");
1631 }
1632
1633 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_SECONDARY, &error);
1634 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_SECONDARY ||
1635 U_FAILURE(error)) {
1636 log_err("Setting and retrieving of the collation strength failed");
1637 }
1638
1639 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &error);
1640 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_TERTIARY ||
1641 U_FAILURE(error)) {
1642 log_err("Setting and retrieving of the collation strength failed");
1643 }
1644
1645 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &error);
1646 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_QUATERNARY ||
1647 U_FAILURE(error)) {
1648 log_err("Setting and retrieving of the collation strength failed");
1649 }
1650
1651 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_IDENTICAL, &error);
1652 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_IDENTICAL ||
1653 U_FAILURE(error)) {
1654 log_err("Setting and retrieving of the collation strength failed");
1655 }
1656
1657 ucol_close(coll);
1658 }
1659
1660 void TestGetTailoredSet() {
1661 struct {
1662 const char *rules;
1663 const char *tests[20];
1664 int32_t testsize;
1665 } setTest[] = {
1666 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1667 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1668 };
1669
1670 int32_t i = 0, j = 0;
1671 UErrorCode status = U_ZERO_ERROR;
1672 UParseError pError;
1673
1674 UCollator *coll = NULL;
1675 UChar buff[1024];
1676 int32_t buffLen = 0;
1677 USet *set = NULL;
1678
1679 for(i = 0; i < sizeof(setTest)/sizeof(setTest[0]); i++) {
1680 buffLen = u_unescape(setTest[i].rules, buff, 1024);
1681 coll = ucol_openRules(buff, buffLen, UCOL_DEFAULT, UCOL_DEFAULT, &pError, &status);
1682 if(U_SUCCESS(status)) {
1683 set = ucol_getTailoredSet(coll, &status);
1684 if(uset_size(set) != setTest[i].testsize) {
1685 log_err("Tailored set size different (%d) than expected (%d)\n", uset_size(set), setTest[i].testsize);
1686 }
1687 for(j = 0; j < setTest[i].testsize; j++) {
1688 buffLen = u_unescape(setTest[i].tests[j], buff, 1024);
1689 if(!uset_containsString(set, buff, buffLen)) {
1690 log_err("Tailored set doesn't contain %s... It should\n", setTest[i].tests[j]);
1691 }
1692 }
1693 uset_close(set);
1694 } else {
1695 log_err("Couldn't open collator with rules %s\n", setTest[i].rules);
1696 }
1697 ucol_close(coll);
1698 }
1699 }
1700
1701 static int tMemCmp(const uint8_t *first, const uint8_t *second) {
1702 int32_t firstLen = (int32_t)strlen((const char *)first);
1703 int32_t secondLen = (int32_t)strlen((const char *)second);
1704 return memcmp(first, second, uprv_min(firstLen, secondLen));
1705 }
1706 static const char * strengthsC[] = {
1707 "UCOL_PRIMARY",
1708 "UCOL_SECONDARY",
1709 "UCOL_TERTIARY",
1710 "UCOL_QUATERNARY",
1711 "UCOL_IDENTICAL"
1712 };
1713
1714 void TestMergeSortKeys(void) {
1715 UErrorCode status = U_ZERO_ERROR;
1716 UCollator *coll = ucol_open("en", &status);
1717 if(U_SUCCESS(status)) {
1718
1719 const char* cases[] = {
1720 "abc",
1721 "abcd",
1722 "abcde"
1723 };
1724 uint32_t casesSize = sizeof(cases)/sizeof(cases[0]);
1725 const char* prefix = "foo";
1726 const char* suffix = "egg";
1727 char outBuff1[256], outBuff2[256];
1728
1729 uint8_t **sortkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1730 uint8_t **mergedPrefixkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1731 uint8_t **mergedSuffixkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1732 uint32_t *sortKeysLen = (uint32_t *)malloc(casesSize*sizeof(uint32_t));
1733 uint8_t prefixKey[256], suffixKey[256];
1734 uint32_t prefixKeyLen = 0, suffixKeyLen = 0, i = 0;
1735 UChar buffer[256];
1736 uint32_t unescapedLen = 0, l1 = 0, l2 = 0;
1737 UColAttributeValue strength;
1738
1739 log_verbose("ucol_mergeSortkeys test\n");
1740 log_verbose("Testing order of the test cases\n");
1741 genericLocaleStarter("en", cases, casesSize);
1742
1743 for(i = 0; i<casesSize; i++) {
1744 sortkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1745 mergedPrefixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1746 mergedSuffixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1747 }
1748
1749 unescapedLen = u_unescape(prefix, buffer, 256);
1750 prefixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, prefixKey, 256);
1751
1752 unescapedLen = u_unescape(suffix, buffer, 256);
1753 suffixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, suffixKey, 256);
1754
1755 log_verbose("Massaging data with prefixes and different strengths\n");
1756 strength = UCOL_PRIMARY;
1757 while(strength <= UCOL_IDENTICAL) {
1758 log_verbose("Strength %s\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1759 ucol_setAttribute(coll, UCOL_STRENGTH, strength, &status);
1760 for(i = 0; i<casesSize; i++) {
1761 unescapedLen = u_unescape(cases[i], buffer, 256);
1762 sortKeysLen[i] = ucol_getSortKey(coll, buffer, unescapedLen, sortkeys[i], 256);
1763 ucol_mergeSortkeys(prefixKey, prefixKeyLen, sortkeys[i], sortKeysLen[i], mergedPrefixkeys[i], 256);
1764 ucol_mergeSortkeys(sortkeys[i], sortKeysLen[i], suffixKey, suffixKeyLen, mergedSuffixkeys[i], 256);
1765 if(i>0) {
1766 if(tMemCmp(mergedPrefixkeys[i-1], mergedPrefixkeys[i]) >= 0) {
1767 log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1768 log_err("%s\n%s\n",
1769 ucol_sortKeyToString(coll, mergedPrefixkeys[i-1], outBuff1, &l1),
1770 ucol_sortKeyToString(coll, mergedPrefixkeys[i], outBuff2, &l2));
1771 }
1772 if(tMemCmp(mergedSuffixkeys[i-1], mergedSuffixkeys[i]) >= 0) {
1773 log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1774 log_err("%s\n%s\n",
1775 ucol_sortKeyToString(coll, mergedSuffixkeys[i-1], outBuff1, &l1),
1776 ucol_sortKeyToString(coll, mergedSuffixkeys[i], outBuff2, &l2));
1777 }
1778 }
1779 }
1780 if(strength == UCOL_QUATERNARY) {
1781 strength = UCOL_IDENTICAL;
1782 } else {
1783 strength++;
1784 }
1785 }
1786
1787 {
1788 uint8_t smallBuf[3];
1789 uint32_t reqLen = 0;
1790 log_verbose("testing buffer overflow\n");
1791 reqLen = ucol_mergeSortkeys(prefixKey, prefixKeyLen, suffixKey, suffixKeyLen, smallBuf, 3);
1792 if(reqLen != (prefixKeyLen+suffixKeyLen-1)) {
1793 log_err("Wrong preflight size for merged sortkey\n");
1794 }
1795 }
1796
1797 {
1798 UChar empty = 0;
1799 uint8_t emptyKey[20], abcKey[50], mergedKey[100];
1800 int32_t emptyKeyLen = 0, abcKeyLen = 0, mergedKeyLen = 0;
1801
1802 log_verbose("testing merging with sortkeys generated for empty strings\n");
1803 emptyKeyLen = ucol_getSortKey(coll, &empty, 0, emptyKey, 20);
1804 unescapedLen = u_unescape(cases[0], buffer, 256);
1805 abcKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, abcKey, 50);
1806 mergedKeyLen = ucol_mergeSortkeys(emptyKey, emptyKeyLen, abcKey, abcKeyLen, mergedKey, 100);
1807 if(mergedKey[0] != 2) {
1808 log_err("Empty sortkey didn't produce a level separator\n");
1809 }
1810 /* try with zeros */
1811 mergedKeyLen = ucol_mergeSortkeys(emptyKey, 0, abcKey, abcKeyLen, mergedKey, 100);
1812 if(mergedKeyLen != 0 || mergedKey[0] != 0) {
1813 log_err("Empty key didn't produce null mergedKey\n");
1814 }
1815 mergedKeyLen = ucol_mergeSortkeys(abcKey, abcKeyLen, emptyKey, 0, mergedKey, 100);
1816 if(mergedKeyLen != 0 || mergedKey[0] != 0) {
1817 log_err("Empty key didn't produce null mergedKey\n");
1818 }
1819
1820 }
1821
1822 for(i = 0; i<casesSize; i++) {
1823 free(sortkeys[i]);
1824 free(mergedPrefixkeys[i]);
1825 free(mergedSuffixkeys[i]);
1826 }
1827 free(sortkeys);
1828 free(mergedPrefixkeys);
1829 free(mergedSuffixkeys);
1830 free(sortKeysLen);
1831 ucol_close(coll);
1832 /* need to finish this up */
1833 } else {
1834 log_data_err("Couldn't open collator");
1835 }
1836 }
1837 static void TestShortString(void)
1838 {
1839 struct {
1840 const char *input;
1841 const char *expectedOutput;
1842 const char *locale;
1843 UErrorCode expectedStatus;
1844 int32_t expectedOffset;
1845 uint32_t expectedIdentifier;
1846 } testCases[] = {
1847 {"LDE_RDE_KPHONEBOOK_T0041_ZLATN","B2C00_KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING, 0, 0 },
1848 {"LEN_RUS_NO_AS_S4","AS_LEN_NO_S4", NULL, U_USING_FALLBACK_WARNING, 0, 0 },
1849 {"LDE_VPHONEBOOK_EO_SI","EO_KPHONEBOOK_LDE_SI", "de@collation=phonebook", U_ZERO_ERROR, 0, 0 },
1850 {"LDE_Kphonebook","KPHONEBOOK_LDE", "de@collation=phonebook", U_ZERO_ERROR, 0, 0 },
1851 {"Xqde_DE@collation=phonebookq_S3_EX","KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING, 0, 0 },
1852 {"LFR_FO", "LFR", NULL, U_ZERO_ERROR, 0, 0 },
1853 {"SO_LX_AS", "", NULL, U_ILLEGAL_ARGUMENT_ERROR, 8, 0 },
1854 {"S3_ASS_MMM", "", NULL, U_ILLEGAL_ARGUMENT_ERROR, 5, 0 }
1855 };
1856
1857 int32_t i = 0;
1858 UCollator *coll = NULL, *fromNormalized = NULL;
1859 UParseError parseError;
1860 UErrorCode status = U_ZERO_ERROR;
1861 char fromShortBuffer[256], normalizedBuffer[256], fromNormalizedBuffer[256];
1862 const char* locale = NULL;
1863
1864
1865 for(i = 0; i < sizeof(testCases)/sizeof(testCases[0]); i++) {
1866 status = U_ZERO_ERROR;
1867 if(testCases[i].locale) {
1868 locale = testCases[i].locale;
1869 } else {
1870 locale = NULL;
1871 }
1872
1873 coll = ucol_openFromShortString(testCases[i].input, FALSE, &parseError, &status);
1874 if(status != testCases[i].expectedStatus) {
1875 log_err("Got status '%s' that is different from expected '%s' for '%s'\n",
1876 u_errorName(status), u_errorName(testCases[i].expectedStatus), testCases[i].input);
1877 }
1878
1879 if(U_SUCCESS(status)) {
1880 ucol_getShortDefinitionString(coll, locale, fromShortBuffer, 256, &status);
1881
1882 if(strcmp(fromShortBuffer, testCases[i].expectedOutput)) {
1883 log_err("Got short string '%s' from the collator. Expected '%s' for input '%s'\n",
1884 fromShortBuffer, testCases[i].expectedOutput, testCases[i].input);
1885 }
1886
1887 ucol_normalizeShortDefinitionString(testCases[i].input, normalizedBuffer, 256, &parseError, &status);
1888 fromNormalized = ucol_openFromShortString(normalizedBuffer, FALSE, &parseError, &status);
1889 ucol_getShortDefinitionString(fromNormalized, locale, fromNormalizedBuffer, 256, &status);
1890
1891 if(strcmp(fromShortBuffer, fromNormalizedBuffer)) {
1892 log_err("Strings obtained from collators instantiated by short string ('%s') and from normalized string ('%s') differ\n",
1893 fromShortBuffer, fromNormalizedBuffer);
1894 }
1895
1896
1897 if(!ucol_equals(coll, fromNormalized)) {
1898 log_err("Collator from short string ('%s') differs from one obtained through a normalized version ('%s')\n",
1899 testCases[i].input, normalizedBuffer);
1900 }
1901
1902 ucol_close(fromNormalized);
1903 ucol_close(coll);
1904
1905 } else {
1906 if(parseError.offset != testCases[i].expectedOffset) {
1907 log_err("Got parse error offset %i, but expected %i instead for '%s'\n",
1908 parseError.offset, testCases[i].expectedOffset, testCases[i].input);
1909 }
1910 }
1911 }
1912
1913 }
1914
1915 static void
1916 doSetsTest(const char *locale, const USet *ref, USet *set, const char* inSet, const char* outSet, UErrorCode *status) {
1917 UChar buffer[512];
1918 int32_t bufLen;
1919
1920 uset_clear(set);
1921 bufLen = u_unescape(inSet, buffer, 512);
1922 uset_applyPattern(set, buffer, bufLen, 0, status);
1923 if(U_FAILURE(*status)) {
1924 log_err("%s: Failure setting pattern %s\n", locale, u_errorName(*status));
1925 }
1926
1927 if(!uset_containsAll(ref, set)) {
1928 log_err("%s: Some stuff from %s is not present in the set\n", locale, inSet);
1929 }
1930
1931 uset_clear(set);
1932 bufLen = u_unescape(outSet, buffer, 512);
1933 uset_applyPattern(set, buffer, bufLen, 0, status);
1934 if(U_FAILURE(*status)) {
1935 log_err("%s: Failure setting pattern %s\n", locale, u_errorName(*status));
1936 }
1937
1938 if(!uset_containsNone(ref, set)) {
1939 log_err("%s: Some stuff from %s is present in the set\n", locale, outSet);
1940 }
1941 }
1942
1943
1944
1945
1946 static void
1947 TestGetContractionsAndUnsafes(void)
1948 {
1949 static struct {
1950 const char* locale;
1951 const char* inConts;
1952 const char* outConts;
1953 const char* inExp;
1954 const char* outExp;
1955 const char* unsafeCodeUnits;
1956 const char* safeCodeUnits;
1957 } tests[] = {
1958 { "ru",
1959 "[{\\u0474\\u030F}{\\u0475\\u030F}{\\u04D8\\u0308}{\\u04D9\\u0308}{\\u04E8\\u0308}{\\u04E9\\u0308}]",
1960 "[{\\u0430\\u0306}{\\u0410\\u0306}{\\u0430\\u0308}{\\u0410\\u0306}{\\u0433\\u0301}{\\u0413\\u0301}]",
1961 "[\\u00e6]",
1962 "[a]",
1963 "[\\u0474\\u0475\\u04d8\\u04d9\\u04e8\\u04e9]",
1964 "[aAbB\\u0430\\u0410\\u0433\\u0413]"
1965 },
1966 { "uk",
1967 "[{\\u0474\\u030F}{\\u0475\\u030F}{\\u04D8\\u0308}{\\u04D9\\u0308}{\\u04E8\\u0308}{\\u04E9\\u0308}"
1968 "{\\u0430\\u0306}{\\u0410\\u0306}{\\u0430\\u0308}{\\u0410\\u0306}{\\u0433\\u0301}{\\u0413\\u0301}]",
1969 "[]",
1970 "[\\u00e6]",
1971 "[a]",
1972 "[\\u0474\\u0475\\u04D8\\u04D9\\u04E8\\u04E9\\u0430\\u0410\\u0433\\u0413]",
1973 "[aAbBxv]",
1974 },
1975 { "sh",
1976 "[{C\\u0301}{C\\u030C}{C\\u0341}{DZ\\u030C}{Dz\\u030C}{D\\u017D}{D\\u017E}{lj}{nj}]",
1977 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
1978 "[\\u00e6]",
1979 "[a]",
1980 "[nlcdzNLCDZ]",
1981 "[jabv]"
1982 },
1983 { "ja",
1984 "[{\\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}]",
1985 "[{\\u30FD\\u3099}{\\u309D\\u3099}{\\u3053\\u3099}{\\u30B3\\u3099}{lj}{nj}]",
1986 "[\\u30FE\\u00e6]",
1987 "[a]",
1988 "[\\u3099]",
1989 "[]"
1990 }
1991 };
1992
1993
1994
1995
1996 UErrorCode status = U_ZERO_ERROR;
1997 UCollator *coll = NULL;
1998 int32_t i = 0;
1999 int32_t noConts = 0;
2000 USet *conts = uset_open(0,0);
2001 USet *exp = uset_open(0, 0);
2002 USet *set = uset_open(0,0);
2003 int32_t setBufferLen = 65536;
2004 UChar buffer[65536];
2005 int32_t setLen = 0;
2006
2007 for(i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
2008 log_verbose("Testing locale: %s\n", tests[i].locale);
2009 coll = ucol_open(tests[i].locale, &status);
2010 ucol_getContractionsAndExpansions(coll, conts, exp, TRUE, &status);
2011 doSetsTest(tests[i].locale, conts, set, tests[i].inConts, tests[i].outConts, &status);
2012 setLen = uset_toPattern(conts, buffer, setBufferLen, TRUE, &status);
2013 if(U_SUCCESS(status)) {
2014 /*log_verbose("Contractions %i: %s\n", uset_getItemCount(conts), aescstrdup(buffer, setLen));*/
2015 } else {
2016 log_err("error %s. %i\n", u_errorName(status), setLen);
2017 status = U_ZERO_ERROR;
2018 }
2019 doSetsTest(tests[i].locale, exp, set, tests[i].inExp, tests[i].outExp, &status);
2020 setLen = uset_toPattern(exp, buffer, setBufferLen, TRUE, &status);
2021 if(U_SUCCESS(status)) {
2022 /*log_verbose("Expansions %i: %s\n", uset_getItemCount(exp), aescstrdup(buffer, setLen));*/
2023 } else {
2024 log_err("error %s. %i\n", u_errorName(status), setLen);
2025 status = U_ZERO_ERROR;
2026 }
2027
2028 noConts = ucol_getUnsafeSet(coll, conts, &status);
2029 doSetsTest(tests[i].locale, conts, set, tests[i].unsafeCodeUnits, tests[i].safeCodeUnits, &status);
2030 setLen = uset_toPattern(conts, buffer, setBufferLen, TRUE, &status);
2031 if(U_SUCCESS(status)) {
2032 log_verbose("Unsafe %i: %s\n", uset_getItemCount(exp), aescstrdup(buffer, setLen));
2033 } else {
2034 log_err("error %s. %i\n", u_errorName(status), setLen);
2035 status = U_ZERO_ERROR;
2036 }
2037
2038 ucol_close(coll);
2039 }
2040
2041
2042 uset_close(conts);
2043 uset_close(exp);
2044 uset_close(set);
2045 }
2046
2047 static void
2048 TestOpenBinary(void)
2049 {
2050 UErrorCode status = U_ZERO_ERROR;
2051 /*
2052 char rule[] = "&h < d < c < b";
2053 char *wUCA[] = { "a", "h", "d", "c", "b", "i" };
2054 char *noUCA[] = {"d", "c", "b", "a", "h", "i" };
2055 */
2056 /* we have to use Cyrillic letters because latin-1 always gets copied */
2057 const char rule[] = "&\\u0452 < \\u0434 < \\u0433 < \\u0432"; /* &dje < d < g < v */
2058 const char *wUCA[] = { "\\u0430", "\\u0452", "\\u0434", "\\u0433", "\\u0432", "\\u0435" }; /* a, dje, d, g, v, e */
2059 const char *noUCA[] = {"\\u0434", "\\u0433", "\\u0432", "\\u0430", "\\u0435", "\\u0452" }; /* d, g, v, a, e, dje */
2060
2061 UChar uRules[256];
2062 int32_t uRulesLen = u_unescape(rule, uRules, 256);
2063
2064 UCollator *coll = ucol_openRules(uRules, uRulesLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
2065 UCollator *UCA = NULL;
2066 UCollator *cloneNOUCA = NULL, *cloneWUCA = NULL;
2067
2068 uint8_t imageBuffer[32768];
2069 uint8_t *image = imageBuffer;
2070 int32_t imageBufferCapacity = 32768;
2071
2072 int32_t imageSize;
2073
2074 if((coll==NULL)||(U_FAILURE(status))) {
2075 log_data_err("could not load collators or error occured: %s\n",
2076 u_errorName(status));
2077 return;
2078 }
2079 UCA = ucol_open("root", &status);
2080 if((UCA==NULL)||(U_FAILURE(status))) {
2081 log_data_err("could not load UCA collator or error occured: %s\n",
2082 u_errorName(status));
2083 return;
2084 }
2085 imageSize = ucol_cloneBinary(coll, image, imageBufferCapacity, &status);
2086 if(U_FAILURE(status)) {
2087 image = (uint8_t *)malloc(imageSize*sizeof(uint8_t));
2088 status = U_ZERO_ERROR;
2089 imageSize = ucol_cloneBinary(coll, imageBuffer, imageSize, &status);
2090 }
2091
2092
2093 cloneWUCA = ucol_openBinary(image, imageSize, UCA, &status);
2094 cloneNOUCA = ucol_openBinary(image, imageSize, NULL, &status);
2095
2096 genericOrderingTest(coll, wUCA, sizeof(wUCA)/sizeof(wUCA[0]));
2097
2098 genericOrderingTest(cloneWUCA, wUCA, sizeof(wUCA)/sizeof(wUCA[0]));
2099 genericOrderingTest(cloneNOUCA, noUCA, sizeof(noUCA)/sizeof(noUCA[0]));
2100
2101 if(image != imageBuffer) {
2102 free(image);
2103 }
2104 ucol_close(coll);
2105 ucol_close(cloneNOUCA);
2106 ucol_close(cloneWUCA);
2107 ucol_close(UCA);
2108 }
2109
2110 static void TestDefault(void) {
2111 /* Tests for code coverage. */
2112 UErrorCode status = U_ZERO_ERROR;
2113 UCollator *coll = ucol_open("es@collation=pinyin", &status);
2114 if (status != U_USING_DEFAULT_WARNING) {
2115 /* What do you mean that you know about using pinyin collation in Spanish!? This should be in the zh locale. */
2116 log_err("es@collation=pinyin should return U_USING_DEFAULT_WARNING, but returned %s\n", u_errorName(status));
2117 }
2118 ucol_close(coll);
2119 if (ucol_getKeywordValues("funky", &status) != NULL) {
2120 log_err("Collators should not know about the funky keyword.\n");
2121 }
2122 if (status != U_ILLEGAL_ARGUMENT_ERROR) {
2123 log_err("funky keyword didn't fail as expected %s\n", u_errorName(status));
2124 }
2125 if (ucol_getKeywordValues("collation", &status) != NULL) {
2126 log_err("ucol_getKeywordValues should not work when given a bad status.\n");
2127 }
2128 }
2129
2130 #endif /* #if !UCONFIG_NO_COLLATION */