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