1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7#include "cintltst.h"
8#include "unicode/ures.h"
9#include "unicode/ucurr.h"
10#include "unicode/ustring.h"
11#include "unicode/uset.h"
12#include "unicode/udat.h"
13#include "unicode/uscript.h"
14#include "unicode/ulocdata.h"
15#include "cstring.h"
16#include "locmap.h"
17#include "uresimp.h"
18
19/*
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 loale 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
992                for (idx = 0; idx < end; idx++) {
993                    const UChar *fromBundleStr = ures_getStringByIndex(resArray, idx, &langSize, &errorCode);
994                    if (U_FAILURE(errorCode)) {
995                        log_err("error ures_getStringByIndex(%d) returned %s\n", idx, u_errorName(errorCode));
996                        continue;
997                    }
998                    if (uprv_strstr(currLoc, "uz_Arab") != currLoc || !log_knownIssue("10405", "skipping exemplar check: %s", currLoc)) { /* TODO: FIX or REMOVE this test! */
999                        strIdx = findStringSetMismatch(currLoc, fromBundleStr, langSize, mergedExemplarSet, TRUE, &badChar);
1000                        if (strIdx >= 0) {
1001                            log_err("getDayNames(%s, %d) at index %d returned characters not in the exemplar characters: %04X.\n",
1002                                currLoc, idx, strIdx, badChar);
1003                        }
1004                    }
1005                }
1006                ures_close(resArray);
1007                ures_close(format);
1008                ures_close(names);
1009
1010                names = ures_getByKeyWithFallback(greg, "monthNames", NULL, &errorCode);
1011                format = ures_getByKeyWithFallback(names,"format", NULL, &errorCode);
1012                resArray = ures_getByKeyWithFallback(format, "wide", NULL, &errorCode);
1013                if (U_FAILURE(errorCode)) {
1014                    log_err("error ures_getByKey returned %s\n", u_errorName(errorCode));
1015                }
1016                if (getTestOption(QUICK_OPTION)) {
1017                    end = 1;
1018                }
1019                else {
1020                    end = ures_getSize(resArray);
1021                }
1022
1023                for (idx = 0; idx < end; idx++) {
1024                    const UChar *fromBundleStr = ures_getStringByIndex(resArray, idx, &langSize, &errorCode);
1025                    if (U_FAILURE(errorCode)) {
1026                        log_err("error ures_getStringByIndex(%d) returned %s\n", idx, u_errorName(errorCode));
1027                        continue;
1028                    }
1029                    if (uprv_strstr(currLoc, "uz_Arab") != currLoc || !log_knownIssue("10405", "skipping exemplar check: %s", currLoc)) { /* TODO: FIX or REMOVE this test! */
1030                        strIdx = findStringSetMismatch(currLoc, fromBundleStr, langSize, mergedExemplarSet, TRUE, &badChar);
1031                        if (strIdx >= 0) {
1032                            log_err("getMonthNames(%s, %d) at index %d returned characters not in the exemplar characters: %04X.\n",
1033                                currLoc, idx, strIdx, badChar);
1034                        }
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 (numScripts == 0) {
1046                log_err("uscript_getCode(%s) doesn't work.\n", currLoc);
1047            }else if(scripts[0] == USCRIPT_COMMON){
1048                log_err("uscript_getCode(%s) returned USCRIPT_COMMON.\n", currLoc);
1049            }
1050
1051            /* test that the scripts are a superset of exemplar characters. */
1052           {
1053                ULocaleData *uld = ulocdata_open(currLoc,&errorCode);
1054                USet *exemplarSet =  ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &errorCode);
1055                /* test if exemplar characters are part of script code */
1056                findSetMatch(scripts, numScripts, exemplarSet, currLoc);
1057                uset_close(exemplarSet);
1058                ulocdata_close(uld);
1059            }
1060
1061           /* test that the paperSize API works */
1062           {
1063               int32_t height=0, width=0;
1064               ulocdata_getPaperSize(currLoc, &height, &width, &errorCode);
1065               if(U_FAILURE(errorCode)){
1066                   log_err("ulocdata_getPaperSize failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode));
1067               }
1068               if(strstr(currLoc, "_US")!=NULL && height != 279 && width != 216 ){
1069                   log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc);
1070               }
1071           }
1072            /* test that the MeasurementSystem API works */
1073           {
1074               char fullLoc[ULOC_FULLNAME_CAPACITY];
1075               UMeasurementSystem measurementSystem;
1076               int32_t height = 0, width = 0;
1077
1078               uloc_addLikelySubtags(currLoc, fullLoc, ULOC_FULLNAME_CAPACITY, &errorCode);
1079
1080               errorCode = U_ZERO_ERROR;
1081               measurementSystem = ulocdata_getMeasurementSystem(currLoc, &errorCode);
1082               if (U_FAILURE(errorCode)) {
1083                   log_err("ulocdata_getMeasurementSystem failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode));
1084               } else {
1085                   if ( strstr(fullLoc, "_US")!=NULL || strstr(fullLoc, "_MM")!=NULL || strstr(fullLoc, "_LR")!=NULL ) {
1086                       if(measurementSystem != UMS_US){
1087                            log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc);
1088                       }
1089                   } else if (measurementSystem != UMS_SI) {
1090                       log_err("ulocdata_getMeasurementSystem did not return expected data for locale %s \n", currLoc);
1091                   }
1092               }
1093
1094               errorCode = U_ZERO_ERROR;
1095               ulocdata_getPaperSize(currLoc, &height, &width, &errorCode);
1096               if (U_FAILURE(errorCode)) {
1097                   log_err("ulocdata_getPaperSize failed for locale %s with error: %s \n", currLoc, u_errorName(errorCode));
1098               } else {
1099                   if ( strstr(fullLoc, "_US")!=NULL || strstr(fullLoc, "_BZ")!=NULL || strstr(fullLoc, "_CA")!=NULL || strstr(fullLoc, "_CL")!=NULL ||
1100                        strstr(fullLoc, "_CO")!=NULL || strstr(fullLoc, "_CR")!=NULL || strstr(fullLoc, "_GT")!=NULL || strstr(fullLoc, "_MX")!=NULL ||
1101                        strstr(fullLoc, "_NI")!=NULL || strstr(fullLoc, "_PA")!=NULL || strstr(fullLoc, "_PH")!=NULL || strstr(fullLoc, "_PR")!=NULL ||
1102                        strstr(fullLoc, "_SV")!=NULL || strstr(fullLoc, "_VE")!=NULL ) {
1103                       if (height != 279 || width != 216) {
1104                            log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc);
1105                       }
1106                   } else if (height != 297 || width != 210) {
1107                       log_err("ulocdata_getPaperSize did not return expected data for locale %s \n", currLoc);
1108                   }
1109               }
1110           }
1111        }
1112        if (mergedExemplarSet != NULL) {
1113            uset_close(mergedExemplarSet);
1114        }
1115        ures_close(currentLocale);
1116    }
1117
1118    ures_close(root);
1119}
1120
1121/* adjust this limit as appropriate */
1122#define MAX_SCRIPTS_PER_LOCALE 8
1123
1124static void TestExemplarSet(void){
1125    int32_t i, j, k, m, n;
1126    int32_t equalCount = 0;
1127    UErrorCode ec = U_ZERO_ERROR;
1128    UEnumeration* avail;
1129    USet* exemplarSets[2];
1130    USet* unassignedSet;
1131    UScriptCode code[MAX_SCRIPTS_PER_LOCALE];
1132    USet* codeSets[MAX_SCRIPTS_PER_LOCALE];
1133    int32_t codeLen;
1134    char cbuf[32]; /* 9 should be enough */
1135    UChar ubuf[64]; /* adjust as needed */
1136    UBool existsInScript;
1137    int32_t itemCount;
1138    int32_t strLen;
1139    UChar32 start, end;
1140
1141    unassignedSet = NULL;
1142    exemplarSets[0] = NULL;
1143    exemplarSets[1] = NULL;
1144    for (i=0; i<MAX_SCRIPTS_PER_LOCALE; ++i) {
1145        codeSets[i] = NULL;
1146    }
1147
1148    avail = ures_openAvailableLocales(NULL, &ec);
1149    if (!assertSuccess("ures_openAvailableLocales", &ec)) goto END;
1150    n = uenum_count(avail, &ec);
1151    if (!assertSuccess("uenum_count", &ec)) goto END;
1152
1153    u_uastrcpy(ubuf, "[:unassigned:]");
1154    unassignedSet = uset_openPattern(ubuf, -1, &ec);
1155    if (!assertSuccess("uset_openPattern", &ec)) goto END;
1156
1157    for(i=0; i<n; i++){
1158        const char* locale = uenum_next(avail, NULL, &ec);
1159        if (!assertSuccess("uenum_next", &ec)) goto END;
1160        log_verbose("%s\n", locale);
1161        for (k=0; k<2; ++k) {
1162            uint32_t option = (k==0) ? 0 : USET_CASE_INSENSITIVE;
1163            ULocaleData *uld = ulocdata_open(locale,&ec);
1164            USet* exemplarSet = ulocdata_getExemplarSet(uld,NULL, option, ULOCDATA_ES_STANDARD, &ec);
1165            uset_close(exemplarSets[k]);
1166            ulocdata_close(uld);
1167            exemplarSets[k] = exemplarSet;
1168            if (!assertSuccess("ulocaledata_getExemplarSet", &ec)) goto END;
1169
1170            if (uset_containsSome(exemplarSet, unassignedSet)) {
1171                log_err("ExemplarSet contains unassigned characters for locale : %s\n", locale);
1172            }
1173            codeLen = uscript_getCode(locale, code, 8, &ec);
1174            if (!assertSuccess("uscript_getCode", &ec)) goto END;
1175
1176            for (j=0; j<MAX_SCRIPTS_PER_LOCALE; ++j) {
1177                uset_close(codeSets[j]);
1178                codeSets[j] = NULL;
1179            }
1180            for (j=0; j<codeLen; ++j) {
1181                uprv_strcpy(cbuf, "[:");
1182                if(code[j]==-1){
1183                    log_err("USCRIPT_INVALID_CODE returned for locale: %s\n", locale);
1184                    continue;
1185                }
1186                uprv_strcat(cbuf, uscript_getShortName(code[j]));
1187                uprv_strcat(cbuf, ":]");
1188                u_uastrcpy(ubuf, cbuf);
1189                codeSets[j] = uset_openPattern(ubuf, -1, &ec);
1190            }
1191            if (!assertSuccess("uset_openPattern", &ec)) goto END;
1192
1193            existsInScript = FALSE;
1194            itemCount = uset_getItemCount(exemplarSet);
1195            for (m=0; m<itemCount && !existsInScript; ++m) {
1196                strLen = uset_getItem(exemplarSet, m, &start, &end, ubuf,
1197                                      sizeof(ubuf)/sizeof(ubuf[0]), &ec);
1198                /* failure here might mean str[] needs to be larger */
1199                if (!assertSuccess("uset_getItem", &ec)) goto END;
1200                if (strLen == 0) {
1201                    for (j=0; j<codeLen; ++j) {
1202                        if (codeSets[j]!=NULL && uset_containsRange(codeSets[j], start, end)) {
1203                            existsInScript = TRUE;
1204                            break;
1205                        }
1206                    }
1207                } else {
1208                    for (j=0; j<codeLen; ++j) {
1209                        if (codeSets[j]!=NULL && uset_containsString(codeSets[j], ubuf, strLen)) {
1210                            existsInScript = TRUE;
1211                            break;
1212                        }
1213                    }
1214                }
1215            }
1216
1217            if (existsInScript == FALSE){
1218                log_err("ExemplarSet containment failed for locale : %s\n", locale);
1219            }
1220        }
1221        assertTrue("case-folded is a superset",
1222                   uset_containsAll(exemplarSets[1], exemplarSets[0]));
1223        if (uset_equals(exemplarSets[1], exemplarSets[0])) {
1224            ++equalCount;
1225        }
1226    }
1227    /* Note: The case-folded set should sometimes be a strict superset
1228       and sometimes be equal. */
1229    assertTrue("case-folded is sometimes a strict superset, and sometimes equal",
1230               equalCount > 0 && equalCount < n);
1231
1232 END:
1233    uenum_close(avail);
1234    uset_close(exemplarSets[0]);
1235    uset_close(exemplarSets[1]);
1236    uset_close(unassignedSet);
1237    for (i=0; i<MAX_SCRIPTS_PER_LOCALE; ++i) {
1238        uset_close(codeSets[i]);
1239    }
1240}
1241
1242enum { kUBufMax = 32 };
1243static void TestLocaleDisplayPattern(void){
1244    UErrorCode status;
1245    UChar pattern[kUBufMax] = {0,};
1246    UChar separator[kUBufMax] = {0,};
1247    ULocaleData *uld;
1248    static const UChar enExpectPat[] = { 0x007B,0x0030,0x007D,0x0020,0x0028,0x007B,0x0031,0x007D,0x0029,0 }; /* "{0} ({1})" */
1249    static const UChar enExpectSep[] = { 0x002C,0x0020,0 }; /* ", " */
1250    static const UChar zhExpectPat[] = { 0x007B,0x0030,0x007D,0xFF08,0x007B,0x0031,0x007D,0xFF09,0 };
1251    static const UChar zhExpectSep[] = { 0x3001,0 };
1252
1253    status = U_ZERO_ERROR;
1254    uld = ulocdata_open("en", &status);
1255    if(U_FAILURE(status)){
1256        log_data_err("ulocdata_open en error %s", u_errorName(status));
1257    } else {
1258        ulocdata_getLocaleDisplayPattern(uld, pattern, kUBufMax, &status);
1259        if (U_FAILURE(status)){
1260            log_err("ulocdata_getLocaleDisplayPattern en error %s", u_errorName(status));
1261        } else if (u_strcmp(pattern, enExpectPat) != 0) {
1262             log_err("ulocdata_getLocaleDisplayPattern en returns unexpected pattern");
1263        }
1264        status = U_ZERO_ERROR;
1265        ulocdata_getLocaleSeparator(uld, separator, kUBufMax, &status);
1266        if (U_FAILURE(status)){
1267            log_err("ulocdata_getLocaleSeparator en error %s", u_errorName(status));
1268        } else if (u_strcmp(separator, enExpectSep) != 0) {
1269             log_err("ulocdata_getLocaleSeparator en returns unexpected string ");
1270        }
1271        ulocdata_close(uld);
1272    }
1273
1274    status = U_ZERO_ERROR;
1275    uld = ulocdata_open("zh", &status);
1276    if(U_FAILURE(status)){
1277        log_data_err("ulocdata_open zh error %s", u_errorName(status));
1278    } else {
1279        ulocdata_getLocaleDisplayPattern(uld, pattern, kUBufMax, &status);
1280        if (U_FAILURE(status)){
1281            log_err("ulocdata_getLocaleDisplayPattern zh error %s", u_errorName(status));
1282        } else if (u_strcmp(pattern, zhExpectPat) != 0) {
1283             log_err("ulocdata_getLocaleDisplayPattern zh returns unexpected pattern");
1284        }
1285        status = U_ZERO_ERROR;
1286        ulocdata_getLocaleSeparator(uld, separator, kUBufMax, &status);
1287        if (U_FAILURE(status)){
1288            log_err("ulocdata_getLocaleSeparator zh error %s", u_errorName(status));
1289        } else if (u_strcmp(separator, zhExpectSep) != 0) {
1290             log_err("ulocdata_getLocaleSeparator zh returns unexpected string ");
1291        }
1292        ulocdata_close(uld);
1293    }
1294}
1295
1296static void TestCoverage(void){
1297    ULocaleDataDelimiterType types[] = {
1298     ULOCDATA_QUOTATION_START,     /* Quotation start */
1299     ULOCDATA_QUOTATION_END,       /* Quotation end */
1300     ULOCDATA_ALT_QUOTATION_START, /* Alternate quotation start */
1301     ULOCDATA_ALT_QUOTATION_END,   /* Alternate quotation end */
1302     ULOCDATA_DELIMITER_COUNT
1303    };
1304    int i;
1305    UBool sub;
1306    UErrorCode status = U_ZERO_ERROR;
1307    ULocaleData *uld = ulocdata_open(uloc_getDefault(), &status);
1308
1309    if(U_FAILURE(status)){
1310        log_data_err("ulocdata_open error");
1311        return;
1312    }
1313
1314
1315    for(i = 0; i < ULOCDATA_DELIMITER_COUNT; i++){
1316        UChar result[32] = {0,};
1317        status = U_ZERO_ERROR;
1318        ulocdata_getDelimiter(uld, types[i], result, 32, &status);
1319        if (U_FAILURE(status)){
1320            log_err("ulocdata_getgetDelimiter error with type %d", types[i]);
1321        }
1322    }
1323
1324    sub = ulocdata_getNoSubstitute(uld);
1325    ulocdata_setNoSubstitute(uld,sub);
1326    ulocdata_close(uld);
1327}
1328
1329static void TestIndexChars(void) {
1330    /* Very basic test of ULOCDATA_ES_INDEX.
1331     * No comprehensive test of data, just basic check that the code path is alive.
1332     */
1333    UErrorCode status = U_ZERO_ERROR;
1334    ULocaleData  *uld;
1335    USet *exemplarChars;
1336    USet *indexChars;
1337
1338    uld = ulocdata_open("en", &status);
1339    exemplarChars = uset_openEmpty();
1340    indexChars = uset_openEmpty();
1341    ulocdata_getExemplarSet(uld, exemplarChars, 0, ULOCDATA_ES_STANDARD, &status);
1342    ulocdata_getExemplarSet(uld, indexChars, 0, ULOCDATA_ES_INDEX, &status);
1343    if (U_FAILURE(status)) {
1344        log_data_err("File %s, line %d, Failure opening exemplar chars: %s", __FILE__, __LINE__, u_errorName(status));
1345        goto close_sets;
1346    }
1347    /* en data, standard exemplars are [a-z], lower case. */
1348    /* en data, index characters are [A-Z], upper case. */
1349    if ((uset_contains(exemplarChars, (UChar32)0x41) || uset_contains(indexChars, (UChar32)0x61))) {
1350        log_err("File %s, line %d, Exemplar characters incorrect.", __FILE__, __LINE__ );
1351        goto close_sets;
1352    }
1353    if (!(uset_contains(exemplarChars, (UChar32)0x61) && uset_contains(indexChars, (UChar32)0x41) )) {
1354        log_err("File %s, line %d, Exemplar characters incorrect.", __FILE__, __LINE__ );
1355        goto close_sets;
1356    }
1357
1358  close_sets:
1359    uset_close(exemplarChars);
1360    uset_close(indexChars);
1361    ulocdata_close(uld);
1362}
1363
1364
1365
1366#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
1367static void TestCurrencyList(void){
1368#if !UCONFIG_NO_FORMATTING
1369    UErrorCode errorCode = U_ZERO_ERROR;
1370    int32_t structLocaleCount, currencyCount;
1371    UEnumeration *en = ucurr_openISOCurrencies(UCURR_ALL, &errorCode);
1372    const char *isoCode, *structISOCode;
1373    UResourceBundle *subBundle;
1374    UResourceBundle *currencies = ures_openDirect(loadTestData(&errorCode), "structLocale", &errorCode);
1375    if(U_FAILURE(errorCode)) {
1376        log_data_err("Can't open structLocale\n");
1377        return;
1378    }
1379    currencies = ures_getByKey(currencies, "Currencies", currencies, &errorCode);
1380    currencyCount = uenum_count(en, &errorCode);
1381    structLocaleCount = ures_getSize(currencies);
1382    if (currencyCount != structLocaleCount) {
1383        log_err("structLocale(%d) and ISO4217(%d) currency list are out of sync.\n", structLocaleCount, currencyCount);
1384#if U_CHARSET_FAMILY == U_ASCII_FAMILY
1385        ures_resetIterator(currencies);
1386        while ((isoCode = uenum_next(en, NULL, &errorCode)) != NULL && ures_hasNext(currencies)) {
1387            subBundle = ures_getNextResource(currencies, NULL, &errorCode);
1388            structISOCode = ures_getKey(subBundle);
1389            ures_close(subBundle);
1390            if (strcmp(structISOCode, isoCode) != 0) {
1391                log_err("First difference found at structLocale(%s) and ISO4217(%s).\n", structISOCode, isoCode);
1392                break;
1393            }
1394        }
1395#endif
1396    }
1397    ures_close(currencies);
1398    uenum_close(en);
1399#endif
1400}
1401#endif
1402
1403static void TestAvailableIsoCodes(void){
1404#if !UCONFIG_NO_FORMATTING
1405    UErrorCode errorCode = U_ZERO_ERROR;
1406    const char* eurCode = "EUR";
1407    const char* usdCode = "USD";
1408    const char* lastCode = "RHD";
1409    const char* zzzCode = "ZZZ";
1410    UDate date1950 = (UDate)-630720000000.0;/* year 1950 */
1411    UDate date1970 = (UDate)0.0;            /* year 1970 */
1412    UDate date1975 = (UDate)173448000000.0; /* year 1975 */
1413    UDate date1978 = (UDate)260172000000.0; /* year 1978 */
1414    UDate date1981 = (UDate)346896000000.0; /* year 1981 */
1415    UDate date1992 = (UDate)693792000000.0; /* year 1992 */
1416    UChar* isoCode = (UChar*)malloc(sizeof(UChar) * (uprv_strlen(usdCode) + 1));
1417
1418    /* testing available codes with no time ranges */
1419    u_charsToUChars(eurCode, isoCode, uprv_strlen(usdCode) + 1);
1420    if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == FALSE) {
1421       log_data_err("FAIL: ISO code (%s) is not found.\n", eurCode);
1422    }
1423
1424    u_charsToUChars(usdCode, isoCode, uprv_strlen(zzzCode) + 1);
1425    if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == FALSE) {
1426       log_data_err("FAIL: ISO code (%s) is not found.\n", usdCode);
1427    }
1428
1429    u_charsToUChars(zzzCode, isoCode, uprv_strlen(zzzCode) + 1);
1430    if (ucurr_isAvailable(isoCode, U_DATE_MIN, U_DATE_MAX, &errorCode) == TRUE) {
1431       log_err("FAIL: ISO code (%s) is reported as available, but it doesn't exist.\n", zzzCode);
1432    }
1433
1434    u_charsToUChars(lastCode, 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", lastCode);
1437    }
1438
1439    /* RHD was used from 1970-02-17  to 1980-04-18*/
1440
1441    /* to = null */
1442    if (ucurr_isAvailable(isoCode, date1970, U_DATE_MAX, &errorCode) == FALSE) {
1443       log_data_err("FAIL: ISO code (%s) was available in time range >1970-01-01.\n", lastCode);
1444    }
1445
1446    if (ucurr_isAvailable(isoCode, date1975, U_DATE_MAX, &errorCode) == FALSE) {
1447       log_data_err("FAIL: ISO code (%s) was available in time range >1975.\n", lastCode);
1448    }
1449
1450    if (ucurr_isAvailable(isoCode, date1981, U_DATE_MAX, &errorCode) == TRUE) {
1451       log_err("FAIL: ISO code (%s) was not available in time range >1981.\n", lastCode);
1452    }
1453
1454    /* from = null */
1455    if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1970, &errorCode) == TRUE) {
1456       log_err("FAIL: ISO code (%s) was not available in time range <1970.\n", lastCode);
1457    }
1458
1459    if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1975, &errorCode) == FALSE) {
1460       log_data_err("FAIL: ISO code (%s) was available in time range <1975.\n", lastCode);
1461    }
1462
1463    if (ucurr_isAvailable(isoCode, U_DATE_MIN, date1981, &errorCode) == FALSE) {
1464       log_data_err("FAIL: ISO code (%s) was available in time range <1981.\n", lastCode);
1465    }
1466
1467    /* full ranges */
1468    if (ucurr_isAvailable(isoCode, date1975, date1978, &errorCode) == FALSE) {
1469       log_data_err("FAIL: ISO code (%s) was available in time range 1975-1978.\n", lastCode);
1470    }
1471
1472    if (ucurr_isAvailable(isoCode, date1970, date1975, &errorCode) == FALSE) {
1473       log_data_err("FAIL: ISO code (%s) was available in time range 1970-1975.\n", lastCode);
1474    }
1475
1476    if (ucurr_isAvailable(isoCode, date1975, date1981, &errorCode) == FALSE) {
1477       log_data_err("FAIL: ISO code (%s) was available in time range 1975-1981.\n", lastCode);
1478    }
1479
1480    if (ucurr_isAvailable(isoCode, date1970,  date1981, &errorCode) == FALSE) {
1481       log_data_err("FAIL: ISO code (%s) was available in time range 1970-1981.\n", lastCode);
1482    }
1483
1484    if (ucurr_isAvailable(isoCode, date1981,  date1992, &errorCode) == TRUE) {
1485       log_err("FAIL: ISO code (%s) was not available in time range 1981-1992.\n", lastCode);
1486    }
1487
1488    if (ucurr_isAvailable(isoCode, date1950,  date1970, &errorCode) == TRUE) {
1489       log_err("FAIL: ISO code (%s) was not available in time range 1950-1970.\n", lastCode);
1490    }
1491
1492    /* wrong range - from > to*/
1493    if (ucurr_isAvailable(isoCode, date1975,  date1970, &errorCode) == TRUE) {
1494       log_err("FAIL: Wrong range 1975-1970 for ISO code (%s) was not reported.\n", lastCode);
1495    } else if (errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
1496       log_data_err("FAIL: Error code not reported for wrong range 1975-1970 for ISO code (%s).\n", lastCode);
1497    }
1498
1499    free(isoCode);
1500#endif
1501}
1502
1503#define TESTCASE(name) addTest(root, &name, "tsutil/cldrtest/" #name)
1504
1505void addCLDRTest(TestNode** root);
1506
1507void addCLDRTest(TestNode** root)
1508{
1509#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
1510    TESTCASE(TestLocaleStructure);
1511    TESTCASE(TestCurrencyList);
1512#endif
1513    TESTCASE(TestConsistentCountryInfo);
1514    TESTCASE(VerifyTranslation);
1515    TESTCASE(TestExemplarSet);
1516    TESTCASE(TestLocaleDisplayPattern);
1517    TESTCASE(TestCoverage);
1518    TESTCASE(TestIndexChars);
1519    TESTCASE(TestAvailableIsoCodes);
1520}
1521
1522