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