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