From dc028412eb5f283d68d11263f14c46cf929be040 Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Wed, 15 May 2002 21:38:37 +0000 Subject: [PATCH] ICU-1097 add test of unum currency API X-SVN-Rev: 8637 --- icu4c/source/test/cintltst/cnmdptst.c | 85 +++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/icu4c/source/test/cintltst/cnmdptst.c b/icu4c/source/test/cintltst/cnmdptst.c index 78dc79619a8..21cf899ff6b 100644 --- a/icu4c/source/test/cintltst/cnmdptst.c +++ b/icu4c/source/test/cintltst/cnmdptst.c @@ -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. */