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