]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cnumtst.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cnumtst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
7 *
8 * File CNUMTST.C
9 *
10 * Madhu Katragadda Creation
11 *
12 * Modification History:
13 *
14 * Date Name Description
15 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
16 * 07/15/99 helena Ported to HPUX 10/11 CC.
17 *********************************************************************************
18 */
19
20 /* C API TEST FOR NUMBER FORMAT */
21
22 #include "unicode/utypes.h"
23
24 #if !UCONFIG_NO_FORMATTING
25
26 #include "unicode/uloc.h"
27 #include "unicode/unum.h"
28 #include "unicode/ustring.h"
29 #include "cintltst.h"
30 #include "cnumtst.h"
31 #include "cmemory.h"
32 #include "putilimp.h"
33
34 #define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
35
36 void addNumForTest(TestNode** root);
37
38 #define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x)
39
40 void addNumForTest(TestNode** root)
41 {
42 TESTCASE(TestNumberFormat);
43 TESTCASE(TestSignificantDigits);
44 TESTCASE(TestNumberFormatPadding);
45 TESTCASE(TestInt64Format);
46 TESTCASE(TestNonExistentCurrency);
47 TESTCASE(TestRBNFFormat);
48 }
49
50 /** copy src to dst with unicode-escapes for values < 0x20 and > 0x7e, null terminate if possible */
51 static int32_t ustrToAstr(const UChar* src, int32_t srcLength, char* dst, int32_t dstLength) {
52 static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
53
54 char *p = dst;
55 const char *e = p + dstLength;
56 if (srcLength < 0) {
57 const UChar* s = src;
58 while (*s) {
59 ++s;
60 }
61 srcLength = (int32_t)(s - src);
62 }
63 while (p < e && --srcLength >= 0) {
64 UChar c = *src++;
65 if (c == 0xd || c == 0xa || c == 0x9 || (c>= 0x20 && c <= 0x7e)) {
66 *p++ = (char) c & 0x7f;
67 } else if (e - p >= 6) {
68 *p++ = '\\';
69 *p++ = 'u';
70 *p++ = hex[(c >> 12) & 0xf];
71 *p++ = hex[(c >> 8) & 0xf];
72 *p++ = hex[(c >> 4) & 0xf];
73 *p++ = hex[c & 0xf];
74 } else {
75 break;
76 }
77 }
78 if (p < e) {
79 *p = 0;
80 }
81 return (int32_t)(p - dst);
82 }
83
84 /* test Number Format API */
85 static void TestNumberFormat()
86 {
87 UChar *result=NULL;
88 UChar temp1[512];
89 UChar temp2[512];
90
91 UChar temp[5];
92
93 UChar prefix[5];
94 UChar suffix[5];
95 UChar symbol[20];
96 int32_t resultlength;
97 int32_t resultlengthneeded;
98 int32_t parsepos;
99 double d1;
100 int32_t l1;
101 double d = -10456.37;
102 double a = 1234.56, a1 = 1235.0;
103 int32_t l = 100000000;
104 UFieldPosition pos1;
105 UFieldPosition pos2;
106 int32_t numlocales;
107 int32_t i;
108
109 UNumberFormatAttribute attr;
110 UNumberFormatSymbol symType = UNUM_DECIMAL_SEPARATOR_SYMBOL;
111 int32_t newvalue;
112 UErrorCode status=U_ZERO_ERROR;
113 UNumberFormatStyle style= UNUM_DEFAULT;
114 UNumberFormat *pattern;
115 UNumberFormat *def, *fr, *cur_def, *cur_fr, *per_def, *per_fr,
116 *cur_frpattern, *myclone, *spellout_def;
117
118 /* Testing unum_open() with various Numberformat styles and locales*/
119 status = U_ZERO_ERROR;
120 log_verbose("Testing unum_open() with default style and locale\n");
121 def=unum_open(style, NULL,0,NULL, NULL,&status);
122 if(U_FAILURE(status))
123 log_err("Error in creating NumberFormat default using unum_open(): %s\n", myErrorName(status));
124
125 log_verbose("\nTesting unum_open() with french locale and default style(decimal)\n");
126 fr=unum_open(style,NULL,0, "fr_FR",NULL, &status);
127 if(U_FAILURE(status))
128 log_err("Error: could not create NumberFormat (french): %s\n", myErrorName(status));
129
130 log_verbose("\nTesting unum_open(currency,NULL,status)\n");
131 style=UNUM_CURRENCY;
132 /* Can't hardcode the result to assume the default locale is "en_US". */
133 cur_def=unum_open(style, NULL,0,"en_US", NULL, &status);
134 if(U_FAILURE(status))
135 log_err("Error: could not create NumberFormat using \n unum_open(currency, NULL, &status) %s\n",
136 myErrorName(status) );
137
138 log_verbose("\nTesting unum_open(currency, frenchlocale, status)\n");
139 cur_fr=unum_open(style,NULL,0, "fr_FR", NULL, &status);
140 if(U_FAILURE(status))
141 log_err("Error: could not create NumberFormat using unum_open(currency, french, &status): %s\n",
142 myErrorName(status));
143
144 log_verbose("\nTesting unum_open(percent, NULL, status)\n");
145 style=UNUM_PERCENT;
146 per_def=unum_open(style,NULL,0, NULL,NULL, &status);
147 if(U_FAILURE(status))
148 log_err("Error: could not create NumberFormat using unum_open(percent, NULL, &status): %s\n", myErrorName(status));
149
150 log_verbose("\nTesting unum_open(percent,frenchlocale, status)\n");
151 per_fr=unum_open(style, NULL,0,"fr_FR", NULL,&status);
152 if(U_FAILURE(status))
153 log_err("Error: could not create NumberFormat using unum_open(percent, french, &status): %s\n", myErrorName(status));
154
155 log_verbose("\nTesting unum_open(spellout, NULL, status)");
156 style=UNUM_SPELLOUT;
157 spellout_def=unum_open(style, NULL, 0, "en_US", NULL, &status);
158 if(U_FAILURE(status))
159 log_err("Error: could not create NumberFormat using unum_open(spellout, NULL, &status): %s\n", myErrorName(status));
160
161 /* Testing unum_clone(..) */
162 log_verbose("\nTesting unum_clone(fmt, status)");
163 status = U_ZERO_ERROR;
164 myclone = unum_clone(def,&status);
165 if(U_FAILURE(status))
166 log_err("Error: could not clone unum_clone(def, &status): %s\n", myErrorName(status));
167 else
168 {
169 log_verbose("unum_clone() successful\n");
170 }
171
172 /*Testing unum_getAvailable() and unum_countAvailable()*/
173 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
174 numlocales=unum_countAvailable();
175 if(numlocales < 0)
176 log_err("error in countAvailable");
177 else{
178 log_verbose("unum_countAvialable() successful\n");
179 log_verbose("The no: of locales where number formattting is applicable is %d\n", numlocales);
180 }
181 for(i=0;i<numlocales;i++)
182 {
183 log_verbose("%s\n", unum_getAvailable(i));
184 if (unum_getAvailable(i) == 0)
185 log_err("No locale for which number formatting patterns are applicable\n");
186 else
187 log_verbose("A locale %s for which number formatting patterns are applicable\n",unum_getAvailable(i));
188 }
189
190
191 /*Testing unum_format() and unum_formatdouble()*/
192 u_uastrcpy(temp1, "$100,000,000.00");
193
194 log_verbose("\nTesting unum_format() \n");
195 resultlength=0;
196 pos1.field = 0; /* Integer Section */
197 resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status);
198 if(status==U_BUFFER_OVERFLOW_ERROR)
199 {
200 status=U_ZERO_ERROR;
201 resultlength=resultlengthneeded+1;
202 result=(UChar*)malloc(sizeof(UChar) * resultlength);
203 /* for (i = 0; i < 100000; i++) */
204 {
205 unum_format(cur_def, l, result, resultlength, &pos1, &status);
206 }
207 }
208
209 if(U_FAILURE(status))
210 {
211 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
212 }
213 if(u_strcmp(result, temp1)==0)
214 log_verbose("Pass: Number formatting using unum_format() successful\n");
215 else
216 log_err("Fail: Error in number Formatting using unum_format()\n");
217 if(pos1.beginIndex == 1 && pos1.endIndex == 12)
218 log_verbose("Pass: Complete number formatting using unum_format() successful\n");
219 else
220 log_err("Fail: Error in complete number Formatting using unum_format()\nGot: b=%d end=%d\nExpected: b=1 end=12\n",
221 pos1.beginIndex, pos1.endIndex);
222
223 free(result);
224 result = 0;
225
226 log_verbose("\nTesting unum_formatDouble()\n");
227 u_uastrcpy(temp1, "($10,456.37)");
228 resultlength=0;
229 pos2.field = 1; /* Fractional Section */
230 resultlengthneeded=unum_formatDouble(cur_def, d, NULL, resultlength, &pos2, &status);
231 if(status==U_BUFFER_OVERFLOW_ERROR)
232 {
233 status=U_ZERO_ERROR;
234 resultlength=resultlengthneeded+1;
235 result=(UChar*)malloc(sizeof(UChar) * resultlength);
236 /* for (i = 0; i < 100000; i++) */
237 {
238 unum_formatDouble(cur_def, d, result, resultlength, &pos2, &status);
239 }
240 }
241 if(U_FAILURE(status))
242 {
243 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
244 }
245 if(u_strcmp(result, temp1)==0)
246 log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
247 else
248 log_err("FAIL: Error in number formatting using unum_formatDouble()\n");
249 if(pos2.beginIndex == 9 && pos2.endIndex == 11)
250 log_verbose("Pass: Complete number formatting using unum_format() successful\n");
251 else
252 log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=9 end=11",
253 pos1.beginIndex, pos1.endIndex);
254
255
256 /* Testing unum_parse() and unum_parseDouble() */
257 log_verbose("\nTesting unum_parseDouble()\n");
258 /* for (i = 0; i < 100000; i++)*/
259 {
260 parsepos=0;
261 d1=unum_parseDouble(cur_def, result, u_strlen(result), &parsepos, &status);
262 }
263 if(U_FAILURE(status))
264 {
265 log_err("parse failed. The error is : %s\n", myErrorName(status));
266 }
267
268 if(d1!=d)
269 log_err("Fail: Error in parsing\n");
270 else
271 log_verbose("Pass: parsing successful\n");
272 if (result)
273 free(result);
274 result = 0;
275
276
277 /* Testing unum_formatDoubleCurrency / unum_parseDoubleCurrency */
278 log_verbose("\nTesting unum_formatDoubleCurrency\n");
279 u_uastrcpy(temp1, "Y1,235");
280 temp1[0] = 0xA5; /* Yen sign */
281 u_uastrcpy(temp, "JPY");
282 resultlength=0;
283 pos2.field = 0; /* integer part */
284 resultlengthneeded=unum_formatDoubleCurrency(cur_def, a, temp, NULL, resultlength, &pos2, &status);
285 if (status==U_BUFFER_OVERFLOW_ERROR) {
286 status=U_ZERO_ERROR;
287 resultlength=resultlengthneeded+1;
288 result=(UChar*)malloc(sizeof(UChar) * resultlength);
289 unum_formatDoubleCurrency(cur_def, a, temp, result, resultlength, &pos2, &status);
290 }
291 if (U_FAILURE(status)) {
292 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
293 }
294 if (result && u_strcmp(result, temp1)==0) {
295 log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
296 } else {
297 log_err("FAIL: Error in number formatting using unum_formatDouble()\n");
298 }
299 if (pos2.beginIndex == 1 && pos2.endIndex == 6) {
300 log_verbose("Pass: Complete number formatting using unum_format() successful\n");
301 } else {
302 log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=1 end=6",
303 pos1.beginIndex, pos1.endIndex);
304 }
305
306 log_verbose("\nTesting unum_parseDoubleCurrency\n");
307 parsepos=0;
308 d1=unum_parseDoubleCurrency(cur_def, result, u_strlen(result), &parsepos, temp2, &status);
309 if (U_FAILURE(status)) {
310 log_err("parse failed. The error is : %s\n", myErrorName(status));
311 }
312 /* Note: a==1234.56, but on parse expect a1=1235.0 */
313 if (d1!=a1) {
314 log_err("Fail: Error in parsing currency, got %f, expected %f\n", d1, a1);
315 } else {
316 log_verbose("Pass: parsed currency ammount successfully\n");
317 }
318 if (u_strcmp(temp2, temp)==0) {
319 log_verbose("Pass: parsed correct currency\n");
320 } else {
321 log_err("Fail: parsed incorrect currency\n");
322 }
323
324 free(result);
325 result = 0;
326
327
328 /* performance testing */
329 u_uastrcpy(temp1, "$462.12345");
330 resultlength=u_strlen(temp1);
331 /* for (i = 0; i < 100000; i++) */
332 {
333 parsepos=0;
334 d1=unum_parseDouble(cur_def, temp1, resultlength, &parsepos, &status);
335 }
336 if(U_FAILURE(status))
337 {
338 log_err("parse failed. The error is : %s\n", myErrorName(status));
339 }
340
341 if(d1!=462.12345)
342 log_err("Fail: Error in parsing\n");
343 else
344 log_verbose("Pass: parsing successful\n");
345
346 free(result);
347
348 u_uastrcpy(temp1, "($10,456.3E1])");
349 parsepos=0;
350 d1=unum_parseDouble(cur_def, temp1, u_strlen(temp1), &parsepos, &status);
351 if(U_SUCCESS(status))
352 {
353 log_err("Error in unum_parseDouble(..., %s, ...): %s\n", temp1, myErrorName(status));
354 }
355
356
357 log_verbose("\nTesting unum_format()\n");
358 status=U_ZERO_ERROR;
359 resultlength=0;
360 parsepos=0;
361 resultlengthneeded=unum_format(per_fr, l, NULL, resultlength, &pos1, &status);
362 if(status==U_BUFFER_OVERFLOW_ERROR)
363 {
364 status=U_ZERO_ERROR;
365 resultlength=resultlengthneeded+1;
366 result=(UChar*)malloc(sizeof(UChar) * resultlength);
367 /* for (i = 0; i < 100000; i++)*/
368 {
369 unum_format(per_fr, l, result, resultlength, &pos1, &status);
370 }
371 }
372 if(U_FAILURE(status))
373 {
374 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
375 }
376
377
378 log_verbose("\nTesting unum_parse()\n");
379 /* for (i = 0; i < 100000; i++) */
380 {
381 parsepos=0;
382 l1=unum_parse(per_fr, result, u_strlen(result), &parsepos, &status);
383 }
384 if(U_FAILURE(status))
385 {
386 log_err("parse failed. The error is : %s\n", myErrorName(status));
387 }
388
389 if(l1!=l)
390 log_err("Fail: Error in parsing\n");
391 else
392 log_verbose("Pass: parsing successful\n");
393
394 free(result);
395
396 /* create a number format using unum_openPattern(....)*/
397 log_verbose("\nTesting unum_openPattern()\n");
398 u_uastrcpy(temp1, "#,##0.0#;(#,##0.0#)");
399 pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), NULL, NULL,&status);
400 if(U_FAILURE(status))
401 {
402 log_err("error in unum_openPattern(): %s\n", myErrorName(status) );;
403 }
404 else
405 log_verbose("Pass: unum_openPattern() works fine\n");
406
407 /*test for unum_toPattern()*/
408 log_verbose("\nTesting unum_toPattern()\n");
409 resultlength=0;
410 resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &status);
411 if(status==U_BUFFER_OVERFLOW_ERROR)
412 {
413 status=U_ZERO_ERROR;
414 resultlength=resultlengthneeded+1;
415 result=(UChar*)malloc(sizeof(UChar) * resultlength);
416 unum_toPattern(pattern, FALSE, result, resultlength, &status);
417 }
418 if(U_FAILURE(status))
419 {
420 log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status));
421 }
422 else
423 {
424 if(u_strcmp(result, temp1)!=0)
425 log_err("FAIL: Error in extracting the pattern using unum_toPattern()\n");
426 else
427 log_verbose("Pass: extracted the pattern correctly using unum_toPattern()\n");
428 free(result);
429 }
430
431 /*Testing unum_getSymbols() and unum_setSymbols()*/
432 log_verbose("\nTesting unum_getSymbols and unum_setSymbols()\n");
433 /*when we try to change the symbols of french to default we need to apply the pattern as well to fetch correct results */
434 resultlength=0;
435 resultlengthneeded=unum_toPattern(cur_def, FALSE, NULL, resultlength, &status);
436 if(status==U_BUFFER_OVERFLOW_ERROR)
437 {
438 status=U_ZERO_ERROR;
439 resultlength=resultlengthneeded+1;
440 result=(UChar*)malloc(sizeof(UChar) * resultlength);
441 unum_toPattern(cur_def, FALSE, result, resultlength, &status);
442 }
443 if(U_FAILURE(status))
444 {
445 log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status));
446 }
447
448 status=U_ZERO_ERROR;
449 cur_frpattern=unum_open(UNUM_IGNORE,result, u_strlen(result), "fr_FR",NULL, &status);
450 if(U_FAILURE(status))
451 {
452 log_err("error in unum_openPattern(): %s\n", myErrorName(status));
453 }
454
455 free(result);
456
457 /*getting the symbols of cur_def */
458 /*set the symbols of cur_frpattern to cur_def */
459 for (symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; symType < UNUM_FORMAT_SYMBOL_COUNT; symType++) {
460 status=U_ZERO_ERROR;
461 unum_getSymbol(cur_def, symType, temp1, sizeof(temp1), &status);
462 unum_setSymbol(cur_frpattern, symType, temp1, -1, &status);
463 if(U_FAILURE(status))
464 {
465 log_err("Error in get/set symbols: %s\n", myErrorName(status));
466 }
467 }
468
469 /*format to check the result */
470 resultlength=0;
471 resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status);
472 if(status==U_BUFFER_OVERFLOW_ERROR)
473 {
474 status=U_ZERO_ERROR;
475 resultlength=resultlengthneeded+1;
476 result=(UChar*)malloc(sizeof(UChar) * resultlength);
477 unum_format(cur_def, l, result, resultlength, &pos1, &status);
478 }
479 if(U_FAILURE(status))
480 {
481 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
482 }
483
484 if(U_FAILURE(status)){
485 log_err("Fail: error in unum_setSymbols: %s\n", myErrorName(status));
486 }
487 unum_applyPattern(cur_frpattern, FALSE, result, u_strlen(result),NULL,NULL);
488
489 for (symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; symType < UNUM_FORMAT_SYMBOL_COUNT; symType++) {
490 status=U_ZERO_ERROR;
491 unum_getSymbol(cur_def, symType, temp1, sizeof(temp1), &status);
492 unum_getSymbol(cur_frpattern, symType, temp2, sizeof(temp2), &status);
493 if(U_FAILURE(status) || u_strcmp(temp1, temp2) != 0)
494 {
495 log_err("Fail: error in getting symbols\n");
496 }
497 else
498 log_verbose("Pass: get and set symbols successful\n");
499 }
500
501 /*format and check with the previous result */
502
503 resultlength=0;
504 resultlengthneeded=unum_format(cur_frpattern, l, NULL, resultlength, &pos1, &status);
505 if(status==U_BUFFER_OVERFLOW_ERROR)
506 {
507 status=U_ZERO_ERROR;
508 resultlength=resultlengthneeded+1;
509 unum_format(cur_frpattern, l, temp1, resultlength, &pos1, &status);
510 }
511 if(U_FAILURE(status))
512 {
513 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
514 }
515 /* TODO:
516 * This test fails because we have not called unum_applyPattern().
517 * Currently, such an applyPattern() does not exist on the C API, and
518 * we have jitterbug 411 for it.
519 * Since it is close to the 1.5 release, I (markus) am disabling this test just
520 * for this release (I added the test itself only last week).
521 * For the next release, we need to fix this.
522 * Then, remove the uprv_strcmp("1.5", ...) and this comment, and the include "cstring.h" at the beginning of this file.
523 */
524 if(u_strcmp(result, temp1) != 0) {
525 log_err("Formatting failed after setting symbols. result=%s temp1=%s\n", result, temp1);
526 }
527
528
529 /*----------- */
530
531 free(result);
532
533 /* Testing unum_get/setSymbol() */
534 for(i = 0; i < UNUM_FORMAT_SYMBOL_COUNT; ++i) {
535 symbol[0] = (UChar)(0x41 + i);
536 symbol[1] = (UChar)(0x61 + i);
537 unum_setSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, 2, &status);
538 if(U_FAILURE(status)) {
539 log_err("Error from unum_setSymbol(%d): %s\n", i, myErrorName(status));
540 return;
541 }
542 }
543 for(i = 0; i < UNUM_FORMAT_SYMBOL_COUNT; ++i) {
544 resultlength = unum_getSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, sizeof(symbol)/U_SIZEOF_UCHAR, &status);
545 if(U_FAILURE(status)) {
546 log_err("Error from unum_getSymbol(%d): %s\n", i, myErrorName(status));
547 return;
548 }
549 if(resultlength != 2 || symbol[0] != 0x41 + i || symbol[1] != 0x61 + i) {
550 log_err("Failure in unum_getSymbol(%d): got unexpected symbol\n", i);
551 }
552 }
553 /*try getting from a bogus symbol*/
554 unum_getSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, sizeof(symbol)/U_SIZEOF_UCHAR, &status);
555 if(U_SUCCESS(status)){
556 log_err("Error : Expected U_ILLEGAL_ARGUMENT_ERROR for bogus symbol");
557 }
558 if(U_FAILURE(status)){
559 if(status != U_ILLEGAL_ARGUMENT_ERROR){
560 log_err("Error: Expected U_ILLEGAL_ARGUMENT_ERROR for bogus symbol, Got %s\n", myErrorName(status));
561 }
562 }
563 status=U_ZERO_ERROR;
564
565 /* Testing unum_getTextAttribute() and unum_setTextAttribute()*/
566 log_verbose("\nTesting getting and setting text attributes\n");
567 resultlength=5;
568 unum_getTextAttribute(cur_fr, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status);
569 if(U_FAILURE(status))
570 {
571 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
572 }
573 unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status);
574 if(U_FAILURE(status))
575 {
576 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
577 }
578 unum_getTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, suffix, resultlength, &status);
579 if(U_FAILURE(status))
580 {
581 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
582 }
583 if(u_strcmp(suffix,temp)!=0)
584 log_err("Fail:Error in setTextAttribute or getTextAttribute in setting and getting suffix\n");
585 else
586 log_verbose("Pass: setting and getting suffix works fine\n");
587 /*set it back to normal */
588 u_uastrcpy(temp,"$");
589 unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status);
590
591 /*checking some more text setter conditions */
592 u_uastrcpy(prefix, "+");
593 unum_setTextAttribute(def, UNUM_POSITIVE_PREFIX, prefix, u_strlen(prefix) , &status);
594 if(U_FAILURE(status))
595 {
596 log_err("error in setting the text attributes : %s\n", myErrorName(status));
597 }
598 unum_getTextAttribute(def, UNUM_POSITIVE_PREFIX, temp, resultlength, &status);
599 if(U_FAILURE(status))
600 {
601 log_err("error in getting the text attributes : %s\n", myErrorName(status));
602 }
603
604 if(u_strcmp(prefix, temp)!=0)
605 log_err("ERROR: get and setTextAttributes with positive prefix failed\n");
606 else
607 log_verbose("Pass: get and setTextAttributes with positive prefix works fine\n");
608
609 u_uastrcpy(prefix, "+");
610 unum_setTextAttribute(def, UNUM_NEGATIVE_PREFIX, prefix, u_strlen(prefix), &status);
611 if(U_FAILURE(status))
612 {
613 log_err("error in setting the text attributes : %s\n", myErrorName(status));
614 }
615 unum_getTextAttribute(def, UNUM_NEGATIVE_PREFIX, temp, resultlength, &status);
616 if(U_FAILURE(status))
617 {
618 log_err("error in getting the text attributes : %s\n", myErrorName(status));
619 }
620 if(u_strcmp(prefix, temp)!=0)
621 log_err("ERROR: get and setTextAttributes with negative prefix failed\n");
622 else
623 log_verbose("Pass: get and setTextAttributes with negative prefix works fine\n");
624
625 u_uastrcpy(suffix, "+");
626 unum_setTextAttribute(def, UNUM_NEGATIVE_SUFFIX, suffix, u_strlen(suffix) , &status);
627 if(U_FAILURE(status))
628 {
629 log_err("error in setting the text attributes: %s\n", myErrorName(status));
630 }
631
632 unum_getTextAttribute(def, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status);
633 if(U_FAILURE(status))
634 {
635 log_err("error in getting the text attributes : %s\n", myErrorName(status));
636 }
637 if(u_strcmp(suffix, temp)!=0)
638 log_err("ERROR: get and setTextAttributes with negative suffix failed\n");
639 else
640 log_verbose("Pass: get and settextAttributes with negative suffix works fine\n");
641
642 u_uastrcpy(suffix, "++");
643 unum_setTextAttribute(def, UNUM_POSITIVE_SUFFIX, suffix, u_strlen(suffix) , &status);
644 if(U_FAILURE(status))
645 {
646 log_err("error in setting the text attributes: %s\n", myErrorName(status));
647 }
648
649 unum_getTextAttribute(def, UNUM_POSITIVE_SUFFIX, temp, resultlength, &status);
650 if(U_FAILURE(status))
651 {
652 log_err("error in getting the text attributes : %s\n", myErrorName(status));
653 }
654 if(u_strcmp(suffix, temp)!=0)
655 log_err("ERROR: get and setTextAttributes with negative suffix failed\n");
656 else
657 log_verbose("Pass: get and settextAttributes with negative suffix works fine\n");
658
659 /*Testing unum_getAttribute and unum_setAttribute() */
660 log_verbose("\nTesting get and set Attributes\n");
661 attr=UNUM_GROUPING_SIZE;
662 newvalue=unum_getAttribute(def, attr);
663 newvalue=2;
664 unum_setAttribute(def, attr, newvalue);
665 if(unum_getAttribute(def,attr)!=2)
666 log_err("Fail: error in setting and getting attributes for UNUM_GROUPING_SIZE\n");
667 else
668 log_verbose("Pass: setting and getting attributes for UNUM_GROUPING_SIZE works fine\n");
669
670 attr=UNUM_MULTIPLIER;
671 newvalue=unum_getAttribute(def, attr);
672 newvalue=8;
673 unum_setAttribute(def, attr, newvalue);
674 if(unum_getAttribute(def,attr) != 8)
675 log_err("error in setting and getting attributes for UNUM_MULTIPLIER\n");
676 else
677 log_verbose("Pass:setting and getting attributes for UNUM_MULTIPLIER works fine\n");
678
679 attr=UNUM_SECONDARY_GROUPING_SIZE;
680 newvalue=unum_getAttribute(def, attr);
681 newvalue=2;
682 unum_setAttribute(def, attr, newvalue);
683 if(unum_getAttribute(def,attr) != 2)
684 log_err("error in setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE\n");
685 else
686 log_verbose("Pass:setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE works fine\n");
687
688 /*testing set and get Attributes extensively */
689 log_verbose("\nTesting get and set attributes extensively\n");
690 for(attr=UNUM_PARSE_INT_ONLY; attr<= UNUM_PADDING_POSITION; attr=(UNumberFormatAttribute)((int32_t)attr + 1) )
691 {
692 newvalue=unum_getAttribute(fr, attr);
693 unum_setAttribute(def, attr, newvalue);
694 if(unum_getAttribute(def,attr)!=unum_getAttribute(fr, attr))
695 log_err("error in setting and getting attributes\n");
696 else
697 log_verbose("Pass: attributes set and retrieved successfully\n");
698 }
699
700 /*testing spellout format to make sure we can use it successfully.*/
701 log_verbose("\nTesting spellout format\n");
702 if (spellout_def)
703 {
704 static const int32_t values[] = { 0, -5, 105, 1005, 105050 };
705 for (i = 0; i < LENGTH(values); ++i) {
706 UChar buffer[128];
707 int32_t len;
708 int32_t value = values[i];
709 status = U_ZERO_ERROR;
710 len = unum_format(spellout_def, value, buffer, LENGTH(buffer), NULL, &status);
711 if(U_FAILURE(status)) {
712 log_err("Error in formatting using unum_format(spellout_fmt, ...): %s\n", myErrorName(status));
713 } else {
714 int32_t pp = 0;
715 int32_t parseResult;
716 char logbuf[256];
717 ustrToAstr(buffer, len, logbuf, LENGTH(logbuf));
718 log_verbose("formatted %d as '%s', length: %d\n", value, logbuf, len);
719
720 parseResult = unum_parse(spellout_def, buffer, len, &pp, &status);
721 if (U_FAILURE(status)) {
722 log_err("Error in parsing using unum_format(spellout_fmt, ...): %s\n", myErrorName(status));
723 } else if (parseResult != value) {
724 log_err("unum_format result %d != value %d\n", parseResult, value);
725 }
726 }
727 }
728 }
729 else {
730 log_err("Spellout format is unavailable\n");
731 }
732
733 /*closing the NumberFormat() using unum_close(UNumberFormat*)")*/
734 unum_close(def);
735 unum_close(fr);
736 unum_close(cur_def);
737 unum_close(cur_fr);
738 unum_close(per_def);
739 unum_close(per_fr);
740 unum_close(spellout_def);
741 unum_close(pattern);
742 unum_close(cur_frpattern);
743 unum_close(myclone);
744
745 }
746
747 static void TestSignificantDigits()
748 {
749 UChar temp[128];
750 int32_t resultlengthneeded;
751 int32_t resultlength;
752 UErrorCode status = U_ZERO_ERROR;
753 UChar *result = NULL;
754 UNumberFormat* fmt;
755 double d = 123456.789;
756
757 u_uastrcpy(temp, "###0.0#");
758 fmt=unum_open(UNUM_IGNORE, temp, -1, NULL, NULL,&status);
759 if (U_FAILURE(status)) {
760 log_err("got unexpected error for unum_open: '%s'\n", u_errorName(status));
761 }
762 unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE);
763 unum_setAttribute(fmt, UNUM_MAX_SIGNIFICANT_DIGITS, 6);
764
765 u_uastrcpy(temp, "123457");
766 resultlength=0;
767 resultlengthneeded=unum_formatDouble(fmt, d, NULL, resultlength, NULL, &status);
768 if(status==U_BUFFER_OVERFLOW_ERROR)
769 {
770 status=U_ZERO_ERROR;
771 resultlength=resultlengthneeded+1;
772 result=(UChar*)malloc(sizeof(UChar) * resultlength);
773 unum_formatDouble(fmt, d, result, resultlength, NULL, &status);
774 }
775 if(U_FAILURE(status))
776 {
777 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
778 return;
779 }
780 if(u_strcmp(result, temp)==0)
781 log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
782 else
783 log_err("FAIL: Error in number formatting using unum_formatDouble()\n");
784 free(result);
785 unum_close(fmt);
786 }
787
788 static void TestNumberFormatPadding()
789 {
790 UChar *result=NULL;
791 UChar temp1[512];
792
793 UErrorCode status=U_ZERO_ERROR;
794 int32_t resultlength;
795 int32_t resultlengthneeded;
796 UNumberFormat *pattern;
797 double d1;
798 double d = -10456.37;
799 UFieldPosition pos1;
800 int32_t parsepos;
801
802 /* create a number format using unum_openPattern(....)*/
803 log_verbose("\nTesting unum_openPattern() with padding\n");
804 u_uastrcpy(temp1, "*#,##0.0#*;(#,##0.0#)");
805 status=U_ZERO_ERROR;
806 pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), NULL, NULL,&status);
807 if(U_SUCCESS(status))
808 {
809 log_err("error in unum_openPattern(%s): %s\n", temp1, myErrorName(status) );;
810 }
811 else
812 {
813 unum_close(pattern);
814 }
815
816 /* u_uastrcpy(temp1, "*x#,###,###,##0.0#;(*x#,###,###,##0.0#)"); */
817 u_uastrcpy(temp1, "*x#,###,###,##0.0#;*x(###,###,##0.0#)");
818 status=U_ZERO_ERROR;
819 pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), "en_US",NULL, &status);
820 if(U_FAILURE(status))
821 {
822 log_err("error in padding unum_openPattern(%s): %s\n", temp1, myErrorName(status) );;
823 }
824 else {
825 log_verbose("Pass: padding unum_openPattern() works fine\n");
826
827 /*test for unum_toPattern()*/
828 log_verbose("\nTesting padding unum_toPattern()\n");
829 resultlength=0;
830 resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &status);
831 if(status==U_BUFFER_OVERFLOW_ERROR)
832 {
833 status=U_ZERO_ERROR;
834 resultlength=resultlengthneeded+1;
835 result=(UChar*)malloc(sizeof(UChar) * resultlength);
836 unum_toPattern(pattern, FALSE, result, resultlength, &status);
837 }
838 if(U_FAILURE(status))
839 {
840 log_err("error in extracting the padding pattern from UNumberFormat: %s\n", myErrorName(status));
841 }
842 else
843 {
844 if(u_strcmp(result, temp1)!=0)
845 log_err("FAIL: Error in extracting the padding pattern using unum_toPattern()\n");
846 else
847 log_verbose("Pass: extracted the padding pattern correctly using unum_toPattern()\n");
848 free(result);
849 }
850 /* u_uastrcpy(temp1, "(xxxxxxx10,456.37)"); */
851 u_uastrcpy(temp1, "xxxxx(10,456.37)");
852 resultlength=0;
853 pos1.field = 1; /* Fraction field */
854 resultlengthneeded=unum_formatDouble(pattern, d, NULL, resultlength, &pos1, &status);
855 if(status==U_BUFFER_OVERFLOW_ERROR)
856 {
857 status=U_ZERO_ERROR;
858 resultlength=resultlengthneeded+1;
859 result=(UChar*)malloc(sizeof(UChar) * resultlength);
860 unum_formatDouble(pattern, d, result, resultlength, NULL, &status);
861 }
862 if(U_FAILURE(status))
863 {
864 log_err("Error in formatting using unum_formatDouble(.....) with padding : %s\n", myErrorName(status));
865 }
866 else
867 {
868 if(u_strcmp(result, temp1)==0)
869 log_verbose("Pass: Number Formatting using unum_formatDouble() padding Successful\n");
870 else
871 log_err("FAIL: Error in number formatting using unum_formatDouble() with padding\n");
872 if(pos1.beginIndex == 13 && pos1.endIndex == 15)
873 log_verbose("Pass: Complete number formatting using unum_formatDouble() successful\n");
874 else
875 log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=13 end=15\n",
876 pos1.beginIndex, pos1.endIndex);
877
878
879 /* Testing unum_parse() and unum_parseDouble() */
880 log_verbose("\nTesting padding unum_parseDouble()\n");
881 parsepos=0;
882 d1=unum_parseDouble(pattern, result, u_strlen(result), &parsepos, &status);
883 if(U_FAILURE(status))
884 {
885 log_err("padding parse failed. The error is : %s\n", myErrorName(status));
886 }
887
888 if(d1!=d)
889 log_err("Fail: Error in padding parsing\n");
890 else
891 log_verbose("Pass: padding parsing successful\n");
892 free(result);
893 }
894 }
895
896 unum_close(pattern);
897 }
898
899 static UBool
900 withinErr(double a, double b, double err) {
901 return uprv_fabs(a - b) < uprv_fabs(a * err);
902 }
903
904 static void TestInt64Format() {
905 UChar temp1[512];
906 UChar result[512];
907 UNumberFormat *fmt;
908 UErrorCode status = U_ZERO_ERROR;
909 const double doubleInt64Max = (double)U_INT64_MAX;
910 const double doubleInt64Min = (double)U_INT64_MIN;
911 const double doubleBig = 10.0 * (double)U_INT64_MAX;
912 int32_t val32;
913 int64_t val64;
914 double valDouble;
915 int32_t parsepos;
916
917 /* create a number format using unum_openPattern(....) */
918 log_verbose("\nTesting Int64Format\n");
919 u_uastrcpy(temp1, "#.#E0");
920 fmt = unum_open(UNUM_IGNORE, temp1, u_strlen(temp1), NULL, NULL, &status);
921 if(U_FAILURE(status)) {
922 log_err("error in unum_openPattern(): %s\n", myErrorName(status));
923 } else {
924 unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 20);
925 unum_formatInt64(fmt, U_INT64_MAX, result, 512, NULL, &status);
926 if (U_FAILURE(status)) {
927 log_err("error in unum_format(): %s\n", myErrorName(status));
928 } else {
929 log_verbose("format int64max: '%s'\n", result);
930 parsepos = 0;
931 val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
932 if (status != U_INVALID_FORMAT_ERROR) {
933 log_err("parse didn't report error: %s\n", myErrorName(status));
934 } else if (val32 != INT32_MAX) {
935 log_err("parse didn't pin return value, got: %d\n", val32);
936 }
937
938 status = U_ZERO_ERROR;
939 parsepos = 0;
940 val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
941 if (U_FAILURE(status)) {
942 log_err("parseInt64 returned error: %s\n", myErrorName(status));
943 } else if (val64 != U_INT64_MAX) {
944 log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
945 }
946
947 status = U_ZERO_ERROR;
948 parsepos = 0;
949 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
950 if (U_FAILURE(status)) {
951 log_err("parseDouble returned error: %s\n", myErrorName(status));
952 } else if (valDouble != doubleInt64Max) {
953 log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
954 }
955 }
956
957 unum_formatInt64(fmt, U_INT64_MIN, result, 512, NULL, &status);
958 if (U_FAILURE(status)) {
959 log_err("error in unum_format(): %s\n", myErrorName(status));
960 } else {
961 log_verbose("format int64min: '%s'\n", result);
962 parsepos = 0;
963 val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
964 if (status != U_INVALID_FORMAT_ERROR) {
965 log_err("parse didn't report error: %s\n", myErrorName(status));
966 } else if (val32 != INT32_MIN) {
967 log_err("parse didn't pin return value, got: %d\n", val32);
968 }
969
970 status = U_ZERO_ERROR;
971 parsepos = 0;
972 val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
973 if (U_FAILURE(status)) {
974 log_err("parseInt64 returned error: %s\n", myErrorName(status));
975 } else if (val64 != U_INT64_MIN) {
976 log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
977 }
978
979 status = U_ZERO_ERROR;
980 parsepos = 0;
981 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
982 if (U_FAILURE(status)) {
983 log_err("parseDouble returned error: %s\n", myErrorName(status));
984 } else if (valDouble != doubleInt64Min) {
985 log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
986 }
987 }
988
989 unum_formatDouble(fmt, doubleBig, result, 512, NULL, &status);
990 if (U_FAILURE(status)) {
991 log_err("error in unum_format(): %s\n", myErrorName(status));
992 } else {
993 log_verbose("format doubleBig: '%s'\n", result);
994 parsepos = 0;
995 val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
996 if (status != U_INVALID_FORMAT_ERROR) {
997 log_err("parse didn't report error: %s\n", myErrorName(status));
998 } else if (val32 != INT32_MAX) {
999 log_err("parse didn't pin return value, got: %d\n", val32);
1000 }
1001
1002 status = U_ZERO_ERROR;
1003 parsepos = 0;
1004 val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
1005 if (status != U_INVALID_FORMAT_ERROR) {
1006 log_err("parseInt64 didn't report error error: %s\n", myErrorName(status));
1007 } else if (val64 != U_INT64_MAX) {
1008 log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
1009 }
1010
1011 status = U_ZERO_ERROR;
1012 parsepos = 0;
1013 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
1014 if (U_FAILURE(status)) {
1015 log_err("parseDouble returned error: %s\n", myErrorName(status));
1016 } else if (!withinErr(valDouble, doubleBig, 1e-15)) {
1017 log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
1018 }
1019 }
1020 }
1021 unum_close(fmt);
1022 }
1023
1024
1025 static void test_fmt(UNumberFormat* fmt, UBool isDecimal) {
1026 char temp[512];
1027 UChar buffer[512];
1028 int32_t BUFSIZE = sizeof(buffer)/sizeof(buffer[0]);
1029 double vals[] = {
1030 -.2, 0, .2, 5.5, 15.2, 250, 123456789
1031 };
1032 int i;
1033
1034 for (i = 0; i < sizeof(vals)/sizeof(vals[0]); ++i) {
1035 UErrorCode status = U_ZERO_ERROR;
1036 unum_formatDouble(fmt, vals[i], buffer, BUFSIZE, NULL, &status);
1037 if (U_FAILURE(status)) {
1038 log_err("failed to format: %g, returned %s\n", vals[i], u_errorName(status));
1039 } else {
1040 u_austrcpy(temp, buffer);
1041 log_verbose("formatting %g returned '%s'\n", vals[i], temp);
1042 }
1043 }
1044
1045 /* check APIs now */
1046 {
1047 UErrorCode status = U_ZERO_ERROR;
1048 UParseError perr;
1049 u_uastrcpy(buffer, "#,##0.0#");
1050 unum_applyPattern(fmt, FALSE, buffer, -1, &perr, &status);
1051 if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) {
1052 log_err("got unexpected error for applyPattern: '%s'\n", u_errorName(status));
1053 }
1054 }
1055
1056 {
1057 int isLenient = unum_getAttribute(fmt, UNUM_LENIENT_PARSE);
1058 log_verbose("lenient: 0x%x\n", isLenient);
1059 if (isDecimal ? (isLenient != -1) : (isLenient == TRUE)) {
1060 log_err("didn't expect lenient value: %d\n", isLenient);
1061 }
1062
1063 unum_setAttribute(fmt, UNUM_LENIENT_PARSE, TRUE);
1064 isLenient = unum_getAttribute(fmt, UNUM_LENIENT_PARSE);
1065 if (isDecimal ? (isLenient != -1) : (isLenient == FALSE)) {
1066 log_err("didn't expect lenient value after set: %d\n", isLenient);
1067 }
1068 }
1069
1070 {
1071 double val2;
1072 double val = unum_getDoubleAttribute(fmt, UNUM_LENIENT_PARSE);
1073 if (val != -1) {
1074 log_err("didn't expect double attribute\n");
1075 }
1076 val = unum_getDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT);
1077 if ((val == -1) == isDecimal) {
1078 log_err("didn't expect -1 rounding increment\n");
1079 }
1080 unum_setDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT, val+.5);
1081 val2 = unum_getDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT);
1082 if (isDecimal && (val2 - val != .5)) {
1083 log_err("set rounding increment had no effect on decimal format");
1084 }
1085 }
1086
1087 {
1088 UErrorCode status = U_ZERO_ERROR;
1089 int len = unum_getTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, BUFSIZE, &status);
1090 if (isDecimal ? (status != U_UNSUPPORTED_ERROR) : U_FAILURE(status)) {
1091 log_err("got unexpected error for get default ruleset: '%s'\n", u_errorName(status));
1092 }
1093 if (U_SUCCESS(status)) {
1094 u_austrcpy(temp, buffer);
1095 log_verbose("default ruleset: '%s'\n", temp);
1096 }
1097
1098 status = U_ZERO_ERROR;
1099 len = unum_getTextAttribute(fmt, UNUM_PUBLIC_RULESETS, buffer, BUFSIZE, &status);
1100 if (isDecimal ? (status != U_UNSUPPORTED_ERROR) : U_FAILURE(status)) {
1101 log_err("got unexpected error for get public rulesets: '%s'\n", u_errorName(status));
1102 }
1103 if (U_SUCCESS(status)) {
1104 u_austrcpy(temp, buffer);
1105 log_verbose("public rulesets: '%s'\n", temp);
1106
1107 /* set the default ruleset to the first one found, and retry */
1108
1109 if (len > 0) {
1110 for (i = 0; i < len && temp[i] != ';'; ++i){};
1111 if (i < len) {
1112 buffer[i] = 0;
1113 unum_setTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, -1, &status);
1114 if (U_FAILURE(status)) {
1115 log_err("unexpected error setting default ruleset: '%s'\n", u_errorName(status));
1116 } else {
1117 int len2 = unum_getTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, BUFSIZE, &status);
1118 if (U_FAILURE(status)) {
1119 log_err("could not fetch default ruleset: '%s'\n", u_errorName(status));
1120 } else if (len2 != i) {
1121 u_austrcpy(temp, buffer);
1122 log_err("unexpected ruleset len: %d ex: %d val: %s\n", len2, i, temp);
1123 } else {
1124 for (i = 0; i < sizeof(vals)/sizeof(vals[0]); ++i) {
1125 status = U_ZERO_ERROR;
1126 unum_formatDouble(fmt, vals[i], buffer, BUFSIZE, NULL, &status);
1127 if (U_FAILURE(status)) {
1128 log_err("failed to format: %g, returned %s\n", vals[i], u_errorName(status));
1129 } else {
1130 u_austrcpy(temp, buffer);
1131 log_verbose("formatting %g returned '%s'\n", vals[i], temp);
1132 }
1133 }
1134 }
1135 }
1136 }
1137 }
1138 }
1139 }
1140
1141 {
1142 UErrorCode status = U_ZERO_ERROR;
1143 unum_toPattern(fmt, FALSE, buffer, BUFSIZE, &status);
1144 if (U_SUCCESS(status)) {
1145 u_austrcpy(temp, buffer);
1146 log_verbose("pattern: '%s'\n", temp);
1147 } else if (status != U_BUFFER_OVERFLOW_ERROR) {
1148 log_err("toPattern failed unexpectedly: %s\n", u_errorName(status));
1149 } else {
1150 log_verbose("pattern too long to display\n");
1151 }
1152 }
1153
1154 {
1155 UErrorCode status = U_ZERO_ERROR;
1156 int len = unum_getSymbol(fmt, UNUM_CURRENCY_SYMBOL, buffer, BUFSIZE, &status);
1157 if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) {
1158 log_err("unexpected error getting symbol: '%s'\n", u_errorName(status));
1159 }
1160
1161 unum_setSymbol(fmt, UNUM_CURRENCY_SYMBOL, buffer, len, &status);
1162 if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) {
1163 log_err("unexpected error setting symbol: '%s'\n", u_errorName(status));
1164 }
1165 }
1166 }
1167
1168 static void TestNonExistentCurrency() {
1169 UNumberFormat *format;
1170 UErrorCode status = U_ZERO_ERROR;
1171 UChar currencySymbol[8];
1172 static const UChar QQQ[] = {0x51, 0x51, 0x51, 0};
1173
1174 /* Get a non-existent currency and make sure it returns the correct currency code. */
1175 format = unum_open(UNUM_CURRENCY, NULL, 0, "th_TH@currency=QQQ", NULL, &status);
1176 if (format == NULL || U_FAILURE(status)) {
1177 log_err("unum_open did not return expected result for non-existent requested currency: '%s'\n", u_errorName(status));
1178 }
1179 else {
1180 unum_getSymbol(format,
1181 UNUM_CURRENCY_SYMBOL,
1182 currencySymbol,
1183 sizeof(currencySymbol)/sizeof(currencySymbol[0]),
1184 &status);
1185 if (u_strcmp(currencySymbol, QQQ) != 0) {
1186 log_err("unum_open set the currency to QQQ\n");
1187 }
1188 }
1189 unum_close(format);
1190 }
1191
1192 static void TestRBNFFormat() {
1193 UErrorCode status;
1194 UParseError perr;
1195 UChar pat[1024];
1196 UChar tempUChars[512];
1197 UNumberFormat *formats[5];
1198 int COUNT = sizeof(formats)/sizeof(formats[0]);
1199 int i;
1200
1201 for (i = 0; i < COUNT; ++i) {
1202 formats[i] = 0;
1203 }
1204
1205 /* instantiation */
1206 status = U_ZERO_ERROR;
1207 u_uastrcpy(pat, "#,##0.0#;(#,##0.0#)");
1208 formats[0] = unum_open(UNUM_PATTERN_DECIMAL, pat, -1, "en_US", &perr, &status);
1209 if (U_FAILURE(status)) {
1210 log_err("unable to open decimal pattern");
1211 }
1212
1213 status = U_ZERO_ERROR;
1214 formats[1] = unum_open(UNUM_SPELLOUT, NULL, 0, "en_US", &perr, &status);
1215 if (U_FAILURE(status)) {
1216 log_err("unable to open spellout");
1217 }
1218
1219 status = U_ZERO_ERROR;
1220 formats[2] = unum_open(UNUM_ORDINAL, NULL, 0, "en_US", &perr, &status);
1221 if (U_FAILURE(status)) {
1222 log_err("unable to open ordinal");
1223 }
1224
1225 status = U_ZERO_ERROR;
1226 formats[3] = unum_open(UNUM_DURATION, NULL, 0, "en_US", &perr, &status);
1227 if (U_FAILURE(status)) {
1228 log_err("unable to open duration");
1229 }
1230
1231 status = U_ZERO_ERROR;
1232 u_uastrcpy(pat,
1233 "%standard:\n"
1234 "-x: minus >>;\n"
1235 "x.x: << point >>;\n"
1236 "zero; one; two; three; four; five; six; seven; eight; nine;\n"
1237 "ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n"
1238 "seventeen; eighteen; nineteen;\n"
1239 "20: twenty[->>];\n"
1240 "30: thirty[->>];\n"
1241 "40: forty[->>];\n"
1242 "50: fifty[->>];\n"
1243 "60: sixty[->>];\n"
1244 "70: seventy[->>];\n"
1245 "80: eighty[->>];\n"
1246 "90: ninety[->>];\n"
1247 "100: =#,##0=;\n");
1248 u_uastrcpy(tempUChars,
1249 "%simple:\n"
1250 "=%standard=;\n"
1251 "20: twenty[ and change];\n"
1252 "30: thirty[ and change];\n"
1253 "40: forty[ and change];\n"
1254 "50: fifty[ and change];\n"
1255 "60: sixty[ and change];\n"
1256 "70: seventy[ and change];\n"
1257 "80: eighty[ and change];\n"
1258 "90: ninety[ and change];\n"
1259 "100: =#,##0=;\n"
1260 "%bogus:\n"
1261 "0.x: tiny;\n"
1262 "x.x: << point something;\n"
1263 "=%standard=;\n"
1264 "20: some reasonable number;\n"
1265 "100: some substantial number;\n"
1266 "100,000,000: some huge number;\n");
1267 /* This is to get around some compiler warnings about char * string length. */
1268 u_strcat(pat, tempUChars);
1269 formats[4] = unum_open(UNUM_PATTERN_RULEBASED, pat, -1, "en_US", &perr, &status);
1270 if (U_FAILURE(status)) {
1271 log_err("unable to open rulebased pattern");
1272 }
1273
1274 for (i = 0; i < COUNT; ++i) {
1275 log_verbose("\n\ntesting format %d\n", i);
1276 test_fmt(formats[i], (UBool)(i == 0));
1277 }
1278
1279 for (i = 0; i < COUNT; ++i) {
1280 unum_close(formats[i]);
1281 }
1282 }
1283
1284 #endif /* #if !UCONFIG_NO_FORMATTING */