]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
2ca993e8 A |
3 | /* |
4 | ******************************************************************************* | |
5 | * Copyright (C) 2015, International Business Machines Corporation and * | |
6 | * others. All Rights Reserved. * | |
7 | ******************************************************************************* | |
8 | * | |
9 | * File NUMBERFORMAT2TEST.CPP | |
10 | * | |
11 | ******************************************************************************* | |
12 | */ | |
13 | #include "unicode/utypes.h" | |
14 | ||
15 | #include "intltest.h" | |
16 | ||
17 | #if !UCONFIG_NO_FORMATTING | |
18 | ||
19 | #include "unicode/localpointer.h" | |
20 | #include "unicode/plurrule.h" | |
21 | ||
22 | #include "affixpatternparser.h" | |
23 | #include "charstr.h" | |
24 | #include "datadrivennumberformattestsuite.h" | |
25 | #include "decimalformatpattern.h" | |
26 | #include "digitaffixesandpadding.h" | |
27 | #include "digitformatter.h" | |
28 | #include "digitgrouping.h" | |
29 | #include "digitinterval.h" | |
30 | #include "digitlst.h" | |
31 | #include "fphdlimp.h" | |
32 | #include "plurrule_impl.h" | |
33 | #include "precision.h" | |
34 | #include "significantdigitinterval.h" | |
35 | #include "smallintformatter.h" | |
36 | #include "uassert.h" | |
37 | #include "valueformatter.h" | |
38 | #include "visibledigits.h" | |
39 | ||
40 | struct NumberFormat2Test_Attributes { | |
41 | int32_t id; | |
42 | int32_t spos; | |
43 | int32_t epos; | |
44 | }; | |
45 | ||
46 | class NumberFormat2Test_FieldPositionHandler : public FieldPositionHandler { | |
47 | public: | |
48 | NumberFormat2Test_Attributes attributes[100]; | |
49 | int32_t count; | |
50 | UBool bRecording; | |
51 | ||
52 | ||
53 | ||
54 | NumberFormat2Test_FieldPositionHandler() : count(0), bRecording(TRUE) { attributes[0].spos = -1; } | |
55 | NumberFormat2Test_FieldPositionHandler(UBool recording) : count(0), bRecording(recording) { attributes[0].spos = -1; } | |
56 | virtual ~NumberFormat2Test_FieldPositionHandler(); | |
57 | virtual void addAttribute(int32_t id, int32_t start, int32_t limit); | |
58 | virtual void shiftLast(int32_t delta); | |
59 | virtual UBool isRecording(void) const; | |
60 | }; | |
61 | ||
62 | NumberFormat2Test_FieldPositionHandler::~NumberFormat2Test_FieldPositionHandler() { | |
63 | } | |
64 | ||
65 | void NumberFormat2Test_FieldPositionHandler::addAttribute( | |
66 | int32_t id, int32_t start, int32_t limit) { | |
67 | if (count == UPRV_LENGTHOF(attributes) - 1) { | |
68 | return; | |
69 | } | |
70 | attributes[count].id = id; | |
71 | attributes[count].spos = start; | |
72 | attributes[count].epos = limit; | |
73 | ++count; | |
74 | attributes[count].spos = -1; | |
75 | } | |
76 | ||
77 | void NumberFormat2Test_FieldPositionHandler::shiftLast(int32_t /* delta */) { | |
78 | } | |
79 | ||
80 | UBool NumberFormat2Test_FieldPositionHandler::isRecording() const { | |
81 | return bRecording; | |
82 | } | |
83 | ||
84 | ||
85 | class NumberFormat2Test : public IntlTest { | |
86 | public: | |
87 | void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0); | |
88 | private: | |
89 | void TestQuantize(); | |
90 | void TestConvertScientificNotation(); | |
91 | void TestLowerUpperExponent(); | |
92 | void TestRounding(); | |
93 | void TestRoundingIncrement(); | |
94 | void TestDigitInterval(); | |
95 | void TestGroupingUsed(); | |
96 | void TestBenchmark(); | |
97 | void TestBenchmark2(); | |
98 | void TestSmallIntFormatter(); | |
99 | void TestPositiveIntDigitFormatter(); | |
100 | void TestDigitListInterval(); | |
101 | void TestLargeIntValue(); | |
102 | void TestIntInitVisibleDigits(); | |
103 | void TestIntInitVisibleDigitsToDigitList(); | |
104 | void TestDoubleInitVisibleDigits(); | |
105 | void TestDoubleInitVisibleDigitsToDigitList(); | |
106 | void TestDigitListInitVisibleDigits(); | |
107 | void TestSpecialInitVisibleDigits(); | |
108 | void TestVisibleDigitsWithExponent(); | |
109 | void TestDigitAffixesAndPadding(); | |
110 | void TestPluralsAndRounding(); | |
111 | void TestPluralsAndRoundingScientific(); | |
112 | void TestValueFormatterIsFastFormattable(); | |
113 | void TestCurrencyAffixInfo(); | |
114 | void TestAffixPattern(); | |
115 | void TestAffixPatternAppend(); | |
116 | void TestAffixPatternAppendAjoiningLiterals(); | |
117 | void TestAffixPatternDoubleQuote(); | |
118 | void TestAffixPatternParser(); | |
119 | void TestPluralAffix(); | |
120 | void TestDigitAffix(); | |
121 | void TestDigitFormatterDefaultCtor(); | |
122 | void TestDigitFormatterMonetary(); | |
123 | void TestDigitFormatter(); | |
124 | void TestSciFormatterDefaultCtor(); | |
125 | void TestSciFormatter(); | |
126 | void TestToPatternScientific11648(); | |
127 | void verifyInterval(const DigitInterval &, int32_t minInclusive, int32_t maxExclusive); | |
128 | void verifyAffix( | |
129 | const UnicodeString &expected, | |
130 | const DigitAffix &affix, | |
131 | const NumberFormat2Test_Attributes *expectedAttributes); | |
132 | void verifyAffixesAndPadding( | |
133 | const UnicodeString &expected, | |
134 | const DigitAffixesAndPadding &aaf, | |
135 | DigitList &digits, | |
136 | const ValueFormatter &vf, | |
137 | const PluralRules *optPluralRules, | |
138 | const NumberFormat2Test_Attributes *expectedAttributes); | |
139 | void verifyAffixesAndPaddingInt32( | |
140 | const UnicodeString &expected, | |
141 | const DigitAffixesAndPadding &aaf, | |
142 | int32_t value, | |
143 | const ValueFormatter &vf, | |
144 | const PluralRules *optPluralRules, | |
145 | const NumberFormat2Test_Attributes *expectedAttributes); | |
146 | void verifyDigitList( | |
147 | const UnicodeString &expected, | |
148 | const DigitList &digits); | |
149 | void verifyVisibleDigits( | |
150 | const UnicodeString &expected, | |
151 | UBool bNegative, | |
152 | const VisibleDigits &digits); | |
153 | void verifyVisibleDigitsWithExponent( | |
154 | const UnicodeString &expected, | |
155 | UBool bNegative, | |
156 | const VisibleDigitsWithExponent &digits); | |
157 | void verifyDigitFormatter( | |
158 | const UnicodeString &expected, | |
159 | const DigitFormatter &formatter, | |
160 | const VisibleDigits &digits, | |
161 | const DigitGrouping &grouping, | |
162 | const DigitFormatterOptions &options, | |
163 | const NumberFormat2Test_Attributes *expectedAttributes); | |
164 | void verifySciFormatter( | |
165 | const UnicodeString &expected, | |
166 | const DigitFormatter &formatter, | |
167 | const VisibleDigitsWithExponent &digits, | |
168 | const SciFormatterOptions &options, | |
169 | const NumberFormat2Test_Attributes *expectedAttributes); | |
170 | void verifySmallIntFormatter( | |
171 | const UnicodeString &expected, | |
172 | int32_t positiveValue, | |
173 | int32_t minDigits, | |
174 | int32_t maxDigits); | |
175 | void verifyPositiveIntDigitFormatter( | |
176 | const UnicodeString &expected, | |
177 | const DigitFormatter &formatter, | |
178 | int32_t value, | |
179 | int32_t minDigits, | |
180 | int32_t maxDigits, | |
181 | const NumberFormat2Test_Attributes *expectedAttributes); | |
182 | void verifyAttributes( | |
183 | const NumberFormat2Test_Attributes *expected, | |
184 | const NumberFormat2Test_Attributes *actual); | |
185 | void verifyIntValue( | |
186 | int64_t expected, const VisibleDigits &digits); | |
187 | void verifySource( | |
188 | double expected, const VisibleDigits &digits); | |
189 | }; | |
190 | ||
191 | void NumberFormat2Test::runIndexedTest( | |
192 | int32_t index, UBool exec, const char *&name, char *) { | |
193 | if (exec) { | |
194 | logln("TestSuite ScientificNumberFormatterTest: "); | |
195 | } | |
196 | TESTCASE_AUTO_BEGIN; | |
197 | TESTCASE_AUTO(TestQuantize); | |
198 | TESTCASE_AUTO(TestConvertScientificNotation); | |
199 | TESTCASE_AUTO(TestLowerUpperExponent); | |
200 | TESTCASE_AUTO(TestRounding); | |
201 | TESTCASE_AUTO(TestRoundingIncrement); | |
202 | TESTCASE_AUTO(TestDigitInterval); | |
203 | TESTCASE_AUTO(TestGroupingUsed); | |
204 | TESTCASE_AUTO(TestDigitListInterval); | |
205 | TESTCASE_AUTO(TestDigitFormatterDefaultCtor); | |
206 | TESTCASE_AUTO(TestDigitFormatterMonetary); | |
207 | TESTCASE_AUTO(TestDigitFormatter); | |
208 | TESTCASE_AUTO(TestSciFormatterDefaultCtor); | |
209 | TESTCASE_AUTO(TestSciFormatter); | |
210 | TESTCASE_AUTO(TestBenchmark); | |
211 | TESTCASE_AUTO(TestBenchmark2); | |
212 | TESTCASE_AUTO(TestSmallIntFormatter); | |
213 | TESTCASE_AUTO(TestPositiveIntDigitFormatter); | |
214 | TESTCASE_AUTO(TestCurrencyAffixInfo); | |
215 | TESTCASE_AUTO(TestAffixPattern); | |
216 | TESTCASE_AUTO(TestAffixPatternAppend); | |
217 | TESTCASE_AUTO(TestAffixPatternAppendAjoiningLiterals); | |
218 | TESTCASE_AUTO(TestAffixPatternDoubleQuote); | |
219 | TESTCASE_AUTO(TestAffixPatternParser); | |
220 | TESTCASE_AUTO(TestPluralAffix); | |
221 | TESTCASE_AUTO(TestDigitAffix); | |
222 | TESTCASE_AUTO(TestValueFormatterIsFastFormattable); | |
223 | TESTCASE_AUTO(TestLargeIntValue); | |
224 | TESTCASE_AUTO(TestIntInitVisibleDigits); | |
225 | TESTCASE_AUTO(TestIntInitVisibleDigitsToDigitList); | |
226 | TESTCASE_AUTO(TestDoubleInitVisibleDigits); | |
227 | TESTCASE_AUTO(TestDoubleInitVisibleDigitsToDigitList); | |
228 | TESTCASE_AUTO(TestDigitListInitVisibleDigits); | |
229 | TESTCASE_AUTO(TestSpecialInitVisibleDigits); | |
230 | TESTCASE_AUTO(TestVisibleDigitsWithExponent); | |
231 | TESTCASE_AUTO(TestDigitAffixesAndPadding); | |
232 | TESTCASE_AUTO(TestPluralsAndRounding); | |
233 | TESTCASE_AUTO(TestPluralsAndRoundingScientific); | |
234 | TESTCASE_AUTO(TestToPatternScientific11648); | |
235 | ||
236 | TESTCASE_AUTO_END; | |
237 | } | |
238 | ||
239 | void NumberFormat2Test::TestDigitInterval() { | |
240 | DigitInterval all; | |
241 | DigitInterval threeInts; | |
242 | DigitInterval fourFrac; | |
243 | threeInts.setIntDigitCount(3); | |
244 | fourFrac.setFracDigitCount(4); | |
245 | verifyInterval(all, INT32_MIN, INT32_MAX); | |
246 | verifyInterval(threeInts, INT32_MIN, 3); | |
247 | verifyInterval(fourFrac, -4, INT32_MAX); | |
248 | { | |
249 | DigitInterval result(threeInts); | |
250 | result.shrinkToFitWithin(fourFrac); | |
251 | verifyInterval(result, -4, 3); | |
252 | assertEquals("", 7, result.length()); | |
253 | } | |
254 | { | |
255 | DigitInterval result(threeInts); | |
256 | result.expandToContain(fourFrac); | |
257 | verifyInterval(result, INT32_MIN, INT32_MAX); | |
258 | } | |
259 | { | |
260 | DigitInterval result(threeInts); | |
261 | result.setIntDigitCount(0); | |
262 | verifyInterval(result, INT32_MIN, 0); | |
263 | result.setIntDigitCount(-1); | |
264 | verifyInterval(result, INT32_MIN, INT32_MAX); | |
265 | } | |
266 | { | |
267 | DigitInterval result(fourFrac); | |
268 | result.setFracDigitCount(0); | |
269 | verifyInterval(result, 0, INT32_MAX); | |
270 | result.setFracDigitCount(-1); | |
271 | verifyInterval(result, INT32_MIN, INT32_MAX); | |
272 | } | |
273 | { | |
274 | DigitInterval result; | |
275 | result.setIntDigitCount(3); | |
276 | result.setFracDigitCount(1); | |
277 | result.expandToContainDigit(0); | |
278 | result.expandToContainDigit(-1); | |
279 | result.expandToContainDigit(2); | |
280 | verifyInterval(result, -1, 3); | |
281 | result.expandToContainDigit(3); | |
282 | verifyInterval(result, -1, 4); | |
283 | result.expandToContainDigit(-2); | |
284 | verifyInterval(result, -2, 4); | |
285 | result.expandToContainDigit(15); | |
286 | result.expandToContainDigit(-15); | |
287 | verifyInterval(result, -15, 16); | |
288 | } | |
289 | { | |
290 | DigitInterval result; | |
291 | result.setIntDigitCount(3); | |
292 | result.setFracDigitCount(1); | |
293 | assertTrue("", result.contains(2)); | |
294 | assertTrue("", result.contains(-1)); | |
295 | assertFalse("", result.contains(3)); | |
296 | assertFalse("", result.contains(-2)); | |
297 | } | |
298 | } | |
299 | ||
300 | void NumberFormat2Test::verifyInterval( | |
301 | const DigitInterval &interval, | |
302 | int32_t minInclusive, int32_t maxExclusive) { | |
303 | assertEquals("", minInclusive, interval.getLeastSignificantInclusive()); | |
304 | assertEquals("", maxExclusive, interval.getMostSignificantExclusive()); | |
305 | assertEquals("", maxExclusive, interval.getIntDigitCount()); | |
306 | } | |
307 | ||
308 | void NumberFormat2Test::TestGroupingUsed() { | |
309 | { | |
310 | DigitGrouping grouping; | |
311 | assertFalse("", grouping.isGroupingUsed()); | |
312 | } | |
313 | { | |
314 | DigitGrouping grouping; | |
315 | grouping.fGrouping = 2; | |
316 | assertTrue("", grouping.isGroupingUsed()); | |
317 | } | |
318 | } | |
319 | ||
320 | void NumberFormat2Test::TestDigitListInterval() { | |
321 | DigitInterval result; | |
322 | DigitList digitList; | |
323 | { | |
f3c0d7a5 | 324 | digitList.set((int32_t)12345); |
2ca993e8 A |
325 | verifyInterval(digitList.getSmallestInterval(result), 0, 5); |
326 | } | |
327 | { | |
328 | digitList.set(1000.00); | |
329 | verifyInterval(digitList.getSmallestInterval(result), 0, 4); | |
330 | } | |
331 | { | |
332 | digitList.set(43.125); | |
333 | verifyInterval(digitList.getSmallestInterval(result), -3, 2); | |
334 | } | |
335 | { | |
336 | digitList.set(.0078125); | |
337 | verifyInterval(digitList.getSmallestInterval(result), -7, 0); | |
338 | } | |
339 | { | |
340 | digitList.set(1000.00); | |
341 | digitList.getSmallestInterval(result); | |
342 | result.expandToContainDigit(3); | |
343 | verifyInterval(result, 0, 4); | |
344 | } | |
345 | { | |
346 | digitList.set(1000.00); | |
347 | digitList.getSmallestInterval(result); | |
348 | result.expandToContainDigit(4); | |
349 | verifyInterval(result, 0, 5); | |
350 | } | |
351 | { | |
352 | digitList.set(1000.00); | |
353 | digitList.getSmallestInterval(result); | |
354 | result.expandToContainDigit(0); | |
355 | verifyInterval(result, 0, 4); | |
356 | } | |
357 | { | |
358 | digitList.set(1000.00); | |
359 | digitList.getSmallestInterval(result); | |
360 | result.expandToContainDigit(-1); | |
361 | verifyInterval(result, -1, 4); | |
362 | } | |
363 | { | |
364 | digitList.set(43.125); | |
365 | digitList.getSmallestInterval(result); | |
366 | result.expandToContainDigit(1); | |
367 | verifyInterval(result, -3, 2); | |
368 | } | |
369 | { | |
370 | digitList.set(43.125); | |
371 | digitList.getSmallestInterval(result); | |
372 | result.expandToContainDigit(2); | |
373 | verifyInterval(result, -3, 3); | |
374 | } | |
375 | { | |
376 | digitList.set(43.125); | |
377 | digitList.getSmallestInterval(result); | |
378 | result.expandToContainDigit(-3); | |
379 | verifyInterval(result, -3, 2); | |
380 | } | |
381 | { | |
382 | digitList.set(43.125); | |
383 | digitList.getSmallestInterval(result); | |
384 | result.expandToContainDigit(-4); | |
385 | verifyInterval(result, -4, 2); | |
386 | } | |
387 | } | |
388 | ||
389 | void NumberFormat2Test::TestQuantize() { | |
390 | DigitList quantity; | |
391 | quantity.set(0.00168); | |
392 | quantity.roundAtExponent(-5); | |
393 | DigitList digits; | |
394 | UErrorCode status = U_ZERO_ERROR; | |
395 | { | |
f3c0d7a5 | 396 | digits.set((int32_t)1); |
2ca993e8 A |
397 | digits.quantize(quantity, status); |
398 | verifyDigitList(".9996", digits); | |
399 | } | |
400 | { | |
401 | // round half even up | |
402 | digits.set(1.00044); | |
403 | digits.roundAtExponent(-5); | |
404 | digits.quantize(quantity, status); | |
405 | verifyDigitList("1.00128", digits); | |
406 | } | |
407 | { | |
408 | // round half down | |
409 | digits.set(0.99876); | |
410 | digits.roundAtExponent(-5); | |
411 | digits.quantize(quantity, status); | |
412 | verifyDigitList(".99792", digits); | |
413 | } | |
414 | assertSuccess("", status); | |
415 | } | |
416 | ||
417 | void NumberFormat2Test::TestConvertScientificNotation() { | |
418 | DigitList digits; | |
419 | { | |
f3c0d7a5 | 420 | digits.set((int32_t)186283); |
2ca993e8 A |
421 | assertEquals("", 5, digits.toScientific(1, 1)); |
422 | verifyDigitList( | |
423 | "1.86283", | |
424 | digits); | |
425 | } | |
426 | { | |
f3c0d7a5 | 427 | digits.set((int32_t)186283); |
2ca993e8 A |
428 | assertEquals("", 0, digits.toScientific(6, 1)); |
429 | verifyDigitList( | |
430 | "186283", | |
431 | digits); | |
432 | } | |
433 | { | |
f3c0d7a5 | 434 | digits.set((int32_t)186283); |
2ca993e8 A |
435 | assertEquals("", -2, digits.toScientific(8, 1)); |
436 | verifyDigitList( | |
437 | "18628300", | |
438 | digits); | |
439 | } | |
440 | { | |
f3c0d7a5 | 441 | digits.set((int32_t)43561); |
2ca993e8 A |
442 | assertEquals("", 6, digits.toScientific(-1, 3)); |
443 | verifyDigitList( | |
444 | ".043561", | |
445 | digits); | |
446 | } | |
447 | { | |
f3c0d7a5 | 448 | digits.set((int32_t)43561); |
2ca993e8 A |
449 | assertEquals("", 3, digits.toScientific(0, 3)); |
450 | verifyDigitList( | |
451 | "43.561", | |
452 | digits); | |
453 | } | |
454 | { | |
f3c0d7a5 | 455 | digits.set((int32_t)43561); |
2ca993e8 A |
456 | assertEquals("", 3, digits.toScientific(2, 3)); |
457 | verifyDigitList( | |
458 | "43.561", | |
459 | digits); | |
460 | } | |
461 | { | |
f3c0d7a5 | 462 | digits.set((int32_t)43561); |
2ca993e8 A |
463 | assertEquals("", 0, digits.toScientific(3, 3)); |
464 | verifyDigitList( | |
465 | "43561", | |
466 | digits); | |
467 | } | |
468 | { | |
f3c0d7a5 | 469 | digits.set((int32_t)43561); |
2ca993e8 A |
470 | assertEquals("", 0, digits.toScientific(5, 3)); |
471 | verifyDigitList( | |
472 | "43561", | |
473 | digits); | |
474 | } | |
475 | { | |
f3c0d7a5 | 476 | digits.set((int32_t)43561); |
2ca993e8 A |
477 | assertEquals("", -3, digits.toScientific(6, 3)); |
478 | verifyDigitList( | |
479 | "43561000", | |
480 | digits); | |
481 | } | |
482 | { | |
f3c0d7a5 | 483 | digits.set((int32_t)43561); |
2ca993e8 A |
484 | assertEquals("", -3, digits.toScientific(8, 3)); |
485 | verifyDigitList( | |
486 | "43561000", | |
487 | digits); | |
488 | } | |
489 | { | |
f3c0d7a5 | 490 | digits.set((int32_t)43561); |
2ca993e8 A |
491 | assertEquals("", -6, digits.toScientific(9, 3)); |
492 | verifyDigitList( | |
493 | "43561000000", | |
494 | digits); | |
495 | } | |
496 | } | |
497 | ||
498 | void NumberFormat2Test::TestLowerUpperExponent() { | |
499 | DigitList digits; | |
500 | ||
501 | digits.set(98.7); | |
502 | assertEquals("", -1, digits.getLowerExponent()); | |
503 | assertEquals("", 2, digits.getUpperExponent()); | |
504 | } | |
505 | ||
506 | void NumberFormat2Test::TestRounding() { | |
507 | DigitList digits; | |
508 | uprv_decContextSetRounding(&digits.fContext, DEC_ROUND_CEILING); | |
509 | { | |
510 | // Round at very large exponent | |
511 | digits.set(789.123); | |
512 | digits.roundAtExponent(100); | |
513 | verifyDigitList( | |
514 | "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", // 100 0's after 1 | |
515 | digits); | |
516 | } | |
517 | { | |
518 | // Round at very large exponent | |
519 | digits.set(789.123); | |
520 | digits.roundAtExponent(1); | |
521 | verifyDigitList( | |
522 | "790", // 100 0's after 1 | |
523 | digits); | |
524 | } | |
525 | { | |
526 | // Round at positive exponent | |
527 | digits.set(789.123); | |
528 | digits.roundAtExponent(1); | |
529 | verifyDigitList("790", digits); | |
530 | } | |
531 | { | |
532 | // Round at zero exponent | |
533 | digits.set(788.123); | |
534 | digits.roundAtExponent(0); | |
535 | verifyDigitList("789", digits); | |
536 | } | |
537 | { | |
538 | // Round at negative exponent | |
539 | digits.set(789.123); | |
540 | digits.roundAtExponent(-2); | |
541 | verifyDigitList("789.13", digits); | |
542 | } | |
543 | { | |
544 | // Round to exponent of digits. | |
545 | digits.set(789.123); | |
546 | digits.roundAtExponent(-3); | |
547 | verifyDigitList("789.123", digits); | |
548 | } | |
549 | { | |
550 | // Round at large negative exponent | |
551 | digits.set(789.123); | |
552 | digits.roundAtExponent(-100); | |
553 | verifyDigitList("789.123", digits); | |
554 | } | |
555 | { | |
556 | // Round negative | |
557 | digits.set(-789.123); | |
558 | digits.roundAtExponent(-2); | |
559 | digits.setPositive(TRUE); | |
560 | verifyDigitList("789.12", digits); | |
561 | } | |
562 | { | |
563 | // Round to 1 significant digit | |
564 | digits.set(789.123); | |
565 | digits.roundAtExponent(INT32_MIN, 1); | |
566 | verifyDigitList("800", digits); | |
567 | } | |
568 | { | |
569 | // Round to 5 significant digit | |
570 | digits.set(789.123); | |
571 | digits.roundAtExponent(INT32_MIN, 5); | |
572 | verifyDigitList("789.13", digits); | |
573 | } | |
574 | { | |
575 | // Round to 6 significant digit | |
576 | digits.set(789.123); | |
577 | digits.roundAtExponent(INT32_MIN, 6); | |
578 | verifyDigitList("789.123", digits); | |
579 | } | |
580 | { | |
581 | // no-op | |
582 | digits.set(789.123); | |
583 | digits.roundAtExponent(INT32_MIN, INT32_MAX); | |
584 | verifyDigitList("789.123", digits); | |
585 | } | |
586 | { | |
587 | // Rounding at -1 produces fewer than 5 significant digits | |
588 | digits.set(789.123); | |
589 | digits.roundAtExponent(-1, 5); | |
590 | verifyDigitList("789.2", digits); | |
591 | } | |
592 | { | |
593 | // Rounding at -1 produces exactly 4 significant digits | |
594 | digits.set(789.123); | |
595 | digits.roundAtExponent(-1, 4); | |
596 | verifyDigitList("789.2", digits); | |
597 | } | |
598 | { | |
599 | // Rounding at -1 produces more than 3 significant digits | |
600 | digits.set(788.123); | |
601 | digits.roundAtExponent(-1, 3); | |
602 | verifyDigitList("789", digits); | |
603 | } | |
604 | { | |
605 | digits.set(123.456); | |
606 | digits.round(INT32_MAX); | |
607 | verifyDigitList("123.456", digits); | |
608 | } | |
609 | { | |
610 | digits.set(123.456); | |
611 | digits.round(1); | |
612 | verifyDigitList("200", digits); | |
613 | } | |
614 | } | |
615 | void NumberFormat2Test::TestBenchmark() { | |
616 | /* | |
617 | UErrorCode status = U_ZERO_ERROR; | |
618 | Locale en("en"); | |
619 | DecimalFormatSymbols *sym = new DecimalFormatSymbols(en, status); | |
620 | DecimalFormat2 fmt(en, "0.0000000", status); | |
f3c0d7a5 | 621 | FieldPosition fpos(FieldPostion::DONT_CARE); |
2ca993e8 A |
622 | clock_t start = clock(); |
623 | for (int32_t i = 0; i < 100000; ++i) { | |
624 | UParseError perror; | |
625 | DecimalFormat2 fmt2("0.0000000", new DecimalFormatSymbols(*sym), perror, status); | |
626 | // UnicodeString append; | |
627 | // fmt.format(4.6692016, append, fpos, status); | |
628 | } | |
629 | errln("Took %f", (double) (clock() - start) / CLOCKS_PER_SEC); | |
630 | assertSuccess("", status); | |
631 | */ | |
632 | } | |
633 | ||
634 | void NumberFormat2Test::TestBenchmark2() { | |
635 | /* | |
636 | UErrorCode status = U_ZERO_ERROR; | |
637 | Locale en("en"); | |
638 | DecimalFormatSymbols *sym = new DecimalFormatSymbols(en, status); | |
639 | DecimalFormat fmt("0.0000000", sym, status); | |
f3c0d7a5 | 640 | FieldPosition fpos(FieldPostion::DONT_CARE); |
2ca993e8 A |
641 | clock_t start = clock(); |
642 | for (int32_t i = 0; i < 100000; ++i) { | |
643 | UParseError perror; | |
644 | DecimalFormat fmt("0.0000000", new DecimalFormatSymbols(*sym), perror, status); | |
645 | // UnicodeString append; | |
646 | // fmt.format(4.6692016, append, fpos, status); | |
647 | } | |
648 | errln("Took %f", (double) (clock() - start) / CLOCKS_PER_SEC); | |
649 | assertSuccess("", status); | |
650 | */ | |
651 | } | |
652 | ||
653 | void NumberFormat2Test::TestSmallIntFormatter() { | |
654 | verifySmallIntFormatter("0", 7, 0, -2); | |
655 | verifySmallIntFormatter("7", 7, 1, -2); | |
656 | verifySmallIntFormatter("07", 7, 2, -2); | |
657 | verifySmallIntFormatter("07", 7, 2, 2); | |
658 | verifySmallIntFormatter("007", 7, 3, 4); | |
659 | verifySmallIntFormatter("7", 7, -1, 3); | |
660 | verifySmallIntFormatter("0", 0, -1, 3); | |
661 | verifySmallIntFormatter("057", 57, 3, 7); | |
662 | verifySmallIntFormatter("0057", 57, 4, 7); | |
663 | // too many digits for small int | |
664 | verifySmallIntFormatter("", 57, 5, 7); | |
665 | // too many digits for small int | |
666 | verifySmallIntFormatter("", 57, 5, 4); | |
667 | verifySmallIntFormatter("03", 3, 2, 3); | |
668 | verifySmallIntFormatter("32", 32, 2, 3); | |
669 | verifySmallIntFormatter("321", 321, 2, 3); | |
670 | verifySmallIntFormatter("219", 3219, 2, 3); | |
671 | verifySmallIntFormatter("4095", 4095, 2, 4); | |
672 | verifySmallIntFormatter("4095", 4095, 2, 5); | |
673 | verifySmallIntFormatter("", 4096, 2, 5); | |
674 | } | |
675 | ||
676 | void NumberFormat2Test::TestPositiveIntDigitFormatter() { | |
677 | DigitFormatter formatter; | |
678 | { | |
679 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
680 | {UNUM_INTEGER_FIELD, 0, 4}, | |
681 | {0, -1, 0}}; | |
682 | verifyPositiveIntDigitFormatter( | |
683 | "0057", | |
684 | formatter, | |
685 | 57, | |
686 | 4, | |
687 | INT32_MAX, | |
688 | expectedAttributes); | |
689 | } | |
690 | { | |
691 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
692 | {UNUM_INTEGER_FIELD, 0, 5}, | |
693 | {0, -1, 0}}; | |
694 | verifyPositiveIntDigitFormatter( | |
695 | "00057", | |
696 | formatter, | |
697 | 57, | |
698 | 5, | |
699 | INT32_MAX, | |
700 | expectedAttributes); | |
701 | } | |
702 | { | |
703 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
704 | {UNUM_INTEGER_FIELD, 0, 5}, | |
705 | {0, -1, 0}}; | |
706 | verifyPositiveIntDigitFormatter( | |
707 | "01000", | |
708 | formatter, | |
709 | 1000, | |
710 | 5, | |
711 | INT32_MAX, | |
712 | expectedAttributes); | |
713 | } | |
714 | { | |
715 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
716 | {UNUM_INTEGER_FIELD, 0, 3}, | |
717 | {0, -1, 0}}; | |
718 | verifyPositiveIntDigitFormatter( | |
719 | "100", | |
720 | formatter, | |
721 | 100, | |
722 | 0, | |
723 | INT32_MAX, | |
724 | expectedAttributes); | |
725 | } | |
726 | { | |
727 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
728 | {UNUM_INTEGER_FIELD, 0, 10}, | |
729 | {0, -1, 0}}; | |
730 | verifyPositiveIntDigitFormatter( | |
731 | "2147483647", | |
732 | formatter, | |
733 | 2147483647, | |
734 | 5, | |
735 | INT32_MAX, | |
736 | expectedAttributes); | |
737 | } | |
738 | { | |
739 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
740 | {UNUM_INTEGER_FIELD, 0, 12}, | |
741 | {0, -1, 0}}; | |
742 | verifyPositiveIntDigitFormatter( | |
743 | "002147483647", | |
744 | formatter, | |
745 | 2147483647, | |
746 | 12, | |
747 | INT32_MAX, | |
748 | expectedAttributes); | |
749 | } | |
750 | { | |
751 | // Test long digit string where we have to append one | |
752 | // character at a time. | |
753 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
754 | {UNUM_INTEGER_FIELD, 0, 40}, | |
755 | {0, -1, 0}}; | |
756 | verifyPositiveIntDigitFormatter( | |
757 | "0000000000000000000000000000002147483647", | |
758 | formatter, | |
759 | 2147483647, | |
760 | 40, | |
761 | INT32_MAX, | |
762 | expectedAttributes); | |
763 | } | |
764 | { | |
765 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
766 | {UNUM_INTEGER_FIELD, 0, 4}, | |
767 | {0, -1, 0}}; | |
768 | verifyPositiveIntDigitFormatter( | |
769 | "6283", | |
770 | formatter, | |
771 | 186283, | |
772 | 2, | |
773 | 4, | |
774 | expectedAttributes); | |
775 | } | |
776 | { | |
777 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
778 | {UNUM_INTEGER_FIELD, 0, 1}, | |
779 | {0, -1, 0}}; | |
780 | verifyPositiveIntDigitFormatter( | |
781 | "0", | |
782 | formatter, | |
783 | 186283, | |
784 | 0, | |
785 | 0, | |
786 | expectedAttributes); | |
787 | } | |
788 | { | |
789 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
790 | {UNUM_INTEGER_FIELD, 0, 1}, | |
791 | {0, -1, 0}}; | |
792 | verifyPositiveIntDigitFormatter( | |
793 | "3", | |
794 | formatter, | |
795 | 186283, | |
796 | 1, | |
797 | 1, | |
798 | expectedAttributes); | |
799 | } | |
800 | } | |
801 | ||
802 | ||
803 | void NumberFormat2Test::TestDigitFormatterDefaultCtor() { | |
804 | DigitFormatter formatter; | |
805 | VisibleDigits digits; | |
806 | FixedPrecision precision; | |
807 | UErrorCode status = U_ZERO_ERROR; | |
808 | precision.initVisibleDigits(246.801, digits, status); | |
809 | assertSuccess("", status); | |
810 | DigitGrouping grouping; | |
811 | DigitFormatterOptions options; | |
812 | verifyDigitFormatter( | |
813 | "246.801", | |
814 | formatter, | |
815 | digits, | |
816 | grouping, | |
817 | options, | |
818 | NULL); | |
819 | } | |
820 | ||
821 | void NumberFormat2Test::TestDigitFormatterMonetary() { | |
822 | UErrorCode status = U_ZERO_ERROR; | |
823 | DecimalFormatSymbols symbols("en", status); | |
824 | if (!assertSuccess("", status)) { | |
825 | return; | |
826 | } | |
827 | symbols.setSymbol( | |
828 | DecimalFormatSymbols::kMonetarySeparatorSymbol, | |
829 | "decimal separator"); | |
830 | symbols.setSymbol( | |
831 | DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol, | |
832 | "grouping separator"); | |
833 | DigitFormatter formatter(symbols); | |
834 | VisibleDigits visibleDigits; | |
835 | DigitGrouping grouping; | |
836 | FixedPrecision precision; | |
837 | precision.initVisibleDigits(43560.02, visibleDigits, status); | |
838 | if (!assertSuccess("", status)) { | |
839 | return; | |
840 | } | |
841 | DigitFormatterOptions options; | |
842 | grouping.fGrouping = 3; | |
843 | { | |
844 | verifyDigitFormatter( | |
845 | "43,560.02", | |
846 | formatter, | |
847 | visibleDigits, | |
848 | grouping, | |
849 | options, | |
850 | NULL); | |
851 | formatter.setDecimalFormatSymbolsForMonetary(symbols); | |
852 | verifyDigitFormatter( | |
853 | "43grouping separator560decimal separator02", | |
854 | formatter, | |
855 | visibleDigits, | |
856 | grouping, | |
857 | options, | |
858 | NULL); | |
859 | } | |
860 | } | |
861 | ||
862 | void NumberFormat2Test::TestDigitFormatter() { | |
863 | UErrorCode status = U_ZERO_ERROR; | |
864 | DecimalFormatSymbols symbols("en", status); | |
865 | if (!assertSuccess("", status)) { | |
866 | return; | |
867 | } | |
868 | DigitFormatter formatter(symbols); | |
869 | DigitInterval interval; | |
870 | { | |
871 | VisibleDigits visibleDigits; | |
872 | DigitGrouping grouping; | |
873 | FixedPrecision precision; | |
874 | precision.initVisibleDigits((int64_t) 8192, visibleDigits, status); | |
875 | if (!assertSuccess("", status)) { | |
876 | return; | |
877 | } | |
878 | DigitFormatterOptions options; | |
879 | verifyDigitFormatter( | |
880 | "8192", | |
881 | formatter, | |
882 | visibleDigits, | |
883 | grouping, | |
884 | options, | |
885 | NULL); | |
886 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
887 | {UNUM_INTEGER_FIELD, 0, 4}, | |
888 | {UNUM_DECIMAL_SEPARATOR_FIELD, 4, 5}, | |
889 | {0, -1, 0}}; | |
890 | options.fAlwaysShowDecimal = TRUE; | |
891 | verifyDigitFormatter( | |
892 | "8192.", | |
893 | formatter, | |
894 | visibleDigits, | |
895 | grouping, | |
896 | options, | |
897 | expectedAttributes); | |
898 | ||
899 | // Turn on grouping | |
900 | grouping.fGrouping = 3; | |
901 | options.fAlwaysShowDecimal = FALSE; | |
902 | verifyDigitFormatter( | |
903 | "8,192", | |
904 | formatter, | |
905 | visibleDigits, | |
906 | grouping, | |
907 | options, | |
908 | NULL); | |
909 | ||
910 | // turn on min grouping which will suppress grouping | |
911 | grouping.fMinGrouping = 2; | |
912 | verifyDigitFormatter( | |
913 | "8192", | |
914 | formatter, | |
915 | visibleDigits, | |
916 | grouping, | |
917 | options, | |
918 | NULL); | |
919 | ||
920 | // adding one more digit will enable grouping once again. | |
921 | precision.initVisibleDigits((int64_t) 43560, visibleDigits, status); | |
922 | if (!assertSuccess("", status)) { | |
923 | return; | |
924 | } | |
925 | verifyDigitFormatter( | |
926 | "43,560", | |
927 | formatter, | |
928 | visibleDigits, | |
929 | grouping, | |
930 | options, | |
931 | NULL); | |
932 | } | |
933 | { | |
934 | DigitGrouping grouping; | |
935 | FixedPrecision precision; | |
936 | VisibleDigits visibleDigits; | |
937 | precision.initVisibleDigits( | |
938 | 31415926.0078125, visibleDigits, status); | |
939 | if (!assertSuccess("", status)) { | |
940 | return; | |
941 | } | |
942 | DigitFormatterOptions options; | |
943 | verifyDigitFormatter( | |
944 | "31415926.0078125", | |
945 | formatter, | |
946 | visibleDigits, | |
947 | grouping, | |
948 | options, | |
949 | NULL); | |
950 | ||
951 | // Turn on grouping with secondary. | |
952 | grouping.fGrouping = 2; | |
953 | grouping.fGrouping2 = 3; | |
954 | verifyDigitFormatter( | |
955 | "314,159,26.0078125", | |
956 | formatter, | |
957 | visibleDigits, | |
958 | grouping, | |
959 | options, | |
960 | NULL); | |
961 | ||
962 | // Pad with zeros by widening interval. | |
963 | precision.fMin.setIntDigitCount(9); | |
964 | precision.fMin.setFracDigitCount(10); | |
965 | precision.initVisibleDigits( | |
966 | 31415926.0078125, visibleDigits, status); | |
967 | if (!assertSuccess("", status)) { | |
968 | return; | |
969 | } | |
970 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
971 | {UNUM_GROUPING_SEPARATOR_FIELD, 1, 2}, | |
972 | {UNUM_GROUPING_SEPARATOR_FIELD, 5, 6}, | |
973 | {UNUM_GROUPING_SEPARATOR_FIELD, 9, 10}, | |
974 | {UNUM_INTEGER_FIELD, 0, 12}, | |
975 | {UNUM_DECIMAL_SEPARATOR_FIELD, 12, 13}, | |
976 | {UNUM_FRACTION_FIELD, 13, 23}, | |
977 | {0, -1, 0}}; | |
978 | verifyDigitFormatter( | |
979 | "0,314,159,26.0078125000", | |
980 | formatter, | |
981 | visibleDigits, | |
982 | grouping, | |
983 | options, | |
984 | expectedAttributes); | |
985 | } | |
986 | { | |
987 | DigitGrouping grouping; | |
988 | FixedPrecision precision; | |
989 | VisibleDigits visibleDigits; | |
990 | DigitFormatterOptions options; | |
991 | precision.fMax.setIntDigitCount(0); | |
992 | precision.fMax.setFracDigitCount(0); | |
993 | precision.initVisibleDigits( | |
994 | 3125.0, visibleDigits, status); | |
995 | if (!assertSuccess("", status)) { | |
996 | return; | |
997 | } | |
998 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
999 | {UNUM_INTEGER_FIELD, 0, 1}, | |
1000 | {0, -1, 0}}; | |
1001 | verifyDigitFormatter( | |
1002 | "0", | |
1003 | formatter, | |
1004 | visibleDigits, | |
1005 | grouping, | |
1006 | options, | |
1007 | expectedAttributes); | |
1008 | NumberFormat2Test_Attributes expectedAttributesWithDecimal[] = { | |
1009 | {UNUM_INTEGER_FIELD, 0, 1}, | |
1010 | {UNUM_DECIMAL_SEPARATOR_FIELD, 1, 2}, | |
1011 | {0, -1, 0}}; | |
1012 | options.fAlwaysShowDecimal = TRUE; | |
1013 | verifyDigitFormatter( | |
1014 | "0.", | |
1015 | formatter, | |
1016 | visibleDigits, | |
1017 | grouping, | |
1018 | options, | |
1019 | expectedAttributesWithDecimal); | |
1020 | } | |
1021 | { | |
1022 | DigitGrouping grouping; | |
1023 | FixedPrecision precision; | |
1024 | VisibleDigits visibleDigits; | |
1025 | DigitFormatterOptions options; | |
1026 | precision.fMax.setIntDigitCount(1); | |
1027 | precision.fMin.setFracDigitCount(1); | |
1028 | precision.initVisibleDigits( | |
1029 | 3125.0, visibleDigits, status); | |
1030 | if (!assertSuccess("", status)) { | |
1031 | return; | |
1032 | } | |
1033 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1034 | {UNUM_INTEGER_FIELD, 0, 1}, | |
1035 | {UNUM_DECIMAL_SEPARATOR_FIELD, 1, 2}, | |
1036 | {UNUM_FRACTION_FIELD, 2, 3}, | |
1037 | {0, -1, 0}}; | |
1038 | options.fAlwaysShowDecimal = TRUE; | |
1039 | verifyDigitFormatter( | |
1040 | "5.0", | |
1041 | formatter, | |
1042 | visibleDigits, | |
1043 | grouping, | |
1044 | options, | |
1045 | expectedAttributes); | |
1046 | } | |
1047 | } | |
1048 | ||
1049 | void NumberFormat2Test::TestSciFormatterDefaultCtor() { | |
1050 | DigitFormatter formatter; | |
1051 | ScientificPrecision precision; | |
1052 | VisibleDigitsWithExponent visibleDigits; | |
1053 | UErrorCode status = U_ZERO_ERROR; | |
1054 | precision.initVisibleDigitsWithExponent( | |
1055 | 6.02E23, visibleDigits, status); | |
1056 | if (!assertSuccess("", status)) { | |
1057 | return; | |
1058 | } | |
1059 | SciFormatterOptions options; | |
1060 | verifySciFormatter( | |
1061 | "6.02E23", | |
1062 | formatter, | |
1063 | visibleDigits, | |
1064 | options, | |
1065 | NULL); | |
1066 | precision.initVisibleDigitsWithExponent( | |
1067 | 6.62E-34, visibleDigits, status); | |
1068 | if (!assertSuccess("", status)) { | |
1069 | return; | |
1070 | } | |
1071 | verifySciFormatter( | |
1072 | "6.62E-34", | |
1073 | formatter, | |
1074 | visibleDigits, | |
1075 | options, | |
1076 | NULL); | |
1077 | } | |
1078 | ||
1079 | void NumberFormat2Test::TestSciFormatter() { | |
1080 | DigitFormatter formatter; | |
1081 | ScientificPrecision precision; | |
1082 | precision.fMantissa.fMin.setIntDigitCount(4); | |
1083 | precision.fMantissa.fMax.setIntDigitCount(4); | |
1084 | precision.fMantissa.fMin.setFracDigitCount(0); | |
1085 | precision.fMantissa.fMax.setFracDigitCount(0); | |
1086 | precision.fMinExponentDigits = 3; | |
1087 | VisibleDigitsWithExponent visibleDigits; | |
1088 | UErrorCode status = U_ZERO_ERROR; | |
1089 | precision.initVisibleDigitsWithExponent( | |
1090 | 1.248E26, visibleDigits, status); | |
1091 | if (!assertSuccess("", status)) { | |
1092 | return; | |
1093 | } | |
1094 | SciFormatterOptions options; | |
1095 | ||
1096 | { | |
1097 | options.fExponent.fAlwaysShowSign = TRUE; | |
1098 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1099 | {UNUM_INTEGER_FIELD, 0, 4}, | |
1100 | {UNUM_EXPONENT_SYMBOL_FIELD, 4, 5}, | |
1101 | {UNUM_EXPONENT_SIGN_FIELD, 5, 6}, | |
1102 | {UNUM_EXPONENT_FIELD, 6, 9}, | |
1103 | {0, -1, 0}}; | |
1104 | verifySciFormatter( | |
1105 | "1248E+023", | |
1106 | formatter, | |
1107 | visibleDigits, | |
1108 | options, | |
1109 | expectedAttributes); | |
1110 | } | |
1111 | { | |
1112 | options.fMantissa.fAlwaysShowDecimal = TRUE; | |
1113 | options.fExponent.fAlwaysShowSign = FALSE; | |
1114 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1115 | {UNUM_INTEGER_FIELD, 0, 4}, | |
1116 | {UNUM_DECIMAL_SEPARATOR_FIELD, 4, 5}, | |
1117 | {UNUM_EXPONENT_SYMBOL_FIELD, 5, 6}, | |
1118 | {UNUM_EXPONENT_FIELD, 6, 9}, | |
1119 | {0, -1, 0}}; | |
1120 | verifySciFormatter( | |
1121 | "1248.E023", | |
1122 | formatter, | |
1123 | visibleDigits, | |
1124 | options, | |
1125 | expectedAttributes); | |
1126 | } | |
1127 | } | |
1128 | ||
1129 | void NumberFormat2Test::TestValueFormatterIsFastFormattable() { | |
1130 | UErrorCode status = U_ZERO_ERROR; | |
1131 | DecimalFormatSymbols symbols("en", status); | |
1132 | if (!assertSuccess("", status)) { | |
1133 | return; | |
1134 | } | |
1135 | DigitFormatter formatter(symbols); | |
1136 | DigitGrouping grouping; | |
1137 | FixedPrecision precision; | |
1138 | DigitFormatterOptions options; | |
1139 | ValueFormatter vf; | |
1140 | vf.prepareFixedDecimalFormatting( | |
1141 | formatter, grouping, precision, options); | |
1142 | assertTrue("", vf.isFastFormattable(0)); | |
1143 | assertTrue("", vf.isFastFormattable(35)); | |
1144 | assertTrue("", vf.isFastFormattable(-48)); | |
1145 | assertTrue("", vf.isFastFormattable(2147483647)); | |
1146 | assertTrue("", vf.isFastFormattable(-2147483647)); | |
1147 | assertFalse("", vf.isFastFormattable(-2147483648L)); | |
1148 | { | |
1149 | DigitGrouping grouping; | |
1150 | grouping.fGrouping = 3; | |
1151 | ValueFormatter vf; | |
1152 | vf.prepareFixedDecimalFormatting( | |
1153 | formatter, grouping, precision, options); | |
1154 | assertTrue("0", vf.isFastFormattable(0)); | |
1155 | assertTrue("62", vf.isFastFormattable(62)); | |
1156 | assertTrue("999", vf.isFastFormattable(999)); | |
1157 | assertFalse("1000", vf.isFastFormattable(1000)); | |
1158 | assertTrue("-1", vf.isFastFormattable(-1)); | |
1159 | assertTrue("-38", vf.isFastFormattable(-38)); | |
1160 | assertTrue("-999", vf.isFastFormattable(-999)); | |
1161 | assertFalse("-1000", vf.isFastFormattable(-1000)); | |
1162 | grouping.fMinGrouping = 2; | |
1163 | assertTrue("-1000", vf.isFastFormattable(-1000)); | |
1164 | assertTrue("-4095", vf.isFastFormattable(-4095)); | |
1165 | assertTrue("4095", vf.isFastFormattable(4095)); | |
1166 | // We give up on acounting digits at 4096 | |
1167 | assertFalse("-4096", vf.isFastFormattable(-4096)); | |
1168 | assertFalse("4096", vf.isFastFormattable(4096)); | |
1169 | } | |
1170 | { | |
1171 | // grouping on but with max integer digits set. | |
1172 | DigitGrouping grouping; | |
1173 | grouping.fGrouping = 4; | |
1174 | FixedPrecision precision; | |
1175 | precision.fMax.setIntDigitCount(4); | |
1176 | ValueFormatter vf; | |
1177 | vf.prepareFixedDecimalFormatting( | |
1178 | formatter, grouping, precision, options); | |
1179 | assertTrue("-4096", vf.isFastFormattable(-4096)); | |
1180 | assertTrue("4096", vf.isFastFormattable(4096)); | |
1181 | assertTrue("-10000", vf.isFastFormattable(-10000)); | |
1182 | assertTrue("10000", vf.isFastFormattable(10000)); | |
1183 | assertTrue("-2147483647", vf.isFastFormattable(-2147483647)); | |
1184 | assertTrue("2147483647", vf.isFastFormattable(2147483647)); | |
1185 | ||
1186 | precision.fMax.setIntDigitCount(5); | |
1187 | assertFalse("-4096", vf.isFastFormattable(-4096)); | |
1188 | assertFalse("4096", vf.isFastFormattable(4096)); | |
1189 | ||
1190 | } | |
1191 | { | |
1192 | // grouping on but with min integer digits set. | |
1193 | DigitGrouping grouping; | |
1194 | grouping.fGrouping = 3; | |
1195 | FixedPrecision precision; | |
1196 | precision.fMin.setIntDigitCount(3); | |
1197 | ValueFormatter vf; | |
1198 | vf.prepareFixedDecimalFormatting( | |
1199 | formatter, grouping, precision, options); | |
1200 | assertTrue("-999", vf.isFastFormattable(-999)); | |
1201 | assertTrue("999", vf.isFastFormattable(999)); | |
1202 | assertFalse("-1000", vf.isFastFormattable(-1000)); | |
1203 | assertFalse("1000", vf.isFastFormattable(1000)); | |
1204 | ||
1205 | precision.fMin.setIntDigitCount(4); | |
1206 | assertFalse("-999", vf.isFastFormattable(-999)); | |
1207 | assertFalse("999", vf.isFastFormattable(999)); | |
1208 | assertFalse("-2147483647", vf.isFastFormattable(-2147483647)); | |
1209 | assertFalse("2147483647", vf.isFastFormattable(2147483647)); | |
1210 | } | |
1211 | { | |
1212 | // options set. | |
1213 | DigitFormatterOptions options; | |
1214 | ValueFormatter vf; | |
1215 | vf.prepareFixedDecimalFormatting( | |
1216 | formatter, grouping, precision, options); | |
1217 | assertTrue("5125", vf.isFastFormattable(5125)); | |
1218 | options.fAlwaysShowDecimal = TRUE; | |
1219 | assertFalse("5125", vf.isFastFormattable(5125)); | |
1220 | options.fAlwaysShowDecimal = FALSE; | |
1221 | assertTrue("5125", vf.isFastFormattable(5125)); | |
1222 | } | |
1223 | { | |
1224 | // test fraction digits | |
1225 | FixedPrecision precision; | |
1226 | ValueFormatter vf; | |
1227 | vf.prepareFixedDecimalFormatting( | |
1228 | formatter, grouping, precision, options); | |
1229 | assertTrue("7127", vf.isFastFormattable(7127)); | |
1230 | precision.fMin.setFracDigitCount(1); | |
1231 | assertFalse("7127", vf.isFastFormattable(7127)); | |
1232 | } | |
1233 | { | |
1234 | // test presence of significant digits | |
1235 | FixedPrecision precision; | |
1236 | ValueFormatter vf; | |
1237 | vf.prepareFixedDecimalFormatting( | |
1238 | formatter, grouping, precision, options); | |
1239 | assertTrue("1049", vf.isFastFormattable(1049)); | |
1240 | precision.fSignificant.setMin(1); | |
1241 | assertFalse("1049", vf.isFastFormattable(1049)); | |
1242 | } | |
1243 | { | |
1244 | // test presence of rounding increment | |
1245 | FixedPrecision precision; | |
1246 | ValueFormatter vf; | |
1247 | vf.prepareFixedDecimalFormatting( | |
1248 | formatter, grouping, precision, options); | |
1249 | assertTrue("1099", vf.isFastFormattable(1099)); | |
1250 | precision.fRoundingIncrement.set(2.3); | |
1251 | assertFalse("1099", vf.isFastFormattable(1099)); | |
1252 | } | |
1253 | { | |
1254 | // test scientific notation | |
1255 | ScientificPrecision precision; | |
1256 | SciFormatterOptions options; | |
1257 | ValueFormatter vf; | |
1258 | vf.prepareScientificFormatting( | |
1259 | formatter, precision, options); | |
1260 | assertFalse("1081", vf.isFastFormattable(1081)); | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | void NumberFormat2Test::TestDigitAffix() { | |
1265 | DigitAffix affix; | |
1266 | { | |
1267 | affix.append("foo"); | |
1268 | affix.append("--", UNUM_SIGN_FIELD); | |
1269 | affix.append("%", UNUM_PERCENT_FIELD); | |
1270 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1271 | {UNUM_SIGN_FIELD, 3, 5}, | |
1272 | {UNUM_PERCENT_FIELD, 5, 6}, | |
1273 | {0, -1, 0}}; | |
1274 | verifyAffix("foo--%", affix, expectedAttributes); | |
1275 | } | |
1276 | { | |
1277 | affix.remove(); | |
1278 | affix.append("USD", UNUM_CURRENCY_FIELD); | |
1279 | affix.append(" "); | |
1280 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1281 | {UNUM_CURRENCY_FIELD, 0, 3}, | |
1282 | {0, -1, 0}}; | |
1283 | verifyAffix("USD ", affix, expectedAttributes); | |
1284 | } | |
1285 | { | |
1286 | affix.setTo("%%", UNUM_PERCENT_FIELD); | |
1287 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1288 | {UNUM_PERCENT_FIELD, 0, 2}, | |
1289 | {0, -1, 0}}; | |
1290 | verifyAffix("%%", affix, expectedAttributes); | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | void NumberFormat2Test::TestPluralAffix() { | |
1295 | UErrorCode status = U_ZERO_ERROR; | |
1296 | PluralAffix part; | |
1297 | part.setVariant("one", "Dollar", status); | |
1298 | part.setVariant("few", "DollarFew", status); | |
1299 | part.setVariant("other", "Dollars", status); | |
1300 | PluralAffix dollar(part); | |
1301 | PluralAffix percent(part); | |
1302 | part.remove(); | |
1303 | part.setVariant("one", "Percent", status); | |
1304 | part.setVariant("many", "PercentMany", status); | |
1305 | part.setVariant("other", "Percents", status); | |
1306 | percent = part; | |
1307 | part.remove(); | |
1308 | part.setVariant("one", "foo", status); | |
1309 | ||
1310 | PluralAffix pa; | |
1311 | assertEquals("", "", pa.getOtherVariant().toString()); | |
1312 | pa.append(dollar, UNUM_CURRENCY_FIELD, status); | |
1313 | pa.append(" and "); | |
1314 | pa.append(percent, UNUM_PERCENT_FIELD, status); | |
1315 | pa.append("-", UNUM_SIGN_FIELD); | |
1316 | ||
1317 | { | |
1318 | // other | |
1319 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1320 | {UNUM_CURRENCY_FIELD, 0, 7}, | |
1321 | {UNUM_PERCENT_FIELD, 12, 20}, | |
1322 | {UNUM_SIGN_FIELD, 20, 21}, | |
1323 | {0, -1, 0}}; | |
1324 | verifyAffix( | |
1325 | "Dollars and Percents-", | |
1326 | pa.getByCategory("other"), | |
1327 | expectedAttributes); | |
1328 | } | |
1329 | { | |
1330 | // two which is same as other | |
1331 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1332 | {UNUM_CURRENCY_FIELD, 0, 7}, | |
1333 | {UNUM_PERCENT_FIELD, 12, 20}, | |
1334 | {UNUM_SIGN_FIELD, 20, 21}, | |
1335 | {0, -1, 0}}; | |
1336 | verifyAffix( | |
1337 | "Dollars and Percents-", | |
1338 | pa.getByCategory("two"), | |
1339 | expectedAttributes); | |
1340 | } | |
1341 | { | |
1342 | // bad which is same as other | |
1343 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1344 | {UNUM_CURRENCY_FIELD, 0, 7}, | |
1345 | {UNUM_PERCENT_FIELD, 12, 20}, | |
1346 | {UNUM_SIGN_FIELD, 20, 21}, | |
1347 | {0, -1, 0}}; | |
1348 | verifyAffix( | |
1349 | "Dollars and Percents-", | |
1350 | pa.getByCategory("bad"), | |
1351 | expectedAttributes); | |
1352 | } | |
1353 | { | |
1354 | // one | |
1355 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1356 | {UNUM_CURRENCY_FIELD, 0, 6}, | |
1357 | {UNUM_PERCENT_FIELD, 11, 18}, | |
1358 | {UNUM_SIGN_FIELD, 18, 19}, | |
1359 | {0, -1, 0}}; | |
1360 | verifyAffix( | |
1361 | "Dollar and Percent-", | |
1362 | pa.getByCategory("one"), | |
1363 | expectedAttributes); | |
1364 | } | |
1365 | { | |
1366 | // few | |
1367 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1368 | {UNUM_CURRENCY_FIELD, 0, 9}, | |
1369 | {UNUM_PERCENT_FIELD, 14, 22}, | |
1370 | {UNUM_SIGN_FIELD, 22, 23}, | |
1371 | {0, -1, 0}}; | |
1372 | verifyAffix( | |
1373 | "DollarFew and Percents-", | |
1374 | pa.getByCategory("few"), | |
1375 | expectedAttributes); | |
1376 | } | |
1377 | { | |
1378 | // many | |
1379 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1380 | {UNUM_CURRENCY_FIELD, 0, 7}, | |
1381 | {UNUM_PERCENT_FIELD, 12, 23}, | |
1382 | {UNUM_SIGN_FIELD, 23, 24}, | |
1383 | {0, -1, 0}}; | |
1384 | verifyAffix( | |
1385 | "Dollars and PercentMany-", | |
1386 | pa.getByCategory("many"), | |
1387 | expectedAttributes); | |
1388 | } | |
1389 | assertTrue("", pa.hasMultipleVariants()); | |
1390 | pa.remove(); | |
1391 | pa.append("$$$", UNUM_CURRENCY_FIELD); | |
1392 | assertFalse("", pa.hasMultipleVariants()); | |
1393 | ||
1394 | } | |
1395 | ||
1396 | void NumberFormat2Test::TestCurrencyAffixInfo() { | |
1397 | CurrencyAffixInfo info; | |
1398 | assertTrue("", info.isDefault()); | |
1399 | UnicodeString expectedSymbol("\\u00a4"); | |
1400 | UnicodeString expectedSymbolIso("\\u00a4\\u00a4"); | |
1401 | UnicodeString expectedSymbols("\\u00a4\\u00a4\\u00a4"); | |
1402 | assertEquals("", expectedSymbol.unescape(), info.getSymbol()); | |
1403 | assertEquals("", expectedSymbolIso.unescape(), info.getISO()); | |
1404 | assertEquals("", expectedSymbols.unescape(), info.getLong().getByCategory("one").toString()); | |
1405 | assertEquals("", expectedSymbols.unescape(), info.getLong().getByCategory("other").toString()); | |
1406 | assertEquals("", expectedSymbols.unescape(), info.getLong().getByCategory("two").toString()); | |
1407 | UErrorCode status = U_ZERO_ERROR; | |
1408 | static UChar USD[] = {0x55, 0x53, 0x44, 0x0}; | |
1409 | LocalPointer<PluralRules> rules(PluralRules::forLocale("en", status)); | |
1410 | if (!assertSuccess("", status)) { | |
1411 | return; | |
1412 | } | |
1413 | info.set("en", rules.getAlias(), USD, status); | |
1414 | assertEquals("", "$", info.getSymbol(), TRUE); | |
1415 | assertEquals("", "USD", info.getISO(), TRUE); | |
1416 | assertEquals("", "US dollar", info.getLong().getByCategory("one").toString(), TRUE); | |
1417 | assertEquals("", "US dollars", info.getLong().getByCategory("other").toString(), TRUE); | |
1418 | assertEquals("", "US dollars", info.getLong().getByCategory("two").toString(), TRUE); | |
1419 | assertFalse("", info.isDefault()); | |
1420 | info.set(NULL, NULL, NULL, status); | |
1421 | assertTrue("", info.isDefault()); | |
1422 | assertEquals("", expectedSymbol.unescape(), info.getSymbol()); | |
1423 | assertEquals("", expectedSymbolIso.unescape(), info.getISO()); | |
1424 | assertEquals("", expectedSymbols.unescape(), info.getLong().getByCategory("one").toString()); | |
1425 | assertEquals("", expectedSymbols.unescape(), info.getLong().getByCategory("other").toString()); | |
1426 | assertEquals("", expectedSymbols.unescape(), info.getLong().getByCategory("two").toString()); | |
1427 | info.setSymbol("$"); | |
1428 | assertFalse("", info.isDefault()); | |
1429 | info.set(NULL, NULL, NULL, status); | |
1430 | assertTrue("", info.isDefault()); | |
1431 | info.setISO("USD"); | |
1432 | assertFalse("", info.isDefault()); | |
1433 | assertSuccess("", status); | |
1434 | } | |
1435 | ||
1436 | void NumberFormat2Test::TestAffixPattern() { | |
1437 | static UChar chars[500]; | |
1438 | for (int32_t i = 0; i < UPRV_LENGTHOF(chars); ++i) { | |
1439 | chars[i] = (UChar) (i + 1); | |
1440 | } | |
1441 | AffixPattern first; | |
1442 | first.add(AffixPattern::kPercent); | |
1443 | first.addLiteral(chars, 0, 200); | |
1444 | first.addLiteral(chars, 200, 300); | |
1445 | first.addCurrency(2); | |
1446 | first.addLiteral(chars, 0, 256); | |
1447 | AffixPattern second; | |
1448 | second.add(AffixPattern::kPercent); | |
1449 | second.addLiteral(chars, 0, 300); | |
1450 | second.addLiteral(chars, 300, 200); | |
1451 | second.addCurrency(2); | |
1452 | second.addLiteral(chars, 0, 150); | |
1453 | second.addLiteral(chars, 150, 106); | |
1454 | assertTrue("", first.equals(second)); | |
1455 | AffixPatternIterator iter; | |
1456 | second.remove(); | |
1457 | assertFalse("", second.iterator(iter).nextToken()); | |
1458 | assertTrue("", first.iterator(iter).nextToken()); | |
f3c0d7a5 | 1459 | assertEquals("", (int32_t)AffixPattern::kPercent, iter.getTokenType()); |
2ca993e8 A |
1460 | assertEquals("", 1, iter.getTokenLength()); |
1461 | assertTrue("", iter.nextToken()); | |
1462 | UnicodeString str; | |
1463 | assertEquals("", 500, iter.getLiteral(str).length()); | |
f3c0d7a5 | 1464 | assertEquals("", (int32_t)AffixPattern::kLiteral, iter.getTokenType()); |
2ca993e8 A |
1465 | assertEquals("", 500, iter.getTokenLength()); |
1466 | assertTrue("", iter.nextToken()); | |
f3c0d7a5 | 1467 | assertEquals("", (int32_t)AffixPattern::kCurrency, iter.getTokenType()); |
2ca993e8 A |
1468 | assertEquals("", 2, iter.getTokenLength()); |
1469 | assertTrue("", iter.nextToken()); | |
1470 | assertEquals("", 256, iter.getLiteral(str).length()); | |
f3c0d7a5 | 1471 | assertEquals("", (int32_t)AffixPattern::kLiteral, iter.getTokenType()); |
2ca993e8 A |
1472 | assertEquals("", 256, iter.getTokenLength()); |
1473 | assertFalse("", iter.nextToken()); | |
1474 | } | |
1475 | ||
1476 | void NumberFormat2Test::TestAffixPatternDoubleQuote() { | |
1477 | UnicodeString str("'Don''t'"); | |
1478 | AffixPattern expected; | |
1479 | // Don't | |
1480 | static UChar chars[] = {0x44, 0x6F, 0x6E, 0x27, 0x74}; | |
1481 | expected.addLiteral(chars, 0, UPRV_LENGTHOF(chars)); | |
1482 | AffixPattern actual; | |
1483 | UErrorCode status = U_ZERO_ERROR; | |
1484 | AffixPattern::parseUserAffixString(str, actual, status); | |
1485 | assertTrue("", expected.equals(actual)); | |
1486 | UnicodeString formattedString; | |
1487 | assertEquals("", "Don''t", actual.toUserString(formattedString)); | |
1488 | assertSuccess("", status); | |
1489 | } | |
1490 | ||
1491 | void NumberFormat2Test::TestAffixPatternParser() { | |
1492 | UErrorCode status = U_ZERO_ERROR; | |
1493 | static UChar USD[] = {0x55, 0x53, 0x44, 0x0}; | |
1494 | LocalPointer<PluralRules> rules(PluralRules::forLocale("en", status)); | |
1495 | DecimalFormatSymbols symbols("en", status); | |
1496 | if (U_FAILURE(status)) { | |
1497 | dataerrln("Error creating DecimalFormatSymbols - %s", u_errorName(status)); | |
1498 | return; | |
1499 | } | |
1500 | AffixPatternParser parser(symbols); | |
1501 | CurrencyAffixInfo currencyAffixInfo; | |
1502 | currencyAffixInfo.set("en", rules.getAlias(), USD, status); | |
1503 | PluralAffix affix; | |
1504 | UnicodeString str("'--y'''dz'%'\\u00a4\\u00a4\\u00a4\\u00a4 y '\\u00a4\\u00a4\\u00a4 or '\\u00a4\\u00a4 but '\\u00a4"); | |
1505 | str = str.unescape(); | |
1506 | assertSuccess("", status); | |
1507 | AffixPattern affixPattern; | |
1508 | parser.parse( | |
1509 | AffixPattern::parseAffixString(str, affixPattern, status), | |
1510 | currencyAffixInfo, | |
1511 | affix, | |
1512 | status); | |
1513 | UnicodeString formattedStr; | |
1514 | affixPattern.toString(formattedStr); | |
1515 | UnicodeString expectedFormattedStr("'--y''dz'%'\\u00a4\\u00a4\\u00a4\\u00a4 y '\\u00a4\\u00a4\\u00a4 or '\\u00a4\\u00a4 but '\\u00a4"); | |
1516 | expectedFormattedStr = expectedFormattedStr.unescape(); | |
1517 | assertEquals("1", expectedFormattedStr, formattedStr); | |
1518 | AffixPattern userAffixPattern; | |
1519 | UnicodeString userStr("-'-'y'''d'z%\\u00a4\\u00a4\\u00a4'\\u00a4' y \\u00a4\\u00a4\\u00a4 or \\u00a4\\u00a4 but \\u00a4"); | |
1520 | userStr = userStr.unescape(); | |
1521 | AffixPattern::parseUserAffixString(userStr, userAffixPattern, status), | |
1522 | assertTrue("", affixPattern.equals(userAffixPattern)); | |
1523 | AffixPattern userAffixPattern2; | |
1524 | UnicodeString formattedUserStr; | |
1525 | AffixPattern::parseUserAffixString( | |
1526 | userAffixPattern.toUserString(formattedUserStr), | |
1527 | userAffixPattern2, | |
1528 | status); | |
1529 | UnicodeString expectedFormattedUserStr( | |
1530 | "-'-'y''dz%\\u00a4\\u00a4\\u00a4'\\u00a4' y \\u00a4\\u00a4\\u00a4 or \\u00a4\\u00a4 but \\u00a4"); | |
1531 | assertEquals("2", expectedFormattedUserStr.unescape(), formattedUserStr); | |
1532 | assertTrue("", userAffixPattern2.equals(userAffixPattern)); | |
1533 | assertSuccess("", status); | |
1534 | assertTrue("", affixPattern.usesCurrency()); | |
1535 | assertTrue("", affixPattern.usesPercent()); | |
1536 | assertFalse("", affixPattern.usesPermill()); | |
1537 | assertTrue("", affix.hasMultipleVariants()); | |
1538 | { | |
1539 | // other | |
1540 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1541 | {UNUM_SIGN_FIELD, 0, 1}, | |
1542 | {UNUM_PERCENT_FIELD, 6, 7}, | |
1543 | {UNUM_CURRENCY_FIELD, 7, 17}, | |
1544 | {UNUM_CURRENCY_FIELD, 21, 31}, | |
1545 | {UNUM_CURRENCY_FIELD, 35, 38}, | |
1546 | {UNUM_CURRENCY_FIELD, 43, 44}, | |
1547 | {0, -1, 0}}; | |
1548 | verifyAffix( | |
1549 | "--y'dz%US dollars\\u00a4 y US dollars or USD but $", | |
1550 | affix.getByCategory("other"), | |
1551 | expectedAttributes); | |
1552 | } | |
1553 | { | |
1554 | // one | |
1555 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1556 | {UNUM_SIGN_FIELD, 0, 1}, | |
1557 | {UNUM_PERCENT_FIELD, 6, 7}, | |
1558 | {UNUM_CURRENCY_FIELD, 7, 16}, | |
1559 | {UNUM_CURRENCY_FIELD, 20, 29}, | |
1560 | {UNUM_CURRENCY_FIELD, 33, 36}, | |
1561 | {UNUM_CURRENCY_FIELD, 41, 42}, | |
1562 | {0, -1, 0}}; | |
1563 | verifyAffix( | |
1564 | "--y'dz%US dollar\\u00a4 y US dollar or USD but $", | |
1565 | affix.getByCategory("one"), | |
1566 | expectedAttributes); | |
1567 | } | |
1568 | affix.remove(); | |
1569 | str = "%'-"; | |
1570 | affixPattern.remove(); | |
1571 | parser.parse( | |
1572 | AffixPattern::parseAffixString(str, affixPattern, status), | |
1573 | currencyAffixInfo, | |
1574 | affix, | |
1575 | status); | |
1576 | assertSuccess("", status); | |
1577 | assertFalse("", affixPattern.usesCurrency()); | |
1578 | assertFalse("", affixPattern.usesPercent()); | |
1579 | assertFalse("", affixPattern.usesPermill()); | |
1580 | assertFalse("", affix.hasMultipleVariants()); | |
1581 | { | |
1582 | // other | |
1583 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1584 | {UNUM_SIGN_FIELD, 1, 2}, | |
1585 | {0, -1, 0}}; | |
1586 | verifyAffix( | |
1587 | "%-", | |
1588 | affix.getByCategory("other"), | |
1589 | expectedAttributes); | |
1590 | } | |
1591 | UnicodeString a4("\\u00a4"); | |
1592 | AffixPattern scratchPattern; | |
1593 | AffixPattern::parseAffixString(a4.unescape(), scratchPattern, status); | |
1594 | assertFalse("", scratchPattern.usesCurrency()); | |
1595 | ||
1596 | // Test really long string > 256 chars. | |
1597 | str = "'\\u2030012345678901234567890123456789012345678901234567890123456789" | |
1598 | "012345678901234567890123456789012345678901234567890123456789" | |
1599 | "012345678901234567890123456789012345678901234567890123456789" | |
1600 | "012345678901234567890123456789012345678901234567890123456789" | |
1601 | "012345678901234567890123456789012345678901234567890123456789"; | |
1602 | str = str.unescape(); | |
1603 | affixPattern.remove(); | |
1604 | affix.remove(); | |
1605 | parser.parse( | |
1606 | AffixPattern::parseAffixString(str, affixPattern, status), | |
1607 | currencyAffixInfo, | |
1608 | affix, | |
1609 | status); | |
1610 | assertSuccess("", status); | |
1611 | assertFalse("", affixPattern.usesCurrency()); | |
1612 | assertFalse("", affixPattern.usesPercent()); | |
1613 | assertTrue("", affixPattern.usesPermill()); | |
1614 | assertFalse("", affix.hasMultipleVariants()); | |
1615 | { | |
1616 | UnicodeString expected = | |
1617 | "\\u2030012345678901234567890123456789012345678901234567890123456789" | |
1618 | "012345678901234567890123456789012345678901234567890123456789" | |
1619 | "012345678901234567890123456789012345678901234567890123456789" | |
1620 | "012345678901234567890123456789012345678901234567890123456789" | |
1621 | "012345678901234567890123456789012345678901234567890123456789"; | |
1622 | expected = expected.unescape(); | |
1623 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
1624 | {UNUM_PERMILL_FIELD, 0, 1}, | |
1625 | {0, -1, 0}}; | |
1626 | verifyAffix( | |
1627 | expected, | |
1628 | affix.getOtherVariant(), | |
1629 | expectedAttributes); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | void NumberFormat2Test::TestAffixPatternAppend() { | |
1634 | AffixPattern pattern; | |
1635 | UErrorCode status = U_ZERO_ERROR; | |
1636 | UnicodeString patternStr("%\\u2030"); | |
1637 | AffixPattern::parseUserAffixString( | |
1638 | patternStr.unescape(), pattern, status); | |
1639 | ||
1640 | AffixPattern appendPattern; | |
1641 | UnicodeString appendPatternStr("-\\u00a4\\u00a4*"); | |
1642 | AffixPattern::parseUserAffixString( | |
1643 | appendPatternStr.unescape(), appendPattern, status); | |
1644 | ||
1645 | AffixPattern expectedPattern; | |
1646 | UnicodeString expectedPatternStr("%\\u2030-\\u00a4\\u00a4*"); | |
1647 | AffixPattern::parseUserAffixString( | |
1648 | expectedPatternStr.unescape(), expectedPattern, status); | |
1649 | ||
1650 | assertTrue("", pattern.append(appendPattern).equals(expectedPattern)); | |
1651 | assertSuccess("", status); | |
1652 | } | |
1653 | ||
1654 | void NumberFormat2Test::TestAffixPatternAppendAjoiningLiterals() { | |
1655 | AffixPattern pattern; | |
1656 | UErrorCode status = U_ZERO_ERROR; | |
1657 | UnicodeString patternStr("%baaa"); | |
1658 | AffixPattern::parseUserAffixString( | |
1659 | patternStr, pattern, status); | |
1660 | ||
1661 | AffixPattern appendPattern; | |
1662 | UnicodeString appendPatternStr("caa%"); | |
1663 | AffixPattern::parseUserAffixString( | |
1664 | appendPatternStr, appendPattern, status); | |
1665 | ||
1666 | AffixPattern expectedPattern; | |
1667 | UnicodeString expectedPatternStr("%baaacaa%"); | |
1668 | AffixPattern::parseUserAffixString( | |
1669 | expectedPatternStr, expectedPattern, status); | |
1670 | ||
1671 | assertTrue("", pattern.append(appendPattern).equals(expectedPattern)); | |
1672 | assertSuccess("", status); | |
1673 | } | |
1674 | ||
1675 | void NumberFormat2Test::TestLargeIntValue() { | |
1676 | VisibleDigits digits; | |
1677 | { | |
1678 | UErrorCode status = U_ZERO_ERROR; | |
1679 | FixedPrecision precision; | |
1680 | ||
1681 | // Last 18 digits for int values. | |
1682 | verifyIntValue( | |
1683 | 223372036854775807LL, | |
f3c0d7a5 A |
1684 | precision.initVisibleDigits(U_INT64_MAX, digits, status)); |
1685 | assertSuccess("U_INT64_MAX", status); | |
2ca993e8 A |
1686 | } |
1687 | { | |
1688 | UErrorCode status = U_ZERO_ERROR; | |
1689 | FixedPrecision precision; | |
1690 | precision.fMax.setIntDigitCount(5); | |
1691 | ||
1692 | // Last 18 digits for int values. | |
1693 | verifyIntValue( | |
1694 | 75807LL, | |
f3c0d7a5 | 1695 | precision.initVisibleDigits(U_INT64_MAX, digits, status)); |
2ca993e8 A |
1696 | verifySource(75807.0, digits); |
1697 | assertSuccess("75807", status); | |
1698 | } | |
1699 | { | |
1700 | UErrorCode status = U_ZERO_ERROR; | |
1701 | FixedPrecision precision; | |
1702 | ||
1703 | // Last 18 digits for int values. | |
1704 | verifyIntValue( | |
1705 | 223372036854775808LL, | |
f3c0d7a5 A |
1706 | precision.initVisibleDigits(U_INT64_MIN, digits, status)); |
1707 | assertSuccess("U_INT64_MIN", status); | |
2ca993e8 A |
1708 | } |
1709 | { | |
1710 | UErrorCode status = U_ZERO_ERROR; | |
1711 | FixedPrecision precision; | |
1712 | precision.fMax.setIntDigitCount(5); | |
1713 | ||
1714 | // Last 18 digits for int values. | |
1715 | verifyIntValue( | |
1716 | 75808LL, | |
f3c0d7a5 | 1717 | precision.initVisibleDigits(U_INT64_MIN, digits, status)); |
2ca993e8 A |
1718 | verifySource(75808.0, digits); |
1719 | assertSuccess("75808", status); | |
1720 | } | |
1721 | ||
1722 | } | |
1723 | ||
1724 | void NumberFormat2Test::TestIntInitVisibleDigits() { | |
1725 | VisibleDigits digits; | |
1726 | { | |
1727 | UErrorCode status = U_ZERO_ERROR; | |
1728 | FixedPrecision precision; | |
1729 | verifyVisibleDigits( | |
1730 | "13", | |
1731 | FALSE, | |
1732 | precision.initVisibleDigits((int64_t) 13LL, digits, status)); | |
1733 | assertSuccess("13", status); | |
1734 | } | |
1735 | { | |
1736 | UErrorCode status = U_ZERO_ERROR; | |
1737 | FixedPrecision precision; | |
1738 | verifyVisibleDigits( | |
1739 | "17", | |
1740 | TRUE, | |
1741 | precision.initVisibleDigits((int64_t) -17LL, digits, status)); | |
1742 | assertSuccess("-17", status); | |
1743 | } | |
1744 | { | |
1745 | UErrorCode status = U_ZERO_ERROR; | |
1746 | FixedPrecision precision; | |
1747 | verifyVisibleDigits( | |
1748 | "9223372036854775808", | |
1749 | TRUE, | |
f3c0d7a5 | 1750 | precision.initVisibleDigits(U_INT64_MIN, digits, status)); |
2ca993e8 A |
1751 | assertSuccess("-9223372036854775808", status); |
1752 | } | |
1753 | { | |
1754 | UErrorCode status = U_ZERO_ERROR; | |
1755 | FixedPrecision precision; | |
1756 | verifyVisibleDigits( | |
1757 | "9223372036854775807", | |
1758 | FALSE, | |
f3c0d7a5 | 1759 | precision.initVisibleDigits(U_INT64_MAX, digits, status)); |
2ca993e8 A |
1760 | assertSuccess("9223372036854775807", status); |
1761 | } | |
1762 | { | |
1763 | UErrorCode status = U_ZERO_ERROR; | |
1764 | FixedPrecision precision; | |
1765 | verifyVisibleDigits( | |
1766 | "31536000", | |
1767 | TRUE, | |
1768 | precision.initVisibleDigits((int64_t) -31536000LL, digits, status)); | |
1769 | assertSuccess("-31536000", status); | |
1770 | } | |
1771 | { | |
1772 | UErrorCode status = U_ZERO_ERROR; | |
1773 | FixedPrecision precision; | |
1774 | verifyVisibleDigits( | |
1775 | "0", | |
1776 | FALSE, | |
1777 | precision.initVisibleDigits((int64_t) 0LL, digits, status)); | |
1778 | assertSuccess("0", status); | |
1779 | } | |
1780 | { | |
1781 | UErrorCode status = U_ZERO_ERROR; | |
1782 | FixedPrecision precision; | |
1783 | precision.fMin.setIntDigitCount(4); | |
1784 | precision.fMin.setFracDigitCount(2); | |
1785 | verifyVisibleDigits( | |
1786 | "0000.00", | |
1787 | FALSE, | |
1788 | precision.initVisibleDigits((int64_t) 0LL, digits, status)); | |
1789 | assertSuccess("0", status); | |
1790 | } | |
1791 | { | |
1792 | UErrorCode status = U_ZERO_ERROR; | |
1793 | FixedPrecision precision; | |
1794 | precision.fMin.setIntDigitCount(4); | |
1795 | precision.fMin.setFracDigitCount(2); | |
1796 | verifyVisibleDigits( | |
1797 | "0057.00", | |
1798 | FALSE, | |
1799 | precision.initVisibleDigits((int64_t) 57LL, digits, status)); | |
1800 | assertSuccess("57", status); | |
1801 | } | |
1802 | { | |
1803 | UErrorCode status = U_ZERO_ERROR; | |
1804 | FixedPrecision precision; | |
1805 | precision.fMin.setIntDigitCount(4); | |
1806 | precision.fMin.setFracDigitCount(2); | |
1807 | verifyVisibleDigits( | |
1808 | "0057.00", | |
1809 | TRUE, | |
1810 | precision.initVisibleDigits((int64_t) -57LL, digits, status)); | |
1811 | assertSuccess("-57", status); | |
1812 | } | |
1813 | { | |
1814 | UErrorCode status = U_ZERO_ERROR; | |
1815 | FixedPrecision precision; | |
1816 | precision.fMax.setIntDigitCount(2); | |
1817 | precision.fMin.setFracDigitCount(1); | |
1818 | verifyVisibleDigits( | |
1819 | "35.0", | |
1820 | FALSE, | |
1821 | precision.initVisibleDigits((int64_t) 235LL, digits, status)); | |
1822 | assertSuccess("235", status); | |
1823 | } | |
1824 | { | |
1825 | UErrorCode status = U_ZERO_ERROR; | |
1826 | FixedPrecision precision; | |
1827 | precision.fMax.setIntDigitCount(2); | |
1828 | precision.fMin.setFracDigitCount(1); | |
1829 | precision.fFailIfOverMax = TRUE; | |
1830 | precision.initVisibleDigits((int64_t) 239LL, digits, status); | |
1831 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
1832 | errln("239: Expected U_ILLEGAL_ARGUMENT_ERROR"); | |
1833 | } | |
1834 | } | |
1835 | { | |
1836 | UErrorCode status = U_ZERO_ERROR; | |
1837 | FixedPrecision precision; | |
1838 | precision.fSignificant.setMin(5); | |
1839 | verifyVisibleDigits( | |
1840 | "153.00", | |
1841 | FALSE, | |
1842 | precision.initVisibleDigits((int64_t) 153LL, digits, status)); | |
1843 | assertSuccess("153", status); | |
1844 | } | |
1845 | { | |
1846 | UErrorCode status = U_ZERO_ERROR; | |
1847 | FixedPrecision precision; | |
1848 | precision.fSignificant.setMax(2); | |
1849 | precision.fExactOnly = TRUE; | |
1850 | precision.initVisibleDigits((int64_t) 154LL, digits, status); | |
1851 | if (status != U_FORMAT_INEXACT_ERROR) { | |
1852 | errln("154: Expected U_FORMAT_INEXACT_ERROR"); | |
1853 | } | |
1854 | } | |
1855 | { | |
1856 | UErrorCode status = U_ZERO_ERROR; | |
1857 | FixedPrecision precision; | |
1858 | precision.fSignificant.setMax(5); | |
1859 | verifyVisibleDigits( | |
1860 | "150", | |
1861 | FALSE, | |
1862 | precision.initVisibleDigits((int64_t) 150LL, digits, status)); | |
1863 | assertSuccess("150", status); | |
1864 | } | |
1865 | } | |
1866 | ||
1867 | void NumberFormat2Test::TestIntInitVisibleDigitsToDigitList() { | |
1868 | VisibleDigits digits; | |
1869 | { | |
1870 | UErrorCode status = U_ZERO_ERROR; | |
1871 | FixedPrecision precision; | |
1872 | precision.fRoundingIncrement.set(7.3); | |
1873 | verifyVisibleDigits( | |
1874 | "29.2", | |
1875 | TRUE, | |
1876 | precision.initVisibleDigits((int64_t) -30LL, digits, status)); | |
1877 | assertSuccess("-29.2", status); | |
1878 | } | |
1879 | { | |
1880 | UErrorCode status = U_ZERO_ERROR; | |
1881 | FixedPrecision precision; | |
1882 | precision.fRoundingIncrement.set(7.3); | |
1883 | precision.fRoundingMode = DecimalFormat::kRoundFloor; | |
1884 | verifyVisibleDigits( | |
1885 | "36.5", | |
1886 | TRUE, | |
1887 | precision.initVisibleDigits((int64_t) -30LL, digits, status)); | |
1888 | assertSuccess("-36.5", status); | |
1889 | } | |
1890 | { | |
1891 | UErrorCode status = U_ZERO_ERROR; | |
1892 | FixedPrecision precision; | |
1893 | precision.fSignificant.setMax(3); | |
1894 | precision.fRoundingMode = DecimalFormat::kRoundCeiling; | |
1895 | verifyVisibleDigits( | |
1896 | "1390", | |
1897 | FALSE, | |
1898 | precision.initVisibleDigits((int64_t) 1381LL, digits, status)); | |
1899 | assertSuccess("1390", status); | |
1900 | } | |
1901 | { | |
1902 | UErrorCode status = U_ZERO_ERROR; | |
1903 | FixedPrecision precision; | |
1904 | precision.fSignificant.setMax(1); | |
1905 | precision.fRoundingMode = DecimalFormat::kRoundFloor; | |
1906 | verifyVisibleDigits( | |
1907 | "2000", | |
1908 | TRUE, | |
1909 | precision.initVisibleDigits((int64_t) -1381LL, digits, status)); | |
1910 | assertSuccess("-2000", status); | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | void NumberFormat2Test::TestDoubleInitVisibleDigits() { | |
1915 | VisibleDigits digits; | |
1916 | { | |
1917 | UErrorCode status = U_ZERO_ERROR; | |
1918 | FixedPrecision precision; | |
1919 | verifyVisibleDigits( | |
1920 | "2.05", | |
1921 | FALSE, | |
1922 | precision.initVisibleDigits(2.05, digits, status)); | |
1923 | assertSuccess("2.05", status); | |
1924 | } | |
1925 | { | |
1926 | UErrorCode status = U_ZERO_ERROR; | |
1927 | FixedPrecision precision; | |
1928 | verifyVisibleDigits( | |
1929 | "3547", | |
1930 | FALSE, | |
1931 | precision.initVisibleDigits(3547.0, digits, status)); | |
1932 | assertSuccess("3547", status); | |
1933 | } | |
1934 | { | |
1935 | UErrorCode status = U_ZERO_ERROR; | |
1936 | FixedPrecision precision; | |
1937 | precision.fMax.setFracDigitCount(2); | |
1938 | precision.fMax.setIntDigitCount(1); | |
1939 | precision.fFailIfOverMax = TRUE; | |
1940 | precision.fExactOnly = TRUE; | |
1941 | verifyVisibleDigits( | |
1942 | "2.05", | |
1943 | TRUE, | |
1944 | precision.initVisibleDigits(-2.05, digits, status)); | |
1945 | assertSuccess("-2.05", status); | |
1946 | } | |
1947 | { | |
1948 | UErrorCode status = U_ZERO_ERROR; | |
1949 | FixedPrecision precision; | |
1950 | precision.fMax.setFracDigitCount(1); | |
1951 | precision.fMax.setIntDigitCount(1); | |
1952 | precision.fFailIfOverMax = TRUE; | |
1953 | precision.fExactOnly = TRUE; | |
1954 | precision.initVisibleDigits(-2.05, digits, status); | |
1955 | if (status != U_FORMAT_INEXACT_ERROR) { | |
1956 | errln("6245.3: Expected U_FORMAT_INEXACT_ERROR"); | |
1957 | } | |
1958 | } | |
1959 | { | |
1960 | UErrorCode status = U_ZERO_ERROR; | |
1961 | FixedPrecision precision; | |
1962 | precision.fMax.setFracDigitCount(2); | |
1963 | precision.fMax.setIntDigitCount(0); | |
1964 | precision.fFailIfOverMax = TRUE; | |
1965 | precision.fExactOnly = TRUE; | |
1966 | precision.initVisibleDigits(-2.05, digits, status); | |
1967 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
1968 | errln("-2.05: Expected U_ILLEGAL_ARGUMENT_ERROR"); | |
1969 | } | |
1970 | } | |
1971 | { | |
1972 | UErrorCode status = U_ZERO_ERROR; | |
1973 | FixedPrecision precision; | |
1974 | precision.fMin.setIntDigitCount(5); | |
1975 | precision.fMin.setFracDigitCount(2); | |
1976 | precision.fExactOnly = TRUE; | |
1977 | verifyVisibleDigits( | |
1978 | "06245.30", | |
1979 | FALSE, | |
1980 | precision.initVisibleDigits(6245.3, digits, status)); | |
1981 | assertSuccess("06245.30", status); | |
1982 | } | |
1983 | { | |
1984 | UErrorCode status = U_ZERO_ERROR; | |
1985 | FixedPrecision precision; | |
1986 | precision.fSignificant.setMax(5); | |
1987 | precision.fExactOnly = TRUE; | |
1988 | verifyVisibleDigits( | |
1989 | "6245.3", | |
1990 | FALSE, | |
1991 | precision.initVisibleDigits(6245.3, digits, status)); | |
1992 | assertSuccess("6245.3", status); | |
1993 | } | |
1994 | { | |
1995 | UErrorCode status = U_ZERO_ERROR; | |
1996 | FixedPrecision precision; | |
1997 | precision.fSignificant.setMax(4); | |
1998 | precision.fExactOnly = TRUE; | |
1999 | precision.initVisibleDigits(6245.3, digits, status); | |
2000 | if (status != U_FORMAT_INEXACT_ERROR) { | |
2001 | errln("6245.3: Expected U_FORMAT_INEXACT_ERROR"); | |
2002 | } | |
2003 | } | |
2004 | { | |
2005 | UErrorCode status = U_ZERO_ERROR; | |
2006 | FixedPrecision precision; | |
2007 | precision.fMax.setIntDigitCount(3); | |
2008 | precision.fMin.setFracDigitCount(2); | |
2009 | verifyVisibleDigits( | |
2010 | "384.90", | |
2011 | FALSE, | |
2012 | precision.initVisibleDigits(2384.9, digits, status)); | |
2013 | assertSuccess("380.00", status); | |
2014 | } | |
2015 | { | |
2016 | UErrorCode status = U_ZERO_ERROR; | |
2017 | FixedPrecision precision; | |
2018 | precision.fMax.setIntDigitCount(3); | |
2019 | precision.fMin.setFracDigitCount(2); | |
2020 | precision.fFailIfOverMax = TRUE; | |
2021 | precision.initVisibleDigits(2384.9, digits, status); | |
2022 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
2023 | errln("2384.9: Expected U_ILLEGAL_ARGUMENT_ERROR"); | |
2024 | } | |
2025 | } | |
2026 | } | |
2027 | ||
2028 | void NumberFormat2Test::TestDoubleInitVisibleDigitsToDigitList() { | |
2029 | VisibleDigits digits; | |
2030 | { | |
2031 | UErrorCode status = U_ZERO_ERROR; | |
2032 | FixedPrecision precision; | |
2033 | // 2.01 produces round off error when multiplied by powers of | |
2034 | // 10 forcing the use of DigitList. | |
2035 | verifyVisibleDigits( | |
2036 | "2.01", | |
2037 | TRUE, | |
2038 | precision.initVisibleDigits(-2.01, digits, status)); | |
2039 | assertSuccess("-2.01", status); | |
2040 | } | |
2041 | { | |
2042 | UErrorCode status = U_ZERO_ERROR; | |
2043 | FixedPrecision precision; | |
2044 | precision.fSignificant.setMax(3); | |
2045 | precision.fMin.setFracDigitCount(2); | |
2046 | verifyVisibleDigits( | |
2047 | "2380.00", | |
2048 | FALSE, | |
2049 | precision.initVisibleDigits(2385.0, digits, status)); | |
2050 | assertSuccess("2380.00", status); | |
2051 | } | |
2052 | { | |
2053 | UErrorCode status = U_ZERO_ERROR; | |
2054 | FixedPrecision precision; | |
2055 | precision.fMax.setFracDigitCount(2); | |
2056 | verifyVisibleDigits( | |
2057 | "45.83", | |
2058 | TRUE, | |
2059 | precision.initVisibleDigits(-45.8251, digits, status)); | |
2060 | assertSuccess("45.83", status); | |
2061 | } | |
2062 | } | |
2063 | ||
2064 | void NumberFormat2Test::TestDigitListInitVisibleDigits() { | |
2065 | VisibleDigits digits; | |
2066 | DigitList dlist; | |
2067 | { | |
2068 | UErrorCode status = U_ZERO_ERROR; | |
2069 | FixedPrecision precision; | |
2070 | precision.fMax.setIntDigitCount(3); | |
2071 | precision.fMin.setFracDigitCount(2); | |
2072 | precision.fFailIfOverMax = TRUE; | |
2073 | dlist.set(2384.9); | |
2074 | precision.initVisibleDigits(dlist, digits, status); | |
2075 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
2076 | errln("2384.9: Expected U_ILLEGAL_ARGUMENT_ERROR"); | |
2077 | } | |
2078 | } | |
2079 | { | |
2080 | UErrorCode status = U_ZERO_ERROR; | |
2081 | FixedPrecision precision; | |
2082 | precision.fSignificant.setMax(4); | |
2083 | precision.fExactOnly = TRUE; | |
2084 | dlist.set(6245.3); | |
2085 | precision.initVisibleDigits(dlist, digits, status); | |
2086 | if (status != U_FORMAT_INEXACT_ERROR) { | |
2087 | errln("6245.3: Expected U_FORMAT_INEXACT_ERROR"); | |
2088 | } | |
2089 | } | |
2090 | } | |
2091 | ||
2092 | void NumberFormat2Test::TestSpecialInitVisibleDigits() { | |
2093 | VisibleDigits digits; | |
2094 | { | |
2095 | UErrorCode status = U_ZERO_ERROR; | |
2096 | FixedPrecision precision; | |
2097 | precision.fSignificant.setMax(3); | |
2098 | precision.fMin.setFracDigitCount(2); | |
2099 | precision.initVisibleDigits(-uprv_getInfinity(), digits, status); | |
2100 | assertFalse("", digits.isNaN()); | |
2101 | assertTrue("", digits.isInfinite()); | |
2102 | assertTrue("", digits.isNegative()); | |
2103 | assertSuccess("-Inf", status); | |
2104 | } | |
2105 | { | |
2106 | UErrorCode status = U_ZERO_ERROR; | |
2107 | FixedPrecision precision; | |
2108 | precision.initVisibleDigits(uprv_getInfinity(), digits, status); | |
2109 | assertFalse("", digits.isNaN()); | |
2110 | assertTrue("", digits.isInfinite()); | |
2111 | assertFalse("", digits.isNegative()); | |
2112 | assertSuccess("Inf", status); | |
2113 | } | |
2114 | { | |
2115 | UErrorCode status = U_ZERO_ERROR; | |
2116 | FixedPrecision precision; | |
2117 | precision.initVisibleDigits(uprv_getNaN(), digits, status); | |
2118 | assertTrue("", digits.isNaN()); | |
2119 | assertSuccess("Inf", status); | |
2120 | } | |
2121 | } | |
2122 | ||
2123 | void NumberFormat2Test::TestVisibleDigitsWithExponent() { | |
2124 | VisibleDigitsWithExponent digits; | |
2125 | { | |
2126 | UErrorCode status = U_ZERO_ERROR; | |
2127 | ScientificPrecision precision; | |
2128 | precision.initVisibleDigitsWithExponent(389.256, digits, status); | |
2129 | verifyVisibleDigitsWithExponent( | |
2130 | "3.89256E2", FALSE, digits); | |
2131 | assertSuccess("3.89256E2", status); | |
2132 | } | |
2133 | { | |
2134 | UErrorCode status = U_ZERO_ERROR; | |
2135 | ScientificPrecision precision; | |
2136 | precision.initVisibleDigitsWithExponent(-389.256, digits, status); | |
2137 | verifyVisibleDigitsWithExponent( | |
2138 | "3.89256E2", TRUE, digits); | |
2139 | assertSuccess("-3.89256E2", status); | |
2140 | } | |
2141 | { | |
2142 | UErrorCode status = U_ZERO_ERROR; | |
2143 | ScientificPrecision precision; | |
2144 | precision.fMinExponentDigits = 3; | |
2145 | precision.fMantissa.fMin.setIntDigitCount(1); | |
2146 | precision.fMantissa.fMax.setIntDigitCount(3); | |
2147 | precision.initVisibleDigitsWithExponent(12345.67, digits, status); | |
2148 | verifyVisibleDigitsWithExponent( | |
2149 | "12.34567E003", FALSE, digits); | |
2150 | assertSuccess("12.34567E003", status); | |
2151 | } | |
2152 | { | |
2153 | UErrorCode status = U_ZERO_ERROR; | |
2154 | ScientificPrecision precision; | |
2155 | precision.fMantissa.fRoundingIncrement.set(0.073); | |
2156 | precision.fMantissa.fMin.setIntDigitCount(2); | |
2157 | precision.fMantissa.fMax.setIntDigitCount(2); | |
2158 | precision.initVisibleDigitsWithExponent(999.74, digits, status); | |
2159 | verifyVisibleDigitsWithExponent( | |
2160 | "10.001E2", FALSE, digits); | |
2161 | assertSuccess("10.001E2", status); | |
2162 | } | |
2163 | } | |
2164 | ||
2165 | void NumberFormat2Test::TestDigitAffixesAndPadding() { | |
2166 | UErrorCode status = U_ZERO_ERROR; | |
2167 | DecimalFormatSymbols symbols("en", status); | |
2168 | if (!assertSuccess("", status)) { | |
2169 | return; | |
2170 | } | |
2171 | DigitFormatter formatter(symbols); | |
2172 | DigitGrouping grouping; | |
2173 | grouping.fGrouping = 3; | |
2174 | FixedPrecision precision; | |
2175 | DigitFormatterOptions options; | |
2176 | options.fAlwaysShowDecimal = TRUE; | |
2177 | ValueFormatter vf; | |
2178 | vf.prepareFixedDecimalFormatting( | |
2179 | formatter, | |
2180 | grouping, | |
2181 | precision, | |
2182 | options); | |
2183 | DigitAffixesAndPadding aap; | |
2184 | aap.fPositivePrefix.append("(+", UNUM_SIGN_FIELD); | |
2185 | aap.fPositiveSuffix.append("+)", UNUM_SIGN_FIELD); | |
2186 | aap.fNegativePrefix.append("(-", UNUM_SIGN_FIELD); | |
2187 | aap.fNegativeSuffix.append("-)", UNUM_SIGN_FIELD); | |
2188 | aap.fWidth = 10; | |
2189 | aap.fPadPosition = DigitAffixesAndPadding::kPadBeforePrefix; | |
2190 | { | |
2191 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2192 | {UNUM_SIGN_FIELD, 4, 6}, | |
2193 | {UNUM_INTEGER_FIELD, 6, 7}, | |
2194 | {UNUM_DECIMAL_SEPARATOR_FIELD, 7, 8}, | |
2195 | {UNUM_SIGN_FIELD, 8, 10}, | |
2196 | {0, -1, 0}}; | |
2197 | verifyAffixesAndPaddingInt32( | |
2198 | "****(+3.+)", | |
2199 | aap, | |
2200 | 3, | |
2201 | vf, | |
2202 | NULL, | |
2203 | expectedAttributes); | |
2204 | } | |
2205 | aap.fPadPosition = DigitAffixesAndPadding::kPadAfterPrefix; | |
2206 | { | |
2207 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2208 | {UNUM_SIGN_FIELD, 0, 2}, | |
2209 | {UNUM_INTEGER_FIELD, 6, 7}, | |
2210 | {UNUM_DECIMAL_SEPARATOR_FIELD, 7, 8}, | |
2211 | {UNUM_SIGN_FIELD, 8, 10}, | |
2212 | {0, -1, 0}}; | |
2213 | verifyAffixesAndPaddingInt32( | |
2214 | "(+****3.+)", | |
2215 | aap, | |
2216 | 3, | |
2217 | vf, | |
2218 | NULL, | |
2219 | expectedAttributes); | |
2220 | } | |
2221 | aap.fPadPosition = DigitAffixesAndPadding::kPadBeforeSuffix; | |
2222 | { | |
2223 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2224 | {UNUM_SIGN_FIELD, 0, 2}, | |
2225 | {UNUM_INTEGER_FIELD, 2, 3}, | |
2226 | {UNUM_DECIMAL_SEPARATOR_FIELD, 3, 4}, | |
2227 | {UNUM_SIGN_FIELD, 8, 10}, | |
2228 | {0, -1, 0}}; | |
2229 | verifyAffixesAndPaddingInt32( | |
2230 | "(+3.****+)", | |
2231 | aap, | |
2232 | 3, | |
2233 | vf, | |
2234 | NULL, | |
2235 | expectedAttributes); | |
2236 | } | |
2237 | aap.fPadPosition = DigitAffixesAndPadding::kPadAfterSuffix; | |
2238 | { | |
2239 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2240 | {UNUM_SIGN_FIELD, 0, 2}, | |
2241 | {UNUM_INTEGER_FIELD, 2, 3}, | |
2242 | {UNUM_DECIMAL_SEPARATOR_FIELD, 3, 4}, | |
2243 | {UNUM_SIGN_FIELD, 4, 6}, | |
2244 | {0, -1, 0}}; | |
2245 | verifyAffixesAndPaddingInt32( | |
2246 | "(+3.+)****", | |
2247 | aap, | |
2248 | 3, | |
2249 | vf, | |
2250 | NULL, | |
2251 | expectedAttributes); | |
2252 | } | |
2253 | aap.fPadPosition = DigitAffixesAndPadding::kPadAfterSuffix; | |
2254 | { | |
2255 | DigitList digits; | |
2256 | digits.set(-1234.5); | |
2257 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2258 | {UNUM_SIGN_FIELD, 0, 2}, | |
2259 | {UNUM_GROUPING_SEPARATOR_FIELD, 3, 4}, | |
2260 | {UNUM_INTEGER_FIELD, 2, 7}, | |
2261 | {UNUM_DECIMAL_SEPARATOR_FIELD, 7, 8}, | |
2262 | {UNUM_FRACTION_FIELD, 8, 9}, | |
2263 | {UNUM_SIGN_FIELD, 9, 11}, | |
2264 | {0, -1, 0}}; | |
2265 | verifyAffixesAndPadding( | |
2266 | "(-1,234.5-)", | |
2267 | aap, | |
2268 | digits, | |
2269 | vf, | |
2270 | NULL, | |
2271 | expectedAttributes); | |
2272 | } | |
2273 | assertFalse("", aap.needsPluralRules()); | |
2274 | ||
2275 | aap.fWidth = 0; | |
2276 | aap.fPositivePrefix.remove(); | |
2277 | aap.fPositiveSuffix.remove(); | |
2278 | aap.fNegativePrefix.remove(); | |
2279 | aap.fNegativeSuffix.remove(); | |
2280 | ||
2281 | // Set up for plural currencies. | |
2282 | aap.fNegativePrefix.append("-", UNUM_SIGN_FIELD); | |
2283 | { | |
2284 | PluralAffix part; | |
2285 | part.setVariant("one", " Dollar", status); | |
2286 | part.setVariant("other", " Dollars", status); | |
2287 | aap.fPositiveSuffix.append(part, UNUM_CURRENCY_FIELD, status); | |
2288 | } | |
2289 | aap.fNegativeSuffix = aap.fPositiveSuffix; | |
2290 | ||
2291 | LocalPointer<PluralRules> rules(PluralRules::forLocale("en", status)); | |
2292 | if (!assertSuccess("", status)) { | |
2293 | return; | |
2294 | } | |
2295 | ||
2296 | // Exercise the fastrack path | |
2297 | { | |
2298 | options.fAlwaysShowDecimal = FALSE; | |
2299 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2300 | {UNUM_SIGN_FIELD, 0, 1}, | |
2301 | {UNUM_INTEGER_FIELD, 1, 3}, | |
2302 | {UNUM_CURRENCY_FIELD, 3, 11}, | |
2303 | {0, -1, 0}}; | |
2304 | verifyAffixesAndPaddingInt32( | |
2305 | "-45 Dollars", | |
2306 | aap, | |
2307 | -45, | |
2308 | vf, | |
2309 | NULL, | |
2310 | expectedAttributes); | |
2311 | options.fAlwaysShowDecimal = TRUE; | |
2312 | } | |
2313 | ||
2314 | // Now test plurals | |
2315 | assertTrue("", aap.needsPluralRules()); | |
2316 | { | |
2317 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2318 | {UNUM_INTEGER_FIELD, 0, 1}, | |
2319 | {UNUM_DECIMAL_SEPARATOR_FIELD, 1, 2}, | |
2320 | {UNUM_CURRENCY_FIELD, 2, 9}, | |
2321 | {0, -1, 0}}; | |
2322 | verifyAffixesAndPaddingInt32( | |
2323 | "1. Dollar", | |
2324 | aap, | |
2325 | 1, | |
2326 | vf, | |
2327 | rules.getAlias(), | |
2328 | expectedAttributes); | |
2329 | } | |
2330 | { | |
2331 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2332 | {UNUM_SIGN_FIELD, 0, 1}, | |
2333 | {UNUM_INTEGER_FIELD, 1, 2}, | |
2334 | {UNUM_DECIMAL_SEPARATOR_FIELD, 2, 3}, | |
2335 | {UNUM_CURRENCY_FIELD, 3, 10}, | |
2336 | {0, -1, 0}}; | |
2337 | verifyAffixesAndPaddingInt32( | |
2338 | "-1. Dollar", | |
2339 | aap, | |
2340 | -1, | |
2341 | vf, | |
2342 | rules.getAlias(), | |
2343 | expectedAttributes); | |
2344 | } | |
2345 | precision.fMin.setFracDigitCount(2); | |
2346 | { | |
2347 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2348 | {UNUM_INTEGER_FIELD, 0, 1}, | |
2349 | {UNUM_DECIMAL_SEPARATOR_FIELD, 1, 2}, | |
2350 | {UNUM_FRACTION_FIELD, 2, 4}, | |
2351 | {UNUM_CURRENCY_FIELD, 4, 12}, | |
2352 | {0, -1, 0}}; | |
2353 | verifyAffixesAndPaddingInt32( | |
2354 | "1.00 Dollars", | |
2355 | aap, | |
2356 | 1, | |
2357 | vf, | |
2358 | rules.getAlias(), | |
2359 | expectedAttributes); | |
2360 | } | |
2361 | } | |
2362 | ||
2363 | void NumberFormat2Test::TestPluralsAndRounding() { | |
2364 | UErrorCode status = U_ZERO_ERROR; | |
2365 | DecimalFormatSymbols symbols("en", status); | |
2366 | if (!assertSuccess("", status)) { | |
2367 | return; | |
2368 | } | |
2369 | DigitFormatter formatter(symbols); | |
2370 | DigitGrouping grouping; | |
2371 | FixedPrecision precision; | |
2372 | precision.fSignificant.setMax(3); | |
2373 | DigitFormatterOptions options; | |
2374 | ValueFormatter vf; | |
2375 | vf.prepareFixedDecimalFormatting( | |
2376 | formatter, | |
2377 | grouping, | |
2378 | precision, | |
2379 | options); | |
2380 | DigitList digits; | |
2381 | DigitAffixesAndPadding aap; | |
2382 | // Set up for plural currencies. | |
2383 | aap.fNegativePrefix.append("-", UNUM_SIGN_FIELD); | |
2384 | { | |
2385 | PluralAffix part; | |
2386 | part.setVariant("one", " Dollar", status); | |
2387 | part.setVariant("other", " Dollars", status); | |
2388 | aap.fPositiveSuffix.append(part, UNUM_CURRENCY_FIELD, status); | |
2389 | } | |
2390 | aap.fNegativeSuffix = aap.fPositiveSuffix; | |
2391 | aap.fWidth = 14; | |
2392 | LocalPointer<PluralRules> rules(PluralRules::forLocale("en", status)); | |
2393 | if (!assertSuccess("", status)) { | |
2394 | return; | |
2395 | } | |
2396 | { | |
2397 | digits.set(0.999); | |
2398 | verifyAffixesAndPadding( | |
2399 | "*0.999 Dollars", | |
2400 | aap, | |
2401 | digits, | |
2402 | vf, | |
2403 | rules.getAlias(), | |
2404 | NULL); | |
2405 | } | |
2406 | { | |
2407 | digits.set(0.9996); | |
2408 | verifyAffixesAndPadding( | |
2409 | "******1 Dollar", | |
2410 | aap, | |
2411 | digits, | |
2412 | vf, | |
2413 | rules.getAlias(), | |
2414 | NULL); | |
2415 | } | |
2416 | { | |
2417 | digits.set(1.004); | |
2418 | verifyAffixesAndPadding( | |
2419 | "******1 Dollar", | |
2420 | aap, | |
2421 | digits, | |
2422 | vf, | |
2423 | rules.getAlias(), | |
2424 | NULL); | |
2425 | } | |
2426 | precision.fSignificant.setMin(2); | |
2427 | { | |
2428 | digits.set(0.9996); | |
2429 | verifyAffixesAndPadding( | |
2430 | "***1.0 Dollars", | |
2431 | aap, | |
2432 | digits, | |
2433 | vf, | |
2434 | rules.getAlias(), | |
2435 | NULL); | |
2436 | } | |
2437 | { | |
2438 | digits.set(1.004); | |
2439 | verifyAffixesAndPadding( | |
2440 | "***1.0 Dollars", | |
2441 | aap, | |
2442 | digits, | |
2443 | vf, | |
2444 | rules.getAlias(), | |
2445 | NULL); | |
2446 | } | |
2447 | precision.fSignificant.setMin(0); | |
2448 | { | |
2449 | digits.set(-79.214); | |
2450 | verifyAffixesAndPadding( | |
2451 | "*-79.2 Dollars", | |
2452 | aap, | |
2453 | digits, | |
2454 | vf, | |
2455 | rules.getAlias(), | |
2456 | NULL); | |
2457 | } | |
2458 | // No more sig digits just max fractions | |
2459 | precision.fSignificant.setMax(0); | |
2460 | precision.fMax.setFracDigitCount(4); | |
2461 | { | |
2462 | digits.set(79.213562); | |
2463 | verifyAffixesAndPadding( | |
2464 | "79.2136 Dollars", | |
2465 | aap, | |
2466 | digits, | |
2467 | vf, | |
2468 | rules.getAlias(), | |
2469 | NULL); | |
2470 | } | |
2471 | ||
2472 | } | |
2473 | ||
2474 | ||
2475 | void NumberFormat2Test::TestPluralsAndRoundingScientific() { | |
2476 | UErrorCode status = U_ZERO_ERROR; | |
2477 | DecimalFormatSymbols symbols("en", status); | |
2478 | if (!assertSuccess("", status)) { | |
2479 | return; | |
2480 | } | |
2481 | DigitFormatter formatter(symbols); | |
2482 | ScientificPrecision precision; | |
2483 | precision.fMantissa.fSignificant.setMax(4); | |
2484 | SciFormatterOptions options; | |
2485 | ValueFormatter vf; | |
2486 | vf.prepareScientificFormatting( | |
2487 | formatter, | |
2488 | precision, | |
2489 | options); | |
2490 | DigitList digits; | |
2491 | DigitAffixesAndPadding aap; | |
2492 | aap.fNegativePrefix.append("-", UNUM_SIGN_FIELD); | |
2493 | { | |
2494 | PluralAffix part; | |
2495 | part.setVariant("one", " Meter", status); | |
2496 | part.setVariant("other", " Meters", status); | |
2497 | aap.fPositiveSuffix.append(part, UNUM_FIELD_COUNT, status); | |
2498 | } | |
2499 | aap.fNegativeSuffix = aap.fPositiveSuffix; | |
2500 | LocalPointer<PluralRules> rules(PluralRules::forLocale("en", status)); | |
2501 | if (!assertSuccess("", status)) { | |
2502 | return; | |
2503 | } | |
2504 | { | |
2505 | digits.set(0.99996); | |
2506 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2507 | {UNUM_INTEGER_FIELD, 0, 1}, | |
2508 | {UNUM_EXPONENT_SYMBOL_FIELD, 1, 2}, | |
2509 | {UNUM_EXPONENT_FIELD, 2, 3}, | |
2510 | {0, -1, 0}}; | |
2511 | verifyAffixesAndPadding( | |
2512 | "1E0 Meters", | |
2513 | aap, | |
2514 | digits, | |
2515 | vf, | |
2516 | rules.getAlias(), | |
2517 | expectedAttributes); | |
2518 | } | |
2519 | options.fMantissa.fAlwaysShowDecimal = TRUE; | |
2520 | { | |
2521 | digits.set(0.99996); | |
2522 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2523 | {UNUM_INTEGER_FIELD, 0, 1}, | |
2524 | {UNUM_DECIMAL_SEPARATOR_FIELD, 1, 2}, | |
2525 | {UNUM_EXPONENT_SYMBOL_FIELD, 2, 3}, | |
2526 | {UNUM_EXPONENT_FIELD, 3, 4}, | |
2527 | {0, -1, 0}}; | |
2528 | verifyAffixesAndPadding( | |
2529 | "1.E0 Meters", | |
2530 | aap, | |
2531 | digits, | |
2532 | vf, | |
2533 | rules.getAlias(), | |
2534 | expectedAttributes); | |
2535 | } | |
2536 | { | |
f3c0d7a5 | 2537 | digits.set(-299792458.0); |
2ca993e8 A |
2538 | NumberFormat2Test_Attributes expectedAttributes[] = { |
2539 | {UNUM_SIGN_FIELD, 0, 1}, | |
2540 | {UNUM_INTEGER_FIELD, 1, 2}, | |
2541 | {UNUM_DECIMAL_SEPARATOR_FIELD, 2, 3}, | |
2542 | {UNUM_FRACTION_FIELD, 3, 6}, | |
2543 | {UNUM_EXPONENT_SYMBOL_FIELD, 6, 7}, | |
2544 | {UNUM_EXPONENT_FIELD, 7, 8}, | |
2545 | {0, -1, 0}}; | |
2546 | verifyAffixesAndPadding( | |
2547 | "-2.998E8 Meters", | |
2548 | aap, | |
2549 | digits, | |
2550 | vf, | |
2551 | rules.getAlias(), | |
2552 | expectedAttributes); | |
2553 | } | |
2554 | precision.fMantissa.fSignificant.setMin(4); | |
2555 | options.fExponent.fAlwaysShowSign = TRUE; | |
2556 | precision.fMinExponentDigits = 3; | |
2557 | { | |
f3c0d7a5 | 2558 | digits.set(3.0); |
2ca993e8 A |
2559 | NumberFormat2Test_Attributes expectedAttributes[] = { |
2560 | {UNUM_INTEGER_FIELD, 0, 1}, | |
2561 | {UNUM_DECIMAL_SEPARATOR_FIELD, 1, 2}, | |
2562 | {UNUM_FRACTION_FIELD, 2, 5}, | |
2563 | {UNUM_EXPONENT_SYMBOL_FIELD, 5, 6}, | |
2564 | {UNUM_EXPONENT_SIGN_FIELD, 6, 7}, | |
2565 | {UNUM_EXPONENT_FIELD, 7, 10}, | |
2566 | {0, -1, 0}}; | |
2567 | verifyAffixesAndPadding( | |
2568 | "3.000E+000 Meters", | |
2569 | aap, | |
2570 | digits, | |
2571 | vf, | |
2572 | rules.getAlias(), | |
2573 | expectedAttributes); | |
2574 | } | |
2575 | precision.fMantissa.fMax.setIntDigitCount(3); | |
2576 | { | |
2577 | digits.set(0.00025001); | |
2578 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2579 | {UNUM_INTEGER_FIELD, 0, 3}, | |
2580 | {UNUM_DECIMAL_SEPARATOR_FIELD, 3, 4}, | |
2581 | {UNUM_FRACTION_FIELD, 4, 5}, | |
2582 | {UNUM_EXPONENT_SYMBOL_FIELD, 5, 6}, | |
2583 | {UNUM_EXPONENT_SIGN_FIELD, 6, 7}, | |
2584 | {UNUM_EXPONENT_FIELD, 7, 10}, | |
2585 | {0, -1, 0}}; | |
2586 | verifyAffixesAndPadding( | |
2587 | "250.0E-006 Meters", | |
2588 | aap, | |
2589 | digits, | |
2590 | vf, | |
2591 | rules.getAlias(), | |
2592 | expectedAttributes); | |
2593 | } | |
2594 | { | |
2595 | digits.set(0.0000025001); | |
2596 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2597 | {UNUM_INTEGER_FIELD, 0, 1}, | |
2598 | {UNUM_DECIMAL_SEPARATOR_FIELD, 1, 2}, | |
2599 | {UNUM_FRACTION_FIELD, 2, 5}, | |
2600 | {UNUM_EXPONENT_SYMBOL_FIELD, 5, 6}, | |
2601 | {UNUM_EXPONENT_SIGN_FIELD, 6, 7}, | |
2602 | {UNUM_EXPONENT_FIELD, 7, 10}, | |
2603 | {0, -1, 0}}; | |
2604 | verifyAffixesAndPadding( | |
2605 | "2.500E-006 Meters", | |
2606 | aap, | |
2607 | digits, | |
2608 | vf, | |
2609 | rules.getAlias(), | |
2610 | expectedAttributes); | |
2611 | } | |
2612 | precision.fMantissa.fMax.setFracDigitCount(1); | |
2613 | { | |
2614 | digits.set(0.0000025499); | |
2615 | NumberFormat2Test_Attributes expectedAttributes[] = { | |
2616 | {UNUM_INTEGER_FIELD, 0, 1}, | |
2617 | {UNUM_DECIMAL_SEPARATOR_FIELD, 1, 2}, | |
2618 | {UNUM_FRACTION_FIELD, 2, 3}, | |
2619 | {UNUM_EXPONENT_SYMBOL_FIELD, 3, 4}, | |
2620 | {UNUM_EXPONENT_SIGN_FIELD, 4, 5}, | |
2621 | {UNUM_EXPONENT_FIELD, 5, 8}, | |
2622 | {0, -1, 0}}; | |
2623 | verifyAffixesAndPadding( | |
2624 | "2.5E-006 Meters", | |
2625 | aap, | |
2626 | digits, | |
2627 | vf, | |
2628 | rules.getAlias(), | |
2629 | expectedAttributes); | |
2630 | } | |
2631 | precision.fMantissa.fMax.setIntDigitCount(1); | |
2632 | precision.fMantissa.fMax.setFracDigitCount(2); | |
2633 | { | |
f3c0d7a5 | 2634 | digits.set((int32_t)299792458); |
2ca993e8 A |
2635 | verifyAffixesAndPadding( |
2636 | "3.00E+008 Meters", | |
2637 | aap, | |
2638 | digits, | |
2639 | vf, | |
2640 | rules.getAlias(), | |
2641 | NULL); | |
2642 | } | |
2643 | // clear significant digits | |
2644 | precision.fMantissa.fSignificant.setMin(0); | |
2645 | precision.fMantissa.fSignificant.setMax(0); | |
2646 | ||
2647 | // set int and fraction digits | |
2648 | precision.fMantissa.fMin.setFracDigitCount(2); | |
2649 | precision.fMantissa.fMax.setFracDigitCount(4); | |
2650 | precision.fMantissa.fMin.setIntDigitCount(2); | |
2651 | precision.fMantissa.fMax.setIntDigitCount(3); | |
2652 | { | |
2653 | digits.set(-0.0000025300001); | |
2654 | verifyAffixesAndPadding( | |
2655 | "-253.00E-008 Meters", | |
2656 | aap, | |
2657 | digits, | |
2658 | vf, | |
2659 | rules.getAlias(), | |
2660 | NULL); | |
2661 | } | |
2662 | { | |
2663 | digits.set(-0.0000025300006); | |
2664 | verifyAffixesAndPadding( | |
2665 | "-253.0001E-008 Meters", | |
2666 | aap, | |
2667 | digits, | |
2668 | vf, | |
2669 | rules.getAlias(), | |
2670 | NULL); | |
2671 | } | |
2672 | { | |
2673 | digits.set(-0.000025300006); | |
2674 | verifyAffixesAndPadding( | |
2675 | "-25.30E-006 Meters", | |
2676 | aap, | |
2677 | digits, | |
2678 | vf, | |
2679 | rules.getAlias(), | |
2680 | NULL); | |
2681 | } | |
2682 | } | |
2683 | ||
2684 | ||
2685 | void NumberFormat2Test::TestRoundingIncrement() { | |
2686 | UErrorCode status = U_ZERO_ERROR; | |
2687 | DecimalFormatSymbols symbols("en", status); | |
2688 | if (U_FAILURE(status)) { | |
2689 | dataerrln("Error creating DecimalFormatSymbols - %s", u_errorName(status)); | |
2690 | return; | |
2691 | } | |
2692 | DigitFormatter formatter(symbols); | |
2693 | ScientificPrecision precision; | |
2694 | SciFormatterOptions options; | |
2695 | precision.fMantissa.fRoundingIncrement.set(0.25); | |
2696 | precision.fMantissa.fSignificant.setMax(4); | |
2697 | DigitGrouping grouping; | |
2698 | ValueFormatter vf; | |
2699 | ||
2700 | // fixed | |
2701 | vf.prepareFixedDecimalFormatting( | |
2702 | formatter, | |
2703 | grouping, | |
2704 | precision.fMantissa, | |
2705 | options.fMantissa); | |
2706 | DigitList digits; | |
2707 | DigitAffixesAndPadding aap; | |
2708 | aap.fNegativePrefix.append("-", UNUM_SIGN_FIELD); | |
2709 | { | |
2710 | digits.set(3.7); | |
2711 | verifyAffixesAndPadding( | |
2712 | "3.75", | |
2713 | aap, | |
2714 | digits, | |
2715 | vf, | |
2716 | NULL, NULL); | |
2717 | } | |
2718 | { | |
2719 | digits.set(-7.4); | |
2720 | verifyAffixesAndPadding( | |
2721 | "-7.5", | |
2722 | aap, | |
2723 | digits, | |
2724 | vf, | |
2725 | NULL, NULL); | |
2726 | } | |
2727 | { | |
2728 | digits.set(99.8); | |
2729 | verifyAffixesAndPadding( | |
2730 | "99.75", | |
2731 | aap, | |
2732 | digits, | |
2733 | vf, | |
2734 | NULL, NULL); | |
2735 | } | |
2736 | precision.fMantissa.fMin.setFracDigitCount(2); | |
2737 | { | |
2738 | digits.set(99.1); | |
2739 | verifyAffixesAndPadding( | |
2740 | "99.00", | |
2741 | aap, | |
2742 | digits, | |
2743 | vf, | |
2744 | NULL, NULL); | |
2745 | } | |
2746 | { | |
2747 | digits.set(-639.65); | |
2748 | verifyAffixesAndPadding( | |
2749 | "-639.80", | |
2750 | aap, | |
2751 | digits, | |
2752 | vf, | |
2753 | NULL, NULL); | |
2754 | } | |
2755 | ||
2756 | precision.fMantissa.fMin.setIntDigitCount(2); | |
2757 | // Scientific notation | |
2758 | vf.prepareScientificFormatting( | |
2759 | formatter, | |
2760 | precision, | |
2761 | options); | |
2762 | { | |
2763 | digits.set(-6396.5); | |
2764 | verifyAffixesAndPadding( | |
2765 | "-64.00E2", | |
2766 | aap, | |
2767 | digits, | |
2768 | vf, | |
2769 | NULL, NULL); | |
2770 | } | |
2771 | { | |
2772 | digits.set(-0.00092374); | |
2773 | verifyAffixesAndPadding( | |
2774 | "-92.25E-5", | |
2775 | aap, | |
2776 | digits, | |
2777 | vf, | |
2778 | NULL, NULL); | |
2779 | } | |
2780 | precision.fMantissa.fMax.setIntDigitCount(3); | |
2781 | { | |
2782 | digits.set(-0.00092374); | |
2783 | verifyAffixesAndPadding( | |
2784 | "-923.80E-6", | |
2785 | aap, | |
2786 | digits, | |
2787 | vf, | |
2788 | NULL, NULL); | |
2789 | } | |
2790 | } | |
2791 | ||
2792 | void NumberFormat2Test::TestToPatternScientific11648() { | |
2793 | /* | |
2794 | UErrorCode status = U_ZERO_ERROR; | |
2795 | Locale en("en"); | |
2796 | DecimalFormat2 fmt(en, "0.00", status); | |
2797 | fmt.setScientificNotation(TRUE); | |
2798 | UnicodeString pattern; | |
2799 | // Fails, produces "0.00E" | |
2800 | assertEquals("", "0.00E0", fmt.toPattern(pattern)); | |
2801 | DecimalFormat fmt2(pattern, status); | |
2802 | // Fails, bad pattern. | |
2803 | assertSuccess("", status); | |
2804 | */ | |
2805 | } | |
2806 | ||
2807 | void NumberFormat2Test::verifyAffixesAndPadding( | |
2808 | const UnicodeString &expected, | |
2809 | const DigitAffixesAndPadding &aaf, | |
2810 | DigitList &digits, | |
2811 | const ValueFormatter &vf, | |
2812 | const PluralRules *optPluralRules, | |
2813 | const NumberFormat2Test_Attributes *expectedAttributes) { | |
2814 | UnicodeString appendTo; | |
2815 | NumberFormat2Test_FieldPositionHandler handler; | |
2816 | UErrorCode status = U_ZERO_ERROR; | |
2817 | assertEquals( | |
2818 | "", | |
2819 | expected, | |
2820 | aaf.format( | |
2821 | digits, | |
2822 | vf, | |
2823 | handler, | |
2824 | optPluralRules, | |
2825 | appendTo, | |
2826 | status)); | |
2827 | if (!assertSuccess("", status)) { | |
2828 | return; | |
2829 | } | |
2830 | if (expectedAttributes != NULL) { | |
2831 | verifyAttributes(expectedAttributes, handler.attributes); | |
2832 | } | |
2833 | } | |
2834 | ||
2835 | void NumberFormat2Test::verifyAffixesAndPaddingInt32( | |
2836 | const UnicodeString &expected, | |
2837 | const DigitAffixesAndPadding &aaf, | |
2838 | int32_t value, | |
2839 | const ValueFormatter &vf, | |
2840 | const PluralRules *optPluralRules, | |
2841 | const NumberFormat2Test_Attributes *expectedAttributes) { | |
2842 | UnicodeString appendTo; | |
2843 | NumberFormat2Test_FieldPositionHandler handler; | |
2844 | UErrorCode status = U_ZERO_ERROR; | |
2845 | assertEquals( | |
2846 | "", | |
2847 | expected, | |
2848 | aaf.formatInt32( | |
2849 | value, | |
2850 | vf, | |
2851 | handler, | |
2852 | optPluralRules, | |
2853 | appendTo, | |
2854 | status)); | |
2855 | if (!assertSuccess("", status)) { | |
2856 | return; | |
2857 | } | |
2858 | if (expectedAttributes != NULL) { | |
2859 | verifyAttributes(expectedAttributes, handler.attributes); | |
2860 | } | |
2861 | DigitList digits; | |
2862 | digits.set(value); | |
2863 | verifyAffixesAndPadding( | |
2864 | expected, aaf, digits, vf, optPluralRules, expectedAttributes); | |
2865 | } | |
2866 | ||
2867 | void NumberFormat2Test::verifyAffix( | |
2868 | const UnicodeString &expected, | |
2869 | const DigitAffix &affix, | |
2870 | const NumberFormat2Test_Attributes *expectedAttributes) { | |
2871 | UnicodeString appendTo; | |
2872 | NumberFormat2Test_FieldPositionHandler handler; | |
2873 | assertEquals( | |
2874 | "", | |
2875 | expected.unescape(), | |
2876 | affix.format(handler, appendTo)); | |
2877 | if (expectedAttributes != NULL) { | |
2878 | verifyAttributes(expectedAttributes, handler.attributes); | |
2879 | } | |
2880 | } | |
2881 | ||
2882 | // Right now only works for positive values. | |
2883 | void NumberFormat2Test::verifyDigitList( | |
2884 | const UnicodeString &expected, | |
2885 | const DigitList &digits) { | |
2886 | DigitFormatter formatter; | |
2887 | DigitGrouping grouping; | |
2888 | VisibleDigits visibleDigits; | |
2889 | FixedPrecision precision; | |
2890 | precision.fMin.setIntDigitCount(0); | |
2891 | DigitFormatterOptions options; | |
2892 | UErrorCode status = U_ZERO_ERROR; | |
2893 | DigitList dlCopy(digits); | |
2894 | precision.initVisibleDigits( | |
2895 | dlCopy, visibleDigits, status); | |
2896 | if (!assertSuccess("", status)) { | |
2897 | return; | |
2898 | } | |
2899 | verifyDigitFormatter( | |
2900 | expected, | |
2901 | formatter, | |
2902 | visibleDigits, | |
2903 | grouping, | |
2904 | options, | |
2905 | NULL); | |
2906 | } | |
2907 | ||
2908 | void NumberFormat2Test::verifyVisibleDigits( | |
2909 | const UnicodeString &expected, | |
2910 | UBool bNegative, | |
2911 | const VisibleDigits &digits) { | |
2912 | DigitFormatter formatter; | |
2913 | DigitGrouping grouping; | |
2914 | DigitFormatterOptions options; | |
2915 | verifyDigitFormatter( | |
2916 | expected, | |
2917 | formatter, | |
2918 | digits, | |
2919 | grouping, | |
2920 | options, | |
2921 | NULL); | |
2922 | if (digits.isNegative() != bNegative) { | |
2923 | errln(expected + ": Wrong sign."); | |
2924 | } | |
2925 | if (digits.isNaN() || digits.isInfinite()) { | |
2926 | errln(expected + ": Require real value."); | |
2927 | } | |
2928 | } | |
2929 | ||
2930 | void NumberFormat2Test::verifyVisibleDigitsWithExponent( | |
2931 | const UnicodeString &expected, | |
2932 | UBool bNegative, | |
2933 | const VisibleDigitsWithExponent &digits) { | |
2934 | DigitFormatter formatter; | |
2935 | SciFormatterOptions options; | |
2936 | verifySciFormatter( | |
2937 | expected, | |
2938 | formatter, | |
2939 | digits, | |
2940 | options, | |
2941 | NULL); | |
2942 | if (digits.isNegative() != bNegative) { | |
2943 | errln(expected + ": Wrong sign."); | |
2944 | } | |
2945 | if (digits.isNaN() || digits.isInfinite()) { | |
2946 | errln(expected + ": Require real value."); | |
2947 | } | |
2948 | } | |
2949 | ||
2950 | void NumberFormat2Test::verifySciFormatter( | |
2951 | const UnicodeString &expected, | |
2952 | const DigitFormatter &formatter, | |
2953 | const VisibleDigitsWithExponent &digits, | |
2954 | const SciFormatterOptions &options, | |
2955 | const NumberFormat2Test_Attributes *expectedAttributes) { | |
2956 | assertEquals( | |
2957 | "", | |
2958 | expected.countChar32(), | |
2959 | formatter.countChar32(digits, options)); | |
2960 | UnicodeString appendTo; | |
2961 | NumberFormat2Test_FieldPositionHandler handler; | |
2962 | assertEquals( | |
2963 | "", | |
2964 | expected, | |
2965 | formatter.format( | |
2966 | digits, | |
2967 | options, | |
2968 | handler, | |
2969 | appendTo)); | |
2970 | if (expectedAttributes != NULL) { | |
2971 | verifyAttributes(expectedAttributes, handler.attributes); | |
2972 | } | |
2973 | } | |
2974 | ||
2975 | void NumberFormat2Test::verifyPositiveIntDigitFormatter( | |
2976 | const UnicodeString &expected, | |
2977 | const DigitFormatter &formatter, | |
2978 | int32_t value, | |
2979 | int32_t minDigits, | |
2980 | int32_t maxDigits, | |
2981 | const NumberFormat2Test_Attributes *expectedAttributes) { | |
2982 | IntDigitCountRange range(minDigits, maxDigits); | |
2983 | UnicodeString appendTo; | |
2984 | NumberFormat2Test_FieldPositionHandler handler; | |
2985 | assertEquals( | |
2986 | "", | |
2987 | expected, | |
2988 | formatter.formatPositiveInt32( | |
2989 | value, | |
2990 | range, | |
2991 | handler, | |
2992 | appendTo)); | |
2993 | if (expectedAttributes != NULL) { | |
2994 | verifyAttributes(expectedAttributes, handler.attributes); | |
2995 | } | |
2996 | } | |
2997 | ||
2998 | void NumberFormat2Test::verifyDigitFormatter( | |
2999 | const UnicodeString &expected, | |
3000 | const DigitFormatter &formatter, | |
3001 | const VisibleDigits &digits, | |
3002 | const DigitGrouping &grouping, | |
3003 | const DigitFormatterOptions &options, | |
3004 | const NumberFormat2Test_Attributes *expectedAttributes) { | |
3005 | assertEquals( | |
3006 | "", | |
3007 | expected.countChar32(), | |
3008 | formatter.countChar32(digits, grouping, options)); | |
3009 | UnicodeString appendTo; | |
3010 | NumberFormat2Test_FieldPositionHandler handler; | |
3011 | assertEquals( | |
3012 | "", | |
3013 | expected, | |
3014 | formatter.format( | |
3015 | digits, | |
3016 | grouping, | |
3017 | options, | |
3018 | handler, | |
3019 | appendTo)); | |
3020 | if (expectedAttributes != NULL) { | |
3021 | verifyAttributes(expectedAttributes, handler.attributes); | |
3022 | } | |
3023 | } | |
3024 | ||
3025 | void NumberFormat2Test::verifySmallIntFormatter( | |
3026 | const UnicodeString &expected, | |
3027 | int32_t positiveValue, | |
3028 | int32_t minDigits, | |
3029 | int32_t maxDigits) { | |
3030 | IntDigitCountRange range(minDigits, maxDigits); | |
3031 | if (!SmallIntFormatter::canFormat(positiveValue, range)) { | |
3032 | UnicodeString actual; | |
3033 | assertEquals("", expected, actual); | |
3034 | return; | |
3035 | } | |
3036 | UnicodeString actual; | |
3037 | assertEquals("", expected, SmallIntFormatter::format(positiveValue, range, actual)); | |
3038 | } | |
3039 | ||
3040 | void NumberFormat2Test::verifyAttributes( | |
3041 | const NumberFormat2Test_Attributes *expected, | |
3042 | const NumberFormat2Test_Attributes *actual) { | |
3043 | int32_t idx = 0; | |
3044 | while (expected[idx].spos != -1 && actual[idx].spos != -1) { | |
3045 | assertEquals("id", expected[idx].id, actual[idx].id); | |
3046 | assertEquals("spos", expected[idx].spos, actual[idx].spos); | |
3047 | assertEquals("epos", expected[idx].epos, actual[idx].epos); | |
3048 | ++idx; | |
3049 | } | |
3050 | assertEquals( | |
3051 | "expected and actual not same length", | |
3052 | expected[idx].spos, | |
3053 | actual[idx].spos); | |
3054 | } | |
3055 | ||
3056 | void NumberFormat2Test::verifyIntValue( | |
3057 | int64_t expected, const VisibleDigits &digits) { | |
3058 | double unusedSource; | |
3059 | int64_t intValue; | |
3060 | int64_t unusedF; | |
3061 | int64_t unusedT; | |
3062 | int32_t unusedV; | |
3063 | UBool unusedHasIntValue; | |
3064 | digits.getFixedDecimal( | |
3065 | unusedSource, intValue, unusedF, | |
3066 | unusedT, unusedV, unusedHasIntValue); | |
3067 | assertEquals("", expected, intValue); | |
3068 | } | |
3069 | ||
3070 | void NumberFormat2Test::verifySource( | |
3071 | double expected, const VisibleDigits &digits) { | |
3072 | double source; | |
3073 | int64_t unusedIntValue; | |
3074 | int64_t unusedF; | |
3075 | int64_t unusedT; | |
3076 | int32_t unusedV; | |
3077 | UBool unusedHasIntValue; | |
3078 | digits.getFixedDecimal( | |
3079 | source, unusedIntValue, unusedF, | |
3080 | unusedT, unusedV, unusedHasIntValue); | |
3081 | if (expected != source) { | |
3082 | errln("Expected %f, got %f instead", expected, source); | |
3083 | } | |
3084 | } | |
3085 | ||
3086 | extern IntlTest *createNumberFormat2Test() { | |
3087 | return new NumberFormat2Test(); | |
3088 | } | |
3089 | ||
3090 | #endif /* !UCONFIG_NO_FORMATTING */ |