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