]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cnmdptst.c
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cnmdptst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2006, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ********************************************************************/
6 /*******************************************************************************
7 *
8 * File CNMDPTST.C
9 *
10 * Madhu Katragadda Creation
11 * Modification History:
12 *
13 * Date Name Description
14 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
15 *******************************************************************************
16 */
17
18 /* C DEPTH TEST FOR NUMBER FORMAT */
19
20 #include "unicode/utypes.h"
21
22 #if !UCONFIG_NO_FORMATTING
23
24 #include "unicode/ucurr.h"
25 #include "unicode/uloc.h"
26 #include "unicode/unum.h"
27 #include "unicode/ustring.h"
28 #include "cintltst.h"
29 #include "cnmdptst.h"
30 #include "cmemory.h"
31 #include "cstring.h"
32
33 #define CHECK(status,str) if (U_FAILURE(status)) { log_err("FAIL: %s\n", str); return; }
34
35 void addNumFrDepTest(TestNode** root);
36 static void TestCurrencyPreEuro(void);
37 static void TestCurrencyObject(void);
38
39 void addNumFrDepTest(TestNode** root)
40 {
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");
52 }
53
54 /*Test Various format patterns*/
55 static void TestPatterns(void)
56 {
57 int32_t pat_length, i, lneed;
58 UNumberFormat *fmt;
59 UChar upat[5];
60 UChar unewpat[5];
61 UChar unum[5];
62 UChar *unewp=NULL;
63 UChar *str=NULL;
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" };
68
69 log_verbose("\nTesting different format patterns\n");
70 pat_length = sizeof(pat) / sizeof(pat[0]);
71 for (i=0; i < pat_length; ++i)
72 {
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]);
78 continue;
79 }
80 lneed=0;
81 lneed=unum_toPattern(fmt, FALSE, NULL, lneed, &status);
82 if(status==U_BUFFER_OVERFLOW_ERROR){
83 status= U_ZERO_ERROR;
84 unewp=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
85 unum_toPattern(fmt, FALSE, unewp, lneed+1, &status);
86 }
87 if(U_FAILURE(status)){
88 log_err("FAIL: Number format extracting the pattern failed for %s\n", pat[i]);
89 }
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) );
93
94 lneed=0;
95 lneed=unum_format(fmt, 0, NULL, lneed, NULL, &status);
96 if(status==U_BUFFER_OVERFLOW_ERROR){
97 status=U_ZERO_ERROR;
98 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
99 unum_format(fmt, 0, str, lneed+1, NULL, &status);
100 }
101 if(U_FAILURE(status)) {
102 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
103 }
104 u_uastrcpy(unum, num[i]);
105 if (u_strcmp(str, unum) != 0)
106 {
107 log_err("FAIL: Pattern %s should format zero as %s; %s Seen instead\n", pat[i], num[i], austrdup(str) );
108
109 }
110 free(unewp);
111 free(str);
112 unum_close(fmt);
113 }
114 }
115
116 /* Test the handling of quotes*/
117 static void TestQuotes(void)
118 {
119 int32_t lneed;
120 UErrorCode status=U_ZERO_ERROR;
121 UChar pat[15];
122 UChar res[15];
123 UChar *str=NULL;
124 UNumberFormat *fmt;
125 char tempBuf[256];
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");
131 }
132 lneed=0;
133 lneed=unum_format(fmt, 123, NULL, lneed, NULL, &status);
134 if(status==U_BUFFER_OVERFLOW_ERROR){
135 status=U_ZERO_ERROR;
136 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
137 unum_format(fmt, 123, str, lneed+1, NULL, &status);
138 }
139 if(U_FAILURE(status) || !str) {
140 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
141 return;
142 }
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");
148
149 free(str);
150 unum_close(fmt);
151
152
153 u_uastrcpy(pat, "");
154 u_uastrcpy(pat, "a''b#");
155
156
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");
160 }
161 lneed=0;
162 lneed=unum_format(fmt, 123, NULL, lneed, NULL, &status);
163 if(status==U_BUFFER_OVERFLOW_ERROR){
164 status=U_ZERO_ERROR;
165 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
166 unum_format(fmt, 123, str, lneed+1, NULL, &status);
167 }
168 if(U_FAILURE(status)) {
169 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
170 }
171 log_verbose("Pattern \"%s\" \n", u_austrcpy(tempBuf, pat) );
172 log_verbose("Format 123 -> %s\n", u_austrcpy(tempBuf, str) );
173 u_uastrcpy(res, "");
174 u_uastrcpy(res, "a'b123");
175 if(u_strcmp(str, res) != 0)
176 log_err("FAIL: Expected a'b123\n");
177
178 free(str);
179 unum_close(fmt);
180 }
181
182 /* Test exponential pattern*/
183 static void TestExponential(void)
184 {
185 int32_t pat_length, val_length, lval_length;
186 int32_t ival, ilval, p, v, lneed;
187 UNumberFormat *fmt;
188 int32_t ppos;
189 UChar *upat;
190 UChar pattern[20];
191 UChar *str=NULL;
192 UChar uvalfor[20], ulvalfor[20];
193 char tempMsgBug[256];
194 double a;
195 UErrorCode status = U_ZERO_ERROR;
196 #ifdef OS390
197 static const double val[] = { 0.01234, 123456789, 1.23e75, -3.141592653e-78 };
198 #else
199 static const double val[] = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 };
200 #endif
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 };
203
204 static const char* valFormat[] =
205 {
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]"
210 };
211 static const char* lvalFormat[] =
212 {
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"
217 };
218 static const double valParse[] =
219 {
220 #ifdef OS390
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
225 #else
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
232 #endif
233 };
234 static const int32_t lvalParse[] =
235 {
236 0, -1, 1, 123460000,
237 0, -1, 1, 123460000,
238 0, -1, 1, 123456800,
239 0, -1, 1, 123500000
240 };
241
242
243 pat_length = sizeof(pat) / sizeof(pat[0]);
244 val_length = sizeof(val) / sizeof(val[0]);
245 lval_length = sizeof(lval) / sizeof(lval[0]);
246 ival = 0;
247 ilval = 0;
248 for (p=0; p < pat_length; ++p)
249 {
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]");
255 continue;
256 }
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)
261 {
262 /*format*/
263 lneed=0;
264 lneed=unum_formatDouble(fmt, val[v], NULL, lneed, NULL, &status);
265 if(status==U_BUFFER_OVERFLOW_ERROR){
266 status=U_ZERO_ERROR;
267 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
268 unum_formatDouble(fmt, val[v], str, lneed+1, NULL, &status);
269 }
270 if(U_FAILURE(status)) {
271 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
272 }
273
274
275
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) );
279
280 /*parsing*/
281 ppos=0;
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);
286 }
287 else
288 log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos, a);
289
290 free(str);
291 }
292 for (v=0; v<lval_length; ++v)
293 {
294 /*format*/
295 lneed=0;
296 lneed=unum_formatDouble(fmt, lval[v], NULL, lneed, NULL, &status);
297 if(status==U_BUFFER_OVERFLOW_ERROR){
298 status=U_ZERO_ERROR;
299 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
300 unum_formatDouble(fmt, lval[v], str, lneed+1, NULL, &status);
301 }
302 if(U_FAILURE(status)) {
303 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
304 }
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) );
309
310 /*parsing*/
311 ppos=0;
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]);
317 }
318 else
319 log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos, a);
320
321 free(str);
322
323 }
324 ival += val_length;
325 ilval += lval_length;
326 unum_close(fmt);
327 free(upat);
328 }
329 }
330
331 /**
332 * Test the handling of the currency symbol in patterns.
333 */
334 static void TestCurrencySign(void)
335 {
336 int32_t lneed;
337 UNumberFormat *fmt;
338 UChar *pattern=NULL;
339 UChar *str=NULL;
340 UChar *pat=NULL;
341 UChar *res=NULL;
342 UErrorCode status = U_ZERO_ERROR;
343 char tempBuf[256];
344
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");
351 }
352 lneed=0;
353 lneed=unum_formatDouble(fmt, 1234.56, NULL, lneed, NULL, &status);
354 if(status==U_BUFFER_OVERFLOW_ERROR){
355 status=U_ZERO_ERROR;
356 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
357 unum_formatDouble(fmt, 1234.56, str, lneed+1, NULL, &status);
358 }
359 if(U_FAILURE(status)) {
360 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
361 }
362 lneed=0;
363 lneed=unum_toPattern(fmt, FALSE, NULL, lneed, &status);
364 if(status==U_BUFFER_OVERFLOW_ERROR){
365 status=U_ZERO_ERROR;
366 pat=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
367 unum_formatDouble(fmt, FALSE, pat, lneed+1, NULL, &status);
368 }
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");
375 } else {
376 log_err("Error formatting\n");
377 }
378 free(str);
379 free(res);
380 free(pat);
381
382 lneed=0;
383 lneed=unum_formatDouble(fmt, -1234.56, NULL, lneed, NULL, &status);
384 if(status==U_BUFFER_OVERFLOW_ERROR){
385 status=U_ZERO_ERROR;
386 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
387 unum_formatDouble(fmt, -1234.56, str, lneed+1, NULL, &status);
388 }
389 if(U_FAILURE(status)) {
390 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
391 }
392 if(str) {
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");
396 free(str);
397 free(res);
398 }
399
400 unum_close(fmt);
401 free(pattern);
402 }
403
404 /**
405 * Test localized currency patterns.
406 */
407 static void TestCurrency(void)
408 {
409 UNumberFormat *currencyFmt;
410 UChar *str;
411 int32_t lneed, i;
412 UFieldPosition pos;
413 UChar res[100];
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");
418 for(i=0; i < 3; i++)
419 {
420 str=NULL;
421 currencyFmt = unum_open(UNUM_CURRENCY, NULL,0,locale[i],NULL, &status);
422
423 if(U_FAILURE(status)){
424 log_err("Error in the construction of number format with style currency:\n%s\n",
425 myErrorName(status));
426 } else {
427 lneed=0;
428 lneed= unum_formatDouble(currencyFmt, 1.50, NULL, lneed, NULL, &status);
429 if(status==U_BUFFER_OVERFLOW_ERROR){
430 status=U_ZERO_ERROR;
431 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
432 pos.field = 0;
433 unum_formatDouble(currencyFmt, 1.50, str, lneed+1, &pos, &status);
434 }
435
436 if(U_FAILURE(status)) {
437 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
438 } else {
439 u_charsToUChars(result[i], res, (int32_t)strlen(result[i])+1);
440
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]);
443 }
444 }
445 }
446
447 unum_close(currencyFmt);
448 free(str);
449 }
450 }
451 /**
452 * Test localized currency patterns for PREEURO variants.
453 */
454 static void TestCurrencyPreEuro(void)
455 {
456 UNumberFormat *currencyFmt;
457 UChar *str=NULL, *res=NULL;
458 int32_t lneed, i;
459 UFieldPosition pos;
460 UErrorCode status = U_ZERO_ERROR;
461
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",
466 "nl_NL_PREEURO",
467 };
468
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",
473 "fl 1,50"
474 };
475
476 log_verbose("\nTesting the number format with different currency patterns\n");
477 for(i=0; i < 19; i++)
478 {
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));
483 continue;
484 }
485 currencyFmt = unum_open(UNUM_CURRENCY, NULL,0,curID,NULL, &status);
486
487 if(U_FAILURE(status)){
488 log_err("Error in the construction of number format with style currency:\n%s\n",
489 myErrorName(status));
490 } else {
491 lneed=0;
492 lneed= unum_formatDouble(currencyFmt, 1.50, NULL, lneed, NULL, &status);
493
494 if(status==U_BUFFER_OVERFLOW_ERROR){
495 status=U_ZERO_ERROR;
496 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
497 pos.field = 0;
498 unum_formatDouble(currencyFmt, 1.50, str, lneed+1, &pos, &status);
499 }
500
501 if(U_FAILURE(status)) {
502 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
503 } else {
504 res=(UChar*)malloc(sizeof(UChar) * (strlen(result[i])+1) );
505 u_unescape(result[i],res,(int32_t)(strlen(result[i])+1));
506
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]);
509 }
510 }
511 }
512
513 unum_close(currencyFmt);
514 free(str);
515 free(res);
516 }
517 }
518
519 /**
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.
523 */
524 static void TestCurrencyObject(void)
525 {
526 UNumberFormat *currencyFmt;
527 UChar *str=NULL, *res=NULL;
528 int32_t lneed, i;
529 UFieldPosition pos;
530 UErrorCode status = U_ZERO_ERROR;
531
532 const char* locale[]={
533 "fr_FR",
534 "fr_FR",
535 };
536
537 const char* currency[]={
538 "",
539 "JPY",
540 };
541
542 const char* result[]={
543 "1\\u00A0234,56 \\u20AC",
544 "1\\u00A0235 \\u00A5",
545 };
546
547 log_verbose("\nTesting the number format with different currency codes\n");
548 for(i=0; i < 2; i++)
549 {
550 char cStr[20]={0};
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));
556 } else {
557 if (*currency[i]) {
558 u_uastrcpy(isoCode, currency[i]);
559 unum_setTextAttribute(currencyFmt, UNUM_CURRENCY_CODE,
560 isoCode, u_strlen(isoCode), &status);
561
562 if(U_FAILURE(status)) {
563 log_err("FAIL: can't set currency code %s\n", myErrorName(status) );
564 }
565 }
566
567 unum_getTextAttribute(currencyFmt, UNUM_CURRENCY_CODE,
568 isoCode, sizeof(isoCode), &status);
569
570 if(U_FAILURE(status)) {
571 log_err("FAIL: can't get currency code %s\n", myErrorName(status) );
572 }
573
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);
578 }
579
580 lneed=0;
581 lneed= unum_formatDouble(currencyFmt, 1234.56, NULL, lneed, NULL, &status);
582 if(status==U_BUFFER_OVERFLOW_ERROR){
583 status=U_ZERO_ERROR;
584 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
585 pos.field = 0;
586 unum_formatDouble(currencyFmt, 1234.56, str, lneed+1, &pos, &status);
587 }
588 if(U_FAILURE(status)) {
589 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
590 } else {
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]);
595 }
596 }
597 }
598
599 unum_close(currencyFmt);
600 free(str);
601 free(res);
602 }
603 }
604
605 /**
606 * Test proper rounding by the format method.
607 */
608 static void TestRounding487(void)
609 {
610 UNumberFormat *nnf;
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);
616
617 if(U_FAILURE(status)){
618 log_err("FAIL: failure in the construction of number format: %s\n", myErrorName(status));
619 } else {
620 roundingTest(nnf, 0.00159999, 4, "0.0016");
621 roundingTest(nnf, 0.00995, 4, "0.01");
622
623 roundingTest(nnf, 12.3995, 3, "12.4");
624
625 roundingTest(nnf, 12.4999, 0, "12");
626 roundingTest(nnf, - 19.5, 0, "-20");
627 }
628
629 unum_close(nnf);
630 }
631
632 /*-------------------------------------*/
633
634 static void roundingTest(UNumberFormat* nf, double x, int32_t maxFractionDigits, const char* expected)
635 {
636 UChar *out = NULL;
637 UChar *res;
638 UFieldPosition pos;
639 UErrorCode status;
640 int32_t lneed;
641 status=U_ZERO_ERROR;
642 unum_setAttribute(nf, UNUM_MAX_FRACTION_DIGITS, maxFractionDigits);
643 lneed=0;
644 lneed=unum_formatDouble(nf, x, NULL, lneed, NULL, &status);
645 if(status==U_BUFFER_OVERFLOW_ERROR){
646 status=U_ZERO_ERROR;
647 out=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
648 pos.field=0;
649 unum_formatDouble(nf, x, out, lneed+1, &pos, &status);
650 }
651 if(U_FAILURE(status)) {
652 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
653 }
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) );
660 free(res);
661 if(out != NULL) {
662 free(out);
663 }
664 }
665
666 /*
667 * Testing unum_getDoubleAttribute and unum_setDoubleAttribute()
668 */
669 static void TestDoubleAttribute(void)
670 {
671 double mydata[] = { 1.11, 22.22, 333.33, 4444.44, 55555.55, 666666.66, 7777777.77, 88888888.88, 999999999.99};
672 double dvalue;
673 int i;
674 UErrorCode status=U_ZERO_ERROR;
675 UNumberFormatAttribute attr;
676 UNumberFormatStyle style= UNUM_DEFAULT;
677 UNumberFormat *def;
678
679 log_verbose("\nTesting get and set DoubleAttributes\n");
680 def=unum_open(style, NULL,0,NULL,NULL, &status);
681
682 if (U_FAILURE(status)) {
683 log_err("Fail: error creating a default number formatter\n");
684 } else {
685 attr=UNUM_ROUNDING_INCREMENT;
686 dvalue=unum_getDoubleAttribute(def, attr);
687 for (i = 0; i<9 ; i++)
688 {
689 dvalue = mydata[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");
693 else
694 log_verbose("Pass: setting and getting double attributes for UNUM_ROUNDING_INCREMENT works fine\n");
695 }
696 }
697
698 unum_close(def);
699 }
700
701 /**
702 * Test the functioning of the secondary grouping value.
703 */
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);
708 UFieldPosition pos;
709 UChar resultBuffer[512];
710 int32_t l = 1876543210L;
711 UBool ok = TRUE;
712 UChar buffer[512];
713 int32_t i;
714 UBool expectGroup = FALSE, isGroup = FALSE;
715
716 u_uastrcpy(buffer, "#,##,###");
717 f = unum_open(UNUM_IGNORE,buffer, -1, "en_US",NULL, &status);
718 CHECK(status, "DecimalFormat ct");
719
720 pos.field = 0;
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))
724 {
725 log_err("Fail: Formatting \"#,##,###\" pattern with 123456789 got %s, expected %s\n", resultBuffer, "12,34,56,789");
726 }
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);
729 }
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))
734 {
735 log_err("Fail: toPattern() got %s, expected %s\n", resultBuffer, "#,##,###");
736 }
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))
741 {
742 log_err("Fail: applyPattern call failed\n");
743 }
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))
748 {
749 log_err("Fail: Formatting \"#,###\" pattern with 123456789 got %s, expected %s\n", resultBuffer, "12,3456,789");
750 }
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))
755 {
756 log_err("Fail: toPattern() got %s, expected %s\n", resultBuffer, "#,####,###");
757 }
758 memset(resultBuffer,0, sizeof(UChar)*512);
759 g = unum_open(UNUM_DECIMAL, NULL,0,"hi_IN",NULL, &status);
760 if (U_FAILURE(status))
761 {
762 log_err("Fail: Cannot create UNumberFormat for \"hi_IN\" locale.\n");
763 }
764
765 unum_format(g, l, resultBuffer, 512, &pos, &status);
766 unum_close(g);
767 /* expect "1,87,65,43,210", but with Hindi digits */
768 /* 01234567890123 */
769 if (u_strlen(resultBuffer) != 14) {
770 ok = FALSE;
771 } else {
772 for (i=0; i<u_strlen(resultBuffer); ++i) {
773 expectGroup = FALSE;
774 switch (i) {
775 case 1:
776 case 4:
777 case 7:
778 case 10:
779 expectGroup = TRUE;
780 break;
781 }
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) {
786 ok = FALSE;
787 break;
788 }
789 }
790 }
791 if (!ok) {
792 log_err("FAIL Expected %s x hi_IN -> \"1,87,65,43,210\" (with Hindi digits), got %s\n", "1876543210L", resultBuffer);
793 }
794 unum_close(f);
795 unum_close(us);
796 }
797
798 static void TestCurrencyKeywords(void)
799 {
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"
836 };
837
838 UErrorCode status = U_ZERO_ERROR;
839 int32_t i = 0, j = 0;
840 int32_t noLocales = uloc_countAvailable();
841 char locale[256];
842 char currLoc[256];
843 UChar result[4];
844 UChar currBuffer[256];
845
846
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);
855 currBuffer[3] = 0;
856 if(u_strcmp(currBuffer, result) != 0) {
857 log_err("Didn't get the right currency for %s\n", locale);
858 }
859 }
860
861 }
862 }
863
864 #endif /* #if !UCONFIG_NO_FORMATTING */