1 /********************************************************************
3 * Copyright (c) 1997-2004, 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 double val
[] = { 0.01234, 123456789, 1.23e75
, -3.141592653e-78 };
199 double val
[] = { 0.01234, 123456789, 1.23e300
, -3.141592653e-271 };
201 const char* pat
[] = { "0.####E0", "00.000E00", "##0.######E000", "0.###E0;[0.###E0]" };
202 int32_t lval
[] = { 0, -1, 1, 123456789 };
204 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 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"
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 0.01234, 123460000, 1.23E300
, -3.1416E-271,
227 0.01234, 123460000, 1.23E300
, -3.1416E-271,
228 0.01234, 123456800, 1.23E300
, -3.141593E-271,
229 0.01234, 123500000, 1.23E300
, -3.142E-271
232 int32_t lvalParse
[] =
241 pat_length
= sizeof(pat
) / sizeof(pat
[0]);
242 val_length
= sizeof(val
) / sizeof(val
[0]);
243 lval_length
= sizeof(lval
) / sizeof(lval
[0]);
246 for (p
=0; p
< pat_length
; ++p
)
248 upat
=(UChar
*)malloc(sizeof(UChar
) * (strlen(pat
[p
])+1) );
249 u_uastrcpy(upat
, pat
[p
]);
250 fmt
=unum_open(UNUM_IGNORE
,upat
, u_strlen(upat
), "en_US",NULL
, &status
);
251 if (U_FAILURE(status
)) {
252 log_err("FAIL: Bad status returned by Number format construction with pattern %s\n, pat[i]");
255 lneed
= u_strlen(upat
) + 1;
256 unum_toPattern(fmt
, FALSE
, pattern
, lneed
, &status
);
257 log_verbose("Pattern \" %s \" -toPattern-> \" %s \" \n", upat
, u_austrcpy(tempMsgBug
, pattern
) );
258 for (v
=0; v
<val_length
; ++v
)
262 lneed
=unum_formatDouble(fmt
, val
[v
], NULL
, lneed
, NULL
, &status
);
263 if(status
==U_BUFFER_OVERFLOW_ERROR
){
265 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
266 unum_formatDouble(fmt
, val
[v
], str
, lneed
+1, NULL
, &status
);
268 if(U_FAILURE(status
)) {
269 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
274 u_uastrcpy(uvalfor
, valFormat
[v
+ival
]);
275 if(u_strcmp(str
, uvalfor
) != 0)
276 log_verbose("FAIL: Expected %s ( %s )\n", valFormat
[v
+ival
], u_austrcpy(tempMsgBug
, uvalfor
) );
280 a
=unum_parseDouble(fmt
, str
, u_strlen(str
), &ppos
, &status
);
281 if (ppos
== u_strlen(str
)) {
282 if (a
!= valParse
[v
+ival
])
283 log_err("FAIL: Expected: %e, Got: %g\n", valParse
[v
+ival
], a
);
286 log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos
, a
);
290 for (v
=0; v
<lval_length
; ++v
)
294 lneed
=unum_formatDouble(fmt
, lval
[v
], NULL
, lneed
, NULL
, &status
);
295 if(status
==U_BUFFER_OVERFLOW_ERROR
){
297 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
298 unum_formatDouble(fmt
, lval
[v
], str
, lneed
+1, NULL
, &status
);
300 if(U_FAILURE(status
)) {
301 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
303 /*printf(" Format %e -> %s\n", lval[v], austrdup(str) );*/
304 u_uastrcpy(ulvalfor
, lvalFormat
[v
+ilval
]);
305 if(u_strcmp(str
, ulvalfor
) != 0)
306 log_err("FAIL: Expected %s ( %s )\n", valFormat
[v
+ilval
], austrdup(ulvalfor
) );
310 a
=unum_parseDouble(fmt
, str
, u_strlen(str
), &ppos
, &status
);
311 if (ppos
== u_strlen(str
)) {
312 /*printf(" Parse -> %e\n", a);*/
313 if (a
!= lvalParse
[v
+ilval
])
314 log_err("FAIL: Expected : %e\n", valParse
[v
+ival
]);
317 log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos
, a
);
323 ilval
+= lval_length
;
330 * Test the handling of the currency symbol in patterns.
332 static void TestCurrencySign(void)
340 UErrorCode status
= U_ZERO_ERROR
;
343 pattern
=(UChar
*)malloc(sizeof(UChar
) * (strlen("*#,##0.00;-*#,##0.00") + 1) );
344 u_uastrcpy(pattern
, "*#,##0.00;-*#,##0.00");
345 pattern
[0]=pattern
[11]=0xa4; /* insert latin-1 currency symbol */
346 fmt
= unum_open(UNUM_IGNORE
,pattern
, u_strlen(pattern
), "en_US",NULL
, &status
);
347 if(U_FAILURE(status
)){
348 log_err("Error in number format construction with pattern \"\\xA4#,##0.00;-\\xA4#,##0.00\\\" \n");
351 lneed
=unum_formatDouble(fmt
, 1234.56, NULL
, lneed
, NULL
, &status
);
352 if(status
==U_BUFFER_OVERFLOW_ERROR
){
354 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
355 unum_formatDouble(fmt
, 1234.56, str
, lneed
+1, NULL
, &status
);
357 if(U_FAILURE(status
)) {
358 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
361 lneed
=unum_toPattern(fmt
, FALSE
, NULL
, lneed
, &status
);
362 if(status
==U_BUFFER_OVERFLOW_ERROR
){
364 pat
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
365 unum_formatDouble(fmt
, FALSE
, pat
, lneed
+1, NULL
, &status
);
367 log_verbose("Pattern \" %s \" \n", u_austrcpy(tempBuf
, pat
));
368 log_verbose("Format 1234.56 -> %s\n", u_austrcpy(tempBuf
, str
) );
369 if(U_SUCCESS(status
) && str
) {
370 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen("$1,234.56")+1) );
371 u_uastrcpy(res
, "$1,234.56");
372 if (u_strcmp(str
, res
) !=0) log_err("FAIL: Expected $1,234.56\n");
374 log_err("Error formatting\n");
381 lneed
=unum_formatDouble(fmt
, -1234.56, NULL
, lneed
, NULL
, &status
);
382 if(status
==U_BUFFER_OVERFLOW_ERROR
){
384 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
385 unum_formatDouble(fmt
, -1234.56, str
, lneed
+1, NULL
, &status
);
387 if(U_FAILURE(status
)) {
388 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
391 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen("-$1,234.56")+1) );
392 u_uastrcpy(res
, "-$1,234.56");
393 if (u_strcmp(str
, res
) != 0) log_err("FAIL: Expected -$1,234.56\n");
403 * Test localized currency patterns.
405 static void TestCurrency(void)
407 UNumberFormat
*currencyFmt
;
412 UErrorCode status
= U_ZERO_ERROR
;
413 const char* locale
[]={"fr_CA", "de_DE_PREEURO", "fr_FR_PREEURO"};
414 const char* result
[]={"1,50 $", "1,50 DM", "1,50 F"};
415 log_verbose("\nTesting the number format with different currency patterns\n");
419 currencyFmt
= unum_open(UNUM_CURRENCY
, NULL
,0,locale
[i
],NULL
, &status
);
420 if(U_FAILURE(status
)){
421 log_err("Error in the construction of number format with style currency:\n%s\n",
422 myErrorName(status
));
425 lneed
= unum_formatDouble(currencyFmt
, 1.50, NULL
, lneed
, NULL
, &status
);
426 if(status
==U_BUFFER_OVERFLOW_ERROR
){
428 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
430 unum_formatDouble(currencyFmt
, 1.50, str
, lneed
+1, &pos
, &status
);
432 if(U_FAILURE(status
)) {
433 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
) );
435 u_charsToUChars(result
[i
], res
, (int32_t)strlen(result
[i
])+1);
436 if (u_strcmp(str
, res
) != 0){
437 log_err("FAIL: Expected %s Got: %s for locale: %s\n", result
[i
], aescstrdup(str
, -1), locale
[i
]);
439 unum_close(currencyFmt
);
444 * Test localized currency patterns for PREEURO variants.
446 static void TestCurrencyPreEuro(void)
448 UNumberFormat
*currencyFmt
;
449 UChar
*str
=NULL
, *res
=NULL
;
452 UErrorCode status
= U_ZERO_ERROR
;
454 const char* locale
[]={
455 "ca_ES_PREEURO", "de_LU_PREEURO", "en_IE_PREEURO", "fi_FI_PREEURO", "fr_LU_PREEURO", "it_IT_PREEURO",
456 "pt_PT_PREEURO", "de_AT_PREEURO", "el_GR_PREEURO", "es_ES_PREEURO", "fr_BE_PREEURO", "ga_IE_PREEURO",
457 "nl_BE_PREEURO", "de_DE_PREEURO", "en_BE_PREEURO", "eu_ES_PREEURO", "fr_FR_PREEURO", "gl_ES_PREEURO",
461 const char* result
[]={
462 "\\u20A7 2", "2 F", "\\u00A31.50", "1,50 mk", "1,50 F", "\\u20A4 2",
463 "1$50 Esc.", "\\u00F6S 1,50", "1,50 \\u0394\\u03C1\\u03C7", "2 \\u20A7", "1,50 FB", "\\u00a31.50",
464 "1,50 BF", "1,50 DM", "1,50 BF", "\\u20A7 2", "1,50 F", "\\u20A7 2",
468 log_verbose("\nTesting the number format with different currency patterns\n");
469 for(i
=0; i
< 19; i
++)
471 char curID
[256] = {0};
472 uloc_canonicalize(locale
[i
], curID
, 256, &status
);
473 if(U_FAILURE(status
)){
474 log_err("Could not canonicalize %s. Error: %s \n", locale
[i
], u_errorName(status
));
477 currencyFmt
= unum_open(UNUM_CURRENCY
, NULL
,0,curID
,NULL
, &status
);
478 if(U_FAILURE(status
)){
479 log_err("Error in the construction of number format with style currency:\n%s\n",
480 myErrorName(status
));
483 lneed
= unum_formatDouble(currencyFmt
, 1.50, NULL
, lneed
, NULL
, &status
);
484 if(status
==U_BUFFER_OVERFLOW_ERROR
){
486 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
488 unum_formatDouble(currencyFmt
, 1.50, str
, lneed
+1, &pos
, &status
);
490 if(U_FAILURE(status
)) {
491 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
) );
493 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen(result
[i
])+1) );
494 u_unescape(result
[i
],res
,(int32_t)(strlen(result
[i
])+1));
495 if (u_strcmp(str
, res
) != 0){
496 log_err("FAIL: Expected %s Got: %s for locale: %s\n", result
[i
],aescstrdup(str
, -1),locale
[i
]);
499 unum_close(currencyFmt
);
506 * Test currency "object" (we use this name to match the other C++
507 * test name and the Jave name). Actually, test ISO currency code
508 * support in the C API.
510 static void TestCurrencyObject(void)
512 UNumberFormat
*currencyFmt
;
513 UChar
*str
=NULL
, *res
=NULL
;
516 UErrorCode status
= U_ZERO_ERROR
;
518 const char* locale
[]={
523 const char* currency
[]={
528 const char* result
[]={
529 "1\\u00A0234,56 \\u20AC",
530 "1\\u00A0235 \\u00A5",
533 log_verbose("\nTesting the number format with different currency codes\n");
537 UChar isoCode
[16]={0};
538 currencyFmt
= unum_open(UNUM_CURRENCY
, NULL
,0,locale
[i
],NULL
, &status
);
539 if(U_FAILURE(status
)){
540 log_err("Error in the construction of number format with style currency:\n%s\n",
541 myErrorName(status
));
544 u_uastrcpy(isoCode
, currency
[i
]);
545 unum_setTextAttribute(currencyFmt
, UNUM_CURRENCY_CODE
,
546 isoCode
, u_strlen(isoCode
), &status
);
547 if(U_FAILURE(status
)) {
548 log_err("FAIL: can't set currency code %s\n", myErrorName(status
) );
551 unum_getTextAttribute(currencyFmt
, UNUM_CURRENCY_CODE
,
552 isoCode
, sizeof(isoCode
), &status
);
553 if(U_FAILURE(status
)) {
554 log_err("FAIL: can't get currency code %s\n", myErrorName(status
) );
556 u_UCharsToChars(isoCode
,cStr
,u_strlen(isoCode
));
557 log_verbose("ISO code %s\n", cStr
);
558 if (*currency
[i
] && uprv_strcmp(cStr
, currency
[i
])) {
559 log_err("FAIL: currency should be %s, but is %s\n", currency
[i
], cStr
);
563 lneed
= unum_formatDouble(currencyFmt
, 1234.56, NULL
, lneed
, NULL
, &status
);
564 if(status
==U_BUFFER_OVERFLOW_ERROR
){
566 str
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
568 unum_formatDouble(currencyFmt
, 1234.56, str
, lneed
+1, &pos
, &status
);
570 if(U_FAILURE(status
)) {
571 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
) );
573 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen(result
[i
])+1) );
574 u_unescape(result
[i
],res
, (int32_t)(strlen(result
[i
])+1));
575 if (u_strcmp(str
, res
) != 0){
576 log_err("FAIL: Expected %s Got: %s for locale: %s\n", result
[i
],aescstrdup(str
, -1),locale
[i
]);
579 unum_close(currencyFmt
);
586 * Test proper rounding by the format method.
588 static void TestRounding487(void)
591 UErrorCode status
= U_ZERO_ERROR
;
592 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
593 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
594 /* nnf = unum_open(UNUM_DEFAULT, NULL, &status); */
595 nnf
= unum_open(UNUM_DEFAULT
, NULL
,0,"en_US",NULL
, &status
);
596 if(U_FAILURE(status
)){
597 log_err("FAIL: failure in the construction of number format: %s\n", myErrorName(status
));
599 roundingTest(nnf
, 0.00159999, 4, "0.0016");
600 roundingTest(nnf
, 0.00995, 4, "0.01");
602 roundingTest(nnf
, 12.3995, 3, "12.4");
604 roundingTest(nnf
, 12.4999, 0, "12");
605 roundingTest(nnf
, - 19.5, 0, "-20");
609 /*-------------------------------------*/
611 static void roundingTest(UNumberFormat
* nf
, double x
, int32_t maxFractionDigits
, const char* expected
)
619 unum_setAttribute(nf
, UNUM_MAX_FRACTION_DIGITS
, maxFractionDigits
);
621 lneed
=unum_formatDouble(nf
, x
, NULL
, lneed
, NULL
, &status
);
622 if(status
==U_BUFFER_OVERFLOW_ERROR
){
624 out
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
626 unum_formatDouble(nf
, x
, out
, lneed
+1, &pos
, &status
);
628 if(U_FAILURE(status
)) {
629 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
) );
631 /*Need to use log_verbose here. Problem with the float*/
632 /*printf("%f format with %d fraction digits to %s\n", x, maxFractionDigits, austrdup(out) );*/
633 res
=(UChar
*)malloc(sizeof(UChar
) * (strlen(expected
)+1) );
634 u_uastrcpy(res
, expected
);
635 if (u_strcmp(out
, res
) != 0)
636 log_err("FAIL: Expected: %s or %s\n", expected
, austrdup(res
) );
644 * Testing unum_getDoubleAttribute and unum_setDoubleAttribute()
646 static void TestDoubleAttribute(void)
648 double mydata
[] = { 1.11, 22.22, 333.33, 4444.44, 55555.55, 666666.66, 7777777.77, 88888888.88, 999999999.99};
651 UErrorCode status
=U_ZERO_ERROR
;
652 UNumberFormatAttribute attr
;
653 UNumberFormatStyle style
= UNUM_DEFAULT
;
655 def
=unum_open(style
, NULL
,0,NULL
,NULL
, &status
);
656 log_verbose("\nTesting get and set DoubleAttributes\n");
657 attr
=UNUM_ROUNDING_INCREMENT
;
658 dvalue
=unum_getDoubleAttribute(def
, attr
);
659 for (i
= 0; i
<9 ; i
++)
662 unum_setDoubleAttribute(def
, attr
, dvalue
);
663 if(unum_getDoubleAttribute(def
,attr
)!=mydata
[i
])
664 log_err("Fail: error in setting and getting double attributes for UNUM_ROUNDING_INCREMENT\n");
666 log_verbose("Pass: setting and getting double attributes for UNUM_ROUNDING_INCREMENT works fine\n");
672 * Test the functioning of the secondary grouping value.
674 static void TestSecondaryGrouping(void) {
675 UErrorCode status
= U_ZERO_ERROR
;
676 UNumberFormat
*f
= NULL
, *g
= NULL
;
677 UNumberFormat
*us
= unum_open(UNUM_DECIMAL
,NULL
,0, "en_US", NULL
,&status
);
679 UChar resultBuffer
[512];
680 int32_t l
= 1876543210L;
684 UBool expectGroup
= FALSE
, isGroup
= FALSE
;
686 u_uastrcpy(buffer
, "#,##,###");
687 f
= unum_open(UNUM_IGNORE
,buffer
, -1, "en_US",NULL
, &status
);
688 CHECK(status
, "DecimalFormat ct");
691 unum_format(f
, (int32_t)123456789L, resultBuffer
, 512 , &pos
, &status
);
692 u_uastrcpy(buffer
, "12,34,56,789");
693 if ((u_strcmp(resultBuffer
, buffer
) != 0) || U_FAILURE(status
))
695 log_err("Fail: Formatting \"#,##,###\" pattern with 123456789 got %s, expected %s\n", resultBuffer
, "12,34,56,789");
697 if (pos
.beginIndex
!= 0 && pos
.endIndex
!= 12) {
698 log_err("Fail: Formatting \"#,##,###\" pattern pos = (%d, %d) expected pos = (0, 12)\n", pos
.beginIndex
, pos
.endIndex
);
700 memset(resultBuffer
,0, sizeof(UChar
)*512);
701 unum_toPattern(f
, FALSE
, resultBuffer
, 512, &status
);
702 u_uastrcpy(buffer
, "#,##,###");
703 if ((u_strcmp(resultBuffer
, buffer
) != 0) || U_FAILURE(status
))
705 log_err("Fail: toPattern() got %s, expected %s\n", resultBuffer
, "#,##,###");
707 memset(resultBuffer
,0, sizeof(UChar
)*512);
708 u_uastrcpy(buffer
, "#,###");
709 unum_applyPattern(f
, FALSE
, buffer
, -1,NULL
,NULL
);
710 if (U_FAILURE(status
))
712 log_err("Fail: applyPattern call failed\n");
714 unum_setAttribute(f
, UNUM_SECONDARY_GROUPING_SIZE
, 4);
715 unum_format(f
, (int32_t)123456789L, resultBuffer
, 512 , &pos
, &status
);
716 u_uastrcpy(buffer
, "12,3456,789");
717 if ((u_strcmp(resultBuffer
, buffer
) != 0) || U_FAILURE(status
))
719 log_err("Fail: Formatting \"#,###\" pattern with 123456789 got %s, expected %s\n", resultBuffer
, "12,3456,789");
721 memset(resultBuffer
,0, sizeof(UChar
)*512);
722 unum_toPattern(f
, FALSE
, resultBuffer
, 512, &status
);
723 u_uastrcpy(buffer
, "#,####,###");
724 if ((u_strcmp(resultBuffer
, buffer
) != 0) || U_FAILURE(status
))
726 log_err("Fail: toPattern() got %s, expected %s\n", resultBuffer
, "#,####,###");
728 memset(resultBuffer
,0, sizeof(UChar
)*512);
729 g
= unum_open(UNUM_DECIMAL
, NULL
,0,"hi_IN",NULL
, &status
);
730 if (U_FAILURE(status
))
732 log_err("Fail: Cannot create UNumberFormat for \"hi_IN\" locale.\n");
735 unum_format(g
, l
, resultBuffer
, 512, &pos
, &status
);
737 /* expect "1,87,65,43,210", but with Hindi digits */
739 if (u_strlen(resultBuffer
) != 14) {
742 for (i
=0; i
<u_strlen(resultBuffer
); ++i
) {
752 /* Later -- fix this to get the actual grouping */
753 /* character from the resource bundle. */
754 isGroup
= (UBool
)(resultBuffer
[i
] == 0x002C);
755 if (isGroup
!= expectGroup
) {
762 log_err("FAIL Expected %s x hi_IN -> \"1,87,65,43,210\" (with Hindi digits), got %s\n", "1876543210L", resultBuffer
);
768 static void TestCurrencyKeywords(void)
770 static const char *currencies
[] = {
771 "ADD", "ADP", "AED", "AFA", "AFN", "AIF", "ALK", "ALL", "ALV", "ALX", "AMD",
772 "ANG", "AOA", "AOK", "AON", "AOR", "AOS", "ARA", "ARM", "ARP", "ARS", "ATS",
773 "AUD", "AUP", "AWG", "AZM", "BAD", "BAM", "BAN", "BBD", "BDT", "BEC", "BEF",
774 "BEL", "BGL", "BGM", "BGN", "BGO", "BGX", "BHD", "BIF", "BMD", "BMP", "BND",
775 "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "BRL", "BRN", "BRR", "BRZ",
776 "BSD", "BSP", "BTN", "BTR", "BUK", "BUR", "BWP", "BYB", "BYL", "BYR", "BZD",
777 "BZH", "CAD", "CDF", "CDG", "CDL", "CFF", "CHF", "CKD", "CLC", "CLE", "CLF",
778 "CLP", "CMF", "CNP", "CNX", "CNY", "COB", "COF", "COP", "CRC", "CSC", "CSK",
779 "CUP", "CUX", "CVE", "CWG", "CYP", "CZK", "DDM", "DEM", "DES", "DJF", "DKK",
780 "DOP", "DZD", "DZF", "DZG", "ECS", "ECV", "EEK", "EGP", "ERN", "ESP", "ETB",
781 "ETD", "EUR", "FIM", "FIN", "FJD", "FJP", "FKP", "FOK", "FRF", "FRG", "GAF",
782 "GBP", "GEK", "GEL", "GHC", "GHO", "GHP", "GHR", "GIP", "GLK", "GMD", "GMP",
783 "GNF", "GNI", "GNS", "GPF", "GQE", "GQF", "GQP", "GRD", "GRN", "GTQ", "GUF",
784 "GWE", "GWM", "GWP", "GYD", "HKD", "HNL", "HRD", "HRK", "HTG", "HUF", "IBP",
785 "IDG", "IDJ", "IDN", "IDR", "IEP", "ILL", "ILP", "ILS", "IMP", "INR", "IQD",
786 "IRR", "ISK", "ITL", "JEP", "JMD", "JMP", "JOD", "JPY", "KES", "KGS", "KHO",
787 "KHR", "KID", "KMF", "KPP", "KPW", "KRH", "KRO", "KRW", "KWD", "KYD", "KZR",
788 "KZT", "LAK", "LBP", "LIF", "LKR", "LNR", "LRD", "LSL", "LTL", "LTT", "LUF",
789 "LVL", "LVR", "LYB", "LYD", "LYP", "MAD", "MAF", "MCF", "MCG", "MDC", "MDL",
790 "MDR", "MGA", "MGF", "MHD", "MKD", "MKN", "MLF", "MMK", "MMX", "MNT", "MOP",
791 "MQF", "MRO", "MTL", "MTP", "MUR", "MVP", "MVR", "MWK", "MWP", "MXN", "MXP",
792 "MXV", "MYR", "MZE", "MZM", "NAD", "NCF", "NGN", "NGP", "NHF", "NIC", "NIG",
793 "NIO", "NLG", "NOK", "NPR", "NZD", "NZP", "OMR", "OMS", "PAB", "PDK", "PDN",
794 "PDR", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLX", "PLZ", "PSP",
795 "PTC", "PTE", "PYG", "QAR", "REF", "ROL", "RON", "RUB", "RUR", "RWF", "SAR",
796 "SAS", "SBD", "SCR", "SDD", "SDP", "SEK", "SGD", "SHP", "SIB", "SIT", "SKK",
797 "SLL", "SML", "SOS", "SQS", "SRG", "SSP", "STD", "STE", "SUN", "SUR", "SVC",
798 "SYP", "SZL", "TCC", "TDF", "THB", "TJR", "TJS", "TMM", "TND", "TOP", "TOS",
799 "TPE", "TPP", "TRL", "TTD", "TTO", "TVD", "TWD", "TZS", "UAH", "UAK", "UGS",
800 "UGX", "USD", "USN", "USS", "UYF", "UYP", "UYU", "UZC", "UZS", "VAL", "VDD",
801 "VDN", "VDP", "VEB", "VGD", "VND", "VNN", "VNR", "VNS", "VUV", "WSP", "WST",
802 "XAD", "XAF", "XAM", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XCF", "XDR",
803 "XEF", "XEU", "XFO", "XFU", "XID", "XMF", "XNF", "XOF", "XPF", "XPS", "XSS",
804 "XTR", "YDD", "YEI", "YER", "YUD", "YUF", "YUG", "YUM", "YUN", "YUO", "YUR",
805 "ZAL", "ZAP", "ZAR", "ZMK", "ZMP", "ZRN", "ZRZ", "ZWD"
808 UErrorCode status
= U_ZERO_ERROR
;
809 int32_t i
= 0, j
= 0;
810 int32_t noLocales
= uloc_countAvailable();
814 UChar currBuffer
[256];
817 for(i
= 0; i
< noLocales
; i
++) {
818 strcpy(currLoc
, uloc_getAvailable(i
));
819 for(j
= 0; j
< sizeof(currencies
)/sizeof(currencies
[0]); j
++) {
820 strcpy(locale
, currLoc
);
821 strcat(locale
, "@currency=");
822 strcat(locale
, currencies
[j
]);
823 ucurr_forLocale(locale
, result
, 4, &status
);
824 u_charsToUChars(currencies
[j
], currBuffer
, 3);
826 if(u_strcmp(currBuffer
, result
) != 0) {
827 log_err("Didn't get the right currency for %s\n", locale
);
834 #endif /* #if !UCONFIG_NO_FORMATTING */