1 /********************************************************************
3 * Copyright (c) 1997-2006, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ********************************************************************/
6 /*******************************************************************************
10 * Madhu Katragadda Creation
11 * Modification History:
13 * Date Name Description
14 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
15 *******************************************************************************
18 /* C DEPTH TEST FOR NUMBER FORMAT */
20 #include "unicode/utypes.h"
22 #if !UCONFIG_NO_FORMATTING
24 #include "unicode/ucurr.h"
25 #include "unicode/uloc.h"
26 #include "unicode/unum.h"
27 #include "unicode/ustring.h"
33 #define CHECK(status,str) if (U_FAILURE(status)) { log_err("FAIL: %s\n", str); return; }
35 void addNumFrDepTest(TestNode
** root
);
36 static void TestCurrencyPreEuro(void);
37 static void TestCurrencyObject(void);
39 void addNumFrDepTest(TestNode
** root
)
41 addTest(root
, &TestPatterns
, "tsformat/cnmdptst/TestPatterns");
42 addTest(root
, &TestQuotes
, "tsformat/cnmdptst/TestQuotes");
43 addTest(root
, &TestExponential
, "tsformat/cnmdptst/TestExponential");
44 addTest(root
, &TestCurrencySign
, "tsformat/cnmdptst/TestCurrencySign");
45 addTest(root
, &TestCurrency
, "tsformat/cnmdptst/TestCurrency");
46 addTest(root
, &TestCurrencyPreEuro
, "tsformat/cnmdptst/TestCurrencyPreEuro");
47 addTest(root
, &TestCurrencyObject
, "tsformat/cnmdptst/TestCurrencyObject");
48 addTest(root
, &TestRounding487
, "tsformat/cnmdptst/TestRounding487");
49 addTest(root
, &TestDoubleAttribute
, "tsformat/cnmdptst/TestDoubleAttribute");
50 addTest(root
, &TestSecondaryGrouping
, "tsformat/cnmdptst/TestSecondaryGrouping");
51 addTest(root
, &TestCurrencyKeywords
, "tsformat/cnmdptst/TestCurrencyKeywords");
54 /*Test Various format patterns*/
55 static void TestPatterns(void)
57 int32_t pat_length
, i
, lneed
;
64 UErrorCode status
= U_ZERO_ERROR
;
65 const char* pat
[] = { "#.#", "#.", ".#", "#" };
66 const char* newpat
[] = { "#0.#", "#0.", "#.0", "#" };
67 const char* num
[] = { "0", "0.", ".0", "0" };
69 log_verbose("\nTesting different format patterns\n");
70 pat_length
= sizeof(pat
) / sizeof(pat
[0]);
71 for (i
=0; i
< pat_length
; ++i
)
73 status
= U_ZERO_ERROR
;
74 u_uastrcpy(upat
, pat
[i
]);
75 fmt
= unum_open(UNUM_IGNORE
,upat
, u_strlen(upat
), "en_US",NULL
, &status
);
76 if (U_FAILURE(status
)) {
77 log_err("FAIL: Number format constructor failed for pattern %s\n", pat
[i
]);
81 lneed
=unum_toPattern(fmt
, FALSE
, NULL
, lneed
, &status
);
82 if(status
==U_BUFFER_OVERFLOW_ERROR
){
84 unewp
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
85 unum_toPattern(fmt
, FALSE
, unewp
, lneed
+1, &status
);
87 if(U_FAILURE(status
)){
88 log_err("FAIL: Number format extracting the pattern failed for %s\n", pat
[i
]);
90 u_uastrcpy(unewpat
, newpat
[i
]);
91 if(u_strcmp(unewp
, unewpat
) != 0)
92 log_err("FAIL: Pattern %s should be transmute to %s; %s seen instead\n", pat
[i
], newpat
[i
], austrdup(unewp
) );
95 lneed
=unum_format(fmt
, 0, NULL
, lneed
, NULL
, &status
);
96 if(status
==U_BUFFER_OVERFLOW_ERROR
){
98 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
99 unum_format(fmt
, 0, str
, lneed
+1, NULL
, &status
);
101 if(U_FAILURE(status
)) {
102 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
104 u_uastrcpy(unum
, num
[i
]);
105 if (u_strcmp(str
, unum
) != 0)
107 log_err("FAIL: Pattern %s should format zero as %s; %s Seen instead\n", pat
[i
], num
[i
], austrdup(str
) );
116 /* Test the handling of quotes*/
117 static void TestQuotes(void)
120 UErrorCode status
=U_ZERO_ERROR
;
126 log_verbose("\nTestting the handling of quotes in number format\n");
127 u_uastrcpy(pat
, "a'fo''o'b#");
128 fmt
=unum_open(UNUM_IGNORE
,pat
, u_strlen(pat
), "en_US",NULL
, &status
);
129 if(U_FAILURE(status
)){
130 log_err("Error in number format costruction using pattern \"a'fo''o'b#\"\n");
133 lneed
=unum_format(fmt
, 123, NULL
, lneed
, NULL
, &status
);
134 if(status
==U_BUFFER_OVERFLOW_ERROR
){
136 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
137 unum_format(fmt
, 123, str
, lneed
+1, NULL
, &status
);
139 if(U_FAILURE(status
) || !str
) {
140 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
143 log_verbose("Pattern \"%s\" \n", u_austrcpy(tempBuf
, pat
) );
144 log_verbose("Format 123 -> %s\n", u_austrcpy(tempBuf
, str
) );
145 u_uastrcpy(res
, "afo'ob123");
146 if(u_strcmp(str
, res
) != 0)
147 log_err("FAIL: Expected afo'ob123");
154 u_uastrcpy(pat
, "a''b#");
157 fmt
=unum_open(UNUM_IGNORE
,pat
, u_strlen(pat
), "en_US",NULL
, &status
);
158 if(U_FAILURE(status
)){
159 log_err("Error in number format costruction using pattern \"a''b#\"\n");
162 lneed
=unum_format(fmt
, 123, NULL
, lneed
, NULL
, &status
);
163 if(status
==U_BUFFER_OVERFLOW_ERROR
){
165 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
166 unum_format(fmt
, 123, str
, lneed
+1, NULL
, &status
);
168 if(U_FAILURE(status
)) {
169 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
171 log_verbose("Pattern \"%s\" \n", u_austrcpy(tempBuf
, pat
) );
172 log_verbose("Format 123 -> %s\n", u_austrcpy(tempBuf
, str
) );
174 u_uastrcpy(res
, "a'b123");
175 if(u_strcmp(str
, res
) != 0)
176 log_err("FAIL: Expected a'b123\n");
182 /* Test exponential pattern*/
183 static void TestExponential(void)
185 int32_t pat_length
, val_length
, lval_length
;
186 int32_t ival
, ilval
, p
, v
, lneed
;
192 UChar uvalfor
[20], ulvalfor
[20];
193 char tempMsgBug
[256];
195 UErrorCode status
= U_ZERO_ERROR
;
197 static const double val
[] = { 0.01234, 123456789, 1.23e75
, -3.141592653e-78 };
199 static const double val
[] = { 0.01234, 123456789, 1.23e300
, -3.141592653e-271 };
201 static const char* pat
[] = { "0.####E0", "00.000E00", "##0.######E000", "0.###E0;[0.###E0]" };
202 static const int32_t lval
[] = { 0, -1, 1, 123456789 };
204 static const char* valFormat
[] =
206 "1.234E-2", "1.2346E8", "1.23E300", "-3.1416E-271",
207 "12.340E-03", "12.346E07", "12.300E299", "-31.416E-272",
208 "12.34E-003", "123.4568E006", "1.23E300", "-314.1593E-273",
209 "1.234E-2", "1.235E8", "1.23E300", "[3.142E-271]"
211 static const char* lvalFormat
[] =
213 "0E0", "-1E0", "1E0", "1.2346E8",
214 "00.000E00", "-10.000E-01", "10.000E-01", "12.346E07",
215 "0E000", "-1E000", "1E000", "123.4568E006",
216 "0E0", "[1E0]", "1E0", "1.235E8"
218 static const double valParse
[] =
221 0.01234, 123460000, 1.23E75
, -3.1416E-78,
222 0.01234, 123460000, 1.23E75
, -3.1416E-78,
223 0.01234, 123456800, 1.23E75
, -3.141593E-78,
224 0.01234, 123500000, 1.23E75
, -3.142E-78
226 /* We define the whole IEEE 754 number in the 4th column because
227 Visual Age 7 has a bug in rounding numbers. */
228 0.01234, 123460000, 1.23E300
, -3.1415999999999999E-271,
229 0.01234, 123460000, 1.23E300
, -3.1415999999999999E-271,
230 0.01234, 123456800, 1.23E300
, -3.1415929999999999E-271,
231 0.01234, 123500000, 1.23E300
, -3.1420000000000001E-271
234 static const int32_t lvalParse
[] =
243 pat_length
= sizeof(pat
) / sizeof(pat
[0]);
244 val_length
= sizeof(val
) / sizeof(val
[0]);
245 lval_length
= sizeof(lval
) / sizeof(lval
[0]);
248 for (p
=0; p
< pat_length
; ++p
)
250 upat
=(UChar
*)malloc(sizeof(UChar
) * (strlen(pat
[p
])+1) );
251 u_uastrcpy(upat
, pat
[p
]);
252 fmt
=unum_open(UNUM_IGNORE
,upat
, u_strlen(upat
), "en_US",NULL
, &status
);
253 if (U_FAILURE(status
)) {
254 log_err("FAIL: Bad status returned by Number format construction with pattern %s\n, pat[i]");
257 lneed
= u_strlen(upat
) + 1;
258 unum_toPattern(fmt
, FALSE
, pattern
, lneed
, &status
);
259 log_verbose("Pattern \" %s \" -toPattern-> \" %s \" \n", upat
, u_austrcpy(tempMsgBug
, pattern
) );
260 for (v
=0; v
<val_length
; ++v
)
264 lneed
=unum_formatDouble(fmt
, val
[v
], NULL
, lneed
, NULL
, &status
);
265 if(status
==U_BUFFER_OVERFLOW_ERROR
){
267 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
268 unum_formatDouble(fmt
, val
[v
], str
, lneed
+1, NULL
, &status
);
270 if(U_FAILURE(status
)) {
271 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
276 u_uastrcpy(uvalfor
, valFormat
[v
+ival
]);
277 if(u_strcmp(str
, uvalfor
) != 0)
278 log_verbose("FAIL: Expected %s ( %s )\n", valFormat
[v
+ival
], u_austrcpy(tempMsgBug
, uvalfor
) );
282 a
=unum_parseDouble(fmt
, str
, u_strlen(str
), &ppos
, &status
);
283 if (ppos
== u_strlen(str
)) {
284 if (a
!= valParse
[v
+ival
])
285 log_err("FAIL: Expected: %e, Got: %g\n", valParse
[v
+ival
], a
);
288 log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos
, a
);
292 for (v
=0; v
<lval_length
; ++v
)
296 lneed
=unum_formatDouble(fmt
, lval
[v
], NULL
, lneed
, NULL
, &status
);
297 if(status
==U_BUFFER_OVERFLOW_ERROR
){
299 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
300 unum_formatDouble(fmt
, lval
[v
], str
, lneed
+1, NULL
, &status
);
302 if(U_FAILURE(status
)) {
303 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
305 /*printf(" Format %e -> %s\n", lval[v], austrdup(str) );*/
306 u_uastrcpy(ulvalfor
, lvalFormat
[v
+ilval
]);
307 if(u_strcmp(str
, ulvalfor
) != 0)
308 log_err("FAIL: Expected %s ( %s )\n", valFormat
[v
+ilval
], austrdup(ulvalfor
) );
312 a
=unum_parseDouble(fmt
, str
, u_strlen(str
), &ppos
, &status
);
313 if (ppos
== u_strlen(str
)) {
314 /*printf(" Parse -> %e\n", a);*/
315 if (a
!= lvalParse
[v
+ilval
])
316 log_err("FAIL: Expected : %e\n", valParse
[v
+ival
]);
319 log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos
, a
);
325 ilval
+= lval_length
;
332 * Test the handling of the currency symbol in patterns.
334 static void TestCurrencySign(void)
342 UErrorCode status
= U_ZERO_ERROR
;
345 pattern
=(UChar
*)malloc(sizeof(UChar
) * (strlen("*#,##0.00;-*#,##0.00") + 1) );
346 u_uastrcpy(pattern
, "*#,##0.00;-*#,##0.00");
347 pattern
[0]=pattern
[11]=0xa4; /* insert latin-1 currency symbol */
348 fmt
= unum_open(UNUM_IGNORE
,pattern
, u_strlen(pattern
), "en_US",NULL
, &status
);
349 if(U_FAILURE(status
)){
350 log_err("Error in number format construction with pattern \"\\xA4#,##0.00;-\\xA4#,##0.00\\\" \n");
353 lneed
=unum_formatDouble(fmt
, 1234.56, NULL
, lneed
, NULL
, &status
);
354 if(status
==U_BUFFER_OVERFLOW_ERROR
){
356 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
357 unum_formatDouble(fmt
, 1234.56, str
, lneed
+1, NULL
, &status
);
359 if(U_FAILURE(status
)) {
360 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
363 lneed
=unum_toPattern(fmt
, FALSE
, NULL
, lneed
, &status
);
364 if(status
==U_BUFFER_OVERFLOW_ERROR
){
366 pat
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
367 unum_formatDouble(fmt
, FALSE
, pat
, lneed
+1, NULL
, &status
);
369 log_verbose("Pattern \" %s \" \n", u_austrcpy(tempBuf
, pat
));
370 log_verbose("Format 1234.56 -> %s\n", u_austrcpy(tempBuf
, str
) );
371 if(U_SUCCESS(status
) && str
) {
372 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen("$1,234.56")+1) );
373 u_uastrcpy(res
, "$1,234.56");
374 if (u_strcmp(str
, res
) !=0) log_err("FAIL: Expected $1,234.56\n");
376 log_err("Error formatting\n");
383 lneed
=unum_formatDouble(fmt
, -1234.56, NULL
, lneed
, NULL
, &status
);
384 if(status
==U_BUFFER_OVERFLOW_ERROR
){
386 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
387 unum_formatDouble(fmt
, -1234.56, str
, lneed
+1, NULL
, &status
);
389 if(U_FAILURE(status
)) {
390 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
393 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen("-$1,234.56")+1) );
394 u_uastrcpy(res
, "-$1,234.56");
395 if (u_strcmp(str
, res
) != 0) log_err("FAIL: Expected -$1,234.56\n");
405 * Test localized currency patterns.
407 static void TestCurrency(void)
409 UNumberFormat
*currencyFmt
;
414 UErrorCode status
= U_ZERO_ERROR
;
415 const char* locale
[]={"fr_CA", "de_DE_PREEURO", "fr_FR_PREEURO"};
416 const char* result
[]={"1,50 $", "1,50 DM", "1,50 F"};
417 log_verbose("\nTesting the number format with different currency patterns\n");
421 currencyFmt
= unum_open(UNUM_CURRENCY
, NULL
,0,locale
[i
],NULL
, &status
);
423 if(U_FAILURE(status
)){
424 log_err("Error in the construction of number format with style currency:\n%s\n",
425 myErrorName(status
));
428 lneed
= unum_formatDouble(currencyFmt
, 1.50, NULL
, lneed
, NULL
, &status
);
429 if(status
==U_BUFFER_OVERFLOW_ERROR
){
431 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
433 unum_formatDouble(currencyFmt
, 1.50, str
, lneed
+1, &pos
, &status
);
436 if(U_FAILURE(status
)) {
437 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
) );
439 u_charsToUChars(result
[i
], res
, (int32_t)strlen(result
[i
])+1);
441 if (u_strcmp(str
, res
) != 0){
442 log_err("FAIL: Expected %s Got: %s for locale: %s\n", result
[i
], aescstrdup(str
, -1), locale
[i
]);
447 unum_close(currencyFmt
);
452 * Test localized currency patterns for PREEURO variants.
454 static void TestCurrencyPreEuro(void)
456 UNumberFormat
*currencyFmt
;
457 UChar
*str
=NULL
, *res
=NULL
;
460 UErrorCode status
= U_ZERO_ERROR
;
462 const char* locale
[]={
463 "ca_ES_PREEURO", "de_LU_PREEURO", "en_IE_PREEURO", "fi_FI_PREEURO", "fr_LU_PREEURO", "it_IT_PREEURO",
464 "pt_PT_PREEURO", "de_AT_PREEURO", "el_GR_PREEURO", "es_ES_PREEURO", "fr_BE_PREEURO", "ga_IE_PREEURO",
465 "nl_BE_PREEURO", "de_DE_PREEURO", "en_BE_PREEURO", "eu_ES_PREEURO", "fr_FR_PREEURO", "gl_ES_PREEURO",
469 const char* result
[]={
470 "\\u20A7 2", "2 F", "\\u00A31.50", "1,50 mk", "2 F", "\\u20A4 2",
471 "1$50 Esc.", "\\u00F6S 1,50", "1,50 \\u0394\\u03C1\\u03C7", "2 \\u20A7", "1,50 FB", "\\u00a31.50",
472 "1,50 BF", "1,50 DM", "1,50 BF", "\\u20A7 2", "1,50 F", "\\u20A7 2",
476 log_verbose("\nTesting the number format with different currency patterns\n");
477 for(i
=0; i
< 19; i
++)
479 char curID
[256] = {0};
480 uloc_canonicalize(locale
[i
], curID
, 256, &status
);
481 if(U_FAILURE(status
)){
482 log_err("Could not canonicalize %s. Error: %s \n", locale
[i
], u_errorName(status
));
485 currencyFmt
= unum_open(UNUM_CURRENCY
, NULL
,0,curID
,NULL
, &status
);
487 if(U_FAILURE(status
)){
488 log_err("Error in the construction of number format with style currency:\n%s\n",
489 myErrorName(status
));
492 lneed
= unum_formatDouble(currencyFmt
, 1.50, NULL
, lneed
, NULL
, &status
);
494 if(status
==U_BUFFER_OVERFLOW_ERROR
){
496 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
498 unum_formatDouble(currencyFmt
, 1.50, str
, lneed
+1, &pos
, &status
);
501 if(U_FAILURE(status
)) {
502 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
) );
504 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen(result
[i
])+1) );
505 u_unescape(result
[i
],res
,(int32_t)(strlen(result
[i
])+1));
507 if (u_strcmp(str
, res
) != 0){
508 log_err("FAIL: Expected %s Got: %s for locale: %s\n", result
[i
],aescstrdup(str
, -1),locale
[i
]);
513 unum_close(currencyFmt
);
520 * Test currency "object" (we use this name to match the other C++
521 * test name and the Jave name). Actually, test ISO currency code
522 * support in the C API.
524 static void TestCurrencyObject(void)
526 UNumberFormat
*currencyFmt
;
527 UChar
*str
=NULL
, *res
=NULL
;
530 UErrorCode status
= U_ZERO_ERROR
;
532 const char* locale
[]={
537 const char* currency
[]={
542 const char* result
[]={
543 "1\\u00A0234,56 \\u20AC",
544 "1\\u00A0235 \\u00A5",
547 log_verbose("\nTesting the number format with different currency codes\n");
551 UChar isoCode
[16]={0};
552 currencyFmt
= unum_open(UNUM_CURRENCY
, NULL
,0,locale
[i
],NULL
, &status
);
553 if(U_FAILURE(status
)){
554 log_err("Error in the construction of number format with style currency:\n%s\n",
555 myErrorName(status
));
558 u_uastrcpy(isoCode
, currency
[i
]);
559 unum_setTextAttribute(currencyFmt
, UNUM_CURRENCY_CODE
,
560 isoCode
, u_strlen(isoCode
), &status
);
562 if(U_FAILURE(status
)) {
563 log_err("FAIL: can't set currency code %s\n", myErrorName(status
) );
567 unum_getTextAttribute(currencyFmt
, UNUM_CURRENCY_CODE
,
568 isoCode
, sizeof(isoCode
), &status
);
570 if(U_FAILURE(status
)) {
571 log_err("FAIL: can't get currency code %s\n", myErrorName(status
) );
574 u_UCharsToChars(isoCode
,cStr
,u_strlen(isoCode
));
575 log_verbose("ISO code %s\n", cStr
);
576 if (*currency
[i
] && uprv_strcmp(cStr
, currency
[i
])) {
577 log_err("FAIL: currency should be %s, but is %s\n", currency
[i
], cStr
);
581 lneed
= unum_formatDouble(currencyFmt
, 1234.56, NULL
, lneed
, NULL
, &status
);
582 if(status
==U_BUFFER_OVERFLOW_ERROR
){
584 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
586 unum_formatDouble(currencyFmt
, 1234.56, str
, lneed
+1, &pos
, &status
);
588 if(U_FAILURE(status
)) {
589 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
) );
591 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen(result
[i
])+1) );
592 u_unescape(result
[i
],res
, (int32_t)(strlen(result
[i
])+1));
593 if (u_strcmp(str
, res
) != 0){
594 log_err("FAIL: Expected %s Got: %s for locale: %s\n", result
[i
],aescstrdup(str
, -1),locale
[i
]);
599 unum_close(currencyFmt
);
606 * Test proper rounding by the format method.
608 static void TestRounding487(void)
611 UErrorCode status
= U_ZERO_ERROR
;
612 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
613 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
614 /* nnf = unum_open(UNUM_DEFAULT, NULL, &status); */
615 nnf
= unum_open(UNUM_DEFAULT
, NULL
,0,"en_US",NULL
, &status
);
617 if(U_FAILURE(status
)){
618 log_err("FAIL: failure in the construction of number format: %s\n", myErrorName(status
));
620 roundingTest(nnf
, 0.00159999, 4, "0.0016");
621 roundingTest(nnf
, 0.00995, 4, "0.01");
623 roundingTest(nnf
, 12.3995, 3, "12.4");
625 roundingTest(nnf
, 12.4999, 0, "12");
626 roundingTest(nnf
, - 19.5, 0, "-20");
632 /*-------------------------------------*/
634 static void roundingTest(UNumberFormat
* nf
, double x
, int32_t maxFractionDigits
, const char* expected
)
642 unum_setAttribute(nf
, UNUM_MAX_FRACTION_DIGITS
, maxFractionDigits
);
644 lneed
=unum_formatDouble(nf
, x
, NULL
, lneed
, NULL
, &status
);
645 if(status
==U_BUFFER_OVERFLOW_ERROR
){
647 out
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
649 unum_formatDouble(nf
, x
, out
, lneed
+1, &pos
, &status
);
651 if(U_FAILURE(status
)) {
652 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
) );
654 /*Need to use log_verbose here. Problem with the float*/
655 /*printf("%f format with %d fraction digits to %s\n", x, maxFractionDigits, austrdup(out) );*/
656 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen(expected
)+1) );
657 u_uastrcpy(res
, expected
);
658 if (u_strcmp(out
, res
) != 0)
659 log_err("FAIL: Expected: %s or %s\n", expected
, austrdup(res
) );
667 * Testing unum_getDoubleAttribute and unum_setDoubleAttribute()
669 static void TestDoubleAttribute(void)
671 double mydata
[] = { 1.11, 22.22, 333.33, 4444.44, 55555.55, 666666.66, 7777777.77, 88888888.88, 999999999.99};
674 UErrorCode status
=U_ZERO_ERROR
;
675 UNumberFormatAttribute attr
;
676 UNumberFormatStyle style
= UNUM_DEFAULT
;
679 log_verbose("\nTesting get and set DoubleAttributes\n");
680 def
=unum_open(style
, NULL
,0,NULL
,NULL
, &status
);
682 if (U_FAILURE(status
)) {
683 log_err("Fail: error creating a default number formatter\n");
685 attr
=UNUM_ROUNDING_INCREMENT
;
686 dvalue
=unum_getDoubleAttribute(def
, attr
);
687 for (i
= 0; i
<9 ; i
++)
690 unum_setDoubleAttribute(def
, attr
, dvalue
);
691 if(unum_getDoubleAttribute(def
,attr
)!=mydata
[i
])
692 log_err("Fail: error in setting and getting double attributes for UNUM_ROUNDING_INCREMENT\n");
694 log_verbose("Pass: setting and getting double attributes for UNUM_ROUNDING_INCREMENT works fine\n");
702 * Test the functioning of the secondary grouping value.
704 static void TestSecondaryGrouping(void) {
705 UErrorCode status
= U_ZERO_ERROR
;
706 UNumberFormat
*f
= NULL
, *g
= NULL
;
707 UNumberFormat
*us
= unum_open(UNUM_DECIMAL
,NULL
,0, "en_US", NULL
,&status
);
709 UChar resultBuffer
[512];
710 int32_t l
= 1876543210L;
714 UBool expectGroup
= FALSE
, isGroup
= FALSE
;
716 u_uastrcpy(buffer
, "#,##,###");
717 f
= unum_open(UNUM_IGNORE
,buffer
, -1, "en_US",NULL
, &status
);
718 CHECK(status
, "DecimalFormat ct");
721 unum_format(f
, (int32_t)123456789L, resultBuffer
, 512 , &pos
, &status
);
722 u_uastrcpy(buffer
, "12,34,56,789");
723 if ((u_strcmp(resultBuffer
, buffer
) != 0) || U_FAILURE(status
))
725 log_err("Fail: Formatting \"#,##,###\" pattern with 123456789 got %s, expected %s\n", resultBuffer
, "12,34,56,789");
727 if (pos
.beginIndex
!= 0 && pos
.endIndex
!= 12) {
728 log_err("Fail: Formatting \"#,##,###\" pattern pos = (%d, %d) expected pos = (0, 12)\n", pos
.beginIndex
, pos
.endIndex
);
730 memset(resultBuffer
,0, sizeof(UChar
)*512);
731 unum_toPattern(f
, FALSE
, resultBuffer
, 512, &status
);
732 u_uastrcpy(buffer
, "#,##,###");
733 if ((u_strcmp(resultBuffer
, buffer
) != 0) || U_FAILURE(status
))
735 log_err("Fail: toPattern() got %s, expected %s\n", resultBuffer
, "#,##,###");
737 memset(resultBuffer
,0, sizeof(UChar
)*512);
738 u_uastrcpy(buffer
, "#,###");
739 unum_applyPattern(f
, FALSE
, buffer
, -1,NULL
,NULL
);
740 if (U_FAILURE(status
))
742 log_err("Fail: applyPattern call failed\n");
744 unum_setAttribute(f
, UNUM_SECONDARY_GROUPING_SIZE
, 4);
745 unum_format(f
, (int32_t)123456789L, resultBuffer
, 512 , &pos
, &status
);
746 u_uastrcpy(buffer
, "12,3456,789");
747 if ((u_strcmp(resultBuffer
, buffer
) != 0) || U_FAILURE(status
))
749 log_err("Fail: Formatting \"#,###\" pattern with 123456789 got %s, expected %s\n", resultBuffer
, "12,3456,789");
751 memset(resultBuffer
,0, sizeof(UChar
)*512);
752 unum_toPattern(f
, FALSE
, resultBuffer
, 512, &status
);
753 u_uastrcpy(buffer
, "#,####,###");
754 if ((u_strcmp(resultBuffer
, buffer
) != 0) || U_FAILURE(status
))
756 log_err("Fail: toPattern() got %s, expected %s\n", resultBuffer
, "#,####,###");
758 memset(resultBuffer
,0, sizeof(UChar
)*512);
759 g
= unum_open(UNUM_DECIMAL
, NULL
,0,"hi_IN",NULL
, &status
);
760 if (U_FAILURE(status
))
762 log_err("Fail: Cannot create UNumberFormat for \"hi_IN\" locale.\n");
765 unum_format(g
, l
, resultBuffer
, 512, &pos
, &status
);
767 /* expect "1,87,65,43,210", but with Hindi digits */
769 if (u_strlen(resultBuffer
) != 14) {
772 for (i
=0; i
<u_strlen(resultBuffer
); ++i
) {
782 /* Later -- fix this to get the actual grouping */
783 /* character from the resource bundle. */
784 isGroup
= (UBool
)(resultBuffer
[i
] == 0x002C);
785 if (isGroup
!= expectGroup
) {
792 log_err("FAIL Expected %s x hi_IN -> \"1,87,65,43,210\" (with Hindi digits), got %s\n", "1876543210L", resultBuffer
);
798 static void TestCurrencyKeywords(void)
800 static const char *currencies
[] = {
801 "ADD", "ADP", "AED", "AFA", "AFN", "AIF", "ALK", "ALL", "ALV", "ALX", "AMD",
802 "ANG", "AOA", "AOK", "AON", "AOR", "AOS", "ARA", "ARM", "ARP", "ARS", "ATS",
803 "AUD", "AUP", "AWG", "AZM", "BAD", "BAM", "BAN", "BBD", "BDT", "BEC", "BEF",
804 "BEL", "BGL", "BGM", "BGN", "BGO", "BGX", "BHD", "BIF", "BMD", "BMP", "BND",
805 "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "BRL", "BRN", "BRR", "BRZ",
806 "BSD", "BSP", "BTN", "BTR", "BUK", "BUR", "BWP", "BYB", "BYL", "BYR", "BZD",
807 "BZH", "CAD", "CDF", "CDG", "CDL", "CFF", "CHF", "CKD", "CLC", "CLE", "CLF",
808 "CLP", "CMF", "CNP", "CNX", "CNY", "COB", "COF", "COP", "CRC", "CSC", "CSK",
809 "CUP", "CUX", "CVE", "CWG", "CYP", "CZK", "DDM", "DEM", "DES", "DJF", "DKK",
810 "DOP", "DZD", "DZF", "DZG", "ECS", "ECV", "EEK", "EGP", "ERN", "ESP", "ETB",
811 "ETD", "EUR", "FIM", "FIN", "FJD", "FJP", "FKP", "FOK", "FRF", "FRG", "GAF",
812 "GBP", "GEK", "GEL", "GHC", "GHO", "GHP", "GHR", "GIP", "GLK", "GMD", "GMP",
813 "GNF", "GNI", "GNS", "GPF", "GQE", "GQF", "GQP", "GRD", "GRN", "GTQ", "GUF",
814 "GWE", "GWM", "GWP", "GYD", "HKD", "HNL", "HRD", "HRK", "HTG", "HUF", "IBP",
815 "IDG", "IDJ", "IDN", "IDR", "IEP", "ILL", "ILP", "ILS", "IMP", "INR", "IQD",
816 "IRR", "ISK", "ITL", "JEP", "JMD", "JMP", "JOD", "JPY", "KES", "KGS", "KHO",
817 "KHR", "KID", "KMF", "KPP", "KPW", "KRH", "KRO", "KRW", "KWD", "KYD", "KZR",
818 "KZT", "LAK", "LBP", "LIF", "LKR", "LNR", "LRD", "LSL", "LTL", "LTT", "LUF",
819 "LVL", "LVR", "LYB", "LYD", "LYP", "MAD", "MAF", "MCF", "MCG", "MDC", "MDL",
820 "MDR", "MGA", "MGF", "MHD", "MKD", "MKN", "MLF", "MMK", "MMX", "MNT", "MOP",
821 "MQF", "MRO", "MTL", "MTP", "MUR", "MVP", "MVR", "MWK", "MWP", "MXN", "MXP",
822 "MXV", "MYR", "MZE", "MZM", "NAD", "NCF", "NGN", "NGP", "NHF", "NIC", "NIG",
823 "NIO", "NLG", "NOK", "NPR", "NZD", "NZP", "OMR", "OMS", "PAB", "PDK", "PDN",
824 "PDR", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLX", "PLZ", "PSP",
825 "PTC", "PTE", "PYG", "QAR", "REF", "ROL", "RON", "RUB", "RUR", "RWF", "SAR",
826 "SAS", "SBD", "SCR", "SDD", "SDP", "SEK", "SGD", "SHP", "SIB", "SIT", "SKK",
827 "SLL", "SML", "SOS", "SQS", "SRG", "SSP", "STD", "STE", "SUN", "SUR", "SVC",
828 "SYP", "SZL", "TCC", "TDF", "THB", "TJR", "TJS", "TMM", "TND", "TOP", "TOS",
829 "TPE", "TPP", "TRL", "TTD", "TTO", "TVD", "TWD", "TZS", "UAH", "UAK", "UGS",
830 "UGX", "USD", "USN", "USS", "UYF", "UYP", "UYU", "UZC", "UZS", "VAL", "VDD",
831 "VDN", "VDP", "VEB", "VGD", "VND", "VNN", "VNR", "VNS", "VUV", "WSP", "WST",
832 "XAD", "XAF", "XAM", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XCF", "XDR",
833 "XEF", "XEU", "XFO", "XFU", "XID", "XMF", "XNF", "XOF", "XPF", "XPS", "XSS",
834 "XTR", "YDD", "YEI", "YER", "YUD", "YUF", "YUG", "YUM", "YUN", "YUO", "YUR",
835 "ZAL", "ZAP", "ZAR", "ZMK", "ZMP", "ZRN", "ZRZ", "ZWD"
838 UErrorCode status
= U_ZERO_ERROR
;
839 int32_t i
= 0, j
= 0;
840 int32_t noLocales
= uloc_countAvailable();
844 UChar currBuffer
[256];
847 for(i
= 0; i
< noLocales
; i
++) {
848 strcpy(currLoc
, uloc_getAvailable(i
));
849 for(j
= 0; j
< sizeof(currencies
)/sizeof(currencies
[0]); j
++) {
850 strcpy(locale
, currLoc
);
851 strcat(locale
, "@currency=");
852 strcat(locale
, currencies
[j
]);
853 ucurr_forLocale(locale
, result
, 4, &status
);
854 u_charsToUChars(currencies
[j
], currBuffer
, 3);
856 if(u_strcmp(currBuffer
, result
) != 0) {
857 log_err("Didn't get the right currency for %s\n", locale
);
864 #endif /* #if !UCONFIG_NO_FORMATTING */