1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2015, 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/*
20returns a new UnicodeSet that is a flattened form of the original
21UnicodeSet.
22*/
23static USet*
24createFlattenSet(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
58static UBool
59isCurrencyPreEuro(const char* currencyKey){
60    if( strcmp(currencyKey, "PTE") == 0 ||
61        strcmp(currencyKey, "ESP") == 0 ||
62        strcmp(currencyKey, "LUF") == 0 ||
63        strcmp(currencyKey, "GRD") == 0 ||
64        strcmp(currencyKey, "BEF") == 0 ||
65        strcmp(currencyKey, "ITL") == 0 ||
66        strcmp(currencyKey, "EEK") == 0){
67            return TRUE;
68    }
69    return FALSE;
70}
71#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
72static void
73TestKeyInRootRecursive(UResourceBundle *root, const char *rootName,
74                       UResourceBundle *currentBundle, const char *locale) {
75    UErrorCode errorCode = U_ZERO_ERROR;
76    UResourceBundle *subRootBundle = NULL, *subBundle = NULL, *arr = NULL;
77
78    ures_resetIterator(root);
79    ures_resetIterator(currentBundle);
80    while (ures_hasNext(currentBundle)) {
81        const char *subBundleKey = NULL;
82        const char *currentBundleKey = NULL;
83
84        errorCode = U_ZERO_ERROR;
85        currentBundleKey = ures_getKey(currentBundle);
86        (void)currentBundleKey;    /* Suppress set but not used warning. */
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                (void)sameArray;    /* Suppress set but not used warning. */
319/*                if (sameArray && strcmp(rootName, "root") == 0) {
320                    log_err("Arrays are the same with key \"%s\" in \"%s\" from root for locale \"%s\"\n",
321                            subBundleKey,
322                            ures_getKey(currentBundle),
323                            locale);
324                }*/
325            }
326            ures_close(subSubBundle);
327            ures_close(subSubRootBundle);
328        }
329        else if (ures_getType(subBundle) == URES_STRING) {
330            int32_t len = 0;
331            const UChar *string = ures_getString(subBundle, &len, &errorCode);
332            if (U_FAILURE(errorCode) || string == NULL) {
333                log_err("Can't open a string with key \"%s\" in \"%s\" for locale \"%s\"\n",
334                        subBundleKey,
335                        ures_getKey(currentBundle),
336                        locale);
337            } else if (string[0] == (UChar)0x20) {
338                log_err("key \"%s\" in \"%s\" starts with a space in locale \"%s\"\n",
339                        subBundleKey,
340                        ures_getKey(currentBundle),
341                        locale);
342            /* localeDisplayPattern/separator can end with a space */
343            } else if (string[len - 1] == (UChar)0x20 && (strcmp(subBundleKey,"separator"))) {
344                log_err("key \"%s\" in \"%s\" ends with a space in locale \"%s\"\n",
345                        subBundleKey,
346                        ures_getKey(currentBundle),
347                        locale);
348            } else if (strcmp(subBundleKey, "localPatternChars") == 0) {
349                /* Note: We no longer import localPatternChars data starting
350                 * ICU 3.8.  So it never comes into this else if block. (ticket#5597)
351                 */
352
353                /* Check well-formedness of localPatternChars.  First, the
354                 * length must match the number of fields defined by
355                 * DateFormat.  Second, each character in the string must
356                 * be in the set [A-Za-z].  Finally, each character must be
357                 * unique.
358                 */
359                int32_t i,j;
360#if !UCONFIG_NO_FORMATTING
361                if (len != UDAT_FIELD_COUNT) {
362                    log_err("key \"%s\" has the wrong number of characters in locale \"%s\"\n",
363                            subBundleKey,
364                            locale);
365                }
366#endif
367                /* Check char validity. */
368                for (i=0; i<len; ++i) {
369                    if (!((string[i] >= 65/*'A'*/ && string[i] <= 90/*'Z'*/) ||
370                          (string[i] >= 97/*'a'*/ && string[i] <= 122/*'z'*/))) {
371                        log_err("key \"%s\" has illegal character '%c' in locale \"%s\"\n",
372                                subBundleKey,
373                                (char) string[i],
374                                locale);
375                    }
376                    /* Do O(n^2) check for duplicate chars. */
377                    for (j=0; j<i; ++j) {
378                        if (string[j] == string[i]) {
379                            log_err("key \"%s\" has duplicate character '%c' in locale \"%s\"\n",
380                                    subBundleKey,
381                                    (char) string[i],
382                                    locale);
383                        }
384                    }
385                }
386            }
387            /* No fallback was done. Check for duplicate data */
388            /* The ures_* API does not do fallback of sub-resource bundles,
389               So we can't do this now. */
390#if 0
391            else if (strcmp(locale, "root") != 0 && errorCode == U_ZERO_ERROR) {
392
393                const UChar *rootString = ures_getString(subRootBundle, &len, &errorCode);
394                if (U_FAILURE(errorCode) || rootString == NULL) {
395                    log_err("Can't open a string with key \"%s\" in \"%s\" in root\n",
396                            ures_getKey(subRootBundle),
397                            ures_getKey(currentBundle));
398                    continue;
399                } else if (u_strcmp(string, rootString) == 0) {
400                    if (strcmp(locale, "de_CH") != 0 && strcmp(subBundleKey, "Countries") != 0 &&
401                        strcmp(subBundleKey, "Version") != 0) {
402                        log_err("Found duplicate data with key \"%s\" in \"%s\" in locale \"%s\"\n",
403                                ures_getKey(subRootBundle),
404                                ures_getKey(currentBundle),
405                                locale);
406                    }
407                    else {
408                        /* Ignore for now. */
409                        /* Can be fixed if fallback through de locale was done. */
410                        log_verbose("Skipping key %s in %s\n", subBundleKey, locale);
411                    }
412                }
413            }
414#endif
415        }
416        else if (ures_getType(subBundle) == URES_TABLE) {
417            if (strcmp(subBundleKey, "availableFormats")!=0) {
418                /* Here is one of the recursive parts */
419                TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale);
420            }
421            else {
422                log_verbose("Skipping key %s in %s\n", subBundleKey, locale);
423            }
424        }
425        else if (ures_getType(subBundle) == URES_BINARY || ures_getType(subBundle) == URES_INT) {
426            /* Can't do anything to check it */
427            /* We'll assume it's all correct */
428            if (strcmp(subBundleKey, "MeasurementSystem") != 0) {
429                log_verbose("Skipping key \"%s\" in \"%s\" for locale \"%s\"\n",
430                        subBundleKey,
431                        ures_getKey(currentBundle),
432                        locale);
433            }
434            /* Testing for MeasurementSystem is done in VerifyTranslation */
435        }
436        else {
437            log_err("Type %d for key \"%s\" in \"%s\" is unknown for locale \"%s\"\n",
438                    ures_getType(subBundle),
439                    subBundleKey,
440                    ures_getKey(currentBundle),
441                    locale);
442        }
443        ures_close(subRootBundle);
444        ures_close(subBundle);
445    }
446}
447#endif
448
449static void
450testLCID(UResourceBundle *currentBundle,
451         const char *localeName)
452{
453    UErrorCode status = U_ZERO_ERROR;
454    uint32_t expectedLCID;
455    char lcidStringC[64] = {0};
456    int32_t len;
457
458    expectedLCID = uloc_getLCID(localeName);
459    if (expectedLCID == 0) {
460        log_verbose("INFO:    %-5s does not have any LCID mapping\n",
461            localeName);
462        return;
463    }
464
465    status = U_ZERO_ERROR;
466    len = uprv_convertToPosix(expectedLCID, lcidStringC, sizeof(lcidStringC)/sizeof(lcidStringC[0]) - 1, &status);
467    if (U_FAILURE(status)) {
468        log_err("ERROR:   %.4x does not have a POSIX mapping due to %s\n",
469            expectedLCID, u_errorName(status));
470    }
471    lcidStringC[len] = 0;
472
473    if(strcmp(localeName, lcidStringC) != 0) {
474        char langName[1024];
475        char langLCID[1024];
476        uloc_getLanguage(localeName, langName, sizeof(langName), &status);
477        uloc_getLanguage(lcidStringC, langLCID, sizeof(langLCID), &status);
478
479        if (strcmp(langName, langLCID) == 0) {
480            log_verbose("WARNING: %-5s resolves to %s (0x%.4x)\n",
481                localeName, lcidStringC, expectedLCID);
482        }
483        else {
484            log_err("ERROR:   %-5s has 0x%.4x and the number resolves wrongfully to %s\n",
485                localeName, expectedLCID, lcidStringC);
486        }
487    }
488}
489
490#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
491static void
492TestLocaleStructure(void) {
493    // This test checks the locale structure against a key file located
494    // at source/test/testdata/structLocale.txt. When adding new data to
495    // a locale file such as en.txt, the structLocale.txt file must be changed
496    // too to include the the template of the new data. Otherwise this test
497    // will fail!
498
499    UResourceBundle *root, *currentLocale;
500    int32_t locCount = uloc_countAvailable();
501    int32_t locIndex;
502    UErrorCode errorCode = U_ZERO_ERROR;
503    const char *currLoc, *resolvedLoc;
504
505    /* TODO: Compare against parent's data too. This code can't handle fallbacks that some tools do already. */
506/*    char locName[ULOC_FULLNAME_CAPACITY];
507    char *locNamePtr;
508
509    for (locIndex = 0; locIndex < locCount; locIndex++) {
510        errorCode=U_ZERO_ERROR;
511        strcpy(locName, uloc_getAvailable(locIndex));
512        locNamePtr = strrchr(locName, '_');
513        if (locNamePtr) {
514            *locNamePtr = 0;
515        }
516        else {
517            strcpy(locName, "root");
518        }
519
520        root = ures_openDirect(NULL, locName, &errorCode);
521        if(U_FAILURE(errorCode)) {
522            log_err("Can't open %s\n", locName);
523            continue;
524        }
525*/
526    if (locCount <= 1) {
527        log_data_err("At least root needs to be installed\n");
528    }
529
530    root = ures_openDirect(loadTestData(&errorCode), "structLocale", &errorCode);
531    if(U_FAILURE(errorCode)) {
532        log_data_err("Can't open structLocale\n");
533        return;
534    }
535    for (locIndex = 0; locIndex < locCount; locIndex++) {
536        errorCode=U_ZERO_ERROR;
537        currLoc = uloc_getAvailable(locIndex);
538        currentLocale = ures_open(NULL, currLoc, &errorCode);
539        if(errorCode != U_ZERO_ERROR) {
540            if(U_SUCCESS(errorCode)) {
541                /* It's installed, but there is no data.
542                   It's installed for the g18n white paper [grhoten] */
543                log_err("ERROR: Locale %-5s not installed, and it should be, err %s\n",
544                    uloc_getAvailable(locIndex), u_errorName(errorCode));
545            } else {
546                log_err("%%%%%%% Unexpected error %d in %s %%%%%%%",
547                    u_errorName(errorCode),
548                    uloc_getAvailable(locIndex));
549            }
550            ures_close(currentLocale);
551            continue;
552        }
553        ures_getStringByKey(currentLocale, "Version", NULL, &errorCode);
554        if(errorCode != U_ZERO_ERROR) {
555            log_err("No version information is available for locale %s, and it should be!\n",
556                currLoc);
557        }
558        else if (ures_getStringByKey(currentLocale, "Version", NULL, &errorCode)[0] == (UChar)(0x78)) {
559            log_verbose("WARNING: The locale %s is experimental! It shouldn't be listed as an installed locale.\n",
560                currLoc);
561        }
562        resolvedLoc = ures_getLocaleByType(currentLocale, ULOC_ACTUAL_LOCALE, &errorCode);
563        if (strcmp(resolvedLoc, currLoc) != 0) {
564            /* All locales have at least a Version resource.
565               If it's absolutely empty, then the previous test will fail too.*/
566            log_err("Locale resolves to different locale. Is %s an alias of %s?\n",
567                currLoc, resolvedLoc);
568        }
569        TestKeyInRootRecursive(root, "root", currentLocale, currLoc);
570
571        testLCID(currentLocale, currLoc);
572
573        ures_close(currentLocale);
574    }
575
576    ures_close(root);
577}
578#endif
579
580static void
581compareArrays(const char *keyName,
582              UResourceBundle *fromArray, const char *fromLocale,
583              UResourceBundle *toArray, const char *toLocale,
584              int32_t start, int32_t end)
585{
586    int32_t fromSize = ures_getSize(fromArray);
587    int32_t toSize = ures_getSize(fromArray);
588    int32_t idx;
589    UErrorCode errorCode = U_ZERO_ERROR;
590
591    if (fromSize > toSize) {
592        fromSize = toSize;
593        log_err("Arrays are different size from \"%s\" to \"%s\"\n",
594                fromLocale,
595                toLocale);
596    }
597
598    for (idx = start; idx <= end; idx++) {
599        const UChar *fromBundleStr = ures_getStringByIndex(fromArray, idx, NULL, &errorCode);
600        const UChar *toBundleStr = ures_getStringByIndex(toArray, idx, NULL, &errorCode);
601        if (fromBundleStr && toBundleStr && u_strcmp(fromBundleStr, toBundleStr) != 0)
602        {
603            log_err("Difference for %s at index %d from %s= \"%s\" to %s= \"%s\"\n",
604                    keyName,
605                    idx,
606                    fromLocale,
607                    austrdup(fromBundleStr),
608                    toLocale,
609                    austrdup(toBundleStr));
610        }
611    }
612}
613
614static void
615compareConsistentCountryInfo(const char *fromLocale, const char *toLocale) {
616    UErrorCode errorCode = U_ZERO_ERROR;
617    UResourceBundle *fromArray, *toArray;
618    UResourceBundle *fromLocaleBund = ures_open(NULL, fromLocale, &errorCode);
619    UResourceBundle *toLocaleBund = ures_open(NULL, toLocale, &errorCode);
620    UResourceBundle *toCalendar, *fromCalendar, *toGregorian, *fromGregorian;
621
622    if(U_FAILURE(errorCode)) {
623        log_err("Can't open resource bundle %s or %s - %s\n", fromLocale, toLocale, u_errorName(errorCode));
624        return;
625    }
626    fromCalendar = ures_getByKey(fromLocaleBund, "calendar", NULL, &errorCode);
627    fromGregorian = ures_getByKeyWithFallback(fromCalendar, "gregorian", NULL, &errorCode);
628
629    toCalendar = ures_getByKey(toLocaleBund, "calendar", NULL, &errorCode);
630    toGregorian = ures_getByKeyWithFallback(toCalendar, "gregorian", NULL, &errorCode);
631
632    fromArray = ures_getByKey(fromLocaleBund, "CurrencyElements", NULL, &errorCode);
633    toArray = ures_getByKey(toLocaleBund, "CurrencyElements", NULL, &errorCode);
634    if (strcmp(fromLocale, "en_CA") != 0)
635    {
636        /* The first one is probably localized. */
637        compareArrays("CurrencyElements", fromArray, fromLocale, toArray, toLocale, 1, 2);
638    }
639    ures_close(fromArray);
640    ures_close(toArray);
641
642    fromArray = ures_getByKey(fromLocaleBund, "NumberPatterns", NULL, &errorCode);
643    toArray = ures_getByKey(toLocaleBund, "NumberPatterns", NULL, &errorCode);
644    if (strcmp(fromLocale, "en_CA") != 0)
645    {
646        compareArrays("NumberPatterns", fromArray, fromLocale, toArray, toLocale, 0, 3);
647    }
648    ures_close(fromArray);
649    ures_close(toArray);
650
651    /* Difficult to test properly */
652/*
653    fromArray = ures_getByKey(fromLocaleBund, "DateTimePatterns", NULL, &errorCode);
654    toArray = ures_getByKey(toLocaleBund, "DateTimePatterns", NULL, &errorCode);
655    {
656        compareArrays("DateTimePatterns", fromArray, fromLocale, toArray, toLocale);
657    }
658    ures_close(fromArray);
659    ures_close(toArray);*/
660
661    fromArray = ures_getByKey(fromLocaleBund, "NumberElements", NULL, &errorCode);
662    toArray = ures_getByKey(toLocaleBund, "NumberElements", NULL, &errorCode);
663    if (strcmp(fromLocale, "en_CA") != 0)
664    {
665        compareArrays("NumberElements", fromArray, fromLocale, toArray, toLocale, 0, 3);
666        /* Index 4 is a script based 0 */
667        compareArrays("NumberElements", fromArray, fromLocale, toArray, toLocale, 5, 10);
668    }
669    ures_close(fromArray);
670    ures_close(toArray);
671    ures_close(fromCalendar);
672    ures_close(toCalendar);
673    ures_close(fromGregorian);
674    ures_close(toGregorian);
675
676    ures_close(fromLocaleBund);
677    ures_close(toLocaleBund);
678}
679
680static void
681TestConsistentCountryInfo(void) {
682/*    UResourceBundle *fromLocale, *toLocale;*/
683    int32_t locCount = uloc_countAvailable();
684    int32_t fromLocIndex, toLocIndex;
685
686    int32_t fromCountryLen, toCountryLen;
687    char fromCountry[ULOC_FULLNAME_CAPACITY], toCountry[ULOC_FULLNAME_CAPACITY];
688
689    int32_t fromVariantLen, toVariantLen;
690    char fromVariant[ULOC_FULLNAME_CAPACITY], toVariant[ULOC_FULLNAME_CAPACITY];
691
692    UErrorCode errorCode = U_ZERO_ERROR;
693
694    for (fromLocIndex = 0; fromLocIndex < locCount; fromLocIndex++) {
695        const char *fromLocale = uloc_getAvailable(fromLocIndex);
696
697        errorCode=U_ZERO_ERROR;
698        fromCountryLen = uloc_getCountry(fromLocale, fromCountry, ULOC_FULLNAME_CAPACITY, &errorCode);
699        if (fromCountryLen <= 0) {
700            /* Ignore countryless locales */
701            continue;
702        }
703        fromVariantLen = uloc_getVariant(fromLocale, fromVariant, ULOC_FULLNAME_CAPACITY, &errorCode);
704        if (fromVariantLen > 0) {
705            /* Most variants are ignorable like PREEURO, or collation variants. */
706            continue;
707        }
708        /* Start comparing only after the current index.
709           Previous loop should have already compared fromLocIndex.
710        */
711        for (toLocIndex = fromLocIndex + 1; toLocIndex < locCount; toLocIndex++) {
712            const char *toLocale = uloc_getAvailable(toLocIndex);
713
714            toCountryLen = uloc_getCountry(toLocale, toCountry, ULOC_FULLNAME_CAPACITY, &errorCode);
715            if(U_FAILURE(errorCode)) {
716                log_err("Unknown failure fromLocale=%s toLocale=%s errorCode=%s\n",
717                    fromLocale, toLocale, u_errorName(errorCode));
718                continue;
719            }
720
721            if (toCountryLen <= 0) {
722                /* Ignore countryless locales */
723                continue;
724            }
725            toVariantLen = uloc_getVariant(toLocale, toVariant, ULOC_FULLNAME_CAPACITY, &errorCode);
726            if (toVariantLen > 0) {
727                /* Most variants are ignorable like PREEURO, or collation variants. */
728                /* They're a variant for a reason. */
729                continue;
730            }
731            if (strcmp(fromCountry, toCountry) == 0) {
732                log_verbose("comparing fromLocale=%s toLocale=%s\n",
733                    fromLocale, toLocale);
734                compareConsistentCountryInfo(fromLocale, toLocale);
735            }
736        }
737    }
738}
739
740static int32_t
741findStringSetMismatch(const char *currLoc, const UChar *string, int32_t langSize,
742                      USet * mergedExemplarSet,
743                      UBool ignoreNumbers, UChar* badCharPtr) {
744    UErrorCode errorCode = U_ZERO_ERROR;
745    USet *exemplarSet;
746    int32_t strIdx;
747    if (mergedExemplarSet == NULL) {
748        return -1;
749    }
750    exemplarSet = createFlattenSet(mergedExemplarSet, &errorCode);
751    if (U_FAILURE(errorCode)) {
752        log_err("%s: error createFlattenSet returned %s\n", currLoc, u_errorName(errorCode));
753        return -1;
754    }
755
756    for (strIdx = 0; strIdx < langSize; strIdx++) {
757        if (!uset_contains(exemplarSet, string[strIdx])
758            && 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
759            && string[strIdx] != 0x200C && string[strIdx] != 0x200D) {
760            if (!ignoreNumbers || (ignoreNumbers && (string[strIdx] < 0x30 || string[strIdx] > 0x39))) {
761                uset_close(exemplarSet);
762                if (badCharPtr) {
763                    *badCharPtr = string[strIdx];
764                }
765                return strIdx;
766            }
767        }
768    }
769    uset_close(exemplarSet);
770    if (badCharPtr) {
771        *badCharPtr = 0;
772    }
773    return -1;
774}
775/* include non-invariant chars */
776static int32_t
777myUCharsToChars(const UChar* us, char* cs, int32_t len){
778    int32_t i=0;
779    for(; i< len; i++){
780        if(us[i] < 0x7f){
781            cs[i] = (char)us[i];
782        }else{
783            return -1;
784        }
785    }
786    return i;
787}
788static void
789findSetMatch( UScriptCode *scriptCodes, int32_t scriptsLen,
790              USet *exemplarSet,
791              const char  *locale){
792    USet *scripts[10]= {0};
793    char pattern[256] = { '[', ':', 0x000 };
794    int32_t patternLen;
795    UChar uPattern[256] = {0};
796    UErrorCode status = U_ZERO_ERROR;
797    int32_t i;
798
799    /* create the sets with script codes */
800    for(i = 0; i<scriptsLen; i++){
801        strcat(pattern, uscript_getShortName(scriptCodes[i]));
802        strcat(pattern, ":]");
803        patternLen = (int32_t)strlen(pattern);
804        u_charsToUChars(pattern, uPattern, patternLen);
805        scripts[i] = uset_openPattern(uPattern, patternLen, &status);
806        if(U_FAILURE(status)){
807            log_err("Could not create set for pattern %s. Error: %s\n", pattern, u_errorName(status));
808            return;
809        }
810        pattern[2] = 0;
811    }
812    if (strcmp(locale, "uk") == 0 || strcmp(locale, "uk_UA") == 0) {
813        /* Special addition. Add the modifying apostrophe, which isn't in Cyrillic. */
814        uset_add(scripts[0], 0x2bc);
815    }
816    if(U_SUCCESS(status)){
817        UBool existsInScript = FALSE;
818        /* iterate over the exemplarSet and ascertain if all
819         * UChars in exemplarSet belong to the scripts returned
820         * by getScript
821         */
822        int32_t count = uset_getItemCount(exemplarSet);
823
824        for( i=0; i < count; i++){
825            UChar32 start = 0;
826            UChar32 end = 0;
827            UChar *str = NULL;
828            int32_t strCapacity = 0;
829
830            strCapacity = uset_getItem(exemplarSet, i, &start, &end, str, strCapacity, &status);
831            if(U_SUCCESS(status)){
832                int32_t j;
833                if(strCapacity == 0){
834                    /* ok the item is a range */
835                     for( j = 0; j < scriptsLen; j++){
836                        if(uset_containsRange(scripts[j], start, end) == TRUE){
837                            existsInScript = TRUE;
838                        }
839                    }
840                    if(existsInScript == FALSE){
841                        for( j = 0; j < scriptsLen; j++){
842                            UChar toPattern[500]={'\0'};
843                            char pat[500]={'\0'};
844                            int32_t len = uset_toPattern(scripts[j], toPattern, 500, TRUE, &status);
845                            len = myUCharsToChars(toPattern, pat, len);
846                            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));
847                            if(len!=-1){
848                                log_err("Pattern: %s\n",pat);
849                            }
850                        }
851                        log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale);
852                    }
853                }else{
854                    strCapacity++; /* increment for NUL termination */
855                    /* allocate the str and call the api again */
856                    str = (UChar*) malloc(U_SIZEOF_UCHAR * strCapacity);
857                    strCapacity =  uset_getItem(exemplarSet, i, &start, &end, str, strCapacity, &status);
858                    /* iterate over the scripts and figure out if the string contained is actually
859                     * in the script set
860                     */
861                    for( j = 0; j < scriptsLen; j++){
862                        if(uset_containsString(scripts[j],str, strCapacity) == TRUE){
863                            existsInScript = TRUE;
864                        }
865                    }
866                    if(existsInScript == FALSE){
867                        log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale);
868                    }
869                }
870            }
871        }
872
873    }
874
875    /* close the sets */
876    for(i = 0; i<scriptsLen; i++){
877        uset_close(scripts[i]);
878    }
879}
880
881static void VerifyTranslation(void) {
882    UResourceBundle *root, *currentLocale;
883    int32_t locCount = uloc_countAvailable();
884    int32_t locIndex;
885    UErrorCode errorCode = U_ZERO_ERROR;
886    const char *currLoc;
887    UScriptCode scripts[USCRIPT_CODE_LIMIT];
888    int32_t numScripts;
889    int32_t idx;
890    int32_t end;
891    UResourceBundle *resArray;
892
893    if (locCount <= 1) {
894        log_data_err("At least root needs to be installed\n");
895    }
896
897    root = ures_openDirect(NULL, "root", &errorCode);
898    if(U_FAILURE(errorCode)) {
899        log_data_err("Can't open root\n");
900        return;
901    }
902    for (locIndex = 0; locIndex < locCount; locIndex++) {
903        USet * mergedExemplarSet = NULL;
904        errorCode=U_ZERO_ERROR;
905        currLoc = uloc_getAvailable(locIndex);
906        currentLocale = ures_open(NULL, currLoc, &errorCode);
907        if(errorCode != U_ZERO_ERROR) {
908            if(U_SUCCESS(errorCode)) {
909                /* It's installed, but there is no data.
910                   It's installed for the g18n white paper [grhoten] */
911                log_err("ERROR: Locale %-5s not installed, and it should be!\n",
912                    uloc_getAvailable(locIndex));
913            } else {
914                log_err("%%%%%%% Unexpected error %d in %s %%%%%%%",
915                    u_errorName(errorCode),
916                    uloc_getAvailable(locIndex));
917            }
918            ures_close(currentLocale);
919            continue;
920        }
921        {
922            UErrorCode exemplarStatus = U_ZERO_ERROR;
923            ULocaleData * uld = ulocdata_open(currLoc, &exemplarStatus);
924            if (U_SUCCESS(exemplarStatus)) {
925                USet * exemplarSet = ulocdata_getExemplarSet(uld, NULL, USET_ADD_CASE_MAPPINGS, ULOCDATA_ES_STANDARD, &exemplarStatus);
926                if (U_SUCCESS(exemplarStatus)) {
927                    mergedExemplarSet = uset_cloneAsThawed(exemplarSet);
928                    uset_close(exemplarSet);
929                    exemplarSet = ulocdata_getExemplarSet(uld, NULL, USET_ADD_CASE_MAPPINGS, ULOCDATA_ES_AUXILIARY, &exemplarStatus);
930                    if (U_SUCCESS(exemplarStatus)) {
931                        uset_addAll(mergedExemplarSet, exemplarSet);
932                        uset_close(exemplarSet);
933                    }
934                    exemplarStatus = U_ZERO_ERROR;
935                    exemplarSet = ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_PUNCTUATION, &exemplarStatus);
936                    if (U_SUCCESS(exemplarStatus)) {
937                        uset_addAll(mergedExemplarSet, exemplarSet);
938                        uset_close(exemplarSet);
939                    }
940                } else {
941                    log_err("error ulocdata_getExemplarSet (main) for locale %s returned %s\n", currLoc, u_errorName(errorCode));
942                }
943                ulocdata_close(uld);
944            } else {
945                log_err("error ulocdata_open for locale %s returned %s\n", currLoc, u_errorName(errorCode));
946            }
947        }
948        if (mergedExemplarSet == NULL /*|| (getTestOption(QUICK_OPTION) && uset_size() > 2048)*/) {
949            log_verbose("skipping test for %s\n", currLoc);
950        }
951        //else if (uprv_strncmp(currLoc,"bem",3) == 0 || uprv_strncmp(currLoc,"mgo",3) == 0 || uprv_strncmp(currLoc,"nl",2) == 0) {
952        //    log_verbose("skipping test for %s, some month and country names known to use aux exemplars\n", currLoc);
953        //}
954        else {
955            UChar langBuffer[128];
956            int32_t langSize;
957            int32_t strIdx;
958            UChar badChar;
959            langSize = uloc_getDisplayLanguage(currLoc, currLoc, langBuffer, sizeof(langBuffer)/sizeof(langBuffer[0]), &errorCode);
960            if (U_FAILURE(errorCode)) {
961                log_err("error uloc_getDisplayLanguage returned %s\n", u_errorName(errorCode));
962            }
963            else {
964                strIdx = findStringSetMismatch(currLoc, langBuffer, langSize, mergedExemplarSet, FALSE, &badChar);
965                if (strIdx >= 0) {
966                    log_err("getDisplayLanguage(%s) at index %d returned characters not in the exemplar characters: %04X.\n",
967                        currLoc, strIdx, badChar);
968                }
969            }
970            langSize = uloc_getDisplayCountry(currLoc, currLoc, langBuffer, sizeof(langBuffer)/sizeof(langBuffer[0]), &errorCode);
971            if (U_FAILURE(errorCode)) {
972                log_err("error uloc_getDisplayCountry returned %s\n", u_errorName(errorCode));
973            }
974            {
975                UResourceBundle* cal = ures_getByKey(currentLocale, "calendar", NULL, &errorCode);
976                UResourceBundle* greg = ures_getByKeyWithFallback(cal, "gregorian", NULL, &errorCode);
977                UResourceBundle* names = ures_getByKeyWithFallback(greg,  "dayNames", NULL, &errorCode);
978                UResourceBundle* format = ures_getByKeyWithFallback(names,  "format", NULL, &errorCode);
979                resArray = ures_getByKeyWithFallback(format,  "wide", NULL, &errorCode);
980
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                if ((uprv_strncmp(currLoc,"lrc",3) == 0 || uprv_strncmp(currLoc,"mzn",3) == 0) &&
992                        log_knownIssue("cldrbug:8899", "lrc and mzn locales don't have translated day names")) {
993                    end = 0;
994                }
995
996                for (idx = 0; idx < end; idx++) {
997                    const UChar *fromBundleStr = ures_getStringByIndex(resArray, idx, &langSize, &errorCode);
998                    if (U_FAILURE(errorCode)) {
999                        log_err("error ures_getStringByIndex(%d) returned %s\n", idx, u_errorName(errorCode));
1000                        continue;
1001                    }
1002                    strIdx = findStringSetMismatch(currLoc, fromBundleStr, langSize, mergedExemplarSet, TRUE, &badChar);
1003                    if ( strIdx >= 0 ) {
1004                        log_err("getDayNames(%s, %d) at index %d returned characters not in the exemplar characters: %04X.\n",
1005                            currLoc, idx, strIdx, badChar);
1006                    }
1007                }
1008                ures_close(resArray);
1009                ures_close(format);
1010                ures_close(names);
1011
1012                names = ures_getByKeyWithFallback(greg, "monthNames", NULL, &errorCode);
1013                format = ures_getByKeyWithFallback(names,"format", NULL, &errorCode);
1014                resArray = ures_getByKeyWithFallback(format, "wide", NULL, &errorCode);
1015                if (U_FAILURE(errorCode)) {
1016                    log_err("error ures_getByKey returned %s\n", u_errorName(errorCode));
1017                }
1018                if (getTestOption(QUICK_OPTION)) {
1019                    end = 1;
1020                }
1021                else {
1022                    end = ures_getSize(resArray);
1023                }
1024
1025                for (idx = 0; idx < end; idx++) {
1026                    const UChar *fromBundleStr = ures_getStringByIndex(resArray, idx, &langSize, &errorCode);
1027                    if (U_FAILURE(errorCode)) {
1028                        log_err("error ures_getStringByIndex(%d) returned %s\n", idx, u_errorName(errorCode));
1029                        continue;
1030                    }
1031                    strIdx = findStringSetMismatch(currLoc, fromBundleStr, langSize, mergedExemplarSet, TRUE, &badChar);
1032                    if (strIdx >= 0) {
1033                        log_err("getMonthNames(%s, %d) at index %d returned characters not in the exemplar characters: %04X.\n",
1034                            currLoc, idx, strIdx, badChar);
1035                    }
1036                }
1037                ures_close(resArray);
1038                ures_close(format);
1039                ures_close(names);
1040                ures_close(greg);
1041                ures_close(cal);
1042            }
1043            errorCode = U_ZERO_ERROR;
1044            numScripts = uscript_getCode(currLoc, scripts, sizeof(scripts)/sizeof(scripts[0]), &errorCode);
1045            if (strcmp(currLoc, "yi") == 0 && numScripts > 0 && log_knownIssue("11217", "Fix result of uscript_getCode for yi: USCRIPT_YI -> USCRIPT_HEBREW")) {
1046                scripts[0] = USCRIPT_HEBREW;
1047            }
1048            if (numScripts == 0) {
1049                log_err("uscript_getCode(%s) doesn't work.\n", currLoc);
1050            }else if(scripts[0] == USCRIPT_COMMON){
1051                log_err("uscript_getCode(%s) returned USCRIPT_COMMON.\n", currLoc);
1052            }
1053
1054            /* test that the scripts are a superset of exemplar characters. */
1055           {
1056                ULocaleData *uld = ulocdata_open(currLoc,&errorCode);
1057                USet *exemplarSet =  ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &errorCode);
1058                /* test if exemplar characters are part of script code */
1059                findSetMatch(scripts, numScripts, exemplarSet, currLoc);
1060                uset_close(exemplarSet);
1061                ulocdata_close(uld);
1062            }
1063
1064           /* test that the paperSize API works */
1065           {
1066               int32_t height=0, width=0;
1067               ulocdata_getPaperSize(currLoc, &height, &width, &errorCode);
1068               if(U_FAILURE(errorCode)){
1069                   log_err("ulocdata_getPaperSize failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode));
1070               }
1071               if(strstr(currLoc, "_US")!=NULL && height != 279 && width != 216 ){
1072                   log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc);
1073               }
1074           }
1075            /* test that the MeasurementSystem API works */
1076           {
1077               char fullLoc[ULOC_FULLNAME_CAPACITY];
1078               UMeasurementSystem measurementSystem;
1079               int32_t height = 0, width = 0;
1080
1081               uloc_addLikelySubtags(currLoc, fullLoc, ULOC_FULLNAME_CAPACITY, &errorCode);
1082
1083               errorCode = U_ZERO_ERROR;
1084               measurementSystem = ulocdata_getMeasurementSystem(currLoc, &errorCode);
1085               if (U_FAILURE(errorCode)) {
1086                   log_err("ulocdata_getMeasurementSystem failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode));
1087               } else {
1088                   if ( strstr(fullLoc, "_US")!=NULL || strstr(fullLoc, "_MM")!=NULL || strstr(fullLoc, "_LR")!=NULL ) {
1089                       if(measurementSystem != UMS_US){
1090                            log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc);
1091                       }
1092                   } else if ( strstr(fullLoc, "_GB")!=NULL ) {
1093                       if(measurementSystem != UMS_UK){
1094                            log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc);
1095                       }
1096                   } else if (measurementSystem != UMS_SI) {
1097                       log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc);
1098                   }
1099               }
1100
1101               errorCode = U_ZERO_ERROR;
1102               ulocdata_getPaperSize(currLoc, &height, &width, &errorCode);
1103               if (U_FAILURE(errorCode)) {
1104                   log_err("ulocdata_getPaperSize failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode));
1105               } else {
1106                   if ( strstr(fullLoc, "_US")!=NULL || strstr(fullLoc, "_BZ")!=NULL || strstr(fullLoc, "_CA")!=NULL || strstr(fullLoc, "_CL")!=NULL ||
1107                        strstr(fullLoc, "_CO")!=NULL || strstr(fullLoc, "_CR")!=NULL || strstr(fullLoc, "_GT")!=NULL || strstr(fullLoc, "_MX")!=NULL ||
1108                        strstr(fullLoc, "_NI")!=NULL || strstr(fullLoc, "_PA")!=NULL || strstr(fullLoc, "_PH")!=NULL || strstr(fullLoc, "_PR")!=NULL ||
1109                        strstr(fullLoc, "_SV")!=NULL || strstr(fullLoc, "_VE")!=NULL ) {
1110                       if (height != 279 || width != 216) {
1111                            log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc);
1112                       }
1113                   } else if (height != 297 || width != 210) {
1114                       log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc);
1115                   }
1116               }
1117           }
1118        }
1119        if (mergedExemplarSet != NULL) {
1120            uset_close(mergedExemplarSet);
1121        }
1122        ures_close(currentLocale);
1123    }
1124
1125    ures_close(root);
1126}
1127
1128/* adjust this limit as appropriate */
1129#define MAX_SCRIPTS_PER_LOCALE 8
1130
1131static void TestExemplarSet(void){
1132    int32_t i, j, k, m, n;
1133    int32_t equalCount = 0;
1134    UErrorCode ec = U_ZERO_ERROR;
1135    UEnumeration* avail;
1136    USet* exemplarSets[2];
1137    USet* unassignedSet;
1138    UScriptCode code[MAX_SCRIPTS_PER_LOCALE];
1139    USet* codeSets[MAX_SCRIPTS_PER_LOCALE];
1140    int32_t codeLen;
1141    char cbuf[32]; /* 9 should be enough */
1142    UChar ubuf[64]; /* adjust as needed */
1143    UBool existsInScript;
1144    int32_t itemCount;
1145    int32_t strLen;
1146    UChar32 start, end;
1147
1148    unassignedSet = NULL;
1149    exemplarSets[0] = NULL;
1150    exemplarSets[1] = NULL;
1151    for (i=0; i<MAX_SCRIPTS_PER_LOCALE; ++i) {
1152        codeSets[i] = NULL;
1153    }
1154
1155    avail = ures_openAvailableLocales(NULL, &ec);
1156    if (!assertSuccess("ures_openAvailableLocales", &ec)) goto END;
1157    n = uenum_count(avail, &ec);
1158    if (!assertSuccess("uenum_count", &ec)) goto END;
1159
1160    u_uastrcpy(ubuf, "[:unassigned:]");
1161    unassignedSet = uset_openPattern(ubuf, -1, &ec);
1162    if (!assertSuccess("uset_openPattern", &ec)) goto END;
1163
1164    for(i=0; i<n; i++){
1165        const char* locale = uenum_next(avail, NULL, &ec);
1166        if (!assertSuccess("uenum_next", &ec)) goto END;
1167        log_verbose("%s\n", locale);
1168        for (k=0; k<2; ++k) {
1169            uint32_t option = (k==0) ? 0 : USET_CASE_INSENSITIVE;
1170            ULocaleData *uld = ulocdata_open(locale,&ec);
1171            USet* exemplarSet = ulocdata_getExemplarSet(uld,NULL, option, ULOCDATA_ES_STANDARD, &ec);
1172            uset_close(exemplarSets[k]);
1173            ulocdata_close(uld);
1174            exemplarSets[k] = exemplarSet;
1175            if (!assertSuccess("ulocaledata_getExemplarSet", &ec)) goto END;
1176
1177            if (uset_containsSome(exemplarSet, unassignedSet)) {
1178                log_err("ExemplarSet contains unassigned characters for locale : %s\n", locale);
1179            }
1180            codeLen = uscript_getCode(locale, code, 8, &ec);
1181            if (strcmp(locale, "yi") == 0 && codeLen > 0 && log_knownIssue("11217", "Fix result of uscript_getCode for yi: USCRIPT_YI -> USCRIPT_HEBREW")) {
1182                code[0] = USCRIPT_HEBREW;
1183            }
1184            if (!assertSuccess("uscript_getCode", &ec)) goto END;
1185
1186            for (j=0; j<MAX_SCRIPTS_PER_LOCALE; ++j) {
1187                uset_close(codeSets[j]);
1188                codeSets[j] = NULL;
1189            }
1190            for (j=0; j<codeLen; ++j) {
1191                uprv_strcpy(cbuf, "[:");
1192                if(code[j]==-1){
1193                    log_err("USCRIPT_INVALID_CODE returned for locale: %s\n", locale);
1194                    continue;
1195                }
1196                uprv_strcat(cbuf, uscript_getShortName(code[j]));
1197                uprv_strcat(cbuf, ":]");
1198                u_uastrcpy(ubuf, cbuf);
1199                codeSets[j] = uset_openPattern(ubuf, -1, &ec);
1200            }
1201            if (!assertSuccess("uset_openPattern", &ec)) goto END;
1202
1203            existsInScript = FALSE;
1204            itemCount = uset_getItemCount(exemplarSet);
1205            for (m=0; m<itemCount && !existsInScript; ++m) {
1206                strLen = uset_getItem(exemplarSet, m, &start, &end, ubuf,
1207                                      sizeof(ubuf)/sizeof(ubuf[0]), &ec);
1208                /* failure here might mean str[] needs to be larger */
1209                if (!assertSuccess("uset_getItem", &ec)) goto END;
1210                if (strLen == 0) {
1211                    for (j=0; j<codeLen; ++j) {
1212                        if (codeSets[j]!=NULL && uset_containsRange(codeSets[j], start, end)) {
1213                            existsInScript = TRUE;
1214                            break;
1215                        }
1216                    }
1217                } else {
1218                    for (j=0; j<codeLen; ++j) {
1219                        if (codeSets[j]!=NULL && uset_containsString(codeSets[j], ubuf, strLen)) {
1220                            existsInScript = TRUE;
1221                            break;
1222                        }
1223                    }
1224                }
1225            }
1226
1227            if (existsInScript == FALSE){
1228                log_err("ExemplarSet containment failed for locale : %s\n", locale);
1229            }
1230        }
1231        assertTrue("case-folded is a superset",
1232                   uset_containsAll(exemplarSets[1], exemplarSets[0]));
1233        if (uset_equals(exemplarSets[1], exemplarSets[0])) {
1234            ++equalCount;
1235        }
1236    }
1237    /* Note: The case-folded set should sometimes be a strict superset
1238       and sometimes be equal. */
1239    assertTrue("case-folded is sometimes a strict superset, and sometimes equal",
1240               equalCount > 0 && equalCount < n);
1241
1242 END:
1243    uenum_close(avail);
1244    uset_close(exemplarSets[0]);
1245    uset_close(exemplarSets[1]);
1246    uset_close(unassignedSet);
1247    for (i=0; i<MAX_SCRIPTS_PER_LOCALE; ++i) {
1248        uset_close(codeSets[i]);
1249    }
1250}
1251
1252enum { kUBufMax = 32 };
1253static void TestLocaleDisplayPattern(void){
1254    UErrorCode status;
1255    UChar pattern[kUBufMax] = {0,};
1256    UChar separator[kUBufMax] = {0,};
1257    ULocaleData *uld;
1258    static const UChar enExpectPat[] = { 0x007B,0x0030,0x007D,0x0020,0x0028,0x007B,0x0031,0x007D,0x0029,0 }; /* "{0} ({1})" */
1259    static const UChar enExpectSep[] = { 0x002C,0x0020,0 }; /* ", " */
1260    static const UChar zhExpectPat[] = { 0x007B,0x0030,0x007D,0xFF08,0x007B,0x0031,0x007D,0xFF09,0 };
1261    static const UChar zhExpectSep[] = { 0x3001,0 };
1262
1263    status = U_ZERO_ERROR;
1264    uld = ulocdata_open("en", &status);
1265    if(U_FAILURE(status)){
1266        log_data_err("ulocdata_open en error %s", u_errorName(status));
1267    } else {
1268        ulocdata_getLocaleDisplayPattern(uld, pattern, kUBufMax, &status);
1269        if (U_FAILURE(status)){
1270            log_err("ulocdata_getLocaleDisplayPattern en error %s", u_errorName(status));
1271        } else if (u_strcmp(pattern, enExpectPat) != 0) {
1272             log_err("ulocdata_getLocaleDisplayPattern en returns unexpected pattern");
1273        }
1274        status = U_ZERO_ERROR;
1275        ulocdata_getLocaleSeparator(uld, separator, kUBufMax, &status);
1276        if (U_FAILURE(status)){
1277            log_err("ulocdata_getLocaleSeparator en error %s", u_errorName(status));
1278        } else if (u_strcmp(separator, enExpectSep) != 0) {
1279             log_err("ulocdata_getLocaleSeparator en returns unexpected string ");
1280        }
1281        ulocdata_close(uld);
1282    }
1283
1284    status = U_ZERO_ERROR;
1285    uld = ulocdata_open("zh", &status);
1286    if(U_FAILURE(status)){
1287        log_data_err("ulocdata_open zh error %s", u_errorName(status));
1288    } else {
1289        ulocdata_getLocaleDisplayPattern(uld, pattern, kUBufMax, &status);
1290        if (U_FAILURE(status)){
1291            log_err("ulocdata_getLocaleDisplayPattern zh error %s", u_errorName(status));
1292        } else if (u_strcmp(pattern, zhExpectPat) != 0) {
1293             log_err("ulocdata_getLocaleDisplayPattern zh returns unexpected pattern");
1294        }
1295        status = U_ZERO_ERROR;
1296        ulocdata_getLocaleSeparator(uld, separator, kUBufMax, &status);
1297        if (U_FAILURE(status)){
1298            log_err("ulocdata_getLocaleSeparator zh error %s", u_errorName(status));
1299        } else if (u_strcmp(separator, zhExpectSep) != 0) {
1300             log_err("ulocdata_getLocaleSeparator zh returns unexpected string ");
1301        }
1302        ulocdata_close(uld);
1303    }
1304}
1305
1306static void TestCoverage(void){
1307    ULocaleDataDelimiterType types[] = {
1308     ULOCDATA_QUOTATION_START,     /* Quotation start */
1309     ULOCDATA_QUOTATION_END,       /* Quotation end */
1310     ULOCDATA_ALT_QUOTATION_START, /* Alternate quotation start */
1311     ULOCDATA_ALT_QUOTATION_END,   /* Alternate quotation end */
1312     ULOCDATA_DELIMITER_COUNT
1313    };
1314    int i;
1315    UBool sub;
1316    UErrorCode status = U_ZERO_ERROR;
1317    ULocaleData *uld = ulocdata_open(uloc_getDefault(), &status);
1318
1319    if(U_FAILURE(status)){
1320        log_data_err("ulocdata_open error");
1321        return;
1322    }
1323
1324
1325    for(i = 0; i < ULOCDATA_DELIMITER_COUNT; i++){
1326        UChar result[32] = {0,};
1327        status = U_ZERO_ERROR;
1328        ulocdata_getDelimiter(uld, types[i], result, 32, &status);
1329        if (U_FAILURE(status)){
1330            log_err("ulocdata_getgetDelimiter error with type %d", types[i]);
1331        }
1332    }
1333
1334    sub = ulocdata_getNoSubstitute(uld);
1335    ulocdata_setNoSubstitute(uld,sub);
1336    ulocdata_close(uld);
1337}
1338
1339static void TestIndexChars(void) {
1340    /* Very basic test of ULOCDATA_ES_INDEX.
1341     * No comprehensive test of data, just basic check that the code path is alive.
1342     */
1343    UErrorCode status = U_ZERO_ERROR;
1344    ULocaleData  *uld;
1345    USet *exemplarChars;
1346    USet *indexChars;
1347
1348    uld = ulocdata_open("en", &status);
1349    exemplarChars = uset_openEmpty();
1350    indexChars = uset_openEmpty();
1351    ulocdata_getExemplarSet(uld, exemplarChars, 0, ULOCDATA_ES_STANDARD, &status);
1352    ulocdata_getExemplarSet(uld, indexChars, 0, ULOCDATA_ES_INDEX, &status);
1353    if (U_FAILURE(status)) {
1354        log_data_err("File %s, line %d, Failure opening exemplar chars: %s", __FILE__, __LINE__, u_errorName(status));
1355        goto close_sets;
1356    }
1357    /* en data, standard exemplars are [a-z], lower case. */
1358    /* en data, index characters are [A-Z], upper case. */
1359    if ((uset_contains(exemplarChars, (UChar32)0x41) || uset_contains(indexChars, (UChar32)0x61))) {
1360        log_err("File %s, line %d, Exemplar characters incorrect.", __FILE__, __LINE__ );
1361        goto close_sets;
1362    }
1363    if (!(uset_contains(exemplarChars, (UChar32)0x61) && uset_contains(indexChars, (UChar32)0x41) )) {
1364        log_err("File %s, line %d, Exemplar characters incorrect.", __FILE__, __LINE__ );
1365        goto close_sets;
1366    }
1367
1368  close_sets:
1369    uset_close(exemplarChars);
1370    uset_close(indexChars);
1371    ulocdata_close(uld);
1372}
1373
1374
1375
1376#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
1377static void TestCurrencyList(void){
1378#if !UCONFIG_NO_FORMATTING
1379    UErrorCode errorCode = U_ZERO_ERROR;
1380    int32_t structLocaleCount, currencyCount;
1381    UEnumeration *en = ucurr_openISOCurrencies(UCURR_ALL, &errorCode);
1382    const char *isoCode, *structISOCode;
1383    UResourceBundle *subBundle;
1384    UResourceBundle *currencies = ures_openDirect(loadTestData(&errorCode), "structLocale", &errorCode);
1385    if(U_FAILURE(errorCode)) {
1386        log_data_err("Can't open structLocale\n");
1387        return;
1388    }
1389    currencies = ures_getByKey(currencies, "Currencies", currencies, &errorCode);
1390    currencyCount = uenum_count(en, &errorCode);
1391    structLocaleCount = ures_getSize(currencies);
1392    if (currencyCount != structLocaleCount) {
1393        log_err("structLocale(%d) and ISO4217(%d) currency list are out of sync.\n", structLocaleCount, currencyCount);
1394#if U_CHARSET_FAMILY == U_ASCII_FAMILY
1395        ures_resetIterator(currencies);
1396        while ((isoCode = uenum_next(en, NULL, &errorCode)) != NULL && ures_hasNext(currencies)) {
1397            subBundle = ures_getNextResource(currencies, NULL, &errorCode);
1398            structISOCode = ures_getKey(subBundle);
1399            ures_close(subBundle);
1400            if (strcmp(structISOCode, isoCode) != 0) {
1401                log_err("First difference found at structLocale(%s) and ISO4217(%s).\n", structISOCode, isoCode);
1402                break;
1403            }
1404        }
1405#endif
1406    }
1407    ures_close(currencies);
1408    uenum_close(en);
1409#endif
1410}
1411#endif
1412
1413static void TestAvailableIsoCodes(void){
1414#if !UCONFIG_NO_FORMATTING
1415    UErrorCode errorCode = U_ZERO_ERROR;
1416    const char* eurCode = "EUR";
1417    const char* usdCode = "USD";
1418    const char* lastCode = "RHD";
1419    const char* zzzCode = "ZZZ";
1420    UDate date1950 = (UDate)-630720000000.0;/* year 1950 */
1421    UDate date1970 = (UDate)0.0;            /* year 1970 */
1422    UDate date1975 = (UDate)173448000000.0; /* year 1975 */
1423    UDate date1978 = (UDate)260172000000.0; /* year 1978 */
1424    UDate date1981 = (UDate)346896000000.0; /* year 1981 */
1425    UDate date1992 = (UDate)693792000000.0; /* year 1992 */
1426    UChar* isoCode = (UChar*)malloc(sizeof(UChar) * (uprv_strlen(usdCode) + 1));
1427
1428    /* testing available codes with no time ranges */
1429    u_charsToUChars(eurCode, isoCode, uprv_strlen(usdCode) + 1);
1430    if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == FALSE) {
1431       log_data_err("FAIL: ISO code (%s) is not found.\n", eurCode);
1432    }
1433
1434    u_charsToUChars(usdCode, isoCode, uprv_strlen(zzzCode) + 1);
1435    if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == FALSE) {
1436       log_data_err("FAIL: ISO code (%s) is not found.\n", usdCode);
1437    }
1438
1439    u_charsToUChars(zzzCode, isoCode, uprv_strlen(zzzCode) + 1);
1440    if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == TRUE) {
1441       log_err("FAIL: ISO code (%s) is reported as available, but it doesn't exist.\n", zzzCode);
1442    }
1443
1444    u_charsToUChars(lastCode, isoCode, uprv_strlen(zzzCode) + 1);
1445    if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == FALSE) {
1446       log_data_err("FAIL: ISO code (%s) is not found.\n", lastCode);
1447    }
1448
1449    /* RHD was used from 1970-02-17  to 1980-04-18*/
1450
1451    /* to = null */
1452    if (ucurr_isAvailable(isoCode, date1970, U_DATE_MAX, &errorCode) == FALSE) {
1453       log_data_err("FAIL: ISO code (%s) was available in time range >1970-01-01.\n", lastCode);
1454    }
1455
1456    if (ucurr_isAvailable(isoCode, date1975, U_DATE_MAX, &errorCode) == FALSE) {
1457       log_data_err("FAIL: ISO code (%s) was available in time range >1975.\n", lastCode);
1458    }
1459
1460    if (ucurr_isAvailable(isoCode, date1981, U_DATE_MAX, &errorCode) == TRUE) {
1461       log_err("FAIL: ISO code (%s) was not available in time range >1981.\n", lastCode);
1462    }
1463
1464    /* from = null */
1465    if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1970, &errorCode) == TRUE) {
1466       log_err("FAIL: ISO code (%s) was not available in time range <1970.\n", lastCode);
1467    }
1468
1469    if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1975, &errorCode) == FALSE) {
1470       log_data_err("FAIL: ISO code (%s) was available in time range <1975.\n", lastCode);
1471    }
1472
1473    if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1981, &errorCode) == FALSE) {
1474       log_data_err("FAIL: ISO code (%s) was available in time range <1981.\n", lastCode);
1475    }
1476
1477    /* full ranges */
1478    if (ucurr_isAvailable(isoCode, date1975, date1978, &errorCode) == FALSE) {
1479       log_data_err("FAIL: ISO code (%s) was available in time range 1975-1978.\n", lastCode);
1480    }
1481
1482    if (ucurr_isAvailable(isoCode, date1970, date1975, &errorCode) == FALSE) {
1483       log_data_err("FAIL: ISO code (%s) was available in time range 1970-1975.\n", lastCode);
1484    }
1485
1486    if (ucurr_isAvailable(isoCode, date1975, date1981, &errorCode) == FALSE) {
1487       log_data_err("FAIL: ISO code (%s) was available in time range 1975-1981.\n", lastCode);
1488    }
1489
1490    if (ucurr_isAvailable(isoCode, date1970,  date1981, &errorCode) == FALSE) {
1491       log_data_err("FAIL: ISO code (%s) was available in time range 1970-1981.\n", lastCode);
1492    }
1493
1494    if (ucurr_isAvailable(isoCode, date1981,  date1992, &errorCode) == TRUE) {
1495       log_err("FAIL: ISO code (%s) was not available in time range 1981-1992.\n", lastCode);
1496    }
1497
1498    if (ucurr_isAvailable(isoCode, date1950,  date1970, &errorCode) == TRUE) {
1499       log_err("FAIL: ISO code (%s) was not available in time range 1950-1970.\n", lastCode);
1500    }
1501
1502    /* wrong range - from > to*/
1503    if (ucurr_isAvailable(isoCode, date1975,  date1970, &errorCode) == TRUE) {
1504       log_err("FAIL: Wrong range 1975-1970 for ISO code (%s) was not reported.\n", lastCode);
1505    } else if (errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
1506       log_data_err("FAIL: Error code not reported for wrong range 1975-1970 for ISO code (%s).\n", lastCode);
1507    }
1508
1509    free(isoCode);
1510#endif
1511}
1512
1513#define TESTCASE(name) addTest(root, &name, "tsutil/cldrtest/" #name)
1514
1515void addCLDRTest(TestNode** root);
1516
1517void addCLDRTest(TestNode** root)
1518{
1519#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
1520    TESTCASE(TestLocaleStructure);
1521    TESTCASE(TestCurrencyList);
1522#endif
1523    TESTCASE(TestConsistentCountryInfo);
1524    TESTCASE(VerifyTranslation);
1525    TESTCASE(TestExemplarSet);
1526    TESTCASE(TestLocaleDisplayPattern);
1527    TESTCASE(TestCoverage);
1528    TESTCASE(TestIndexChars);
1529    TESTCASE(TestAvailableIsoCodes);
1530}
1531
1532