]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/capitst.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / cintltst / capitst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
374ca955 3 * Copyright (c) 1997-2004, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6/********************************************************************************
7*
8* File CAPITEST.C
9*
10* Modification History:
11* Name Description
12* Madhu Katragadda Ported for C API
13*********************************************************************************
14*//* C API TEST For COLLATOR */
15
16#include "unicode/utypes.h"
17
18#if !UCONFIG_NO_COLLATION
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include "unicode/uloc.h"
24#include "unicode/ustring.h"
25#include "unicode/ures.h"
26#include "unicode/ucoleitr.h"
27#include "cintltst.h"
28#include "capitst.h"
29#include "ccolltst.h"
374ca955 30#include "putilimp.h"
b75a7d8f
A
31
32static void TestAttribute(void);
374ca955 33 int TestBufferSize(); /* defined in "colutil.c" */
b75a7d8f
A
34
35
374ca955
A
36
37
b75a7d8f
A
38/* next two function is modified from "i18n/ucol.cpp" to avoid include "ucol_imp.h" */
39static void uprv_appendByteToHexString(char *dst, uint8_t val) {
40 uint32_t len = (uint32_t)strlen(dst);
41 sprintf(dst+len, "%02X", val);
42}
43
44static char* U_EXPORT2 ucol_sortKeyToString(const UCollator *coll, const uint8_t *sortkey, char *buffer, uint32_t *len) {
45 int32_t strength = UCOL_PRIMARY;
46 uint32_t res_size = 0;
47 UBool doneCase = FALSE;
48
49 char *current = buffer;
50 const uint8_t *currentSk = sortkey;
51
52 UErrorCode error_code = U_ZERO_ERROR;
53
54 strcpy(current, "[");
55
56 while(strength <= UCOL_QUATERNARY && strength <= ucol_getAttribute(coll,UCOL_STRENGTH, &error_code)) {
57 if(U_FAILURE(error_code)) {
58 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code));
59 }
60 if(strength > UCOL_PRIMARY) {
61 strcat(current, " . ");
62 }
63 while(*currentSk != 0x01 && *currentSk != 0x00) { /* print a level */
64 uprv_appendByteToHexString(current, *currentSk++);
65 strcat(current, " ");
66 }
67 if(ucol_getAttribute(coll,UCOL_CASE_LEVEL, &error_code) == UCOL_ON && strength == UCOL_SECONDARY && doneCase == FALSE) {
68 doneCase = TRUE;
69 } else if(ucol_getAttribute(coll,UCOL_CASE_LEVEL, &error_code) == UCOL_OFF || doneCase == TRUE || strength != UCOL_SECONDARY) {
70 strength ++;
71 }
72 if(U_FAILURE(error_code)) {
73 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code));
74 }
75 uprv_appendByteToHexString(current, *currentSk++); /* This should print '01' */
76 if(strength == UCOL_QUATERNARY && ucol_getAttribute(coll,UCOL_ALTERNATE_HANDLING, &error_code) == UCOL_NON_IGNORABLE) {
77 break;
78 }
79 }
80
81 if(ucol_getAttribute(coll,UCOL_STRENGTH, &error_code) == UCOL_IDENTICAL) {
82 strcat(current, " . ");
83 while(*currentSk != 0) {
84 uprv_appendByteToHexString(current, *currentSk++);
85 strcat(current, " ");
86 }
87
88 uprv_appendByteToHexString(current, *currentSk++);
89 }
90 if(U_FAILURE(error_code)) {
91 log_err("ucol_getAttribute returned error: %s\n", u_errorName(error_code));
92 }
93 strcat(current, "]");
94
95 if(res_size > *len) {
96 return NULL;
97 }
98
99 return buffer;
100}
101/* end of avoid include "ucol_imp.h" */
102
103
104void addCollAPITest(TestNode** root)
105{
106 /* WEIVTODO: return tests here */
107 addTest(root, &TestProperty, "tscoll/capitst/TestProperty");
108 addTest(root, &TestRuleBasedColl, "tscoll/capitst/TestRuleBasedColl");
109 addTest(root, &TestCompare, "tscoll/capitst/TestCompare");
110 addTest(root, &TestSortKey, "tscoll/capitst/TestSortKey");
111 addTest(root, &TestHashCode, "tscoll/capitst/TestHashCode");
112 addTest(root, &TestElemIter, "tscoll/capitst/TestElemIter");
113 addTest(root, &TestGetAll, "tscoll/capitst/TestGetAll");
114 /*addTest(root, &TestGetDefaultRules, "tscoll/capitst/TestGetDefaultRules");*/
115 addTest(root, &TestDecomposition, "tscoll/capitst/TestDecomposition");
116 addTest(root, &TestSafeClone, "tscoll/capitst/TestSafeClone");
117 addTest(root, &TestGetSetAttr, "tscoll/capitst/TestGetSetAttr");
118 addTest(root, &TestBounds, "tscoll/capitst/TestBounds");
119 addTest(root, &TestGetLocale, "tscoll/capitst/TestGetLocale");
120 addTest(root, &TestSortKeyBufferOverrun, "tscoll/capitst/TestSortKeyBufferOverrun");
121 addTest(root, &TestAttribute, "tscoll/capitst/TestAttribute");
122 addTest(root, &TestGetTailoredSet, "tscoll/capitst/TestGetTailoredSet");
123 addTest(root, &TestMergeSortKeys, "tscoll/capitst/TestMergeSortKeys");
374ca955
A
124 addTest(root, &TestShortString, "tscoll/capitst/TestShortString");
125 addTest(root, &TestGetContractionsAndUnsafes, "tscoll/capitst/TestGetContractionsAndUnsafes");
b75a7d8f
A
126}
127
128void TestGetSetAttr(void) {
129 UErrorCode status = U_ZERO_ERROR;
130 UCollator *coll = ucol_open(NULL, &status);
131 struct attrTest {
132 UColAttribute att;
133 UColAttributeValue val[5];
134 uint32_t valueSize;
135 UColAttributeValue nonValue;
136 } attrs[] = {
137 {UCOL_FRENCH_COLLATION, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},
138 {UCOL_ALTERNATE_HANDLING, {UCOL_NON_IGNORABLE, UCOL_SHIFTED}, 2, UCOL_OFF},/* attribute for handling variable elements*/
139 {UCOL_CASE_FIRST, {UCOL_OFF, UCOL_LOWER_FIRST, UCOL_UPPER_FIRST}, 3, UCOL_SHIFTED},/* who goes first, lower case or uppercase */
140 {UCOL_CASE_LEVEL, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* do we have an extra case level */
141 {UCOL_NORMALIZATION_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* attribute for normalization */
142 {UCOL_DECOMPOSITION_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},
143 {UCOL_STRENGTH, {UCOL_PRIMARY, UCOL_SECONDARY, UCOL_TERTIARY, UCOL_QUATERNARY, UCOL_IDENTICAL}, 5, UCOL_SHIFTED},/* attribute for strength */
144 {UCOL_HIRAGANA_QUATERNARY_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* when turned on, this attribute */
145 };
146 UColAttribute currAttr;
147 UColAttributeValue value;
148 uint32_t i = 0, j = 0;
149
150 for(i = 0; i<sizeof(attrs)/sizeof(attrs[0]); i++) {
151 currAttr = attrs[i].att;
152 ucol_setAttribute(coll, currAttr, UCOL_DEFAULT, &status);
153 if(U_FAILURE(status)) {
154 log_err("ucol_setAttribute with the default value returned error: %s\n", u_errorName(status));
155 break;
156 }
157 value = ucol_getAttribute(coll, currAttr, &status);
158 if(U_FAILURE(status)) {
159 log_err("ucol_getAttribute returned error: %s\n", u_errorName(status));
160 break;
161 }
162 for(j = 0; j<attrs[i].valueSize; j++) {
163 ucol_setAttribute(coll, currAttr, attrs[i].val[j], &status);
164 if(U_FAILURE(status)) {
165 log_err("ucol_setAttribute with the value %i returned error: %s\n", attrs[i].val[j], u_errorName(status));
166 break;
167 }
168 }
169 status = U_ZERO_ERROR;
170 ucol_setAttribute(coll, currAttr, attrs[i].nonValue, &status);
171 if(U_SUCCESS(status)) {
172 log_err("ucol_setAttribute with the bad value didn't return an error\n");
173 break;
174 }
175 status = U_ZERO_ERROR;
176
177 ucol_setAttribute(coll, currAttr, value, &status);
178 if(U_FAILURE(status)) {
179 log_err("ucol_setAttribute with the default valuereturned error: %s\n", u_errorName(status));
180 break;
181 }
182
183 }
184 status = U_ZERO_ERROR;
185 value = ucol_getAttribute(coll, UCOL_ATTRIBUTE_COUNT, &status);
186 if(U_SUCCESS(status)) {
187 log_err("ucol_getAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
188 }
189 status = U_ZERO_ERROR;
190 ucol_setAttribute(coll, UCOL_ATTRIBUTE_COUNT, UCOL_DEFAULT, &status);
191 if(U_SUCCESS(status)) {
192 log_err("ucol_setAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
193 }
194 status = U_ZERO_ERROR;
195 ucol_close(coll);
196}
197
198
199static void doAssert(int condition, const char *message)
200{
201 if (condition==0) {
202 log_err("ERROR : %s\n", message);
203 }
204}
205
206#if 0
207/* We don't have default rules, at least not in the previous sense */
208void TestGetDefaultRules(){
209 uint32_t size=0;
210 UErrorCode status=U_ZERO_ERROR;
211 UCollator *coll=NULL;
212 int32_t len1 = 0, len2=0;
213 uint8_t *binColData = NULL;
214
215 UResourceBundle *res = NULL;
216 UResourceBundle *binColl = NULL;
217 uint8_t *binResult = NULL;
218
219
220 const UChar * defaultRulesArray=ucol_getDefaultRulesArray(&size);
221 log_verbose("Test the function ucol_getDefaultRulesArray()\n");
222
223 coll = ucol_openRules(defaultRulesArray, size, UCOL_ON, UCOL_PRIMARY, &status);
224 if(U_SUCCESS(status) && coll !=NULL) {
225 binColData = (uint8_t*)ucol_cloneRuleData(coll, &len1, &status);
226
227 }
228
229
230 status=U_ZERO_ERROR;
231 res=ures_open(NULL, "root", &status);
232 if(U_FAILURE(status)){
233 log_err("ERROR: Failed to get resource for \"root Locale\" with %s", myErrorName(status));
234 return;
235 }
236 binColl=ures_getByKey(res, "%%Collation", binColl, &status);
237 if(U_SUCCESS(status)){
238 binResult=(uint8_t*)ures_getBinary(binColl, &len2, &status);
239 if(U_FAILURE(status)){
240 log_err("ERROR: ures_getBinary() failed\n");
241 }
242 }else{
243 log_err("ERROR: ures_getByKey(locale(default), %%Collation) failed");
244 }
245
246
247 if(len1 != len2){
248 log_err("Error: ucol_getDefaultRulesArray() failed to return the correct length.\n");
249 }
250 if(memcmp(binColData, binResult, len1) != 0){
251 log_err("Error: ucol_getDefaultRulesArray() failed\n");
252 }
253
254 free(binColData);
255 ures_close(binColl);
256 ures_close(res);
257 ucol_close(coll);
258
259}
260#endif
261
262/* Collator Properties
263 ucol_open, ucol_strcoll, getStrength/setStrength
264 getDecomposition/setDecomposition, getDisplayName*/
265void TestProperty()
266{
267 UCollator *col, *ruled;
268 UChar *disName;
269 int32_t len = 0, i = 0;
270 UChar *source, *target;
271 int32_t tempLength;
272 UErrorCode status = U_ZERO_ERROR;
273 /*
274 All the collations have the same version in an ICU
275 version.
276 ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};
277 ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};
278 ICU 2.2 currVersionArray = {0x21, 0x40, 0x04, 0x04};
279 ICU 2.4 currVersionArray = {0x21, 0x40, 0x04, 0x04};
374ca955 280 ICU 2.6 currVersionArray = {0x21, 0x40, 0x03, 0x03};
b75a7d8f 281 */
374ca955
A
282 UVersionInfo currVersionArray = {0x29, 0x80, 0x00, 0x04};
283 UVersionInfo currUCAVersionArray = {4, 0, 0, 0};
284 UVersionInfo versionArray = {0, 0, 0, 0};
285 UVersionInfo versionUCAArray = {0, 0, 0, 0};
b75a7d8f
A
286
287 log_verbose("The property tests begin : \n");
288 log_verbose("Test ucol_strcoll : \n");
289 col = ucol_open("en_US", &status);
290 if (U_FAILURE(status)) {
291 log_err("Default Collator creation failed.: %s\n", myErrorName(status));
292 return;
293 }
294
295 ucol_getVersion(col, versionArray);
296 for (i=0; i<4; ++i) {
297 if (versionArray[i] != currVersionArray[i]) {
298 log_err("Testing ucol_getVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
299 versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
300 break;
301 }
302 }
303
374ca955
A
304 ucol_getUCAVersion(col, versionUCAArray);
305 for (i=0; i<4; ++i) {
306 if (versionUCAArray[i] != currUCAVersionArray[i]) {
307 log_err("Testing ucol_getUCAVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
308 versionUCAArray[0], versionUCAArray[1], versionUCAArray[2], versionUCAArray[3]);
309 break;
310 }
311 }
312
b75a7d8f
A
313 source=(UChar*)malloc(sizeof(UChar) * 12);
314 target=(UChar*)malloc(sizeof(UChar) * 12);
315
316
317 u_uastrcpy(source, "ab");
318 u_uastrcpy(target, "abc");
319
320 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS), "ab < abc comparison failed");
321
322 u_uastrcpy(source, "ab");
323 u_uastrcpy(target, "AB");
324
325 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS), "ab < AB comparison failed");
326/* u_uastrcpy(source, "black-bird");
327 u_uastrcpy(target, "blackbird"); */
328 u_uastrcpy(target, "black-bird");
329 u_uastrcpy(source, "blackbird");
330
331 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_GREATER),
332 "black-bird > blackbird comparison failed");
333 u_uastrcpy(source, "black bird");
334 u_uastrcpy(target, "black-bird");
335 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_LESS),
336 "black bird < black-bird comparison failed");
337 u_uastrcpy(source, "Hello");
338 u_uastrcpy(target, "hello");
339
340 doAssert((ucol_strcoll(col, source, u_strlen(source), target, u_strlen(target)) == UCOL_GREATER),
341 "Hello > hello comparison failed");
342 free(source);
343 free(target);
344 log_verbose("Test ucol_strcoll ends.\n");
345
346 log_verbose("testing ucol_getStrength() method ...\n");
347 doAssert( (ucol_getStrength(col) == UCOL_TERTIARY), "collation object has the wrong strength");
348 doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
349
350 log_verbose("testing ucol_setStrength() method ...\n");
351 ucol_setStrength(col, UCOL_SECONDARY);
352 doAssert( (ucol_getStrength(col) != UCOL_TERTIARY), "collation object's strength is secondary difference");
353 doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
354 doAssert( (ucol_getStrength(col) == UCOL_SECONDARY), "collation object has the wrong strength");
355
356
357 log_verbose("Get display name for the default collation in German : \n");
358
359 len=ucol_getDisplayName("en_US", "de_DE", NULL, 0, &status);
360 if(status==U_BUFFER_OVERFLOW_ERROR){
361 status=U_ZERO_ERROR;
362 disName=(UChar*)malloc(sizeof(UChar) * (len+1));
363 ucol_getDisplayName("en_US", "de_DE", disName, len+1, &status);
364 log_verbose("the display name for default collation in german: %s\n", austrdup(disName) );
365 free(disName);
366 }
367 if(U_FAILURE(status)){
368 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status));
369 return;
370 }
371 log_verbose("Default collation getDisplayName ended.\n");
372
373 ruled = ucol_open("da_DK", &status);
374 log_verbose("ucol_getRules() testing ...\n");
375 ucol_getRules(ruled, &tempLength);
376 doAssert( tempLength != 0, "getRules() result incorrect" );
377 log_verbose("getRules tests end.\n");
378 {
379 UChar *buffer = (UChar *)malloc(200000*sizeof(UChar));
380 int32_t bufLen = 200000;
381 buffer[0] = '\0';
382 log_verbose("ucol_getRulesEx() testing ...\n");
383 tempLength = ucol_getRulesEx(col,UCOL_TAILORING_ONLY,buffer,bufLen );
374ca955 384 doAssert( tempLength == 0x0a, "getRulesEx() result incorrect" );
b75a7d8f
A
385 log_verbose("getRules tests end.\n");
386
387 log_verbose("ucol_getRulesEx() testing ...\n");
388 tempLength=ucol_getRulesEx(col,UCOL_FULL_RULES,buffer,bufLen );
389 doAssert( tempLength != 0, "getRulesEx() result incorrect" );
390 log_verbose("getRules tests end.\n");
391 free(buffer);
392 }
393 ucol_close(ruled);
394 ucol_close(col);
395
396 log_verbose("open an collator for french locale");
397 col = ucol_open("fr_FR", &status);
398 if (U_FAILURE(status)) {
399 log_err("ERROR: Creating French collation failed.: %s\n", myErrorName(status));
400 return;
401 }
402 ucol_setStrength(col, UCOL_PRIMARY);
403 log_verbose("testing ucol_getStrength() method again ...\n");
404 doAssert( (ucol_getStrength(col) != UCOL_TERTIARY), "collation object has the wrong strength");
405 doAssert( (ucol_getStrength(col) == UCOL_PRIMARY), "collation object's strength is not primary difference");
406
407 log_verbose("testing French ucol_setStrength() method ...\n");
408 ucol_setStrength(col, UCOL_TERTIARY);
409 doAssert( (ucol_getStrength(col) == UCOL_TERTIARY), "collation object's strength is not tertiary difference");
410 doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
411 doAssert( (ucol_getStrength(col) != UCOL_SECONDARY), "collation object's strength is secondary difference");
412 ucol_close(col);
413
414 log_verbose("Get display name for the french collation in english : \n");
415 len=ucol_getDisplayName("fr_FR", "en_US", NULL, 0, &status);
416 if(status==U_BUFFER_OVERFLOW_ERROR){
417 status=U_ZERO_ERROR;
418 disName=(UChar*)malloc(sizeof(UChar) * (len+1));
419 ucol_getDisplayName("fr_FR", "en_US", disName, len+1, &status);
420 log_verbose("the display name for french collation in english: %s\n", austrdup(disName) );
421 free(disName);
422 }
423 if(U_FAILURE(status)){
424 log_err("ERROR: in getDisplayName: %s\n", myErrorName(status));
425 return;
426 }
427 log_verbose("Default collation getDisplayName ended.\n");
428
429}
430
431/* Test RuleBasedCollator and getRules*/
432void TestRuleBasedColl()
433{
434 UCollator *col1, *col2, *col3, *col4;
435 UCollationElements *iter1, *iter2;
436 UChar ruleset1[60];
437 UChar ruleset2[50];
438 UChar teststr[10];
439 UChar teststr2[10];
440 const UChar *rule1, *rule2, *rule3, *rule4;
441 int32_t tempLength;
442 UErrorCode status = U_ZERO_ERROR;
443 u_uastrcpy(ruleset1, "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
444 u_uastrcpy(ruleset2, "&9 < a, A < b, B < c, C < d, D, e, E");
445
374ca955 446
b75a7d8f
A
447 col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL,&status);
448 if (U_FAILURE(status)) {
449 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
450 return;
451 }
452 else
453 log_verbose("PASS: RuleBased Collator creation passed\n");
454
455 status = U_ZERO_ERROR;
456 col2 = ucol_openRules(ruleset2, u_strlen(ruleset2), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
457 if (U_FAILURE(status)) {
458 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
459 return;
460 }
461 else
462 log_verbose("PASS: RuleBased Collator creation passed\n");
463
464
465 status = U_ZERO_ERROR;
466 col3= ucol_open(NULL, &status);
467 if (U_FAILURE(status)) {
468 log_err("Default Collator creation failed.: %s\n", myErrorName(status));
469 return;
470 }
471 else
472 log_verbose("PASS: Default Collator creation passed\n");
473
474 rule1 = ucol_getRules(col1, &tempLength);
475 rule2 = ucol_getRules(col2, &tempLength);
476 rule3 = ucol_getRules(col3, &tempLength);
477
478 doAssert((u_strcmp(rule1, rule2) != 0), "Default collator getRules failed");
479 doAssert((u_strcmp(rule2, rule3) != 0), "Default collator getRules failed");
480 doAssert((u_strcmp(rule1, rule3) != 0), "Default collator getRules failed");
481
482 col4=ucol_openRules(rule2, u_strlen(rule2), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
483 if (U_FAILURE(status)) {
484 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
485 return;
486 }
487 rule4= ucol_getRules(col4, &tempLength);
488 doAssert((u_strcmp(rule2, rule4) == 0), "Default collator getRules failed");
489
490 ucol_close(col1);
491 ucol_close(col2);
492 ucol_close(col3);
493 ucol_close(col4);
494
495 /* tests that modifier ! is always ignored */
496 u_uastrcpy(ruleset1, "!&a<b");
497 teststr[0] = 0x0e40;
498 teststr[1] = 0x0e01;
499 teststr[2] = 0x0e2d;
500 col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
501 if (U_FAILURE(status)) {
502 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
503 return;
504 }
505 col2 = ucol_open("en_US", &status);
506 if (U_FAILURE(status)) {
507 log_err("en_US Collator creation failed.: %s\n", myErrorName(status));
508 return;
509 }
510 iter1 = ucol_openElements(col1, teststr, 3, &status);
511 iter2 = ucol_openElements(col2, teststr, 3, &status);
512 if(U_FAILURE(status)) {
513 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status));
514 return;
515 }
516 while (TRUE) {
517 /* testing with en since thai has its own tailoring */
518 uint32_t ce = ucol_next(iter1, &status);
519 uint32_t ce2 = ucol_next(iter2, &status);
374ca955 520 if(U_FAILURE(status)) {
b75a7d8f
A
521 log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status));
522 return;
374ca955 523 }
b75a7d8f
A
524 if (ce2 != ce) {
525 log_err("! modifier test failed");
526 }
527 if (ce == UCOL_NULLORDER) {
528 break;
529 }
530 }
531 ucol_closeElements(iter1);
532 ucol_closeElements(iter2);
533 ucol_close(col1);
534 ucol_close(col2);
535 /* test that we can start a rule without a & or < */
536 u_uastrcpy(ruleset1, "< z < a");
537 col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
538 if (U_FAILURE(status)) {
539 log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
540 return;
541 }
542 u_uastrcpy(teststr, "z");
543 u_uastrcpy(teststr2, "a");
544 if (ucol_greaterOrEqual(col1, teststr, 1, teststr2, 1)) {
545 log_err("Rule \"z < a\" fails");
546 }
547 ucol_close(col1);
548}
549
550void TestCompare()
551{
552 UErrorCode status = U_ZERO_ERROR;
553 UCollator *col;
554 UChar* test1;
555 UChar* test2;
556
557 log_verbose("The compare tests begin : \n");
558 status=U_ZERO_ERROR;
559 col = ucol_open("en_US", &status);
560 if(U_FAILURE(status)) {
561 log_err("ucal_open() collation creation failed.: %s\n", myErrorName(status));
562 return;
563 }
564 test1=(UChar*)malloc(sizeof(UChar) * 6);
565 test2=(UChar*)malloc(sizeof(UChar) * 6);
566 u_uastrcpy(test1, "Abcda");
567 u_uastrcpy(test2, "abcda");
568
569 log_verbose("Use tertiary comparison level testing ....\n");
570
571 doAssert( (!ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" != \"abcda\" ");
572 doAssert( (ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\" ");
573 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\"");
574
575 ucol_setStrength(col, UCOL_SECONDARY);
576 log_verbose("Use secondary comparison level testing ....\n");
577
578 doAssert( (ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\"");
579 doAssert( (!ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
580 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\"");
581
582 ucol_setStrength(col, UCOL_PRIMARY);
583 log_verbose("Use primary comparison level testing ....\n");
584
585 doAssert( (ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
586 doAssert( (!ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
587 doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
588
589
590 log_verbose("The compare tests end.\n");
591 ucol_close(col);
592 free(test1);
593 free(test2);
594
595}
596/*
597---------------------------------------------
598 tests decomposition setting
599*/
600void TestDecomposition() {
601 UErrorCode status = U_ZERO_ERROR;
602 UCollator *en_US, *el_GR, *vi_VN;
603 en_US = ucol_open("en_US", &status);
604 el_GR = ucol_open("el_GR", &status);
605 vi_VN = ucol_open("vi_VN", &status);
606
607 if (U_FAILURE(status)) {
608 log_err("ERROR: collation creation failed.: %s\n", myErrorName(status));
609 return;
610 }
611
612 if (ucol_getAttribute(vi_VN, UCOL_NORMALIZATION_MODE, &status) != UCOL_ON ||
613 U_FAILURE(status))
614 {
615 log_err("ERROR: vi_VN collation did not have cannonical decomposition for normalization!\n");
616 }
617
618 status = U_ZERO_ERROR;
619 if (ucol_getAttribute(el_GR, UCOL_NORMALIZATION_MODE, &status) != UCOL_ON ||
620 U_FAILURE(status))
621 {
622 log_err("ERROR: el_GR collation did not have cannonical decomposition for normalization!\n");
623 }
624
625 status = U_ZERO_ERROR;
626 if (ucol_getAttribute(en_US, UCOL_NORMALIZATION_MODE, &status) != UCOL_OFF ||
627 U_FAILURE(status))
628 {
629 log_err("ERROR: en_US collation had cannonical decomposition for normalization!\n");
630 }
631
632 ucol_close(en_US);
633 ucol_close(el_GR);
634 ucol_close(vi_VN);
635}
636
637#define CLONETEST_COLLATOR_COUNT 3
638
639void TestSafeClone() {
640 UChar* test1;
641 UChar* test2;
642 UCollator * someCollators [CLONETEST_COLLATOR_COUNT];
643 UCollator * someClonedCollators [CLONETEST_COLLATOR_COUNT];
644 UCollator * col;
645 UErrorCode err = U_ZERO_ERROR;
646 int8_t testSize = 6; /* Leave this here to test buffer alingment in memory*/
647 uint8_t buffer [CLONETEST_COLLATOR_COUNT] [U_COL_SAFECLONE_BUFFERSIZE];
648 int32_t bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
649 int index;
650
651 if (TestBufferSize()) {
652 log_err("U_COL_SAFECLONE_BUFFERSIZE should be larger than sizeof(UCollator)\n");
653 return;
654 }
655
656 test1=(UChar*)malloc(sizeof(UChar) * testSize);
657 test2=(UChar*)malloc(sizeof(UChar) * testSize);
658 u_uastrcpy(test1, "abCda");
659 u_uastrcpy(test2, "abcda");
660
661 /* one default collator & two complex ones */
662 someCollators[0] = ucol_open("en_US", &err);
663 someCollators[1] = ucol_open("ko", &err);
664 someCollators[2] = ucol_open("ja_JP", &err);
665 if(U_FAILURE(err)) {
666 log_data_err("Couldn't open one or more collators\n");
667 return;
668 }
669
670 /* Check the various error & informational states: */
671
672 /* Null status - just returns NULL */
673 if (0 != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, 0))
674 {
675 log_err("FAIL: Cloned Collator failed to deal correctly with null status\n");
676 }
677 /* error status - should return 0 & keep error the same */
678 err = U_MEMORY_ALLOCATION_ERROR;
679 if (0 != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err) || err != U_MEMORY_ALLOCATION_ERROR)
680 {
681 log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
682 }
683 err = U_ZERO_ERROR;
684
685 /* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
686 if (0 != ucol_safeClone(someCollators[0], buffer[0], 0, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
687 {
688 log_err("FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer\n");
689 }
690 err = U_ZERO_ERROR;
691
692 /* buffer size pointer is 0 - fill in pbufferSize with a size */
693 bufferSize = 0;
694 if (0 != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err) || U_FAILURE(err) || bufferSize <= 0)
695 {
696 log_err("FAIL: Cloned Collator failed a sizing request ('preflighting')\n");
697 }
698 /* Verify our define is large enough */
699 if (U_COL_SAFECLONE_BUFFERSIZE < bufferSize)
700 {
701 log_err("FAIL: Pre-calculated buffer size is too small\n");
702 }
703 /* Verify we can use this run-time calculated size */
704 if (0 == (col = ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err)) || U_FAILURE(err))
705 {
706 log_err("FAIL: Collator can't be cloned with run-time size\n");
707 }
708 if (col) ucol_close(col);
709 /* size one byte too small - should allocate & let us know */
710 --bufferSize;
711 if (0 == (col = ucol_safeClone(someCollators[0], 0, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
712 {
713 log_err("FAIL: Cloned Collator failed to deal correctly with too-small buffer size\n");
714 }
715 if (col) ucol_close(col);
716 err = U_ZERO_ERROR;
717 bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
718
719
720 /* Null buffer pointer - return Collator & set error to U_SAFECLONE_ALLOCATED_ERROR */
721 if (0 == (col = ucol_safeClone(someCollators[0], 0, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
722 {
723 log_err("FAIL: Cloned Collator failed to deal correctly with null buffer pointer\n");
724 }
725 if (col) ucol_close(col);
726 err = U_ZERO_ERROR;
727
728 /* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
729 if (0 != ucol_safeClone(0, buffer[0], &bufferSize, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
730 {
731 log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
732 }
733
734 err = U_ZERO_ERROR;
735
736 /* change orig & clone & make sure they are independent */
737
738 for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++)
739 {
740 bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
741 someClonedCollators[index] = ucol_safeClone(someCollators[index], buffer[index], &bufferSize, &err);
742
743 ucol_setStrength(someClonedCollators[index], UCOL_TERTIARY);
744 ucol_setStrength(someCollators[index], UCOL_PRIMARY);
745 ucol_setAttribute(someClonedCollators[index], UCOL_CASE_LEVEL, UCOL_OFF, &err);
746 ucol_setAttribute(someCollators[index], UCOL_CASE_LEVEL, UCOL_OFF, &err);
747
748 doAssert( (ucol_greater(someClonedCollators[index], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abCda\" >>> \"abcda\" ");
749 doAssert( (ucol_equal(someCollators[index], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abcda\" == \"abCda\"");
750
751 ucol_close(someClonedCollators[index]);
752 ucol_close(someCollators[index]);
753 }
754 free(test1);
755 free(test2);
756}
757
758/*
759----------------------------------------------------------------------------
760 ctor -- Tests the getSortKey
761*/
762void TestSortKey()
763{
764 uint8_t *sortk1 = NULL, *sortk2 = NULL, *sortk3 = NULL, *sortkEmpty = NULL;
765 uint8_t sortk2_compat[] = {
374ca955
A
766 /* 2.6.1 key */
767 0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01,
768 0x09, 0x01, 0x09, 0x01, 0x25, 0x01,
769 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
b75a7d8f 770 /* 2.2 key */
374ca955 771 /*0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01, 0x09, 0x01, 0x09, 0x01, 0x1C, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
b75a7d8f
A
772 /* 2.0 key */
773 /*0x19, 0x1B, 0x1D, 0x1F, 0x19, 0x01, 0x09, 0x01, 0x09, 0x01, 0x18, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
774 /* 1.8.1 key.*/
775 /*0x19, 0x1B, 0x1D, 0x1F, 0x19, 0x01, 0x0A, 0x01, 0x0A, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
776 /*this is a 1.8 sortkey */
777 /*0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00*/
778 /*this is a 1.7 sortkey */
779 /*0x02, 0x54, 0x02, 0x55, 0x02, 0x56, 0x02, 0x57, 0x02, 0x54, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00*/
780 /* this is a 1.6 sortkey */
781 /*0x00, 0x53, 0x00, 0x54, 0x00, 0x55, 0x00, 0x56, 0x00, 0x53, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00*/
782 };
783
784 int32_t sortklen, osortklen;
785 uint32_t toStringLen=0;
786 UCollator *col;
787 UChar *test1, *test2, *test3;
788 UErrorCode status = U_ZERO_ERROR;
789 char toStringBuffer[256], *resultP;
790
791
792 uint8_t s1[] = { 0x9f, 0x00 };
793 uint8_t s2[] = { 0x61, 0x00 };
794 int strcmpResult;
795
796 strcmpResult = strcmp((const char *)s1, (const char *)s2);
797 log_verbose("strcmp(0x9f..., 0x61...) = %d\n", strcmpResult);
798
799 if(strcmpResult <= 0) {
800 log_err("ERR: expected strcmp(\"9f 00\", \"61 00\") to be >=0 (GREATER).. got %d. Calling strcmp() for sortkeys may not work! \n",
801 strcmpResult);
802 }
803
804
805 log_verbose("testing SortKey begins...\n");
806 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
807 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
808 /* col = ucol_open(NULL, &status); */
809 col = ucol_open("en_US", &status);
810 if (U_FAILURE(status)) {
811 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status));
812 return;
813 }
814
815
816 if(ucol_getStrength(col) != UCOL_DEFAULT_STRENGTH)
817 {
818 log_err("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
819 }
820 /* Need to use identical strength */
821 ucol_setAttribute(col, UCOL_STRENGTH, UCOL_IDENTICAL, &status);
822
823 test1=(UChar*)malloc(sizeof(UChar) * 6);
824 test2=(UChar*)malloc(sizeof(UChar) * 6);
825 test3=(UChar*)malloc(sizeof(UChar) * 6);
826
827 memset(test1,0xFE, sizeof(UChar)*6);
828 memset(test2,0xFE, sizeof(UChar)*6);
829 memset(test3,0xFE, sizeof(UChar)*6);
830
831
832 u_uastrcpy(test1, "Abcda");
833 u_uastrcpy(test2, "abcda");
834 u_uastrcpy(test3, "abcda");
835
836 log_verbose("Use tertiary comparison level testing ....\n");
837
838 sortklen=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
839 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
840 memset(sortk1,0xFE, sortklen);
841 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortklen+1);
842
843 sortklen=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
844 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
845 memset(sortk2,0xFE, sortklen);
846 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortklen+1);
847
848 osortklen = sortklen;
849 sortklen=ucol_getSortKey(col, test2, u_strlen(test3), NULL, 0);
850 sortk3=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
851 memset(sortk3,0xFE, sortklen);
852 ucol_getSortKey(col, test2, u_strlen(test2), sortk3, sortklen+1);
853
854 doAssert( (sortklen == osortklen), "Sortkey length should be the same (abcda, abcda)");
855
856 doAssert( (memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" > \"abcda\"");
857 doAssert( (memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" < \"Abcda\"");
858 doAssert( (memcmp(sortk2, sortk3, sortklen) == 0), "Result should be \"abcda\" == \"abcda\"");
859
860 doAssert( (memcmp(sortk2, sortk2_compat, sortklen) == 0), "Binary format for 'abcda' sortkey different!");
861
862 resultP = ucol_sortKeyToString(col, sortk2_compat, toStringBuffer, &toStringLen);
863 doAssert( (resultP != 0), "sortKeyToString failed!");
864
865#if 1 /* verobse log of sortkeys */
866 {
867 char junk2[1000];
868 char junk3[1000];
869 int i;
870
871 strcpy(junk2, "abcda[2] ");
872 strcpy(junk3, " abcda[3] ");
873
874 for(i=0;i<sortklen;i++)
875 {
876 sprintf(junk2+strlen(junk2), "%02X ",(int)( 0xFF & sortk2[i]));
877 sprintf(junk3+strlen(junk3), "%02X ",(int)( 0xFF & sortk3[i]));
878 }
879
880 log_verbose("%s\n", junk2);
881 log_verbose("%s\n", junk3);
882 }
883#endif
884
885 free(sortk1);
886 free(sortk2);
887 free(sortk3);
888
889 log_verbose("Use secondary comparision level testing ...\n");
890 ucol_setStrength(col, UCOL_SECONDARY);
891 sortklen=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
892 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
893 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortklen+1);
894 sortklen=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
895 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortklen+1));
896 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortklen+1);
897
898 doAssert( !(memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" == \"abcda\"");
899 doAssert( !(memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" == \"Abcda\"");
900 doAssert( (memcmp(sortk1, sortk2, sortklen) == 0), "Result should be \"abcda\" == \"abcda\"");
901
902 log_verbose("getting sortkey for an empty string\n");
903 ucol_setAttribute(col, UCOL_STRENGTH, UCOL_TERTIARY, &status);
904 sortklen = ucol_getSortKey(col, test1, 0, NULL, 0);
905 sortkEmpty = (uint8_t*)malloc(sizeof(uint8_t) * sortklen+1);
906 sortklen = ucol_getSortKey(col, test1, 0, sortkEmpty, sortklen+1);
907 if(sortklen != 3 || sortkEmpty[0] != 1 || sortkEmpty[0] != 1 || sortkEmpty[2] != 0) {
908 log_err("Empty string generated wrong sortkey!\n");
909 }
910 free(sortkEmpty);
911
912 log_verbose("testing passing invalid string\n");
913 sortklen = ucol_getSortKey(col, NULL, 0, NULL, 0);
914 if(sortklen != 0) {
915 log_err("Invalid string didn't return sortkey size of 0\n");
916 }
917
918
919 log_verbose("testing sortkey ends...\n");
920 ucol_close(col);
921 free(test1);
922 free(test2);
923 free(test3);
924 free(sortk1);
925 free(sortk2);
926
927}
928void TestHashCode()
929{
930 uint8_t *sortk1, *sortk2, *sortk3;
931 int32_t sortk1len, sortk2len, sortk3len;
932 UCollator *col;
933 UChar *test1, *test2, *test3;
934 UErrorCode status = U_ZERO_ERROR;
935 log_verbose("testing getHashCode begins...\n");
936 col = ucol_open("en_US", &status);
937 if (U_FAILURE(status)) {
938 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status));
939 return;
940 }
941 test1=(UChar*)malloc(sizeof(UChar) * 6);
942 test2=(UChar*)malloc(sizeof(UChar) * 6);
943 test3=(UChar*)malloc(sizeof(UChar) * 6);
944 u_uastrcpy(test1, "Abcda");
945 u_uastrcpy(test2, "abcda");
946 u_uastrcpy(test3, "abcda");
947
948 log_verbose("Use tertiary comparison level testing ....\n");
949 sortk1len=ucol_getSortKey(col, test1, u_strlen(test1), NULL, 0);
950 sortk1=(uint8_t*)malloc(sizeof(uint8_t) * (sortk1len+1));
951 ucol_getSortKey(col, test1, u_strlen(test1), sortk1, sortk1len+1);
952 sortk2len=ucol_getSortKey(col, test2, u_strlen(test2), NULL, 0);
953 sortk2=(uint8_t*)malloc(sizeof(uint8_t) * (sortk2len+1));
954 ucol_getSortKey(col, test2, u_strlen(test2), sortk2, sortk2len+1);
955 sortk3len=ucol_getSortKey(col, test2, u_strlen(test3), NULL, 0);
956 sortk3=(uint8_t*)malloc(sizeof(uint8_t) * (sortk3len+1));
957 ucol_getSortKey(col, test2, u_strlen(test2), sortk3, sortk3len+1);
958
959
960 log_verbose("ucol_hashCode() testing ...\n");
961
962 doAssert( ucol_keyHashCode(sortk1, sortk1len) != ucol_keyHashCode(sortk2, sortk2len), "Hash test1 result incorrect" );
963 doAssert( !(ucol_keyHashCode(sortk1, sortk1len) == ucol_keyHashCode(sortk2, sortk2len)), "Hash test2 result incorrect" );
964 doAssert( ucol_keyHashCode(sortk2, sortk2len) == ucol_keyHashCode(sortk3, sortk3len), "Hash result not equal" );
965
966 log_verbose("hashCode tests end.\n");
967 ucol_close(col);
968 free(sortk1);
969 free(sortk2);
970 free(sortk3);
971 free(test1);
972 free(test2);
973 free(test3);
974
975
976}
977/*
978 *----------------------------------------------------------------------------
979 * Tests the UCollatorElements API.
980 *
981 */
982void TestElemIter()
983{
984 int32_t offset;
985 int32_t order1, order2, order3;
986 UChar *testString1, *testString2;
987 UCollator *col;
988 UCollationElements *iterator1, *iterator2, *iterator3;
989 UErrorCode status = U_ZERO_ERROR;
990 log_verbose("testing UCollatorElements begins...\n");
991 col = ucol_open("en_US", &status);
992 ucol_setAttribute(col, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
993 if (U_FAILURE(status)) {
994 log_err("ERROR: Default collation creation failed.: %s\n", myErrorName(status));
995 return;
996 }
997
998 testString1=(UChar*)malloc(sizeof(UChar) * 150);
999 testString2=(UChar*)malloc(sizeof(UChar) * 150);
1000 u_uastrcpy(testString1, "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
1001 u_uastrcpy(testString2, "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
1002
1003 log_verbose("Constructors and comparison testing....\n");
1004
1005 iterator1 = ucol_openElements(col, testString1, u_strlen(testString1), &status);
1006 if(U_FAILURE(status)) {
1007 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1008 ucol_close(col);
1009 return;
1010 }
1011 else{ log_verbose("PASS: Default collationElement iterator1 creation passed\n");}
1012
1013 iterator2 = ucol_openElements(col, testString1, u_strlen(testString1), &status);
1014 if(U_FAILURE(status)) {
1015 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1016 ucol_close(col);
1017 return;
1018 }
1019 else{ log_verbose("PASS: Default collationElement iterator2 creation passed\n");}
1020
1021 iterator3 = ucol_openElements(col, testString2, u_strlen(testString2), &status);
1022 if(U_FAILURE(status)) {
1023 log_err("ERROR: Default collationElement iterator creation failed.: %s\n", myErrorName(status));
1024 ucol_close(col);
1025 return;
1026 }
1027 else{ log_verbose("PASS: Default collationElement iterator3 creation passed\n");}
1028
1029 offset=ucol_getOffset(iterator1);
1030 ucol_setOffset(iterator1, 6, &status);
1031 if (U_FAILURE(status)) {
1032 log_err("Error in setOffset for UCollatorElements iterator.: %s\n", myErrorName(status));
1033 return;
1034 }
1035 if(ucol_getOffset(iterator1)==6)
1036 log_verbose("setOffset and getOffset working fine\n");
1037 else{
1038 log_err("error in set and get Offset got %d instead of 6\n", ucol_getOffset(iterator1));
1039 }
1040
1041 ucol_setOffset(iterator1, 0, &status);
1042 order1 = ucol_next(iterator1, &status);
1043 if (U_FAILURE(status)) {
1044 log_err("Somehow ran out of memory stepping through the iterator1.: %s\n", myErrorName(status));
1045 return;
1046 }
1047 order2=ucol_getOffset(iterator2);
1048 doAssert((order1 != order2), "The first iterator advance failed");
1049 order2 = ucol_next(iterator2, &status);
1050 if (U_FAILURE(status)) {
1051 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1052 return;
1053 }
1054 order3 = ucol_next(iterator3, &status);
1055 if (U_FAILURE(status)) {
1056 log_err("Somehow ran out of memory stepping through the iterator3.: %s\n", myErrorName(status));
1057 return;
1058 }
1059
1060 doAssert((order1 == order2), "The second iterator advance failed should be the same as first one");
1061
1062doAssert( (ucol_primaryOrder(order1) == ucol_primaryOrder(order3)), "The primary orders should be identical");
1063doAssert( (ucol_secondaryOrder(order1) == ucol_secondaryOrder(order3)), "The secondary orders should be identical");
1064doAssert( (ucol_tertiaryOrder(order1) == ucol_tertiaryOrder(order3)), "The tertiary orders should be identical");
1065
1066 order1=ucol_next(iterator1, &status);
1067 if (U_FAILURE(status)) {
1068 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1069 return;
1070 }
1071 order3=ucol_next(iterator3, &status);
1072 if (U_FAILURE(status)) {
1073 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1074 return;
1075 }
1076doAssert( (ucol_primaryOrder(order1) == ucol_primaryOrder(order3)), "The primary orders should be identical");
1077doAssert( (ucol_tertiaryOrder(order1) != ucol_tertiaryOrder(order3)), "The tertiary orders should be different");
1078
1079 order1=ucol_next(iterator1, &status);
1080 if (U_FAILURE(status)) {
1081 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1082 return;
1083 }
1084 order3=ucol_next(iterator3, &status);
1085 if (U_FAILURE(status)) {
1086 log_err("Somehow ran out of memory stepping through the iterator2.: %s\n", myErrorName(status));
1087 return;
1088 }
1089 /* this here, my friends, is either pure lunacy or something so obsolete that even it's mother
1090 * doesn't care about it. Essentialy, this test complains if secondary values for 'I' and '_'
1091 * are the same. According to the UCA, this is not true. Therefore, remove the test.
1092 * Besides, if primary strengths for two code points are different, it doesn't matter one bit
1093 * what is the relation between secondary or any other strengths.
1094 * killed by weiv 06/11/2002.
1095 */
1096 /*
1097 doAssert( ((order1 & UCOL_SECONDARYMASK) != (order3 & UCOL_SECONDARYMASK)), "The secondary orders should be different");
1098 */
1099 doAssert( (order1 != UCOL_NULLORDER), "Unexpected end of iterator reached");
1100
1101 free(testString1);
1102 free(testString2);
1103 ucol_closeElements(iterator1);
1104 ucol_closeElements(iterator2);
1105 ucol_closeElements(iterator3);
1106 ucol_close(col);
1107
1108 log_verbose("testing CollationElementIterator ends...\n");
1109}
1110
1111void TestGetLocale() {
1112 UErrorCode status = U_ZERO_ERROR;
1113 const char *rules = "&a<x<y<z";
1114 UChar rlz[256] = {0};
1115 uint32_t rlzLen = u_unescape(rules, rlz, 256);
1116
1117 UCollator *coll = NULL;
1118 const char *locale = NULL;
1119
1120 int32_t i = 0;
1121
374ca955 1122 /* Now that the collation tree is separate, actual==valid at all times. [alan] */
b75a7d8f
A
1123 static const struct {
1124 const char* requestedLocale;
1125 const char* validLocale;
1126 const char* actualLocale;
1127 } testStruct[] = {
1128 { "sr_YU", "sr_YU", "ru" },
374ca955
A
1129 { "sh_YU", "sh_YU", "hr" }, /* this used to be sh, but now sh collation aliases hr */
1130 { "en_BE_FOO", "en_BE", "en_BE" },
b75a7d8f
A
1131 { "fr_FR_NONEXISTANT", "fr_FR", "fr" }
1132 };
1133
1134 /* test opening collators for different locales */
1135 for(i = 0; i<sizeof(testStruct)/sizeof(testStruct[0]); i++) {
1136 status = U_ZERO_ERROR;
1137 coll = ucol_open(testStruct[i].requestedLocale, &status);
1138 if(U_FAILURE(status)) {
1139 log_err("Failed to open collator for %s with %s\n", testStruct[i].requestedLocale, u_errorName(status));
1140 ucol_close(coll);
1141 continue;
1142 }
1143 locale = ucol_getLocale(coll, ULOC_REQUESTED_LOCALE, &status);
1144 if(strcmp(locale, testStruct[i].requestedLocale) != 0) {
1145 log_err("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].requestedLocale, locale);
1146 }
1147 locale = ucol_getLocale(coll, ULOC_VALID_LOCALE, &status);
1148 if(strcmp(locale, testStruct[i].validLocale) != 0) {
1149 log_err("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].validLocale, locale);
1150 }
1151 locale = ucol_getLocale(coll, ULOC_ACTUAL_LOCALE, &status);
1152 if(strcmp(locale, testStruct[i].actualLocale) != 0) {
1153 log_err("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].actualLocale, locale);
1154 }
1155 ucol_close(coll);
1156 }
1157
1158 /* completely non-existant locale for collator should get a default collator */
1159 {
1160 UCollator *defaultColl = ucol_open(NULL, &status);
1161 coll = ucol_open("blahaha", &status);
1162 if(U_SUCCESS(status)) {
1163 if(strcmp(ucol_getLocale(coll, ULOC_REQUESTED_LOCALE, &status), "blahaha")) {
1164 log_err("Nonexisting locale didn't preserve the requested locale\n");
1165 }
1166 if(strcmp(ucol_getLocale(coll, ULOC_VALID_LOCALE, &status),
1167 ucol_getLocale(defaultColl, ULOC_VALID_LOCALE, &status))) {
1168 log_err("Valid locale for nonexisting locale locale collator differs "
1169 "from valid locale for default collator\n");
1170 }
1171 if(strcmp(ucol_getLocale(coll, ULOC_ACTUAL_LOCALE, &status),
1172 ucol_getLocale(defaultColl, ULOC_ACTUAL_LOCALE, &status))) {
1173 log_err("Actual locale for nonexisting locale locale collator differs "
1174 "from actual locale for default collator\n");
1175 }
1176 ucol_close(coll);
1177 ucol_close(defaultColl);
1178 } else {
1179 log_data_err("Couldn't open collators\n");
1180 }
1181 }
1182
1183
1184
1185 /* collator instantiated from rules should have all three locales NULL */
1186 coll = ucol_openRules(rlz, rlzLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
1187 locale = ucol_getLocale(coll, ULOC_REQUESTED_LOCALE, &status);
1188 if(locale != NULL) {
1189 log_err("For collator instantiated from rules, requested locale returned %s instead of NULL\n", locale);
1190 }
1191 locale = ucol_getLocale(coll, ULOC_VALID_LOCALE, &status);
1192 if(locale != NULL) {
1193 log_err("For collator instantiated from rules, valid locale returned %s instead of NULL\n", locale);
1194 }
1195 locale = ucol_getLocale(coll, ULOC_ACTUAL_LOCALE, &status);
1196 if(locale != NULL) {
1197 log_err("For collator instantiated from rules, actual locale returned %s instead of NULL\n", locale);
1198 }
1199 ucol_close(coll);
1200
1201}
1202
1203
1204void TestGetAll()
1205{
1206 int32_t i, count;
1207 count=ucol_countAvailable();
1208 /* use something sensible w/o hardcoding the count */
1209 if(count < 0){
1210 log_err("Error in countAvailable(), it returned %d\n", count);
1211 }
1212 else{
1213 log_verbose("PASS: countAvailable() successful, it returned %d\n", count);
1214 }
1215 for(i=0;i<count;i++)
1216 log_verbose("%s\n", ucol_getAvailable(i));
1217
1218
1219}
1220
1221
1222struct teststruct {
1223 const char *original;
1224 uint8_t key[256];
1225 } ;
1226
1227static int compare_teststruct(const void *string1, const void *string2) {
1228 return(strcmp((const char *)((struct teststruct *)string1)->key, (const char *)((struct teststruct *)string2)->key));
1229}
1230
1231void TestBounds() {
1232 UErrorCode status = U_ZERO_ERROR;
1233
1234 UCollator *coll = ucol_open("sh", &status);
1235
1236 uint8_t sortkey[512], lower[512], upper[512];
1237 UChar buffer[512];
1238
1239 const char *test[] = {
1240 "John Smith",
1241 "JOHN SMITH",
1242 "john SMITH",
1243 "j\\u00F6hn sm\\u00EFth",
1244 "J\\u00F6hn Sm\\u00EFth",
1245 "J\\u00D6HN SM\\u00CFTH",
1246 "john smithsonian",
1247 "John Smithsonian",
1248 };
1249
1250 static struct teststruct tests[] = {
1251 {"\\u010CAKI MIHALJ" } ,
1252 {"\\u010CAKI MIHALJ" } ,
1253 {"\\u010CAKI PIRO\\u0160KA" },
1254{ "\\u010CABAI ANDRIJA" } ,
1255 {"\\u010CABAI LAJO\\u0160" } ,
1256 {"\\u010CABAI MARIJA" } ,
1257 {"\\u010CABAI STEVAN" } ,
1258 {"\\u010CABAI STEVAN" } ,
1259 {"\\u010CABARKAPA BRANKO" } ,
1260 {"\\u010CABARKAPA MILENKO" } ,
1261 {"\\u010CABARKAPA MIROSLAV" } ,
1262 {"\\u010CABARKAPA SIMO" } ,
1263 {"\\u010CABARKAPA STANKO" } ,
1264 {"\\u010CABARKAPA TAMARA" } ,
1265 {"\\u010CABARKAPA TOMA\\u0160" } ,
1266 {"\\u010CABDARI\\u0106 NIKOLA" } ,
1267 {"\\u010CABDARI\\u0106 ZORICA" } ,
1268 {"\\u010CABI NANDOR" } ,
1269 {"\\u010CABOVI\\u0106 MILAN" } ,
1270 {"\\u010CABRADI AGNEZIJA" } ,
1271 {"\\u010CABRADI IVAN" } ,
1272 {"\\u010CABRADI JELENA" } ,
1273 {"\\u010CABRADI LJUBICA" } ,
1274 {"\\u010CABRADI STEVAN" } ,
1275 {"\\u010CABRDA MARTIN" } ,
1276 {"\\u010CABRILO BOGDAN" } ,
1277 {"\\u010CABRILO BRANISLAV" } ,
1278 {"\\u010CABRILO LAZAR" } ,
1279 {"\\u010CABRILO LJUBICA" } ,
1280 {"\\u010CABRILO SPASOJA" } ,
1281 {"\\u010CADE\\u0160 ZDENKA" } ,
1282 {"\\u010CADESKI BLAGOJE" } ,
1283 {"\\u010CADOVSKI VLADIMIR" } ,
1284 {"\\u010CAGLJEVI\\u0106 TOMA" } ,
1285 {"\\u010CAGOROVI\\u0106 VLADIMIR" } ,
1286 {"\\u010CAJA VANKA" } ,
1287 {"\\u010CAJI\\u0106 BOGOLJUB" } ,
1288 {"\\u010CAJI\\u0106 BORISLAV" } ,
1289 {"\\u010CAJI\\u0106 RADOSLAV" } ,
1290 {"\\u010CAK\\u0160IRAN MILADIN" } ,
1291 {"\\u010CAKAN EUGEN" } ,
1292 {"\\u010CAKAN EVGENIJE" } ,
1293 {"\\u010CAKAN IVAN" } ,
1294 {"\\u010CAKAN JULIJAN" } ,
1295 {"\\u010CAKAN MIHAJLO" } ,
1296 {"\\u010CAKAN STEVAN" } ,
1297 {"\\u010CAKAN VLADIMIR" } ,
1298 {"\\u010CAKAN VLADIMIR" } ,
1299 {"\\u010CAKAN VLADIMIR" } ,
1300 {"\\u010CAKARA ANA" } ,
1301 {"\\u010CAKAREVI\\u0106 MOMIR" } ,
1302 {"\\u010CAKAREVI\\u0106 NEDELJKO" } ,
1303 {"\\u010CAKI \\u0160ANDOR" } ,
1304 {"\\u010CAKI AMALIJA" } ,
1305 {"\\u010CAKI ANDRA\\u0160" } ,
1306 {"\\u010CAKI LADISLAV" } ,
1307 {"\\u010CAKI LAJO\\u0160" } ,
1308 {"\\u010CAKI LASLO" } ,
1309 };
1310
1311
1312
1313 int32_t i = 0, j = 0, k = 0, buffSize = 0, skSize = 0, lowerSize = 0, upperSize = 0;
1314 int32_t arraySize = sizeof(tests)/sizeof(tests[0]);
1315
1316 if(U_SUCCESS(status) && coll) {
1317 for(i = 0; i<arraySize; i++) {
1318 buffSize = u_unescape(tests[i].original, buffer, 512);
1319 skSize = ucol_getSortKey(coll, buffer, buffSize, tests[i].key, 512);
1320 }
1321
1322 qsort(tests, arraySize, sizeof(struct teststruct), compare_teststruct);
1323
1324 for(i = 0; i < arraySize-1; i++) {
1325 for(j = i+1; j < arraySize; j++) {
1326 lowerSize = ucol_getBound(tests[i].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1327 upperSize = ucol_getBound(tests[j].key, -1, UCOL_BOUND_UPPER, 1, upper, 512, &status);
1328 for(k = i; k <= j; k++) {
1329 if(strcmp((const char *)lower, (const char *)tests[k].key) > 0) {
1330 log_err("Problem with lower! j = %i (%s vs %s)\n", k, tests[k].original, tests[i].original);
1331 }
1332 if(strcmp((const char *)upper, (const char *)tests[k].key) <= 0) {
1333 log_err("Problem with upper! j = %i (%s vs %s)\n", k, tests[k].original, tests[j].original);
1334 }
1335 }
1336 }
1337 }
1338
1339
1340#if 0
1341 for(i = 0; i < 1000; i++) {
1342 lowerRND = (rand()/(RAND_MAX/arraySize));
1343 upperRND = lowerRND + (rand()/(RAND_MAX/(arraySize-lowerRND)));
1344
1345 lowerSize = ucol_getBound(tests[lowerRND].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1346 upperSize = ucol_getBound(tests[upperRND].key, -1, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
1347
1348 for(j = lowerRND; j<=upperRND; j++) {
1349 if(strcmp(lower, tests[j].key) > 0) {
1350 log_err("Problem with lower! j = %i (%s vs %s)\n", j, tests[j].original, tests[lowerRND].original);
1351 }
1352 if(strcmp(upper, tests[j].key) <= 0) {
1353 log_err("Problem with upper! j = %i (%s vs %s)\n", j, tests[j].original, tests[upperRND].original);
1354 }
1355 }
1356 }
1357#endif
1358
1359
1360
1361
1362
1363 for(i = 0; i<sizeof(test)/sizeof(test[0]); i++) {
1364 buffSize = u_unescape(test[i], buffer, 512);
1365 skSize = ucol_getSortKey(coll, buffer, buffSize, sortkey, 512);
1366 lowerSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1367 upperSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
1368 for(j = i+1; j<sizeof(test)/sizeof(test[0]); j++) {
1369 buffSize = u_unescape(test[j], buffer, 512);
1370 skSize = ucol_getSortKey(coll, buffer, buffSize, sortkey, 512);
1371 if(strcmp((const char *)lower, (const char *)sortkey) > 0) {
1372 log_err("Problem with lower! i = %i, j = %i (%s vs %s)\n", i, j, test[i], test[j]);
1373 }
1374 if(strcmp((const char *)upper, (const char *)sortkey) <= 0) {
1375 log_err("Problem with upper! i = %i, j = %i (%s vs %s)\n", i, j, test[i], test[j]);
1376 }
1377 }
1378 }
1379 ucol_close(coll);
1380 } else {
1381 log_data_err("Couldn't open collator\n");
1382 }
1383
1384}
1385
1386static void doOverrunTest(UCollator *coll, const UChar *uString, int32_t strLen) {
1387 int32_t skLen = 0, skLen2 = 0;
1388 uint8_t sortKey[256];
1389 int32_t i, j;
1390 uint8_t filler = 0xFF;
1391
1392 skLen = ucol_getSortKey(coll, uString, strLen, NULL, 0);
1393
1394 for(i = 0; i < skLen; i++) {
1395 memset(sortKey, filler, 256);
1396 skLen2 = ucol_getSortKey(coll, uString, strLen, sortKey, i);
1397 if(skLen != skLen2) {
1398 log_err("For buffer size %i, got different sortkey length. Expected %i got %i\n", i, skLen, skLen2);
1399 }
1400 for(j = i; j < 256; j++) {
1401 if(sortKey[j] != filler) {
1402 log_err("Something run over index %i\n", j);
1403 break;
1404 }
1405 }
1406 }
1407}
1408
1409/* j1865 reports that if a shorter buffer is passed to
1410 * to get sort key, a buffer overrun happens in some
1411 * cases. This test tries to check this.
1412 */
1413void TestSortKeyBufferOverrun(void) {
1414 UErrorCode status = U_ZERO_ERROR;
1415 const char* cString = "A very Merry liTTle-lamB..";
1416 UChar uString[256];
1417 int32_t strLen = 0;
1418 UCollator *coll = ucol_open("root", &status);
1419 strLen = u_unescape(cString, uString, 256);
1420
1421 if(U_SUCCESS(status)) {
1422 log_verbose("testing non ignorable\n");
1423 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
1424 doOverrunTest(coll, uString, strLen);
1425
1426 log_verbose("testing shifted\n");
1427 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
1428 doOverrunTest(coll, uString, strLen);
1429
1430 log_verbose("testing shifted quaternary\n");
1431 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
1432 doOverrunTest(coll, uString, strLen);
1433
1434 log_verbose("testing with french secondaries\n");
1435 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
1436 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &status);
1437 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
1438 doOverrunTest(coll, uString, strLen);
1439
1440 }
1441 ucol_close(coll);
1442}
1443
1444static void TestAttribute()
1445{
1446 UErrorCode error = U_ZERO_ERROR;
1447 UCollator *coll = ucol_open(NULL, &error);
1448
1449 if (U_FAILURE(error)) {
1450 log_err("Creation of default collator failed");
1451 return;
1452 }
1453
1454 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_OFF, &error);
1455 if (ucol_getAttribute(coll, UCOL_FRENCH_COLLATION, &error) != UCOL_OFF ||
1456 U_FAILURE(error)) {
1457 log_err("Setting and retrieving of the french collation failed");
1458 }
1459
1460 ucol_setAttribute(coll, UCOL_FRENCH_COLLATION, UCOL_ON, &error);
1461 if (ucol_getAttribute(coll, UCOL_FRENCH_COLLATION, &error) != UCOL_ON ||
1462 U_FAILURE(error)) {
1463 log_err("Setting and retrieving of the french collation failed");
1464 }
1465
1466 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &error);
1467 if (ucol_getAttribute(coll, UCOL_ALTERNATE_HANDLING, &error) != UCOL_SHIFTED ||
1468 U_FAILURE(error)) {
1469 log_err("Setting and retrieving of the alternate handling failed");
1470 }
1471
1472 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &error);
1473 if (ucol_getAttribute(coll, UCOL_ALTERNATE_HANDLING, &error) != UCOL_NON_IGNORABLE ||
1474 U_FAILURE(error)) {
1475 log_err("Setting and retrieving of the alternate handling failed");
1476 }
1477
1478 ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_LOWER_FIRST, &error);
1479 if (ucol_getAttribute(coll, UCOL_CASE_FIRST, &error) != UCOL_LOWER_FIRST ||
1480 U_FAILURE(error)) {
1481 log_err("Setting and retrieving of the case first attribute failed");
1482 }
1483
1484 ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &error);
1485 if (ucol_getAttribute(coll, UCOL_CASE_FIRST, &error) != UCOL_UPPER_FIRST ||
1486 U_FAILURE(error)) {
1487 log_err("Setting and retrieving of the case first attribute failed");
1488 }
1489
1490 ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_ON, &error);
1491 if (ucol_getAttribute(coll, UCOL_CASE_LEVEL, &error) != UCOL_ON ||
1492 U_FAILURE(error)) {
1493 log_err("Setting and retrieving of the case level attribute failed");
1494 }
1495
1496 ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_OFF, &error);
1497 if (ucol_getAttribute(coll, UCOL_CASE_LEVEL, &error) != UCOL_OFF ||
1498 U_FAILURE(error)) {
1499 log_err("Setting and retrieving of the case level attribute failed");
1500 }
1501
1502 ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &error);
1503 if (ucol_getAttribute(coll, UCOL_NORMALIZATION_MODE, &error) != UCOL_ON ||
1504 U_FAILURE(error)) {
1505 log_err("Setting and retrieving of the normalization on/off attribute failed");
1506 }
1507
1508 ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &error);
1509 if (ucol_getAttribute(coll, UCOL_NORMALIZATION_MODE, &error) != UCOL_OFF ||
1510 U_FAILURE(error)) {
1511 log_err("Setting and retrieving of the normalization on/off attribute failed");
1512 }
1513
1514 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &error);
1515 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_PRIMARY ||
1516 U_FAILURE(error)) {
1517 log_err("Setting and retrieving of the collation strength failed");
1518 }
1519
1520 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_SECONDARY, &error);
1521 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_SECONDARY ||
1522 U_FAILURE(error)) {
1523 log_err("Setting and retrieving of the collation strength failed");
1524 }
1525
1526 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &error);
1527 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_TERTIARY ||
1528 U_FAILURE(error)) {
1529 log_err("Setting and retrieving of the collation strength failed");
1530 }
1531
1532 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &error);
1533 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_QUATERNARY ||
1534 U_FAILURE(error)) {
1535 log_err("Setting and retrieving of the collation strength failed");
1536 }
1537
1538 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_IDENTICAL, &error);
1539 if (ucol_getAttribute(coll, UCOL_STRENGTH, &error) != UCOL_IDENTICAL ||
1540 U_FAILURE(error)) {
1541 log_err("Setting and retrieving of the collation strength failed");
1542 }
1543
1544 ucol_close(coll);
1545}
1546
1547void TestGetTailoredSet() {
1548 struct {
1549 const char *rules;
1550 const char *tests[20];
1551 int32_t testsize;
1552 } setTest[] = {
1553 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1554 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1555 };
1556
1557 int32_t i = 0, j = 0;
1558 UErrorCode status = U_ZERO_ERROR;
1559 UParseError pError;
1560
1561 UCollator *coll = NULL;
1562 UChar buff[1024];
1563 int32_t buffLen = 0;
1564 USet *set = NULL;
1565
1566 for(i = 0; i < sizeof(setTest)/sizeof(setTest[0]); i++) {
1567 buffLen = u_unescape(setTest[i].rules, buff, 1024);
1568 coll = ucol_openRules(buff, buffLen, UCOL_DEFAULT, UCOL_DEFAULT, &pError, &status);
1569 if(U_SUCCESS(status)) {
1570 set = ucol_getTailoredSet(coll, &status);
1571 if(uset_size(set) != setTest[i].testsize) {
1572 log_err("Tailored set size different (%d) than expected (%d)\n", uset_size(set), setTest[i].testsize);
1573 }
1574 for(j = 0; j < setTest[i].testsize; j++) {
1575 buffLen = u_unescape(setTest[i].tests[j], buff, 1024);
1576 if(!uset_containsString(set, buff, buffLen)) {
1577 log_err("Tailored set doesn't contain %s... It should\n", setTest[i].tests[j]);
1578 }
1579 }
1580 uset_close(set);
1581 } else {
1582 log_err("Couldn't open collator with rules %s\n", setTest[i].rules);
1583 }
1584 ucol_close(coll);
1585 }
1586}
1587
1588static int tMemCmp(const uint8_t *first, const uint8_t *second) {
374ca955
A
1589 int32_t firstLen = (int32_t)strlen((const char *)first);
1590 int32_t secondLen = (int32_t)strlen((const char *)second);
b75a7d8f
A
1591 return memcmp(first, second, uprv_min(firstLen, secondLen));
1592}
1593static const char * strengthsC[] = {
1594 "UCOL_PRIMARY",
1595 "UCOL_SECONDARY",
1596 "UCOL_TERTIARY",
1597 "UCOL_QUATERNARY",
1598 "UCOL_IDENTICAL"
1599};
1600
1601void TestMergeSortKeys(void) {
1602 UErrorCode status = U_ZERO_ERROR;
1603 UCollator *coll = ucol_open("en", &status);
1604 if(U_SUCCESS(status)) {
1605
1606 const char* cases[] = {
1607 "abc",
1608 "abcd",
1609 "abcde"
1610 };
1611 uint32_t casesSize = sizeof(cases)/sizeof(cases[0]);
1612 const char* prefix = "foo";
1613 const char* suffix = "egg";
1614 char outBuff1[256], outBuff2[256];
1615
1616 uint8_t **sortkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1617 uint8_t **mergedPrefixkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1618 uint8_t **mergedSuffixkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *));
1619 uint32_t *sortKeysLen = (uint32_t *)malloc(casesSize*sizeof(uint32_t));
1620 uint8_t prefixKey[256], suffixKey[256];
1621 uint32_t prefixKeyLen = 0, suffixKeyLen = 0, i = 0;
1622 UChar buffer[256];
1623 uint32_t unescapedLen = 0, l1 = 0, l2 = 0;
1624 UColAttributeValue strength;
1625
1626 log_verbose("ucol_mergeSortkeys test\n");
1627 log_verbose("Testing order of the test cases\n");
1628 genericLocaleStarter("en", cases, casesSize);
1629
1630 for(i = 0; i<casesSize; i++) {
1631 sortkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1632 mergedPrefixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1633 mergedSuffixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
1634 }
1635
1636 unescapedLen = u_unescape(prefix, buffer, 256);
1637 prefixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, prefixKey, 256);
1638
1639 unescapedLen = u_unescape(suffix, buffer, 256);
1640 suffixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, suffixKey, 256);
1641
1642 log_verbose("Massaging data with prefixes and different strengths\n");
1643 strength = UCOL_PRIMARY;
1644 while(strength <= UCOL_IDENTICAL) {
1645 log_verbose("Strength %s\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1646 ucol_setAttribute(coll, UCOL_STRENGTH, strength, &status);
1647 for(i = 0; i<casesSize; i++) {
1648 unescapedLen = u_unescape(cases[i], buffer, 256);
1649 sortKeysLen[i] = ucol_getSortKey(coll, buffer, unescapedLen, sortkeys[i], 256);
1650 ucol_mergeSortkeys(prefixKey, prefixKeyLen, sortkeys[i], sortKeysLen[i], mergedPrefixkeys[i], 256);
1651 ucol_mergeSortkeys(sortkeys[i], sortKeysLen[i], suffixKey, suffixKeyLen, mergedSuffixkeys[i], 256);
1652 if(i>0) {
1653 if(tMemCmp(mergedPrefixkeys[i-1], mergedPrefixkeys[i]) >= 0) {
1654 log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1655 log_err("%s\n%s\n",
1656 ucol_sortKeyToString(coll, mergedPrefixkeys[i-1], outBuff1, &l1),
1657 ucol_sortKeyToString(coll, mergedPrefixkeys[i], outBuff2, &l2));
1658 }
1659 if(tMemCmp(mergedSuffixkeys[i-1], mergedSuffixkeys[i]) >= 0) {
1660 log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
1661 log_err("%s\n%s\n",
1662 ucol_sortKeyToString(coll, mergedSuffixkeys[i-1], outBuff1, &l1),
1663 ucol_sortKeyToString(coll, mergedSuffixkeys[i], outBuff2, &l2));
1664 }
1665 }
1666 }
1667 if(strength == UCOL_QUATERNARY) {
1668 strength = UCOL_IDENTICAL;
1669 } else {
1670 strength++;
1671 }
1672 }
1673
1674 {
1675 uint8_t smallBuf[3];
1676 uint32_t reqLen = 0;
1677 log_verbose("testing buffer overflow\n");
1678 reqLen = ucol_mergeSortkeys(prefixKey, prefixKeyLen, suffixKey, suffixKeyLen, smallBuf, 3);
1679 if(reqLen != (prefixKeyLen+suffixKeyLen-1)) {
1680 log_err("Wrong preflight size for merged sortkey\n");
1681 }
1682 }
1683
1684 {
1685 UChar empty = 0;
1686 uint8_t emptyKey[20], abcKey[50], mergedKey[100];
1687 int32_t emptyKeyLen = 0, abcKeyLen = 0, mergedKeyLen = 0;
1688
1689 log_verbose("testing merging with sortkeys generated for empty strings\n");
1690 emptyKeyLen = ucol_getSortKey(coll, &empty, 0, emptyKey, 20);
1691 unescapedLen = u_unescape(cases[0], buffer, 256);
1692 abcKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, abcKey, 50);
1693 mergedKeyLen = ucol_mergeSortkeys(emptyKey, emptyKeyLen, abcKey, abcKeyLen, mergedKey, 100);
1694 if(mergedKey[0] != 2) {
1695 log_err("Empty sortkey didn't produce a level separator\n");
1696 }
1697 /* try with zeros */
1698 mergedKeyLen = ucol_mergeSortkeys(emptyKey, 0, abcKey, abcKeyLen, mergedKey, 100);
1699 if(mergedKeyLen != 0 || mergedKey[0] != 0) {
1700 log_err("Empty key didn't produce null mergedKey\n");
1701 }
1702 mergedKeyLen = ucol_mergeSortkeys(abcKey, abcKeyLen, emptyKey, 0, mergedKey, 100);
1703 if(mergedKeyLen != 0 || mergedKey[0] != 0) {
1704 log_err("Empty key didn't produce null mergedKey\n");
1705 }
1706
1707 }
1708
1709 for(i = 0; i<casesSize; i++) {
1710 free(sortkeys[i]);
1711 free(mergedPrefixkeys[i]);
1712 free(mergedSuffixkeys[i]);
1713 }
1714 free(sortkeys);
1715 free(mergedPrefixkeys);
1716 free(mergedSuffixkeys);
1717 free(sortKeysLen);
1718 ucol_close(coll);
1719 /* need to finish this up */
1720 } else {
1721 log_data_err("Couldn't open collator");
1722 }
1723}
374ca955
A
1724static void TestShortString(void)
1725{
1726 struct {
1727 const char *input;
1728 const char *expectedOutput;
1729 const char *locale;
1730 UErrorCode expectedStatus;
1731 int32_t expectedOffset;
1732 uint32_t expectedIdentifier;
1733 } testCases[] = {
1734 {"LDE_RDE_KPHONEBOOK_T0041_ZLATN","B2600_KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING, 0, 0 },
1735 {"LEN_RUS_NO_AS_S4","AS_LEN_NO_S4", NULL, U_USING_FALLBACK_WARNING, 0, 0 },
1736 {"LDE_VPHONEBOOK_EO_SI","EO_KPHONEBOOK_LDE_SI", "de@collation=phonebook", U_ZERO_ERROR, 0, 0 },
1737 {"LDE_Kphonebook","KPHONEBOOK_LDE", "de@collation=phonebook", U_ZERO_ERROR, 0, 0 },
1738 {"Xqde_DE@collation=phonebookq_S3_EX","KPHONEBOOK_LDE", "de@collation=phonebook", U_USING_FALLBACK_WARNING, 0, 0 },
1739 {"LFR_FO", "LFR", NULL, U_ZERO_ERROR, 0, 0 },
1740 {"SO_LX_AS", "", NULL, U_ILLEGAL_ARGUMENT_ERROR, 8, 0 },
1741 {"S3_ASS_MMM", "", NULL, U_ILLEGAL_ARGUMENT_ERROR, 5, 0 }
1742 };
1743
1744 int32_t i = 0, j = 0;
1745 UCollator *coll = NULL, *fromID = NULL, *fromNormalized = NULL;
1746 UParseError parseError;
1747 UErrorCode status = U_ZERO_ERROR;
1748 char fromShortBuffer[256], fromIDBuffer[256], fromIDRoundtrip[256], normalizedBuffer[256], fromNormalizedBuffer[256];
1749 uint32_t identifier = 0, idFromSS = 0;
1750 const char* locale = NULL;
1751
1752
1753 for(i = 0; i < sizeof(testCases)/sizeof(testCases[0]); i++) {
1754 status = U_ZERO_ERROR;
1755 if(testCases[i].locale) {
1756 locale = testCases[i].locale;
1757 } else {
1758 locale = NULL;
1759 }
1760
1761 coll = ucol_openFromShortString(testCases[i].input, FALSE, &parseError, &status);
1762 if(status != testCases[i].expectedStatus) {
1763 log_err("Got status '%s' that is different from expected '%s' for '%s'\n",
1764 u_errorName(status), u_errorName(testCases[i].expectedStatus), testCases[i].input);
1765 }
1766
1767 if(U_SUCCESS(status)) {
1768 ucol_getShortDefinitionString(coll, locale, fromShortBuffer, 256, &status);
1769
1770 if(strcmp(fromShortBuffer, testCases[i].expectedOutput)) {
1771 log_err("Got short string '%s' from the collator. Expected '%s' for input '%s'\n",
1772 fromShortBuffer, testCases[i].expectedOutput, testCases[i].input);
1773 }
1774
1775 ucol_normalizeShortDefinitionString(testCases[i].input, normalizedBuffer, 256, &parseError, &status);
1776 fromNormalized = ucol_openFromShortString(normalizedBuffer, FALSE, &parseError, &status);
1777 ucol_getShortDefinitionString(fromNormalized, locale, fromNormalizedBuffer, 256, &status);
1778
1779 if(strcmp(fromShortBuffer, fromNormalizedBuffer)) {
1780 log_err("Strings obtained from collators instantiated by short string ('%s') and from normalized string ('%s') differ\n",
1781 fromShortBuffer, fromNormalizedBuffer);
1782 }
1783
1784
1785 if(!ucol_equals(coll, fromNormalized)) {
1786 log_err("Collator from short string ('%s') differs from one obtained through a normalized version ('%s')\n",
1787 testCases[i].input, normalizedBuffer);
1788 }
1789
1790 /* test identifiers */
1791 identifier = ucol_collatorToIdentifier(coll, locale, &status);
1792 if(identifier < UCOL_SIT_COLLATOR_NOT_ENCODABLE) {
1793 ucol_identifierToShortString(identifier, fromIDBuffer, 256, FALSE, &status);
1794 fromID = ucol_openFromIdentifier(identifier, FALSE, &status);
1795 if(!ucol_equals(coll, fromID)) {
1796 log_err("Collator from short string ('%s') differs from one obtained through an identifier ('%s')\n",
1797 testCases[i].input, fromIDBuffer);
1798 }
1799 ucol_close(fromID);
1800 }
1801
1802 /* round-trip short string - identifier */
1803 for(j = 1; j < 2; j++) {
1804 idFromSS = ucol_shortStringToIdentifier(testCases[i].input, (UBool)j, &status);
1805 ucol_identifierToShortString(idFromSS, fromIDBuffer, 256, (UBool)j, &status);
1806 identifier = ucol_shortStringToIdentifier(fromIDBuffer, (UBool)j, &status);
1807 ucol_identifierToShortString(identifier, fromIDRoundtrip, 256, (UBool)j, &status);
1808
1809 if(idFromSS != identifier) {
1810 log_err("FD = %i, id didn't round trip. %08X vs %08X (%s)\n",
1811 j, idFromSS, identifier, testCases[i]);
1812 }
1813 if(strcmp(fromIDBuffer, fromIDRoundtrip)) {
1814 log_err("FD = %i, SS didn't round trip. %s vs %s (%s)\n",
1815 j, fromIDBuffer, fromIDRoundtrip, testCases[i]);
1816 }
1817 }
1818
1819 ucol_close(fromNormalized);
1820 ucol_close(coll);
1821
1822 } else {
1823 if(parseError.offset != testCases[i].expectedOffset) {
1824 log_err("Got parse error offset %i, but expected %i instead for '%s'\n",
1825 parseError.offset, testCases[i].expectedOffset, testCases[i].input);
1826 }
1827 }
1828 }
1829
1830}
1831
1832static void
1833doSetsTest(const USet *ref, USet *set, const char* inSet, const char* outSet, UErrorCode *status) {
1834 UChar buffer[512];
1835 int32_t bufLen;
1836
1837 uset_clear(set);
1838 bufLen = u_unescape(inSet, buffer, 512);
1839 uset_applyPattern(set, buffer, bufLen, 0, status);
1840 if(U_FAILURE(*status)) {
1841 log_err("Failure setting pattern %s\n", u_errorName(*status));
1842 }
1843
1844 if(!uset_containsAll(ref, set)) {
1845 log_err("Some stuff from %s is not present in the set\n", inSet);
1846 }
1847
1848 uset_clear(set);
1849 bufLen = u_unescape(outSet, buffer, 512);
1850 uset_applyPattern(set, buffer, bufLen, 0, status);
1851 if(U_FAILURE(*status)) {
1852 log_err("Failure setting pattern %s\n", u_errorName(*status));
1853 }
1854
1855 if(!uset_containsNone(ref, set)) {
1856 log_err("Some stuff from %s is present in the set\n", outSet);
1857 }
1858}
1859
1860
1861
1862
1863static void
1864TestGetContractionsAndUnsafes(void)
1865{
1866 static struct {
1867 const char* locale;
1868 const char* inConts;
1869 const char* outConts;
1870 const char* unsafeCodeUnits;
1871 const char* safeCodeUnits;
1872 } tests[] = {
1873 { "ru",
1874 "[{\\u0474\\u030F}{\\u0475\\u030F}{\\u04D8\\u0308}{\\u04D9\\u0308}{\\u04E8\\u0308}{\\u04E9\\u0308}]",
1875 "[{\\u0430\\u0306}{\\u0410\\u0306}{\\u0430\\u0308}{\\u0410\\u0306}{\\u0433\\u0301}{\\u0413\\u0301}]",
1876 "[\\u0474\\u0475\\u04d8\\u04d9\\u04e8\\u04e9]",
1877 "[aAbB\\u0430\\u0410\\u0433\\u0413]"
1878 },
1879 { "uk",
1880 "[{\\u0474\\u030F}{\\u0475\\u030F}{\\u04D8\\u0308}{\\u04D9\\u0308}{\\u04E8\\u0308}{\\u04E9\\u0308}"
1881 "{\\u0430\\u0306}{\\u0410\\u0306}{\\u0430\\u0308}{\\u0410\\u0306}{\\u0433\\u0301}{\\u0413\\u0301}]",
1882 "[]",
1883 "[\\u0474\\u0475\\u04D8\\u04D9\\u04E8\\u04E9\\u0430\\u0410\\u0433\\u0413]",
1884 "[aAbBxv]",
1885 },
1886 { "ja",
1887 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
1888 "[{lj}{nj}]",
1889 "[\\u3099\\u309d\\u30fd]",
1890 "[\\u30a6\\u3044\\uff73]"
1891 },
1892 { "sh",
1893 "[{C\\u0301}{C\\u030C}{C\\u0341}{DZ\\u030C}{Dz\\u030C}{D\\u017D}{D\\u017E}{lj}{nj}]",
1894 "[{\\u309d\\u3099}{\\u30fd\\u3099}]",
1895 "[nlcdzNLCDZ]",
1896 "[jabv]"
1897 }
1898 };
1899
1900
1901
1902
1903 UErrorCode status = U_ZERO_ERROR;
1904 UCollator *coll = NULL;
1905 int32_t i = 0;
1906 int32_t noConts = 0;
1907 USet *conts = uset_open(0,0);
1908 USet *set = uset_open(0,0);
1909 UChar buffer[32768];
1910 int32_t setLen = 0;
1911
1912 for(i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
1913 log_verbose("Testing locale: %s\n", tests[i].locale);
1914 coll = ucol_open(tests[i].locale, &status);
1915 noConts = ucol_getContractions(coll, conts, &status);
1916 doSetsTest(conts, set, tests[i].inConts, tests[i].outConts, &status);
1917 setLen = uset_toPattern(conts, buffer, 32768, TRUE, &status);
1918 if(U_SUCCESS(status)) {
1919 log_verbose("%i: %s\n", noConts, aescstrdup(buffer, setLen));
1920 } else {
1921 log_err("error %s. %i\n", u_errorName(status), setLen);
1922 }
1923
1924 noConts = ucol_getUnsafeSet(coll, conts, &status);
1925 doSetsTest(conts, set, tests[i].unsafeCodeUnits, tests[i].safeCodeUnits, &status);
1926
1927 ucol_close(coll);
1928 }
1929
1930
1931 uset_close(conts);
1932 uset_close(set);
1933}
1934
b75a7d8f 1935#endif /* #if !UCONFIG_NO_COLLATION */