mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-13 17:01:16 +00:00
ICU-10676 unum_open should support UNUM_CURRENCY_ISO, UNUM_CURRENCY_PLURAL styles
X-SVN-Rev: 35093
This commit is contained in:
parent
897a4241a5
commit
67551b09e9
2 changed files with 78 additions and 3 deletions
|
@ -52,6 +52,8 @@ unum_open( UNumberFormatStyle style,
|
|||
case UNUM_CURRENCY:
|
||||
case UNUM_PERCENT:
|
||||
case UNUM_SCIENTIFIC:
|
||||
case UNUM_CURRENCY_ISO:
|
||||
case UNUM_CURRENCY_PLURAL:
|
||||
retVal = NumberFormat::createInstance(Locale(locale), style, *status);
|
||||
break;
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
#include "cstring.h"
|
||||
#include "putilimp.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||
|
||||
|
@ -55,6 +56,7 @@ static void TestMaxInt(void);
|
|||
static void TestNoExponent(void);
|
||||
static void TestUFormattable(void);
|
||||
static void TestUNumberingSystem(void);
|
||||
static void TestCurrencyIsoPluralFormat(void);
|
||||
static void TestContext(void);
|
||||
|
||||
#define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x)
|
||||
|
@ -80,6 +82,7 @@ void addNumForTest(TestNode** root)
|
|||
TESTCASE(TestNoExponent);
|
||||
TESTCASE(TestUFormattable);
|
||||
TESTCASE(TestUNumberingSystem);
|
||||
TESTCASE(TestCurrencyIsoPluralFormat);
|
||||
TESTCASE(TestContext);
|
||||
}
|
||||
|
||||
|
@ -1074,8 +1077,6 @@ static void TestParseCurrency()
|
|||
log_data_err("unexpected error in unum_open UNUM_CURRENCY for locale %s: '%s'\n", itemPtr->locale, u_errorName(status));
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* Hmm, for UNUM_CURRENCY_PLURAL, currently unum_open always sets U_UNSUPPORTED_ERROR, save this test until it is supported */
|
||||
if (itemPtr->plurStr != NULL) {
|
||||
status = U_ZERO_ERROR;
|
||||
unum = unum_open(UNUM_CURRENCY_PLURAL, NULL, 0, itemPtr->locale, NULL, &status);
|
||||
|
@ -1106,7 +1107,6 @@ static void TestParseCurrency()
|
|||
log_data_err("unexpected error in unum_open UNUM_CURRENCY_PLURAL for locale %s: '%s'\n", itemPtr->locale, u_errorName(status));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2381,6 +2381,79 @@ static void TestUNumberingSystem(void) {
|
|||
}
|
||||
}
|
||||
|
||||
/* plain-C version of test in numfmtst.cpp */
|
||||
enum { kUBufMax = 64 };
|
||||
static void TestCurrencyIsoPluralFormat(void) {
|
||||
static const char* DATA[][6] = {
|
||||
// the data are:
|
||||
// locale,
|
||||
// currency amount to be formatted,
|
||||
// currency ISO code to be formatted,
|
||||
// format result using CURRENCYSTYLE,
|
||||
// format result using ISOCURRENCYSTYLE,
|
||||
// format result using PLURALCURRENCYSTYLE,
|
||||
|
||||
{"en_US", "1", "USD", "$1.00", "USD1.00", "1.00 US dollars"},
|
||||
{"en_US", "1234.56", "USD", "$1,234.56", "USD1,234.56", "1,234.56 US dollars"},
|
||||
{"en_US", "-1234.56", "USD", "-$1,234.56", "-USD1,234.56", "-1,234.56 US dollars"},
|
||||
{"zh_CN", "1", "USD", "US$\\u00A01.00", "USD\\u00A01.00", "1.00\\u7F8E\\u5143"},
|
||||
{"zh_CN", "1234.56", "USD", "US$\\u00A01,234.56", "USD\\u00A01,234.56", "1,234.56\\u7F8E\\u5143"},
|
||||
// wrong ISO code {"zh_CN", "1", "CHY", "CHY1.00", "CHY1.00", "1.00 CHY"},
|
||||
// wrong ISO code {"zh_CN", "1234.56", "CHY", "CHY1,234.56", "CHY1,234.56", "1,234.56 CHY"},
|
||||
{"zh_CN", "1", "CNY", "\\uFFE5\\u00A01.00", "CNY\\u00A01.00", "1.00\\u4EBA\\u6C11\\u5E01"},
|
||||
{"zh_CN", "1234.56", "CNY", "\\uFFE5\\u00A01,234.56", "CNY\\u00A01,234.56", "1,234.56\\u4EBA\\u6C11\\u5E01"},
|
||||
{"ru_RU", "1", "RUB", "1,00\\u00A0\\u0440\\u0443\\u0431.", "1,00\\u00A0RUB", "1,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E \\u0440\\u0443\\u0431\\u043B\\u044F"},
|
||||
{"ru_RU", "2", "RUB", "2,00\\u00A0\\u0440\\u0443\\u0431.", "2,00\\u00A0RUB", "2,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E \\u0440\\u0443\\u0431\\u043B\\u044F"},
|
||||
{"ru_RU", "5", "RUB", "5,00\\u00A0\\u0440\\u0443\\u0431.", "5,00\\u00A0RUB", "5,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E \\u0440\\u0443\\u0431\\u043B\\u044F"},
|
||||
// test locale without currency information
|
||||
{"root", "-1.23", "USD", "-US$\\u00A01.23", "-USD\\u00A01.23", "-1.23 USD"},
|
||||
// test choice format
|
||||
{"es_AR", "1", "INR", "INR1,00", "INR1,00", "1,00 rupia india"},
|
||||
};
|
||||
static const UNumberFormatStyle currencyStyles[] = {
|
||||
UNUM_CURRENCY,
|
||||
UNUM_CURRENCY_ISO,
|
||||
UNUM_CURRENCY_PLURAL
|
||||
};
|
||||
|
||||
int32_t i, sIndex;
|
||||
|
||||
for (i=0; i<LENGTH(DATA); ++i) {
|
||||
const char* localeString = DATA[i][0];
|
||||
double numberToBeFormat = atof(DATA[i][1]);
|
||||
const char* currencyISOCode = DATA[i][2];
|
||||
for (sIndex = 0; sIndex < LENGTH(currencyStyles); ++sIndex) {
|
||||
UNumberFormatStyle style = currencyStyles[sIndex];
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
UChar currencyCode[4];
|
||||
UChar ubufResult[kUBufMax];
|
||||
UChar ubufExpected[kUBufMax];
|
||||
int32_t ulenRes;
|
||||
|
||||
UNumberFormat* unumFmt = unum_open(style, NULL, 0, localeString, NULL, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", localeString, (int)style, myErrorName(status));
|
||||
continue;
|
||||
}
|
||||
u_charsToUChars(currencyISOCode, currencyCode, 4);
|
||||
unum_setTextAttribute(unumFmt, UNUM_CURRENCY_CODE, currencyCode, 3, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
log_err("FAIL: unum_setTextAttribute, locale %s, UNUM_CURRENCY_CODE %s\n", localeString, currencyISOCode);
|
||||
}
|
||||
ulenRes = unum_formatDouble(unumFmt, numberToBeFormat, ubufResult, kUBufMax, NULL, &status);
|
||||
if (U_FAILURE(status)) {
|
||||
log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s - %s\n", localeString, currencyISOCode, myErrorName(status));
|
||||
} else {
|
||||
int32_t ulenExp = u_unescape(DATA[i][3 + sIndex], ubufExpected, kUBufMax);
|
||||
if (ulenRes != ulenExp || u_strncmp(ubufResult, ubufExpected, ulenExp) != 0) {
|
||||
log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s, expected %s, got something else\n",
|
||||
localeString, currencyISOCode, DATA[i][3 + sIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void TestContext(void) {
|
||||
/* just a minimal sanity check for now */
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
|
|
Loading…
Add table
Reference in a new issue