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