static void TestGetKeywordValuesForLocale(void) { #define PREFERRED_SIZE 13 #define MAX_NUMBER_OF_KEYWORDS 3 const char *PREFERRED[PREFERRED_SIZE][MAX_NUMBER_OF_KEYWORDS] = { { "root", "USD", NULL }, { "und", "USD", NULL }, { "und_ZZ", "USD", NULL }, { "en_US", "USD", NULL }, { "en_029", "USD", NULL }, { "en_TH", "THB", NULL }, { "de", "EUR", NULL }, { "de_DE", "EUR", NULL }, { "ar", "EGP", NULL }, { "ar_PS", "JOD", "ILS" }, { "en@currency=CAD", "USD", NULL }, { "fr@currency=zzz", "EUR", NULL }, { "de_DE@currency=DEM", "EUR", NULL }, }; const int32_t EXPECTED_SIZE[PREFERRED_SIZE] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1 }; UErrorCode status = U_ZERO_ERROR; int32_t i, j, size; UEnumeration *pref, *all; const char *loc = NULL; UBool matchPref, matchAll; const char *value = NULL; int32_t valueLength = 0; UList *ALLList = NULL; UEnumeration *ALL = ucurr_getKeywordValuesForLocale("currency", uloc_getDefault(), FALSE, &status); if (ALL == NULL) { log_err_status(status, "ERROR getting keyword value for default locale. -> %s\n", u_errorName(status)); return; } for (i = 0; i < PREFERRED_SIZE; i++) { pref = NULL; all = NULL; loc = PREFERRED[i][0]; pref = ucurr_getKeywordValuesForLocale("currency", loc, TRUE, &status); matchPref = FALSE; matchAll = FALSE; size = uenum_count(pref, &status); if (size == EXPECTED_SIZE[i]) { matchPref = TRUE; for (j = 0; j < size; j++) { if ((value = uenum_next(pref, &valueLength, &status)) != NULL && U_SUCCESS(status)) { if (uprv_strcmp(value, PREFERRED[i][j+1]) != 0) { log_err("ERROR: locale %s got keywords #%d %s expected %s\n", loc, j, value, PREFERRED[i][j+1]); matchPref = FALSE; break; } } else { matchPref = FALSE; log_err("ERROR getting keyword value for locale \"%s\"\n", loc); break; } } } else { log_err("FAIL: size of locale \"%s\" %d does not match expected size %d\n", loc, size, EXPECTED_SIZE[i]); } if (!matchPref) { log_err("FAIL: Preferred values for locale \"%s\" does not match expected.\n", loc); break; } uenum_close(pref); all = ucurr_getKeywordValuesForLocale("currency", loc, FALSE, &status); size = uenum_count(all, &status); if (U_SUCCESS(status) && size == uenum_count(ALL, &status)) { matchAll = TRUE; ALLList = ulist_getListFromEnum(ALL); for (j = 0; j < size; j++) { if ((value = uenum_next(all, &valueLength, &status)) != NULL && U_SUCCESS(status)) { if (!ulist_containsString(ALLList, value, uprv_strlen(value))) { log_err("Locale %s have %s not in ALL\n", loc, value); matchAll = FALSE; break; } } else { matchAll = FALSE; log_err("ERROR getting \"all\" keyword value for locale \"%s\"\n", loc); break; } } if (!matchAll) { log_err("FAIL: All values for locale \"%s\" does not match expected.\n", loc); } } else { if(U_FAILURE(status)) { log_err("ERROR: %s\n", u_errorName(status)); } else if(size!=uenum_count(ALL, &status)) { log_err("ERROR: got size of %d, wanted %d\n", size, uenum_count(ALL, &status)); } } uenum_close(all); } uenum_close(ALL); }
static void TestGetKeywordValuesForLocale(void) { #define PREFERRED_SIZE 15 #define MAX_NUMBER_OF_KEYWORDS 4 const char *PREFERRED[PREFERRED_SIZE][MAX_NUMBER_OF_KEYWORDS] = { { "root", "USD", "USN", NULL }, { "und", "USD", "USN", NULL }, /* { "und_ZZ", "USD", NULL, NULL }, -- temporarily remove as this locale now has 15 entries */ { "en_US", "USD", "USN", NULL }, { "en_029", "USD", "USN", NULL }, { "en_TH", "THB", NULL, NULL }, { "de", "EUR", NULL, NULL }, { "de_DE", "EUR", NULL, NULL }, { "ar", "EGP", NULL, NULL }, { "ar_PS", "ILS", "JOD", NULL }, { "en@currency=CAD", "USD", "USN", NULL }, { "fr@currency=zzz", "EUR", NULL, NULL }, { "de_DE@currency=DEM", "EUR", NULL, NULL }, { "en_US@rg=THZZZZ", "THB", NULL, NULL }, { "de@rg=USZZZZ", "USD", "USN", NULL }, { "en_US@currency=CAD;rg=THZZZZ", "THB", NULL, NULL }, }; const int32_t EXPECTED_SIZE[PREFERRED_SIZE] = { 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1 }; /* ucurr_forLocale results for same locales; "" if no result expected */ const char *FORLOCALE[PREFERRED_SIZE] = { "", "", "USD", "", "THB", "", "EUR", "", "ILS", "CAD", "ZZZ", "DEM", "THB", "USD", "CAD" }; UErrorCode status = U_ZERO_ERROR; int32_t i, j, size; UEnumeration *pref, *all; const char *loc = NULL; UBool matchPref, matchAll; const char *value = NULL; int32_t valueLength = 0; UList *ALLList = NULL; UEnumeration *ALL = ucurr_getKeywordValuesForLocale("currency", uloc_getDefault(), FALSE, &status); if (ALL == NULL) { log_err_status(status, "ERROR getting keyword value for default locale. -> %s\n", u_errorName(status)); return; } for (i = 0; i < PREFERRED_SIZE; i++) { UChar getCurrU[4]; int32_t getCurrLen; status = U_ZERO_ERROR; pref = NULL; all = NULL; loc = PREFERRED[i][0]; pref = ucurr_getKeywordValuesForLocale("currency", loc, TRUE, &status); matchPref = FALSE; matchAll = FALSE; size = uenum_count(pref, &status); if (size == EXPECTED_SIZE[i]) { matchPref = TRUE; for (j = 0; j < size; j++) { if ((value = uenum_next(pref, &valueLength, &status)) != NULL && U_SUCCESS(status)) { if (uprv_strcmp(value, PREFERRED[i][j+1]) != 0) { log_err("ERROR: locale %s got keywords #%d %s expected %s\n", loc, j, value, PREFERRED[i][j+1]); matchPref = FALSE; break; } } else { matchPref = FALSE; log_err("ERROR getting keyword value for locale \"%s\"\n", loc); break; } } } else { log_err("FAIL: size of locale \"%s\" %d does not match expected size %d\n", loc, size, EXPECTED_SIZE[i]); } if (!matchPref) { log_err("FAIL: Preferred values for locale \"%s\" does not match expected.\n", loc); break; } uenum_close(pref); all = ucurr_getKeywordValuesForLocale("currency", loc, FALSE, &status); size = uenum_count(all, &status); if (U_SUCCESS(status) && size == uenum_count(ALL, &status)) { matchAll = TRUE; ALLList = ulist_getListFromEnum(ALL); for (j = 0; j < size; j++) { if ((value = uenum_next(all, &valueLength, &status)) != NULL && U_SUCCESS(status)) { if (!ulist_containsString(ALLList, value, uprv_strlen(value))) { log_err("Locale %s have %s not in ALL\n", loc, value); matchAll = FALSE; break; } } else { matchAll = FALSE; log_err("ERROR getting \"all\" keyword value for locale \"%s\"\n", loc); break; } } if (!matchAll) { log_err("FAIL: All values for locale \"%s\" does not match expected.\n", loc); } } else { if(U_FAILURE(status)) { log_err("ERROR: %s\n", u_errorName(status)); } else if(size!=uenum_count(ALL, &status)) { log_err("ERROR: got size of %d, wanted %d\n", size, uenum_count(ALL, &status)); } } uenum_close(all); status = U_ZERO_ERROR; getCurrLen = ucurr_forLocale(loc, getCurrU, 4, &status); if(U_FAILURE(status)) { if (FORLOCALE[i][0] != 0) { log_err("ERROR: ucurr_forLocale %s, status %s\n", loc, u_errorName(status)); } } else if (getCurrLen != 3) { if (FORLOCALE[i][0] != 0 || getCurrLen != -1) { log_err("ERROR: ucurr_forLocale %s, returned len %d\n", loc, getCurrLen); } } else { char getCurrB[4]; u_UCharsToChars(getCurrU, getCurrB, 4); if ( uprv_strncmp(getCurrB, FORLOCALE[i], 4) != 0 ) { log_err("ERROR: ucurr_forLocale %s, expected %s, got %s\n", loc, FORLOCALE[i], getCurrB); } } } uenum_close(ALL); }
U_CAPI UEnumeration* U_EXPORT2 ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale, UBool commonlyUsed, UErrorCode *status) { // Resolve region char prefRegion[ULOC_FULLNAME_CAPACITY] = ""; int32_t prefRegionLength = 0; prefRegionLength = uloc_getCountry(locale, prefRegion, sizeof(prefRegion), status); if (prefRegionLength == 0) { char loc[ULOC_FULLNAME_CAPACITY] = ""; int32_t locLength = 0; locLength = uloc_addLikelySubtags(locale, loc, sizeof(loc), status); prefRegionLength = uloc_getCountry(loc, prefRegion, sizeof(prefRegion), status); } // Read preferred calendar values from supplementalData calendarPreference UResourceBundle *rb = ures_openDirect(NULL, "supplementalData", status); ures_getByKey(rb, "calendarPreferenceData", rb, status); UResourceBundle *order = ures_getByKey(rb, prefRegion, NULL, status); if (*status == U_MISSING_RESOURCE_ERROR && rb != NULL) { *status = U_ZERO_ERROR; order = ures_getByKey(rb, "001", NULL, status); } // Create a list of calendar type strings UList *values = NULL; if (U_SUCCESS(*status)) { values = ulist_createEmptyList(status); if (U_SUCCESS(*status)) { for (int i = 0; i < ures_getSize(order); i++) { int32_t len; const UChar *type = ures_getStringByIndex(order, i, &len, status); char *caltype = (char*)uprv_malloc(len + 1); if (caltype == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; break; } u_UCharsToChars(type, caltype, len); *(caltype + len) = 0; ulist_addItemEndList(values, caltype, TRUE, status); if (U_FAILURE(*status)) { break; } } if (U_SUCCESS(*status) && !commonlyUsed) { // If not commonlyUsed, add other available values for (int32_t i = 0; CAL_TYPES[i] != NULL; i++) { if (!ulist_containsString(values, CAL_TYPES[i], (int32_t)uprv_strlen(CAL_TYPES[i]))) { ulist_addItemEndList(values, CAL_TYPES[i], FALSE, status); if (U_FAILURE(*status)) { break; } } } } if (U_FAILURE(*status)) { ulist_deleteList(values); values = NULL; } } } ures_close(order); ures_close(rb); if (U_FAILURE(*status) || values == NULL) { return NULL; } // Create string enumeration UEnumeration *en = (UEnumeration*)uprv_malloc(sizeof(UEnumeration)); if (en == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; ulist_deleteList(values); return NULL; } ulist_resetList(values); memcpy(en, &defaultKeywordValues, sizeof(UEnumeration)); en->context = values; return en; }