]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/cnmdptst.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cnmdptst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
374ca955
A
3 * Copyright (c) 1997-2004, International Business Machines Corporation
4 * and others. All Rights Reserved.
b75a7d8f
A
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
374ca955 24#include "unicode/ucurr.h"
b75a7d8f
A
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
35void addNumFrDepTest(TestNode** root);
36static void TestCurrencyPreEuro(void);
37static void TestCurrencyObject(void);
38
39void 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");
374ca955 51 addTest(root, &TestCurrencyKeywords, "tsformat/cnmdptst/TestCurrencyKeywords");
b75a7d8f
A
52}
53
54/*Test Various format patterns*/
55static void TestPatterns(void)
56{
374ca955
A
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)
b75a7d8f 72 {
374ca955
A
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)
b75a7d8f 106 {
374ca955
A
107 log_err("FAIL: Pattern %s should format zero as %s; %s Seen instead\n", pat[i], num[i], austrdup(str) );
108
b75a7d8f 109 }
374ca955
A
110 free(unewp);
111 free(str);
112 unum_close(fmt);
b75a7d8f
A
113 }
114}
115
116/* Test the handling of quotes*/
117static void TestQuotes(void)
118{
374ca955
A
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");
b75a7d8f 148
374ca955
A
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);
b75a7d8f
A
180}
181
182/* Test exponential pattern*/
183static void TestExponential(void)
184{
374ca955
A
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;
b75a7d8f 196#ifdef OS390
374ca955 197 double val[] = { 0.01234, 123456789, 1.23e75, -3.141592653e-78 };
b75a7d8f 198#else
374ca955 199 double val[] = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 };
b75a7d8f 200#endif
374ca955
A
201 const char* pat[] = { "0.####E0", "00.000E00", "##0.######E000", "0.###E0;[0.###E0]" };
202 int32_t lval[] = { 0, -1, 1, 123456789 };
203
204 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 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 double valParse[] =
219 {
b75a7d8f 220#ifdef OS390
374ca955
A
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
b75a7d8f 225#else
374ca955
A
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
b75a7d8f 230#endif
374ca955
A
231 };
232 int32_t lvalParse[] =
b75a7d8f 233 {
374ca955
A
234 0, -1, 1, 123460000,
235 0, -1, 1, 123460000,
236 0, -1, 1, 123456800,
237 0, -1, 1, 123500000
238 };
239
240
241 pat_length = sizeof(pat) / sizeof(pat[0]);
242 val_length = sizeof(val) / sizeof(val[0]);
243 lval_length = sizeof(lval) / sizeof(lval[0]);
244 ival = 0;
245 ilval = 0;
246 for (p=0; p < pat_length; ++p)
247 {
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]");
253 continue;
254 }
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)
b75a7d8f 259 {
374ca955
A
260 /*format*/
261 lneed=0;
262 lneed=unum_formatDouble(fmt, val[v], NULL, lneed, NULL, &status);
263 if(status==U_BUFFER_OVERFLOW_ERROR){
264 status=U_ZERO_ERROR;
265 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
266 unum_formatDouble(fmt, val[v], str, lneed+1, NULL, &status);
267 }
268 if(U_FAILURE(status)) {
269 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
270 }
271
272
273
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) );
277
278 /*parsing*/
279 ppos=0;
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);
284 }
285 else
286 log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos, a);
287
288 free(str);
b75a7d8f 289 }
374ca955 290 for (v=0; v<lval_length; ++v)
b75a7d8f 291 {
374ca955
A
292 /*format*/
293 lneed=0;
294 lneed=unum_formatDouble(fmt, lval[v], NULL, lneed, NULL, &status);
295 if(status==U_BUFFER_OVERFLOW_ERROR){
296 status=U_ZERO_ERROR;
297 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
298 unum_formatDouble(fmt, lval[v], str, lneed+1, NULL, &status);
299 }
300 if(U_FAILURE(status)) {
301 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
302 }
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) );
307
308 /*parsing*/
309 ppos=0;
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]);
315 }
316 else
317 log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos, a);
318
319 free(str);
320
b75a7d8f 321 }
374ca955
A
322 ival += val_length;
323 ilval += lval_length;
324 unum_close(fmt);
325 free(upat);
b75a7d8f
A
326 }
327}
328
329/**
330 * Test the handling of the currency symbol in patterns.
331 */
332static void TestCurrencySign(void)
333{
374ca955
A
334 int32_t lneed;
335 UNumberFormat *fmt;
336 UChar *pattern=NULL;
337 UChar *str=NULL;
338 UChar *pat=NULL;
339 UChar *res=NULL;
340 UErrorCode status = U_ZERO_ERROR;
341 char tempBuf[256];
342
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");
349 }
350 lneed=0;
351 lneed=unum_formatDouble(fmt, 1234.56, NULL, lneed, NULL, &status);
352 if(status==U_BUFFER_OVERFLOW_ERROR){
353 status=U_ZERO_ERROR;
354 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
355 unum_formatDouble(fmt, 1234.56, str, lneed+1, NULL, &status);
356 }
357 if(U_FAILURE(status)) {
358 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
359 }
360 lneed=0;
361 lneed=unum_toPattern(fmt, FALSE, NULL, lneed, &status);
362 if(status==U_BUFFER_OVERFLOW_ERROR){
363 status=U_ZERO_ERROR;
364 pat=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
365 unum_formatDouble(fmt, FALSE, pat, lneed+1, NULL, &status);
366 }
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");
373 } else {
374 log_err("Error formatting\n");
375 }
b75a7d8f
A
376 free(str);
377 free(res);
374ca955
A
378 free(pat);
379
380 lneed=0;
381 lneed=unum_formatDouble(fmt, -1234.56, NULL, lneed, NULL, &status);
382 if(status==U_BUFFER_OVERFLOW_ERROR){
383 status=U_ZERO_ERROR;
384 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
385 unum_formatDouble(fmt, -1234.56, str, lneed+1, NULL, &status);
386 }
387 if(U_FAILURE(status)) {
388 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
389 }
390 if(str) {
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");
394 free(str);
395 free(res);
396 }
397
398 unum_close(fmt);
399 free(pattern);
b75a7d8f
A
400}
401
402/**
403 * Test localized currency patterns.
404 */
405static void TestCurrency(void)
406{
374ca955
A
407 UNumberFormat *currencyFmt;
408 UChar *str;
409 int32_t lneed, i;
410 UFieldPosition pos;
411 UChar res[100];
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");
416 for(i=0; i < 3; i++)
b75a7d8f 417 {
374ca955
A
418 str=NULL;
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));
423 }
424 lneed=0;
425 lneed= unum_formatDouble(currencyFmt, 1.50, NULL, lneed, NULL, &status);
426 if(status==U_BUFFER_OVERFLOW_ERROR){
427 status=U_ZERO_ERROR;
428 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
429 pos.field = 0;
430 unum_formatDouble(currencyFmt, 1.50, str, lneed+1, &pos, &status);
431 }
432 if(U_FAILURE(status)) {
433 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
434 }
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]);
438 }
439 unum_close(currencyFmt);
440 free(str);
b75a7d8f
A
441 }
442}
443/**
444 * Test localized currency patterns for PREEURO variants.
445 */
446static void TestCurrencyPreEuro(void)
447{
374ca955
A
448 UNumberFormat *currencyFmt;
449 UChar *str=NULL, *res=NULL;
450 int32_t lneed, i;
451 UFieldPosition pos;
452 UErrorCode status = U_ZERO_ERROR;
453
454 const char* locale[]={
b75a7d8f
A
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",
458 "nl_NL_PREEURO",
374ca955
A
459 };
460
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",
b75a7d8f 465 "fl 1,50"
374ca955
A
466 };
467
468 log_verbose("\nTesting the number format with different currency patterns\n");
469 for(i=0; i < 19; i++)
b75a7d8f 470 {
374ca955
A
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));
475 continue;
476 }
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));
481 }
482 lneed=0;
483 lneed= unum_formatDouble(currencyFmt, 1.50, NULL, lneed, NULL, &status);
484 if(status==U_BUFFER_OVERFLOW_ERROR){
485 status=U_ZERO_ERROR;
486 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
487 pos.field = 0;
488 unum_formatDouble(currencyFmt, 1.50, str, lneed+1, &pos, &status);
489 }
490 if(U_FAILURE(status)) {
491 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
492 }
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]);
497 }
498
499 unum_close(currencyFmt);
500 free(str);
501 free(res);
b75a7d8f
A
502 }
503}
504
505/**
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.
509 */
510static void TestCurrencyObject(void)
511{
374ca955
A
512 UNumberFormat *currencyFmt;
513 UChar *str=NULL, *res=NULL;
514 int32_t lneed, i;
515 UFieldPosition pos;
516 UErrorCode status = U_ZERO_ERROR;
517
518 const char* locale[]={
519 "fr_FR",
520 "fr_FR",
521 };
522
523 const char* currency[]={
524 "",
525 "JPY",
526 };
527
528 const char* result[]={
529 "1\\u00A0234,56 \\u20AC",
530 "1\\u00A0235 \\u00A5",
531 };
532
533 log_verbose("\nTesting the number format with different currency codes\n");
534 for(i=0; i < 2; i++)
b75a7d8f 535 {
374ca955
A
536 char cStr[20]={0};
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));
542 }
543 if (*currency[i]) {
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) );
549 }
550 }
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) );
555 }
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);
560 }
561
562 lneed=0;
563 lneed= unum_formatDouble(currencyFmt, 1234.56, NULL, lneed, NULL, &status);
564 if(status==U_BUFFER_OVERFLOW_ERROR){
565 status=U_ZERO_ERROR;
566 str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
567 pos.field = 0;
568 unum_formatDouble(currencyFmt, 1234.56, str, lneed+1, &pos, &status);
569 }
570 if(U_FAILURE(status)) {
571 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
572 }
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]);
577 }
578
579 unum_close(currencyFmt);
580 free(str);
581 free(res);
b75a7d8f
A
582 }
583}
584
585/**
586 * Test proper rounding by the format method.
587 */
588static void TestRounding487(void)
589{
374ca955
A
590 UNumberFormat *nnf;
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"
b75a7d8f 593 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
374ca955
A
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));
598 }
599 roundingTest(nnf, 0.00159999, 4, "0.0016");
600 roundingTest(nnf, 0.00995, 4, "0.01");
601
602 roundingTest(nnf, 12.3995, 3, "12.4");
603
604 roundingTest(nnf, 12.4999, 0, "12");
605 roundingTest(nnf, - 19.5, 0, "-20");
606 unum_close(nnf);
b75a7d8f
A
607}
608
609/*-------------------------------------*/
610
611static void roundingTest(UNumberFormat* nf, double x, int32_t maxFractionDigits, const char* expected)
612{
374ca955
A
613 UChar *out = NULL;
614 UChar *res;
615 UFieldPosition pos;
616 UErrorCode status;
617 int32_t lneed;
b75a7d8f 618 status=U_ZERO_ERROR;
374ca955
A
619 unum_setAttribute(nf, UNUM_MAX_FRACTION_DIGITS, maxFractionDigits);
620 lneed=0;
621 lneed=unum_formatDouble(nf, x, NULL, lneed, NULL, &status);
622 if(status==U_BUFFER_OVERFLOW_ERROR){
623 status=U_ZERO_ERROR;
624 out=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
625 pos.field=0;
626 unum_formatDouble(nf, x, out, lneed+1, &pos, &status);
627 }
628 if(U_FAILURE(status)) {
629 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
630 }
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) );
637 free(res);
638 if(out != NULL) {
639 free(out);
640 }
b75a7d8f
A
641}
642
643/*
644 * Testing unum_getDoubleAttribute and unum_setDoubleAttribute()
645 */
646static void TestDoubleAttribute(void)
647{
648 double mydata[] = { 1.11, 22.22, 333.33, 4444.44, 55555.55, 666666.66, 7777777.77, 88888888.88, 999999999.99};
649 double dvalue;
650 int i;
651 UErrorCode status=U_ZERO_ERROR;
652 UNumberFormatAttribute attr;
653 UNumberFormatStyle style= UNUM_DEFAULT;
654 UNumberFormat *def;
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++)
660 {
661 dvalue = mydata[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");
665 else
666 log_verbose("Pass: setting and getting double attributes for UNUM_ROUNDING_INCREMENT works fine\n");
667 }
668 unum_close(def);
669}
670
671/**
672 * Test the functioning of the secondary grouping value.
673 */
674static 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);
678 UFieldPosition pos;
679 UChar resultBuffer[512];
680 int32_t l = 1876543210L;
681 UBool ok = TRUE;
682 UChar buffer[512];
683 int32_t i;
684 UBool expectGroup = FALSE, isGroup = FALSE;
685
686 u_uastrcpy(buffer, "#,##,###");
687 f = unum_open(UNUM_IGNORE,buffer, -1, "en_US",NULL, &status);
688 CHECK(status, "DecimalFormat ct");
689
690 pos.field = 0;
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))
694 {
695 log_err("Fail: Formatting \"#,##,###\" pattern with 123456789 got %s, expected %s\n", resultBuffer, "12,34,56,789");
696 }
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);
699 }
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))
704 {
705 log_err("Fail: toPattern() got %s, expected %s\n", resultBuffer, "#,##,###");
706 }
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))
711 {
712 log_err("Fail: applyPattern call failed\n");
713 }
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))
718 {
719 log_err("Fail: Formatting \"#,###\" pattern with 123456789 got %s, expected %s\n", resultBuffer, "12,3456,789");
720 }
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))
725 {
726 log_err("Fail: toPattern() got %s, expected %s\n", resultBuffer, "#,####,###");
727 }
728 memset(resultBuffer,0, sizeof(UChar)*512);
729 g = unum_open(UNUM_DECIMAL, NULL,0,"hi_IN",NULL, &status);
730 if (U_FAILURE(status))
731 {
732 log_err("Fail: Cannot create UNumberFormat for \"hi_IN\" locale.\n");
733 }
734
735 unum_format(g, l, resultBuffer, 512, &pos, &status);
736 unum_close(g);
737 /* expect "1,87,65,43,210", but with Hindi digits */
738 /* 01234567890123 */
739 if (u_strlen(resultBuffer) != 14) {
740 ok = FALSE;
741 } else {
742 for (i=0; i<u_strlen(resultBuffer); ++i) {
743 expectGroup = FALSE;
744 switch (i) {
745 case 1:
746 case 4:
747 case 7:
748 case 10:
749 expectGroup = TRUE;
750 break;
751 }
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) {
756 ok = FALSE;
757 break;
758 }
759 }
760 }
761 if (!ok) {
762 log_err("FAIL Expected %s x hi_IN -> \"1,87,65,43,210\" (with Hindi digits), got %s\n", "1876543210L", resultBuffer);
763 }
764 unum_close(f);
765 unum_close(us);
766}
767
374ca955
A
768static void TestCurrencyKeywords(void)
769{
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"
806 };
807
808 UErrorCode status = U_ZERO_ERROR;
809 int32_t i = 0, j = 0;
810 int32_t noLocales = uloc_countAvailable();
811 char locale[256];
812 char currLoc[256];
813 UChar result[4];
814 UChar currBuffer[256];
815
816
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);
825 currBuffer[3] = 0;
826 if(u_strcmp(currBuffer, result) != 0) {
827 log_err("Didn't get the right currency for %s\n", locale);
828 }
829 }
830
831 }
832}
833
b75a7d8f 834#endif /* #if !UCONFIG_NO_FORMATTING */