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