/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2007, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************** * * File CNUMTST.C * * Madhu Katragadda Creation * * Modification History: * * Date Name Description * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes * 07/15/99 helena Ported to HPUX 10/11 CC. ********************************************************************************* */ /* C API TEST FOR NUMBER FORMAT */ #include "unicode/utypes.h" #if !UCONFIG_NO_FORMATTING #include "unicode/uloc.h" #include "unicode/unum.h" #include "unicode/ustring.h" #include "cintltst.h" #include "cnumtst.h" #include "cmemory.h" #include "putilimp.h" #define LENGTH(arr) (sizeof(arr)/sizeof(arr[0])) void addNumForTest(TestNode** root); static void TestTextAttributeCrash(void); #define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x) void addNumForTest(TestNode** root) { TESTCASE(TestNumberFormat); TESTCASE(TestSignificantDigits); TESTCASE(TestNumberFormatPadding); TESTCASE(TestInt64Format); TESTCASE(TestNonExistentCurrency); TESTCASE(TestCurrencyRegression); TESTCASE(TestTextAttributeCrash); TESTCASE(TestRBNFFormat); } /** copy src to dst with unicode-escapes for values < 0x20 and > 0x7e, null terminate if possible */ static int32_t ustrToAstr(const UChar* src, int32_t srcLength, char* dst, int32_t dstLength) { static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char *p = dst; const char *e = p + dstLength; if (srcLength < 0) { const UChar* s = src; while (*s) { ++s; } srcLength = (int32_t)(s - src); } while (p < e && --srcLength >= 0) { UChar c = *src++; if (c == 0xd || c == 0xa || c == 0x9 || (c>= 0x20 && c <= 0x7e)) { *p++ = (char) c & 0x7f; } else if (e - p >= 6) { *p++ = '\\'; *p++ = 'u'; *p++ = hex[(c >> 12) & 0xf]; *p++ = hex[(c >> 8) & 0xf]; *p++ = hex[(c >> 4) & 0xf]; *p++ = hex[c & 0xf]; } else { break; } } if (p < e) { *p = 0; } return (int32_t)(p - dst); } /* test Number Format API */ static void TestNumberFormat() { UChar *result=NULL; UChar temp1[512]; UChar temp2[512]; UChar temp[5]; UChar prefix[5]; UChar suffix[5]; UChar symbol[20]; int32_t resultlength; int32_t resultlengthneeded; int32_t parsepos; double d1 = -1.0; int32_t l1; double d = -10456.37; double a = 1234.56, a1 = 1235.0; int32_t l = 100000000; UFieldPosition pos1; UFieldPosition pos2; int32_t numlocales; int32_t i; UNumberFormatAttribute attr; UNumberFormatSymbol symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; int32_t newvalue; UErrorCode status=U_ZERO_ERROR; UNumberFormatStyle style= UNUM_DEFAULT; UNumberFormat *pattern; UNumberFormat *def, *fr, *cur_def, *cur_fr, *per_def, *per_fr, *cur_frpattern, *myclone, *spellout_def; /* Testing unum_open() with various Numberformat styles and locales*/ status = U_ZERO_ERROR; log_verbose("Testing unum_open() with default style and locale\n"); def=unum_open(style, NULL,0,NULL, NULL,&status); /* Might as well pack it in now if we can't even get a default NumberFormat... */ if(U_FAILURE(status)) { log_err("Error in creating default NumberFormat using unum_open(): %s\n", myErrorName(status)); return; } log_verbose("\nTesting unum_open() with french locale and default style(decimal)\n"); fr=unum_open(style,NULL,0, "fr_FR",NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat (french): %s\n", myErrorName(status)); log_verbose("\nTesting unum_open(currency,NULL,status)\n"); style=UNUM_CURRENCY; /* Can't hardcode the result to assume the default locale is "en_US". */ cur_def=unum_open(style, NULL,0,"en_US", NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using \n unum_open(currency, NULL, &status) %s\n", myErrorName(status) ); log_verbose("\nTesting unum_open(currency, frenchlocale, status)\n"); cur_fr=unum_open(style,NULL,0, "fr_FR", NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using unum_open(currency, french, &status): %s\n", myErrorName(status)); log_verbose("\nTesting unum_open(percent, NULL, status)\n"); style=UNUM_PERCENT; per_def=unum_open(style,NULL,0, NULL,NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using unum_open(percent, NULL, &status): %s\n", myErrorName(status)); log_verbose("\nTesting unum_open(percent,frenchlocale, status)\n"); per_fr=unum_open(style, NULL,0,"fr_FR", NULL,&status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using unum_open(percent, french, &status): %s\n", myErrorName(status)); log_verbose("\nTesting unum_open(spellout, NULL, status)"); style=UNUM_SPELLOUT; spellout_def=unum_open(style, NULL, 0, "en_US", NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using unum_open(spellout, NULL, &status): %s\n", myErrorName(status)); /* Testing unum_clone(..) */ log_verbose("\nTesting unum_clone(fmt, status)"); status = U_ZERO_ERROR; myclone = unum_clone(def,&status); if(U_FAILURE(status)) log_err("Error: could not clone unum_clone(def, &status): %s\n", myErrorName(status)); else { log_verbose("unum_clone() successful\n"); } /*Testing unum_getAvailable() and unum_countAvailable()*/ log_verbose("\nTesting getAvailableLocales and countAvailable()\n"); numlocales=unum_countAvailable(); if(numlocales < 0) log_err("error in countAvailable"); else{ log_verbose("unum_countAvialable() successful\n"); log_verbose("The no: of locales where number formattting is applicable is %d\n", numlocales); } for(i=0;i 0) { for (i = 0; i < len && temp[i] != ';'; ++i){}; if (i < len) { buffer[i] = 0; unum_setTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, -1, &status); if (U_FAILURE(status)) { log_err("unexpected error setting default ruleset: '%s'\n", u_errorName(status)); } else { int len2 = unum_getTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, BUFSIZE, &status); if (U_FAILURE(status)) { log_err("could not fetch default ruleset: '%s'\n", u_errorName(status)); } else if (len2 != i) { u_austrcpy(temp, buffer); log_err("unexpected ruleset len: %d ex: %d val: %s\n", len2, i, temp); } else { for (i = 0; i < sizeof(vals)/sizeof(vals[0]); ++i) { status = U_ZERO_ERROR; unum_formatDouble(fmt, vals[i], buffer, BUFSIZE, NULL, &status); if (U_FAILURE(status)) { log_err("failed to format: %g, returned %s\n", vals[i], u_errorName(status)); } else { u_austrcpy(temp, buffer); log_verbose("formatting %g returned '%s'\n", vals[i], temp); } } } } } } } } { UErrorCode status = U_ZERO_ERROR; unum_toPattern(fmt, FALSE, buffer, BUFSIZE, &status); if (U_SUCCESS(status)) { u_austrcpy(temp, buffer); log_verbose("pattern: '%s'\n", temp); } else if (status != U_BUFFER_OVERFLOW_ERROR) { log_err("toPattern failed unexpectedly: %s\n", u_errorName(status)); } else { log_verbose("pattern too long to display\n"); } } { UErrorCode status = U_ZERO_ERROR; int len = unum_getSymbol(fmt, UNUM_CURRENCY_SYMBOL, buffer, BUFSIZE, &status); if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) { log_err("unexpected error getting symbol: '%s'\n", u_errorName(status)); } unum_setSymbol(fmt, UNUM_CURRENCY_SYMBOL, buffer, len, &status); if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) { log_err("unexpected error setting symbol: '%s'\n", u_errorName(status)); } } } static void TestNonExistentCurrency() { UNumberFormat *format; UErrorCode status = U_ZERO_ERROR; UChar currencySymbol[8]; static const UChar QQQ[] = {0x51, 0x51, 0x51, 0}; /* Get a non-existent currency and make sure it returns the correct currency code. */ format = unum_open(UNUM_CURRENCY, NULL, 0, "th_TH@currency=QQQ", NULL, &status); if (format == NULL || U_FAILURE(status)) { log_err("unum_open did not return expected result for non-existent requested currency: '%s'\n", u_errorName(status)); } else { unum_getSymbol(format, UNUM_CURRENCY_SYMBOL, currencySymbol, sizeof(currencySymbol)/sizeof(currencySymbol[0]), &status); if (u_strcmp(currencySymbol, QQQ) != 0) { log_err("unum_open set the currency to QQQ\n"); } } unum_close(format); } static void TestRBNFFormat() { UErrorCode status; UParseError perr; UChar pat[1024]; UChar tempUChars[512]; UNumberFormat *formats[5]; int COUNT = sizeof(formats)/sizeof(formats[0]); int i; for (i = 0; i < COUNT; ++i) { formats[i] = 0; } /* instantiation */ status = U_ZERO_ERROR; u_uastrcpy(pat, "#,##0.0#;(#,##0.0#)"); formats[0] = unum_open(UNUM_PATTERN_DECIMAL, pat, -1, "en_US", &perr, &status); if (U_FAILURE(status)) { log_err("unable to open decimal pattern"); } status = U_ZERO_ERROR; formats[1] = unum_open(UNUM_SPELLOUT, NULL, 0, "en_US", &perr, &status); if (U_FAILURE(status)) { log_err("unable to open spellout"); } status = U_ZERO_ERROR; formats[2] = unum_open(UNUM_ORDINAL, NULL, 0, "en_US", &perr, &status); if (U_FAILURE(status)) { log_err("unable to open ordinal"); } status = U_ZERO_ERROR; formats[3] = unum_open(UNUM_DURATION, NULL, 0, "en_US", &perr, &status); if (U_FAILURE(status)) { log_err("unable to open duration"); } status = U_ZERO_ERROR; u_uastrcpy(pat, "%standard:\n" "-x: minus >>;\n" "x.x: << point >>;\n" "zero; one; two; three; four; five; six; seven; eight; nine;\n" "ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n" "seventeen; eighteen; nineteen;\n" "20: twenty[->>];\n" "30: thirty[->>];\n" "40: forty[->>];\n" "50: fifty[->>];\n" "60: sixty[->>];\n" "70: seventy[->>];\n" "80: eighty[->>];\n" "90: ninety[->>];\n" "100: =#,##0=;\n"); u_uastrcpy(tempUChars, "%simple:\n" "=%standard=;\n" "20: twenty[ and change];\n" "30: thirty[ and change];\n" "40: forty[ and change];\n" "50: fifty[ and change];\n" "60: sixty[ and change];\n" "70: seventy[ and change];\n" "80: eighty[ and change];\n" "90: ninety[ and change];\n" "100: =#,##0=;\n" "%bogus:\n" "0.x: tiny;\n" "x.x: << point something;\n" "=%standard=;\n" "20: some reasonable number;\n" "100: some substantial number;\n" "100,000,000: some huge number;\n"); /* This is to get around some compiler warnings about char * string length. */ u_strcat(pat, tempUChars); formats[4] = unum_open(UNUM_PATTERN_RULEBASED, pat, -1, "en_US", &perr, &status); if (U_FAILURE(status)) { log_err("unable to open rulebased pattern"); } for (i = 0; i < COUNT; ++i) { log_verbose("\n\ntesting format %d\n", i); test_fmt(formats[i], (UBool)(i == 0)); } for (i = 0; i < COUNT; ++i) { unum_close(formats[i]); } } static void TestCurrencyRegression(void) { /* I've found a case where unum_parseDoubleCurrency is not doing what I expect. The value I pass in is $1234567890q123460000.00 and this returns with a status of zero error & a parse pos of 22 (I would expect a parse error at position 11). I stepped into DecimalFormat::subparse() and it looks like it parses the first 10 digits and then stops parsing at the q but doesn't set an error. Then later in DecimalFormat::parse() the value gets crammed into a long (which greatly truncates the value). This is very problematic for me 'cause I try to remove chars that are invalid but this allows my users to enter bad chars and truncates their data! */ UChar buf[1024]; UChar currency[8]; char acurrency[16]; double d; UNumberFormat *cur; int32_t pos; UErrorCode status = U_ZERO_ERROR; const int32_t expected = 11; currency[0]=0; u_uastrcpy(buf, "$1234567890q643210000.00"); cur = unum_open(UNUM_CURRENCY, NULL,0,"en_US", NULL, &status); if(U_FAILURE(status)) { log_err("unum_open failed: %s\n", u_errorName(status)); return; } status = U_ZERO_ERROR; /* so we can test it later. */ pos = 0; d = unum_parseDoubleCurrency(cur, buf, -1, &pos, /* 0 = start */ currency, &status); u_austrcpy(acurrency, currency); if(U_FAILURE(status) || (pos != expected)) { log_err("unum_parseDoubleCurrency should have failed with pos %d, but gave: value %.9f, err %s, pos=%d, currency [%s]\n", expected, d, u_errorName(status), pos, acurrency); } else { log_verbose("unum_parseDoubleCurrency failed, value %.9f err %s, pos %d, currency [%s]\n", d, u_errorName(status), pos, acurrency); } unum_close(cur); } static void TestTextAttributeCrash(void) { UChar ubuffer[64] = {0x0049,0x004E,0x0052,0}; static const UChar expectedNeg[] = {0x0049,0x004E,0x0052,0x0031,0x0032,0x0033,0x0034,0x002E,0x0035,0}; static const UChar expectedPos[] = {0x0031,0x0032,0x0033,0x0034,0x002E,0x0035,0}; int32_t used; UErrorCode status = U_ZERO_ERROR; UNumberFormat *nf = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status); if (U_FAILURE(status)) { log_err("FAILED 1\n"); return; } unum_setTextAttribute(nf, UNUM_CURRENCY_CODE, ubuffer, 3, &status); /* * the usual negative prefix and suffix seem to be '($' and ')' at this point * also crashes if UNUM_NEGATIVE_SUFFIX is substituted for UNUM_NEGATIVE_PREFIX here */ used = unum_getTextAttribute(nf, UNUM_NEGATIVE_PREFIX, ubuffer, 64, &status); unum_setTextAttribute(nf, UNUM_NEGATIVE_PREFIX, ubuffer, used, &status); if (U_FAILURE(status)) { log_err("FAILED 2\n"); exit(1); } log_verbose("attempting to format...\n"); used = unum_formatDouble(nf, -1234.5, ubuffer, 64, NULL, &status); if (U_FAILURE(status) || 64 < used) { log_err("Failed formatting %s\n", u_errorName(status)); return; } if (u_strcmp(expectedNeg, ubuffer) == 0) { log_err("Didn't get expected negative result\n"); } used = unum_formatDouble(nf, 1234.5, ubuffer, 64, NULL, &status); if (U_FAILURE(status) || 64 < used) { log_err("Failed formatting %s\n", u_errorName(status)); return; } if (u_strcmp(expectedPos, ubuffer) == 0) { log_err("Didn't get expected positive result\n"); } unum_close(nf); } #endif /* #if !UCONFIG_NO_FORMATTING */