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