]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/cintltst/cldrtest.c
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cldrtest.c
index 053b109e46ef1ee1cc969af2251d98e919597a3c..fe4716eddeeeb96f8d97b6521ff13646babf51df 100644 (file)
@@ -1,11 +1,12 @@
 /********************************************************************
  * COPYRIGHT:
- * Copyright (c) 1997-2004, International Business Machines Corporation and
+ * Copyright (c) 1997-2006, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 
 #include "cintltst.h"
 #include "unicode/ures.h"
+#include "unicode/ucurr.h"
 #include "unicode/ustring.h"
 #include "unicode/uset.h"
 #include "unicode/udat.h"
 #include "locmap.h"
 #include "uresimp.h"
 
-static UBool isCurrencyPreEuro(const char* currencyKey){
+/*--------------------------------------------------------------------
+  Time bomb - allows temporary behavior that expires at a given
+              release
+ ---------------------------------------------------------------------*/
+static const UVersionInfo ICU_37 = {3,7,0,0};
+
+/*
+returns a new UnicodeSet that is a flattened form of the original
+UnicodeSet.
+*/
+static USet*
+createFlattenSet(USet *origSet, UErrorCode *status) {
+
+
+    USet *newSet = NULL;
+    int32_t origItemCount = 0;
+    int32_t idx, graphmeSize;
+    UChar32 start, end;
+    UChar graphme[64];
+    if (U_FAILURE(*status)) {
+        log_err("createFlattenSet called with %s\n", u_errorName(*status));
+        return NULL;
+    }
+    newSet = uset_open(1, 0);
+    origItemCount = uset_getItemCount(origSet);
+    for (idx = 0; idx < origItemCount; idx++) {
+        graphmeSize = uset_getItem(origSet, idx,
+            &start, &end, 
+            graphme, (int32_t)(sizeof(graphme)/sizeof(graphme[0])),
+            status);
+        if (U_FAILURE(*status)) {
+            log_err("ERROR: uset_getItem returned %s\n", u_errorName(*status));
+            *status = U_ZERO_ERROR;
+        }
+        if (graphmeSize) {
+            uset_addAllCodePoints(newSet, graphme, graphmeSize);
+        }
+        else {
+            uset_addRange(newSet, start, end);
+        }
+    }
+    return newSet;
+}
+static UBool 
+isCurrencyPreEuro(const char* currencyKey){
     if( strcmp(currencyKey, "PTE") == 0 ||
         strcmp(currencyKey, "ESP") == 0 ||
         strcmp(currencyKey, "LUF") == 0 ||
         strcmp(currencyKey, "GRD") == 0 ||
         strcmp(currencyKey, "BEF") == 0 ||
-        strcmp(currencyKey, "ITL") == 0 ){
+        strcmp(currencyKey, "ITL") == 0 ||
+        strcmp(currencyKey, "EEK") == 0){
             return TRUE;
     }
     return FALSE;
@@ -115,26 +161,8 @@ TestKeyInRootRecursive(UResourceBundle *root, const char *rootName,
             if (U_SUCCESS(errorCode)
                 && (ures_getType(subSubBundle) == URES_ARRAY || ures_getType(subSubRootBundle) == URES_ARRAY))
             {
-                /* TODO: Properly check for 2D arrays and zoneStrings */
-                if (subBundleKey != NULL && strcmp(subBundleKey, "zoneStrings") == 0) {
-/*                    int32_t minSize = ures_getSize(subBundle);
-                    int32_t idx;
-
-                    for (idx = 0; idx < minSize; idx++) {
-                        UResourceBundle *subSubBundleAtIndex = ures_getByIndex(subBundle, idx, NULL, &errorCode);
-                        if (ures_getSize(subSubBundleAtIndex) != 6) {
-                            log_err("zoneStrings at index %d has wrong size for locale \"%s\". array size=%d\n",
-                                    idx,
-                                    locale,
-                                    ures_getSize(subSubBundleAtIndex));
-                        }
-                        ures_close(subSubBundleAtIndex);
-                    }*/
-                }
-                else {
-                    /* Here is one of the recursive parts */
-                    TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale);
-                }
+                /* Here is one of the recursive parts */
+                TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale);
             }
             else {
                 int32_t minSize = ures_getSize(subRootBundle);
@@ -281,7 +309,8 @@ TestKeyInRootRecursive(UResourceBundle *root, const char *rootName,
                         subBundleKey,
                         ures_getKey(currentBundle),
                         locale);
-            } else if (strcmp(subBundleKey, "localPatternChars") == 0) {
+            } else if (strcmp(subBundleKey, "localPatternChars") == 0 &&
+                       isICUVersionAtLeast(ICU_37)) {
                 /* Check well-formedness of localPatternChars.  First, the
                  * length must match the number of fields defined by
                  * DateFormat.  Second, each character in the string must
@@ -346,8 +375,13 @@ TestKeyInRootRecursive(UResourceBundle *root, const char *rootName,
 #endif
         }
         else if (ures_getType(subBundle) == URES_TABLE) {
-            /* Here is one of the recursive parts */
-            TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale);
+            if (strcmp(subBundleKey, "availableFormats")!=0) {
+                /* Here is one of the recursive parts */
+                TestKeyInRootRecursive(subRootBundle, rootName, subBundle, locale);
+            }
+            else {
+                log_verbose("Skipping key %s in %s\n", subBundleKey, locale);
+            }
         }
         else if (ures_getType(subBundle) == URES_BINARY || ures_getType(subBundle) == URES_INT) {
             /* Can't do anything to check it */
@@ -418,7 +452,7 @@ TestLocaleStructure(void) {
     int32_t locCount = uloc_countAvailable();
     int32_t locIndex;
     UErrorCode errorCode = U_ZERO_ERROR;
-    const char *currLoc;
+    const char *currLoc, *resolvedLoc;
 
     /* TODO: Compare against parent's data too. This code can't handle fallbacks that some tools do already. */
 /*    char locName[ULOC_FULLNAME_CAPACITY];
@@ -477,6 +511,13 @@ TestLocaleStructure(void) {
             log_verbose("WARNING: The locale %s is experimental! It shouldn't be listed as an installed locale.\n",
                 currLoc);
         }
+        resolvedLoc = ures_getLocaleByType(currentLocale, ULOC_ACTUAL_LOCALE, &errorCode);
+        if (strcmp(resolvedLoc, currLoc) != 0) {
+            /* All locales have at least a Version resource.
+               If it's absolutely empty, then the previous test will fail too.*/
+            log_err("Locale resolves to different locale. Is %s an alias of %s?\n",
+                currLoc, resolvedLoc);
+        }
         TestKeyInRootRecursive(root, "root", currentLocale, currLoc);
 
         testLCID(currentLocale, currLoc);
@@ -728,17 +769,21 @@ findStringSetMismatch(const char *currLoc, const UChar *string, int32_t langSize
                       const UChar *exemplarCharacters, int32_t exemplarLen,
                       UBool ignoreNumbers) {
     UErrorCode errorCode = U_ZERO_ERROR;
-    USet *exemplarSet = uset_openPatternOptions(exemplarCharacters, exemplarLen, USET_CASE_INSENSITIVE, &errorCode);
+    USet *origSet = uset_openPatternOptions(exemplarCharacters, exemplarLen, USET_CASE_INSENSITIVE, &errorCode);
+    USet *exemplarSet = createFlattenSet(origSet, &errorCode);
     int32_t strIdx;
+    uset_close(origSet);
     if (U_FAILURE(errorCode)) {
-      log_err("%s: error uset_openPattern returned %s\n", currLoc, u_errorName(errorCode));
+        log_err("%s: error uset_openPattern returned %s\n", currLoc, u_errorName(errorCode));
         return -1;
     }
 
     for (strIdx = 0; strIdx < langSize; strIdx++) {
         if (!uset_contains(exemplarSet, string[strIdx])
-            && string[strIdx] != 0x0020 && string[strIdx] != 0x00A0 && string[strIdx] != 0x002e && string[strIdx] != 0x002c && string[strIdx] != 0x002d && string[strIdx] != 0x0027) {
+            && string[strIdx] != 0x0020 && string[strIdx] != 0x00A0 && string[strIdx] != 0x002e && string[strIdx] != 0x002c && string[strIdx] != 0x002d && string[strIdx] != 0x0027
+            && string[strIdx] != 0x200C && string[strIdx] != 0x200D) {
             if (!ignoreNumbers || (ignoreNumbers && (string[strIdx] < 0x30 || string[strIdx] > 0x39))) {
+                uset_close(exemplarSet);
                 return strIdx;
             }
         }
@@ -746,7 +791,19 @@ findStringSetMismatch(const char *currLoc, const UChar *string, int32_t langSize
     uset_close(exemplarSet);
     return -1;
 }
-
+/* include non-invariant chars */
+static int32_t
+myUCharsToChars(const UChar* us, char* cs, int32_t len){
+    int32_t i=0;
+    for(; i< len; i++){
+        if(us[i] < 0x7f){
+            cs[i] = (char)us[i];
+        }else{
+            return -1;
+        }
+    }
+    return i;
+}
 static void 
 findSetMatch( UScriptCode *scriptCodes, int32_t scriptsLen, 
               USet *exemplarSet,
@@ -788,7 +845,7 @@ findSetMatch( UScriptCode *scriptCodes, int32_t scriptsLen,
             UChar32 end = 0;
             UChar *str = NULL;
             int32_t strCapacity = 0;
-
+            
             strCapacity = uset_getItem(exemplarSet, i, &start, &end, str, strCapacity, &status);
             if(U_SUCCESS(status)){
                 int32_t j;
@@ -800,6 +857,16 @@ findSetMatch( UScriptCode *scriptCodes, int32_t scriptsLen,
                         }
                     }
                     if(existsInScript == FALSE){
+                        for( j = 0; j < scriptsLen; j++){
+                            UChar toPattern[500]={'\0'};
+                            char pat[500]={'\0'};
+                            int32_t len = uset_toPattern(scripts[j], toPattern, 500, TRUE, &status);
+                            len = myUCharsToChars(toPattern, pat, len);
+                            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));
+                            if(len!=-1){
+                                log_err("Pattern: %s\n",pat);
+                            }
+                        }
                         log_err("ExemplarCharacters and LocaleScript containment test failed for locale %s. \n", locale);
                     }
                 }else{
@@ -979,10 +1046,12 @@ static void VerifyTranslation(void) {
 
             /* test that the scripts are a superset of exemplar characters. */
            {
-                USet *exemplarSet =  ulocdata_getExemplarSet(NULL,currLoc, 0, &errorCode);
+                ULocaleData *uld = ulocdata_open(currLoc,&errorCode);
+                USet *exemplarSet =  ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &errorCode);
                 /* test if exemplar characters are part of script code */
                 findSetMatch(scripts, numScripts, exemplarSet, currLoc);
                 uset_close(exemplarSet);
+                ulocdata_close(uld);
             }
 
            /* test that the paperSize API works */
@@ -1026,6 +1095,7 @@ static void TestExemplarSet(void){
     UErrorCode ec = U_ZERO_ERROR;
     UEnumeration* avail;
     USet* exemplarSets[2];
+    USet* unassignedSet;
     UScriptCode code[MAX_SCRIPTS_PER_LOCALE];
     USet* codeSets[MAX_SCRIPTS_PER_LOCALE];
     int32_t codeLen;
@@ -1036,7 +1106,9 @@ static void TestExemplarSet(void){
     int32_t strLen;
     UChar32 start, end;
     
-    exemplarSets[0] = exemplarSets[1] = NULL;
+    unassignedSet = NULL;
+    exemplarSets[0] = NULL;
+    exemplarSets[1] = NULL;
     for (i=0; i<MAX_SCRIPTS_PER_LOCALE; ++i) {
         codeSets[i] = NULL;
     }
@@ -1046,17 +1118,26 @@ static void TestExemplarSet(void){
     n = uenum_count(avail, &ec);
     if (!assertSuccess("uenum_count", &ec)) goto END;
 
+    u_uastrcpy(ubuf, "[:unassigned:]");
+    unassignedSet = uset_openPattern(ubuf, -1, &ec);
+    if (!assertSuccess("uset_openPattern", &ec)) goto END;
+
     for(i=0; i<n; i++){
         const char* locale = uenum_next(avail, NULL, &ec);
         if (!assertSuccess("uenum_next", &ec)) goto END;
         log_verbose("%s\n", locale);
         for (k=0; k<2; ++k) {
             uint32_t option = (k==0) ? 0 : USET_CASE_INSENSITIVE;
-            USet* exemplarSet = ulocdata_getExemplarSet(NULL, locale, option, &ec);
+            ULocaleData *uld = ulocdata_open(locale,&ec); 
+            USet* exemplarSet = ulocdata_getExemplarSet(uld,NULL, option, ULOCDATA_ES_STANDARD, &ec);
             uset_close(exemplarSets[k]);
+            ulocdata_close(uld);
             exemplarSets[k] = exemplarSet;
             if (!assertSuccess("ulocaledata_getExemplarSet", &ec)) goto END;
 
+            if (uset_containsSome(exemplarSet, unassignedSet)) {
+                log_err("ExemplarSet contains unassigned characters for locale : %s\n", locale);
+            }
             codeLen = uscript_getCode(locale, code, 8, &ec);
             if (!assertSuccess("uscript_getCode", &ec)) goto END;
 
@@ -1066,6 +1147,10 @@ static void TestExemplarSet(void){
             }
             for (j=0; j<codeLen; ++j) {
                 uprv_strcpy(cbuf, "[:");
+                if(code[j]==-1){
+                    log_err("USCRIPT_INVALID_CODE returned for locale: %s\n", locale);
+                    continue;
+                }
                 uprv_strcat(cbuf, uscript_getShortName(code[j]));
                 uprv_strcat(cbuf, ":]");
                 u_uastrcpy(ubuf, cbuf);
@@ -1082,14 +1167,14 @@ static void TestExemplarSet(void){
                 if (!assertSuccess("uset_getItem", &ec)) goto END;
                 if (strLen == 0) {
                     for (j=0; j<codeLen; ++j) {
-                        if (uset_containsRange(codeSets[j], start, end)) {
+                        if (codeSets[j]!=NULL && uset_containsRange(codeSets[j], start, end)) {
                             existsInScript = TRUE;
                             break;
                         }
                     }
                 } else {
                     for (j=0; j<codeLen; ++j) {
-                        if (uset_containsString(codeSets[j], ubuf, strLen)) {
+                        if (codeSets[j]!=NULL && uset_containsString(codeSets[j], ubuf, strLen)) {
                             existsInScript = TRUE;
                             break;
                         }
@@ -1098,7 +1183,7 @@ static void TestExemplarSet(void){
             }
 
             if (existsInScript == FALSE){
-                log_err("ExemplarSet containment failed for locale : %s", locale);
+                log_err("ExemplarSet containment failed for locale : %s\n", locale);
             }
         }
         assertTrue("case-folded is a superset",
@@ -1116,11 +1201,80 @@ static void TestExemplarSet(void){
     uenum_close(avail);
     uset_close(exemplarSets[0]);
     uset_close(exemplarSets[1]);
+    uset_close(unassignedSet);
     for (i=0; i<MAX_SCRIPTS_PER_LOCALE; ++i) {
         uset_close(codeSets[i]);
     }
 }
 
+static void TestCoverage(void){
+    ULocaleDataDelimiterType types[] = {
+     ULOCDATA_QUOTATION_START,     /* Quotation start */
+     ULOCDATA_QUOTATION_END,       /* Quotation end */
+     ULOCDATA_ALT_QUOTATION_START, /* Alternate quotation start */
+     ULOCDATA_ALT_QUOTATION_END,   /* Alternate quotation end */
+     ULOCDATA_DELIMITER_COUNT
+    };
+    int i;
+    UBool sub;
+    UErrorCode status = U_ZERO_ERROR;
+    ULocaleData *uld = ulocdata_open(uloc_getDefault(), &status);
+
+    if(U_FAILURE(status)){
+        log_err("ulocdata_open error");
+        return;
+    }
+
+
+    for(i = 0; i < ULOCDATA_DELIMITER_COUNT; i++){
+        UChar result[32] = {0,};
+        status = U_ZERO_ERROR;
+        ulocdata_getDelimiter(uld, types[i], result, 32, &status);
+        if (U_FAILURE(status)){
+            log_err("ulocdata_getgetDelimiter error with type %d", types[i]);
+        }
+    }
+
+    sub = ulocdata_getNoSubstitute(uld);
+    ulocdata_setNoSubstitute(uld,sub);
+    ulocdata_close(uld);
+}
+
+static void TestCurrencyList(void){
+#if !UCONFIG_NO_FORMATTING
+    UErrorCode errorCode = U_ZERO_ERROR;
+    int32_t structLocaleCount, currencyCount;
+    UEnumeration *en = ucurr_openISOCurrencies(UCURR_ALL, &errorCode);
+    const char *isoCode, *structISOCode;
+    UResourceBundle *subBundle;
+    UResourceBundle *currencies = ures_openDirect(loadTestData(&errorCode), "structLocale", &errorCode);
+    if(U_FAILURE(errorCode)) {
+        log_data_err("Can't open structLocale\n");
+        return;
+    }
+    currencies = ures_getByKey(currencies, "Currencies", currencies, &errorCode);
+    currencyCount = uenum_count(en, &errorCode);
+    structLocaleCount = ures_getSize(currencies);
+    if (currencyCount != structLocaleCount) {
+        log_err("structLocale(%d) and ISO4217(%d) currency list are out of sync.\n", structLocaleCount, currencyCount);
+#if U_CHARSET_FAMILY == U_ASCII_FAMILY
+        ures_resetIterator(currencies);
+        while ((isoCode = uenum_next(en, NULL, &errorCode)) != NULL && ures_hasNext(currencies)) {
+            subBundle = ures_getNextResource(currencies, NULL, &errorCode);
+            structISOCode = ures_getKey(subBundle);
+            ures_close(subBundle);
+            if (strcmp(structISOCode, isoCode) != 0) {
+                log_err("First difference found at structLocale(%s) and ISO4217(%s).\n", structISOCode, isoCode);
+                break;
+            }
+        }
+#endif
+    }
+    ures_close(currencies);
+    uenum_close(en);
+#endif
+}
+
 #define TESTCASE(name) addTest(root, &name, "tsutil/cldrtest/" #name)
 
 void addCLDRTest(TestNode** root);
@@ -1131,4 +1285,6 @@ void addCLDRTest(TestNode** root)
     TESTCASE(TestConsistentCountryInfo);
     TESTCASE(VerifyTranslation);
     TESTCASE(TestExemplarSet);
+    TESTCASE(TestCurrencyList);
+    TESTCASE(TestCoverage);
 }