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