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