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