1 /********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
8 #include "unicode/ures.h"
9 #include "unicode/ustring.h"
10 #include "unicode/uset.h"
11 #include "unicode/udat.h"
12 #include "unicode/uscript.h"
13 #include "unicode/ulocdata.h"
18 static UBool
isCurrencyPreEuro(const char* currencyKey
){
19 if( strcmp(currencyKey
, "PTE") == 0 ||
20 strcmp(currencyKey
, "ESP") == 0 ||
21 strcmp(currencyKey
, "LUF") == 0 ||
22 strcmp(currencyKey
, "GRD") == 0 ||
23 strcmp(currencyKey
, "BEF") == 0 ||
24 strcmp(currencyKey
, "ITL") == 0 ){
30 TestKeyInRootRecursive(UResourceBundle
*root
, const char *rootName
,
31 UResourceBundle
*currentBundle
, const char *locale
) {
32 UErrorCode errorCode
= U_ZERO_ERROR
;
33 UResourceBundle
*subRootBundle
= NULL
, *subBundle
= NULL
;
35 ures_resetIterator(root
);
36 ures_resetIterator(currentBundle
);
37 while (ures_hasNext(currentBundle
)) {
38 const char *subBundleKey
= NULL
;
39 const char *currentBundleKey
= NULL
;
41 errorCode
= U_ZERO_ERROR
;
42 currentBundleKey
= ures_getKey(currentBundle
);
43 subBundle
= ures_getNextResource(currentBundle
, NULL
, &errorCode
);
44 if (U_FAILURE(errorCode
)) {
45 log_err("Can't open a resource for locale %s. Error: %s\n", locale
, u_errorName(errorCode
));
48 subBundleKey
= ures_getKey(subBundle
);
51 subRootBundle
= ures_getByKey(root
, subBundleKey
, NULL
, &errorCode
);
52 if (U_FAILURE(errorCode
)) {
53 log_err("Can't open a resource with key \"%s\" in \"%s\" from %s for locale \"%s\"\n",
55 ures_getKey(currentBundle
),
58 ures_close(subBundle
);
61 if (ures_getType(subRootBundle
) != ures_getType(subBundle
)) {
62 log_err("key \"%s\" in \"%s\" has a different type from root for locale \"%s\"\n"
63 "\troot=%d, locale=%d\n",
65 ures_getKey(currentBundle
),
67 ures_getType(subRootBundle
),
68 ures_getType(subBundle
));
71 else if (ures_getType(subBundle
) == URES_INT_VECTOR
) {
73 int32_t subBundleSize
;
75 UBool sameArray
= TRUE
;
76 const int32_t *subRootBundleArr
= ures_getIntVector(subRootBundle
, &minSize
, &errorCode
);
77 const int32_t *subBundleArr
= ures_getIntVector(subBundle
, &subBundleSize
, &errorCode
);
79 if (minSize
> subBundleSize
) {
80 minSize
= subBundleSize
;
81 log_err("Arrays are different size with key \"%s\" in \"%s\" from root for locale \"%s\"\n",
83 ures_getKey(currentBundle
),
87 for (idx
= 0; idx
< minSize
&& sameArray
; idx
++) {
88 if (subRootBundleArr
[idx
] != subBundleArr
[idx
]) {
91 if (strcmp(subBundleKey
, "DateTimeElements") == 0
92 && (subBundleArr
[idx
] < 1 || 7 < subBundleArr
[idx
]))
94 log_err("Value out of range with key \"%s\" at index %d in \"%s\" for locale \"%s\"\n",
97 ures_getKey(currentBundle
),
101 /* Special exception es_US and DateTimeElements */
103 && !(strcmp(locale
, "es_US") == 0 && strcmp(subBundleKey
, "DateTimeElements") == 0))
105 log_err("Integer vectors are the same with key \"%s\" in \"%s\" from root for locale \"%s\"\n",
107 ures_getKey(currentBundle
),
111 else if (ures_getType(subBundle
) == URES_ARRAY
) {
112 UResourceBundle
*subSubBundle
= ures_getByIndex(subBundle
, 0, NULL
, &errorCode
);
113 UResourceBundle
*subSubRootBundle
= ures_getByIndex(subRootBundle
, 0, NULL
, &errorCode
);
115 if (U_SUCCESS(errorCode
)
116 && (ures_getType(subSubBundle
) == URES_ARRAY
|| ures_getType(subSubRootBundle
) == URES_ARRAY
))
118 /* TODO: Properly check for 2D arrays and zoneStrings */
119 if (subBundleKey
!= NULL
&& strcmp(subBundleKey
, "zoneStrings") == 0) {
120 /* int32_t minSize = ures_getSize(subBundle);
123 for (idx = 0; idx < minSize; idx++) {
124 UResourceBundle *subSubBundleAtIndex = ures_getByIndex(subBundle, idx, NULL, &errorCode);
125 if (ures_getSize(subSubBundleAtIndex) != 6) {
126 log_err("zoneStrings at index %d has wrong size for locale \"%s\". array size=%d\n",
129 ures_getSize(subSubBundleAtIndex));
131 ures_close(subSubBundleAtIndex);
135 /* Here is one of the recursive parts */
136 TestKeyInRootRecursive(subRootBundle
, rootName
, subBundle
, locale
);
140 int32_t minSize
= ures_getSize(subRootBundle
);
142 UBool sameArray
= TRUE
;
144 if (minSize
> ures_getSize(subBundle
)) {
145 minSize
= ures_getSize(subBundle
);
148 if ((subBundleKey
== NULL
149 || (subBundleKey
!= NULL
&& strcmp(subBundleKey
, "LocaleScript") != 0 && !isCurrencyPreEuro(subBundleKey
)))
150 && ures_getSize(subRootBundle
) != ures_getSize(subBundle
))
152 log_err("Different size array with key \"%s\" in \"%s\" from root for locale \"%s\"\n"
153 "\troot array size=%d, locale array size=%d\n",
155 ures_getKey(currentBundle
),
157 ures_getSize(subRootBundle
),
158 ures_getSize(subBundle
));
161 if(isCurrencyPreEuro(subBundleKey) && ures_getSize(subBundle)!=3){
162 log_err("Different size array with key \"%s\" in \"%s\" for locale \"%s\" the expected size is 3 got size=%d\n",
164 ures_getKey(currentBundle),
166 ures_getSize(subBundle));
169 for (idx
= 0; idx
< minSize
; idx
++) {
170 int32_t rootStrLen
, localeStrLen
;
171 const UChar
*rootStr
= ures_getStringByIndex(subRootBundle
,idx
,&rootStrLen
,&errorCode
);
172 const UChar
*localeStr
= ures_getStringByIndex(subBundle
,idx
,&localeStrLen
,&errorCode
);
173 if (rootStr
&& localeStr
&& U_SUCCESS(errorCode
)) {
174 if (u_strcmp(rootStr
, localeStr
) != 0) {
179 log_err("Got a NULL string with key \"%s\" in \"%s\" at index %d for root or locale \"%s\"\n",
181 ures_getKey(currentBundle
),
186 if (localeStr
[0] == (UChar
)0x20) {
187 log_err("key \"%s\" at index %d in \"%s\" starts with a space in locale \"%s\"\n",
190 ures_getKey(currentBundle
),
193 else if (localeStr
[localeStrLen
- 1] == (UChar
)0x20) {
194 log_err("key \"%s\" at index %d in \"%s\" ends with a space in locale \"%s\"\n",
197 ures_getKey(currentBundle
),
200 else if (subBundleKey
!= NULL
201 && strcmp(subBundleKey
, "DateTimePatterns") == 0)
204 const UChar
*localeStrItr
= localeStr
;
205 while (*localeStrItr
) {
206 if (*localeStrItr
== (UChar
)0x27 /* ' */) {
209 else if ((quoted
% 2) == 0) {
210 /* Search for unquoted characters */
211 if (4 <= idx
&& idx
<= 7
212 && (*localeStrItr
== (UChar
)0x6B /* k */
213 || *localeStrItr
== (UChar
)0x48 /* H */
214 || *localeStrItr
== (UChar
)0x6D /* m */
215 || *localeStrItr
== (UChar
)0x73 /* s */
216 || *localeStrItr
== (UChar
)0x53 /* S */
217 || *localeStrItr
== (UChar
)0x61 /* a */
218 || *localeStrItr
== (UChar
)0x68 /* h */
219 || *localeStrItr
== (UChar
)0x7A /* z */))
221 log_err("key \"%s\" at index %d has time pattern chars in date for locale \"%s\"\n",
226 else if (0 <= idx
&& idx
<= 3
227 && (*localeStrItr
== (UChar
)0x47 /* G */
228 || *localeStrItr
== (UChar
)0x79 /* y */
229 || *localeStrItr
== (UChar
)0x4D /* M */
230 || *localeStrItr
== (UChar
)0x64 /* d */
231 || *localeStrItr
== (UChar
)0x45 /* E */
232 || *localeStrItr
== (UChar
)0x44 /* D */
233 || *localeStrItr
== (UChar
)0x46 /* F */
234 || *localeStrItr
== (UChar
)0x77 /* w */
235 || *localeStrItr
== (UChar
)0x57 /* W */))
237 log_err("key \"%s\" at index %d has date pattern chars in time for locale \"%s\"\n",
246 else if (idx
== 4 && subBundleKey
!= NULL
247 && strcmp(subBundleKey
, "NumberElements") == 0
248 && u_charDigitValue(localeStr
[0]) != 0)
250 log_err("key \"%s\" at index %d has a non-zero based number for locale \"%s\"\n",
256 /* if (sameArray && strcmp(rootName, "root") == 0) {
257 log_err("Arrays are the same with key \"%s\" in \"%s\" from root for locale \"%s\"\n",
259 ures_getKey(currentBundle),
263 ures_close(subSubBundle
);
264 ures_close(subSubRootBundle
);
266 else if (ures_getType(subBundle
) == URES_STRING
) {
268 const UChar
*string
= ures_getString(subBundle
, &len
, &errorCode
);
269 if (U_FAILURE(errorCode
) || string
== NULL
) {
270 log_err("Can't open a string with key \"%s\" in \"%s\" for locale \"%s\"\n",
272 ures_getKey(currentBundle
),
274 } else if (string
[0] == (UChar
)0x20) {
275 log_err("key \"%s\" in \"%s\" starts with a space in locale \"%s\"\n",
277 ures_getKey(currentBundle
),
279 } else if (string
[len
- 1] == (UChar
)0x20) {
280 log_err("key \"%s\" in \"%s\" ends with a space in locale \"%s\"\n",
282 ures_getKey(currentBundle
),
284 } else if (strcmp(subBundleKey
, "localPatternChars") == 0) {
285 /* Check well-formedness of localPatternChars. First, the
286 * length must match the number of fields defined by
287 * DateFormat. Second, each character in the string must
288 * be in the set [A-Za-z]. Finally, each character must be
292 #if !UCONFIG_NO_FORMATTING
293 if (len
!= UDAT_FIELD_COUNT
) {
294 log_err("key \"%s\" has the wrong number of characters in locale \"%s\"\n",
299 /* Check char validity. */
300 for (i
=0; i
<len
; ++i
) {
301 if (!((string
[i
] >= 65/*'A'*/ && string
[i
] <= 90/*'Z'*/) ||
302 (string
[i
] >= 97/*'a'*/ && string
[i
] <= 122/*'z'*/))) {
303 log_err("key \"%s\" has illegal character '%c' in locale \"%s\"\n",
308 /* Do O(n^2) check for duplicate chars. */
309 for (j
=0; j
<i
; ++j
) {
310 if (string
[j
] == string
[i
]) {
311 log_err("key \"%s\" has duplicate character '%c' in locale \"%s\"\n",
319 /* No fallback was done. Check for duplicate data */
320 /* The ures_* API does not do fallback of sub-resource bundles,
321 So we can't do this now. */
323 else if (strcmp(locale
, "root") != 0 && errorCode
== U_ZERO_ERROR
) {
325 const UChar
*rootString
= ures_getString(subRootBundle
, &len
, &errorCode
);
326 if (U_FAILURE(errorCode
) || rootString
== NULL
) {
327 log_err("Can't open a string with key \"%s\" in \"%s\" in root\n",
328 ures_getKey(subRootBundle
),
329 ures_getKey(currentBundle
));
331 } else if (u_strcmp(string
, rootString
) == 0) {
332 if (strcmp(locale
, "de_CH") != 0 && strcmp(subBundleKey
, "Countries") != 0 &&
333 strcmp(subBundleKey
, "Version") != 0) {
334 log_err("Found duplicate data with key \"%s\" in \"%s\" in locale \"%s\"\n",
335 ures_getKey(subRootBundle
),
336 ures_getKey(currentBundle
),
340 /* Ignore for now. */
341 /* Can be fixed if fallback through de locale was done. */
342 log_verbose("Skipping key %s in %s\n", subBundleKey
, locale
);
348 else if (ures_getType(subBundle
) == URES_TABLE
) {
349 /* Here is one of the recursive parts */
350 TestKeyInRootRecursive(subRootBundle
, rootName
, subBundle
, locale
);
352 else if (ures_getType(subBundle
) == URES_BINARY
|| ures_getType(subBundle
) == URES_INT
) {
353 /* Can't do anything to check it */
354 /* We'll assume it's all correct */
355 if (strcmp(subBundleKey
, "MeasurementSystem") != 0) {
356 log_verbose("Skipping key \"%s\" in \"%s\" for locale \"%s\"\n",
358 ures_getKey(currentBundle
),
361 /* Testing for MeasurementSystem is done in VerifyTranslation */
364 log_err("Type %d for key \"%s\" in \"%s\" is unknown for locale \"%s\"\n",
365 ures_getType(subBundle
),
367 ures_getKey(currentBundle
),
370 ures_close(subRootBundle
);
371 ures_close(subBundle
);
377 testLCID(UResourceBundle
*currentBundle
,
378 const char *localeName
)
380 UErrorCode status
= U_ZERO_ERROR
;
381 uint32_t expectedLCID
;
382 char lcidStringC
[64] = {0};
384 expectedLCID
= uloc_getLCID(localeName
);
385 if (expectedLCID
== 0) {
386 log_verbose("INFO: %-5s does not have any LCID mapping\n",
391 status
= U_ZERO_ERROR
;
392 uprv_strcpy(lcidStringC
, uprv_convertToPosix(expectedLCID
, &status
));
393 if (U_FAILURE(status
)) {
394 log_err("ERROR: %.4x does not have a POSIX mapping due to %s\n",
395 expectedLCID
, u_errorName(status
));
398 if(strcmp(localeName
, lcidStringC
) != 0) {
401 uloc_getLanguage(localeName
, langName
, sizeof(langName
), &status
);
402 uloc_getLanguage(lcidStringC
, langLCID
, sizeof(langLCID
), &status
);
404 if (strcmp(langName
, langLCID
) == 0) {
405 log_verbose("WARNING: %-5s resolves to %s (0x%.4x)\n",
406 localeName
, lcidStringC
, expectedLCID
);
409 log_err("ERROR: %-5s has 0x%.4x and the number resolves wrongfully to %s\n",
410 localeName
, expectedLCID
, lcidStringC
);
416 TestLocaleStructure(void) {
417 UResourceBundle
*root
, *currentLocale
;
418 int32_t locCount
= uloc_countAvailable();
420 UErrorCode errorCode
= U_ZERO_ERROR
;
423 /* TODO: Compare against parent's data too. This code can't handle fallbacks that some tools do already. */
424 /* char locName[ULOC_FULLNAME_CAPACITY];
427 for (locIndex = 0; locIndex < locCount; locIndex++) {
428 errorCode=U_ZERO_ERROR;
429 strcpy(locName, uloc_getAvailable(locIndex));
430 locNamePtr = strrchr(locName, '_');
435 strcpy(locName, "root");
438 root = ures_openDirect(NULL, locName, &errorCode);
439 if(U_FAILURE(errorCode)) {
440 log_err("Can't open %s\n", locName);
445 log_data_err("At least root needs to be installed\n");
448 root
= ures_openDirect(loadTestData(&errorCode
), "structLocale", &errorCode
);
449 if(U_FAILURE(errorCode
)) {
450 log_data_err("Can't open structLocale\n");
453 for (locIndex
= 0; locIndex
< locCount
; locIndex
++) {
454 errorCode
=U_ZERO_ERROR
;
455 currLoc
= uloc_getAvailable(locIndex
);
456 currentLocale
= ures_open(NULL
, currLoc
, &errorCode
);
457 if(errorCode
!= U_ZERO_ERROR
) {
458 if(U_SUCCESS(errorCode
)) {
459 /* It's installed, but there is no data.
460 It's installed for the g18n white paper [grhoten] */
461 log_err("ERROR: Locale %-5s not installed, and it should be!\n",
462 uloc_getAvailable(locIndex
));
464 log_err("%%%%%%% Unexpected error %d in %s %%%%%%%",
465 u_errorName(errorCode
),
466 uloc_getAvailable(locIndex
));
468 ures_close(currentLocale
);
471 ures_getStringByKey(currentLocale
, "Version", NULL
, &errorCode
);
472 if(errorCode
!= U_ZERO_ERROR
) {
473 log_err("No version information is available for locale %s, and it should be!\n",
476 else if (ures_getStringByKey(currentLocale
, "Version", NULL
, &errorCode
)[0] == (UChar
)(0x78)) {
477 log_verbose("WARNING: The locale %s is experimental! It shouldn't be listed as an installed locale.\n",
480 TestKeyInRootRecursive(root
, "root", currentLocale
, currLoc
);
482 testLCID(currentLocale
, currLoc
);
484 ures_close(currentLocale
);
491 compareArrays(const char *keyName
,
492 UResourceBundle
*fromArray
, const char *fromLocale
,
493 UResourceBundle
*toArray
, const char *toLocale
,
494 int32_t start
, int32_t end
)
496 int32_t fromSize
= ures_getSize(fromArray
);
497 int32_t toSize
= ures_getSize(fromArray
);
499 UErrorCode errorCode
= U_ZERO_ERROR
;
501 if (fromSize
> toSize
) {
503 log_err("Arrays are different size from \"%s\" to \"%s\"\n",
508 for (idx
= start
; idx
<= end
; idx
++) {
509 const UChar
*fromBundleStr
= ures_getStringByIndex(fromArray
, idx
, NULL
, &errorCode
);
510 const UChar
*toBundleStr
= ures_getStringByIndex(toArray
, idx
, NULL
, &errorCode
);
511 if (fromBundleStr
&& toBundleStr
&& u_strcmp(fromBundleStr
, toBundleStr
) != 0)
513 log_err("Difference for %s at index %d from %s= \"%s\" to %s= \"%s\"\n",
517 austrdup(fromBundleStr
),
519 austrdup(toBundleStr
));
525 compareConsistentCountryInfo(const char *fromLocale
, const char *toLocale
) {
526 UErrorCode errorCode
= U_ZERO_ERROR
;
527 UResourceBundle
*fromDateTimeElements
, *toDateTimeElements
, *fromWeekendData
= NULL
, *toWeekendData
= NULL
;
528 UResourceBundle
*fromArray
, *toArray
;
529 UResourceBundle
*fromLocaleBund
= ures_open(NULL
, fromLocale
, &errorCode
);
530 UResourceBundle
*toLocaleBund
= ures_open(NULL
, toLocale
, &errorCode
);
531 UResourceBundle
*toCalendar
, *fromCalendar
, *toGregorian
, *fromGregorian
;
533 if(U_FAILURE(errorCode
)) {
534 log_err("Can't open resource bundle %s or %s - %s\n", fromLocale
, toLocale
, u_errorName(errorCode
));
537 fromCalendar
= ures_getByKey(fromLocaleBund
, "calendar", NULL
, &errorCode
);
538 fromGregorian
= ures_getByKeyWithFallback(fromCalendar
, "gregorian", NULL
, &errorCode
);
539 fromDateTimeElements
= ures_getByKeyWithFallback(fromGregorian
, "DateTimeElements", NULL
, &errorCode
);
541 toCalendar
= ures_getByKey(toLocaleBund
, "calendar", NULL
, &errorCode
);
542 toGregorian
= ures_getByKeyWithFallback(toCalendar
, "gregorian", NULL
, &errorCode
);
543 toDateTimeElements
= ures_getByKeyWithFallback(toGregorian
, "DateTimeElements", NULL
, &errorCode
);
545 if(U_FAILURE(errorCode
)){
546 log_err("Did not get DateTimeElements from the bundle %s or %s\n", fromLocale
, toLocale
);
550 fromWeekendData
= ures_getByKeyWithFallback(fromGregorian
, "weekend", NULL
, &errorCode
);
551 if(U_FAILURE(errorCode
)){
552 log_err("Did not get weekend data from the bundle %s to compare against %s\n", fromLocale
, toLocale
);
555 toWeekendData
= ures_getByKeyWithFallback(toGregorian
, "weekend", NULL
, &errorCode
);
556 if(U_FAILURE(errorCode
)){
557 log_err("Did not get weekend data from the bundle %s to compare against %s\n", toLocale
, fromLocale
);
561 if (strcmp(fromLocale
, "ar_IN") != 0)
566 const int32_t *fromBundleArr
= ures_getIntVector(fromDateTimeElements
, &fromSize
, &errorCode
);
567 const int32_t *toBundleArr
= ures_getIntVector(toDateTimeElements
, &toSize
, &errorCode
);
569 if (fromSize
> toSize
) {
571 log_err("Arrays are different size with key \"DateTimeElements\" from \"%s\" to \"%s\"\n",
576 for (idx
= 0; idx
< fromSize
; idx
++) {
577 if (fromBundleArr
[idx
] != toBundleArr
[idx
]) {
578 log_err("Difference with key \"DateTimeElements\" at index %d from \"%s\" to \"%s\"\n",
586 /* test for weekend data */
591 const int32_t *fromBundleArr
= ures_getIntVector(fromWeekendData
, &fromSize
, &errorCode
);
592 const int32_t *toBundleArr
= ures_getIntVector(toWeekendData
, &toSize
, &errorCode
);
594 if (fromSize
> toSize
) {
596 log_err("Arrays are different size with key \"weekend\" data from \"%s\" to \"%s\"\n",
601 for (idx
= 0; idx
< fromSize
; idx
++) {
602 if (fromBundleArr
[idx
] != toBundleArr
[idx
]) {
603 log_err("Difference with key \"weekend\" data at index %d from \"%s\" to \"%s\"\n",
611 fromArray
= ures_getByKey(fromLocaleBund
, "CurrencyElements", NULL
, &errorCode
);
612 toArray
= ures_getByKey(toLocaleBund
, "CurrencyElements", NULL
, &errorCode
);
613 if (strcmp(fromLocale
, "en_CA") != 0)
615 /* The first one is probably localized. */
616 compareArrays("CurrencyElements", fromArray
, fromLocale
, toArray
, toLocale
, 1, 2);
618 ures_close(fromArray
);
621 fromArray
= ures_getByKey(fromLocaleBund
, "NumberPatterns", NULL
, &errorCode
);
622 toArray
= ures_getByKey(toLocaleBund
, "NumberPatterns", NULL
, &errorCode
);
623 if (strcmp(fromLocale
, "en_CA") != 0)
625 compareArrays("NumberPatterns", fromArray
, fromLocale
, toArray
, toLocale
, 0, 3);
627 ures_close(fromArray
);
630 /* Difficult to test properly */
632 fromArray = ures_getByKey(fromLocaleBund, "DateTimePatterns", NULL, &errorCode);
633 toArray = ures_getByKey(toLocaleBund, "DateTimePatterns", NULL, &errorCode);
635 compareArrays("DateTimePatterns", fromArray, fromLocale, toArray, toLocale);
637 ures_close(fromArray);
638 ures_close(toArray);*/
640 fromArray
= ures_getByKey(fromLocaleBund
, "NumberElements", NULL
, &errorCode
);
641 toArray
= ures_getByKey(toLocaleBund
, "NumberElements", NULL
, &errorCode
);
642 if (strcmp(fromLocale
, "en_CA") != 0)
644 compareArrays("NumberElements", fromArray
, fromLocale
, toArray
, toLocale
, 0, 3);
645 /* Index 4 is a script based 0 */
646 compareArrays("NumberElements", fromArray
, fromLocale
, toArray
, toLocale
, 5, 10);
648 ures_close(fromArray
);
652 ures_close(fromDateTimeElements
);
653 ures_close(toDateTimeElements
);
654 ures_close(fromWeekendData
);
655 ures_close(toWeekendData
);
657 ures_close(fromCalendar
);
658 ures_close(toCalendar
);
659 ures_close(fromGregorian
);
660 ures_close(toGregorian
);
662 ures_close(fromLocaleBund
);
663 ures_close(toLocaleBund
);
667 TestConsistentCountryInfo(void) {
668 /* UResourceBundle *fromLocale, *toLocale;*/
669 int32_t locCount
= uloc_countAvailable();
670 int32_t fromLocIndex
, toLocIndex
;
672 int32_t fromCountryLen
, toCountryLen
;
673 char fromCountry
[ULOC_FULLNAME_CAPACITY
], toCountry
[ULOC_FULLNAME_CAPACITY
];
675 int32_t fromVariantLen
, toVariantLen
;
676 char fromVariant
[ULOC_FULLNAME_CAPACITY
], toVariant
[ULOC_FULLNAME_CAPACITY
];
678 UErrorCode errorCode
= U_ZERO_ERROR
;
680 for (fromLocIndex
= 0; fromLocIndex
< locCount
; fromLocIndex
++) {
681 const char *fromLocale
= uloc_getAvailable(fromLocIndex
);
683 errorCode
=U_ZERO_ERROR
;
684 fromCountryLen
= uloc_getCountry(fromLocale
, fromCountry
, ULOC_FULLNAME_CAPACITY
, &errorCode
);
685 if (fromCountryLen
<= 0) {
686 /* Ignore countryless locales */
689 fromVariantLen
= uloc_getVariant(fromLocale
, fromVariant
, ULOC_FULLNAME_CAPACITY
, &errorCode
);
690 if (fromVariantLen
> 0) {
691 /* Most variants are ignorable like PREEURO, or collation variants. */
694 /* Start comparing only after the current index.
695 Previous loop should have already compared fromLocIndex.
697 for (toLocIndex
= fromLocIndex
+ 1; toLocIndex
< locCount
; toLocIndex
++) {
698 const char *toLocale
= uloc_getAvailable(toLocIndex
);
700 toCountryLen
= uloc_getCountry(toLocale
, toCountry
, ULOC_FULLNAME_CAPACITY
, &errorCode
);
701 if(U_FAILURE(errorCode
)) {
702 log_err("Unknown failure fromLocale=%s toLocale=%s errorCode=%s\n",
703 fromLocale
, toLocale
, u_errorName(errorCode
));
707 if (toCountryLen
<= 0) {
708 /* Ignore countryless locales */
711 toVariantLen
= uloc_getVariant(toLocale
, toVariant
, ULOC_FULLNAME_CAPACITY
, &errorCode
);
712 if (toVariantLen
> 0) {
713 /* Most variants are ignorable like PREEURO, or collation variants. */
714 /* They're a variant for a reason. */
717 if (strcmp(fromCountry
, toCountry
) == 0) {
718 log_verbose("comparing fromLocale=%s toLocale=%s\n",
719 fromLocale
, toLocale
);
720 compareConsistentCountryInfo(fromLocale
, toLocale
);
727 findStringSetMismatch(const char *currLoc
, const UChar
*string
, int32_t langSize
,
728 const UChar
*exemplarCharacters
, int32_t exemplarLen
,
729 UBool ignoreNumbers
) {
730 UErrorCode errorCode
= U_ZERO_ERROR
;
731 USet
*exemplarSet
= uset_openPatternOptions(exemplarCharacters
, exemplarLen
, USET_CASE_INSENSITIVE
, &errorCode
);
733 if (U_FAILURE(errorCode
)) {
734 log_err("%s: error uset_openPattern returned %s\n", currLoc
, u_errorName(errorCode
));
738 for (strIdx
= 0; strIdx
< langSize
; strIdx
++) {
739 if (!uset_contains(exemplarSet
, string
[strIdx
])
740 && string
[strIdx
] != 0x0020 && string
[strIdx
] != 0x00A0 && string
[strIdx
] != 0x002e && string
[strIdx
] != 0x002c && string
[strIdx
] != 0x002d && string
[strIdx
] != 0x0027) {
741 if (!ignoreNumbers
|| (ignoreNumbers
&& (string
[strIdx
] < 0x30 || string
[strIdx
] > 0x39))) {
746 uset_close(exemplarSet
);
751 findSetMatch( UScriptCode
*scriptCodes
, int32_t scriptsLen
,
754 USet
*scripts
[10]= {0};
755 char pattern
[256] = { '[', ':', 0x000 };
757 UChar uPattern
[256] = {0};
758 UErrorCode status
= U_ZERO_ERROR
;
761 /* create the sets with script codes */
762 for(i
= 0; i
<scriptsLen
; i
++){
763 strcat(pattern
, uscript_getShortName(scriptCodes
[i
]));
764 strcat(pattern
, ":]");
765 patternLen
= (int32_t)strlen(pattern
);
766 u_charsToUChars(pattern
, uPattern
, patternLen
);
767 scripts
[i
] = uset_openPattern(uPattern
, patternLen
, &status
);
768 if(U_FAILURE(status
)){
769 log_err("Could not create set for patter %s. Error: %s\n", pattern
, u_errorName(status
));
774 if (strcmp(locale
, "uk") == 0 || strcmp(locale
, "uk_UA") == 0) {
775 /* Special addition. Add the modifying apostrophe, which isn't in Cyrillic. */
776 uset_add(scripts
[0], 0x2bc);
778 if(U_SUCCESS(status
)){
779 UBool existsInScript
= FALSE
;
780 /* iterate over the exemplarSet and ascertain if all
781 * UChars in exemplarSet belong to the scripts returned
784 int32_t count
= uset_getItemCount(exemplarSet
);
786 for( i
=0; i
< count
; i
++){
790 int32_t strCapacity
= 0;
792 strCapacity
= uset_getItem(exemplarSet
, i
, &start
, &end
, str
, strCapacity
, &status
);
793 if(U_SUCCESS(status
)){
795 if(strCapacity
== 0){
796 /* ok the item is a range */
797 for( j
= 0; j
< scriptsLen
; j
++){
798 if(uset_containsRange(scripts
[j
], start
, end
) == TRUE
){
799 existsInScript
= TRUE
;
802 if(existsInScript
== FALSE
){
803 log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale
);
806 strCapacity
++; /* increment for NUL termination */
807 /* allocate the str and call the api again */
808 str
= (UChar
*) malloc(U_SIZEOF_UCHAR
* strCapacity
);
809 strCapacity
= uset_getItem(exemplarSet
, i
, &start
, &end
, str
, strCapacity
, &status
);
810 /* iterate over the scripts and figure out if the string contained is actually
813 for( j
= 0; j
< scriptsLen
; j
++){
814 if(uset_containsString(scripts
[j
],str
, strCapacity
) == TRUE
){
815 existsInScript
= TRUE
;
818 if(existsInScript
== FALSE
){
819 log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale
);
828 for(i
= 0; i
<scriptsLen
; i
++){
829 uset_close(scripts
[i
]);
833 static void VerifyTranslation(void) {
834 UResourceBundle
*root
, *currentLocale
;
835 int32_t locCount
= uloc_countAvailable();
837 UErrorCode errorCode
= U_ZERO_ERROR
;
839 const UChar
*exemplarCharacters
;
841 UScriptCode scripts
[USCRIPT_CODE_LIMIT
];
845 UResourceBundle
*resArray
;
848 log_data_err("At least root needs to be installed\n");
851 root
= ures_openDirect(NULL
, "root", &errorCode
);
852 if(U_FAILURE(errorCode
)) {
853 log_data_err("Can't open root\n");
856 for (locIndex
= 0; locIndex
< locCount
; locIndex
++) {
857 errorCode
=U_ZERO_ERROR
;
858 currLoc
= uloc_getAvailable(locIndex
);
859 currentLocale
= ures_open(NULL
, currLoc
, &errorCode
);
860 if(errorCode
!= U_ZERO_ERROR
) {
861 if(U_SUCCESS(errorCode
)) {
862 /* It's installed, but there is no data.
863 It's installed for the g18n white paper [grhoten] */
864 log_err("ERROR: Locale %-5s not installed, and it should be!\n",
865 uloc_getAvailable(locIndex
));
867 log_err("%%%%%%% Unexpected error %d in %s %%%%%%%",
868 u_errorName(errorCode
),
869 uloc_getAvailable(locIndex
));
871 ures_close(currentLocale
);
874 exemplarCharacters
= ures_getStringByKey(currentLocale
, "ExemplarCharacters", &exemplarLen
, &errorCode
);
875 if (U_FAILURE(errorCode
)) {
876 log_err("error ures_getStringByKey returned %s\n", u_errorName(errorCode
));
878 else if (QUICK
&& exemplarLen
> 2048) {
879 log_verbose("skipping test for %s\n", currLoc
);
882 UChar langBuffer
[128];
885 langSize
= uloc_getDisplayLanguage(currLoc
, currLoc
, langBuffer
, sizeof(langBuffer
)/sizeof(langBuffer
[0]), &errorCode
);
886 if (U_FAILURE(errorCode
)) {
887 log_err("error uloc_getDisplayLanguage returned %s\n", u_errorName(errorCode
));
890 strIdx
= findStringSetMismatch(currLoc
, langBuffer
, langSize
, exemplarCharacters
, exemplarLen
, FALSE
);
892 log_err("getDisplayLanguage(%s) at index %d returned characters not in the exemplar characters.\n",
896 langSize
= uloc_getDisplayCountry(currLoc
, currLoc
, langBuffer
, sizeof(langBuffer
)/sizeof(langBuffer
[0]), &errorCode
);
897 if (U_FAILURE(errorCode
)) {
898 log_err("error uloc_getDisplayCountry returned %s\n", u_errorName(errorCode
));
901 strIdx
= findStringSetMismatch(currLoc
, langBuffer
, langSize
, exemplarCharacters
, exemplarLen
, FALSE
);
903 log_err("getDisplayCountry(%s) at index %d returned characters not in the exemplar characters.\n",
908 UResourceBundle
* cal
= ures_getByKey(currentLocale
, "calendar", NULL
, &errorCode
);
909 UResourceBundle
* greg
= ures_getByKeyWithFallback(cal
, "gregorian", NULL
, &errorCode
);
910 UResourceBundle
* names
= ures_getByKeyWithFallback(greg
, "dayNames", NULL
, &errorCode
);
911 UResourceBundle
* format
= ures_getByKeyWithFallback(names
, "format", NULL
, &errorCode
);
912 resArray
= ures_getByKeyWithFallback(format
, "wide", NULL
, &errorCode
);
914 if (U_FAILURE(errorCode
)) {
915 log_err("error ures_getByKey returned %s\n", u_errorName(errorCode
));
921 end
= ures_getSize(resArray
);
925 for (idx
= 0; idx
< end
; idx
++) {
926 const UChar
*fromBundleStr
= ures_getStringByIndex(resArray
, idx
, &langSize
, &errorCode
);
927 if (U_FAILURE(errorCode
)) {
928 log_err("error ures_getStringByIndex(%d) returned %s\n", idx
, u_errorName(errorCode
));
931 strIdx
= findStringSetMismatch(currLoc
, fromBundleStr
, langSize
, exemplarCharacters
, exemplarLen
, TRUE
);
933 log_err("getDayNames(%s, %d) at index %d returned characters not in the exemplar characters.\n",
934 currLoc
, idx
, strIdx
);
937 ures_close(resArray
);
941 names
= ures_getByKeyWithFallback(greg
, "monthNames", NULL
, &errorCode
);
942 format
= ures_getByKeyWithFallback(names
,"format", NULL
, &errorCode
);
943 resArray
= ures_getByKeyWithFallback(format
, "wide", NULL
, &errorCode
);
944 if (U_FAILURE(errorCode
)) {
945 log_err("error ures_getByKey returned %s\n", u_errorName(errorCode
));
951 end
= ures_getSize(resArray
);
954 for (idx
= 0; idx
< end
; idx
++) {
955 const UChar
*fromBundleStr
= ures_getStringByIndex(resArray
, idx
, &langSize
, &errorCode
);
956 if (U_FAILURE(errorCode
)) {
957 log_err("error ures_getStringByIndex(%d) returned %s\n", idx
, u_errorName(errorCode
));
960 strIdx
= findStringSetMismatch(currLoc
, fromBundleStr
, langSize
, exemplarCharacters
, exemplarLen
, TRUE
);
962 log_err("getMonthNames(%s, %d) at index %d returned characters not in the exemplar characters.\n",
963 currLoc
, idx
, strIdx
);
966 ures_close(resArray
);
972 errorCode
= U_ZERO_ERROR
;
973 numScripts
= uscript_getCode(currLoc
, scripts
, sizeof(scripts
)/sizeof(scripts
[0]), &errorCode
);
974 if (numScripts
== 0) {
975 log_err("uscript_getCode(%s) doesn't work.\n", currLoc
);
976 }else if(scripts
[0] == USCRIPT_COMMON
){
977 log_err("uscript_getCode(%s) returned USCRIPT_COMMON.\n", currLoc
);
980 /* test that the scripts are a superset of exemplar characters. */
982 USet
*exemplarSet
= ulocdata_getExemplarSet(NULL
,currLoc
, 0, &errorCode
);
983 /* test if exemplar characters are part of script code */
984 findSetMatch(scripts
, numScripts
, exemplarSet
, currLoc
);
985 uset_close(exemplarSet
);
988 /* test that the paperSize API works */
990 int32_t height
=0, width
=0;
991 ulocdata_getPaperSize(currLoc
, &height
, &width
, &errorCode
);
992 if(U_FAILURE(errorCode
)){
993 log_err("ulocdata_getPaperSize failed for locale %s with error: %s \n", currLoc
, u_errorName(errorCode
));
995 if(strstr(currLoc
, "_US")!=NULL
&& height
!= 279 && width
!= 216 ){
996 log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc
);
999 /* test that the MeasurementSystem works API works */
1001 UMeasurementSystem measurementSystem
= ulocdata_getMeasurementSystem(currLoc
, &errorCode
);
1002 if(U_FAILURE(errorCode
)){
1003 log_err("ulocdata_getMeasurementSystem failed for locale %s with error: %s \n", currLoc
, u_errorName(errorCode
));
1005 if(strstr(currLoc
, "_US")!=NULL
){
1006 if(measurementSystem
!= UMS_US
){
1007 log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc
);
1009 }else if(measurementSystem
!= UMS_SI
){
1010 log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc
);
1014 ures_close(currentLocale
);
1020 /* adjust this limit as appropriate */
1021 #define MAX_SCRIPTS_PER_LOCALE 8
1023 static void TestExemplarSet(void){
1024 int32_t i
, j
, k
, m
, n
;
1025 int32_t equalCount
= 0;
1026 UErrorCode ec
= U_ZERO_ERROR
;
1027 UEnumeration
* avail
;
1028 USet
* exemplarSets
[2];
1029 UScriptCode code
[MAX_SCRIPTS_PER_LOCALE
];
1030 USet
* codeSets
[MAX_SCRIPTS_PER_LOCALE
];
1032 char cbuf
[32]; /* 9 should be enough */
1033 UChar ubuf
[64]; /* adjust as needed */
1034 UBool existsInScript
;
1039 exemplarSets
[0] = exemplarSets
[1] = NULL
;
1040 for (i
=0; i
<MAX_SCRIPTS_PER_LOCALE
; ++i
) {
1044 avail
= ures_openAvailableLocales(NULL
, &ec
);
1045 if (!assertSuccess("ures_openAvailableLocales", &ec
)) goto END
;
1046 n
= uenum_count(avail
, &ec
);
1047 if (!assertSuccess("uenum_count", &ec
)) goto END
;
1050 const char* locale
= uenum_next(avail
, NULL
, &ec
);
1051 if (!assertSuccess("uenum_next", &ec
)) goto END
;
1052 log_verbose("%s\n", locale
);
1053 for (k
=0; k
<2; ++k
) {
1054 uint32_t option
= (k
==0) ? 0 : USET_CASE_INSENSITIVE
;
1055 USet
* exemplarSet
= ulocdata_getExemplarSet(NULL
, locale
, option
, &ec
);
1056 uset_close(exemplarSets
[k
]);
1057 exemplarSets
[k
] = exemplarSet
;
1058 if (!assertSuccess("ulocaledata_getExemplarSet", &ec
)) goto END
;
1060 codeLen
= uscript_getCode(locale
, code
, 8, &ec
);
1061 if (!assertSuccess("uscript_getCode", &ec
)) goto END
;
1063 for (j
=0; j
<MAX_SCRIPTS_PER_LOCALE
; ++j
) {
1064 uset_close(codeSets
[j
]);
1067 for (j
=0; j
<codeLen
; ++j
) {
1068 uprv_strcpy(cbuf
, "[:");
1069 uprv_strcat(cbuf
, uscript_getShortName(code
[j
]));
1070 uprv_strcat(cbuf
, ":]");
1071 u_uastrcpy(ubuf
, cbuf
);
1072 codeSets
[j
] = uset_openPattern(ubuf
, -1, &ec
);
1074 if (!assertSuccess("uset_openPattern", &ec
)) goto END
;
1076 existsInScript
= FALSE
;
1077 itemCount
= uset_getItemCount(exemplarSet
);
1078 for (m
=0; m
<itemCount
&& !existsInScript
; ++m
) {
1079 strLen
= uset_getItem(exemplarSet
, m
, &start
, &end
, ubuf
,
1080 sizeof(ubuf
)/sizeof(ubuf
[0]), &ec
);
1081 /* failure here might mean str[] needs to be larger */
1082 if (!assertSuccess("uset_getItem", &ec
)) goto END
;
1084 for (j
=0; j
<codeLen
; ++j
) {
1085 if (uset_containsRange(codeSets
[j
], start
, end
)) {
1086 existsInScript
= TRUE
;
1091 for (j
=0; j
<codeLen
; ++j
) {
1092 if (uset_containsString(codeSets
[j
], ubuf
, strLen
)) {
1093 existsInScript
= TRUE
;
1100 if (existsInScript
== FALSE
){
1101 log_err("ExemplarSet containment failed for locale : %s", locale
);
1104 assertTrue("case-folded is a superset",
1105 uset_containsAll(exemplarSets
[1], exemplarSets
[0]));
1106 if (uset_equals(exemplarSets
[1], exemplarSets
[0])) {
1110 /* Note: The case-folded set should sometimes be a strict superset
1111 and sometimes be equal. */
1112 assertTrue("case-folded is sometimes a strict superset, and sometimes equal",
1113 equalCount
> 0 && equalCount
< n
);
1117 uset_close(exemplarSets
[0]);
1118 uset_close(exemplarSets
[1]);
1119 for (i
=0; i
<MAX_SCRIPTS_PER_LOCALE
; ++i
) {
1120 uset_close(codeSets
[i
]);
1124 #define TESTCASE(name) addTest(root, &name, "tsutil/cldrtest/" #name)
1126 void addCLDRTest(TestNode
** root
);
1128 void addCLDRTest(TestNode
** root
)
1130 TESTCASE(TestLocaleStructure
);
1131 TESTCASE(TestConsistentCountryInfo
);
1132 TESTCASE(VerifyTranslation
);
1133 TESTCASE(TestExemplarSet
);