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