1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 /********************************************************************************
12 * Madhu Katragadda Creation
14 * Modification History:
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 *********************************************************************************
22 /* C API TEST FOR NUMBER FORMAT */
24 #include "unicode/utypes.h"
26 #if !UCONFIG_NO_FORMATTING
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"
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
);
49 #define ASSERT_TRUE(x) assertTrue(tagAssert(__FILE__, __LINE__, #x), (x))
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 TestParseAltNum(void);
68 static void TestParseCurrPatternWithDecStyle(void);
70 #define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x)
72 void addNumForTest(TestNode
** root
)
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(TestNBSPInPattern
);
85 TESTCASE(TestInt64Parse
);
86 TESTCASE(TestParseZero
);
87 TESTCASE(TestParseCurrency
);
88 TESTCASE(TestCloneWithRBNF
);
90 TESTCASE(TestNoExponent
);
91 TESTCASE(TestUFormattable
);
92 TESTCASE(TestUNumberingSystem
);
93 TESTCASE(TestCurrencyIsoPluralFormat
);
94 TESTCASE(TestContext
);
95 TESTCASE(TestCurrencyUsage
);
96 TESTCASE(TestCurrFmtNegSameAsPositive
);
97 TESTCASE(TestVariousStylesAndAttributes
);
98 TESTCASE(TestParseCurrPatternWithDecStyle
);
99 TESTCASE(TestFormatForFields
);
100 TESTCASE(TestParseAltNum
);
101 TESTCASE(TestParseCurrPatternWithDecStyle
);
104 /* test Parse int 64 */
106 static void TestInt64Parse()
109 UErrorCode st
= U_ZERO_ERROR
;
110 UErrorCode
* status
= &st
;
112 const char* st1
= "009223372036854775808";
121 u_charsToUChars(st1
, text
, size
);
122 nf
= unum_open(UNUM_DEFAULT
, NULL
, -1, NULL
, NULL
, status
);
124 if(U_FAILURE(*status
))
126 log_data_err("Error in unum_open() %s \n", myErrorName(*status
));
130 log_verbose("About to test unum_parseInt64() with out of range number\n");
132 a
= unum_parseInt64(nf
, text
, size
, 0, status
);
133 (void)a
; /* Suppress set but not used warning. */
136 if(!U_FAILURE(*status
))
138 log_err("Error in unum_parseInt64(): %s \n", myErrorName(*status
));
142 log_verbose("unum_parseInt64() successful\n");
149 /* test Number Format API */
150 static void TestNumberFormat()
161 int32_t resultlength
;
162 int32_t resultlengthneeded
;
166 double d
= -10456.37;
167 double a
= 1234.56, a1
= 1235.0;
168 int32_t l
= 100000000;
174 UNumberFormatAttribute attr
;
175 UNumberFormatSymbol symType
= UNUM_DECIMAL_SEPARATOR_SYMBOL
;
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
;
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
);
188 /* Might as well pack it in now if we can't even get a default NumberFormat... */
189 if(U_FAILURE(status
))
191 log_data_err("Error in creating default NumberFormat using unum_open(): %s (Are you missing data?)\n", myErrorName(status
));
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
));
200 log_verbose("\nTesting unum_open(currency,NULL,status)\n");
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
) );
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
));
214 log_verbose("\nTesting unum_open(percent, NULL, status)\n");
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
));
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
));
225 log_verbose("\nTesting unum_open(spellout, NULL, status)");
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
));
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
));
239 log_verbose("unum_clone() successful\n");
242 /*Testing unum_getAvailable() and unum_countAvailable()*/
243 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
244 numlocales
=unum_countAvailable();
246 log_err("error in countAvailable");
248 log_verbose("unum_countAvialable() successful\n");
249 log_verbose("The no: of locales where number formattting is applicable is %d\n", numlocales
);
251 for(i
=0;i
<numlocales
;i
++)
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");
257 log_verbose("A locale %s for which number formatting patterns are applicable\n",unum_getAvailable(i
));
261 /*Testing unum_format() and unum_formatdouble()*/
262 u_uastrcpy(temp1
, "$100,000,000.00");
264 log_verbose("\nTesting unum_format() \n");
266 pos1
.field
= UNUM_INTEGER_FIELD
;
267 resultlengthneeded
=unum_format(cur_def
, l
, NULL
, resultlength
, &pos1
, &status
);
268 if(status
==U_BUFFER_OVERFLOW_ERROR
)
271 resultlength
=resultlengthneeded
+1;
272 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
273 /* for (i = 0; i < 100000; i++) */
275 unum_format(cur_def
, l
, result
, resultlength
, &pos1
, &status
);
279 if(U_FAILURE(status
))
281 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
) );
283 if(u_strcmp(result
, temp1
)==0)
284 log_verbose("Pass: Number formatting using unum_format() successful\n");
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");
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
);
296 log_verbose("\nTesting unum_formatDouble()\n");
297 u_uastrcpy(temp1
, "-$10,456.37");
299 pos2
.field
= UNUM_FRACTION_FIELD
;
300 resultlengthneeded
=unum_formatDouble(cur_def
, d
, NULL
, resultlength
, &pos2
, &status
);
301 if(status
==U_BUFFER_OVERFLOW_ERROR
)
304 resultlength
=resultlengthneeded
+1;
305 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
306 /* for (i = 0; i < 100000; i++) */
308 unum_formatDouble(cur_def
, d
, result
, resultlength
, &pos2
, &status
);
311 if(U_FAILURE(status
))
313 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
));
315 if(result
&& u_strcmp(result
, temp1
)==0)
316 log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
318 log_err("FAIL: Error in number formatting using unum_formatDouble() - got '%s' expected '%s'\n",
319 aescstrdup(result
, -1), aescstrdup(temp1
, -1));
321 if(pos2
.beginIndex
== 9 && pos2
.endIndex
== 11)
322 log_verbose("Pass: Complete number formatting using unum_format() successful\n");
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
);
328 /* Testing unum_parse() and unum_parseDouble() */
329 log_verbose("\nTesting unum_parseDouble()\n");
330 /* for (i = 0; i < 100000; i++)*/
332 if (result
!= NULL
) {
333 d1
=unum_parseDouble(cur_def
, result
, u_strlen(result
), &parsepos
, &status
);
335 log_err("result is NULL\n");
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
));
342 log_err("Fail: Error in parsing\n");
344 log_verbose("Pass: parsing successful\n");
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");
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
) {
360 resultlength
=resultlengthneeded
+1;
361 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
362 unum_formatDoubleCurrency(cur_def
, a
, temp
, result
, resultlength
, &pos2
, &status
);
364 if (U_FAILURE(status
)) {
365 log_err("Error in formatting using unum_formatDoubleCurrency(.....): %s\n", myErrorName(status
));
367 if (result
&& u_strcmp(result
, temp1
)==0) {
368 log_verbose("Pass: Number Formatting using unum_formatDoubleCurrency() Successful\n");
370 log_err("FAIL: Error in number formatting using unum_formatDoubleCurrency() - got '%s' expected '%s'\n",
371 aescstrdup(result
, -1), aescstrdup(temp1
, -1));
373 if (pos2
.beginIndex
== 1 && pos2
.endIndex
== 6) {
374 log_verbose("Pass: Complete number formatting using unum_format() successful\n");
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
);
380 log_verbose("\nTesting unum_parseDoubleCurrency\n");
382 if (result
== NULL
) {
383 log_err("result is NULL\n");
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
));
390 /* Note: a==1234.56, but on parse expect a1=1235.0 */
392 log_err("Fail: Error in parsing currency, got %f, expected %f\n", d1
, a1
);
394 log_verbose("Pass: parsed currency amount successfully\n");
396 if (u_strcmp(temp2
, temp
)==0) {
397 log_verbose("Pass: parsed correct currency\n");
399 log_err("Fail: parsed incorrect currency\n");
402 status
= U_ZERO_ERROR
; /* reset */
408 /* performance testing */
409 u_uastrcpy(temp1
, "$462.12345");
410 resultlength
=u_strlen(temp1
);
411 /* for (i = 0; i < 100000; i++) */
414 d1
=unum_parseDouble(cur_def
, temp1
, resultlength
, &parsepos
, &status
);
416 if(U_FAILURE(status
))
418 log_err("parseDouble('%s') failed. The error is : %s\n", aescstrdup(temp1
, resultlength
), myErrorName(status
));
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.
428 log_err("Fail: Error in parsing\n");
430 log_verbose("Pass: parsing successful\n");
434 u_uastrcpy(temp1
, "($10,456.3E1])");
436 d1
=unum_parseDouble(cur_def
, temp1
, u_strlen(temp1
), &parsepos
, &status
);
437 if(U_SUCCESS(status
))
439 log_err("Error in unum_parseDouble(..., %s, ...): %s\n", temp1
, myErrorName(status
));
443 log_verbose("\nTesting unum_format()\n");
447 resultlengthneeded
=unum_format(per_fr
, l
, NULL
, resultlength
, &pos1
, &status
);
448 if(status
==U_BUFFER_OVERFLOW_ERROR
)
451 resultlength
=resultlengthneeded
+1;
452 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
453 /* for (i = 0; i < 100000; i++)*/
455 unum_format(per_fr
, l
, result
, resultlength
, &pos1
, &status
);
458 if(U_FAILURE(status
))
460 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
));
464 log_verbose("\nTesting unum_parse()\n");
465 /* for (i = 0; i < 100000; i++) */
468 l1
=unum_parse(per_fr
, result
, u_strlen(result
), &parsepos
, &status
);
470 if(U_FAILURE(status
))
472 log_err("parse failed. The error is : %s\n", myErrorName(status
));
476 log_err("Fail: Error in parsing\n");
478 log_verbose("Pass: parsing successful\n");
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
))
488 log_err("error in unum_openPattern(): %s\n", myErrorName(status
) );;
491 log_verbose("Pass: unum_openPattern() works fine\n");
493 /*test for unum_toPattern()*/
494 log_verbose("\nTesting unum_toPattern()\n");
496 resultlengthneeded
=unum_toPattern(pattern
, FALSE
, NULL
, resultlength
, &status
);
497 if(status
==U_BUFFER_OVERFLOW_ERROR
)
500 resultlength
=resultlengthneeded
+1;
501 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
502 unum_toPattern(pattern
, FALSE
, result
, resultlength
, &status
);
504 if(U_FAILURE(status
))
506 log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status
));
510 if(u_strcmp(result
, temp1
)!=0)
511 log_err("FAIL: Error in extracting the pattern using unum_toPattern()\n");
513 log_verbose("Pass: extracted the pattern correctly using unum_toPattern()\n");
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 */
521 resultlengthneeded
=unum_toPattern(cur_def
, FALSE
, NULL
, resultlength
, &status
);
522 if(status
==U_BUFFER_OVERFLOW_ERROR
)
525 resultlength
=resultlengthneeded
+1;
526 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
527 unum_toPattern(cur_def
, FALSE
, result
, resultlength
, &status
);
529 if(U_FAILURE(status
))
531 log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status
));
535 cur_frpattern
=unum_open(UNUM_IGNORE
,result
, u_strlen(result
), "fr_FR",NULL
, &status
);
536 if(U_FAILURE(status
))
538 log_err("error in unum_openPattern(): %s\n", myErrorName(status
));
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
++) {
547 unum_getSymbol(cur_def
, symType
, temp1
, sizeof(temp1
), &status
);
548 unum_setSymbol(cur_frpattern
, symType
, temp1
, -1, &status
);
549 if(U_FAILURE(status
))
551 log_err("Error in get/set symbols: %s\n", myErrorName(status
));
555 /*format to check the result */
557 resultlengthneeded
=unum_format(cur_def
, l
, NULL
, resultlength
, &pos1
, &status
);
558 if(status
==U_BUFFER_OVERFLOW_ERROR
)
561 resultlength
=resultlengthneeded
+1;
562 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
563 unum_format(cur_def
, l
, result
, resultlength
, &pos1
, &status
);
565 if(U_FAILURE(status
))
567 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
));
570 if(U_FAILURE(status
)){
571 log_err("Fail: error in unum_setSymbols: %s\n", myErrorName(status
));
573 unum_applyPattern(cur_frpattern
, FALSE
, result
, u_strlen(result
),NULL
,NULL
);
575 for (symType
= UNUM_DECIMAL_SEPARATOR_SYMBOL
; symType
< UNUM_FORMAT_SYMBOL_COUNT
; symType
++) {
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)
581 log_err("Fail: error in getting symbols\n");
584 log_verbose("Pass: get and set symbols successful\n");
587 /*format and check with the previous result */
590 resultlengthneeded
=unum_format(cur_frpattern
, l
, NULL
, resultlength
, &pos1
, &status
);
591 if(status
==U_BUFFER_OVERFLOW_ERROR
)
594 resultlength
=resultlengthneeded
+1;
595 unum_format(cur_frpattern
, l
, temp1
, resultlength
, &pos1
, &status
);
597 if(U_FAILURE(status
))
599 log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status
));
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.
610 if(u_strcmp(result
, temp1
) != 0) {
611 log_err("Formatting failed after setting symbols. result=%s temp1=%s\n", result
, temp1
);
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
));
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
));
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
);
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");
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
));
651 /* Testing unum_getTextAttribute() and unum_setTextAttribute()*/
652 log_verbose("\nTesting getting and setting text attributes\n");
654 unum_getTextAttribute(cur_fr
, UNUM_NEGATIVE_SUFFIX
, temp
, resultlength
, &status
);
655 if(U_FAILURE(status
))
657 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status
));
659 unum_setTextAttribute(cur_def
, UNUM_NEGATIVE_SUFFIX
, temp
, u_strlen(temp
), &status
);
660 if(U_FAILURE(status
))
662 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status
));
664 unum_getTextAttribute(cur_def
, UNUM_NEGATIVE_SUFFIX
, suffix
, resultlength
, &status
);
665 if(U_FAILURE(status
))
667 log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status
));
669 if(u_strcmp(suffix
,temp
)!=0)
670 log_err("Fail:Error in setTextAttribute or getTextAttribute in setting and getting suffix\n");
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
);
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
))
682 log_err("error in setting the text attributes : %s\n", myErrorName(status
));
684 unum_getTextAttribute(def
, UNUM_POSITIVE_PREFIX
, temp
, resultlength
, &status
);
685 if(U_FAILURE(status
))
687 log_err("error in getting the text attributes : %s\n", myErrorName(status
));
690 if(u_strcmp(prefix
, temp
)!=0)
691 log_err("ERROR: get and setTextAttributes with positive prefix failed\n");
693 log_verbose("Pass: get and setTextAttributes with positive prefix works fine\n");
695 u_uastrcpy(prefix
, "+");
696 unum_setTextAttribute(def
, UNUM_NEGATIVE_PREFIX
, prefix
, u_strlen(prefix
), &status
);
697 if(U_FAILURE(status
))
699 log_err("error in setting the text attributes : %s\n", myErrorName(status
));
701 unum_getTextAttribute(def
, UNUM_NEGATIVE_PREFIX
, temp
, resultlength
, &status
);
702 if(U_FAILURE(status
))
704 log_err("error in getting the text attributes : %s\n", myErrorName(status
));
706 if(u_strcmp(prefix
, temp
)!=0)
707 log_err("ERROR: get and setTextAttributes with negative prefix failed\n");
709 log_verbose("Pass: get and setTextAttributes with negative prefix works fine\n");
711 u_uastrcpy(suffix
, "+");
712 unum_setTextAttribute(def
, UNUM_NEGATIVE_SUFFIX
, suffix
, u_strlen(suffix
) , &status
);
713 if(U_FAILURE(status
))
715 log_err("error in setting the text attributes: %s\n", myErrorName(status
));
718 unum_getTextAttribute(def
, UNUM_NEGATIVE_SUFFIX
, temp
, resultlength
, &status
);
719 if(U_FAILURE(status
))
721 log_err("error in getting the text attributes : %s\n", myErrorName(status
));
723 if(u_strcmp(suffix
, temp
)!=0)
724 log_err("ERROR: get and setTextAttributes with negative suffix failed\n");
726 log_verbose("Pass: get and settextAttributes with negative suffix works fine\n");
728 u_uastrcpy(suffix
, "++");
729 unum_setTextAttribute(def
, UNUM_POSITIVE_SUFFIX
, suffix
, u_strlen(suffix
) , &status
);
730 if(U_FAILURE(status
))
732 log_err("error in setting the text attributes: %s\n", myErrorName(status
));
735 unum_getTextAttribute(def
, UNUM_POSITIVE_SUFFIX
, temp
, resultlength
, &status
);
736 if(U_FAILURE(status
))
738 log_err("error in getting the text attributes : %s\n", myErrorName(status
));
740 if(u_strcmp(suffix
, temp
)!=0)
741 log_err("ERROR: get and setTextAttributes with negative suffix failed\n");
743 log_verbose("Pass: get and settextAttributes with negative suffix works fine\n");
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
);
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");
754 log_verbose("Pass: setting and getting attributes for UNUM_GROUPING_SIZE works fine\n");
756 attr
=UNUM_MULTIPLIER
;
757 newvalue
=unum_getAttribute(def
, attr
);
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");
763 log_verbose("Pass:setting and getting attributes for UNUM_MULTIPLIER works fine\n");
765 attr
=UNUM_SECONDARY_GROUPING_SIZE
;
766 newvalue
=unum_getAttribute(def
, attr
);
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\n");
772 log_verbose("Pass:setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE works fine\n");
774 /*testing set and get Attributes extensively */
775 log_verbose("\nTesting get and set attributes extensively\n");
776 for(attr
=UNUM_PARSE_INT_ONLY
; attr
<= UNUM_PADDING_POSITION
; attr
=(UNumberFormatAttribute
)((int32_t)attr
+ 1) )
778 newvalue
=unum_getAttribute(fr
, attr
);
779 unum_setAttribute(def
, attr
, newvalue
);
780 if(unum_getAttribute(def
,attr
)!=unum_getAttribute(fr
, attr
))
781 log_err("error in setting and getting attributes\n");
783 log_verbose("Pass: attributes set and retrieved successfully\n");
786 /*testing spellout format to make sure we can use it successfully.*/
787 log_verbose("\nTesting spellout format\n");
790 static const int32_t values
[] = { 0, -5, 105, 1005, 105050 };
791 for (i
= 0; i
< UPRV_LENGTHOF(values
); ++i
) {
794 int32_t value
= values
[i
];
795 status
= U_ZERO_ERROR
;
796 len
= unum_format(spellout_def
, value
, buffer
, UPRV_LENGTHOF(buffer
), NULL
, &status
);
797 if(U_FAILURE(status
)) {
798 log_err("Error in formatting using unum_format(spellout_fmt, ...): %s\n", myErrorName(status
));
802 /*ustrToAstr(buffer, len, logbuf, UPRV_LENGTHOF(logbuf));*/
803 log_verbose("formatted %d as '%s', length: %d\n", value
, aescstrdup(buffer
, len
), len
);
805 parseResult
= unum_parse(spellout_def
, buffer
, len
, &pp
, &status
);
806 if (U_FAILURE(status
)) {
807 log_err("Error in parsing using unum_format(spellout_fmt, ...): %s\n", myErrorName(status
));
808 } else if (parseResult
!= value
) {
809 log_err("unum_format result %d != value %d\n", parseResult
, value
);
815 log_err("Spellout format is unavailable\n");
818 { /* Test for ticket #7079 */
819 UNumberFormat
* dec_en
;
820 UChar groupingSep
[] = { 0 };
821 UChar numPercent
[] = { 0x0031, 0x0032, 0x0025, 0 }; /* "12%" */
822 double parseResult
= 0.0;
825 dec_en
= unum_open(UNUM_DECIMAL
, NULL
, 0, "en_US", NULL
, &status
);
826 unum_setAttribute(dec_en
, UNUM_LENIENT_PARSE
, 0);
827 unum_setSymbol(dec_en
, UNUM_GROUPING_SEPARATOR_SYMBOL
, groupingSep
, 0, &status
);
828 parseResult
= unum_parseDouble(dec_en
, numPercent
, -1, NULL
, &status
);
829 /* Without the fix in #7079, the above call will hang */
830 if ( U_FAILURE(status
) || parseResult
!= 12.0 ) {
831 log_err("unum_parseDouble with empty groupingSep: status %s, parseResult %f not 12.0\n",
832 myErrorName(status
), parseResult
);
834 log_verbose("unum_parseDouble with empty groupingSep: no hang, OK\n");
839 { /* Test parse & format of big decimals. Use a number with too many digits to fit in a double,
840 to verify that it is taking the pure decimal path. */
842 const char *bdpattern
= "#,##0.#########";
843 const char *numInitial
= "12345678900987654321.1234567896";
844 const char *numFormatted
= "12,345,678,900,987,654,321.12345679";
845 const char *parseExpected
= "12345678900987654321.12345679";
846 int32_t resultSize
= 0;
847 int32_t parsePos
= 0; /* Output parameter for Parse operations. */
848 #define DESTCAPACITY 100
849 UChar dest
[DESTCAPACITY
];
850 char desta
[DESTCAPACITY
];
851 UFieldPosition fieldPos
= {0};
855 status
= U_ZERO_ERROR
;
856 u_uastrcpy(dest
, bdpattern
);
857 fmt
= unum_open(UNUM_PATTERN_DECIMAL
, dest
, -1, "en", NULL
/*parseError*/, &status
);
858 if (U_FAILURE(status
)) log_err("File %s, Line %d, status = %s\n", __FILE__
, __LINE__
, u_errorName(status
));
860 resultSize
= unum_formatDecimal(fmt
, numInitial
, -1, dest
, DESTCAPACITY
, NULL
, &status
);
861 if (U_FAILURE(status
)) {
862 log_err("File %s, Line %d, status = %s\n", __FILE__
, __LINE__
, u_errorName(status
));
864 u_austrncpy(desta
, dest
, DESTCAPACITY
);
865 if (strcmp(numFormatted
, desta
) != 0) {
866 log_err("File %s, Line %d, (expected, acutal) = (\"%s\", \"%s\")\n",
867 __FILE__
, __LINE__
, numFormatted
, desta
);
869 if (strlen(numFormatted
) != resultSize
) {
870 log_err("File %s, Line %d, (expected, actual) = (%d, %d)\n",
871 __FILE__
, __LINE__
, strlen(numFormatted
), resultSize
);
874 /* Format with a FieldPosition parameter */
876 fieldPos
.field
= UNUM_DECIMAL_SEPARATOR_FIELD
;
877 resultSize
= unum_formatDecimal(fmt
, numInitial
, -1, dest
, DESTCAPACITY
, &fieldPos
, &status
);
878 if (U_FAILURE(status
)) {
879 log_err("File %s, Line %d, status = %s\n", __FILE__
, __LINE__
, u_errorName(status
));
881 u_austrncpy(desta
, dest
, DESTCAPACITY
);
882 if (strcmp(numFormatted
, desta
) != 0) {
883 log_err("File %s, Line %d, (expected, acutal) = (\"%s\", \"%s\")\n",
884 __FILE__
, __LINE__
, numFormatted
, desta
);
886 if (fieldPos
.beginIndex
!= 26) { /* index of "." in formatted number */
887 log_err("File %s, Line %d, (expected, acutal) = (%d, %d)\n",
888 __FILE__
, __LINE__
, 0, fieldPos
.beginIndex
);
890 if (fieldPos
.endIndex
!= 27) {
891 log_err("File %s, Line %d, (expected, acutal) = (%d, %d)\n",
892 __FILE__
, __LINE__
, 0, fieldPos
.endIndex
);
897 status
= U_ZERO_ERROR
;
898 u_uastrcpy(dest
, numFormatted
); /* Parse the expected output of the formatting test */
899 resultSize
= unum_parseDecimal(fmt
, dest
, -1, NULL
, desta
, DESTCAPACITY
, &status
);
900 if (U_FAILURE(status
)) {
901 log_err("File %s, Line %d, status = %s\n", __FILE__
, __LINE__
, u_errorName(status
));
903 if (strcmp(parseExpected
, desta
) != 0) {
904 log_err("File %s, Line %d, (expected, actual) = (\"%s\", \"%s\")\n",
905 __FILE__
, __LINE__
, parseExpected
, desta
);
907 if (strlen(parseExpected
) != resultSize
) {
908 log_err("File %s, Line %d, (expected, actual) = (%d, %d)\n",
909 __FILE__
, __LINE__
, strlen(parseExpected
), resultSize
);
912 /* Parse with a parsePos parameter */
914 status
= U_ZERO_ERROR
;
915 u_uastrcpy(dest
, numFormatted
); /* Parse the expected output of the formatting test */
916 parsePos
= 3; /* 12,345,678,900,987,654,321.12345679 */
917 /* start parsing at the the third char */
918 resultSize
= unum_parseDecimal(fmt
, dest
, -1, &parsePos
, desta
, DESTCAPACITY
, &status
);
919 if (U_FAILURE(status
)) {
920 log_err("File %s, Line %d, status = %s\n", __FILE__
, __LINE__
, u_errorName(status
));
922 if (strcmp(parseExpected
+2, desta
) != 0) { /* "345678900987654321.12345679" */
923 log_err("File %s, Line %d, (expected, actual) = (\"%s\", \"%s\")\n",
924 __FILE__
, __LINE__
, parseExpected
+2, desta
);
926 if (strlen(numFormatted
) != parsePos
) {
927 log_err("File %s, Line %d, parsePos (expected, actual) = (\"%d\", \"%d\")\n",
928 __FILE__
, __LINE__
, strlen(parseExpected
), parsePos
);
934 status
= U_ZERO_ERROR
;
935 /* Test invalid symbol argument */
937 int32_t badsymbolLarge
= UNUM_FORMAT_SYMBOL_COUNT
+ 1;
938 int32_t badsymbolSmall
= -1;
940 int32_t valueLength
= 10;
941 UNumberFormat
*fmt
= unum_open(UNUM_DEFAULT
, NULL
, 0, NULL
, NULL
, &status
);
942 if (U_FAILURE(status
)) {
943 log_err("File %s, Line %d, status = %s\n", __FILE__
, __LINE__
, u_errorName(status
));
945 unum_getSymbol(fmt
, (UNumberFormatSymbol
)badsymbolLarge
, NULL
, 0, &status
);
946 if (U_SUCCESS(status
)) log_err("unum_getSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (> UNUM_FORMAT_SYMBOL_COUNT) argument\n");
948 status
= U_ZERO_ERROR
;
949 unum_getSymbol(fmt
, (UNumberFormatSymbol
)badsymbolSmall
, NULL
, 0, &status
);
950 if (U_SUCCESS(status
)) log_err("unum_getSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (less than 0) argument\n");
952 status
= U_ZERO_ERROR
;
953 unum_setSymbol(fmt
, (UNumberFormatSymbol
)badsymbolLarge
, value
, valueLength
, &status
);
954 if (U_SUCCESS(status
)) log_err("unum_setSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (> UNUM_FORMAT_SYMBOL_COUNT) argument\n");
956 status
= U_ZERO_ERROR
;
957 unum_setSymbol(fmt
, (UNumberFormatSymbol
)badsymbolSmall
, value
, valueLength
, &status
);
958 if (U_SUCCESS(status
)) log_err("unum_setSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (less than 0) argument\n");
965 /*closing the NumberFormat() using unum_close(UNumberFormat*)")*/
972 unum_close(spellout_def
);
974 unum_close(cur_frpattern
);
979 static void TestParseZero(void)
981 UErrorCode errorCode
= U_ZERO_ERROR
;
982 UChar input
[] = {0x30, 0}; /* Input text is decimal '0' */
983 UChar pat
[] = {0x0023,0x003b,0x0023,0}; /* {'#', ';', '#', 0}; */
987 UNumberFormat
* unum
= unum_open( UNUM_DECIMAL
/*or UNUM_DEFAULT*/, NULL
, -1, NULL
, NULL
, &errorCode
);
989 UNumberFormat
* unum
= unum_open( UNUM_PATTERN_DECIMAL
/*needs pattern*/, pat
, -1, NULL
, NULL
, &errorCode
);
992 dbl
= unum_parseDouble( unum
, input
, -1 /*u_strlen(input)*/, 0 /* 0 = start */, &errorCode
);
993 if (U_FAILURE(errorCode
)) {
994 log_data_err("Result - %s\n", u_errorName(errorCode
));
996 log_verbose("Double: %f\n", dbl
);
1001 static const UChar dollars2Sym
[] = { 0x24,0x32,0x2E,0x30,0x30,0 }; /* $2.00 */
1002 static const UChar dollars4Sym
[] = { 0x24,0x34,0 }; /* $4 */
1003 static const UChar dollarsUS4Sym
[] = { 0x55,0x53,0x24,0x34,0 }; /* US$4 */
1004 static const UChar dollars9Sym
[] = { 0x39,0xA0,0x24,0 }; /* 9 $ */
1005 static const UChar pounds3Sym
[] = { 0xA3,0x33,0x2E,0x30,0x30,0 }; /* [POUND]3.00 */
1006 static const UChar pounds5Sym
[] = { 0xA3,0x35,0 }; /* [POUND]5 */
1007 static const UChar pounds7Sym
[] = { 0x37,0xA0,0xA3,0 }; /* 7 [POUND] */
1008 static const UChar euros4Sym
[] = { 0x34,0x2C,0x30,0x30,0xA0,0x20AC,0 }; /* 4,00 [EURO] */
1009 static const UChar euros6Sym
[] = { 0x36,0xA0,0x20AC,0 }; /* 6 [EURO] */
1010 static const UChar euros8Sym
[] = { 0x20AC,0x38,0 }; /* [EURO]8 */
1011 static const UChar dollars4PluEn
[] = { 0x34,0x20,0x55,0x53,0x20,0x64,0x6F,0x6C,0x6C,0x61,0x72,0x73,0 }; /* 4 US dollars*/
1012 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 */
1013 static const UChar euros8PluEn
[] = { 0x38,0x20,0x65,0x75,0x72,0x6F,0x73,0 }; /* 8 euros*/
1014 static const UChar euros6PluFr
[] = { 0x36,0x20,0x65,0x75,0x72,0x6F,0x73,0 }; /* 6 euros*/
1017 const char * locale
;
1018 const char * descrip
;
1019 const UChar
* currStr
;
1020 const UChar
* plurStr
;
1021 UErrorCode parsDoubExpectErr
;
1022 int32_t parsDoubExpectPos
;
1023 double parsDoubExpectVal
;
1024 UErrorCode parsCurrExpectErr
;
1025 int32_t parsCurrExpectPos
;
1026 double parsCurrExpectVal
;
1027 const char * parsCurrExpectCurr
;
1028 } ParseCurrencyItem
;
1030 static const ParseCurrencyItem parseCurrencyItems
[] = {
1031 { "en_US", "dollars2", dollars2Sym
, NULL
, U_ZERO_ERROR
, 5, 2.0, U_ZERO_ERROR
, 5, 2.0, "USD" },
1032 { "en_US", "dollars4", dollars4Sym
, dollars4PluEn
, U_ZERO_ERROR
, 2, 4.0, U_ZERO_ERROR
, 2, 4.0, "USD" },
1033 { "en_US", "dollars9", dollars9Sym
, NULL
, U_PARSE_ERROR
, 1, 0.0, U_PARSE_ERROR
, 1, 0.0, "" },
1034 { "en_US", "pounds3", pounds3Sym
, NULL
, U_PARSE_ERROR
, 0, 0.0, U_ZERO_ERROR
, 5, 3.0, "GBP" },
1035 { "en_US", "pounds5", pounds5Sym
, pounds5PluEn
, U_PARSE_ERROR
, 0, 0.0, U_ZERO_ERROR
, 2, 5.0, "GBP" },
1036 { "en_US", "pounds7", pounds7Sym
, NULL
, U_PARSE_ERROR
, 1, 0.0, U_PARSE_ERROR
, 1, 0.0, "" },
1037 { "en_US", "euros8", euros8Sym
, euros8PluEn
, U_PARSE_ERROR
, 0, 0.0, U_ZERO_ERROR
, 2, 8.0, "EUR" },
1039 { "en_GB", "pounds3", pounds3Sym
, NULL
, U_ZERO_ERROR
, 5, 3.0, U_ZERO_ERROR
, 5, 3.0, "GBP" },
1040 { "en_GB", "pounds5", pounds5Sym
, pounds5PluEn
, U_ZERO_ERROR
, 2, 5.0, U_ZERO_ERROR
, 2, 5.0, "GBP" },
1041 { "en_GB", "pounds7", pounds7Sym
, NULL
, U_PARSE_ERROR
, 1, 0.0, U_PARSE_ERROR
, 1, 0.0, "" },
1042 { "en_GB", "euros4", euros4Sym
, NULL
, U_PARSE_ERROR
, 4, 0.0, U_PARSE_ERROR
, 4, 0.0, "" },
1043 { "en_GB", "euros6", euros6Sym
, NULL
, U_PARSE_ERROR
, 1, 0.0, U_PARSE_ERROR
, 1, 0.0, "" },
1044 { "en_GB", "euros8", euros8Sym
, euros8PluEn
, U_PARSE_ERROR
, 0, 0.0, U_ZERO_ERROR
, 2, 8.0, "EUR" },
1045 { "en_GB", "dollars4", dollarsUS4Sym
, dollars4PluEn
, U_PARSE_ERROR
, 0, 0.0, U_ZERO_ERROR
, 4, 4.0, "USD" },
1047 { "fr_FR", "euros4", euros4Sym
, NULL
, U_ZERO_ERROR
, 6, 4.0, U_ZERO_ERROR
, 6, 4.0, "EUR" },
1048 { "fr_FR", "euros6", euros6Sym
, euros6PluFr
, U_ZERO_ERROR
, 3, 6.0, U_ZERO_ERROR
, 3, 6.0, "EUR" },
1049 { "fr_FR", "euros8", euros8Sym
, NULL
, U_PARSE_ERROR
, 0, 0.0, U_PARSE_ERROR
, 0, 0.0, "" },
1050 { "fr_FR", "dollars2", dollars2Sym
, NULL
, U_PARSE_ERROR
, 0, 0.0, U_PARSE_ERROR
, 0, 0.0, "" },
1051 { "fr_FR", "dollars4", dollars4Sym
, NULL
, U_PARSE_ERROR
, 0, 0.0, U_PARSE_ERROR
, 0, 0.0, "" },
1053 { NULL
, NULL
, NULL
, NULL
, 0, 0, 0.0, 0, 0, 0.0, NULL
}
1056 static void TestParseCurrency()
1058 const ParseCurrencyItem
* itemPtr
;
1059 for (itemPtr
= parseCurrencyItems
; itemPtr
->locale
!= NULL
; ++itemPtr
) {
1060 UNumberFormat
* unum
;
1067 status
= U_ZERO_ERROR
;
1068 unum
= unum_open(UNUM_CURRENCY
, NULL
, 0, itemPtr
->locale
, NULL
, &status
);
1069 if (U_SUCCESS(status
)) {
1070 status
= U_ZERO_ERROR
;
1072 parseVal
= unum_parseDouble(unum
, itemPtr
->currStr
, -1, &parsePos
, &status
);
1073 if (status
!= itemPtr
->parsDoubExpectErr
|| parsePos
!= itemPtr
->parsDoubExpectPos
|| parseVal
!= itemPtr
->parsDoubExpectVal
) {
1074 log_err("UNUM_CURRENCY parseDouble %s/%s, expect %s pos %d val %.1f, get %s pos %d val %.1f\n",
1075 itemPtr
->locale
, itemPtr
->descrip
,
1076 u_errorName(itemPtr
->parsDoubExpectErr
), itemPtr
->parsDoubExpectPos
, itemPtr
->parsDoubExpectVal
,
1077 u_errorName(status
), parsePos
, parseVal
);
1079 status
= U_ZERO_ERROR
;
1082 parseVal
= unum_parseDoubleCurrency(unum
, itemPtr
->currStr
, -1, &parsePos
, parseCurr
, &status
);
1083 u_austrncpy(parseCurrB
, parseCurr
, 4);
1084 if (status
!= itemPtr
->parsCurrExpectErr
|| parsePos
!= itemPtr
->parsCurrExpectPos
|| parseVal
!= itemPtr
->parsCurrExpectVal
||
1085 strncmp(parseCurrB
, itemPtr
->parsCurrExpectCurr
, 4) != 0) {
1086 log_err("UNUM_CURRENCY parseDoubleCurrency %s/%s, expect %s pos %d val %.1f cur %s, get %s pos %d val %.1f cur %s\n",
1087 itemPtr
->locale
, itemPtr
->descrip
,
1088 u_errorName(itemPtr
->parsCurrExpectErr
), itemPtr
->parsCurrExpectPos
, itemPtr
->parsCurrExpectVal
, itemPtr
->parsCurrExpectCurr
,
1089 u_errorName(status
), parsePos
, parseVal
, parseCurrB
);
1093 log_data_err("unexpected error in unum_open UNUM_CURRENCY for locale %s: '%s'\n", itemPtr
->locale
, u_errorName(status
));
1096 if (itemPtr
->plurStr
!= NULL
) {
1097 status
= U_ZERO_ERROR
;
1098 unum
= unum_open(UNUM_CURRENCY_PLURAL
, NULL
, 0, itemPtr
->locale
, NULL
, &status
);
1099 if (U_SUCCESS(status
)) {
1100 status
= U_ZERO_ERROR
;
1102 parseVal
= unum_parseDouble(unum
, itemPtr
->plurStr
, -1, &parsePos
, &status
);
1103 if (status
!= itemPtr
->parsDoubExpectErr
|| parseVal
!= itemPtr
->parsDoubExpectVal
) {
1104 log_err("UNUM_CURRENCY parseDouble %s/%s, expect %s val %.1f, get %s val %.1f\n",
1105 itemPtr
->locale
, itemPtr
->descrip
,
1106 u_errorName(itemPtr
->parsDoubExpectErr
), itemPtr
->parsDoubExpectVal
,
1107 u_errorName(status
), parseVal
);
1109 status
= U_ZERO_ERROR
;
1112 parseVal
= unum_parseDoubleCurrency(unum
, itemPtr
->plurStr
, -1, &parsePos
, parseCurr
, &status
);
1113 u_austrncpy(parseCurrB
, parseCurr
, 4);
1114 if (status
!= itemPtr
->parsCurrExpectErr
|| parseVal
!= itemPtr
->parsCurrExpectVal
||
1115 strncmp(parseCurrB
, itemPtr
->parsCurrExpectCurr
, 4) != 0) {
1116 log_err("UNUM_CURRENCY parseDoubleCurrency %s/%s, expect %s val %.1f cur %s, get %s val %.1f cur %s\n",
1117 itemPtr
->locale
, itemPtr
->descrip
,
1118 u_errorName(itemPtr
->parsCurrExpectErr
), itemPtr
->parsCurrExpectVal
, itemPtr
->parsCurrExpectCurr
,
1119 u_errorName(status
), parseVal
, parseCurrB
);
1123 log_data_err("unexpected error in unum_open UNUM_CURRENCY_PLURAL for locale %s: '%s'\n", itemPtr
->locale
, u_errorName(status
));
1130 const char * testname
;
1131 const char * locale
;
1133 const UChar
* source
;
1140 static const UChar ustr_zh50d
[] = {0x4E94, 0x3007, 0}; /* decimal 50 */
1141 static const UChar ustr_zh05a
[] = {0x96F6, 0x4E94, 0}; /* decimal-alt 05 */
1142 static const UChar ustr_zh05d
[] = {0x3007, 0x4E94, 0}; /* decimal 05 */
1144 static const NumParseTestItem altnumParseTests
[] = {
1145 /* name loc lenent src start val end status */
1146 { "zh@hd,50dL","zh@numbers=hanidec", TRUE
, ustr_zh50d
, 0, 50, 2, U_USING_DEFAULT_WARNING
},
1147 { "zh@hd,05aL","zh@numbers=hanidec", TRUE
, ustr_zh05a
, 0, 5, 2, U_USING_DEFAULT_WARNING
},
1148 { "zh@hd,05dL","zh@numbers=hanidec", TRUE
, ustr_zh05d
, 0, 5, 2, U_USING_DEFAULT_WARNING
},
1149 { NULL
, NULL
, FALSE
, NULL
, 0, 0, 0, 0 } /* terminator */
1152 static void TestParseAltNum(void)
1154 const NumParseTestItem
* testPtr
;
1155 for (testPtr
= altnumParseTests
; testPtr
->testname
!= NULL
; ++testPtr
) {
1156 UErrorCode status
= U_ZERO_ERROR
;
1157 int32_t value
, position
= testPtr
->startPos
;
1158 UNumberFormat
*nf
= unum_open(UNUM_DECIMAL
, NULL
, 0, testPtr
->locale
, NULL
, &status
);
1159 if (U_FAILURE(status
)) {
1160 log_err_status(status
, "unum_open fails for UNUM_DECIMAL with locale %s, status %s\n", testPtr
->locale
, myErrorName(status
));
1163 unum_setAttribute(nf
, UNUM_LENIENT_PARSE
, testPtr
->lenient
);
1164 value
= unum_parse(nf
, testPtr
->source
, -1, &position
, &status
);
1165 if ( value
!= testPtr
->value
|| position
!= testPtr
->endPos
|| status
!= testPtr
->status
) {
1166 log_err("unum_parse DECIMAL, locale %s, testname %s, startPos %d: for value / endPos / status, expected %d / %d / %s, got %d / %d / %s\n",
1167 testPtr
->locale
, testPtr
->testname
, testPtr
->startPos
,
1168 testPtr
->value
, testPtr
->endPos
, myErrorName(testPtr
->status
),
1169 value
, position
, myErrorName(status
) );
1175 static const UChar ustr_en0
[] = {0x7A, 0x65, 0x72, 0x6F, 0}; /* zero */
1176 static const UChar ustr_123
[] = {0x31, 0x32, 0x33, 0}; /* 123 */
1177 static const UChar ustr_en123
[] = {0x6f, 0x6e, 0x65, 0x20, 0x68, 0x75, 0x6e, 0x64, 0x72, 0x65, 0x64,
1178 0x20, 0x74, 0x77, 0x65, 0x6e, 0x74, 0x79,
1179 0x2d, 0x74, 0x68, 0x72, 0x65, 0x65, 0}; /* one hundred twenty-three */
1180 static const UChar ustr_fr123
[] = {0x63, 0x65, 0x6e, 0x74, 0x20, 0x76, 0x69, 0x6e, 0x67, 0x74, 0x2d,
1181 0x74, 0x72, 0x6f, 0x69, 0x73, 0}; /* cent vingt-trois */
1182 static const UChar ustr_ja123
[] = {0x767e, 0x4e8c, 0x5341, 0x4e09, 0}; /* kanji 100(+)2(*)10(+)3 */
1183 static const UChar ustr_zh50s
[] = {0x4E94, 0x5341, 0}; /* spellout 50 */
1184 //static const UChar ustr_zh50d[] = [reuse from above] /* decimal 50 */
1185 //static const UChar ustr_zh05a[] = [reuse from above] /* decimal-alt 05 */
1186 //static const UChar ustr_zh05d[] = [reuse from above] /* decimal 05 */
1188 #define NUMERIC_STRINGS_NOT_PARSEABLE 1 // ticket/8224
1190 static const NumParseTestItem spelloutParseTests
[] = {
1191 /* name loc lenent src start val end status */
1192 { "en0", "en", FALSE
, ustr_en0
, 0, 0, 4, U_ZERO_ERROR
},
1193 { "en0", "en", FALSE
, ustr_en0
, 2, 0, 2, U_PARSE_ERROR
},
1194 { "en0", "ja", FALSE
, ustr_en0
, 0, 0, 0, U_PARSE_ERROR
},
1195 #if NUMERIC_STRINGS_NOT_PARSEABLE
1196 { "123", "en", FALSE
, ustr_123
, 0, 0, 0, U_PARSE_ERROR
},
1198 { "123", "en", FALSE
, ustr_123
, 0, 123, 3, U_ZERO_ERROR
},
1200 { "123L", "en", TRUE
, ustr_123
, 0, 123, 3, U_ZERO_ERROR
},
1201 { "en123", "en", FALSE
, ustr_en123
, 0, 123, 24, U_ZERO_ERROR
},
1202 { "en123", "en", FALSE
, ustr_en123
, 12, 23, 24, U_ZERO_ERROR
},
1203 { "en123", "fr", FALSE
, ustr_en123
, 16, 0, 16, U_PARSE_ERROR
},
1204 { "fr123", "fr", FALSE
, ustr_fr123
, 0, 123, 16, U_ZERO_ERROR
},
1205 { "fr123", "fr", FALSE
, ustr_fr123
, 5, 23, 16, U_ZERO_ERROR
},
1206 { "fr123", "en", FALSE
, ustr_fr123
, 0, 0, 0, U_PARSE_ERROR
},
1207 { "ja123", "ja", FALSE
, ustr_ja123
, 0, 123, 4, U_ZERO_ERROR
},
1208 { "ja123", "ja", FALSE
, ustr_ja123
, 1, 23, 4, U_ZERO_ERROR
},
1209 { "ja123", "fr", FALSE
, ustr_ja123
, 0, 0, 0, U_PARSE_ERROR
},
1210 { "zh,50s", "zh", FALSE
, ustr_zh50s
, 0, 50, 2, U_ZERO_ERROR
},
1211 // from ICU50m2, NUMERIC_STRINGS_NOT_PARSEABLE no longer affects the next three
1212 { "zh@hd,50d", "zh@numbers=hanidec", FALSE
, ustr_zh50d
, 0, 50, 2, U_ZERO_ERROR
},
1213 { "zh@hd,05a", "zh@numbers=hanidec", FALSE
, ustr_zh05a
, 0, 5, 2, U_ZERO_ERROR
},
1214 { "zh@hd,05d", "zh@numbers=hanidec", FALSE
, ustr_zh05d
, 0, 5, 2, U_ZERO_ERROR
},
1215 { "zh@hd,50dL","zh@numbers=hanidec", TRUE
, ustr_zh50d
, 0, 50, 2, U_ZERO_ERROR
},
1216 { "zh@hd,05aL","zh@numbers=hanidec", TRUE
, ustr_zh05a
, 0, 5, 2, U_ZERO_ERROR
},
1217 { "zh@hd,05dL","zh@numbers=hanidec", TRUE
, ustr_zh05d
, 0, 5, 2, U_ZERO_ERROR
},
1218 { "zh,50dL","zh", TRUE
, ustr_zh50d
, 0, 50, 2, U_ZERO_ERROR
}, /* changed in ICU50m2 */
1219 { "zh,05aL","zh", TRUE
, ustr_zh05a
, 0, 0, 1, U_ZERO_ERROR
},
1220 { "zh,05dL","zh", TRUE
, ustr_zh05d
, 0, 5, 2, U_ZERO_ERROR
}, /* changed in ICU50m2 */
1221 { NULL
, NULL
, FALSE
, NULL
, 0, 0, 0, 0 } /* terminator */
1224 static void TestSpelloutNumberParse()
1226 const NumParseTestItem
* testPtr
;
1227 for (testPtr
= spelloutParseTests
; testPtr
->testname
!= NULL
; ++testPtr
) {
1228 UErrorCode status
= U_ZERO_ERROR
;
1229 int32_t value
, position
= testPtr
->startPos
;
1230 UNumberFormat
*nf
= unum_open(UNUM_SPELLOUT
, NULL
, 0, testPtr
->locale
, NULL
, &status
);
1231 if (U_FAILURE(status
)) {
1232 log_err_status(status
, "unum_open fails for UNUM_SPELLOUT with locale %s, status %s\n", testPtr
->locale
, myErrorName(status
));
1235 unum_setAttribute(nf
, UNUM_LENIENT_PARSE
, testPtr
->lenient
);
1236 status
= U_ZERO_ERROR
;
1237 value
= unum_parse(nf
, testPtr
->source
, -1, &position
, &status
);
1238 if ( value
!= testPtr
->value
|| position
!= testPtr
->endPos
|| status
!= testPtr
->status
) {
1239 log_err("unum_parse SPELLOUT, locale %s, testname %s, startPos %d: for value / endPos / status, expected %d / %d / %s, got %d / %d / %s\n",
1240 testPtr
->locale
, testPtr
->testname
, testPtr
->startPos
,
1241 testPtr
->value
, testPtr
->endPos
, myErrorName(testPtr
->status
),
1242 value
, position
, myErrorName(status
) );
1248 static void TestSignificantDigits()
1251 int32_t resultlengthneeded
;
1252 int32_t resultlength
;
1253 UErrorCode status
= U_ZERO_ERROR
;
1254 UChar
*result
= NULL
;
1256 double d
= 123456.789;
1258 u_uastrcpy(temp
, "###0.0#");
1259 fmt
=unum_open(UNUM_IGNORE
, temp
, -1, NULL
, NULL
,&status
);
1260 if (U_FAILURE(status
)) {
1261 log_data_err("got unexpected error for unum_open: '%s'\n", u_errorName(status
));
1264 unum_setAttribute(fmt
, UNUM_SIGNIFICANT_DIGITS_USED
, TRUE
);
1265 unum_setAttribute(fmt
, UNUM_MAX_SIGNIFICANT_DIGITS
, 6);
1267 u_uastrcpy(temp
, "123457");
1269 resultlengthneeded
=unum_formatDouble(fmt
, d
, NULL
, resultlength
, NULL
, &status
);
1270 if(status
==U_BUFFER_OVERFLOW_ERROR
)
1272 status
=U_ZERO_ERROR
;
1273 resultlength
=resultlengthneeded
+1;
1274 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
1275 unum_formatDouble(fmt
, d
, result
, resultlength
, NULL
, &status
);
1277 if(U_FAILURE(status
))
1279 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
));
1282 if(u_strcmp(result
, temp
)==0)
1283 log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
1285 log_err("FAIL: Error in number formatting using unum_formatDouble()\n");
1290 static void TestSigDigRounding()
1292 UErrorCode status
= U_ZERO_ERROR
;
1293 UChar expected
[128];
1300 fmt
=unum_open(UNUM_DECIMAL
, NULL
, 0, NULL
/* "en_US"*/, NULL
, &status
);
1301 if (U_FAILURE(status
)) {
1302 log_data_err("got unexpected error for unum_open: '%s'\n", u_errorName(status
));
1305 unum_setAttribute(fmt
, UNUM_LENIENT_PARSE
, FALSE
);
1306 unum_setAttribute(fmt
, UNUM_SIGNIFICANT_DIGITS_USED
, TRUE
);
1307 unum_setAttribute(fmt
, UNUM_MAX_SIGNIFICANT_DIGITS
, 2);
1308 /* unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 0); */
1310 unum_setAttribute(fmt
, UNUM_ROUNDING_MODE
, UNUM_ROUND_UP
);
1311 unum_setDoubleAttribute(fmt
, UNUM_ROUNDING_INCREMENT
, 20.0);
1313 (void)unum_formatDouble(fmt
, d
, result
, UPRV_LENGTHOF(result
), NULL
, &status
);
1314 if(U_FAILURE(status
))
1316 log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status
));
1320 u_uastrcpy(expected
, "140");
1321 if(u_strcmp(result
, expected
)!=0)
1322 log_err("FAIL: Error in unum_formatDouble result %s instead of %s\n", u_austrcpy(temp1
, result
), u_austrcpy(temp2
, expected
) );
1327 static void TestNumberFormatPadding()
1332 UErrorCode status
=U_ZERO_ERROR
;
1333 int32_t resultlength
;
1334 int32_t resultlengthneeded
;
1335 UNumberFormat
*pattern
;
1337 double d
= -10456.37;
1338 UFieldPosition pos1
;
1341 /* create a number format using unum_openPattern(....)*/
1342 log_verbose("\nTesting unum_openPattern() with padding\n");
1343 u_uastrcpy(temp1
, "*#,##0.0#*;(#,##0.0#)");
1344 status
=U_ZERO_ERROR
;
1345 pattern
=unum_open(UNUM_IGNORE
,temp1
, u_strlen(temp1
), NULL
, NULL
,&status
);
1346 if(U_SUCCESS(status
))
1348 log_err("error in unum_openPattern(%s): %s\n", temp1
, myErrorName(status
) );
1352 unum_close(pattern
);
1355 /* u_uastrcpy(temp1, "*x#,###,###,##0.0#;(*x#,###,###,##0.0#)"); */
1356 u_uastrcpy(temp1
, "*x#,###,###,##0.0#;*x(###,###,##0.0#)");
1357 status
=U_ZERO_ERROR
;
1358 pattern
=unum_open(UNUM_IGNORE
,temp1
, u_strlen(temp1
), "en_US",NULL
, &status
);
1359 if(U_FAILURE(status
))
1361 log_err_status(status
, "error in padding unum_openPattern(%s): %s\n", temp1
, myErrorName(status
) );;
1364 log_verbose("Pass: padding unum_openPattern() works fine\n");
1366 /*test for unum_toPattern()*/
1367 log_verbose("\nTesting padding unum_toPattern()\n");
1369 resultlengthneeded
=unum_toPattern(pattern
, FALSE
, NULL
, resultlength
, &status
);
1370 if(status
==U_BUFFER_OVERFLOW_ERROR
)
1372 status
=U_ZERO_ERROR
;
1373 resultlength
=resultlengthneeded
+1;
1374 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
1375 unum_toPattern(pattern
, FALSE
, result
, resultlength
, &status
);
1377 if(U_FAILURE(status
))
1379 log_err("error in extracting the padding pattern from UNumberFormat: %s\n", myErrorName(status
));
1383 if(u_strcmp(result
, temp1
)!=0)
1384 log_err("FAIL: Error in extracting the padding pattern using unum_toPattern()\n");
1386 log_verbose("Pass: extracted the padding pattern correctly using unum_toPattern()\n");
1389 /* u_uastrcpy(temp1, "(xxxxxxx10,456.37)"); */
1390 u_uastrcpy(temp1
, "xxxxx(10,456.37)");
1392 pos1
.field
= UNUM_FRACTION_FIELD
;
1393 resultlengthneeded
=unum_formatDouble(pattern
, d
, NULL
, resultlength
, &pos1
, &status
);
1394 if(status
==U_BUFFER_OVERFLOW_ERROR
)
1396 status
=U_ZERO_ERROR
;
1397 resultlength
=resultlengthneeded
+1;
1398 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
1399 unum_formatDouble(pattern
, d
, result
, resultlength
, NULL
, &status
);
1401 if(U_FAILURE(status
))
1403 log_err("Error in formatting using unum_formatDouble(.....) with padding : %s\n", myErrorName(status
));
1407 if(u_strcmp(result
, temp1
)==0)
1408 log_verbose("Pass: Number Formatting using unum_formatDouble() padding Successful\n");
1410 log_data_err("FAIL: Error in number formatting using unum_formatDouble() with padding\n");
1411 if(pos1
.beginIndex
== 13 && pos1
.endIndex
== 15)
1412 log_verbose("Pass: Complete number formatting using unum_formatDouble() successful\n");
1414 log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=13 end=15\n",
1415 pos1
.beginIndex
, pos1
.endIndex
);
1418 /* Testing unum_parse() and unum_parseDouble() */
1419 log_verbose("\nTesting padding unum_parseDouble()\n");
1421 d1
=unum_parseDouble(pattern
, result
, u_strlen(result
), &parsepos
, &status
);
1422 if(U_FAILURE(status
))
1424 log_err("padding parse failed. The error is : %s\n", myErrorName(status
));
1428 log_err("Fail: Error in padding parsing\n");
1430 log_verbose("Pass: padding parsing successful\n");
1435 unum_close(pattern
);
1439 withinErr(double a
, double b
, double err
) {
1440 return uprv_fabs(a
- b
) < uprv_fabs(a
* err
);
1443 static void TestInt64Format() {
1447 UErrorCode status
= U_ZERO_ERROR
;
1448 const double doubleInt64Max
= (double)U_INT64_MAX
;
1449 const double doubleInt64Min
= (double)U_INT64_MIN
;
1450 const double doubleBig
= 10.0 * (double)U_INT64_MAX
;
1456 /* create a number format using unum_openPattern(....) */
1457 log_verbose("\nTesting Int64Format\n");
1458 u_uastrcpy(temp1
, "#.#E0");
1459 fmt
= unum_open(UNUM_IGNORE
, temp1
, u_strlen(temp1
), "en_US", NULL
, &status
);
1460 if(U_FAILURE(status
)) {
1461 log_data_err("error in unum_openPattern() - %s\n", myErrorName(status
));
1463 unum_setAttribute(fmt
, UNUM_MAX_FRACTION_DIGITS
, 20);
1464 unum_formatInt64(fmt
, U_INT64_MAX
, result
, 512, NULL
, &status
);
1465 if (U_FAILURE(status
)) {
1466 log_err("error in unum_format(): %s\n", myErrorName(status
));
1468 log_verbose("format int64max: '%s'\n", result
);
1470 val32
= unum_parse(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1471 if (status
!= U_INVALID_FORMAT_ERROR
) {
1472 log_err("parse didn't report error: %s\n", myErrorName(status
));
1473 } else if (val32
!= INT32_MAX
) {
1474 log_err("parse didn't pin return value, got: %d\n", val32
);
1477 status
= U_ZERO_ERROR
;
1479 val64
= unum_parseInt64(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1480 if (U_FAILURE(status
)) {
1481 log_err("parseInt64 returned error: %s\n", myErrorName(status
));
1482 } else if (val64
!= U_INT64_MAX
) {
1483 log_err("parseInt64 returned incorrect value, got: %ld\n", val64
);
1486 status
= U_ZERO_ERROR
;
1488 valDouble
= unum_parseDouble(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1489 if (U_FAILURE(status
)) {
1490 log_err("parseDouble returned error: %s\n", myErrorName(status
));
1491 } else if (valDouble
!= doubleInt64Max
) {
1492 log_err("parseDouble returned incorrect value, got: %g\n", valDouble
);
1496 unum_formatInt64(fmt
, U_INT64_MIN
, result
, 512, NULL
, &status
);
1497 if (U_FAILURE(status
)) {
1498 log_err("error in unum_format(): %s\n", myErrorName(status
));
1500 log_verbose("format int64min: '%s'\n", result
);
1502 val32
= unum_parse(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1503 if (status
!= U_INVALID_FORMAT_ERROR
) {
1504 log_err("parse didn't report error: %s\n", myErrorName(status
));
1505 } else if (val32
!= INT32_MIN
) {
1506 log_err("parse didn't pin return value, got: %d\n", val32
);
1509 status
= U_ZERO_ERROR
;
1511 val64
= unum_parseInt64(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1512 if (U_FAILURE(status
)) {
1513 log_err("parseInt64 returned error: %s\n", myErrorName(status
));
1514 } else if (val64
!= U_INT64_MIN
) {
1515 log_err("parseInt64 returned incorrect value, got: %ld\n", val64
);
1518 status
= U_ZERO_ERROR
;
1520 valDouble
= unum_parseDouble(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1521 if (U_FAILURE(status
)) {
1522 log_err("parseDouble returned error: %s\n", myErrorName(status
));
1523 } else if (valDouble
!= doubleInt64Min
) {
1524 log_err("parseDouble returned incorrect value, got: %g\n", valDouble
);
1528 unum_formatDouble(fmt
, doubleBig
, result
, 512, NULL
, &status
);
1529 if (U_FAILURE(status
)) {
1530 log_err("error in unum_format(): %s\n", myErrorName(status
));
1532 log_verbose("format doubleBig: '%s'\n", result
);
1534 val32
= unum_parse(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1535 if (status
!= U_INVALID_FORMAT_ERROR
) {
1536 log_err("parse didn't report error: %s\n", myErrorName(status
));
1537 } else if (val32
!= INT32_MAX
) {
1538 log_err("parse didn't pin return value, got: %d\n", val32
);
1541 status
= U_ZERO_ERROR
;
1543 val64
= unum_parseInt64(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1544 if (status
!= U_INVALID_FORMAT_ERROR
) {
1545 log_err("parseInt64 didn't report error error: %s\n", myErrorName(status
));
1546 } else if (val64
!= U_INT64_MAX
) {
1547 log_err("parseInt64 returned incorrect value, got: %ld\n", val64
);
1550 status
= U_ZERO_ERROR
;
1552 valDouble
= unum_parseDouble(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1553 if (U_FAILURE(status
)) {
1554 log_err("parseDouble returned error: %s\n", myErrorName(status
));
1555 } else if (!withinErr(valDouble
, doubleBig
, 1e-15)) {
1556 log_err("parseDouble returned incorrect value, got: %g\n", valDouble
);
1560 u_uastrcpy(result
, "5.06e-27");
1562 valDouble
= unum_parseDouble(fmt
, result
, u_strlen(result
), &parsepos
, &status
);
1563 if (U_FAILURE(status
)) {
1564 log_err("parseDouble() returned error: %s\n", myErrorName(status
));
1565 } else if (!withinErr(valDouble
, 5.06e-27, 1e-15)) {
1566 log_err("parseDouble() returned incorrect value, got: %g\n", valDouble
);
1573 static void test_fmt(UNumberFormat
* fmt
, UBool isDecimal
) {
1576 int32_t BUFSIZE
= UPRV_LENGTHOF(buffer
);
1578 -.2, 0, .2, 5.5, 15.2, 250, 123456789
1582 for (i
= 0; i
< UPRV_LENGTHOF(vals
); ++i
) {
1583 UErrorCode status
= U_ZERO_ERROR
;
1584 unum_formatDouble(fmt
, vals
[i
], buffer
, BUFSIZE
, NULL
, &status
);
1585 if (U_FAILURE(status
)) {
1586 log_err("failed to format: %g, returned %s\n", vals
[i
], u_errorName(status
));
1588 u_austrcpy(temp
, buffer
);
1589 log_verbose("formatting %g returned '%s'\n", vals
[i
], temp
);
1593 /* check APIs now */
1595 UErrorCode status
= U_ZERO_ERROR
;
1597 u_uastrcpy(buffer
, "#,##0.0#");
1598 unum_applyPattern(fmt
, FALSE
, buffer
, -1, &perr
, &status
);
1599 if (isDecimal
? U_FAILURE(status
) : (status
!= U_UNSUPPORTED_ERROR
)) {
1600 log_err("got unexpected error for applyPattern: '%s'\n", u_errorName(status
));
1605 int isLenient
= unum_getAttribute(fmt
, UNUM_LENIENT_PARSE
);
1606 log_verbose("lenient: 0x%x\n", isLenient
);
1607 if (isLenient
!= FALSE
) {
1608 log_err("didn't expect lenient value: %d\n", isLenient
);
1611 unum_setAttribute(fmt
, UNUM_LENIENT_PARSE
, TRUE
);
1612 isLenient
= unum_getAttribute(fmt
, UNUM_LENIENT_PARSE
);
1613 if (isLenient
!= TRUE
) {
1614 log_err("didn't expect lenient value after set: %d\n", isLenient
);
1620 double val
= unum_getDoubleAttribute(fmt
, UNUM_LENIENT_PARSE
);
1622 log_err("didn't expect double attribute\n");
1624 val
= unum_getDoubleAttribute(fmt
, UNUM_ROUNDING_INCREMENT
);
1625 if ((val
== -1) == isDecimal
) {
1626 log_err("didn't expect -1 rounding increment\n");
1628 unum_setDoubleAttribute(fmt
, UNUM_ROUNDING_INCREMENT
, val
+.5);
1629 val2
= unum_getDoubleAttribute(fmt
, UNUM_ROUNDING_INCREMENT
);
1630 if (isDecimal
&& (val2
- val
!= .5)) {
1631 log_err("set rounding increment had no effect on decimal format");
1636 UErrorCode status
= U_ZERO_ERROR
;
1637 int len
= unum_getTextAttribute(fmt
, UNUM_DEFAULT_RULESET
, buffer
, BUFSIZE
, &status
);
1638 if (isDecimal
? (status
!= U_UNSUPPORTED_ERROR
) : U_FAILURE(status
)) {
1639 log_err("got unexpected error for get default ruleset: '%s'\n", u_errorName(status
));
1641 if (U_SUCCESS(status
)) {
1642 u_austrcpy(temp
, buffer
);
1643 log_verbose("default ruleset: '%s'\n", temp
);
1646 status
= U_ZERO_ERROR
;
1647 len
= unum_getTextAttribute(fmt
, UNUM_PUBLIC_RULESETS
, buffer
, BUFSIZE
, &status
);
1648 if (isDecimal
? (status
!= U_UNSUPPORTED_ERROR
) : U_FAILURE(status
)) {
1649 log_err("got unexpected error for get public rulesets: '%s'\n", u_errorName(status
));
1651 if (U_SUCCESS(status
)) {
1652 u_austrcpy(temp
, buffer
);
1653 log_verbose("public rulesets: '%s'\n", temp
);
1655 /* set the default ruleset to the first one found, and retry */
1658 for (i
= 0; i
< len
&& temp
[i
] != ';'; ++i
){};
1661 unum_setTextAttribute(fmt
, UNUM_DEFAULT_RULESET
, buffer
, -1, &status
);
1662 if (U_FAILURE(status
)) {
1663 log_err("unexpected error setting default ruleset: '%s'\n", u_errorName(status
));
1665 int len2
= unum_getTextAttribute(fmt
, UNUM_DEFAULT_RULESET
, buffer
, BUFSIZE
, &status
);
1666 if (U_FAILURE(status
)) {
1667 log_err("could not fetch default ruleset: '%s'\n", u_errorName(status
));
1668 } else if (len2
!= i
) {
1669 u_austrcpy(temp
, buffer
);
1670 log_err("unexpected ruleset len: %d ex: %d val: %s\n", len2
, i
, temp
);
1672 for (i
= 0; i
< UPRV_LENGTHOF(vals
); ++i
) {
1673 status
= U_ZERO_ERROR
;
1674 unum_formatDouble(fmt
, vals
[i
], buffer
, BUFSIZE
, NULL
, &status
);
1675 if (U_FAILURE(status
)) {
1676 log_err("failed to format: %g, returned %s\n", vals
[i
], u_errorName(status
));
1678 u_austrcpy(temp
, buffer
);
1679 log_verbose("formatting %g returned '%s'\n", vals
[i
], temp
);
1690 UErrorCode status
= U_ZERO_ERROR
;
1691 unum_toPattern(fmt
, FALSE
, buffer
, BUFSIZE
, &status
);
1692 if (U_SUCCESS(status
)) {
1693 u_austrcpy(temp
, buffer
);
1694 log_verbose("pattern: '%s'\n", temp
);
1695 } else if (status
!= U_BUFFER_OVERFLOW_ERROR
) {
1696 log_err("toPattern failed unexpectedly: %s\n", u_errorName(status
));
1698 log_verbose("pattern too long to display\n");
1703 UErrorCode status
= U_ZERO_ERROR
;
1704 int len
= unum_getSymbol(fmt
, UNUM_CURRENCY_SYMBOL
, buffer
, BUFSIZE
, &status
);
1705 if (isDecimal
? U_FAILURE(status
) : (status
!= U_UNSUPPORTED_ERROR
)) {
1706 log_err("unexpected error getting symbol: '%s'\n", u_errorName(status
));
1709 unum_setSymbol(fmt
, UNUM_CURRENCY_SYMBOL
, buffer
, len
, &status
);
1710 if (isDecimal
? U_FAILURE(status
) : (status
!= U_UNSUPPORTED_ERROR
)) {
1711 log_err("unexpected error setting symbol: '%s'\n", u_errorName(status
));
1716 static void TestNonExistentCurrency() {
1717 UNumberFormat
*format
;
1718 UErrorCode status
= U_ZERO_ERROR
;
1719 UChar currencySymbol
[8];
1720 static const UChar QQQ
[] = {0x51, 0x51, 0x51, 0};
1722 /* Get a non-existent currency and make sure it returns the correct currency code. */
1723 format
= unum_open(UNUM_CURRENCY
, NULL
, 0, "th_TH@currency=QQQ", NULL
, &status
);
1724 if (format
== NULL
|| U_FAILURE(status
)) {
1725 log_data_err("unum_open did not return expected result for non-existent requested currency: '%s' (Are you missing data?)\n", u_errorName(status
));
1728 unum_getSymbol(format
,
1729 UNUM_CURRENCY_SYMBOL
,
1731 UPRV_LENGTHOF(currencySymbol
),
1733 if (u_strcmp(currencySymbol
, QQQ
) != 0) {
1734 log_err("unum_open set the currency to QQQ\n");
1740 static void TestRBNFFormat() {
1744 UChar tempUChars
[512];
1745 UNumberFormat
*formats
[5];
1746 int COUNT
= UPRV_LENGTHOF(formats
);
1749 for (i
= 0; i
< COUNT
; ++i
) {
1754 status
= U_ZERO_ERROR
;
1755 u_uastrcpy(pat
, "#,##0.0#;(#,##0.0#)");
1756 formats
[0] = unum_open(UNUM_PATTERN_DECIMAL
, pat
, -1, "en_US", &perr
, &status
);
1757 if (U_FAILURE(status
)) {
1758 log_err_status(status
, "unable to open decimal pattern -> %s\n", u_errorName(status
));
1762 status
= U_ZERO_ERROR
;
1763 formats
[1] = unum_open(UNUM_SPELLOUT
, NULL
, 0, "en_US", &perr
, &status
);
1764 if (U_FAILURE(status
)) {
1765 log_err_status(status
, "unable to open spellout -> %s\n", u_errorName(status
));
1769 status
= U_ZERO_ERROR
;
1770 formats
[2] = unum_open(UNUM_ORDINAL
, NULL
, 0, "en_US", &perr
, &status
);
1771 if (U_FAILURE(status
)) {
1772 log_err_status(status
, "unable to open ordinal -> %s\n", u_errorName(status
));
1776 status
= U_ZERO_ERROR
;
1777 formats
[3] = unum_open(UNUM_DURATION
, NULL
, 0, "en_US", &perr
, &status
);
1778 if (U_FAILURE(status
)) {
1779 log_err_status(status
, "unable to open duration %s\n", u_errorName(status
));
1783 status
= U_ZERO_ERROR
;
1787 "x.x: << point >>;\n"
1788 "zero; one; two; three; four; five; six; seven; eight; nine;\n"
1789 "ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n"
1790 "seventeen; eighteen; nineteen;\n"
1791 "20: twenty[->>];\n"
1792 "30: thirty[->>];\n"
1796 "70: seventy[->>];\n"
1797 "80: eighty[->>];\n"
1798 "90: ninety[->>];\n"
1800 u_uastrcpy(tempUChars
,
1803 "20: twenty[ and change];\n"
1804 "30: thirty[ and change];\n"
1805 "40: forty[ and change];\n"
1806 "50: fifty[ and change];\n"
1807 "60: sixty[ and change];\n"
1808 "70: seventy[ and change];\n"
1809 "80: eighty[ and change];\n"
1810 "90: ninety[ and change];\n"
1814 "x.x: << point something;\n"
1816 "20: some reasonable number;\n"
1817 "100: some substantial number;\n"
1818 "100,000,000: some huge number;\n");
1819 /* This is to get around some compiler warnings about char * string length. */
1820 u_strcat(pat
, tempUChars
);
1821 formats
[4] = unum_open(UNUM_PATTERN_RULEBASED
, pat
, -1, "en_US", &perr
, &status
);
1822 if (U_FAILURE(status
)) {
1823 log_err_status(status
, "unable to open rulebased pattern -> %s\n", u_errorName(status
));
1825 if (U_FAILURE(status
)) {
1826 log_err_status(status
, "Something failed with %s\n", u_errorName(status
));
1830 for (i
= 0; i
< COUNT
; ++i
) {
1831 log_verbose("\n\ntesting format %d\n", i
);
1832 test_fmt(formats
[i
], (UBool
)(i
== 0));
1835 #define FORMAT_BUF_CAPACITY 64
1837 UChar fmtbuf
[FORMAT_BUF_CAPACITY
];
1839 double nanvalue
= uprv_getNaN();
1840 status
= U_ZERO_ERROR
;
1841 len
= unum_formatDouble(formats
[1], nanvalue
, fmtbuf
, FORMAT_BUF_CAPACITY
, NULL
, &status
);
1842 if (U_FAILURE(status
)) {
1843 log_err_status(status
, "unum_formatDouble NAN failed with %s\n", u_errorName(status
));
1845 UChar nansym
[] = { 0x4E, 0x61, 0x4E, 0 }; /* NaN */
1846 if ( len
!= 3 || u_strcmp(fmtbuf
, nansym
) != 0 ) {
1847 log_err("unum_formatDouble NAN produced wrong answer for en_US\n");
1852 for (i
= 0; i
< COUNT
; ++i
) {
1853 unum_close(formats
[i
]);
1857 static void TestCurrencyRegression(void) {
1859 I've found a case where unum_parseDoubleCurrency is not doing what I
1860 expect. The value I pass in is $1234567890q123460000.00 and this
1861 returns with a status of zero error & a parse pos of 22 (I would
1862 expect a parse error at position 11).
1864 I stepped into DecimalFormat::subparse() and it looks like it parses
1865 the first 10 digits and then stops parsing at the q but doesn't set an
1866 error. Then later in DecimalFormat::parse() the value gets crammed
1867 into a long (which greatly truncates the value).
1869 This is very problematic for me 'cause I try to remove chars that are
1870 invalid but this allows my users to enter bad chars and truncates
1880 UErrorCode status
= U_ZERO_ERROR
;
1881 const int32_t expected
= 11;
1884 u_uastrcpy(buf
, "$1234567890q643210000.00");
1885 cur
= unum_open(UNUM_CURRENCY
, NULL
,0,"en_US", NULL
, &status
);
1887 if(U_FAILURE(status
)) {
1888 log_data_err("unum_open failed: %s (Are you missing data?)\n", u_errorName(status
));
1892 status
= U_ZERO_ERROR
; /* so we can test it later. */
1895 d
= unum_parseDoubleCurrency(cur
,
1898 &pos
, /* 0 = start */
1902 u_austrcpy(acurrency
, currency
);
1904 if(U_FAILURE(status
) || (pos
!= expected
)) {
1905 log_err("unum_parseDoubleCurrency should have failed with pos %d, but gave: value %.9f, err %s, pos=%d, currency [%s]\n",
1906 expected
, d
, u_errorName(status
), pos
, acurrency
);
1908 log_verbose("unum_parseDoubleCurrency failed, value %.9f err %s, pos %d, currency [%s]\n", d
, u_errorName(status
), pos
, acurrency
);
1914 static void TestTextAttributeCrash(void) {
1915 UChar ubuffer
[64] = {0x0049,0x004E,0x0052,0};
1916 static const UChar expectedNeg
[] = {0x0049,0x004E,0x0052,0x0031,0x0032,0x0033,0x0034,0x002E,0x0035,0};
1917 static const UChar expectedPos
[] = {0x0031,0x0032,0x0033,0x0034,0x002E,0x0035,0};
1919 UErrorCode status
= U_ZERO_ERROR
;
1920 UNumberFormat
*nf
= unum_open(UNUM_CURRENCY
, NULL
, 0, "en_US", NULL
, &status
);
1921 if (U_FAILURE(status
)) {
1922 log_data_err("FAILED 1 -> %s (Are you missing data?)\n", u_errorName(status
));
1925 unum_setTextAttribute(nf
, UNUM_CURRENCY_CODE
, ubuffer
, 3, &status
);
1927 * the usual negative prefix and suffix seem to be '($' and ')' at this point
1928 * also crashes if UNUM_NEGATIVE_SUFFIX is substituted for UNUM_NEGATIVE_PREFIX here
1930 used
= unum_getTextAttribute(nf
, UNUM_NEGATIVE_PREFIX
, ubuffer
, 64, &status
);
1931 unum_setTextAttribute(nf
, UNUM_NEGATIVE_PREFIX
, ubuffer
, used
, &status
);
1932 if (U_FAILURE(status
)) {
1933 log_err("FAILED 2\n"); exit(1);
1935 log_verbose("attempting to format...\n");
1936 used
= unum_formatDouble(nf
, -1234.5, ubuffer
, 64, NULL
, &status
);
1937 if (U_FAILURE(status
) || 64 < used
) {
1938 log_err("Failed formatting %s\n", u_errorName(status
));
1941 if (u_strcmp(expectedNeg
, ubuffer
) == 0) {
1942 log_err("Didn't get expected negative result\n");
1944 used
= unum_formatDouble(nf
, 1234.5, ubuffer
, 64, NULL
, &status
);
1945 if (U_FAILURE(status
) || 64 < used
) {
1946 log_err("Failed formatting %s\n", u_errorName(status
));
1949 if (u_strcmp(expectedPos
, ubuffer
) == 0) {
1950 log_err("Didn't get expected positive result\n");
1955 static void TestNBSPPatternRtNum(const char *testcase
, int line
, UNumberFormat
*nf
, double myNumber
) {
1956 UErrorCode status
= U_ZERO_ERROR
;
1959 double aNumber
= -1.0;
1960 unum_formatDouble(nf
, myNumber
, myString
, 20, NULL
, &status
);
1961 log_verbose("%s:%d: formatted %.2f into %s\n", testcase
, line
, myNumber
, u_austrcpy(tmpbuf
, myString
));
1962 if(U_FAILURE(status
)) {
1963 log_err("%s:%d: failed format of %.2g with %s\n", testcase
, line
, myNumber
, u_errorName(status
));
1966 aNumber
= unum_parse(nf
, myString
, -1, NULL
, &status
);
1967 if(U_FAILURE(status
)) {
1968 log_err("%s:%d: failed parse with %s\n", testcase
, line
, u_errorName(status
));
1971 if(uprv_fabs(aNumber
-myNumber
)>.001) {
1972 log_err("FAIL: %s:%d formatted %.2f, parsed into %.2f\n", testcase
, line
, myNumber
, aNumber
);
1974 log_verbose("PASS: %s:%d formatted %.2f, parsed into %.2f\n", testcase
, line
, myNumber
, aNumber
);
1978 static void TestNBSPPatternRT(const char *testcase
, UNumberFormat
*nf
) {
1979 TestNBSPPatternRtNum(testcase
, __LINE__
, nf
, 12345.);
1980 TestNBSPPatternRtNum(testcase
, __LINE__
, nf
, -12345.);
1983 static void TestNBSPInPattern(void) {
1984 UErrorCode status
= U_ZERO_ERROR
;
1985 UNumberFormat
* nf
= NULL
;
1986 const char *testcase
;
1989 testcase
="ar_AE UNUM_CURRENCY";
1990 nf
= unum_open(UNUM_CURRENCY
, NULL
, -1, "ar_AE", NULL
, &status
);
1991 if(U_FAILURE(status
) || nf
== NULL
) {
1992 log_data_err("%s:%d: %s: unum_open failed with %s (Are you missing data?)\n", __FILE__
, __LINE__
, testcase
, u_errorName(status
));
1995 TestNBSPPatternRT(testcase
, nf
);
1997 /* if we don't have CLDR 1.6 data, bring out the problem anyways */
1999 #define SPECIAL_PATTERN "\\u00A4\\u00A4'\\u062f.\\u0625.\\u200f\\u00a0'###0.00"
2001 testcase
= "ar_AE special pattern: " SPECIAL_PATTERN
;
2002 u_unescape(SPECIAL_PATTERN
, pat
, UPRV_LENGTHOF(pat
));
2003 unum_applyPattern(nf
, FALSE
, pat
, -1, NULL
, &status
);
2004 if(U_FAILURE(status
)) {
2005 log_err("%s: unum_applyPattern failed with %s\n", testcase
, u_errorName(status
));
2007 TestNBSPPatternRT(testcase
, nf
);
2009 #undef SPECIAL_PATTERN
2011 unum_close(nf
); status
= U_ZERO_ERROR
;
2013 testcase
="ar_AE UNUM_DECIMAL";
2014 nf
= unum_open(UNUM_DECIMAL
, NULL
, -1, "ar_AE", NULL
, &status
);
2015 if(U_FAILURE(status
)) {
2016 log_err("%s: unum_open failed with %s\n", testcase
, u_errorName(status
));
2018 TestNBSPPatternRT(testcase
, nf
);
2019 unum_close(nf
); status
= U_ZERO_ERROR
;
2021 testcase
="ar_AE UNUM_PERCENT";
2022 nf
= unum_open(UNUM_PERCENT
, NULL
, -1, "ar_AE", NULL
, &status
);
2023 if(U_FAILURE(status
)) {
2024 log_err("%s: unum_open failed with %s\n", testcase
, u_errorName(status
));
2026 TestNBSPPatternRT(testcase
, nf
);
2027 unum_close(nf
); status
= U_ZERO_ERROR
;
2032 static void TestCloneWithRBNF(void) {
2033 UChar pattern
[1024];
2035 UErrorCode status
= U_ZERO_ERROR
;
2037 UChar buffer_cloned
[256];
2040 UNumberFormat
*pform_cloned
;
2041 UNumberFormat
*pform
;
2045 "0.x: >%%millis-only>;\n"
2046 "x.0: <%%duration<;\n"
2047 "x.x: <%%durationwithmillis<>%%millis-added>;\n"
2048 "-x: ->>;%%millis-only:\n"
2049 "1000: 00:00.<%%millis<;\n"
2051 "1000: .<%%millis<;\n"
2055 "0: =%%seconds-only=;\n"
2056 "60: =%%min-sec=;\n"
2057 "3600: =%%hr-min-sec=;\n"
2058 "86400/86400: <%%ddaayyss<[, >>];\n"
2059 "%%durationwithmillis:\n"
2060 "0: =%%seconds-only=;\n"
2061 "60: =%%min-sec=;\n"
2062 "3600: =%%hr-min-sec=;\n"
2063 "86400/86400: <%%ddaayyss<, >>;\n");
2073 "3600/60: <0<:>>>;\n"
2079 /* This is to get around some compiler warnings about char * string length. */
2080 u_strcat(pattern
, pat2
);
2082 pform
= unum_open(UNUM_PATTERN_RULEBASED
, pattern
, -1, "en_US", NULL
, &status
);
2083 unum_formatDouble(pform
, 3600, buffer
, 256, NULL
, &status
);
2085 pform_cloned
= unum_clone(pform
,&status
);
2086 unum_formatDouble(pform_cloned
, 3600, buffer_cloned
, 256, NULL
, &status
);
2089 unum_close(pform_cloned
);
2091 if (u_strcmp(buffer
,buffer_cloned
)) {
2092 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
));
2097 static void TestNoExponent(void) {
2098 UErrorCode status
= U_ZERO_ERROR
;
2106 fmt
= unum_open(UNUM_DECIMAL
, NULL
, -1, "en_US", NULL
, &status
);
2108 if(U_FAILURE(status
) || fmt
== NULL
) {
2109 log_data_err("%s:%d: unum_open failed with %s (Are you missing data?)\n", __FILE__
, __LINE__
, u_errorName(status
));
2114 u_uastrcpy(str
, cstr
);
2117 num
= unum_parse(fmt
, str
, -1, &pos
, &status
);
2118 ASSERT_TRUE(pos
==4);
2119 if(U_FAILURE(status
)) {
2120 log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__
, __LINE__
, u_errorName(status
), cstr
);
2121 } else if(expect
!=num
) {
2122 log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__
, __LINE__
, num
, expect
, cstr
);
2124 log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__
, __LINE__
, num
, cstr
);
2127 ASSERT_TRUE(unum_getAttribute(fmt
, UNUM_PARSE_NO_EXPONENT
)==0);
2129 unum_setAttribute(fmt
, UNUM_PARSE_NO_EXPONENT
, 1); /* no error code */
2130 log_verbose("set UNUM_PARSE_NO_EXPONENT\n");
2132 ASSERT_TRUE(unum_getAttribute(fmt
, UNUM_PARSE_NO_EXPONENT
)==1);
2136 num
= unum_parse(fmt
, str
, -1, &pos
, &status
);
2138 log_err("%s:%d: FAIL: unum_parse should have returned 10, not 10000000 on %s after UNUM_PARSE_NO_EXPONENT\n", __FILE__
, __LINE__
, cstr
);
2139 } else if(num
==expect
) {
2140 log_verbose("%s:%d: unum_parse gave %d for %s - good.\n", __FILE__
, __LINE__
, num
, cstr
);
2142 ASSERT_TRUE(pos
==2);
2144 status
= U_ZERO_ERROR
;
2148 /* ok, now try scientific */
2149 fmt
= unum_open(UNUM_SCIENTIFIC
, NULL
, -1, "en_US", NULL
, &status
);
2150 assertSuccess("unum_open(UNUM_SCIENTIFIC, ...)", &status
);
2152 ASSERT_TRUE(unum_getAttribute(fmt
, UNUM_PARSE_NO_EXPONENT
)==0);
2155 u_uastrcpy(str
, cstr
);
2158 num
= unum_parse(fmt
, str
, -1, &pos
, &status
);
2159 ASSERT_TRUE(pos
==4);
2160 if(U_FAILURE(status
)) {
2161 log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__
, __LINE__
, u_errorName(status
), cstr
);
2162 } else if(expect
!=num
) {
2163 log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__
, __LINE__
, num
, expect
, cstr
);
2165 log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__
, __LINE__
, num
, cstr
);
2168 unum_setAttribute(fmt
, UNUM_PARSE_NO_EXPONENT
, 1); /* no error code */
2169 log_verbose("set UNUM_PARSE_NO_EXPONENT\n");
2171 ASSERT_TRUE(unum_getAttribute(fmt
, UNUM_PARSE_NO_EXPONENT
)==1);
2175 u_uastrcpy(str
, cstr
);
2178 num
= unum_parse(fmt
, str
, -1, &pos
, &status
);
2179 ASSERT_TRUE(pos
==4);
2180 if(U_FAILURE(status
)) {
2181 log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__
, __LINE__
, u_errorName(status
), cstr
);
2182 } else if(expect
!=num
) {
2183 log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__
, __LINE__
, num
, expect
, cstr
);
2185 log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__
, __LINE__
, num
, cstr
);
2191 static void TestMaxInt(void) {
2192 UErrorCode status
= U_ZERO_ERROR
;
2193 UChar pattern_hash
[] = { 0x23, 0x00 }; /* "#" */
2194 UChar result1
[1024] = { 0 }, result2
[1024] = { 0 };
2196 UChar expect
[] = { 0x0039, 0x0037, 0 };
2197 UNumberFormat
*fmt
= unum_open(
2198 UNUM_PATTERN_DECIMAL
, /* style */
2199 &pattern_hash
[0], /* pattern */
2200 u_strlen(pattern_hash
), /* patternLength */
2204 if(U_FAILURE(status
) || fmt
== NULL
) {
2205 log_data_err("%s:%d: %s: unum_open failed with %s (Are you missing data?)\n", __FILE__
, __LINE__
, "TestMaxInt", u_errorName(status
));
2209 unum_setAttribute(fmt
, UNUM_MAX_INTEGER_DIGITS
, 2);
2211 status
= U_ZERO_ERROR
;
2213 len1
= unum_formatInt64(fmt
, 1997, result1
, 1024, NULL
, &status
);
2215 if(U_FAILURE(status
) || u_strcmp(expect
, result1
)) {
2216 log_err("unum_formatInt64 Expected %s but got %s status %s\n", austrdup(expect
), austrdup(result1
), u_errorName(status
));
2219 status
= U_ZERO_ERROR
;
2221 len2
= unum_formatDouble(fmt
, 1997.0, result2
, 1024, NULL
, &status
);
2223 if(U_FAILURE(status
) || u_strcmp(expect
, result2
)) {
2224 log_err("unum_formatDouble Expected %s but got %s status %s\n", austrdup(expect
), austrdup(result2
), u_errorName(status
));
2229 /* test UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS */
2230 ASSERT_TRUE(unum_getAttribute(fmt
, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS
)==0);
2232 unum_setAttribute(fmt
, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS
, 1);
2233 /* test UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS */
2234 ASSERT_TRUE(unum_getAttribute(fmt
, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS
)==1);
2236 status
= U_ZERO_ERROR
;
2237 /* max int digits still '2' */
2238 len1
= unum_formatInt64(fmt
, 1997, result1
, 1024, NULL
, &status
);
2239 ASSERT_TRUE(status
==U_ILLEGAL_ARGUMENT_ERROR
);
2240 status
= U_ZERO_ERROR
;
2242 /* But, formatting 97->'97' works fine. */
2245 len1
= unum_formatInt64(fmt
, 97, result1
, 1024, NULL
, &status
);
2247 if(U_FAILURE(status
) || u_strcmp(expect
, result1
)) {
2248 log_err("unum_formatInt64 Expected %s but got %s status %s\n", austrdup(expect
), austrdup(result1
), u_errorName(status
));
2251 status
= U_ZERO_ERROR
;
2253 len2
= unum_formatDouble(fmt
, 97.0, result2
, 1024, NULL
, &status
);
2255 if(U_FAILURE(status
) || u_strcmp(expect
, result2
)) {
2256 log_err("unum_formatDouble Expected %s but got %s status %s\n", austrdup(expect
), austrdup(result2
), u_errorName(status
));
2263 static void TestUFormattable(void) {
2265 // simple test for API docs
2267 UErrorCode status
= U_ZERO_ERROR
;
2268 UNumberFormat
*unum
= unum_open(UNUM_DEFAULT
, NULL
, -1, "en_US_POSIX", NULL
, &status
);
2269 if(assertSuccessCheck("calling unum_open()", &status
, TRUE
)) {
2270 //! [unum_parseToUFormattable]
2271 const UChar str
[] = { 0x0031, 0x0032, 0x0033, 0x0000 }; /* 123 */
2273 UFormattable
*ufmt
= ufmt_open(&status
);
2274 unum_parseToUFormattable(unum
, ufmt
, str
, -1, NULL
, &status
);
2275 if (ufmt_isNumeric(ufmt
)) {
2276 result
= ufmt_getLong(ufmt
, &status
); /* == 123 */
2277 } /* else { ... } */
2279 //! [unum_parseToUFormattable]
2280 assertTrue("result == 123", (result
== 123));
2284 // test with explicitly created ufmt_open
2287 UErrorCode status
= U_ZERO_ERROR
;
2289 UNumberFormat
*unum
;
2290 const char *pattern
= "";
2292 ufmt
= ufmt_open(&status
);
2293 unum
= unum_open(UNUM_DEFAULT
, NULL
, -1, "en_US_POSIX", NULL
, &status
);
2294 if(assertSuccessCheck("calling ufmt_open() || unum_open()", &status
, TRUE
)) {
2297 log_verbose("-- pattern: %s\n", pattern
);
2298 u_uastrcpy(buffer
, pattern
);
2299 unum_parseToUFormattable(unum
, ufmt
, buffer
, -1, NULL
, &status
);
2300 if(assertSuccess("unum_parseToUFormattable(31337)", &status
)) {
2301 assertTrue("ufmt_getLong()=31337", ufmt_getLong(ufmt
, &status
) == 31337);
2302 assertTrue("ufmt_getType()=UFMT_LONG", ufmt_getType(ufmt
, &status
) == UFMT_LONG
);
2303 log_verbose("long = %d\n", ufmt_getLong(ufmt
, &status
));
2304 assertSuccess("ufmt_getLong()", &status
);
2306 unum_formatUFormattable(unum
, ufmt
, out2k
, 2048, NULL
, &status
);
2307 if(assertSuccess("unum_formatUFormattable(31337)", &status
)) {
2308 assertEquals("unum_formatUFormattable r/t", austrdup(buffer
), austrdup(out2k
));
2311 pattern
= "3.14159";
2312 log_verbose("-- pattern: %s\n", pattern
);
2313 u_uastrcpy(buffer
, pattern
);
2314 unum_parseToUFormattable(unum
, ufmt
, buffer
, -1, NULL
, &status
);
2315 if(assertSuccess("unum_parseToUFormattable(3.14159)", &status
)) {
2316 assertTrue("ufmt_getDouble()==3.14159", withinErr(ufmt_getDouble(ufmt
, &status
), 3.14159, 1e-15));
2317 assertSuccess("ufmt_getDouble()", &status
);
2318 assertTrue("ufmt_getType()=UFMT_DOUBLE", ufmt_getType(ufmt
, &status
) == UFMT_DOUBLE
);
2319 log_verbose("double = %g\n", ufmt_getDouble(ufmt
, &status
));
2321 unum_formatUFormattable(unum
, ufmt
, out2k
, 2048, NULL
, &status
);
2322 if(assertSuccess("unum_formatUFormattable(3.14159)", &status
)) {
2323 assertEquals("unum_formatUFormattable r/t", austrdup(buffer
), austrdup(out2k
));
2330 // test with auto-generated ufmt
2333 UErrorCode status
= U_ZERO_ERROR
;
2334 UFormattable
*ufmt
= NULL
;
2335 UNumberFormat
*unum
;
2336 const char *pattern
= "73476730924573500000000"; // weight of the moon, kg
2338 log_verbose("-- pattern: %s (testing auto-opened UFormattable)\n", pattern
);
2339 u_uastrcpy(buffer
, pattern
);
2341 unum
= unum_open(UNUM_DEFAULT
, NULL
, -1, "en_US_POSIX", NULL
, &status
);
2342 if(assertSuccessCheck("calling unum_open()", &status
, TRUE
)) {
2344 ufmt
= unum_parseToUFormattable(unum
, NULL
, /* will be ufmt_open()'ed for us */
2345 buffer
, -1, NULL
, &status
);
2346 if(assertSuccess("unum_parseToUFormattable(weight of the moon)", &status
)) {
2347 log_verbose("new formattable allocated at %p\n", (void*)ufmt
);
2348 assertTrue("ufmt_isNumeric() TRUE", ufmt_isNumeric(ufmt
));
2349 unum_formatUFormattable(unum
, ufmt
, out2k
, 2048, NULL
, &status
);
2350 if(assertSuccess("unum_formatUFormattable(3.14159)", &status
)) {
2351 assertEquals("unum_formatUFormattable r/t", austrdup(buffer
), austrdup(out2k
));
2354 log_verbose("double: %g\n", ufmt_getDouble(ufmt
, &status
));
2355 assertSuccess("ufmt_getDouble()", &status
);
2357 log_verbose("long: %ld\n", ufmt_getLong(ufmt
, &status
));
2358 assertTrue("failure on ufmt_getLong() for huge number:", U_FAILURE(status
));
2359 // status is now a failure due to ufmt_getLong() above.
2360 // the intltest does extensive r/t testing of Formattable vs. UFormattable.
2365 ufmt_close(ufmt
); // was implicitly opened for us by the first unum_parseToUFormattable()
2373 UBool isAlgorithmic
;
2374 const UChar
* description
;
2378 static const UChar latnDesc
[] = {0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039,0}; // 0123456789
2379 static const UChar romanDesc
[] = {0x25,0x72,0x6F,0x6D,0x61,0x6E,0x2D,0x75,0x70,0x70,0x65,0x72,0}; // %roman-upper
2380 static const UChar arabDesc
[] = {0x0660,0x0661,0x0662,0x0663,0x0664,0x0665,0x0666,0x0667,0x0668,0x0669,0}; //
2381 static const UChar arabextDesc
[] = {0x06F0,0x06F1,0x06F2,0x06F3,0x06F4,0x06F5,0x06F6,0x06F7,0x06F8,0x06F9,0}; //
2382 static const UChar hanidecDesc
[] = {0x3007,0x4E00,0x4E8C,0x4E09,0x56DB,0x4E94,0x516D,0x4E03,0x516B,0x4E5D,0}; //
2383 static const UChar hantDesc
[] = {0x7A,0x68,0x5F,0x48,0x61,0x6E,0x74,0x2F,0x53,0x70,0x65,0x6C,0x6C,0x6F,0x75,0x74,
2384 0x52,0x75,0x6C,0x65,0x73,0x2F,0x25,0x73,0x70,0x65,0x6C,0x6C,0x6F,0x75,0x74,0x2D,
2385 0x63,0x61,0x72,0x64,0x69,0x6E,0x61,0x6C,0}; // zh_Hant/SpelloutRules/%spellout-cardinal
2387 static const NumSysTestItem numSysTestItems
[] = {
2388 //locale numsys radix isAlgo description
2389 { "en", "latn", 10, FALSE
, latnDesc
},
2390 { "en@numbers=roman", "roman", 10, TRUE
, romanDesc
},
2391 { "en@numbers=finance", "latn", 10, FALSE
, latnDesc
},
2392 { "ar", "arab", 10, FALSE
, arabDesc
},
2393 { "fa", "arabext", 10, FALSE
, arabextDesc
},
2394 { "zh_Hans@numbers=hanidec", "hanidec", 10, FALSE
, hanidecDesc
},
2395 { "zh_Hant@numbers=traditional", "hant", 10, TRUE
, hantDesc
},
2396 { NULL
, NULL
, 0, FALSE
, NULL
},
2398 enum { kNumSysDescripBufMax
= 64 };
2400 static void TestUNumberingSystem(void) {
2401 const NumSysTestItem
* itemPtr
;
2402 UNumberingSystem
* unumsys
;
2403 UEnumeration
* uenum
;
2404 const char * numsys
;
2407 for (itemPtr
= numSysTestItems
; itemPtr
->locale
!= NULL
; itemPtr
++) {
2408 status
= U_ZERO_ERROR
;
2409 unumsys
= unumsys_open(itemPtr
->locale
, &status
);
2410 if ( U_SUCCESS(status
) ) {
2411 UChar ubuf
[kNumSysDescripBufMax
];
2412 int32_t ulen
, radix
= unumsys_getRadix(unumsys
);
2413 UBool isAlgorithmic
= unumsys_isAlgorithmic(unumsys
);
2414 numsys
= unumsys_getName(unumsys
);
2415 if ( uprv_strcmp(numsys
, itemPtr
->numsys
) != 0 || radix
!= itemPtr
->radix
|| !isAlgorithmic
!= !itemPtr
->isAlgorithmic
) {
2416 log_data_err("unumsys name/radix/isAlgorithmic for locale %s, expected %s/%d/%d, got %s/%d/%d\n",
2417 itemPtr
->locale
, itemPtr
->numsys
, itemPtr
->radix
, itemPtr
->isAlgorithmic
, numsys
, radix
, isAlgorithmic
);
2419 ulen
= unumsys_getDescription(unumsys
, ubuf
, kNumSysDescripBufMax
, &status
);
2420 (void)ulen
; // Suppress variable not used warning.
2421 if ( U_FAILURE(status
) || u_strcmp(ubuf
, itemPtr
->description
) != 0 ) {
2422 log_data_err("unumsys description for locale %s, description unexpected and/or status %\n", myErrorName(status
));
2424 unumsys_close(unumsys
);
2426 log_data_err("unumsys_open for locale %s fails with status %s\n", itemPtr
->locale
, myErrorName(status
));
2430 status
= U_ZERO_ERROR
;
2431 uenum
= unumsys_openAvailableNames(&status
);
2432 if ( U_SUCCESS(status
) ) {
2433 int32_t numsysCount
= 0;
2434 // sanity check for a couple of number systems that must be in the enumeration
2435 UBool foundLatn
= FALSE
;
2436 UBool foundArab
= FALSE
;
2437 while ( (numsys
= uenum_next(uenum
, NULL
, &status
)) != NULL
&& U_SUCCESS(status
) ) {
2438 status
= U_ZERO_ERROR
;
2439 unumsys
= unumsys_openByName(numsys
, &status
);
2440 if ( U_SUCCESS(status
) ) {
2442 if ( uprv_strcmp(numsys
, "latn") ) foundLatn
= TRUE
;
2443 if ( uprv_strcmp(numsys
, "arab") ) foundArab
= TRUE
;
2444 unumsys_close(unumsys
);
2446 log_err("unumsys_openAvailableNames includes %s but unumsys_openByName on it fails with status %s\n",
2447 numsys
, myErrorName(status
));
2451 if ( numsysCount
< 40 || !foundLatn
|| !foundArab
) {
2452 log_err("unumsys_openAvailableNames results incomplete: numsysCount %d, foundLatn %d, foundArab %d\n",
2453 numsysCount
, foundLatn
, foundArab
);
2456 log_data_err("unumsys_openAvailableNames fails with status %s\n", myErrorName(status
));
2460 /* plain-C version of test in numfmtst.cpp */
2461 enum { kUBufMax
= 64 };
2462 static void TestCurrencyIsoPluralFormat(void) {
2463 static const char* DATA
[][8] = {
2466 // currency amount to be formatted,
2467 // currency ISO code to be formatted,
2468 // format result using CURRENCYSTYLE,
2469 // format result using CURRENCY_STANDARD,
2470 // format result using CURRENCY_ACCOUNTING,
2471 // format result using ISOCURRENCYSTYLE,
2472 // format result using PLURALCURRENCYSTYLE,
2474 // locale amount ISOcode CURRENCYSTYLE CURRENCY_STANDARD CURRENCY_ACCOUNTING ISOCURRENCYSTYLE PLURALCURRENCYSTYLE
2475 {"en_US", "1", "USD", "$1.00", "$1.00", "$1.00", "USD1.00", "1.00 US dollars"},
2476 {"en_US", "1234.56", "USD", "$1,234.56", "$1,234.56", "$1,234.56", "USD1,234.56", "1,234.56 US dollars"},
2477 {"en_US@cf=account", "1234.56", "USD", "$1,234.56", "$1,234.56", "$1,234.56", "USD1,234.56", "1,234.56 US dollars"},
2478 {"en_US", "-1234.56", "USD", "-$1,234.56", "-$1,234.56", "($1,234.56)", "-USD1,234.56", "-1,234.56 US dollars"},
2479 {"en_US@cf=account", "-1234.56", "USD", "($1,234.56)", "-$1,234.56", "($1,234.56)", "-USD1,234.56", "-1,234.56 US dollars"},
2480 {"en_US@cf=standard", "-1234.56", "USD", "-$1,234.56", "-$1,234.56", "($1,234.56)", "-USD1,234.56", "-1,234.56 US dollars"},
2481 {"zh_CN", "1", "USD", "US$1.00", "US$1.00", "US$1.00", "USD1.00", "1.00\\u7F8E\\u5143"},
2482 {"zh_CN", "-1", "USD", "-US$1.00", "-US$1.00", "(US$1.00)", "-USD1.00", "-1.00\\u7F8E\\u5143"},
2483 {"zh_CN@cf=account", "-1", "USD", "(US$1.00)", "-US$1.00", "(US$1.00)", "-USD1.00", "-1.00\\u7F8E\\u5143"},
2484 {"zh_CN@cf=standard", "-1", "USD", "-US$1.00", "-US$1.00", "(US$1.00)", "-USD1.00", "-1.00\\u7F8E\\u5143"},
2485 {"zh_CN", "1234.56", "USD", "US$1,234.56", "US$1,234.56", "US$1,234.56", "USD1,234.56", "1,234.56\\u7F8E\\u5143"},
2486 // {"zh_CN", "1", "CHY", "CHY1.00", "CHY1.00", "CHY1.00", "CHY1.00", "1.00 CHY"}, // wrong ISO code
2487 // {"zh_CN", "1234.56", "CHY", "CHY1,234.56", "CHY1,234.56", "CHY1,234.56", "CHY1,234.56", "1,234.56 CHY"}, // wrong ISO code
2488 {"zh_CN", "1", "CNY", "\\u00A51.00", "\\u00A51.00", "\\u00A51.00", "CNY1.00", "1.00\\u4EBA\\u6C11\\u5E01"},
2489 {"zh_CN", "1234.56", "CNY", "\\u00A51,234.56", "\\u00A51,234.56", "\\u00A51,234.56", "CNY1,234.56", "1,234.56\\u4EBA\\u6C11\\u5E01"},
2490 {"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 "
2491 "\\u0440\\u0443\\u0431\\u043B\\u044F"},
2492 {"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 "
2493 "\\u0440\\u0443\\u0431\\u043B\\u044F"},
2494 {"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 "
2495 "\\u0440\\u0443\\u0431\\u043B\\u044F"},
2496 // test locale without currency information
2497 {"root", "-1.23", "USD", "-US$\\u00A01.23", "-US$\\u00A01.23", "-US$\\u00A01.23", "-USD\\u00A01.23", "-1.23 USD"},
2498 {"root@cf=account", "-1.23", "USD", "-US$\\u00A01.23", "-US$\\u00A01.23", "-US$\\u00A01.23", "-USD\\u00A01.23", "-1.23 USD"},
2499 // test choice format
2500 {"es_AR", "1", "INR", "INR\\u00A01,00", "INR\\u00A01,00", "INR\\u00A01,00", "INR\\u00A01,00", "1,00 rupia india"},
2502 static const UNumberFormatStyle currencyStyles
[] = {
2504 UNUM_CURRENCY_STANDARD
,
2505 UNUM_CURRENCY_ACCOUNTING
,
2507 UNUM_CURRENCY_PLURAL
2512 for (i
=0; i
<UPRV_LENGTHOF(DATA
); ++i
) {
2513 const char* localeString
= DATA
[i
][0];
2514 double numberToBeFormat
= atof(DATA
[i
][1]);
2515 const char* currencyISOCode
= DATA
[i
][2];
2516 for (sIndex
= 0; sIndex
< UPRV_LENGTHOF(currencyStyles
); ++sIndex
) {
2517 UNumberFormatStyle style
= currencyStyles
[sIndex
];
2518 UErrorCode status
= U_ZERO_ERROR
;
2519 UChar currencyCode
[4];
2520 UChar ubufResult
[kUBufMax
];
2521 UChar ubufExpected
[kUBufMax
];
2524 UNumberFormat
* unumFmt
= unum_open(style
, NULL
, 0, localeString
, NULL
, &status
);
2525 if (U_FAILURE(status
)) {
2526 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", localeString
, (int)style
, myErrorName(status
));
2529 u_charsToUChars(currencyISOCode
, currencyCode
, 4);
2530 unum_setTextAttribute(unumFmt
, UNUM_CURRENCY_CODE
, currencyCode
, 3, &status
);
2531 if (U_FAILURE(status
)) {
2532 log_err("FAIL: unum_setTextAttribute, locale %s, UNUM_CURRENCY_CODE %s\n", localeString
, currencyISOCode
);
2534 ulenRes
= unum_formatDouble(unumFmt
, numberToBeFormat
, ubufResult
, kUBufMax
, NULL
, &status
);
2535 if (U_FAILURE(status
)) {
2536 log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s - %s\n", localeString
, currencyISOCode
, myErrorName(status
));
2538 int32_t ulenExp
= u_unescape(DATA
[i
][3 + sIndex
], ubufExpected
, kUBufMax
);
2539 if (ulenRes
!= ulenExp
|| u_strncmp(ubufResult
, ubufExpected
, ulenExp
) != 0) {
2540 log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s, expected %s, got something else\n",
2541 localeString
, currencyISOCode
, DATA
[i
][3 + sIndex
]);
2544 unum_close(unumFmt
);
2550 const char * locale
;
2551 UNumberFormatStyle style
;
2552 UDisplayContext context
;
2553 const char * expectedResult
;
2556 /* currently no locales have contextTransforms data for "symbol" type */
2557 static const TestContextItem tcItems
[] = { /* results for 123.45 */
2558 { "sv", UNUM_SPELLOUT
, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE
, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" },
2559 { "sv", UNUM_SPELLOUT
, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE
, "Ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" },
2560 { "sv", UNUM_SPELLOUT
, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU
, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" },
2561 { "sv", UNUM_SPELLOUT
, UDISPCTX_CAPITALIZATION_FOR_STANDALONE
, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" },
2562 { "en", UNUM_SPELLOUT
, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE
, "one hundred twenty-three point four five" },
2563 { "en", UNUM_SPELLOUT
, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE
, "One hundred twenty-three point four five" },
2564 { "en", UNUM_SPELLOUT
, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU
, "One hundred twenty-three point four five" },
2565 { "en", UNUM_SPELLOUT
, UDISPCTX_CAPITALIZATION_FOR_STANDALONE
, "One hundred twenty-three point four five" },
2566 { NULL
, (UNumberFormatStyle
)0, (UDisplayContext
)0, NULL
}
2569 static void TestContext(void) {
2570 UErrorCode status
= U_ZERO_ERROR
;
2571 const TestContextItem
* itemPtr
;
2573 UNumberFormat
*unum
= unum_open(UNUM_SPELLOUT
, NULL
, 0, "en", NULL
, &status
);
2574 if ( U_SUCCESS(status
) ) {
2575 UDisplayContext context
= unum_getContext(unum
, UDISPCTX_TYPE_CAPITALIZATION
, &status
);
2576 if ( U_FAILURE(status
) || context
!= UDISPCTX_CAPITALIZATION_NONE
) {
2577 log_err("FAIL: Initial unum_getContext is not UDISPCTX_CAPITALIZATION_NONE\n");
2578 status
= U_ZERO_ERROR
;
2580 unum_setContext(unum
, UDISPCTX_CAPITALIZATION_FOR_STANDALONE
, &status
);
2581 context
= unum_getContext(unum
, UDISPCTX_TYPE_CAPITALIZATION
, &status
);
2582 if ( U_FAILURE(status
) || context
!= UDISPCTX_CAPITALIZATION_FOR_STANDALONE
) {
2583 log_err("FAIL: unum_getContext does not return the value set, UDISPCTX_CAPITALIZATION_FOR_STANDALONE\n");
2587 log_data_err("unum_open UNUM_SPELLOUT for en fails with status %s\n", myErrorName(status
));
2589 #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_BREAK_ITERATION
2590 for (itemPtr
= tcItems
; itemPtr
->locale
!= NULL
; itemPtr
++) {
2591 UChar ubufResult
[kUBufMax
];
2594 status
= U_ZERO_ERROR
;
2595 unum
= unum_open(itemPtr
->style
, NULL
, 0, itemPtr
->locale
, NULL
, &status
);
2596 if (U_FAILURE(status
)) {
2597 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n",
2598 itemPtr
->locale
, (int)itemPtr
->style
, myErrorName(status
));
2601 unum_setContext(unum
, itemPtr
->context
, &status
);
2602 ulenRes
= unum_formatDouble(unum
, 123.45, ubufResult
, kUBufMax
, NULL
, &status
);
2603 if (U_FAILURE(status
)) {
2604 log_err("FAIL: unum_formatDouble, locale %s, style %d, context %d - %s\n",
2605 itemPtr
->locale
, (int)itemPtr
->style
, (int)itemPtr
->context
, myErrorName(status
));
2607 UChar ubufExpected
[kUBufMax
];
2608 int32_t ulenExp
= u_unescape(itemPtr
->expectedResult
, ubufExpected
, kUBufMax
);
2609 if (ulenRes
!= ulenExp
|| u_strncmp(ubufResult
, ubufExpected
, ulenExp
) != 0) {
2610 char bbuf
[kUBufMax
*2];
2611 u_austrncpy(bbuf
, ubufResult
, sizeof(bbuf
));
2612 log_err("FAIL: unum_formatDouble, locale %s, style %d, context %d, expected %d:\"%s\", got %d:\"%s\"\n",
2613 itemPtr
->locale
, (int)itemPtr
->style
, (int)itemPtr
->context
, ulenExp
,
2614 itemPtr
->expectedResult
, ulenRes
, bbuf
);
2619 #endif /* #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_BREAK_ITERATION */
2622 static void TestCurrencyUsage(void) {
2623 static const char* DATA
[][2] = {
2625 * currency ISO code to be formatted,
2626 * format result using CURRENCYSTYLE with CASH purpose,-
2627 * Note that as of CLDR 26:-
2628 * - TWD switches from 0 decimals to 2; PKR still has 0, so change test to that
2629 * - CAD rounds to .05
2633 {"CAD", "CA$123.55"},
2637 // 1st time for getter/setter, 2nd for factory method
2640 const char* localeString
= "en_US";
2641 double numberToBeFormat
= 123.567;
2642 UNumberFormat
* unumFmt
;
2643 UNumberFormatStyle style
= UNUM_CURRENCY
;
2644 UErrorCode status
= U_ZERO_ERROR
;
2647 if(i
== 1){ // change for factory method
2648 style
= UNUM_CASH_CURRENCY
;
2651 unumFmt
= unum_open(style
, NULL
, 0, localeString
, NULL
, &status
);
2652 if (U_FAILURE(status
)) {
2653 log_data_err("FAIL: unum_open, locale %s, style %d - %s\n",
2654 localeString
, (int)style
, myErrorName(status
));
2658 if(i
== 0){ // this is for the getter/setter
2659 if(unum_getAttribute(unumFmt
, UNUM_CURRENCY_USAGE
) != UCURR_USAGE_STANDARD
) {
2660 log_err("FAIL: currency usage attribute is not UNUM_CURRENCY_STANDARD\n");
2663 unum_setAttribute(unumFmt
, UNUM_CURRENCY_USAGE
, UCURR_USAGE_CASH
);
2666 if(unum_getAttribute(unumFmt
, UNUM_CURRENCY_USAGE
) != UCURR_USAGE_CASH
) {
2667 log_err("FAIL: currency usage attribute is not UNUM_CURRENCY_CASH\n");
2670 for (j
=0; j
<UPRV_LENGTHOF(DATA
); ++j
) {
2673 UChar currencyCode
[4];
2676 UFieldPosition pos
= {0};
2678 u_charsToUChars(DATA
[j
][0], currencyCode
, 3);
2679 expectLen
= u_unescape(DATA
[j
][1], expect
, UPRV_LENGTHOF(expect
));
2681 unum_setTextAttribute(unumFmt
, UNUM_CURRENCY_CODE
, currencyCode
, 3, &status
);
2682 assertSuccess("num_setTextAttribute()", &status
);
2684 resultLen
= unum_formatDouble(unumFmt
, numberToBeFormat
, result
, UPRV_LENGTHOF(result
),
2686 assertSuccess("num_formatDouble()", &status
);
2688 if(resultLen
!= expectLen
|| u_strcmp(result
, expect
) != 0) {
2689 log_err("Fail: Error in Number Format Currency Purpose using unum_setAttribute() expected: %s, got %s\n",
2690 aescstrdup(expect
, expectLen
), aescstrdup(result
, resultLen
));
2695 unum_close(unumFmt
);
2699 static UChar currFmtNegSameAsPos
[] = /* "\u00A4#,##0.00;\u00A4#,##0.00" */
2700 {0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0x3B,0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0};
2702 static UChar currFmtToPatExpected
[] = /* "\u00A4#,##0.00" */
2703 {0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0};
2705 static UChar currFmtResultExpected
[] = /* "$100.00" */
2706 {0x24,0x31,0x30,0x30,0x2E,0x30,0x30,0};
2708 static UChar emptyString
[] = {0};
2710 enum { kUBufSize
= 64, kBBufSize
= 128 };
2712 static void TestCurrFmtNegSameAsPositive(void) {
2713 UErrorCode status
= U_ZERO_ERROR
;
2714 UNumberFormat
* unumfmt
= unum_open(UNUM_CURRENCY
, NULL
, 0, "en_US", NULL
, &status
);
2715 if ( U_SUCCESS(status
) ) {
2716 unum_applyPattern(unumfmt
, FALSE
, currFmtNegSameAsPos
, -1, NULL
, &status
);
2717 if (U_SUCCESS(status
)) {
2718 UChar ubuf
[kUBufSize
];
2719 int32_t ulen
= unum_toPattern(unumfmt
, FALSE
, ubuf
, kUBufSize
, &status
);
2720 if (U_FAILURE(status
)) {
2721 log_err("unum_toPattern fails with status %s\n", myErrorName(status
));
2722 } else if (u_strcmp(ubuf
, currFmtToPatExpected
) != 0) {
2723 log_err("unum_toPattern result wrong, expected %s, got %s\n", aescstrdup(currFmtToPatExpected
,-1), aescstrdup(ubuf
,ulen
));
2725 unum_setSymbol(unumfmt
, UNUM_MINUS_SIGN_SYMBOL
, emptyString
, 0, &status
);
2726 if (U_SUCCESS(status
)) {
2727 ulen
= unum_formatDouble(unumfmt
, -100.0, ubuf
, kUBufSize
, NULL
, &status
);
2728 if (U_FAILURE(status
)) {
2729 log_err("unum_formatDouble fails with status %s\n", myErrorName(status
));
2730 } else if (u_strcmp(ubuf
, currFmtResultExpected
) != 0) {
2731 log_err("unum_formatDouble result wrong, expected %s, got %s\n", aescstrdup(currFmtResultExpected
,-1), aescstrdup(ubuf
,ulen
));
2734 log_err("unum_setSymbol fails with status %s\n", myErrorName(status
));
2737 log_err("unum_applyPattern fails with status %s\n", myErrorName(status
));
2739 unum_close(unumfmt
);
2741 log_data_err("unum_open UNUM_CURRENCY for en_US fails with status %s\n", myErrorName(status
));
2748 const char *expected
;
2749 } ValueAndExpectedString
;
2751 static const ValueAndExpectedString enShort
[] = {
2758 {1234567.0, "1.23M"},
2759 {12345678.0, "12.3M"},
2760 {123456789.0, "123M"},
2761 {1.23456789E9
, "1.23B"},
2762 {1.23456789E10
, "12.3B"},
2763 {1.23456789E11
, "123B"},
2764 {1.23456789E12
, "1.23T"},
2765 {1.23456789E13
, "12.3T"},
2766 {1.23456789E14
, "123T"},
2767 {1.23456789E15
, "1230T"},
2771 static const ValueAndExpectedString enShortMax2
[] = {
2778 {1234567.0, "1.2M"},
2779 {12345678.0, "12M"},
2780 {123456789.0, "120M"},
2781 {1.23456789E9
, "1.2B"},
2782 {1.23456789E10
, "12B"},
2783 {1.23456789E11
, "120B"},
2784 {1.23456789E12
, "1.2T"},
2785 {1.23456789E13
, "12T"},
2786 {1.23456789E14
, "120T"},
2787 {1.23456789E15
, "1200T"},
2791 static const ValueAndExpectedString enShortMax5
[] = {
2796 {12345.0, "12.345K"},
2797 {123456.0, "123.46K"},
2798 {1234567.0, "1.2346M"},
2799 {12345678.0, "12.346M"},
2800 {123456789.0, "123.46M"},
2801 {1.23456789E9
, "1.2346B"},
2802 {1.23456789E10
, "12.346B"},
2803 {1.23456789E11
, "123.46B"},
2804 {1.23456789E12
, "1.2346T"},
2805 {1.23456789E13
, "12.346T"},
2806 {1.23456789E14
, "123.46T"},
2807 {1.23456789E15
, "1234.6T"},
2811 static const ValueAndExpectedString enShortMin3
[] = {
2818 {1234567.0, "1.23M"},
2819 {12345678.0, "12.3M"},
2820 {123456789.0, "123M"},
2821 {1.23456789E9
, "1.23B"},
2822 {1.23456789E10
, "12.3B"},
2823 {1.23456789E11
, "123B"},
2824 {1.23456789E12
, "1.23T"},
2825 {1.23456789E13
, "12.3T"},
2826 {1.23456789E14
, "123T"},
2827 {1.23456789E15
, "1230T"},
2831 static const ValueAndExpectedString jaShortMax2
[] = {
2833 {12345.0, "1.2\\u4E07"},
2834 {123456.0, "12\\u4E07"},
2835 {1234567.0, "120\\u4E07"},
2836 {12345678.0, "1200\\u4E07"},
2837 {123456789.0, "1.2\\u5104"},
2838 {1.23456789E9
, "12\\u5104"},
2839 {1.23456789E10
, "120\\u5104"},
2840 {1.23456789E11
, "1200\\u5104"},
2841 {1.23456789E12
, "1.2\\u5146"},
2842 {1.23456789E13
, "12\\u5146"},
2843 {1.23456789E14
, "120\\u5146"},
2847 static const ValueAndExpectedString srLongMax2
[] = {
2848 {1234.0, "1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few
2849 {12345.0, "12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other
2850 {21789.0, "22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few
2851 {123456.0, "120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other
2852 {999999.0, "1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"}, // 10^6 one
2853 {1234567.0, "1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 few
2854 {12345678.0, "12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 other
2855 {123456789.0, "120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 other
2856 {1.23456789E9
, "1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, // 10^9 few
2857 {1.23456789E10
, "12 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, // 10^9 other
2858 {2.08901234E10
, "21 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0430"}, // 10^9 one
2859 {2.18901234E10
, "22 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, // 10^9 few
2860 {1.23456789E11
, "120 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, // 10^9 other
2861 {-1234.0, "-1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"},
2862 {-12345.0, "-12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"},
2863 {-21789.0, "-22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"},
2864 {-123456.0, "-120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"},
2865 {-999999.0, "-1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"},
2866 {-1234567.0, "-1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"},
2867 {-12345678.0, "-12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"},
2868 {-123456789.0, "-120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"},
2869 {-1.23456789E9
, "-1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"},
2870 {-1.23456789E10
, "-12 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"},
2871 {-2.08901234E10
, "-21 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0430"},
2872 {-2.18901234E10
, "-22 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"},
2873 {-1.23456789E11
, "-120 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"},
2878 const char * locale
;
2879 UNumberFormatStyle style
;
2880 int32_t attribute
; // UNumberFormatAttribute, or -1 for none
2881 int32_t attrValue
; //
2882 const ValueAndExpectedString
* veItems
;
2883 } LocStyleAttributeTest
;
2885 static const LocStyleAttributeTest lsaTests
[] = {
2886 { "en", UNUM_DECIMAL_COMPACT_SHORT
, -1, 0, enShort
},
2887 { "en", UNUM_DECIMAL_COMPACT_SHORT
, UNUM_MAX_SIGNIFICANT_DIGITS
, 2, enShortMax2
},
2888 { "en", UNUM_DECIMAL_COMPACT_SHORT
, UNUM_MAX_SIGNIFICANT_DIGITS
, 5, enShortMax5
},
2889 { "en", UNUM_DECIMAL_COMPACT_SHORT
, UNUM_MIN_SIGNIFICANT_DIGITS
, 3, enShortMin3
},
2890 { "ja", UNUM_DECIMAL_COMPACT_SHORT
, UNUM_MAX_SIGNIFICANT_DIGITS
, 2, jaShortMax2
},
2891 { "sr", UNUM_DECIMAL_COMPACT_LONG
, UNUM_MAX_SIGNIFICANT_DIGITS
, 2, srLongMax2
},
2892 { NULL
, (UNumberFormatStyle
)0, -1, 0, NULL
}
2895 static void TestVariousStylesAndAttributes(void) {
2896 const LocStyleAttributeTest
* lsaTestPtr
;
2897 for (lsaTestPtr
= lsaTests
; lsaTestPtr
->locale
!= NULL
; lsaTestPtr
++) {
2898 UErrorCode status
= U_ZERO_ERROR
;
2899 UNumberFormat
* unum
= unum_open(lsaTestPtr
->style
, NULL
, 0, lsaTestPtr
->locale
, NULL
, &status
);
2900 if ( U_FAILURE(status
) ) {
2901 log_data_err("FAIL: unum_open style %d, locale %s: error %s\n", (int)lsaTestPtr
->style
, lsaTestPtr
->locale
, u_errorName(status
));
2903 const ValueAndExpectedString
* veItemPtr
;
2904 if (lsaTestPtr
->attribute
>= 0) {
2905 unum_setAttribute(unum
, (UNumberFormatAttribute
)lsaTestPtr
->attribute
, lsaTestPtr
->attrValue
);
2907 for (veItemPtr
= lsaTestPtr
->veItems
; veItemPtr
->expected
!= NULL
; veItemPtr
++) {
2908 UChar uexp
[kUBufSize
];
2909 UChar uget
[kUBufSize
];
2910 int32_t uexplen
, ugetlen
;
2912 status
= U_ZERO_ERROR
;
2913 uexplen
= u_unescape(veItemPtr
->expected
, uexp
, kUBufSize
);
2914 ugetlen
= unum_formatDouble(unum
, veItemPtr
->value
, uget
, kUBufSize
, NULL
, &status
);
2915 if ( U_FAILURE(status
) ) {
2916 log_err("FAIL: unum_formatDouble style %d, locale %s, attr %d, value %.2f: error %s\n",
2917 (int)lsaTestPtr
->style
, lsaTestPtr
->locale
, lsaTestPtr
->attribute
, veItemPtr
->value
, u_errorName(status
));
2918 } else if (ugetlen
!= uexplen
|| u_strncmp(uget
, uexp
, uexplen
) != 0) {
2919 char bexp
[kBBufSize
];
2920 char bget
[kBBufSize
];
2921 u_strToUTF8(bexp
, kBBufSize
, NULL
, uexp
, uexplen
, &status
);
2922 u_strToUTF8(bget
, kBBufSize
, NULL
, uget
, ugetlen
, &status
);
2923 log_err("FAIL: unum_formatDouble style %d, locale %s, attr %d, value %.2f: expect \"%s\", get \"%s\"\n",
2924 (int)lsaTestPtr
->style
, lsaTestPtr
->locale
, lsaTestPtr
->attribute
, veItemPtr
->value
, bexp
, bget
);
2932 static const UChar currpat
[] = { 0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0}; /* ¤#,##0.00 */
2933 static const UChar parsetxt
[] = { 0x78,0x30,0x79,0x24,0 }; /* x0y$ */
2935 static void TestParseCurrPatternWithDecStyle() {
2936 UErrorCode status
= U_ZERO_ERROR
;
2937 UNumberFormat
*unumfmt
= unum_open(UNUM_DECIMAL
, NULL
, 0, "en_US", NULL
, &status
);
2938 if (U_FAILURE(status
)) {
2939 log_data_err("unum_open DECIMAL failed for en_US: %s (Are you missing data?)\n", u_errorName(status
));
2941 unum_applyPattern(unumfmt
, FALSE
, currpat
, -1, NULL
, &status
);
2942 if (U_FAILURE(status
)) {
2943 log_err_status(status
, "unum_applyPattern failed: %s\n", u_errorName(status
));
2946 double value
= unum_parseDouble(unumfmt
, parsetxt
, -1, &pos
, &status
);
2947 if (U_SUCCESS(status
)) {
2948 log_err_status(status
, "unum_parseDouble expected to fail but got status %s, value %f\n", u_errorName(status
), value
);
2951 unum_close(unumfmt
);
2957 * Test unum_formatDoubleForFields (and UFieldPositionIterator)
2967 const char * locale
;
2968 UNumberFormatStyle style
;
2970 const FieldsData
* expectedFields
;
2971 } FormatForFieldsItem
;
2973 static const UChar patNoFields
[] = { 0x0027, 0x0078, 0x0027, 0 }; /* "'x'", for UNUM_PATTERN_DECIMAL */
2976 /* "en_US", UNUM_CURRENCY, 123456.0 : "¤#,##0.00" => "$123,456.00" */
2977 static const FieldsData fields_en_CURR
[] = {
2978 { UNUM_CURRENCY_FIELD
/*7*/, 0, 1 },
2979 { UNUM_GROUPING_SEPARATOR_FIELD
/*6*/, 4, 5 },
2980 { UNUM_INTEGER_FIELD
/*0*/, 1, 8 },
2981 { UNUM_DECIMAL_SEPARATOR_FIELD
/*2*/, 8, 9 },
2982 { UNUM_FRACTION_FIELD
/*1*/, 9, 11 },
2985 /* "en_US", UNUM_PERCENT, -34 : "#,##0%" => "-34%" */
2986 static const FieldsData fields_en_PRCT
[] = {
2987 { UNUM_SIGN_FIELD
/*10*/, 0, 1 },
2988 { UNUM_INTEGER_FIELD
/*0*/, 1, 3 },
2989 { UNUM_PERCENT_FIELD
/*8*/, 3, 4 },
2992 /* "fr_FR", UNUM_CURRENCY, 123456.0 : "#,##0.00 ¤" => "123,456.00 €" */
2993 static const FieldsData fields_fr_CURR
[] = {
2994 { UNUM_GROUPING_SEPARATOR_FIELD
/*6*/, 3, 4 },
2995 { UNUM_INTEGER_FIELD
/*0*/, 0, 7 },
2996 { UNUM_DECIMAL_SEPARATOR_FIELD
/*2*/, 7, 8 },
2997 { UNUM_FRACTION_FIELD
/*1*/, 8, 10 },
2998 { UNUM_CURRENCY_FIELD
/*7*/, 11, 12 },
3001 /* "en_US", UNUM_PATTERN_DECIMAL, 12.0 : "'x'" => "x12" */
3002 static const FieldsData fields_en_PATN
[] = {
3003 { UNUM_INTEGER_FIELD
/*0*/, 1, 3 },
3007 static const FormatForFieldsItem fffItems
[] = {
3008 { "en_US", UNUM_CURRENCY_STANDARD
, 123456.0, fields_en_CURR
},
3009 { "en_US", UNUM_PERCENT
, -0.34, fields_en_PRCT
},
3010 { "fr_FR", UNUM_CURRENCY_STANDARD
, 123456.0, fields_fr_CURR
},
3011 { "en_US", UNUM_PATTERN_DECIMAL
, 12.0, fields_en_PATN
},
3012 { NULL
, (UNumberFormatStyle
)0, 0, NULL
},
3015 static void TestFormatForFields(void) {
3016 UErrorCode status
= U_ZERO_ERROR
;
3017 UFieldPositionIterator
* fpositer
= ufieldpositer_open(&status
);
3018 if ( U_FAILURE(status
) ) {
3019 log_err("ufieldpositer_open fails, status %s\n", u_errorName(status
));
3021 const FormatForFieldsItem
* itemPtr
;
3022 for (itemPtr
= fffItems
; itemPtr
->locale
!= NULL
; itemPtr
++) {
3023 UNumberFormat
* unum
;
3024 status
= U_ZERO_ERROR
;
3025 unum
= (itemPtr
->style
== UNUM_PATTERN_DECIMAL
)?
3026 unum_open(itemPtr
->style
, patNoFields
, -1, itemPtr
->locale
, NULL
, &status
):
3027 unum_open(itemPtr
->style
, NULL
, 0, itemPtr
->locale
, NULL
, &status
);
3028 if ( U_FAILURE(status
) ) {
3029 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
));
3031 UChar ubuf
[kUBufSize
];
3032 int32_t ulen
= unum_formatDoubleForFields(unum
, itemPtr
->value
, ubuf
, kUBufSize
, fpositer
, &status
);
3033 if ( U_FAILURE(status
) ) {
3034 log_err("unum_formatDoubleForFields fails for locale %s, style %d: status %s\n", itemPtr
->locale
, itemPtr
->style
, u_errorName(status
));
3036 const FieldsData
* fptr
;
3037 int32_t field
, beginPos
, endPos
;
3038 for (fptr
= itemPtr
->expectedFields
; TRUE
; fptr
++) {
3039 field
= ufieldpositer_next(fpositer
, &beginPos
, &endPos
);
3040 if (field
!= fptr
->field
|| (field
>= 0 && (beginPos
!= fptr
->beginPos
|| endPos
!= fptr
->endPos
))) {
3041 if (fptr
->field
>= 0) {
3042 log_err("unum_formatDoubleForFields for locale %s as \"%s\"; expect field %d range %d-%d, get field %d range %d-%d\n",
3043 itemPtr
->locale
, aescstrdup(ubuf
, ulen
), fptr
->field
, fptr
->beginPos
, fptr
->endPos
, field
, beginPos
, endPos
);
3045 log_err("unum_formatDoubleForFields for locale %s as \"%s\"; expect field < 0, get field %d range %d-%d\n",
3046 itemPtr
->locale
, aescstrdup(ubuf
, ulen
), field
, beginPos
, endPos
);
3058 ufieldpositer_close(fpositer
);
3062 #endif /* #if !UCONFIG_NO_FORMATTING */