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