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