/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2012, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /***************************************************************************** * * File CLOCTST.C * * Modification History: * Name Description * Madhu Katragadda Ported for C API ****************************************************************************** */ #include "cloctst.h" #include #include #include #include "cintltst.h" #include "cstring.h" #include "uparse.h" #include "uresimp.h" #include "unicode/putil.h" #include "unicode/ubrk.h" #include "unicode/uchar.h" #include "unicode/ucol.h" #include "unicode/udat.h" #include "unicode/uloc.h" #include "unicode/umsg.h" #include "unicode/ures.h" #include "unicode/uset.h" #include "unicode/ustring.h" #include "unicode/utypes.h" #include "unicode/ulocdata.h" #include "unicode/parseerr.h" /* may not be included with some uconfig switches */ #include "udbgutil.h" #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) static void TestNullDefault(void); static void TestNonexistentLanguageExemplars(void); static void TestLocDataErrorCodeChaining(void); static void TestLanguageExemplarsFallbacks(void); static void TestUnicodeDefines(void); void PrintDataTable(); /*--------------------------------------------------- table of valid data --------------------------------------------------- */ #define LOCALE_SIZE 9 #define LOCALE_INFO_SIZE 28 static const char* const rawData2[LOCALE_INFO_SIZE][LOCALE_SIZE] = { /* language code */ { "en", "fr", "ca", "el", "no", "zh", "de", "es", "ja" }, /* script code */ { "", "", "", "", "", "", "", "", "" }, /* country code */ { "US", "FR", "ES", "GR", "NO", "CN", "DE", "", "JP" }, /* variant code */ { "", "", "", "", "NY", "", "", "", "" }, /* full name */ { "en_US", "fr_FR", "ca_ES", "el_GR", "no_NO_NY", "zh_Hans_CN", "de_DE@collation=phonebook", "es@collation=traditional", "ja_JP@calendar=japanese" }, /* ISO-3 language */ { "eng", "fra", "cat", "ell", "nor", "zho", "deu", "spa", "jpn" }, /* ISO-3 country */ { "USA", "FRA", "ESP", "GRC", "NOR", "CHN", "DEU", "", "JPN" }, /* LCID */ { "409", "40c", "403", "408", "814", "804", "10407", "40a", "411" }, /* display language (English) */ { "English", "French", "Catalan", "Greek", "Norwegian", "Chinese", "German", "Spanish", "Japanese" }, /* display script code (English) */ { "", "", "", "", "", "Simplified Han", "", "", "" }, /* display country (English) */ { "United States", "France", "Spain", "Greece", "Norway", "China", "Germany", "", "Japan" }, /* display variant (English) */ { "", "", "", "", "NY", "", "", "", "" }, /* display name (English) */ { "English (United States)", "French (France)", "Catalan (Spain)", "Greek (Greece)", "Norwegian (Norway, NY)", "Chinese (Simplified, China)", "German (Germany, Sort Order=Phonebook Sort Order)", "Spanish (Sort Order=Traditional Sort Order)", "Japanese (Japan, Calendar=Japanese Calendar)" }, /* display language (French) */ { "anglais", "fran\\u00E7ais", "catalan", "grec", "norv\\u00E9gien", "chinois", "allemand", "espagnol", "japonais" }, /* display script code (French) */ { "", "", "", "", "", "chinois simplifi\\u00e9", "", "", "" }, /* display country (French) */ { "\\u00C9tats-Unis", "France", "Espagne", "Gr\\u00E8ce", "Norv\\u00E8ge", "Chine", "Allemagne", "", "Japon" }, /* display variant (French) */ { "", "", "", "", "NY", "", "", "", "" }, /* display name (French) */ { "anglais (\\u00C9tats-Unis)", "fran\\u00E7ais (France)", "catalan (Espagne)", "grec (Gr\\u00E8ce)", "norv\\u00E9gien (Norv\\u00E8ge, NY)", "chinois (id\\u00e9ogrammes han simplifi\\u00e9s, Chine)", "allemand (Allemagne, Ordonnancement=Ordre de l\\u2019annuaire)", "espagnol (Ordonnancement=Ordre traditionnel)", "japonais (Japon, Calendrier=Calendrier japonais)" }, /* display language (Catalan) */ { "angl\\u00E8s", "franc\\u00E8s", "catal\\u00E0", "grec", "noruec", "xin\\u00E8s", "alemany", "espanyol", "japon\\u00E8s" }, /* display script code (Catalan) */ { "", "", "", "", "", "xin\\u00E8s simplificat", "", "", "" }, /* display country (Catalan) */ { "Estats Units", "Fran\\u00E7a", "Espanya", "Gr\\u00E8cia", "Noruega", "Xina", "Alemanya", "", "Jap\\u00F3" }, /* display variant (Catalan) */ { "", "", "", "", "NY", "", "", "", "" }, /* display name (Catalan) */ { "angl\\u00E8s (Estats Units)", "franc\\u00E8s (Fran\\u00E7a)", "catal\\u00E0 (Espanya)", "grec (Gr\\u00E8cia)", "noruec (Noruega, NY)", "xin\\u00E8s (simplificat, Xina)", "alemany (Alemanya, ordre alfab\\u00e8tic=ordre de la guia telef\\u00F2nica)", "espanyol (ordre alfab\\u00e8tic=ordre tradicional)", "japon\\u00E8s (Jap\\u00F3, calendari=calendari japon\\u00e8s)" }, /* display language (Greek) */ { "\\u0391\\u03b3\\u03b3\\u03bb\\u03b9\\u03ba\\u03ac", "\\u0393\\u03b1\\u03bb\\u03bb\\u03b9\\u03ba\\u03ac", "\\u039a\\u03b1\\u03c4\\u03b1\\u03bb\\u03b1\\u03bd\\u03b9\\u03ba\\u03ac", "\\u0395\\u03bb\\u03bb\\u03b7\\u03bd\\u03b9\\u03ba\\u03ac", "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03b9\\u03ba\\u03ac", "\\u039A\\u03B9\\u03BD\\u03B5\\u03B6\\u03B9\\u03BA\\u03AC", "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC", "\\u0399\\u03C3\\u03C0\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC", "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03B9\\u03BA\\u03AC" }, /* display script code (Greek) */ { "", "", "", "", "", "\\u0391\\u03c0\\u03bb\\u03bf\\u03c0\\u03bf\\u03b9\\u03b7\\u03bc\\u03ad\\u03bd\\u03b1 \\u03a7\\u03b1\\u03bd", "", "", "" }, /* display country (Greek) */ { "\\u0397\\u03BD\\u03C9\\u03BC\\u03AD\\u03BD\\u03B5\\u03C2 \\u03A0\\u03BF\\u03BB\\u03B9\\u03C4\\u03B5\\u03AF\\u03B5\\u03C2 \\u03C4\\u03B7\\u03C2 \\u0391\\u03BC\\u03B5\\u03C1\\u03B9\\u03BA\\u03AE\\u03C2", "\\u0393\\u03b1\\u03bb\\u03bb\\u03af\\u03b1", "\\u0399\\u03c3\\u03c0\\u03b1\\u03bd\\u03af\\u03b1", "\\u0395\\u03bb\\u03bb\\u03ac\\u03b4\\u03b1", "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03af\\u03b1", "\\u039A\\u03AF\\u03BD\\u03B1", "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03AF\\u03B1", "", "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03AF\\u03B1" }, /* display variant (Greek) */ { "", "", "", "", "NY", "", "", "", "" }, /* TODO: currently there is no translation for NY in Greek fix this test when we have it */ /* display name (Greek) */ { "\\u0391\\u03b3\\u03b3\\u03bb\\u03b9\\u03ba\\u03ac (\\u0397\\u03BD\\u03C9\\u03BC\\u03AD\\u03BD\\u03B5\\u03C2 \\u03A0\\u03BF\\u03BB\\u03B9\\u03C4\\u03B5\\u03AF\\u03B5\\u03C2 \\u03C4\\u03B7\\u03C2 \\u0391\\u03BC\\u03B5\\u03C1\\u03B9\\u03BA\\u03AE\\u03C2)", "\\u0393\\u03b1\\u03bb\\u03bb\\u03b9\\u03ba\\u03ac (\\u0393\\u03b1\\u03bb\\u03bb\\u03af\\u03b1)", "\\u039a\\u03b1\\u03c4\\u03b1\\u03bb\\u03b1\\u03bd\\u03b9\\u03ba\\u03ac (\\u0399\\u03c3\\u03c0\\u03b1\\u03bd\\u03af\\u03b1)", "\\u0395\\u03bb\\u03bb\\u03b7\\u03bd\\u03b9\\u03ba\\u03ac (\\u0395\\u03bb\\u03bb\\u03ac\\u03b4\\u03b1)", "\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03b9\\u03ba\\u03ac (\\u039d\\u03bf\\u03c1\\u03b2\\u03b7\\u03b3\\u03af\\u03b1, NY)", "\\u039A\\u03B9\\u03BD\\u03B5\\u03B6\\u03B9\\u03BA\\u03AC (\\u0391\\u03c0\\u03bb\\u03bf\\u03c0\\u03bf\\u03b9\\u03b7\\u03bc\\u03ad\\u03bd\\u03bf \\u039a\\u03b9\\u03bd\\u03b5\\u03b6\\u03b9\\u03ba\\u03cc, \\u039A\\u03AF\\u03BD\\u03B1)", "\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC (\\u0393\\u03B5\\u03C1\\u03BC\\u03B1\\u03BD\\u03AF\\u03B1, \\u03C4\\u03B1\\u03BA\\u03C4\\u03BF\\u03C0\\u03BF\\u03AF\\u03B7\\u03C3\\u03B7=\\u03A3\\u03B5\\u03B9\\u03C1\\u03AC \\u03C4\\u03B1\\u03BE\\u03B9\\u03BD\\u03CC\\u03BC\\u03B7\\u03C3\\u03B7\\u03C2 \\u03C4\\u03B7\\u03BB\\u03B5\\u03C6\\u03C9\\u03BD\\u03B9\\u03BA\\u03BF\\u03CD \\u03BA\\u03B1\\u03C4\\u03B1\\u03BB\\u03CC\\u03B3\\u03BF\\u03C5)", "\\u0399\\u03C3\\u03C0\\u03B1\\u03BD\\u03B9\\u03BA\\u03AC (\\u03C4\\u03B1\\u03BA\\u03C4\\u03BF\\u03C0\\u03BF\\u03AF\\u03B7\\u03C3\\u03B7=\\u03A0\\u03B1\\u03C1\\u03B1\\u03B4\\u03BF\\u03C3\\u03B9\\u03B1\\u03BA\\u03AE \\u03C3\\u03B5\\u03B9\\u03C1\\u03AC \\u03C4\\u03B1\\u03BE\\u03B9\\u03BD\\u03CC\\u03BC\\u03B7\\u03C3\\u03B7\\u03C2)", "\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03B9\\u03BA\\u03AC (\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03AF\\u03B1, \\u03B7\\u03BC\\u03B5\\u03C1\\u03BF\\u03BB\\u03CC\\u03B3\\u03B9\\u03BF=\\u0399\\u03B1\\u03C0\\u03C9\\u03BD\\u03B9\\u03BA\\u03CC \\u03B7\\u03BC\\u03B5\\u03C1\\u03BF\\u03BB\\u03CC\\u03B3\\u03B9\\u03BF)" } }; static UChar*** dataTable=0; enum { ENGLISH = 0, FRENCH = 1, CATALAN = 2, GREEK = 3, NORWEGIAN = 4 }; enum { LANG = 0, SCRIPT = 1, CTRY = 2, VAR = 3, NAME = 4, LANG3 = 5, CTRY3 = 6, LCID = 7, DLANG_EN = 8, DSCRIPT_EN = 9, DCTRY_EN = 10, DVAR_EN = 11, DNAME_EN = 12, DLANG_FR = 13, DSCRIPT_FR = 14, DCTRY_FR = 15, DVAR_FR = 16, DNAME_FR = 17, DLANG_CA = 18, DSCRIPT_CA = 19, DCTRY_CA = 20, DVAR_CA = 21, DNAME_CA = 22, DLANG_EL = 23, DSCRIPT_EL = 24, DCTRY_EL = 25, DVAR_EL = 26, DNAME_EL = 27 }; #define TESTCASE(name) addTest(root, &name, "tsutil/cloctst/" #name) void addLocaleTest(TestNode** root); void addLocaleTest(TestNode** root) { TESTCASE(TestObsoleteNames); /* srl- move */ TESTCASE(TestBasicGetters); TESTCASE(TestNullDefault); TESTCASE(TestPrefixes); TESTCASE(TestSimpleResourceInfo); TESTCASE(TestDisplayNames); TESTCASE(TestGetAvailableLocales); TESTCASE(TestDataDirectory); #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION TESTCASE(TestISOFunctions); #endif TESTCASE(TestISO3Fallback); TESTCASE(TestUninstalledISO3Names); TESTCASE(TestSimpleDisplayNames); TESTCASE(TestVariantParsing); TESTCASE(TestKeywordVariants); TESTCASE(TestKeywordVariantParsing); TESTCASE(TestCanonicalization); TESTCASE(TestKeywordSet); TESTCASE(TestKeywordSetError); TESTCASE(TestDisplayKeywords); TESTCASE(TestDisplayKeywordValues); TESTCASE(TestGetBaseName); #if !UCONFIG_NO_FILE_IO TESTCASE(TestGetLocale); #endif TESTCASE(TestDisplayNameWarning); TESTCASE(TestNonexistentLanguageExemplars); TESTCASE(TestLocDataErrorCodeChaining); TESTCASE(TestLanguageExemplarsFallbacks); TESTCASE(TestCalendar); TESTCASE(TestDateFormat); TESTCASE(TestCollation); TESTCASE(TestULocale); TESTCASE(TestUResourceBundle); TESTCASE(TestDisplayName); TESTCASE(TestAcceptLanguage); TESTCASE(TestGetLocaleForLCID); TESTCASE(TestOrientation); TESTCASE(TestLikelySubtags); TESTCASE(TestToLanguageTag); TESTCASE(TestForLanguageTag); TESTCASE(TestTrailingNull); TESTCASE(TestUnicodeDefines); } /* testing uloc(), uloc_getName(), uloc_getLanguage(), uloc_getVariant(), uloc_getCountry() */ static void TestBasicGetters() { int32_t i; int32_t cap; UErrorCode status = U_ZERO_ERROR; char *testLocale = 0; char *temp = 0, *name = 0; log_verbose("Testing Basic Getters\n"); for (i = 0; i < LOCALE_SIZE; i++) { testLocale=(char*)malloc(sizeof(char) * (strlen(rawData2[NAME][i])+1)); strcpy(testLocale,rawData2[NAME][i]); log_verbose("Testing %s .....\n", testLocale); cap=uloc_getLanguage(testLocale, NULL, 0, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; temp=(char*)malloc(sizeof(char) * (cap+1)); uloc_getLanguage(testLocale, temp, cap+1, &status); } if(U_FAILURE(status)){ log_err("ERROR: in uloc_getLanguage %s\n", myErrorName(status)); } if (0 !=strcmp(temp,rawData2[LANG][i])) { log_err(" Language code mismatch: %s versus %s\n", temp, rawData2[LANG][i]); } cap=uloc_getCountry(testLocale, temp, cap, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; temp=(char*)realloc(temp, sizeof(char) * (cap+1)); uloc_getCountry(testLocale, temp, cap+1, &status); } if(U_FAILURE(status)){ log_err("ERROR: in uloc_getCountry %s\n", myErrorName(status)); } if (0 != strcmp(temp, rawData2[CTRY][i])) { log_err(" Country code mismatch: %s versus %s\n", temp, rawData2[CTRY][i]); } cap=uloc_getVariant(testLocale, temp, cap, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; temp=(char*)realloc(temp, sizeof(char) * (cap+1)); uloc_getVariant(testLocale, temp, cap+1, &status); } if(U_FAILURE(status)){ log_err("ERROR: in uloc_getVariant %s\n", myErrorName(status)); } if (0 != strcmp(temp, rawData2[VAR][i])) { log_err("Variant code mismatch: %s versus %s\n", temp, rawData2[VAR][i]); } cap=uloc_getName(testLocale, NULL, 0, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; name=(char*)malloc(sizeof(char) * (cap+1)); uloc_getName(testLocale, name, cap+1, &status); } else if(status==U_ZERO_ERROR) { log_err("ERROR: in uloc_getName(%s,NULL,0,..), expected U_BUFFER_OVERFLOW_ERROR!\n", testLocale); } if(U_FAILURE(status)){ log_err("ERROR: in uloc_getName %s\n", myErrorName(status)); } if (0 != strcmp(name, rawData2[NAME][i])){ log_err(" Mismatch in getName: %s versus %s\n", name, rawData2[NAME][i]); } free(temp); free(name); free(testLocale); } } static void TestNullDefault() { UErrorCode status = U_ZERO_ERROR; char original[ULOC_FULLNAME_CAPACITY]; uprv_strcpy(original, uloc_getDefault()); uloc_setDefault("qq_BLA", &status); if (uprv_strcmp(uloc_getDefault(), "qq_BLA") != 0) { log_err(" Mismatch in uloc_setDefault: qq_BLA versus %s\n", uloc_getDefault()); } uloc_setDefault(NULL, &status); if (uprv_strcmp(uloc_getDefault(), original) != 0) { log_err(" uloc_setDefault(NULL, &status) didn't get the default locale back!\n"); } { /* Test that set & get of default locale work, and that * default locales are cached and reused, and not overwritten. */ const char *n_en_US; const char *n_fr_FR; const char *n2_en_US; status = U_ZERO_ERROR; uloc_setDefault("en_US", &status); n_en_US = uloc_getDefault(); if (strcmp(n_en_US, "en_US") != 0) { log_err("Wrong result from uloc_getDefault(). Expected \"en_US\", got \"%s\"\n", n_en_US); } uloc_setDefault("fr_FR", &status); n_fr_FR = uloc_getDefault(); if (strcmp(n_en_US, "en_US") != 0) { log_err("uloc_setDefault altered previously default string." "Expected \"en_US\", got \"%s\"\n", n_en_US); } if (strcmp(n_fr_FR, "fr_FR") != 0) { log_err("Wrong result from uloc_getDefault(). Expected \"fr_FR\", got %s\n", n_fr_FR); } uloc_setDefault("en_US", &status); n2_en_US = uloc_getDefault(); if (strcmp(n2_en_US, "en_US") != 0) { log_err("Wrong result from uloc_getDefault(). Expected \"en_US\", got \"%s\"\n", n_en_US); } if (n2_en_US != n_en_US) { log_err("Default locale cache failed to reuse en_US locale.\n"); } if (U_FAILURE(status)) { log_err("Failure returned from uloc_setDefault - \"%s\"\n", u_errorName(status)); } } } /* Test the i- and x- and @ and . functionality */ #define PREFIXBUFSIZ 128 static void TestPrefixes() { int row = 0; int n; const char *loc, *expected; static const char * const testData[][7] = { /* NULL canonicalize() column means "expect same as getName()" */ {"sv", "", "FI", "AL", "sv-fi-al", "sv_FI_AL", NULL}, {"en", "", "GB", "", "en-gb", "en_GB", NULL}, {"i-hakka", "", "MT", "XEMXIJA", "i-hakka_MT_XEMXIJA", "i-hakka_MT_XEMXIJA", NULL}, {"i-hakka", "", "CN", "", "i-hakka_CN", "i-hakka_CN", NULL}, {"i-hakka", "", "MX", "", "I-hakka_MX", "i-hakka_MX", NULL}, {"x-klingon", "", "US", "SANJOSE", "X-KLINGON_us_SANJOSE", "x-klingon_US_SANJOSE", NULL}, {"zh", "Hans", "", "PINYIN", "zh-Hans-pinyin", "zh_Hans_PINYIN", "zh_Hans@collation=pinyin"}, {"hy", "", "", "AREVMDA", "hy_AREVMDA", "hy_AREVMDA", NULL}, {"de", "", "", "1901", "de-1901", "de_1901", NULL}, {"mr", "", "", "", "mr.utf8", "mr.utf8", "mr"}, {"de", "", "TV", "", "de-tv.koi8r", "de_TV.koi8r", "de_TV"}, {"x-piglatin", "", "ML", "", "x-piglatin_ML.MBE", "x-piglatin_ML.MBE", "x-piglatin_ML"}, /* Multibyte English */ {"i-cherokee", "","US", "", "i-Cherokee_US.utf7", "i-cherokee_US.utf7", "i-cherokee_US"}, {"x-filfli", "", "MT", "FILFLA", "x-filfli_MT_FILFLA.gb-18030", "x-filfli_MT_FILFLA.gb-18030", "x-filfli_MT_FILFLA"}, {"no", "", "NO", "NY", "no-no-ny.utf32@B", "no_NO_NY.utf32@B", "no_NO_NY_B"}, {"no", "", "NO", "", "no-no.utf32@B", "no_NO.utf32@B", "no_NO_B"}, {"no", "", "", "NY", "no__ny", "no__NY", NULL}, {"no", "", "", "", "no@ny", "no@ny", "no__NY"}, {"el", "Latn", "", "", "el-latn", "el_Latn", NULL}, {"en", "Cyrl", "RU", "", "en-cyrl-ru", "en_Cyrl_RU", NULL}, {"zh", "Hant", "TW", "STROKE", "zh-hant_TW_STROKE", "zh_Hant_TW_STROKE", "zh_Hant_TW@collation=stroke"}, {"qq", "Qqqq", "QQ", "QQ", "qq_Qqqq_QQ_QQ", "qq_Qqqq_QQ_QQ", NULL}, {"qq", "Qqqq", "", "QQ", "qq_Qqqq__QQ", "qq_Qqqq__QQ", NULL}, {"ab", "Cdef", "GH", "IJ", "ab_cdef_gh_ij", "ab_Cdef_GH_IJ", NULL}, /* total garbage */ {NULL,NULL,NULL,NULL,NULL,NULL,NULL} }; static const char * const testTitles[] = { "uloc_getLanguage()", "uloc_getScript()", "uloc_getCountry()", "uloc_getVariant()", "name", "uloc_getName()", "uloc_canonicalize()" }; char buf[PREFIXBUFSIZ]; int32_t len; UErrorCode err; for(row=0;testData[row][0] != NULL;row++) { loc = testData[row][NAME]; log_verbose("Test #%d: %s\n", row, loc); err = U_ZERO_ERROR; len=0; buf[0]=0; for(n=0;n<=(NAME+2);n++) { if(n==NAME) continue; for(len=0;len [%s] (length %d)\n", row, testTitles[n], loc, buf, len); if(len != (int32_t)strlen(buf)) { log_err("#%d: %s on %s: -> [%s] (length returned %d, actual %d!)\n", row, testTitles[n], loc, buf, len, strlen(buf)+1); } /* see if they smashed something */ if(buf[len+1] != '%') { log_err("#%d: %s on %s: -> [%s] - wrote [%X] out ofbounds!\n", row, testTitles[n], loc, buf, buf[len+1]); } expected = testData[row][n]; if (expected == NULL && n == (NAME+2)) { /* NULL expected canonicalize() means "expect same as getName()" */ expected = testData[row][NAME+1]; } if(strcmp(buf, expected)) { log_err("#%d: %s on %s: -> [%s] (expected '%s'!)\n", row, testTitles[n], loc, buf, expected); } } } } } /* testing uloc_getISO3Language(), uloc_getISO3Country(), */ static void TestSimpleResourceInfo() { int32_t i; char* testLocale = 0; UChar* expected = 0; const char* temp; char temp2[20]; testLocale=(char*)malloc(sizeof(char) * 1); expected=(UChar*)malloc(sizeof(UChar) * 1); setUpDataTable(); log_verbose("Testing getISO3Language and getISO3Country\n"); for (i = 0; i < LOCALE_SIZE; i++) { testLocale=(char*)realloc(testLocale, sizeof(char) * (u_strlen(dataTable[NAME][i])+1)); u_austrcpy(testLocale, dataTable[NAME][i]); log_verbose("Testing %s ......\n", testLocale); temp=uloc_getISO3Language(testLocale); expected=(UChar*)realloc(expected, sizeof(UChar) * (strlen(temp) + 1)); u_uastrcpy(expected,temp); if (0 != u_strcmp(expected, dataTable[LANG3][i])) { log_err(" ISO-3 language code mismatch: %s versus %s\n", austrdup(expected), austrdup(dataTable[LANG3][i])); } temp=uloc_getISO3Country(testLocale); expected=(UChar*)realloc(expected, sizeof(UChar) * (strlen(temp) + 1)); u_uastrcpy(expected,temp); if (0 != u_strcmp(expected, dataTable[CTRY3][i])) { log_err(" ISO-3 Country code mismatch: %s versus %s\n", austrdup(expected), austrdup(dataTable[CTRY3][i])); } sprintf(temp2, "%x", (int)uloc_getLCID(testLocale)); if (strcmp(temp2, rawData2[LCID][i]) != 0) { log_err("LCID mismatch: %s versus %s\n", temp2 , rawData2[LCID][i]); } } free(expected); free(testLocale); cleanUpDataTable(); } /* if len < 0, we convert until we hit UChar 0x0000, which is not output. will add trailing null * if there's room but won't be included in result. result < 0 indicates an error. * Returns the number of chars written (not those that would be written if there's enough room.*/ static int32_t UCharsToEscapedAscii(const UChar* utext, int32_t len, char* resultChars, int32_t buflen) { static const struct { char escapedChar; UChar sourceVal; } ESCAPE_MAP[] = { /*a*/ {'a', 0x07}, /*b*/ {'b', 0x08}, /*e*/ {'e', 0x1b}, /*f*/ {'f', 0x0c}, /*n*/ {'n', 0x0a}, /*r*/ {'r', 0x0d}, /*t*/ {'t', 0x09}, /*v*/ {'v', 0x0b} }; static const int32_t ESCAPE_MAP_LENGTH = sizeof(ESCAPE_MAP)/sizeof(ESCAPE_MAP[0]); static const char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; int32_t i, j; int32_t resultLen = 0; const int32_t limit = len<0 ? buflen : len; /* buflen is long enough to hit the buffer limit */ const int32_t escapeLimit1 = buflen-2; const int32_t escapeLimit2 = buflen-6; UChar uc; if(utext==NULL || resultChars==NULL || buflen<0) { return -1; } for(i=0;iescapeLimit1) { break; } resultChars[resultLen++]='\\'; resultChars[resultLen++]=ESCAPE_MAP[j].escapedChar; continue; } } else if(uc<0x7f) { u_austrncpy(resultChars + resultLen, &uc, 1); resultLen++; continue; } if(resultLen>escapeLimit2) { break; } /* have to escape the uchar */ resultChars[resultLen++]='\\'; resultChars[resultLen++]='u'; resultChars[resultLen++]=HEX_DIGITS[(uc>>12)&0xff]; resultChars[resultLen++]=HEX_DIGITS[(uc>>8)&0xff]; resultChars[resultLen++]=HEX_DIGITS[(uc>>4)&0xff]; resultChars[resultLen++]=HEX_DIGITS[uc&0xff]; } if(resultLen %s\n", aLocale, testL[i], u_errorName(errorCode)); } else { expectBuffer = CharsToUChars(expect[i]); if(u_strcmp(buffer,expectBuffer)) { log_data_err("FAIL in uloc_getDisplayName(%s,%s,..) expected '%s' got '%s' (Are you missing data?)\n", aLocale, testL[i], expect[i], austrdup(buffer)); } else { log_verbose("pass in uloc_getDisplayName(%s,%s,..) got '%s'\n", aLocale, testL[i], expect[i]); } free(expectBuffer); } } } /* test that we properly preflight and return data when there's a non-default pattern, see ticket #8262. */ { int32_t i; static const char *locale="az_Cyrl"; static const char *displayLocale="ja"; static const char *expectedChars = "\\u30a2\\u30bc\\u30eb\\u30d0\\u30a4\\u30b8\\u30e3\\u30f3\\u8a9e" "(\\u30ad\\u30ea\\u30eb\\u6587\\u5b57)"; UErrorCode ec=U_ZERO_ERROR; UChar result[256]; int32_t len; int32_t preflightLen=uloc_getDisplayName(locale, displayLocale, NULL, 0, &ec); /* inconvenient semantics when preflighting, this condition is expected... */ if(ec==U_BUFFER_OVERFLOW_ERROR) { ec=U_ZERO_ERROR; } len=uloc_getDisplayName(locale, displayLocale, result, LENGTHOF(result), &ec); if(U_FAILURE(ec)) { log_err("uloc_getDisplayName(%s, %s...) returned error: %s", locale, displayLocale, u_errorName(ec)); } else { UChar *expected=CharsToUChars(expectedChars); int32_t expectedLen=u_strlen(expected); if(len!=expectedLen) { log_data_err("uloc_getDisplayName(%s, %s...) returned string of length %d, expected length %d", locale, displayLocale, len, expectedLen); } else if(preflightLen!=expectedLen) { log_err("uloc_getDisplayName(%s, %s...) returned preflight length %d, expected length %d", locale, displayLocale, preflightLen, expectedLen); } else if(u_strncmp(result, expected, len)) { int32_t cap=len*6+1; /* worst case + space for trailing null */ char* resultChars=(char*)malloc(cap); int32_t resultCharsLen=UCharsToEscapedAscii(result, len, resultChars, cap); if(resultCharsLen<0 || resultCharsLen=0;--i) { len=uloc_getDisplayName(locale, displayLocale, result, i, &ec); if(ec==U_BUFFER_OVERFLOW_ERROR) { ec=U_ZERO_ERROR; } if(U_FAILURE(ec)) { log_err("using buffer of length %d returned error %s", i, u_errorName(ec)); break; } if(len!=expectedLen) { log_err("with buffer of length %d, expected length %d but got %d", i, expectedLen, len); break; } /* There's no guarantee about what's in the buffer if we've overflowed, in particular, * we don't know that it's been filled, so no point in checking. */ } } free(expected); } } } /* test for uloc_getAvialable() and uloc_countAvilable()*/ static void TestGetAvailableLocales() { const char *locList; int32_t locCount,i; log_verbose("Testing the no of avialable locales\n"); locCount=uloc_countAvailable(); if (locCount == 0) log_data_err("countAvailable() returned an empty list!\n"); /* use something sensible w/o hardcoding the count */ else if(locCount < 0){ log_data_err("countAvailable() returned a wrong value!= %d\n", locCount); } else{ log_info("Number of locales returned = %d\n", locCount); } for(i=0;i status %s. Got %s instead\n", testCases[i].localeID, u_errorName(testCases[i].expectedStatus), u_errorName(status)); } status = U_ZERO_ERROR; if(keywords) { if((keyCount = uenum_count(keywords, &status)) != testCases[i].numKeywords) { log_err("Expected to get %i keywords, got %i\n", testCases[i].numKeywords, keyCount); } if(keyCount) { j = 0; while((keyword = uenum_next(keywords, &keywordLen, &status))) { if(strcmp(keyword, testCases[i].expectedKeywords[j]) != 0) { log_err("Expected to get keyword value %s, got %s\n", testCases[i].expectedKeywords[j], keyword); } j++; } j = 0; uenum_reset(keywords, &status); while((keyword = uenum_next(keywords, &keywordLen, &status))) { if(strcmp(keyword, testCases[i].expectedKeywords[j]) != 0) { log_err("Expected to get keyword value %s, got %s\n", testCases[i].expectedKeywords[j], keyword); } j++; } } uenum_close(keywords); } resultLen = uloc_getName(testCases[i].localeID, buffer, 256, &status); if (uprv_strcmp(testCases[i].expectedLocaleID, buffer) != 0) { log_err("Expected uloc_getName(\"%s\") => \"%s\"; got \"%s\"\n", testCases[i].localeID, testCases[i].expectedLocaleID, buffer); } resultLen = uloc_canonicalize(testCases[i].localeID, buffer, 256, &status); if (uprv_strcmp(testCases[i].expectedCanonicalID, buffer) != 0) { log_err("Expected uloc_canonicalize(\"%s\") => \"%s\"; got \"%s\"\n", testCases[i].localeID, testCases[i].expectedCanonicalID, buffer); } } } static void TestKeywordVariantParsing(void) { static const struct { const char *localeID; const char *keyword; const char *expectedValue; } testCases[] = { { "de_DE@ C o ll A t i o n = Phonebook ", "c o ll a t i o n", "Phonebook" }, { "de_DE", "collation", ""}, { "de_DE@collation=PHONEBOOK", "collation", "PHONEBOOK" }, { "de_DE@currency = euro; CoLLaTion = PHONEBOOk", "collatiON", "PHONEBOOk" }, }; UErrorCode status = U_ZERO_ERROR; int32_t i = 0; int32_t resultLen = 0; char buffer[256]; for(i = 0; i < sizeof(testCases)/sizeof(testCases[0]); i++) { *buffer = 0; resultLen = uloc_getKeywordValue(testCases[i].localeID, testCases[i].keyword, buffer, 256, &status); if(uprv_strcmp(testCases[i].expectedValue, buffer) != 0) { log_err("Expected to extract \"%s\" from \"%s\" for keyword \"%s\". Got \"%s\" instead\n", testCases[i].expectedValue, testCases[i].localeID, testCases[i].keyword, buffer); } } } static const struct { const char *l; /* locale */ const char *k; /* kw */ const char *v; /* value */ const char *x; /* expected */ } kwSetTestCases[] = { #if 1 { "en_US", "calendar", "japanese", "en_US@calendar=japanese" }, { "en_US@", "calendar", "japanese", "en_US@calendar=japanese" }, { "en_US@calendar=islamic", "calendar", "japanese", "en_US@calendar=japanese" }, { "en_US@calendar=slovakian", "calendar", "gregorian", "en_US@calendar=gregorian" }, /* don't know what this means, but it has the same # of chars as gregorian */ { "en_US@calendar=gregorian", "calendar", "japanese", "en_US@calendar=japanese" }, { "de", "Currency", "CHF", "de@currency=CHF" }, { "de", "Currency", "CHF", "de@currency=CHF" }, { "en_US@collation=phonebook", "calendar", "japanese", "en_US@calendar=japanese;collation=phonebook" }, { "en_US@calendar=japanese", "collation", "phonebook", "en_US@calendar=japanese;collation=phonebook" }, { "de@collation=phonebook", "Currency", "CHF", "de@collation=phonebook;currency=CHF" }, { "en_US@calendar=gregorian;collation=phonebook", "calendar", "japanese", "en_US@calendar=japanese;collation=phonebook" }, { "en_US@calendar=slovakian;collation=phonebook", "calendar", "gregorian", "en_US@calendar=gregorian;collation=phonebook" }, /* don't know what this means, but it has the same # of chars as gregorian */ { "en_US@calendar=slovakian;collation=videobook", "collation", "phonebook", "en_US@calendar=slovakian;collation=phonebook" }, /* don't know what this means, but it has the same # of chars as gregorian */ { "en_US@calendar=islamic;collation=phonebook", "calendar", "japanese", "en_US@calendar=japanese;collation=phonebook" }, { "de@collation=phonebook", "Currency", "CHF", "de@collation=phonebook;currency=CHF" }, #endif #if 1 { "mt@a=0;b=1;c=2;d=3", "c","j", "mt@a=0;b=1;c=j;d=3" }, { "mt@a=0;b=1;c=2;d=3", "x","j", "mt@a=0;b=1;c=2;d=3;x=j" }, { "mt@a=0;b=1;c=2;d=3", "a","f", "mt@a=f;b=1;c=2;d=3" }, { "mt@a=0;aa=1;aaa=3", "a","x", "mt@a=x;aa=1;aaa=3" }, { "mt@a=0;aa=1;aaa=3", "aa","x", "mt@a=0;aa=x;aaa=3" }, { "mt@a=0;aa=1;aaa=3", "aaa","x", "mt@a=0;aa=1;aaa=x" }, { "mt@a=0;aa=1;aaa=3", "a","yy", "mt@a=yy;aa=1;aaa=3" }, { "mt@a=0;aa=1;aaa=3", "aa","yy", "mt@a=0;aa=yy;aaa=3" }, { "mt@a=0;aa=1;aaa=3", "aaa","yy", "mt@a=0;aa=1;aaa=yy" }, #endif #if 1 /* removal tests */ /* 1. removal of item at end */ { "de@collation=phonebook;currency=CHF", "currency", "", "de@collation=phonebook" }, { "de@collation=phonebook;currency=CHF", "currency", NULL, "de@collation=phonebook" }, /* 2. removal of item at beginning */ { "de@collation=phonebook;currency=CHF", "collation", "", "de@currency=CHF" }, { "de@collation=phonebook;currency=CHF", "collation", NULL, "de@currency=CHF" }, /* 3. removal of an item not there */ { "de@collation=phonebook;currency=CHF", "calendar", NULL, "de@collation=phonebook;currency=CHF" }, /* 4. removal of only item */ { "de@collation=phonebook", "collation", NULL, "de" }, #endif { "de@collation=phonebook", "Currency", "CHF", "de@collation=phonebook;currency=CHF" } }; static void TestKeywordSet(void) { int32_t i = 0; int32_t resultLen = 0; char buffer[1024]; char cbuffer[1024]; for(i = 0; i < sizeof(kwSetTestCases)/sizeof(kwSetTestCases[0]); i++) { UErrorCode status = U_ZERO_ERROR; memset(buffer,'%',1023); strcpy(buffer, kwSetTestCases[i].l); uloc_canonicalize(kwSetTestCases[i].l, cbuffer, 1023, &status); if(strcmp(buffer,cbuffer)) { log_verbose("note: [%d] wasn't canonical, should be: '%s' not '%s'. Won't check for canonicity in output.\n", i, cbuffer, buffer); } /* sanity check test case results for canonicity */ uloc_canonicalize(kwSetTestCases[i].x, cbuffer, 1023, &status); if(strcmp(kwSetTestCases[i].x,cbuffer)) { log_err("%s:%d: ERROR: kwSetTestCases[%d].x = '%s', should be %s (must be canonical)\n", __FILE__, __LINE__, i, kwSetTestCases[i].x, cbuffer); } resultLen = uloc_setKeywordValue(kwSetTestCases[i].k, kwSetTestCases[i].v, buffer, 1023, &status); if(U_FAILURE(status)) { log_err("Err on test case %d: got error %s\n", i, u_errorName(status)); continue; } if(strcmp(buffer,kwSetTestCases[i].x) || ((int32_t)strlen(buffer)!=resultLen)) { log_err("FAIL: #%d: %s + [%s=%s] -> %s (%d) expected %s (%d)\n", i, kwSetTestCases[i].l, kwSetTestCases[i].k, kwSetTestCases[i].v, buffer, resultLen, kwSetTestCases[i].x, strlen(buffer)); } else { log_verbose("pass: #%d: %s + [%s=%s] -> %s\n", i, kwSetTestCases[i].l, kwSetTestCases[i].k, kwSetTestCases[i].v,buffer); } } } static void TestKeywordSetError(void) { char buffer[1024]; UErrorCode status; int32_t res; int32_t i; int32_t blen; /* 0-test whether an error condition modifies the buffer at all */ blen=0; i=0; memset(buffer,'%',1023); status = U_ZERO_ERROR; res = uloc_setKeywordValue(kwSetTestCases[i].k, kwSetTestCases[i].v, buffer, blen, &status); if(status != U_ILLEGAL_ARGUMENT_ERROR) { log_err("expected illegal err got %s\n", u_errorName(status)); return; } /* if(res!=strlen(kwSetTestCases[i].x)) { log_err("expected result %d got %d\n", strlen(kwSetTestCases[i].x), res); return; } */ if(buffer[blen]!='%') { log_err("Buffer byte %d was modified: now %c\n", blen, buffer[blen]); return; } log_verbose("0-buffer modify OK\n"); for(i=0;i<=2;i++) { /* 1- test a short buffer with growing text */ blen=(int32_t)strlen(kwSetTestCases[i].l)+1; memset(buffer,'%',1023); strcpy(buffer,kwSetTestCases[i].l); status = U_ZERO_ERROR; res = uloc_setKeywordValue(kwSetTestCases[i].k, kwSetTestCases[i].v, buffer, blen, &status); if(status != U_BUFFER_OVERFLOW_ERROR) { log_err("expected buffer overflow on buffer %d got %s, len %d (%s + [%s=%s])\n", blen, u_errorName(status), res, kwSetTestCases[i].l, kwSetTestCases[i].k, kwSetTestCases[i].v); return; } if(res!=(int32_t)strlen(kwSetTestCases[i].x)) { log_err("expected result %d got %d\n", strlen(kwSetTestCases[i].x), res); return; } if(buffer[blen]!='%') { log_err("Buffer byte %d was modified: now %c\n", blen, buffer[blen]); return; } log_verbose("1/%d-buffer modify OK\n",i); } for(i=3;i<=4;i++) { /* 2- test a short buffer - text the same size or shrinking */ blen=(int32_t)strlen(kwSetTestCases[i].l)+1; memset(buffer,'%',1023); strcpy(buffer,kwSetTestCases[i].l); status = U_ZERO_ERROR; res = uloc_setKeywordValue(kwSetTestCases[i].k, kwSetTestCases[i].v, buffer, blen, &status); if(status != U_ZERO_ERROR) { log_err("expected zero error got %s\n", u_errorName(status)); return; } if(buffer[blen+1]!='%') { log_err("Buffer byte %d was modified: now %c\n", blen+1, buffer[blen+1]); return; } if(res!=(int32_t)strlen(kwSetTestCases[i].x)) { log_err("expected result %d got %d\n", strlen(kwSetTestCases[i].x), res); return; } if(strcmp(buffer,kwSetTestCases[i].x) || ((int32_t)strlen(buffer)!=res)) { log_err("FAIL: #%d: %s + [%s=%s] -> %s (%d) expected %s (%d)\n", i, kwSetTestCases[i].l, kwSetTestCases[i].k, kwSetTestCases[i].v, buffer, res, kwSetTestCases[i].x, strlen(buffer)); } else { log_verbose("pass: #%d: %s + [%s=%s] -> %s\n", i, kwSetTestCases[i].l, kwSetTestCases[i].k, kwSetTestCases[i].v, buffer); } log_verbose("2/%d-buffer modify OK\n",i); } } static int32_t _canonicalize(int32_t selector, /* 0==getName, 1==canonicalize */ const char* localeID, char* result, int32_t resultCapacity, UErrorCode* ec) { /* YOU can change this to use function pointers if you like */ switch (selector) { case 0: return uloc_getName(localeID, result, resultCapacity, ec); case 1: return uloc_canonicalize(localeID, result, resultCapacity, ec); default: return -1; } } static void TestCanonicalization(void) { static const struct { const char *localeID; /* input */ const char *getNameID; /* expected getName() result */ const char *canonicalID; /* expected canonicalize() result */ } testCases[] = { { "ca_ES_PREEURO-with-extra-stuff-that really doesn't make any sense-unless-you're trying to increase code coverage", "ca_ES_PREEURO_WITH_EXTRA_STUFF_THAT REALLY DOESN'T MAKE ANY SENSE_UNLESS_YOU'RE TRYING TO INCREASE CODE COVERAGE", "ca_ES_PREEURO_WITH_EXTRA_STUFF_THAT REALLY DOESN'T MAKE ANY SENSE_UNLESS_YOU'RE TRYING TO INCREASE CODE COVERAGE"}, { "ca_ES_PREEURO", "ca_ES_PREEURO", "ca_ES@currency=ESP" }, { "de_AT_PREEURO", "de_AT_PREEURO", "de_AT@currency=ATS" }, { "de_DE_PREEURO", "de_DE_PREEURO", "de_DE@currency=DEM" }, { "de_LU_PREEURO", "de_LU_PREEURO", "de_LU@currency=LUF" }, { "el_GR_PREEURO", "el_GR_PREEURO", "el_GR@currency=GRD" }, { "en_BE_PREEURO", "en_BE_PREEURO", "en_BE@currency=BEF" }, { "en_IE_PREEURO", "en_IE_PREEURO", "en_IE@currency=IEP" }, { "es_ES_PREEURO", "es_ES_PREEURO", "es_ES@currency=ESP" }, { "eu_ES_PREEURO", "eu_ES_PREEURO", "eu_ES@currency=ESP" }, { "fi_FI_PREEURO", "fi_FI_PREEURO", "fi_FI@currency=FIM" }, { "fr_BE_PREEURO", "fr_BE_PREEURO", "fr_BE@currency=BEF" }, { "fr_FR_PREEURO", "fr_FR_PREEURO", "fr_FR@currency=FRF" }, { "fr_LU_PREEURO", "fr_LU_PREEURO", "fr_LU@currency=LUF" }, { "ga_IE_PREEURO", "ga_IE_PREEURO", "ga_IE@currency=IEP" }, { "gl_ES_PREEURO", "gl_ES_PREEURO", "gl_ES@currency=ESP" }, { "it_IT_PREEURO", "it_IT_PREEURO", "it_IT@currency=ITL" }, { "nl_BE_PREEURO", "nl_BE_PREEURO", "nl_BE@currency=BEF" }, { "nl_NL_PREEURO", "nl_NL_PREEURO", "nl_NL@currency=NLG" }, { "pt_PT_PREEURO", "pt_PT_PREEURO", "pt_PT@currency=PTE" }, { "de__PHONEBOOK", "de__PHONEBOOK", "de@collation=phonebook" }, { "en_GB_EURO", "en_GB_EURO", "en_GB@currency=EUR" }, { "en_GB@EURO", "en_GB@EURO", "en_GB@currency=EUR" }, /* POSIX ID */ { "es__TRADITIONAL", "es__TRADITIONAL", "es@collation=traditional" }, { "hi__DIRECT", "hi__DIRECT", "hi@collation=direct" }, { "ja_JP_TRADITIONAL", "ja_JP_TRADITIONAL", "ja_JP@calendar=japanese" }, { "th_TH_TRADITIONAL", "th_TH_TRADITIONAL", "th_TH@calendar=buddhist" }, { "zh_TW_STROKE", "zh_TW_STROKE", "zh_TW@collation=stroke" }, { "zh__PINYIN", "zh__PINYIN", "zh@collation=pinyin" }, { "zh@collation=pinyin", "zh@collation=pinyin", "zh@collation=pinyin" }, { "zh_CN@collation=pinyin", "zh_CN@collation=pinyin", "zh_CN@collation=pinyin" }, { "zh_CN_STROKE", "zh_CN_STROKE", "zh_CN@collation=stroke" }, { "zh_CN_CA@collation=pinyin", "zh_CN_CA@collation=pinyin", "zh_CN_CA@collation=pinyin" }, { "en_US_POSIX", "en_US_POSIX", "en_US_POSIX" }, { "hy_AM_REVISED", "hy_AM_REVISED", "hy_AM_REVISED" }, { "no_NO_NY", "no_NO_NY", "no_NO_NY" /* not: "nn_NO" [alan ICU3.0] */ }, { "no@ny", "no@ny", "no__NY" /* not: "nn" [alan ICU3.0] */ }, /* POSIX ID */ { "no-no.utf32@B", "no_NO.utf32@B", "no_NO_B" /* not: "nb_NO_B" [alan ICU3.0] */ }, /* POSIX ID */ { "qz-qz@Euro", "qz_QZ@Euro", "qz_QZ@currency=EUR" }, /* qz-qz uses private use iso codes */ { "en-BOONT", "en_BOONT", "en_BOONT" }, /* registered name */ { "de-1901", "de_1901", "de_1901" }, /* registered name */ { "de-1906", "de_1906", "de_1906" }, /* registered name */ { "sr-SP-Cyrl", "sr_SP_CYRL", "sr_Cyrl_RS" }, /* .NET name */ { "sr-SP-Latn", "sr_SP_LATN", "sr_Latn_RS" }, /* .NET name */ { "sr_YU_CYRILLIC", "sr_YU_CYRILLIC", "sr_Cyrl_RS" }, /* Linux name */ { "uz-UZ-Cyrl", "uz_UZ_CYRL", "uz_Cyrl_UZ" }, /* .NET name */ { "uz-UZ-Latn", "uz_UZ_LATN", "uz_Latn_UZ" }, /* .NET name */ { "zh-CHS", "zh_CHS", "zh_Hans" }, /* .NET name */ { "zh-CHT", "zh_CHT", "zh_Hant" }, /* .NET name This may change back to zh_Hant */ /* posix behavior that used to be performed by getName */ { "mr.utf8", "mr.utf8", "mr" }, { "de-tv.koi8r", "de_TV.koi8r", "de_TV" }, { "x-piglatin_ML.MBE", "x-piglatin_ML.MBE", "x-piglatin_ML" }, { "i-cherokee_US.utf7", "i-cherokee_US.utf7", "i-cherokee_US" }, { "x-filfli_MT_FILFLA.gb-18030", "x-filfli_MT_FILFLA.gb-18030", "x-filfli_MT_FILFLA" }, { "no-no-ny.utf8@B", "no_NO_NY.utf8@B", "no_NO_NY_B" /* not: "nn_NO" [alan ICU3.0] */ }, /* @ ignored unless variant is empty */ /* fleshing out canonicalization */ /* trim space and sort keywords, ';' is separator so not present at end in canonical form */ { "en_Hant_IL_VALLEY_GIRL@ currency = EUR; calendar = Japanese ;", "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR", "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR" }, /* already-canonical ids are not changed */ { "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR", "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR", "en_Hant_IL_VALLEY_GIRL@calendar=Japanese;currency=EUR" }, /* PRE_EURO and EURO conversions don't affect other keywords */ { "es_ES_PREEURO@CALendar=Japanese", "es_ES_PREEURO@calendar=Japanese", "es_ES@calendar=Japanese;currency=ESP" }, { "es_ES_EURO@SHOUT=zipeedeedoodah", "es_ES_EURO@shout=zipeedeedoodah", "es_ES@currency=EUR;shout=zipeedeedoodah" }, /* currency keyword overrides PRE_EURO and EURO currency */ { "es_ES_PREEURO@currency=EUR", "es_ES_PREEURO@currency=EUR", "es_ES@currency=EUR" }, { "es_ES_EURO@currency=ESP", "es_ES_EURO@currency=ESP", "es_ES@currency=ESP" }, /* norwegian is just too weird, if we handle things in their full generality */ { "no-Hant-GB_NY@currency=$$$", "no_Hant_GB_NY@currency=$$$", "no_Hant_GB_NY@currency=$$$" /* not: "nn_Hant_GB@currency=$$$" [alan ICU3.0] */ }, /* test cases reflecting internal resource bundle usage */ { "root@kw=foo", "root@kw=foo", "root@kw=foo" }, { "@calendar=gregorian", "@calendar=gregorian", "@calendar=gregorian" }, { "ja_JP@calendar=Japanese", "ja_JP@calendar=Japanese", "ja_JP@calendar=Japanese" }, { "ja_JP", "ja_JP", "ja_JP" }, /* test case for "i-default" */ { "i-default", "en@x=i-default", "en@x=i-default" } }; static const char* label[] = { "getName", "canonicalize" }; UErrorCode status = U_ZERO_ERROR; int32_t i, j, resultLen = 0, origResultLen; char buffer[256]; for (i=0; i < sizeof(testCases)/sizeof(testCases[0]); i++) { for (j=0; j<2; ++j) { const char* expected = (j==0) ? testCases[i].getNameID : testCases[i].canonicalID; *buffer = 0; status = U_ZERO_ERROR; if (expected == NULL) { expected = uloc_getDefault(); } /* log_verbose("testing %s -> %s\n", testCases[i], testCases[i].canonicalID); */ origResultLen = _canonicalize(j, testCases[i].localeID, NULL, 0, &status); if (status != U_BUFFER_OVERFLOW_ERROR) { log_err("FAIL: uloc_%s(%s) => %s, expected U_BUFFER_OVERFLOW_ERROR\n", label[j], testCases[i].localeID, u_errorName(status)); continue; } status = U_ZERO_ERROR; resultLen = _canonicalize(j, testCases[i].localeID, buffer, sizeof(buffer), &status); if (U_FAILURE(status)) { log_err("FAIL: uloc_%s(%s) => %s, expected U_ZERO_ERROR\n", label[j], testCases[i].localeID, u_errorName(status)); continue; } if(uprv_strcmp(expected, buffer) != 0) { log_err("FAIL: uloc_%s(%s) => \"%s\", expected \"%s\"\n", label[j], testCases[i].localeID, buffer, expected); } else { log_verbose("Ok: uloc_%s(%s) => \"%s\"\n", label[j], testCases[i].localeID, buffer); } if (resultLen != (int32_t)strlen(buffer)) { log_err("FAIL: uloc_%s(%s) => len %d, expected len %d\n", label[j], testCases[i].localeID, resultLen, strlen(buffer)); } if (origResultLen != resultLen) { log_err("FAIL: uloc_%s(%s) => preflight len %d != actual len %d\n", label[j], testCases[i].localeID, origResultLen, resultLen); } } } } static void TestDisplayKeywords(void) { int32_t i; static const struct { const char *localeID; const char *displayLocale; UChar displayKeyword[200]; } testCases[] = { { "ca_ES@currency=ESP", "de_AT", {0x0057, 0x00e4, 0x0068, 0x0072, 0x0075, 0x006e, 0x0067, 0x0000}, }, { "ja_JP@calendar=japanese", "de", { 0x004b, 0x0061, 0x006c, 0x0065, 0x006e, 0x0064, 0x0065, 0x0072, 0x0000} }, { "de_DE@collation=traditional", "de_DE", {0x0053, 0x006f, 0x0072, 0x0074, 0x0069, 0x0065, 0x0072, 0x0075, 0x006e, 0x0067, 0x0000} }, }; for(i = 0; i < sizeof(testCases)/sizeof(testCases[0]); i++) { UErrorCode status = U_ZERO_ERROR; const char* keyword =NULL; int32_t keywordLen = 0; int32_t keywordCount = 0; UChar *displayKeyword=NULL; int32_t displayKeywordLen = 0; UEnumeration* keywordEnum = uloc_openKeywords(testCases[i].localeID, &status); for(keywordCount = uenum_count(keywordEnum, &status); keywordCount > 0 ; keywordCount--){ if(U_FAILURE(status)){ log_err("uloc_getKeywords failed for locale id: %s with error : %s \n", testCases[i].localeID, u_errorName(status)); break; } /* the uenum_next returns NUL terminated string */ keyword = uenum_next(keywordEnum, &keywordLen, &status); /* fetch the displayKeyword */ displayKeywordLen = uloc_getDisplayKeyword(keyword, testCases[i].displayLocale, displayKeyword, displayKeywordLen, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status = U_ZERO_ERROR; displayKeywordLen++; /* for null termination */ displayKeyword = (UChar*) malloc(displayKeywordLen * U_SIZEOF_UCHAR); displayKeywordLen = uloc_getDisplayKeyword(keyword, testCases[i].displayLocale, displayKeyword, displayKeywordLen, &status); if(U_FAILURE(status)){ log_err("uloc_getDisplayKeyword filed for keyword : %s in locale id: %s for display locale: %s \n", testCases[i].localeID, keyword, testCases[i].displayLocale, u_errorName(status)); break; } if(u_strncmp(displayKeyword, testCases[i].displayKeyword, displayKeywordLen)!=0){ if (status == U_USING_DEFAULT_WARNING) { log_data_err("uloc_getDisplayKeyword did not get the expected value for keyword : %s in locale id: %s for display locale: %s . Got error: %s. Perhaps you are missing data?\n", testCases[i].localeID, keyword, testCases[i].displayLocale, u_errorName(status)); } else { log_err("uloc_getDisplayKeyword did not get the expected value for keyword : %s in locale id: %s for display locale: %s \n", testCases[i].localeID, keyword, testCases[i].displayLocale); } break; } }else{ log_err("uloc_getDisplayKeyword did not return the expected error. Error: %s\n", u_errorName(status)); } free(displayKeyword); } uenum_close(keywordEnum); } } static void TestDisplayKeywordValues(void){ int32_t i; static const struct { const char *localeID; const char *displayLocale; UChar displayKeywordValue[500]; } testCases[] = { { "ca_ES@currency=ESP", "de_AT", {0x0053, 0x0070, 0x0061, 0x006e, 0x0069, 0x0073, 0x0063, 0x0068, 0x0065, 0x0020, 0x0050, 0x0065, 0x0073, 0x0065, 0x0074, 0x0061, 0x0000} }, { "de_AT@currency=ATS", "fr_FR", {0x0073, 0x0063, 0x0068, 0x0069, 0x006c, 0x006c, 0x0069, 0x006e, 0x0067, 0x0020, 0x0061, 0x0075, 0x0074, 0x0072, 0x0069, 0x0063, 0x0068, 0x0069, 0x0065, 0x006e, 0x0000} }, { "de_DE@currency=DEM", "it", {0x004d, 0x0061, 0x0072, 0x0063, 0x006f, 0x0020, 0x0054, 0x0065, 0x0064, 0x0065, 0x0073, 0x0063, 0x006f, 0x0000} }, { "el_GR@currency=GRD", "en", {0x0047, 0x0072, 0x0065, 0x0065, 0x006b, 0x0020, 0x0044, 0x0072, 0x0061, 0x0063, 0x0068, 0x006d, 0x0061, 0x0000} }, { "eu_ES@currency=ESP", "it_IT", {0x0050, 0x0065, 0x0073, 0x0065, 0x0074, 0x0061, 0x0020, 0x0053, 0x0070, 0x0061, 0x0067, 0x006e, 0x006f, 0x006c, 0x0061, 0x0000} }, { "de@collation=phonebook", "es", {0x006F, 0x0072, 0x0064, 0x0065, 0x006E, 0x0020, 0x0064, 0x0065, 0x0020, 0x006C, 0x0069, 0x0073, 0x0074, 0x00ED, 0x006E, 0x0020, 0x0074, 0x0065, 0x006C, 0x0065, 0x0066, 0x00F3, 0x006E, 0x0069, 0x0063, 0x006F, 0x0000} }, { "de_DE@collation=phonebook", "es", {0x006F, 0x0072, 0x0064, 0x0065, 0x006E, 0x0020, 0x0064, 0x0065, 0x0020, 0x006C, 0x0069, 0x0073, 0x0074, 0x00ED, 0x006E, 0x0020, 0x0074, 0x0065, 0x006C, 0x0065, 0x0066, 0x00F3, 0x006E, 0x0069, 0x0063, 0x006F, 0x0000} }, { "es_ES@collation=traditional","de", {0x0054, 0x0072, 0x0061, 0x0064, 0x0069, 0x0074, 0x0069, 0x006f, 0x006e, 0x0065, 0x006c, 0x006c, 0x0065, 0x0020, 0x0053, 0x006f, 0x0072, 0x0074, 0x0069, 0x0065, 0x0072, 0x0072, 0x0065, 0x0067, 0x0065, 0x006c, 0x006e, 0x0000} }, { "ja_JP@calendar=japanese", "de", {0x004a, 0x0061, 0x0070, 0x0061, 0x006e, 0x0069, 0x0073, 0x0063, 0x0068, 0x0065, 0x0072, 0x0020, 0x004b, 0x0061, 0x006c, 0x0065, 0x006e, 0x0064, 0x0065, 0x0072, 0x0000} }, }; for(i = 0; i < sizeof(testCases)/sizeof(testCases[0]); i++) { UErrorCode status = U_ZERO_ERROR; const char* keyword =NULL; int32_t keywordLen = 0; int32_t keywordCount = 0; UChar *displayKeywordValue = NULL; int32_t displayKeywordValueLen = 0; UEnumeration* keywordEnum = uloc_openKeywords(testCases[i].localeID, &status); for(keywordCount = uenum_count(keywordEnum, &status); keywordCount > 0 ; keywordCount--){ if(U_FAILURE(status)){ log_err("uloc_getKeywords failed for locale id: %s in display locale: % with error : %s \n", testCases[i].localeID, testCases[i].displayLocale, u_errorName(status)); break; } /* the uenum_next returns NUL terminated string */ keyword = uenum_next(keywordEnum, &keywordLen, &status); /* fetch the displayKeywordValue */ displayKeywordValueLen = uloc_getDisplayKeywordValue(testCases[i].localeID, keyword, testCases[i].displayLocale, displayKeywordValue, displayKeywordValueLen, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status = U_ZERO_ERROR; displayKeywordValueLen++; /* for null termination */ displayKeywordValue = (UChar*)malloc(displayKeywordValueLen * U_SIZEOF_UCHAR); displayKeywordValueLen = uloc_getDisplayKeywordValue(testCases[i].localeID, keyword, testCases[i].displayLocale, displayKeywordValue, displayKeywordValueLen, &status); if(U_FAILURE(status)){ log_err("uloc_getDisplayKeywordValue failed for keyword : %s in locale id: %s for display locale: %s with error : %s \n", testCases[i].localeID, keyword, testCases[i].displayLocale, u_errorName(status)); break; } if(u_strncmp(displayKeywordValue, testCases[i].displayKeywordValue, displayKeywordValueLen)!=0){ if (status == U_USING_DEFAULT_WARNING) { log_data_err("uloc_getDisplayKeywordValue did not return the expected value keyword : %s in locale id: %s for display locale: %s with error : %s Perhaps you are missing data\n", testCases[i].localeID, keyword, testCases[i].displayLocale, u_errorName(status)); } else { log_err("uloc_getDisplayKeywordValue did not return the expected value keyword : %s in locale id: %s for display locale: %s with error : %s \n", testCases[i].localeID, keyword, testCases[i].displayLocale, u_errorName(status)); } break; } }else{ log_err("uloc_getDisplayKeywordValue did not return the expected error. Error: %s\n", u_errorName(status)); } free(displayKeywordValue); } uenum_close(keywordEnum); } { /* test a multiple keywords */ UErrorCode status = U_ZERO_ERROR; const char* keyword =NULL; int32_t keywordLen = 0; int32_t keywordCount = 0; const char* localeID = "es@collation=phonebook;calendar=buddhist;currency=DEM"; const char* displayLocale = "de"; static const UChar expected[][50] = { {0x0042, 0x0075, 0x0064, 0x0064, 0x0068, 0x0069, 0x0073, 0x0074, 0x0069, 0x0073, 0x0063, 0x0068, 0x0065, 0x0072, 0x0020, 0x004b, 0x0061, 0x006c, 0x0065, 0x006e, 0x0064, 0x0065, 0x0072, 0x0000}, {0x0054, 0x0065, 0x006c, 0x0065, 0x0066, 0x006f, 0x006e, 0x0062, 0x0075, 0x0063, 0x0068, 0x002d, 0x0053, 0x006f, 0x0072, 0x0074, 0x0069, 0x0065, 0x0072, 0x0072, 0x0065, 0x0067, 0x0065, 0x006c, 0x006e, 0x0000}, {0x0044, 0x0065, 0x0075, 0x0074, 0x0073, 0x0063, 0x0068, 0x0065, 0x0020, 0x004d, 0x0061, 0x0072, 0x006b, 0x0000}, }; UEnumeration* keywordEnum = uloc_openKeywords(localeID, &status); for(keywordCount = 0; keywordCount < uenum_count(keywordEnum, &status) ; keywordCount++){ UChar *displayKeywordValue = NULL; int32_t displayKeywordValueLen = 0; if(U_FAILURE(status)){ log_err("uloc_getKeywords failed for locale id: %s in display locale: % with error : %s \n", localeID, displayLocale, u_errorName(status)); break; } /* the uenum_next returns NUL terminated string */ keyword = uenum_next(keywordEnum, &keywordLen, &status); /* fetch the displayKeywordValue */ displayKeywordValueLen = uloc_getDisplayKeywordValue(localeID, keyword, displayLocale, displayKeywordValue, displayKeywordValueLen, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status = U_ZERO_ERROR; displayKeywordValueLen++; /* for null termination */ displayKeywordValue = (UChar*)malloc(displayKeywordValueLen * U_SIZEOF_UCHAR); displayKeywordValueLen = uloc_getDisplayKeywordValue(localeID, keyword, displayLocale, displayKeywordValue, displayKeywordValueLen, &status); if(U_FAILURE(status)){ log_err("uloc_getDisplayKeywordValue failed for keyword : %s in locale id: %s for display locale: %s with error : %s \n", localeID, keyword, displayLocale, u_errorName(status)); break; } if(u_strncmp(displayKeywordValue, expected[keywordCount], displayKeywordValueLen)!=0){ if (status == U_USING_DEFAULT_WARNING) { log_data_err("uloc_getDisplayKeywordValue did not return the expected value keyword : %s in locale id: %s for display locale: %s got error: %s. Perhaps you are missing data?\n", localeID, keyword, displayLocale, u_errorName(status)); } else { log_err("uloc_getDisplayKeywordValue did not return the expected value keyword : %s in locale id: %s for display locale: %s \n", localeID, keyword, displayLocale); } break; } }else{ log_err("uloc_getDisplayKeywordValue did not return the expected error. Error: %s\n", u_errorName(status)); } free(displayKeywordValue); } uenum_close(keywordEnum); } { /* Test non existent keywords */ UErrorCode status = U_ZERO_ERROR; const char* localeID = "es"; const char* displayLocale = "de"; UChar *displayKeywordValue = NULL; int32_t displayKeywordValueLen = 0; /* fetch the displayKeywordValue */ displayKeywordValueLen = uloc_getDisplayKeywordValue(localeID, "calendar", displayLocale, displayKeywordValue, displayKeywordValueLen, &status); if(U_FAILURE(status)) { log_err("uloc_getDisplaykeywordValue returned error status %s\n", u_errorName(status)); } else if(displayKeywordValueLen != 0) { log_err("uloc_getDisplaykeywordValue returned %d should be 0 \n", displayKeywordValueLen); } } } static void TestGetBaseName(void) { static const struct { const char *localeID; const char *baseName; } testCases[] = { { "de_DE@ C o ll A t i o n = Phonebook ", "de_DE" }, { "de@currency = euro; CoLLaTion = PHONEBOOk", "de" }, { "ja@calendar = buddhist", "ja" } }; int32_t i = 0, baseNameLen = 0; char baseName[256]; UErrorCode status = U_ZERO_ERROR; for(i = 0; i < sizeof(testCases)/sizeof(testCases[0]); i++) { baseNameLen = uloc_getBaseName(testCases[i].localeID, baseName, 256, &status); if(strcmp(testCases[i].baseName, baseName)) { log_err("For locale \"%s\" expected baseName \"%s\", but got \"%s\"\n", testCases[i].localeID, testCases[i].baseName, baseName); return; } } } static void TestTrailingNull(void) { const char* localeId = "zh_Hans"; UChar buffer[128]; /* sufficient for this test */ int32_t len; UErrorCode status = U_ZERO_ERROR; int i; len = uloc_getDisplayName(localeId, localeId, buffer, 128, &status); if (len > 128) { log_err("buffer too small"); return; } for (i = 0; i < len; ++i) { if (buffer[i] == 0) { log_err("name contained null"); return; } } } /* Jitterbug 4115 */ static void TestDisplayNameWarning(void) { UChar name[256]; int32_t size; UErrorCode status = U_ZERO_ERROR; size = uloc_getDisplayLanguage("qqq", "kl", name, sizeof(name)/sizeof(name[0]), &status); if (status != U_USING_DEFAULT_WARNING) { log_err("For language \"qqq\" in locale \"kl\", expecting U_USING_DEFAULT_WARNING, but got %s\n", u_errorName(status)); } } /** * Compare two locale IDs. If they are equal, return 0. If `string' * starts with `prefix' plus an additional element, that is, string == * prefix + '_' + x, then return 1. Otherwise return a value < 0. */ static UBool _loccmp(const char* string, const char* prefix) { int32_t slen = (int32_t)uprv_strlen(string), plen = (int32_t)uprv_strlen(prefix); int32_t c = uprv_strncmp(string, prefix, plen); /* 'root' is less than everything */ if (uprv_strcmp(prefix, "root") == 0) { return (uprv_strcmp(string, "root") == 0) ? 0 : 1; } if (c) return -1; /* mismatch */ if (slen == plen) return 0; if (string[plen] == '_') return 1; return -2; /* false match, e.g. "en_USX" cmp "en_US" */ } static void _checklocs(const char* label, const char* req, const char* valid, const char* actual) { /* We want the valid to be strictly > the bogus requested locale, and the valid to be >= the actual. */ if (_loccmp(req, valid) > 0 && _loccmp(valid, actual) >= 0) { log_verbose("%s; req=%s, valid=%s, actual=%s\n", label, req, valid, actual); } else { log_err("FAIL: %s; req=%s, valid=%s, actual=%s\n", label, req, valid, actual); } } static void TestGetLocale(void) { UErrorCode ec = U_ZERO_ERROR; UParseError pe; UChar EMPTY[1] = {0}; /* === udat === */ #if !UCONFIG_NO_FORMATTING { UDateFormat *obj; const char *req = "en_US_REDWOODSHORES", *valid, *actual; obj = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, req, NULL, 0, NULL, 0, &ec); if (U_FAILURE(ec)) { log_data_err("udat_open failed.Error %s\n", u_errorName(ec)); return; } valid = udat_getLocaleByType(obj, ULOC_VALID_LOCALE, &ec); actual = udat_getLocaleByType(obj, ULOC_ACTUAL_LOCALE, &ec); if (U_FAILURE(ec)) { log_err("udat_getLocaleByType() failed\n"); return; } _checklocs("udat", req, valid, actual); udat_close(obj); } #endif /* === ucal === */ #if !UCONFIG_NO_FORMATTING { UCalendar *obj; const char *req = "fr_FR_PROVENCAL", *valid, *actual; obj = ucal_open(NULL, 0, req, UCAL_GREGORIAN, &ec); if (U_FAILURE(ec)) { log_err("ucal_open failed with error: %s\n", u_errorName(ec)); return; } valid = ucal_getLocaleByType(obj, ULOC_VALID_LOCALE, &ec); actual = ucal_getLocaleByType(obj, ULOC_ACTUAL_LOCALE, &ec); if (U_FAILURE(ec)) { log_err("ucal_getLocaleByType() failed\n"); return; } _checklocs("ucal", req, valid, actual); ucal_close(obj); } #endif /* === unum === */ #if !UCONFIG_NO_FORMATTING { UNumberFormat *obj; const char *req = "zh_Hant_TW_TAINAN", *valid, *actual; obj = unum_open(UNUM_DECIMAL, NULL, 0, req, &pe, &ec); if (U_FAILURE(ec)) { log_err("unum_open failed\n"); return; } valid = unum_getLocaleByType(obj, ULOC_VALID_LOCALE, &ec); actual = unum_getLocaleByType(obj, ULOC_ACTUAL_LOCALE, &ec); if (U_FAILURE(ec)) { log_err("unum_getLocaleByType() failed\n"); return; } _checklocs("unum", req, valid, actual); unum_close(obj); } #endif /* === umsg === */ #if 0 /* commented out by weiv 01/12/2005. umsg_getLocaleByType is to be removed */ #if !UCONFIG_NO_FORMATTING { UMessageFormat *obj; const char *req = "ja_JP_TAKAYAMA", *valid, *actual; UBool test; obj = umsg_open(EMPTY, 0, req, &pe, &ec); if (U_FAILURE(ec)) { log_err("umsg_open failed\n"); return; } valid = umsg_getLocaleByType(obj, ULOC_VALID_LOCALE, &ec); actual = umsg_getLocaleByType(obj, ULOC_ACTUAL_LOCALE, &ec); if (U_FAILURE(ec)) { log_err("umsg_getLocaleByType() failed\n"); return; } /* We want the valid to be strictly > the bogus requested locale, and the valid to be >= the actual. */ /* TODO MessageFormat is currently just storing the locale it is given. As a result, it will return whatever it was given, even if the locale is invalid. */ test = (_cmpversion("3.2") <= 0) ? /* Here is the weakened test for 3.0: */ (_loccmp(req, valid) >= 0) : /* Here is what the test line SHOULD be: */ (_loccmp(req, valid) > 0); if (test && _loccmp(valid, actual) >= 0) { log_verbose("umsg; req=%s, valid=%s, actual=%s\n", req, valid, actual); } else { log_err("FAIL: umsg; req=%s, valid=%s, actual=%s\n", req, valid, actual); } umsg_close(obj); } #endif #endif /* === ubrk === */ #if !UCONFIG_NO_BREAK_ITERATION { UBreakIterator *obj; const char *req = "ar_KW_ABDALI", *valid, *actual; obj = ubrk_open(UBRK_WORD, req, EMPTY, 0, &ec); if (U_FAILURE(ec)) { log_err("ubrk_open failed. Error: %s \n", u_errorName(ec)); return; } valid = ubrk_getLocaleByType(obj, ULOC_VALID_LOCALE, &ec); actual = ubrk_getLocaleByType(obj, ULOC_ACTUAL_LOCALE, &ec); if (U_FAILURE(ec)) { log_err("ubrk_getLocaleByType() failed\n"); return; } _checklocs("ubrk", req, valid, actual); ubrk_close(obj); } #endif /* === ucol === */ #if !UCONFIG_NO_COLLATION { UCollator *obj; const char *req = "es_AR_BUENOSAIRES", *valid, *actual; obj = ucol_open(req, &ec); if (U_FAILURE(ec)) { log_err("ucol_open failed - %s\n", u_errorName(ec)); return; } valid = ucol_getLocaleByType(obj, ULOC_VALID_LOCALE, &ec); actual = ucol_getLocaleByType(obj, ULOC_ACTUAL_LOCALE, &ec); if (U_FAILURE(ec)) { log_err("ucol_getLocaleByType() failed\n"); return; } _checklocs("ucol", req, valid, actual); ucol_close(obj); } #endif } static void TestNonexistentLanguageExemplars(void) { /* JB 4068 - Nonexistent language */ UErrorCode ec = U_ZERO_ERROR; ULocaleData *uld = ulocdata_open("qqq",&ec); if (ec != U_USING_DEFAULT_WARNING) { log_err_status(ec, "Exemplar set for \"qqq\", expecting U_USING_DEFAULT_WARNING, but got %s\n", u_errorName(ec)); } uset_close(ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &ec)); ulocdata_close(uld); } static void TestLocDataErrorCodeChaining(void) { UErrorCode ec = U_USELESS_COLLATOR_ERROR; ulocdata_open(NULL, &ec); ulocdata_getExemplarSet(NULL, NULL, 0, ULOCDATA_ES_STANDARD, &ec); ulocdata_getDelimiter(NULL, ULOCDATA_DELIMITER_COUNT, NULL, -1, &ec); ulocdata_getMeasurementSystem(NULL, &ec); ulocdata_getPaperSize(NULL, NULL, NULL, &ec); if (ec != U_USELESS_COLLATOR_ERROR) { log_err("ulocdata API changed the error code to %s\n", u_errorName(ec)); } } static void TestLanguageExemplarsFallbacks(void) { /* Test that en_US fallsback, but en doesn't fallback. */ UErrorCode ec = U_ZERO_ERROR; ULocaleData *uld = ulocdata_open("en_US",&ec); uset_close(ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &ec)); if (ec != U_USING_FALLBACK_WARNING) { log_err_status(ec, "Exemplar set for \"en_US\", expecting U_USING_FALLBACK_WARNING, but got %s\n", u_errorName(ec)); } ulocdata_close(uld); ec = U_ZERO_ERROR; uld = ulocdata_open("en",&ec); uset_close(ulocdata_getExemplarSet(uld, NULL, 0, ULOCDATA_ES_STANDARD, &ec)); if (ec != U_ZERO_ERROR) { log_err_status(ec, "Exemplar set for \"en\", expecting U_ZERO_ERROR, but got %s\n", u_errorName(ec)); } ulocdata_close(uld); } static const char *acceptResult(UAcceptResult uar) { return udbg_enumName(UDBG_UAcceptResult, uar); } static void TestAcceptLanguage(void) { UErrorCode status = U_ZERO_ERROR; UAcceptResult outResult; UEnumeration *available; char tmp[200]; int i; int32_t rc = 0; struct { int32_t httpSet; /**< Which of http[] should be used? */ const char *icuSet; /**< ? */ const char *expect; /**< The expected locale result */ UAcceptResult res; /**< The expected error code */ } tests[] = { /*0*/{ 0, NULL, "mt_MT", ULOC_ACCEPT_VALID }, /*1*/{ 1, NULL, "en", ULOC_ACCEPT_VALID }, /*2*/{ 2, NULL, "en", ULOC_ACCEPT_FALLBACK }, /*3*/{ 3, NULL, "", ULOC_ACCEPT_FAILED }, /*4*/{ 4, NULL, "es", ULOC_ACCEPT_VALID }, /*5*/{ 5, NULL, "en", ULOC_ACCEPT_VALID }, /* XF */ /*6*/{ 6, NULL, "ja", ULOC_ACCEPT_FALLBACK }, /* XF */ /*7*/{ 7, NULL, "zh", ULOC_ACCEPT_FALLBACK }, /* XF */ }; const int32_t numTests = sizeof(tests)/sizeof(tests[0]); static const char *http[] = { /*0*/ "mt-mt, ja;q=0.76, en-us;q=0.95, en;q=0.92, en-gb;q=0.89, fr;q=0.87, iu-ca;q=0.84, iu;q=0.82, ja-jp;q=0.79, mt;q=0.97, de-de;q=0.74, de;q=0.71, es;q=0.68, it-it;q=0.66, it;q=0.63, vi-vn;q=0.61, vi;q=0.58, nl-nl;q=0.55, nl;q=0.53, th-th-traditional;q=.01", /*1*/ "ja;q=0.5, en;q=0.8, tlh", /*2*/ "en-wf, de-lx;q=0.8", /*3*/ "mga-ie;q=0.9, tlh", /*4*/ "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, " "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, " "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, " "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, " "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, " "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, " "xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xx-yy;q=.1, " "es", /*5*/ "zh-xx;q=0.9, en;q=0.6", /*6*/ "ja-JA", /*7*/ "zh-xx;q=0.9", }; for(i=0;i0)&&uprv_strcmp(tmp, tests[i].expect)) { log_err_status(status, "FAIL: #%d: expected %s but got %s\n", i, tests[i].expect, tmp); log_info("test #%d: http[%s], ICU[%s], expect %s, %s\n", i, http[tests[i].httpSet], tests[i].icuSet, tests[i].expect, acceptResult(tests[i].res)); } } } static const char* LOCALE_ALIAS[][2] = { {"in", "id"}, {"in_ID", "id_ID"}, {"iw", "he"}, {"iw_IL", "he_IL"}, {"ji", "yi"}, {"en_BU", "en_MM"}, {"en_DY", "en_BJ"}, {"en_HV", "en_BF"}, {"en_NH", "en_VU"}, {"en_RH", "en_ZW"}, {"en_TP", "en_TL"}, {"en_ZR", "en_CD"} }; static UBool isLocaleAvailable(UResourceBundle* resIndex, const char* loc){ UErrorCode status = U_ZERO_ERROR; int32_t len = 0; ures_getStringByKey(resIndex, loc,&len, &status); if(U_FAILURE(status)){ return FALSE; } return TRUE; } static void TestCalendar() { #if !UCONFIG_NO_FORMATTING int i; UErrorCode status = U_ZERO_ERROR; UResourceBundle *resIndex = ures_open(NULL,"res_index", &status); if(U_FAILURE(status)){ log_err_status(status, "Could not open res_index.res. Exiting. Error: %s\n", u_errorName(status)); return; } for (i=0; iuerror == U_BUFFER_OVERFLOW_ERROR || data->uerror == U_STRING_NOT_TERMINATED_WARNING) { return strlen(data->expected); } else { return -1; } } static int32_t getBufferSize(const errorData* data, int32_t actualSize) { if (data->expected == NULL) { return actualSize; } else if (data->bufferSize < 0) { return strlen(data->expected) + 1; } else { return data->bufferSize; } } static void TestLikelySubtags() { char buffer[ULOC_FULLNAME_CAPACITY + ULOC_KEYWORD_AND_VALUES_CAPACITY + 1]; int32_t i = 0; for (; i < sizeof(basic_maximize_data) / sizeof(basic_maximize_data[0]); ++i) { UErrorCode status = U_ZERO_ERROR; const char* const minimal = basic_maximize_data[i][0]; const char* const maximal = basic_maximize_data[i][1]; /* const int32_t length = */ uloc_addLikelySubtags( minimal, buffer, sizeof(buffer), &status); if (U_FAILURE(status)) { log_err_status(status, " unexpected failure of uloc_addLikelySubtags(), minimal \"%s\" status %s\n", minimal, u_errorName(status)); status = U_ZERO_ERROR; } else if (uprv_strlen(maximal) == 0) { if (uprv_stricmp(minimal, buffer) != 0) { log_err(" unexpected maximal value \"%s\" in uloc_addLikelySubtags(), minimal \"%s\" = \"%s\"\n", maximal, minimal, buffer); } } else if (uprv_stricmp(maximal, buffer) != 0) { log_err(" maximal doesn't match expected %s in uloc_addLikelySubtags(), minimal \"%s\" = %s\n", maximal, minimal, buffer); } } for (i = 0; i < sizeof(basic_minimize_data) / sizeof(basic_minimize_data[0]); ++i) { UErrorCode status = U_ZERO_ERROR; const char* const maximal = basic_minimize_data[i][0]; const char* const minimal = basic_minimize_data[i][1]; /* const int32_t length = */ uloc_minimizeSubtags( maximal, buffer, sizeof(buffer), &status); if (U_FAILURE(status)) { log_err_status(status, " unexpected failure of uloc_MinimizeSubtags(), maximal \"%s\" status %s\n", maximal, u_errorName(status)); status = U_ZERO_ERROR; } else if (uprv_strlen(minimal) == 0) { if (uprv_stricmp(maximal, buffer) != 0) { log_err(" unexpected minimal value \"%s\" in uloc_minimizeSubtags(), maximal \"%s\" = \"%s\"\n", minimal, maximal, buffer); } } else if (uprv_stricmp(minimal, buffer) != 0) { log_err(" minimal doesn't match expected %s in uloc_MinimizeSubtags(), maximal \"%s\" = %s\n", minimal, maximal, buffer); } } for (i = 0; i < sizeof(full_data) / sizeof(full_data[0]); ++i) { UErrorCode status = U_ZERO_ERROR; const char* const minimal = full_data[i][0]; const char* const maximal = full_data[i][1]; /* const int32_t length = */ uloc_addLikelySubtags( minimal, buffer, sizeof(buffer), &status); if (U_FAILURE(status)) { log_err_status(status, " unexpected failure of uloc_addLikelySubtags(), minimal \"%s\" status \"%s\"\n", minimal, u_errorName(status)); status = U_ZERO_ERROR; } else if (uprv_strlen(maximal) == 0) { if (uprv_stricmp(minimal, buffer) != 0) { log_err(" unexpected maximal value \"%s\" in uloc_addLikelySubtags(), minimal \"%s\" = \"%s\"\n", maximal, minimal, buffer); } } else if (uprv_stricmp(maximal, buffer) != 0) { log_err(" maximal doesn't match expected \"%s\" in uloc_addLikelySubtags(), minimal \"%s\" = \"%s\"\n", maximal, minimal, buffer); } } for (i = 0; i < sizeof(full_data) / sizeof(full_data[0]); ++i) { UErrorCode status = U_ZERO_ERROR; const char* const maximal = full_data[i][1]; const char* const minimal = full_data[i][2]; if (strlen(maximal) > 0) { /* const int32_t length = */ uloc_minimizeSubtags( maximal, buffer, sizeof(buffer), &status); if (U_FAILURE(status)) { log_err_status(status, " unexpected failure of uloc_minimizeSubtags(), maximal \"%s\" status %s\n", maximal, u_errorName(status)); status = U_ZERO_ERROR; } else if (uprv_strlen(minimal) == 0) { if (uprv_stricmp(maximal, buffer) != 0) { log_err(" unexpected minimal value \"%s\" in uloc_minimizeSubtags(), maximal \"%s\" = \"%s\"\n", minimal, maximal, buffer); } } else if (uprv_stricmp(minimal, buffer) != 0) { log_err(" minimal doesn't match expected %s in uloc_MinimizeSubtags(), maximal \"%s\" = %s\n", minimal, maximal, buffer); } } } for (i = 0; i < sizeof(maximizeErrors) / sizeof(maximizeErrors[0]); ++i) { UErrorCode status = U_ZERO_ERROR; const char* const minimal = maximizeErrors[i].tag; const char* const maximal = maximizeErrors[i].expected; const UErrorCode expectedStatus = maximizeErrors[i].uerror; const int32_t expectedLength = getExpectedReturnValue(&maximizeErrors[i]); const int32_t bufferSize = getBufferSize(&maximizeErrors[i], sizeof(buffer)); const int32_t length = uloc_addLikelySubtags( minimal, buffer, bufferSize, &status); if (status == U_ZERO_ERROR) { log_err(" unexpected U_ZERO_ERROR for uloc_addLikelySubtags(), minimal \"%s\" expected status %s\n", minimal, u_errorName(expectedStatus)); status = U_ZERO_ERROR; } else if (status != expectedStatus) { log_err_status(status, " unexpected status for uloc_addLikelySubtags(), minimal \"%s\" expected status %s, but got %s\n", minimal, u_errorName(expectedStatus), u_errorName(status)); } else if (length != expectedLength) { log_err(" unexpected length for uloc_addLikelySubtags(), minimal \"%s\" expected length %d, but got %d\n", minimal, expectedLength, length); } else if (status == U_BUFFER_OVERFLOW_ERROR || status == U_STRING_NOT_TERMINATED_WARNING) { if (uprv_strnicmp(maximal, buffer, bufferSize) != 0) { log_err(" maximal doesn't match expected %s in uloc_addLikelySubtags(), minimal \"%s\" = %*s\n", maximal, minimal, (int)sizeof(buffer), buffer); } } } for (i = 0; i < sizeof(minimizeErrors) / sizeof(minimizeErrors[0]); ++i) { UErrorCode status = U_ZERO_ERROR; const char* const maximal = minimizeErrors[i].tag; const char* const minimal = minimizeErrors[i].expected; const UErrorCode expectedStatus = minimizeErrors[i].uerror; const int32_t expectedLength = getExpectedReturnValue(&minimizeErrors[i]); const int32_t bufferSize = getBufferSize(&minimizeErrors[i], sizeof(buffer)); const int32_t length = uloc_minimizeSubtags( maximal, buffer, bufferSize, &status); if (status == U_ZERO_ERROR) { log_err(" unexpected U_ZERO_ERROR for uloc_minimizeSubtags(), maximal \"%s\" expected status %s\n", maximal, u_errorName(expectedStatus)); status = U_ZERO_ERROR; } else if (status != expectedStatus) { log_err_status(status, " unexpected status for uloc_minimizeSubtags(), maximal \"%s\" expected status %s, but got %s\n", maximal, u_errorName(expectedStatus), u_errorName(status)); } else if (length != expectedLength) { log_err(" unexpected length for uloc_minimizeSubtags(), maximal \"%s\" expected length %d, but got %d\n", maximal, expectedLength, length); } else if (status == U_BUFFER_OVERFLOW_ERROR || status == U_STRING_NOT_TERMINATED_WARNING) { if (uprv_strnicmp(minimal, buffer, bufferSize) != 0) { log_err(" minimal doesn't match expected \"%s\" in uloc_minimizeSubtags(), minimal \"%s\" = \"%*s\"\n", minimal, maximal, (int)sizeof(buffer), buffer); } } } } const char* const locale_to_langtag[][3] = { {"@x=elmer", "x-elmer", "x-elmer"}, {"", "und", "und"}, {"en", "en", "en"}, {"en_US", "en-US", "en-US"}, {"iw_IL", "he-IL", "he-IL"}, {"sr_Latn_SR", "sr-Latn-SR", "sr-Latn-SR"}, {"en__POSIX", "en-u-va-posix", "en-u-va-posix"}, {"en_POSIX", "en-u-va-posix", "en-u-va-posix"}, {"en_US_POSIX_VAR", "en-US-posix-x-lvariant-var", NULL}, /* variant POSIX_VAR is processed as regular variant */ {"en_US_VAR_POSIX", "en-US-x-lvariant-var-posix", NULL}, /* variant VAR_POSIX is processed as regular variant */ {"en_US_POSIX@va=posix2", "en-US-u-va-posix2", "en-US-u-va-posix2"}, /* if keyword va=xxx already exists, variant POSIX is simply dropped */ {"en_US_POSIX@ca=japanese", "en-US-u-ca-japanese-va-posix", "en-US-u-ca-japanese-va-posix"}, {"und_555", "und-555", "und-555"}, {"123", "und", NULL}, {"%$#&", "und", NULL}, {"_Latn", "und-Latn", "und-Latn"}, {"_DE", "und-DE", "und-DE"}, {"und_FR", "und-FR", "und-FR"}, {"th_TH_TH", "th-TH-x-lvariant-th", NULL}, {"bogus", "bogus", "bogus"}, {"foooobarrr", "und", NULL}, {"az_AZ_CYRL", "az-Cyrl-AZ", "az-Cyrl-AZ"}, {"aa_BB_CYRL", "aa-BB-x-lvariant-cyrl", NULL}, {"en_US_1234", "en-US-1234", "en-US-1234"}, {"en_US_VARIANTA_VARIANTB", "en-US-varianta-variantb", "en-US-varianta-variantb"}, {"ja__9876_5432", "ja-9876-5432", "ja-9876-5432"}, {"zh_Hant__VAR", "zh-Hant-x-lvariant-var", NULL}, {"es__BADVARIANT_GOODVAR", "es-goodvar", NULL}, {"en@calendar=gregorian", "en-u-ca-gregory", "en-u-ca-gregory"}, {"de@collation=phonebook;calendar=gregorian", "de-u-ca-gregory-co-phonebk", "de-u-ca-gregory-co-phonebk"}, {"th@numbers=thai;z=extz;x=priv-use;a=exta", "th-a-exta-u-nu-thai-z-extz-x-priv-use", "th-a-exta-u-nu-thai-z-extz-x-priv-use"}, {"en@timezone=America/New_York;calendar=japanese", "en-u-ca-japanese-tz-usnyc", "en-u-ca-japanese-tz-usnyc"}, {"en@timezone=US/Eastern", "en-u-tz-usnyc", "en-u-tz-usnyc"}, {"en@x=x-y-z;a=a-b-c", "en-x-x-y-z", NULL}, {"it@collation=badcollationtype;colStrength=identical;cu=usd-eur", "it-u-ks-identic", NULL}, {"en_US_POSIX", "en-US-u-va-posix", "en-US-u-va-posix"}, {"en_US_POSIX@calendar=japanese;currency=EUR","en-US-u-ca-japanese-cu-EUR-va-posix", "en-US-u-ca-japanese-cu-EUR-va-posix"}, {"@x=elmer", "x-elmer", "x-elmer"}, {"en@x=elmer", "en-x-elmer", "en-x-elmer"}, {"@x=elmer;a=exta", "und-a-exta-x-elmer", "und-a-exta-x-elmer"}, {"en_US@attribute=attr1-attr2;calendar=gregorian", "en-US-u-attr1-attr2-ca-gregory", "en-US-u-attr1-attr2-ca-gregory"}, {NULL, NULL, NULL} }; static void TestToLanguageTag(void) { char langtag[256]; int32_t i; UErrorCode status; int32_t len; const char *inloc; const char *expected; for (i = 0; locale_to_langtag[i][0] != NULL; i++) { inloc = locale_to_langtag[i][0]; /* testing non-strict mode */ status = U_ZERO_ERROR; langtag[0] = 0; expected = locale_to_langtag[i][1]; len = uloc_toLanguageTag(inloc, langtag, sizeof(langtag), FALSE, &status); if (U_FAILURE(status)) { if (expected != NULL) { log_err("Error returned by uloc_toLanguageTag for locale id [%s] - error: %s\n", inloc, u_errorName(status)); } } else { if (expected == NULL) { log_err("Error should be returned by uloc_toLanguageTag for locale id [%s], but [%s] is returned without errors\n", inloc, langtag); } else if (uprv_strcmp(langtag, expected) != 0) { log_data_err("uloc_toLanguageTag returned language tag [%s] for input locale [%s] - expected: [%s]. Are you missing data?\n", langtag, inloc, expected); } } /* testing strict mode */ status = U_ZERO_ERROR; langtag[0] = 0; expected = locale_to_langtag[i][2]; len = uloc_toLanguageTag(inloc, langtag, sizeof(langtag), TRUE, &status); if (U_FAILURE(status)) { if (expected != NULL) { log_data_err("Error returned by uloc_toLanguageTag {strict} for locale id [%s] - error: %s Are you missing data?\n", inloc, u_errorName(status)); } } else { if (expected == NULL) { log_err("Error should be returned by uloc_toLanguageTag {strict} for locale id [%s], but [%s] is returned without errors\n", inloc, langtag); } else if (uprv_strcmp(langtag, expected) != 0) { log_err("uloc_toLanguageTag {strict} returned language tag [%s] for input locale [%s] - expected: [%s]\n", langtag, inloc, expected); } } } } #define FULL_LENGTH -1 static const struct { const char *bcpID; const char *locID; int32_t len; } langtag_to_locale[] = { {"en", "en", FULL_LENGTH}, {"en-us", "en_US", FULL_LENGTH}, {"und-US", "_US", FULL_LENGTH}, {"und-latn", "_Latn", FULL_LENGTH}, {"en-US-posix", "en_US_POSIX", FULL_LENGTH}, {"de-de_euro", "de", 2}, {"kok-IN", "kok_IN", FULL_LENGTH}, {"123", "", 0}, {"en_us", "", 0}, {"en-latn-x", "en_Latn", 7}, {"art-lojban", "jbo", FULL_LENGTH}, {"zh-hakka", "hak", FULL_LENGTH}, {"zh-cmn-CH", "cmn_CH", FULL_LENGTH}, {"xxx-yy", "xxx_YY", FULL_LENGTH}, {"fr-234", "fr_234", FULL_LENGTH}, {"i-default", "en@x=i-default", FULL_LENGTH}, {"i-test", "", 0}, {"ja-jp-jp", "ja_JP", 5}, {"bogus", "bogus", FULL_LENGTH}, {"boguslang", "", 0}, {"EN-lATN-us", "en_Latn_US", FULL_LENGTH}, {"und-variant-1234", "__VARIANT_1234", FULL_LENGTH}, {"und-varzero-var1-vartwo", "__VARZERO", 11}, {"en-u-ca-gregory", "en@calendar=gregorian", FULL_LENGTH}, {"en-U-cu-USD", "en@currency=usd", FULL_LENGTH}, {"en-US-u-va-posix", "en_US_POSIX", FULL_LENGTH}, {"en-us-u-ca-gregory-va-posix", "en_US_POSIX@calendar=gregorian", FULL_LENGTH}, {"en-us-posix-u-va-posix", "en_US_POSIX@va=posix", FULL_LENGTH}, {"en-us-u-va-posix2", "en_US@va=posix2", FULL_LENGTH}, {"en-us-vari1-u-va-posix", "en_US_VARI1@va=posix", FULL_LENGTH}, {"ar-x-1-2-3", "ar@x=1-2-3", FULL_LENGTH}, {"fr-u-nu-latn-cu-eur", "fr@currency=eur;numbers=latn", FULL_LENGTH}, {"de-k-kext-u-co-phonebk-nu-latn", "de@collation=phonebook;k=kext;numbers=latn", FULL_LENGTH}, {"ja-u-cu-jpy-ca-jp", "ja@calendar=yes;currency=jpy;jp=yes", FULL_LENGTH}, {"en-us-u-tz-usnyc", "en_US@timezone=America/New_York", FULL_LENGTH}, {"und-a-abc-def", "und@a=abc-def", FULL_LENGTH}, {"zh-u-ca-chinese-x-u-ca-chinese", "zh@calendar=chinese;x=u-ca-chinese", FULL_LENGTH}, {"x-elmer", "@x=elmer", FULL_LENGTH}, {"en-US-u-attr1-attr2-ca-gregory", "en_US@attribute=attr1-attr2;calendar=gregorian", FULL_LENGTH}, {"sr-u-kn", "sr@colnumeric=yes", FULL_LENGTH}, {"de-u-kn-co-phonebk", "de@collation=phonebook;colnumeric=yes", FULL_LENGTH}, {"en-u-attr2-attr1-kn-kb", "en@attribute=attr1-attr2;colbackwards=yes;colnumeric=yes", FULL_LENGTH}, {"ja-u-ijkl-efgh-abcd-ca-japanese-xx-yyy-zzz-kn", "ja@attribute=abcd-efgh-ijkl;calendar=japanese;colnumeric=yes;xx=yyy-zzz", FULL_LENGTH}, {"de-u-xc-xphonebk-co-phonebk-ca-buddhist-mo-very-lo-extensi-xd-that-de-should-vc-probably-xz-killthebuffer", "de@calendar=buddhist;collation=phonebook;de=should;lo=extensi;mo=very;vc=probably;xc=xphonebk;xd=that;xz=yes", 91}, {NULL, NULL, 0} }; static void TestForLanguageTag(void) { char locale[256]; int32_t i; UErrorCode status; int32_t parsedLen; int32_t expParsedLen; for (i = 0; langtag_to_locale[i].bcpID != NULL; i++) { status = U_ZERO_ERROR; locale[0] = 0; expParsedLen = langtag_to_locale[i].len; if (expParsedLen == FULL_LENGTH) { expParsedLen = uprv_strlen(langtag_to_locale[i].bcpID); } uloc_forLanguageTag(langtag_to_locale[i].bcpID, locale, sizeof(locale), &parsedLen, &status); if (U_FAILURE(status)) { log_err_status(status, "Error returned by uloc_forLanguageTag for language tag [%s] - error: %s\n", langtag_to_locale[i].bcpID, u_errorName(status)); } else { if (uprv_strcmp(langtag_to_locale[i].locID, locale) != 0) { log_err("uloc_forLanguageTag returned locale [%s] for input language tag [%s] - expected: [%s]\n", locale, langtag_to_locale[i].bcpID, langtag_to_locale[i].locID); } if (parsedLen != expParsedLen) { log_err("uloc_forLanguageTag parsed length of %d for input language tag [%s] - expected parsed length: %d\n", parsedLen, langtag_to_locale[i].bcpID, expParsedLen); } } } } static void test_unicode_define(const char *namech, char ch, const char *nameu, UChar uch) { UChar asUch[1]; asUch[0]=0; log_verbose("Testing whether %s[\\x%02x,'%c'] == %s[U+%04X]\n", namech, ch,(int)ch, nameu, (int) uch); u_charsToUChars(&ch, asUch, 1); if(asUch[0] != uch) { log_err("FAIL: %s[\\x%02x,'%c'] maps to U+%04X, but %s = U+%04X\n", namech, ch, (int)ch, (int)asUch[0], nameu, (int)uch); } else { log_verbose(" .. OK, == U+%04X\n", (int)asUch[0]); } } #define TEST_UNICODE_DEFINE(x,y) test_unicode_define(#x, (char)(x), #y, (UChar)(y)) static void TestUnicodeDefines(void) { TEST_UNICODE_DEFINE(ULOC_KEYWORD_SEPARATOR, ULOC_KEYWORD_SEPARATOR_UNICODE); TEST_UNICODE_DEFINE(ULOC_KEYWORD_ASSIGN, ULOC_KEYWORD_ASSIGN_UNICODE); TEST_UNICODE_DEFINE(ULOC_KEYWORD_ITEM_SEPARATOR, ULOC_KEYWORD_ITEM_SEPARATOR_UNICODE); }