1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 /********************************************************************************
9 *
10 * File CNUMTST.C
11 *
12 * Madhu Katragadda Creation
13 *
14 * Modification History:
15 *
16 * Date Name Description
17 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
18 * 07/15/99 helena Ported to HPUX 10/11 CC.
19 *********************************************************************************
20 */
21
22 /* C API TEST FOR NUMBER FORMAT */
23
24 #include "unicode/utypes.h"
25
26 #if !UCONFIG_NO_FORMATTING
27
28 #include "unicode/uloc.h"
29 #include "unicode/umisc.h"
30 #include "unicode/unum.h"
31 #include "unicode/unumsys.h"
32 #include "unicode/ustring.h"
33 #include "unicode/udisplaycontext.h"
34
35 #include "cintltst.h"
36 #include "cnumtst.h"
37 #include "cmemory.h"
38 #include "cstring.h"
39 #include "putilimp.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42
tagAssert(const char * f,int32_t l,const char * msg)43 static const char *tagAssert(const char *f, int32_t l, const char *msg) {
44 static char _fileline[1000];
45 sprintf(_fileline, "%s:%d: ASSERT_TRUE(%s)", f, l, msg);
46 return _fileline;
47 }
48
49 #define ASSERT_TRUE(x) assertTrue(tagAssert(__FILE__, __LINE__, #x), (x))
50
51 void addNumForTest(TestNode** root);
52 static void TestTextAttributeCrash(void);
53 static void TestNBSPInPattern(void);
54 static void TestInt64Parse(void);
55 static void TestParseCurrency(void);
56 static void TestMaxInt(void);
57 static void TestNoExponent(void);
58 static void TestUFormattable(void);
59 static void TestUNumberingSystem(void);
60 static void TestCurrencyIsoPluralFormat(void);
61 static void TestContext(void);
62 static void TestCurrencyUsage(void);
63 static void TestCurrFmtNegSameAsPositive(void);
64 static void TestVariousStylesAndAttributes(void);
65 static void TestParseCurrPatternWithDecStyle(void);
66 static void TestFormatForFields(void);
67 static void TestRBNFRounding(void);
68 static void Test12052_NullPointer(void);
69
70 #define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x)
71
addNumForTest(TestNode ** root)72 void addNumForTest(TestNode** root)
73 {
74 TESTCASE(TestNumberFormat);
75 TESTCASE(TestSpelloutNumberParse);
76 TESTCASE(TestSignificantDigits);
77 TESTCASE(TestSigDigRounding);
78 TESTCASE(TestNumberFormatPadding);
79 TESTCASE(TestInt64Format);
80 TESTCASE(TestNonExistentCurrency);
81 TESTCASE(TestCurrencyRegression);
82 TESTCASE(TestTextAttributeCrash);
83 TESTCASE(TestRBNFFormat);
84 TESTCASE(TestRBNFRounding);
85 TESTCASE(TestNBSPInPattern);
86 TESTCASE(TestInt64Parse);
87 TESTCASE(TestParseZero);
88 TESTCASE(TestParseCurrency);
89 TESTCASE(TestCloneWithRBNF);
90 TESTCASE(TestMaxInt);
91 TESTCASE(TestNoExponent);
92 TESTCASE(TestUFormattable);
93 TESTCASE(TestUNumberingSystem);
94 TESTCASE(TestCurrencyIsoPluralFormat);
95 TESTCASE(TestContext);
96 TESTCASE(TestCurrencyUsage);
97 TESTCASE(TestCurrFmtNegSameAsPositive);
98 TESTCASE(TestVariousStylesAndAttributes);
99 TESTCASE(TestParseCurrPatternWithDecStyle);
100 TESTCASE(TestFormatForFields);
101 TESTCASE(Test12052_NullPointer);
102 }
103
104 /* test Parse int 64 */
105
TestInt64Parse()106 static void TestInt64Parse()
107 {
108
109 UErrorCode st = U_ZERO_ERROR;
110 UErrorCode* status = &st;
111
112 const char* st1 = "009223372036854775808";
113 const int size = 21;
114 UChar text[21];
115
116
117 UNumberFormat* nf;
118
119 int64_t a;
120
121 u_charsToUChars(st1, text, size);
122 nf = unum_open(UNUM_DEFAULT, NULL, -1, NULL, NULL, status);
123
124 if(U_FAILURE(*status))
125 {
126 log_data_err("Error in unum_open() %s \n", myErrorName(*status));
127 return;
128 }
129
130 log_verbose("About to test unum_parseInt64() with out of range number\n");
131
132 a = unum_parseInt64(nf, text, size, 0, status);
133 (void)a; /* Suppress set but not used warning. */
134
135
136 if(!U_FAILURE(*status))
137 {
138 log_err("Error in unum_parseInt64(): %s \n", myErrorName(*status));
139 }
140 else
141 {
142 log_verbose("unum_parseInt64() successful\n");
143 }
144
145 unum_close(nf);
146 return;
147 }
148
149 /* test Number Format API */
TestNumberFormat()150 static void TestNumberFormat()
151 {
152 UChar *result=NULL;
153 UChar temp1[512];
154 UChar temp2[512];
155
156 UChar temp[5];
157
158 UChar prefix[5];
159 UChar suffix[5];
160 UChar symbol[20];
161 int32_t resultlength;
162 int32_t resultlengthneeded;
163 int32_t parsepos;
164 double d1 = -1.0;
165 int32_t l1;
166 double d = -10456.37;
167 double a = 1234.56, a1 = 1235.0;
168 int32_t l = 100000000;
169 UFieldPosition pos1;
170 UFieldPosition pos2;
171 int32_t numlocales;
172 int32_t i;
173
174 UNumberFormatAttribute attr;
175 UNumberFormatSymbol symType = UNUM_DECIMAL_SEPARATOR_SYMBOL;
176 int32_t newvalue;
177 UErrorCode status=U_ZERO_ERROR;
178 UNumberFormatStyle style= UNUM_DEFAULT;
179 UNumberFormat *pattern;
180 UNumberFormat *def, *fr, *cur_def, *cur_fr, *per_def, *per_fr,
181 *cur_frpattern, *myclone, *spellout_def;
182
183 /* Testing unum_open() with various Numberformat styles and locales*/
184 status = U_ZERO_ERROR;
185 log_verbose("Testing unum_open() with default style and locale\n");
186 def=unum_open(style, NULL,0,NULL, NULL,&status);
187
188 /* Might as well pack it in now if we can't even get a default NumberFormat... */
189 if(U_FAILURE(status))
190 {
191 log_data_err("Error in creating default NumberFormat using unum_open(): %s (Are you missing data?)\n", myErrorName(status));
192 return;
193 }
194
195 log_verbose("\nTesting unum_open() with french locale and default style(decimal)\n");
196 fr=unum_open(style,NULL,0, "fr_FR",NULL, &status);
197 if(U_FAILURE(status))
198 log_err("Error: could not create NumberFormat (french): %s\n", myErrorName(status));
199
200 log_verbose("\nTesting unum_open(currency,NULL,status)\n");
201 style=UNUM_CURRENCY;
202 /* Can't hardcode the result to assume the default locale is "en_US". */
203 cur_def=unum_open(style, NULL,0,"en_US", NULL, &status);
204 if(U_FAILURE(status))
205 log_err("Error: could not create NumberFormat using \n unum_open(currency, NULL, &status) %s\n",
206 myErrorName(status) );
207
208 log_verbose("\nTesting unum_open(currency, frenchlocale, status)\n");
209 cur_fr=unum_open(style,NULL,0, "fr_FR", NULL, &status);
210 if(U_FAILURE(status))
211 log_err("Error: could not create NumberFormat using unum_open(currency, french, &status): %s\n",
212 myErrorName(status));
213
214 log_verbose("\nTesting unum_open(percent, NULL, status)\n");
215 style=UNUM_PERCENT;
216 per_def=unum_open(style,NULL,0, NULL,NULL, &status);
217 if(U_FAILURE(status))
218 log_err("Error: could not create NumberFormat using unum_open(percent, NULL, &status): %s\n", myErrorName(status));
219
220 log_verbose("\nTesting unum_open(percent,frenchlocale, status)\n");
221 per_fr=unum_open(style, NULL,0,"fr_FR", NULL,&status);
222 if(U_FAILURE(status))
223 log_err("Error: could not create NumberFormat using unum_open(percent, french, &status): %s\n", myErrorName(status));
224
225 log_verbose("\nTesting unum_open(spellout, NULL, status)");
226 style=UNUM_SPELLOUT;
227 spellout_def=unum_open(style, NULL, 0, "en_US", NULL, &status);
228 if(U_FAILURE(status))
229 log_err("Error: could not create NumberFormat using unum_open(spellout, NULL, &status): %s\n", myErrorName(status));
230
231 /* Testing unum_clone(..) */
232 log_verbose("\nTesting unum_clone(fmt, status)");
233 status = U_ZERO_ERROR;
234 myclone = unum_clone(def,&status);
235 if(U_FAILURE(status))
236 log_err("Error: could not clone unum_clone(def, &status): %s\n", myErrorName(status));
237 else
238 {
239 log_verbose("unum_clone() successful\n");
240 }
241
242 /*Testing unum_getAvailable() and unum_countAvailable()*/
243 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
244 numlocales=unum_countAvailable();
245 if(numlocales < 0)
246 log_err("error in countAvailable");
247 else{
248 log_verbose("unum_countAvialable() successful\n");
249 log_verbose("The no: of locales where number formattting is applicable is %d\n", numlocales);
250 }
251 for(i=0;i<numlocales;i++)
252 {
253 log_verbose("%s\n", unum_getAvailable(i));
254 if (unum_getAvailable(i) == 0)
255 log_err("No locale for which number formatting patterns are applicable\n");
256 else
257 log_verbose("A locale %s for which number formatting patterns are applicable\n",unum_getAvailable(i));
258 }
259
260
261 /*Testing unum_format() and unum_formatdouble()*/
262 u_uastrcpy(temp1, "$100,000,000.00");
263
264 log_verbose("\nTesting unum_format() \n");
265 resultlength=0;
266 pos1.field = UNUM_INTEGER_FIELD;
267 resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status);
268 if(status==U_BUFFER_OVERFLOW_ERROR)
269 {
270 status=U_ZERO_ERROR;
271 resultlength=resultlengthneeded+1;
272 result=(UChar*)malloc(sizeof(UChar) * resultlength);
273 /* for (i = 0; i < 100000; i++) */
274 {
275 unum_format(cur_def, l, result, resultlength, &pos1, &status);
276 }
277 }
278
279 if(U_FAILURE(status))
280 {
281 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
282 }
283 if(u_strcmp(result, temp1)==0)
284 log_verbose("Pass: Number formatting using unum_format() successful\n");
285 else
286 log_err("Fail: Error in number Formatting using unum_format()\n");
287 if(pos1.beginIndex == 1 && pos1.endIndex == 12)
288 log_verbose("Pass: Complete number formatting using unum_format() successful\n");
289 else
290 log_err("Fail: Error in complete number Formatting using unum_format()\nGot: b=%d end=%d\nExpected: b=1 end=12\n",
291 pos1.beginIndex, pos1.endIndex);
292
293 free(result);
294 result = 0;
295
296 log_verbose("\nTesting unum_formatDouble()\n");
297 u_uastrcpy(temp1, "-$10,456.37");
298 resultlength=0;
299 pos2.field = UNUM_FRACTION_FIELD;
300 resultlengthneeded=unum_formatDouble(cur_def, d, NULL, resultlength, &pos2, &status);
301 if(status==U_BUFFER_OVERFLOW_ERROR)
302 {
303 status=U_ZERO_ERROR;
304 resultlength=resultlengthneeded+1;
305 result=(UChar*)malloc(sizeof(UChar) * resultlength);
306 /* for (i = 0; i < 100000; i++) */
307 {
308 unum_formatDouble(cur_def, d, result, resultlength, &pos2, &status);
309 }
310 }
311 if(U_FAILURE(status))
312 {
313 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
314 }
315 if(result && u_strcmp(result, temp1)==0)
316 log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
317 else {
318 log_err("FAIL: Error in number formatting using unum_formatDouble() - got '%s' expected '%s'\n",
319 aescstrdup(result, -1), aescstrdup(temp1, -1));
320 }
321 if(pos2.beginIndex == 9 && pos2.endIndex == 11)
322 log_verbose("Pass: Complete number formatting using unum_format() successful\n");
323 else
324 log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=9 end=11",
325 pos1.beginIndex, pos1.endIndex);
326
327
328 /* Testing unum_parse() and unum_parseDouble() */
329 log_verbose("\nTesting unum_parseDouble()\n");
330 /* for (i = 0; i < 100000; i++)*/
331 parsepos=0;
332 if (result != NULL) {
333 d1=unum_parseDouble(cur_def, result, u_strlen(result), &parsepos, &status);
334 } else {
335 log_err("result is NULL\n");
336 }
337 if(U_FAILURE(status)) {
338 log_err("parse of '%s' failed. Parsepos=%d. The error is : %s\n", aescstrdup(result,u_strlen(result)),parsepos, myErrorName(status));
339 }
340
341 if(d1!=d)
342 log_err("Fail: Error in parsing\n");
343 else
344 log_verbose("Pass: parsing successful\n");
345 if (result)
346 free(result);
347 result = 0;
348
349 status = U_ZERO_ERROR;
350 /* Testing unum_formatDoubleCurrency / unum_parseDoubleCurrency */
351 log_verbose("\nTesting unum_formatDoubleCurrency\n");
352 u_uastrcpy(temp1, "Y1,235");
353 temp1[0] = 0xA5; /* Yen sign */
354 u_uastrcpy(temp, "JPY");
355 resultlength=0;
356 pos2.field = UNUM_INTEGER_FIELD;
357 resultlengthneeded=unum_formatDoubleCurrency(cur_def, a, temp, NULL, resultlength, &pos2, &status);
358 if (status==U_BUFFER_OVERFLOW_ERROR) {
359 status=U_ZERO_ERROR;
360 resultlength=resultlengthneeded+1;
361 result=(UChar*)malloc(sizeof(UChar) * resultlength);
362 unum_formatDoubleCurrency(cur_def, a, temp, result, resultlength, &pos2, &status);
363 }
364 if (U_FAILURE(status)) {
365 log_err("Error in formatting using unum_formatDoubleCurrency(.....): %s\n", myErrorName(status));
366 }
367 if (result && u_strcmp(result, temp1)==0) {
368 log_verbose("Pass: Number Formatting using unum_formatDoubleCurrency() Successful\n");
369 } else {
370 log_err("FAIL: Error in number formatting using unum_formatDoubleCurrency() - got '%s' expected '%s'\n",
371 aescstrdup(result, -1), aescstrdup(temp1, -1));
372 }
373 if (pos2.beginIndex == 1 && pos2.endIndex == 6) {
374 log_verbose("Pass: Complete number formatting using unum_format() successful\n");
375 } else {
376 log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=1 end=6\n",
377 pos1.beginIndex, pos1.endIndex);
378 }
379
380 log_verbose("\nTesting unum_parseDoubleCurrency\n");
381 parsepos=0;
382 if (result == NULL) {
383 log_err("result is NULL\n");
384 }
385 else {
386 d1=unum_parseDoubleCurrency(cur_def, result, u_strlen(result), &parsepos, temp2, &status);
387 if (U_FAILURE(status)) {
388 log_err("parseDoubleCurrency '%s' failed. The error is : %s\n", aescstrdup(result, u_strlen(result)), myErrorName(status));
389 }
390 /* Note: a==1234.56, but on parse expect a1=1235.0 */
391 if (d1!=a1) {
392 log_err("Fail: Error in parsing currency, got %f, expected %f\n", d1, a1);
393 } else {
394 log_verbose("Pass: parsed currency amount successfully\n");
395 }
396 if (u_strcmp(temp2, temp)==0) {
397 log_verbose("Pass: parsed correct currency\n");
398 } else {
399 log_err("Fail: parsed incorrect currency\n");
400 }
401 }
402 status = U_ZERO_ERROR; /* reset */
403
404 free(result);
405 result = 0;
406
407
408 /* performance testing */
409 u_uastrcpy(temp1, "$462.12345");
410 resultlength=u_strlen(temp1);
411 /* for (i = 0; i < 100000; i++) */
412 {
413 parsepos=0;
414 d1=unum_parseDouble(cur_def, temp1, resultlength, &parsepos, &status);
415 }
416 if(U_FAILURE(status))
417 {
418 log_err("parseDouble('%s') failed. The error is : %s\n", aescstrdup(temp1, resultlength), myErrorName(status));
419 }
420
421 /*
422 * Note: "for strict standard conformance all operations and constants are now supposed to be
423 evaluated in precision of long double". So, we assign a1 before comparing to a double. Bug #7932.
424 */
425 a1 = 462.12345;
426
427 if(d1!=a1)
428 log_err("Fail: Error in parsing\n");
429 else
430 log_verbose("Pass: parsing successful\n");
431
432 free(result);
433
434 u_uastrcpy(temp1, "($10,456.3E1])");
435 parsepos=0;
436 d1=unum_parseDouble(cur_def, temp1, u_strlen(temp1), &parsepos, &status);
437 if(U_SUCCESS(status))
438 {
439 log_err("Error in unum_parseDouble(..., %s, ...): %s\n", temp1, myErrorName(status));
440 }
441
442
443 log_verbose("\nTesting unum_format()\n");
444 status=U_ZERO_ERROR;
445 resultlength=0;
446 parsepos=0;
447 resultlengthneeded=unum_format(per_fr, l, NULL, resultlength, &pos1, &status);
448 if(status==U_BUFFER_OVERFLOW_ERROR)
449 {
450 status=U_ZERO_ERROR;
451 resultlength=resultlengthneeded+1;
452 result=(UChar*)malloc(sizeof(UChar) * resultlength);
453 /* for (i = 0; i < 100000; i++)*/
454 {
455 unum_format(per_fr, l, result, resultlength, &pos1, &status);
456 }
457 }
458 if(U_FAILURE(status))
459 {
460 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
461 }
462
463
464 log_verbose("\nTesting unum_parse()\n");
465 /* for (i = 0; i < 100000; i++) */
466 {
467 parsepos=0;
468 l1=unum_parse(per_fr, result, u_strlen(result), &parsepos, &status);
469 }
470 if(U_FAILURE(status))
471 {
472 log_err("parse failed. The error is : %s\n", myErrorName(status));
473 }
474
475 if(l1!=l)
476 log_err("Fail: Error in parsing\n");
477 else
478 log_verbose("Pass: parsing successful\n");
479
480 free(result);
481
482 /* create a number format using unum_openPattern(....)*/
483 log_verbose("\nTesting unum_openPattern()\n");
484 u_uastrcpy(temp1, "#,##0.0#;(#,##0.0#)");
485 pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), NULL, NULL,&status);
486 if(U_FAILURE(status))
487 {
488 log_err("error in unum_openPattern(): %s\n", myErrorName(status) );;
489 }
490 else
491 log_verbose("Pass: unum_openPattern() works fine\n");
492
493 /*test for unum_toPattern()*/
494 log_verbose("\nTesting unum_toPattern()\n");
495 resultlength=0;
496 resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &status);
497 if(status==U_BUFFER_OVERFLOW_ERROR)
498 {
499 status=U_ZERO_ERROR;
500 resultlength=resultlengthneeded+1;
501 result=(UChar*)malloc(sizeof(UChar) * resultlength);
502 unum_toPattern(pattern, FALSE, result, resultlength, &status);
503 }
504 if(U_FAILURE(status))
505 {
506 log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status));
507 }
508 else
509 {
510 if(u_strcmp(result, temp1)!=0)
511 log_err("FAIL: Error in extracting the pattern using unum_toPattern()\n");
512 else
513 log_verbose("Pass: extracted the pattern correctly using unum_toPattern()\n");
514 free(result);
515 }
516
517 /*Testing unum_getSymbols() and unum_setSymbols()*/
518 log_verbose("\nTesting unum_getSymbols and unum_setSymbols()\n");
519 /*when we try to change the symbols of french to default we need to apply the pattern as well to fetch correct results */
520 resultlength=0;
521 resultlengthneeded=unum_toPattern(cur_def, FALSE, NULL, resultlength, &status);
522 if(status==U_BUFFER_OVERFLOW_ERROR)
523 {
524 status=U_ZERO_ERROR;
525 resultlength=resultlengthneeded+1;
526 result=(UChar*)malloc(sizeof(UChar) * resultlength);
527 unum_toPattern(cur_def, FALSE, result, resultlength, &status);
528 }
529 if(U_FAILURE(status))
530 {
531 log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status));
532 }
533
534 status=U_ZERO_ERROR;
535 cur_frpattern=unum_open(UNUM_IGNORE,result, u_strlen(result), "fr_FR",NULL, &status);
536 if(U_FAILURE(status))
537 {
538 log_err("error in unum_openPattern(): %s\n", myErrorName(status));
539 }
540
541 free(result);
542
543 /*getting the symbols of cur_def */
544 /*set the symbols of cur_frpattern to cur_def */
545 for (symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; symType < UNUM_FORMAT_SYMBOL_COUNT; symType++) {
546 status=U_ZERO_ERROR;
547 unum_getSymbol(cur_def, symType, temp1, sizeof(temp1), &status);
548 unum_setSymbol(cur_frpattern, symType, temp1, -1, &status);
549 if(U_FAILURE(status))
550 {
551 log_err("Error in get/set symbols: %s\n", myErrorName(status));
552 }
553 }
554
555 /*format to check the result */
556 resultlength=0;
557 resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status);
558 if(status==U_BUFFER_OVERFLOW_ERROR)
559 {
560 status=U_ZERO_ERROR;
561 resultlength=resultlengthneeded+1;
562 result=(UChar*)malloc(sizeof(UChar) * resultlength);
563 unum_format(cur_def, l, result, resultlength, &pos1, &status);
564 }
565 if(U_FAILURE(status))
566 {
567 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
568 }
569
570 if(U_FAILURE(status)){
571 log_err("Fail: error in unum_setSymbols: %s\n", myErrorName(status));
572 }
573 unum_applyPattern(cur_frpattern, FALSE, result, u_strlen(result),NULL,NULL);
574
575 for (symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; symType < UNUM_FORMAT_SYMBOL_COUNT; symType++) {
576 status=U_ZERO_ERROR;
577 unum_getSymbol(cur_def, symType, temp1, sizeof(temp1), &status);
578 unum_getSymbol(cur_frpattern, symType, temp2, sizeof(temp2), &status);
579 if(U_FAILURE(status) || u_strcmp(temp1, temp2) != 0)
580 {
581 log_err("Fail: error in getting symbols\n");
582 }
583 else
584 log_verbose("Pass: get and set symbols successful\n");
585 }
586
587 /*format and check with the previous result */
588
589 resultlength=0;
590 resultlengthneeded=unum_format(cur_frpattern, l, NULL, resultlength, &pos1, &status);
591 if(status==U_BUFFER_OVERFLOW_ERROR)
592 {
593 status=U_ZERO_ERROR;
594 resultlength=resultlengthneeded+1;
595 unum_format(cur_frpattern, l, temp1, resultlength, &pos1, &status);
596 }
597 if(U_FAILURE(status))
598 {
599 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
600 }
601 /* TODO:
602 * This test fails because we have not called unum_applyPattern().
603 * Currently, such an applyPattern() does not exist on the C API, and
604 * we have jitterbug 411 for it.
605 * Since it is close to the 1.5 release, I (markus) am disabling this test just
606 * for this release (I added the test itself only last week).
607 * For the next release, we need to fix this.
608 * Then, remove the uprv_strcmp("1.5", ...) and this comment, and the include "cstring.h" at the beginning of this file.
609 */
610 if(u_strcmp(result, temp1) != 0) {
611 log_err("Formatting failed after setting symbols. result=%s temp1=%s\n", result, temp1);
612 }
613
614
615 /*----------- */
616
617 free(result);
618
619 /* Testing unum_get/setSymbol() */
620 for(i = 0; i < UNUM_FORMAT_SYMBOL_COUNT; ++i) {
621 symbol[0] = (UChar)(0x41 + i);
622 symbol[1] = (UChar)(0x61 + i);
623 unum_setSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, 2, &status);
624 if(U_FAILURE(status)) {
625 log_err("Error from unum_setSymbol(%d): %s\n", i, myErrorName(status));
626 return;
627 }
628 }
629 for(i = 0; i < UNUM_FORMAT_SYMBOL_COUNT; ++i) {
630 resultlength = unum_getSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, UPRV_LENGTHOF(symbol), &status);
631 if(U_FAILURE(status)) {
632 log_err("Error from unum_getSymbol(%d): %s\n", i, myErrorName(status));
633 return;
634 }
635 if(resultlength != 2 || symbol[0] != 0x41 + i || symbol[1] != 0x61 + i) {
636 log_err("Failure in unum_getSymbol(%d): got unexpected symbol\n", i);
637 }
638 }
639 /*try getting from a bogus symbol*/
640 unum_getSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, UPRV_LENGTHOF(symbol), &status);
641 if(U_SUCCESS(status)){
642 log_err("Error : Expected U_ILLEGAL_ARGUMENT_ERROR for bogus symbol");
643 }
644 if(U_FAILURE(status)){
645 if(status != U_ILLEGAL_ARGUMENT_ERROR){
646 log_err("Error: Expected U_ILLEGAL_ARGUMENT_ERROR for bogus symbol, Got %s\n", myErrorName(status));
647 }
648 }
649 status=U_ZERO_ERROR;
650
651 /* Testing unum_getTextAttribute() and unum_setTextAttribute()*/
652 log_verbose("\nTesting getting and setting text attributes\n");
653 resultlength=5;
654 unum_getTextAttribute(cur_fr, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status);
655 if(U_FAILURE(status))
656 {
657 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
658 }
659 unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status);
660 if(U_FAILURE(status))
661 {
662 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
663 }
664 unum_getTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, suffix, resultlength, &status);
665 if(U_FAILURE(status))
666 {
667 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
668 }
669 if(u_strcmp(suffix,temp)!=0)
670 log_err("Fail:Error in setTextAttribute or getTextAttribute in setting and getting suffix\n");
671 else
672 log_verbose("Pass: setting and getting suffix works fine\n");
673 /*set it back to normal */
674 u_uastrcpy(temp,"$");
675 unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status);
676
677 /*checking some more text setter conditions */
678 u_uastrcpy(prefix, "+");
679 unum_setTextAttribute(def, UNUM_POSITIVE_PREFIX, prefix, u_strlen(prefix) , &status);
680 if(U_FAILURE(status))
681 {
682 log_err("error in setting the text attributes : %s\n", myErrorName(status));
683 }
684 unum_getTextAttribute(def, UNUM_POSITIVE_PREFIX, temp, resultlength, &status);
685 if(U_FAILURE(status))
686 {
687 log_err("error in getting the text attributes : %s\n", myErrorName(status));
688 }
689
690 if(u_strcmp(prefix, temp)!=0)
691 log_err("ERROR: get and setTextAttributes with positive prefix failed\n");
692 else
693 log_verbose("Pass: get and setTextAttributes with positive prefix works fine\n");
694
695 u_uastrcpy(prefix, "+");
696 unum_setTextAttribute(def, UNUM_NEGATIVE_PREFIX, prefix, u_strlen(prefix), &status);
697 if(U_FAILURE(status))
698 {
699 log_err("error in setting the text attributes : %s\n", myErrorName(status));
700 }
701 unum_getTextAttribute(def, UNUM_NEGATIVE_PREFIX, temp, resultlength, &status);
702 if(U_FAILURE(status))
703 {
704 log_err("error in getting the text attributes : %s\n", myErrorName(status));
705 }
706 if(u_strcmp(prefix, temp)!=0)
707 log_err("ERROR: get and setTextAttributes with negative prefix failed\n");
708 else
709 log_verbose("Pass: get and setTextAttributes with negative prefix works fine\n");
710
711 u_uastrcpy(suffix, "+");
712 unum_setTextAttribute(def, UNUM_NEGATIVE_SUFFIX, suffix, u_strlen(suffix) , &status);
713 if(U_FAILURE(status))
714 {
715 log_err("error in setting the text attributes: %s\n", myErrorName(status));
716 }
717
718 unum_getTextAttribute(def, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status);
719 if(U_FAILURE(status))
720 {
721 log_err("error in getting the text attributes : %s\n", myErrorName(status));
722 }
723 if(u_strcmp(suffix, temp)!=0)
724 log_err("ERROR: get and setTextAttributes with negative suffix failed\n");
725 else
726 log_verbose("Pass: get and settextAttributes with negative suffix works fine\n");
727
728 u_uastrcpy(suffix, "++");
729 unum_setTextAttribute(def, UNUM_POSITIVE_SUFFIX, suffix, u_strlen(suffix) , &status);
730 if(U_FAILURE(status))
731 {
732 log_err("error in setting the text attributes: %s\n", myErrorName(status));
733 }
734
735 unum_getTextAttribute(def, UNUM_POSITIVE_SUFFIX, temp, resultlength, &status);
736 if(U_FAILURE(status))
737 {
738 log_err("error in getting the text attributes : %s\n", myErrorName(status));
739 }
740 if(u_strcmp(suffix, temp)!=0)
741 log_err("ERROR: get and setTextAttributes with negative suffix failed\n");
742 else
743 log_verbose("Pass: get and settextAttributes with negative suffix works fine\n");
744
745 /*Testing unum_getAttribute and unum_setAttribute() */
746 log_verbose("\nTesting get and set Attributes\n");
747 attr=UNUM_GROUPING_SIZE;
748 newvalue=unum_getAttribute(def, attr);
749 newvalue=2;
750 unum_setAttribute(def, attr, newvalue);
751 if(unum_getAttribute(def,attr)!=2)
752 log_err("Fail: error in setting and getting attributes for UNUM_GROUPING_SIZE\n");
753 else
754 log_verbose("Pass: setting and getting attributes for UNUM_GROUPING_SIZE works fine\n");
755
756 attr=UNUM_MULTIPLIER;
757 newvalue=unum_getAttribute(def, attr);
758 newvalue=8;
759 unum_setAttribute(def, attr, newvalue);
760 if(unum_getAttribute(def,attr) != 8)
761 log_err("error in setting and getting attributes for UNUM_MULTIPLIER\n");
762 else
763 log_verbose("Pass:setting and getting attributes for UNUM_MULTIPLIER works fine\n");
764
765 attr=UNUM_SECONDARY_GROUPING_SIZE;
766 newvalue=unum_getAttribute(def, attr);
767 newvalue=2;
768 unum_setAttribute(def, attr, newvalue);
769 if(unum_getAttribute(def,attr) != 2)
770 log_err("error in setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE: got %d\n",
771 unum_getAttribute(def,attr));
772 else
773 log_verbose("Pass:setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE works fine\n");
774
775 /*testing set and get Attributes extensively */
776 log_verbose("\nTesting get and set attributes extensively\n");
777 for(attr=UNUM_PARSE_INT_ONLY; attr<= UNUM_PADDING_POSITION; attr=(UNumberFormatAttribute)((int32_t)attr + 1) )
778 {
779 newvalue=unum_getAttribute(fr, attr);
780 unum_setAttribute(def, attr, newvalue);
781 if(unum_getAttribute(def,attr)!=unum_getAttribute(fr, attr))
782 log_err("error in setting and getting attributes\n");
783 else
784 log_verbose("Pass: attributes set and retrieved successfully\n");
785 }
786
787 /*testing spellout format to make sure we can use it successfully.*/
788 log_verbose("\nTesting spellout format\n");
789 if (spellout_def)
790 {
791 static const int32_t values[] = { 0, -5, 105, 1005, 105050 };
792 for (i = 0; i < UPRV_LENGTHOF(values); ++i) {
793 UChar buffer[128];
794 int32_t len;
795 int32_t value = values[i];
796 status = U_ZERO_ERROR;
797 len = unum_format(spellout_def, value, buffer, UPRV_LENGTHOF(buffer), NULL, &status);
798 if(U_FAILURE(status)) {
799 log_err("Error in formatting using unum_format(spellout_fmt, ...): %s\n", myErrorName(status));
800 } else {
801 int32_t pp = 0;
802 int32_t parseResult;
803 /*ustrToAstr(buffer, len, logbuf, UPRV_LENGTHOF(logbuf));*/
804 log_verbose("formatted %d as '%s', length: %d\n", value, aescstrdup(buffer, len), len);
805
806 parseResult = unum_parse(spellout_def, buffer, len, &pp, &status);
807 if (U_FAILURE(status)) {
808 log_err("Error in parsing using unum_format(spellout_fmt, ...): %s\n", myErrorName(status));
809 } else if (parseResult != value) {
810 log_err("unum_format result %d != value %d\n", parseResult, value);
811 }
812 }
813 }
814 }
815 else {
816 log_err("Spellout format is unavailable\n");
817 }
818
819 { /* Test for ticket #7079 */
820 UNumberFormat* dec_en;
821 UChar groupingSep[] = { 0 };
822 UChar numPercent[] = { 0x0031, 0x0032, 0x0025, 0 }; /* "12%" */
823 double parseResult = 0.0;
824
825 status=U_ZERO_ERROR;
826 dec_en = unum_open(UNUM_DECIMAL, NULL, 0, "en_US", NULL, &status);
827 unum_setAttribute(dec_en, UNUM_LENIENT_PARSE, 0);
828 unum_setSymbol(dec_en, UNUM_GROUPING_SEPARATOR_SYMBOL, groupingSep, 0, &status);
829 parseResult = unum_parseDouble(dec_en, numPercent, -1, NULL, &status);
830 /* Without the fix in #7079, the above call will hang */
831 if ( U_FAILURE(status) || parseResult != 12.0 ) {
832 log_err("unum_parseDouble with empty groupingSep: status %s, parseResult %f not 12.0\n",
833 myErrorName(status), parseResult);
834 } else {
835 log_verbose("unum_parseDouble with empty groupingSep: no hang, OK\n");
836 }
837 unum_close(dec_en);
838 }
839
840 { /* Test parse & format of big decimals. Use a number with too many digits to fit in a double,
841 to verify that it is taking the pure decimal path. */
842 UNumberFormat *fmt;
843 const char *bdpattern = "#,##0.#########";
844 const char *numInitial = "12345678900987654321.1234567896";
845 const char *numFormatted = "12,345,678,900,987,654,321.12345679";
846 const char *parseExpected = "1.234567890098765432112345679E+19";
847 const char *parseExpected2 = "3.4567890098765432112345679E+17";
848 int32_t resultSize = 0;
849 int32_t parsePos = 0; /* Output parameter for Parse operations. */
850 #define DESTCAPACITY 100
851 UChar dest[DESTCAPACITY];
852 char desta[DESTCAPACITY];
853 UFieldPosition fieldPos = {0};
854
855 /* Format */
856
857 status = U_ZERO_ERROR;
858 u_uastrcpy(dest, bdpattern);
859 fmt = unum_open(UNUM_PATTERN_DECIMAL, dest, -1, "en", NULL /*parseError*/, &status);
860 if (U_FAILURE(status)) log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status));
861
862 resultSize = unum_formatDecimal(fmt, numInitial, -1, dest, DESTCAPACITY, NULL, &status);
863 if (U_FAILURE(status)) {
864 log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status));
865 }
866 u_austrncpy(desta, dest, DESTCAPACITY);
867 if (strcmp(numFormatted, desta) != 0) {
868 log_err("File %s, Line %d, (expected, acutal) = (\"%s\", \"%s\")\n",
869 __FILE__, __LINE__, numFormatted, desta);
870 }
871 if (strlen(numFormatted) != resultSize) {
872 log_err("File %s, Line %d, (expected, actual) = (%d, %d)\n",
873 __FILE__, __LINE__, strlen(numFormatted), resultSize);
874 }
875
876 /* Format with a FieldPosition parameter */
877
878 fieldPos.field = UNUM_DECIMAL_SEPARATOR_FIELD;
879 resultSize = unum_formatDecimal(fmt, numInitial, -1, dest, DESTCAPACITY, &fieldPos, &status);
880 if (U_FAILURE(status)) {
881 log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status));
882 }
883 u_austrncpy(desta, dest, DESTCAPACITY);
884 if (strcmp(numFormatted, desta) != 0) {
885 log_err("File %s, Line %d, (expected, acutal) = (\"%s\", \"%s\")\n",
886 __FILE__, __LINE__, numFormatted, desta);
887 }
888 if (fieldPos.beginIndex != 26) { /* index of "." in formatted number */
889 log_err("File %s, Line %d, (expected, acutal) = (%d, %d)\n",
890 __FILE__, __LINE__, 0, fieldPos.beginIndex);
891 }
892 if (fieldPos.endIndex != 27) {
893 log_err("File %s, Line %d, (expected, acutal) = (%d, %d)\n",
894 __FILE__, __LINE__, 0, fieldPos.endIndex);
895 }
896
897 /* Parse */
898
899 status = U_ZERO_ERROR;
900 u_uastrcpy(dest, numFormatted); /* Parse the expected output of the formatting test */
901 resultSize = unum_parseDecimal(fmt, dest, -1, NULL, desta, DESTCAPACITY, &status);
902 if (U_FAILURE(status)) {
903 log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status));
904 }
905 if (uprv_strcmp(parseExpected, desta) != 0) {
906 log_err("File %s, Line %d, (expected, actual) = (\"%s\", \"%s\")\n",
907 __FILE__, __LINE__, parseExpected, desta);
908 } else {
909 log_verbose("File %s, Line %d, got expected = \"%s\"\n",
910 __FILE__, __LINE__, desta);
911 }
912 if (strlen(parseExpected) != resultSize) {
913 log_err("File %s, Line %d, (expected, actual) = (%d, %d)\n",
914 __FILE__, __LINE__, strlen(parseExpected), resultSize);
915 }
916
917 /* Parse with a parsePos parameter */
918
919 status = U_ZERO_ERROR;
920 u_uastrcpy(dest, numFormatted); /* Parse the expected output of the formatting test */
921 parsePos = 3; /* 12,345,678,900,987,654,321.12345679 */
922 /* start parsing at the the third char */
923 resultSize = unum_parseDecimal(fmt, dest, -1, &parsePos, desta, DESTCAPACITY, &status);
924 if (U_FAILURE(status)) {
925 log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status));
926 }
927 if (strcmp(parseExpected2, desta) != 0) { /* "3.4567890098765432112345679E+17" */
928 log_err("File %s, Line %d, (expected, actual) = (\"%s\", \"%s\")\n",
929 __FILE__, __LINE__, parseExpected2, desta);
930 } else {
931 log_verbose("File %s, Line %d, got expected = \"%s\"\n",
932 __FILE__, __LINE__, desta);
933 }
934 if (strlen(numFormatted) != parsePos) {
935 log_err("File %s, Line %d, parsePos (expected, actual) = (\"%d\", \"%d\")\n",
936 __FILE__, __LINE__, strlen(parseExpected), parsePos);
937 }
938
939 unum_close(fmt);
940 }
941
942 status = U_ZERO_ERROR;
943 /* Test invalid symbol argument */
944 {
945 int32_t badsymbolLarge = UNUM_FORMAT_SYMBOL_COUNT + 1;
946 int32_t badsymbolSmall = -1;
947 UChar value[10];
948 int32_t valueLength = 10;
949 UNumberFormat *fmt = unum_open(UNUM_DEFAULT, NULL, 0, NULL, NULL, &status);
950 if (U_FAILURE(status)) {
951 log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status));
952 } else {
953 unum_getSymbol(fmt, (UNumberFormatSymbol)badsymbolLarge, NULL, 0, &status);
954 if (U_SUCCESS(status)) log_err("unum_getSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (> UNUM_FORMAT_SYMBOL_COUNT) argument\n");
955
956 status = U_ZERO_ERROR;
957 unum_getSymbol(fmt, (UNumberFormatSymbol)badsymbolSmall, NULL, 0, &status);
958 if (U_SUCCESS(status)) log_err("unum_getSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (less than 0) argument\n");
959
960 status = U_ZERO_ERROR;
961 unum_setSymbol(fmt, (UNumberFormatSymbol)badsymbolLarge, value, valueLength, &status);
962 if (U_SUCCESS(status)) log_err("unum_setSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (> UNUM_FORMAT_SYMBOL_COUNT) argument\n");
963
964 status = U_ZERO_ERROR;
965 unum_setSymbol(fmt, (UNumberFormatSymbol)badsymbolSmall, value, valueLength, &status);
966 if (U_SUCCESS(status)) log_err("unum_setSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (less than 0) argument\n");
967
968 unum_close(fmt);
969 }
970 }
971
972
973 /*closing the NumberFormat() using unum_close(UNumberFormat*)")*/
974 unum_close(def);
975 unum_close(fr);
976 unum_close(cur_def);
977 unum_close(cur_fr);
978 unum_close(per_def);
979 unum_close(per_fr);
980 unum_close(spellout_def);
981 unum_close(pattern);
982 unum_close(cur_frpattern);
983 unum_close(myclone);
984
985 }
986
TestParseZero(void)987 static void TestParseZero(void)
988 {
989 UErrorCode errorCode = U_ZERO_ERROR;
990 UChar input[] = {0x30, 0}; /* Input text is decimal '0' */
991 UChar pat[] = {0x0023,0x003b,0x0023,0}; /* {'#', ';', '#', 0}; */
992 double dbl;
993
994 #if 0
995 UNumberFormat* unum = unum_open( UNUM_DECIMAL /*or UNUM_DEFAULT*/, NULL, -1, NULL, NULL, &errorCode);
996 #else
997 UNumberFormat* unum = unum_open( UNUM_PATTERN_DECIMAL /*needs pattern*/, pat, -1, NULL, NULL, &errorCode);
998 #endif
999
1000 dbl = unum_parseDouble( unum, input, -1 /*u_strlen(input)*/, 0 /* 0 = start */, &errorCode );
1001 if (U_FAILURE(errorCode)) {
1002 log_data_err("Result - %s\n", u_errorName(errorCode));
1003 } else {
1004 log_verbose("Double: %f\n", dbl);
1005 }
1006 unum_close(unum);
1007 }
1008
1009 static const UChar dollars2Sym[] = { 0x24,0x32,0x2E,0x30,0x30,0 }; /* $2.00 */
1010 static const UChar dollars4Sym[] = { 0x24,0x34,0 }; /* $4 */
1011 static const UChar dollarsUS4Sym[] = { 0x55,0x53,0x24,0x34,0 }; /* US$4 */
1012 static const UChar dollars9Sym[] = { 0x39,0xA0,0x24,0 }; /* 9 $ */
1013 static const UChar pounds3Sym[] = { 0xA3,0x33,0x2E,0x30,0x30,0 }; /* [POUND]3.00 */
1014 static const UChar pounds5Sym[] = { 0xA3,0x35,0 }; /* [POUND]5 */
1015 static const UChar pounds7Sym[] = { 0x37,0xA0,0xA3,0 }; /* 7 [POUND] */
1016 static const UChar euros4Sym[] = { 0x34,0x2C,0x30,0x30,0xA0,0x20AC,0 }; /* 4,00 [EURO] */
1017 static const UChar euros6Sym[] = { 0x36,0xA0,0x20AC,0 }; /* 6 [EURO] */
1018 static const UChar euros8Sym[] = { 0x20AC,0x38,0 }; /* [EURO]8 */
1019 static const UChar dollars4PluEn[] = { 0x34,0x20,0x55,0x53,0x20,0x64,0x6F,0x6C,0x6C,0x61,0x72,0x73,0 }; /* 4 US dollars*/
1020 static const UChar pounds5PluEn[] = { 0x35,0x20,0x42,0x72,0x69,0x74,0x69,0x73,0x68,0x20,0x70,0x6F,0x75,0x6E,0x64,0x73,0x20,0x73,0x74,0x65,0x72,0x6C,0x69,0x6E,0x67,0 }; /* 5 British pounds sterling */
1021 static const UChar euros8PluEn[] = { 0x38,0x20,0x65,0x75,0x72,0x6F,0x73,0 }; /* 8 euros*/
1022 static const UChar euros6PluFr[] = { 0x36,0x20,0x65,0x75,0x72,0x6F,0x73,0 }; /* 6 euros*/
1023
1024 typedef struct {
1025 const char * locale;
1026 const char * descrip;
1027 const UChar * currStr;
1028 const UChar * plurStr;
1029 UErrorCode parsDoubExpectErr;
1030 int32_t parsDoubExpectPos;
1031 double parsDoubExpectVal;
1032 UErrorCode parsCurrExpectErr;
1033 int32_t parsCurrExpectPos;
1034 double parsCurrExpectVal;
1035 const char * parsCurrExpectCurr;
1036 } ParseCurrencyItem;
1037
1038 static const ParseCurrencyItem parseCurrencyItems[] = {
1039 { "en_US", "dollars2", dollars2Sym, NULL, U_ZERO_ERROR, 5, 2.0, U_ZERO_ERROR, 5, 2.0, "USD" },
1040 { "en_US", "dollars4", dollars4Sym, dollars4PluEn, U_ZERO_ERROR, 2, 4.0, U_ZERO_ERROR, 2, 4.0, "USD" },
1041 { "en_US", "dollars9", dollars9Sym, NULL, U_PARSE_ERROR, 1, 0.0, U_PARSE_ERROR, 1, 0.0, "" },
1042 { "en_US", "pounds3", pounds3Sym, NULL, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 5, 3.0, "GBP" },
1043 { "en_US", "pounds5", pounds5Sym, pounds5PluEn, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 2, 5.0, "GBP" },
1044 { "en_US", "pounds7", pounds7Sym, NULL, U_PARSE_ERROR, 1, 0.0, U_PARSE_ERROR, 1, 0.0, "" },
1045 { "en_US", "euros8", euros8Sym, euros8PluEn, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 2, 8.0, "EUR" },
1046
1047 { "en_GB", "pounds3", pounds3Sym, NULL, U_ZERO_ERROR, 5, 3.0, U_ZERO_ERROR, 5, 3.0, "GBP" },
1048 { "en_GB", "pounds5", pounds5Sym, pounds5PluEn, U_ZERO_ERROR, 2, 5.0, U_ZERO_ERROR, 2, 5.0, "GBP" },
1049 { "en_GB", "pounds7", pounds7Sym, NULL, U_PARSE_ERROR, 1, 0.0, U_PARSE_ERROR, 1, 0.0, "" },
1050 { "en_GB", "euros4", euros4Sym, NULL, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, 0.0, "" },
1051 { "en_GB", "euros6", euros6Sym, NULL, U_PARSE_ERROR, 1, 0.0, U_PARSE_ERROR, 1, 0.0, "" },
1052 { "en_GB", "euros8", euros8Sym, euros8PluEn, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 2, 8.0, "EUR" },
1053 { "en_GB", "dollars4", dollarsUS4Sym,dollars4PluEn, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 4, 4.0, "USD" },
1054
1055 { "fr_FR", "euros4", euros4Sym, NULL, U_ZERO_ERROR, 6, 4.0, U_ZERO_ERROR, 6, 4.0, "EUR" },
1056 { "fr_FR", "euros6", euros6Sym, euros6PluFr, U_ZERO_ERROR, 3, 6.0, U_ZERO_ERROR, 3, 6.0, "EUR" },
1057 { "fr_FR", "euros8", euros8Sym, NULL, U_PARSE_ERROR, 2, 0.0, U_PARSE_ERROR, 2, 0.0, "" },
1058 { "fr_FR", "dollars2", dollars2Sym, NULL, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, 0.0, "" },
1059 { "fr_FR", "dollars4", dollars4Sym, NULL, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, 0.0, "" },
1060
1061 { NULL, NULL, NULL, NULL, 0, 0, 0.0, 0, 0, 0.0, NULL }
1062 };
1063
TestParseCurrency()1064 static void TestParseCurrency()
1065 {
1066 const ParseCurrencyItem * itemPtr;
1067 for (itemPtr = parseCurrencyItems; itemPtr->locale != NULL; ++itemPtr) {
1068 UNumberFormat* unum;
1069 UErrorCode status;
1070 double parseVal;
1071 int32_t parsePos;
1072 UChar parseCurr[4];
1073 char parseCurrB[4];
1074
1075 status = U_ZERO_ERROR;
1076 unum = unum_open(UNUM_CURRENCY, NULL, 0, itemPtr->locale, NULL, &status);
1077 if (U_SUCCESS(status)) {
1078 const UChar * currStr = itemPtr->currStr;
1079 status = U_ZERO_ERROR;
1080 parsePos = 0;
1081 parseVal = unum_parseDouble(unum, currStr, -1, &parsePos, &status);
1082 if (status != itemPtr->parsDoubExpectErr || parsePos != itemPtr->parsDoubExpectPos || parseVal != itemPtr->parsDoubExpectVal) {
1083 log_err("UNUM_CURRENCY parseDouble %s/%s, expect %s pos %d val %.1f, get %s pos %d val %.1f\n",
1084 itemPtr->locale, itemPtr->descrip,
1085 u_errorName(itemPtr->parsDoubExpectErr), itemPtr->parsDoubExpectPos, itemPtr->parsDoubExpectVal,
1086 u_errorName(status), parsePos, parseVal );
1087 }
1088 status = U_ZERO_ERROR;
1089 parsePos = 0;
1090 parseCurr[0] = 0;
1091 parseVal = unum_parseDoubleCurrency(unum, currStr, -1, &parsePos, parseCurr, &status);
1092 u_austrncpy(parseCurrB, parseCurr, 4);
1093 if (status != itemPtr->parsCurrExpectErr || parsePos != itemPtr->parsCurrExpectPos || parseVal != itemPtr->parsCurrExpectVal ||
1094 strncmp(parseCurrB, itemPtr->parsCurrExpectCurr, 4) != 0) {
1095 log_err("UNUM_CURRENCY parseDoubleCurrency %s/%s, expect %s pos %d val %.1f cur %s, get %s pos %d val %.1f cur %s\n",
1096 itemPtr->locale, itemPtr->descrip,
1097 u_errorName(itemPtr->parsCurrExpectErr), itemPtr->parsCurrExpectPos, itemPtr->parsCurrExpectVal, itemPtr->parsCurrExpectCurr,
1098 u_errorName(status), parsePos, parseVal, parseCurrB );
1099 }
1100 unum_close(unum);
1101 } else {
1102 log_data_err("unexpected error in unum_open UNUM_CURRENCY for locale %s: '%s'\n", itemPtr->locale, u_errorName(status));
1103 }
1104
1105 if (itemPtr->plurStr != NULL) {
1106 status = U_ZERO_ERROR;
1107 unum = unum_open(UNUM_CURRENCY_PLURAL, NULL, 0, itemPtr->locale, NULL, &status);
1108 if (U_SUCCESS(status)) {
1109 status = U_ZERO_ERROR;
1110 parsePos = 0;
1111 parseVal = unum_parseDouble(unum, itemPtr->plurStr, -1, &parsePos, &status);
1112 if (status != itemPtr->parsDoubExpectErr || parseVal != itemPtr->parsDoubExpectVal) {
1113 log_err("UNUM_CURRENCY parseDouble Plural %s/%s, expect %s val %.1f, get %s val %.1f\n",
1114 itemPtr->locale, itemPtr->descrip,
1115 u_errorName(itemPtr->parsDoubExpectErr), itemPtr->parsDoubExpectVal,
1116 u_errorName(status), parseVal );
1117 }
1118 status = U_ZERO_ERROR;
1119 parsePos = 0;
1120 parseCurr[0] = 0;
1121 parseVal = unum_parseDoubleCurrency(unum, itemPtr->plurStr, -1, &parsePos, parseCurr, &status);
1122 u_austrncpy(parseCurrB, parseCurr, 4);
1123 if (status != itemPtr->parsCurrExpectErr || parseVal != itemPtr->parsCurrExpectVal ||
1124 strncmp(parseCurrB, itemPtr->parsCurrExpectCurr, 4) != 0) {
1125 log_err("UNUM_CURRENCY parseDoubleCurrency Plural %s/%s, expect %s val %.1f cur %s, get %s val %.1f cur %s\n",
1126 itemPtr->locale, itemPtr->descrip,
1127 u_errorName(itemPtr->parsCurrExpectErr), itemPtr->parsCurrExpectVal, itemPtr->parsCurrExpectCurr,
1128 u_errorName(status), parseVal, parseCurrB );
1129 }
1130 unum_close(unum);
1131 } else {
1132 log_data_err("unexpected error in unum_open UNUM_CURRENCY_PLURAL for locale %s: '%s'\n", itemPtr->locale, u_errorName(status));
1133 }
1134 }
1135 }
1136 }
1137
1138 typedef struct {
1139 const char * testname;
1140 const char * locale;
1141 const UChar * source;
1142 int32_t startPos;
1143 int32_t value;
1144 int32_t endPos;
1145 UErrorCode status;
1146 } SpelloutParseTest;
1147
1148 static const UChar ustr_en0[] = {0x7A, 0x65, 0x72, 0x6F, 0}; /* zero */
1149 static const UChar ustr_123[] = {0x31, 0x32, 0x33, 0}; /* 123 */
1150 static const UChar ustr_en123[] = {0x6f, 0x6e, 0x65, 0x20, 0x68, 0x75, 0x6e, 0x64, 0x72, 0x65, 0x64,
1151 0x20, 0x74, 0x77, 0x65, 0x6e, 0x74, 0x79,
1152 0x2d, 0x74, 0x68, 0x72, 0x65, 0x65, 0}; /* one hundred twenty-three */
1153 static const UChar ustr_fr123[] = {0x63, 0x65, 0x6e, 0x74, 0x20, 0x76, 0x69, 0x6e, 0x67, 0x74, 0x2d,
1154 0x74, 0x72, 0x6f, 0x69, 0x73, 0}; /* cent vingt-trois */
1155 static const UChar ustr_ja123[] = {0x767e, 0x4e8c, 0x5341, 0x4e09, 0}; /* kanji 100(+)2(*)10(+)3 */
1156
1157 static const SpelloutParseTest spelloutParseTests[] = {
1158 /* name loc src start val end status */
1159 { "en0", "en", ustr_en0, 0, 0, 4, U_ZERO_ERROR },
1160 { "en0", "en", ustr_en0, 2, 0, 2, U_PARSE_ERROR },
1161 { "en0", "ja", ustr_en0, 0, 0, 0, U_PARSE_ERROR },
1162 { "123", "en", ustr_123, 0, 123, 3, U_ZERO_ERROR },
1163 { "en123", "en", ustr_en123, 0, 123, 24, U_ZERO_ERROR },
1164 { "en123", "en", ustr_en123, 12, 23, 24, U_ZERO_ERROR },
1165 { "en123", "fr", ustr_en123, 16, 0, 16, U_PARSE_ERROR },
1166 { "fr123", "fr", ustr_fr123, 0, 123, 16, U_ZERO_ERROR },
1167 { "fr123", "fr", ustr_fr123, 5, 23, 16, U_ZERO_ERROR },
1168 { "fr123", "en", ustr_fr123, 0, 0, 0, U_PARSE_ERROR },
1169 { "ja123", "ja", ustr_ja123, 0, 123, 4, U_ZERO_ERROR },
1170 { "ja123", "ja", ustr_ja123, 1, 23, 4, U_ZERO_ERROR },
1171 { "ja123", "fr", ustr_ja123, 0, 0, 0, U_PARSE_ERROR },
1172 { NULL, NULL, NULL, 0, 0, 0, 0 } /* terminator */
1173 };
1174
TestSpelloutNumberParse()1175 static void TestSpelloutNumberParse()
1176 {
1177 const SpelloutParseTest * testPtr;
1178 for (testPtr = spelloutParseTests; testPtr->testname != NULL; ++testPtr) {
1179 UErrorCode status = U_ZERO_ERROR;
1180 int32_t value, position = testPtr->startPos;
1181 UNumberFormat *nf = unum_open(UNUM_SPELLOUT, NULL, 0, testPtr->locale, NULL, &status);
1182 if (U_FAILURE(status)) {
1183 log_err_status(status, "unum_open fails for UNUM_SPELLOUT with locale %s, status %s\n", testPtr->locale, myErrorName(status));
1184 continue;
1185 }
1186 status = U_ZERO_ERROR;
1187 value = unum_parse(nf, testPtr->source, -1, &position, &status);
1188 if ( value != testPtr->value || position != testPtr->endPos || status != testPtr->status ) {
1189 log_err("unum_parse SPELLOUT, locale %s, testname %s, startPos %d: for value / endPos / status, expected %d / %d / %s, got %d / %d / %s\n",
1190 testPtr->locale, testPtr->testname, testPtr->startPos,
1191 testPtr->value, testPtr->endPos, myErrorName(testPtr->status),
1192 value, position, myErrorName(status) );
1193 }
1194 unum_close(nf);
1195 }
1196 }
1197
TestSignificantDigits()1198 static void TestSignificantDigits()
1199 {
1200 UChar temp[128];
1201 int32_t resultlengthneeded;
1202 int32_t resultlength;
1203 UErrorCode status = U_ZERO_ERROR;
1204 UChar *result = NULL;
1205 UNumberFormat* fmt;
1206 double d = 123456.789;
1207
1208 u_uastrcpy(temp, "###0.0#");
1209 fmt=unum_open(UNUM_IGNORE, temp, -1, "en", NULL, &status);
1210 if (U_FAILURE(status)) {
1211 log_data_err("got unexpected error for unum_open: '%s'\n", u_errorName(status));
1212 return;
1213 }
1214 unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE);
1215 unum_setAttribute(fmt, UNUM_MAX_SIGNIFICANT_DIGITS, 6);
1216
1217 u_uastrcpy(temp, "123457");
1218 resultlength=0;
1219 resultlengthneeded=unum_formatDouble(fmt, d, NULL, resultlength, NULL, &status);
1220 if(status==U_BUFFER_OVERFLOW_ERROR)
1221 {
1222 status=U_ZERO_ERROR;
1223 resultlength=resultlengthneeded+1;
1224 result=(UChar*)malloc(sizeof(UChar) * resultlength);
1225 unum_formatDouble(fmt, d, result, resultlength, NULL, &status);
1226 }
1227 if(U_FAILURE(status))
1228 {
1229 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
1230 return;
1231 }
1232 if(u_strcmp(result, temp)==0)
1233 log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
1234 else
1235 log_err("FAIL: Error in number formatting using unum_formatDouble()\n");
1236 free(result);
1237 unum_close(fmt);
1238 }
1239
TestSigDigRounding()1240 static void TestSigDigRounding()
1241 {
1242 UErrorCode status = U_ZERO_ERROR;
1243 UChar expected[128];
1244 UChar result[128];
1245 char temp1[128];
1246 char temp2[128];
1247 UNumberFormat* fmt;
1248 double d = 123.4;
1249
1250 fmt=unum_open(UNUM_DECIMAL, NULL, 0, "en", NULL, &status);
1251 if (U_FAILURE(status)) {
1252 log_data_err("got unexpected error for unum_open: '%s'\n", u_errorName(status));
1253 return;
1254 }
1255 unum_setAttribute(fmt, UNUM_LENIENT_PARSE, FALSE);
1256 unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE);
1257 unum_setAttribute(fmt, UNUM_MAX_SIGNIFICANT_DIGITS, 2);
1258 /* unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 0); */
1259
1260 unum_setAttribute(fmt, UNUM_ROUNDING_MODE, UNUM_ROUND_UP);
1261 unum_setDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT, 20.0);
1262
1263 (void)unum_formatDouble(fmt, d, result, UPRV_LENGTHOF(result), NULL, &status);
1264 if(U_FAILURE(status))
1265 {
1266 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
1267 return;
1268 }
1269
1270 u_uastrcpy(expected, "140");
1271 if(u_strcmp(result, expected)!=0)
1272 log_err("FAIL: Error in unum_formatDouble result %s instead of %s\n", u_austrcpy(temp1, result), u_austrcpy(temp2, expected) );
1273
1274 unum_close(fmt);
1275 }
1276
TestNumberFormatPadding()1277 static void TestNumberFormatPadding()
1278 {
1279 UChar *result=NULL;
1280 UChar temp1[512];
1281 UChar temp2[512];
1282
1283 UErrorCode status=U_ZERO_ERROR;
1284 int32_t resultlength;
1285 int32_t resultlengthneeded;
1286 UNumberFormat *pattern;
1287 double d1;
1288 double d = -10456.37;
1289 UFieldPosition pos1;
1290 int32_t parsepos;
1291
1292 /* create a number format using unum_openPattern(....)*/
1293 log_verbose("\nTesting unum_openPattern() with padding\n");
1294 u_uastrcpy(temp1, "*#,##0.0#*;(#,##0.0#)");
1295 status=U_ZERO_ERROR;
1296 pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), NULL, NULL,&status);
1297 if(U_SUCCESS(status))
1298 {
1299 log_err("error in unum_openPattern(%s): %s\n", temp1, myErrorName(status) );
1300 }
1301 else
1302 {
1303 unum_close(pattern);
1304 }
1305
1306 /* u_uastrcpy(temp1, "*x#,###,###,##0.0#;(*x#,###,###,##0.0#)"); */
1307 u_uastrcpy(temp1, "*x#,###,###,##0.0#;*x(###,###,##0.0#)"); // input pattern
1308 u_uastrcpy(temp2, "*x#########,##0.0#;(#########,##0.0#)"); // equivalent (?) output pattern
1309 status=U_ZERO_ERROR;
1310 pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), "en_US",NULL, &status);
1311 if(U_FAILURE(status))
1312 {
1313 log_err_status(status, "error in padding unum_openPattern(%s): %s\n", temp1, myErrorName(status) );;
1314 }
1315 else {
1316 log_verbose("Pass: padding unum_openPattern() works fine\n");
1317
1318 /*test for unum_toPattern()*/
1319 log_verbose("\nTesting padding unum_toPattern()\n");
1320 resultlength=0;
1321 resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &status);
1322 if(status==U_BUFFER_OVERFLOW_ERROR)
1323 {
1324 status=U_ZERO_ERROR;
1325 resultlength=resultlengthneeded+1;
1326 result=(UChar*)malloc(sizeof(UChar) * resultlength);
1327 unum_toPattern(pattern, FALSE, result, resultlength, &status);
1328 }
1329 if(U_FAILURE(status))
1330 {
1331 log_err("error in extracting the padding pattern from UNumberFormat: %s\n", myErrorName(status));
1332 }
1333 else
1334 {
1335 if(u_strncmp(result, temp2, resultlengthneeded)!=0) {
1336 log_err(
1337 "FAIL: Error in extracting the padding pattern using unum_toPattern(): %d: %s != %s\n",
1338 resultlengthneeded,
1339 austrdup(temp2),
1340 austrdup(result));
1341 } else {
1342 log_verbose("Pass: extracted the padding pattern correctly using unum_toPattern()\n");
1343 }
1344 }
1345 free(result);
1346 /* u_uastrcpy(temp1, "(xxxxxxx10,456.37)"); */
1347 u_uastrcpy(temp1, "xxxxx(10,456.37)");
1348 resultlength=0;
1349 pos1.field = UNUM_FRACTION_FIELD;
1350 resultlengthneeded=unum_formatDouble(pattern, d, NULL, resultlength, &pos1, &status);
1351 if(status==U_BUFFER_OVERFLOW_ERROR)
1352 {
1353 status=U_ZERO_ERROR;
1354 resultlength=resultlengthneeded+1;
1355 result=(UChar*)malloc(sizeof(UChar) * resultlength);
1356 unum_formatDouble(pattern, d, result, resultlength, NULL, &status);
1357 }
1358 if(U_FAILURE(status))
1359 {
1360 log_err("Error in formatting using unum_formatDouble(.....) with padding : %s\n", myErrorName(status));
1361 }
1362 else
1363 {
1364 if(u_strcmp(result, temp1)==0)
1365 log_verbose("Pass: Number Formatting using unum_formatDouble() padding Successful\n");
1366 else
1367 log_data_err("FAIL: Error in number formatting using unum_formatDouble() with padding\n");
1368 if(pos1.beginIndex == 13 && pos1.endIndex == 15)
1369 log_verbose("Pass: Complete number formatting using unum_formatDouble() successful\n");
1370 else
1371 log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=13 end=15\n",
1372 pos1.beginIndex, pos1.endIndex);
1373
1374
1375 /* Testing unum_parse() and unum_parseDouble() */
1376 log_verbose("\nTesting padding unum_parseDouble()\n");
1377 parsepos=0;
1378 d1=unum_parseDouble(pattern, result, u_strlen(result), &parsepos, &status);
1379 if(U_FAILURE(status))
1380 {
1381 log_err("padding parse failed. The error is : %s\n", myErrorName(status));
1382 }
1383
1384 if(d1!=d)
1385 log_err("Fail: Error in padding parsing\n");
1386 else
1387 log_verbose("Pass: padding parsing successful\n");
1388 free(result);
1389 }
1390 }
1391
1392 unum_close(pattern);
1393 }
1394
1395 static UBool
withinErr(double a,double b,double err)1396 withinErr(double a, double b, double err) {
1397 return uprv_fabs(a - b) < uprv_fabs(a * err);
1398 }
1399
TestInt64Format()1400 static void TestInt64Format() {
1401 UChar temp1[512];
1402 UChar result[512];
1403 UNumberFormat *fmt;
1404 UErrorCode status = U_ZERO_ERROR;
1405 const double doubleInt64Max = (double)U_INT64_MAX;
1406 const double doubleInt64Min = (double)U_INT64_MIN;
1407 const double doubleBig = 10.0 * (double)U_INT64_MAX;
1408 int32_t val32;
1409 int64_t val64;
1410 double valDouble;
1411 int32_t parsepos;
1412
1413 /* create a number format using unum_openPattern(....) */
1414 log_verbose("\nTesting Int64Format\n");
1415 u_uastrcpy(temp1, "#.#E0");
1416 fmt = unum_open(UNUM_IGNORE, temp1, u_strlen(temp1), "en_US", NULL, &status);
1417 if(U_FAILURE(status)) {
1418 log_data_err("error in unum_openPattern() - %s\n", myErrorName(status));
1419 } else {
1420 unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 20);
1421 unum_formatInt64(fmt, U_INT64_MAX, result, 512, NULL, &status);
1422 if (U_FAILURE(status)) {
1423 log_err("error in unum_format(): %s\n", myErrorName(status));
1424 } else {
1425 log_verbose("format int64max: '%s'\n", result);
1426 parsepos = 0;
1427 val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
1428 if (status != U_INVALID_FORMAT_ERROR) {
1429 log_err("parse didn't report error: %s\n", myErrorName(status));
1430 } else if (val32 != INT32_MAX) {
1431 log_err("parse didn't pin return value, got: %d\n", val32);
1432 }
1433
1434 status = U_ZERO_ERROR;
1435 parsepos = 0;
1436 val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
1437 if (U_FAILURE(status)) {
1438 log_err("parseInt64 returned error: %s\n", myErrorName(status));
1439 } else if (val64 != U_INT64_MAX) {
1440 log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
1441 }
1442
1443 status = U_ZERO_ERROR;
1444 parsepos = 0;
1445 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
1446 if (U_FAILURE(status)) {
1447 log_err("parseDouble returned error: %s\n", myErrorName(status));
1448 } else if (valDouble != doubleInt64Max) {
1449 log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
1450 }
1451 }
1452
1453 unum_formatInt64(fmt, U_INT64_MIN, result, 512, NULL, &status);
1454 if (U_FAILURE(status)) {
1455 log_err("error in unum_format(): %s\n", myErrorName(status));
1456 } else {
1457 log_verbose("format int64min: '%s'\n", result);
1458 parsepos = 0;
1459 val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
1460 if (status != U_INVALID_FORMAT_ERROR) {
1461 log_err("parse didn't report error: %s\n", myErrorName(status));
1462 } else if (val32 != INT32_MIN) {
1463 log_err("parse didn't pin return value, got: %d\n", val32);
1464 }
1465
1466 status = U_ZERO_ERROR;
1467 parsepos = 0;
1468 val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
1469 if (U_FAILURE(status)) {
1470 log_err("parseInt64 returned error: %s\n", myErrorName(status));
1471 } else if (val64 != U_INT64_MIN) {
1472 log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
1473 }
1474
1475 status = U_ZERO_ERROR;
1476 parsepos = 0;
1477 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
1478 if (U_FAILURE(status)) {
1479 log_err("parseDouble returned error: %s\n", myErrorName(status));
1480 } else if (valDouble != doubleInt64Min) {
1481 log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
1482 }
1483 }
1484
1485 unum_formatDouble(fmt, doubleBig, result, 512, NULL, &status);
1486 if (U_FAILURE(status)) {
1487 log_err("error in unum_format(): %s\n", myErrorName(status));
1488 } else {
1489 log_verbose("format doubleBig: '%s'\n", result);
1490 parsepos = 0;
1491 val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status);
1492 if (status != U_INVALID_FORMAT_ERROR) {
1493 log_err("parse didn't report error: %s\n", myErrorName(status));
1494 } else if (val32 != INT32_MAX) {
1495 log_err("parse didn't pin return value, got: %d\n", val32);
1496 }
1497
1498 status = U_ZERO_ERROR;
1499 parsepos = 0;
1500 val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status);
1501 if (status != U_INVALID_FORMAT_ERROR) {
1502 log_err("parseInt64 didn't report error error: %s\n", myErrorName(status));
1503 } else if (val64 != U_INT64_MAX) {
1504 log_err("parseInt64 returned incorrect value, got: %ld\n", val64);
1505 }
1506
1507 status = U_ZERO_ERROR;
1508 parsepos = 0;
1509 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
1510 if (U_FAILURE(status)) {
1511 log_err("parseDouble returned error: %s\n", myErrorName(status));
1512 } else if (!withinErr(valDouble, doubleBig, 1e-15)) {
1513 log_err("parseDouble returned incorrect value, got: %g\n", valDouble);
1514 }
1515 }
1516
1517 u_uastrcpy(result, "5.06e-27");
1518 parsepos = 0;
1519 valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status);
1520 if (U_FAILURE(status)) {
1521 log_err("parseDouble() returned error: %s\n", myErrorName(status));
1522 } else if (!withinErr(valDouble, 5.06e-27, 1e-15)) {
1523 log_err("parseDouble() returned incorrect value, got: %g\n", valDouble);
1524 }
1525 }
1526 unum_close(fmt);
1527 }
1528
1529
test_fmt(UNumberFormat * fmt,UBool isDecimal)1530 static void test_fmt(UNumberFormat* fmt, UBool isDecimal) {
1531 char temp[512];
1532 UChar buffer[512];
1533 int32_t BUFSIZE = UPRV_LENGTHOF(buffer);
1534 double vals[] = {
1535 -.2, 0, .2, 5.5, 15.2, 250, 123456789
1536 };
1537 int i;
1538
1539 for (i = 0; i < UPRV_LENGTHOF(vals); ++i) {
1540 UErrorCode status = U_ZERO_ERROR;
1541 unum_formatDouble(fmt, vals[i], buffer, BUFSIZE, NULL, &status);
1542 if (U_FAILURE(status)) {
1543 log_err("failed to format: %g, returned %s\n", vals[i], u_errorName(status));
1544 } else {
1545 u_austrcpy(temp, buffer);
1546 log_verbose("formatting %g returned '%s'\n", vals[i], temp);
1547 }
1548 }
1549
1550 /* check APIs now */
1551 {
1552 UErrorCode status = U_ZERO_ERROR;
1553 UParseError perr;
1554 u_uastrcpy(buffer, "#,##0.0#");
1555 unum_applyPattern(fmt, FALSE, buffer, -1, &perr, &status);
1556 if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) {
1557 log_err("got unexpected error for applyPattern: '%s'\n", u_errorName(status));
1558 }
1559 }
1560
1561 {
1562 int isLenient = unum_getAttribute(fmt, UNUM_LENIENT_PARSE);
1563 log_verbose("lenient: 0x%x\n", isLenient);
1564 if (isLenient != FALSE) {
1565 log_err("didn't expect lenient value: %d\n", isLenient);
1566 }
1567
1568 unum_setAttribute(fmt, UNUM_LENIENT_PARSE, TRUE);
1569 isLenient = unum_getAttribute(fmt, UNUM_LENIENT_PARSE);
1570 if (isLenient != TRUE) {
1571 log_err("didn't expect lenient value after set: %d\n", isLenient);
1572 }
1573 }
1574
1575 {
1576 double val2;
1577 double val = unum_getDoubleAttribute(fmt, UNUM_LENIENT_PARSE);
1578 if (val != -1) {
1579 log_err("didn't expect double attribute\n");
1580 }
1581 val = unum_getDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT);
1582 if ((val == -1) == isDecimal) {
1583 log_err("didn't expect -1 rounding increment\n");
1584 }
1585 unum_setDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT, val+.5);
1586 val2 = unum_getDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT);
1587 if (isDecimal && (val2 - val != .5)) {
1588 log_err("set rounding increment had no effect on decimal format");
1589 }
1590 }
1591
1592 {
1593 UErrorCode status = U_ZERO_ERROR;
1594 int len = unum_getTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, BUFSIZE, &status);
1595 if (isDecimal ? (status != U_UNSUPPORTED_ERROR) : U_FAILURE(status)) {
1596 log_err("got unexpected error for get default ruleset: '%s'\n", u_errorName(status));
1597 }
1598 if (U_SUCCESS(status)) {
1599 u_austrcpy(temp, buffer);
1600 log_verbose("default ruleset: '%s'\n", temp);
1601 }
1602
1603 status = U_ZERO_ERROR;
1604 len = unum_getTextAttribute(fmt, UNUM_PUBLIC_RULESETS, buffer, BUFSIZE, &status);
1605 if (isDecimal ? (status != U_UNSUPPORTED_ERROR) : U_FAILURE(status)) {
1606 log_err("got unexpected error for get public rulesets: '%s'\n", u_errorName(status));
1607 }
1608 if (U_SUCCESS(status)) {
1609 u_austrcpy(temp, buffer);
1610 log_verbose("public rulesets: '%s'\n", temp);
1611
1612 /* set the default ruleset to the first one found, and retry */
1613
1614 if (len > 0) {
1615 for (i = 0; i < len && temp[i] != ';'; ++i){};
1616 if (i < len) {
1617 buffer[i] = 0;
1618 unum_setTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, -1, &status);
1619 if (U_FAILURE(status)) {
1620 log_err("unexpected error setting default ruleset: '%s'\n", u_errorName(status));
1621 } else {
1622 int len2 = unum_getTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, BUFSIZE, &status);
1623 if (U_FAILURE(status)) {
1624 log_err("could not fetch default ruleset: '%s'\n", u_errorName(status));
1625 } else if (len2 != i) {
1626 u_austrcpy(temp, buffer);
1627 log_err("unexpected ruleset len: %d ex: %d val: %s\n", len2, i, temp);
1628 } else {
1629 for (i = 0; i < UPRV_LENGTHOF(vals); ++i) {
1630 status = U_ZERO_ERROR;
1631 unum_formatDouble(fmt, vals[i], buffer, BUFSIZE, NULL, &status);
1632 if (U_FAILURE(status)) {
1633 log_err("failed to format: %g, returned %s\n", vals[i], u_errorName(status));
1634 } else {
1635 u_austrcpy(temp, buffer);
1636 log_verbose("formatting %g returned '%s'\n", vals[i], temp);
1637 }
1638 }
1639 }
1640 }
1641 }
1642 }
1643 }
1644 }
1645
1646 {
1647 UErrorCode status = U_ZERO_ERROR;
1648 unum_toPattern(fmt, FALSE, buffer, BUFSIZE, &status);
1649 if (U_SUCCESS(status)) {
1650 u_austrcpy(temp, buffer);
1651 log_verbose("pattern: '%s'\n", temp);
1652 } else if (status != U_BUFFER_OVERFLOW_ERROR) {
1653 log_err("toPattern failed unexpectedly: %s\n", u_errorName(status));
1654 } else {
1655 log_verbose("pattern too long to display\n");
1656 }
1657 }
1658
1659 {
1660 UErrorCode status = U_ZERO_ERROR;
1661 int len = unum_getSymbol(fmt, UNUM_CURRENCY_SYMBOL, buffer, BUFSIZE, &status);
1662 if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) {
1663 log_err("unexpected error getting symbol: '%s'\n", u_errorName(status));
1664 }
1665
1666 unum_setSymbol(fmt, UNUM_CURRENCY_SYMBOL, buffer, len, &status);
1667 if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) {
1668 log_err("unexpected error setting symbol: '%s'\n", u_errorName(status));
1669 }
1670 }
1671 }
1672
TestNonExistentCurrency()1673 static void TestNonExistentCurrency() {
1674 UNumberFormat *format;
1675 UErrorCode status = U_ZERO_ERROR;
1676 UChar currencySymbol[8];
1677 static const UChar QQQ[] = {0x51, 0x51, 0x51, 0};
1678
1679 /* Get a non-existent currency and make sure it returns the correct currency code. */
1680 format = unum_open(UNUM_CURRENCY, NULL, 0, "th_TH@currency=QQQ", NULL, &status);
1681 if (format == NULL || U_FAILURE(status)) {
1682 log_data_err("unum_open did not return expected result for non-existent requested currency: '%s' (Are you missing data?)\n", u_errorName(status));
1683 }
1684 else {
1685 unum_getSymbol(format,
1686 UNUM_CURRENCY_SYMBOL,
1687 currencySymbol,
1688 UPRV_LENGTHOF(currencySymbol),
1689 &status);
1690 if (u_strcmp(currencySymbol, QQQ) != 0) {
1691 log_err("unum_open set the currency to QQQ\n");
1692 }
1693 }
1694 unum_close(format);
1695 }
1696
TestRBNFFormat()1697 static void TestRBNFFormat() {
1698 UErrorCode status;
1699 UParseError perr;
1700 UChar pat[1024];
1701 UChar tempUChars[512];
1702 UNumberFormat *formats[5];
1703 int COUNT = UPRV_LENGTHOF(formats);
1704 int i;
1705
1706 for (i = 0; i < COUNT; ++i) {
1707 formats[i] = 0;
1708 }
1709
1710 /* instantiation */
1711 status = U_ZERO_ERROR;
1712 u_uastrcpy(pat, "#,##0.0#;(#,##0.0#)");
1713 formats[0] = unum_open(UNUM_PATTERN_DECIMAL, pat, -1, "en_US", &perr, &status);
1714 if (U_FAILURE(status)) {
1715 log_err_status(status, "unable to open decimal pattern -> %s\n", u_errorName(status));
1716 return;
1717 }
1718
1719 status = U_ZERO_ERROR;
1720 formats[1] = unum_open(UNUM_SPELLOUT, NULL, 0, "en_US", &perr, &status);
1721 if (U_FAILURE(status)) {
1722 log_err_status(status, "unable to open spellout -> %s\n", u_errorName(status));
1723 return;
1724 }
1725
1726 status = U_ZERO_ERROR;
1727 formats[2] = unum_open(UNUM_ORDINAL, NULL, 0, "en_US", &perr, &status);
1728 if (U_FAILURE(status)) {
1729 log_err_status(status, "unable to open ordinal -> %s\n", u_errorName(status));
1730 return;
1731 }
1732
1733 status = U_ZERO_ERROR;
1734 formats[3] = unum_open(UNUM_DURATION, NULL, 0, "en_US", &perr, &status);
1735 if (U_FAILURE(status)) {
1736 log_err_status(status, "unable to open duration %s\n", u_errorName(status));
1737 return;
1738 }
1739
1740 status = U_ZERO_ERROR;
1741 u_uastrcpy(pat,
1742 "%standard:\n"
1743 "-x: minus >>;\n"
1744 "x.x: << point >>;\n"
1745 "zero; one; two; three; four; five; six; seven; eight; nine;\n"
1746 "ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n"
1747 "seventeen; eighteen; nineteen;\n"
1748 "20: twenty[->>];\n"
1749 "30: thirty[->>];\n"
1750 "40: forty[->>];\n"
1751 "50: fifty[->>];\n"
1752 "60: sixty[->>];\n"
1753 "70: seventy[->>];\n"
1754 "80: eighty[->>];\n"
1755 "90: ninety[->>];\n"
1756 "100: =#,##0=;\n");
1757 u_uastrcpy(tempUChars,
1758 "%simple:\n"
1759 "=%standard=;\n"
1760 "20: twenty[ and change];\n"
1761 "30: thirty[ and change];\n"
1762 "40: forty[ and change];\n"
1763 "50: fifty[ and change];\n"
1764 "60: sixty[ and change];\n"
1765 "70: seventy[ and change];\n"
1766 "80: eighty[ and change];\n"
1767 "90: ninety[ and change];\n"
1768 "100: =#,##0=;\n"
1769 "%bogus:\n"
1770 "0.x: tiny;\n"
1771 "x.x: << point something;\n"
1772 "=%standard=;\n"
1773 "20: some reasonable number;\n"
1774 "100: some substantial number;\n"
1775 "100,000,000: some huge number;\n");
1776 /* This is to get around some compiler warnings about char * string length. */
1777 u_strcat(pat, tempUChars);
1778 formats[4] = unum_open(UNUM_PATTERN_RULEBASED, pat, -1, "en_US", &perr, &status);
1779 if (U_FAILURE(status)) {
1780 log_err_status(status, "unable to open rulebased pattern -> %s\n", u_errorName(status));
1781 }
1782 if (U_FAILURE(status)) {
1783 log_err_status(status, "Something failed with %s\n", u_errorName(status));
1784 return;
1785 }
1786
1787 for (i = 0; i < COUNT; ++i) {
1788 log_verbose("\n\ntesting format %d\n", i);
1789 test_fmt(formats[i], (UBool)(i == 0));
1790 }
1791
1792 #define FORMAT_BUF_CAPACITY 64
1793 {
1794 UChar fmtbuf[FORMAT_BUF_CAPACITY];
1795 int32_t len;
1796 double nanvalue = uprv_getNaN();
1797 status = U_ZERO_ERROR;
1798 len = unum_formatDouble(formats[1], nanvalue, fmtbuf, FORMAT_BUF_CAPACITY, NULL, &status);
1799 if (U_FAILURE(status)) {
1800 log_err_status(status, "unum_formatDouble NAN failed with %s\n", u_errorName(status));
1801 } else {
1802 UChar nansym[] = { 0x4E, 0x61, 0x4E, 0 }; /* NaN */
1803 if ( len != 3 || u_strcmp(fmtbuf, nansym) != 0 ) {
1804 log_err("unum_formatDouble NAN produced wrong answer for en_US\n");
1805 }
1806 }
1807 }
1808
1809 for (i = 0; i < COUNT; ++i) {
1810 unum_close(formats[i]);
1811 }
1812 }
1813
TestRBNFRounding()1814 static void TestRBNFRounding() {
1815 UChar fmtbuf[FORMAT_BUF_CAPACITY];
1816 UChar expectedBuf[FORMAT_BUF_CAPACITY];
1817 int32_t len;
1818 UErrorCode status = U_ZERO_ERROR;
1819 UNumberFormat* fmt = unum_open(UNUM_SPELLOUT, NULL, 0, "en_US", NULL, &status);
1820 if (U_FAILURE(status)) {
1821 log_err_status(status, "unable to open spellout -> %s\n", u_errorName(status));
1822 return;
1823 }
1824 len = unum_formatDouble(fmt, 10.123456789, fmtbuf, FORMAT_BUF_CAPACITY, NULL, &status);
1825 if (U_FAILURE(status)) {
1826 log_err_status(status, "unum_formatDouble 10.123456789 failed with %s\n", u_errorName(status));
1827 }
1828 u_uastrcpy(expectedBuf, "ten point one two three four five six seven eight nine");
1829 if (u_strcmp(expectedBuf, fmtbuf) != 0) {
1830 log_err("Wrong result for unrounded value\n");
1831 }
1832 unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 3);
1833 if (unum_getAttribute(fmt, UNUM_MAX_FRACTION_DIGITS) != 3) {
1834 log_err("UNUM_MAX_FRACTION_DIGITS was incorrectly ignored -> %d\n", unum_getAttribute(fmt, UNUM_MAX_FRACTION_DIGITS));
1835 }
1836 if (unum_getAttribute(fmt, UNUM_ROUNDING_MODE) != UNUM_ROUND_UNNECESSARY) {
1837 log_err("UNUM_ROUNDING_MODE was set -> %d\n", unum_getAttribute(fmt, UNUM_ROUNDING_MODE));
1838 }
1839 unum_setAttribute(fmt, UNUM_ROUNDING_MODE, UNUM_ROUND_HALFUP);
1840 if (unum_getAttribute(fmt, UNUM_ROUNDING_MODE) != UNUM_ROUND_HALFUP) {
1841 log_err("UNUM_ROUNDING_MODE was not set -> %d\n", unum_getAttribute(fmt, UNUM_ROUNDING_MODE));
1842 }
1843 len = unum_formatDouble(fmt, 10.123456789, fmtbuf, FORMAT_BUF_CAPACITY, NULL, &status);
1844 if (U_FAILURE(status)) {
1845 log_err_status(status, "unum_formatDouble 10.123456789 failed with %s\n", u_errorName(status));
1846 }
1847 u_uastrcpy(expectedBuf, "ten point one two three");
1848 if (u_strcmp(expectedBuf, fmtbuf) != 0) {
1849 char temp[512];
1850 u_austrcpy(temp, fmtbuf);
1851 log_err("Wrong result for rounded value. Got: %s\n", temp);
1852 }
1853 unum_close(fmt);
1854 }
1855
TestCurrencyRegression(void)1856 static void TestCurrencyRegression(void) {
1857 /*
1858 I've found a case where unum_parseDoubleCurrency is not doing what I
1859 expect. The value I pass in is $1234567890q123460000.00 and this
1860 returns with a status of zero error & a parse pos of 22 (I would
1861 expect a parse error at position 11).
1862
1863 I stepped into DecimalFormat::subparse() and it looks like it parses
1864 the first 10 digits and then stops parsing at the q but doesn't set an
1865 error. Then later in DecimalFormat::parse() the value gets crammed
1866 into a long (which greatly truncates the value).
1867
1868 This is very problematic for me 'cause I try to remove chars that are
1869 invalid but this allows my users to enter bad chars and truncates
1870 their data!
1871 */
1872
1873 UChar buf[1024];
1874 UChar currency[8];
1875 char acurrency[16];
1876 double d;
1877 UNumberFormat *cur;
1878 int32_t pos;
1879 UErrorCode status = U_ZERO_ERROR;
1880 const int32_t expected = 11;
1881
1882 currency[0]=0;
1883 u_uastrcpy(buf, "$1234567890q643210000.00");
1884 cur = unum_open(UNUM_CURRENCY, NULL,0,"en_US", NULL, &status);
1885
1886 if(U_FAILURE(status)) {
1887 log_data_err("unum_open failed: %s (Are you missing data?)\n", u_errorName(status));
1888 return;
1889 }
1890
1891 status = U_ZERO_ERROR; /* so we can test it later. */
1892 pos = 0;
1893
1894 d = unum_parseDoubleCurrency(cur,
1895 buf,
1896 -1,
1897 &pos, /* 0 = start */
1898 currency,
1899 &status);
1900
1901 u_austrcpy(acurrency, currency);
1902
1903 if(U_FAILURE(status) || (pos != expected)) {
1904 log_err("unum_parseDoubleCurrency should have failed with pos %d, but gave: value %.9f, err %s, pos=%d, currency [%s]\n",
1905 expected, d, u_errorName(status), pos, acurrency);
1906 } else {
1907 log_verbose("unum_parseDoubleCurrency failed, value %.9f err %s, pos %d, currency [%s]\n", d, u_errorName(status), pos, acurrency);
1908 }
1909
1910 unum_close(cur);
1911 }
1912
TestTextAttributeCrash(void)1913 static void TestTextAttributeCrash(void) {
1914 UChar ubuffer[64] = {0x0049,0x004E,0x0052,0};
1915 static const UChar expectedNeg[] = {0x0049,0x004E,0x0052,0x0031,0x0032,0x0033,0x0034,0x002E,0x0035,0};
1916 static const UChar expectedPos[] = {0x0031,0x0032,0x0033,0x0034,0x002E,0x0035,0};
1917 int32_t used;
1918 UErrorCode status = U_ZERO_ERROR;
1919 UNumberFormat *nf = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status);
1920 if (U_FAILURE(status)) {
1921 log_data_err("FAILED 1 -> %s (Are you missing data?)\n", u_errorName(status));
1922 return;
1923 }
1924 unum_setTextAttribute(nf, UNUM_CURRENCY_CODE, ubuffer, 3, &status);
1925 /*
1926 * the usual negative prefix and suffix seem to be '($' and ')' at this point
1927 * also crashes if UNUM_NEGATIVE_SUFFIX is substituted for UNUM_NEGATIVE_PREFIX here
1928 */
1929 used = unum_getTextAttribute(nf, UNUM_NEGATIVE_PREFIX, ubuffer, 64, &status);
1930 unum_setTextAttribute(nf, UNUM_NEGATIVE_PREFIX, ubuffer, used, &status);
1931 if (U_FAILURE(status)) {
1932 log_err("FAILED 2\n"); exit(1);
1933 }
1934 log_verbose("attempting to format...\n");
1935 used = unum_formatDouble(nf, -1234.5, ubuffer, 64, NULL, &status);
1936 if (U_FAILURE(status) || 64 < used) {
1937 log_err("Failed formatting %s\n", u_errorName(status));
1938 return;
1939 }
1940 if (u_strcmp(expectedNeg, ubuffer) == 0) {
1941 log_err("Didn't get expected negative result\n");
1942 }
1943 used = unum_formatDouble(nf, 1234.5, ubuffer, 64, NULL, &status);
1944 if (U_FAILURE(status) || 64 < used) {
1945 log_err("Failed formatting %s\n", u_errorName(status));
1946 return;
1947 }
1948 if (u_strcmp(expectedPos, ubuffer) == 0) {
1949 log_err("Didn't get expected positive result\n");
1950 }
1951 unum_close(nf);
1952 }
1953
TestNBSPPatternRtNum(const char * testcase,int line,UNumberFormat * nf,double myNumber)1954 static void TestNBSPPatternRtNum(const char *testcase, int line, UNumberFormat *nf, double myNumber) {
1955 UErrorCode status = U_ZERO_ERROR;
1956 UChar myString[20];
1957 char tmpbuf[200];
1958 double aNumber = -1.0;
1959 unum_formatDouble(nf, myNumber, myString, 20, NULL, &status);
1960 log_verbose("%s:%d: formatted %.2f into %s\n", testcase, line, myNumber, u_austrcpy(tmpbuf, myString));
1961 if(U_FAILURE(status)) {
1962 log_err("%s:%d: failed format of %.2g with %s\n", testcase, line, myNumber, u_errorName(status));
1963 return;
1964 }
1965 aNumber = unum_parse(nf, myString, -1, NULL, &status);
1966 if(U_FAILURE(status)) {
1967 log_err("%s:%d: failed parse with %s\n", testcase, line, u_errorName(status));
1968 return;
1969 }
1970 if(uprv_fabs(aNumber-myNumber)>.001) {
1971 log_err("FAIL: %s:%d formatted %.2f, parsed into %.2f\n", testcase, line, myNumber, aNumber);
1972 } else {
1973 log_verbose("PASS: %s:%d formatted %.2f, parsed into %.2f\n", testcase, line, myNumber, aNumber);
1974 }
1975 }
1976
TestNBSPPatternRT(const char * testcase,UNumberFormat * nf)1977 static void TestNBSPPatternRT(const char *testcase, UNumberFormat *nf) {
1978 TestNBSPPatternRtNum(testcase, __LINE__, nf, 12345.);
1979 TestNBSPPatternRtNum(testcase, __LINE__, nf, -12345.);
1980 }
1981
TestNBSPInPattern(void)1982 static void TestNBSPInPattern(void) {
1983 UErrorCode status = U_ZERO_ERROR;
1984 UNumberFormat* nf = NULL;
1985 const char *testcase;
1986
1987
1988 testcase="ar_AE UNUM_CURRENCY";
1989 nf = unum_open(UNUM_CURRENCY, NULL, -1, "ar_AE", NULL, &status);
1990 if(U_FAILURE(status) || nf == NULL) {
1991 log_data_err("%s:%d: %s: unum_open failed with %s (Are you missing data?)\n", __FILE__, __LINE__, testcase, u_errorName(status));
1992 return;
1993 }
1994 TestNBSPPatternRT(testcase, nf);
1995
1996 /* if we don't have CLDR 1.6 data, bring out the problem anyways */
1997 {
1998 #define SPECIAL_PATTERN "\\u00A4\\u00A4'\\u062f.\\u0625.\\u200f\\u00a0'###0.00"
1999 UChar pat[200];
2000 testcase = "ar_AE special pattern: " SPECIAL_PATTERN;
2001 u_unescape(SPECIAL_PATTERN, pat, UPRV_LENGTHOF(pat));
2002 unum_applyPattern(nf, FALSE, pat, -1, NULL, &status);
2003 if(U_FAILURE(status)) {
2004 log_err("%s: unum_applyPattern failed with %s\n", testcase, u_errorName(status));
2005 } else {
2006 TestNBSPPatternRT(testcase, nf);
2007 }
2008 #undef SPECIAL_PATTERN
2009 }
2010 unum_close(nf); status = U_ZERO_ERROR;
2011
2012 testcase="ar_AE UNUM_DECIMAL";
2013 nf = unum_open(UNUM_DECIMAL, NULL, -1, "ar_AE", NULL, &status);
2014 if(U_FAILURE(status)) {
2015 log_err("%s: unum_open failed with %s\n", testcase, u_errorName(status));
2016 }
2017 TestNBSPPatternRT(testcase, nf);
2018 unum_close(nf); status = U_ZERO_ERROR;
2019
2020 testcase="ar_AE UNUM_PERCENT";
2021 nf = unum_open(UNUM_PERCENT, NULL, -1, "ar_AE", NULL, &status);
2022 if(U_FAILURE(status)) {
2023 log_err("%s: unum_open failed with %s\n", testcase, u_errorName(status));
2024 }
2025 TestNBSPPatternRT(testcase, nf);
2026 unum_close(nf); status = U_ZERO_ERROR;
2027
2028
2029
2030 }
TestCloneWithRBNF(void)2031 static void TestCloneWithRBNF(void) {
2032 UChar pattern[1024];
2033 UChar pat2[512];
2034 UErrorCode status = U_ZERO_ERROR;
2035 UChar buffer[256];
2036 UChar buffer_cloned[256];
2037 char temp1[256];
2038 char temp2[256];
2039 UNumberFormat *pform_cloned;
2040 UNumberFormat *pform;
2041
2042 u_uastrcpy(pattern,
2043 "%main:\n"
2044 "0.x: >%%millis-only>;\n"
2045 "x.0: <%%duration<;\n"
2046 "x.x: <%%durationwithmillis<>%%millis-added>;\n"
2047 "-x: ->>;%%millis-only:\n"
2048 "1000: 00:00.<%%millis<;\n"
2049 "%%millis-added:\n"
2050 "1000: .<%%millis<;\n"
2051 "%%millis:\n"
2052 "0: =000=;\n"
2053 "%%duration:\n"
2054 "0: =%%seconds-only=;\n"
2055 "60: =%%min-sec=;\n"
2056 "3600: =%%hr-min-sec=;\n"
2057 "86400/86400: <%%ddaayyss<[, >>];\n"
2058 "%%durationwithmillis:\n"
2059 "0: =%%seconds-only=;\n"
2060 "60: =%%min-sec=;\n"
2061 "3600: =%%hr-min-sec=;\n"
2062 "86400/86400: <%%ddaayyss<, >>;\n");
2063 u_uastrcpy(pat2,
2064 "%%seconds-only:\n"
2065 "0: 0:00:=00=;\n"
2066 "%%min-sec:\n"
2067 "0: :=00=;\n"
2068 "0/60: 0:<00<>>;\n"
2069 "%%hr-min-sec:\n"
2070 "0: :=00=;\n"
2071 "60/60: <00<>>;\n"
2072 "3600/60: <0<:>>>;\n"
2073 "%%ddaayyss:\n"
2074 "0 days;\n"
2075 "1 day;\n"
2076 "=0= days;");
2077
2078 /* This is to get around some compiler warnings about char * string length. */
2079 u_strcat(pattern, pat2);
2080
2081 pform = unum_open(UNUM_PATTERN_RULEBASED, pattern, -1, "en_US", NULL, &status);
2082 unum_formatDouble(pform, 3600, buffer, 256, NULL, &status);
2083
2084 pform_cloned = unum_clone(pform,&status);
2085 unum_formatDouble(pform_cloned, 3600, buffer_cloned, 256, NULL, &status);
2086
2087 unum_close(pform);
2088 unum_close(pform_cloned);
2089
2090 if (u_strcmp(buffer,buffer_cloned)) {
2091 log_data_err("Result from cloned formatter not identical to the original. Original: %s Cloned: %s - (Are you missing data?)",u_austrcpy(temp1, buffer),u_austrcpy(temp2,buffer_cloned));
2092 }
2093 }
2094
2095
TestNoExponent(void)2096 static void TestNoExponent(void) {
2097 UErrorCode status = U_ZERO_ERROR;
2098 UChar str[100];
2099 const char *cstr;
2100 UNumberFormat *fmt;
2101 int32_t pos;
2102 int32_t expect = 0;
2103 int32_t num;
2104
2105 fmt = unum_open(UNUM_DECIMAL, NULL, -1, "en_US", NULL, &status);
2106
2107 if(U_FAILURE(status) || fmt == NULL) {
2108 log_data_err("%s:%d: unum_open failed with %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status));
2109 return;
2110 }
2111
2112 cstr = "10E6";
2113 u_uastrcpy(str, cstr);
2114 expect = 10000000;
2115 pos = 0;
2116 num = unum_parse(fmt, str, -1, &pos, &status);
2117 ASSERT_TRUE(pos==4);
2118 if(U_FAILURE(status)) {
2119 log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status), cstr);
2120 } else if(expect!=num) {
2121 log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__, __LINE__, num, expect, cstr);
2122 } else {
2123 log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__, __LINE__, num, cstr);
2124 }
2125
2126 ASSERT_TRUE(unum_getAttribute(fmt, UNUM_PARSE_NO_EXPONENT)==0);
2127
2128 unum_setAttribute(fmt, UNUM_PARSE_NO_EXPONENT, 1); /* no error code */
2129 log_verbose("set UNUM_PARSE_NO_EXPONENT\n");
2130
2131 ASSERT_TRUE(unum_getAttribute(fmt, UNUM_PARSE_NO_EXPONENT)==1);
2132
2133 pos = 0;
2134 expect=10;
2135 num = unum_parse(fmt, str, -1, &pos, &status);
2136 if(num==10000000) {
2137 log_err("%s:%d: FAIL: unum_parse should have returned 10, not 10000000 on %s after UNUM_PARSE_NO_EXPONENT\n", __FILE__, __LINE__, cstr);
2138 } else if(num==expect) {
2139 log_verbose("%s:%d: unum_parse gave %d for %s - good.\n", __FILE__, __LINE__, num, cstr);
2140 }
2141 ASSERT_TRUE(pos==2);
2142
2143 status = U_ZERO_ERROR;
2144
2145 unum_close(fmt);
2146
2147 /* ok, now try scientific */
2148 fmt = unum_open(UNUM_SCIENTIFIC, NULL, -1, "en_US", NULL, &status);
2149 assertSuccess("unum_open(UNUM_SCIENTIFIC, ...)", &status);
2150
2151 ASSERT_TRUE(unum_getAttribute(fmt, UNUM_PARSE_NO_EXPONENT)==0);
2152
2153 cstr = "10E6";
2154 u_uastrcpy(str, cstr);
2155 expect = 10000000;
2156 pos = 0;
2157 num = unum_parse(fmt, str, -1, &pos, &status);
2158 ASSERT_TRUE(pos==4);
2159 if(U_FAILURE(status)) {
2160 log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status), cstr);
2161 } else if(expect!=num) {
2162 log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__, __LINE__, num, expect, cstr);
2163 } else {
2164 log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__, __LINE__, num, cstr);
2165 }
2166
2167 unum_setAttribute(fmt, UNUM_PARSE_NO_EXPONENT, 1); /* no error code */
2168 log_verbose("set UNUM_PARSE_NO_EXPONENT\n");
2169
2170 ASSERT_TRUE(unum_getAttribute(fmt, UNUM_PARSE_NO_EXPONENT)==1);
2171
2172 // A scientific formatter should parse the exponent even if UNUM_PARSE_NO_EXPONENT is set
2173 cstr = "10E6";
2174 u_uastrcpy(str, cstr);
2175 expect = 10000000;
2176 pos = 0;
2177 num = unum_parse(fmt, str, -1, &pos, &status);
2178 ASSERT_TRUE(pos==4);
2179 if(U_FAILURE(status)) {
2180 log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status), cstr);
2181 } else if(expect!=num) {
2182 log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__, __LINE__, num, expect, cstr);
2183 } else {
2184 log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__, __LINE__, num, cstr);
2185 }
2186
2187 unum_close(fmt);
2188 }
2189
TestMaxInt(void)2190 static void TestMaxInt(void) {
2191 UErrorCode status = U_ZERO_ERROR;
2192 UChar pattern_hash[] = { 0x23, 0x00 }; /* "#" */
2193 UChar result1[1024] = { 0 }, result2[1024] = { 0 };
2194 int32_t len1, len2;
2195 UChar expect[] = { 0x0039, 0x0037, 0 };
2196 UNumberFormat *fmt = unum_open(
2197 UNUM_PATTERN_DECIMAL, /* style */
2198 &pattern_hash[0], /* pattern */
2199 u_strlen(pattern_hash), /* patternLength */
2200 "en",
2201 0, /* parseErr */
2202 &status);
2203 if(U_FAILURE(status) || fmt == NULL) {
2204 log_data_err("%s:%d: %s: unum_open failed with %s (Are you missing data?)\n", __FILE__, __LINE__, "TestMaxInt", u_errorName(status));
2205 return;
2206 }
2207
2208 unum_setAttribute(fmt, UNUM_MAX_INTEGER_DIGITS, 2);
2209
2210 status = U_ZERO_ERROR;
2211 /* #1 */
2212 len1 = unum_formatInt64(fmt, 1997, result1, 1024, NULL, &status);
2213 result1[len1]=0;
2214 if(U_FAILURE(status) || u_strcmp(expect, result1)) {
2215 log_err("unum_formatInt64 Expected %s but got %s status %s\n", austrdup(expect), austrdup(result1), u_errorName(status));
2216 }
2217
2218 status = U_ZERO_ERROR;
2219 /* #2 */
2220 len2 = unum_formatDouble(fmt, 1997.0, result2, 1024, NULL, &status);
2221 result2[len2]=0;
2222 if(U_FAILURE(status) || u_strcmp(expect, result2)) {
2223 log_err("unum_formatDouble Expected %s but got %s status %s\n", austrdup(expect), austrdup(result2), u_errorName(status));
2224 }
2225
2226
2227
2228 /* test UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS */
2229 ASSERT_TRUE(unum_getAttribute(fmt, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS)==0);
2230
2231 unum_setAttribute(fmt, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS, 1);
2232 /* test UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS */
2233 ASSERT_TRUE(unum_getAttribute(fmt, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS)==1);
2234
2235 status = U_ZERO_ERROR;
2236 /* max int digits still '2' */
2237 len1 = unum_formatInt64(fmt, 1997, result1, 1024, NULL, &status);
2238 ASSERT_TRUE(status==U_ILLEGAL_ARGUMENT_ERROR);
2239 status = U_ZERO_ERROR;
2240
2241 /* But, formatting 97->'97' works fine. */
2242
2243 /* #1 */
2244 len1 = unum_formatInt64(fmt, 97, result1, 1024, NULL, &status);
2245 result1[len1]=0;
2246 if(U_FAILURE(status) || u_strcmp(expect, result1)) {
2247 log_err("unum_formatInt64 Expected %s but got %s status %s\n", austrdup(expect), austrdup(result1), u_errorName(status));
2248 }
2249
2250 status = U_ZERO_ERROR;
2251 /* #2 */
2252 len2 = unum_formatDouble(fmt, 97.0, result2, 1024, NULL, &status);
2253 result2[len2]=0;
2254 if(U_FAILURE(status) || u_strcmp(expect, result2)) {
2255 log_err("unum_formatDouble Expected %s but got %s status %s\n", austrdup(expect), austrdup(result2), u_errorName(status));
2256 }
2257
2258
2259 unum_close(fmt);
2260 }
2261
TestUFormattable(void)2262 static void TestUFormattable(void) {
2263 UChar out2k[2048];
2264 // simple test for API docs
2265 {
2266 UErrorCode status = U_ZERO_ERROR;
2267 UNumberFormat *unum = unum_open(UNUM_DEFAULT, NULL, -1, "en_US_POSIX", NULL, &status);
2268 if(assertSuccessCheck("calling unum_open()", &status, TRUE)) {
2269 //! [unum_parseToUFormattable]
2270 const UChar str[] = { 0x0031, 0x0032, 0x0033, 0x0000 }; /* 123 */
2271 int32_t result = 0;
2272 UFormattable *ufmt = ufmt_open(&status);
2273 unum_parseToUFormattable(unum, ufmt, str, -1, NULL, &status);
2274 if (ufmt_isNumeric(ufmt)) {
2275 result = ufmt_getLong(ufmt, &status); /* == 123 */
2276 } /* else { ... } */
2277 ufmt_close(ufmt);
2278 //! [unum_parseToUFormattable]
2279 assertTrue("result == 123", (result == 123));
2280 }
2281 unum_close(unum);
2282 }
2283 // test with explicitly created ufmt_open
2284 {
2285 UChar buffer[2048];
2286 UErrorCode status = U_ZERO_ERROR;
2287 UFormattable *ufmt;
2288 UNumberFormat *unum;
2289 const char *pattern = "";
2290
2291 ufmt = ufmt_open(&status);
2292 unum = unum_open(UNUM_DEFAULT, NULL, -1, "en_US_POSIX", NULL, &status);
2293 if(assertSuccessCheck("calling ufmt_open() || unum_open()", &status, TRUE)) {
2294
2295 pattern = "31337";
2296 log_verbose("-- pattern: %s\n", pattern);
2297 u_uastrcpy(buffer, pattern);
2298 unum_parseToUFormattable(unum, ufmt, buffer, -1, NULL, &status);
2299 if(assertSuccess("unum_parseToUFormattable(31337)", &status)) {
2300 assertTrue("ufmt_getLong()=31337", ufmt_getLong(ufmt, &status) == 31337);
2301 assertTrue("ufmt_getType()=UFMT_LONG", ufmt_getType(ufmt, &status) == UFMT_LONG);
2302 log_verbose("long = %d\n", ufmt_getLong(ufmt, &status));
2303 assertSuccess("ufmt_getLong()", &status);
2304 }
2305 unum_formatUFormattable(unum, ufmt, out2k, 2048, NULL, &status);
2306 if(assertSuccess("unum_formatUFormattable(31337)", &status)) {
2307 assertEquals("unum_formatUFormattable r/t", austrdup(buffer), austrdup(out2k));
2308 }
2309
2310 pattern = "3.14159";
2311 log_verbose("-- pattern: %s\n", pattern);
2312 u_uastrcpy(buffer, pattern);
2313 unum_parseToUFormattable(unum, ufmt, buffer, -1, NULL, &status);
2314 if(assertSuccess("unum_parseToUFormattable(3.14159)", &status)) {
2315 assertTrue("ufmt_getDouble()==3.14159", withinErr(ufmt_getDouble(ufmt, &status), 3.14159, 1e-15));
2316 assertSuccess("ufmt_getDouble()", &status);
2317 assertTrue("ufmt_getType()=UFMT_DOUBLE", ufmt_getType(ufmt, &status) == UFMT_DOUBLE);
2318 log_verbose("double = %g\n", ufmt_getDouble(ufmt, &status));
2319 }
2320 unum_formatUFormattable(unum, ufmt, out2k, 2048, NULL, &status);
2321 if(assertSuccess("unum_formatUFormattable(3.14159)", &status)) {
2322 assertEquals("unum_formatUFormattable r/t", austrdup(buffer), austrdup(out2k));
2323 }
2324 }
2325 ufmt_close(ufmt);
2326 unum_close(unum);
2327 }
2328
2329 // test with auto-generated ufmt
2330 {
2331 UChar buffer[2048];
2332 UErrorCode status = U_ZERO_ERROR;
2333 UFormattable *ufmt = NULL;
2334 UNumberFormat *unum;
2335 const char *pattern = "73476730924573500000000"; // weight of the moon, kg
2336
2337 log_verbose("-- pattern: %s (testing auto-opened UFormattable)\n", pattern);
2338 u_uastrcpy(buffer, pattern);
2339
2340 unum = unum_open(UNUM_DEFAULT, NULL, -1, "en_US_POSIX", NULL, &status);
2341 if(assertSuccessCheck("calling unum_open()", &status, TRUE)) {
2342
2343 ufmt = unum_parseToUFormattable(unum, NULL, /* will be ufmt_open()'ed for us */
2344 buffer, -1, NULL, &status);
2345 if(assertSuccess("unum_parseToUFormattable(weight of the moon)", &status)) {
2346 log_verbose("new formattable allocated at %p\n", (void*)ufmt);
2347 assertTrue("ufmt_isNumeric() TRUE", ufmt_isNumeric(ufmt));
2348 unum_formatUFormattable(unum, ufmt, out2k, 2048, NULL, &status);
2349 if(assertSuccess("unum_formatUFormattable(3.14159)", &status)) {
2350 assertEquals("unum_formatUFormattable r/t", austrdup(buffer), austrdup(out2k));
2351 }
2352
2353 log_verbose("double: %g\n", ufmt_getDouble(ufmt, &status));
2354 assertSuccess("ufmt_getDouble()", &status);
2355
2356 log_verbose("long: %ld\n", ufmt_getLong(ufmt, &status));
2357 assertTrue("failure on ufmt_getLong() for huge number:", U_FAILURE(status));
2358 // status is now a failure due to ufmt_getLong() above.
2359 // the intltest does extensive r/t testing of Formattable vs. UFormattable.
2360 }
2361 }
2362
2363 unum_close(unum);
2364 ufmt_close(ufmt); // was implicitly opened for us by the first unum_parseToUFormattable()
2365 }
2366 }
2367
2368 typedef struct {
2369 const char* locale;
2370 const char* numsys;
2371 int32_t radix;
2372 UBool isAlgorithmic;
2373 const UChar* description;
2374 } NumSysTestItem;
2375
2376
2377 static const UChar latnDesc[] = {0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039,0}; // 0123456789
2378 static const UChar romanDesc[] = {0x25,0x72,0x6F,0x6D,0x61,0x6E,0x2D,0x75,0x70,0x70,0x65,0x72,0}; // %roman-upper
2379 static const UChar arabDesc[] = {0x0660,0x0661,0x0662,0x0663,0x0664,0x0665,0x0666,0x0667,0x0668,0x0669,0}; //
2380 static const UChar arabextDesc[] = {0x06F0,0x06F1,0x06F2,0x06F3,0x06F4,0x06F5,0x06F6,0x06F7,0x06F8,0x06F9,0}; //
2381 static const UChar hanidecDesc[] = {0x3007,0x4E00,0x4E8C,0x4E09,0x56DB,0x4E94,0x516D,0x4E03,0x516B,0x4E5D,0}; //
2382 static const UChar hantDesc[] = {0x7A,0x68,0x5F,0x48,0x61,0x6E,0x74,0x2F,0x53,0x70,0x65,0x6C,0x6C,0x6F,0x75,0x74,
2383 0x52,0x75,0x6C,0x65,0x73,0x2F,0x25,0x73,0x70,0x65,0x6C,0x6C,0x6F,0x75,0x74,0x2D,
2384 0x63,0x61,0x72,0x64,0x69,0x6E,0x61,0x6C,0}; // zh_Hant/SpelloutRules/%spellout-cardinal
2385
2386 static const NumSysTestItem numSysTestItems[] = {
2387 //locale numsys radix isAlgo description
2388 { "en", "latn", 10, FALSE, latnDesc },
2389 { "en@numbers=roman", "roman", 10, TRUE, romanDesc },
2390 { "en@numbers=finance", "latn", 10, FALSE, latnDesc },
2391 { "ar-EG", "arab", 10, FALSE, arabDesc },
2392 { "fa", "arabext", 10, FALSE, arabextDesc },
2393 { "zh_Hans@numbers=hanidec", "hanidec", 10, FALSE, hanidecDesc },
2394 { "zh_Hant@numbers=traditional", "hant", 10, TRUE, hantDesc },
2395 { NULL, NULL, 0, FALSE, NULL },
2396 };
2397 enum { kNumSysDescripBufMax = 64 };
2398
TestUNumberingSystem(void)2399 static void TestUNumberingSystem(void) {
2400 const NumSysTestItem * itemPtr;
2401 UNumberingSystem * unumsys;
2402 UEnumeration * uenum;
2403 const char * numsys;
2404 UErrorCode status;
2405
2406 for (itemPtr = numSysTestItems; itemPtr->locale != NULL; itemPtr++) {
2407 status = U_ZERO_ERROR;
2408 unumsys = unumsys_open(itemPtr->locale, &status);
2409 if ( U_SUCCESS(status) ) {
2410 UChar ubuf[kNumSysDescripBufMax];
2411 int32_t ulen, radix = unumsys_getRadix(unumsys);
2412 UBool isAlgorithmic = unumsys_isAlgorithmic(unumsys);
2413 numsys = unumsys_getName(unumsys);
2414 if ( uprv_strcmp(numsys, itemPtr->numsys) != 0 || radix != itemPtr->radix || !isAlgorithmic != !itemPtr->isAlgorithmic ) {
2415 log_data_err("unumsys name/radix/isAlgorithmic for locale %s, expected %s/%d/%d, got %s/%d/%d\n",
2416 itemPtr->locale, itemPtr->numsys, itemPtr->radix, itemPtr->isAlgorithmic, numsys, radix, isAlgorithmic);
2417 }
2418 ulen = unumsys_getDescription(unumsys, ubuf, kNumSysDescripBufMax, &status);
2419 (void)ulen; // Suppress variable not used warning.
2420 if ( U_FAILURE(status) || u_strcmp(ubuf, itemPtr->description) != 0 ) {
2421 log_data_err("unumsys description for locale %s, description unexpected and/or status %\n", myErrorName(status));
2422 }
2423 unumsys_close(unumsys);
2424 } else {
2425 log_data_err("unumsys_open for locale %s fails with status %s\n", itemPtr->locale, myErrorName(status));
2426 }
2427 }
2428
2429 status = U_ZERO_ERROR;
2430 uenum = unumsys_openAvailableNames(&status);
2431 if ( U_SUCCESS(status) ) {
2432 int32_t numsysCount = 0;
2433 // sanity check for a couple of number systems that must be in the enumeration
2434 UBool foundLatn = FALSE;
2435 UBool foundArab = FALSE;
2436 while ( (numsys = uenum_next(uenum, NULL, &status)) != NULL && U_SUCCESS(status) ) {
2437 status = U_ZERO_ERROR;
2438 unumsys = unumsys_openByName(numsys, &status);
2439 if ( U_SUCCESS(status) ) {
2440 numsysCount++;
2441 if ( uprv_strcmp(numsys, "latn") ) foundLatn = TRUE;
2442 if ( uprv_strcmp(numsys, "arab") ) foundArab = TRUE;
2443 unumsys_close(unumsys);
2444 } else {
2445 log_err("unumsys_openAvailableNames includes %s but unumsys_openByName on it fails with status %s\n",
2446 numsys, myErrorName(status));
2447 }
2448 }
2449 uenum_close(uenum);
2450 if ( numsysCount < 40 || !foundLatn || !foundArab ) {
2451 log_err("unumsys_openAvailableNames results incomplete: numsysCount %d, foundLatn %d, foundArab %d\n",
2452 numsysCount, foundLatn, foundArab);
2453 }
2454 } else {
2455 log_data_err("unumsys_openAvailableNames fails with status %s\n", myErrorName(status));
2456 }
2457 }
2458
2459 /* plain-C version of test in numfmtst.cpp */
2460 enum { kUBufMax = 64 };
TestCurrencyIsoPluralFormat(void)2461 static void TestCurrencyIsoPluralFormat(void) {
2462 static const char* DATA[][8] = {
2463 // the data are:
2464 // locale,
2465 // currency amount to be formatted,
2466 // currency ISO code to be formatted,
2467 // format result using CURRENCYSTYLE,
2468 // format result using CURRENCY_STANDARD,
2469 // format result using CURRENCY_ACCOUNTING,
2470 // format result using ISOCURRENCYSTYLE,
2471 // format result using PLURALCURRENCYSTYLE,
2472
2473 // locale amount ISOcode CURRENCYSTYLE CURRENCY_STANDARD CURRENCY_ACCOUNTING ISOCURRENCYSTYLE PLURALCURRENCYSTYLE
2474 {"en_US", "1", "USD", "$1.00", "$1.00", "$1.00", "USD\\u00A01.00", "1.00 US dollars"},
2475 {"en_US", "1234.56", "USD", "$1,234.56", "$1,234.56", "$1,234.56", "USD\\u00A01,234.56", "1,234.56 US dollars"},
2476 {"en_US@cf=account", "1234.56", "USD", "$1,234.56", "$1,234.56", "$1,234.56", "USD\\u00A01,234.56", "1,234.56 US dollars"},
2477 {"en_US", "-1234.56", "USD", "-$1,234.56", "-$1,234.56", "($1,234.56)", "-USD\\u00A01,234.56", "-1,234.56 US dollars"},
2478 {"en_US@cf=account", "-1234.56", "USD", "($1,234.56)", "-$1,234.56", "($1,234.56)", "-USD\\u00A01,234.56", "-1,234.56 US dollars"},
2479 {"en_US@cf=standard", "-1234.56", "USD", "-$1,234.56", "-$1,234.56", "($1,234.56)", "-USD\\u00A01,234.56", "-1,234.56 US dollars"},
2480 {"zh_CN", "1", "USD", "US$1.00", "US$1.00", "US$1.00", "USD\\u00A01.00", "1.00\\u00A0\\u7F8E\\u5143"},
2481 {"zh_CN", "-1", "USD", "-US$1.00", "-US$1.00", "(US$1.00)", "-USD\\u00A01.00", "-1.00\\u00A0\\u7F8E\\u5143"},
2482 {"zh_CN@cf=account", "-1", "USD", "(US$1.00)", "-US$1.00", "(US$1.00)", "-USD\\u00A01.00", "-1.00\\u00A0\\u7F8E\\u5143"},
2483 {"zh_CN@cf=standard", "-1", "USD", "-US$1.00", "-US$1.00", "(US$1.00)", "-USD\\u00A01.00", "-1.00\\u00A0\\u7F8E\\u5143"},
2484 {"zh_CN", "1234.56", "USD", "US$1,234.56", "US$1,234.56", "US$1,234.56", "USD\\u00A01,234.56", "1,234.56\\u00A0\\u7F8E\\u5143"},
2485 // {"zh_CN", "1", "CHY", "CHY1.00", "CHY1.00", "CHY1.00", "CHY1.00", "1.00 CHY"}, // wrong ISO code
2486 // {"zh_CN", "1234.56", "CHY", "CHY1,234.56", "CHY1,234.56", "CHY1,234.56", "CHY1,234.56", "1,234.56 CHY"}, // wrong ISO code
2487 {"zh_CN", "1", "CNY", "\\uFFE51.00", "\\uFFE51.00", "\\uFFE51.00", "CNY\\u00A01.00", "1.00\\u00A0\\u4EBA\\u6C11\\u5E01"},
2488 {"zh_CN", "1234.56", "CNY", "\\uFFE51,234.56", "\\uFFE51,234.56", "\\uFFE51,234.56", "CNY\\u00A01,234.56", "1,234.56\\u00A0\\u4EBA\\u6C11\\u5E01"},
2489 {"ru_RU", "1", "RUB", "1,00\\u00A0\\u20BD", "1,00\\u00A0\\u20BD", "1,00\\u00A0\\u20BD", "1,00\\u00A0RUB", "1,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E "
2490 "\\u0440\\u0443\\u0431\\u043B\\u044F"},
2491 {"ru_RU", "2", "RUB", "2,00\\u00A0\\u20BD", "2,00\\u00A0\\u20BD", "2,00\\u00A0\\u20BD", "2,00\\u00A0RUB", "2,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E "
2492 "\\u0440\\u0443\\u0431\\u043B\\u044F"},
2493 {"ru_RU", "5", "RUB", "5,00\\u00A0\\u20BD", "5,00\\u00A0\\u20BD", "5,00\\u00A0\\u20BD", "5,00\\u00A0RUB", "5,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E "
2494 "\\u0440\\u0443\\u0431\\u043B\\u044F"},
2495 // test locale without currency information
2496 {"root", "-1.23", "USD", "-US$\\u00A01.23", "-US$\\u00A01.23", "-US$\\u00A01.23", "-USD\\u00A01.23", "-1.23 USD"},
2497 {"root@cf=account", "-1.23", "USD", "-US$\\u00A01.23", "-US$\\u00A01.23", "-US$\\u00A01.23", "-USD\\u00A01.23", "-1.23 USD"},
2498 // test choice format
2499 {"es_AR", "1", "INR", "INR\\u00A01,00", "INR\\u00A01,00", "INR\\u00A01,00", "INR\\u00A01,00", "1,00 rupia india"},
2500 };
2501 static const UNumberFormatStyle currencyStyles[] = {
2502 UNUM_CURRENCY,
2503 UNUM_CURRENCY_STANDARD,
2504 UNUM_CURRENCY_ACCOUNTING,
2505 UNUM_CURRENCY_ISO,
2506 UNUM_CURRENCY_PLURAL
2507 };
2508
2509 int32_t i, sIndex;
2510
2511 for (i=0; i<UPRV_LENGTHOF(DATA); ++i) {
2512 const char* localeString = DATA[i][0];
2513 double numberToBeFormat = atof(DATA[i][1]);
2514 const char* currencyISOCode = DATA[i][2];
2515 for (sIndex = 0; sIndex < UPRV_LENGTHOF(currencyStyles); ++sIndex) {
2516 UNumberFormatStyle style = currencyStyles[sIndex];
2517 UErrorCode status = U_ZERO_ERROR;
2518 UChar currencyCode[4];
2519 UChar ubufResult[kUBufMax];
2520 UChar ubufExpected[kUBufMax];
2521 int32_t ulenRes;
2522
2523 UNumberFormat* unumFmt = unum_open(style, NULL, 0, localeString, NULL, &status);
2524 if (U_FAILURE(status)) {
2525 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", localeString, (int)style, myErrorName(status));
2526 continue;
2527 }
2528 u_charsToUChars(currencyISOCode, currencyCode, 4);
2529 unum_setTextAttribute(unumFmt, UNUM_CURRENCY_CODE, currencyCode, 3, &status);
2530 if (U_FAILURE(status)) {
2531 log_err("FAIL: unum_setTextAttribute, locale %s, UNUM_CURRENCY_CODE %s\n", localeString, currencyISOCode);
2532 }
2533 ulenRes = unum_formatDouble(unumFmt, numberToBeFormat, ubufResult, kUBufMax, NULL, &status);
2534 if (U_FAILURE(status)) {
2535 log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s - %s\n", localeString, currencyISOCode, myErrorName(status));
2536 } else {
2537 int32_t ulenExp = u_unescape(DATA[i][3 + sIndex], ubufExpected, kUBufMax);
2538 if (ulenRes != ulenExp || u_strncmp(ubufResult, ubufExpected, ulenExp) != 0) {
2539 log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s, expected %s, got something else\n",
2540 localeString, currencyISOCode, DATA[i][3 + sIndex]);
2541 }
2542 }
2543 unum_close(unumFmt);
2544 }
2545 }
2546 }
2547
2548 typedef struct {
2549 const char * locale;
2550 UNumberFormatStyle style;
2551 UDisplayContext context;
2552 const char * expectedResult;
2553 } TestContextItem;
2554
2555 /* currently no locales have contextTransforms data for "symbol" type */
2556 static const TestContextItem tcItems[] = { /* results for 123.45 */
2557 { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" },
2558 { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "Ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" },
2559 { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" },
2560 { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" },
2561 { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "one hundred twenty-three point four five" },
2562 { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "One hundred twenty-three point four five" },
2563 { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, "One hundred twenty-three point four five" },
2564 { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, "One hundred twenty-three point four five" },
2565 { NULL, (UNumberFormatStyle)0, (UDisplayContext)0, NULL }
2566 };
2567
TestContext(void)2568 static void TestContext(void) {
2569 UErrorCode status = U_ZERO_ERROR;
2570 const TestContextItem* itemPtr;
2571
2572 UNumberFormat *unum = unum_open(UNUM_SPELLOUT, NULL, 0, "en", NULL, &status);
2573 if ( U_SUCCESS(status) ) {
2574 UDisplayContext context = unum_getContext(unum, UDISPCTX_TYPE_CAPITALIZATION, &status);
2575 if ( U_FAILURE(status) || context != UDISPCTX_CAPITALIZATION_NONE) {
2576 log_err("FAIL: Initial unum_getContext is not UDISPCTX_CAPITALIZATION_NONE\n");
2577 status = U_ZERO_ERROR;
2578 }
2579 unum_setContext(unum, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, &status);
2580 context = unum_getContext(unum, UDISPCTX_TYPE_CAPITALIZATION, &status);
2581 if ( U_FAILURE(status) || context != UDISPCTX_CAPITALIZATION_FOR_STANDALONE) {
2582 log_err("FAIL: unum_getContext does not return the value set, UDISPCTX_CAPITALIZATION_FOR_STANDALONE\n");
2583 }
2584 unum_close(unum);
2585 } else {
2586 log_data_err("unum_open UNUM_SPELLOUT for en fails with status %s\n", myErrorName(status));
2587 }
2588 #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_BREAK_ITERATION
2589 for (itemPtr = tcItems; itemPtr->locale != NULL; itemPtr++) {
2590 UChar ubufResult[kUBufMax];
2591 int32_t ulenRes;
2592
2593 status = U_ZERO_ERROR;
2594 unum = unum_open(itemPtr->style, NULL, 0, itemPtr->locale, NULL, &status);
2595 if (U_FAILURE(status)) {
2596 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n",
2597 itemPtr->locale, (int)itemPtr->style, myErrorName(status));
2598 continue;
2599 }
2600 unum_setContext(unum, itemPtr->context, &status);
2601 ulenRes = unum_formatDouble(unum, 123.45, ubufResult, kUBufMax, NULL, &status);
2602 if (U_FAILURE(status)) {
2603 log_err("FAIL: unum_formatDouble, locale %s, style %d, context %d - %s\n",
2604 itemPtr->locale, (int)itemPtr->style, (int)itemPtr->context, myErrorName(status));
2605 } else {
2606 UChar ubufExpected[kUBufMax];
2607 int32_t ulenExp = u_unescape(itemPtr->expectedResult, ubufExpected, kUBufMax);
2608 if (ulenRes != ulenExp || u_strncmp(ubufResult, ubufExpected, ulenExp) != 0) {
2609 char bbuf[kUBufMax*2];
2610 u_austrncpy(bbuf, ubufResult, sizeof(bbuf));
2611 log_err("FAIL: unum_formatDouble, locale %s, style %d, context %d, expected %d:\"%s\", got %d:\"%s\"\n",
2612 itemPtr->locale, (int)itemPtr->style, (int)itemPtr->context, ulenExp,
2613 itemPtr->expectedResult, ulenRes, bbuf);
2614 }
2615 }
2616 unum_close(unum);
2617 }
2618 #endif /* #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_BREAK_ITERATION */
2619 }
2620
TestCurrencyUsage(void)2621 static void TestCurrencyUsage(void) {
2622 static const char* DATA[][2] = {
2623 /* the data are:
2624 * currency ISO code to be formatted,
2625 * format result using CURRENCYSTYLE with CASH purpose,-
2626 * Note that as of CLDR 26:-
2627 * - TWD switches from 0 decimals to 2; PKR still has 0, so change test to that
2628 * - CAD rounds to .05
2629 */
2630
2631 {"PKR", "PKR\\u00A0124"},
2632 {"CAD", "CA$123.55"},
2633 {"USD", "$123.57"}
2634 };
2635
2636 // 1st time for getter/setter, 2nd for factory method
2637 int32_t i;
2638 for(i=0; i<2; i++){
2639 const char* localeString = "en_US";
2640 double numberToBeFormat = 123.567;
2641 UNumberFormat* unumFmt;
2642 UNumberFormatStyle style = UNUM_CURRENCY;
2643 UErrorCode status = U_ZERO_ERROR;
2644 int32_t j;
2645
2646 if(i == 1){ // change for factory method
2647 style = UNUM_CASH_CURRENCY;
2648 }
2649
2650 unumFmt = unum_open(style, NULL, 0, localeString, NULL, &status);
2651 if (U_FAILURE(status)) {
2652 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n",
2653 localeString, (int)style, myErrorName(status));
2654 continue;
2655 }
2656
2657 if(i == 0){ // this is for the getter/setter
2658 if(unum_getAttribute(unumFmt, UNUM_CURRENCY_USAGE) != UCURR_USAGE_STANDARD) {
2659 log_err("FAIL: currency usage attribute is not UNUM_CURRENCY_STANDARD\n");
2660 }
2661
2662 unum_setAttribute(unumFmt, UNUM_CURRENCY_USAGE, UCURR_USAGE_CASH);
2663 }
2664
2665 if(unum_getAttribute(unumFmt, UNUM_CURRENCY_USAGE) != UCURR_USAGE_CASH) {
2666 log_err("FAIL: currency usage attribute is not UNUM_CURRENCY_CASH\n");
2667 }
2668
2669 for (j=0; j<UPRV_LENGTHOF(DATA); ++j) {
2670 UChar expect[64];
2671 int32_t expectLen;
2672 UChar currencyCode[4];
2673 UChar result[64];
2674 int32_t resultLen;
2675 UFieldPosition pos = {0};
2676
2677 u_charsToUChars(DATA[j][0], currencyCode, 3);
2678 expectLen = u_unescape(DATA[j][1], expect, UPRV_LENGTHOF(expect));
2679
2680 unum_setTextAttribute(unumFmt, UNUM_CURRENCY_CODE, currencyCode, 3, &status);
2681 assertSuccess("num_setTextAttribute()", &status);
2682
2683 resultLen = unum_formatDouble(unumFmt, numberToBeFormat, result, UPRV_LENGTHOF(result),
2684 &pos, &status);
2685 assertSuccess("num_formatDouble()", &status);
2686
2687 if(resultLen != expectLen || u_strcmp(result, expect) != 0) {
2688 log_err("Fail: Error in Number Format Currency Purpose using unum_setAttribute() expected: %s, got %s\n",
2689 aescstrdup(expect, expectLen), aescstrdup(result, resultLen));
2690 }
2691
2692 }
2693
2694 unum_close(unumFmt);
2695 }
2696 }
2697
2698 static UChar currFmtNegSameAsPos[] = /* "\u00A4#,##0.00;\u00A4#,##0.00" */
2699 {0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0x3B,0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0};
2700
2701 // NOTE: As of ICU 62, identical positive and negative subpatterns means no minus sign!
2702 // See CLDR ticket https://unicode.org/cldr/trac/ticket/10703
2703 //static UChar currFmtToPatExpected[] = /* "\u00A4#,##0.00" */
2704 // {0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0};
2705 static const UChar* currFmtToPatExpected = currFmtNegSameAsPos;
2706
2707 static UChar currFmtResultExpected[] = /* "$100.00" */
2708 {0x24,0x31,0x30,0x30,0x2E,0x30,0x30,0};
2709
2710 static UChar emptyString[] = {0};
2711
2712 enum { kUBufSize = 64, kBBufSize = 128 };
2713
TestCurrFmtNegSameAsPositive(void)2714 static void TestCurrFmtNegSameAsPositive(void) {
2715 UErrorCode status = U_ZERO_ERROR;
2716 UNumberFormat* unumfmt = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status);
2717 if ( U_SUCCESS(status) ) {
2718 unum_applyPattern(unumfmt, FALSE, currFmtNegSameAsPos, -1, NULL, &status);
2719 if (U_SUCCESS(status)) {
2720 UChar ubuf[kUBufSize];
2721 int32_t ulen = unum_toPattern(unumfmt, FALSE, ubuf, kUBufSize, &status);
2722 if (U_FAILURE(status)) {
2723 log_err("unum_toPattern fails with status %s\n", myErrorName(status));
2724 } else if (u_strcmp(ubuf, currFmtToPatExpected) != 0) {
2725 log_err("unum_toPattern result wrong, expected %s, got %s\n", aescstrdup(currFmtToPatExpected,-1), aescstrdup(ubuf,ulen));
2726 }
2727 unum_setSymbol(unumfmt, UNUM_MINUS_SIGN_SYMBOL, emptyString, 0, &status);
2728 if (U_SUCCESS(status)) {
2729 ulen = unum_formatDouble(unumfmt, -100.0, ubuf, kUBufSize, NULL, &status);
2730 if (U_FAILURE(status)) {
2731 log_err("unum_formatDouble fails with status %s\n", myErrorName(status));
2732 } else if (u_strcmp(ubuf, currFmtResultExpected) != 0) {
2733 log_err("unum_formatDouble result wrong, expected %s, got %s\n", aescstrdup(currFmtResultExpected,-1), aescstrdup(ubuf,ulen));
2734 }
2735 } else {
2736 log_err("unum_setSymbol fails with status %s\n", myErrorName(status));
2737 }
2738 } else {
2739 log_err("unum_applyPattern fails with status %s\n", myErrorName(status));
2740 }
2741 unum_close(unumfmt);
2742 } else {
2743 log_data_err("unum_open UNUM_CURRENCY for en_US fails with status %s\n", myErrorName(status));
2744 }
2745 }
2746
2747
2748 typedef struct {
2749 double value;
2750 const char *expected;
2751 } ValueAndExpectedString;
2752
2753 static const ValueAndExpectedString enShort[] = {
2754 {0.0, "0"},
2755 {0.17, "0.17"},
2756 {1.0, "1"},
2757 {1234.0, "1.2K"},
2758 {12345.0, "12K"},
2759 {123456.0, "123K"},
2760 {1234567.0, "1.2M"},
2761 {12345678.0, "12M"},
2762 {123456789.0, "123M"},
2763 {1.23456789E9, "1.2B"},
2764 {1.23456789E10, "12B"},
2765 {1.23456789E11, "123B"},
2766 {1.23456789E12, "1.2T"},
2767 {1.23456789E13, "12T"},
2768 {1.23456789E14, "123T"},
2769 {1.23456789E15, "1235T"},
2770 {0.0, NULL}
2771 };
2772
2773 static const ValueAndExpectedString enShortMax2[] = {
2774 {0.0, "0"},
2775 {0.17, "0.17"},
2776 {1.0, "1"},
2777 {1234.0, "1.2K"},
2778 {12345.0, "12K"},
2779 {123456.0, "120K"},
2780 {1234567.0, "1.2M"},
2781 {12345678.0, "12M"},
2782 {123456789.0, "120M"},
2783 {1.23456789E9, "1.2B"},
2784 {1.23456789E10, "12B"},
2785 {1.23456789E11, "120B"},
2786 {1.23456789E12, "1.2T"},
2787 {1.23456789E13, "12T"},
2788 {1.23456789E14, "120T"},
2789 {1.23456789E15, "1200T"},
2790 {0.0, NULL}
2791 };
2792
2793 static const ValueAndExpectedString enShortMax5[] = {
2794 {0.0, "0"},
2795 {0.17, "0.17"},
2796 {1.0, "1"},
2797 {1234.0, "1.234K"},
2798 {12345.0, "12.345K"},
2799 {123456.0, "123.46K"},
2800 {1234567.0, "1.2346M"},
2801 {12345678.0, "12.346M"},
2802 {123456789.0, "123.46M"},
2803 {1.23456789E9, "1.2346B"},
2804 {1.23456789E10, "12.346B"},
2805 {1.23456789E11, "123.46B"},
2806 {1.23456789E12, "1.2346T"},
2807 {1.23456789E13, "12.346T"},
2808 {1.23456789E14, "123.46T"},
2809 {1.23456789E15, "1234.6T"},
2810 {0.0, NULL}
2811 };
2812
2813 static const ValueAndExpectedString enShortMin3[] = {
2814 {0.0, "0.00"},
2815 {0.17, "0.170"},
2816 {1.0, "1.00"},
2817 {1234.0, "1.23K"},
2818 {12345.0, "12.3K"},
2819 {123456.0, "123K"},
2820 {1234567.0, "1.23M"},
2821 {12345678.0, "12.3M"},
2822 {123456789.0, "123M"},
2823 {1.23456789E9, "1.23B"},
2824 {1.23456789E10, "12.3B"},
2825 {1.23456789E11, "123B"},
2826 {1.23456789E12, "1.23T"},
2827 {1.23456789E13, "12.3T"},
2828 {1.23456789E14, "123T"},
2829 {1.23456789E15, "1230T"},
2830 {0.0, NULL}
2831 };
2832
2833 static const ValueAndExpectedString jaShortMax2[] = {
2834 {1234.0, "1200"},
2835 {12345.0, "1.2\\u4E07"},
2836 {123456.0, "12\\u4E07"},
2837 {1234567.0, "120\\u4E07"},
2838 {12345678.0, "1200\\u4E07"},
2839 {123456789.0, "1.2\\u5104"},
2840 {1.23456789E9, "12\\u5104"},
2841 {1.23456789E10, "120\\u5104"},
2842 {1.23456789E11, "1200\\u5104"},
2843 {1.23456789E12, "1.2\\u5146"},
2844 {1.23456789E13, "12\\u5146"},
2845 {1.23456789E14, "120\\u5146"},
2846 {0.0, NULL}
2847 };
2848
2849 static const ValueAndExpectedString srLongMax2[] = {
2850 {1234.0, "1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few
2851 {12345.0, "12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other
2852 {21789.0, "22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few
2853 {123456.0, "120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other
2854 {999999.0, "1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"}, // 10^6 one
2855 {1234567.0, "1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 few
2856 {12345678.0, "12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 other
2857 {123456789.0, "120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 other
2858 {1.23456789E9, "1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, // 10^9 few
2859 {1.23456789E10, "12 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, // 10^9 other
2860 {2.08901234E10, "21 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0430"}, // 10^9 one
2861 {2.18901234E10, "22 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, // 10^9 few
2862 {1.23456789E11, "120 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, // 10^9 other
2863 {-1234.0, "-1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"},
2864 {-12345.0, "-12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"},
2865 {-21789.0, "-22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"},
2866 {-123456.0, "-120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"},
2867 {-999999.0, "-1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"},
2868 {-1234567.0, "-1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"},
2869 {-12345678.0, "-12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"},
2870 {-123456789.0, "-120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"},
2871 {-1.23456789E9, "-1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"},
2872 {-1.23456789E10, "-12 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"},
2873 {-2.08901234E10, "-21 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0430"},
2874 {-2.18901234E10, "-22 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"},
2875 {-1.23456789E11, "-120 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"},
2876 {0.0, NULL}
2877 };
2878
2879 typedef struct {
2880 const char * locale;
2881 UNumberFormatStyle style;
2882 int32_t attribute; // UNumberFormatAttribute, or -1 for none
2883 int32_t attrValue; //
2884 const ValueAndExpectedString * veItems;
2885 } LocStyleAttributeTest;
2886
2887 static const LocStyleAttributeTest lsaTests[] = {
2888 { "en", UNUM_DECIMAL_COMPACT_SHORT, -1, 0, enShort },
2889 { "en", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MAX_SIGNIFICANT_DIGITS, 2, enShortMax2 },
2890 { "en", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MAX_SIGNIFICANT_DIGITS, 5, enShortMax5 },
2891 { "en", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MIN_SIGNIFICANT_DIGITS, 3, enShortMin3 },
2892 { "ja", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MAX_SIGNIFICANT_DIGITS, 2, jaShortMax2 },
2893 { "sr", UNUM_DECIMAL_COMPACT_LONG, UNUM_MAX_SIGNIFICANT_DIGITS, 2, srLongMax2 },
2894 { NULL, (UNumberFormatStyle)0, -1, 0, NULL }
2895 };
2896
TestVariousStylesAndAttributes(void)2897 static void TestVariousStylesAndAttributes(void) {
2898 const LocStyleAttributeTest * lsaTestPtr;
2899 for (lsaTestPtr = lsaTests; lsaTestPtr->locale != NULL; lsaTestPtr++) {
2900 UErrorCode status = U_ZERO_ERROR;
2901 UNumberFormat * unum = unum_open(lsaTestPtr->style, NULL, 0, lsaTestPtr->locale, NULL, &status);
2902 if ( U_FAILURE(status) ) {
2903 log_data_err("FAIL: unum_open style %d, locale %s: error %s\n", (int)lsaTestPtr->style, lsaTestPtr->locale, u_errorName(status));
2904 } else {
2905 const ValueAndExpectedString * veItemPtr;
2906 if (lsaTestPtr->attribute >= 0) {
2907 unum_setAttribute(unum, (UNumberFormatAttribute)lsaTestPtr->attribute, lsaTestPtr->attrValue);
2908 }
2909 // ICU 62: should call minSignificantDigits in tandem with maxSignificantDigits.
2910 if (lsaTestPtr->attribute == UNUM_MIN_SIGNIFICANT_DIGITS) {
2911 unum_setAttribute(unum, UNUM_MAX_SIGNIFICANT_DIGITS, lsaTestPtr->attrValue);
2912 }
2913 for (veItemPtr = lsaTestPtr->veItems; veItemPtr->expected != NULL; veItemPtr++) {
2914 UChar uexp[kUBufSize];
2915 UChar uget[kUBufSize];
2916 int32_t uexplen, ugetlen;
2917
2918 status = U_ZERO_ERROR;
2919 uexplen = u_unescape(veItemPtr->expected, uexp, kUBufSize);
2920 ugetlen = unum_formatDouble(unum, veItemPtr->value, uget, kUBufSize, NULL, &status);
2921 if ( U_FAILURE(status) ) {
2922 log_err("FAIL: unum_formatDouble style %d, locale %s, attr %d, value %.2f: error %s\n",
2923 (int)lsaTestPtr->style, lsaTestPtr->locale, lsaTestPtr->attribute, veItemPtr->value, u_errorName(status));
2924 } else if (ugetlen != uexplen || u_strncmp(uget, uexp, uexplen) != 0) {
2925 char bexp[kBBufSize];
2926 char bget[kBBufSize];
2927 u_strToUTF8(bexp, kBBufSize, NULL, uexp, uexplen, &status);
2928 u_strToUTF8(bget, kBBufSize, NULL, uget, ugetlen, &status);
2929 log_err("FAIL: unum_formatDouble style %d, locale %s, attr %d, value %.2f: expect \"%s\", get \"%s\"\n",
2930 (int)lsaTestPtr->style, lsaTestPtr->locale, lsaTestPtr->attribute, veItemPtr->value, bexp, bget);
2931 }
2932 }
2933 unum_close(unum);
2934 }
2935 }
2936 }
2937
2938 static const UChar currpat[] = { 0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0};
2939 static const UChar parsetxt[] = { 0x78,0x30,0x79,0x24,0 }; /* x0y$ */
2940
TestParseCurrPatternWithDecStyle()2941 static void TestParseCurrPatternWithDecStyle() {
2942 UErrorCode status = U_ZERO_ERROR;
2943 UNumberFormat *unumfmt = unum_open(UNUM_DECIMAL, NULL, 0, "en_US", NULL, &status);
2944 if (U_FAILURE(status)) {
2945 log_data_err("unum_open DECIMAL failed for en_US: %s (Are you missing data?)\n", u_errorName(status));
2946 } else {
2947 unum_applyPattern(unumfmt, FALSE, currpat, -1, NULL, &status);
2948 if (U_FAILURE(status)) {
2949 log_err_status(status, "unum_applyPattern failed: %s\n", u_errorName(status));
2950 } else {
2951 int32_t pos = 0;
2952 double value = unum_parseDouble(unumfmt, parsetxt, -1, &pos, &status);
2953 if (U_SUCCESS(status)) {
2954 log_err_status(status, "unum_parseDouble expected to fail but got status %s, value %f\n", u_errorName(status), value);
2955 }
2956 }
2957 unum_close(unumfmt);
2958 }
2959 }
2960
2961 /*
2962 * Ticket #12684
2963 * Test unum_formatDoubleForFields (and UFieldPositionIterator)
2964 */
2965
2966 typedef struct {
2967 int32_t field;
2968 int32_t beginPos;
2969 int32_t endPos;
2970 } FieldsData;
2971
2972 typedef struct {
2973 const char * locale;
2974 UNumberFormatStyle style;
2975 double value;
2976 const FieldsData * expectedFields;
2977 } FormatForFieldsItem;
2978
2979 static const UChar patNoFields[] = { 0x0027, 0x0078, 0x0027, 0 }; /* "'x'", for UNUM_PATTERN_DECIMAL */
2980
2981
2982 /* "en_US", UNUM_CURRENCY, 123456.0 : "¤#,##0.00" => "$123,456.00" */
2983 static const FieldsData fields_en_CURR[] = {
2984 { UNUM_CURRENCY_FIELD /*7*/, 0, 1 },
2985 { UNUM_GROUPING_SEPARATOR_FIELD /*6*/, 4, 5 },
2986 { UNUM_INTEGER_FIELD /*0*/, 1, 8 },
2987 { UNUM_DECIMAL_SEPARATOR_FIELD /*2*/, 8, 9 },
2988 { UNUM_FRACTION_FIELD /*1*/, 9, 11 },
2989 { -1, -1, -1 },
2990 };
2991 /* "en_US", UNUM_PERCENT, -34 : "#,##0%" => "-34%" */
2992 static const FieldsData fields_en_PRCT[] = {
2993 { UNUM_SIGN_FIELD /*10*/, 0, 1 },
2994 { UNUM_INTEGER_FIELD /*0*/, 1, 3 },
2995 { UNUM_PERCENT_FIELD /*8*/, 3, 4 },
2996 { -1, -1, -1 },
2997 };
2998 /* "fr_FR", UNUM_CURRENCY, 123456.0 : "#,##0.00 ¤" => "123,456.00 €" */
2999 static const FieldsData fields_fr_CURR[] = {
3000 { UNUM_GROUPING_SEPARATOR_FIELD /*6*/, 3, 4 },
3001 { UNUM_INTEGER_FIELD /*0*/, 0, 7 },
3002 { UNUM_DECIMAL_SEPARATOR_FIELD /*2*/, 7, 8 },
3003 { UNUM_FRACTION_FIELD /*1*/, 8, 10 },
3004 { UNUM_CURRENCY_FIELD /*7*/, 11, 12 },
3005 { -1, -1, -1 },
3006 };
3007 /* "en_US", UNUM_PATTERN_DECIMAL, 12.0 : "'x'" => "x12" */
3008 static const FieldsData fields_en_PATN[] = {
3009 { UNUM_INTEGER_FIELD /*0*/, 1, 3 },
3010 { -1, -1, -1 },
3011 };
3012
3013 static const FormatForFieldsItem fffItems[] = {
3014 { "en_US", UNUM_CURRENCY_STANDARD, 123456.0, fields_en_CURR },
3015 { "en_US", UNUM_PERCENT, -0.34, fields_en_PRCT },
3016 { "fr_FR", UNUM_CURRENCY_STANDARD, 123456.0, fields_fr_CURR },
3017 { "en_US", UNUM_PATTERN_DECIMAL, 12.0, fields_en_PATN },
3018 { NULL, (UNumberFormatStyle)0, 0, NULL },
3019 };
3020
TestFormatForFields(void)3021 static void TestFormatForFields(void) {
3022 UErrorCode status = U_ZERO_ERROR;
3023 UFieldPositionIterator* fpositer = ufieldpositer_open(&status);
3024 if ( U_FAILURE(status) ) {
3025 log_err("ufieldpositer_open fails, status %s\n", u_errorName(status));
3026 } else {
3027 const FormatForFieldsItem * itemPtr;
3028 for (itemPtr = fffItems; itemPtr->locale != NULL; itemPtr++) {
3029 UNumberFormat* unum;
3030 status = U_ZERO_ERROR;
3031 unum = (itemPtr->style == UNUM_PATTERN_DECIMAL)?
3032 unum_open(itemPtr->style, patNoFields, -1, itemPtr->locale, NULL, &status):
3033 unum_open(itemPtr->style, NULL, 0, itemPtr->locale, NULL, &status);
3034 if ( U_FAILURE(status) ) {
3035 log_data_err("unum_open fails for locale %s, style %d: status %s (Are you missing data?)\n", itemPtr->locale, itemPtr->style, u_errorName(status));
3036 } else {
3037 UChar ubuf[kUBufSize];
3038 int32_t ulen = unum_formatDoubleForFields(unum, itemPtr->value, ubuf, kUBufSize, fpositer, &status);
3039 if ( U_FAILURE(status) ) {
3040 log_err("unum_formatDoubleForFields fails for locale %s, style %d: status %s\n", itemPtr->locale, itemPtr->style, u_errorName(status));
3041 } else {
3042 const FieldsData * fptr;
3043 int32_t field, beginPos, endPos;
3044 for (fptr = itemPtr->expectedFields; TRUE; fptr++) {
3045 field = ufieldpositer_next(fpositer, &beginPos, &endPos);
3046 if (field != fptr->field || (field >= 0 && (beginPos != fptr->beginPos || endPos != fptr->endPos))) {
3047 if (fptr->field >= 0) {
3048 log_err("unum_formatDoubleForFields for locale %s as \"%s\"; expect field %d range %d-%d, get field %d range %d-%d\n",
3049 itemPtr->locale, aescstrdup(ubuf, ulen), fptr->field, fptr->beginPos, fptr->endPos, field, beginPos, endPos);
3050 } else {
3051 log_err("unum_formatDoubleForFields for locale %s as \"%s\"; expect field < 0, get field %d range %d-%d\n",
3052 itemPtr->locale, aescstrdup(ubuf, ulen), field, beginPos, endPos);
3053 }
3054 break;
3055 }
3056 if (field < 0) {
3057 break;
3058 }
3059 }
3060 }
3061 unum_close(unum);
3062 }
3063 }
3064 ufieldpositer_close(fpositer);
3065 }
3066 }
3067
Test12052_NullPointer()3068 static void Test12052_NullPointer() {
3069 UErrorCode status = U_ZERO_ERROR;
3070 static const UChar input[] = u"199a";
3071 UChar currency[200] = {0};
3072 UNumberFormat *theFormatter = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status);
3073 if (!assertSuccessCheck("unum_open() failed", &status, TRUE)) { return; }
3074 status = U_ZERO_ERROR;
3075 unum_setAttribute(theFormatter, UNUM_LENIENT_PARSE, 1);
3076 int32_t pos = 1;
3077 unum_parseDoubleCurrency(theFormatter, input, -1, &pos, currency, &status);
3078 assertEquals("should fail gracefully", "U_PARSE_ERROR", u_errorName(status));
3079 unum_close(theFormatter);
3080 }
3081
3082 #endif /* #if !UCONFIG_NO_FORMATTING */
3083