mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-13 08:53:20 +00:00
ICU-1097 add test of unum currency API
X-SVN-Rev: 8637
This commit is contained in:
parent
5e02c2eb16
commit
dc028412eb
1 changed files with 85 additions and 0 deletions
|
@ -24,11 +24,13 @@
|
|||
#include "cintltst.h"
|
||||
#include "cnmdptst.h"
|
||||
#include "cmemory.h"
|
||||
#include "cstring.h"
|
||||
|
||||
#define CHECK(status,str) if (U_FAILURE(status)) { log_err("FAIL: %s\n", str); return; }
|
||||
|
||||
void addNumFrDepTest(TestNode** root);
|
||||
static void TestCurrencyPreEuro(void);
|
||||
static void TestCurrencyObject(void);
|
||||
|
||||
void addNumFrDepTest(TestNode** root)
|
||||
{
|
||||
|
@ -38,6 +40,7 @@ void addNumFrDepTest(TestNode** root)
|
|||
addTest(root, &TestCurrencySign, "tsformat/cnmdptst/TestCurrencySign");
|
||||
addTest(root, &TestCurrency, "tsformat/cnmdptst/TestCurrency");
|
||||
addTest(root, &TestCurrencyPreEuro, "tsformat/cnmdptst/TestCurrencyPreEuro");
|
||||
addTest(root, &TestCurrencyObject, "tsformat/cnmdptst/TestCurrencyObject");
|
||||
addTest(root, &TestRounding487, "tsformat/cnmdptst/TestRounding487");
|
||||
addTest(root, &TestDoubleAttribute, "tsformat/cnmdptst/TestDoubleAttribute");
|
||||
addTest(root, &TestSecondaryGrouping, "tsformat/cnmdptst/TestSecondaryGrouping");
|
||||
|
@ -487,6 +490,88 @@ static void TestCurrencyPreEuro(void)
|
|||
uprv_free(res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test currency "object" (we use this name to match the other C++
|
||||
* test name and the Jave name). Actually, test ISO currency code
|
||||
* support in the C API.
|
||||
*/
|
||||
static void TestCurrencyObject(void)
|
||||
{
|
||||
UNumberFormat *currencyFmt;
|
||||
UChar *str=NULL, *res=NULL;
|
||||
int32_t lneed, i;
|
||||
UFieldPosition pos;
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
|
||||
const char* locale[]={
|
||||
"fr_FR",
|
||||
"fr_FR",
|
||||
};
|
||||
|
||||
const char* currency[]={
|
||||
"",
|
||||
"JPY",
|
||||
};
|
||||
|
||||
const char* result[]={
|
||||
"1\\u00A0234,56 \\u20AC",
|
||||
"1\\u00A0235 \\uFFE5",
|
||||
};
|
||||
|
||||
log_verbose("\nTesting the number format with different currency codes\n");
|
||||
for(i=0; i < 2; i++)
|
||||
{
|
||||
char cStr[20]={0};
|
||||
UChar isoCode[16]={0};
|
||||
currencyFmt = unum_open(UNUM_CURRENCY, NULL,0,locale[i],NULL, &status);
|
||||
if(U_FAILURE(status)){
|
||||
log_err("Error in the construction of number format with style currency:\n%s\n",
|
||||
myErrorName(status));
|
||||
}
|
||||
if (*currency[i]) {
|
||||
u_uastrcpy(isoCode, currency[i]);
|
||||
unum_setTextAttribute(currencyFmt, UNUM_CURRENCY_CODE,
|
||||
isoCode, u_strlen(isoCode), &status);
|
||||
if(U_FAILURE(status)) {
|
||||
log_err("FAIL: can't set currency code %s\n", myErrorName(status) );
|
||||
}
|
||||
}
|
||||
unum_getTextAttribute(currencyFmt, UNUM_CURRENCY_CODE,
|
||||
isoCode, sizeof(isoCode), &status);
|
||||
if(U_FAILURE(status)) {
|
||||
log_err("FAIL: can't get currency code %s\n", myErrorName(status) );
|
||||
}
|
||||
u_UCharsToChars(isoCode,cStr,u_strlen(isoCode));
|
||||
log_verbose("ISO code %s\n", cStr);
|
||||
if (*currency[i] && uprv_strcmp(cStr, currency[i])) {
|
||||
log_err("FAIL: currency should be %s, but is %s\n", currency[i], cStr);
|
||||
}
|
||||
|
||||
lneed=0;
|
||||
lneed= unum_formatDouble(currencyFmt, 1234.56, NULL, lneed, NULL, &status);
|
||||
if(status==U_BUFFER_OVERFLOW_ERROR){
|
||||
status=U_ZERO_ERROR;
|
||||
str=(UChar*)uprv_malloc(sizeof(UChar) * (lneed+1) );
|
||||
pos.field = 0;
|
||||
unum_formatDouble(currencyFmt, 1234.56, str, lneed+1, &pos, &status);
|
||||
}
|
||||
if(U_FAILURE(status)) {
|
||||
log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
|
||||
}
|
||||
res=(UChar*)uprv_malloc(sizeof(UChar) * (strlen(result[i])+1) );
|
||||
u_unescape(result[i],res,strlen(result[i])+1);
|
||||
u_UCharsToChars(str,cStr,u_strlen(str));
|
||||
if (u_strcmp(str, res) != 0){
|
||||
log_err("FAIL: Expected %s Got: %s for locale: %s\n", result[i],cStr,locale[i]);
|
||||
}
|
||||
|
||||
unum_close(currencyFmt);
|
||||
uprv_free(str);
|
||||
uprv_free(res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test proper rounding by the format method.
|
||||
*/
|
||||
|
|
Loading…
Add table
Reference in a new issue