]>
Commit | Line | Data |
---|---|---|
1 | // © 2016 and later: Unicode, Inc. and others. | |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
3 | /******************************************************************** | |
4 | * COPYRIGHT: | |
5 | * Copyright (c) 1997-2016, International Business Machines Corporation and | |
6 | * others. All Rights Reserved. | |
7 | ********************************************************************/ | |
8 | /******************************************************************************** | |
9 | * | |
10 | * File CNUMTST.C | |
11 | * | |
12 | * Madhu Katragadda Creation | |
13 | * | |
14 | * Modification History: | |
15 | * | |
16 | * Date Name Description | |
17 | * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes | |
18 | * 07/15/99 helena Ported to HPUX 10/11 CC. | |
19 | ********************************************************************************* | |
20 | */ | |
21 | ||
22 | /* C API TEST FOR NUMBER FORMAT */ | |
23 | ||
24 | #include "unicode/utypes.h" | |
25 | ||
26 | #if !UCONFIG_NO_FORMATTING | |
27 | ||
28 | #include "unicode/uloc.h" | |
29 | #include "unicode/umisc.h" | |
30 | #include "unicode/unum.h" | |
31 | #include "unicode/unumsys.h" | |
32 | #include "unicode/ustring.h" | |
33 | #include "unicode/udisplaycontext.h" | |
34 | #include "unicode/uchar.h" | |
35 | #include "unicode/ures.h" // TAKE ME OUT!!! | |
36 | ||
37 | #include "cintltst.h" | |
38 | #include "cnumtst.h" | |
39 | #include "cmemory.h" | |
40 | #include "cstring.h" | |
41 | #include "putilimp.h" | |
42 | #include "uassert.h" | |
43 | #include <stdio.h> | |
44 | #include <stdlib.h> | |
45 | ||
46 | #define APPLE_ADDITIONS 1 | |
47 | ||
48 | static const char *tagAssert(const char *f, int32_t l, const char *msg) { | |
49 | static char _fileline[1000]; | |
50 | sprintf(_fileline, "%s:%d: ASSERT_TRUE(%s)", f, l, msg); | |
51 | return _fileline; | |
52 | } | |
53 | ||
54 | #define ASSERT_TRUE(x) assertTrue(tagAssert(__FILE__, __LINE__, #x), (x)) | |
55 | ||
56 | void addNumForTest(TestNode** root); | |
57 | static void TestTextAttributeCrash(void); | |
58 | static void TestNBSPInPattern(void); | |
59 | static void TestInt64Parse(void); | |
60 | static void TestParseCurrency(void); | |
61 | static void TestMaxInt(void); | |
62 | static void TestNoExponent(void); | |
63 | static void TestSignAlwaysShown(void); | |
64 | static void TestMinimumGroupingDigits(void); | |
65 | static void TestParseCaseSensitive(void); | |
66 | static void TestUFormattable(void); | |
67 | static void TestUNumberingSystem(void); | |
68 | static void TestCurrencyIsoPluralFormat(void); | |
69 | static void TestContext(void); | |
70 | static void TestCurrencyUsage(void); | |
71 | static void TestCurrFmtNegSameAsPositive(void); | |
72 | static void TestVariousStylesAndAttributes(void); | |
73 | static void TestParseCurrPatternWithDecStyle(void); | |
74 | static void TestFormatForFields(void); | |
75 | static void TestRBNFRounding(void); | |
76 | static void Test12052_NullPointer(void); | |
77 | static void TestParseCases(void); | |
78 | static void TestSetMaxFracAndRoundIncr(void); | |
79 | static void TestIgnorePadding(void); | |
80 | static void TestSciNotationMaxFracCap(void); | |
81 | ||
82 | static void TestParseAltNum(void); | |
83 | static void TestParseCurrPatternWithDecStyle(void); | |
84 | static void TestFormatPrecision(void); | |
85 | static void TestSetSigDigAndRoundIncr(void); // Apple <rdar://problem/52538227> | |
86 | static void TestSetAffixOnCurrFmt(void); // Apple <rdar://problem/46755430> | |
87 | static void TestParseWithEmptyCurr(void); // Apple <rdar://problem/46915356> | |
88 | static void TestSciNotationNumbers(void); // Apple <rdar://problem/50113359> | |
89 | static void TestSciNotationPrecision(void); // Apple <rdar://problem/51601250> | |
90 | static void TestMinimumGrouping(void); // Apple <rdar://problem/49808819> | |
91 | static void TestNumberSystemsMultiplier(void); // Apple <rdar://problem/49120648> | |
92 | static void TestParseScientific(void); // Apple <rdar://problem/39156484> | |
93 | static void TestCurrForUnkRegion(void); // Apple <rdar://problem/51985640> | |
94 | static void TestMinIntMinFracZero(void); // Apple <rdar://problem/54569257> | |
95 | #if APPLE_ADDITIONS | |
96 | static void TestFormatDecPerf(void); // Apple <rdar://problem/51672521> | |
97 | static void TestCountryFallback(void); // Apple <rdar://problem/54886964> | |
98 | #endif | |
99 | ||
100 | #define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x) | |
101 | ||
102 | void addNumForTest(TestNode** root) | |
103 | { | |
104 | TESTCASE(TestNumberFormat); | |
105 | TESTCASE(TestSpelloutNumberParse); | |
106 | TESTCASE(TestSignificantDigits); | |
107 | TESTCASE(TestSigDigRounding); | |
108 | TESTCASE(TestNumberFormatPadding); | |
109 | TESTCASE(TestInt64Format); | |
110 | TESTCASE(TestNonExistentCurrency); | |
111 | TESTCASE(TestCurrencyRegression); | |
112 | TESTCASE(TestTextAttributeCrash); | |
113 | TESTCASE(TestRBNFFormat); | |
114 | TESTCASE(TestRBNFRounding); | |
115 | TESTCASE(TestNBSPInPattern); | |
116 | TESTCASE(TestInt64Parse); | |
117 | TESTCASE(TestParseZero); | |
118 | TESTCASE(TestParseCurrency); | |
119 | TESTCASE(TestCloneWithRBNF); | |
120 | TESTCASE(TestMaxInt); | |
121 | TESTCASE(TestNoExponent); | |
122 | TESTCASE(TestSignAlwaysShown); | |
123 | TESTCASE(TestMinimumGroupingDigits); | |
124 | TESTCASE(TestParseCaseSensitive); | |
125 | TESTCASE(TestUFormattable); | |
126 | TESTCASE(TestUNumberingSystem); | |
127 | TESTCASE(TestCurrencyIsoPluralFormat); | |
128 | TESTCASE(TestContext); | |
129 | TESTCASE(TestCurrencyUsage); | |
130 | TESTCASE(TestCurrFmtNegSameAsPositive); | |
131 | TESTCASE(TestVariousStylesAndAttributes); | |
132 | TESTCASE(TestParseCurrPatternWithDecStyle); | |
133 | TESTCASE(TestFormatForFields); | |
134 | TESTCASE(Test12052_NullPointer); | |
135 | TESTCASE(TestParseCases); | |
136 | TESTCASE(TestSetMaxFracAndRoundIncr); | |
137 | TESTCASE(TestIgnorePadding); | |
138 | TESTCASE(TestSciNotationMaxFracCap); | |
139 | ||
140 | TESTCASE(TestParseAltNum); | |
141 | TESTCASE(TestParseCurrPatternWithDecStyle); | |
142 | TESTCASE(TestFormatPrecision); | |
143 | TESTCASE(TestSetSigDigAndRoundIncr); // Apple <rdar://problem/52538227> | |
144 | TESTCASE(TestSetAffixOnCurrFmt); // Apple <rdar://problem/46755430> | |
145 | TESTCASE(TestParseWithEmptyCurr); // Apple <rdar://problem/46915356> | |
146 | TESTCASE(TestSciNotationNumbers); // Apple <rdar://problem/50113359> | |
147 | TESTCASE(TestSciNotationPrecision); // Apple <rdar://problem/51601250> | |
148 | TESTCASE(TestMinimumGrouping); // Apple <rdar://problem/49808819> | |
149 | TESTCASE(TestNumberSystemsMultiplier); // Apple <rdar://problem/49120648> | |
150 | TESTCASE(TestParseScientific); // Apple <rdar://problem/39156484> | |
151 | TESTCASE(TestCurrForUnkRegion); // Apple <rdar://problem/51985640> | |
152 | TESTCASE(TestMinIntMinFracZero); // Apple <rdar://problem/54569257> | |
153 | #if APPLE_ADDITIONS | |
154 | TESTCASE(TestFormatDecPerf); // Apple <rdar://problem/51672521> | |
155 | TESTCASE(TestCountryFallback); // Apple <rdar://problem/51672521> | |
156 | #endif | |
157 | } | |
158 | ||
159 | /* test Parse int 64 */ | |
160 | ||
161 | static void TestInt64Parse() | |
162 | { | |
163 | ||
164 | UErrorCode st = U_ZERO_ERROR; | |
165 | UErrorCode* status = &st; | |
166 | ||
167 | const char* st1 = "009223372036854775808"; | |
168 | const int size = 21; | |
169 | UChar text[21]; | |
170 | ||
171 | ||
172 | UNumberFormat* nf; | |
173 | ||
174 | int64_t a; | |
175 | ||
176 | u_charsToUChars(st1, text, size); | |
177 | nf = unum_open(UNUM_DEFAULT, NULL, -1, NULL, NULL, status); | |
178 | ||
179 | if(U_FAILURE(*status)) | |
180 | { | |
181 | log_data_err("Error in unum_open() %s \n", myErrorName(*status)); | |
182 | return; | |
183 | } | |
184 | ||
185 | log_verbose("About to test unum_parseInt64() with out of range number\n"); | |
186 | ||
187 | a = unum_parseInt64(nf, text, size, 0, status); | |
188 | (void)a; /* Suppress set but not used warning. */ | |
189 | ||
190 | ||
191 | if(!U_FAILURE(*status)) | |
192 | { | |
193 | log_err("Error in unum_parseInt64(): %s \n", myErrorName(*status)); | |
194 | } | |
195 | else | |
196 | { | |
197 | log_verbose("unum_parseInt64() successful\n"); | |
198 | } | |
199 | ||
200 | unum_close(nf); | |
201 | return; | |
202 | } | |
203 | ||
204 | /* test Number Format API */ | |
205 | static void TestNumberFormat() | |
206 | { | |
207 | UChar *result=NULL; | |
208 | UChar temp1[512]; | |
209 | UChar temp2[512]; | |
210 | ||
211 | UChar temp[5]; | |
212 | ||
213 | UChar prefix[5]; | |
214 | UChar suffix[5]; | |
215 | UChar symbol[20]; | |
216 | int32_t resultlength; | |
217 | int32_t resultlengthneeded; | |
218 | int32_t parsepos; | |
219 | double d1 = -1.0; | |
220 | int32_t l1; | |
221 | double d = -10456.37; | |
222 | double a = 1234.56, a1 = 1235.0; | |
223 | int32_t l = 100000000; | |
224 | UFieldPosition pos1; | |
225 | UFieldPosition pos2; | |
226 | int32_t numlocales; | |
227 | int32_t i; | |
228 | ||
229 | UNumberFormatAttribute attr; | |
230 | UNumberFormatSymbol symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; | |
231 | int32_t newvalue; | |
232 | UErrorCode status=U_ZERO_ERROR; | |
233 | UNumberFormatStyle style= UNUM_DEFAULT; | |
234 | UNumberFormat *pattern; | |
235 | UNumberFormat *def, *fr, *cur_def, *cur_fr, *per_def, *per_fr, | |
236 | *cur_frpattern, *myclone, *spellout_def; | |
237 | ||
238 | /* Testing unum_open() with various Numberformat styles and locales*/ | |
239 | status = U_ZERO_ERROR; | |
240 | log_verbose("Testing unum_open() with default style and locale\n"); | |
241 | def=unum_open(style, NULL,0,NULL, NULL,&status); | |
242 | ||
243 | /* Might as well pack it in now if we can't even get a default NumberFormat... */ | |
244 | if(U_FAILURE(status)) | |
245 | { | |
246 | log_data_err("Error in creating default NumberFormat using unum_open(): %s (Are you missing data?)\n", myErrorName(status)); | |
247 | return; | |
248 | } | |
249 | ||
250 | log_verbose("\nTesting unum_open() with french locale and default style(decimal)\n"); | |
251 | fr=unum_open(style,NULL,0, "fr_FR",NULL, &status); | |
252 | if(U_FAILURE(status)) | |
253 | log_err("Error: could not create NumberFormat (french): %s\n", myErrorName(status)); | |
254 | ||
255 | log_verbose("\nTesting unum_open(currency,NULL,status)\n"); | |
256 | style=UNUM_CURRENCY; | |
257 | /* Can't hardcode the result to assume the default locale is "en_US". */ | |
258 | cur_def=unum_open(style, NULL,0,"en_US", NULL, &status); | |
259 | if(U_FAILURE(status)) | |
260 | log_err("Error: could not create NumberFormat using \n unum_open(currency, NULL, &status) %s\n", | |
261 | myErrorName(status) ); | |
262 | ||
263 | log_verbose("\nTesting unum_open(currency, frenchlocale, status)\n"); | |
264 | cur_fr=unum_open(style,NULL,0, "fr_FR", NULL, &status); | |
265 | if(U_FAILURE(status)) | |
266 | log_err("Error: could not create NumberFormat using unum_open(currency, french, &status): %s\n", | |
267 | myErrorName(status)); | |
268 | ||
269 | log_verbose("\nTesting unum_open(percent, NULL, status)\n"); | |
270 | style=UNUM_PERCENT; | |
271 | per_def=unum_open(style,NULL,0, NULL,NULL, &status); | |
272 | if(U_FAILURE(status)) | |
273 | log_err("Error: could not create NumberFormat using unum_open(percent, NULL, &status): %s\n", myErrorName(status)); | |
274 | ||
275 | log_verbose("\nTesting unum_open(percent,frenchlocale, status)\n"); | |
276 | per_fr=unum_open(style, NULL,0,"fr_FR", NULL,&status); | |
277 | if(U_FAILURE(status)) | |
278 | log_err("Error: could not create NumberFormat using unum_open(percent, french, &status): %s\n", myErrorName(status)); | |
279 | ||
280 | log_verbose("\nTesting unum_open(spellout, NULL, status)"); | |
281 | style=UNUM_SPELLOUT; | |
282 | spellout_def=unum_open(style, NULL, 0, "en_US", NULL, &status); | |
283 | if(U_FAILURE(status)) | |
284 | log_err("Error: could not create NumberFormat using unum_open(spellout, NULL, &status): %s\n", myErrorName(status)); | |
285 | ||
286 | /* Testing unum_clone(..) */ | |
287 | log_verbose("\nTesting unum_clone(fmt, status)"); | |
288 | status = U_ZERO_ERROR; | |
289 | myclone = unum_clone(def,&status); | |
290 | if(U_FAILURE(status)) | |
291 | log_err("Error: could not clone unum_clone(def, &status): %s\n", myErrorName(status)); | |
292 | else | |
293 | { | |
294 | log_verbose("unum_clone() successful\n"); | |
295 | } | |
296 | ||
297 | /*Testing unum_getAvailable() and unum_countAvailable()*/ | |
298 | log_verbose("\nTesting getAvailableLocales and countAvailable()\n"); | |
299 | numlocales=unum_countAvailable(); | |
300 | if(numlocales < 0) | |
301 | log_err("error in countAvailable"); | |
302 | else{ | |
303 | log_verbose("unum_countAvialable() successful\n"); | |
304 | log_verbose("The no: of locales where number formattting is applicable is %d\n", numlocales); | |
305 | } | |
306 | for(i=0;i<numlocales;i++) | |
307 | { | |
308 | log_verbose("%s\n", unum_getAvailable(i)); | |
309 | if (unum_getAvailable(i) == 0) | |
310 | log_err("No locale for which number formatting patterns are applicable\n"); | |
311 | else | |
312 | log_verbose("A locale %s for which number formatting patterns are applicable\n",unum_getAvailable(i)); | |
313 | } | |
314 | ||
315 | ||
316 | /*Testing unum_format() and unum_formatdouble()*/ | |
317 | u_uastrcpy(temp1, "$100,000,000.00"); | |
318 | ||
319 | log_verbose("\nTesting unum_format() \n"); | |
320 | resultlength=0; | |
321 | pos1.field = UNUM_INTEGER_FIELD; | |
322 | resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status); | |
323 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
324 | { | |
325 | status=U_ZERO_ERROR; | |
326 | resultlength=resultlengthneeded+1; | |
327 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
328 | /* for (i = 0; i < 100000; i++) */ | |
329 | { | |
330 | unum_format(cur_def, l, result, resultlength, &pos1, &status); | |
331 | } | |
332 | } | |
333 | ||
334 | if(U_FAILURE(status)) | |
335 | { | |
336 | log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) ); | |
337 | } | |
338 | if(u_strcmp(result, temp1)==0) | |
339 | log_verbose("Pass: Number formatting using unum_format() successful\n"); | |
340 | else | |
341 | log_err("Fail: Error in number Formatting using unum_format()\n"); | |
342 | if(pos1.beginIndex == 1 && pos1.endIndex == 12) | |
343 | log_verbose("Pass: Complete number formatting using unum_format() successful\n"); | |
344 | else | |
345 | log_err("Fail: Error in complete number Formatting using unum_format()\nGot: b=%d end=%d\nExpected: b=1 end=12\n", | |
346 | pos1.beginIndex, pos1.endIndex); | |
347 | ||
348 | free(result); | |
349 | result = 0; | |
350 | ||
351 | log_verbose("\nTesting unum_formatDouble()\n"); | |
352 | u_uastrcpy(temp1, "-$10,456.37"); | |
353 | resultlength=0; | |
354 | pos2.field = UNUM_FRACTION_FIELD; | |
355 | resultlengthneeded=unum_formatDouble(cur_def, d, NULL, resultlength, &pos2, &status); | |
356 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
357 | { | |
358 | status=U_ZERO_ERROR; | |
359 | resultlength=resultlengthneeded+1; | |
360 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
361 | /* for (i = 0; i < 100000; i++) */ | |
362 | { | |
363 | unum_formatDouble(cur_def, d, result, resultlength, &pos2, &status); | |
364 | } | |
365 | } | |
366 | if(U_FAILURE(status)) | |
367 | { | |
368 | log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status)); | |
369 | } | |
370 | if(result && u_strcmp(result, temp1)==0) | |
371 | log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n"); | |
372 | else { | |
373 | log_err("FAIL: Error in number formatting using unum_formatDouble() - got '%s' expected '%s'\n", | |
374 | aescstrdup(result, -1), aescstrdup(temp1, -1)); | |
375 | } | |
376 | if(pos2.beginIndex == 9 && pos2.endIndex == 11) | |
377 | log_verbose("Pass: Complete number formatting using unum_format() successful\n"); | |
378 | else | |
379 | log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=9 end=11", | |
380 | pos1.beginIndex, pos1.endIndex); | |
381 | ||
382 | ||
383 | /* Testing unum_parse() and unum_parseDouble() */ | |
384 | log_verbose("\nTesting unum_parseDouble()\n"); | |
385 | /* for (i = 0; i < 100000; i++)*/ | |
386 | parsepos=0; | |
387 | if (result != NULL) { | |
388 | d1=unum_parseDouble(cur_def, result, u_strlen(result), &parsepos, &status); | |
389 | } else { | |
390 | log_err("result is NULL\n"); | |
391 | } | |
392 | if(U_FAILURE(status)) { | |
393 | log_err("parse of '%s' failed. Parsepos=%d. The error is : %s\n", aescstrdup(result,u_strlen(result)),parsepos, myErrorName(status)); | |
394 | } | |
395 | ||
396 | if(d1!=d) | |
397 | log_err("Fail: Error in parsing\n"); | |
398 | else | |
399 | log_verbose("Pass: parsing successful\n"); | |
400 | if (result) | |
401 | free(result); | |
402 | result = 0; | |
403 | ||
404 | status = U_ZERO_ERROR; | |
405 | /* Testing unum_formatDoubleCurrency / unum_parseDoubleCurrency */ | |
406 | log_verbose("\nTesting unum_formatDoubleCurrency\n"); | |
407 | u_uastrcpy(temp1, "Y1,235"); | |
408 | temp1[0] = 0xA5; /* Yen sign */ | |
409 | u_uastrcpy(temp, "JPY"); | |
410 | resultlength=0; | |
411 | pos2.field = UNUM_INTEGER_FIELD; | |
412 | resultlengthneeded=unum_formatDoubleCurrency(cur_def, a, temp, NULL, resultlength, &pos2, &status); | |
413 | if (status==U_BUFFER_OVERFLOW_ERROR) { | |
414 | status=U_ZERO_ERROR; | |
415 | resultlength=resultlengthneeded+1; | |
416 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
417 | unum_formatDoubleCurrency(cur_def, a, temp, result, resultlength, &pos2, &status); | |
418 | } | |
419 | if (U_FAILURE(status)) { | |
420 | log_err("Error in formatting using unum_formatDoubleCurrency(.....): %s\n", myErrorName(status)); | |
421 | } | |
422 | if (result && u_strcmp(result, temp1)==0) { | |
423 | log_verbose("Pass: Number Formatting using unum_formatDoubleCurrency() Successful\n"); | |
424 | } else { | |
425 | log_err("FAIL: Error in number formatting using unum_formatDoubleCurrency() - got '%s' expected '%s'\n", | |
426 | aescstrdup(result, -1), aescstrdup(temp1, -1)); | |
427 | } | |
428 | if (pos2.beginIndex == 1 && pos2.endIndex == 6) { | |
429 | log_verbose("Pass: Complete number formatting using unum_format() successful\n"); | |
430 | } else { | |
431 | log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=1 end=6\n", | |
432 | pos1.beginIndex, pos1.endIndex); | |
433 | } | |
434 | ||
435 | log_verbose("\nTesting unum_parseDoubleCurrency\n"); | |
436 | parsepos=0; | |
437 | if (result == NULL) { | |
438 | log_err("result is NULL\n"); | |
439 | } | |
440 | else { | |
441 | d1=unum_parseDoubleCurrency(cur_def, result, u_strlen(result), &parsepos, temp2, &status); | |
442 | if (U_FAILURE(status)) { | |
443 | log_err("parseDoubleCurrency '%s' failed. The error is : %s\n", aescstrdup(result, u_strlen(result)), myErrorName(status)); | |
444 | } | |
445 | /* Note: a==1234.56, but on parse expect a1=1235.0 */ | |
446 | if (d1!=a1) { | |
447 | log_err("Fail: Error in parsing currency, got %f, expected %f\n", d1, a1); | |
448 | } else { | |
449 | log_verbose("Pass: parsed currency amount successfully\n"); | |
450 | } | |
451 | if (u_strcmp(temp2, temp)==0) { | |
452 | log_verbose("Pass: parsed correct currency\n"); | |
453 | } else { | |
454 | log_err("Fail: parsed incorrect currency\n"); | |
455 | } | |
456 | } | |
457 | status = U_ZERO_ERROR; /* reset */ | |
458 | ||
459 | free(result); | |
460 | result = 0; | |
461 | ||
462 | ||
463 | /* performance testing */ | |
464 | u_uastrcpy(temp1, "$462.12345"); | |
465 | resultlength=u_strlen(temp1); | |
466 | /* for (i = 0; i < 100000; i++) */ | |
467 | { | |
468 | parsepos=0; | |
469 | d1=unum_parseDouble(cur_def, temp1, resultlength, &parsepos, &status); | |
470 | } | |
471 | if(U_FAILURE(status)) | |
472 | { | |
473 | log_err("parseDouble('%s') failed. The error is : %s\n", aescstrdup(temp1, resultlength), myErrorName(status)); | |
474 | } | |
475 | ||
476 | /* | |
477 | * Note: "for strict standard conformance all operations and constants are now supposed to be | |
478 | evaluated in precision of long double". So, we assign a1 before comparing to a double. Bug #7932. | |
479 | */ | |
480 | a1 = 462.12345; | |
481 | ||
482 | if(d1!=a1) | |
483 | log_err("Fail: Error in parsing\n"); | |
484 | else | |
485 | log_verbose("Pass: parsing successful\n"); | |
486 | ||
487 | free(result); | |
488 | ||
489 | u_uastrcpy(temp1, "($10,456.3E1])"); | |
490 | parsepos=0; | |
491 | d1=unum_parseDouble(cur_def, temp1, u_strlen(temp1), &parsepos, &status); | |
492 | if(U_SUCCESS(status)) | |
493 | { | |
494 | log_err("Error in unum_parseDouble(..., %s, ...): %s\n", temp1, myErrorName(status)); | |
495 | } | |
496 | ||
497 | ||
498 | log_verbose("\nTesting unum_format()\n"); | |
499 | status=U_ZERO_ERROR; | |
500 | resultlength=0; | |
501 | parsepos=0; | |
502 | resultlengthneeded=unum_format(per_fr, l, NULL, resultlength, &pos1, &status); | |
503 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
504 | { | |
505 | status=U_ZERO_ERROR; | |
506 | resultlength=resultlengthneeded+1; | |
507 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
508 | /* for (i = 0; i < 100000; i++)*/ | |
509 | { | |
510 | unum_format(per_fr, l, result, resultlength, &pos1, &status); | |
511 | } | |
512 | } | |
513 | if(U_FAILURE(status)) | |
514 | { | |
515 | log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status)); | |
516 | } | |
517 | ||
518 | ||
519 | log_verbose("\nTesting unum_parse()\n"); | |
520 | /* for (i = 0; i < 100000; i++) */ | |
521 | { | |
522 | parsepos=0; | |
523 | l1=unum_parse(per_fr, result, u_strlen(result), &parsepos, &status); | |
524 | } | |
525 | if(U_FAILURE(status)) | |
526 | { | |
527 | log_err("parse failed. The error is : %s\n", myErrorName(status)); | |
528 | } | |
529 | ||
530 | if(l1!=l) | |
531 | log_err("Fail: Error in parsing\n"); | |
532 | else | |
533 | log_verbose("Pass: parsing successful\n"); | |
534 | ||
535 | free(result); | |
536 | ||
537 | /* create a number format using unum_openPattern(....)*/ | |
538 | log_verbose("\nTesting unum_openPattern()\n"); | |
539 | u_uastrcpy(temp1, "#,##0.0#;(#,##0.0#)"); | |
540 | pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), NULL, NULL,&status); | |
541 | if(U_FAILURE(status)) | |
542 | { | |
543 | log_err("error in unum_openPattern(): %s\n", myErrorName(status) ); | |
544 | } | |
545 | else | |
546 | log_verbose("Pass: unum_openPattern() works fine\n"); | |
547 | ||
548 | /*test for unum_toPattern()*/ | |
549 | log_verbose("\nTesting unum_toPattern()\n"); | |
550 | resultlength=0; | |
551 | resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &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_toPattern(pattern, FALSE, result, resultlength, &status); | |
558 | } | |
559 | if(U_FAILURE(status)) | |
560 | { | |
561 | log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status)); | |
562 | } | |
563 | else | |
564 | { | |
565 | if(u_strcmp(result, temp1)!=0) | |
566 | log_err("FAIL: Error in extracting the pattern using unum_toPattern()\n"); | |
567 | else | |
568 | log_verbose("Pass: extracted the pattern correctly using unum_toPattern()\n"); | |
569 | free(result); | |
570 | } | |
571 | ||
572 | /*Testing unum_getSymbols() and unum_setSymbols()*/ | |
573 | log_verbose("\nTesting unum_getSymbols and unum_setSymbols()\n"); | |
574 | /*when we try to change the symbols of french to default we need to apply the pattern as well to fetch correct results */ | |
575 | resultlength=0; | |
576 | resultlengthneeded=unum_toPattern(cur_def, FALSE, NULL, resultlength, &status); | |
577 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
578 | { | |
579 | status=U_ZERO_ERROR; | |
580 | resultlength=resultlengthneeded+1; | |
581 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
582 | unum_toPattern(cur_def, FALSE, result, resultlength, &status); | |
583 | } | |
584 | if(U_FAILURE(status)) | |
585 | { | |
586 | log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status)); | |
587 | } | |
588 | ||
589 | status=U_ZERO_ERROR; | |
590 | cur_frpattern=unum_open(UNUM_IGNORE,result, u_strlen(result), "fr_FR",NULL, &status); | |
591 | if(U_FAILURE(status)) | |
592 | { | |
593 | log_err("error in unum_openPattern(): %s\n", myErrorName(status)); | |
594 | } | |
595 | ||
596 | free(result); | |
597 | ||
598 | /*getting the symbols of cur_def */ | |
599 | /*set the symbols of cur_frpattern to cur_def */ | |
600 | for (symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; symType < UNUM_FORMAT_SYMBOL_COUNT; symType++) { | |
601 | status=U_ZERO_ERROR; | |
602 | unum_getSymbol(cur_def, symType, temp1, sizeof(temp1), &status); | |
603 | unum_setSymbol(cur_frpattern, symType, temp1, -1, &status); | |
604 | if(U_FAILURE(status)) | |
605 | { | |
606 | log_err("Error in get/set symbols: %s\n", myErrorName(status)); | |
607 | } | |
608 | } | |
609 | ||
610 | /*format to check the result */ | |
611 | resultlength=0; | |
612 | resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status); | |
613 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
614 | { | |
615 | status=U_ZERO_ERROR; | |
616 | resultlength=resultlengthneeded+1; | |
617 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
618 | unum_format(cur_def, l, result, resultlength, &pos1, &status); | |
619 | } | |
620 | if(U_FAILURE(status)) | |
621 | { | |
622 | log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status)); | |
623 | } | |
624 | ||
625 | if(U_FAILURE(status)){ | |
626 | log_err("Fail: error in unum_setSymbols: %s\n", myErrorName(status)); | |
627 | } | |
628 | unum_applyPattern(cur_frpattern, FALSE, result, u_strlen(result),NULL,NULL); | |
629 | ||
630 | for (symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; symType < UNUM_FORMAT_SYMBOL_COUNT; symType++) { | |
631 | status=U_ZERO_ERROR; | |
632 | unum_getSymbol(cur_def, symType, temp1, sizeof(temp1), &status); | |
633 | unum_getSymbol(cur_frpattern, symType, temp2, sizeof(temp2), &status); | |
634 | if(U_FAILURE(status) || u_strcmp(temp1, temp2) != 0) | |
635 | { | |
636 | log_err("Fail: error in getting symbols\n"); | |
637 | } | |
638 | else | |
639 | log_verbose("Pass: get and set symbols successful\n"); | |
640 | } | |
641 | ||
642 | /*format and check with the previous result */ | |
643 | ||
644 | resultlength=0; | |
645 | resultlengthneeded=unum_format(cur_frpattern, l, NULL, resultlength, &pos1, &status); | |
646 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
647 | { | |
648 | status=U_ZERO_ERROR; | |
649 | resultlength=resultlengthneeded+1; | |
650 | unum_format(cur_frpattern, l, temp1, resultlength, &pos1, &status); | |
651 | } | |
652 | if(U_FAILURE(status)) | |
653 | { | |
654 | log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status)); | |
655 | } | |
656 | /* TODO: | |
657 | * This test fails because we have not called unum_applyPattern(). | |
658 | * Currently, such an applyPattern() does not exist on the C API, and | |
659 | * we have jitterbug 411 for it. | |
660 | * Since it is close to the 1.5 release, I (markus) am disabling this test just | |
661 | * for this release (I added the test itself only last week). | |
662 | * For the next release, we need to fix this. | |
663 | * Then, remove the uprv_strcmp("1.5", ...) and this comment, and the include "cstring.h" at the beginning of this file. | |
664 | */ | |
665 | if(u_strcmp(result, temp1) != 0) { | |
666 | log_err("Formatting failed after setting symbols. result=%s temp1=%s\n", result, temp1); | |
667 | } | |
668 | ||
669 | ||
670 | /*----------- */ | |
671 | ||
672 | free(result); | |
673 | ||
674 | /* Testing unum_get/setSymbol() */ | |
675 | for(i = 0; i < UNUM_FORMAT_SYMBOL_COUNT; ++i) { | |
676 | symbol[0] = (UChar)(0x41 + i); | |
677 | symbol[1] = (UChar)(0x61 + i); | |
678 | unum_setSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, 2, &status); | |
679 | if(U_FAILURE(status)) { | |
680 | log_err("Error from unum_setSymbol(%d): %s\n", i, myErrorName(status)); | |
681 | return; | |
682 | } | |
683 | } | |
684 | for(i = 0; i < UNUM_FORMAT_SYMBOL_COUNT; ++i) { | |
685 | resultlength = unum_getSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, UPRV_LENGTHOF(symbol), &status); | |
686 | if(U_FAILURE(status)) { | |
687 | log_err("Error from unum_getSymbol(%d): %s\n", i, myErrorName(status)); | |
688 | return; | |
689 | } | |
690 | if(resultlength != 2 || symbol[0] != 0x41 + i || symbol[1] != 0x61 + i) { | |
691 | log_err("Failure in unum_getSymbol(%d): got unexpected symbol\n", i); | |
692 | } | |
693 | } | |
694 | /*try getting from a bogus symbol*/ | |
695 | unum_getSymbol(cur_frpattern, (UNumberFormatSymbol)i, symbol, UPRV_LENGTHOF(symbol), &status); | |
696 | if(U_SUCCESS(status)){ | |
697 | log_err("Error : Expected U_ILLEGAL_ARGUMENT_ERROR for bogus symbol"); | |
698 | } | |
699 | if(U_FAILURE(status)){ | |
700 | if(status != U_ILLEGAL_ARGUMENT_ERROR){ | |
701 | log_err("Error: Expected U_ILLEGAL_ARGUMENT_ERROR for bogus symbol, Got %s\n", myErrorName(status)); | |
702 | } | |
703 | } | |
704 | status=U_ZERO_ERROR; | |
705 | ||
706 | /* Testing unum_getTextAttribute() and unum_setTextAttribute()*/ | |
707 | log_verbose("\nTesting getting and setting text attributes\n"); | |
708 | resultlength=5; | |
709 | unum_getTextAttribute(cur_fr, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status); | |
710 | if(U_FAILURE(status)) | |
711 | { | |
712 | log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status)); | |
713 | } | |
714 | unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status); | |
715 | if(U_FAILURE(status)) | |
716 | { | |
717 | log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status)); | |
718 | } | |
719 | unum_getTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, suffix, resultlength, &status); | |
720 | if(U_FAILURE(status)) | |
721 | { | |
722 | log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status)); | |
723 | } | |
724 | if(u_strcmp(suffix,temp)!=0) | |
725 | log_err("Fail:Error in setTextAttribute or getTextAttribute in setting and getting suffix\n"); | |
726 | else | |
727 | log_verbose("Pass: setting and getting suffix works fine\n"); | |
728 | /*set it back to normal */ | |
729 | u_uastrcpy(temp,"$"); | |
730 | unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status); | |
731 | ||
732 | /*checking some more text setter conditions */ | |
733 | u_uastrcpy(prefix, "+"); | |
734 | unum_setTextAttribute(def, UNUM_POSITIVE_PREFIX, prefix, u_strlen(prefix) , &status); | |
735 | if(U_FAILURE(status)) | |
736 | { | |
737 | log_err("error in setting the text attributes : %s\n", myErrorName(status)); | |
738 | } | |
739 | unum_getTextAttribute(def, UNUM_POSITIVE_PREFIX, temp, resultlength, &status); | |
740 | if(U_FAILURE(status)) | |
741 | { | |
742 | log_err("error in getting the text attributes : %s\n", myErrorName(status)); | |
743 | } | |
744 | ||
745 | if(u_strcmp(prefix, temp)!=0) | |
746 | log_err("ERROR: get and setTextAttributes with positive prefix failed\n"); | |
747 | else | |
748 | log_verbose("Pass: get and setTextAttributes with positive prefix works fine\n"); | |
749 | ||
750 | u_uastrcpy(prefix, "+"); | |
751 | unum_setTextAttribute(def, UNUM_NEGATIVE_PREFIX, prefix, u_strlen(prefix), &status); | |
752 | if(U_FAILURE(status)) | |
753 | { | |
754 | log_err("error in setting the text attributes : %s\n", myErrorName(status)); | |
755 | } | |
756 | unum_getTextAttribute(def, UNUM_NEGATIVE_PREFIX, temp, resultlength, &status); | |
757 | if(U_FAILURE(status)) | |
758 | { | |
759 | log_err("error in getting the text attributes : %s\n", myErrorName(status)); | |
760 | } | |
761 | if(u_strcmp(prefix, temp)!=0) | |
762 | log_err("ERROR: get and setTextAttributes with negative prefix failed\n"); | |
763 | else | |
764 | log_verbose("Pass: get and setTextAttributes with negative prefix works fine\n"); | |
765 | ||
766 | u_uastrcpy(suffix, "+"); | |
767 | unum_setTextAttribute(def, UNUM_NEGATIVE_SUFFIX, suffix, u_strlen(suffix) , &status); | |
768 | if(U_FAILURE(status)) | |
769 | { | |
770 | log_err("error in setting the text attributes: %s\n", myErrorName(status)); | |
771 | } | |
772 | ||
773 | unum_getTextAttribute(def, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status); | |
774 | if(U_FAILURE(status)) | |
775 | { | |
776 | log_err("error in getting the text attributes : %s\n", myErrorName(status)); | |
777 | } | |
778 | if(u_strcmp(suffix, temp)!=0) | |
779 | log_err("ERROR: get and setTextAttributes with negative suffix failed\n"); | |
780 | else | |
781 | log_verbose("Pass: get and settextAttributes with negative suffix works fine\n"); | |
782 | ||
783 | u_uastrcpy(suffix, "++"); | |
784 | unum_setTextAttribute(def, UNUM_POSITIVE_SUFFIX, suffix, u_strlen(suffix) , &status); | |
785 | if(U_FAILURE(status)) | |
786 | { | |
787 | log_err("error in setting the text attributes: %s\n", myErrorName(status)); | |
788 | } | |
789 | ||
790 | unum_getTextAttribute(def, UNUM_POSITIVE_SUFFIX, temp, resultlength, &status); | |
791 | if(U_FAILURE(status)) | |
792 | { | |
793 | log_err("error in getting the text attributes : %s\n", myErrorName(status)); | |
794 | } | |
795 | if(u_strcmp(suffix, temp)!=0) | |
796 | log_err("ERROR: get and setTextAttributes with negative suffix failed\n"); | |
797 | else | |
798 | log_verbose("Pass: get and settextAttributes with negative suffix works fine\n"); | |
799 | ||
800 | /*Testing unum_getAttribute and unum_setAttribute() */ | |
801 | log_verbose("\nTesting get and set Attributes\n"); | |
802 | attr=UNUM_GROUPING_SIZE; | |
803 | newvalue=unum_getAttribute(def, attr); | |
804 | newvalue=2; | |
805 | unum_setAttribute(def, attr, newvalue); | |
806 | if(unum_getAttribute(def,attr)!=2) | |
807 | log_err("Fail: error in setting and getting attributes for UNUM_GROUPING_SIZE\n"); | |
808 | else | |
809 | log_verbose("Pass: setting and getting attributes for UNUM_GROUPING_SIZE works fine\n"); | |
810 | ||
811 | attr=UNUM_MULTIPLIER; | |
812 | newvalue=unum_getAttribute(def, attr); | |
813 | newvalue=8; | |
814 | unum_setAttribute(def, attr, newvalue); | |
815 | if(unum_getAttribute(def,attr) != 8) | |
816 | log_err("error in setting and getting attributes for UNUM_MULTIPLIER\n"); | |
817 | else | |
818 | log_verbose("Pass:setting and getting attributes for UNUM_MULTIPLIER works fine\n"); | |
819 | ||
820 | attr=UNUM_SECONDARY_GROUPING_SIZE; | |
821 | newvalue=unum_getAttribute(def, attr); | |
822 | newvalue=2; | |
823 | unum_setAttribute(def, attr, newvalue); | |
824 | if(unum_getAttribute(def,attr) != 2) | |
825 | log_err("error in setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE: got %d\n", | |
826 | unum_getAttribute(def,attr)); | |
827 | else | |
828 | log_verbose("Pass:setting and getting attributes for UNUM_SECONDARY_GROUPING_SIZE works fine\n"); | |
829 | ||
830 | /*testing set and get Attributes extensively */ | |
831 | log_verbose("\nTesting get and set attributes extensively\n"); | |
832 | for(attr=UNUM_PARSE_INT_ONLY; attr<= UNUM_PADDING_POSITION; attr=(UNumberFormatAttribute)((int32_t)attr + 1) ) | |
833 | { | |
834 | newvalue=unum_getAttribute(fr, attr); | |
835 | unum_setAttribute(def, attr, newvalue); | |
836 | if(unum_getAttribute(def,attr)!=unum_getAttribute(fr, attr)) | |
837 | log_err("error in setting and getting attributes\n"); | |
838 | else | |
839 | log_verbose("Pass: attributes set and retrieved successfully\n"); | |
840 | } | |
841 | ||
842 | /*testing spellout format to make sure we can use it successfully.*/ | |
843 | log_verbose("\nTesting spellout format\n"); | |
844 | if (spellout_def) | |
845 | { | |
846 | static const int32_t values[] = { 0, -5, 105, 1005, 105050 }; | |
847 | for (i = 0; i < UPRV_LENGTHOF(values); ++i) { | |
848 | UChar buffer[128]; | |
849 | int32_t len; | |
850 | int32_t value = values[i]; | |
851 | status = U_ZERO_ERROR; | |
852 | len = unum_format(spellout_def, value, buffer, UPRV_LENGTHOF(buffer), NULL, &status); | |
853 | if(U_FAILURE(status)) { | |
854 | log_err("Error in formatting using unum_format(spellout_fmt, ...): %s\n", myErrorName(status)); | |
855 | } else { | |
856 | int32_t pp = 0; | |
857 | int32_t parseResult; | |
858 | /*ustrToAstr(buffer, len, logbuf, UPRV_LENGTHOF(logbuf));*/ | |
859 | log_verbose("formatted %d as '%s', length: %d\n", value, aescstrdup(buffer, len), len); | |
860 | ||
861 | parseResult = unum_parse(spellout_def, buffer, len, &pp, &status); | |
862 | if (U_FAILURE(status)) { | |
863 | log_err("Error in parsing using unum_format(spellout_fmt, ...): %s\n", myErrorName(status)); | |
864 | } else if (parseResult != value) { | |
865 | log_err("unum_format result %d != value %d\n", parseResult, value); | |
866 | } | |
867 | } | |
868 | } | |
869 | } | |
870 | else { | |
871 | log_err("Spellout format is unavailable\n"); | |
872 | } | |
873 | ||
874 | { /* Test for ticket #7079 */ | |
875 | UNumberFormat* dec_en; | |
876 | UChar groupingSep[] = { 0 }; | |
877 | UChar numPercent[] = { 0x0031, 0x0032, 0x0025, 0 }; /* "12%" */ | |
878 | double parseResult = 0.0; | |
879 | ||
880 | status=U_ZERO_ERROR; | |
881 | dec_en = unum_open(UNUM_DECIMAL, NULL, 0, "en_US", NULL, &status); | |
882 | unum_setAttribute(dec_en, UNUM_LENIENT_PARSE, 0); | |
883 | unum_setSymbol(dec_en, UNUM_GROUPING_SEPARATOR_SYMBOL, groupingSep, 0, &status); | |
884 | parseResult = unum_parseDouble(dec_en, numPercent, -1, NULL, &status); | |
885 | /* Without the fix in #7079, the above call will hang */ | |
886 | if ( U_FAILURE(status) || parseResult != 12.0 ) { | |
887 | log_err("unum_parseDouble with empty groupingSep: status %s, parseResult %f not 12.0\n", | |
888 | myErrorName(status), parseResult); | |
889 | } else { | |
890 | log_verbose("unum_parseDouble with empty groupingSep: no hang, OK\n"); | |
891 | } | |
892 | unum_close(dec_en); | |
893 | } | |
894 | ||
895 | { /* Test parse & format of big decimals. Use a number with too many digits to fit in a double, | |
896 | to verify that it is taking the pure decimal path. */ | |
897 | UNumberFormat *fmt; | |
898 | const char *bdpattern = "#,##0.#########"; | |
899 | const char *numInitial = "12345678900987654321.1234567896"; | |
900 | const char *numFormatted = "12,345,678,900,987,654,321.12345679"; | |
901 | const char *parseExpected = "1.234567890098765432112345679E+19"; | |
902 | const char *parseExpected2 = "3.4567890098765432112345679E+17"; | |
903 | int32_t resultSize = 0; | |
904 | int32_t parsePos = 0; /* Output parameter for Parse operations. */ | |
905 | #define DESTCAPACITY 100 | |
906 | UChar dest[DESTCAPACITY]; | |
907 | char desta[DESTCAPACITY]; | |
908 | UFieldPosition fieldPos = {0}; | |
909 | ||
910 | /* Format */ | |
911 | ||
912 | status = U_ZERO_ERROR; | |
913 | u_uastrcpy(dest, bdpattern); | |
914 | fmt = unum_open(UNUM_PATTERN_DECIMAL, dest, -1, "en", NULL /*parseError*/, &status); | |
915 | if (U_FAILURE(status)) log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status)); | |
916 | ||
917 | resultSize = unum_formatDecimal(fmt, numInitial, -1, dest, DESTCAPACITY, NULL, &status); | |
918 | if (U_FAILURE(status)) { | |
919 | log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status)); | |
920 | } | |
921 | u_austrncpy(desta, dest, DESTCAPACITY); | |
922 | if (strcmp(numFormatted, desta) != 0) { | |
923 | log_err("File %s, Line %d, (expected, acutal) = (\"%s\", \"%s\")\n", | |
924 | __FILE__, __LINE__, numFormatted, desta); | |
925 | } | |
926 | if ((int32_t)strlen(numFormatted) != resultSize) { | |
927 | log_err("File %s, Line %d, (expected, actual) = (%d, %d)\n", | |
928 | __FILE__, __LINE__, (int32_t)strlen(numFormatted), resultSize); | |
929 | } | |
930 | ||
931 | /* Format with a FieldPosition parameter */ | |
932 | ||
933 | fieldPos.field = UNUM_DECIMAL_SEPARATOR_FIELD; | |
934 | resultSize = unum_formatDecimal(fmt, numInitial, -1, dest, DESTCAPACITY, &fieldPos, &status); | |
935 | if (U_FAILURE(status)) { | |
936 | log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status)); | |
937 | } | |
938 | u_austrncpy(desta, dest, DESTCAPACITY); | |
939 | if (strcmp(numFormatted, desta) != 0) { | |
940 | log_err("File %s, Line %d, (expected, acutal) = (\"%s\", \"%s\")\n", | |
941 | __FILE__, __LINE__, numFormatted, desta); | |
942 | } | |
943 | if (fieldPos.beginIndex != 26) { /* index of "." in formatted number */ | |
944 | log_err("File %s, Line %d, (expected, acutal) = (%d, %d)\n", | |
945 | __FILE__, __LINE__, 0, fieldPos.beginIndex); | |
946 | } | |
947 | if (fieldPos.endIndex != 27) { | |
948 | log_err("File %s, Line %d, (expected, acutal) = (%d, %d)\n", | |
949 | __FILE__, __LINE__, 0, fieldPos.endIndex); | |
950 | } | |
951 | ||
952 | /* Parse */ | |
953 | ||
954 | status = U_ZERO_ERROR; | |
955 | u_uastrcpy(dest, numFormatted); /* Parse the expected output of the formatting test */ | |
956 | resultSize = unum_parseDecimal(fmt, dest, -1, NULL, desta, DESTCAPACITY, &status); | |
957 | if (U_FAILURE(status)) { | |
958 | log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status)); | |
959 | } | |
960 | if (uprv_strcmp(parseExpected, desta) != 0) { | |
961 | log_err("File %s, Line %d, (expected, actual) = (\"%s\", \"%s\")\n", | |
962 | __FILE__, __LINE__, parseExpected, desta); | |
963 | } else { | |
964 | log_verbose("File %s, Line %d, got expected = \"%s\"\n", | |
965 | __FILE__, __LINE__, desta); | |
966 | } | |
967 | if ((int32_t)strlen(parseExpected) != resultSize) { | |
968 | log_err("File %s, Line %d, (expected, actual) = (%d, %d)\n", | |
969 | __FILE__, __LINE__, (int32_t)strlen(parseExpected), resultSize); | |
970 | } | |
971 | ||
972 | /* Parse with a parsePos parameter */ | |
973 | ||
974 | status = U_ZERO_ERROR; | |
975 | u_uastrcpy(dest, numFormatted); /* Parse the expected output of the formatting test */ | |
976 | parsePos = 3; /* 12,345,678,900,987,654,321.12345679 */ | |
977 | /* start parsing at the the third char */ | |
978 | resultSize = unum_parseDecimal(fmt, dest, -1, &parsePos, desta, DESTCAPACITY, &status); | |
979 | if (U_FAILURE(status)) { | |
980 | log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status)); | |
981 | } | |
982 | if (strcmp(parseExpected2, desta) != 0) { /* "3.4567890098765432112345679E+17" */ | |
983 | log_err("File %s, Line %d, (expected, actual) = (\"%s\", \"%s\")\n", | |
984 | __FILE__, __LINE__, parseExpected2, desta); | |
985 | } else { | |
986 | log_verbose("File %s, Line %d, got expected = \"%s\"\n", | |
987 | __FILE__, __LINE__, desta); | |
988 | } | |
989 | if ((int32_t)strlen(numFormatted) != parsePos) { | |
990 | log_err("File %s, Line %d, parsePos (expected, actual) = (\"%d\", \"%d\")\n", | |
991 | __FILE__, __LINE__, (int32_t)strlen(parseExpected), parsePos); | |
992 | } | |
993 | ||
994 | unum_close(fmt); | |
995 | } | |
996 | ||
997 | status = U_ZERO_ERROR; | |
998 | /* Test invalid symbol argument */ | |
999 | { | |
1000 | int32_t badsymbolLarge = UNUM_FORMAT_SYMBOL_COUNT + 1; | |
1001 | int32_t badsymbolSmall = -1; | |
1002 | UChar value[10]; | |
1003 | int32_t valueLength = 10; | |
1004 | UNumberFormat *fmt = unum_open(UNUM_DEFAULT, NULL, 0, NULL, NULL, &status); | |
1005 | if (U_FAILURE(status)) { | |
1006 | log_err("File %s, Line %d, status = %s\n", __FILE__, __LINE__, u_errorName(status)); | |
1007 | } else { | |
1008 | unum_getSymbol(fmt, (UNumberFormatSymbol)badsymbolLarge, NULL, 0, &status); | |
1009 | if (U_SUCCESS(status)) log_err("unum_getSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (> UNUM_FORMAT_SYMBOL_COUNT) argument\n"); | |
1010 | ||
1011 | status = U_ZERO_ERROR; | |
1012 | unum_getSymbol(fmt, (UNumberFormatSymbol)badsymbolSmall, NULL, 0, &status); | |
1013 | if (U_SUCCESS(status)) log_err("unum_getSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (less than 0) argument\n"); | |
1014 | ||
1015 | status = U_ZERO_ERROR; | |
1016 | unum_setSymbol(fmt, (UNumberFormatSymbol)badsymbolLarge, value, valueLength, &status); | |
1017 | if (U_SUCCESS(status)) log_err("unum_setSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (> UNUM_FORMAT_SYMBOL_COUNT) argument\n"); | |
1018 | ||
1019 | status = U_ZERO_ERROR; | |
1020 | unum_setSymbol(fmt, (UNumberFormatSymbol)badsymbolSmall, value, valueLength, &status); | |
1021 | if (U_SUCCESS(status)) log_err("unum_setSymbol()'s status should be ILLEGAL_ARGUMENT with invalid symbol (less than 0) argument\n"); | |
1022 | ||
1023 | unum_close(fmt); | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | ||
1028 | /*closing the NumberFormat() using unum_close(UNumberFormat*)")*/ | |
1029 | unum_close(def); | |
1030 | unum_close(fr); | |
1031 | unum_close(cur_def); | |
1032 | unum_close(cur_fr); | |
1033 | unum_close(per_def); | |
1034 | unum_close(per_fr); | |
1035 | unum_close(spellout_def); | |
1036 | unum_close(pattern); | |
1037 | unum_close(cur_frpattern); | |
1038 | unum_close(myclone); | |
1039 | ||
1040 | } | |
1041 | ||
1042 | static void TestParseZero(void) | |
1043 | { | |
1044 | UErrorCode errorCode = U_ZERO_ERROR; | |
1045 | UChar input[] = {0x30, 0}; /* Input text is decimal '0' */ | |
1046 | UChar pat[] = {0x0023,0x003b,0x0023,0}; /* {'#', ';', '#', 0}; */ | |
1047 | double dbl; | |
1048 | ||
1049 | #if 0 | |
1050 | UNumberFormat* unum = unum_open( UNUM_DECIMAL /*or UNUM_DEFAULT*/, NULL, -1, NULL, NULL, &errorCode); | |
1051 | #else | |
1052 | UNumberFormat* unum = unum_open( UNUM_PATTERN_DECIMAL /*needs pattern*/, pat, -1, NULL, NULL, &errorCode); | |
1053 | #endif | |
1054 | ||
1055 | dbl = unum_parseDouble( unum, input, -1 /*u_strlen(input)*/, 0 /* 0 = start */, &errorCode ); | |
1056 | if (U_FAILURE(errorCode)) { | |
1057 | log_data_err("Result - %s\n", u_errorName(errorCode)); | |
1058 | } else { | |
1059 | log_verbose("Double: %f\n", dbl); | |
1060 | } | |
1061 | unum_close(unum); | |
1062 | } | |
1063 | ||
1064 | static const UChar dollars2Sym[] = { 0x24,0x32,0x2E,0x30,0x30,0 }; /* $2.00 */ | |
1065 | static const UChar dollars4Sym[] = { 0x24,0x34,0 }; /* $4 */ | |
1066 | static const UChar dollarsUS4Sym[] = { 0x55,0x53,0x24,0x34,0 }; /* US$4 */ | |
1067 | static const UChar dollars9Sym[] = { 0x39,0xA0,0x24,0 }; /* 9 $ */ | |
1068 | static const UChar pounds3Sym[] = { 0xA3,0x33,0x2E,0x30,0x30,0 }; /* [POUND]3.00 */ | |
1069 | static const UChar pounds5Sym[] = { 0xA3,0x35,0 }; /* [POUND]5 */ | |
1070 | static const UChar pounds7Sym[] = { 0x37,0xA0,0xA3,0 }; /* 7 [POUND] */ | |
1071 | static const UChar euros4Sym[] = { 0x34,0x2C,0x30,0x30,0xA0,0x20AC,0 }; /* 4,00 [EURO] */ | |
1072 | static const UChar euros6Sym[] = { 0x36,0xA0,0x20AC,0 }; /* 6 [EURO] */ | |
1073 | static const UChar euros8Sym[] = { 0x20AC,0x38,0 }; /* [EURO]8 */ | |
1074 | static const UChar dollars4PluEn[] = { 0x34,0x20,0x55,0x53,0x20,0x64,0x6F,0x6C,0x6C,0x61,0x72,0x73,0 }; /* 4 US dollars*/ | |
1075 | static const UChar pounds5PluEn[] = { 0x35,0x20,0x42,0x72,0x69,0x74,0x69,0x73,0x68,0x20,0x70,0x6F,0x75,0x6E,0x64,0x73,0x20,0x73,0x74,0x65,0x72,0x6C,0x69,0x6E,0x67,0 }; /* 5 British pounds sterling */ | |
1076 | static const UChar euros8PluEn[] = { 0x38,0x20,0x65,0x75,0x72,0x6F,0x73,0 }; /* 8 euros*/ | |
1077 | static const UChar euros6PluFr[] = { 0x36,0x20,0x65,0x75,0x72,0x6F,0x73,0 }; /* 6 euros*/ | |
1078 | ||
1079 | typedef struct { | |
1080 | const char * locale; | |
1081 | const char * descrip; | |
1082 | const UChar * currStr; | |
1083 | const UChar * plurStr; | |
1084 | UErrorCode parsDoubExpectErr; | |
1085 | int32_t parsDoubExpectPos; | |
1086 | double parsDoubExpectVal; | |
1087 | UErrorCode parsCurrExpectErr; | |
1088 | int32_t parsCurrExpectPos; | |
1089 | double parsCurrExpectVal; | |
1090 | const char * parsCurrExpectCurr; | |
1091 | } ParseCurrencyItem; | |
1092 | ||
1093 | static const ParseCurrencyItem parseCurrencyItems[] = { | |
1094 | { "en_US", "dollars2", dollars2Sym, NULL, U_ZERO_ERROR, 5, 2.0, U_ZERO_ERROR, 5, 2.0, "USD" }, | |
1095 | { "en_US", "dollars4", dollars4Sym, dollars4PluEn, U_ZERO_ERROR, 2, 4.0, U_ZERO_ERROR, 2, 4.0, "USD" }, | |
1096 | { "en_US", "dollars9", dollars9Sym, NULL, U_PARSE_ERROR, 1, 0.0, U_PARSE_ERROR, 1, 0.0, "" }, | |
1097 | { "en_US", "pounds3", pounds3Sym, NULL, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 5, 3.0, "GBP" }, | |
1098 | { "en_US", "pounds5", pounds5Sym, pounds5PluEn, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 2, 5.0, "GBP" }, | |
1099 | { "en_US", "pounds7", pounds7Sym, NULL, U_PARSE_ERROR, 1, 0.0, U_PARSE_ERROR, 1, 0.0, "" }, | |
1100 | { "en_US", "euros8", euros8Sym, euros8PluEn, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 2, 8.0, "EUR" }, | |
1101 | ||
1102 | { "en_GB", "pounds3", pounds3Sym, NULL, U_ZERO_ERROR, 5, 3.0, U_ZERO_ERROR, 5, 3.0, "GBP" }, | |
1103 | { "en_GB", "pounds5", pounds5Sym, pounds5PluEn, U_ZERO_ERROR, 2, 5.0, U_ZERO_ERROR, 2, 5.0, "GBP" }, | |
1104 | { "en_GB", "pounds7", pounds7Sym, NULL, U_PARSE_ERROR, 1, 0.0, U_PARSE_ERROR, 1, 0.0, "" }, | |
1105 | { "en_GB", "euros4", euros4Sym, NULL, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, 0.0, "" }, | |
1106 | { "en_GB", "euros6", euros6Sym, NULL, U_PARSE_ERROR, 1, 0.0, U_PARSE_ERROR, 1, 0.0, "" }, | |
1107 | { "en_GB", "euros8", euros8Sym, euros8PluEn, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 2, 8.0, "EUR" }, | |
1108 | { "en_GB", "dollars4", dollarsUS4Sym,dollars4PluEn, U_PARSE_ERROR, 0, 0.0, U_ZERO_ERROR, 4, 4.0, "USD" }, | |
1109 | ||
1110 | { "fr_FR", "euros4", euros4Sym, NULL, U_ZERO_ERROR, 6, 4.0, U_ZERO_ERROR, 6, 4.0, "EUR" }, | |
1111 | { "fr_FR", "euros6", euros6Sym, euros6PluFr, U_ZERO_ERROR, 3, 6.0, U_ZERO_ERROR, 3, 6.0, "EUR" }, | |
1112 | { "fr_FR", "euros8", euros8Sym, NULL, U_PARSE_ERROR, 2, 0.0, U_PARSE_ERROR, 2, 0.0, "" }, | |
1113 | { "fr_FR", "dollars2", dollars2Sym, NULL, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, 0.0, "" }, | |
1114 | { "fr_FR", "dollars4", dollars4Sym, NULL, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, 0.0, "" }, | |
1115 | ||
1116 | { NULL, NULL, NULL, NULL, 0, 0, 0.0, 0, 0, 0.0, NULL } | |
1117 | }; | |
1118 | ||
1119 | static void TestParseCurrency() | |
1120 | { | |
1121 | const ParseCurrencyItem * itemPtr; | |
1122 | for (itemPtr = parseCurrencyItems; itemPtr->locale != NULL; ++itemPtr) { | |
1123 | UNumberFormat* unum; | |
1124 | UErrorCode status; | |
1125 | double parseVal; | |
1126 | int32_t parsePos; | |
1127 | UChar parseCurr[4]; | |
1128 | char parseCurrB[4]; | |
1129 | ||
1130 | status = U_ZERO_ERROR; | |
1131 | unum = unum_open(UNUM_CURRENCY, NULL, 0, itemPtr->locale, NULL, &status); | |
1132 | if (U_SUCCESS(status)) { | |
1133 | const UChar * currStr = itemPtr->currStr; | |
1134 | status = U_ZERO_ERROR; | |
1135 | parsePos = 0; | |
1136 | parseVal = unum_parseDouble(unum, currStr, -1, &parsePos, &status); | |
1137 | if (status != itemPtr->parsDoubExpectErr || parsePos != itemPtr->parsDoubExpectPos || parseVal != itemPtr->parsDoubExpectVal) { | |
1138 | log_err("UNUM_CURRENCY parseDouble %s/%s, expect %s pos %d val %.1f, get %s pos %d val %.1f\n", | |
1139 | itemPtr->locale, itemPtr->descrip, | |
1140 | u_errorName(itemPtr->parsDoubExpectErr), itemPtr->parsDoubExpectPos, itemPtr->parsDoubExpectVal, | |
1141 | u_errorName(status), parsePos, parseVal ); | |
1142 | } | |
1143 | status = U_ZERO_ERROR; | |
1144 | parsePos = 0; | |
1145 | parseCurr[0] = 0; | |
1146 | parseVal = unum_parseDoubleCurrency(unum, currStr, -1, &parsePos, parseCurr, &status); | |
1147 | u_austrncpy(parseCurrB, parseCurr, 4); | |
1148 | if (status != itemPtr->parsCurrExpectErr || parsePos != itemPtr->parsCurrExpectPos || parseVal != itemPtr->parsCurrExpectVal || | |
1149 | strncmp(parseCurrB, itemPtr->parsCurrExpectCurr, 4) != 0) { | |
1150 | log_err("UNUM_CURRENCY parseDoubleCurrency %s/%s, expect %s pos %d val %.1f cur %s, get %s pos %d val %.1f cur %s\n", | |
1151 | itemPtr->locale, itemPtr->descrip, | |
1152 | u_errorName(itemPtr->parsCurrExpectErr), itemPtr->parsCurrExpectPos, itemPtr->parsCurrExpectVal, itemPtr->parsCurrExpectCurr, | |
1153 | u_errorName(status), parsePos, parseVal, parseCurrB ); | |
1154 | } | |
1155 | unum_close(unum); | |
1156 | } else { | |
1157 | log_data_err("unexpected error in unum_open UNUM_CURRENCY for locale %s: '%s'\n", itemPtr->locale, u_errorName(status)); | |
1158 | } | |
1159 | ||
1160 | if (itemPtr->plurStr != NULL) { | |
1161 | status = U_ZERO_ERROR; | |
1162 | unum = unum_open(UNUM_CURRENCY_PLURAL, NULL, 0, itemPtr->locale, NULL, &status); | |
1163 | if (U_SUCCESS(status)) { | |
1164 | status = U_ZERO_ERROR; | |
1165 | parsePos = 0; | |
1166 | parseVal = unum_parseDouble(unum, itemPtr->plurStr, -1, &parsePos, &status); | |
1167 | if (status != itemPtr->parsDoubExpectErr || parseVal != itemPtr->parsDoubExpectVal) { | |
1168 | log_err("UNUM_CURRENCY parseDouble Plural %s/%s, expect %s val %.1f, get %s val %.1f\n", | |
1169 | itemPtr->locale, itemPtr->descrip, | |
1170 | u_errorName(itemPtr->parsDoubExpectErr), itemPtr->parsDoubExpectVal, | |
1171 | u_errorName(status), parseVal ); | |
1172 | } | |
1173 | status = U_ZERO_ERROR; | |
1174 | parsePos = 0; | |
1175 | parseCurr[0] = 0; | |
1176 | parseVal = unum_parseDoubleCurrency(unum, itemPtr->plurStr, -1, &parsePos, parseCurr, &status); | |
1177 | u_austrncpy(parseCurrB, parseCurr, 4); | |
1178 | if (status != itemPtr->parsCurrExpectErr || parseVal != itemPtr->parsCurrExpectVal || | |
1179 | strncmp(parseCurrB, itemPtr->parsCurrExpectCurr, 4) != 0) { | |
1180 | log_err("UNUM_CURRENCY parseDoubleCurrency Plural %s/%s, expect %s val %.1f cur %s, get %s val %.1f cur %s\n", | |
1181 | itemPtr->locale, itemPtr->descrip, | |
1182 | u_errorName(itemPtr->parsCurrExpectErr), itemPtr->parsCurrExpectVal, itemPtr->parsCurrExpectCurr, | |
1183 | u_errorName(status), parseVal, parseCurrB ); | |
1184 | } | |
1185 | unum_close(unum); | |
1186 | } else { | |
1187 | log_data_err("unexpected error in unum_open UNUM_CURRENCY_PLURAL for locale %s: '%s'\n", itemPtr->locale, u_errorName(status)); | |
1188 | } | |
1189 | } | |
1190 | } | |
1191 | } | |
1192 | ||
1193 | typedef struct { | |
1194 | const char * testname; | |
1195 | const char * locale; | |
1196 | UBool lenient; | |
1197 | const UChar * source; | |
1198 | int32_t startPos; | |
1199 | int32_t value; | |
1200 | int32_t endPos; | |
1201 | UErrorCode status; | |
1202 | } NumParseTestItem; | |
1203 | ||
1204 | static const UChar ustr_zh50d[] = {0x4E94, 0x3007, 0}; /* decimal 50 */ | |
1205 | static const UChar ustr_zh05a[] = {0x96F6, 0x4E94, 0}; /* decimal-alt 05 */ | |
1206 | static const UChar ustr_zh05d[] = {0x3007, 0x4E94, 0}; /* decimal 05 */ | |
1207 | ||
1208 | static const NumParseTestItem altnumParseTests[] = { | |
1209 | /* name loc lenent src start val end status */ | |
1210 | { "zh@hd,50dL","zh@numbers=hanidec", TRUE, ustr_zh50d, 0, 50, 2, U_USING_DEFAULT_WARNING }, | |
1211 | { "zh@hd,05aL","zh@numbers=hanidec", TRUE, ustr_zh05a, 0, 5, 2, U_USING_DEFAULT_WARNING }, | |
1212 | { "zh@hd,05dL","zh@numbers=hanidec", TRUE, ustr_zh05d, 0, 5, 2, U_USING_DEFAULT_WARNING }, | |
1213 | { NULL, NULL, FALSE, NULL, 0, 0, 0, 0 } /* terminator */ | |
1214 | }; | |
1215 | ||
1216 | static void TestParseAltNum(void) | |
1217 | { | |
1218 | const NumParseTestItem * testPtr; | |
1219 | for (testPtr = altnumParseTests; testPtr->testname != NULL; ++testPtr) { | |
1220 | UErrorCode status = U_ZERO_ERROR; | |
1221 | int32_t value, position = testPtr->startPos; | |
1222 | UNumberFormat *nf = unum_open(UNUM_DECIMAL, NULL, 0, testPtr->locale, NULL, &status); | |
1223 | if (U_FAILURE(status)) { | |
1224 | log_err_status(status, "unum_open fails for UNUM_DECIMAL with locale %s, status %s\n", testPtr->locale, myErrorName(status)); | |
1225 | continue; | |
1226 | } | |
1227 | unum_setAttribute(nf, UNUM_LENIENT_PARSE, testPtr->lenient); | |
1228 | value = unum_parse(nf, testPtr->source, -1, &position, &status); | |
1229 | if ( value != testPtr->value || position != testPtr->endPos || status != testPtr->status ) { | |
1230 | log_err("unum_parse DECIMAL, locale %s, testname %s, startPos %d: for value / endPos / status, expected %d / %d / %s, got %d / %d / %s\n", | |
1231 | testPtr->locale, testPtr->testname, testPtr->startPos, | |
1232 | testPtr->value, testPtr->endPos, myErrorName(testPtr->status), | |
1233 | value, position, myErrorName(status) ); | |
1234 | } | |
1235 | unum_close(nf); | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | static const UChar ustr_en0[] = {0x7A, 0x65, 0x72, 0x6F, 0}; /* zero */ | |
1240 | static const UChar ustr_123[] = {0x31, 0x32, 0x33, 0}; /* 123 */ | |
1241 | static const UChar ustr_en123[] = {0x6f, 0x6e, 0x65, 0x20, 0x68, 0x75, 0x6e, 0x64, 0x72, 0x65, 0x64, | |
1242 | 0x20, 0x74, 0x77, 0x65, 0x6e, 0x74, 0x79, | |
1243 | 0x2d, 0x74, 0x68, 0x72, 0x65, 0x65, 0}; /* one hundred twenty-three */ | |
1244 | static const UChar ustr_fr123[] = {0x63, 0x65, 0x6e, 0x74, 0x20, 0x76, 0x69, 0x6e, 0x67, 0x74, 0x2d, | |
1245 | 0x74, 0x72, 0x6f, 0x69, 0x73, 0}; /* cent vingt-trois */ | |
1246 | static const UChar ustr_ja123[] = {0x767e, 0x4e8c, 0x5341, 0x4e09, 0}; /* kanji 100(+)2(*)10(+)3 */ | |
1247 | static const UChar ustr_zh50s[] = {0x4E94, 0x5341, 0}; /* spellout 50 */ | |
1248 | //static const UChar ustr_zh50d[] = [reuse from above] /* decimal 50 */ | |
1249 | //static const UChar ustr_zh05a[] = [reuse from above] /* decimal-alt 05 */ | |
1250 | //static const UChar ustr_zh05d[] = [reuse from above] /* decimal 05 */ | |
1251 | ||
1252 | #define NUMERIC_STRINGS_NOT_PARSEABLE 1 // ticket/8224 | |
1253 | ||
1254 | static const NumParseTestItem spelloutParseTests[] = { | |
1255 | /* name loc lenent src start val end status */ | |
1256 | { "en0", "en", FALSE, ustr_en0, 0, 0, 4, U_ZERO_ERROR }, | |
1257 | { "en0", "en", FALSE, ustr_en0, 2, 0, 2, U_PARSE_ERROR }, | |
1258 | { "en0", "ja", FALSE, ustr_en0, 0, 0, 0, U_PARSE_ERROR }, | |
1259 | #if NUMERIC_STRINGS_NOT_PARSEABLE | |
1260 | { "123", "en", FALSE, ustr_123, 0, 0, 0, U_PARSE_ERROR }, | |
1261 | #else | |
1262 | { "123", "en", FALSE, ustr_123, 0, 123, 3, U_ZERO_ERROR }, | |
1263 | #endif | |
1264 | { "123L", "en", TRUE, ustr_123, 0, 123, 3, U_ZERO_ERROR }, | |
1265 | { "en123", "en", FALSE, ustr_en123, 0, 123, 24, U_ZERO_ERROR }, | |
1266 | { "en123", "en", FALSE, ustr_en123, 12, 23, 24, U_ZERO_ERROR }, | |
1267 | { "en123", "fr", FALSE, ustr_en123, 16, 0, 16, U_PARSE_ERROR }, | |
1268 | { "fr123", "fr", FALSE, ustr_fr123, 0, 123, 16, U_ZERO_ERROR }, | |
1269 | { "fr123", "fr", FALSE, ustr_fr123, 5, 23, 16, U_ZERO_ERROR }, | |
1270 | { "fr123", "en", FALSE, ustr_fr123, 0, 0, 0, U_PARSE_ERROR }, | |
1271 | { "ja123", "ja", FALSE, ustr_ja123, 0, 123, 4, U_ZERO_ERROR }, | |
1272 | { "ja123", "ja", FALSE, ustr_ja123, 1, 23, 4, U_ZERO_ERROR }, | |
1273 | { "ja123", "fr", FALSE, ustr_ja123, 0, 0, 0, U_PARSE_ERROR }, | |
1274 | { "zh,50s", "zh", FALSE, ustr_zh50s, 0, 50, 2, U_ZERO_ERROR }, | |
1275 | // from ICU50m2, NUMERIC_STRINGS_NOT_PARSEABLE no longer affects the next three | |
1276 | { "zh@hd,50d", "zh@numbers=hanidec", FALSE, ustr_zh50d, 0, 50, 2, U_ZERO_ERROR }, | |
1277 | { "zh@hd,05a", "zh@numbers=hanidec", FALSE, ustr_zh05a, 0, 5, 2, U_ZERO_ERROR }, | |
1278 | { "zh@hd,05d", "zh@numbers=hanidec", FALSE, ustr_zh05d, 0, 5, 2, U_ZERO_ERROR }, | |
1279 | { "zh@hd,50dL","zh@numbers=hanidec", TRUE, ustr_zh50d, 0, 50, 2, U_ZERO_ERROR }, | |
1280 | { "zh@hd,05aL","zh@numbers=hanidec", TRUE, ustr_zh05a, 0, 5, 2, U_ZERO_ERROR }, | |
1281 | { "zh@hd,05dL","zh@numbers=hanidec", TRUE, ustr_zh05d, 0, 5, 2, U_ZERO_ERROR }, | |
1282 | { "zh,50dL","zh", TRUE, ustr_zh50d, 0, 50, 2, U_ZERO_ERROR }, /* changed in ICU50m2 */ | |
1283 | { "zh,05aL","zh", TRUE, ustr_zh05a, 0, 0, 1, U_ZERO_ERROR }, | |
1284 | { "zh,05dL","zh", TRUE, ustr_zh05d, 0, 5, 2, U_ZERO_ERROR }, /* changed in ICU50m2 */ | |
1285 | { NULL, NULL, FALSE, NULL, 0, 0, 0, 0 } /* terminator */ | |
1286 | }; | |
1287 | ||
1288 | static void TestSpelloutNumberParse() | |
1289 | { | |
1290 | const NumParseTestItem * testPtr; | |
1291 | for (testPtr = spelloutParseTests; testPtr->testname != NULL; ++testPtr) { | |
1292 | UErrorCode status = U_ZERO_ERROR; | |
1293 | int32_t value, position = testPtr->startPos; | |
1294 | UNumberFormat *nf = unum_open(UNUM_SPELLOUT, NULL, 0, testPtr->locale, NULL, &status); | |
1295 | if (U_FAILURE(status)) { | |
1296 | log_err_status(status, "unum_open fails for UNUM_SPELLOUT with locale %s, status %s\n", testPtr->locale, myErrorName(status)); | |
1297 | continue; | |
1298 | } | |
1299 | unum_setAttribute(nf, UNUM_LENIENT_PARSE, testPtr->lenient); | |
1300 | status = U_ZERO_ERROR; | |
1301 | value = unum_parse(nf, testPtr->source, -1, &position, &status); | |
1302 | if ( value != testPtr->value || position != testPtr->endPos || status != testPtr->status ) { | |
1303 | log_err("unum_parse SPELLOUT, locale %s, testname %s, startPos %d: for value / endPos / status, expected %d / %d / %s, got %d / %d / %s\n", | |
1304 | testPtr->locale, testPtr->testname, testPtr->startPos, | |
1305 | testPtr->value, testPtr->endPos, myErrorName(testPtr->status), | |
1306 | value, position, myErrorName(status) ); | |
1307 | } | |
1308 | unum_close(nf); | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | static void TestSignificantDigits() | |
1313 | { | |
1314 | UChar temp[128]; | |
1315 | int32_t resultlengthneeded; | |
1316 | int32_t resultlength; | |
1317 | UErrorCode status = U_ZERO_ERROR; | |
1318 | UChar *result = NULL; | |
1319 | UNumberFormat* fmt; | |
1320 | double d = 123456.789; | |
1321 | ||
1322 | u_uastrcpy(temp, "###0.0#"); | |
1323 | fmt=unum_open(UNUM_IGNORE, temp, -1, "en", NULL, &status); | |
1324 | if (U_FAILURE(status)) { | |
1325 | log_data_err("got unexpected error for unum_open: '%s'\n", u_errorName(status)); | |
1326 | return; | |
1327 | } | |
1328 | unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE); | |
1329 | unum_setAttribute(fmt, UNUM_MAX_SIGNIFICANT_DIGITS, 6); | |
1330 | ||
1331 | u_uastrcpy(temp, "123457"); | |
1332 | resultlength=0; | |
1333 | resultlengthneeded=unum_formatDouble(fmt, d, NULL, resultlength, NULL, &status); | |
1334 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
1335 | { | |
1336 | status=U_ZERO_ERROR; | |
1337 | resultlength=resultlengthneeded+1; | |
1338 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
1339 | unum_formatDouble(fmt, d, result, resultlength, NULL, &status); | |
1340 | } | |
1341 | if(U_FAILURE(status)) | |
1342 | { | |
1343 | log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status)); | |
1344 | return; | |
1345 | } | |
1346 | if(u_strcmp(result, temp)==0) | |
1347 | log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n"); | |
1348 | else | |
1349 | log_err("FAIL: Error in number formatting using unum_formatDouble()\n"); | |
1350 | free(result); | |
1351 | unum_close(fmt); | |
1352 | } | |
1353 | ||
1354 | static void TestSigDigRounding() | |
1355 | { | |
1356 | UErrorCode status = U_ZERO_ERROR; | |
1357 | UChar expected[128]; | |
1358 | UChar result[128]; | |
1359 | char temp1[128]; | |
1360 | char temp2[128]; | |
1361 | UNumberFormat* fmt; | |
1362 | double d = 123.4; | |
1363 | ||
1364 | fmt=unum_open(UNUM_DECIMAL, NULL, 0, "en", NULL, &status); | |
1365 | if (U_FAILURE(status)) { | |
1366 | log_data_err("got unexpected error for unum_open: '%s'\n", u_errorName(status)); | |
1367 | return; | |
1368 | } | |
1369 | unum_setAttribute(fmt, UNUM_LENIENT_PARSE, FALSE); | |
1370 | unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE); | |
1371 | unum_setAttribute(fmt, UNUM_MAX_SIGNIFICANT_DIGITS, 2); | |
1372 | /* unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 0); */ | |
1373 | ||
1374 | unum_setAttribute(fmt, UNUM_ROUNDING_MODE, UNUM_ROUND_UP); | |
1375 | unum_setDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT, 20.0); | |
1376 | ||
1377 | (void)unum_formatDouble(fmt, d, result, UPRV_LENGTHOF(result), NULL, &status); | |
1378 | if(U_FAILURE(status)) | |
1379 | { | |
1380 | log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status)); | |
1381 | return; | |
1382 | } | |
1383 | ||
1384 | u_uastrcpy(expected, "140"); | |
1385 | if(u_strcmp(result, expected)!=0) | |
1386 | log_err("FAIL: Error in unum_formatDouble result %s instead of %s\n", u_austrcpy(temp1, result), u_austrcpy(temp2, expected) ); | |
1387 | ||
1388 | unum_close(fmt); | |
1389 | } | |
1390 | ||
1391 | static void TestNumberFormatPadding() | |
1392 | { | |
1393 | UChar *result=NULL; | |
1394 | UChar temp1[512]; | |
1395 | UChar temp2[512]; | |
1396 | ||
1397 | UErrorCode status=U_ZERO_ERROR; | |
1398 | int32_t resultlength; | |
1399 | int32_t resultlengthneeded; | |
1400 | UNumberFormat *pattern; | |
1401 | double d1; | |
1402 | double d = -10456.37; | |
1403 | UFieldPosition pos1; | |
1404 | int32_t parsepos; | |
1405 | ||
1406 | /* create a number format using unum_openPattern(....)*/ | |
1407 | log_verbose("\nTesting unum_openPattern() with padding\n"); | |
1408 | u_uastrcpy(temp1, "*#,##0.0#*;(#,##0.0#)"); | |
1409 | status=U_ZERO_ERROR; | |
1410 | pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), NULL, NULL,&status); | |
1411 | if(U_SUCCESS(status)) | |
1412 | { | |
1413 | log_err("error in unum_openPattern(%s): %s\n", temp1, myErrorName(status) ); | |
1414 | } | |
1415 | else | |
1416 | { | |
1417 | unum_close(pattern); | |
1418 | } | |
1419 | ||
1420 | /* u_uastrcpy(temp1, "*x#,###,###,##0.0#;(*x#,###,###,##0.0#)"); */ | |
1421 | u_uastrcpy(temp1, "*x#,###,###,##0.0#;*x(###,###,##0.0#)"); // input pattern | |
1422 | u_uastrcpy(temp2, "*x#########,##0.0#;(#########,##0.0#)"); // equivalent (?) output pattern | |
1423 | status=U_ZERO_ERROR; | |
1424 | pattern=unum_open(UNUM_IGNORE,temp1, u_strlen(temp1), "en_US",NULL, &status); | |
1425 | if(U_FAILURE(status)) | |
1426 | { | |
1427 | log_err_status(status, "error in padding unum_openPattern(%s): %s\n", temp1, myErrorName(status) ); | |
1428 | } | |
1429 | else { | |
1430 | log_verbose("Pass: padding unum_openPattern() works fine\n"); | |
1431 | ||
1432 | /*test for unum_toPattern()*/ | |
1433 | log_verbose("\nTesting padding unum_toPattern()\n"); | |
1434 | resultlength=0; | |
1435 | resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &status); | |
1436 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
1437 | { | |
1438 | status=U_ZERO_ERROR; | |
1439 | resultlength=resultlengthneeded+1; | |
1440 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
1441 | unum_toPattern(pattern, FALSE, result, resultlength, &status); | |
1442 | } | |
1443 | if(U_FAILURE(status)) | |
1444 | { | |
1445 | log_err("error in extracting the padding pattern from UNumberFormat: %s\n", myErrorName(status)); | |
1446 | } | |
1447 | else | |
1448 | { | |
1449 | if(u_strncmp(result, temp2, resultlengthneeded)!=0) { | |
1450 | log_err( | |
1451 | "FAIL: Error in extracting the padding pattern using unum_toPattern(): %d: %s != %s\n", | |
1452 | resultlengthneeded, | |
1453 | austrdup(temp2), | |
1454 | austrdup(result)); | |
1455 | } else { | |
1456 | log_verbose("Pass: extracted the padding pattern correctly using unum_toPattern()\n"); | |
1457 | } | |
1458 | } | |
1459 | free(result); | |
1460 | /* u_uastrcpy(temp1, "(xxxxxxx10,456.37)"); */ | |
1461 | u_uastrcpy(temp1, "xxxxx(10,456.37)"); | |
1462 | resultlength=0; | |
1463 | pos1.field = UNUM_FRACTION_FIELD; | |
1464 | resultlengthneeded=unum_formatDouble(pattern, d, NULL, resultlength, &pos1, &status); | |
1465 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
1466 | { | |
1467 | status=U_ZERO_ERROR; | |
1468 | resultlength=resultlengthneeded+1; | |
1469 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
1470 | unum_formatDouble(pattern, d, result, resultlength, NULL, &status); | |
1471 | } | |
1472 | if(U_FAILURE(status)) | |
1473 | { | |
1474 | log_err("Error in formatting using unum_formatDouble(.....) with padding : %s\n", myErrorName(status)); | |
1475 | } | |
1476 | else | |
1477 | { | |
1478 | if(u_strcmp(result, temp1)==0) | |
1479 | log_verbose("Pass: Number Formatting using unum_formatDouble() padding Successful\n"); | |
1480 | else | |
1481 | log_data_err("FAIL: Error in number formatting using unum_formatDouble() with padding\n"); | |
1482 | if(pos1.beginIndex == 13 && pos1.endIndex == 15) | |
1483 | log_verbose("Pass: Complete number formatting using unum_formatDouble() successful\n"); | |
1484 | else | |
1485 | log_err("Fail: Error in complete number Formatting using unum_formatDouble()\nGot: b=%d end=%d\nExpected: b=13 end=15\n", | |
1486 | pos1.beginIndex, pos1.endIndex); | |
1487 | ||
1488 | ||
1489 | /* Testing unum_parse() and unum_parseDouble() */ | |
1490 | log_verbose("\nTesting padding unum_parseDouble()\n"); | |
1491 | parsepos=0; | |
1492 | d1=unum_parseDouble(pattern, result, u_strlen(result), &parsepos, &status); | |
1493 | if(U_FAILURE(status)) | |
1494 | { | |
1495 | log_err("padding parse failed. The error is : %s\n", myErrorName(status)); | |
1496 | } | |
1497 | ||
1498 | if(d1!=d) | |
1499 | log_err("Fail: Error in padding parsing\n"); | |
1500 | else | |
1501 | log_verbose("Pass: padding parsing successful\n"); | |
1502 | free(result); | |
1503 | } | |
1504 | } | |
1505 | ||
1506 | unum_close(pattern); | |
1507 | } | |
1508 | ||
1509 | static UBool | |
1510 | withinErr(double a, double b, double err) { | |
1511 | return uprv_fabs(a - b) < uprv_fabs(a * err); | |
1512 | } | |
1513 | ||
1514 | static void TestInt64Format() { | |
1515 | UChar temp1[512]; | |
1516 | UChar result[512]; | |
1517 | UNumberFormat *fmt; | |
1518 | UErrorCode status = U_ZERO_ERROR; | |
1519 | const double doubleInt64Max = (double)U_INT64_MAX; | |
1520 | const double doubleInt64Min = (double)U_INT64_MIN; | |
1521 | const double doubleBig = 10.0 * (double)U_INT64_MAX; | |
1522 | int32_t val32; | |
1523 | int64_t val64; | |
1524 | double valDouble; | |
1525 | int32_t parsepos; | |
1526 | ||
1527 | /* create a number format using unum_openPattern(....) */ | |
1528 | log_verbose("\nTesting Int64Format\n"); | |
1529 | u_uastrcpy(temp1, "#.#E0"); | |
1530 | fmt = unum_open(UNUM_IGNORE, temp1, u_strlen(temp1), "en_US", NULL, &status); | |
1531 | if(U_FAILURE(status)) { | |
1532 | log_data_err("error in unum_openPattern() - %s\n", myErrorName(status)); | |
1533 | } else { | |
1534 | unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 20); | |
1535 | unum_formatInt64(fmt, U_INT64_MAX, result, 512, NULL, &status); | |
1536 | if (U_FAILURE(status)) { | |
1537 | log_err("error in unum_format(): %s\n", myErrorName(status)); | |
1538 | } else { | |
1539 | log_verbose("format int64max: '%s'\n", result); | |
1540 | parsepos = 0; | |
1541 | val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status); | |
1542 | if (status != U_INVALID_FORMAT_ERROR) { | |
1543 | log_err("parse didn't report error: %s\n", myErrorName(status)); | |
1544 | } else if (val32 != INT32_MAX) { | |
1545 | log_err("parse didn't pin return value, got: %d\n", val32); | |
1546 | } | |
1547 | ||
1548 | status = U_ZERO_ERROR; | |
1549 | parsepos = 0; | |
1550 | val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status); | |
1551 | if (U_FAILURE(status)) { | |
1552 | log_err("parseInt64 returned error: %s\n", myErrorName(status)); | |
1553 | } else if (val64 != U_INT64_MAX) { | |
1554 | log_err("parseInt64 returned incorrect value, got: %ld\n", val64); | |
1555 | } | |
1556 | ||
1557 | status = U_ZERO_ERROR; | |
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 (valDouble != doubleInt64Max) { | |
1563 | log_err("parseDouble returned incorrect value, got: %g\n", valDouble); | |
1564 | } | |
1565 | } | |
1566 | ||
1567 | unum_formatInt64(fmt, U_INT64_MIN, result, 512, NULL, &status); | |
1568 | if (U_FAILURE(status)) { | |
1569 | log_err("error in unum_format(): %s\n", myErrorName(status)); | |
1570 | } else { | |
1571 | log_verbose("format int64min: '%s'\n", result); | |
1572 | parsepos = 0; | |
1573 | val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status); | |
1574 | if (status != U_INVALID_FORMAT_ERROR) { | |
1575 | log_err("parse didn't report error: %s\n", myErrorName(status)); | |
1576 | } else if (val32 != INT32_MIN) { | |
1577 | log_err("parse didn't pin return value, got: %d\n", val32); | |
1578 | } | |
1579 | ||
1580 | status = U_ZERO_ERROR; | |
1581 | parsepos = 0; | |
1582 | val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status); | |
1583 | if (U_FAILURE(status)) { | |
1584 | log_err("parseInt64 returned error: %s\n", myErrorName(status)); | |
1585 | } else if (val64 != U_INT64_MIN) { | |
1586 | log_err("parseInt64 returned incorrect value, got: %ld\n", val64); | |
1587 | } | |
1588 | ||
1589 | status = U_ZERO_ERROR; | |
1590 | parsepos = 0; | |
1591 | valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status); | |
1592 | if (U_FAILURE(status)) { | |
1593 | log_err("parseDouble returned error: %s\n", myErrorName(status)); | |
1594 | } else if (valDouble != doubleInt64Min) { | |
1595 | log_err("parseDouble returned incorrect value, got: %g\n", valDouble); | |
1596 | } | |
1597 | } | |
1598 | ||
1599 | unum_formatDouble(fmt, doubleBig, result, 512, NULL, &status); | |
1600 | if (U_FAILURE(status)) { | |
1601 | log_err("error in unum_format(): %s\n", myErrorName(status)); | |
1602 | } else { | |
1603 | log_verbose("format doubleBig: '%s'\n", result); | |
1604 | parsepos = 0; | |
1605 | val32 = unum_parse(fmt, result, u_strlen(result), &parsepos, &status); | |
1606 | if (status != U_INVALID_FORMAT_ERROR) { | |
1607 | log_err("parse didn't report error: %s\n", myErrorName(status)); | |
1608 | } else if (val32 != INT32_MAX) { | |
1609 | log_err("parse didn't pin return value, got: %d\n", val32); | |
1610 | } | |
1611 | ||
1612 | status = U_ZERO_ERROR; | |
1613 | parsepos = 0; | |
1614 | val64 = unum_parseInt64(fmt, result, u_strlen(result), &parsepos, &status); | |
1615 | if (status != U_INVALID_FORMAT_ERROR) { | |
1616 | log_err("parseInt64 didn't report error error: %s\n", myErrorName(status)); | |
1617 | } else if (val64 != U_INT64_MAX) { | |
1618 | log_err("parseInt64 returned incorrect value, got: %ld\n", val64); | |
1619 | } | |
1620 | ||
1621 | status = U_ZERO_ERROR; | |
1622 | parsepos = 0; | |
1623 | valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status); | |
1624 | if (U_FAILURE(status)) { | |
1625 | log_err("parseDouble returned error: %s\n", myErrorName(status)); | |
1626 | } else if (!withinErr(valDouble, doubleBig, 1e-15)) { | |
1627 | log_err("parseDouble returned incorrect value, got: %g\n", valDouble); | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | u_uastrcpy(result, "5.06e-27"); | |
1632 | parsepos = 0; | |
1633 | valDouble = unum_parseDouble(fmt, result, u_strlen(result), &parsepos, &status); | |
1634 | if (U_FAILURE(status)) { | |
1635 | log_err("parseDouble() returned error: %s\n", myErrorName(status)); | |
1636 | } else if (!withinErr(valDouble, 5.06e-27, 1e-15)) { | |
1637 | log_err("parseDouble() returned incorrect value, got: %g\n", valDouble); | |
1638 | } | |
1639 | } | |
1640 | unum_close(fmt); | |
1641 | } | |
1642 | ||
1643 | ||
1644 | static void test_fmt(UNumberFormat* fmt, UBool isDecimal) { | |
1645 | char temp[512]; | |
1646 | UChar buffer[512]; | |
1647 | int32_t BUFSIZE = UPRV_LENGTHOF(buffer); | |
1648 | double vals[] = { | |
1649 | -.2, 0, .2, 5.5, 15.2, 250, 123456789 | |
1650 | }; | |
1651 | int i; | |
1652 | ||
1653 | for (i = 0; i < UPRV_LENGTHOF(vals); ++i) { | |
1654 | UErrorCode status = U_ZERO_ERROR; | |
1655 | unum_formatDouble(fmt, vals[i], buffer, BUFSIZE, NULL, &status); | |
1656 | if (U_FAILURE(status)) { | |
1657 | log_err("failed to format: %g, returned %s\n", vals[i], u_errorName(status)); | |
1658 | } else { | |
1659 | u_austrcpy(temp, buffer); | |
1660 | log_verbose("formatting %g returned '%s'\n", vals[i], temp); | |
1661 | } | |
1662 | } | |
1663 | ||
1664 | /* check APIs now */ | |
1665 | { | |
1666 | UErrorCode status = U_ZERO_ERROR; | |
1667 | UParseError perr; | |
1668 | u_uastrcpy(buffer, "#,##0.0#"); | |
1669 | unum_applyPattern(fmt, FALSE, buffer, -1, &perr, &status); | |
1670 | if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) { | |
1671 | log_err("got unexpected error for applyPattern: '%s'\n", u_errorName(status)); | |
1672 | } | |
1673 | } | |
1674 | ||
1675 | { | |
1676 | int isLenient = unum_getAttribute(fmt, UNUM_LENIENT_PARSE); | |
1677 | log_verbose("lenient: 0x%x\n", isLenient); | |
1678 | if (isLenient != FALSE) { | |
1679 | log_err("didn't expect lenient value: %d\n", isLenient); | |
1680 | } | |
1681 | ||
1682 | unum_setAttribute(fmt, UNUM_LENIENT_PARSE, TRUE); | |
1683 | isLenient = unum_getAttribute(fmt, UNUM_LENIENT_PARSE); | |
1684 | if (isLenient != TRUE) { | |
1685 | log_err("didn't expect lenient value after set: %d\n", isLenient); | |
1686 | } | |
1687 | } | |
1688 | ||
1689 | { | |
1690 | double val2; | |
1691 | double val = unum_getDoubleAttribute(fmt, UNUM_LENIENT_PARSE); | |
1692 | if (val != -1) { | |
1693 | log_err("didn't expect double attribute\n"); | |
1694 | } | |
1695 | val = unum_getDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT); | |
1696 | if ((val == -1) == isDecimal) { | |
1697 | log_err("didn't expect -1 rounding increment\n"); | |
1698 | } | |
1699 | unum_setDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT, val+.5); | |
1700 | val2 = unum_getDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT); | |
1701 | if (isDecimal && (val2 - val != .5)) { | |
1702 | log_err("set rounding increment had no effect on decimal format"); | |
1703 | } | |
1704 | } | |
1705 | ||
1706 | { | |
1707 | UErrorCode status = U_ZERO_ERROR; | |
1708 | int len = unum_getTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, BUFSIZE, &status); | |
1709 | if (isDecimal ? (status != U_UNSUPPORTED_ERROR) : U_FAILURE(status)) { | |
1710 | log_err("got unexpected error for get default ruleset: '%s'\n", u_errorName(status)); | |
1711 | } | |
1712 | if (U_SUCCESS(status)) { | |
1713 | u_austrcpy(temp, buffer); | |
1714 | log_verbose("default ruleset: '%s'\n", temp); | |
1715 | } | |
1716 | ||
1717 | status = U_ZERO_ERROR; | |
1718 | len = unum_getTextAttribute(fmt, UNUM_PUBLIC_RULESETS, buffer, BUFSIZE, &status); | |
1719 | if (isDecimal ? (status != U_UNSUPPORTED_ERROR) : U_FAILURE(status)) { | |
1720 | log_err("got unexpected error for get public rulesets: '%s'\n", u_errorName(status)); | |
1721 | } | |
1722 | if (U_SUCCESS(status)) { | |
1723 | u_austrcpy(temp, buffer); | |
1724 | log_verbose("public rulesets: '%s'\n", temp); | |
1725 | ||
1726 | /* set the default ruleset to the first one found, and retry */ | |
1727 | ||
1728 | if (len > 0) { | |
1729 | for (i = 0; i < len && temp[i] != ';'; ++i){} | |
1730 | if (i < len) { | |
1731 | buffer[i] = 0; | |
1732 | unum_setTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, -1, &status); | |
1733 | if (U_FAILURE(status)) { | |
1734 | log_err("unexpected error setting default ruleset: '%s'\n", u_errorName(status)); | |
1735 | } else { | |
1736 | int len2 = unum_getTextAttribute(fmt, UNUM_DEFAULT_RULESET, buffer, BUFSIZE, &status); | |
1737 | if (U_FAILURE(status)) { | |
1738 | log_err("could not fetch default ruleset: '%s'\n", u_errorName(status)); | |
1739 | } else if (len2 != i) { | |
1740 | u_austrcpy(temp, buffer); | |
1741 | log_err("unexpected ruleset len: %d ex: %d val: %s\n", len2, i, temp); | |
1742 | } else { | |
1743 | for (i = 0; i < UPRV_LENGTHOF(vals); ++i) { | |
1744 | status = U_ZERO_ERROR; | |
1745 | unum_formatDouble(fmt, vals[i], buffer, BUFSIZE, NULL, &status); | |
1746 | if (U_FAILURE(status)) { | |
1747 | log_err("failed to format: %g, returned %s\n", vals[i], u_errorName(status)); | |
1748 | } else { | |
1749 | u_austrcpy(temp, buffer); | |
1750 | log_verbose("formatting %g returned '%s'\n", vals[i], temp); | |
1751 | } | |
1752 | } | |
1753 | } | |
1754 | } | |
1755 | } | |
1756 | } | |
1757 | } | |
1758 | } | |
1759 | ||
1760 | { | |
1761 | UErrorCode status = U_ZERO_ERROR; | |
1762 | unum_toPattern(fmt, FALSE, buffer, BUFSIZE, &status); | |
1763 | if (U_SUCCESS(status)) { | |
1764 | u_austrcpy(temp, buffer); | |
1765 | log_verbose("pattern: '%s'\n", temp); | |
1766 | } else if (status != U_BUFFER_OVERFLOW_ERROR) { | |
1767 | log_err("toPattern failed unexpectedly: %s\n", u_errorName(status)); | |
1768 | } else { | |
1769 | log_verbose("pattern too long to display\n"); | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | { | |
1774 | UErrorCode status = U_ZERO_ERROR; | |
1775 | int len = unum_getSymbol(fmt, UNUM_CURRENCY_SYMBOL, buffer, BUFSIZE, &status); | |
1776 | if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) { | |
1777 | log_err("unexpected error getting symbol: '%s'\n", u_errorName(status)); | |
1778 | } | |
1779 | ||
1780 | unum_setSymbol(fmt, UNUM_CURRENCY_SYMBOL, buffer, len, &status); | |
1781 | if (isDecimal ? U_FAILURE(status) : (status != U_UNSUPPORTED_ERROR)) { | |
1782 | log_err("unexpected error setting symbol: '%s'\n", u_errorName(status)); | |
1783 | } | |
1784 | } | |
1785 | } | |
1786 | ||
1787 | static void TestNonExistentCurrency() { | |
1788 | UNumberFormat *format; | |
1789 | UErrorCode status = U_ZERO_ERROR; | |
1790 | UChar currencySymbol[8]; | |
1791 | static const UChar QQQ[] = {0x51, 0x51, 0x51, 0}; | |
1792 | ||
1793 | /* Get a non-existent currency and make sure it returns the correct currency code. */ | |
1794 | format = unum_open(UNUM_CURRENCY, NULL, 0, "th_TH@currency=QQQ", NULL, &status); | |
1795 | if (format == NULL || U_FAILURE(status)) { | |
1796 | log_data_err("unum_open did not return expected result for non-existent requested currency: '%s' (Are you missing data?)\n", u_errorName(status)); | |
1797 | } | |
1798 | else { | |
1799 | unum_getSymbol(format, | |
1800 | UNUM_CURRENCY_SYMBOL, | |
1801 | currencySymbol, | |
1802 | UPRV_LENGTHOF(currencySymbol), | |
1803 | &status); | |
1804 | if (u_strcmp(currencySymbol, QQQ) != 0) { | |
1805 | log_err("unum_open set the currency to QQQ\n"); | |
1806 | } | |
1807 | } | |
1808 | unum_close(format); | |
1809 | } | |
1810 | ||
1811 | static void TestRBNFFormat() { | |
1812 | UErrorCode status; | |
1813 | UParseError perr; | |
1814 | UChar pat[1024]; | |
1815 | UChar tempUChars[512]; | |
1816 | UNumberFormat *formats[5]; | |
1817 | int COUNT = UPRV_LENGTHOF(formats); | |
1818 | int i; | |
1819 | ||
1820 | for (i = 0; i < COUNT; ++i) { | |
1821 | formats[i] = 0; | |
1822 | } | |
1823 | ||
1824 | /* instantiation */ | |
1825 | status = U_ZERO_ERROR; | |
1826 | u_uastrcpy(pat, "#,##0.0#;(#,##0.0#)"); | |
1827 | formats[0] = unum_open(UNUM_PATTERN_DECIMAL, pat, -1, "en_US", &perr, &status); | |
1828 | if (U_FAILURE(status)) { | |
1829 | log_err_status(status, "unable to open decimal pattern -> %s\n", u_errorName(status)); | |
1830 | return; | |
1831 | } | |
1832 | ||
1833 | status = U_ZERO_ERROR; | |
1834 | formats[1] = unum_open(UNUM_SPELLOUT, NULL, 0, "en_US", &perr, &status); | |
1835 | if (U_FAILURE(status)) { | |
1836 | log_err_status(status, "unable to open spellout -> %s\n", u_errorName(status)); | |
1837 | return; | |
1838 | } | |
1839 | ||
1840 | status = U_ZERO_ERROR; | |
1841 | formats[2] = unum_open(UNUM_ORDINAL, NULL, 0, "en_US", &perr, &status); | |
1842 | if (U_FAILURE(status)) { | |
1843 | log_err_status(status, "unable to open ordinal -> %s\n", u_errorName(status)); | |
1844 | return; | |
1845 | } | |
1846 | ||
1847 | status = U_ZERO_ERROR; | |
1848 | formats[3] = unum_open(UNUM_DURATION, NULL, 0, "en_US", &perr, &status); | |
1849 | if (U_FAILURE(status)) { | |
1850 | log_err_status(status, "unable to open duration %s\n", u_errorName(status)); | |
1851 | return; | |
1852 | } | |
1853 | ||
1854 | status = U_ZERO_ERROR; | |
1855 | u_uastrcpy(pat, | |
1856 | "%standard:\n" | |
1857 | "-x: minus >>;\n" | |
1858 | "x.x: << point >>;\n" | |
1859 | "zero; one; two; three; four; five; six; seven; eight; nine;\n" | |
1860 | "ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n" | |
1861 | "seventeen; eighteen; nineteen;\n" | |
1862 | "20: twenty[->>];\n" | |
1863 | "30: thirty[->>];\n" | |
1864 | "40: forty[->>];\n" | |
1865 | "50: fifty[->>];\n" | |
1866 | "60: sixty[->>];\n" | |
1867 | "70: seventy[->>];\n" | |
1868 | "80: eighty[->>];\n" | |
1869 | "90: ninety[->>];\n" | |
1870 | "100: =#,##0=;\n"); | |
1871 | u_uastrcpy(tempUChars, | |
1872 | "%simple:\n" | |
1873 | "=%standard=;\n" | |
1874 | "20: twenty[ and change];\n" | |
1875 | "30: thirty[ and change];\n" | |
1876 | "40: forty[ and change];\n" | |
1877 | "50: fifty[ and change];\n" | |
1878 | "60: sixty[ and change];\n" | |
1879 | "70: seventy[ and change];\n" | |
1880 | "80: eighty[ and change];\n" | |
1881 | "90: ninety[ and change];\n" | |
1882 | "100: =#,##0=;\n" | |
1883 | "%bogus:\n" | |
1884 | "0.x: tiny;\n" | |
1885 | "x.x: << point something;\n" | |
1886 | "=%standard=;\n" | |
1887 | "20: some reasonable number;\n" | |
1888 | "100: some substantial number;\n" | |
1889 | "100,000,000: some huge number;\n"); | |
1890 | /* This is to get around some compiler warnings about char * string length. */ | |
1891 | u_strcat(pat, tempUChars); | |
1892 | formats[4] = unum_open(UNUM_PATTERN_RULEBASED, pat, -1, "en_US", &perr, &status); | |
1893 | if (U_FAILURE(status)) { | |
1894 | log_err_status(status, "unable to open rulebased pattern -> %s\n", u_errorName(status)); | |
1895 | } | |
1896 | if (U_FAILURE(status)) { | |
1897 | log_err_status(status, "Something failed with %s\n", u_errorName(status)); | |
1898 | return; | |
1899 | } | |
1900 | ||
1901 | for (i = 0; i < COUNT; ++i) { | |
1902 | log_verbose("\n\ntesting format %d\n", i); | |
1903 | test_fmt(formats[i], (UBool)(i == 0)); | |
1904 | } | |
1905 | ||
1906 | #define FORMAT_BUF_CAPACITY 64 | |
1907 | { | |
1908 | UChar fmtbuf[FORMAT_BUF_CAPACITY]; | |
1909 | int32_t len; | |
1910 | double nanvalue = uprv_getNaN(); | |
1911 | status = U_ZERO_ERROR; | |
1912 | len = unum_formatDouble(formats[1], nanvalue, fmtbuf, FORMAT_BUF_CAPACITY, NULL, &status); | |
1913 | if (U_FAILURE(status)) { | |
1914 | log_err_status(status, "unum_formatDouble NAN failed with %s\n", u_errorName(status)); | |
1915 | } else { | |
1916 | UChar nansym[] = { 0x4E, 0x61, 0x4E, 0 }; /* NaN */ | |
1917 | if ( len != 3 || u_strcmp(fmtbuf, nansym) != 0 ) { | |
1918 | log_err("unum_formatDouble NAN produced wrong answer for en_US\n"); | |
1919 | } | |
1920 | } | |
1921 | } | |
1922 | ||
1923 | for (i = 0; i < COUNT; ++i) { | |
1924 | unum_close(formats[i]); | |
1925 | } | |
1926 | } | |
1927 | ||
1928 | static void TestRBNFRounding() { | |
1929 | UChar fmtbuf[FORMAT_BUF_CAPACITY]; | |
1930 | UChar expectedBuf[FORMAT_BUF_CAPACITY]; | |
1931 | int32_t len; | |
1932 | UErrorCode status = U_ZERO_ERROR; | |
1933 | UNumberFormat* fmt = unum_open(UNUM_SPELLOUT, NULL, 0, "en_US", NULL, &status); | |
1934 | if (U_FAILURE(status)) { | |
1935 | log_err_status(status, "unable to open spellout -> %s\n", u_errorName(status)); | |
1936 | return; | |
1937 | } | |
1938 | len = unum_formatDouble(fmt, 10.123456789, fmtbuf, FORMAT_BUF_CAPACITY, NULL, &status); | |
1939 | U_ASSERT(len < FORMAT_BUF_CAPACITY); | |
1940 | (void)len; | |
1941 | if (U_FAILURE(status)) { | |
1942 | log_err_status(status, "unum_formatDouble 10.123456789 failed with %s\n", u_errorName(status)); | |
1943 | } | |
1944 | u_uastrcpy(expectedBuf, "ten point one two three four five six seven eight nine"); | |
1945 | if (u_strcmp(expectedBuf, fmtbuf) != 0) { | |
1946 | log_err("Wrong result for unrounded value\n"); | |
1947 | } | |
1948 | unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 3); | |
1949 | if (unum_getAttribute(fmt, UNUM_MAX_FRACTION_DIGITS) != 3) { | |
1950 | log_err("UNUM_MAX_FRACTION_DIGITS was incorrectly ignored -> %d\n", unum_getAttribute(fmt, UNUM_MAX_FRACTION_DIGITS)); | |
1951 | } | |
1952 | if (unum_getAttribute(fmt, UNUM_ROUNDING_MODE) != UNUM_ROUND_UNNECESSARY) { | |
1953 | log_err("UNUM_ROUNDING_MODE was set -> %d\n", unum_getAttribute(fmt, UNUM_ROUNDING_MODE)); | |
1954 | } | |
1955 | unum_setAttribute(fmt, UNUM_ROUNDING_MODE, UNUM_ROUND_HALFUP); | |
1956 | if (unum_getAttribute(fmt, UNUM_ROUNDING_MODE) != UNUM_ROUND_HALFUP) { | |
1957 | log_err("UNUM_ROUNDING_MODE was not set -> %d\n", unum_getAttribute(fmt, UNUM_ROUNDING_MODE)); | |
1958 | } | |
1959 | len = unum_formatDouble(fmt, 10.123456789, fmtbuf, FORMAT_BUF_CAPACITY, NULL, &status); | |
1960 | U_ASSERT(len < FORMAT_BUF_CAPACITY); | |
1961 | if (U_FAILURE(status)) { | |
1962 | log_err_status(status, "unum_formatDouble 10.123456789 failed with %s\n", u_errorName(status)); | |
1963 | } | |
1964 | u_uastrcpy(expectedBuf, "ten point one two three"); | |
1965 | if (u_strcmp(expectedBuf, fmtbuf) != 0) { | |
1966 | char temp[512]; | |
1967 | u_austrcpy(temp, fmtbuf); | |
1968 | log_err("Wrong result for rounded value. Got: %s\n", temp); | |
1969 | } | |
1970 | unum_close(fmt); | |
1971 | } | |
1972 | ||
1973 | static void TestCurrencyRegression(void) { | |
1974 | /* | |
1975 | I've found a case where unum_parseDoubleCurrency is not doing what I | |
1976 | expect. The value I pass in is $1234567890q123460000.00 and this | |
1977 | returns with a status of zero error & a parse pos of 22 (I would | |
1978 | expect a parse error at position 11). | |
1979 | ||
1980 | I stepped into DecimalFormat::subparse() and it looks like it parses | |
1981 | the first 10 digits and then stops parsing at the q but doesn't set an | |
1982 | error. Then later in DecimalFormat::parse() the value gets crammed | |
1983 | into a long (which greatly truncates the value). | |
1984 | ||
1985 | This is very problematic for me 'cause I try to remove chars that are | |
1986 | invalid but this allows my users to enter bad chars and truncates | |
1987 | their data! | |
1988 | */ | |
1989 | ||
1990 | UChar buf[1024]; | |
1991 | UChar currency[8]; | |
1992 | char acurrency[16]; | |
1993 | double d; | |
1994 | UNumberFormat *cur; | |
1995 | int32_t pos; | |
1996 | UErrorCode status = U_ZERO_ERROR; | |
1997 | const int32_t expected = 11; | |
1998 | ||
1999 | currency[0]=0; | |
2000 | u_uastrcpy(buf, "$1234567890q643210000.00"); | |
2001 | cur = unum_open(UNUM_CURRENCY, NULL,0,"en_US", NULL, &status); | |
2002 | ||
2003 | if(U_FAILURE(status)) { | |
2004 | log_data_err("unum_open failed: %s (Are you missing data?)\n", u_errorName(status)); | |
2005 | return; | |
2006 | } | |
2007 | ||
2008 | status = U_ZERO_ERROR; /* so we can test it later. */ | |
2009 | pos = 0; | |
2010 | ||
2011 | d = unum_parseDoubleCurrency(cur, | |
2012 | buf, | |
2013 | -1, | |
2014 | &pos, /* 0 = start */ | |
2015 | currency, | |
2016 | &status); | |
2017 | ||
2018 | u_austrcpy(acurrency, currency); | |
2019 | ||
2020 | if(U_FAILURE(status) || (pos != expected)) { | |
2021 | log_err("unum_parseDoubleCurrency should have failed with pos %d, but gave: value %.9f, err %s, pos=%d, currency [%s]\n", | |
2022 | expected, d, u_errorName(status), pos, acurrency); | |
2023 | } else { | |
2024 | log_verbose("unum_parseDoubleCurrency failed, value %.9f err %s, pos %d, currency [%s]\n", d, u_errorName(status), pos, acurrency); | |
2025 | } | |
2026 | ||
2027 | unum_close(cur); | |
2028 | } | |
2029 | ||
2030 | static void TestTextAttributeCrash(void) { | |
2031 | UChar ubuffer[64] = {0x0049,0x004E,0x0052,0}; | |
2032 | static const UChar expectedNeg[] = {0x0049,0x004E,0x0052,0x0031,0x0032,0x0033,0x0034,0x002E,0x0035,0}; | |
2033 | static const UChar expectedPos[] = {0x0031,0x0032,0x0033,0x0034,0x002E,0x0035,0}; | |
2034 | int32_t used; | |
2035 | UErrorCode status = U_ZERO_ERROR; | |
2036 | UNumberFormat *nf = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status); | |
2037 | if (U_FAILURE(status)) { | |
2038 | log_data_err("FAILED 1 -> %s (Are you missing data?)\n", u_errorName(status)); | |
2039 | return; | |
2040 | } | |
2041 | unum_setTextAttribute(nf, UNUM_CURRENCY_CODE, ubuffer, 3, &status); | |
2042 | /* | |
2043 | * the usual negative prefix and suffix seem to be '($' and ')' at this point | |
2044 | * also crashes if UNUM_NEGATIVE_SUFFIX is substituted for UNUM_NEGATIVE_PREFIX here | |
2045 | */ | |
2046 | used = unum_getTextAttribute(nf, UNUM_NEGATIVE_PREFIX, ubuffer, 64, &status); | |
2047 | unum_setTextAttribute(nf, UNUM_NEGATIVE_PREFIX, ubuffer, used, &status); | |
2048 | if (U_FAILURE(status)) { | |
2049 | log_err("FAILED 2\n"); exit(1); | |
2050 | } | |
2051 | log_verbose("attempting to format...\n"); | |
2052 | used = unum_formatDouble(nf, -1234.5, ubuffer, 64, NULL, &status); | |
2053 | if (U_FAILURE(status) || 64 < used) { | |
2054 | log_err("Failed formatting %s\n", u_errorName(status)); | |
2055 | return; | |
2056 | } | |
2057 | if (u_strcmp(expectedNeg, ubuffer) == 0) { | |
2058 | log_err("Didn't get expected negative result\n"); | |
2059 | } | |
2060 | used = unum_formatDouble(nf, 1234.5, ubuffer, 64, NULL, &status); | |
2061 | if (U_FAILURE(status) || 64 < used) { | |
2062 | log_err("Failed formatting %s\n", u_errorName(status)); | |
2063 | return; | |
2064 | } | |
2065 | if (u_strcmp(expectedPos, ubuffer) == 0) { | |
2066 | log_err("Didn't get expected positive result\n"); | |
2067 | } | |
2068 | unum_close(nf); | |
2069 | } | |
2070 | ||
2071 | static void TestNBSPPatternRtNum(const char *testcase, int line, UNumberFormat *nf, double myNumber) { | |
2072 | UErrorCode status = U_ZERO_ERROR; | |
2073 | UChar myString[20]; | |
2074 | char tmpbuf[200]; | |
2075 | double aNumber = -1.0; | |
2076 | unum_formatDouble(nf, myNumber, myString, 20, NULL, &status); | |
2077 | log_verbose("%s:%d: formatted %.2f into %s\n", testcase, line, myNumber, u_austrcpy(tmpbuf, myString)); | |
2078 | if(U_FAILURE(status)) { | |
2079 | log_err("%s:%d: failed format of %.2g with %s\n", testcase, line, myNumber, u_errorName(status)); | |
2080 | return; | |
2081 | } | |
2082 | aNumber = unum_parse(nf, myString, -1, NULL, &status); | |
2083 | if(U_FAILURE(status)) { | |
2084 | log_err("%s:%d: failed parse with %s\n", testcase, line, u_errorName(status)); | |
2085 | return; | |
2086 | } | |
2087 | if(uprv_fabs(aNumber-myNumber)>.001) { | |
2088 | log_err("FAIL: %s:%d formatted %.2f, parsed into %.2f\n", testcase, line, myNumber, aNumber); | |
2089 | } else { | |
2090 | log_verbose("PASS: %s:%d formatted %.2f, parsed into %.2f\n", testcase, line, myNumber, aNumber); | |
2091 | } | |
2092 | } | |
2093 | ||
2094 | static void TestNBSPPatternRT(const char *testcase, UNumberFormat *nf) { | |
2095 | TestNBSPPatternRtNum(testcase, __LINE__, nf, 12345.); | |
2096 | TestNBSPPatternRtNum(testcase, __LINE__, nf, -12345.); | |
2097 | } | |
2098 | ||
2099 | static void TestNBSPInPattern(void) { | |
2100 | UErrorCode status = U_ZERO_ERROR; | |
2101 | UNumberFormat* nf = NULL; | |
2102 | const char *testcase; | |
2103 | ||
2104 | ||
2105 | testcase="ar_AE UNUM_CURRENCY"; | |
2106 | nf = unum_open(UNUM_CURRENCY, NULL, -1, "ar_AE", NULL, &status); | |
2107 | if(U_FAILURE(status) || nf == NULL) { | |
2108 | log_data_err("%s:%d: %s: unum_open failed with %s (Are you missing data?)\n", __FILE__, __LINE__, testcase, u_errorName(status)); | |
2109 | return; | |
2110 | } | |
2111 | TestNBSPPatternRT(testcase, nf); | |
2112 | ||
2113 | /* if we don't have CLDR 1.6 data, bring out the problem anyways */ | |
2114 | { | |
2115 | #define SPECIAL_PATTERN "\\u00A4\\u00A4'\\u062f.\\u0625.\\u200f\\u00a0'###0.00" | |
2116 | UChar pat[200]; | |
2117 | testcase = "ar_AE special pattern: " SPECIAL_PATTERN; | |
2118 | u_unescape(SPECIAL_PATTERN, pat, UPRV_LENGTHOF(pat)); | |
2119 | unum_applyPattern(nf, FALSE, pat, -1, NULL, &status); | |
2120 | if(U_FAILURE(status)) { | |
2121 | log_err("%s: unum_applyPattern failed with %s\n", testcase, u_errorName(status)); | |
2122 | } else { | |
2123 | TestNBSPPatternRT(testcase, nf); | |
2124 | } | |
2125 | #undef SPECIAL_PATTERN | |
2126 | } | |
2127 | unum_close(nf); status = U_ZERO_ERROR; | |
2128 | ||
2129 | testcase="ar_AE UNUM_DECIMAL"; | |
2130 | nf = unum_open(UNUM_DECIMAL, NULL, -1, "ar_AE", NULL, &status); | |
2131 | if(U_FAILURE(status)) { | |
2132 | log_err("%s: unum_open failed with %s\n", testcase, u_errorName(status)); | |
2133 | } | |
2134 | TestNBSPPatternRT(testcase, nf); | |
2135 | unum_close(nf); status = U_ZERO_ERROR; | |
2136 | ||
2137 | testcase="ar_AE UNUM_PERCENT"; | |
2138 | nf = unum_open(UNUM_PERCENT, NULL, -1, "ar_AE", NULL, &status); | |
2139 | if(U_FAILURE(status)) { | |
2140 | log_err("%s: unum_open failed with %s\n", testcase, u_errorName(status)); | |
2141 | } | |
2142 | TestNBSPPatternRT(testcase, nf); | |
2143 | unum_close(nf); status = U_ZERO_ERROR; | |
2144 | ||
2145 | ||
2146 | ||
2147 | } | |
2148 | static void TestCloneWithRBNF(void) { | |
2149 | UChar pattern[1024]; | |
2150 | UChar pat2[512]; | |
2151 | UErrorCode status = U_ZERO_ERROR; | |
2152 | UChar buffer[256]; | |
2153 | UChar buffer_cloned[256]; | |
2154 | char temp1[256]; | |
2155 | char temp2[256]; | |
2156 | UNumberFormat *pform_cloned; | |
2157 | UNumberFormat *pform; | |
2158 | ||
2159 | u_uastrcpy(pattern, | |
2160 | "%main:\n" | |
2161 | "0.x: >%%millis-only>;\n" | |
2162 | "x.0: <%%duration<;\n" | |
2163 | "x.x: <%%durationwithmillis<>%%millis-added>;\n" | |
2164 | "-x: ->>;%%millis-only:\n" | |
2165 | "1000: 00:00.<%%millis<;\n" | |
2166 | "%%millis-added:\n" | |
2167 | "1000: .<%%millis<;\n" | |
2168 | "%%millis:\n" | |
2169 | "0: =000=;\n" | |
2170 | "%%duration:\n" | |
2171 | "0: =%%seconds-only=;\n" | |
2172 | "60: =%%min-sec=;\n" | |
2173 | "3600: =%%hr-min-sec=;\n" | |
2174 | "86400/86400: <%%ddaayyss<[, >>];\n" | |
2175 | "%%durationwithmillis:\n" | |
2176 | "0: =%%seconds-only=;\n" | |
2177 | "60: =%%min-sec=;\n" | |
2178 | "3600: =%%hr-min-sec=;\n" | |
2179 | "86400/86400: <%%ddaayyss<, >>;\n"); | |
2180 | u_uastrcpy(pat2, | |
2181 | "%%seconds-only:\n" | |
2182 | "0: 0:00:=00=;\n" | |
2183 | "%%min-sec:\n" | |
2184 | "0: :=00=;\n" | |
2185 | "0/60: 0:<00<>>;\n" | |
2186 | "%%hr-min-sec:\n" | |
2187 | "0: :=00=;\n" | |
2188 | "60/60: <00<>>;\n" | |
2189 | "3600/60: <0<:>>>;\n" | |
2190 | "%%ddaayyss:\n" | |
2191 | "0 days;\n" | |
2192 | "1 day;\n" | |
2193 | "=0= days;"); | |
2194 | ||
2195 | /* This is to get around some compiler warnings about char * string length. */ | |
2196 | u_strcat(pattern, pat2); | |
2197 | ||
2198 | pform = unum_open(UNUM_PATTERN_RULEBASED, pattern, -1, "en_US", NULL, &status); | |
2199 | unum_formatDouble(pform, 3600, buffer, 256, NULL, &status); | |
2200 | ||
2201 | pform_cloned = unum_clone(pform,&status); | |
2202 | unum_formatDouble(pform_cloned, 3600, buffer_cloned, 256, NULL, &status); | |
2203 | ||
2204 | unum_close(pform); | |
2205 | unum_close(pform_cloned); | |
2206 | ||
2207 | if (u_strcmp(buffer,buffer_cloned)) { | |
2208 | 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)); | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | ||
2213 | static void TestNoExponent(void) { | |
2214 | UErrorCode status = U_ZERO_ERROR; | |
2215 | UChar str[100]; | |
2216 | const char *cstr; | |
2217 | UNumberFormat *fmt; | |
2218 | int32_t pos; | |
2219 | int32_t expect = 0; | |
2220 | int32_t num; | |
2221 | ||
2222 | fmt = unum_open(UNUM_DECIMAL, NULL, -1, "en_US", NULL, &status); | |
2223 | ||
2224 | if(U_FAILURE(status) || fmt == NULL) { | |
2225 | log_data_err("%s:%d: unum_open failed with %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status)); | |
2226 | return; | |
2227 | } | |
2228 | ||
2229 | cstr = "10E6"; | |
2230 | u_uastrcpy(str, cstr); | |
2231 | expect = 10000000; | |
2232 | pos = 0; | |
2233 | num = unum_parse(fmt, str, -1, &pos, &status); | |
2234 | ASSERT_TRUE(pos==4); | |
2235 | if(U_FAILURE(status)) { | |
2236 | log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status), cstr); | |
2237 | } else if(expect!=num) { | |
2238 | log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__, __LINE__, num, expect, cstr); | |
2239 | } else { | |
2240 | log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__, __LINE__, num, cstr); | |
2241 | } | |
2242 | ||
2243 | ASSERT_TRUE(unum_getAttribute(fmt, UNUM_PARSE_NO_EXPONENT)==0); | |
2244 | ||
2245 | unum_setAttribute(fmt, UNUM_PARSE_NO_EXPONENT, 1); /* no error code */ | |
2246 | log_verbose("set UNUM_PARSE_NO_EXPONENT\n"); | |
2247 | ||
2248 | ASSERT_TRUE(unum_getAttribute(fmt, UNUM_PARSE_NO_EXPONENT)==1); | |
2249 | ||
2250 | pos = 0; | |
2251 | expect=10; | |
2252 | num = unum_parse(fmt, str, -1, &pos, &status); | |
2253 | if(num==10000000) { | |
2254 | log_err("%s:%d: FAIL: unum_parse should have returned 10, not 10000000 on %s after UNUM_PARSE_NO_EXPONENT\n", __FILE__, __LINE__, cstr); | |
2255 | } else if(num==expect) { | |
2256 | log_verbose("%s:%d: unum_parse gave %d for %s - good.\n", __FILE__, __LINE__, num, cstr); | |
2257 | } | |
2258 | ASSERT_TRUE(pos==2); | |
2259 | ||
2260 | status = U_ZERO_ERROR; | |
2261 | ||
2262 | unum_close(fmt); | |
2263 | ||
2264 | /* ok, now try scientific */ | |
2265 | fmt = unum_open(UNUM_SCIENTIFIC, NULL, -1, "en_US", NULL, &status); | |
2266 | assertSuccess("unum_open(UNUM_SCIENTIFIC, ...)", &status); | |
2267 | ||
2268 | ASSERT_TRUE(unum_getAttribute(fmt, UNUM_PARSE_NO_EXPONENT)==0); | |
2269 | ||
2270 | cstr = "10E6"; | |
2271 | u_uastrcpy(str, cstr); | |
2272 | expect = 10000000; | |
2273 | pos = 0; | |
2274 | num = unum_parse(fmt, str, -1, &pos, &status); | |
2275 | ASSERT_TRUE(pos==4); | |
2276 | if(U_FAILURE(status)) { | |
2277 | log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status), cstr); | |
2278 | } else if(expect!=num) { | |
2279 | log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__, __LINE__, num, expect, cstr); | |
2280 | } else { | |
2281 | log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__, __LINE__, num, cstr); | |
2282 | } | |
2283 | ||
2284 | unum_setAttribute(fmt, UNUM_PARSE_NO_EXPONENT, 1); /* no error code */ | |
2285 | log_verbose("set UNUM_PARSE_NO_EXPONENT\n"); | |
2286 | ||
2287 | ASSERT_TRUE(unum_getAttribute(fmt, UNUM_PARSE_NO_EXPONENT)==1); | |
2288 | ||
2289 | // A scientific formatter should parse the exponent even if UNUM_PARSE_NO_EXPONENT is set | |
2290 | cstr = "10E6"; | |
2291 | u_uastrcpy(str, cstr); | |
2292 | expect = 10000000; | |
2293 | pos = 0; | |
2294 | num = unum_parse(fmt, str, -1, &pos, &status); | |
2295 | ASSERT_TRUE(pos==4); | |
2296 | if(U_FAILURE(status)) { | |
2297 | log_data_err("%s:%d: unum_parse failed with %s for %s (Are you missing data?)\n", __FILE__, __LINE__, u_errorName(status), cstr); | |
2298 | } else if(expect!=num) { | |
2299 | log_data_err("%s:%d: unum_parse failed, got %d expected %d for '%s'(Are you missing data?)\n", __FILE__, __LINE__, num, expect, cstr); | |
2300 | } else { | |
2301 | log_verbose("%s:%d: unum_parse returned %d for '%s'\n", __FILE__, __LINE__, num, cstr); | |
2302 | } | |
2303 | ||
2304 | unum_close(fmt); | |
2305 | } | |
2306 | ||
2307 | static void TestMaxInt(void) { | |
2308 | UErrorCode status = U_ZERO_ERROR; | |
2309 | UChar pattern_hash[] = { 0x23, 0x00 }; /* "#" */ | |
2310 | UChar result1[1024] = { 0 }, result2[1024] = { 0 }; | |
2311 | int32_t len1, len2; | |
2312 | UChar expect[] = { 0x0039, 0x0037, 0 }; | |
2313 | UNumberFormat *fmt = unum_open( | |
2314 | UNUM_PATTERN_DECIMAL, /* style */ | |
2315 | &pattern_hash[0], /* pattern */ | |
2316 | u_strlen(pattern_hash), /* patternLength */ | |
2317 | "en", | |
2318 | 0, /* parseErr */ | |
2319 | &status); | |
2320 | if(U_FAILURE(status) || fmt == NULL) { | |
2321 | log_data_err("%s:%d: %s: unum_open failed with %s (Are you missing data?)\n", __FILE__, __LINE__, "TestMaxInt", u_errorName(status)); | |
2322 | return; | |
2323 | } | |
2324 | ||
2325 | unum_setAttribute(fmt, UNUM_MAX_INTEGER_DIGITS, 2); | |
2326 | ||
2327 | status = U_ZERO_ERROR; | |
2328 | /* #1 */ | |
2329 | len1 = unum_formatInt64(fmt, 1997, result1, 1024, NULL, &status); | |
2330 | result1[len1]=0; | |
2331 | if(U_FAILURE(status) || u_strcmp(expect, result1)) { | |
2332 | log_err("unum_formatInt64 Expected %s but got %s status %s\n", austrdup(expect), austrdup(result1), u_errorName(status)); | |
2333 | } | |
2334 | ||
2335 | status = U_ZERO_ERROR; | |
2336 | /* #2 */ | |
2337 | len2 = unum_formatDouble(fmt, 1997.0, result2, 1024, NULL, &status); | |
2338 | result2[len2]=0; | |
2339 | if(U_FAILURE(status) || u_strcmp(expect, result2)) { | |
2340 | log_err("unum_formatDouble Expected %s but got %s status %s\n", austrdup(expect), austrdup(result2), u_errorName(status)); | |
2341 | } | |
2342 | ||
2343 | ||
2344 | ||
2345 | /* test UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS */ | |
2346 | ASSERT_TRUE(unum_getAttribute(fmt, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS)==0); | |
2347 | ||
2348 | unum_setAttribute(fmt, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS, 1); | |
2349 | /* test UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS */ | |
2350 | ASSERT_TRUE(unum_getAttribute(fmt, UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS)==1); | |
2351 | ||
2352 | status = U_ZERO_ERROR; | |
2353 | /* max int digits still '2' */ | |
2354 | len1 = unum_formatInt64(fmt, 1997, result1, 1024, NULL, &status); | |
2355 | ASSERT_TRUE(status==U_ILLEGAL_ARGUMENT_ERROR); | |
2356 | status = U_ZERO_ERROR; | |
2357 | ||
2358 | /* But, formatting 97->'97' works fine. */ | |
2359 | ||
2360 | /* #1 */ | |
2361 | len1 = unum_formatInt64(fmt, 97, result1, 1024, NULL, &status); | |
2362 | result1[len1]=0; | |
2363 | if(U_FAILURE(status) || u_strcmp(expect, result1)) { | |
2364 | log_err("unum_formatInt64 Expected %s but got %s status %s\n", austrdup(expect), austrdup(result1), u_errorName(status)); | |
2365 | } | |
2366 | ||
2367 | status = U_ZERO_ERROR; | |
2368 | /* #2 */ | |
2369 | len2 = unum_formatDouble(fmt, 97.0, result2, 1024, NULL, &status); | |
2370 | result2[len2]=0; | |
2371 | if(U_FAILURE(status) || u_strcmp(expect, result2)) { | |
2372 | log_err("unum_formatDouble Expected %s but got %s status %s\n", austrdup(expect), austrdup(result2), u_errorName(status)); | |
2373 | } | |
2374 | ||
2375 | ||
2376 | unum_close(fmt); | |
2377 | } | |
2378 | ||
2379 | static void TestSignAlwaysShown(void) { | |
2380 | UErrorCode status = U_ZERO_ERROR; | |
2381 | UNumberFormat *fmt = unum_open( | |
2382 | UNUM_DECIMAL, /* style */ | |
2383 | NULL, /* pattern */ | |
2384 | 0, /* patternLength */ | |
2385 | "en-US", | |
2386 | NULL, /* parseErr */ | |
2387 | &status); | |
2388 | assertSuccess("Creating UNumberFormat", &status); | |
2389 | unum_setAttribute(fmt, UNUM_SIGN_ALWAYS_SHOWN, 1); | |
2390 | UChar result[100]; | |
2391 | unum_formatDouble(fmt, 42, result, 100, NULL, &status); | |
2392 | assertSuccess("Formatting with UNumberFormat", &status); | |
2393 | assertUEquals("Result with sign always shown", u"+42", result); | |
2394 | unum_close(fmt); | |
2395 | } | |
2396 | ||
2397 | static void TestMinimumGroupingDigits(void) { | |
2398 | UErrorCode status = U_ZERO_ERROR; | |
2399 | UNumberFormat *fmt = unum_open( | |
2400 | UNUM_DECIMAL, /* style */ | |
2401 | NULL, /* pattern */ | |
2402 | 0, /* patternLength */ | |
2403 | "en-US", | |
2404 | NULL, /* parseErr */ | |
2405 | &status); | |
2406 | assertSuccess("Creating UNumberFormat", &status); | |
2407 | unum_setAttribute(fmt, UNUM_MINIMUM_GROUPING_DIGITS, 2); | |
2408 | UChar result[100]; | |
2409 | unum_formatDouble(fmt, 1234, result, 100, NULL, &status); | |
2410 | assertSuccess("Formatting with UNumberFormat A", &status); | |
2411 | assertUEquals("Result with minimum grouping digits A", u"1234", result); | |
2412 | unum_formatDouble(fmt, 12345, result, 100, NULL, &status); | |
2413 | assertSuccess("Formatting with UNumberFormat B", &status); | |
2414 | assertUEquals("Result with minimum grouping digits B", u"12,345", result); | |
2415 | unum_close(fmt); | |
2416 | } | |
2417 | ||
2418 | static void TestParseCaseSensitive(void) { | |
2419 | UErrorCode status = U_ZERO_ERROR; | |
2420 | UNumberFormat *fmt = unum_open( | |
2421 | UNUM_DECIMAL, /* style */ | |
2422 | NULL, /* pattern */ | |
2423 | 0, /* patternLength */ | |
2424 | "en-US", | |
2425 | NULL, /* parseErr */ | |
2426 | &status); | |
2427 | assertSuccess("Creating UNumberFormat", &status); | |
2428 | double result = unum_parseDouble(fmt, u"1e2", -1, NULL, &status); | |
2429 | assertSuccess("Parsing with UNumberFormat, case insensitive", &status); | |
2430 | assertIntEquals("Result with case sensitive", 100, (int64_t)result); | |
2431 | unum_setAttribute(fmt, UNUM_PARSE_CASE_SENSITIVE, 1); | |
2432 | int32_t ppos = 0; | |
2433 | result = unum_parseDouble(fmt, u"1e2", -1, &ppos, &status); | |
2434 | assertSuccess("Parsing with UNumberFormat, case sensitive", &status); | |
2435 | assertIntEquals("Position with case sensitive", 1, ppos); | |
2436 | assertIntEquals("Result with case sensitive", 1, (int64_t)result); | |
2437 | unum_close(fmt); | |
2438 | } | |
2439 | ||
2440 | static void TestUFormattable(void) { | |
2441 | UChar out2k[2048]; | |
2442 | // simple test for API docs | |
2443 | { | |
2444 | UErrorCode status = U_ZERO_ERROR; | |
2445 | UNumberFormat *unum = unum_open(UNUM_DEFAULT, NULL, -1, "en_US_POSIX", NULL, &status); | |
2446 | if(assertSuccessCheck("calling unum_open()", &status, TRUE)) { | |
2447 | //! [unum_parseToUFormattable] | |
2448 | const UChar str[] = { 0x0031, 0x0032, 0x0033, 0x0000 }; /* 123 */ | |
2449 | int32_t result = 0; | |
2450 | UFormattable *ufmt = ufmt_open(&status); | |
2451 | unum_parseToUFormattable(unum, ufmt, str, -1, NULL, &status); | |
2452 | if (ufmt_isNumeric(ufmt)) { | |
2453 | result = ufmt_getLong(ufmt, &status); /* == 123 */ | |
2454 | } /* else { ... } */ | |
2455 | ufmt_close(ufmt); | |
2456 | //! [unum_parseToUFormattable] | |
2457 | assertTrue("result == 123", (result == 123)); | |
2458 | } | |
2459 | unum_close(unum); | |
2460 | } | |
2461 | // test with explicitly created ufmt_open | |
2462 | { | |
2463 | UChar buffer[2048]; | |
2464 | UErrorCode status = U_ZERO_ERROR; | |
2465 | UFormattable *ufmt; | |
2466 | UNumberFormat *unum; | |
2467 | const char *pattern = ""; | |
2468 | ||
2469 | ufmt = ufmt_open(&status); | |
2470 | unum = unum_open(UNUM_DEFAULT, NULL, -1, "en_US_POSIX", NULL, &status); | |
2471 | if(assertSuccessCheck("calling ufmt_open() || unum_open()", &status, TRUE)) { | |
2472 | ||
2473 | pattern = "31337"; | |
2474 | log_verbose("-- pattern: %s\n", pattern); | |
2475 | u_uastrcpy(buffer, pattern); | |
2476 | unum_parseToUFormattable(unum, ufmt, buffer, -1, NULL, &status); | |
2477 | if(assertSuccess("unum_parseToUFormattable(31337)", &status)) { | |
2478 | assertTrue("ufmt_getLong()=31337", ufmt_getLong(ufmt, &status) == 31337); | |
2479 | assertTrue("ufmt_getType()=UFMT_LONG", ufmt_getType(ufmt, &status) == UFMT_LONG); | |
2480 | log_verbose("long = %d\n", ufmt_getLong(ufmt, &status)); | |
2481 | assertSuccess("ufmt_getLong()", &status); | |
2482 | } | |
2483 | unum_formatUFormattable(unum, ufmt, out2k, 2048, NULL, &status); | |
2484 | if(assertSuccess("unum_formatUFormattable(31337)", &status)) { | |
2485 | assertEquals("unum_formatUFormattable r/t", austrdup(buffer), austrdup(out2k)); | |
2486 | } | |
2487 | ||
2488 | pattern = "3.14159"; | |
2489 | log_verbose("-- pattern: %s\n", pattern); | |
2490 | u_uastrcpy(buffer, pattern); | |
2491 | unum_parseToUFormattable(unum, ufmt, buffer, -1, NULL, &status); | |
2492 | if(assertSuccess("unum_parseToUFormattable(3.14159)", &status)) { | |
2493 | assertTrue("ufmt_getDouble()==3.14159", withinErr(ufmt_getDouble(ufmt, &status), 3.14159, 1e-15)); | |
2494 | assertSuccess("ufmt_getDouble()", &status); | |
2495 | assertTrue("ufmt_getType()=UFMT_DOUBLE", ufmt_getType(ufmt, &status) == UFMT_DOUBLE); | |
2496 | log_verbose("double = %g\n", ufmt_getDouble(ufmt, &status)); | |
2497 | } | |
2498 | unum_formatUFormattable(unum, ufmt, out2k, 2048, NULL, &status); | |
2499 | if(assertSuccess("unum_formatUFormattable(3.14159)", &status)) { | |
2500 | assertEquals("unum_formatUFormattable r/t", austrdup(buffer), austrdup(out2k)); | |
2501 | } | |
2502 | } | |
2503 | ufmt_close(ufmt); | |
2504 | unum_close(unum); | |
2505 | } | |
2506 | ||
2507 | // test with auto-generated ufmt | |
2508 | { | |
2509 | UChar buffer[2048]; | |
2510 | UErrorCode status = U_ZERO_ERROR; | |
2511 | UFormattable *ufmt = NULL; | |
2512 | UNumberFormat *unum; | |
2513 | const char *pattern = "73476730924573500000000"; // weight of the moon, kg | |
2514 | ||
2515 | log_verbose("-- pattern: %s (testing auto-opened UFormattable)\n", pattern); | |
2516 | u_uastrcpy(buffer, pattern); | |
2517 | ||
2518 | unum = unum_open(UNUM_DEFAULT, NULL, -1, "en_US_POSIX", NULL, &status); | |
2519 | if(assertSuccessCheck("calling unum_open()", &status, TRUE)) { | |
2520 | ||
2521 | ufmt = unum_parseToUFormattable(unum, NULL, /* will be ufmt_open()'ed for us */ | |
2522 | buffer, -1, NULL, &status); | |
2523 | if(assertSuccess("unum_parseToUFormattable(weight of the moon)", &status)) { | |
2524 | log_verbose("new formattable allocated at %p\n", (void*)ufmt); | |
2525 | assertTrue("ufmt_isNumeric() TRUE", ufmt_isNumeric(ufmt)); | |
2526 | unum_formatUFormattable(unum, ufmt, out2k, 2048, NULL, &status); | |
2527 | if(assertSuccess("unum_formatUFormattable(3.14159)", &status)) { | |
2528 | assertEquals("unum_formatUFormattable r/t", austrdup(buffer), austrdup(out2k)); | |
2529 | } | |
2530 | ||
2531 | log_verbose("double: %g\n", ufmt_getDouble(ufmt, &status)); | |
2532 | assertSuccess("ufmt_getDouble()", &status); | |
2533 | ||
2534 | log_verbose("long: %ld\n", ufmt_getLong(ufmt, &status)); | |
2535 | assertTrue("failure on ufmt_getLong() for huge number:", U_FAILURE(status)); | |
2536 | // status is now a failure due to ufmt_getLong() above. | |
2537 | // the intltest does extensive r/t testing of Formattable vs. UFormattable. | |
2538 | } | |
2539 | } | |
2540 | ||
2541 | unum_close(unum); | |
2542 | ufmt_close(ufmt); // was implicitly opened for us by the first unum_parseToUFormattable() | |
2543 | } | |
2544 | } | |
2545 | ||
2546 | typedef struct { | |
2547 | const char* locale; | |
2548 | const char* numsys; | |
2549 | int32_t radix; | |
2550 | UBool isAlgorithmic; | |
2551 | const UChar* description; | |
2552 | } NumSysTestItem; | |
2553 | ||
2554 | ||
2555 | static const UChar latnDesc[] = {0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039,0}; // 0123456789 | |
2556 | static const UChar romanDesc[] = {0x25,0x72,0x6F,0x6D,0x61,0x6E,0x2D,0x75,0x70,0x70,0x65,0x72,0}; // %roman-upper | |
2557 | static const UChar arabDesc[] = {0x0660,0x0661,0x0662,0x0663,0x0664,0x0665,0x0666,0x0667,0x0668,0x0669,0}; // | |
2558 | static const UChar arabextDesc[] = {0x06F0,0x06F1,0x06F2,0x06F3,0x06F4,0x06F5,0x06F6,0x06F7,0x06F8,0x06F9,0}; // | |
2559 | static const UChar hanidecDesc[] = {0x3007,0x4E00,0x4E8C,0x4E09,0x56DB,0x4E94,0x516D,0x4E03,0x516B,0x4E5D,0}; // | |
2560 | static const UChar hantDesc[] = {0x7A,0x68,0x5F,0x48,0x61,0x6E,0x74,0x2F,0x53,0x70,0x65,0x6C,0x6C,0x6F,0x75,0x74, | |
2561 | 0x52,0x75,0x6C,0x65,0x73,0x2F,0x25,0x73,0x70,0x65,0x6C,0x6C,0x6F,0x75,0x74,0x2D, | |
2562 | 0x63,0x61,0x72,0x64,0x69,0x6E,0x61,0x6C,0}; // zh_Hant/SpelloutRules/%spellout-cardinal | |
2563 | ||
2564 | static const NumSysTestItem numSysTestItems[] = { | |
2565 | //locale numsys radix isAlgo description | |
2566 | { "en", "latn", 10, FALSE, latnDesc }, | |
2567 | { "en@numbers=roman", "roman", 10, TRUE, romanDesc }, | |
2568 | { "en@numbers=finance", "latn", 10, FALSE, latnDesc }, | |
2569 | { "ar-EG", "arab", 10, FALSE, arabDesc }, | |
2570 | { "fa", "arabext", 10, FALSE, arabextDesc }, | |
2571 | { "zh_Hans@numbers=hanidec", "hanidec", 10, FALSE, hanidecDesc }, | |
2572 | { "zh_Hant@numbers=traditional", "hant", 10, TRUE, hantDesc }, | |
2573 | { NULL, NULL, 0, FALSE, NULL }, | |
2574 | }; | |
2575 | enum { kNumSysDescripBufMax = 64 }; | |
2576 | ||
2577 | static void TestUNumberingSystem(void) { | |
2578 | const NumSysTestItem * itemPtr; | |
2579 | UNumberingSystem * unumsys; | |
2580 | UEnumeration * uenum; | |
2581 | const char * numsys; | |
2582 | UErrorCode status; | |
2583 | ||
2584 | for (itemPtr = numSysTestItems; itemPtr->locale != NULL; itemPtr++) { | |
2585 | status = U_ZERO_ERROR; | |
2586 | unumsys = unumsys_open(itemPtr->locale, &status); | |
2587 | if ( U_SUCCESS(status) ) { | |
2588 | UChar ubuf[kNumSysDescripBufMax]; | |
2589 | int32_t ulen, radix = unumsys_getRadix(unumsys); | |
2590 | UBool isAlgorithmic = unumsys_isAlgorithmic(unumsys); | |
2591 | numsys = unumsys_getName(unumsys); | |
2592 | if ( uprv_strcmp(numsys, itemPtr->numsys) != 0 || radix != itemPtr->radix || !isAlgorithmic != !itemPtr->isAlgorithmic ) { | |
2593 | log_data_err("unumsys name/radix/isAlgorithmic for locale %s, expected %s/%d/%d, got %s/%d/%d\n", | |
2594 | itemPtr->locale, itemPtr->numsys, itemPtr->radix, itemPtr->isAlgorithmic, numsys, radix, isAlgorithmic); | |
2595 | } | |
2596 | ulen = unumsys_getDescription(unumsys, ubuf, kNumSysDescripBufMax, &status); | |
2597 | (void)ulen; // Suppress variable not used warning. | |
2598 | if ( U_FAILURE(status) || u_strcmp(ubuf, itemPtr->description) != 0 ) { | |
2599 | log_data_err("unumsys description for locale %s, description unexpected and/or status %\n", myErrorName(status)); | |
2600 | } | |
2601 | unumsys_close(unumsys); | |
2602 | } else { | |
2603 | log_data_err("unumsys_open for locale %s fails with status %s\n", itemPtr->locale, myErrorName(status)); | |
2604 | } | |
2605 | } | |
2606 | ||
2607 | for (int i=0; i<3; ++i) { | |
2608 | // Run the test of unumsys_openAvailableNames() multiple times. | |
2609 | // Helps verify the management of the internal cache of the names. | |
2610 | status = U_ZERO_ERROR; | |
2611 | uenum = unumsys_openAvailableNames(&status); | |
2612 | if ( U_SUCCESS(status) ) { | |
2613 | int32_t numsysCount = 0; | |
2614 | // sanity check for a couple of number systems that must be in the enumeration | |
2615 | UBool foundLatn = FALSE; | |
2616 | UBool foundArab = FALSE; | |
2617 | while ( (numsys = uenum_next(uenum, NULL, &status)) != NULL && U_SUCCESS(status) ) { | |
2618 | status = U_ZERO_ERROR; | |
2619 | unumsys = unumsys_openByName(numsys, &status); | |
2620 | if ( U_SUCCESS(status) ) { | |
2621 | numsysCount++; | |
2622 | if ( uprv_strcmp(numsys, "latn") ) foundLatn = TRUE; | |
2623 | if ( uprv_strcmp(numsys, "arab") ) foundArab = TRUE; | |
2624 | unumsys_close(unumsys); | |
2625 | } else { | |
2626 | log_err("unumsys_openAvailableNames includes %s but unumsys_openByName on it fails with status %s\n", | |
2627 | numsys, myErrorName(status)); | |
2628 | } | |
2629 | } | |
2630 | uenum_close(uenum); | |
2631 | if ( numsysCount < 40 || !foundLatn || !foundArab ) { | |
2632 | log_err("unumsys_openAvailableNames results incomplete: numsysCount %d, foundLatn %d, foundArab %d\n", | |
2633 | numsysCount, foundLatn, foundArab); | |
2634 | } | |
2635 | } else { | |
2636 | log_data_err("unumsys_openAvailableNames fails with status %s\n", myErrorName(status)); | |
2637 | } | |
2638 | } | |
2639 | } | |
2640 | ||
2641 | /* plain-C version of test in numfmtst.cpp */ | |
2642 | enum { kUBufMax = 64, kBBufMax = 128 }; | |
2643 | static void TestCurrencyIsoPluralFormat(void) { | |
2644 | static const char* DATA[][8] = { | |
2645 | // the data are: | |
2646 | // locale, | |
2647 | // currency amount to be formatted, | |
2648 | // currency ISO code to be formatted, | |
2649 | // format result using CURRENCYSTYLE, | |
2650 | // format result using CURRENCY_STANDARD, | |
2651 | // format result using CURRENCY_ACCOUNTING, | |
2652 | // format result using ISOCURRENCYSTYLE, | |
2653 | // format result using PLURALCURRENCYSTYLE, | |
2654 | ||
2655 | // locale amount ISOcode CURRENCYSTYLE CURRENCY_STANDARD CURRENCY_ACCOUNTING ISOCURRENCYSTYLE PLURALCURRENCYSTYLE | |
2656 | {"en_US", "1", "USD", "$1.00", "$1.00", "$1.00", "USD\\u00A01.00", "1.00 US dollars"}, | |
2657 | {"en_US", "1234.56", "USD", "$1,234.56", "$1,234.56", "$1,234.56", "USD\\u00A01,234.56", "1,234.56 US dollars"}, | |
2658 | {"en_US@cf=account", "1234.56", "USD", "$1,234.56", "$1,234.56", "$1,234.56", "USD\\u00A01,234.56", "1,234.56 US dollars"}, | |
2659 | {"en_US", "-1234.56", "USD", "-$1,234.56", "-$1,234.56", "($1,234.56)", "-USD\\u00A01,234.56", "-1,234.56 US dollars"}, | |
2660 | {"en_US@cf=account", "-1234.56", "USD", "($1,234.56)", "-$1,234.56", "($1,234.56)", "-USD\\u00A01,234.56", "-1,234.56 US dollars"}, | |
2661 | {"en_US@cf=standard", "-1234.56", "USD", "-$1,234.56", "-$1,234.56", "($1,234.56)", "-USD\\u00A01,234.56", "-1,234.56 US dollars"}, | |
2662 | {"zh_CN", "1", "USD", "US$1.00", "US$1.00", "US$1.00", "USD\\u00A01.00", "1.00\\u00A0\\u7F8E\\u5143"}, | |
2663 | {"zh_CN", "-1", "USD", "-US$1.00", "-US$1.00", "(US$1.00)", "-USD\\u00A01.00", "-1.00\\u00A0\\u7F8E\\u5143"}, | |
2664 | {"zh_CN@cf=account", "-1", "USD", "(US$1.00)", "-US$1.00", "(US$1.00)", "-USD\\u00A01.00", "-1.00\\u00A0\\u7F8E\\u5143"}, | |
2665 | {"zh_CN@cf=standard", "-1", "USD", "-US$1.00", "-US$1.00", "(US$1.00)", "-USD\\u00A01.00", "-1.00\\u00A0\\u7F8E\\u5143"}, | |
2666 | {"zh_CN", "1234.56", "USD", "US$1,234.56", "US$1,234.56", "US$1,234.56", "USD\\u00A01,234.56", "1,234.56\\u00A0\\u7F8E\\u5143"}, | |
2667 | // {"zh_CN", "1", "CHY", "CHY1.00", "CHY1.00", "CHY1.00", "CHY1.00", "1.00 CHY"}, // wrong ISO code | |
2668 | // {"zh_CN", "1234.56", "CHY", "CHY1,234.56", "CHY1,234.56", "CHY1,234.56", "CHY1,234.56", "1,234.56 CHY"}, // wrong ISO code | |
2669 | {"zh_CN", "1", "CNY", "\\u00A51.00", "\\u00A51.00", "\\u00A51.00", "CNY\\u00A01.00", "1.00\\u00A0\\u4EBA\\u6C11\\u5E01"}, | |
2670 | {"zh_CN", "1234.56", "CNY", "\\u00A51,234.56", "\\u00A51,234.56", "\\u00A51,234.56", "CNY\\u00A01,234.56", "1,234.56\\u00A0\\u4EBA\\u6C11\\u5E01"}, | |
2671 | {"ru_RU", "1", "RUB", "1,00\\u00A0\\u20BD", "1,00\\u00A0\\u20BD", "1,00\\u00A0\\u20BD", "1,00\\u00A0RUB", "1,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E " | |
2672 | "\\u0440\\u0443\\u0431\\u043B\\u044F"}, | |
2673 | {"ru_RU", "2", "RUB", "2,00\\u00A0\\u20BD", "2,00\\u00A0\\u20BD", "2,00\\u00A0\\u20BD", "2,00\\u00A0RUB", "2,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E " | |
2674 | "\\u0440\\u0443\\u0431\\u043B\\u044F"}, | |
2675 | {"ru_RU", "5", "RUB", "5,00\\u00A0\\u20BD", "5,00\\u00A0\\u20BD", "5,00\\u00A0\\u20BD", "5,00\\u00A0RUB", "5,00 \\u0440\\u043E\\u0441\\u0441\\u0438\\u0439\\u0441\\u043A\\u043E\\u0433\\u043E " | |
2676 | "\\u0440\\u0443\\u0431\\u043B\\u044F"}, | |
2677 | {"ja_JP", "42", NULL, "\\u00A542", "\\u00A542", "\\u00A542", "JPY\\u00A042", "42\\u00A0\\u5186"}, | |
2678 | {"ja_JP@currency=USD", "42", NULL, "$42.00", "$42.00", "$42.00", "USD\\u00A042.00", "42.00\\u00A0\\u7C73\\u30C9\\u30EB"}, | |
2679 | {"ms_MY", "1234.56", "MYR", "RM1,234.56", "RM1,234.56", "RM1,234.56", "MYR1,234.56", "1,234.56 Ringgit Malaysia"}, | |
2680 | {"id_ID", "1234.56", "IDR", "Rp1.235", "Rp1.235", "Rp1.235", "IDR\\u00A01.235", "1.235 Rupiah Indonesia"}, | |
2681 | // test locale without currency information | |
2682 | {"root", "-1.23", "USD", "-US$\\u00A01.23", "-US$\\u00A01.23", "-US$\\u00A01.23", "-USD\\u00A01.23", "-1.23 USD"}, | |
2683 | {"root@cf=account", "-1.23", "USD", "-US$\\u00A01.23", "-US$\\u00A01.23", "-US$\\u00A01.23", "-USD\\u00A01.23", "-1.23 USD"}, | |
2684 | // test choice format | |
2685 | {"es_AR", "1", "INR", "INR\\u00A01,00", "INR\\u00A01,00", "INR\\u00A01,00", "INR\\u00A01,00", "1,00 rupia india"}, | |
2686 | // test EUR format for some now-redundant locale data removed in rdar://62544359 | |
2687 | {"en_NO", "1234.56", "EUR", "\\u20AC\\u00A01\\u00A0234,56", "\\u20AC\\u00A01\\u00A0234,56", "\\u20AC\\u00A01\\u00A0234,56", "EUR\\u00A01\\u00A0234,56", "1\\u00A0234,56 euros"}, | |
2688 | {"en_PL", "1234.56", "EUR", "1\\u00A0234,56\\u00A0\\u20AC", "1\\u00A0234,56\\u00A0\\u20AC", "1\\u00A0234,56\\u00A0\\u20AC", "1\\u00A0234,56\\u00A0EUR", "1\\u00A0234,56 euros"}, | |
2689 | }; | |
2690 | static const UNumberFormatStyle currencyStyles[] = { | |
2691 | UNUM_CURRENCY, | |
2692 | UNUM_CURRENCY_STANDARD, | |
2693 | UNUM_CURRENCY_ACCOUNTING, | |
2694 | UNUM_CURRENCY_ISO, | |
2695 | UNUM_CURRENCY_PLURAL | |
2696 | }; | |
2697 | ||
2698 | int32_t i, sIndex; | |
2699 | ||
2700 | for (i=0; i<UPRV_LENGTHOF(DATA); ++i) { | |
2701 | const char* localeString = DATA[i][0]; | |
2702 | double numberToBeFormat = atof(DATA[i][1]); | |
2703 | const char* currencyISOCode = DATA[i][2]; | |
2704 | for (sIndex = 0; sIndex < UPRV_LENGTHOF(currencyStyles); ++sIndex) { | |
2705 | UNumberFormatStyle style = currencyStyles[sIndex]; | |
2706 | UErrorCode status = U_ZERO_ERROR; | |
2707 | UChar currencyCode[4] = {0}; | |
2708 | UChar ubufResult[kUBufMax]; | |
2709 | UChar ubufExpected[kUBufMax]; | |
2710 | int32_t ulenRes; | |
2711 | const char* currencyISOCodeForLog = currencyISOCode; | |
2712 | ||
2713 | UNumberFormat* unumFmt = unum_open(style, NULL, 0, localeString, NULL, &status); | |
2714 | if (U_FAILURE(status)) { | |
2715 | log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", localeString, (int)style, myErrorName(status)); | |
2716 | continue; | |
2717 | } | |
2718 | if (currencyISOCode != NULL) { | |
2719 | u_charsToUChars(currencyISOCode, currencyCode, 4); | |
2720 | unum_setTextAttribute(unumFmt, UNUM_CURRENCY_CODE, currencyCode, 3, &status); | |
2721 | } else { | |
2722 | unum_setTextAttribute(unumFmt, UNUM_CURRENCY_CODE, NULL, 0, &status); | |
2723 | currencyISOCodeForLog = "(null)"; | |
2724 | } | |
2725 | if (U_FAILURE(status)) { | |
2726 | log_err("FAIL: unum_setTextAttribute, locale %s, UNUM_CURRENCY_CODE %s: %s\n", localeString, currencyISOCodeForLog, myErrorName(status)); | |
2727 | } | |
2728 | ulenRes = unum_formatDouble(unumFmt, numberToBeFormat, ubufResult, kUBufMax, NULL, &status); | |
2729 | if (U_FAILURE(status)) { | |
2730 | log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s: %s\n", localeString, currencyISOCodeForLog, myErrorName(status)); | |
2731 | } else { | |
2732 | int32_t ulenExp = u_unescape(DATA[i][3 + sIndex], ubufExpected, kUBufMax); | |
2733 | if (ulenRes != ulenExp || u_strncmp(ubufResult, ubufExpected, ulenExp) != 0) { | |
2734 | char bbufExpected[kBBufMax]; | |
2735 | char bbufResult[kBBufMax]; | |
2736 | u_strToUTF8(bbufExpected, kBBufMax, NULL, ubufExpected, ulenExp, &status); | |
2737 | u_strToUTF8(bbufResult, kBBufMax, NULL, ubufResult, ulenRes, &status); | |
2738 | log_err("FAIL: unum_formatDouble, locale %s, UNUM_CURRENCY_CODE %s, expected %s, got %s\n", | |
2739 | localeString, currencyISOCodeForLog, bbufExpected, bbufResult); | |
2740 | } | |
2741 | } | |
2742 | unum_close(unumFmt); | |
2743 | } | |
2744 | } | |
2745 | } | |
2746 | ||
2747 | typedef struct { | |
2748 | const char * locale; | |
2749 | UNumberFormatStyle style; | |
2750 | UDisplayContext context; | |
2751 | const char * expectedResult; | |
2752 | } TestContextItem; | |
2753 | ||
2754 | /* currently no locales have contextTransforms data for "symbol" type */ | |
2755 | static const TestContextItem tcItems[] = { /* results for 123.45 */ | |
2756 | { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" }, | |
2757 | { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "Ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" }, | |
2758 | { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" }, | |
2759 | { "sv", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, "ett\\u00ADhundra\\u00ADtjugo\\u00ADtre komma fyra fem" }, | |
2760 | { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "one hundred twenty-three point four five" }, | |
2761 | { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "One hundred twenty-three point four five" }, | |
2762 | { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, "One hundred twenty-three point four five" }, | |
2763 | { "en", UNUM_SPELLOUT, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, "One hundred twenty-three point four five" }, | |
2764 | { NULL, (UNumberFormatStyle)0, (UDisplayContext)0, NULL } | |
2765 | }; | |
2766 | ||
2767 | static void TestContext(void) { | |
2768 | UErrorCode status = U_ZERO_ERROR; | |
2769 | const TestContextItem* itemPtr; | |
2770 | ||
2771 | UNumberFormat *unum = unum_open(UNUM_SPELLOUT, NULL, 0, "en", NULL, &status); | |
2772 | if ( U_SUCCESS(status) ) { | |
2773 | UDisplayContext context = unum_getContext(unum, UDISPCTX_TYPE_CAPITALIZATION, &status); | |
2774 | if ( U_FAILURE(status) || context != UDISPCTX_CAPITALIZATION_NONE) { | |
2775 | log_err("FAIL: Initial unum_getContext is not UDISPCTX_CAPITALIZATION_NONE\n"); | |
2776 | status = U_ZERO_ERROR; | |
2777 | } | |
2778 | unum_setContext(unum, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, &status); | |
2779 | context = unum_getContext(unum, UDISPCTX_TYPE_CAPITALIZATION, &status); | |
2780 | if ( U_FAILURE(status) || context != UDISPCTX_CAPITALIZATION_FOR_STANDALONE) { | |
2781 | log_err("FAIL: unum_getContext does not return the value set, UDISPCTX_CAPITALIZATION_FOR_STANDALONE\n"); | |
2782 | } | |
2783 | unum_close(unum); | |
2784 | } else { | |
2785 | log_data_err("unum_open UNUM_SPELLOUT for en fails with status %s\n", myErrorName(status)); | |
2786 | } | |
2787 | #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_BREAK_ITERATION | |
2788 | for (itemPtr = tcItems; itemPtr->locale != NULL; itemPtr++) { | |
2789 | UChar ubufResult[kUBufMax]; | |
2790 | int32_t ulenRes; | |
2791 | ||
2792 | status = U_ZERO_ERROR; | |
2793 | unum = unum_open(itemPtr->style, NULL, 0, itemPtr->locale, NULL, &status); | |
2794 | if (U_FAILURE(status)) { | |
2795 | log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", | |
2796 | itemPtr->locale, (int)itemPtr->style, myErrorName(status)); | |
2797 | continue; | |
2798 | } | |
2799 | unum_setContext(unum, itemPtr->context, &status); | |
2800 | ulenRes = unum_formatDouble(unum, 123.45, ubufResult, kUBufMax, NULL, &status); | |
2801 | if (U_FAILURE(status)) { | |
2802 | log_err("FAIL: unum_formatDouble, locale %s, style %d, context %d - %s\n", | |
2803 | itemPtr->locale, (int)itemPtr->style, (int)itemPtr->context, myErrorName(status)); | |
2804 | } else { | |
2805 | UChar ubufExpected[kUBufMax]; | |
2806 | int32_t ulenExp = u_unescape(itemPtr->expectedResult, ubufExpected, kUBufMax); | |
2807 | if (ulenRes != ulenExp || u_strncmp(ubufResult, ubufExpected, ulenExp) != 0) { | |
2808 | char bbuf[kUBufMax*2]; | |
2809 | u_austrncpy(bbuf, ubufResult, sizeof(bbuf)); | |
2810 | log_err("FAIL: unum_formatDouble, locale %s, style %d, context %d, expected %d:\"%s\", got %d:\"%s\"\n", | |
2811 | itemPtr->locale, (int)itemPtr->style, (int)itemPtr->context, ulenExp, | |
2812 | itemPtr->expectedResult, ulenRes, bbuf); | |
2813 | } | |
2814 | } | |
2815 | unum_close(unum); | |
2816 | } | |
2817 | #endif /* #if !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_BREAK_ITERATION */ | |
2818 | } | |
2819 | ||
2820 | static void TestCurrencyUsage(void) { | |
2821 | static const char* DATA[][2] = { | |
2822 | /* the data are: | |
2823 | * currency ISO code to be formatted, | |
2824 | * format result using CURRENCYSTYLE with CASH purpose,- | |
2825 | * Note that as of CLDR 26:- | |
2826 | * - TWD switches from 0 decimals to 2; PKR still has 0, so change test to that | |
2827 | * - CAD rounds to .05 | |
2828 | */ | |
2829 | ||
2830 | {"PKR", "PKR\\u00A0124"}, | |
2831 | {"CAD", "CA$123.55"}, | |
2832 | {"USD", "$123.57"} | |
2833 | }; | |
2834 | ||
2835 | // 1st time for getter/setter, 2nd for factory method | |
2836 | int32_t i; | |
2837 | for(i=0; i<2; i++){ | |
2838 | const char* localeString = "en_US"; | |
2839 | double numberToBeFormat = 123.567; | |
2840 | UNumberFormat* unumFmt; | |
2841 | UNumberFormatStyle style = UNUM_CURRENCY; | |
2842 | UErrorCode status = U_ZERO_ERROR; | |
2843 | int32_t j; | |
2844 | ||
2845 | if(i == 1){ // change for factory method | |
2846 | style = UNUM_CASH_CURRENCY; | |
2847 | } | |
2848 | ||
2849 | unumFmt = unum_open(style, NULL, 0, localeString, NULL, &status); | |
2850 | if (U_FAILURE(status)) { | |
2851 | log_data_err("FAIL: unum_open, locale %s, style %d - %s\n", | |
2852 | localeString, (int)style, myErrorName(status)); | |
2853 | continue; | |
2854 | } | |
2855 | ||
2856 | if(i == 0){ // this is for the getter/setter | |
2857 | if(unum_getAttribute(unumFmt, UNUM_CURRENCY_USAGE) != UCURR_USAGE_STANDARD) { | |
2858 | log_err("FAIL: currency usage attribute is not UNUM_CURRENCY_STANDARD\n"); | |
2859 | } | |
2860 | ||
2861 | unum_setAttribute(unumFmt, UNUM_CURRENCY_USAGE, UCURR_USAGE_CASH); | |
2862 | } | |
2863 | ||
2864 | if(unum_getAttribute(unumFmt, UNUM_CURRENCY_USAGE) != UCURR_USAGE_CASH) { | |
2865 | log_err("FAIL: currency usage attribute is not UNUM_CURRENCY_CASH\n"); | |
2866 | } | |
2867 | ||
2868 | for (j=0; j<UPRV_LENGTHOF(DATA); ++j) { | |
2869 | UChar expect[64]; | |
2870 | int32_t expectLen; | |
2871 | UChar currencyCode[4]; | |
2872 | UChar result[64]; | |
2873 | int32_t resultLen; | |
2874 | UFieldPosition pos = {0}; | |
2875 | ||
2876 | u_charsToUChars(DATA[j][0], currencyCode, 3); | |
2877 | expectLen = u_unescape(DATA[j][1], expect, UPRV_LENGTHOF(expect)); | |
2878 | ||
2879 | unum_setTextAttribute(unumFmt, UNUM_CURRENCY_CODE, currencyCode, 3, &status); | |
2880 | assertSuccess("num_setTextAttribute()", &status); | |
2881 | ||
2882 | resultLen = unum_formatDouble(unumFmt, numberToBeFormat, result, UPRV_LENGTHOF(result), | |
2883 | &pos, &status); | |
2884 | assertSuccess("num_formatDouble()", &status); | |
2885 | ||
2886 | if(resultLen != expectLen || u_strcmp(result, expect) != 0) { | |
2887 | log_err("Fail: Error in Number Format Currency Purpose using unum_setAttribute() expected: %s, got %s\n", | |
2888 | aescstrdup(expect, expectLen), aescstrdup(result, resultLen)); | |
2889 | } | |
2890 | ||
2891 | } | |
2892 | ||
2893 | unum_close(unumFmt); | |
2894 | } | |
2895 | } | |
2896 | ||
2897 | static UChar currFmtNegSameAsPos[] = /* "\u00A4#,##0.00;\u00A4#,##0.00" */ | |
2898 | {0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0x3B,0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0}; | |
2899 | ||
2900 | // NOTE: As of ICU 62, identical positive and negative subpatterns means no minus sign! | |
2901 | // See CLDR ticket https://unicode.org/cldr/trac/ticket/10703 | |
2902 | //static UChar currFmtToPatExpected[] = /* "\u00A4#,##0.00" */ | |
2903 | // {0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0}; | |
2904 | static const UChar* currFmtToPatExpected = currFmtNegSameAsPos; | |
2905 | ||
2906 | static UChar currFmtResultExpected[] = /* "$100.00" */ | |
2907 | {0x24,0x31,0x30,0x30,0x2E,0x30,0x30,0}; | |
2908 | ||
2909 | static UChar emptyString[] = {0}; | |
2910 | ||
2911 | enum { kUBufSize = 64, kBBufSize = 128 }; | |
2912 | ||
2913 | static void TestCurrFmtNegSameAsPositive(void) { | |
2914 | UErrorCode status = U_ZERO_ERROR; | |
2915 | UNumberFormat* unumfmt = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status); | |
2916 | if ( U_SUCCESS(status) ) { | |
2917 | unum_applyPattern(unumfmt, FALSE, currFmtNegSameAsPos, -1, NULL, &status); | |
2918 | if (U_SUCCESS(status)) { | |
2919 | UChar ubuf[kUBufSize]; | |
2920 | int32_t ulen = unum_toPattern(unumfmt, FALSE, ubuf, kUBufSize, &status); | |
2921 | if (U_FAILURE(status)) { | |
2922 | log_err("unum_toPattern fails with status %s\n", myErrorName(status)); | |
2923 | } else if (u_strcmp(ubuf, currFmtToPatExpected) != 0) { | |
2924 | log_err("unum_toPattern result wrong, expected %s, got %s\n", aescstrdup(currFmtToPatExpected,-1), aescstrdup(ubuf,ulen)); | |
2925 | } | |
2926 | unum_setSymbol(unumfmt, UNUM_MINUS_SIGN_SYMBOL, emptyString, 0, &status); | |
2927 | if (U_SUCCESS(status)) { | |
2928 | ulen = unum_formatDouble(unumfmt, -100.0, ubuf, kUBufSize, NULL, &status); | |
2929 | if (U_FAILURE(status)) { | |
2930 | log_err("unum_formatDouble fails with status %s\n", myErrorName(status)); | |
2931 | } else if (u_strcmp(ubuf, currFmtResultExpected) != 0) { | |
2932 | log_err("unum_formatDouble result wrong, expected %s, got %s\n", aescstrdup(currFmtResultExpected,-1), aescstrdup(ubuf,ulen)); | |
2933 | } | |
2934 | } else { | |
2935 | log_err("unum_setSymbol fails with status %s\n", myErrorName(status)); | |
2936 | } | |
2937 | } else { | |
2938 | log_err("unum_applyPattern fails with status %s\n", myErrorName(status)); | |
2939 | } | |
2940 | unum_close(unumfmt); | |
2941 | } else { | |
2942 | log_data_err("unum_open UNUM_CURRENCY for en_US fails with status %s\n", myErrorName(status)); | |
2943 | } | |
2944 | } | |
2945 | ||
2946 | ||
2947 | typedef struct { | |
2948 | double value; | |
2949 | const char *valueStr; | |
2950 | const char *expected; | |
2951 | } ValueAndExpectedString; | |
2952 | ||
2953 | static const ValueAndExpectedString enDecMinFrac[] = { | |
2954 | {0.0, "0.0", "0.0"}, | |
2955 | {0.17, "0.17", "0.17"}, | |
2956 | {1.0, "1.0", "1.0"}, | |
2957 | {1234.0, "1234.0", "1,234.0"}, | |
2958 | {12345.0, "12345.0", "12,345.0"}, | |
2959 | {123456.0, "123456.0", "123,456.0"}, | |
2960 | {1234567.0, "1234567.0", "1,234,567.0"}, | |
2961 | {12345678.0, "12345678.0", "12,345,678.0"}, | |
2962 | {0.0, NULL, NULL} | |
2963 | }; | |
2964 | static const ValueAndExpectedString enShort[] = { | |
2965 | {0.0, "0.0", "0"}, | |
2966 | {0.17, "0.17", "0.17"}, | |
2967 | {1.0, "1.0", "1"}, | |
2968 | {1234.0, "1234.0", "1.2K"}, | |
2969 | {12345.0, "12345.0", "12K"}, | |
2970 | {123456.0, "123456.0", "123K"}, | |
2971 | {1234567.0, "1234567.0", "1.2M"}, | |
2972 | {12345678.0, "12345678.0", "12M"}, | |
2973 | {123456789.0, "123456789.0", "123M"}, | |
2974 | {1.23456789E9, "1.23456789E9", "1.2B"}, | |
2975 | {1.23456789E10, "1.23456789E10", "12B"}, | |
2976 | {1.23456789E11, "1.23456789E11", "123B"}, | |
2977 | {1.23456789E12, "1.23456789E12", "1.2T"}, | |
2978 | {1.23456789E13, "1.23456789E13", "12T"}, | |
2979 | {1.23456789E14, "1.23456789E14", "123T"}, | |
2980 | {1.23456789E15, "1.23456789E15", "1235T"}, | |
2981 | {0.0, NULL, NULL} | |
2982 | }; | |
2983 | ||
2984 | static const ValueAndExpectedString enShortMax2[] = { | |
2985 | {0.0, "0.0", "0"}, | |
2986 | {0.17, "0.17", "0.17"}, | |
2987 | {1.0, "1.0", "1"}, | |
2988 | {1234.0, "1234.0", "1.2K"}, | |
2989 | {12345.0, "12345.0", "12K"}, | |
2990 | {123456.0, "123456.0", "120K"}, | |
2991 | {1234567.0, "1234567.0", "1.2M"}, | |
2992 | {12345678.0, "12345678.0", "12M"}, | |
2993 | {123456789.0, "123456789.0", "120M"}, | |
2994 | {1.23456789E9, "1.23456789E9", "1.2B"}, | |
2995 | {1.23456789E10, "1.23456789E10", "12B"}, | |
2996 | {1.23456789E11, "1.23456789E11", "120B"}, | |
2997 | {1.23456789E12, "1.23456789E12", "1.2T"}, | |
2998 | {1.23456789E13, "1.23456789E13", "12T"}, | |
2999 | {1.23456789E14, "1.23456789E14", "120T"}, | |
3000 | {1.23456789E15, "1.23456789E15", "1200T"}, | |
3001 | {0.0, NULL, NULL} | |
3002 | }; | |
3003 | ||
3004 | static const ValueAndExpectedString enShortMax5[] = { | |
3005 | {0.0, "0.0", "0"}, | |
3006 | {0.17, "0.17", "0.17"}, | |
3007 | {1.0, "1.0", "1"}, | |
3008 | {1234.0, "1234.0", "1.234K"}, | |
3009 | {12345.0, "12345.0", "12.345K"}, | |
3010 | {123456.0, "123456.0", "123.46K"}, | |
3011 | {1234567.0, "1234567.0", "1.2346M"}, | |
3012 | {12345678.0, "12345678.0", "12.346M"}, | |
3013 | {123456789.0, "123456789.0", "123.46M"}, | |
3014 | {1.23456789E9, "1.23456789E9", "1.2346B"}, | |
3015 | {1.23456789E10, "1.23456789E10", "12.346B"}, | |
3016 | {1.23456789E11, "1.23456789E11", "123.46B"}, | |
3017 | {1.23456789E12, "1.23456789E12", "1.2346T"}, | |
3018 | {1.23456789E13, "1.23456789E13", "12.346T"}, | |
3019 | {1.23456789E14, "1.23456789E14", "123.46T"}, | |
3020 | {1.23456789E15, "1.23456789E15", "1234.6T"}, | |
3021 | {0.0, NULL, NULL} | |
3022 | }; | |
3023 | ||
3024 | static const ValueAndExpectedString enShortMin3[] = { | |
3025 | {0.0, "0.0", "0.00"}, | |
3026 | {0.17, "0.17", "0.170"}, | |
3027 | {1.0, "1.0", "1.00"}, | |
3028 | {1234.0, "1234.0", "1.23K"}, | |
3029 | {12345.0, "12345.0", "12.3K"}, | |
3030 | {123456.0, "123456.0", "123K"}, | |
3031 | {1234567.0, "1234567.0", "1.23M"}, | |
3032 | {12345678.0, "12345678.0", "12.3M"}, | |
3033 | {123456789.0, "123456789.0", "123M"}, | |
3034 | {1.23456789E9, "1.23456789E9", "1.23B"}, | |
3035 | {1.23456789E10, "1.23456789E10", "12.3B"}, | |
3036 | {1.23456789E11, "1.23456789E11", "123B"}, | |
3037 | {1.23456789E12, "1.23456789E12", "1.23T"}, | |
3038 | {1.23456789E13, "1.23456789E13", "12.3T"}, | |
3039 | {1.23456789E14, "1.23456789E14", "123T"}, | |
3040 | {1.23456789E15, "1.23456789E15", "1230T"}, | |
3041 | {0.0, NULL, NULL} | |
3042 | }; | |
3043 | ||
3044 | static const ValueAndExpectedString jaShortMax2[] = { | |
3045 | {1234.0, "1234.0", "1200"}, | |
3046 | {12345.0, "12345.0", "1.2\\u4E07"}, | |
3047 | {123456.0, "123456.0", "12\\u4E07"}, | |
3048 | {1234567.0, "1234567.0", "120\\u4E07"}, | |
3049 | {12345678.0, "12345678.0", "1200\\u4E07"}, | |
3050 | {123456789.0, "123456789.0", "1.2\\u5104"}, | |
3051 | {1.23456789E9, "1.23456789E9", "12\\u5104"}, | |
3052 | {1.23456789E10, "1.23456789E10", "120\\u5104"}, | |
3053 | {1.23456789E11, "1.23456789E11", "1200\\u5104"}, | |
3054 | {1.23456789E12, "1.23456789E12", "1.2\\u5146"}, | |
3055 | {1.23456789E13, "1.23456789E13", "12\\u5146"}, | |
3056 | {1.23456789E14, "1.23456789E14", "120\\u5146"}, | |
3057 | {0.0, NULL, NULL} | |
3058 | }; | |
3059 | ||
3060 | static const ValueAndExpectedString srLongMax2[] = { | |
3061 | {1234.0, "1234.0", "1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few | |
3062 | {12345.0, "12345.0", "12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other | |
3063 | {21789.0, "21789.0", "22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few | |
3064 | {123456.0, "123456.0", "120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other | |
3065 | {999999.0, "999999.0", "1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"}, // 10^6 one | |
3066 | {1234567.0, "1234567.0", "1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 few | |
3067 | {12345678.0, "12345678.0", "12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 other | |
3068 | {123456789.0, "123456789.0", "120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 other | |
3069 | {1.23456789E9, "1.23456789E9", "1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, // 10^9 few | |
3070 | {1.23456789E10, "1.23456789E10", "12 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, // 10^9 other | |
3071 | {2.08901234E10, "2.08901234E10", "21 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0430"}, // 10^9 one | |
3072 | {2.18901234E10, "2.18901234E10", "22 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, // 10^9 few | |
3073 | {1.23456789E11, "1.23456789E11", "120 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, // 10^9 other | |
3074 | {-1234.0, "-1234.0", "-1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, | |
3075 | {-12345.0, "-12345.0", "-12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, | |
3076 | {-21789.0, "-21789.0", "-22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, | |
3077 | {-123456.0, "-123456.0", "-120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, | |
3078 | {-999999.0, "-999999.0", "-1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"}, | |
3079 | {-1234567.0, "-1234567.0", "-1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, | |
3080 | {-12345678.0, "-12345678.0", "-12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, | |
3081 | {-123456789.0, "-123456789.0", "-120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, | |
3082 | {-1.23456789E9, "-1.23456789E9", "-1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, | |
3083 | {-1.23456789E10, "-1.23456789E10", "-12 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, | |
3084 | {-2.08901234E10, "-2.08901234E10", "-21 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0430"}, | |
3085 | {-2.18901234E10, "-2.18901234E10", "-22 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, | |
3086 | {-1.23456789E11, "-1.23456789E11", "-120 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, | |
3087 | {0.0, NULL, NULL} | |
3088 | }; | |
3089 | ||
3090 | // rdar://52188411 | |
3091 | static const ValueAndExpectedString enINShortMax2[] = { | |
3092 | {0.0, "0.0", "0"}, | |
3093 | {0.17, "0.17", "0.17"}, | |
3094 | {1.0, "1.0", "1"}, | |
3095 | {1234.0, "1234.0", "1.2K"}, | |
3096 | {12345.0, "12345.0", "12K"}, | |
3097 | {123456.0, "123456.0", "1.2L"}, | |
3098 | {1234567.0, "1234567.0", "12L"}, | |
3099 | {12345678.0, "12345678.0", "1.2Cr"}, | |
3100 | {123456789.0, "123456789.0", "12Cr"}, | |
3101 | {1.23456789E9, "1.23456789E9", "120Cr"}, | |
3102 | {1.23456789E10, "1.23456789E10", "1.2KCr"}, | |
3103 | {1.23456789E11, "1.23456789E11", "12KCr"}, | |
3104 | {1.23456789E12, "1.23456789E12", "1.2LCr"}, | |
3105 | {1.23456789E13, "1.23456789E13", "12LCr"}, | |
3106 | {1.23456789E14, "1.23456789E14", "120LCr"}, | |
3107 | {1.23456789E15, "1.23456789E15", "1200LCr"}, | |
3108 | {0.0, NULL, NULL} | |
3109 | }; | |
3110 | ||
3111 | typedef struct { | |
3112 | const char * locale; | |
3113 | UNumberFormatStyle style; | |
3114 | int32_t attribute; // UNumberFormatAttribute, or -1 for none | |
3115 | int32_t attrValue; // | |
3116 | const ValueAndExpectedString * veItems; | |
3117 | } LocStyleAttributeTest; | |
3118 | ||
3119 | static const LocStyleAttributeTest lsaTests[] = { | |
3120 | { "en", UNUM_DECIMAL, UNUM_MIN_FRACTION_DIGITS, 1, enDecMinFrac }, | |
3121 | { "en", UNUM_DECIMAL_COMPACT_SHORT, -1, 0, enShort }, | |
3122 | { "en", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MAX_SIGNIFICANT_DIGITS, 2, enShortMax2 }, | |
3123 | { "en", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MAX_SIGNIFICANT_DIGITS, 5, enShortMax5 }, | |
3124 | { "en", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MIN_SIGNIFICANT_DIGITS, 3, enShortMin3 }, | |
3125 | { "ja", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MAX_SIGNIFICANT_DIGITS, 2, jaShortMax2 }, | |
3126 | { "sr", UNUM_DECIMAL_COMPACT_LONG, UNUM_MAX_SIGNIFICANT_DIGITS, 2, srLongMax2 }, | |
3127 | { "en_IN", UNUM_DECIMAL_COMPACT_SHORT, UNUM_MAX_SIGNIFICANT_DIGITS, 2, enINShortMax2 }, // rdar://52188411 | |
3128 | { NULL, (UNumberFormatStyle)0, -1, 0, NULL } | |
3129 | }; | |
3130 | ||
3131 | static void TestVariousStylesAndAttributes(void) { | |
3132 | const LocStyleAttributeTest * lsaTestPtr; | |
3133 | for (lsaTestPtr = lsaTests; lsaTestPtr->locale != NULL; lsaTestPtr++) { | |
3134 | UErrorCode status = U_ZERO_ERROR; | |
3135 | UNumberFormat * unum = unum_open(lsaTestPtr->style, NULL, 0, lsaTestPtr->locale, NULL, &status); | |
3136 | if ( U_FAILURE(status) ) { | |
3137 | log_data_err("FAIL: unum_open style %d, locale %s: error %s\n", (int)lsaTestPtr->style, lsaTestPtr->locale, u_errorName(status)); | |
3138 | } else { | |
3139 | const ValueAndExpectedString * veItemPtr; | |
3140 | if (lsaTestPtr->attribute >= 0) { | |
3141 | unum_setAttribute(unum, (UNumberFormatAttribute)lsaTestPtr->attribute, lsaTestPtr->attrValue); | |
3142 | } | |
3143 | // ICU 62: should call minSignificantDigits in tandem with maxSignificantDigits. | |
3144 | if (lsaTestPtr->attribute == UNUM_MIN_SIGNIFICANT_DIGITS) { | |
3145 | unum_setAttribute(unum, UNUM_MAX_SIGNIFICANT_DIGITS, lsaTestPtr->attrValue); | |
3146 | } | |
3147 | for (veItemPtr = lsaTestPtr->veItems; veItemPtr->expected != NULL; veItemPtr++) { | |
3148 | UChar uexp[kUBufSize]; | |
3149 | UChar uget[kUBufSize]; | |
3150 | int32_t uexplen, ugetlen; | |
3151 | ||
3152 | status = U_ZERO_ERROR; | |
3153 | uexplen = u_unescape(veItemPtr->expected, uexp, kUBufSize); | |
3154 | ugetlen = unum_formatDouble(unum, veItemPtr->value, uget, kUBufSize, NULL, &status); | |
3155 | if ( U_FAILURE(status) ) { | |
3156 | log_err("FAIL: unum_formatDouble style %d, locale %s, attr %d, value %.2f: error %s\n", | |
3157 | (int)lsaTestPtr->style, lsaTestPtr->locale, lsaTestPtr->attribute, veItemPtr->value, u_errorName(status)); | |
3158 | } else if (ugetlen != uexplen || u_strncmp(uget, uexp, uexplen) != 0) { | |
3159 | char bexp[kBBufSize]; | |
3160 | char bget[kBBufSize]; | |
3161 | u_strToUTF8(bexp, kBBufSize, NULL, uexp, uexplen, &status); | |
3162 | u_strToUTF8(bget, kBBufSize, NULL, uget, ugetlen, &status); | |
3163 | log_err("FAIL: unum_formatDouble style %d, locale %s, attr %d, value %.2f: expect \"%s\", get \"%s\"\n", | |
3164 | (int)lsaTestPtr->style, lsaTestPtr->locale, lsaTestPtr->attribute, veItemPtr->value, bexp, bget); | |
3165 | } | |
3166 | status = U_ZERO_ERROR; | |
3167 | ugetlen = unum_formatDecimal(unum, veItemPtr->valueStr, -1, uget, kUBufSize, NULL, &status); | |
3168 | if ( U_FAILURE(status) ) { | |
3169 | log_err("FAIL: unum_formatDecimal style %d, locale %s, attr %d, valueStr %s: error %s\n", | |
3170 | (int)lsaTestPtr->style, lsaTestPtr->locale, lsaTestPtr->attribute, veItemPtr->valueStr, u_errorName(status)); | |
3171 | } else if (ugetlen != uexplen || u_strncmp(uget, uexp, uexplen) != 0) { | |
3172 | char bexp[kBBufSize]; | |
3173 | char bget[kBBufSize]; | |
3174 | u_strToUTF8(bexp, kBBufSize, NULL, uexp, uexplen, &status); | |
3175 | u_strToUTF8(bget, kBBufSize, NULL, uget, ugetlen, &status); | |
3176 | log_err("FAIL: unum_formatDecimal style %d, locale %s, attr %d, valueStr %s: expect \"%s\", get \"%s\"\n", | |
3177 | (int)lsaTestPtr->style, lsaTestPtr->locale, lsaTestPtr->attribute, veItemPtr->valueStr, bexp, bget); | |
3178 | } | |
3179 | } | |
3180 | unum_close(unum); | |
3181 | } | |
3182 | } | |
3183 | } | |
3184 | ||
3185 | static const UChar currpat[] = { 0xA4,0x23,0x2C,0x23,0x23,0x30,0x2E,0x30,0x30,0}; /* ¤#,##0.00 */ | |
3186 | static const UChar parsetxt[] = { 0x78,0x30,0x79,0x24,0 }; /* x0y$ */ | |
3187 | ||
3188 | static void TestParseCurrPatternWithDecStyle() { | |
3189 | UErrorCode status = U_ZERO_ERROR; | |
3190 | UNumberFormat *unumfmt = unum_open(UNUM_DECIMAL, NULL, 0, "en_US", NULL, &status); | |
3191 | if (U_FAILURE(status)) { | |
3192 | log_data_err("unum_open DECIMAL failed for en_US: %s (Are you missing data?)\n", u_errorName(status)); | |
3193 | } else { | |
3194 | unum_applyPattern(unumfmt, FALSE, currpat, -1, NULL, &status); | |
3195 | if (U_FAILURE(status)) { | |
3196 | log_err_status(status, "unum_applyPattern failed: %s\n", u_errorName(status)); | |
3197 | } else { | |
3198 | int32_t pos = 0; | |
3199 | double value = unum_parseDouble(unumfmt, parsetxt, -1, &pos, &status); | |
3200 | if (U_SUCCESS(status)) { | |
3201 | log_err_status(status, "unum_parseDouble expected to fail but got status %s, value %f\n", u_errorName(status), value); | |
3202 | } | |
3203 | } | |
3204 | unum_close(unumfmt); | |
3205 | } | |
3206 | } | |
3207 | ||
3208 | /* | |
3209 | * Ticket #12684 | |
3210 | * Test unum_formatDoubleForFields (and UFieldPositionIterator) | |
3211 | */ | |
3212 | ||
3213 | typedef struct { | |
3214 | int32_t field; | |
3215 | int32_t beginPos; | |
3216 | int32_t endPos; | |
3217 | } FieldsData; | |
3218 | ||
3219 | typedef struct { | |
3220 | const char * locale; | |
3221 | UNumberFormatStyle style; | |
3222 | double value; | |
3223 | const FieldsData * expectedFields; | |
3224 | } FormatForFieldsItem; | |
3225 | ||
3226 | static const UChar patNoFields[] = { 0x0027, 0x0078, 0x0027, 0 }; /* "'x'", for UNUM_PATTERN_DECIMAL */ | |
3227 | ||
3228 | ||
3229 | /* "en_US", UNUM_CURRENCY, 123456.0 : "¤#,##0.00" => "$123,456.00" */ | |
3230 | static const FieldsData fields_en_CURR[] = { | |
3231 | { UNUM_CURRENCY_FIELD /*7*/, 0, 1 }, | |
3232 | { UNUM_GROUPING_SEPARATOR_FIELD /*6*/, 4, 5 }, | |
3233 | { UNUM_INTEGER_FIELD /*0*/, 1, 8 }, | |
3234 | { UNUM_DECIMAL_SEPARATOR_FIELD /*2*/, 8, 9 }, | |
3235 | { UNUM_FRACTION_FIELD /*1*/, 9, 11 }, | |
3236 | { -1, -1, -1 }, | |
3237 | }; | |
3238 | /* "en_US"/"es_US/MX" , UNUM_PERCENT, -34 : "#,##0%" => "-34%" */ | |
3239 | static const FieldsData fields_en_PRCT[] = { | |
3240 | { UNUM_SIGN_FIELD /*10*/, 0, 1 }, | |
3241 | { UNUM_INTEGER_FIELD /*0*/, 1, 3 }, | |
3242 | { UNUM_PERCENT_FIELD /*8*/, 3, 4 }, | |
3243 | { -1, -1, -1 }, | |
3244 | }; | |
3245 | /* "fr_FR", UNUM_CURRENCY, 123456.0 : "#,##0.00 ¤" => "123,456.00 €" */ | |
3246 | static const FieldsData fields_fr_CURR[] = { | |
3247 | { UNUM_GROUPING_SEPARATOR_FIELD /*6*/, 3, 4 }, | |
3248 | { UNUM_INTEGER_FIELD /*0*/, 0, 7 }, | |
3249 | { UNUM_DECIMAL_SEPARATOR_FIELD /*2*/, 7, 8 }, | |
3250 | { UNUM_FRACTION_FIELD /*1*/, 8, 10 }, | |
3251 | { UNUM_CURRENCY_FIELD /*7*/, 11, 12 }, | |
3252 | { -1, -1, -1 }, | |
3253 | }; | |
3254 | /* "en_US", UNUM_PATTERN_DECIMAL, 12.0 : "'x'" => "x12" */ | |
3255 | static const FieldsData fields_en_PATN[] = { | |
3256 | { UNUM_INTEGER_FIELD /*0*/, 1, 3 }, | |
3257 | { -1, -1, -1 }, | |
3258 | }; | |
3259 | ||
3260 | static const FormatForFieldsItem fffItems[] = { | |
3261 | { "en_US", UNUM_CURRENCY_STANDARD, 123456.0, fields_en_CURR }, | |
3262 | { "en_US", UNUM_PERCENT, -0.34, fields_en_PRCT }, | |
3263 | { "es_US", UNUM_PERCENT, -0.34, fields_en_PRCT }, // rdar://57000745 | |
3264 | { "es_MX", UNUM_PERCENT, -0.34, fields_en_PRCT }, // rdar://42948387 | |
3265 | { "fr_FR", UNUM_CURRENCY_STANDARD, 123456.0, fields_fr_CURR }, | |
3266 | { "en_US", UNUM_PATTERN_DECIMAL, 12.0, fields_en_PATN }, | |
3267 | { NULL, (UNumberFormatStyle)0, 0, NULL }, | |
3268 | }; | |
3269 | ||
3270 | static void TestFormatForFields(void) { | |
3271 | UErrorCode status = U_ZERO_ERROR; | |
3272 | UFieldPositionIterator* fpositer = ufieldpositer_open(&status); | |
3273 | if ( U_FAILURE(status) ) { | |
3274 | log_err("ufieldpositer_open fails, status %s\n", u_errorName(status)); | |
3275 | } else { | |
3276 | const FormatForFieldsItem * itemPtr; | |
3277 | for (itemPtr = fffItems; itemPtr->locale != NULL; itemPtr++) { | |
3278 | UNumberFormat* unum; | |
3279 | status = U_ZERO_ERROR; | |
3280 | unum = (itemPtr->style == UNUM_PATTERN_DECIMAL)? | |
3281 | unum_open(itemPtr->style, patNoFields, -1, itemPtr->locale, NULL, &status): | |
3282 | unum_open(itemPtr->style, NULL, 0, itemPtr->locale, NULL, &status); | |
3283 | if ( U_FAILURE(status) ) { | |
3284 | log_data_err("unum_open fails for locale %s, style %d: status %s (Are you missing data?)\n", itemPtr->locale, itemPtr->style, u_errorName(status)); | |
3285 | } else { | |
3286 | UChar ubuf[kUBufSize]; | |
3287 | int32_t ulen = unum_formatDoubleForFields(unum, itemPtr->value, ubuf, kUBufSize, fpositer, &status); | |
3288 | if ( U_FAILURE(status) ) { | |
3289 | log_err("unum_formatDoubleForFields fails for locale %s, style %d: status %s\n", itemPtr->locale, itemPtr->style, u_errorName(status)); | |
3290 | } else { | |
3291 | const FieldsData * fptr; | |
3292 | int32_t field, beginPos, endPos; | |
3293 | for (fptr = itemPtr->expectedFields; TRUE; fptr++) { | |
3294 | field = ufieldpositer_next(fpositer, &beginPos, &endPos); | |
3295 | if (field != fptr->field || (field >= 0 && (beginPos != fptr->beginPos || endPos != fptr->endPos))) { | |
3296 | if (fptr->field >= 0) { | |
3297 | log_err("unum_formatDoubleForFields for locale %s as \"%s\"; expect field %d range %d-%d, get field %d range %d-%d\n", | |
3298 | itemPtr->locale, aescstrdup(ubuf, ulen), fptr->field, fptr->beginPos, fptr->endPos, field, beginPos, endPos); | |
3299 | } else { | |
3300 | log_err("unum_formatDoubleForFields for locale %s as \"%s\"; expect field < 0, get field %d range %d-%d\n", | |
3301 | itemPtr->locale, aescstrdup(ubuf, ulen), field, beginPos, endPos); | |
3302 | } | |
3303 | break; | |
3304 | } | |
3305 | if (field < 0) { | |
3306 | break; | |
3307 | } | |
3308 | } | |
3309 | } | |
3310 | unum_close(unum); | |
3311 | } | |
3312 | } | |
3313 | ufieldpositer_close(fpositer); | |
3314 | } | |
3315 | } | |
3316 | ||
3317 | static void Test12052_NullPointer() { | |
3318 | UErrorCode status = U_ZERO_ERROR; | |
3319 | static const UChar input[] = u"199a"; | |
3320 | UChar currency[200] = {0}; | |
3321 | UNumberFormat *theFormatter = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status); | |
3322 | if (!assertSuccessCheck("unum_open() failed", &status, TRUE)) { return; } | |
3323 | status = U_ZERO_ERROR; | |
3324 | unum_setAttribute(theFormatter, UNUM_LENIENT_PARSE, 1); | |
3325 | int32_t pos = 1; | |
3326 | unum_parseDoubleCurrency(theFormatter, input, -1, &pos, currency, &status); | |
3327 | assertEquals("should fail gracefully", "U_PARSE_ERROR", u_errorName(status)); | |
3328 | unum_close(theFormatter); | |
3329 | } | |
3330 | ||
3331 | typedef struct { | |
3332 | const char* locale; | |
3333 | const UChar* text; // text to parse | |
3334 | UBool lenient; // leniency to use | |
3335 | UBool intOnly; // whether to set PARSE_INT_ONLY | |
3336 | UErrorCode intStatus; // expected status from parse | |
3337 | int32_t intPos; // expected final pos from parse | |
3338 | int32_t intValue; // expected value from parse | |
3339 | UErrorCode doubStatus; // expected status from parseDouble | |
3340 | int32_t doubPos; // expected final pos from parseDouble | |
3341 | double doubValue; // expected value from parseDouble | |
3342 | UErrorCode decStatus; // expected status from parseDecimal | |
3343 | int32_t decPos; // expected final pos from parseDecimal | |
3344 | const char* decString; // expected output string from parseDecimal | |
3345 | ||
3346 | } ParseCaseItem; | |
3347 | ||
3348 | static const ParseCaseItem parseCaseItems[] = { | |
3349 | { "en", u"0,000", FALSE, FALSE, U_ZERO_ERROR, 5, 0, U_ZERO_ERROR, 5, 0.0, U_ZERO_ERROR, 5, "0" }, | |
3350 | { "en", u"0,000", TRUE, FALSE, U_ZERO_ERROR, 5, 0, U_ZERO_ERROR, 5, 0.0, U_ZERO_ERROR, 5, "0" }, | |
3351 | { "en", u",024", FALSE, FALSE, U_ZERO_ERROR, 4, 24, U_ZERO_ERROR, 4, 24.0, U_ZERO_ERROR, 4, "24" }, | |
3352 | { "en", u",024", TRUE, FALSE, U_ZERO_ERROR, 4, 24, U_ZERO_ERROR, 4, 24.0, U_ZERO_ERROR, 4, "24" }, | |
3353 | { "en", u"1000,000", FALSE, FALSE, U_PARSE_ERROR, 0, 0, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, "" }, | |
3354 | { "en", u"1000,000", TRUE, FALSE, U_ZERO_ERROR, 8, 1000000, U_ZERO_ERROR, 8, 1000000.0, U_ZERO_ERROR, 8, "1000000" }, | |
3355 | { "en", u"", FALSE, FALSE, U_PARSE_ERROR, 0, 0, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, "" }, | |
3356 | { "en", u"", TRUE, FALSE, U_PARSE_ERROR, 0, 0, U_PARSE_ERROR, 0, 0.0, U_PARSE_ERROR, 0, "" }, | |
3357 | { "en", u"9999990000503021", FALSE, FALSE, U_INVALID_FORMAT_ERROR, 16, 2147483647, U_ZERO_ERROR, 16, 9999990000503020.0, U_ZERO_ERROR, 16, "9999990000503021" }, | |
3358 | { "en", u"9999990000503021", FALSE, TRUE, U_INVALID_FORMAT_ERROR, 16, 2147483647, U_ZERO_ERROR, 16, 9999990000503020.0, U_ZERO_ERROR, 16, "9999990000503021" }, | |
3359 | { "en", u"1000000.5", FALSE, FALSE, U_ZERO_ERROR, 9, 1000000, U_ZERO_ERROR, 9, 1000000.5, U_ZERO_ERROR, 9, "1.0000005E+6" /* change from ICU-62nnn */}, | |
3360 | { "en", u"1000000.5", FALSE, TRUE, U_ZERO_ERROR, 7, 1000000, U_ZERO_ERROR, 7, 1000000.0, U_ZERO_ERROR, 7, "1000000" }, | |
3361 | { "en", u"123.5", FALSE, FALSE, U_ZERO_ERROR, 5, 123, U_ZERO_ERROR, 5, 123.5, U_ZERO_ERROR, 5, "123.5" }, | |
3362 | { "en", u"123.5", FALSE, TRUE, U_ZERO_ERROR, 3, 123, U_ZERO_ERROR, 3, 123.0, U_ZERO_ERROR, 3, "123" }, | |
3363 | { NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, NULL } | |
3364 | }; | |
3365 | ||
3366 | static void TestParseCases(void) { | |
3367 | const ParseCaseItem* itemPtr; | |
3368 | for (itemPtr = parseCaseItems; itemPtr->locale != NULL; itemPtr++) { | |
3369 | UErrorCode status = U_ZERO_ERROR; | |
3370 | UNumberFormat* unumDec = unum_open(UNUM_DECIMAL, NULL, 0, itemPtr->locale, NULL, &status); | |
3371 | if (U_FAILURE(status)) { | |
3372 | log_data_err("unum_open UNUM_DECIMAL fails for locale %s: %s\n", itemPtr->locale, u_errorName(status)); | |
3373 | continue; | |
3374 | } | |
3375 | int32_t intValue, parsePos, dclen; | |
3376 | double doubValue; | |
3377 | char decstr[32]; | |
3378 | unum_setAttribute(unumDec, UNUM_LENIENT_PARSE, itemPtr->lenient); | |
3379 | unum_setAttribute(unumDec, UNUM_PARSE_INT_ONLY, itemPtr->intOnly); | |
3380 | ||
3381 | parsePos = 0; | |
3382 | status = U_ZERO_ERROR; | |
3383 | intValue = unum_parse(unumDec, itemPtr->text, -1, &parsePos, &status); | |
3384 | if (status != itemPtr->intStatus || parsePos != itemPtr->intPos || intValue != itemPtr->intValue) { | |
3385 | char btext[32]; | |
3386 | u_austrcpy(btext, itemPtr->text); | |
3387 | log_err("locale %s, text \"%s\", lenient %d, intOnly %d;\n parse expected status %s, pos %d, value %d;\n got %s, %d, %d\n", | |
3388 | itemPtr->locale, btext, itemPtr->lenient, itemPtr->intOnly, | |
3389 | u_errorName(itemPtr->intStatus), itemPtr->intPos, itemPtr->intValue, | |
3390 | u_errorName(status), parsePos, intValue); | |
3391 | } | |
3392 | ||
3393 | parsePos = 0; | |
3394 | status = U_ZERO_ERROR; | |
3395 | doubValue = unum_parseDouble(unumDec, itemPtr->text, -1, &parsePos, &status); | |
3396 | if (status != itemPtr->doubStatus || parsePos != itemPtr->doubPos || doubValue != itemPtr->doubValue) { | |
3397 | char btext[32]; | |
3398 | u_austrcpy(btext, itemPtr->text); | |
3399 | log_err("locale %s, text \"%s\", lenient %d, intOnly %d;\n parseDouble expected status %s, pos %d, value %.1f;\n got %s, %d, %.1f\n", | |
3400 | itemPtr->locale, btext, itemPtr->lenient, itemPtr->intOnly, | |
3401 | u_errorName(itemPtr->doubStatus), itemPtr->doubPos, itemPtr->doubValue, | |
3402 | u_errorName(status), parsePos, doubValue); | |
3403 | } | |
3404 | ||
3405 | parsePos = 0; | |
3406 | status = U_ZERO_ERROR; | |
3407 | decstr[0] = 0; | |
3408 | dclen = unum_parseDecimal(unumDec, itemPtr->text, -1, &parsePos, decstr, 32, &status); | |
3409 | (void)dclen; | |
3410 | if (status != itemPtr->decStatus || parsePos != itemPtr->decPos || uprv_strcmp(decstr,itemPtr->decString) != 0) { | |
3411 | char btext[32]; | |
3412 | u_austrcpy(btext, itemPtr->text); | |
3413 | log_err("locale %s, text \"%s\", lenient %d, intOnly %d;\n parseDecimal expected status %s, pos %d, str \"%s\";\n got %s, %d, \"%s\"\n", | |
3414 | itemPtr->locale, btext, itemPtr->lenient, itemPtr->intOnly, | |
3415 | u_errorName(itemPtr->decStatus), itemPtr->decPos, itemPtr->decString, | |
3416 | u_errorName(status), parsePos, decstr); | |
3417 | } | |
3418 | ||
3419 | unum_close(unumDec); | |
3420 | } | |
3421 | } | |
3422 | ||
3423 | typedef struct { | |
3424 | const char* descrip; | |
3425 | const char* locale; | |
3426 | UNumberFormatStyle style; | |
3427 | int32_t minInt; | |
3428 | int32_t minFrac; | |
3429 | int32_t maxFrac; | |
3430 | double roundIncr; | |
3431 | const UChar* expPattern; | |
3432 | double valueToFmt; | |
3433 | const UChar* expFormat; | |
3434 | } SetMaxFracAndRoundIncrItem; | |
3435 | ||
3436 | static const SetMaxFracAndRoundIncrItem maxFracAndRoundIncrItems[] = { | |
3437 | // descrip locale style mnI mnF mxF rdInc expPat value expFmt | |
3438 | { "01 en_US DEC 1/0/3/0.0", "en_US", UNUM_DECIMAL, 1, 0, 3, 0.0, u"#,##0.###", 0.128, u"0.128" }, | |
3439 | { "02 en_US DEC 1/0/1/0.0", "en_US", UNUM_DECIMAL, 1, 0, 1, 0.0, u"#,##0.#", 0.128, u"0.1" }, | |
3440 | { "03 en_US DEC 1/0/1/0.01", "en_US", UNUM_DECIMAL, 1, 0, 1, 0.01, u"#,##0.#", 0.128, u"0.1" }, | |
3441 | { "04 en_US DEC 1/1/1/0.01", "en_US", UNUM_DECIMAL, 1, 1, 1, 0.01, u"#,##0.0", 0.128, u"0.1" }, | |
3442 | { "05 en_US DEC 1/0/1/0.1", "en_US", UNUM_DECIMAL, 1, 0, 1, 0.1, u"#,##0.1", 0.128, u"0.1" }, // use incr | |
3443 | { "06 en_US DEC 1/1/1/0.1", "en_US", UNUM_DECIMAL, 1, 1, 1, 0.1, u"#,##0.1", 0.128, u"0.1" }, // use incr | |
3444 | ||
3445 | { "10 en_US DEC 1/0/1/0.02", "en_US", UNUM_DECIMAL, 1, 0, 1, 0.02, u"#,##0.#", 0.128, u"0.1" }, | |
3446 | { "11 en_US DEC 1/0/2/0.02", "en_US", UNUM_DECIMAL, 1, 0, 2, 0.02, u"#,##0.02", 0.128, u"0.12" }, // use incr | |
3447 | { "12 en_US DEC 1/0/3/0.02", "en_US", UNUM_DECIMAL, 1, 0, 3, 0.02, u"#,##0.02#", 0.128, u"0.12" }, // use incr | |
3448 | { "13 en_US DEC 1/1/1/0.02", "en_US", UNUM_DECIMAL, 1, 1, 1, 0.02, u"#,##0.0", 0.128, u"0.1" }, | |
3449 | { "14 en_US DEC 1/1/2/0.02", "en_US", UNUM_DECIMAL, 1, 1, 2, 0.02, u"#,##0.02", 0.128, u"0.12" }, // use incr | |
3450 | { "15 en_US DEC 1/1/3/0.02", "en_US", UNUM_DECIMAL, 1, 1, 3, 0.02, u"#,##0.02#", 0.128, u"0.12" }, // use incr | |
3451 | { "16 en_US DEC 1/2/2/0.02", "en_US", UNUM_DECIMAL, 1, 2, 2, 0.02, u"#,##0.02", 0.128, u"0.12" }, // use incr | |
3452 | { "17 en_US DEC 1/2/3/0.02", "en_US", UNUM_DECIMAL, 1, 2, 3, 0.02, u"#,##0.02#", 0.128, u"0.12" }, // use incr | |
3453 | { "18 en_US DEC 1/3/3/0.02", "en_US", UNUM_DECIMAL, 1, 3, 3, 0.02, u"#,##0.020", 0.128, u"0.120" }, // use incr | |
3454 | ||
3455 | { "20 en_US DEC 1/1/1/0.0075", "en_US", UNUM_DECIMAL, 1, 1, 1, 0.0075, u"#,##0.0", 0.019, u"0.0" }, | |
3456 | { "21 en_US DEC 1/1/2/0.0075", "en_US", UNUM_DECIMAL, 1, 1, 2, 0.0075, u"#,##0.0075", 0.004, u"0.0075" }, // use incr | |
3457 | { "22 en_US DEC 1/1/2/0.0075", "en_US", UNUM_DECIMAL, 1, 1, 2, 0.0075, u"#,##0.0075", 0.019, u"0.0225" }, // use incr | |
3458 | { "23 en_US DEC 1/1/3/0.0075", "en_US", UNUM_DECIMAL, 1, 1, 3, 0.0075, u"#,##0.0075", 0.004, u"0.0075" }, // use incr | |
3459 | { "24 en_US DEC 1/1/3/0.0075", "en_US", UNUM_DECIMAL, 1, 1, 3, 0.0075, u"#,##0.0075", 0.019, u"0.0225" }, // use incr | |
3460 | { "25 en_US DEC 1/2/2/0.0075", "en_US", UNUM_DECIMAL, 1, 2, 2, 0.0075, u"#,##0.0075", 0.004, u"0.0075" }, // use incr | |
3461 | { "26 en_US DEC 1/2/2/0.0075", "en_US", UNUM_DECIMAL, 1, 2, 2, 0.0075, u"#,##0.0075", 0.019, u"0.0225" }, // use incr | |
3462 | { "27 en_US DEC 1/2/3/0.0075", "en_US", UNUM_DECIMAL, 1, 2, 3, 0.0075, u"#,##0.0075", 0.004, u"0.0075" }, // use incr | |
3463 | { "28 en_US DEC 1/2/3/0.0075", "en_US", UNUM_DECIMAL, 1, 2, 3, 0.0075, u"#,##0.0075", 0.019, u"0.0225" }, // use incr | |
3464 | { "29 en_US DEC 1/3/3/0.0075", "en_US", UNUM_DECIMAL, 1, 3, 3, 0.0075, u"#,##0.0075", 0.004, u"0.0075" }, // use incr | |
3465 | { "2A en_US DEC 1/3/3/0.0075", "en_US", UNUM_DECIMAL, 1, 3, 3, 0.0075, u"#,##0.0075", 0.019, u"0.0225" }, // use incr | |
3466 | ||
3467 | // Additions for rdar://51452216 | |
3468 | { "30 en_US DEC 1/0/1/0.01", "en_US", UNUM_DECIMAL, 1, 1, 3, 0.001, u"#,##0.001", 1.23456789, u"1.235" }, | |
3469 | { "31 en_US DEC 1/1/1/0.01", "en_US", UNUM_DECIMAL, 1, 1, 3, 0.001f, u"#,##0.001", 1.23456789, u"1.235" }, | |
3470 | ||
3471 | { NULL, NULL, UNUM_IGNORE, 0, 0, 0, 0.0, NULL, 0.0, NULL } | |
3472 | }; | |
3473 | ||
3474 | // The following is copied from C++ number_patternstring.cpp for this C test. | |
3475 | // | |
3476 | // Determine whether a given roundingIncrement should be ignored for formatting | |
3477 | // based on the current maxFrac value (maximum fraction digits). For example a | |
3478 | // roundingIncrement of 0.01 should be ignored if maxFrac is 1, but not if maxFrac | |
3479 | // is 2 or more. Note that roundingIncrements are rounded in significance, so | |
3480 | // a roundingIncrement of 0.006 is treated like 0.01 for this determination, i.e. | |
3481 | // it should not be ignored if maxFrac is 2 or more (but a roundingIncrement of | |
3482 | // 0.005 is treated like 0.001 for significance). This is the reason for the | |
3483 | // initial doubling below. | |
3484 | // roundIncr must be non-zero | |
3485 | static UBool ignoreRoundingIncrement(double roundIncr, int32_t maxFrac) { | |
3486 | if (maxFrac < 0) { | |
3487 | return FALSE; | |
3488 | } | |
3489 | int32_t frac = 0; | |
3490 | roundIncr *= 2.0; | |
3491 | for (frac = 0; frac <= maxFrac && roundIncr <= 1.0; frac++, roundIncr *= 10.0); | |
3492 | return (frac > maxFrac); | |
3493 | } | |
3494 | ||
3495 | static void TestSetMaxFracAndRoundIncr(void) { | |
3496 | const SetMaxFracAndRoundIncrItem* itemPtr; | |
3497 | for (itemPtr = maxFracAndRoundIncrItems; itemPtr->descrip != NULL; itemPtr++) { | |
3498 | UChar ubuf[kUBufMax]; | |
3499 | char bbufe[kBBufMax]; | |
3500 | char bbufg[kBBufMax]; | |
3501 | int32_t ulen; | |
3502 | UErrorCode status = U_ZERO_ERROR; | |
3503 | UNumberFormat* unf = unum_open(itemPtr->style, NULL, 0, itemPtr->locale, NULL, &status); | |
3504 | if (U_FAILURE(status)) { | |
3505 | log_data_err("locale %s: unum_open style %d fails with %s\n", itemPtr->locale, itemPtr->style, u_errorName(status)); | |
3506 | continue; | |
3507 | } | |
3508 | ||
3509 | unum_setAttribute(unf, UNUM_MIN_INTEGER_DIGITS, itemPtr->minInt); | |
3510 | unum_setAttribute(unf, UNUM_MIN_FRACTION_DIGITS, itemPtr->minFrac); | |
3511 | unum_setAttribute(unf, UNUM_MAX_FRACTION_DIGITS, itemPtr->maxFrac); | |
3512 | unum_setDoubleAttribute(unf, UNUM_ROUNDING_INCREMENT, itemPtr->roundIncr); | |
3513 | ||
3514 | UBool roundIncrUsed = (itemPtr->roundIncr != 0.0 && !ignoreRoundingIncrement(itemPtr->roundIncr, itemPtr->maxFrac)); | |
3515 | ||
3516 | int32_t minInt = unum_getAttribute(unf, UNUM_MIN_INTEGER_DIGITS); | |
3517 | if (minInt != itemPtr->minInt) { | |
3518 | log_err("test %s: unum_getAttribute UNUM_MIN_INTEGER_DIGITS, expected %d, got %d\n", | |
3519 | itemPtr->descrip, itemPtr->minInt, minInt); | |
3520 | } | |
3521 | int32_t minFrac = unum_getAttribute(unf, UNUM_MIN_FRACTION_DIGITS); | |
3522 | if (minFrac != itemPtr->minFrac) { | |
3523 | log_err("test %s: unum_getAttribute UNUM_MIN_FRACTION_DIGITS, expected %d, got %d\n", | |
3524 | itemPtr->descrip, itemPtr->minFrac, minFrac); | |
3525 | } | |
3526 | // If incrementRounding is used, maxFrac is set equal to minFrac | |
3527 | int32_t maxFrac = unum_getAttribute(unf, UNUM_MAX_FRACTION_DIGITS); | |
3528 | // If incrementRounding is used, maxFrac is set equal to minFrac | |
3529 | int32_t expMaxFrac = (roundIncrUsed)? itemPtr->minFrac: itemPtr->maxFrac; | |
3530 | if (maxFrac != expMaxFrac) { | |
3531 | log_err("test %s: unum_getAttribute UNUM_MAX_FRACTION_DIGITS, expected %d, got %d\n", | |
3532 | itemPtr->descrip, expMaxFrac, maxFrac); | |
3533 | } | |
3534 | double roundIncr = unum_getDoubleAttribute(unf, UNUM_ROUNDING_INCREMENT); | |
3535 | // If incrementRounding is not used, roundIncr is set to 0.0 | |
3536 | double expRoundIncr = (roundIncrUsed)? itemPtr->roundIncr: 0.0; | |
3537 | if (roundIncr != expRoundIncr) { | |
3538 | log_err("test %s: unum_getDoubleAttribute UNUM_ROUNDING_INCREMENT, expected %f, got %f\n", | |
3539 | itemPtr->descrip, expRoundIncr, roundIncr); | |
3540 | } | |
3541 | ||
3542 | status = U_ZERO_ERROR; | |
3543 | ulen = unum_toPattern(unf, FALSE, ubuf, kUBufMax, &status); | |
3544 | (void)ulen; | |
3545 | if ( U_FAILURE(status) ) { | |
3546 | log_err("test %s: unum_toPattern fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3547 | } else if (u_strcmp(ubuf,itemPtr->expPattern)!=0) { | |
3548 | u_austrcpy(bbufe, itemPtr->expPattern); | |
3549 | u_austrcpy(bbufg, ubuf); | |
3550 | log_err("test %s: unum_toPattern expect \"%s\", get \"%s\"\n", itemPtr->descrip, bbufe, bbufg); | |
3551 | } | |
3552 | ||
3553 | status = U_ZERO_ERROR; | |
3554 | ulen = unum_formatDouble(unf, itemPtr->valueToFmt, ubuf, kUBufMax, NULL, &status); | |
3555 | if ( U_FAILURE(status) ) { | |
3556 | log_err("test %s: unum_formatDouble fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3557 | } else if (u_strcmp(ubuf,itemPtr->expFormat)!=0) { | |
3558 | u_austrcpy(bbufe, itemPtr->expFormat); | |
3559 | u_austrcpy(bbufg, ubuf); | |
3560 | log_err("test %s: unum_formatDouble expect \"%s\", get \"%s\"\n", itemPtr->descrip, bbufe, bbufg); | |
3561 | } | |
3562 | ||
3563 | unum_close(unf); | |
3564 | } | |
3565 | } | |
3566 | ||
3567 | static void TestIgnorePadding(void) { | |
3568 | UErrorCode status = U_ZERO_ERROR; | |
3569 | UNumberFormat* unum = unum_open(UNUM_PATTERN_DECIMAL, NULL, 0, "en_US", NULL, &status); | |
3570 | if (U_FAILURE(status)) { | |
3571 | log_data_err("unum_open UNUM_PATTERN_DECIMAL for en_US and NULL pattern fails:%s\n", u_errorName(status)); | |
3572 | } else { | |
3573 | unum_setAttribute(unum, UNUM_GROUPING_USED, 0); | |
3574 | unum_setAttribute(unum, UNUM_FORMAT_WIDTH, 0); | |
3575 | unum_setTextAttribute(unum, UNUM_PADDING_CHARACTER, u"*", 1, &status); | |
3576 | if (U_FAILURE(status)) { | |
3577 | log_err("unum_setTextAttribute UNUM_PADDING_CHARACTER to '*' fails: %s\n", u_errorName(status)); | |
3578 | } else { | |
3579 | unum_setAttribute(unum, UNUM_PADDING_POSITION, 0); | |
3580 | unum_setAttribute(unum, UNUM_MIN_INTEGER_DIGITS, 0); | |
3581 | unum_setAttribute(unum, UNUM_MAX_INTEGER_DIGITS, 8); | |
3582 | unum_setAttribute(unum, UNUM_MIN_FRACTION_DIGITS, 0); | |
3583 | unum_setAttribute(unum, UNUM_MAX_FRACTION_DIGITS, 0); | |
3584 | ||
3585 | UChar ubuf[kUBufMax]; | |
3586 | int32_t ulen = unum_toPattern(unum, FALSE, ubuf, kUBufMax, &status); | |
3587 | if (U_FAILURE(status)) { | |
3588 | log_err("unum_toPattern fails: %s\n", u_errorName(status)); | |
3589 | } else { | |
3590 | char bbuf[kBBufMax]; | |
3591 | if (ulen > 0 && ubuf[0]==u'*') { | |
3592 | ubuf[kUBufMax-1] = 0; // ensure zero termination | |
3593 | u_austrncpy(bbuf, ubuf, kBBufMax); | |
3594 | log_err("unum_toPattern result should ignore padding but get %s\n", bbuf); | |
3595 | } | |
3596 | unum_applyPattern(unum, FALSE, ubuf, ulen, NULL, &status); | |
3597 | if (U_FAILURE(status)) { | |
3598 | log_err("unum_applyPattern fails: %s\n", u_errorName(status)); | |
3599 | } else { | |
3600 | ulen = unum_formatDecimal(unum, "24", -1, ubuf, kUBufMax, NULL, &status); | |
3601 | if (U_FAILURE(status)) { | |
3602 | log_err("unum_formatDecimal fails: %s\n", u_errorName(status)); | |
3603 | } else if (u_strcmp(ubuf, u"24") != 0) { | |
3604 | ubuf[kUBufMax-1] = 0; // ensure zero termination | |
3605 | u_austrncpy(bbuf, ubuf, kBBufMax); | |
3606 | log_err("unum_formatDecimal result expect 24 but get %s\n", bbuf); | |
3607 | } | |
3608 | } | |
3609 | } | |
3610 | } | |
3611 | unum_close(unum); | |
3612 | } | |
3613 | } | |
3614 | ||
3615 | static void TestSciNotationMaxFracCap(void) { | |
3616 | static const UChar* pat1 = u"#.##E+00;-#.##E+00"; | |
3617 | UErrorCode status = U_ZERO_ERROR; | |
3618 | UNumberFormat* unum = unum_open(UNUM_PATTERN_DECIMAL, pat1, -1, "en_US", NULL, &status); | |
3619 | if ( U_FAILURE(status) ) { | |
3620 | log_data_err("unum_open UNUM_PATTERN_DECIMAL with scientific pattern for \"en_US\" fails with %s\n", u_errorName(status)); | |
3621 | } else { | |
3622 | double value; | |
3623 | UChar ubuf[kUBufMax]; | |
3624 | char bbuf[kBBufMax]; | |
3625 | int32_t ulen; | |
3626 | ||
3627 | unum_setAttribute(unum, UNUM_MIN_FRACTION_DIGITS, 0); | |
3628 | unum_setAttribute(unum, UNUM_MAX_FRACTION_DIGITS, 2147483647); | |
3629 | ulen = unum_toPattern(unum, FALSE, ubuf, kUBufMax, &status); | |
3630 | if ( U_SUCCESS(status) ) { | |
3631 | u_austrncpy(bbuf, ubuf, kUBufMax); | |
3632 | log_info("unum_toPattern (%d): %s\n", ulen, bbuf); | |
3633 | } | |
3634 | ||
3635 | for (value = 10.0; value < 1000000000.0; value *= 10.0) { | |
3636 | status = U_ZERO_ERROR; | |
3637 | ulen = unum_formatDouble(unum, value, ubuf, kUBufMax, NULL, &status); | |
3638 | if ( U_FAILURE(status) ) { | |
3639 | log_err("unum_formatDouble value %.1f status %s\n", value, u_errorName(status)); | |
3640 | } else if (u_strncmp(ubuf,u"1E+0",4) != 0) { | |
3641 | u_austrncpy(bbuf, ubuf, kUBufMax); | |
3642 | log_err("unum_formatDouble value %.1f expected result to begin with 1E+0, got %s\n", value, bbuf); | |
3643 | } | |
3644 | } | |
3645 | unum_close(unum); | |
3646 | } | |
3647 | } | |
3648 | ||
3649 | typedef struct { | |
3650 | const char* locale; | |
3651 | double value; | |
3652 | const UChar* formatLimitPrecision; | |
3653 | const UChar* formatFullPrecision; | |
3654 | } FormatPrecisionItem; | |
3655 | ||
3656 | static const FormatPrecisionItem formatPrecisionItems[] = { | |
3657 | { "en_US", 0.33333333 - 0.00333333, u"0.33", u"0.32999999999999996" }, | |
3658 | { "en_US", 0.07 * 100.0, u"7", u"7.000000000000001" }, | |
3659 | { NULL, 0.0, NULL, NULL } | |
3660 | }; | |
3661 | ||
3662 | static const UChar* patternTestPrecision = u"#0.################################################################################"; // 80 fraction places | |
3663 | ||
3664 | // Currently Apple only | |
3665 | static void TestFormatPrecision(void) { | |
3666 | const FormatPrecisionItem* itemPtr; | |
3667 | for (itemPtr = formatPrecisionItems; itemPtr->locale != NULL; itemPtr++) { | |
3668 | UErrorCode status = U_ZERO_ERROR; | |
3669 | UParseError perr; | |
3670 | UNumberFormat *unum = unum_open(UNUM_PATTERN_DECIMAL, patternTestPrecision, -1, itemPtr->locale, &perr, &status); | |
3671 | if (U_FAILURE(status)) { | |
3672 | log_data_err("unum_open UNUM_PATTERN_DECIMAL fails for locale %s: %s\n", itemPtr->locale, u_errorName(status)); | |
3673 | continue; | |
3674 | } | |
3675 | UChar ubuf[kUBufSize]; | |
3676 | int32_t ulen; | |
3677 | UFieldPosition fpos; | |
3678 | UBool formatFullPrecision; | |
3679 | ||
3680 | formatFullPrecision = (UBool)unum_getAttribute(unum, UNUM_FORMAT_WITH_FULL_PRECISION); | |
3681 | if (formatFullPrecision) { | |
3682 | log_err("unum_getAttribute, default for UNUM_FORMAT_WITH_FULL_PRECISION is not FALSE\n"); | |
3683 | } else { | |
3684 | fpos.field = UNUM_DECIMAL_SEPARATOR_FIELD; | |
3685 | fpos.beginIndex = 0; | |
3686 | fpos.endIndex = 0; | |
3687 | status = U_ZERO_ERROR; | |
3688 | ulen = unum_formatDouble(unum, itemPtr->value, ubuf, kUBufSize, &fpos, &status); | |
3689 | if (U_FAILURE(status)) { | |
3690 | log_err("unum_formatDouble locale %s val %.20f limit precision fails with %s\n", itemPtr->locale, itemPtr->value, u_errorName(status)); | |
3691 | } else if (ulen != u_strlen(itemPtr->formatLimitPrecision) || u_strcmp(ubuf, itemPtr->formatLimitPrecision) != 0) { | |
3692 | char bbufe[kBBufSize]; | |
3693 | char bbufg[kBBufSize]; | |
3694 | u_strToUTF8(bbufe, kBBufSize, NULL, itemPtr->formatLimitPrecision, -1, &status); | |
3695 | u_strToUTF8(bbufg, kBBufSize, NULL, ubuf, ulen, &status); | |
3696 | log_err("unum_formatDouble locale %s val %.20f limit precision, expect %s, get %s\n", itemPtr->locale, itemPtr->value, bbufe, bbufg); | |
3697 | } | |
3698 | } | |
3699 | ||
3700 | unum_setAttribute(unum, UNUM_FORMAT_WITH_FULL_PRECISION, TRUE); | |
3701 | formatFullPrecision = (UBool)unum_getAttribute(unum, UNUM_FORMAT_WITH_FULL_PRECISION); | |
3702 | if (!formatFullPrecision) { | |
3703 | log_err("unum_getAttribute, after set UNUM_FORMAT_WITH_FULL_PRECISION is not TRUE\n"); | |
3704 | } else { | |
3705 | fpos.field = UNUM_DECIMAL_SEPARATOR_FIELD; | |
3706 | fpos.beginIndex = 0; | |
3707 | fpos.endIndex = 0; | |
3708 | status = U_ZERO_ERROR; | |
3709 | ulen = unum_formatDouble(unum, itemPtr->value, ubuf, kUBufSize, &fpos, &status); | |
3710 | if (U_FAILURE(status)) { | |
3711 | log_err("unum_formatDouble locale %s val %.20f full precision fails with %s\n", itemPtr->locale, itemPtr->value, u_errorName(status)); | |
3712 | } else if (ulen != u_strlen(itemPtr->formatFullPrecision) || u_strcmp(ubuf, itemPtr->formatFullPrecision) != 0) { | |
3713 | char bbufe[kBBufSize]; | |
3714 | char bbufg[kBBufSize]; | |
3715 | u_strToUTF8(bbufe, kBBufSize, NULL, itemPtr->formatFullPrecision, -1, &status); | |
3716 | u_strToUTF8(bbufg, kBBufSize, NULL, ubuf, ulen, &status); | |
3717 | log_err("unum_formatDouble locale %s val %.20f full precision, expect %s, get %s\n", itemPtr->locale, itemPtr->value, bbufe, bbufg); | |
3718 | } | |
3719 | } | |
3720 | ||
3721 | unum_close(unum); | |
3722 | } | |
3723 | } | |
3724 | ||
3725 | // Apple only for <rdar://problem/52538227> | |
3726 | static void TestSetSigDigAndRoundIncr(void) { | |
3727 | UErrorCode status = U_ZERO_ERROR; | |
3728 | UNumberFormat* unum = unum_open(UNUM_PATTERN_DECIMAL, u"#", 1, "en_US", NULL, &status); | |
3729 | if ( U_FAILURE(status) ) { | |
3730 | log_data_err("unum_open UNUM_PATTERN_DECIMAL # for \"en_US\" fails with %s\n", u_errorName(status)); | |
3731 | } else { | |
3732 | static const double value = 1.034000; | |
3733 | UChar ubuf[kUBufMax]; | |
3734 | char bbuf[kBBufMax]; | |
3735 | int32_t ulen; | |
3736 | ||
3737 | unum_setAttribute(unum, UNUM_MAX_INTEGER_DIGITS, 42); | |
3738 | unum_setAttribute(unum, UNUM_MAX_FRACTION_DIGITS, 0); | |
3739 | unum_setAttribute(unum, UNUM_ROUNDING_MODE, UNUM_ROUND_HALFUP); // =6 | |
3740 | unum_setDoubleAttribute(unum, UNUM_ROUNDING_INCREMENT, 0.01); | |
3741 | unum_setAttribute(unum, UNUM_SIGNIFICANT_DIGITS_USED, 1); | |
3742 | unum_setAttribute(unum, UNUM_MAX_SIGNIFICANT_DIGITS, 5); | |
3743 | unum_setAttribute(unum, UNUM_MIN_SIGNIFICANT_DIGITS, 1); | |
3744 | ||
3745 | log_info("unum_getAttribute minSig %d maxSig %d sigUsed %d\n", | |
3746 | unum_getAttribute(unum,UNUM_MIN_SIGNIFICANT_DIGITS), unum_getAttribute(unum,UNUM_MAX_SIGNIFICANT_DIGITS), | |
3747 | unum_getAttribute(unum,UNUM_SIGNIFICANT_DIGITS_USED)); | |
3748 | log_info("unum_getDoubleAttribute UNUM_ROUNDING_INCREMENT %f\n", unum_getDoubleAttribute(unum,UNUM_SIGNIFICANT_DIGITS_USED)); | |
3749 | ulen = unum_toPattern(unum, FALSE, ubuf, kUBufMax, &status); | |
3750 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
3751 | if ( U_SUCCESS(status) ) { | |
3752 | log_info("unum_toPattern (%d): %s\n", ulen, bbuf); | |
3753 | } | |
3754 | ||
3755 | status = U_ZERO_ERROR; | |
3756 | ulen = unum_formatDouble(unum, value, ubuf, kUBufMax, NULL, &status); | |
3757 | if ( U_FAILURE(status) ) { | |
3758 | log_err("unum_formatDouble value %.1f status %s\n", value, u_errorName(status)); | |
3759 | } else if (u_strcmp(ubuf,u"1.03") != 0) { | |
3760 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
3761 | log_err("unum_formatDouble value %.1f expected 1.03, got %s\n", value, bbuf); | |
3762 | } | |
3763 | unum_close(unum); | |
3764 | } | |
3765 | } | |
3766 | ||
3767 | // Apple for <rdar://problem/46755430> | |
3768 | typedef struct { | |
3769 | const char* descrip; | |
3770 | const char* locale; | |
3771 | const UChar* setPosPrefix; | |
3772 | const UChar* setPosSuffix; | |
3773 | const UChar* setNegPrefix; | |
3774 | const UChar* setNegSuffix; | |
3775 | const UChar* expPosPrefix; | |
3776 | const UChar* expPosSuffix; | |
3777 | const UChar* expNegPrefix; | |
3778 | const UChar* expNegSuffix; | |
3779 | const UChar* expPattern; | |
3780 | double value; | |
3781 | const UChar* expPosFormat; | |
3782 | const UChar* expNegFormat; | |
3783 | } SetAffixOnCurrFmtItem; | |
3784 | ||
3785 | static const SetAffixOnCurrFmtItem affixOnCurrFmtItems[] = { | |
3786 | // descrip loc sPP sPS sNP sNS ePP ePS eNP eNS ePattern value ePosFmt eNegFmt | |
3787 | { "01 en set no affix ", "en", NULL, NULL, NULL, NULL, u"¤", u"", u"-¤", u"", u"¤#,##0.00", 123.4, u"¤123.40", u"-¤123.40" }, | |
3788 | { "02 en set + prefix", "en", u"$", NULL, NULL, NULL, u"$", u"", u"-¤", u"", u"$#,##0.00;-¤#,##0.00", 123.4, u"$123.40", u"-¤123.40" }, | |
3789 | { "03 en set +- prefix", "en", u"$", NULL, u"-$", NULL, u"$", u"", u"-$", u"", u"$#,##0.00;'-'$#,##0.00", 123.4, u"$123.40", u"-$123.40" }, | |
3790 | { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0.0,NULL,NULL } | |
3791 | }; | |
3792 | ||
3793 | // Apple only for <rdar://problem/46755430> | |
3794 | static void TestSetAffixOnCurrFmt(void) { | |
3795 | const SetAffixOnCurrFmtItem* itemPtr; | |
3796 | for (itemPtr = affixOnCurrFmtItems; itemPtr->descrip != NULL; itemPtr++) { | |
3797 | UChar ubuf[kUBufMax]; | |
3798 | char bbufe[kBBufMax]; | |
3799 | char bbufg[kBBufMax]; | |
3800 | int32_t ulen; | |
3801 | UErrorCode status = U_ZERO_ERROR; | |
3802 | UNumberFormat* unf = unum_open(UNUM_CURRENCY, NULL, 0, itemPtr->locale, NULL, &status); | |
3803 | if (U_FAILURE(status)) { | |
3804 | log_data_err("locale %s: unum_open UNUM_CURRENCY fails with %s\n", itemPtr->locale, u_errorName(status)); | |
3805 | continue; | |
3806 | } | |
3807 | ||
3808 | if (itemPtr->setPosPrefix) { | |
3809 | status = U_ZERO_ERROR; | |
3810 | unum_setTextAttribute(unf, UNUM_POSITIVE_PREFIX, itemPtr->setPosPrefix, -1, &status); | |
3811 | if (U_FAILURE(status)) { | |
3812 | log_err("test %s: unum_setTextAttribute UNUM_POSITIVE_PREFIX fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3813 | } | |
3814 | } | |
3815 | if (itemPtr->setPosSuffix) { | |
3816 | status = U_ZERO_ERROR; | |
3817 | unum_setTextAttribute(unf, UNUM_POSITIVE_SUFFIX, itemPtr->setPosSuffix, -1, &status); | |
3818 | if (U_FAILURE(status)) { | |
3819 | log_err("test %s: unum_setTextAttribute UNUM_POSITIVE_SUFFIX fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3820 | } | |
3821 | } | |
3822 | if (itemPtr->setNegPrefix) { | |
3823 | status = U_ZERO_ERROR; | |
3824 | unum_setTextAttribute(unf, UNUM_NEGATIVE_PREFIX, itemPtr->setNegPrefix, -1, &status); | |
3825 | if (U_FAILURE(status)) { | |
3826 | log_err("test %s: unum_setTextAttribute UNUM_NEGATIVE_PREFIX fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3827 | } | |
3828 | } | |
3829 | if (itemPtr->setNegSuffix) { | |
3830 | status = U_ZERO_ERROR; | |
3831 | unum_setTextAttribute(unf, UNUM_NEGATIVE_SUFFIX, itemPtr->setNegSuffix, -1, &status); | |
3832 | if (U_FAILURE(status)) { | |
3833 | log_err("test %s: unum_setTextAttribute UNUM_NEGATIVE_SUFFIX fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3834 | } | |
3835 | } | |
3836 | ||
3837 | status = U_ZERO_ERROR; | |
3838 | ulen = unum_getTextAttribute(unf, UNUM_POSITIVE_PREFIX, ubuf, kUBufMax, &status); | |
3839 | if (U_FAILURE(status)) { | |
3840 | log_err("test %s: unum_getTextAttribute UNUM_POSITIVE_PREFIX fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3841 | } else if (u_strcmp(ubuf,itemPtr->expPosPrefix)!=0) { | |
3842 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expPosPrefix, -1, &status); | |
3843 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
3844 | log_err("test %s: unum_getTextAttribute UNUM_POSITIVE_PREFIX expect %s, get %s\n", itemPtr->descrip, bbufe, bbufg); | |
3845 | } | |
3846 | status = U_ZERO_ERROR; | |
3847 | ulen = unum_getTextAttribute(unf, UNUM_POSITIVE_SUFFIX, ubuf, kUBufMax, &status); | |
3848 | if (U_FAILURE(status)) { | |
3849 | log_err("test %s: unum_getTextAttribute UNUM_POSITIVE_SUFFIX fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3850 | } else if (u_strcmp(ubuf,itemPtr->expPosSuffix)!=0) { | |
3851 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expPosSuffix, -1, &status); | |
3852 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
3853 | log_err("test %s: unum_getTextAttribute UNUM_POSITIVE_SUFFIX expect %s, get %s\n", itemPtr->descrip, bbufe, bbufg); | |
3854 | } | |
3855 | status = U_ZERO_ERROR; | |
3856 | ulen = unum_getTextAttribute(unf, UNUM_NEGATIVE_PREFIX, ubuf, kUBufMax, &status); | |
3857 | if (U_FAILURE(status)) { | |
3858 | log_err("test %s: unum_getTextAttribute UNUM_NEGATIVE_PREFIX fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3859 | } else if (u_strcmp(ubuf,itemPtr->expNegPrefix)!=0) { | |
3860 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expNegPrefix, -1, &status); | |
3861 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
3862 | log_err("test %s: unum_getTextAttribute UNUM_NEGATIVE_PREFIX expect %s, get %s\n", itemPtr->descrip, bbufe, bbufg); | |
3863 | } | |
3864 | status = U_ZERO_ERROR; | |
3865 | ulen = unum_getTextAttribute(unf, UNUM_NEGATIVE_SUFFIX, ubuf, kUBufMax, &status); | |
3866 | if (U_FAILURE(status)) { | |
3867 | log_err("test %s: unum_getTextAttribute UNUM_NEGATIVE_SUFFIX fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3868 | } else if (u_strcmp(ubuf,itemPtr->expNegSuffix)!=0) { | |
3869 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expNegSuffix, -1, &status); | |
3870 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
3871 | log_err("test %s: unum_getTextAttribute UNUM_NEGATIVE_SUFFIX expect %s, get %s\n", itemPtr->descrip, bbufe, bbufg); | |
3872 | } | |
3873 | status = U_ZERO_ERROR; | |
3874 | ulen = unum_toPattern(unf, FALSE, ubuf, kUBufMax, &status); | |
3875 | if (U_FAILURE(status)) { | |
3876 | log_err("test %s: unum_toPattern fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3877 | } else if (u_strcmp(ubuf,itemPtr->expPattern)!=0) { | |
3878 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expPattern, -1, &status); | |
3879 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
3880 | log_err("test %s: unum_toPattern expect %s, get %s\n", itemPtr->descrip, bbufe, bbufg); | |
3881 | } | |
3882 | ||
3883 | status = U_ZERO_ERROR; | |
3884 | ulen = unum_formatDouble(unf, itemPtr->value, ubuf, kUBufMax, NULL, &status); | |
3885 | if (U_FAILURE(status)) { | |
3886 | log_err("test %s: unum_formatDouble positive fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3887 | } else if (u_strcmp(ubuf,itemPtr->expPosFormat)!=0) { | |
3888 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expPosFormat, -1, &status); | |
3889 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
3890 | log_err("test %s: unum_formatDouble positive expect %s, get %s\n", itemPtr->descrip, bbufe, bbufg); | |
3891 | } | |
3892 | status = U_ZERO_ERROR; | |
3893 | ulen = unum_formatDouble(unf, -1.0*itemPtr->value, ubuf, kUBufMax, NULL, &status); | |
3894 | if (U_FAILURE(status)) { | |
3895 | log_err("test %s: unum_formatDouble negative fails with %s\n", itemPtr->descrip, u_errorName(status)); | |
3896 | } else if (u_strcmp(ubuf,itemPtr->expNegFormat)!=0) { | |
3897 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expNegFormat, -1, &status); | |
3898 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
3899 | log_err("test %s: unum_formatDouble negative expect %s, get %s\n", itemPtr->descrip, bbufe, bbufg); | |
3900 | } | |
3901 | ||
3902 | unum_close(unf); | |
3903 | } | |
3904 | } | |
3905 | ||
3906 | // Apple only for <rdar://problem/46915356> | |
3907 | static void TestParseWithEmptyCurr(void) { | |
3908 | UErrorCode status = U_ZERO_ERROR; | |
3909 | UNumberFormat* unum = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status); | |
3910 | if (U_FAILURE(status)) { | |
3911 | log_data_err("unum_open UNUM_CURRENCY for \"en_US\" fails with %s\n", u_errorName(status)); | |
3912 | } else { | |
3913 | unum_setSymbol(unum, UNUM_CURRENCY_SYMBOL, u"", 0, &status); | |
3914 | if (U_FAILURE(status)) { | |
3915 | log_err("unum_setSymbol UNUM_CURRENCY_SYMBOL u\"\" fails with %s\n", u_errorName(status)); | |
3916 | } else { | |
3917 | char bbuf[kBBufMax] = { 0 }; | |
3918 | UChar curr[4] = { 0 }; | |
3919 | int32_t ppos, blen; | |
3920 | double val; | |
3921 | const UChar* text = u"3"; | |
3922 | ||
3923 | status = U_ZERO_ERROR; | |
3924 | ppos = 0; | |
3925 | blen = unum_parseDecimal(unum, text, -1, &ppos, bbuf, kBBufMax, &status); | |
3926 | if (U_FAILURE(status)) { | |
3927 | log_err("unum_parseDecimal u\"3\" with empty curr symbol fails with %s, ppos %d\n", u_errorName(status), ppos); | |
3928 | } else if (ppos != 1 || blen != 1 || bbuf[0] != '3') { | |
3929 | log_err("unum_parseDecimal expect ppos 1, blen 1, str 3; get %d, %d, %s\n", ppos, blen, bbuf); | |
3930 | } | |
3931 | ||
3932 | status = U_ZERO_ERROR; | |
3933 | ppos = 0; | |
3934 | val = unum_parseDouble(unum, text, -1, &ppos, &status); | |
3935 | if (U_FAILURE(status)) { | |
3936 | log_err("unum_parseDouble u\"3\" with empty curr symbol fails with %s, ppos %d\n", u_errorName(status), ppos); | |
3937 | } else if (ppos != 1 || val != 3.0) { | |
3938 | log_err("unum_parseDouble expect ppos 1, val 3.0; get %d, %.2f\n", ppos, val); | |
3939 | } | |
3940 | ||
3941 | status = U_ZERO_ERROR; | |
3942 | ppos = 0; | |
3943 | val = unum_parseDoubleCurrency(unum, text, -1, &ppos, curr, &status); | |
3944 | if (U_SUCCESS(status)) { | |
3945 | log_err("unum_parseDoubleCurrency u\"3\" with empty curr symbol succeeds, get ppos %d, val %.2f\n", ppos, val); | |
3946 | } | |
3947 | } | |
3948 | unum_close(unum); | |
3949 | } | |
3950 | ||
3951 | // Additions for <rdar://problem/51938595> | |
3952 | // "¤#,##0.00" "¤ #,##0.00" "#,##0.00 ¤" "#,##,##0.00¤" | |
3953 | static const char* locales[] = {"en_US", "en_NO", "en_CZ", "en_BD", NULL }; | |
3954 | const char ** localesPtr = locales; | |
3955 | const char* locale; | |
3956 | while ((locale = *localesPtr++) != NULL) { | |
3957 | status = U_ZERO_ERROR; | |
3958 | unum = unum_open(UNUM_CURRENCY, NULL, 0, locale, NULL, &status); | |
3959 | if (U_FAILURE(status)) { | |
3960 | log_data_err("locale %s unum_open UNUM_CURRENCY fails with %s\n", locale, u_errorName(status)); | |
3961 | } else { | |
3962 | char bbuf[kBBufMax] = { 0 }; | |
3963 | UChar curr[4] = { 0 }; | |
3964 | UChar ubuf[kUBufMax]; | |
3965 | int32_t ppos, blen, ulen; | |
3966 | const double posValToUse = 37.0; | |
3967 | const double negValToUse = -3.0; | |
3968 | double val; | |
3969 | ||
3970 | status = U_ZERO_ERROR; | |
3971 | unum_setSymbol(unum, UNUM_CURRENCY_SYMBOL, u"", 0, &status); | |
3972 | if (U_FAILURE(status)) { | |
3973 | log_err("locale %s unum_setSymbol UNUM_CURRENCY_SYMBOL u\"\" fails with %s, skipping\n", locale, u_errorName(status)); | |
3974 | continue; | |
3975 | } | |
3976 | ||
3977 | status = U_ZERO_ERROR; | |
3978 | ulen = unum_formatDouble(unum, posValToUse, ubuf, kUBufMax, NULL, &status); | |
3979 | if (U_FAILURE(status)) { | |
3980 | log_err("locale %s unum_formatDouble %.1f fails with %s, skipping\n", locale, posValToUse, u_errorName(status)); | |
3981 | continue; | |
3982 | } | |
3983 | ||
3984 | status = U_ZERO_ERROR; | |
3985 | ppos = 0; | |
3986 | val = unum_parseDouble(unum, ubuf, ulen, &ppos, &status); | |
3987 | if (U_FAILURE(status)) { | |
3988 | log_err("locale %s unum_parseDouble fails with %s, ppos %d, expect %.1f\n", locale, u_errorName(status), ppos, posValToUse); | |
3989 | } else if (ppos != ulen || val != posValToUse) { | |
3990 | log_err("locale %s unum_parseDouble expect ppos %d, val %.1f; get %d, %.2f\n", locale, ulen, posValToUse, ppos, val); | |
3991 | } | |
3992 | ||
3993 | status = U_ZERO_ERROR; | |
3994 | ulen = unum_formatDouble(unum, negValToUse, ubuf, kUBufMax, NULL, &status); | |
3995 | if (U_FAILURE(status)) { | |
3996 | log_err("locale %s unum_formatDouble %.1f fails with %s, skipping\n", locale, negValToUse, u_errorName(status)); | |
3997 | continue; | |
3998 | } | |
3999 | ||
4000 | status = U_ZERO_ERROR; | |
4001 | ppos = 0; | |
4002 | val = unum_parseDouble(unum, ubuf, ulen, &ppos, &status); | |
4003 | if (U_FAILURE(status)) { | |
4004 | log_err("locale %s unum_parseDouble fails with %s, ppos %d, expect %.1f\n", locale, u_errorName(status), ppos, negValToUse); | |
4005 | } else if (ppos != ulen || val != negValToUse) { | |
4006 | log_err("locale %s unum_parseDouble expect ppos %d, val %.1f; get %d, %.2f\n", locale, ulen, negValToUse, ppos, val); | |
4007 | } | |
4008 | ||
4009 | status = U_ZERO_ERROR; | |
4010 | unum_applyPattern(unum, FALSE, u"#,##0.00¤", -1, NULL, &status); | |
4011 | if (U_FAILURE(status)) { | |
4012 | log_err("locale %s unum_applyPattern \"#,##0.00¤\" fails with %s, skipping\n", locale, u_errorName(status)); | |
4013 | continue; | |
4014 | } | |
4015 | ||
4016 | status = U_ZERO_ERROR; | |
4017 | ulen = unum_formatDouble(unum, posValToUse, ubuf, kUBufMax, NULL, &status); | |
4018 | if (U_FAILURE(status)) { | |
4019 | log_err("locale %s with \"#,##0.00¤\" unum_formatDouble %.1f fails with %s, skipping\n", locale, posValToUse, u_errorName(status)); | |
4020 | continue; | |
4021 | } | |
4022 | ||
4023 | status = U_ZERO_ERROR; | |
4024 | ppos = 0; | |
4025 | val = unum_parseDouble(unum, ubuf, ulen, &ppos, &status); | |
4026 | if (U_FAILURE(status)) { | |
4027 | log_err("locale %s with \"#,##0.00¤\" unum_parseDouble fails with %s, ppos %d, expect %.1f\n", locale, u_errorName(status), ppos, posValToUse); | |
4028 | } else if (ppos != ulen || val != posValToUse) { | |
4029 | log_err("locale %s with \"#,##0.00¤\" unum_parseDouble expect ppos %d, val %.1f; get %d, %.2f\n", locale, ulen, posValToUse, ppos, val); | |
4030 | } | |
4031 | ||
4032 | status = U_ZERO_ERROR; | |
4033 | ulen = unum_formatDouble(unum, negValToUse, ubuf, kUBufMax, NULL, &status); | |
4034 | if (U_FAILURE(status)) { | |
4035 | log_err("locale %s with \"#,##0.00¤\" unum_formatDouble %.1f fails with %s, skipping\n", locale, negValToUse, u_errorName(status)); | |
4036 | continue; | |
4037 | } | |
4038 | ||
4039 | status = U_ZERO_ERROR; | |
4040 | ppos = 0; | |
4041 | val = unum_parseDouble(unum, ubuf, ulen, &ppos, &status); | |
4042 | if (U_FAILURE(status)) { | |
4043 | log_err("locale %s with \"#,##0.00¤\" unum_parseDouble fails with %s, ppos %d, expect %.1f\n", locale, u_errorName(status), ppos, negValToUse); | |
4044 | } else if (ppos != ulen || val != negValToUse) { | |
4045 | log_err("locale %s with \"#,##0.00¤\" unum_parseDouble expect ppos %d, val %.1f; get %d, %.2f\n", locale, ulen, negValToUse, ppos, val); | |
4046 | } | |
4047 | ||
4048 | unum_close(unum); | |
4049 | } | |
4050 | } | |
4051 | } | |
4052 | ||
4053 | // Apple only for <rdar://problem/50113359> | |
4054 | static const UChar* pat1 = u"#.##E+00;-#.##E+00"; | |
4055 | static void TestSciNotationNumbers(void) { | |
4056 | UErrorCode status = U_ZERO_ERROR; | |
4057 | UNumberFormat* unum = unum_open(UNUM_PATTERN_DECIMAL, NULL, 0, "en_US", NULL, &status); | |
4058 | if ( U_FAILURE(status) ) { | |
4059 | log_data_err("unum_open UNUM_PATTERN_DECIMAL with null pattern for \"en_US\" fails with %s\n", u_errorName(status)); | |
4060 | } else { | |
4061 | unum_applyPattern(unum, FALSE, pat1, u_strlen(pat1), NULL, &status); | |
4062 | if ( U_FAILURE(status) ) { | |
4063 | log_err("unum_applyPattern fails with %s\n", u_errorName(status)); | |
4064 | } else { | |
4065 | double value; | |
4066 | UChar ubuf[kUBufMax]; | |
4067 | char bbuf[kBBufMax]; | |
4068 | int32_t ulen; | |
4069 | ||
4070 | unum_setAttribute(unum, UNUM_MIN_FRACTION_DIGITS, 0); | |
4071 | unum_setAttribute(unum, UNUM_MAX_FRACTION_DIGITS, 2147483647); | |
4072 | log_info("unum_getAttribute minInt %d maxInt %d minFrac %d maxFrac %d\n", | |
4073 | unum_getAttribute(unum,UNUM_MIN_INTEGER_DIGITS), unum_getAttribute(unum,UNUM_MAX_INTEGER_DIGITS), | |
4074 | unum_getAttribute(unum,UNUM_MIN_FRACTION_DIGITS), unum_getAttribute(unum,UNUM_MAX_FRACTION_DIGITS)); | |
4075 | ulen = unum_toPattern(unum, FALSE, ubuf, kUBufMax, &status); | |
4076 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4077 | if ( U_SUCCESS(status) ) { | |
4078 | log_info("unum_toPattern (%d): %s\n", ulen, bbuf); | |
4079 | } | |
4080 | ||
4081 | for (value = 10.0; value < 1000000000.0; value *= 10.0) { | |
4082 | status = U_ZERO_ERROR; | |
4083 | ulen = unum_formatDouble(unum, value, ubuf, kUBufMax, NULL, &status); | |
4084 | if ( U_FAILURE(status) ) { | |
4085 | log_err("unum_formatDouble value %.1f status %s\n", value, u_errorName(status)); | |
4086 | } else if (u_strncmp(ubuf,u"1E+0",4) != 0) { | |
4087 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4088 | log_err("unum_formatDouble value %.1f expected result to begin with 1E+0, got %s\n", value, bbuf); | |
4089 | } | |
4090 | } | |
4091 | } | |
4092 | unum_close(unum); | |
4093 | } | |
4094 | } | |
4095 | ||
4096 | // Apple only for <rdar://problem/51601250> | |
4097 | static const UChar* patFmt3Exp = u"0.000E+00"; | |
4098 | static void TestSciNotationPrecision(void) { | |
4099 | UErrorCode status = U_ZERO_ERROR; | |
4100 | UNumberFormat* unum = unum_open(UNUM_PATTERN_DECIMAL, NULL, 0, "en_US", NULL, &status); | |
4101 | if ( U_FAILURE(status) ) { | |
4102 | log_data_err("unum_open UNUM_PATTERN_DECIMAL with null pattern for \"en_US\" fails with %s\n", u_errorName(status)); | |
4103 | } else { | |
4104 | unum_applyPattern(unum, FALSE, pat1, u_strlen(pat1), NULL, &status); | |
4105 | if ( U_FAILURE(status) ) { | |
4106 | log_err("unum_applyPattern fails with %s\n", u_errorName(status)); | |
4107 | } else { | |
4108 | double value; | |
4109 | UChar ubuf[kUBufMax]; | |
4110 | char bexp[kBBufMax]; | |
4111 | char bget[kBBufMax]; | |
4112 | int32_t ulen; | |
4113 | ||
4114 | unum_setAttribute(unum, UNUM_MIN_FRACTION_DIGITS, 3); | |
4115 | unum_setAttribute(unum, UNUM_MAX_FRACTION_DIGITS, 3); | |
4116 | unum_setAttribute(unum, UNUM_GROUPING_USED, 0); | |
4117 | ||
4118 | status = U_ZERO_ERROR; | |
4119 | ulen = unum_toPattern(unum, FALSE, ubuf, kUBufMax, &status); | |
4120 | if ( U_FAILURE(status) ) { | |
4121 | log_err("unum_toPattern fails with %s\n", u_errorName(status)); | |
4122 | } else if (u_strcmp(ubuf,patFmt3Exp) != 0) { | |
4123 | u_strToUTF8(bget, kBBufMax, NULL, ubuf, ulen, &status); | |
4124 | log_info("unum_toPattern, get \"%s\"\n", bget); | |
4125 | } | |
4126 | ||
4127 | status = U_ZERO_ERROR; | |
4128 | ulen = unum_formatDouble(unum, 0.0, ubuf, ulen, NULL, &status); | |
4129 | if ( U_FAILURE(status) ) { | |
4130 | log_err("unum_formatDouble fails with %s\n", u_errorName(status)); | |
4131 | } else if (u_strcmp(ubuf,patFmt3Exp) != 0) { | |
4132 | u_strToUTF8(bexp, kBBufMax, NULL, patFmt3Exp, -1, &status); | |
4133 | u_strToUTF8(bget, kBBufMax, NULL, ubuf, ulen, &status); | |
4134 | log_err("unum_formatDouble error, expect \"%s\", get \"%s\"\n", bexp, bget); | |
4135 | } | |
4136 | } | |
4137 | unum_close(unum); | |
4138 | } | |
4139 | } | |
4140 | ||
4141 | // Apple only for <rdar://problem/49808819> | |
4142 | static const char* minGroupLocale = "pt_PT"; // has minimumGroupingDigits{"2"} | |
4143 | typedef struct { | |
4144 | double value; | |
4145 | const char* string; | |
4146 | const UChar* expectDecimal; | |
4147 | const UChar* expectCurrency; | |
4148 | } TestMinGroupItem; | |
4149 | static const TestMinGroupItem minGroupItems[] = { | |
4150 | { 123.0, "123.0", u"123,00", u"123,00 €" }, | |
4151 | { 1234.0, "1234.0", u"1234,00", u"1234,00 €" }, | |
4152 | { 12345.0, "12345.0", u"12 345,00", u"12 345,00 €" }, | |
4153 | { 123456.0, "123456.0", u"123 456,00", u"123 456,00 €" }, | |
4154 | { 1234567.0, "1234567.0", u"1 234 567,00", u"1 234 567,00 €" }, | |
4155 | { 12345678.0, "12345678.0", u"12 345 678,00", u"12 345 678,00 €" }, | |
4156 | { 0.0, NULL, NULL, NULL } // terminator | |
4157 | }; | |
4158 | static void TestMinimumGrouping(void) { | |
4159 | UErrorCode status = U_ZERO_ERROR; | |
4160 | UNumberFormat* unumd = unum_open(UNUM_DECIMAL, NULL, 0, minGroupLocale, NULL, &status); | |
4161 | UNumberFormat* unumc = unum_open(UNUM_CURRENCY, NULL, 0, minGroupLocale, NULL, &status); | |
4162 | if ( U_FAILURE(status) ) { | |
4163 | log_data_err("unum_open UNUM_PATTERN_DECIMAL/CURRENCY for %s fails with %s\n", minGroupLocale, u_errorName(status)); | |
4164 | } else { | |
4165 | const TestMinGroupItem* itemPtr; | |
4166 | unum_setAttribute(unumd, UNUM_MIN_FRACTION_DIGITS, 2); | |
4167 | for (itemPtr = minGroupItems; itemPtr->value != 0.0; itemPtr++) { | |
4168 | UChar ubuf[kUBufMax]; | |
4169 | char bbufe[kBBufMax]; | |
4170 | char bbufg[kBBufMax]; | |
4171 | int32_t ulen; | |
4172 | ||
4173 | status = U_ZERO_ERROR; | |
4174 | ulen = unum_formatDecimal(unumd, itemPtr->string, -1, ubuf, kUBufMax, NULL, &status); | |
4175 | if ( U_FAILURE(status) ) { | |
4176 | log_err("unum_formatDecimal DEC for locale %s, value %.1f fails with %s\n", minGroupLocale, itemPtr->value, u_errorName(status)); | |
4177 | } else if (u_strcmp(ubuf, itemPtr->expectDecimal) != 0) { | |
4178 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expectDecimal, -1, &status); | |
4179 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
4180 | log_err("unum_formatDecimal DEC for locale %s, value %.1f, expected \"%s\" but got \"%s\"\n", | |
4181 | minGroupLocale, itemPtr->value, bbufe, bbufg); | |
4182 | } | |
4183 | ||
4184 | status = U_ZERO_ERROR; | |
4185 | ulen = unum_formatDouble(unumd, itemPtr->value, ubuf, kUBufMax, NULL, &status); | |
4186 | if ( U_FAILURE(status) ) { | |
4187 | log_err("unum_formatDouble DEC for locale %s, value %.1f fails with %s\n", minGroupLocale, itemPtr->value, u_errorName(status)); | |
4188 | } else if (u_strcmp(ubuf, itemPtr->expectDecimal) != 0) { | |
4189 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expectDecimal, -1, &status); | |
4190 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
4191 | log_err("unum_formatDouble DEC for locale %s, value %.1f, expected \"%s\" but got \"%s\"\n", | |
4192 | minGroupLocale, itemPtr->value, bbufe, bbufg); | |
4193 | } | |
4194 | ||
4195 | status = U_ZERO_ERROR; | |
4196 | ulen = unum_formatDouble(unumc, itemPtr->value, ubuf, kUBufMax, NULL, &status); | |
4197 | if ( U_FAILURE(status) ) { | |
4198 | log_err("unum_formatDouble CUR for locale %s, value %.1f fails with %s\n", minGroupLocale, itemPtr->value, u_errorName(status)); | |
4199 | } else if (u_strcmp(ubuf, itemPtr->expectCurrency) != 0) { | |
4200 | u_strToUTF8(bbufe, kBBufMax, NULL, itemPtr->expectCurrency, -1, &status); | |
4201 | u_strToUTF8(bbufg, kBBufMax, NULL, ubuf, ulen, &status); | |
4202 | log_err("unum_formatDouble CUR for locale %s, value %.1f, expected \"%s\" but got \"%s\"\n", | |
4203 | minGroupLocale, itemPtr->value, bbufe, bbufg); | |
4204 | } | |
4205 | } | |
4206 | unum_close(unumc); | |
4207 | unum_close(unumd); | |
4208 | } | |
4209 | } | |
4210 | ||
4211 | // Apple only for <rdar://problem/49120648> | |
4212 | static const char* const numSysLocales[] = { | |
4213 | "en_US", "en_US@numbers=arab", "en_US@numbers=roman", "en_US@numbers=grek", "en_US@numbers=hebr", | |
4214 | "zh@numbers=hanidec", "zh_Hant@numbers=traditional", "en@numbers=finance", NULL | |
4215 | }; | |
4216 | static void TestNumberSystemsMultiplier(void) { | |
4217 | const char* const* localesPtr = numSysLocales; | |
4218 | const char* locale; | |
4219 | while ((locale = *localesPtr++) != NULL) { | |
4220 | UErrorCode status = U_ZERO_ERROR; | |
4221 | UNumberFormat *unum = unum_open(UNUM_DECIMAL, NULL, 0, locale, NULL, &status); | |
4222 | if ( U_FAILURE(status) ) { | |
4223 | log_data_err("unum_open UNUM_DECIMAL for %s fails with %s\n", locale, u_errorName(status)); | |
4224 | } else { | |
4225 | int32_t multiplier = unum_getAttribute(unum, UNUM_MULTIPLIER); | |
4226 | log_info("for locale %s with UNUM_DECIMAL, UNUM_MULTIPLIER is %d\n", locale, multiplier); | |
4227 | unum_close(unum); | |
4228 | } | |
4229 | } | |
4230 | } | |
4231 | ||
4232 | // Apple only for <rdar://problem/39156484> | |
4233 | typedef struct { | |
4234 | const char* locale; | |
4235 | const UChar* stringToParse; | |
4236 | double expectValue; | |
4237 | int32_t expectPos; | |
4238 | } ParseScientificItem; | |
4239 | static const ParseScientificItem parseSciItems[] = { | |
4240 | { "en_US", u"4E\u200E+05", 400000.0, 6 }, | |
4241 | { "en_US", u"4E+05", 400000.0, 5 }, | |
4242 | { "en_US", u"4E\u200E-02", 0.04, 6 }, | |
4243 | { "en_US", u"4E-02", 0.04, 5 }, | |
4244 | { "he_IL", u"4E\u200E+05", 400000.0, 6 }, | |
4245 | { "he_IL", u"4E+05", 400000.0, 5 }, | |
4246 | { "he_IL", u"4E\u200E-02", 0.04, 6 }, | |
4247 | { "he_IL", u"4E-02", 0.04, 5 }, | |
4248 | { "en@numbers=arabext", u"\u06F4\u00D7\u06F1\u06F0^\u200E+\u200E\u06F0\u06F5", 400000.0, 10 }, | |
4249 | { "en@numbers=arabext", u"\u06F4\u00D7\u06F1\u06F0^+\u06F0\u06F5", 400000.0, 8 }, | |
4250 | { NULL,NULL, 0.0, 0 } // terminator | |
4251 | }; | |
4252 | static void TestParseScientific(void) { | |
4253 | const ParseScientificItem* itemPtr; | |
4254 | for (itemPtr = parseSciItems; itemPtr->locale != NULL; itemPtr++) { | |
4255 | UErrorCode status = U_ZERO_ERROR; | |
4256 | UNumberFormat *unum = unum_open(UNUM_SCIENTIFIC, NULL, 0, itemPtr->locale, NULL, &status); | |
4257 | if ( U_FAILURE(status) ) { | |
4258 | log_data_err("unum_open UNUM_SCIENTIFIC for %s fails with %s\n", itemPtr->locale, u_errorName(status)); | |
4259 | } else { | |
4260 | int32_t parsePos = 0; | |
4261 | double result = unum_parseDouble(unum, itemPtr->stringToParse, -1, &parsePos, &status); | |
4262 | if ( U_FAILURE(status) ) { | |
4263 | log_err("unum_parseDouble for %s fails with %s\n", itemPtr->locale, u_errorName(status)); | |
4264 | } else if (result != itemPtr->expectValue || parsePos != itemPtr->expectPos) { | |
4265 | log_err("unum_parseDouble for %s, expected result %.1f pos %d, got %.1f %d\n", | |
4266 | itemPtr->locale, itemPtr->expectValue, itemPtr->expectPos, result, parsePos); | |
4267 | } | |
4268 | unum_close(unum); | |
4269 | } | |
4270 | } | |
4271 | } | |
4272 | ||
4273 | // Apple only for <rdar://problem/51985640> | |
4274 | typedef struct { | |
4275 | const char* locale; | |
4276 | const UChar* expCurrCode; | |
4277 | } LocaleCurrCodeItem; | |
4278 | static const LocaleCurrCodeItem currCodeItems[] = { | |
4279 | { "en", u"" }, // curr ICU has "XXX" | |
4280 | { "en@currency=EUR", u"EUR" }, | |
4281 | { "en_US", u"USD" }, | |
4282 | { "en_ZZ", u"XAG" }, | |
4283 | { "_US", u"USD" }, | |
4284 | { "_ZZ", u"XAG" }, | |
4285 | { "us", u"" }, // curr ICU has "XXX" | |
4286 | { "", u"" }, // curr ICU has "XXX" | |
4287 | { NULL, NULL } | |
4288 | }; | |
4289 | static void TestCurrForUnkRegion(void) { | |
4290 | const LocaleCurrCodeItem* itemPtr; | |
4291 | for (itemPtr = currCodeItems; itemPtr->locale != NULL; itemPtr++) { | |
4292 | UErrorCode status = U_ZERO_ERROR; | |
4293 | UNumberFormat* unum = unum_open(UNUM_CURRENCY, NULL, 0, itemPtr->locale, NULL, &status); | |
4294 | if ( U_FAILURE(status) ) { | |
4295 | log_data_err("unum_open UNUM_CURRENCY for %s fails with %s\n", itemPtr->locale, u_errorName(status)); | |
4296 | } else { | |
4297 | UChar ubuf[kUBufMax]; | |
4298 | int32_t ulen = unum_getTextAttribute(unum, UNUM_CURRENCY_CODE, ubuf, kUBufMax, &status); | |
4299 | if ( U_FAILURE(status) ) { | |
4300 | log_err("unum_getTextAttribute UNUM_CURRENCY_CODE for %s fails with %s\n", itemPtr->locale, u_errorName(status)); | |
4301 | } else if (u_strcmp(ubuf,itemPtr->expCurrCode)!=0) { | |
4302 | char bexp[kBBufMax]; | |
4303 | char bget[kBBufMax]; | |
4304 | u_strToUTF8(bexp, kBBufMax, NULL, itemPtr->expCurrCode, -1, &status); | |
4305 | u_strToUTF8(bget, kBBufMax, NULL, ubuf, ulen, &status); | |
4306 | log_err("unum_getTextAttribute UNUM_CURRENCY_CODE error for %s, expect \"%s\" but get \"%s\"\n", itemPtr->locale, bexp, bget); | |
4307 | } | |
4308 | status = U_ZERO_ERROR; | |
4309 | unum_setTextAttribute(unum, UNUM_CURRENCY_CODE, u"XXX", 3, &status); | |
4310 | if ( U_FAILURE(status) ) { | |
4311 | log_err("unum_setTextAttribute UNUM_CURRENCY_CODE for %s fails with %s\n", itemPtr->locale, u_errorName(status)); | |
4312 | } else { | |
4313 | ulen = unum_getTextAttribute(unum, UNUM_CURRENCY_CODE, ubuf, kUBufMax, &status); | |
4314 | if ( U_FAILURE(status) ) { | |
4315 | log_err("unum_getTextAttribute UNUM_CURRENCY_CODE XXX for %s fails with %s\n", itemPtr->locale, u_errorName(status)); | |
4316 | } else if (u_strcmp(ubuf,u"XXX")!=0) { | |
4317 | char bget[kBBufMax]; | |
4318 | u_strToUTF8(bget, kBBufMax, NULL, ubuf, ulen, &status); | |
4319 | log_err("unum_getTextAttribute UNUM_CURRENCY_CODE error for %s, expect XXX but get \"%s\"\n", itemPtr->locale, bget); | |
4320 | } | |
4321 | } | |
4322 | unum_close(unum); | |
4323 | } | |
4324 | } | |
4325 | } | |
4326 | ||
4327 | static void TestMinIntMinFracZero(void) { | |
4328 | UChar ubuf[kUBufMax]; | |
4329 | char bbuf[kBBufMax]; | |
4330 | int minInt, minFrac, maxFrac, ulen; | |
4331 | ||
4332 | UErrorCode status = U_ZERO_ERROR; | |
4333 | UNumberFormat* unum = unum_open(UNUM_DECIMAL, NULL, 0, "en_US", NULL, &status); | |
4334 | if ( U_FAILURE(status) ) { | |
4335 | log_data_err("unum_open UNUM_DECIMAL for en_US fails with %s\n", u_errorName(status)); | |
4336 | } else { | |
4337 | unum_setAttribute(unum, UNUM_MIN_INTEGER_DIGITS, 0); | |
4338 | unum_setAttribute(unum, UNUM_MIN_FRACTION_DIGITS, 0); | |
4339 | minInt = unum_getAttribute(unum, UNUM_MIN_INTEGER_DIGITS); | |
4340 | minFrac = unum_getAttribute(unum, UNUM_MIN_FRACTION_DIGITS); | |
4341 | if (minInt != 0 || minFrac != 0) { | |
4342 | log_err("after setting minInt=minFrac=0, get minInt %d, minFrac %d\n", minInt, minFrac); | |
4343 | } | |
4344 | ||
4345 | ulen = unum_toPattern(unum, FALSE, ubuf, kUBufMax, &status); | |
4346 | if ( U_FAILURE(status) ) { | |
4347 | log_err("unum_toPattern fails with %s\n", u_errorName(status)); | |
4348 | } else if (ulen < 3 || u_strstr(ubuf, u"#.#")==NULL) { | |
4349 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4350 | log_err("after setting minInt=minFrac=0, expect pattern to contain \"#.#\", but get (%d): \"%s\"\n", ulen, bbuf); | |
4351 | } | |
4352 | ||
4353 | status = U_ZERO_ERROR; | |
4354 | ulen = unum_formatDouble(unum, 10.0, ubuf, kUBufMax, NULL, &status); | |
4355 | if ( U_FAILURE(status) ) { | |
4356 | log_err("unum_formatDouble 10.0 ulen %d fails with %s\n", ulen, u_errorName(status)); | |
4357 | } else if (u_strcmp(ubuf, u"10") != 0) { | |
4358 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4359 | log_err("unum_formatDouble 10.0 expected \"10\", got \"%s\"\n", bbuf); | |
4360 | } | |
4361 | ||
4362 | status = U_ZERO_ERROR; | |
4363 | ulen = unum_formatDouble(unum, 0.9, ubuf, kUBufMax, NULL, &status); | |
4364 | if ( U_FAILURE(status) ) { | |
4365 | log_err("unum_formatDouble 0.9 ulen %d fails with %s\n", ulen, u_errorName(status)); | |
4366 | } else if (u_strcmp(ubuf, u".9") != 0) { | |
4367 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4368 | log_err("unum_formatDouble 0.9 expected \".9\", got \"%s\"\n", bbuf); | |
4369 | } | |
4370 | ||
4371 | status = U_ZERO_ERROR; | |
4372 | ulen = unum_formatDouble(unum, 0.0, ubuf, kUBufMax, NULL, &status); | |
4373 | if ( U_FAILURE(status) ) { | |
4374 | log_err("unum_formatDouble 0.0 ulen %d fails with %s\n", ulen, u_errorName(status)); | |
4375 | } else if (u_strcmp(ubuf, u"0") != 0) { | |
4376 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4377 | log_err("unum_formatDouble 0.0 expected \"0\", got \"%s\"\n", bbuf); | |
4378 | } | |
4379 | ||
4380 | unum_close(unum); | |
4381 | } | |
4382 | ||
4383 | status = U_ZERO_ERROR; | |
4384 | unum = unum_open(UNUM_CURRENCY, NULL, 0, "en_US", NULL, &status); | |
4385 | if ( U_FAILURE(status) ) { | |
4386 | log_data_err("unum_open UNUM_CURRENCY for en_US fails with %s\n", u_errorName(status)); | |
4387 | } else { | |
4388 | unum_setAttribute(unum, UNUM_MIN_INTEGER_DIGITS, 0); | |
4389 | unum_setAttribute(unum, UNUM_MIN_FRACTION_DIGITS, 0); | |
4390 | minInt = unum_getAttribute(unum, UNUM_MIN_INTEGER_DIGITS); | |
4391 | minFrac = unum_getAttribute(unum, UNUM_MIN_FRACTION_DIGITS); | |
4392 | if (minInt != 0 || minFrac != 0) { | |
4393 | log_err("after setting CURRENCY minInt=minFrac=0, get minInt %d, minFrac %d\n", minInt, minFrac); | |
4394 | } | |
4395 | ||
4396 | status = U_ZERO_ERROR; | |
4397 | ulen = unum_formatDouble(unum, 10.0, ubuf, kUBufMax, NULL, &status); | |
4398 | if ( U_FAILURE(status) ) { | |
4399 | log_err("unum_formatDouble (CURRRENCY) 10.0 ulen %d fails with %s\n", ulen, u_errorName(status)); | |
4400 | } else if (u_strcmp(ubuf, u"$10") != 0) { | |
4401 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4402 | log_err("unum_formatDouble (CURRRENCY) 10.0 expected \"$10\", got \"%s\"\n", bbuf); | |
4403 | } | |
4404 | ||
4405 | status = U_ZERO_ERROR; | |
4406 | ulen = unum_formatDouble(unum, 0.9, ubuf, kUBufMax, NULL, &status); | |
4407 | if ( U_FAILURE(status) ) { | |
4408 | log_err("unum_formatDouble (CURRRENCY) 0.9 ulen %d fails with %s\n", ulen, u_errorName(status)); | |
4409 | } else if (u_strcmp(ubuf, u"$.9") != 0) { | |
4410 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4411 | log_err("unum_formatDouble (CURRRENCY) 0.9 expected \"$.9\", got \"%s\"\n", bbuf); | |
4412 | } | |
4413 | ||
4414 | status = U_ZERO_ERROR; | |
4415 | ulen = unum_formatDouble(unum, 0.0, ubuf, kUBufMax, NULL, &status); | |
4416 | if ( U_FAILURE(status) ) { | |
4417 | log_err("unum_formatDouble (CURRRENCY) 0.0 ulen %d fails with %s\n", ulen, u_errorName(status)); | |
4418 | } else if (u_strcmp(ubuf, u"$0") != 0) { | |
4419 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4420 | log_err("unum_formatDouble (CURRRENCY) 0.0 expected \"$0\", got \"%s\"\n", bbuf); | |
4421 | } | |
4422 | ||
4423 | unum_close(unum); | |
4424 | } | |
4425 | ||
4426 | // addition for rdar://57291456 | |
4427 | status = U_ZERO_ERROR; | |
4428 | unum = unum_open(UNUM_PATTERN_DECIMAL, NULL, 0, "en_IE", NULL, &status); | |
4429 | if ( U_FAILURE(status) ) { | |
4430 | log_data_err("unum_open UNUM_DECIMAL for en_US fails with %s\n", u_errorName(status)); | |
4431 | } else { | |
4432 | unum_setAttribute(unum, UNUM_MIN_INTEGER_DIGITS, 0); | |
4433 | unum_setAttribute(unum, UNUM_MAX_FRACTION_DIGITS, 1); | |
4434 | minInt = unum_getAttribute(unum, UNUM_MIN_INTEGER_DIGITS); | |
4435 | maxFrac = unum_getAttribute(unum, UNUM_MAX_FRACTION_DIGITS); | |
4436 | if (minInt != 0 || maxFrac != 1) { | |
4437 | log_err("after setting minInt=0, maxFrac=1, get minInt %d, maxFrac %d\n", minInt, maxFrac); | |
4438 | } | |
4439 | ||
4440 | status = U_ZERO_ERROR; | |
4441 | ulen = unum_formatDouble(unum, 0.0, ubuf, kUBufMax, NULL, &status); | |
4442 | if ( U_FAILURE(status) ) { | |
4443 | log_err("unum_formatDouble (maxFrac 1) 0.0 ulen %d fails with %s\n", ulen, u_errorName(status)); | |
4444 | } else if (u_strcmp(ubuf, u".0") != 0) { | |
4445 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4446 | log_err("unum_formatDouble (maxFrac 1) 0.0 expected \".0\", got \"%s\"\n", bbuf); | |
4447 | } | |
4448 | ||
4449 | unum_close(unum); | |
4450 | } | |
4451 | } | |
4452 | ||
4453 | #if APPLE_ADDITIONS | |
4454 | #include <stdio.h> | |
4455 | #if U_PLATFORM_IS_DARWIN_BASED | |
4456 | #include <unistd.h> | |
4457 | #include <mach/mach_time.h> | |
4458 | #define GET_START() mach_absolute_time() | |
4459 | #define GET_DURATION(start, info) ((mach_absolute_time() - start) * info.numer)/info.denom | |
4460 | #elif !U_PLATFORM_HAS_WIN32_API | |
4461 | #include <unistd.h> | |
4462 | #include "putilimp.h" | |
4463 | #define GET_START() (uint64_t)uprv_getUTCtime() | |
4464 | #define GET_DURATION(start, info) ((uint64_t)uprv_getUTCtime() - start) | |
4465 | #else | |
4466 | #include "putilimp.h" | |
4467 | #define GET_START() (unsigned long long)uprv_getUTCtime() | |
4468 | #define GET_DURATION(start, info) ((unsigned long long)uprv_getUTCtime() - start) | |
4469 | #endif | |
4470 | ||
4471 | // Apple only for <rdar://problem/51672521> | |
4472 | ||
4473 | static void TestFormatDecPerf(void) { | |
4474 | static const char* locales[] = | |
4475 | { "en_US", "ar_EG", "ar_EG@numbers=latn", "zh_CN@numbers=traditional", NULL }; | |
4476 | static const UNumberFormatStyle styles[] = | |
4477 | { UNUM_DECIMAL, UNUM_CURRENCY, UNUM_PATTERN_DECIMAL, UNUM_FORMAT_STYLE_COUNT }; | |
4478 | static const char* values[] = | |
4479 | { "123.4", "234.5", "345.6", "456.7", NULL }; | |
4480 | static const UChar* pattern = u"#"; | |
4481 | #if U_PLATFORM_IS_DARWIN_BASED | |
4482 | mach_timebase_info_data_t info; | |
4483 | mach_timebase_info(&info); | |
4484 | #endif | |
4485 | const char** localesPtr = locales; | |
4486 | const char* locale; | |
4487 | while ((locale = *localesPtr++) != NULL) { | |
4488 | const UNumberFormatStyle* stylesPtr = styles; | |
4489 | UNumberFormatStyle style; | |
4490 | while ((style = *stylesPtr++) < UNUM_FORMAT_STYLE_COUNT) { | |
4491 | #if !U_PLATFORM_HAS_WIN32_API | |
4492 | uint64_t start, duration; | |
4493 | #else | |
4494 | unsigned long long start, duration; | |
4495 | #endif | |
4496 | UErrorCode status = U_ZERO_ERROR; | |
4497 | const UChar* patternPtr = (style == UNUM_PATTERN_DECIMAL)? pattern: NULL; | |
4498 | int32_t patternLen = (style == UNUM_PATTERN_DECIMAL)? u_strlen(pattern): 0; | |
4499 | UNumberFormat *unum; | |
4500 | ||
4501 | start = GET_START(); | |
4502 | unum = unum_open(style, patternPtr, patternLen, locale, NULL, &status); | |
4503 | duration = GET_DURATION(start, info); | |
4504 | log_info("== start locale %s, style %d\n", locale, (int)style); | |
4505 | if ( U_FAILURE(status) ) { | |
4506 | log_data_err("unum_open fails with %s\n", u_errorName(status)); | |
4507 | } else { | |
4508 | log_info("unum_open nsec %5llu\n", duration); | |
4509 | const char** valuesPtr = values; | |
4510 | const char* value; | |
4511 | while ((value = *valuesPtr++) != NULL) { | |
4512 | UChar ubuf[kUBufMax]; | |
4513 | char bbuf[kBBufMax]; | |
4514 | int32_t ulen; | |
4515 | ||
4516 | status = U_ZERO_ERROR; | |
4517 | start = GET_START(); | |
4518 | ulen = unum_formatDecimal(unum, value, strlen(value), ubuf, kUBufMax, NULL, &status); | |
4519 | duration = GET_DURATION(start, info); | |
4520 | u_strToUTF8(bbuf, kBBufMax, NULL, ubuf, ulen, &status); | |
4521 | if ( U_FAILURE(status) ) { | |
4522 | log_err("unum_formatDecimal %s fails with %s\n", value, u_errorName(status)); | |
4523 | } else { | |
4524 | log_info("unum_formatDecimal %s, nsec %5llu, result %s\n", value, duration, bbuf); | |
4525 | } | |
4526 | } | |
4527 | unum_close(unum); | |
4528 | } | |
4529 | } | |
4530 | } | |
4531 | } | |
4532 | ||
4533 | // Apple only for <rdar://problem/54886964> and <rdar://problem/62544359> | |
4534 | ||
4535 | static void TestCountryFallback(void) { | |
4536 | // Locales to check fallback behavior for-- the first column (subscript % 3 == 0) | |
4537 | // is the locale to test with; the second column (subscript % 3 == 1) is the locale | |
4538 | // whose number-formatting properties we should be matching when we use the test locale, | |
4539 | // and the third column (subscript % 3 == 2) is the locale we should be inheriting | |
4540 | // certain language-dependent symbols from (generally, the locale you'd normally get | |
4541 | // as a fallback). | |
4542 | // NOTE: The currency format always follows the country locale, except for the Euro in a handful | |
4543 | // of countries, where it follows the US English currency format. If the country-fallback locale | |
4544 | // ID begins with a *, that's a signal that the currency format comes from the language locale | |
4545 | // instead of the country locale. | |
4546 | char* testLocales[] = { | |
4547 | // locales we still have synthetic resources for that were working okay to begin with | |
4548 | "en_BG", "bg_BG", "en_150", | |
4549 | "en_CN", "zh_CN", "en_001", | |
4550 | "en_CZ", "cs_CZ", "en_CZ", | |
4551 | "en_JP", "ja_JP", "en_001", | |
4552 | "en_KR", "ko_KR", "en_001", | |
4553 | "en_TH", "th_TH", "en_001", | |
4554 | "en_TW", "zh_TW", "en_001", | |
4555 | ||
4556 | // locales that were okay except for currency-pattern issues | |
4557 | // (note that we have to specify the language locale as en, not en_150-- the en_150 currency format is wrong too) | |
4558 | "en_EE", "*et_EE", "en", | |
4559 | "en_LT", "*lt_LT", "en_LT", | |
4560 | "en_LV", "*lv_LV", "en", | |
4561 | "en_PT", "*pt_PT", "en", | |
4562 | "en_SK", "*sk_SK", "en_SK", | |
4563 | "en_FR", "*fr_FR", "en_FR", | |
4564 | ||
4565 | // locales we still have synthetic resources for that we had to modify to match the country-fallback results | |
4566 | "en_AL", "sq_AL", "en_150", | |
4567 | "en_AR", "es_AR", "en_AR", | |
4568 | "en_BD", "bn_BD@numbers=latn", "en_BD", | |
4569 | "en_BR", "pt_BR", "en_001", | |
4570 | "en_CL", "es_CL", "en_CL", | |
4571 | "en_CO", "es_CO", "en_CO", | |
4572 | "en_GR", "el_GR", "en_150", | |
4573 | "en_HU", "hu_HU", "en_150", | |
4574 | "en_ID", "id_ID", "en_001", | |
4575 | "en_MM", "my_MM@numbers=latn", "en_001", | |
4576 | "en_MV", "dv_MV", "en_001", | |
4577 | "en_MX", "es_MX", "en_001", | |
4578 | "en_RU", "ru_RU", "en_RU", | |
4579 | "en_TR", "tr_TR", "en_TR", | |
4580 | "en_UA", "uk_UA", "en_150", | |
4581 | "es_AG", "en_AG", "es_AG", | |
4582 | "es_BB", "en_BB", "es_BB", | |
4583 | "es_BM", "en_BM", "es_BM", | |
4584 | "es_BQ", "nl_BQ", "es_BQ", | |
4585 | "es_BS", "en_BS", "es_BS", | |
4586 | "es_CA", "en_CA", "es_CA", | |
4587 | "es_CW", "nl_CW", "es_CW", | |
4588 | "es_DM", "en_DM", "es_DM", | |
4589 | "es_GD", "en_GD", "es_GD", | |
4590 | "es_GY", "en_GY", "es_GY", | |
4591 | "es_HT", "fr_HT", "es_HT", | |
4592 | "es_KN", "en_KN", "es_KN", | |
4593 | "es_KY", "en_KY", "es_KY", | |
4594 | "es_LC", "en_LC", "es_LC", | |
4595 | "es_TC", "en_TC", "es_TC", | |
4596 | "es_TT", "en_TT", "es_TT", | |
4597 | "es_VC", "en_VC", "es_VC", | |
4598 | "es_VG", "en_VG", "es_VG", | |
4599 | "es_VI", "en_VI", "es_VI", | |
4600 | ||
4601 | // locales we used to have synthetic resources for but don't anymore | |
4602 | "en_AD", "ca_AD", "en_150", | |
4603 | "en_BA", "bs_BA", "en_150", | |
4604 | "en_ES", "es_ES", "en_150", | |
4605 | "en_HR", "hr_HR", "en_150", | |
4606 | "en_IS", "is_IS", "en_150", | |
4607 | "en_IT", "it_IT", "en_150", | |
4608 | "en_LU", "fr_LU", "en_150", | |
4609 | "en_ME", "sr_ME", "en_150", | |
4610 | "en_NO", "nb_NO", "en_NO", | |
4611 | "en_PL", "pl_PL", "en_150", | |
4612 | "en_RO", "ro_RO", "en_150", | |
4613 | "en_RS", "sr_RS", "en_150", | |
4614 | "en_SA", "ar_SA@numbers=latn", "en", | |
4615 | "es_AI", "en_AI", "es_419", | |
4616 | "es_AW", "nl_AW", "es_419", | |
4617 | "es_BL", "fr_BL", "es_419", | |
4618 | "es_FK", "en_FK", "es_419", | |
4619 | "es_GF", "fr_GF", "es_419", | |
4620 | "es_GL", "kl_GL", "es", | |
4621 | "es_GP", "fr_GP", "es_419", | |
4622 | "es_MF", "fr_MF", "es_419", | |
4623 | "es_MQ", "fr_MQ", "es_419", | |
4624 | "es_MS", "en_MS", "es_419", | |
4625 | "es_PM", "fr_PM", "es_419", | |
4626 | "es_SR", "nl_SR", "es_419", | |
4627 | "es_SX", "en_SX", "es_419", | |
4628 | ||
4629 | // locales we have synthetic resources for, but they come from open-source ICU | |
4630 | "en_DE", "de_DE", "en_DE", | |
4631 | "en_ER", "ti_ER", "en_001", | |
4632 | "en_GH", "ak_GH", "en_001", | |
4633 | "en_HK", "zh_HK", "en_001", | |
4634 | "en_ME", "sr_ME", "en_150", | |
4635 | "en_MO", "zh_MO", "en_001", | |
4636 | "en_MT", "mt_MT", "en_150", | |
4637 | "en_MY", "ms_MY", "en_001", | |
4638 | "en_NL", "nl_NL", "en_150", | |
4639 | "en_PH", "fil_PH", "en_001", | |
4640 | "en_ZW", "sn_ZW", "en_001", | |
4641 | "es_US", "en_US", "es_US", | |
4642 | ||
4643 | // locales where we're adding a NEW synthetic resource bundle, to PREVENT us from falling back by country | |
4644 | "en_BN", "en_001", "en_001", // rdar://69272236 | |
4645 | ||
4646 | // locales specifically mentioned in Radars | |
4647 | "fr_US", "en_US", "fr", // rdar://problem/54886964 | |
4648 | // "en_TH", "th_TH", "en", // rdar://problem/29299919 (listed above) | |
4649 | // "en_BG", "bg_BG", "en", // rdar://problem/29299919 (listed above) | |
4650 | "en_LI", "de_LI", "en", // rdar://problem/29299919 | |
4651 | "en_MC", "fr_MC", "en", // rdar://problem/29299919 | |
4652 | "en_MD", "ro_MD", "en", // rdar://problem/29299919 | |
4653 | "en_VA", "it_VA", "en", // rdar://problem/29299919 | |
4654 | "fr_GB", "en_GB", "fr", // rdar://problem/36020946 | |
4655 | "fr_CN", "zh_CN", "fr", // rdar://problem/50083902 | |
4656 | "es_IE", "en_IE", "es", // rdar://problem/58733843 | |
4657 | "en_LB", "ar_LB@numbers=latn", "en_001", // rdar://66609948 | |
4658 | ||
4659 | // tests for situations where the default number system is different depending on whether you | |
4660 | // fall back by language or by country: | |
4661 | // "en_SA", "ar_SA@numbers=latn", "en", // (listed above) | |
4662 | "ar_US", "en_US@numbers=arab", "ar", | |
4663 | "en_SA@numbers=arab", "ar_SA", "en@numbers=arab", | |
4664 | "ar_US@numbers=latn", "en_US", "ar@numbers=latn", | |
4665 | ||
4666 | // tests for situations where the original locale ID specifies a script: | |
4667 | "sr_Cyrl_SA", "ar_SA@numbers=latn", "sr_Cyrl", | |
4668 | "ru_Cyrl_BA", "bs_Cyrl_BA", "ru", | |
4669 | ||
4670 | // a few additional arbitrary combinations: | |
4671 | "ja_US", "en_US", "ja", | |
4672 | "fr_DE", "de_DE", "fr", | |
4673 | "de_FR", "fr_FR", "de", | |
4674 | "es_TW", "zh_TW", "es", | |
4675 | ||
4676 | // not in the resources, but to exercise the new ures_openWithCountryFallback() logic (AQ's default language is "und") | |
4677 | "fr_AQ", "en", "fr", | |
4678 | ||
4679 | // test to make sure that nothing goes wrong if language and country fallback both lead to the same resource | |
4680 | // (This won't happen for any "real" locales, because ICU has resources for all of them, but we can fake it with | |
4681 | // a nonexistent country code such as QQ.) | |
4682 | "en_QQ", "en", "en" | |
4683 | }; | |
4684 | ||
4685 | for (int32_t i = 0; i < (sizeof(testLocales) / sizeof(char*)); i += 3) { | |
4686 | const char* testLocale = testLocales[i]; | |
4687 | const char* fallbackLocale = testLocales[i + 1]; | |
4688 | const char* languageLocale = testLocales[i + 2]; | |
4689 | char errorMessage[200]; | |
4690 | UBool currencyFollowsLanguage = FALSE; | |
4691 | UErrorCode err = U_ZERO_ERROR; | |
4692 | ||
4693 | if (fallbackLocale[0] == '*') { | |
4694 | currencyFollowsLanguage = TRUE; | |
4695 | ++fallbackLocale; | |
4696 | } | |
4697 | ||
4698 | // Check that the decimal, percentage, currency, and scientific formatting patterns | |
4699 | // for the test locale are the same as those for the fallback locale. | |
4700 | for (UNumberFormatStyle style = UNUM_DECIMAL; style <= UNUM_SCIENTIFIC; ++style) { | |
4701 | err = U_ZERO_ERROR; | |
4702 | UNumberFormat* testFormatter = unum_open(style, NULL, -1, testLocale, NULL, &err); | |
4703 | UNumberFormat* fallbackFormatter = unum_open(style, NULL, -1, fallbackLocale, NULL, &err); | |
4704 | UNumberFormat* languageLocaleFormatter = unum_open(style, NULL, -1, languageLocale, NULL, &err); | |
4705 | ||
4706 | sprintf(errorMessage, "Error creating formatters for %s and %s", testLocale, fallbackLocale); | |
4707 | if (assertSuccess(errorMessage, &err)) { | |
4708 | UChar testPattern[100]; | |
4709 | UChar fallbackPattern[100]; | |
4710 | ||
4711 | unum_toPattern(testFormatter, FALSE, testPattern, 100, &err); | |
4712 | if (style == UNUM_PERCENT || (style == UNUM_CURRENCY && currencyFollowsLanguage)) { | |
4713 | unum_toPattern(languageLocaleFormatter, FALSE, fallbackPattern, 100, &err); | |
4714 | } else { | |
4715 | unum_toPattern(fallbackFormatter, FALSE, fallbackPattern, 100, &err); | |
4716 | } | |
4717 | ||
4718 | if (assertSuccess("Error getting number format patterns", &err)) { | |
4719 | sprintf(errorMessage, "In %s, formatting pattern for style %d doesn't match", testLocale, style); | |
4720 | assertUEquals(errorMessage, fallbackPattern, testPattern); | |
4721 | } | |
4722 | } | |
4723 | unum_close(testFormatter); | |
4724 | unum_close(fallbackFormatter); | |
4725 | unum_close(languageLocaleFormatter); | |
4726 | } | |
4727 | ||
4728 | // Check that all of the number formatting symbols for the test locale (except for the | |
4729 | // currency, exponential, infinity, and NaN symbols) are the same as those for the fallback locale. | |
4730 | UNumberFormat* testFormatter = unum_open(UNUM_DECIMAL, NULL, -1, testLocale, NULL, &err); | |
4731 | UNumberFormat* fallbackFormatter = unum_open(UNUM_DECIMAL, NULL, -1, fallbackLocale, NULL, &err); | |
4732 | UNumberFormat* languageLocaleFormatter = unum_open(UNUM_DECIMAL, NULL, -1, languageLocale, NULL, &err); | |
4733 | if (assertSuccess("Error creating formatters", &err)) { | |
4734 | for (UNumberFormatSymbol symbol = UNUM_DECIMAL_SEPARATOR_SYMBOL; symbol < UNUM_FORMAT_SYMBOL_COUNT; ++symbol) { | |
4735 | UBool compareToFallbackLocale = TRUE; | |
4736 | switch (symbol) { | |
4737 | case UNUM_CURRENCY_SYMBOL: | |
4738 | case UNUM_INTL_CURRENCY_SYMBOL: | |
4739 | case UNUM_MONETARY_SEPARATOR_SYMBOL: | |
4740 | case UNUM_MONETARY_GROUPING_SEPARATOR_SYMBOL: | |
4741 | // we don't do anything special with currency here | |
4742 | continue; | |
4743 | case UNUM_PLUS_SIGN_SYMBOL: | |
4744 | case UNUM_MINUS_SIGN_SYMBOL: | |
4745 | case UNUM_PERCENT_SYMBOL: | |
4746 | case UNUM_EXPONENTIAL_SYMBOL: | |
4747 | case UNUM_INFINITY_SYMBOL: | |
4748 | case UNUM_NAN_SYMBOL: | |
4749 | // unlike most number symbols, these should follow the language | |
4750 | compareToFallbackLocale = FALSE; | |
4751 | break; | |
4752 | default: | |
4753 | // everything else follows the country | |
4754 | compareToFallbackLocale = TRUE; | |
4755 | break; | |
4756 | } | |
4757 | ||
4758 | UChar testSymbol[50]; | |
4759 | UChar fallbackSymbol[50]; | |
4760 | ||
4761 | err = U_ZERO_ERROR; | |
4762 | unum_getSymbol(testFormatter, symbol, testSymbol, 50, &err); | |
4763 | if (compareToFallbackLocale) { | |
4764 | unum_getSymbol(fallbackFormatter, symbol, fallbackSymbol, 50, &err); | |
4765 | } else { | |
4766 | unum_getSymbol(languageLocaleFormatter, symbol, fallbackSymbol, 50, &err); | |
4767 | } | |
4768 | ||
4769 | if (assertSuccess("Error getting number format symbol", &err)) { | |
4770 | sprintf(errorMessage, "In %s, formatting symbol #%d doesn't match the fallback", testLocale, symbol); | |
4771 | assertUEquals(errorMessage, fallbackSymbol, testSymbol); | |
4772 | } | |
4773 | } | |
4774 | } | |
4775 | unum_close(testFormatter); | |
4776 | unum_close(fallbackFormatter); | |
4777 | unum_close(languageLocaleFormatter); | |
4778 | } | |
4779 | } | |
4780 | ||
4781 | #endif /* APPLE_ADDITIONS */ | |
4782 | ||
4783 | #endif /* #if !UCONFIG_NO_FORMATTING */ |