/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2001, International Business Machines Corporation and
+ * Copyright (c) 1997-2006, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/********************************************************************************
#include "cintltst.h"
#include "cnumtst.h"
#include "cmemory.h"
+#include "putilimp.h"
#define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
void addNumForTest(TestNode** root);
+#define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x)
+
void addNumForTest(TestNode** root)
{
- addTest(root, &TestNumberFormat, "tsformat/cnumtst/TestNumberFormat");
- addTest(root, &TestNumberFormatPadding, "tsformat/cnumtst/TestNumberFormatPadding");
+ TESTCASE(TestNumberFormat);
+ TESTCASE(TestSignificantDigits);
+ TESTCASE(TestNumberFormatPadding);
+ TESTCASE(TestInt64Format);
+ TESTCASE(TestNonExistentCurrency);
+ TESTCASE(TestCurrencyRegression);
+ TESTCASE(TestRBNFFormat);
}
/** copy src to dst with unicode-escapes for values < 0x20 and > 0x7e, null terminate if possible */
int32_t resultlength;
int32_t resultlengthneeded;
int32_t parsepos;
- double d1;
+ 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;
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 NumberFormat default using unum_open(): %s\n", myErrorName(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);
{
log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
}
- if(u_strcmp(result, temp1)==0)
+ if(result && u_strcmp(result, temp1)==0)
log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
else
log_err("FAIL: Error in number formatting using unum_formatDouble()\n");
/* Testing unum_parse() and unum_parseDouble() */
log_verbose("\nTesting unum_parseDouble()\n");
/* for (i = 0; i < 100000; i++)*/
+ if (result != NULL)
{
parsepos=0;
d1=unum_parseDouble(cur_def, result, u_strlen(result), &parsepos, &status);
}
+ else {
+ log_err("result is NULL\n");
+ }
if(U_FAILURE(status))
{
log_err("parse failed. The error is : %s\n", myErrorName(status));
log_err("Fail: Error in parsing\n");
else
log_verbose("Pass: parsing successful\n");
+ if (result)
+ free(result);
+ result = 0;
+
+
+ /* Testing unum_formatDoubleCurrency / unum_parseDoubleCurrency */
+ log_verbose("\nTesting unum_formatDoubleCurrency\n");
+ u_uastrcpy(temp1, "Y1,235");
+ temp1[0] = 0xA5; /* Yen sign */
+ u_uastrcpy(temp, "JPY");
+ resultlength=0;
+ pos2.field = 0; /* integer part */
+ resultlengthneeded=unum_formatDoubleCurrency(cur_def, a, temp, NULL, resultlength, &pos2, &status);
+ if (status==U_BUFFER_OVERFLOW_ERROR) {
+ status=U_ZERO_ERROR;
+ resultlength=resultlengthneeded+1;
+ result=(UChar*)malloc(sizeof(UChar) * resultlength);
+ unum_formatDoubleCurrency(cur_def, a, temp, result, resultlength, &pos2, &status);
+ }
+ if (U_FAILURE(status)) {
+ log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
+ }
+ if (result && u_strcmp(result, temp1)==0) {
+ log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
+ } else {
+ log_err("FAIL: Error in number formatting using unum_formatDouble()\n");
+ }
+ if (pos2.beginIndex == 1 && pos2.endIndex == 6) {
+ log_verbose("Pass: Complete number formatting using unum_format() successful\n");
+ } else {
+ log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=1 end=6\n",
+ pos1.beginIndex, pos1.endIndex);
+ }
+
+ log_verbose("\nTesting unum_parseDoubleCurrency\n");
+ parsepos=0;
+ if (result == NULL) {
+ log_err("result is NULL\n");
+ }
+ else {
+ d1=unum_parseDoubleCurrency(cur_def, result, u_strlen(result), &parsepos, temp2, &status);
+ if (U_FAILURE(status)) {
+ log_err("parse failed. The error is : %s\n", myErrorName(status));
+ }
+ /* Note: a==1234.56, but on parse expect a1=1235.0 */
+ if (d1!=a1) {
+ log_err("Fail: Error in parsing currency, got %f, expected %f\n", d1, a1);
+ } else {
+ log_verbose("Pass: parsed currency ammount successfully\n");
+ }
+ if (u_strcmp(temp2, temp)==0) {
+ log_verbose("Pass: parsed correct currency\n");
+ } else {
+ log_err("Fail: parsed incorrect currency\n");
+ }
+ }
+
+free(result);
+ result = 0;
+
/* performance testing */
u_uastrcpy(temp1, "$462.12345");
else
log_verbose("Pass: get and settextAttributes with negative suffix works fine\n");
-
-
-
/*Testing unum_getAttribute and unum_setAttribute() */
log_verbose("\nTesting get and set Attributes\n");
attr=UNUM_GROUPING_SIZE;
else
log_verbose("Pass:setting and getting attributes for UNUM_MULTIPLIER works fine\n");
+ attr=UNUM_SECONDARY_GROUPING_SIZE;
+ newvalue=unum_getAttribute(def, attr);
+ newvalue=2;
+ unum_setAttribute(def, attr, newvalue);
+ if(unum_getAttribute(def,attr) != 2)
+ log_err("error in setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE\n");
+ else
+ log_verbose("Pass:setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE works fine\n");
+
/*testing set and get Attributes extensively */
log_verbose("\nTesting get and set attributes extensively\n");
for(attr=UNUM_PARSE_INT_ONLY; attr<= UNUM_PADDING_POSITION; attr=(UNumberFormatAttribute)((int32_t)attr + 1) )
}
+static void TestSignificantDigits()
+{
+ UChar temp[128];
+ int32_t resultlengthneeded;
+ int32_t resultlength;
+ UErrorCode status = U_ZERO_ERROR;
+ UChar *result = NULL;
+ UNumberFormat* fmt;
+ double d = 123456.789;
+
+ u_uastrcpy(temp, "###0.0#");
+ fmt=unum_open(UNUM_IGNORE, temp, -1, NULL, NULL,&status);
+ if (U_FAILURE(status)) {
+ log_err("got unexpected error for unum_open: '%s'\n", u_errorName(status));
+ }
+ unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE);
+ unum_setAttribute(fmt, UNUM_MAX_SIGNIFICANT_DIGITS, 6);
+
+ u_uastrcpy(temp, "123457");
+ resultlength=0;
+ resultlengthneeded=unum_formatDouble(fmt, d, NULL, resultlength, NULL, &status);
+ if(status==U_BUFFER_OVERFLOW_ERROR)
+ {
+ status=U_ZERO_ERROR;
+ resultlength=resultlengthneeded+1;
+ result=(UChar*)malloc(sizeof(UChar) * resultlength);
+ unum_formatDouble(fmt, d, result, resultlength, NULL, &status);
+ }
+ if(U_FAILURE(status))
+ {
+ log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
+ return;
+ }
+ if(u_strcmp(result, temp)==0)
+ log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
+ else
+ log_err("FAIL: Error in number formatting using unum_formatDouble()\n");
+ free(result);
+ unum_close(fmt);
+}
+
static void TestNumberFormatPadding()
{
UChar *result=NULL;
unum_close(pattern);
}
+static UBool
+withinErr(double a, double b, double err) {
+ return uprv_fabs(a - b) < uprv_fabs(a * err);
+}
+
+static void TestInt64Format() {
+ UChar temp1[512];
+ UChar result[512];
+ UNumberFormat *fmt;
+ UErrorCode status = U_ZERO_ERROR;
+ const double doubleInt64Max = (double)U_INT64_MAX;
+ const double doubleInt64Min = (double)U_INT64_MIN;
+ const double doubleBig = 10.0 * (double)U_INT64_MAX;
+ int32_t val32;
+ int64_t val64;
+ double valDouble;
+ int32_t parsepos;
+
+ /* create a number format using unum_openPattern(....) */
+ log_verbose("\nTesting Int64Format\n");
+ u_uastrcpy(temp1, "#.#E0");
+ fmt = unum_open(UNUM_IGNORE, temp1, u_strlen(temp1), NULL, NULL, &status);
+ if(U_FAILURE(status)) {
+ log_err("error in unum_openPattern(): %s\n", myErrorName(status));
+ } else {
+ unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 20);
+ unum_formatInt64(fmt, U_INT64_MAX, result, 512, NULL, &status);
+ if (U_FAILURE(status)) {
+ log_err("error in unum_format(): %s\n", myErrorName(status));
+ } else {
+ log_verbose("format int64max: '%s'\n", result);
+ parsepos = 0;
+ val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
+ if (status != U_INVALID_FORMAT_ERROR) {
+ log_err("parse didn't report error: %s\n", myErrorName(status));
+ } else if (val32 != INT32_MAX) {
+ log_err("parse didn't pin return value, got: %d\n", val32);
+ }
+
+ status = U_ZERO_ERROR;
+ parsepos = 0;
+ val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
+ if (U_FAILURE(status)) {
+ log_err("parseInt64 returned error: %s\n", myErrorName(status));
+ } else if (val64 != U_INT64_MAX) {
+ log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
+ }
+
+ status = U_ZERO_ERROR;
+ parsepos = 0;
+ valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
+ if (U_FAILURE(status)) {
+ log_err("parseDouble returned error: %s\n", myErrorName(status));
+ } else if (valDouble != doubleInt64Max) {
+ log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
+ }
+ }
+
+ unum_formatInt64(fmt, U_INT64_MIN, result, 512, NULL, &status);
+ if (U_FAILURE(status)) {
+ log_err("error in unum_format(): %s\n", myErrorName(status));
+ } else {
+ log_verbose("format int64min: '%s'\n", result);
+ parsepos = 0;
+ val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
+ if (status != U_INVALID_FORMAT_ERROR) {
+ log_err("parse didn't report error: %s\n", myErrorName(status));
+ } else if (val32 != INT32_MIN) {
+ log_err("parse didn't pin return value, got: %d\n", val32);
+ }
+
+ status = U_ZERO_ERROR;
+ parsepos = 0;
+ val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
+ if (U_FAILURE(status)) {
+ log_err("parseInt64 returned error: %s\n", myErrorName(status));
+ } else if (val64 != U_INT64_MIN) {
+ log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
+ }
+
+ status = U_ZERO_ERROR;
+ parsepos = 0;
+ valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
+ if (U_FAILURE(status)) {
+ log_err("parseDouble returned error: %s\n", myErrorName(status));
+ } else if (valDouble != doubleInt64Min) {
+ log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
+ }
+ }
+
+ unum_formatDouble(fmt, doubleBig, result, 512, NULL, &status);
+ if (U_FAILURE(status)) {
+ log_err("error in unum_format(): %s\n", myErrorName(status));
+ } else {
+ log_verbose("format doubleBig: '%s'\n", result);
+ parsepos = 0;
+ val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
+ if (status != U_INVALID_FORMAT_ERROR) {
+ log_err("parse didn't report error: %s\n", myErrorName(status));
+ } else if (val32 != INT32_MAX) {
+ log_err("parse didn't pin return value, got: %d\n", val32);
+ }
+
+ status = U_ZERO_ERROR;
+ parsepos = 0;
+ val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
+ if (status != U_INVALID_FORMAT_ERROR) {
+ log_err("parseInt64 didn't report error error: %s\n", myErrorName(status));
+ } else if (val64 != U_INT64_MAX) {
+ log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
+ }
+
+ status = U_ZERO_ERROR;
+ parsepos = 0;
+ valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
+ if (U_FAILURE(status)) {
+ log_err("parseDouble returned error: %s\n", myErrorName(status));
+ } else if (!withinErr(valDouble, doubleBig, 1e-15)) {
+ log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
+ }
+ }
+ }
+ unum_close(fmt);
+}
+
+
+static void test_fmt(UNumberFormat* fmt, UBool isDecimal) {
+ char temp[512];
+ UChar buffer[512];
+ int32_t BUFSIZE = sizeof(buffer)/sizeof(buffer[0]);
+ double vals[] = {
+ -.2, 0, .2, 5.5, 15.2, 250, 123456789
+ };
+ int i;
+
+ for (i = 0; i < sizeof(vals)/sizeof(vals[0]); ++i) {
+ UErrorCode 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);
+ }
+ }
+
+ /* check APIs now */
+ {
+ UErrorCode status = U_ZERO_ERROR;
+ UParseError perr;
+ u_uastrcpy(buffer, "#,##0.0#");
+ unum_applyPattern(fmt, FALSE, buffer, -1, &perr, &status);
+ if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) {
+ log_err("got unexpected error for applyPattern: '%s'\n", u_errorName(status));
+ }
+ }
+
+ {
+ int isLenient = unum_getAttribute(fmt, UNUM_LENIENT_PARSE);
+ log_verbose("lenient: 0x%x\n", isLenient);
+ if (isDecimal ? (isLenient != -1) : (isLenient == TRUE)) {
+ log_err("didn't expect lenient value: %d\n", isLenient);
+ }
+
+ unum_setAttribute(fmt, UNUM_LENIENT_PARSE, TRUE);
+ isLenient = unum_getAttribute(fmt, UNUM_LENIENT_PARSE);
+ if (isDecimal ? (isLenient != -1) : (isLenient == FALSE)) {
+ log_err("didn't expect lenient value after set: %d\n", isLenient);
+ }
+ }
+
+ {
+ double val2;
+ double val = unum_getDoubleAttribute(fmt, UNUM_LENIENT_PARSE);
+ if (val != -1) {
+ log_err("didn't expect double attribute\n");
+ }
+ val = unum_getDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT);
+ if ((val == -1) == isDecimal) {
+ log_err("didn't expect -1 rounding increment\n");
+ }
+ unum_setDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT, val+.5);
+ val2 = unum_getDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT);
+ if (isDecimal && (val2 - val != .5)) {
+ log_err("set rounding increment had no effect on decimal format");
+ }
+ }
+
+ {
+ UErrorCode status = U_ZERO_ERROR;
+ int len = unum_getTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, BUFSIZE, &status);
+ if (isDecimal ? (status != U_UNSUPPORTED_ERROR) : U_FAILURE(status)) {
+ log_err("got unexpected error for get default ruleset: '%s'\n", u_errorName(status));
+ }
+ if (U_SUCCESS(status)) {
+ u_austrcpy(temp, buffer);
+ log_verbose("default ruleset: '%s'\n", temp);
+ }
+
+ status = U_ZERO_ERROR;
+ len = unum_getTextAttribute(fmt, UNUM_PUBLIC_RULESETS, buffer, BUFSIZE, &status);
+ if (isDecimal ? (status != U_UNSUPPORTED_ERROR) : U_FAILURE(status)) {
+ log_err("got unexpected error for get public rulesets: '%s'\n", u_errorName(status));
+ }
+ if (U_SUCCESS(status)) {
+ u_austrcpy(temp, buffer);
+ log_verbose("public rulesets: '%s'\n", temp);
+
+ /* set the default ruleset to the first one found, and retry */
+
+ if (len > 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);
+}
+
#endif /* #if !UCONFIG_NO_FORMATTING */