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/uloc.h"
15#include "unicode/ulocdata.h"
16#include "cstring.h"
17#include "locmap.h"
18#include "uresimp.h"
19
20/*
21returns a new UnicodeSet that is a flattened form of the original
22UnicodeSet.
23*/
24static USet*
25createFlattenSet(USet *origSet, UErrorCode *status) {
26
27
28    USet *newSet = NULL;
29    int32_t origItemCount = 0;
30    int32_t idx, graphmeSize;
31    UChar32 start, end;
32    UChar graphme[64];
33    if (U_FAILURE(*status)) {
34        log_err("createFlattenSet called with %s\n", u_errorName(*status));
35        return NULL;
36    }
37    newSet = uset_open(1, 0);
38    origItemCount = uset_getItemCount(origSet);
39    for (idx = 0; idx < origItemCount; idx++) {
40        graphmeSize = uset_getItem(origSet, idx,
41            &start, &end,
42            graphme, (int32_t)(sizeof(graphme)/sizeof(graphme[0])),
43            status);
44        if (U_FAILURE(*status)) {
45            log_err("ERROR: uset_getItem returned %s\n", u_errorName(*status));
46            *status = U_ZERO_ERROR;
47        }
48        if (graphmeSize) {
49            uset_addAllCodePoints(newSet, graphme, graphmeSize);
50        }
51        else {
52            uset_addRange(newSet, start, end);
53        }
54    }
55    uset_closeOver(newSet,USET_CASE_INSENSITIVE);
56    return newSet;
57}
58
59static UBool
60isCurrencyPreEuro(const char* currencyKey){
61    if( strcmp(currencyKey, "PTE") == 0 ||
62        strcmp(currencyKey, "ESP") == 0 ||
63        strcmp(currencyKey, "LUF") == 0 ||
64        strcmp(currencyKey, "GRD") == 0 ||
65        strcmp(currencyKey, "BEF") == 0 ||
66        strcmp(currencyKey, "ITL") == 0 ||
67        strcmp(currencyKey, "EEK") == 0){
68            return TRUE;
69    }
70    return FALSE;
71}
72#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
73static void
74TestKeyInRootRecursive(UResourceBundle *root, const char *rootName,
75                       UResourceBundle *currentBundle, const char *locale) {
76    UErrorCode errorCode = U_ZERO_ERROR;
77    UResourceBundle *subRootBundle = NULL, *subBundle = NULL, *arr = NULL;
78
79    ures_resetIterator(root);
80    ures_resetIterator(currentBundle);
81    while (ures_hasNext(currentBundle)) {
82        const char *subBundleKey = NULL;
83        const char *currentBundleKey = NULL;
84
85        errorCode = U_ZERO_ERROR;
86        currentBundleKey = ures_getKey(currentBundle);
87        subBundle = ures_getNextResource(currentBundle, NULL, &errorCode);
88        if (U_FAILURE(errorCode)) {
89            log_err("Can't open a resource for lnocale %s. Error: %s\n", locale, u_errorName(errorCode));
90            continue;
91        }
92        subBundleKey = ures_getKey(subBundle);
93
94
95        subRootBundle = ures_getByKey(root, subBundleKey, NULL, &errorCode);
96        if (U_FAILURE(errorCode)) {
97            log_err("Can't open a resource with key \"%s\" in \"%s\" from %s for locale \"%s\"\n",
98                    subBundleKey,
99                    ures_getKey(currentBundle),
100                    rootName,
101                    locale);
102            ures_close(subBundle);
103            continue;
104        }
105        if (ures_getType(subRootBundle) != ures_getType(subBundle)) {
106            log_err("key \"%s\" in \"%s\" has a different type from root for locale \"%s\"\n"
107                    "\troot=%d, locale=%d\n",
108                    subBundleKey,
109                    ures_getKey(currentBundle),
110                    locale,
111                    ures_getType(subRootBundle),
112                    ures_getType(subBundle));
113            ures_close(subBundle);
114            continue;
115        }
116        else if (ures_getType(subBundle) == URES_INT_VECTOR) {
117            int32_t minSize;
118            int32_t subBundleSize;
119            int32_t idx;
120            UBool sameArray = TRUE;
121            const int32_t *subRootBundleArr = ures_getIntVector(subRootBundle, &minSize, &errorCode);
122            const int32_t *subBundleArr = ures_getIntVector(subBundle, &subBundleSize, &errorCode);
123
124            if (minSize > subBundleSize) {
125                minSize = subBundleSize;
126                log_err("Arrays are different size with key \"%s\" in \"%s\" from root for locale \"%s\"\n",
127                        subBundleKey,
128                        ures_getKey(currentBundle),
129                        locale);
130            }
131
132            for (idx = 0; idx < minSize && sameArray; idx++) {
133                if (subRootBundleArr[idx] != subBundleArr[idx]) {
134                    sameArray = FALSE;
135                }
136                if (strcmp(subBundleKey, "DateTimeElements") == 0
137                    && (subBundleArr[idx] < 1 || 7 < subBundleArr[idx]))
138                {
139                    log_err("Value out of range with key \"%s\" at index %d in \"%s\" for locale \"%s\"\n",
140                            subBundleKey,
141                            idx,
142                            ures_getKey(currentBundle),
143                            locale);
144                }
145            }
146            /* Special exception es_US and DateTimeElements */
147            if (sameArray
148                && !(strcmp(locale, "es_US") == 0 && strcmp(subBundleKey, "DateTimeElements") == 0))
149            {
150                log_err("Integer vectors are the same with key \"%s\" in \"%s\" from root for locale \"%s\"\n",
151                        subBundleKey,
152                        ures_getKey(currentBundle),
153                        locale);
154            }
155        }
156        else if (ures_getType(subBundle) == URES_ARRAY) {
157            UResourceBundle *subSubBundle = ures_getByIndex(subBundle, 0, NULL, &errorCode);
158            UResourceBundle *subSubRootBundle = ures_getByIndex(subRootBundle, 0, NULL, &errorCode);
159
160            if (U_SUCCESS(errorCode)
161                && (ures_getType(subSubBundle) == URES_ARRAY || ures_getType(subSubRootBundle) == URES_ARRAY))
162            {
163                /* Here is one of the recursive parts */
164                TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale);
165            }
166            else {
167                int32_t minSize = ures_getSize(subRootBundle);
168                int32_t idx;
169                UBool sameArray = TRUE;
170
171                if (minSize > ures_getSize(subBundle)) {
172                    minSize = ures_getSize(subBundle);
173                }
174
175                if ((subBundleKey == NULL
176                    || (subBundleKey != NULL &&  strcmp(subBundleKey, "LocaleScript") != 0 && !isCurrencyPreEuro(subBundleKey)))
177                    && ures_getSize(subRootBundle) != ures_getSize(subBundle))
178                {
179                    log_err("Different size array with key \"%s\" in \"%s\" from root for locale \"%s\"\n"
180                            "\troot array size=%d, locale array size=%d\n",
181                            subBundleKey,
182                            ures_getKey(currentBundle),
183                            locale,
184                            ures_getSize(subRootBundle),
185                            ures_getSize(subBundle));
186                }
187                /*
188                if(isCurrencyPreEuro(subBundleKey) && ures_getSize(subBundle)!=3){
189                    log_err("Different size array with key \"%s\" in \"%s\" for locale \"%s\" the expected size is 3 got size=%d\n",
190                            subBundleKey,
191                            ures_getKey(currentBundle),
192                            locale,
193                            ures_getSize(subBundle));
194                }
195                */
196                for (idx = 0; idx < minSize; idx++) {
197                    int32_t rootStrLen, localeStrLen;
198                    const UChar *rootStr = ures_getStringByIndex(subRootBundle,idx,&rootStrLen,&errorCode);
199                    const UChar *localeStr = ures_getStringByIndex(subBundle,idx,&localeStrLen,&errorCode);
200                    if (rootStr && localeStr && U_SUCCESS(errorCode)) {
201                        if (u_strcmp(rootStr, localeStr) != 0) {
202                            sameArray = FALSE;
203                        }
204                    }
205                    else {
206                        if ( rootStrLen > 1 && rootStr[0] == 0x41 && rootStr[1] >= 0x30 && rootStr[1] <= 0x39 ) {
207                           /* A2 or A4 in the root string indicates that the resource can optionally be an array instead of a */
208                           /* string.  Attempt to read it as an array. */
209                          errorCode = U_ZERO_ERROR;
210                          arr = ures_getByIndex(subBundle,idx,NULL,&errorCode);
211                          if (U_FAILURE(errorCode)) {
212                              log_err("Got a NULL string with key \"%s\" in \"%s\" at index %d for root or locale \"%s\"\n",
213                                      subBundleKey,
214                                      ures_getKey(currentBundle),
215                                      idx,
216                                      locale);
217                              continue;
218                          }
219                          if (ures_getType(arr) != URES_ARRAY || ures_getSize(arr) != (int32_t)rootStr[1] - 0x30) {
220                              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",
221                                      rootStr[1] - 0x30,
222                                      subBundleKey,
223                                      ures_getKey(currentBundle),
224                                      idx,
225                                      locale);
226                              ures_close(arr);
227                              continue;
228                          }
229                          localeStr = ures_getStringByIndex(arr,0,&localeStrLen,&errorCode);
230                          ures_close(arr);
231                          if (U_FAILURE(errorCode)) {
232                              log_err("Got something other than a string or array for key \"%s\" in \"%s\" at index %d for root or locale \"%s\"\n",
233                                      subBundleKey,
234                                      ures_getKey(currentBundle),
235                                      idx,
236                                      locale);
237                              continue;
238                          }
239                        } else {
240                            log_err("Got a NULL string with key \"%s\" in \"%s\" at index %d for root or locale \"%s\"\n",
241                                subBundleKey,
242                                ures_getKey(currentBundle),
243                                idx,
244                                locale);
245                            continue;
246                        }
247                    }
248                    if (localeStr[0] == (UChar)0x20) {
249                        log_err("key \"%s\" at index %d in \"%s\" starts with a space in locale \"%s\"\n",
250                                subBundleKey,
251                                idx,
252                                ures_getKey(currentBundle),
253                                locale);
254                    }
255                    else if ((localeStr[localeStrLen - 1] == (UChar)0x20) && (strcmp(subBundleKey,"separator") != 0)) {
256                        log_err("key \"%s\" at index %d in \"%s\" ends with a space in locale \"%s\"\n",
257                                subBundleKey,
258                                idx,
259                                ures_getKey(currentBundle),
260                                locale);
261                    }
262                    else if (subBundleKey != NULL
263                        && strcmp(subBundleKey, "DateTimePatterns") == 0)
264                    {
265                        int32_t quoted = 0;
266                        const UChar *localeStrItr = localeStr;
267                        while (*localeStrItr) {
268                            if (*localeStrItr == (UChar)0x27 /* ' */) {
269                                quoted++;
270                            }
271                            else if ((quoted % 2) == 0) {
272                                /* Search for unquoted characters */
273                                if (4 <= idx && idx <= 7
274                                    && (*localeStrItr == (UChar)0x6B /* k */
275                                    || *localeStrItr == (UChar)0x48 /* H */
276                                    || *localeStrItr == (UChar)0x6D /* m */
277                                    || *localeStrItr == (UChar)0x73 /* s */
278                                    || *localeStrItr == (UChar)0x53 /* S */
279                                    || *localeStrItr == (UChar)0x61 /* a */
280                                    || *localeStrItr == (UChar)0x68 /* h */
281                                    || *localeStrItr == (UChar)0x7A /* z */))
282                                {
283                                    log_err("key \"%s\" at index %d has time pattern chars in date for locale \"%s\"\n",
284                                            subBundleKey,
285                                            idx,
286                                            locale);
287                                }
288                                else if (0 <= idx && idx <= 3
289                                    && (*localeStrItr == (UChar)0x47 /* G */
290                                    || *localeStrItr == (UChar)0x79 /* y */
291                                    || *localeStrItr == (UChar)0x4D /* M */
292                                    || *localeStrItr == (UChar)0x64 /* d */
293                                    || *localeStrItr == (UChar)0x45 /* E */
294                                    || *localeStrItr == (UChar)0x44 /* D */
295                                    || *localeStrItr == (UChar)0x46 /* F */
296                                    || *localeStrItr == (UChar)0x77 /* w */
297                                    || *localeStrItr == (UChar)0x57 /* W */))
298                                {
299                                    log_err("key \"%s\" at index %d has date pattern chars in time for locale \"%s\"\n",
300                                            subBundleKey,
301                                            idx,
302                                            locale);
303                                }
304                            }
305                            localeStrItr++;
306                        }
307                    }
308                    else if (idx == 4 && subBundleKey != NULL
309                        && strcmp(subBundleKey, "NumberElements") == 0
310                        && u_charDigitValue(localeStr[0]) != 0)
311                    {
312                        log_err("key \"%s\" at index %d has a non-zero based number for locale \"%s\"\n",
313                                subBundleKey,
314                                idx,
315                                locale);
316                    }
317                }
318/*                if (sameArray && strcmp(rootName, "root") == 0) {
319                    log_err("Arrays are the same with key \"%s\" in \"%s\" from root for locale \"%s\"\n",
320                            subBundleKey,
321                            ures_getKey(currentBundle),
322                            locale);
323                }*/
324            }
325            ures_close(subSubBundle);
326            ures_close(subSubRootBundle);
327        }
328        else if (ures_getType(subBundle) == URES_STRING) {
329            int32_t len = 0;
330            const UChar *string = ures_getString(subBundle, &len, &errorCode);
331            if (U_FAILURE(errorCode) || string == NULL) {
332                log_err("Can't open a string with key \"%s\" in \"%s\" for locale \"%s\"\n",
333                        subBundleKey,
334                        ures_getKey(currentBundle),
335                        locale);
336            } else if (string[0] == (UChar)0x20) {
337                log_err("key \"%s\" in \"%s\" starts with a space in locale \"%s\"\n",
338                        subBundleKey,
339                        ures_getKey(currentBundle),
340                        locale);
341            /* localeDisplayPattern/separator can end with a space */
342            } else if (string[len - 1] == (UChar)0x20 && (strcmp(subBundleKey,"separator"))) {
343                log_err("key \"%s\" in \"%s\" ends with a space in locale \"%s\"\n",
344                        subBundleKey,
345                        ures_getKey(currentBundle),
346                        locale);
347            } else if (strcmp(subBundleKey, "localPatternChars") == 0) {
348                /* Note: We no longer import localPatternChars data starting
349                 * ICU 3.8.  So it never comes into this else if block. (ticket#5597)
350                 */
351
352                /* Check well-formedness of localPatternChars.  First, the
353                 * length must match the number of fields defined by
354                 * DateFormat.  Second, each character in the string must
355                 * be in the set [A-Za-z].  Finally, each character must be
356                 * unique.
357                 */
358                int32_t i,j;
359#if !UCONFIG_NO_FORMATTING
360                if (len != UDAT_FIELD_COUNT) {
361                    log_err("key \"%s\" has the wrong number of characters in locale \"%s\"\n",
362                            subBundleKey,
363                            locale);
364                }
365#endif
366                /* Check char validity. */
367                for (i=0; i<len; ++i) {
368                    if (!((string[i] >= 65/*'A'*/ && string[i] <= 90/*'Z'*/) ||
369                          (string[i] >= 97/*'a'*/ && string[i] <= 122/*'z'*/))) {
370                        log_err("key \"%s\" has illegal character '%c' in locale \"%s\"\n",
371                                subBundleKey,
372                                (char) string[i],
373                                locale);
374                    }
375                    /* Do O(n^2) check for duplicate chars. */
376                    for (j=0; j<i; ++j) {
377                        if (string[j] == string[i]) {
378                            log_err("key \"%s\" has duplicate character '%c' in locale \"%s\"\n",
379                                    subBundleKey,
380                                    (char) string[i],
381                                    locale);
382                        }
383                    }
384                }
385            }
386            /* No fallback was done. Check for duplicate data */
387            /* The ures_* API does not do fallback of sub-resource bundles,
388               So we can't do this now. */
389#if 0
390            else if (strcmp(locale, "root") != 0 && errorCode == U_ZERO_ERROR) {
391
392                const UChar *rootString = ures_getString(subRootBundle, &len, &errorCode);
393                if (U_FAILURE(errorCode) || rootString == NULL) {
394                    log_err("Can't open a string with key \"%s\" in \"%s\" in root\n",
395                            ures_getKey(subRootBundle),
396                            ures_getKey(currentBundle));
397                    continue;
398                } else if (u_strcmp(string, rootString) == 0) {
399                    if (strcmp(locale, "de_CH") != 0 && strcmp(subBundleKey, "Countries") != 0 &&
400                        strcmp(subBundleKey, "Version") != 0) {
401                        log_err("Found duplicate data with key \"%s\" in \"%s\" in locale \"%s\"\n",
402                                ures_getKey(subRootBundle),
403                                ures_getKey(currentBundle),
404                                locale);
405                    }
406                    else {
407                        /* Ignore for now. */
408                        /* Can be fixed if fallback through de locale was done. */
409                        log_verbose("Skipping key %s in %s\n", subBundleKey, locale);
410                    }
411                }
412            }
413#endif
414        }
415        else if (ures_getType(subBundle) == URES_TABLE) {
416            if (strcmp(subBundleKey, "availableFormats")!=0) {
417                /* Here is one of the recursive parts */
418                TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale);
419            }
420            else {
421                log_verbose("Skipping key %s in %s\n", subBundleKey, locale);
422            }
423        }
424        else if (ures_getType(subBundle) == URES_BINARY || ures_getType(subBundle) == URES_INT) {
425            /* Can't do anything to check it */
426            /* We'll assume it's all correct */
427            if (strcmp(subBundleKey, "MeasurementSystem") != 0) {
428                log_verbose("Skipping key \"%s\" in \"%s\" for locale \"%s\"\n",
429                        subBundleKey,
430                        ures_getKey(currentBundle),
431                        locale);
432            }
433            /* Testing for MeasurementSystem is done in VerifyTranslation */
434        }
435        else {
436            log_err("Type %d for key \"%s\" in \"%s\" is unknown for locale \"%s\"\n",
437                    ures_getType(subBundle),
438                    subBundleKey,
439                    ures_getKey(currentBundle),
440                    locale);
441        }
442        ures_close(subRootBundle);
443        ures_close(subBundle);
444    }
445}
446#endif
447
448static void
449testLCID(UResourceBundle *currentBundle,
450         const char *localeName)
451{
452    UErrorCode status = U_ZERO_ERROR;
453    uint32_t expectedLCID;
454    char lcidStringC[64] = {0};
455
456    expectedLCID = uloc_getLCID(localeName);
457    if (expectedLCID == 0) {
458        log_verbose("INFO:    %-5s does not have any LCID mapping\n",
459            localeName);
460        return;
461    }
462
463    status = U_ZERO_ERROR;
464    uprv_strcpy(lcidStringC, uprv_convertToPosix(expectedLCID, &status));
465    if (U_FAILURE(status)) {
466        log_err("ERROR:   %.4x does not have a POSIX mapping due to %s\n",
467            expectedLCID, u_errorName(status));
468    }
469
470    if(strcmp(localeName, lcidStringC) != 0) {
471        char langName[1024];
472        char langLCID[1024];
473        uloc_getLanguage(localeName, langName, sizeof(langName), &status);
474        uloc_getLanguage(lcidStringC, langLCID, sizeof(langLCID), &status);
475
476        if (strcmp(langName, langLCID) == 0) {
477            log_verbose("WARNING: %-5s resolves to %s (0x%.4x)\n",
478                localeName, lcidStringC, expectedLCID);
479        }
480        else {
481            log_err("ERROR:   %-5s has 0x%.4x and the number resolves wrongfully to %s\n",
482                localeName, expectedLCID, lcidStringC);
483        }
484    }
485}
486
487#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
488static void
489TestLocaleStructure(void) {
490    UResourceBundle *root, *currentLocale;
491    int32_t locCount = uloc_countAvailable();
492    int32_t locIndex;
493    UErrorCode errorCode = U_ZERO_ERROR;
494    const char *currLoc, *resolvedLoc;
495
496    /* TODO: Compare against parent's data too. This code can't handle fallbacks that some tools do already. */
497/*    char locName[ULOC_FULLNAME_CAPACITY];
498    char *locNamePtr;
499
500    for (locIndex = 0; locIndex < locCount; locIndex++) {
501        errorCode=U_ZERO_ERROR;
502        strcpy(locName, uloc_getAvailable(locIndex));
503        locNamePtr = strrchr(locName, '_');
504        if (locNamePtr) {
505            *locNamePtr = 0;
506        }
507        else {
508            strcpy(locName, "root");
509        }
510
511        root = ures_openDirect(NULL, locName, &errorCode);
512        if(U_FAILURE(errorCode)) {
513            log_err("Can't open %s\n", locName);
514            continue;
515        }
516*/
517    if (locCount <= 1) {
518        log_data_err("At least root needs to be installed\n");
519    }
520
521    root = ures_openDirect(loadTestData(&errorCode), "structLocale", &errorCode);
522    if(U_FAILURE(errorCode)) {
523        log_data_err("Can't open structLocale\n");
524        return;
525    }
526    for (locIndex = 0; locIndex < locCount; locIndex++) {
527        errorCode=U_ZERO_ERROR;
528        currLoc = uloc_getAvailable(locIndex);
529        currentLocale = ures_open(NULL, currLoc, &errorCode);
530        if(errorCode != U_ZERO_ERROR) {
531            if(U_SUCCESS(errorCode)) {
532                /* It's installed, but there is no data.
533                   It's installed for the g18n white paper [grhoten] */
534                log_err("ERROR: Locale %-5s not installed, and it should be, err %s\n",
535                    uloc_getAvailable(locIndex), u_errorName(errorCode));
536            } else {
537                log_err("%%%%%%% Unexpected error %d in %s %%%%%%%",
538                    u_errorName(errorCode),
539                    uloc_getAvailable(locIndex));
540            }
541            ures_close(currentLocale);
542            continue;
543        }
544        ures_getStringByKey(currentLocale, "Version", NULL, &errorCode);
545        if(errorCode != U_ZERO_ERROR) {
546            log_err("No version information is available for locale %s, and it should be!\n",
547                currLoc);
548        }
549        else if (ures_getStringByKey(currentLocale, "Version", NULL, &errorCode)[0] == (UChar)(0x78)) {
550            log_verbose("WARNING: The locale %s is experimental! It shouldn't be listed as an installed locale.\n",
551                currLoc);
552        }
553        resolvedLoc = ures_getLocaleByType(currentLocale, ULOC_ACTUAL_LOCALE, &errorCode);
554        if (strcmp(resolvedLoc, currLoc) != 0) {
555            /* All locales have at least a Version resource.
556               If it's absolutely empty, then the previous test will fail too.*/
557            log_err("Locale resolves to different locale. Is %s an alias of %s?\n",
558                currLoc, resolvedLoc);
559        }
560        TestKeyInRootRecursive(root, "root", currentLocale, currLoc);
561
562        testLCID(currentLocale, currLoc);
563
564        ures_close(currentLocale);
565    }
566
567    ures_close(root);
568}
569#endif
570
571static void
572compareArrays(const char *keyName,
573              UResourceBundle *fromArray, const char *fromLocale,
574              UResourceBundle *toArray, const char *toLocale,
575              int32_t start, int32_t end)
576{
577    int32_t fromSize = ures_getSize(fromArray);
578    int32_t toSize = ures_getSize(fromArray);
579    int32_t idx;
580    UErrorCode errorCode = U_ZERO_ERROR;
581
582    if (fromSize > toSize) {
583        fromSize = toSize;
584        log_err("Arrays are different size from \"%s\" to \"%s\"\n",
585                fromLocale,
586                toLocale);
587    }
588
589    for (idx = start; idx <= end; idx++) {
590        const UChar *fromBundleStr = ures_getStringByIndex(fromArray, idx, NULL, &errorCode);
591        const UChar *toBundleStr = ures_getStringByIndex(toArray, idx, NULL, &errorCode);
592        if (fromBundleStr && toBundleStr && u_strcmp(fromBundleStr, toBundleStr) != 0)
593        {
594            log_err("Difference for %s at index %d from %s= \"%s\" to %s= \"%s\"\n",
595                    keyName,
596                    idx,
597                    fromLocale,
598                    austrdup(fromBundleStr),
599                    toLocale,
600                    austrdup(toBundleStr));
601        }
602    }
603}
604
605static void
606compareConsistentCountryInfo(const char *fromLocale, const char *toLocale) {
607    UErrorCode errorCode = U_ZERO_ERROR;
608    UResourceBundle *fromArray, *toArray;
609    UResourceBundle *fromLocaleBund = ures_open(NULL, fromLocale, &errorCode);
610    UResourceBundle *toLocaleBund = ures_open(NULL, toLocale, &errorCode);
611    UResourceBundle *toCalendar, *fromCalendar, *toGregorian, *fromGregorian;
612
613    if(U_FAILURE(errorCode)) {
614        log_err("Can't open resource bundle %s or %s - %s\n", fromLocale, toLocale, u_errorName(errorCode));
615        return;
616    }
617    fromCalendar = ures_getByKey(fromLocaleBund, "calendar", NULL, &errorCode);
618    fromGregorian = ures_getByKeyWithFallback(fromCalendar, "gregorian", NULL, &errorCode);
619
620    toCalendar = ures_getByKey(toLocaleBund, "calendar", NULL, &errorCode);
621    toGregorian = ures_getByKeyWithFallback(toCalendar, "gregorian", NULL, &errorCode);
622
623    fromArray = ures_getByKey(fromLocaleBund, "CurrencyElements", NULL, &errorCode);
624    toArray = ures_getByKey(toLocaleBund, "CurrencyElements", NULL, &errorCode);
625    if (strcmp(fromLocale, "en_CA") != 0)
626    {
627        /* The first one is probably localized. */
628        compareArrays("CurrencyElements", fromArray, fromLocale, toArray, toLocale, 1, 2);
629    }
630    ures_close(fromArray);
631    ures_close(toArray);
632
633    fromArray = ures_getByKey(fromLocaleBund, "NumberPatterns", NULL, &errorCode);
634    toArray = ures_getByKey(toLocaleBund, "NumberPatterns", NULL, &errorCode);
635    if (strcmp(fromLocale, "en_CA") != 0)
636    {
637        compareArrays("NumberPatterns", fromArray, fromLocale, toArray, toLocale, 0, 3);
638    }
639    ures_close(fromArray);
640    ures_close(toArray);
641
642    /* Difficult to test properly */
643/*
644    fromArray = ures_getByKey(fromLocaleBund, "DateTimePatterns", NULL, &errorCode);
645    toArray = ures_getByKey(toLocaleBund, "DateTimePatterns", NULL, &errorCode);
646    {
647        compareArrays("DateTimePatterns", fromArray, fromLocale, toArray, toLocale);
648    }
649    ures_close(fromArray);
650    ures_close(toArray);*/
651
652    fromArray = ures_getByKey(fromLocaleBund, "NumberElements", NULL, &errorCode);
653    toArray = ures_getByKey(toLocaleBund, "NumberElements", NULL, &errorCode);
654    if (strcmp(fromLocale, "en_CA") != 0)
655    {
656        compareArrays("NumberElements", fromArray, fromLocale, toArray, toLocale, 0, 3);
657        /* Index 4 is a script based 0 */
658        compareArrays("NumberElements", fromArray, fromLocale, toArray, toLocale, 5, 10);
659    }
660    ures_close(fromArray);
661    ures_close(toArray);
662    ures_close(fromCalendar);
663    ures_close(toCalendar);
664    ures_close(fromGregorian);
665    ures_close(toGregorian);
666
667    ures_close(fromLocaleBund);
668    ures_close(toLocaleBund);
669}
670
671static void
672TestConsistentCountryInfo(void) {
673/*    UResourceBundle *fromLocale, *toLocale;*/
674    int32_t locCount = uloc_countAvailable();
675    int32_t fromLocIndex, toLocIndex;
676
677    int32_t fromCountryLen, toCountryLen;
678    char fromCountry[ULOC_FULLNAME_CAPACITY], toCountry[ULOC_FULLNAME_CAPACITY];
679
680    int32_t fromVariantLen, toVariantLen;
681    char fromVariant[ULOC_FULLNAME_CAPACITY], toVariant[ULOC_FULLNAME_CAPACITY];
682
683    UErrorCode errorCode = U_ZERO_ERROR;
684
685    for (fromLocIndex = 0; fromLocIndex < locCount; fromLocIndex++) {
686        const char *fromLocale = uloc_getAvailable(fromLocIndex);
687
688        errorCode=U_ZERO_ERROR;
689        fromCountryLen = uloc_getCountry(fromLocale, fromCountry, ULOC_FULLNAME_CAPACITY, &errorCode);
690        if (fromCountryLen <= 0) {
691            /* Ignore countryless locales */
692            continue;
693        }
694        fromVariantLen = uloc_getVariant(fromLocale, fromVariant, ULOC_FULLNAME_CAPACITY, &errorCode);
695        if (fromVariantLen > 0) {
696            /* Most variants are ignorable like PREEURO, or collation variants. */
697            continue;
698        }
699        /* Start comparing only after the current index.
700           Previous loop should have already compared fromLocIndex.
701        */
702        for (toLocIndex = fromLocIndex + 1; toLocIndex < locCount; toLocIndex++) {
703            const char *toLocale = uloc_getAvailable(toLocIndex);
704
705            toCountryLen = uloc_getCountry(toLocale, toCountry, ULOC_FULLNAME_CAPACITY, &errorCode);
706            if(U_FAILURE(errorCode)) {
707                log_err("Unknown failure fromLocale=%s toLocale=%s errorCode=%s\n",
708                    fromLocale, toLocale, u_errorName(errorCode));
709                continue;
710            }
711
712            if (toCountryLen <= 0) {
713                /* Ignore countryless locales */
714                continue;
715            }
716            toVariantLen = uloc_getVariant(toLocale, toVariant, ULOC_FULLNAME_CAPACITY, &errorCode);
717            if (toVariantLen > 0) {
718                /* Most variants are ignorable like PREEURO, or collation variants. */
719                /* They're a variant for a reason. */
720                continue;
721            }
722            if (strcmp(fromCountry, toCountry) == 0) {
723                log_verbose("comparing fromLocale=%s toLocale=%s\n",
724                    fromLocale, toLocale);
725                compareConsistentCountryInfo(fromLocale, toLocale);
726            }
727        }
728    }
729}
730
731static int32_t
732findStringSetMismatch(const char *currLoc, const UChar *string, int32_t langSize,
733                      const UChar *exemplarCharacters, int32_t exemplarLen,
734                      UBool ignoreNumbers, UChar* badCharPtr) {
735    UErrorCode errorCode = U_ZERO_ERROR;
736    USet *origSet = uset_openPatternOptions(exemplarCharacters, exemplarLen, USET_CASE_INSENSITIVE, &errorCode);
737    USet *exemplarSet = createFlattenSet(origSet, &errorCode);
738    int32_t strIdx;
739    uset_close(origSet);
740    if (U_FAILURE(errorCode)) {
741        log_err("%s: error uset_openPattern returned %s\n", currLoc, u_errorName(errorCode));
742        return -1;
743    }
744
745    for (strIdx = 0; strIdx < langSize; strIdx++) {
746        if (!uset_contains(exemplarSet, string[strIdx])
747            && 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
748            && string[strIdx] != 0x200C && string[strIdx] != 0x200D) {
749            if (!ignoreNumbers || (ignoreNumbers && (string[strIdx] < 0x30 || string[strIdx] > 0x39))) {
750                uset_close(exemplarSet);
751                if (badCharPtr) {
752                    *badCharPtr = string[strIdx];
753                }
754                return strIdx;
755            }
756        }
757    }
758    uset_close(exemplarSet);
759    if (badCharPtr) {
760        *badCharPtr = 0;
761    }
762    return -1;
763}
764/* include non-invariant chars */
765static int32_t
766myUCharsToChars(const UChar* us, char* cs, int32_t len){
767    int32_t i=0;
768    for(; i< len; i++){
769        if(us[i] < 0x7f){
770            cs[i] = (char)us[i];
771        }else{
772            return -1;
773        }
774    }
775    return i;
776}
777static void
778findSetMatch( UScriptCode *scriptCodes, int32_t scriptsLen,
779              USet *exemplarSet,
780              const char  *locale){
781    USet *scripts[10]= {0};
782    char pattern[256] = { '[', ':', 0x000 };
783    int32_t patternLen;
784    UChar uPattern[256] = {0};
785    UErrorCode status = U_ZERO_ERROR;
786    int32_t i;
787
788    /* create the sets with script codes */
789    for(i = 0; i<scriptsLen; i++){
790        strcat(pattern, uscript_getShortName(scriptCodes[i]));
791        strcat(pattern, ":]");
792        patternLen = (int32_t)strlen(pattern);
793        u_charsToUChars(pattern, uPattern, patternLen);
794        scripts[i] = uset_openPattern(uPattern, patternLen, &status);
795        if(U_FAILURE(status)){
796            log_err("Could not create set for pattern %s. Error: %s\n", pattern, u_errorName(status));
797            return;
798        }
799        pattern[2] = 0;
800    }
801    if (strcmp(locale, "uk") == 0 || strcmp(locale, "uk_UA") == 0) {
802        /* Special addition. Add the modifying apostrophe, which isn't in Cyrillic. */
803        uset_add(scripts[0], 0x2bc);
804    }
805    if(U_SUCCESS(status)){
806        UBool existsInScript = FALSE;
807        /* iterate over the exemplarSet and ascertain if all
808         * UChars in exemplarSet belong to the scripts returned
809         * by getScript
810         */
811        int32_t count = uset_getItemCount(exemplarSet);
812
813        for( i=0; i < count; i++){
814            UChar32 start = 0;
815            UChar32 end = 0;
816            UChar *str = NULL;
817            int32_t strCapacity = 0;
818
819            strCapacity = uset_getItem(exemplarSet, i, &start, &end, str, strCapacity, &status);
820            if(U_SUCCESS(status)){
821                int32_t j;
822                if(strCapacity == 0){
823                    /* ok the item is a range */
824                     for( j = 0; j < scriptsLen; j++){
825                        if(uset_containsRange(scripts[j], start, end) == TRUE){
826                            existsInScript = TRUE;
827                        }
828                    }
829                    if(existsInScript == FALSE){
830                        for( j = 0; j < scriptsLen; j++){
831                            UChar toPattern[500]={'\0'};
832                            char pat[500]={'\0'};
833                            int32_t len = uset_toPattern(scripts[j], toPattern, 500, TRUE, &status);
834                            len = myUCharsToChars(toPattern, pat, len);
835                            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));
836                            if(len!=-1){
837                                log_err("Pattern: %s\n",pat);
838                            }
839                        }
840                        log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale);
841                    }
842                }else{
843                    strCapacity++; /* increment for NUL termination */
844                    /* allocate the str and call the api again */
845                    str = (UChar*) malloc(U_SIZEOF_UCHAR * strCapacity);
846                    strCapacity =  uset_getItem(exemplarSet, i, &start, &end, str, strCapacity, &status);
847                    /* iterate over the scripts and figure out if the string contained is actually
848                     * in the script set
849                     */
850                    for( j = 0; j < scriptsLen; j++){
851                        if(uset_containsString(scripts[j],str, strCapacity) == TRUE){
852                            existsInScript = TRUE;
853                        }
854                    }
855                    if(existsInScript == FALSE){
856                        log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale);
857                    }
858                }
859            }
860        }
861
862    }
863
864    /* close the sets */
865    for(i = 0; i<scriptsLen; i++){
866        uset_close(scripts[i]);
867    }
868}
869
870static void VerifyTranslation(void) {
871    UResourceBundle *root, *currentLocale;
872    int32_t locCount = uloc_countAvailable();
873    int32_t locIndex;
874    UErrorCode errorCode = U_ZERO_ERROR;
875    int32_t exemplarLen;
876    const UChar *exemplarCharacters;
877    const char *currLoc;
878    UScriptCode scripts[USCRIPT_CODE_LIMIT];
879    int32_t numScripts;
880    int32_t idx;
881    int32_t end;
882    UResourceBundle *resArray;
883
884    if (locCount <= 1) {
885        log_data_err("At least root needs to be installed\n");
886    }
887
888    root = ures_openDirect(NULL, "root", &errorCode);
889    if(U_FAILURE(errorCode)) {
890        log_data_err("Can't open root\n");
891        return;
892    }
893    for (locIndex = 0; locIndex < locCount; locIndex++) {
894        errorCode=U_ZERO_ERROR;
895        currLoc = uloc_getAvailable(locIndex);
896        currentLocale = ures_open(NULL, currLoc, &errorCode);
897        if(errorCode != U_ZERO_ERROR) {
898            if(U_SUCCESS(errorCode)) {
899                /* It's installed, but there is no data.
900                   It's installed for the g18n white paper [grhoten] */
901                log_err("ERROR: Locale %-5s not installed, and it should be!\n",
902                    uloc_getAvailable(locIndex));
903            } else {
904                log_err("%%%%%%% Unexpected error %d in %s %%%%%%%",
905                    u_errorName(errorCode),
906                    uloc_getAvailable(locIndex));
907            }
908            ures_close(currentLocale);
909            continue;
910        }
911        exemplarCharacters = ures_getStringByKey(currentLocale, "ExemplarCharacters", &exemplarLen, &errorCode);
912        if (U_FAILURE(errorCode)) {
913            log_err("error ures_getStringByKey returned %s\n", u_errorName(errorCode));
914        }
915        else if (getTestOption(QUICK_OPTION) && exemplarLen > 2048) {
916            log_verbose("skipping test for %s\n", currLoc);
917        }
918        else if (uprv_strncmp(currLoc,"bem",3) == 0 || uprv_strncmp(currLoc,"mgo",3) == 0 || uprv_strncmp(currLoc,"nl",2) == 0 || uprv_strncmp(currLoc,"ky",2) == 0) {
919            log_verbose("skipping test for %s, some month and country names known to use aux exemplars\n", currLoc);
920        }
921        else {
922            UChar langBuffer[128];
923            int32_t langSize;
924            int32_t strIdx;
925            UChar badChar;
926            langSize = uloc_getDisplayLanguage(currLoc, currLoc, langBuffer, sizeof(langBuffer)/sizeof(langBuffer[0]), &errorCode);
927            if (U_FAILURE(errorCode)) {
928                log_err("error uloc_getDisplayLanguage returned %s\n", u_errorName(errorCode));
929            }
930            else {
931                strIdx = findStringSetMismatch(currLoc, langBuffer, langSize, exemplarCharacters, exemplarLen, FALSE, &badChar);
932                if (strIdx >= 0) {
933                    log_err("getDisplayLanguage(%s) at index %d returned characters not in the exemplar characters: %04X.\n",
934                        currLoc, strIdx, badChar);
935                }
936            }
937            langSize = uloc_getDisplayCountry(currLoc, currLoc, langBuffer, sizeof(langBuffer)/sizeof(langBuffer[0]), &errorCode);
938            if (U_FAILURE(errorCode)) {
939                log_err("error uloc_getDisplayCountry returned %s\n", u_errorName(errorCode));
940            }
941            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 */
942              strIdx = findStringSetMismatch(currLoc, langBuffer, langSize, exemplarCharacters, exemplarLen, FALSE, &badChar);
943                if (strIdx >= 0) {
944                    log_err("getDisplayCountry(%s) at index %d returned characters not in the exemplar characters: %04X.\n",
945                        currLoc, strIdx, badChar);
946                }
947            }
948            {
949                UResourceBundle* cal = ures_getByKey(currentLocale, "calendar", NULL, &errorCode);
950                UResourceBundle* greg = ures_getByKeyWithFallback(cal, "gregorian", NULL, &errorCode);
951                UResourceBundle* names = ures_getByKeyWithFallback(greg,  "dayNames", NULL, &errorCode);
952                UResourceBundle* format = ures_getByKeyWithFallback(names,  "format", NULL, &errorCode);
953                resArray = ures_getByKeyWithFallback(format,  "wide", NULL, &errorCode);
954
955                if (U_FAILURE(errorCode)) {
956                    log_err("error ures_getByKey returned %s\n", u_errorName(errorCode));
957                }
958                if (getTestOption(QUICK_OPTION)) {
959                    end = 1;
960                }
961                else {
962                    end = ures_getSize(resArray);
963                }
964
965
966                for (idx = 0; idx < end; idx++) {
967                    const UChar *fromBundleStr = ures_getStringByIndex(resArray, idx, &langSize, &errorCode);
968                    if (U_FAILURE(errorCode)) {
969                        log_err("error ures_getStringByIndex(%d) returned %s\n", idx, u_errorName(errorCode));
970                        continue;
971                    }
972                    if (uprv_strstr(currLoc, "uz_Arab") != currLoc || isICUVersionAtLeast(52, 0, 1)) { /* TODO: FIX or REMOVE this test! */
973                        strIdx = findStringSetMismatch(currLoc, fromBundleStr, langSize, exemplarCharacters, exemplarLen, TRUE, &badChar);
974                        if (strIdx >= 0) {
975                            log_err("getDayNames(%s, %d) at index %d returned characters not in the exemplar characters: %04X.\n",
976                                currLoc, idx, strIdx, badChar);
977                        }
978                    }
979                }
980                ures_close(resArray);
981                ures_close(format);
982                ures_close(names);
983
984                names = ures_getByKeyWithFallback(greg, "monthNames", NULL, &errorCode);
985                format = ures_getByKeyWithFallback(names,"format", NULL, &errorCode);
986                resArray = ures_getByKeyWithFallback(format, "wide", NULL, &errorCode);
987                if (U_FAILURE(errorCode)) {
988                    log_err("error ures_getByKey returned %s\n", u_errorName(errorCode));
989                }
990                if (getTestOption(QUICK_OPTION)) {
991                    end = 1;
992                }
993                else {
994                    end = ures_getSize(resArray);
995                }
996
997                for (idx = 0; idx < end; idx++) {
998                    const UChar *fromBundleStr = ures_getStringByIndex(resArray, idx, &langSize, &errorCode);
999                    if (U_FAILURE(errorCode)) {
1000                        log_err("error ures_getStringByIndex(%d) returned %s\n", idx, u_errorName(errorCode));
1001                        continue;
1002                    }
1003                    if (uprv_strstr(currLoc, "uz_Arab") != currLoc || isICUVersionAtLeast(52, 0, 1)) { /* TODO: FIX or REMOVE this test! */
1004                        strIdx = findStringSetMismatch(currLoc, fromBundleStr, langSize, exemplarCharacters, exemplarLen, TRUE, &badChar);
1005                        if (strIdx >= 0) {
1006                            log_err("getMonthNames(%s, %d) at index %d returned characters not in the exemplar characters: %04X.\n",
1007                                currLoc, idx, strIdx, badChar);
1008                        }
1009                    }
1010                }
1011                ures_close(resArray);
1012                ures_close(format);
1013                ures_close(names);
1014                ures_close(greg);
1015                ures_close(cal);
1016            }
1017            errorCode = U_ZERO_ERROR;
1018            numScripts = uscript_getCode(currLoc, scripts, sizeof(scripts)/sizeof(scripts[0]), &errorCode);
1019            if (numScripts == 0) {
1020                log_err("uscript_getCode(%s) doesn't work.\n", currLoc);
1021            }else if(scripts[0] == USCRIPT_COMMON){
1022                log_err("uscript_getCode(%s) returned USCRIPT_COMMON.\n", currLoc);
1023            }
1024
1025            /* test that the scripts are a superset of exemplar characters. */
1026           {
1027                ULocaleData *uld = ulocdata_open(currLoc,&errorCode);
1028                USet *exemplarSet =  ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &errorCode);
1029                /* test if exemplar characters are part of script code */
1030                findSetMatch(scripts, numScripts, exemplarSet, currLoc);
1031                uset_close(exemplarSet);
1032                ulocdata_close(uld);
1033            }
1034
1035           /* test that the paperSize API works */
1036           {
1037               int32_t height=0, width=0;
1038               ulocdata_getPaperSize(currLoc, &height, &width, &errorCode);
1039               if(U_FAILURE(errorCode)){
1040                   log_err("ulocdata_getPaperSize failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode));
1041               }
1042               if(strstr(currLoc, "_US")!=NULL && height != 279 && width != 216 ){
1043                   log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc);
1044               }
1045           }
1046            /* test that the MeasurementSystem works API works */
1047           {
1048               char fullLoc[ULOC_FULLNAME_CAPACITY];
1049               UMeasurementSystem measurementSystem = ulocdata_getMeasurementSystem(currLoc, &errorCode);
1050               if(U_FAILURE(errorCode)){
1051                   log_err("ulocdata_getMeasurementSystem failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode));
1052               }
1053               uloc_addLikelySubtags(currLoc, fullLoc, ULOC_FULLNAME_CAPACITY, &errorCode);
1054               if(strstr(fullLoc, "_US")!=NULL || strstr(fullLoc, "_MM")!=NULL || strstr(fullLoc, "_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
1072static 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
1190static 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
1212static 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
1245static 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
1283static 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
1319static 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
1421void addCLDRTest(TestNode** root);
1422
1423void 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