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