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