StandardPlural::Form QuantityFormatter::selectPlural( const Formattable &number, const NumberFormat &fmt, const PluralRules &rules, UnicodeString &formattedNumber, FieldPosition &pos, UErrorCode &status) { if (U_FAILURE(status)) { return StandardPlural::OTHER; } UnicodeString pluralKeyword; const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt); if (decFmt != NULL) { number::impl::DecimalQuantity dq; decFmt->formatToDecimalQuantity(number, dq, status); if (U_FAILURE(status)) { return StandardPlural::OTHER; } pluralKeyword = rules.select(dq); decFmt->format(number, formattedNumber, pos, status); } else { if (number.getType() == Formattable::kDouble) { pluralKeyword = rules.select(number.getDouble()); } else if (number.getType() == Formattable::kLong) { pluralKeyword = rules.select(number.getLong()); } else if (number.getType() == Formattable::kInt64) { pluralKeyword = rules.select((double) number.getInt64()); } else { status = U_ILLEGAL_ARGUMENT_ERROR; return StandardPlural::OTHER; } fmt.format(number, formattedNumber, pos, status); } return StandardPlural::orOtherFromString(pluralKeyword); }
void PluralRulesTest::testGetSamples() { // TODO: fix samples, re-enable this test. // no get functional equivalent API in ICU4C, so just // test every locale... UErrorCode status = U_ZERO_ERROR; int32_t numLocales; const Locale* locales = Locale::getAvailableLocales(numLocales); double values[1000]; for (int32_t i = 0; U_SUCCESS(status) && i < numLocales; ++i) { PluralRules *rules = PluralRules::forLocale(locales[i], status); if (U_FAILURE(status)) { break; } StringEnumeration *keywords = rules->getKeywords(status); if (U_FAILURE(status)) { delete rules; break; } const UnicodeString* keyword; while (NULL != (keyword = keywords->snext(status))) { int32_t count = rules->getSamples(*keyword, values, LENGTHOF(values), status); if (U_FAILURE(status)) { errln(UNICODE_STRING_SIMPLE("getSamples() failed for locale ") + locales[i].getName() + UNICODE_STRING_SIMPLE(", keyword ") + *keyword); continue; } if (count == 0) { // TODO: Lots of these. // errln(UNICODE_STRING_SIMPLE("no samples for keyword ") + *keyword + UNICODE_STRING_SIMPLE(" in locale ") + locales[i].getName() ); } if (count > LENGTHOF(values)) { errln(UNICODE_STRING_SIMPLE("getSamples()=") + count + UNICODE_STRING_SIMPLE(", too many values, for locale ") + locales[i].getName() + UNICODE_STRING_SIMPLE(", keyword ") + *keyword); count = LENGTHOF(values); } for (int32_t j = 0; j < count; ++j) { if (values[j] == UPLRULES_NO_UNIQUE_VALUE) { errln("got 'no unique value' among values"); } else { UnicodeString resultKeyword = rules->select(values[j]); // if (strcmp(locales[i].getName(), "uk") == 0) { // Debug only. // std::cout << " uk " << US(resultKeyword).cstr() << " " << values[j] << std::endl; // } if (*keyword != resultKeyword) { errln("file %s, line %d, Locale %s, sample for keyword \"%s\": %g, select(%g) returns keyword \"%s\"", __FILE__, __LINE__, locales[i].getName(), US(*keyword).cstr(), values[j], values[j], US(resultKeyword).cstr()); } } } } delete keywords; delete rules; } }
UBool checkEqual(const PluralRules &test, char *result, int32_t max) { UnicodeString key; UBool isEqual = TRUE; for (int32_t i=0; i<max; ++i) { key= test.select(i); if ( key.charAt(0)!=result[i] ) { isEqual = FALSE; } } return isEqual; }
void PluralRulesTest::testWithin() { // goes to show you what lack of testing will do. // of course, this has been broken for two years and no one has noticed... UErrorCode status = U_ZERO_ERROR; PluralRules *rules = PluralRules::createRules("a: n mod 10 in 5..8", status); if (!rules) { errln("couldn't instantiate rules"); return; } UnicodeString keyword = rules->select((int32_t)26); if (keyword != "a") { errln("expected 'a' for 26 but didn't get it."); } keyword = rules->select(26.5); if (keyword != "other") { errln("expected 'other' for 26.5 but didn't get it."); } delete rules; }
UBool PluralRules::operator==(const PluralRules& other) const { int32_t limit; const UnicodeString *ptrKeyword; UErrorCode status= U_ZERO_ERROR; if ( this == &other ) { return TRUE; } LocalPointer<StringEnumeration> myKeywordList(getKeywords(status)); LocalPointer<StringEnumeration> otherKeywordList(other.getKeywords(status)); if (U_FAILURE(status)) { return FALSE; } if (myKeywordList->count(status)!=otherKeywordList->count(status)) { return FALSE; } myKeywordList->reset(status); while ((ptrKeyword=myKeywordList->snext(status))!=NULL) { if (!other.isKeyword(*ptrKeyword)) { return FALSE; } } otherKeywordList->reset(status); while ((ptrKeyword=otherKeywordList->snext(status))!=NULL) { if (!this->isKeyword(*ptrKeyword)) { return FALSE; } } if (U_FAILURE(status)) { return FALSE; } if ((limit=this->getRepeatLimit()) != other.getRepeatLimit()) { return FALSE; } UnicodeString myKeyword, otherKeyword; for (int32_t i=0; i<limit; ++i) { myKeyword = this->select(i); otherKeyword = other.select(i); if (myKeyword!=otherKeyword) { return FALSE; } } return TRUE; }
void PluralRulesTest::testAvailbleLocales() { // Hash set of (char *) strings. UErrorCode status = U_ZERO_ERROR; UHashtable *localeSet = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, uhash_compareLong, &status); uhash_setKeyDeleter(localeSet, uprv_deleteUObject); if (U_FAILURE(status)) { errln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status)); return; } // Check that each locale returned by the iterator is unique. StringEnumeration *localesEnum = PluralRules::getAvailableLocales(status); int localeCount = 0; for (;;) { const char *locale = localesEnum->next(NULL, status); if (U_FAILURE(status)) { dataerrln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status)); return; } if (locale == NULL) { break; } localeCount++; int32_t oldVal = uhash_puti(localeSet, new UnicodeString(locale), 1, &status); if (oldVal != 0) { errln("file %s, line %d: locale %s was seen before.", __FILE__, __LINE__, locale); } } // Reset the iterator, verify that we get the same count. localesEnum->reset(status); int32_t localeCount2 = 0; while (localesEnum->next(NULL, status) != NULL) { if (U_FAILURE(status)) { errln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status)); break; } localeCount2++; } if (localeCount != localeCount2) { errln("file %s, line %d: locale counts differ. They are (%d, %d)", __FILE__, __LINE__, localeCount, localeCount2); } // Instantiate plural rules for each available locale. localesEnum->reset(status); for (;;) { status = U_ZERO_ERROR; const char *localeName = localesEnum->next(NULL, status); if (U_FAILURE(status)) { errln("file %s, line %d: Error status = %s, locale = %s", __FILE__, __LINE__, u_errorName(status), localeName); return; } if (localeName == NULL) { break; } Locale locale = Locale::createFromName(localeName); PluralRules *pr = PluralRules::forLocale(locale, status); if (U_FAILURE(status)) { errln("file %s, line %d: Error %s creating plural rules for locale %s", __FILE__, __LINE__, u_errorName(status), localeName); continue; } if (pr == NULL) { errln("file %s, line %d: Null plural rules for locale %s", __FILE__, __LINE__, localeName); continue; } // Pump some numbers through the plural rules. Can't check for correct results, // mostly this to tickle any asserts or crashes that may be lurking. for (double n=0; n<120.0; n+=0.5) { UnicodeString keyword = pr->select(n); if (keyword.length() == 0) { errln("file %s, line %d, empty keyword for n = %g, locale %s", __FILE__, __LINE__, n, localeName); } } delete pr; } uhash_close(localeSet); delete localesEnum; }