1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1997-2013, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 7 #include "cintltst.h" 8 #include "unicode/ures.h" 9 #include "unicode/ucurr.h" 10 #include "unicode/ustring.h" 11 #include "unicode/uset.h" 12 #include "unicode/udat.h" 13 #include "unicode/uscript.h" 14 #include "unicode/ulocdata.h" 15 #include "cstring.h" 16 #include "locmap.h" 17 #include "uresimp.h" 18 19 /* 20 returns a new UnicodeSet that is a flattened form of the original 21 UnicodeSet. 22 */ 23 static USet* 24 createFlattenSet(USet *origSet, UErrorCode *status) { 25 26 27 USet *newSet = NULL; 28 int32_t origItemCount = 0; 29 int32_t idx, graphmeSize; 30 UChar32 start, end; 31 UChar graphme[64]; 32 if (U_FAILURE(*status)) { 33 log_err("createFlattenSet called with %s\n", u_errorName(*status)); 34 return NULL; 35 } 36 newSet = uset_open(1, 0); 37 origItemCount = uset_getItemCount(origSet); 38 for (idx = 0; idx < origItemCount; idx++) { 39 graphmeSize = uset_getItem(origSet, idx, 40 &start, &end, 41 graphme, (int32_t)(sizeof(graphme)/sizeof(graphme[0])), 42 status); 43 if (U_FAILURE(*status)) { 44 log_err("ERROR: uset_getItem returned %s\n", u_errorName(*status)); 45 *status = U_ZERO_ERROR; 46 } 47 if (graphmeSize) { 48 uset_addAllCodePoints(newSet, graphme, graphmeSize); 49 } 50 else { 51 uset_addRange(newSet, start, end); 52 } 53 } 54 uset_closeOver(newSet,USET_CASE_INSENSITIVE); 55 return newSet; 56 } 57 58 static UBool 59 isCurrencyPreEuro(const char* currencyKey){ 60 if( strcmp(currencyKey, "PTE") == 0 || 61 strcmp(currencyKey, "ESP") == 0 || 62 strcmp(currencyKey, "LUF") == 0 || 63 strcmp(currencyKey, "GRD") == 0 || 64 strcmp(currencyKey, "BEF") == 0 || 65 strcmp(currencyKey, "ITL") == 0 || 66 strcmp(currencyKey, "EEK") == 0){ 67 return TRUE; 68 } 69 return FALSE; 70 } 71 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION 72 static void 73 TestKeyInRootRecursive(UResourceBundle *root, const char *rootName, 74 UResourceBundle *currentBundle, const char *locale) { 75 UErrorCode errorCode = U_ZERO_ERROR; 76 UResourceBundle *subRootBundle = NULL, *subBundle = NULL, *arr = NULL; 77 78 ures_resetIterator(root); 79 ures_resetIterator(currentBundle); 80 while (ures_hasNext(currentBundle)) { 81 const char *subBundleKey = NULL; 82 const char *currentBundleKey = NULL; 83 84 errorCode = U_ZERO_ERROR; 85 currentBundleKey = ures_getKey(currentBundle); 86 subBundle = ures_getNextResource(currentBundle, NULL, &errorCode); 87 if (U_FAILURE(errorCode)) { 88 log_err("Can't open a resource for lnocale %s. Error: %s\n", locale, u_errorName(errorCode)); 89 continue; 90 } 91 subBundleKey = ures_getKey(subBundle); 92 93 94 subRootBundle = ures_getByKey(root, subBundleKey, NULL, &errorCode); 95 if (U_FAILURE(errorCode)) { 96 log_err("Can't open a resource with key \"%s\" in \"%s\" from %s for locale \"%s\"\n", 97 subBundleKey, 98 ures_getKey(currentBundle), 99 rootName, 100 locale); 101 ures_close(subBundle); 102 continue; 103 } 104 if (ures_getType(subRootBundle) != ures_getType(subBundle)) { 105 log_err("key \"%s\" in \"%s\" has a different type from root for locale \"%s\"\n" 106 "\troot=%d, locale=%d\n", 107 subBundleKey, 108 ures_getKey(currentBundle), 109 locale, 110 ures_getType(subRootBundle), 111 ures_getType(subBundle)); 112 ures_close(subBundle); 113 continue; 114 } 115 else if (ures_getType(subBundle) == URES_INT_VECTOR) { 116 int32_t minSize; 117 int32_t subBundleSize; 118 int32_t idx; 119 UBool sameArray = TRUE; 120 const int32_t *subRootBundleArr = ures_getIntVector(subRootBundle, &minSize, &errorCode); 121 const int32_t *subBundleArr = ures_getIntVector(subBundle, &subBundleSize, &errorCode); 122 123 if (minSize > subBundleSize) { 124 minSize = subBundleSize; 125 log_err("Arrays are different size with key \"%s\" in \"%s\" from root for locale \"%s\"\n", 126 subBundleKey, 127 ures_getKey(currentBundle), 128 locale); 129 } 130 131 for (idx = 0; idx < minSize && sameArray; idx++) { 132 if (subRootBundleArr[idx] != subBundleArr[idx]) { 133 sameArray = FALSE; 134 } 135 if (strcmp(subBundleKey, "DateTimeElements") == 0 136 && (subBundleArr[idx] < 1 || 7 < subBundleArr[idx])) 137 { 138 log_err("Value out of range with key \"%s\" at index %d in \"%s\" for locale \"%s\"\n", 139 subBundleKey, 140 idx, 141 ures_getKey(currentBundle), 142 locale); 143 } 144 } 145 /* Special exception es_US and DateTimeElements */ 146 if (sameArray 147 && !(strcmp(locale, "es_US") == 0 && strcmp(subBundleKey, "DateTimeElements") == 0)) 148 { 149 log_err("Integer vectors are the same with key \"%s\" in \"%s\" from root for locale \"%s\"\n", 150 subBundleKey, 151 ures_getKey(currentBundle), 152 locale); 153 } 154 } 155 else if (ures_getType(subBundle) == URES_ARRAY) { 156 UResourceBundle *subSubBundle = ures_getByIndex(subBundle, 0, NULL, &errorCode); 157 UResourceBundle *subSubRootBundle = ures_getByIndex(subRootBundle, 0, NULL, &errorCode); 158 159 if (U_SUCCESS(errorCode) 160 && (ures_getType(subSubBundle) == URES_ARRAY || ures_getType(subSubRootBundle) == URES_ARRAY)) 161 { 162 /* Here is one of the recursive parts */ 163 TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale); 164 } 165 else { 166 int32_t minSize = ures_getSize(subRootBundle); 167 int32_t idx; 168 UBool sameArray = TRUE; 169 170 if (minSize > ures_getSize(subBundle)) { 171 minSize = ures_getSize(subBundle); 172 } 173 174 if ((subBundleKey == NULL 175 || (subBundleKey != NULL && strcmp(subBundleKey, "LocaleScript") != 0 && !isCurrencyPreEuro(subBundleKey))) 176 && ures_getSize(subRootBundle) != ures_getSize(subBundle)) 177 { 178 log_err("Different size array with key \"%s\" in \"%s\" from root for locale \"%s\"\n" 179 "\troot array size=%d, locale array size=%d\n", 180 subBundleKey, 181 ures_getKey(currentBundle), 182 locale, 183 ures_getSize(subRootBundle), 184 ures_getSize(subBundle)); 185 } 186 /* 187 if(isCurrencyPreEuro(subBundleKey) && ures_getSize(subBundle)!=3){ 188 log_err("Different size array with key \"%s\" in \"%s\" for locale \"%s\" the expected size is 3 got size=%d\n", 189 subBundleKey, 190 ures_getKey(currentBundle), 191 locale, 192 ures_getSize(subBundle)); 193 } 194 */ 195 for (idx = 0; idx < minSize; idx++) { 196 int32_t rootStrLen, localeStrLen; 197 const UChar *rootStr = ures_getStringByIndex(subRootBundle,idx,&rootStrLen,&errorCode); 198 const UChar *localeStr = ures_getStringByIndex(subBundle,idx,&localeStrLen,&errorCode); 199 if (rootStr && localeStr && U_SUCCESS(errorCode)) { 200 if (u_strcmp(rootStr, localeStr) != 0) { 201 sameArray = FALSE; 202 } 203 } 204 else { 205 if ( rootStrLen > 1 && rootStr[0] == 0x41 && rootStr[1] >= 0x30 && rootStr[1] <= 0x39 ) { 206 /* A2 or A4 in the root string indicates that the resource can optionally be an array instead of a */ 207 /* string. Attempt to read it as an array. */ 208 errorCode = U_ZERO_ERROR; 209 arr = ures_getByIndex(subBundle,idx,NULL,&errorCode); 210 if (U_FAILURE(errorCode)) { 211 log_err("Got a NULL string with key \"%s\" in \"%s\" at index %d for root or locale \"%s\"\n", 212 subBundleKey, 213 ures_getKey(currentBundle), 214 idx, 215 locale); 216 continue; 217 } 218 if (ures_getType(arr) != URES_ARRAY || ures_getSize(arr) != (int32_t)rootStr[1] - 0x30) { 219 log_err("Got something other than a string or array of size %d for key \"%s\" in \"%s\" at index %d for root or locale \"%s\"\n", 220 rootStr[1] - 0x30, 221 subBundleKey, 222 ures_getKey(currentBundle), 223 idx, 224 locale); 225 ures_close(arr); 226 continue; 227 } 228 localeStr = ures_getStringByIndex(arr,0,&localeStrLen,&errorCode); 229 ures_close(arr); 230 if (U_FAILURE(errorCode)) { 231 log_err("Got something other than a string or array for key \"%s\" in \"%s\" at index %d for root or locale \"%s\"\n", 232 subBundleKey, 233 ures_getKey(currentBundle), 234 idx, 235 locale); 236 continue; 237 } 238 } else { 239 log_err("Got a NULL string with key \"%s\" in \"%s\" at index %d for root or locale \"%s\"\n", 240 subBundleKey, 241 ures_getKey(currentBundle), 242 idx, 243 locale); 244 continue; 245 } 246 } 247 if (localeStr[0] == (UChar)0x20) { 248 log_err("key \"%s\" at index %d in \"%s\" starts with a space in locale \"%s\"\n", 249 subBundleKey, 250 idx, 251 ures_getKey(currentBundle), 252 locale); 253 } 254 else if ((localeStr[localeStrLen - 1] == (UChar)0x20) && (strcmp(subBundleKey,"separator") != 0)) { 255 log_err("key \"%s\" at index %d in \"%s\" ends with a space in locale \"%s\"\n", 256 subBundleKey, 257 idx, 258 ures_getKey(currentBundle), 259 locale); 260 } 261 else if (subBundleKey != NULL 262 && strcmp(subBundleKey, "DateTimePatterns") == 0) 263 { 264 int32_t quoted = 0; 265 const UChar *localeStrItr = localeStr; 266 while (*localeStrItr) { 267 if (*localeStrItr == (UChar)0x27 /* ' */) { 268 quoted++; 269 } 270 else if ((quoted % 2) == 0) { 271 /* Search for unquoted characters */ 272 if (4 <= idx && idx <= 7 273 && (*localeStrItr == (UChar)0x6B /* k */ 274 || *localeStrItr == (UChar)0x48 /* H */ 275 || *localeStrItr == (UChar)0x6D /* m */ 276 || *localeStrItr == (UChar)0x73 /* s */ 277 || *localeStrItr == (UChar)0x53 /* S */ 278 || *localeStrItr == (UChar)0x61 /* a */ 279 || *localeStrItr == (UChar)0x68 /* h */ 280 || *localeStrItr == (UChar)0x7A /* z */)) 281 { 282 log_err("key \"%s\" at index %d has time pattern chars in date for locale \"%s\"\n", 283 subBundleKey, 284 idx, 285 locale); 286 } 287 else if (0 <= idx && idx <= 3 288 && (*localeStrItr == (UChar)0x47 /* G */ 289 || *localeStrItr == (UChar)0x79 /* y */ 290 || *localeStrItr == (UChar)0x4D /* M */ 291 || *localeStrItr == (UChar)0x64 /* d */ 292 || *localeStrItr == (UChar)0x45 /* E */ 293 || *localeStrItr == (UChar)0x44 /* D */ 294 || *localeStrItr == (UChar)0x46 /* F */ 295 || *localeStrItr == (UChar)0x77 /* w */ 296 || *localeStrItr == (UChar)0x57 /* W */)) 297 { 298 log_err("key \"%s\" at index %d has date pattern chars in time for locale \"%s\"\n", 299 subBundleKey, 300 idx, 301 locale); 302 } 303 } 304 localeStrItr++; 305 } 306 } 307 else if (idx == 4 && subBundleKey != NULL 308 && strcmp(subBundleKey, "NumberElements") == 0 309 && u_charDigitValue(localeStr[0]) != 0) 310 { 311 log_err("key \"%s\" at index %d has a non-zero based number for locale \"%s\"\n", 312 subBundleKey, 313 idx, 314 locale); 315 } 316 } 317 /* if (sameArray && strcmp(rootName, "root") == 0) { 318 log_err("Arrays are the same with key \"%s\" in \"%s\" from root for locale \"%s\"\n", 319 subBundleKey, 320 ures_getKey(currentBundle), 321 locale); 322 }*/ 323 } 324 ures_close(subSubBundle); 325 ures_close(subSubRootBundle); 326 } 327 else if (ures_getType(subBundle) == URES_STRING) { 328 int32_t len = 0; 329 const UChar *string = ures_getString(subBundle, &len, &errorCode); 330 if (U_FAILURE(errorCode) || string == NULL) { 331 log_err("Can't open a string with key \"%s\" in \"%s\" for locale \"%s\"\n", 332 subBundleKey, 333 ures_getKey(currentBundle), 334 locale); 335 } else if (string[0] == (UChar)0x20) { 336 log_err("key \"%s\" in \"%s\" starts with a space in locale \"%s\"\n", 337 subBundleKey, 338 ures_getKey(currentBundle), 339 locale); 340 /* localeDisplayPattern/separator can end with a space */ 341 } else if (string[len - 1] == (UChar)0x20 && (strcmp(subBundleKey,"separator"))) { 342 log_err("key \"%s\" in \"%s\" ends with a space in locale \"%s\"\n", 343 subBundleKey, 344 ures_getKey(currentBundle), 345 locale); 346 } else if (strcmp(subBundleKey, "localPatternChars") == 0) { 347 /* Note: We no longer import localPatternChars data starting 348 * ICU 3.8. So it never comes into this else if block. (ticket#5597) 349 */ 350 351 /* Check well-formedness of localPatternChars. First, the 352 * length must match the number of fields defined by 353 * DateFormat. Second, each character in the string must 354 * be in the set [A-Za-z]. Finally, each character must be 355 * unique. 356 */ 357 int32_t i,j; 358 #if !UCONFIG_NO_FORMATTING 359 if (len != UDAT_FIELD_COUNT) { 360 log_err("key \"%s\" has the wrong number of characters in locale \"%s\"\n", 361 subBundleKey, 362 locale); 363 } 364 #endif 365 /* Check char validity. */ 366 for (i=0; i<len; ++i) { 367 if (!((string[i] >= 65/*'A'*/ && string[i] <= 90/*'Z'*/) || 368 (string[i] >= 97/*'a'*/ && string[i] <= 122/*'z'*/))) { 369 log_err("key \"%s\" has illegal character '%c' in locale \"%s\"\n", 370 subBundleKey, 371 (char) string[i], 372 locale); 373 } 374 /* Do O(n^2) check for duplicate chars. */ 375 for (j=0; j<i; ++j) { 376 if (string[j] == string[i]) { 377 log_err("key \"%s\" has duplicate character '%c' in locale \"%s\"\n", 378 subBundleKey, 379 (char) string[i], 380 locale); 381 } 382 } 383 } 384 } 385 /* No fallback was done. Check for duplicate data */ 386 /* The ures_* API does not do fallback of sub-resource bundles, 387 So we can't do this now. */ 388 #if 0 389 else if (strcmp(locale, "root") != 0 && errorCode == U_ZERO_ERROR) { 390 391 const UChar *rootString = ures_getString(subRootBundle, &len, &errorCode); 392 if (U_FAILURE(errorCode) || rootString == NULL) { 393 log_err("Can't open a string with key \"%s\" in \"%s\" in root\n", 394 ures_getKey(subRootBundle), 395 ures_getKey(currentBundle)); 396 continue; 397 } else if (u_strcmp(string, rootString) == 0) { 398 if (strcmp(locale, "de_CH") != 0 && strcmp(subBundleKey, "Countries") != 0 && 399 strcmp(subBundleKey, "Version") != 0) { 400 log_err("Found duplicate data with key \"%s\" in \"%s\" in locale \"%s\"\n", 401 ures_getKey(subRootBundle), 402 ures_getKey(currentBundle), 403 locale); 404 } 405 else { 406 /* Ignore for now. */ 407 /* Can be fixed if fallback through de locale was done. */ 408 log_verbose("Skipping key %s in %s\n", subBundleKey, locale); 409 } 410 } 411 } 412 #endif 413 } 414 else if (ures_getType(subBundle) == URES_TABLE) { 415 if (strcmp(subBundleKey, "availableFormats")!=0) { 416 /* Here is one of the recursive parts */ 417 TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale); 418 } 419 else { 420 log_verbose("Skipping key %s in %s\n", subBundleKey, locale); 421 } 422 } 423 else if (ures_getType(subBundle) == URES_BINARY || ures_getType(subBundle) == URES_INT) { 424 /* Can't do anything to check it */ 425 /* We'll assume it's all correct */ 426 if (strcmp(subBundleKey, "MeasurementSystem") != 0) { 427 log_verbose("Skipping key \"%s\" in \"%s\" for locale \"%s\"\n", 428 subBundleKey, 429 ures_getKey(currentBundle), 430 locale); 431 } 432 /* Testing for MeasurementSystem is done in VerifyTranslation */ 433 } 434 else { 435 log_err("Type %d for key \"%s\" in \"%s\" is unknown for locale \"%s\"\n", 436 ures_getType(subBundle), 437 subBundleKey, 438 ures_getKey(currentBundle), 439 locale); 440 } 441 ures_close(subRootBundle); 442 ures_close(subBundle); 443 } 444 } 445 #endif 446 447 static void 448 testLCID(UResourceBundle *currentBundle, 449 const char *localeName) 450 { 451 UErrorCode status = U_ZERO_ERROR; 452 uint32_t expectedLCID; 453 char lcidStringC[64] = {0}; 454 455 expectedLCID = uloc_getLCID(localeName); 456 if (expectedLCID == 0) { 457 log_verbose("INFO: %-5s does not have any LCID mapping\n", 458 localeName); 459 return; 460 } 461 462 status = U_ZERO_ERROR; 463 uprv_strcpy(lcidStringC, uprv_convertToPosix(expectedLCID, &status)); 464 if (U_FAILURE(status)) { 465 log_err("ERROR: %.4x does not have a POSIX mapping due to %s\n", 466 expectedLCID, u_errorName(status)); 467 } 468 469 if(strcmp(localeName, lcidStringC) != 0) { 470 char langName[1024]; 471 char langLCID[1024]; 472 uloc_getLanguage(localeName, langName, sizeof(langName), &status); 473 uloc_getLanguage(lcidStringC, langLCID, sizeof(langLCID), &status); 474 475 if (strcmp(langName, langLCID) == 0) { 476 log_verbose("WARNING: %-5s resolves to %s (0x%.4x)\n", 477 localeName, lcidStringC, expectedLCID); 478 } 479 else { 480 log_err("ERROR: %-5s has 0x%.4x and the number resolves wrongfully to %s\n", 481 localeName, expectedLCID, lcidStringC); 482 } 483 } 484 } 485 486 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION 487 static void 488 TestLocaleStructure(void) { 489 UResourceBundle *root, *currentLocale; 490 int32_t locCount = uloc_countAvailable(); 491 int32_t locIndex; 492 UErrorCode errorCode = U_ZERO_ERROR; 493 const char *currLoc, *resolvedLoc; 494 495 /* TODO: Compare against parent's data too. This code can't handle fallbacks that some tools do already. */ 496 /* char locName[ULOC_FULLNAME_CAPACITY]; 497 char *locNamePtr; 498 499 for (locIndex = 0; locIndex < locCount; locIndex++) { 500 errorCode=U_ZERO_ERROR; 501 strcpy(locName, uloc_getAvailable(locIndex)); 502 locNamePtr = strrchr(locName, '_'); 503 if (locNamePtr) { 504 *locNamePtr = 0; 505 } 506 else { 507 strcpy(locName, "root"); 508 } 509 510 root = ures_openDirect(NULL, locName, &errorCode); 511 if(U_FAILURE(errorCode)) { 512 log_err("Can't open %s\n", locName); 513 continue; 514 } 515 */ 516 if (locCount <= 1) { 517 log_data_err("At least root needs to be installed\n"); 518 } 519 520 root = ures_openDirect(loadTestData(&errorCode), "structLocale", &errorCode); 521 if(U_FAILURE(errorCode)) { 522 log_data_err("Can't open structLocale\n"); 523 return; 524 } 525 for (locIndex = 0; locIndex < locCount; locIndex++) { 526 errorCode=U_ZERO_ERROR; 527 currLoc = uloc_getAvailable(locIndex); 528 currentLocale = ures_open(NULL, currLoc, &errorCode); 529 if(errorCode != U_ZERO_ERROR) { 530 if(U_SUCCESS(errorCode)) { 531 /* It's installed, but there is no data. 532 It's installed for the g18n white paper [grhoten] */ 533 log_err("ERROR: Locale %-5s not installed, and it should be, err %s\n", 534 uloc_getAvailable(locIndex), u_errorName(errorCode)); 535 } else { 536 log_err("%%%%%%% Unexpected error %d in %s %%%%%%%", 537 u_errorName(errorCode), 538 uloc_getAvailable(locIndex)); 539 } 540 ures_close(currentLocale); 541 continue; 542 } 543 ures_getStringByKey(currentLocale, "Version", NULL, &errorCode); 544 if(errorCode != U_ZERO_ERROR) { 545 log_err("No version information is available for locale %s, and it should be!\n", 546 currLoc); 547 } 548 else if (ures_getStringByKey(currentLocale, "Version", NULL, &errorCode)[0] == (UChar)(0x78)) { 549 log_verbose("WARNING: The locale %s is experimental! It shouldn't be listed as an installed locale.\n", 550 currLoc); 551 } 552 resolvedLoc = ures_getLocaleByType(currentLocale, ULOC_ACTUAL_LOCALE, &errorCode); 553 if (strcmp(resolvedLoc, currLoc) != 0) { 554 /* All locales have at least a Version resource. 555 If it's absolutely empty, then the previous test will fail too.*/ 556 /* Google Patch: tl_PH and fil_PH are aliases of each other */ 557 if (!(strcmp(currLoc, "tl_PH")==0 && strcmp(resolvedLoc, "fil_PH")==0) && 558 !(strcmp(currLoc, "tl")==0 && strcmp(resolvedLoc, "fil")==0)) 559 log_err("Locale resolves to different locale. Is %s an alias of %s?\n", 560 currLoc, resolvedLoc); 561 } 562 TestKeyInRootRecursive(root, "root", currentLocale, currLoc); 563 564 testLCID(currentLocale, currLoc); 565 566 ures_close(currentLocale); 567 } 568 569 ures_close(root); 570 } 571 #endif 572 573 static void 574 compareArrays(const char *keyName, 575 UResourceBundle *fromArray, const char *fromLocale, 576 UResourceBundle *toArray, const char *toLocale, 577 int32_t start, int32_t end) 578 { 579 int32_t fromSize = ures_getSize(fromArray); 580 int32_t toSize = ures_getSize(fromArray); 581 int32_t idx; 582 UErrorCode errorCode = U_ZERO_ERROR; 583 584 if (fromSize > toSize) { 585 fromSize = toSize; 586 log_err("Arrays are different size from \"%s\" to \"%s\"\n", 587 fromLocale, 588 toLocale); 589 } 590 591 for (idx = start; idx <= end; idx++) { 592 const UChar *fromBundleStr = ures_getStringByIndex(fromArray, idx, NULL, &errorCode); 593 const UChar *toBundleStr = ures_getStringByIndex(toArray, idx, NULL, &errorCode); 594 if (fromBundleStr && toBundleStr && u_strcmp(fromBundleStr, toBundleStr) != 0) 595 { 596 log_err("Difference for %s at index %d from %s= \"%s\" to %s= \"%s\"\n", 597 keyName, 598 idx, 599 fromLocale, 600 austrdup(fromBundleStr), 601 toLocale, 602 austrdup(toBundleStr)); 603 } 604 } 605 } 606 607 static void 608 compareConsistentCountryInfo(const char *fromLocale, const char *toLocale) { 609 UErrorCode errorCode = U_ZERO_ERROR; 610 UResourceBundle *fromArray, *toArray; 611 UResourceBundle *fromLocaleBund = ures_open(NULL, fromLocale, &errorCode); 612 UResourceBundle *toLocaleBund = ures_open(NULL, toLocale, &errorCode); 613 UResourceBundle *toCalendar, *fromCalendar, *toGregorian, *fromGregorian; 614 615 if(U_FAILURE(errorCode)) { 616 log_err("Can't open resource bundle %s or %s - %s\n", fromLocale, toLocale, u_errorName(errorCode)); 617 return; 618 } 619 fromCalendar = ures_getByKey(fromLocaleBund, "calendar", NULL, &errorCode); 620 fromGregorian = ures_getByKeyWithFallback(fromCalendar, "gregorian", NULL, &errorCode); 621 622 toCalendar = ures_getByKey(toLocaleBund, "calendar", NULL, &errorCode); 623 toGregorian = ures_getByKeyWithFallback(toCalendar, "gregorian", NULL, &errorCode); 624 625 fromArray = ures_getByKey(fromLocaleBund, "CurrencyElements", NULL, &errorCode); 626 toArray = ures_getByKey(toLocaleBund, "CurrencyElements", NULL, &errorCode); 627 if (strcmp(fromLocale, "en_CA") != 0) 628 { 629 /* The first one is probably localized. */ 630 compareArrays("CurrencyElements", fromArray, fromLocale, toArray, toLocale, 1, 2); 631 } 632 ures_close(fromArray); 633 ures_close(toArray); 634 635 fromArray = ures_getByKey(fromLocaleBund, "NumberPatterns", NULL, &errorCode); 636 toArray = ures_getByKey(toLocaleBund, "NumberPatterns", NULL, &errorCode); 637 if (strcmp(fromLocale, "en_CA") != 0) 638 { 639 compareArrays("NumberPatterns", fromArray, fromLocale, toArray, toLocale, 0, 3); 640 } 641 ures_close(fromArray); 642 ures_close(toArray); 643 644 /* Difficult to test properly */ 645 /* 646 fromArray = ures_getByKey(fromLocaleBund, "DateTimePatterns", NULL, &errorCode); 647 toArray = ures_getByKey(toLocaleBund, "DateTimePatterns", NULL, &errorCode); 648 { 649 compareArrays("DateTimePatterns", fromArray, fromLocale, toArray, toLocale); 650 } 651 ures_close(fromArray); 652 ures_close(toArray);*/ 653 654 fromArray = ures_getByKey(fromLocaleBund, "NumberElements", NULL, &errorCode); 655 toArray = ures_getByKey(toLocaleBund, "NumberElements", NULL, &errorCode); 656 if (strcmp(fromLocale, "en_CA") != 0) 657 { 658 compareArrays("NumberElements", fromArray, fromLocale, toArray, toLocale, 0, 3); 659 /* Index 4 is a script based 0 */ 660 compareArrays("NumberElements", fromArray, fromLocale, toArray, toLocale, 5, 10); 661 } 662 ures_close(fromArray); 663 ures_close(toArray); 664 ures_close(fromCalendar); 665 ures_close(toCalendar); 666 ures_close(fromGregorian); 667 ures_close(toGregorian); 668 669 ures_close(fromLocaleBund); 670 ures_close(toLocaleBund); 671 } 672 673 static void 674 TestConsistentCountryInfo(void) { 675 /* UResourceBundle *fromLocale, *toLocale;*/ 676 int32_t locCount = uloc_countAvailable(); 677 int32_t fromLocIndex, toLocIndex; 678 679 int32_t fromCountryLen, toCountryLen; 680 char fromCountry[ULOC_FULLNAME_CAPACITY], toCountry[ULOC_FULLNAME_CAPACITY]; 681 682 int32_t fromVariantLen, toVariantLen; 683 char fromVariant[ULOC_FULLNAME_CAPACITY], toVariant[ULOC_FULLNAME_CAPACITY]; 684 685 UErrorCode errorCode = U_ZERO_ERROR; 686 687 for (fromLocIndex = 0; fromLocIndex < locCount; fromLocIndex++) { 688 const char *fromLocale = uloc_getAvailable(fromLocIndex); 689 690 errorCode=U_ZERO_ERROR; 691 fromCountryLen = uloc_getCountry(fromLocale, fromCountry, ULOC_FULLNAME_CAPACITY, &errorCode); 692 if (fromCountryLen <= 0) { 693 /* Ignore countryless locales */ 694 continue; 695 } 696 fromVariantLen = uloc_getVariant(fromLocale, fromVariant, ULOC_FULLNAME_CAPACITY, &errorCode); 697 if (fromVariantLen > 0) { 698 /* Most variants are ignorable like PREEURO, or collation variants. */ 699 continue; 700 } 701 /* Start comparing only after the current index. 702 Previous loop should have already compared fromLocIndex. 703 */ 704 for (toLocIndex = fromLocIndex + 1; toLocIndex < locCount; toLocIndex++) { 705 const char *toLocale = uloc_getAvailable(toLocIndex); 706 707 toCountryLen = uloc_getCountry(toLocale, toCountry, ULOC_FULLNAME_CAPACITY, &errorCode); 708 if(U_FAILURE(errorCode)) { 709 log_err("Unknown failure fromLocale=%s toLocale=%s errorCode=%s\n", 710 fromLocale, toLocale, u_errorName(errorCode)); 711 continue; 712 } 713 714 if (toCountryLen <= 0) { 715 /* Ignore countryless locales */ 716 continue; 717 } 718 toVariantLen = uloc_getVariant(toLocale, toVariant, ULOC_FULLNAME_CAPACITY, &errorCode); 719 if (toVariantLen > 0) { 720 /* Most variants are ignorable like PREEURO, or collation variants. */ 721 /* They're a variant for a reason. */ 722 continue; 723 } 724 if (strcmp(fromCountry, toCountry) == 0) { 725 log_verbose("comparing fromLocale=%s toLocale=%s\n", 726 fromLocale, toLocale); 727 compareConsistentCountryInfo(fromLocale, toLocale); 728 } 729 } 730 } 731 } 732 733 static int32_t 734 findStringSetMismatch(const char *currLoc, const UChar *string, int32_t langSize, 735 const UChar *exemplarCharacters, int32_t exemplarLen, 736 UBool ignoreNumbers, UChar* badCharPtr) { 737 UErrorCode errorCode = U_ZERO_ERROR; 738 USet *origSet = uset_openPatternOptions(exemplarCharacters, exemplarLen, USET_CASE_INSENSITIVE, &errorCode); 739 USet *exemplarSet = createFlattenSet(origSet, &errorCode); 740 int32_t strIdx; 741 uset_close(origSet); 742 if (U_FAILURE(errorCode)) { 743 log_err("%s: error uset_openPattern returned %s\n", currLoc, u_errorName(errorCode)); 744 return -1; 745 } 746 747 for (strIdx = 0; strIdx < langSize; strIdx++) { 748 if (!uset_contains(exemplarSet, string[strIdx]) 749 && string[strIdx] != 0x0020 && string[strIdx] != 0x00A0 && string[strIdx] != 0x002e && string[strIdx] != 0x002c && string[strIdx] != 0x002d && string[strIdx] != 0x0027 && string[strIdx] != 0x005B && string[strIdx] != 0x005D && string[strIdx] != 0x2019 && string[strIdx] != 0x0f0b 750 && string[strIdx] != 0x200C && string[strIdx] != 0x200D) { 751 if (!ignoreNumbers || (ignoreNumbers && (string[strIdx] < 0x30 || string[strIdx] > 0x39))) { 752 uset_close(exemplarSet); 753 if (badCharPtr) { 754 *badCharPtr = string[strIdx]; 755 } 756 return strIdx; 757 } 758 } 759 } 760 uset_close(exemplarSet); 761 if (badCharPtr) { 762 *badCharPtr = 0; 763 } 764 return -1; 765 } 766 /* include non-invariant chars */ 767 static int32_t 768 myUCharsToChars(const UChar* us, char* cs, int32_t len){ 769 int32_t i=0; 770 for(; i< len; i++){ 771 if(us[i] < 0x7f){ 772 cs[i] = (char)us[i]; 773 }else{ 774 return -1; 775 } 776 } 777 return i; 778 } 779 static void 780 findSetMatch( UScriptCode *scriptCodes, int32_t scriptsLen, 781 USet *exemplarSet, 782 const char *locale){ 783 USet *scripts[10]= {0}; 784 char pattern[256] = { '[', ':', 0x000 }; 785 int32_t patternLen; 786 UChar uPattern[256] = {0}; 787 UErrorCode status = U_ZERO_ERROR; 788 int32_t i; 789 790 /* create the sets with script codes */ 791 for(i = 0; i<scriptsLen; i++){ 792 strcat(pattern, uscript_getShortName(scriptCodes[i])); 793 strcat(pattern, ":]"); 794 patternLen = (int32_t)strlen(pattern); 795 u_charsToUChars(pattern, uPattern, patternLen); 796 scripts[i] = uset_openPattern(uPattern, patternLen, &status); 797 if(U_FAILURE(status)){ 798 log_err("Could not create set for pattern %s. Error: %s\n", pattern, u_errorName(status)); 799 return; 800 } 801 pattern[2] = 0; 802 } 803 if (strcmp(locale, "uk") == 0 || strcmp(locale, "uk_UA") == 0) { 804 /* Special addition. Add the modifying apostrophe, which isn't in Cyrillic. */ 805 uset_add(scripts[0], 0x2bc); 806 } 807 if(U_SUCCESS(status)){ 808 UBool existsInScript = FALSE; 809 /* iterate over the exemplarSet and ascertain if all 810 * UChars in exemplarSet belong to the scripts returned 811 * by getScript 812 */ 813 int32_t count = uset_getItemCount(exemplarSet); 814 815 for( i=0; i < count; i++){ 816 UChar32 start = 0; 817 UChar32 end = 0; 818 UChar *str = NULL; 819 int32_t strCapacity = 0; 820 821 strCapacity = uset_getItem(exemplarSet, i, &start, &end, str, strCapacity, &status); 822 if(U_SUCCESS(status)){ 823 int32_t j; 824 if(strCapacity == 0){ 825 /* ok the item is a range */ 826 for( j = 0; j < scriptsLen; j++){ 827 if(uset_containsRange(scripts[j], start, end) == TRUE){ 828 existsInScript = TRUE; 829 } 830 } 831 if(existsInScript == FALSE){ 832 for( j = 0; j < scriptsLen; j++){ 833 UChar toPattern[500]={'\0'}; 834 char pat[500]={'\0'}; 835 int32_t len = uset_toPattern(scripts[j], toPattern, 500, TRUE, &status); 836 len = myUCharsToChars(toPattern, pat, len); 837 log_err("uset_indexOf(\\u%04X)=%i uset_indexOf(\\u%04X)=%i\n", start, uset_indexOf(scripts[0], start), end, uset_indexOf(scripts[0], end)); 838 if(len!=-1){ 839 log_err("Pattern: %s\n",pat); 840 } 841 } 842 log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale); 843 } 844 }else{ 845 strCapacity++; /* increment for NUL termination */ 846 /* allocate the str and call the api again */ 847 str = (UChar*) malloc(U_SIZEOF_UCHAR * strCapacity); 848 strCapacity = uset_getItem(exemplarSet, i, &start, &end, str, strCapacity, &status); 849 /* iterate over the scripts and figure out if the string contained is actually 850 * in the script set 851 */ 852 for( j = 0; j < scriptsLen; j++){ 853 if(uset_containsString(scripts[j],str, strCapacity) == TRUE){ 854 existsInScript = TRUE; 855 } 856 } 857 if(existsInScript == FALSE){ 858 log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale); 859 } 860 } 861 } 862 } 863 864 } 865 866 /* close the sets */ 867 for(i = 0; i<scriptsLen; i++){ 868 uset_close(scripts[i]); 869 } 870 } 871 872 static void VerifyTranslation(void) { 873 UResourceBundle *root, *currentLocale; 874 int32_t locCount = uloc_countAvailable(); 875 int32_t locIndex; 876 UErrorCode errorCode = U_ZERO_ERROR; 877 int32_t exemplarLen; 878 const UChar *exemplarCharacters; 879 const char *currLoc; 880 UScriptCode scripts[USCRIPT_CODE_LIMIT]; 881 int32_t numScripts; 882 int32_t idx; 883 int32_t end; 884 UResourceBundle *resArray; 885 886 if (locCount <= 1) { 887 log_data_err("At least root needs to be installed\n"); 888 } 889 890 root = ures_openDirect(NULL, "root", &errorCode); 891 if(U_FAILURE(errorCode)) { 892 log_data_err("Can't open root\n"); 893 return; 894 } 895 for (locIndex = 0; locIndex < locCount; locIndex++) { 896 errorCode=U_ZERO_ERROR; 897 currLoc = uloc_getAvailable(locIndex); 898 currentLocale = ures_open(NULL, currLoc, &errorCode); 899 if(errorCode != U_ZERO_ERROR) { 900 if(U_SUCCESS(errorCode)) { 901 /* It's installed, but there is no data. 902 It's installed for the g18n white paper [grhoten] */ 903 log_err("ERROR: Locale %-5s not installed, and it should be!\n", 904 uloc_getAvailable(locIndex)); 905 } else { 906 log_err("%%%%%%% Unexpected error %d in %s %%%%%%%", 907 u_errorName(errorCode), 908 uloc_getAvailable(locIndex)); 909 } 910 ures_close(currentLocale); 911 continue; 912 } 913 exemplarCharacters = ures_getStringByKey(currentLocale, "ExemplarCharacters", &exemplarLen, &errorCode); 914 if (U_FAILURE(errorCode)) { 915 log_err("error ures_getStringByKey returned %s\n", u_errorName(errorCode)); 916 } 917 else if (getTestOption(QUICK_OPTION) && exemplarLen > 2048) { 918 log_verbose("skipping test for %s\n", currLoc); 919 } 920 else if (uprv_strncmp(currLoc,"bem",3) == 0 || uprv_strncmp(currLoc,"mgo",3) == 0 || uprv_strncmp(currLoc,"nl",2) == 0) { 921 log_verbose("skipping test for %s, some month and country names known to use aux exemplars\n", currLoc); 922 } 923 else { 924 UChar langBuffer[128]; 925 int32_t langSize; 926 int32_t strIdx; 927 UChar badChar; 928 langSize = uloc_getDisplayLanguage(currLoc, currLoc, langBuffer, sizeof(langBuffer)/sizeof(langBuffer[0]), &errorCode); 929 if (U_FAILURE(errorCode)) { 930 log_err("error uloc_getDisplayLanguage returned %s\n", u_errorName(errorCode)); 931 } 932 else { 933 strIdx = findStringSetMismatch(currLoc, langBuffer, langSize, exemplarCharacters, exemplarLen, FALSE, &badChar); 934 if (strIdx >= 0) { 935 log_err("getDisplayLanguage(%s) at index %d returned characters not in the exemplar characters: %04X.\n", 936 currLoc, strIdx, badChar); 937 } 938 } 939 langSize = uloc_getDisplayCountry(currLoc, currLoc, langBuffer, sizeof(langBuffer)/sizeof(langBuffer[0]), &errorCode); 940 if (U_FAILURE(errorCode)) { 941 log_err("error uloc_getDisplayCountry returned %s\n", u_errorName(errorCode)); 942 } 943 else if (uprv_strstr(currLoc, "ti_") != currLoc || isICUVersionAtLeast(52, 0, 1)) { /* TODO: FIX or REMOVE this test! Was: restore DisplayCountry test for ti_* when cldrbug 3058 is fixed) - but CldrBug:3058 is wontfix */ 944 strIdx = findStringSetMismatch(currLoc, langBuffer, langSize, exemplarCharacters, exemplarLen, FALSE, &badChar); 945 if (strIdx >= 0) { 946 log_err("getDisplayCountry(%s) at index %d returned characters not in the exemplar characters: %04X.\n", 947 currLoc, strIdx, badChar); 948 } 949 } 950 { 951 UResourceBundle* cal = ures_getByKey(currentLocale, "calendar", NULL, &errorCode); 952 UResourceBundle* greg = ures_getByKeyWithFallback(cal, "gregorian", NULL, &errorCode); 953 UResourceBundle* names = ures_getByKeyWithFallback(greg, "dayNames", NULL, &errorCode); 954 UResourceBundle* format = ures_getByKeyWithFallback(names, "format", NULL, &errorCode); 955 resArray = ures_getByKeyWithFallback(format, "wide", NULL, &errorCode); 956 957 if (U_FAILURE(errorCode)) { 958 log_err("error ures_getByKey returned %s\n", u_errorName(errorCode)); 959 } 960 if (getTestOption(QUICK_OPTION)) { 961 end = 1; 962 } 963 else { 964 end = ures_getSize(resArray); 965 } 966 967 968 for (idx = 0; idx < end; idx++) { 969 const UChar *fromBundleStr = ures_getStringByIndex(resArray, idx, &langSize, &errorCode); 970 if (U_FAILURE(errorCode)) { 971 log_err("error ures_getStringByIndex(%d) returned %s\n", idx, u_errorName(errorCode)); 972 continue; 973 } 974 if (uprv_strstr(currLoc, "uz_Arab") != currLoc || isICUVersionAtLeast(52, 0, 1)) { /* TODO: FIX or REMOVE this test! */ 975 strIdx = findStringSetMismatch(currLoc, fromBundleStr, langSize, exemplarCharacters, exemplarLen, TRUE, &badChar); 976 if (strIdx >= 0) { 977 log_err("getDayNames(%s, %d) at index %d returned characters not in the exemplar characters: %04X.\n", 978 currLoc, idx, strIdx, badChar); 979 } 980 } 981 } 982 ures_close(resArray); 983 ures_close(format); 984 ures_close(names); 985 986 names = ures_getByKeyWithFallback(greg, "monthNames", NULL, &errorCode); 987 format = ures_getByKeyWithFallback(names,"format", NULL, &errorCode); 988 resArray = ures_getByKeyWithFallback(format, "wide", NULL, &errorCode); 989 if (U_FAILURE(errorCode)) { 990 log_err("error ures_getByKey returned %s\n", u_errorName(errorCode)); 991 } 992 if (getTestOption(QUICK_OPTION)) { 993 end = 1; 994 } 995 else { 996 end = ures_getSize(resArray); 997 } 998 999 for (idx = 0; idx < end; idx++) { 1000 const UChar *fromBundleStr = ures_getStringByIndex(resArray, idx, &langSize, &errorCode); 1001 if (U_FAILURE(errorCode)) { 1002 log_err("error ures_getStringByIndex(%d) returned %s\n", idx, u_errorName(errorCode)); 1003 continue; 1004 } 1005 if (uprv_strstr(currLoc, "uz_Arab") != currLoc || isICUVersionAtLeast(52, 0, 1)) { /* TODO: FIX or REMOVE this test! */ 1006 strIdx = findStringSetMismatch(currLoc, fromBundleStr, langSize, exemplarCharacters, exemplarLen, TRUE, &badChar); 1007 if (strIdx >= 0) { 1008 log_err("getMonthNames(%s, %d) at index %d returned characters not in the exemplar characters: %04X.\n", 1009 currLoc, idx, strIdx, badChar); 1010 } 1011 } 1012 } 1013 ures_close(resArray); 1014 ures_close(format); 1015 ures_close(names); 1016 ures_close(greg); 1017 ures_close(cal); 1018 } 1019 errorCode = U_ZERO_ERROR; 1020 numScripts = uscript_getCode(currLoc, scripts, sizeof(scripts)/sizeof(scripts[0]), &errorCode); 1021 if (numScripts == 0) { 1022 log_err("uscript_getCode(%s) doesn't work.\n", currLoc); 1023 }else if(scripts[0] == USCRIPT_COMMON){ 1024 log_err("uscript_getCode(%s) returned USCRIPT_COMMON.\n", currLoc); 1025 } 1026 1027 /* test that the scripts are a superset of exemplar characters. */ 1028 { 1029 ULocaleData *uld = ulocdata_open(currLoc,&errorCode); 1030 USet *exemplarSet = ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &errorCode); 1031 /* test if exemplar characters are part of script code */ 1032 findSetMatch(scripts, numScripts, exemplarSet, currLoc); 1033 uset_close(exemplarSet); 1034 ulocdata_close(uld); 1035 } 1036 1037 /* test that the paperSize API works */ 1038 { 1039 int32_t height=0, width=0; 1040 ulocdata_getPaperSize(currLoc, &height, &width, &errorCode); 1041 if(U_FAILURE(errorCode)){ 1042 log_err("ulocdata_getPaperSize failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode)); 1043 } 1044 if(strstr(currLoc, "_US")!=NULL && height != 279 && width != 216 ){ 1045 log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc); 1046 } 1047 } 1048 /* test that the MeasurementSystem works API works */ 1049 { 1050 UMeasurementSystem measurementSystem = ulocdata_getMeasurementSystem(currLoc, &errorCode); 1051 if(U_FAILURE(errorCode)){ 1052 log_err("ulocdata_getMeasurementSystem failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode)); 1053 } 1054 if(strstr(currLoc, "_US")!=NULL || strstr(currLoc, "_MM")!=NULL || strstr(currLoc, "_LR")!=NULL){ 1055 if(measurementSystem != UMS_US){ 1056 log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc); 1057 } 1058 }else if(measurementSystem != UMS_SI){ 1059 log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc); 1060 } 1061 } 1062 } 1063 ures_close(currentLocale); 1064 } 1065 1066 ures_close(root); 1067 } 1068 1069 /* adjust this limit as appropriate */ 1070 #define MAX_SCRIPTS_PER_LOCALE 8 1071 1072 static void TestExemplarSet(void){ 1073 int32_t i, j, k, m, n; 1074 int32_t equalCount = 0; 1075 UErrorCode ec = U_ZERO_ERROR; 1076 UEnumeration* avail; 1077 USet* exemplarSets[2]; 1078 USet* unassignedSet; 1079 UScriptCode code[MAX_SCRIPTS_PER_LOCALE]; 1080 USet* codeSets[MAX_SCRIPTS_PER_LOCALE]; 1081 int32_t codeLen; 1082 char cbuf[32]; /* 9 should be enough */ 1083 UChar ubuf[64]; /* adjust as needed */ 1084 UBool existsInScript; 1085 int32_t itemCount; 1086 int32_t strLen; 1087 UChar32 start, end; 1088 1089 unassignedSet = NULL; 1090 exemplarSets[0] = NULL; 1091 exemplarSets[1] = NULL; 1092 for (i=0; i<MAX_SCRIPTS_PER_LOCALE; ++i) { 1093 codeSets[i] = NULL; 1094 } 1095 1096 avail = ures_openAvailableLocales(NULL, &ec); 1097 if (!assertSuccess("ures_openAvailableLocales", &ec)) goto END; 1098 n = uenum_count(avail, &ec); 1099 if (!assertSuccess("uenum_count", &ec)) goto END; 1100 1101 u_uastrcpy(ubuf, "[:unassigned:]"); 1102 unassignedSet = uset_openPattern(ubuf, -1, &ec); 1103 if (!assertSuccess("uset_openPattern", &ec)) goto END; 1104 1105 for(i=0; i<n; i++){ 1106 const char* locale = uenum_next(avail, NULL, &ec); 1107 if (!assertSuccess("uenum_next", &ec)) goto END; 1108 log_verbose("%s\n", locale); 1109 for (k=0; k<2; ++k) { 1110 uint32_t option = (k==0) ? 0 : USET_CASE_INSENSITIVE; 1111 ULocaleData *uld = ulocdata_open(locale,&ec); 1112 USet* exemplarSet = ulocdata_getExemplarSet(uld,NULL, option, ULOCDATA_ES_STANDARD, &ec); 1113 uset_close(exemplarSets[k]); 1114 ulocdata_close(uld); 1115 exemplarSets[k] = exemplarSet; 1116 if (!assertSuccess("ulocaledata_getExemplarSet", &ec)) goto END; 1117 1118 if (uset_containsSome(exemplarSet, unassignedSet)) { 1119 log_err("ExemplarSet contains unassigned characters for locale : %s\n", locale); 1120 } 1121 codeLen = uscript_getCode(locale, code, 8, &ec); 1122 if (!assertSuccess("uscript_getCode", &ec)) goto END; 1123 1124 for (j=0; j<MAX_SCRIPTS_PER_LOCALE; ++j) { 1125 uset_close(codeSets[j]); 1126 codeSets[j] = NULL; 1127 } 1128 for (j=0; j<codeLen; ++j) { 1129 uprv_strcpy(cbuf, "[:"); 1130 if(code[j]==-1){ 1131 log_err("USCRIPT_INVALID_CODE returned for locale: %s\n", locale); 1132 continue; 1133 } 1134 uprv_strcat(cbuf, uscript_getShortName(code[j])); 1135 uprv_strcat(cbuf, ":]"); 1136 u_uastrcpy(ubuf, cbuf); 1137 codeSets[j] = uset_openPattern(ubuf, -1, &ec); 1138 } 1139 if (!assertSuccess("uset_openPattern", &ec)) goto END; 1140 1141 existsInScript = FALSE; 1142 itemCount = uset_getItemCount(exemplarSet); 1143 for (m=0; m<itemCount && !existsInScript; ++m) { 1144 strLen = uset_getItem(exemplarSet, m, &start, &end, ubuf, 1145 sizeof(ubuf)/sizeof(ubuf[0]), &ec); 1146 /* failure here might mean str[] needs to be larger */ 1147 if (!assertSuccess("uset_getItem", &ec)) goto END; 1148 if (strLen == 0) { 1149 for (j=0; j<codeLen; ++j) { 1150 if (codeSets[j]!=NULL && uset_containsRange(codeSets[j], start, end)) { 1151 existsInScript = TRUE; 1152 break; 1153 } 1154 } 1155 } else { 1156 for (j=0; j<codeLen; ++j) { 1157 if (codeSets[j]!=NULL && uset_containsString(codeSets[j], ubuf, strLen)) { 1158 existsInScript = TRUE; 1159 break; 1160 } 1161 } 1162 } 1163 } 1164 1165 if (existsInScript == FALSE){ 1166 log_err("ExemplarSet containment failed for locale : %s\n", locale); 1167 } 1168 } 1169 assertTrue("case-folded is a superset", 1170 uset_containsAll(exemplarSets[1], exemplarSets[0])); 1171 if (uset_equals(exemplarSets[1], exemplarSets[0])) { 1172 ++equalCount; 1173 } 1174 } 1175 /* Note: The case-folded set should sometimes be a strict superset 1176 and sometimes be equal. */ 1177 assertTrue("case-folded is sometimes a strict superset, and sometimes equal", 1178 equalCount > 0 && equalCount < n); 1179 1180 END: 1181 uenum_close(avail); 1182 uset_close(exemplarSets[0]); 1183 uset_close(exemplarSets[1]); 1184 uset_close(unassignedSet); 1185 for (i=0; i<MAX_SCRIPTS_PER_LOCALE; ++i) { 1186 uset_close(codeSets[i]); 1187 } 1188 } 1189 1190 static void TestLocaleDisplayPattern(void){ 1191 UErrorCode status = U_ZERO_ERROR; 1192 UChar pattern[32] = {0,}; 1193 UChar separator[32] = {0,}; 1194 ULocaleData *uld = ulocdata_open(uloc_getDefault(), &status); 1195 1196 if(U_FAILURE(status)){ 1197 log_data_err("ulocdata_open error"); 1198 return; 1199 } 1200 ulocdata_getLocaleDisplayPattern(uld, pattern, 32, &status); 1201 if (U_FAILURE(status)){ 1202 log_err("ulocdata_getLocaleDisplayPattern error!"); 1203 } 1204 status = U_ZERO_ERROR; 1205 ulocdata_getLocaleSeparator(uld, separator, 32, &status); 1206 if (U_FAILURE(status)){ 1207 log_err("ulocdata_getLocaleSeparator error!"); 1208 } 1209 ulocdata_close(uld); 1210 } 1211 1212 static void TestCoverage(void){ 1213 ULocaleDataDelimiterType types[] = { 1214 ULOCDATA_QUOTATION_START, /* Quotation start */ 1215 ULOCDATA_QUOTATION_END, /* Quotation end */ 1216 ULOCDATA_ALT_QUOTATION_START, /* Alternate quotation start */ 1217 ULOCDATA_ALT_QUOTATION_END, /* Alternate quotation end */ 1218 ULOCDATA_DELIMITER_COUNT 1219 }; 1220 int i; 1221 UBool sub; 1222 UErrorCode status = U_ZERO_ERROR; 1223 ULocaleData *uld = ulocdata_open(uloc_getDefault(), &status); 1224 1225 if(U_FAILURE(status)){ 1226 log_data_err("ulocdata_open error"); 1227 return; 1228 } 1229 1230 1231 for(i = 0; i < ULOCDATA_DELIMITER_COUNT; i++){ 1232 UChar result[32] = {0,}; 1233 status = U_ZERO_ERROR; 1234 ulocdata_getDelimiter(uld, types[i], result, 32, &status); 1235 if (U_FAILURE(status)){ 1236 log_err("ulocdata_getgetDelimiter error with type %d", types[i]); 1237 } 1238 } 1239 1240 sub = ulocdata_getNoSubstitute(uld); 1241 ulocdata_setNoSubstitute(uld,sub); 1242 ulocdata_close(uld); 1243 } 1244 1245 static void TestIndexChars(void) { 1246 /* Very basic test of ULOCDATA_ES_INDEX. 1247 * No comprehensive test of data, just basic check that the code path is alive. 1248 */ 1249 UErrorCode status = U_ZERO_ERROR; 1250 ULocaleData *uld; 1251 USet *exemplarChars; 1252 USet *indexChars; 1253 1254 uld = ulocdata_open("en", &status); 1255 exemplarChars = uset_openEmpty(); 1256 indexChars = uset_openEmpty(); 1257 ulocdata_getExemplarSet(uld, exemplarChars, 0, ULOCDATA_ES_STANDARD, &status); 1258 ulocdata_getExemplarSet(uld, indexChars, 0, ULOCDATA_ES_INDEX, &status); 1259 if (U_FAILURE(status)) { 1260 log_data_err("File %s, line %d, Failure opening exemplar chars: %s", __FILE__, __LINE__, u_errorName(status)); 1261 goto close_sets; 1262 } 1263 /* en data, standard exemplars are [a-z], lower case. */ 1264 /* en data, index characters are [A-Z], upper case. */ 1265 if ((uset_contains(exemplarChars, (UChar32)0x41) || uset_contains(indexChars, (UChar32)0x61))) { 1266 log_err("File %s, line %d, Exemplar characters incorrect.", __FILE__, __LINE__ ); 1267 goto close_sets; 1268 } 1269 if (!(uset_contains(exemplarChars, (UChar32)0x61) && uset_contains(indexChars, (UChar32)0x41) )) { 1270 log_err("File %s, line %d, Exemplar characters incorrect.", __FILE__, __LINE__ ); 1271 goto close_sets; 1272 } 1273 1274 close_sets: 1275 uset_close(exemplarChars); 1276 uset_close(indexChars); 1277 ulocdata_close(uld); 1278 } 1279 1280 1281 1282 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION 1283 static void TestCurrencyList(void){ 1284 #if !UCONFIG_NO_FORMATTING 1285 UErrorCode errorCode = U_ZERO_ERROR; 1286 int32_t structLocaleCount, currencyCount; 1287 UEnumeration *en = ucurr_openISOCurrencies(UCURR_ALL, &errorCode); 1288 const char *isoCode, *structISOCode; 1289 UResourceBundle *subBundle; 1290 UResourceBundle *currencies = ures_openDirect(loadTestData(&errorCode), "structLocale", &errorCode); 1291 if(U_FAILURE(errorCode)) { 1292 log_data_err("Can't open structLocale\n"); 1293 return; 1294 } 1295 currencies = ures_getByKey(currencies, "Currencies", currencies, &errorCode); 1296 currencyCount = uenum_count(en, &errorCode); 1297 structLocaleCount = ures_getSize(currencies); 1298 if (currencyCount != structLocaleCount) { 1299 log_err("structLocale(%d) and ISO4217(%d) currency list are out of sync.\n", structLocaleCount, currencyCount); 1300 #if U_CHARSET_FAMILY == U_ASCII_FAMILY 1301 ures_resetIterator(currencies); 1302 while ((isoCode = uenum_next(en, NULL, &errorCode)) != NULL && ures_hasNext(currencies)) { 1303 subBundle = ures_getNextResource(currencies, NULL, &errorCode); 1304 structISOCode = ures_getKey(subBundle); 1305 ures_close(subBundle); 1306 if (strcmp(structISOCode, isoCode) != 0) { 1307 log_err("First difference found at structLocale(%s) and ISO4217(%s).\n", structISOCode, isoCode); 1308 break; 1309 } 1310 } 1311 #endif 1312 } 1313 ures_close(currencies); 1314 uenum_close(en); 1315 #endif 1316 } 1317 #endif 1318 1319 static void TestAvailableIsoCodes(void){ 1320 #if !UCONFIG_NO_FORMATTING 1321 UErrorCode errorCode = U_ZERO_ERROR; 1322 const char* eurCode = "EUR"; 1323 const char* usdCode = "USD"; 1324 const char* lastCode = "RHD"; 1325 const char* zzzCode = "ZZZ"; 1326 UDate date1950 = (UDate)-630720000000.0;/* year 1950 */ 1327 UDate date1970 = (UDate)0.0; /* year 1970 */ 1328 UDate date1975 = (UDate)173448000000.0; /* year 1975 */ 1329 UDate date1978 = (UDate)260172000000.0; /* year 1978 */ 1330 UDate date1981 = (UDate)346896000000.0; /* year 1981 */ 1331 UDate date1992 = (UDate)693792000000.0; /* year 1992 */ 1332 UChar* isoCode = (UChar*)malloc(sizeof(UChar) * (uprv_strlen(usdCode) + 1)); 1333 1334 /* testing available codes with no time ranges */ 1335 u_charsToUChars(eurCode, isoCode, uprv_strlen(usdCode) + 1); 1336 if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == FALSE) { 1337 log_data_err("FAIL: ISO code (%s) is not found.\n", eurCode); 1338 } 1339 1340 u_charsToUChars(usdCode, isoCode, uprv_strlen(zzzCode) + 1); 1341 if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == FALSE) { 1342 log_data_err("FAIL: ISO code (%s) is not found.\n", usdCode); 1343 } 1344 1345 u_charsToUChars(zzzCode, isoCode, uprv_strlen(zzzCode) + 1); 1346 if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == TRUE) { 1347 log_err("FAIL: ISO code (%s) is reported as available, but it doesn't exist.\n", zzzCode); 1348 } 1349 1350 u_charsToUChars(lastCode, isoCode, uprv_strlen(zzzCode) + 1); 1351 if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == FALSE) { 1352 log_data_err("FAIL: ISO code (%s) is not found.\n", lastCode); 1353 } 1354 1355 /* RHD was used from 1970-02-17 to 1980-04-18*/ 1356 1357 /* to = null */ 1358 if (ucurr_isAvailable(isoCode, date1970, U_DATE_MAX, &errorCode) == FALSE) { 1359 log_data_err("FAIL: ISO code (%s) was available in time range >1970-01-01.\n", lastCode); 1360 } 1361 1362 if (ucurr_isAvailable(isoCode, date1975, U_DATE_MAX, &errorCode) == FALSE) { 1363 log_data_err("FAIL: ISO code (%s) was available in time range >1975.\n", lastCode); 1364 } 1365 1366 if (ucurr_isAvailable(isoCode, date1981, U_DATE_MAX, &errorCode) == TRUE) { 1367 log_err("FAIL: ISO code (%s) was not available in time range >1981.\n", lastCode); 1368 } 1369 1370 /* from = null */ 1371 if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1970, &errorCode) == TRUE) { 1372 log_err("FAIL: ISO code (%s) was not available in time range <1970.\n", lastCode); 1373 } 1374 1375 if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1975, &errorCode) == FALSE) { 1376 log_data_err("FAIL: ISO code (%s) was available in time range <1975.\n", lastCode); 1377 } 1378 1379 if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1981, &errorCode) == FALSE) { 1380 log_data_err("FAIL: ISO code (%s) was available in time range <1981.\n", lastCode); 1381 } 1382 1383 /* full ranges */ 1384 if (ucurr_isAvailable(isoCode, date1975, date1978, &errorCode) == FALSE) { 1385 log_data_err("FAIL: ISO code (%s) was available in time range 1975-1978.\n", lastCode); 1386 } 1387 1388 if (ucurr_isAvailable(isoCode, date1970, date1975, &errorCode) == FALSE) { 1389 log_data_err("FAIL: ISO code (%s) was available in time range 1970-1975.\n", lastCode); 1390 } 1391 1392 if (ucurr_isAvailable(isoCode, date1975, date1981, &errorCode) == FALSE) { 1393 log_data_err("FAIL: ISO code (%s) was available in time range 1975-1981.\n", lastCode); 1394 } 1395 1396 if (ucurr_isAvailable(isoCode, date1970, date1981, &errorCode) == FALSE) { 1397 log_data_err("FAIL: ISO code (%s) was available in time range 1970-1981.\n", lastCode); 1398 } 1399 1400 if (ucurr_isAvailable(isoCode, date1981, date1992, &errorCode) == TRUE) { 1401 log_err("FAIL: ISO code (%s) was not available in time range 1981-1992.\n", lastCode); 1402 } 1403 1404 if (ucurr_isAvailable(isoCode, date1950, date1970, &errorCode) == TRUE) { 1405 log_err("FAIL: ISO code (%s) was not available in time range 1950-1970.\n", lastCode); 1406 } 1407 1408 /* wrong range - from > to*/ 1409 if (ucurr_isAvailable(isoCode, date1975, date1970, &errorCode) == TRUE) { 1410 log_err("FAIL: Wrong range 1975-1970 for ISO code (%s) was not reported.\n", lastCode); 1411 } else if (errorCode != U_ILLEGAL_ARGUMENT_ERROR) { 1412 log_data_err("FAIL: Error code not reported for wrong range 1975-1970 for ISO code (%s).\n", lastCode); 1413 } 1414 1415 free(isoCode); 1416 #endif 1417 } 1418 1419 #define TESTCASE(name) addTest(root, &name, "tsutil/cldrtest/" #name) 1420 1421 void addCLDRTest(TestNode** root); 1422 1423 void addCLDRTest(TestNode** root) 1424 { 1425 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION 1426 TESTCASE(TestLocaleStructure); 1427 TESTCASE(TestCurrencyList); 1428 #endif 1429 TESTCASE(TestConsistentCountryInfo); 1430 TESTCASE(VerifyTranslation); 1431 TESTCASE(TestExemplarSet); 1432 TESTCASE(TestLocaleDisplayPattern); 1433 TESTCASE(TestCoverage); 1434 TESTCASE(TestIndexChars); 1435 TESTCASE(TestAvailableIsoCodes); 1436 } 1437 1438