]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/capitst.c
ICU-531.30.tar.gz
[apple/icu.git] / icuSources / test / cintltst / capitst.c
CommitLineData
b75a7d8f 1/********************************************************************
57a6839d 2 * Copyright (c) 1997-2014, International Business Machines
729e4ab9 3 * Corporation and others. All Rights Reserved.
b75a7d8f 4 ********************************************************************/
46f4442e 5/*****************************************************************************
b75a7d8f
A
6*
7* File CAPITEST.C
8*
9* Modification History:
729e4ab9 10* Name Description
b75a7d8f 11* Madhu Katragadda Ported for C API
729e4ab9 12* Brian Rower Added TestOpenVsOpenRules
46f4442e 13******************************************************************************
b75a7d8f
A
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"
729e4ab9 24#include "unicode/ulocdata.h"
b75a7d8f
A
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"
374ca955 31#include "putilimp.h"
729e4ab9
A
32#include "cmemory.h"
33#include "cstring.h"
57a6839d
A
34#include "ucol_imp.h"
35
36#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
b75a7d8f
A
37
38static void TestAttribute(void);
46f4442e 39static void TestDefault(void);
729e4ab9 40static void TestDefaultKeyword(void);
4388f060 41static void TestBengaliSortKey(void);
b75a7d8f
A
42
43
4388f060
A
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;
729e4ab9 47
4388f060
A
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 }
b75a7d8f 56 }
4388f060
A
57 if (position + 3 < len)
58 position += sprintf(buffer + position, "%02X]", b);
59 return buffer;
b75a7d8f 60}
b75a7d8f
A
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");
73c04bcf 75 addTest(root, &TestCloneBinary, "tscoll/capitst/TestCloneBinary");
b75a7d8f
A
76 addTest(root, &TestGetSetAttr, "tscoll/capitst/TestGetSetAttr");
77 addTest(root, &TestBounds, "tscoll/capitst/TestBounds");
46f4442e 78 addTest(root, &TestGetLocale, "tscoll/capitst/TestGetLocale");
b75a7d8f
A
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");
374ca955
A
83 addTest(root, &TestShortString, "tscoll/capitst/TestShortString");
84 addTest(root, &TestGetContractionsAndUnsafes, "tscoll/capitst/TestGetContractionsAndUnsafes");
73c04bcf 85 addTest(root, &TestOpenBinary, "tscoll/capitst/TestOpenBinary");
46f4442e 86 addTest(root, &TestDefault, "tscoll/capitst/TestDefault");
729e4ab9
A
87 addTest(root, &TestDefaultKeyword, "tscoll/capitst/TestDefaultKeyword");
88 addTest(root, &TestOpenVsOpenRules, "tscoll/capitst/TestOpenVsOpenRules");
4388f060 89 addTest(root, &TestBengaliSortKey, "tscoll/capitst/TestBengaliSortKey");
729e4ab9 90 addTest(root, &TestGetKeywordValuesForLocale, "tscoll/capitst/TestGetKeywordValuesForLocale");
57a6839d 91 addTest(root, &TestStrcollNull, "tscoll/capitst/TestStrcollNull");
b75a7d8f
A
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;
729e4ab9 113 UColAttributeValue value;
b75a7d8f
A
114 uint32_t i = 0, j = 0;
115
729e4ab9
A
116 if (coll == NULL) {
117 log_err_status(status, "Unable to open collator. %s\n", u_errorName(status));
118 return;
119 }
b75a7d8f
A
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)) {
729e4ab9 124 log_err_status(status, "ucol_setAttribute with the default value returned error: %s\n", u_errorName(status));
b75a7d8f
A
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 }
b75a7d8f
A
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
51004dcb
A
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
b75a7d8f
A
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;
729e4ab9
A
223
224
b75a7d8f
A
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);
729e4ab9 231
b75a7d8f
A
232 }
233
729e4ab9 234
b75a7d8f
A
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 }
729e4ab9 241 binColl=ures_getByKey(res, "%%Collation", binColl, &status);
b75a7d8f
A
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);
729e4ab9 263
b75a7d8f
A
264}
265#endif
266
267/* Collator Properties
268 ucol_open, ucol_strcoll, getStrength/setStrength
269 getDecomposition/setDecomposition, getDisplayName*/
270void TestProperty()
729e4ab9 271{
b75a7d8f
A
272 UCollator *col, *ruled;
273 UChar *disName;
729e4ab9 274 int32_t len = 0;
51004dcb 275 UChar source[12], target[12];
b75a7d8f
A
276 int32_t tempLength;
277 UErrorCode status = U_ZERO_ERROR;
729e4ab9
A
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 */
374ca955
A
289 UVersionInfo versionArray = {0, 0, 0, 0};
290 UVersionInfo versionUCAArray = {0, 0, 0, 0};
729e4ab9
A
291 UVersionInfo versionUCDArray = {0, 0, 0, 0};
292
b75a7d8f
A
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)) {
729e4ab9 297 log_err_status(status, "Default Collator creation failed.: %s\n", myErrorName(status));
b75a7d8f
A
298 return;
299 }
300
301 ucol_getVersion(col, versionArray);
729e4ab9
A
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]);
b75a7d8f
A
310 }
311
729e4ab9
A
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);
374ca955 315 ucol_getUCAVersion(col, versionUCAArray);
729e4ab9
A
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]);
374ca955
A
319 }
320
b75a7d8f
A
321 u_uastrcpy(source, "ab");
322 u_uastrcpy(target, "abc");
729e4ab9 323
51004dcb 324 doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_LESS, "ab < abc comparison failed");
b75a7d8f
A
325
326 u_uastrcpy(source, "ab");
327 u_uastrcpy(target, "AB");
328
51004dcb
A
329 doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_LESS, "ab < AB comparison failed");
330
b75a7d8f 331 u_uastrcpy(source, "blackbird");
51004dcb
A
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");
b75a7d8f 335
b75a7d8f
A
336 u_uastrcpy(source, "black bird");
337 u_uastrcpy(target, "black-bird");
51004dcb
A
338
339 doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_LESS, "black bird < black-bird comparison failed");
340
b75a7d8f
A
341 u_uastrcpy(source, "Hello");
342 u_uastrcpy(target, "hello");
343
51004dcb
A
344 doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_GREATER, "Hello > hello comparison failed");
345
b75a7d8f
A
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");
729e4ab9 351
b75a7d8f
A
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
729e4ab9 358
b75a7d8f
A
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 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 );
729e4ab9 386 doAssert( tempLength == 0x00, "getRulesEx() result incorrect" );
b75a7d8f 387 log_verbose("getRules tests end.\n");
729e4ab9 388
b75a7d8f
A
389 log_verbose("ucol_getRulesEx() testing ...\n");
390 tempLength=ucol_getRulesEx(col,UCOL_FULL_RULES,buffer,bufLen );
391 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);
729e4ab9 397
b75a7d8f
A
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");
729e4ab9 408
b75a7d8f
A
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);
729e4ab9 415
b75a7d8f
A
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];
b75a7d8f
A
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");
729e4ab9 446
374ca955 447
b75a7d8f
A
448 col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL,&status);
449 if (U_FAILURE(status)) {
729e4ab9 450 log_err_status(status, "RuleBased Collator creation failed.: %s\n", myErrorName(status));
b75a7d8f
A
451 return;
452 }
453 else
454 log_verbose("PASS: RuleBased Collator creation passed\n");
729e4ab9 455
b75a7d8f
A
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");
729e4ab9
A
464
465
b75a7d8f
A
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");
729e4ab9 474
b75a7d8f
A
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");
729e4ab9 482
b75a7d8f
A
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);
729e4ab9 495
b75a7d8f
A
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);
374ca955 521 if(U_FAILURE(status)) {
b75a7d8f
A
522 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status));
523 return;
374ca955 524 }
b75a7d8f
A
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);
57a6839d 536 /* CLDR 24+ requires a reset before the first relation */
b75a7d8f
A
537 u_uastrcpy(ruleset1, "< z < a");
538 col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
57a6839d
A
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));
729e4ab9 543 }
b75a7d8f
A
544 ucol_close(col1);
545}
546
547void TestCompare()
548{
549 UErrorCode status = U_ZERO_ERROR;
550 UCollator *col;
551 UChar* test1;
552 UChar* test2;
729e4ab9 553
b75a7d8f
A
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)) {
729e4ab9 558 log_err_status(status, "ucal_open() collation creation failed.: %s\n", myErrorName(status));
b75a7d8f
A
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");
729e4ab9 565
b75a7d8f 566 log_verbose("Use tertiary comparison level testing ....\n");
729e4ab9 567
b75a7d8f
A
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\" ");
729e4ab9 570 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\"");
b75a7d8f
A
571
572 ucol_setStrength(col, UCOL_SECONDARY);
573 log_verbose("Use secondary comparison level testing ....\n");
729e4ab9 574
b75a7d8f
A
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\"");
729e4ab9 577 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\"");
b75a7d8f
A
578
579 ucol_setStrength(col, UCOL_PRIMARY);
580 log_verbose("Use primary comparison level testing ....\n");
729e4ab9 581
b75a7d8f
A
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\"");
729e4ab9
A
584 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
585
b75a7d8f 586
b75a7d8f
A
587 log_verbose("The compare tests end.\n");
588 ucol_close(col);
589 free(test1);
590 free(test2);
729e4ab9 591
b75a7d8f
A
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)) {
729e4ab9 605 log_err_status(status, "ERROR: collation creation failed.: %s\n", myErrorName(status));
b75a7d8f
A
606 return;
607 }
608
609 if (ucol_getAttribute(vi_VN, UCOL_NORMALIZATION_MODE, &status) != UCOL_ON ||
610 U_FAILURE(status))
611 {
51004dcb 612 log_err("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
b75a7d8f
A
613 }
614
615 status = U_ZERO_ERROR;
616 if (ucol_getAttribute(el_GR, UCOL_NORMALIZATION_MODE, &status) != UCOL_ON ||
617 U_FAILURE(status))
618 {
51004dcb 619 log_err("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
b75a7d8f
A
620 }
621
622 status = U_ZERO_ERROR;
623 if (ucol_getAttribute(en_US, UCOL_NORMALIZATION_MODE, &status) != UCOL_OFF ||
624 U_FAILURE(status))
625 {
51004dcb 626 log_err("ERROR: en_US collation had canonical decomposition for normalization!\n");
b75a7d8f
A
627 }
628
629 ucol_close(en_US);
630 ucol_close(el_GR);
631 ucol_close(vi_VN);
632}
633
73c04bcf 634#define CLONETEST_COLLATOR_COUNT 4
b75a7d8f
A
635
636void TestSafeClone() {
46f4442e
A
637 UChar test1[6];
638 UChar test2[6];
73c04bcf
A
639 static const UChar umlautUStr[] = {0x00DC, 0};
640 static const UChar oeStr[] = {0x0055, 0x0045, 0};
b75a7d8f
A
641 UCollator * someCollators [CLONETEST_COLLATOR_COUNT];
642 UCollator * someClonedCollators [CLONETEST_COLLATOR_COUNT];
643 UCollator * col;
644 UErrorCode err = U_ZERO_ERROR;
51004dcb 645 int8_t idx = 6; /* Leave this here to test buffer alingment in memory*/
b75a7d8f
A
646 uint8_t buffer [CLONETEST_COLLATOR_COUNT] [U_COL_SAFECLONE_BUFFERSIZE];
647 int32_t bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
73c04bcf
A
648 const char sampleRuleChars[] = "&Z < CH";
649 UChar sampleRule[sizeof(sampleRuleChars)];
b75a7d8f 650
b75a7d8f
A
651 u_uastrcpy(test1, "abCda");
652 u_uastrcpy(test2, "abcda");
73c04bcf 653 u_uastrcpy(sampleRule, sampleRuleChars);
729e4ab9 654
b75a7d8f
A
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);
73c04bcf 659 someCollators[3] = ucol_openRules(sampleRule, -1, UCOL_ON, UCOL_TERTIARY, NULL, &err);
b75a7d8f 660 if(U_FAILURE(err)) {
51004dcb
A
661 for (idx = 0; idx < CLONETEST_COLLATOR_COUNT; idx++) {
662 ucol_close(someCollators[idx]);
46f4442e 663 }
73c04bcf
A
664 log_data_err("Couldn't open one or more collators\n");
665 return;
b75a7d8f
A
666 }
667
668 /* Check the various error & informational states: */
669
670 /* Null status - just returns NULL */
57a6839d 671 if (NULL != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, NULL))
b75a7d8f
A
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;
57a6839d 677 if (NULL != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err) || err != U_MEMORY_ALLOCATION_ERROR)
b75a7d8f
A
678 {
679 log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
680 }
681 err = U_ZERO_ERROR;
682
57a6839d
A
683 /* Null buffer size pointer is ok */
684 if (NULL == (col = ucol_safeClone(someCollators[0], buffer[0], NULL, &err)) || U_FAILURE(err))
b75a7d8f
A
685 {
686 log_err("FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer\n");
687 }
57a6839d 688 ucol_close(col);
b75a7d8f 689 err = U_ZERO_ERROR;
729e4ab9 690
b75a7d8f
A
691 /* buffer size pointer is 0 - fill in pbufferSize with a size */
692 bufferSize = 0;
57a6839d
A
693 if (NULL != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err) ||
694 U_FAILURE(err) || bufferSize <= 0)
b75a7d8f
A
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 */
57a6839d 704 if (NULL == (col = ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err)) || U_FAILURE(err))
b75a7d8f
A
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 */
57a6839d
A
710 if (bufferSize > 1) {
711 --bufferSize;
712 }
713 if (NULL == (col = ucol_safeClone(someCollators[0], 0, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
b75a7d8f
A
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 */
57a6839d 723 if (NULL == (col = ucol_safeClone(someCollators[0], 0, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
b75a7d8f
A
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 */
57a6839d 731 if (NULL != ucol_safeClone(NULL, buffer[0], &bufferSize, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
b75a7d8f
A
732 {
733 log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
734 }
735
736 err = U_ZERO_ERROR;
737
73c04bcf
A
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
b75a7d8f
A
752 /* change orig & clone & make sure they are independent */
753
51004dcb 754 for (idx = 0; idx < CLONETEST_COLLATOR_COUNT; idx++)
b75a7d8f 755 {
51004dcb 756 ucol_setStrength(someCollators[idx], UCOL_IDENTICAL);
73c04bcf
A
757 bufferSize = 1;
758 err = U_ZERO_ERROR;
51004dcb 759 ucol_close(ucol_safeClone(someCollators[idx], buffer[idx], &bufferSize, &err));
73c04bcf 760 if (err != U_SAFECLONE_ALLOCATED_WARNING) {
51004dcb
A
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);
73c04bcf
A
763 }
764
b75a7d8f 765 bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
73c04bcf 766 err = U_ZERO_ERROR;
51004dcb 767 someClonedCollators[idx] = ucol_safeClone(someCollators[idx], buffer[idx], &bufferSize, &err);
57a6839d
A
768 if (U_FAILURE(err)) {
769 log_err("FAIL: Unable to clone collator %d - %s\n", idx, u_errorName(err));
770 continue;
73c04bcf 771 }
51004dcb
A
772 if (!ucol_equals(someClonedCollators[idx], someCollators[idx])) {
773 log_err("FAIL: Cloned collator is not equal to original at index = %d.\n", idx);
73c04bcf 774 }
b75a7d8f 775
73c04bcf 776 /* Check the usability */
51004dcb
A
777 ucol_setStrength(someCollators[idx], UCOL_PRIMARY);
778 ucol_setAttribute(someCollators[idx], UCOL_CASE_LEVEL, UCOL_OFF, &err);
729e4ab9 779
51004dcb 780 doAssert( (ucol_equal(someCollators[idx], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abcda\" == \"abCda\"");
729e4ab9 781
73c04bcf 782 /* Close the original to make sure that the clone is usable. */
51004dcb 783 ucol_close(someCollators[idx]);
73c04bcf 784
51004dcb
A
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\" ");
73c04bcf 788
51004dcb 789 ucol_close(someClonedCollators[idx]);
b75a7d8f 790 }
b75a7d8f
A
791}
792
73c04bcf
A
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}
4388f060
A
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
729e4ab9
A
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;
4388f060
A
928 char sortKeyStr1[512], sortKeyStr2[512];
929 uint32_t sortKeyStrLen1 = sizeof(sortKeyStr1) / sizeof(sortKeyStr1[0]),
930 sortKeyStrLen2 = sizeof(sortKeyStr2) / sizeof(sortKeyStr2[0]);
729e4ab9
A
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);
57a6839d 946 (void)sizeOfStdSet; /* Suppress set but not used warning. */
729e4ab9
A
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);
4388f060 1013 ucol_sortKeyToString(c1, sortKey1, sortKeyStr1, sortKeyStrLen1);
729e4ab9
A
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);
4388f060 1019 ucol_sortKeyToString(c2, sortKey2, sortKeyStr2, sortKeyStrLen2);
729e4ab9
A
1020
1021 /* Check that the lengths are the same */
4388f060
A
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 }
729e4ab9
A
1026
1027 /* check that the keys are the same */
4388f060
A
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 }
729e4ab9
A
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}
b75a7d8f
A
1046/*
1047----------------------------------------------------------------------------
1048 ctor -- Tests the getSortKey
1049*/
1050void TestSortKey()
729e4ab9 1051{
b75a7d8f 1052 uint8_t *sortk1 = NULL, *sortk2 = NULL, *sortk3 = NULL, *sortkEmpty = NULL;
b75a7d8f 1053 int32_t sortklen, osortklen;
b75a7d8f
A
1054 UCollator *col;
1055 UChar *test1, *test2, *test3;
1056 UErrorCode status = U_ZERO_ERROR;
1057 char toStringBuffer[256], *resultP;
4388f060 1058 uint32_t toStringLen=sizeof(toStringBuffer)/sizeof(toStringBuffer[0]);
b75a7d8f
A
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);
729e4ab9 1067
b75a7d8f
A
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");
729e4ab9 1075 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
b75a7d8f
A
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)) {
729e4ab9 1080 log_err_status(status, "ERROR: Default collation creation failed.: %s\n", myErrorName(status));
b75a7d8f
A
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);
729e4ab9 1095
b75a7d8f
A
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
4388f060 1129 resultP = ucol_sortKeyToString(col, sortk3, toStringBuffer, toStringLen);
b75a7d8f
A
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 }
729e4ab9 1146
b75a7d8f
A
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);
729e4ab9 1164
b75a7d8f
A
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");
57a6839d 1180 sortklen = ucol_getSortKey(col, NULL, 10, NULL, 0);
b75a7d8f
A
1181 if(sortklen != 0) {
1182 log_err("Invalid string didn't return sortkey size of 0\n");
1183 }
729e4ab9
A
1184
1185
b75a7d8f
A
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);
729e4ab9 1193
b75a7d8f
A
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)) {
729e4ab9 1205 log_err_status(status, "ERROR: Default collation creation failed.: %s\n", myErrorName(status));
b75a7d8f
A
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);
729e4ab9
A
1225
1226
b75a7d8f 1227 log_verbose("ucol_hashCode() testing ...\n");
729e4ab9
A
1228
1229 doAssert( ucol_keyHashCode(sortk1, sortk1len) != ucol_keyHashCode(sortk2, sortk2len), "Hash test1 result incorrect" );
b75a7d8f
A
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" );
729e4ab9 1232
b75a7d8f
A
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.
729e4ab9
A
1247 *
1248 */
b75a7d8f
A
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)) {
729e4ab9 1261 log_err_status(status, "ERROR: Default collation creation failed.: %s\n", myErrorName(status));
b75a7d8f
A
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?");
729e4ab9 1269
b75a7d8f 1270 log_verbose("Constructors and comparison testing....\n");
729e4ab9 1271
b75a7d8f
A
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);
57a6839d 1297 (void)offset; /* Suppress set but not used warning. */
b75a7d8f
A
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 }
729e4ab9 1327
b75a7d8f 1328 doAssert((order1 == order2), "The second iterator advance failed should be the same as first one");
729e4ab9 1329
b75a7d8f
A
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");
729e4ab9 1333
b75a7d8f
A
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");
729e4ab9 1346
b75a7d8f
A
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);
729e4ab9 1375
b75a7d8f
A
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[] = {
729e4ab9 1395 { "sr_RS", "sr_Cyrl_RS", "sr" },
51004dcb 1396 { "sh_YU", "sr_Latn_RS", "sr_Latn" }, /* was sh, then aliased to hr, now sr_Latn via import per cldrbug 5647: */
57a6839d
A
1397 { "en_BE_FOO", "en", "root" },
1398 { "sv_SE_NONEXISTANT", "sv", "sv" }
b75a7d8f
A
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)) {
729e4ab9 1406 log_err_status(status, "Failed to open collator for %s with %s\n", testStruct[i].requestedLocale, u_errorName(status));
b75a7d8f
A
1407 ucol_close(coll);
1408 continue;
1409 }
57a6839d
A
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) {
b75a7d8f
A
1416 log_err("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].requestedLocale, locale);
1417 }
729e4ab9 1418 locale = ucol_getLocaleByType(coll, ULOC_VALID_LOCALE, &status);
b75a7d8f
A
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 }
729e4ab9 1422 locale = ucol_getLocaleByType(coll, ULOC_ACTUAL_LOCALE, &status);
b75a7d8f
A
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)) {
57a6839d 1434 /* See comment above about ticket #10477.
729e4ab9 1435 if(strcmp(ucol_getLocaleByType(coll, ULOC_REQUESTED_LOCALE, &status), "blahaha")) {
b75a7d8f 1436 log_err("Nonexisting locale didn't preserve the requested locale\n");
57a6839d 1437 } */
729e4ab9
A
1438 if(strcmp(ucol_getLocaleByType(coll, ULOC_VALID_LOCALE, &status),
1439 ucol_getLocaleByType(defaultColl, ULOC_VALID_LOCALE, &status))) {
b75a7d8f
A
1440 log_err("Valid locale for nonexisting locale locale collator differs "
1441 "from valid locale for default collator\n");
1442 }
729e4ab9
A
1443 if(strcmp(ucol_getLocaleByType(coll, ULOC_ACTUAL_LOCALE, &status),
1444 ucol_getLocaleByType(defaultColl, ULOC_ACTUAL_LOCALE, &status))) {
b75a7d8f
A
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
729e4ab9 1455
b75a7d8f
A
1456
1457 /* collator instantiated from rules should have all three locales NULL */
1458 coll = ucol_openRules(rlz, rlzLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
729e4ab9 1459 locale = ucol_getLocaleByType(coll, ULOC_REQUESTED_LOCALE, &status);
b75a7d8f
A
1460 if(locale != NULL) {
1461 log_err("For collator instantiated from rules, requested locale returned %s instead of NULL\n", locale);
1462 }
729e4ab9 1463 locale = ucol_getLocaleByType(coll, ULOC_VALID_LOCALE, &status);
b75a7d8f
A
1464 if(locale != NULL) {
1465 log_err("For collator instantiated from rules, valid locale returned %s instead of NULL\n", locale);
1466 }
729e4ab9 1467 locale = ucol_getLocaleByType(coll, ULOC_ACTUAL_LOCALE, &status);
b75a7d8f
A
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];
46f4442e 1497} ;
b75a7d8f
A
1498
1499static int compare_teststruct(const void *string1, const void *string2) {
46f4442e 1500 return(strcmp((const char *)((struct teststruct *)string1)->key, (const char *)((struct teststruct *)string2)->key));
b75a7d8f
A
1501}
1502
1503void TestBounds() {
46f4442e 1504 UErrorCode status = U_ZERO_ERROR;
b75a7d8f 1505
46f4442e 1506 UCollator *coll = ucol_open("sh", &status);
b75a7d8f 1507
46f4442e
A
1508 uint8_t sortkey[512], lower[512], upper[512];
1509 UChar buffer[512];
b75a7d8f 1510
46f4442e
A
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 };
b75a7d8f 1521
729e4ab9 1522 struct teststruct tests[] = {
46f4442e
A
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 };
b75a7d8f 1582
b75a7d8f 1583
b75a7d8f 1584
46f4442e
A
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]);
b75a7d8f 1587
46f4442e
A
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);
57a6839d
A
1600 (void)lowerSize; /* Suppress set but not used warning. */
1601 (void)upperSize;
46f4442e
A
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 }
b75a7d8f 1611 }
b75a7d8f
A
1612
1613
1614#if 0
46f4442e
A
1615 for(i = 0; i < 1000; i++) {
1616 lowerRND = (rand()/(RAND_MAX/arraySize));
1617 upperRND = lowerRND + (rand()/(RAND_MAX/(arraySize-lowerRND)));
b75a7d8f 1618
46f4442e
A
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);
b75a7d8f 1621
46f4442e
A
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 }
b75a7d8f
A
1631#endif
1632
1633
1634
1635
1636
46f4442e
A
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 }
b75a7d8f 1652 }
46f4442e
A
1653 ucol_close(coll);
1654 } else {
1655 log_data_err("Couldn't open collator\n");
b75a7d8f 1656 }
b75a7d8f
A
1657
1658}
1659
1660static void doOverrunTest(UCollator *coll, const UChar *uString, int32_t strLen) {
46f4442e
A
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 }
b75a7d8f 1680 }
b75a7d8f
A
1681}
1682
1683/* j1865 reports that if a shorter buffer is passed to
729e4ab9 1684* to get sort key, a buffer overrun happens in some
46f4442e
A
1685* cases. This test tries to check this.
1686*/
b75a7d8f 1687void TestSortKeyBufferOverrun(void) {
46f4442e
A
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);
b75a7d8f 1694
46f4442e
A
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);
b75a7d8f 1699
46f4442e
A
1700 log_verbose("testing shifted\n");
1701 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
1702 doOverrunTest(coll, uString, strLen);
b75a7d8f 1703
46f4442e
A
1704 log_verbose("testing shifted quaternary\n");
1705 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
1706 doOverrunTest(coll, uString, strLen);
b75a7d8f 1707
46f4442e
A
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);
b75a7d8f 1713
46f4442e
A
1714 }
1715 ucol_close(coll);
b75a7d8f
A
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)) {
729e4ab9 1724 log_err_status(error, "Creation of default collator failed\n");
b75a7d8f
A
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)) {
729e4ab9 1731 log_err_status(error, "Setting and retrieving of the french collation failed\n");
b75a7d8f
A
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)) {
729e4ab9 1737 log_err_status(error, "Setting and retrieving of the french collation failed\n");
b75a7d8f
A
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)) {
729e4ab9 1743 log_err_status(error, "Setting and retrieving of the alternate handling failed\n");
b75a7d8f
A
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)) {
729e4ab9 1749 log_err_status(error, "Setting and retrieving of the alternate handling failed\n");
b75a7d8f
A
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)) {
729e4ab9 1755 log_err_status(error, "Setting and retrieving of the case first attribute failed\n");
b75a7d8f
A
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)) {
729e4ab9 1761 log_err_status(error, "Setting and retrieving of the case first attribute failed\n");
b75a7d8f
A
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)) {
729e4ab9 1767 log_err_status(error, "Setting and retrieving of the case level attribute failed\n");
b75a7d8f
A
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)) {
729e4ab9 1773 log_err_status(error, "Setting and retrieving of the case level attribute failed\n");
b75a7d8f
A
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)) {
729e4ab9 1779 log_err_status(error, "Setting and retrieving of the normalization on/off attribute failed\n");
b75a7d8f
A
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)) {
729e4ab9 1785 log_err_status(error, "Setting and retrieving of the normalization on/off attribute failed\n");
b75a7d8f
A
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)) {
729e4ab9 1791 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
b75a7d8f
A
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)) {
729e4ab9 1797 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
b75a7d8f
A
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)) {
729e4ab9 1803 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
b75a7d8f
A
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)) {
729e4ab9 1809 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
b75a7d8f
A
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)) {
729e4ab9 1815 log_err_status(error, "Setting and retrieving of the collation strength failed\n");
b75a7d8f
A
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
57a6839d 1840 for(i = 0; i < LENGTHOF(setTest); i++) {
b75a7d8f
A
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);
57a6839d
A
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);
b75a7d8f
A
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 {
729e4ab9 1856 log_err_status(status, "Couldn't open collator with rules %s\n", setTest[i].rules);
b75a7d8f
A
1857 }
1858 ucol_close(coll);
1859 }
1860}
1861
1862static int tMemCmp(const uint8_t *first, const uint8_t *second) {
374ca955
A
1863 int32_t firstLen = (int32_t)strlen((const char *)first);
1864 int32_t secondLen = (int32_t)strlen((const char *)second);
b75a7d8f
A
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};
729e4ab9 1874
b75a7d8f
A
1875void TestMergeSortKeys(void) {
1876 UErrorCode status = U_ZERO_ERROR;
1877 UCollator *coll = ucol_open("en", &status);
1878 if(U_SUCCESS(status)) {
729e4ab9 1879
b75a7d8f
A
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];
729e4ab9 1889
b75a7d8f
A
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;
729e4ab9 1899
b75a7d8f
A
1900 log_verbose("ucol_mergeSortkeys test\n");
1901 log_verbose("Testing order of the test cases\n");
1902 genericLocaleStarter("en", cases, casesSize);
729e4ab9 1903
b75a7d8f
A
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 }
729e4ab9 1909
b75a7d8f
A
1910 unescapedLen = u_unescape(prefix, buffer, 256);
1911 prefixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, prefixKey, 256);
729e4ab9 1912
b75a7d8f
A
1913 unescapedLen = u_unescape(suffix, buffer, 256);
1914 suffixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, suffixKey, 256);
729e4ab9 1915
b75a7d8f
A
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]);
729e4ab9 1929 log_err("%s\n%s\n",
4388f060
A
1930 ucol_sortKeyToString(coll, mergedPrefixkeys[i-1], outBuff1, l1),
1931 ucol_sortKeyToString(coll, mergedPrefixkeys[i], outBuff2, l2));
b75a7d8f
A
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]);
729e4ab9 1935 log_err("%s\n%s\n",
4388f060
A
1936 ucol_sortKeyToString(coll, mergedSuffixkeys[i-1], outBuff1, l1),
1937 ucol_sortKeyToString(coll, mergedSuffixkeys[i], outBuff2, l2));
b75a7d8f
A
1938 }
1939 }
1940 }
1941 if(strength == UCOL_QUATERNARY) {
1942 strength = UCOL_IDENTICAL;
1943 } else {
1944 strength++;
1945 }
1946 }
729e4ab9 1947
b75a7d8f
A
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);
51004dcb 1953 if(reqLen != (prefixKeyLen+suffixKeyLen)) {
b75a7d8f
A
1954 log_err("Wrong preflight size for merged sortkey\n");
1955 }
1956 }
729e4ab9 1957
b75a7d8f
A
1958 {
1959 UChar empty = 0;
1960 uint8_t emptyKey[20], abcKey[50], mergedKey[100];
1961 int32_t emptyKeyLen = 0, abcKeyLen = 0, mergedKeyLen = 0;
729e4ab9 1962
b75a7d8f
A
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 }
729e4ab9 1980
b75a7d8f 1981 }
729e4ab9 1982
b75a7d8f
A
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}
729e4ab9 1998static void TestShortString(void)
374ca955
A
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[] = {
729e4ab9 2008 /*
57a6839d
A
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.
729e4ab9 2022 */
57a6839d 2023 {"LDE_RDE_KPHONEBOOK_T0024_ZLATN","KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING, 0, 0 },
729e4ab9
A
2024
2025 {"LEN_RUS_NO_AS_S4","AS_LROOT_NO_S4", NULL, U_USING_DEFAULT_WARNING, 0, 0 },
374ca955
A
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 },
729e4ab9 2029 {"LFR_FO", "FO_LROOT", NULL, U_USING_DEFAULT_WARNING, 0, 0 },
374ca955
A
2030 {"SO_LX_AS", "", NULL, U_ILLEGAL_ARGUMENT_ERROR, 8, 0 },
2031 {"S3_ASS_MMM", "", NULL, U_ILLEGAL_ARGUMENT_ERROR, 5, 0 }
2032 };
2033
46f4442e
A
2034 int32_t i = 0;
2035 UCollator *coll = NULL, *fromNormalized = NULL;
374ca955
A
2036 UParseError parseError;
2037 UErrorCode status = U_ZERO_ERROR;
46f4442e 2038 char fromShortBuffer[256], normalizedBuffer[256], fromNormalizedBuffer[256];
374ca955
A
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) {
729e4ab9 2052 log_err_status(status, "Got status '%s' that is different from expected '%s' for '%s'\n",
374ca955 2053 u_errorName(status), u_errorName(testCases[i].expectedStatus), testCases[i].input);
729e4ab9 2054 continue;
374ca955 2055 }
729e4ab9 2056
374ca955
A
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)) {
729e4ab9 2070 log_err("Strings obtained from collators instantiated by short string ('%s') and from normalized string ('%s') differ\n",
374ca955
A
2071 fromShortBuffer, fromNormalizedBuffer);
2072 }
2073
2074
2075 if(!ucol_equals(coll, fromNormalized)) {
729e4ab9 2076 log_err("Collator from short string ('%s') differs from one obtained through a normalized version ('%s')\n",
374ca955
A
2077 testCases[i].input, normalizedBuffer);
2078 }
2079
374ca955
A
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
46f4442e 2094doSetsTest(const char *locale, const USet *ref, USet *set, const char* inSet, const char* outSet, UErrorCode *status) {
57a6839d 2095 UChar buffer[65536];
374ca955
A
2096 int32_t bufLen;
2097
2098 uset_clear(set);
729e4ab9 2099 bufLen = u_unescape(inSet, buffer, 512);
374ca955
A
2100 uset_applyPattern(set, buffer, bufLen, 0, status);
2101 if(U_FAILURE(*status)) {
46f4442e 2102 log_err("%s: Failure setting pattern %s\n", locale, u_errorName(*status));
374ca955
A
2103 }
2104
2105 if(!uset_containsAll(ref, set)) {
46f4442e 2106 log_err("%s: Some stuff from %s is not present in the set\n", locale, inSet);
57a6839d
A
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));
374ca955
A
2112 }
2113
2114 uset_clear(set);
729e4ab9 2115 bufLen = u_unescape(outSet, buffer, 512);
374ca955
A
2116 uset_applyPattern(set, buffer, bufLen, 0, status);
2117 if(U_FAILURE(*status)) {
46f4442e 2118 log_err("%s: Failure setting pattern %s\n", locale, u_errorName(*status));
374ca955
A
2119 }
2120
2121 if(!uset_containsNone(ref, set)) {
46f4442e 2122 log_err("%s: Some stuff from %s is present in the set\n", locale, outSet);
374ca955
A
2123 }
2124}
2125
2126
2127
2128
729e4ab9
A
2129static void
2130TestGetContractionsAndUnsafes(void)
374ca955
A
2131{
2132 static struct {
2133 const char* locale;
2134 const char* inConts;
2135 const char* outConts;
73c04bcf
A
2136 const char* inExp;
2137 const char* outExp;
374ca955
A
2138 const char* unsafeCodeUnits;
2139 const char* safeCodeUnits;
2140 } tests[] = {
729e4ab9
A
2141 { "ru",
2142 "[{\\u0418\\u0306}{\\u0438\\u0306}]",
2143 "[\\u0439\\u0457]",
73c04bcf 2144 "[\\u00e6]",
729e4ab9
A
2145 "[ae]",
2146 "[\\u0418\\u0438]",
374ca955
A
2147 "[aAbB\\u0430\\u0410\\u0433\\u0413]"
2148 },
2149 { "uk",
729e4ab9
A
2150 "[{\\u0406\\u0308}{\\u0456\\u0308}{\\u0418\\u0306}{\\u0438\\u0306}]",
2151 "[\\u0407\\u0419\\u0439\\u0457]",
73c04bcf 2152 "[\\u00e6]",
729e4ab9
A
2153 "[ae]",
2154 "[\\u0406\\u0456\\u0418\\u0438]",
374ca955
A
2155 "[aAbBxv]",
2156 },
374ca955
A
2157 { "sh",
2158 "[{C\\u0301}{C\\u030C}{C\\u0341}{DZ\\u030C}{Dz\\u030C}{D\\u017D}{D\\u017E}{lj}{nj}]",
2159 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
73c04bcf
A
2160 "[\\u00e6]",
2161 "[a]",
374ca955
A
2162 "[nlcdzNLCDZ]",
2163 "[jabv]"
73c04bcf
A
2164 },
2165 { "ja",
57a6839d
A
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}]",
73c04bcf
A
2179 "[{\\u30FD\\u3099}{\\u309D\\u3099}{\\u3053\\u3099}{\\u30B3\\u3099}{lj}{nj}]",
2180 "[\\u30FE\\u00e6]",
2181 "[a]",
2182 "[\\u3099]",
2183 "[]"
374ca955
A
2184 }
2185 };
2186
374ca955
A
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);
73c04bcf 2192 USet *exp = uset_open(0, 0);
374ca955 2193 USet *set = uset_open(0,0);
73c04bcf
A
2194 int32_t setBufferLen = 65536;
2195 UChar buffer[65536];
374ca955
A
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);
729e4ab9
A
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 }
73c04bcf 2205 ucol_getContractionsAndExpansions(coll, conts, exp, TRUE, &status);
46f4442e 2206 doSetsTest(tests[i].locale, conts, set, tests[i].inConts, tests[i].outConts, &status);
73c04bcf 2207 setLen = uset_toPattern(conts, buffer, setBufferLen, TRUE, &status);
374ca955 2208 if(U_SUCCESS(status)) {
73c04bcf 2209 /*log_verbose("Contractions %i: %s\n", uset_getItemCount(conts), aescstrdup(buffer, setLen));*/
374ca955
A
2210 } else {
2211 log_err("error %s. %i\n", u_errorName(status), setLen);
73c04bcf
A
2212 status = U_ZERO_ERROR;
2213 }
46f4442e 2214 doSetsTest(tests[i].locale, exp, set, tests[i].inExp, tests[i].outExp, &status);
73c04bcf
A
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;
374ca955
A
2221 }
2222
2223 noConts = ucol_getUnsafeSet(coll, conts, &status);
57a6839d 2224 (void)noConts; /* Suppress set but not used warning */
46f4442e 2225 doSetsTest(tests[i].locale, conts, set, tests[i].unsafeCodeUnits, tests[i].safeCodeUnits, &status);
73c04bcf
A
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 }
374ca955
A
2233
2234 ucol_close(coll);
2235 }
2236
2237
2238 uset_close(conts);
73c04bcf 2239 uset_close(exp);
374ca955
A
2240 uset_close(set);
2241}
2242
729e4ab9
A
2243static void
2244TestOpenBinary(void)
73c04bcf 2245{
57a6839d
A
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
46f4442e
A
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 */
57a6839d 2269#if OPEN_BINARY_ACCEPTS_NULL_BASE
46f4442e 2270 const char *noUCA[] = {"\\u0434", "\\u0433", "\\u0432", "\\u0430", "\\u0435", "\\u0452" }; /* d, g, v, a, e, dje */
57a6839d 2271#endif
73c04bcf 2272
46f4442e
A
2273 UChar uRules[256];
2274 int32_t uRulesLen = u_unescape(rule, uRules, 256);
73c04bcf 2275
46f4442e
A
2276 UCollator *coll = ucol_openRules(uRules, uRulesLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
2277 UCollator *UCA = NULL;
2278 UCollator *cloneNOUCA = NULL, *cloneWUCA = NULL;
73c04bcf 2279
46f4442e
A
2280 uint8_t imageBuffer[32768];
2281 uint8_t *image = imageBuffer;
2282 int32_t imageBufferCapacity = 32768;
73c04bcf 2283
46f4442e 2284 int32_t imageSize;
73c04bcf 2285
46f4442e
A
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;
729e4ab9 2290 }
46f4442e
A
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;
729e4ab9 2296 }
46f4442e
A
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 }
73c04bcf
A
2303
2304
46f4442e
A
2305 cloneWUCA = ucol_openBinary(image, imageSize, UCA, &status);
2306 cloneNOUCA = ucol_openBinary(image, imageSize, NULL, &status);
57a6839d
A
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
46f4442e
A
2312
2313 genericOrderingTest(coll, wUCA, sizeof(wUCA)/sizeof(wUCA[0]));
2314
2315 genericOrderingTest(cloneWUCA, wUCA, sizeof(wUCA)/sizeof(wUCA[0]));
57a6839d 2316#if OPEN_BINARY_ACCEPTS_NULL_BASE
46f4442e 2317 genericOrderingTest(cloneNOUCA, noUCA, sizeof(noUCA)/sizeof(noUCA[0]));
57a6839d 2318#endif
73c04bcf 2319
46f4442e
A
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);
729e4ab9
A
2333 if (coll == NULL || status == U_FILE_ACCESS_ERROR) {
2334 log_data_err("Unable to open collator es@collation=pinyin\n");
2335 return;
2336 }
46f4442e
A
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 }
73c04bcf
A
2351}
2352
729e4ab9
A
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 1
2371#define PREFERRED_SIZE 16
57a6839d 2372#define MAX_NUMBER_OF_KEYWORDS 9
729e4ab9 2373 const char *PREFERRED[PREFERRED_SIZE][MAX_NUMBER_OF_KEYWORDS+1] = {
57a6839d
A
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 },
729e4ab9 2379#if INCLUDE_UNIHAN_COLLATION
57a6839d
A
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" },
729e4ab9 2386#else
57a6839d
A
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 },
729e4ab9 2393#endif
57a6839d
A
2394 { "es_ES", "standard", "search", "traditional", "eor", NULL, NULL, NULL, NULL, NULL },
2395 { "es__TRADITIONAL","traditional", "search", "standard", "eor", NULL, NULL, NULL, NULL, NULL },
2396 { "und@collation=phonebook", "standard", "eor", "search", NULL, NULL, NULL, NULL, NULL, NULL },
2397 { "de_DE@collation=big5han", "standard", "phonebook", "search", "eor", NULL, NULL, NULL, NULL, NULL },
2398 { "zzz@collation=xxx", "standard", "eor", "search", NULL, NULL, NULL, NULL, NULL, NULL }
729e4ab9
A
2399 };
2400#if INCLUDE_UNIHAN_COLLATION
57a6839d 2401 const int32_t expectedLength[PREFERRED_SIZE] = { 3, 3, 3, 4, 4, 9, 9, 9, 9, 9, 9, 4, 4, 3, 4, 3 };
729e4ab9 2402#else
57a6839d 2403 const int32_t expectedLength[PREFERRED_SIZE] = { 3, 3, 3, 4, 4, 8, 8, 8, 8, 8, 8, 4, 4, 3, 4, 3 };
729e4ab9
A
2404#endif
2405
2406 UErrorCode status = U_ZERO_ERROR;
2407 UEnumeration *keywordValues = NULL;
2408 int32_t i, n, size, valueLength;
2409 const char *locale = NULL, *value = NULL;
2410 UBool errorOccurred = FALSE;
2411
2412 for (i = 0; i < PREFERRED_SIZE; i++) {
2413 locale = PREFERRED[i][0];
2414 value = NULL;
2415 valueLength = 0;
2416 size = 0;
2417
2418 keywordValues = ucol_getKeywordValuesForLocale("collation", locale, TRUE, &status);
2419 if (keywordValues == NULL || U_FAILURE(status)) {
2420 log_err_status(status, "Error getting keyword values: %s\n", u_errorName(status));
2421 break;
2422 }
2423 size = uenum_count(keywordValues, &status);
2424
2425 if (size == expectedLength[i]) {
2426 for (n = 0; n < expectedLength[i]; n++) {
2427 if ((value = uenum_next(keywordValues, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
2428 if (uprv_strcmp(value, PREFERRED[i][n+1]) != 0) {
2429 log_err("Keyword values differ: Got [%s] Expected [%s] for locale: %s\n", value, PREFERRED[i][n+1], locale);
2430 errorOccurred = TRUE;
2431 break;
2432 }
2433
2434 } else {
2435 log_err("While getting keyword value from locale: %s got this error: %s\n", locale, u_errorName(status));
2436 errorOccurred = TRUE;
2437 break;
2438 }
2439 }
2440 if (errorOccurred) {
2441 break;
2442 }
2443 } else {
2444 log_err("Number of keywords (%d) does not match expected size (%d) for locale: %s\n", size, expectedLength[i], locale);
2445 break;
2446 }
2447 uenum_close(keywordValues);
2448 keywordValues = NULL;
2449 }
2450 if (keywordValues != NULL) {
2451 uenum_close(keywordValues);
2452 }
2453}
2454
57a6839d
A
2455static void TestStrcollNull(void) {
2456 UErrorCode status = U_ZERO_ERROR;
2457 UCollator *coll;
2458
2459 const UChar u16asc[] = {0x0049, 0x0042, 0x004D, 0};
2460 const int32_t u16ascLen = 3;
2461
2462 const UChar u16han[] = {0x5c71, 0x5ddd, 0};
2463 const int32_t u16hanLen = 2;
2464
2465 const char *u8asc = "\x49\x42\x4D";
2466 const int32_t u8ascLen = 3;
2467
2468 const char *u8han = "\xE5\xB1\xB1\xE5\xB7\x9D";
2469 const int32_t u8hanLen = 6;
2470
2471 coll = ucol_open(NULL, &status);
2472 if (U_FAILURE(status)) {
2473 log_err_status(status, "Default Collator creation failed.: %s\n", myErrorName(status));
2474 return;
2475 }
2476
2477 /* UChar API */
2478 if (ucol_strcoll(coll, NULL, 0, NULL, 0) != 0) {
2479 log_err("ERROR : ucol_strcoll NULL/0 and NULL/0");
2480 }
2481
2482 if (ucol_strcoll(coll, NULL, -1, NULL, 0) != 0) {
2483 /* No error arg, should return equal without crash */
2484 log_err("ERROR : ucol_strcoll NULL/-1 and NULL/0");
2485 }
2486
2487 if (ucol_strcoll(coll, u16asc, -1, NULL, 10) != 0) {
2488 /* No error arg, should return equal without crash */
2489 log_err("ERROR : ucol_strcoll u16asc/u16ascLen and NULL/10");
2490 }
2491
2492 if (ucol_strcoll(coll, u16asc, -1, NULL, 0) <= 0) {
2493 log_err("ERROR : ucol_strcoll u16asc/-1 and NULL/0");
2494 }
2495 if (ucol_strcoll(coll, NULL, 0, u16asc, -1) >= 0) {
2496 log_err("ERROR : ucol_strcoll NULL/0 and u16asc/-1");
2497 }
2498 if (ucol_strcoll(coll, u16asc, u16ascLen, NULL, 0) <= 0) {
2499 log_err("ERROR : ucol_strcoll u16asc/u16ascLen and NULL/0");
2500 }
2501
2502 if (ucol_strcoll(coll, u16han, -1, NULL, 0) <= 0) {
2503 log_err("ERROR : ucol_strcoll u16han/-1 and NULL/0");
2504 }
2505 if (ucol_strcoll(coll, NULL, 0, u16han, -1) >= 0) {
2506 log_err("ERROR : ucol_strcoll NULL/0 and u16han/-1");
2507 }
2508 if (ucol_strcoll(coll, NULL, 0, u16han, u16hanLen) >= 0) {
2509 log_err("ERROR : ucol_strcoll NULL/0 and u16han/u16hanLen");
2510 }
2511
2512 /* UTF-8 API */
2513 status = U_ZERO_ERROR;
2514 if (ucol_strcollUTF8(coll, NULL, 0, NULL, 0, &status) != 0 || U_FAILURE(status)) {
2515 log_err("ERROR : ucol_strcollUTF8 NULL/0 and NULL/0");
2516 }
2517 status = U_ZERO_ERROR;
2518 ucol_strcollUTF8(coll, NULL, -1, NULL, 0, &status);
2519 if (status != U_ILLEGAL_ARGUMENT_ERROR) {
2520 log_err("ERROR: ucol_strcollUTF8 NULL/-1 and NULL/0, should return U_ILLEGAL_ARGUMENT_ERROR");
2521 }
2522 status = U_ZERO_ERROR;
2523 ucol_strcollUTF8(coll, u8asc, u8ascLen, NULL, 10, &status);
2524 if (status != U_ILLEGAL_ARGUMENT_ERROR) {
2525 log_err("ERROR: ucol_strcollUTF8 u8asc/u8ascLen and NULL/10, should return U_ILLEGAL_ARGUMENT_ERROR");
2526 }
2527
2528 status = U_ZERO_ERROR;
2529 if (ucol_strcollUTF8(coll, u8asc, -1, NULL, 0, &status) <= 0 || U_FAILURE(status)) {
2530 log_err("ERROR : ucol_strcollUTF8 u8asc/-1 and NULL/0");
2531 }
2532 status = U_ZERO_ERROR;
2533 if (ucol_strcollUTF8(coll, NULL, 0, u8asc, -1, &status) >= 0 || U_FAILURE(status)) {
2534 log_err("ERROR : ucol_strcollUTF8 NULL/0 and u8asc/-1");
2535 }
2536 status = U_ZERO_ERROR;
2537 if (ucol_strcollUTF8(coll, u8asc, u8ascLen, NULL, 0, &status) <= 0 || U_FAILURE(status)) {
2538 log_err("ERROR : ucol_strcollUTF8 u8asc/u8ascLen and NULL/0");
2539 }
2540
2541 status = U_ZERO_ERROR;
2542 if (ucol_strcollUTF8(coll, u8han, -1, NULL, 0, &status) <= 0 || U_FAILURE(status)) {
2543 log_err("ERROR : ucol_strcollUTF8 u8han/-1 and NULL/0");
2544 }
2545 status = U_ZERO_ERROR;
2546 if (ucol_strcollUTF8(coll, NULL, 0, u8han, -1, &status) >= 0 || U_FAILURE(status)) {
2547 log_err("ERROR : ucol_strcollUTF8 NULL/0 and u8han/-1");
2548 }
2549 status = U_ZERO_ERROR;
2550 if (ucol_strcollUTF8(coll, NULL, 0, u8han, u8hanLen, &status) >= 0 || U_FAILURE(status)) {
2551 log_err("ERROR : ucol_strcollUTF8 NULL/0 and u8han/u8hanLen");
2552 }
2553
2554 ucol_close(coll);
2555}
729e4ab9 2556
b75a7d8f 2557#endif /* #if !UCONFIG_NO_COLLATION */