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