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