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