diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 38756a41d2e..79866fd53ce 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -11,6 +11,7 @@ #include "numfmtst.h" #include "unicode/dcfmtsym.h" #include "unicode/decimfmt.h" +#include "unicode/currency.h" #include // ***************************************************************************** @@ -42,6 +43,8 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n CASE(12,TestSurrogateSupport); CASE(13,TestAPI); + CASE(14,TestCurrencyObject); + default: name = ""; break; } } @@ -431,6 +434,89 @@ NumberFormatTest::TestCurrency(void) // ------------------------------------- +/** + * Test the Currency object handling, new as of ICU 2.2. + */ +void NumberFormatTest::TestCurrencyObject() { + UErrorCode ec = U_ZERO_ERROR; + NumberFormat* fmt = + NumberFormat::createCurrencyInstance(Locale::getUS(), ec); + + if (U_FAILURE(ec)) { + errln("FAIL: getCurrencyInstance(US)"); + delete fmt; + return; + } + + Locale null("", "", ""); + + expectCurrency(*fmt, null, 1234.56, "$1,234.56"); + + expectCurrency(*fmt, Locale::getFrance(), + 1234.56, CharsToUnicodeString("\\u20AC1,234.56")); // Euro + + expectCurrency(*fmt, Locale::getJapan(), + 1234.56, CharsToUnicodeString("\\uFFE51,235")); // Yen + + expectCurrency(*fmt, Locale("fr", "CH", ""), + 1234.56, "CHF1,234.50"); // 0.25 rounding + + expectCurrency(*fmt, Locale::getUS(), + 1234.56, "$1,234.56"); + + delete fmt; + fmt = NumberFormat::createCurrencyInstance(Locale::getFrance(), ec); + + if (U_FAILURE(ec)) { + errln("FAIL: getCurrencyInstance(FRANCE)"); + delete fmt; + return; + } + + expectCurrency(*fmt, null, 1234.56, CharsToUnicodeString("1 234,56 \\u20AC")); + + expectCurrency(*fmt, Locale::getJapan(), + 1234.56, CharsToUnicodeString("1 235 \\uFFE5")); // Yen + + expectCurrency(*fmt, Locale("fr", "CH", ""), + 1234.56, "1 234,50 CHF"); // 0.25 rounding + + expectCurrency(*fmt, Locale::getUS(), + 1234.56, "1 234,56 USD"); + + expectCurrency(*fmt, Locale::getFrance(), + 1234.56, CharsToUnicodeString("1 234,56 \\u20AC")); // Euro + + delete fmt; +} + +void NumberFormatTest::expectCurrency(NumberFormat& nf, const Locale& locale, + double value, const UnicodeString& string) { + DecimalFormat& fmt = * (DecimalFormat*) &nf; + UnicodeString curr(""); + if (*locale.getLanguage() != 0) { + UErrorCode ec = U_ZERO_ERROR; + curr = UCurrency::forLocale(locale, ec); + if (U_FAILURE(ec)) { + errln("FAIL: UCurrency::forLocale"); + return; + } + fmt.setCurrency(curr); + } + UnicodeString s; + fmt.format(value, s); + s.findAndReplace((UChar32)0x00A0, (UChar32)0x0020); + + if (s == string) { + logln((UnicodeString)"Ok: " + value + " x " + curr + " => " + prettify(s)); + } else { + errln((UnicodeString)"FAIL: " + value + " x " + curr + " => " + prettify(s) + + ", expected " + prettify(string)); + } +} + +// ------------------------------------- + /** * Do rudimentary testing of parsing. */ diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index af35a49c236..db31e73c126 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -48,6 +48,15 @@ public: * Test localized currency patterns. */ virtual void TestCurrency(void); + + /** + * Test the Currency object handling, new as of ICU 2.2. + */ + void TestCurrencyObject(void); + + void expectCurrency(NumberFormat& nf, const Locale& locale, + double value, const UnicodeString& string); + /** * Do rudimentary testing of parsing. */