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