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