1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2015, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
9 * File NUMBERFORMAT2TEST.CPP
11 *******************************************************************************
13 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_FORMATTING
19 #include "unicode/localpointer.h"
20 #include "unicode/plurrule.h"
22 #include "affixpatternparser.h"
24 #include "datadrivennumberformattestsuite.h"
25 #include "decimalformatpattern.h"
26 #include "digitaffixesandpadding.h"
27 #include "digitformatter.h"
28 #include "digitgrouping.h"
29 #include "digitinterval.h"
32 #include "plurrule_impl.h"
33 #include "precision.h"
34 #include "significantdigitinterval.h"
35 #include "smallintformatter.h"
37 #include "valueformatter.h"
38 #include "visibledigits.h"
40 struct NumberFormat2Test_Attributes
{
46 class NumberFormat2Test_FieldPositionHandler
: public FieldPositionHandler
{
48 NumberFormat2Test_Attributes attributes
[100];
54 NumberFormat2Test_FieldPositionHandler() : count(0), bRecording(TRUE
) { attributes
[0].spos
= -1; }
55 NumberFormat2Test_FieldPositionHandler(UBool recording
) : count(0), bRecording(recording
) { attributes
[0].spos
= -1; }
56 virtual ~NumberFormat2Test_FieldPositionHandler();
57 virtual void addAttribute(int32_t id
, int32_t start
, int32_t limit
);
58 virtual void shiftLast(int32_t delta
);
59 virtual UBool
isRecording(void) const;
62 NumberFormat2Test_FieldPositionHandler::~NumberFormat2Test_FieldPositionHandler() {
65 void NumberFormat2Test_FieldPositionHandler::addAttribute(
66 int32_t id
, int32_t start
, int32_t limit
) {
67 if (count
== UPRV_LENGTHOF(attributes
) - 1) {
70 attributes
[count
].id
= id
;
71 attributes
[count
].spos
= start
;
72 attributes
[count
].epos
= limit
;
74 attributes
[count
].spos
= -1;
77 void NumberFormat2Test_FieldPositionHandler::shiftLast(int32_t /* delta */) {
80 UBool
NumberFormat2Test_FieldPositionHandler::isRecording() const {
85 class NumberFormat2Test
: public IntlTest
{
87 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=0);
90 void TestConvertScientificNotation();
91 void TestLowerUpperExponent();
93 void TestRoundingIncrement();
94 void TestDigitInterval();
95 void TestGroupingUsed();
97 void TestBenchmark2();
98 void TestSmallIntFormatter();
99 void TestPositiveIntDigitFormatter();
100 void TestDigitListInterval();
101 void TestLargeIntValue();
102 void TestIntInitVisibleDigits();
103 void TestIntInitVisibleDigitsToDigitList();
104 void TestDoubleInitVisibleDigits();
105 void TestDoubleInitVisibleDigitsToDigitList();
106 void TestDigitListInitVisibleDigits();
107 void TestSpecialInitVisibleDigits();
108 void TestVisibleDigitsWithExponent();
109 void TestDigitAffixesAndPadding();
110 void TestPluralsAndRounding();
111 void TestPluralsAndRoundingScientific();
112 void TestValueFormatterIsFastFormattable();
113 void TestCurrencyAffixInfo();
114 void TestAffixPattern();
115 void TestAffixPatternAppend();
116 void TestAffixPatternAppendAjoiningLiterals();
117 void TestAffixPatternDoubleQuote();
118 void TestAffixPatternParser();
119 void TestPluralAffix();
120 void TestDigitAffix();
121 void TestDigitFormatterDefaultCtor();
122 void TestDigitFormatterMonetary();
123 void TestDigitFormatter();
124 void TestSciFormatterDefaultCtor();
125 void TestSciFormatter();
126 void TestToPatternScientific11648();
127 void verifyInterval(const DigitInterval
&, int32_t minInclusive
, int32_t maxExclusive
);
129 const UnicodeString
&expected
,
130 const DigitAffix
&affix
,
131 const NumberFormat2Test_Attributes
*expectedAttributes
);
132 void verifyAffixesAndPadding(
133 const UnicodeString
&expected
,
134 const DigitAffixesAndPadding
&aaf
,
136 const ValueFormatter
&vf
,
137 const PluralRules
*optPluralRules
,
138 const NumberFormat2Test_Attributes
*expectedAttributes
);
139 void verifyAffixesAndPaddingInt32(
140 const UnicodeString
&expected
,
141 const DigitAffixesAndPadding
&aaf
,
143 const ValueFormatter
&vf
,
144 const PluralRules
*optPluralRules
,
145 const NumberFormat2Test_Attributes
*expectedAttributes
);
146 void verifyDigitList(
147 const UnicodeString
&expected
,
148 const DigitList
&digits
);
149 void verifyVisibleDigits(
150 const UnicodeString
&expected
,
152 const VisibleDigits
&digits
);
153 void verifyVisibleDigitsWithExponent(
154 const UnicodeString
&expected
,
156 const VisibleDigitsWithExponent
&digits
);
157 void verifyDigitFormatter(
158 const UnicodeString
&expected
,
159 const DigitFormatter
&formatter
,
160 const VisibleDigits
&digits
,
161 const DigitGrouping
&grouping
,
162 const DigitFormatterOptions
&options
,
163 const NumberFormat2Test_Attributes
*expectedAttributes
);
164 void verifySciFormatter(
165 const UnicodeString
&expected
,
166 const DigitFormatter
&formatter
,
167 const VisibleDigitsWithExponent
&digits
,
168 const SciFormatterOptions
&options
,
169 const NumberFormat2Test_Attributes
*expectedAttributes
);
170 void verifySmallIntFormatter(
171 const UnicodeString
&expected
,
172 int32_t positiveValue
,
175 void verifyPositiveIntDigitFormatter(
176 const UnicodeString
&expected
,
177 const DigitFormatter
&formatter
,
181 const NumberFormat2Test_Attributes
*expectedAttributes
);
182 void verifyAttributes(
183 const NumberFormat2Test_Attributes
*expected
,
184 const NumberFormat2Test_Attributes
*actual
);
186 int64_t expected
, const VisibleDigits
&digits
);
188 double expected
, const VisibleDigits
&digits
);
191 void NumberFormat2Test::runIndexedTest(
192 int32_t index
, UBool exec
, const char *&name
, char *) {
194 logln("TestSuite ScientificNumberFormatterTest: ");
197 TESTCASE_AUTO(TestQuantize
);
198 TESTCASE_AUTO(TestConvertScientificNotation
);
199 TESTCASE_AUTO(TestLowerUpperExponent
);
200 TESTCASE_AUTO(TestRounding
);
201 TESTCASE_AUTO(TestRoundingIncrement
);
202 TESTCASE_AUTO(TestDigitInterval
);
203 TESTCASE_AUTO(TestGroupingUsed
);
204 TESTCASE_AUTO(TestDigitListInterval
);
205 TESTCASE_AUTO(TestDigitFormatterDefaultCtor
);
206 TESTCASE_AUTO(TestDigitFormatterMonetary
);
207 TESTCASE_AUTO(TestDigitFormatter
);
208 TESTCASE_AUTO(TestSciFormatterDefaultCtor
);
209 TESTCASE_AUTO(TestSciFormatter
);
210 TESTCASE_AUTO(TestBenchmark
);
211 TESTCASE_AUTO(TestBenchmark2
);
212 TESTCASE_AUTO(TestSmallIntFormatter
);
213 TESTCASE_AUTO(TestPositiveIntDigitFormatter
);
214 TESTCASE_AUTO(TestCurrencyAffixInfo
);
215 TESTCASE_AUTO(TestAffixPattern
);
216 TESTCASE_AUTO(TestAffixPatternAppend
);
217 TESTCASE_AUTO(TestAffixPatternAppendAjoiningLiterals
);
218 TESTCASE_AUTO(TestAffixPatternDoubleQuote
);
219 TESTCASE_AUTO(TestAffixPatternParser
);
220 TESTCASE_AUTO(TestPluralAffix
);
221 TESTCASE_AUTO(TestDigitAffix
);
222 TESTCASE_AUTO(TestValueFormatterIsFastFormattable
);
223 TESTCASE_AUTO(TestLargeIntValue
);
224 TESTCASE_AUTO(TestIntInitVisibleDigits
);
225 TESTCASE_AUTO(TestIntInitVisibleDigitsToDigitList
);
226 TESTCASE_AUTO(TestDoubleInitVisibleDigits
);
227 TESTCASE_AUTO(TestDoubleInitVisibleDigitsToDigitList
);
228 TESTCASE_AUTO(TestDigitListInitVisibleDigits
);
229 TESTCASE_AUTO(TestSpecialInitVisibleDigits
);
230 TESTCASE_AUTO(TestVisibleDigitsWithExponent
);
231 TESTCASE_AUTO(TestDigitAffixesAndPadding
);
232 TESTCASE_AUTO(TestPluralsAndRounding
);
233 TESTCASE_AUTO(TestPluralsAndRoundingScientific
);
234 TESTCASE_AUTO(TestToPatternScientific11648
);
239 void NumberFormat2Test::TestDigitInterval() {
241 DigitInterval threeInts
;
242 DigitInterval fourFrac
;
243 threeInts
.setIntDigitCount(3);
244 fourFrac
.setFracDigitCount(4);
245 verifyInterval(all
, INT32_MIN
, INT32_MAX
);
246 verifyInterval(threeInts
, INT32_MIN
, 3);
247 verifyInterval(fourFrac
, -4, INT32_MAX
);
249 DigitInterval
result(threeInts
);
250 result
.shrinkToFitWithin(fourFrac
);
251 verifyInterval(result
, -4, 3);
252 assertEquals("", 7, result
.length());
255 DigitInterval
result(threeInts
);
256 result
.expandToContain(fourFrac
);
257 verifyInterval(result
, INT32_MIN
, INT32_MAX
);
260 DigitInterval
result(threeInts
);
261 result
.setIntDigitCount(0);
262 verifyInterval(result
, INT32_MIN
, 0);
263 result
.setIntDigitCount(-1);
264 verifyInterval(result
, INT32_MIN
, INT32_MAX
);
267 DigitInterval
result(fourFrac
);
268 result
.setFracDigitCount(0);
269 verifyInterval(result
, 0, INT32_MAX
);
270 result
.setFracDigitCount(-1);
271 verifyInterval(result
, INT32_MIN
, INT32_MAX
);
274 DigitInterval result
;
275 result
.setIntDigitCount(3);
276 result
.setFracDigitCount(1);
277 result
.expandToContainDigit(0);
278 result
.expandToContainDigit(-1);
279 result
.expandToContainDigit(2);
280 verifyInterval(result
, -1, 3);
281 result
.expandToContainDigit(3);
282 verifyInterval(result
, -1, 4);
283 result
.expandToContainDigit(-2);
284 verifyInterval(result
, -2, 4);
285 result
.expandToContainDigit(15);
286 result
.expandToContainDigit(-15);
287 verifyInterval(result
, -15, 16);
290 DigitInterval result
;
291 result
.setIntDigitCount(3);
292 result
.setFracDigitCount(1);
293 assertTrue("", result
.contains(2));
294 assertTrue("", result
.contains(-1));
295 assertFalse("", result
.contains(3));
296 assertFalse("", result
.contains(-2));
300 void NumberFormat2Test::verifyInterval(
301 const DigitInterval
&interval
,
302 int32_t minInclusive
, int32_t maxExclusive
) {
303 assertEquals("", minInclusive
, interval
.getLeastSignificantInclusive());
304 assertEquals("", maxExclusive
, interval
.getMostSignificantExclusive());
305 assertEquals("", maxExclusive
, interval
.getIntDigitCount());
308 void NumberFormat2Test::TestGroupingUsed() {
310 DigitGrouping grouping
;
311 assertFalse("", grouping
.isGroupingUsed());
314 DigitGrouping grouping
;
315 grouping
.fGrouping
= 2;
316 assertTrue("", grouping
.isGroupingUsed());
320 void NumberFormat2Test::TestDigitListInterval() {
321 DigitInterval result
;
324 digitList
.set((int32_t)12345);
325 verifyInterval(digitList
.getSmallestInterval(result
), 0, 5);
328 digitList
.set(1000.00);
329 verifyInterval(digitList
.getSmallestInterval(result
), 0, 4);
332 digitList
.set(43.125);
333 verifyInterval(digitList
.getSmallestInterval(result
), -3, 2);
336 digitList
.set(.0078125);
337 verifyInterval(digitList
.getSmallestInterval(result
), -7, 0);
340 digitList
.set(1000.00);
341 digitList
.getSmallestInterval(result
);
342 result
.expandToContainDigit(3);
343 verifyInterval(result
, 0, 4);
346 digitList
.set(1000.00);
347 digitList
.getSmallestInterval(result
);
348 result
.expandToContainDigit(4);
349 verifyInterval(result
, 0, 5);
352 digitList
.set(1000.00);
353 digitList
.getSmallestInterval(result
);
354 result
.expandToContainDigit(0);
355 verifyInterval(result
, 0, 4);
358 digitList
.set(1000.00);
359 digitList
.getSmallestInterval(result
);
360 result
.expandToContainDigit(-1);
361 verifyInterval(result
, -1, 4);
364 digitList
.set(43.125);
365 digitList
.getSmallestInterval(result
);
366 result
.expandToContainDigit(1);
367 verifyInterval(result
, -3, 2);
370 digitList
.set(43.125);
371 digitList
.getSmallestInterval(result
);
372 result
.expandToContainDigit(2);
373 verifyInterval(result
, -3, 3);
376 digitList
.set(43.125);
377 digitList
.getSmallestInterval(result
);
378 result
.expandToContainDigit(-3);
379 verifyInterval(result
, -3, 2);
382 digitList
.set(43.125);
383 digitList
.getSmallestInterval(result
);
384 result
.expandToContainDigit(-4);
385 verifyInterval(result
, -4, 2);
389 void NumberFormat2Test::TestQuantize() {
391 quantity
.set(0.00168);
392 quantity
.roundAtExponent(-5);
394 UErrorCode status
= U_ZERO_ERROR
;
396 digits
.set((int32_t)1);
397 digits
.quantize(quantity
, status
);
398 verifyDigitList(".9996", digits
);
401 // round half even up
403 digits
.roundAtExponent(-5);
404 digits
.quantize(quantity
, status
);
405 verifyDigitList("1.00128", digits
);
410 digits
.roundAtExponent(-5);
411 digits
.quantize(quantity
, status
);
412 verifyDigitList(".99792", digits
);
414 assertSuccess("", status
);
417 void NumberFormat2Test::TestConvertScientificNotation() {
420 digits
.set((int32_t)186283);
421 assertEquals("", 5, digits
.toScientific(1, 1));
427 digits
.set((int32_t)186283);
428 assertEquals("", 0, digits
.toScientific(6, 1));
434 digits
.set((int32_t)186283);
435 assertEquals("", -2, digits
.toScientific(8, 1));
441 digits
.set((int32_t)43561);
442 assertEquals("", 6, digits
.toScientific(-1, 3));
448 digits
.set((int32_t)43561);
449 assertEquals("", 3, digits
.toScientific(0, 3));
455 digits
.set((int32_t)43561);
456 assertEquals("", 3, digits
.toScientific(2, 3));
462 digits
.set((int32_t)43561);
463 assertEquals("", 0, digits
.toScientific(3, 3));
469 digits
.set((int32_t)43561);
470 assertEquals("", 0, digits
.toScientific(5, 3));
476 digits
.set((int32_t)43561);
477 assertEquals("", -3, digits
.toScientific(6, 3));
483 digits
.set((int32_t)43561);
484 assertEquals("", -3, digits
.toScientific(8, 3));
490 digits
.set((int32_t)43561);
491 assertEquals("", -6, digits
.toScientific(9, 3));
498 void NumberFormat2Test::TestLowerUpperExponent() {
502 assertEquals("", -1, digits
.getLowerExponent());
503 assertEquals("", 2, digits
.getUpperExponent());
506 void NumberFormat2Test::TestRounding() {
508 uprv_decContextSetRounding(&digits
.fContext
, DEC_ROUND_CEILING
);
510 // Round at very large exponent
512 digits
.roundAtExponent(100);
514 "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", // 100 0's after 1
518 // Round at very large exponent
520 digits
.roundAtExponent(1);
522 "790", // 100 0's after 1
526 // Round at positive exponent
528 digits
.roundAtExponent(1);
529 verifyDigitList("790", digits
);
532 // Round at zero exponent
534 digits
.roundAtExponent(0);
535 verifyDigitList("789", digits
);
538 // Round at negative exponent
540 digits
.roundAtExponent(-2);
541 verifyDigitList("789.13", digits
);
544 // Round to exponent of digits.
546 digits
.roundAtExponent(-3);
547 verifyDigitList("789.123", digits
);
550 // Round at large negative exponent
552 digits
.roundAtExponent(-100);
553 verifyDigitList("789.123", digits
);
557 digits
.set(-789.123);
558 digits
.roundAtExponent(-2);
559 digits
.setPositive(TRUE
);
560 verifyDigitList("789.12", digits
);
563 // Round to 1 significant digit
565 digits
.roundAtExponent(INT32_MIN
, 1);
566 verifyDigitList("800", digits
);
569 // Round to 5 significant digit
571 digits
.roundAtExponent(INT32_MIN
, 5);
572 verifyDigitList("789.13", digits
);
575 // Round to 6 significant digit
577 digits
.roundAtExponent(INT32_MIN
, 6);
578 verifyDigitList("789.123", digits
);
583 digits
.roundAtExponent(INT32_MIN
, INT32_MAX
);
584 verifyDigitList("789.123", digits
);
587 // Rounding at -1 produces fewer than 5 significant digits
589 digits
.roundAtExponent(-1, 5);
590 verifyDigitList("789.2", digits
);
593 // Rounding at -1 produces exactly 4 significant digits
595 digits
.roundAtExponent(-1, 4);
596 verifyDigitList("789.2", digits
);
599 // Rounding at -1 produces more than 3 significant digits
601 digits
.roundAtExponent(-1, 3);
602 verifyDigitList("789", digits
);
606 digits
.round(INT32_MAX
);
607 verifyDigitList("123.456", digits
);
612 verifyDigitList("200", digits
);
615 void NumberFormat2Test::TestBenchmark() {
617 UErrorCode status = U_ZERO_ERROR;
619 DecimalFormatSymbols *sym = new DecimalFormatSymbols(en, status);
620 DecimalFormat2 fmt(en, "0.0000000", status);
621 FieldPosition fpos(FieldPostion::DONT_CARE);
622 clock_t start = clock();
623 for (int32_t i = 0; i < 100000; ++i) {
625 DecimalFormat2 fmt2("0.0000000", new DecimalFormatSymbols(*sym), perror, status);
626 // UnicodeString append;
627 // fmt.format(4.6692016, append, fpos, status);
629 errln("Took %f", (double) (clock() - start) / CLOCKS_PER_SEC);
630 assertSuccess("", status);
634 void NumberFormat2Test::TestBenchmark2() {
636 UErrorCode status = U_ZERO_ERROR;
638 DecimalFormatSymbols *sym = new DecimalFormatSymbols(en, status);
639 DecimalFormat fmt("0.0000000", sym, status);
640 FieldPosition fpos(FieldPostion::DONT_CARE);
641 clock_t start = clock();
642 for (int32_t i = 0; i < 100000; ++i) {
644 DecimalFormat fmt("0.0000000", new DecimalFormatSymbols(*sym), perror, status);
645 // UnicodeString append;
646 // fmt.format(4.6692016, append, fpos, status);
648 errln("Took %f", (double) (clock() - start) / CLOCKS_PER_SEC);
649 assertSuccess("", status);
653 void NumberFormat2Test::TestSmallIntFormatter() {
654 verifySmallIntFormatter("0", 7, 0, -2);
655 verifySmallIntFormatter("7", 7, 1, -2);
656 verifySmallIntFormatter("07", 7, 2, -2);
657 verifySmallIntFormatter("07", 7, 2, 2);
658 verifySmallIntFormatter("007", 7, 3, 4);
659 verifySmallIntFormatter("7", 7, -1, 3);
660 verifySmallIntFormatter("0", 0, -1, 3);
661 verifySmallIntFormatter("057", 57, 3, 7);
662 verifySmallIntFormatter("0057", 57, 4, 7);
663 // too many digits for small int
664 verifySmallIntFormatter("", 57, 5, 7);
665 // too many digits for small int
666 verifySmallIntFormatter("", 57, 5, 4);
667 verifySmallIntFormatter("03", 3, 2, 3);
668 verifySmallIntFormatter("32", 32, 2, 3);
669 verifySmallIntFormatter("321", 321, 2, 3);
670 verifySmallIntFormatter("219", 3219, 2, 3);
671 verifySmallIntFormatter("4095", 4095, 2, 4);
672 verifySmallIntFormatter("4095", 4095, 2, 5);
673 verifySmallIntFormatter("", 4096, 2, 5);
676 void NumberFormat2Test::TestPositiveIntDigitFormatter() {
677 DigitFormatter formatter
;
679 NumberFormat2Test_Attributes expectedAttributes
[] = {
680 {UNUM_INTEGER_FIELD
, 0, 4},
682 verifyPositiveIntDigitFormatter(
691 NumberFormat2Test_Attributes expectedAttributes
[] = {
692 {UNUM_INTEGER_FIELD
, 0, 5},
694 verifyPositiveIntDigitFormatter(
703 NumberFormat2Test_Attributes expectedAttributes
[] = {
704 {UNUM_INTEGER_FIELD
, 0, 5},
706 verifyPositiveIntDigitFormatter(
715 NumberFormat2Test_Attributes expectedAttributes
[] = {
716 {UNUM_INTEGER_FIELD
, 0, 3},
718 verifyPositiveIntDigitFormatter(
727 NumberFormat2Test_Attributes expectedAttributes
[] = {
728 {UNUM_INTEGER_FIELD
, 0, 10},
730 verifyPositiveIntDigitFormatter(
739 NumberFormat2Test_Attributes expectedAttributes
[] = {
740 {UNUM_INTEGER_FIELD
, 0, 12},
742 verifyPositiveIntDigitFormatter(
751 // Test long digit string where we have to append one
752 // character at a time.
753 NumberFormat2Test_Attributes expectedAttributes
[] = {
754 {UNUM_INTEGER_FIELD
, 0, 40},
756 verifyPositiveIntDigitFormatter(
757 "0000000000000000000000000000002147483647",
765 NumberFormat2Test_Attributes expectedAttributes
[] = {
766 {UNUM_INTEGER_FIELD
, 0, 4},
768 verifyPositiveIntDigitFormatter(
777 NumberFormat2Test_Attributes expectedAttributes
[] = {
778 {UNUM_INTEGER_FIELD
, 0, 1},
780 verifyPositiveIntDigitFormatter(
789 NumberFormat2Test_Attributes expectedAttributes
[] = {
790 {UNUM_INTEGER_FIELD
, 0, 1},
792 verifyPositiveIntDigitFormatter(
803 void NumberFormat2Test::TestDigitFormatterDefaultCtor() {
804 DigitFormatter formatter
;
805 VisibleDigits digits
;
806 FixedPrecision precision
;
807 UErrorCode status
= U_ZERO_ERROR
;
808 precision
.initVisibleDigits(246.801, digits
, status
);
809 assertSuccess("", status
);
810 DigitGrouping grouping
;
811 DigitFormatterOptions options
;
812 verifyDigitFormatter(
821 void NumberFormat2Test::TestDigitFormatterMonetary() {
822 UErrorCode status
= U_ZERO_ERROR
;
823 DecimalFormatSymbols
symbols("en", status
);
824 if (!assertSuccess("", status
)) {
828 DecimalFormatSymbols::kMonetarySeparatorSymbol
,
829 "decimal separator");
831 DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol
,
832 "grouping separator");
833 DigitFormatter
formatter(symbols
);
834 VisibleDigits visibleDigits
;
835 DigitGrouping grouping
;
836 FixedPrecision precision
;
837 precision
.initVisibleDigits(43560.02, visibleDigits
, status
);
838 if (!assertSuccess("", status
)) {
841 DigitFormatterOptions options
;
842 grouping
.fGrouping
= 3;
844 verifyDigitFormatter(
851 formatter
.setDecimalFormatSymbolsForMonetary(symbols
);
852 verifyDigitFormatter(
853 "43grouping separator560decimal separator02",
862 void NumberFormat2Test::TestDigitFormatter() {
863 UErrorCode status
= U_ZERO_ERROR
;
864 DecimalFormatSymbols
symbols("en", status
);
865 if (!assertSuccess("", status
)) {
868 DigitFormatter
formatter(symbols
);
869 DigitInterval interval
;
871 VisibleDigits visibleDigits
;
872 DigitGrouping grouping
;
873 FixedPrecision precision
;
874 precision
.initVisibleDigits((int64_t) 8192, visibleDigits
, status
);
875 if (!assertSuccess("", status
)) {
878 DigitFormatterOptions options
;
879 verifyDigitFormatter(
886 NumberFormat2Test_Attributes expectedAttributes
[] = {
887 {UNUM_INTEGER_FIELD
, 0, 4},
888 {UNUM_DECIMAL_SEPARATOR_FIELD
, 4, 5},
890 options
.fAlwaysShowDecimal
= TRUE
;
891 verifyDigitFormatter(
900 grouping
.fGrouping
= 3;
901 options
.fAlwaysShowDecimal
= FALSE
;
902 verifyDigitFormatter(
910 // turn on min grouping which will suppress grouping
911 grouping
.fMinGrouping
= 2;
912 verifyDigitFormatter(
920 // adding one more digit will enable grouping once again.
921 precision
.initVisibleDigits((int64_t) 43560, visibleDigits
, status
);
922 if (!assertSuccess("", status
)) {
925 verifyDigitFormatter(
934 DigitGrouping grouping
;
935 FixedPrecision precision
;
936 VisibleDigits visibleDigits
;
937 precision
.initVisibleDigits(
938 31415926.0078125, visibleDigits
, status
);
939 if (!assertSuccess("", status
)) {
942 DigitFormatterOptions options
;
943 verifyDigitFormatter(
951 // Turn on grouping with secondary.
952 grouping
.fGrouping
= 2;
953 grouping
.fGrouping2
= 3;
954 verifyDigitFormatter(
955 "314,159,26.0078125",
962 // Pad with zeros by widening interval.
963 precision
.fMin
.setIntDigitCount(9);
964 precision
.fMin
.setFracDigitCount(10);
965 precision
.initVisibleDigits(
966 31415926.0078125, visibleDigits
, status
);
967 if (!assertSuccess("", status
)) {
970 NumberFormat2Test_Attributes expectedAttributes
[] = {
971 {UNUM_GROUPING_SEPARATOR_FIELD
, 1, 2},
972 {UNUM_GROUPING_SEPARATOR_FIELD
, 5, 6},
973 {UNUM_GROUPING_SEPARATOR_FIELD
, 9, 10},
974 {UNUM_INTEGER_FIELD
, 0, 12},
975 {UNUM_DECIMAL_SEPARATOR_FIELD
, 12, 13},
976 {UNUM_FRACTION_FIELD
, 13, 23},
978 verifyDigitFormatter(
979 "0,314,159,26.0078125000",
987 DigitGrouping grouping
;
988 FixedPrecision precision
;
989 VisibleDigits visibleDigits
;
990 DigitFormatterOptions options
;
991 precision
.fMax
.setIntDigitCount(0);
992 precision
.fMax
.setFracDigitCount(0);
993 precision
.initVisibleDigits(
994 3125.0, visibleDigits
, status
);
995 if (!assertSuccess("", status
)) {
998 NumberFormat2Test_Attributes expectedAttributes
[] = {
999 {UNUM_INTEGER_FIELD
, 0, 1},
1001 verifyDigitFormatter(
1007 expectedAttributes
);
1008 NumberFormat2Test_Attributes expectedAttributesWithDecimal
[] = {
1009 {UNUM_INTEGER_FIELD
, 0, 1},
1010 {UNUM_DECIMAL_SEPARATOR_FIELD
, 1, 2},
1012 options
.fAlwaysShowDecimal
= TRUE
;
1013 verifyDigitFormatter(
1019 expectedAttributesWithDecimal
);
1022 DigitGrouping grouping
;
1023 FixedPrecision precision
;
1024 VisibleDigits visibleDigits
;
1025 DigitFormatterOptions options
;
1026 precision
.fMax
.setIntDigitCount(1);
1027 precision
.fMin
.setFracDigitCount(1);
1028 precision
.initVisibleDigits(
1029 3125.0, visibleDigits
, status
);
1030 if (!assertSuccess("", status
)) {
1033 NumberFormat2Test_Attributes expectedAttributes
[] = {
1034 {UNUM_INTEGER_FIELD
, 0, 1},
1035 {UNUM_DECIMAL_SEPARATOR_FIELD
, 1, 2},
1036 {UNUM_FRACTION_FIELD
, 2, 3},
1038 options
.fAlwaysShowDecimal
= TRUE
;
1039 verifyDigitFormatter(
1045 expectedAttributes
);
1049 void NumberFormat2Test::TestSciFormatterDefaultCtor() {
1050 DigitFormatter formatter
;
1051 ScientificPrecision precision
;
1052 VisibleDigitsWithExponent visibleDigits
;
1053 UErrorCode status
= U_ZERO_ERROR
;
1054 precision
.initVisibleDigitsWithExponent(
1055 6.02E23
, visibleDigits
, status
);
1056 if (!assertSuccess("", status
)) {
1059 SciFormatterOptions options
;
1066 precision
.initVisibleDigitsWithExponent(
1067 6.62E-34, visibleDigits
, status
);
1068 if (!assertSuccess("", status
)) {
1079 void NumberFormat2Test::TestSciFormatter() {
1080 DigitFormatter formatter
;
1081 ScientificPrecision precision
;
1082 precision
.fMantissa
.fMin
.setIntDigitCount(4);
1083 precision
.fMantissa
.fMax
.setIntDigitCount(4);
1084 precision
.fMantissa
.fMin
.setFracDigitCount(0);
1085 precision
.fMantissa
.fMax
.setFracDigitCount(0);
1086 precision
.fMinExponentDigits
= 3;
1087 VisibleDigitsWithExponent visibleDigits
;
1088 UErrorCode status
= U_ZERO_ERROR
;
1089 precision
.initVisibleDigitsWithExponent(
1090 1.248E26
, visibleDigits
, status
);
1091 if (!assertSuccess("", status
)) {
1094 SciFormatterOptions options
;
1097 options
.fExponent
.fAlwaysShowSign
= TRUE
;
1098 NumberFormat2Test_Attributes expectedAttributes
[] = {
1099 {UNUM_INTEGER_FIELD
, 0, 4},
1100 {UNUM_EXPONENT_SYMBOL_FIELD
, 4, 5},
1101 {UNUM_EXPONENT_SIGN_FIELD
, 5, 6},
1102 {UNUM_EXPONENT_FIELD
, 6, 9},
1109 expectedAttributes
);
1112 options
.fMantissa
.fAlwaysShowDecimal
= TRUE
;
1113 options
.fExponent
.fAlwaysShowSign
= FALSE
;
1114 NumberFormat2Test_Attributes expectedAttributes
[] = {
1115 {UNUM_INTEGER_FIELD
, 0, 4},
1116 {UNUM_DECIMAL_SEPARATOR_FIELD
, 4, 5},
1117 {UNUM_EXPONENT_SYMBOL_FIELD
, 5, 6},
1118 {UNUM_EXPONENT_FIELD
, 6, 9},
1125 expectedAttributes
);
1129 void NumberFormat2Test::TestValueFormatterIsFastFormattable() {
1130 UErrorCode status
= U_ZERO_ERROR
;
1131 DecimalFormatSymbols
symbols("en", status
);
1132 if (!assertSuccess("", status
)) {
1135 DigitFormatter
formatter(symbols
);
1136 DigitGrouping grouping
;
1137 FixedPrecision precision
;
1138 DigitFormatterOptions options
;
1140 vf
.prepareFixedDecimalFormatting(
1141 formatter
, grouping
, precision
, options
);
1142 assertTrue("", vf
.isFastFormattable(0));
1143 assertTrue("", vf
.isFastFormattable(35));
1144 assertTrue("", vf
.isFastFormattable(-48));
1145 assertTrue("", vf
.isFastFormattable(2147483647));
1146 assertTrue("", vf
.isFastFormattable(-2147483647));
1147 assertFalse("", vf
.isFastFormattable(-2147483648L));
1149 DigitGrouping grouping
;
1150 grouping
.fGrouping
= 3;
1152 vf
.prepareFixedDecimalFormatting(
1153 formatter
, grouping
, precision
, options
);
1154 assertTrue("0", vf
.isFastFormattable(0));
1155 assertTrue("62", vf
.isFastFormattable(62));
1156 assertTrue("999", vf
.isFastFormattable(999));
1157 assertFalse("1000", vf
.isFastFormattable(1000));
1158 assertTrue("-1", vf
.isFastFormattable(-1));
1159 assertTrue("-38", vf
.isFastFormattable(-38));
1160 assertTrue("-999", vf
.isFastFormattable(-999));
1161 assertFalse("-1000", vf
.isFastFormattable(-1000));
1162 grouping
.fMinGrouping
= 2;
1163 assertTrue("-1000", vf
.isFastFormattable(-1000));
1164 assertTrue("-4095", vf
.isFastFormattable(-4095));
1165 assertTrue("4095", vf
.isFastFormattable(4095));
1166 // We give up on acounting digits at 4096
1167 assertFalse("-4096", vf
.isFastFormattable(-4096));
1168 assertFalse("4096", vf
.isFastFormattable(4096));
1171 // grouping on but with max integer digits set.
1172 DigitGrouping grouping
;
1173 grouping
.fGrouping
= 4;
1174 FixedPrecision precision
;
1175 precision
.fMax
.setIntDigitCount(4);
1177 vf
.prepareFixedDecimalFormatting(
1178 formatter
, grouping
, precision
, options
);
1179 assertTrue("-4096", vf
.isFastFormattable(-4096));
1180 assertTrue("4096", vf
.isFastFormattable(4096));
1181 assertTrue("-10000", vf
.isFastFormattable(-10000));
1182 assertTrue("10000", vf
.isFastFormattable(10000));
1183 assertTrue("-2147483647", vf
.isFastFormattable(-2147483647));
1184 assertTrue("2147483647", vf
.isFastFormattable(2147483647));
1186 precision
.fMax
.setIntDigitCount(5);
1187 assertFalse("-4096", vf
.isFastFormattable(-4096));
1188 assertFalse("4096", vf
.isFastFormattable(4096));
1192 // grouping on but with min integer digits set.
1193 DigitGrouping grouping
;
1194 grouping
.fGrouping
= 3;
1195 FixedPrecision precision
;
1196 precision
.fMin
.setIntDigitCount(3);
1198 vf
.prepareFixedDecimalFormatting(
1199 formatter
, grouping
, precision
, options
);
1200 assertTrue("-999", vf
.isFastFormattable(-999));
1201 assertTrue("999", vf
.isFastFormattable(999));
1202 assertFalse("-1000", vf
.isFastFormattable(-1000));
1203 assertFalse("1000", vf
.isFastFormattable(1000));
1205 precision
.fMin
.setIntDigitCount(4);
1206 assertFalse("-999", vf
.isFastFormattable(-999));
1207 assertFalse("999", vf
.isFastFormattable(999));
1208 assertFalse("-2147483647", vf
.isFastFormattable(-2147483647));
1209 assertFalse("2147483647", vf
.isFastFormattable(2147483647));
1213 DigitFormatterOptions options
;
1215 vf
.prepareFixedDecimalFormatting(
1216 formatter
, grouping
, precision
, options
);
1217 assertTrue("5125", vf
.isFastFormattable(5125));
1218 options
.fAlwaysShowDecimal
= TRUE
;
1219 assertFalse("5125", vf
.isFastFormattable(5125));
1220 options
.fAlwaysShowDecimal
= FALSE
;
1221 assertTrue("5125", vf
.isFastFormattable(5125));
1224 // test fraction digits
1225 FixedPrecision precision
;
1227 vf
.prepareFixedDecimalFormatting(
1228 formatter
, grouping
, precision
, options
);
1229 assertTrue("7127", vf
.isFastFormattable(7127));
1230 precision
.fMin
.setFracDigitCount(1);
1231 assertFalse("7127", vf
.isFastFormattable(7127));
1234 // test presence of significant digits
1235 FixedPrecision precision
;
1237 vf
.prepareFixedDecimalFormatting(
1238 formatter
, grouping
, precision
, options
);
1239 assertTrue("1049", vf
.isFastFormattable(1049));
1240 precision
.fSignificant
.setMin(1);
1241 assertFalse("1049", vf
.isFastFormattable(1049));
1244 // test presence of rounding increment
1245 FixedPrecision precision
;
1247 vf
.prepareFixedDecimalFormatting(
1248 formatter
, grouping
, precision
, options
);
1249 assertTrue("1099", vf
.isFastFormattable(1099));
1250 precision
.fRoundingIncrement
.set(2.3);
1251 assertFalse("1099", vf
.isFastFormattable(1099));
1254 // test scientific notation
1255 ScientificPrecision precision
;
1256 SciFormatterOptions options
;
1258 vf
.prepareScientificFormatting(
1259 formatter
, precision
, options
);
1260 assertFalse("1081", vf
.isFastFormattable(1081));
1264 void NumberFormat2Test::TestDigitAffix() {
1267 affix
.append("foo");
1268 affix
.append("--", UNUM_SIGN_FIELD
);
1269 affix
.append("%", UNUM_PERCENT_FIELD
);
1270 NumberFormat2Test_Attributes expectedAttributes
[] = {
1271 {UNUM_SIGN_FIELD
, 3, 5},
1272 {UNUM_PERCENT_FIELD
, 5, 6},
1274 verifyAffix("foo--%", affix
, expectedAttributes
);
1278 affix
.append("USD", UNUM_CURRENCY_FIELD
);
1280 NumberFormat2Test_Attributes expectedAttributes
[] = {
1281 {UNUM_CURRENCY_FIELD
, 0, 3},
1283 verifyAffix("USD ", affix
, expectedAttributes
);
1286 affix
.setTo("%%", UNUM_PERCENT_FIELD
);
1287 NumberFormat2Test_Attributes expectedAttributes
[] = {
1288 {UNUM_PERCENT_FIELD
, 0, 2},
1290 verifyAffix("%%", affix
, expectedAttributes
);
1294 void NumberFormat2Test::TestPluralAffix() {
1295 UErrorCode status
= U_ZERO_ERROR
;
1297 part
.setVariant("one", "Dollar", status
);
1298 part
.setVariant("few", "DollarFew", status
);
1299 part
.setVariant("other", "Dollars", status
);
1300 PluralAffix
dollar(part
);
1301 PluralAffix
percent(part
);
1303 part
.setVariant("one", "Percent", status
);
1304 part
.setVariant("many", "PercentMany", status
);
1305 part
.setVariant("other", "Percents", status
);
1308 part
.setVariant("one", "foo", status
);
1311 assertEquals("", "", pa
.getOtherVariant().toString());
1312 pa
.append(dollar
, UNUM_CURRENCY_FIELD
, status
);
1314 pa
.append(percent
, UNUM_PERCENT_FIELD
, status
);
1315 pa
.append("-", UNUM_SIGN_FIELD
);
1319 NumberFormat2Test_Attributes expectedAttributes
[] = {
1320 {UNUM_CURRENCY_FIELD
, 0, 7},
1321 {UNUM_PERCENT_FIELD
, 12, 20},
1322 {UNUM_SIGN_FIELD
, 20, 21},
1325 "Dollars and Percents-",
1326 pa
.getByCategory("other"),
1327 expectedAttributes
);
1330 // two which is same as other
1331 NumberFormat2Test_Attributes expectedAttributes
[] = {
1332 {UNUM_CURRENCY_FIELD
, 0, 7},
1333 {UNUM_PERCENT_FIELD
, 12, 20},
1334 {UNUM_SIGN_FIELD
, 20, 21},
1337 "Dollars and Percents-",
1338 pa
.getByCategory("two"),
1339 expectedAttributes
);
1342 // bad which is same as other
1343 NumberFormat2Test_Attributes expectedAttributes
[] = {
1344 {UNUM_CURRENCY_FIELD
, 0, 7},
1345 {UNUM_PERCENT_FIELD
, 12, 20},
1346 {UNUM_SIGN_FIELD
, 20, 21},
1349 "Dollars and Percents-",
1350 pa
.getByCategory("bad"),
1351 expectedAttributes
);
1355 NumberFormat2Test_Attributes expectedAttributes
[] = {
1356 {UNUM_CURRENCY_FIELD
, 0, 6},
1357 {UNUM_PERCENT_FIELD
, 11, 18},
1358 {UNUM_SIGN_FIELD
, 18, 19},
1361 "Dollar and Percent-",
1362 pa
.getByCategory("one"),
1363 expectedAttributes
);
1367 NumberFormat2Test_Attributes expectedAttributes
[] = {
1368 {UNUM_CURRENCY_FIELD
, 0, 9},
1369 {UNUM_PERCENT_FIELD
, 14, 22},
1370 {UNUM_SIGN_FIELD
, 22, 23},
1373 "DollarFew and Percents-",
1374 pa
.getByCategory("few"),
1375 expectedAttributes
);
1379 NumberFormat2Test_Attributes expectedAttributes
[] = {
1380 {UNUM_CURRENCY_FIELD
, 0, 7},
1381 {UNUM_PERCENT_FIELD
, 12, 23},
1382 {UNUM_SIGN_FIELD
, 23, 24},
1385 "Dollars and PercentMany-",
1386 pa
.getByCategory("many"),
1387 expectedAttributes
);
1389 assertTrue("", pa
.hasMultipleVariants());
1391 pa
.append("$$$", UNUM_CURRENCY_FIELD
);
1392 assertFalse("", pa
.hasMultipleVariants());
1396 void NumberFormat2Test::TestCurrencyAffixInfo() {
1397 CurrencyAffixInfo info
;
1398 assertTrue("", info
.isDefault());
1399 UnicodeString
expectedSymbol("\\u00a4");
1400 UnicodeString
expectedSymbolIso("\\u00a4\\u00a4");
1401 UnicodeString
expectedSymbols("\\u00a4\\u00a4\\u00a4");
1402 assertEquals("", expectedSymbol
.unescape(), info
.getSymbol());
1403 assertEquals("", expectedSymbolIso
.unescape(), info
.getISO());
1404 assertEquals("", expectedSymbols
.unescape(), info
.getLong().getByCategory("one").toString());
1405 assertEquals("", expectedSymbols
.unescape(), info
.getLong().getByCategory("other").toString());
1406 assertEquals("", expectedSymbols
.unescape(), info
.getLong().getByCategory("two").toString());
1407 UErrorCode status
= U_ZERO_ERROR
;
1408 static UChar USD
[] = {0x55, 0x53, 0x44, 0x0};
1409 LocalPointer
<PluralRules
> rules(PluralRules::forLocale("en", status
));
1410 if (!assertSuccess("", status
)) {
1413 info
.set("en", rules
.getAlias(), USD
, status
);
1414 assertEquals("", "$", info
.getSymbol(), TRUE
);
1415 assertEquals("", "USD", info
.getISO(), TRUE
);
1416 assertEquals("", "US dollar", info
.getLong().getByCategory("one").toString(), TRUE
);
1417 assertEquals("", "US dollars", info
.getLong().getByCategory("other").toString(), TRUE
);
1418 assertEquals("", "US dollars", info
.getLong().getByCategory("two").toString(), TRUE
);
1419 assertFalse("", info
.isDefault());
1420 info
.set(NULL
, NULL
, NULL
, status
);
1421 assertTrue("", info
.isDefault());
1422 assertEquals("", expectedSymbol
.unescape(), info
.getSymbol());
1423 assertEquals("", expectedSymbolIso
.unescape(), info
.getISO());
1424 assertEquals("", expectedSymbols
.unescape(), info
.getLong().getByCategory("one").toString());
1425 assertEquals("", expectedSymbols
.unescape(), info
.getLong().getByCategory("other").toString());
1426 assertEquals("", expectedSymbols
.unescape(), info
.getLong().getByCategory("two").toString());
1427 info
.setSymbol("$");
1428 assertFalse("", info
.isDefault());
1429 info
.set(NULL
, NULL
, NULL
, status
);
1430 assertTrue("", info
.isDefault());
1432 assertFalse("", info
.isDefault());
1433 assertSuccess("", status
);
1436 void NumberFormat2Test::TestAffixPattern() {
1437 static UChar chars
[500];
1438 for (int32_t i
= 0; i
< UPRV_LENGTHOF(chars
); ++i
) {
1439 chars
[i
] = (UChar
) (i
+ 1);
1442 first
.add(AffixPattern::kPercent
);
1443 first
.addLiteral(chars
, 0, 200);
1444 first
.addLiteral(chars
, 200, 300);
1445 first
.addCurrency(2);
1446 first
.addLiteral(chars
, 0, 256);
1447 AffixPattern second
;
1448 second
.add(AffixPattern::kPercent
);
1449 second
.addLiteral(chars
, 0, 300);
1450 second
.addLiteral(chars
, 300, 200);
1451 second
.addCurrency(2);
1452 second
.addLiteral(chars
, 0, 150);
1453 second
.addLiteral(chars
, 150, 106);
1454 assertTrue("", first
.equals(second
));
1455 AffixPatternIterator iter
;
1457 assertFalse("", second
.iterator(iter
).nextToken());
1458 assertTrue("", first
.iterator(iter
).nextToken());
1459 assertEquals("", (int32_t)AffixPattern::kPercent
, iter
.getTokenType());
1460 assertEquals("", 1, iter
.getTokenLength());
1461 assertTrue("", iter
.nextToken());
1463 assertEquals("", 500, iter
.getLiteral(str
).length());
1464 assertEquals("", (int32_t)AffixPattern::kLiteral
, iter
.getTokenType());
1465 assertEquals("", 500, iter
.getTokenLength());
1466 assertTrue("", iter
.nextToken());
1467 assertEquals("", (int32_t)AffixPattern::kCurrency
, iter
.getTokenType());
1468 assertEquals("", 2, iter
.getTokenLength());
1469 assertTrue("", iter
.nextToken());
1470 assertEquals("", 256, iter
.getLiteral(str
).length());
1471 assertEquals("", (int32_t)AffixPattern::kLiteral
, iter
.getTokenType());
1472 assertEquals("", 256, iter
.getTokenLength());
1473 assertFalse("", iter
.nextToken());
1476 void NumberFormat2Test::TestAffixPatternDoubleQuote() {
1477 UnicodeString
str("'Don''t'");
1478 AffixPattern expected
;
1480 static UChar chars
[] = {0x44, 0x6F, 0x6E, 0x27, 0x74};
1481 expected
.addLiteral(chars
, 0, UPRV_LENGTHOF(chars
));
1482 AffixPattern actual
;
1483 UErrorCode status
= U_ZERO_ERROR
;
1484 AffixPattern::parseUserAffixString(str
, actual
, status
);
1485 assertTrue("", expected
.equals(actual
));
1486 UnicodeString formattedString
;
1487 assertEquals("", "Don''t", actual
.toUserString(formattedString
));
1488 assertSuccess("", status
);
1491 void NumberFormat2Test::TestAffixPatternParser() {
1492 UErrorCode status
= U_ZERO_ERROR
;
1493 static UChar USD
[] = {0x55, 0x53, 0x44, 0x0};
1494 LocalPointer
<PluralRules
> rules(PluralRules::forLocale("en", status
));
1495 DecimalFormatSymbols
symbols("en", status
);
1496 if (U_FAILURE(status
)) {
1497 dataerrln("Error creating DecimalFormatSymbols - %s", u_errorName(status
));
1500 AffixPatternParser
parser(symbols
);
1501 CurrencyAffixInfo currencyAffixInfo
;
1502 currencyAffixInfo
.set("en", rules
.getAlias(), USD
, status
);
1504 UnicodeString
str("'--y'''dz'%'\\u00a4\\u00a4\\u00a4\\u00a4 y '\\u00a4\\u00a4\\u00a4 or '\\u00a4\\u00a4 but '\\u00a4");
1505 str
= str
.unescape();
1506 assertSuccess("", status
);
1507 AffixPattern affixPattern
;
1509 AffixPattern::parseAffixString(str
, affixPattern
, status
),
1513 UnicodeString formattedStr
;
1514 affixPattern
.toString(formattedStr
);
1515 UnicodeString
expectedFormattedStr("'--y''dz'%'\\u00a4\\u00a4\\u00a4\\u00a4 y '\\u00a4\\u00a4\\u00a4 or '\\u00a4\\u00a4 but '\\u00a4");
1516 expectedFormattedStr
= expectedFormattedStr
.unescape();
1517 assertEquals("1", expectedFormattedStr
, formattedStr
);
1518 AffixPattern userAffixPattern
;
1519 UnicodeString
userStr("-'-'y'''d'z%\\u00a4\\u00a4\\u00a4'\\u00a4' y \\u00a4\\u00a4\\u00a4 or \\u00a4\\u00a4 but \\u00a4");
1520 userStr
= userStr
.unescape();
1521 AffixPattern::parseUserAffixString(userStr
, userAffixPattern
, status
),
1522 assertTrue("", affixPattern
.equals(userAffixPattern
));
1523 AffixPattern userAffixPattern2
;
1524 UnicodeString formattedUserStr
;
1525 AffixPattern::parseUserAffixString(
1526 userAffixPattern
.toUserString(formattedUserStr
),
1529 UnicodeString
expectedFormattedUserStr(
1530 "-'-'y''dz%\\u00a4\\u00a4\\u00a4'\\u00a4' y \\u00a4\\u00a4\\u00a4 or \\u00a4\\u00a4 but \\u00a4");
1531 assertEquals("2", expectedFormattedUserStr
.unescape(), formattedUserStr
);
1532 assertTrue("", userAffixPattern2
.equals(userAffixPattern
));
1533 assertSuccess("", status
);
1534 assertTrue("", affixPattern
.usesCurrency());
1535 assertTrue("", affixPattern
.usesPercent());
1536 assertFalse("", affixPattern
.usesPermill());
1537 assertTrue("", affix
.hasMultipleVariants());
1540 NumberFormat2Test_Attributes expectedAttributes
[] = {
1541 {UNUM_SIGN_FIELD
, 0, 1},
1542 {UNUM_PERCENT_FIELD
, 6, 7},
1543 {UNUM_CURRENCY_FIELD
, 7, 17},
1544 {UNUM_CURRENCY_FIELD
, 21, 31},
1545 {UNUM_CURRENCY_FIELD
, 35, 38},
1546 {UNUM_CURRENCY_FIELD
, 43, 44},
1549 "--y'dz%US dollars\\u00a4 y US dollars or USD but $",
1550 affix
.getByCategory("other"),
1551 expectedAttributes
);
1555 NumberFormat2Test_Attributes expectedAttributes
[] = {
1556 {UNUM_SIGN_FIELD
, 0, 1},
1557 {UNUM_PERCENT_FIELD
, 6, 7},
1558 {UNUM_CURRENCY_FIELD
, 7, 16},
1559 {UNUM_CURRENCY_FIELD
, 20, 29},
1560 {UNUM_CURRENCY_FIELD
, 33, 36},
1561 {UNUM_CURRENCY_FIELD
, 41, 42},
1564 "--y'dz%US dollar\\u00a4 y US dollar or USD but $",
1565 affix
.getByCategory("one"),
1566 expectedAttributes
);
1570 affixPattern
.remove();
1572 AffixPattern::parseAffixString(str
, affixPattern
, status
),
1576 assertSuccess("", status
);
1577 assertFalse("", affixPattern
.usesCurrency());
1578 assertFalse("", affixPattern
.usesPercent());
1579 assertFalse("", affixPattern
.usesPermill());
1580 assertFalse("", affix
.hasMultipleVariants());
1583 NumberFormat2Test_Attributes expectedAttributes
[] = {
1584 {UNUM_SIGN_FIELD
, 1, 2},
1588 affix
.getByCategory("other"),
1589 expectedAttributes
);
1591 UnicodeString
a4("\\u00a4");
1592 AffixPattern scratchPattern
;
1593 AffixPattern::parseAffixString(a4
.unescape(), scratchPattern
, status
);
1594 assertFalse("", scratchPattern
.usesCurrency());
1596 // Test really long string > 256 chars.
1597 str
= "'\\u2030012345678901234567890123456789012345678901234567890123456789"
1598 "012345678901234567890123456789012345678901234567890123456789"
1599 "012345678901234567890123456789012345678901234567890123456789"
1600 "012345678901234567890123456789012345678901234567890123456789"
1601 "012345678901234567890123456789012345678901234567890123456789";
1602 str
= str
.unescape();
1603 affixPattern
.remove();
1606 AffixPattern::parseAffixString(str
, affixPattern
, status
),
1610 assertSuccess("", status
);
1611 assertFalse("", affixPattern
.usesCurrency());
1612 assertFalse("", affixPattern
.usesPercent());
1613 assertTrue("", affixPattern
.usesPermill());
1614 assertFalse("", affix
.hasMultipleVariants());
1616 UnicodeString expected
=
1617 "\\u2030012345678901234567890123456789012345678901234567890123456789"
1618 "012345678901234567890123456789012345678901234567890123456789"
1619 "012345678901234567890123456789012345678901234567890123456789"
1620 "012345678901234567890123456789012345678901234567890123456789"
1621 "012345678901234567890123456789012345678901234567890123456789";
1622 expected
= expected
.unescape();
1623 NumberFormat2Test_Attributes expectedAttributes
[] = {
1624 {UNUM_PERMILL_FIELD
, 0, 1},
1628 affix
.getOtherVariant(),
1629 expectedAttributes
);
1633 void NumberFormat2Test::TestAffixPatternAppend() {
1634 AffixPattern pattern
;
1635 UErrorCode status
= U_ZERO_ERROR
;
1636 UnicodeString
patternStr("%\\u2030");
1637 AffixPattern::parseUserAffixString(
1638 patternStr
.unescape(), pattern
, status
);
1640 AffixPattern appendPattern
;
1641 UnicodeString
appendPatternStr("-\\u00a4\\u00a4*");
1642 AffixPattern::parseUserAffixString(
1643 appendPatternStr
.unescape(), appendPattern
, status
);
1645 AffixPattern expectedPattern
;
1646 UnicodeString
expectedPatternStr("%\\u2030-\\u00a4\\u00a4*");
1647 AffixPattern::parseUserAffixString(
1648 expectedPatternStr
.unescape(), expectedPattern
, status
);
1650 assertTrue("", pattern
.append(appendPattern
).equals(expectedPattern
));
1651 assertSuccess("", status
);
1654 void NumberFormat2Test::TestAffixPatternAppendAjoiningLiterals() {
1655 AffixPattern pattern
;
1656 UErrorCode status
= U_ZERO_ERROR
;
1657 UnicodeString
patternStr("%baaa");
1658 AffixPattern::parseUserAffixString(
1659 patternStr
, pattern
, status
);
1661 AffixPattern appendPattern
;
1662 UnicodeString
appendPatternStr("caa%");
1663 AffixPattern::parseUserAffixString(
1664 appendPatternStr
, appendPattern
, status
);
1666 AffixPattern expectedPattern
;
1667 UnicodeString
expectedPatternStr("%baaacaa%");
1668 AffixPattern::parseUserAffixString(
1669 expectedPatternStr
, expectedPattern
, status
);
1671 assertTrue("", pattern
.append(appendPattern
).equals(expectedPattern
));
1672 assertSuccess("", status
);
1675 void NumberFormat2Test::TestLargeIntValue() {
1676 VisibleDigits digits
;
1678 UErrorCode status
= U_ZERO_ERROR
;
1679 FixedPrecision precision
;
1681 // Last 18 digits for int values.
1683 223372036854775807LL,
1684 precision
.initVisibleDigits(U_INT64_MAX
, digits
, status
));
1685 assertSuccess("U_INT64_MAX", status
);
1688 UErrorCode status
= U_ZERO_ERROR
;
1689 FixedPrecision precision
;
1690 precision
.fMax
.setIntDigitCount(5);
1692 // Last 18 digits for int values.
1695 precision
.initVisibleDigits(U_INT64_MAX
, digits
, status
));
1696 verifySource(75807.0, digits
);
1697 assertSuccess("75807", status
);
1700 UErrorCode status
= U_ZERO_ERROR
;
1701 FixedPrecision precision
;
1703 // Last 18 digits for int values.
1705 223372036854775808LL,
1706 precision
.initVisibleDigits(U_INT64_MIN
, digits
, status
));
1707 assertSuccess("U_INT64_MIN", status
);
1710 UErrorCode status
= U_ZERO_ERROR
;
1711 FixedPrecision precision
;
1712 precision
.fMax
.setIntDigitCount(5);
1714 // Last 18 digits for int values.
1717 precision
.initVisibleDigits(U_INT64_MIN
, digits
, status
));
1718 verifySource(75808.0, digits
);
1719 assertSuccess("75808", status
);
1724 void NumberFormat2Test::TestIntInitVisibleDigits() {
1725 VisibleDigits digits
;
1727 UErrorCode status
= U_ZERO_ERROR
;
1728 FixedPrecision precision
;
1729 verifyVisibleDigits(
1732 precision
.initVisibleDigits((int64_t) 13LL, digits
, status
));
1733 assertSuccess("13", status
);
1736 UErrorCode status
= U_ZERO_ERROR
;
1737 FixedPrecision precision
;
1738 verifyVisibleDigits(
1741 precision
.initVisibleDigits((int64_t) -17LL, digits
, status
));
1742 assertSuccess("-17", status
);
1745 UErrorCode status
= U_ZERO_ERROR
;
1746 FixedPrecision precision
;
1747 verifyVisibleDigits(
1748 "9223372036854775808",
1750 precision
.initVisibleDigits(U_INT64_MIN
, digits
, status
));
1751 assertSuccess("-9223372036854775808", status
);
1754 UErrorCode status
= U_ZERO_ERROR
;
1755 FixedPrecision precision
;
1756 verifyVisibleDigits(
1757 "9223372036854775807",
1759 precision
.initVisibleDigits(U_INT64_MAX
, digits
, status
));
1760 assertSuccess("9223372036854775807", status
);
1763 UErrorCode status
= U_ZERO_ERROR
;
1764 FixedPrecision precision
;
1765 verifyVisibleDigits(
1768 precision
.initVisibleDigits((int64_t) -31536000LL, digits
, status
));
1769 assertSuccess("-31536000", status
);
1772 UErrorCode status
= U_ZERO_ERROR
;
1773 FixedPrecision precision
;
1774 verifyVisibleDigits(
1777 precision
.initVisibleDigits((int64_t) 0LL, digits
, status
));
1778 assertSuccess("0", status
);
1781 UErrorCode status
= U_ZERO_ERROR
;
1782 FixedPrecision precision
;
1783 precision
.fMin
.setIntDigitCount(4);
1784 precision
.fMin
.setFracDigitCount(2);
1785 verifyVisibleDigits(
1788 precision
.initVisibleDigits((int64_t) 0LL, digits
, status
));
1789 assertSuccess("0", status
);
1792 UErrorCode status
= U_ZERO_ERROR
;
1793 FixedPrecision precision
;
1794 precision
.fMin
.setIntDigitCount(4);
1795 precision
.fMin
.setFracDigitCount(2);
1796 verifyVisibleDigits(
1799 precision
.initVisibleDigits((int64_t) 57LL, digits
, status
));
1800 assertSuccess("57", status
);
1803 UErrorCode status
= U_ZERO_ERROR
;
1804 FixedPrecision precision
;
1805 precision
.fMin
.setIntDigitCount(4);
1806 precision
.fMin
.setFracDigitCount(2);
1807 verifyVisibleDigits(
1810 precision
.initVisibleDigits((int64_t) -57LL, digits
, status
));
1811 assertSuccess("-57", status
);
1814 UErrorCode status
= U_ZERO_ERROR
;
1815 FixedPrecision precision
;
1816 precision
.fMax
.setIntDigitCount(2);
1817 precision
.fMin
.setFracDigitCount(1);
1818 verifyVisibleDigits(
1821 precision
.initVisibleDigits((int64_t) 235LL, digits
, status
));
1822 assertSuccess("235", status
);
1825 UErrorCode status
= U_ZERO_ERROR
;
1826 FixedPrecision precision
;
1827 precision
.fMax
.setIntDigitCount(2);
1828 precision
.fMin
.setFracDigitCount(1);
1829 precision
.fFailIfOverMax
= TRUE
;
1830 precision
.initVisibleDigits((int64_t) 239LL, digits
, status
);
1831 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1832 errln("239: Expected U_ILLEGAL_ARGUMENT_ERROR");
1836 UErrorCode status
= U_ZERO_ERROR
;
1837 FixedPrecision precision
;
1838 precision
.fSignificant
.setMin(5);
1839 verifyVisibleDigits(
1842 precision
.initVisibleDigits((int64_t) 153LL, digits
, status
));
1843 assertSuccess("153", status
);
1846 UErrorCode status
= U_ZERO_ERROR
;
1847 FixedPrecision precision
;
1848 precision
.fSignificant
.setMax(2);
1849 precision
.fExactOnly
= TRUE
;
1850 precision
.initVisibleDigits((int64_t) 154LL, digits
, status
);
1851 if (status
!= U_FORMAT_INEXACT_ERROR
) {
1852 errln("154: Expected U_FORMAT_INEXACT_ERROR");
1856 UErrorCode status
= U_ZERO_ERROR
;
1857 FixedPrecision precision
;
1858 precision
.fSignificant
.setMax(5);
1859 verifyVisibleDigits(
1862 precision
.initVisibleDigits((int64_t) 150LL, digits
, status
));
1863 assertSuccess("150", status
);
1867 void NumberFormat2Test::TestIntInitVisibleDigitsToDigitList() {
1868 VisibleDigits digits
;
1870 UErrorCode status
= U_ZERO_ERROR
;
1871 FixedPrecision precision
;
1872 precision
.fRoundingIncrement
.set(7.3);
1873 verifyVisibleDigits(
1876 precision
.initVisibleDigits((int64_t) -30LL, digits
, status
));
1877 assertSuccess("-29.2", status
);
1880 UErrorCode status
= U_ZERO_ERROR
;
1881 FixedPrecision precision
;
1882 precision
.fRoundingIncrement
.set(7.3);
1883 precision
.fRoundingMode
= DecimalFormat::kRoundFloor
;
1884 verifyVisibleDigits(
1887 precision
.initVisibleDigits((int64_t) -30LL, digits
, status
));
1888 assertSuccess("-36.5", status
);
1891 UErrorCode status
= U_ZERO_ERROR
;
1892 FixedPrecision precision
;
1893 precision
.fSignificant
.setMax(3);
1894 precision
.fRoundingMode
= DecimalFormat::kRoundCeiling
;
1895 verifyVisibleDigits(
1898 precision
.initVisibleDigits((int64_t) 1381LL, digits
, status
));
1899 assertSuccess("1390", status
);
1902 UErrorCode status
= U_ZERO_ERROR
;
1903 FixedPrecision precision
;
1904 precision
.fSignificant
.setMax(1);
1905 precision
.fRoundingMode
= DecimalFormat::kRoundFloor
;
1906 verifyVisibleDigits(
1909 precision
.initVisibleDigits((int64_t) -1381LL, digits
, status
));
1910 assertSuccess("-2000", status
);
1914 void NumberFormat2Test::TestDoubleInitVisibleDigits() {
1915 VisibleDigits digits
;
1917 UErrorCode status
= U_ZERO_ERROR
;
1918 FixedPrecision precision
;
1919 verifyVisibleDigits(
1922 precision
.initVisibleDigits(2.05, digits
, status
));
1923 assertSuccess("2.05", status
);
1926 UErrorCode status
= U_ZERO_ERROR
;
1927 FixedPrecision precision
;
1928 verifyVisibleDigits(
1931 precision
.initVisibleDigits(3547.0, digits
, status
));
1932 assertSuccess("3547", status
);
1935 UErrorCode status
= U_ZERO_ERROR
;
1936 FixedPrecision precision
;
1937 precision
.fMax
.setFracDigitCount(2);
1938 precision
.fMax
.setIntDigitCount(1);
1939 precision
.fFailIfOverMax
= TRUE
;
1940 precision
.fExactOnly
= TRUE
;
1941 verifyVisibleDigits(
1944 precision
.initVisibleDigits(-2.05, digits
, status
));
1945 assertSuccess("-2.05", status
);
1948 UErrorCode status
= U_ZERO_ERROR
;
1949 FixedPrecision precision
;
1950 precision
.fMax
.setFracDigitCount(1);
1951 precision
.fMax
.setIntDigitCount(1);
1952 precision
.fFailIfOverMax
= TRUE
;
1953 precision
.fExactOnly
= TRUE
;
1954 precision
.initVisibleDigits(-2.05, digits
, status
);
1955 if (status
!= U_FORMAT_INEXACT_ERROR
) {
1956 errln("6245.3: Expected U_FORMAT_INEXACT_ERROR");
1960 UErrorCode status
= U_ZERO_ERROR
;
1961 FixedPrecision precision
;
1962 precision
.fMax
.setFracDigitCount(2);
1963 precision
.fMax
.setIntDigitCount(0);
1964 precision
.fFailIfOverMax
= TRUE
;
1965 precision
.fExactOnly
= TRUE
;
1966 precision
.initVisibleDigits(-2.05, digits
, status
);
1967 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1968 errln("-2.05: Expected U_ILLEGAL_ARGUMENT_ERROR");
1972 UErrorCode status
= U_ZERO_ERROR
;
1973 FixedPrecision precision
;
1974 precision
.fMin
.setIntDigitCount(5);
1975 precision
.fMin
.setFracDigitCount(2);
1976 precision
.fExactOnly
= TRUE
;
1977 verifyVisibleDigits(
1980 precision
.initVisibleDigits(6245.3, digits
, status
));
1981 assertSuccess("06245.30", status
);
1984 UErrorCode status
= U_ZERO_ERROR
;
1985 FixedPrecision precision
;
1986 precision
.fSignificant
.setMax(5);
1987 precision
.fExactOnly
= TRUE
;
1988 verifyVisibleDigits(
1991 precision
.initVisibleDigits(6245.3, digits
, status
));
1992 assertSuccess("6245.3", status
);
1995 UErrorCode status
= U_ZERO_ERROR
;
1996 FixedPrecision precision
;
1997 precision
.fSignificant
.setMax(4);
1998 precision
.fExactOnly
= TRUE
;
1999 precision
.initVisibleDigits(6245.3, digits
, status
);
2000 if (status
!= U_FORMAT_INEXACT_ERROR
) {
2001 errln("6245.3: Expected U_FORMAT_INEXACT_ERROR");
2005 UErrorCode status
= U_ZERO_ERROR
;
2006 FixedPrecision precision
;
2007 precision
.fMax
.setIntDigitCount(3);
2008 precision
.fMin
.setFracDigitCount(2);
2009 verifyVisibleDigits(
2012 precision
.initVisibleDigits(2384.9, digits
, status
));
2013 assertSuccess("380.00", status
);
2016 UErrorCode status
= U_ZERO_ERROR
;
2017 FixedPrecision precision
;
2018 precision
.fMax
.setIntDigitCount(3);
2019 precision
.fMin
.setFracDigitCount(2);
2020 precision
.fFailIfOverMax
= TRUE
;
2021 precision
.initVisibleDigits(2384.9, digits
, status
);
2022 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
2023 errln("2384.9: Expected U_ILLEGAL_ARGUMENT_ERROR");
2028 void NumberFormat2Test::TestDoubleInitVisibleDigitsToDigitList() {
2029 VisibleDigits digits
;
2031 UErrorCode status
= U_ZERO_ERROR
;
2032 FixedPrecision precision
;
2033 // 2.01 produces round off error when multiplied by powers of
2034 // 10 forcing the use of DigitList.
2035 verifyVisibleDigits(
2038 precision
.initVisibleDigits(-2.01, digits
, status
));
2039 assertSuccess("-2.01", status
);
2042 UErrorCode status
= U_ZERO_ERROR
;
2043 FixedPrecision precision
;
2044 precision
.fSignificant
.setMax(3);
2045 precision
.fMin
.setFracDigitCount(2);
2046 verifyVisibleDigits(
2049 precision
.initVisibleDigits(2385.0, digits
, status
));
2050 assertSuccess("2380.00", status
);
2053 UErrorCode status
= U_ZERO_ERROR
;
2054 FixedPrecision precision
;
2055 precision
.fMax
.setFracDigitCount(2);
2056 verifyVisibleDigits(
2059 precision
.initVisibleDigits(-45.8251, digits
, status
));
2060 assertSuccess("45.83", status
);
2064 void NumberFormat2Test::TestDigitListInitVisibleDigits() {
2065 VisibleDigits digits
;
2068 UErrorCode status
= U_ZERO_ERROR
;
2069 FixedPrecision precision
;
2070 precision
.fMax
.setIntDigitCount(3);
2071 precision
.fMin
.setFracDigitCount(2);
2072 precision
.fFailIfOverMax
= TRUE
;
2074 precision
.initVisibleDigits(dlist
, digits
, status
);
2075 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
2076 errln("2384.9: Expected U_ILLEGAL_ARGUMENT_ERROR");
2080 UErrorCode status
= U_ZERO_ERROR
;
2081 FixedPrecision precision
;
2082 precision
.fSignificant
.setMax(4);
2083 precision
.fExactOnly
= TRUE
;
2085 precision
.initVisibleDigits(dlist
, digits
, status
);
2086 if (status
!= U_FORMAT_INEXACT_ERROR
) {
2087 errln("6245.3: Expected U_FORMAT_INEXACT_ERROR");
2092 void NumberFormat2Test::TestSpecialInitVisibleDigits() {
2093 VisibleDigits digits
;
2095 UErrorCode status
= U_ZERO_ERROR
;
2096 FixedPrecision precision
;
2097 precision
.fSignificant
.setMax(3);
2098 precision
.fMin
.setFracDigitCount(2);
2099 precision
.initVisibleDigits(-uprv_getInfinity(), digits
, status
);
2100 assertFalse("", digits
.isNaN());
2101 assertTrue("", digits
.isInfinite());
2102 assertTrue("", digits
.isNegative());
2103 assertSuccess("-Inf", status
);
2106 UErrorCode status
= U_ZERO_ERROR
;
2107 FixedPrecision precision
;
2108 precision
.initVisibleDigits(uprv_getInfinity(), digits
, status
);
2109 assertFalse("", digits
.isNaN());
2110 assertTrue("", digits
.isInfinite());
2111 assertFalse("", digits
.isNegative());
2112 assertSuccess("Inf", status
);
2115 UErrorCode status
= U_ZERO_ERROR
;
2116 FixedPrecision precision
;
2117 precision
.initVisibleDigits(uprv_getNaN(), digits
, status
);
2118 assertTrue("", digits
.isNaN());
2119 assertSuccess("Inf", status
);
2123 void NumberFormat2Test::TestVisibleDigitsWithExponent() {
2124 VisibleDigitsWithExponent digits
;
2126 UErrorCode status
= U_ZERO_ERROR
;
2127 ScientificPrecision precision
;
2128 precision
.initVisibleDigitsWithExponent(389.256, digits
, status
);
2129 verifyVisibleDigitsWithExponent(
2130 "3.89256E2", FALSE
, digits
);
2131 assertSuccess("3.89256E2", status
);
2134 UErrorCode status
= U_ZERO_ERROR
;
2135 ScientificPrecision precision
;
2136 precision
.initVisibleDigitsWithExponent(-389.256, digits
, status
);
2137 verifyVisibleDigitsWithExponent(
2138 "3.89256E2", TRUE
, digits
);
2139 assertSuccess("-3.89256E2", status
);
2142 UErrorCode status
= U_ZERO_ERROR
;
2143 ScientificPrecision precision
;
2144 precision
.fMinExponentDigits
= 3;
2145 precision
.fMantissa
.fMin
.setIntDigitCount(1);
2146 precision
.fMantissa
.fMax
.setIntDigitCount(3);
2147 precision
.initVisibleDigitsWithExponent(12345.67, digits
, status
);
2148 verifyVisibleDigitsWithExponent(
2149 "12.34567E003", FALSE
, digits
);
2150 assertSuccess("12.34567E003", status
);
2153 UErrorCode status
= U_ZERO_ERROR
;
2154 ScientificPrecision precision
;
2155 precision
.fMantissa
.fRoundingIncrement
.set(0.073);
2156 precision
.fMantissa
.fMin
.setIntDigitCount(2);
2157 precision
.fMantissa
.fMax
.setIntDigitCount(2);
2158 precision
.initVisibleDigitsWithExponent(999.74, digits
, status
);
2159 verifyVisibleDigitsWithExponent(
2160 "10.001E2", FALSE
, digits
);
2161 assertSuccess("10.001E2", status
);
2165 void NumberFormat2Test::TestDigitAffixesAndPadding() {
2166 UErrorCode status
= U_ZERO_ERROR
;
2167 DecimalFormatSymbols
symbols("en", status
);
2168 if (!assertSuccess("", status
)) {
2171 DigitFormatter
formatter(symbols
);
2172 DigitGrouping grouping
;
2173 grouping
.fGrouping
= 3;
2174 FixedPrecision precision
;
2175 DigitFormatterOptions options
;
2176 options
.fAlwaysShowDecimal
= TRUE
;
2178 vf
.prepareFixedDecimalFormatting(
2183 DigitAffixesAndPadding aap
;
2184 aap
.fPositivePrefix
.append("(+", UNUM_SIGN_FIELD
);
2185 aap
.fPositiveSuffix
.append("+)", UNUM_SIGN_FIELD
);
2186 aap
.fNegativePrefix
.append("(-", UNUM_SIGN_FIELD
);
2187 aap
.fNegativeSuffix
.append("-)", UNUM_SIGN_FIELD
);
2189 aap
.fPadPosition
= DigitAffixesAndPadding::kPadBeforePrefix
;
2191 NumberFormat2Test_Attributes expectedAttributes
[] = {
2192 {UNUM_SIGN_FIELD
, 4, 6},
2193 {UNUM_INTEGER_FIELD
, 6, 7},
2194 {UNUM_DECIMAL_SEPARATOR_FIELD
, 7, 8},
2195 {UNUM_SIGN_FIELD
, 8, 10},
2197 verifyAffixesAndPaddingInt32(
2203 expectedAttributes
);
2205 aap
.fPadPosition
= DigitAffixesAndPadding::kPadAfterPrefix
;
2207 NumberFormat2Test_Attributes expectedAttributes
[] = {
2208 {UNUM_SIGN_FIELD
, 0, 2},
2209 {UNUM_INTEGER_FIELD
, 6, 7},
2210 {UNUM_DECIMAL_SEPARATOR_FIELD
, 7, 8},
2211 {UNUM_SIGN_FIELD
, 8, 10},
2213 verifyAffixesAndPaddingInt32(
2219 expectedAttributes
);
2221 aap
.fPadPosition
= DigitAffixesAndPadding::kPadBeforeSuffix
;
2223 NumberFormat2Test_Attributes expectedAttributes
[] = {
2224 {UNUM_SIGN_FIELD
, 0, 2},
2225 {UNUM_INTEGER_FIELD
, 2, 3},
2226 {UNUM_DECIMAL_SEPARATOR_FIELD
, 3, 4},
2227 {UNUM_SIGN_FIELD
, 8, 10},
2229 verifyAffixesAndPaddingInt32(
2235 expectedAttributes
);
2237 aap
.fPadPosition
= DigitAffixesAndPadding::kPadAfterSuffix
;
2239 NumberFormat2Test_Attributes expectedAttributes
[] = {
2240 {UNUM_SIGN_FIELD
, 0, 2},
2241 {UNUM_INTEGER_FIELD
, 2, 3},
2242 {UNUM_DECIMAL_SEPARATOR_FIELD
, 3, 4},
2243 {UNUM_SIGN_FIELD
, 4, 6},
2245 verifyAffixesAndPaddingInt32(
2251 expectedAttributes
);
2253 aap
.fPadPosition
= DigitAffixesAndPadding::kPadAfterSuffix
;
2256 digits
.set(-1234.5);
2257 NumberFormat2Test_Attributes expectedAttributes
[] = {
2258 {UNUM_SIGN_FIELD
, 0, 2},
2259 {UNUM_GROUPING_SEPARATOR_FIELD
, 3, 4},
2260 {UNUM_INTEGER_FIELD
, 2, 7},
2261 {UNUM_DECIMAL_SEPARATOR_FIELD
, 7, 8},
2262 {UNUM_FRACTION_FIELD
, 8, 9},
2263 {UNUM_SIGN_FIELD
, 9, 11},
2265 verifyAffixesAndPadding(
2271 expectedAttributes
);
2273 assertFalse("", aap
.needsPluralRules());
2276 aap
.fPositivePrefix
.remove();
2277 aap
.fPositiveSuffix
.remove();
2278 aap
.fNegativePrefix
.remove();
2279 aap
.fNegativeSuffix
.remove();
2281 // Set up for plural currencies.
2282 aap
.fNegativePrefix
.append("-", UNUM_SIGN_FIELD
);
2285 part
.setVariant("one", " Dollar", status
);
2286 part
.setVariant("other", " Dollars", status
);
2287 aap
.fPositiveSuffix
.append(part
, UNUM_CURRENCY_FIELD
, status
);
2289 aap
.fNegativeSuffix
= aap
.fPositiveSuffix
;
2291 LocalPointer
<PluralRules
> rules(PluralRules::forLocale("en", status
));
2292 if (!assertSuccess("", status
)) {
2296 // Exercise the fastrack path
2298 options
.fAlwaysShowDecimal
= FALSE
;
2299 NumberFormat2Test_Attributes expectedAttributes
[] = {
2300 {UNUM_SIGN_FIELD
, 0, 1},
2301 {UNUM_INTEGER_FIELD
, 1, 3},
2302 {UNUM_CURRENCY_FIELD
, 3, 11},
2304 verifyAffixesAndPaddingInt32(
2310 expectedAttributes
);
2311 options
.fAlwaysShowDecimal
= TRUE
;
2315 assertTrue("", aap
.needsPluralRules());
2317 NumberFormat2Test_Attributes expectedAttributes
[] = {
2318 {UNUM_INTEGER_FIELD
, 0, 1},
2319 {UNUM_DECIMAL_SEPARATOR_FIELD
, 1, 2},
2320 {UNUM_CURRENCY_FIELD
, 2, 9},
2322 verifyAffixesAndPaddingInt32(
2328 expectedAttributes
);
2331 NumberFormat2Test_Attributes expectedAttributes
[] = {
2332 {UNUM_SIGN_FIELD
, 0, 1},
2333 {UNUM_INTEGER_FIELD
, 1, 2},
2334 {UNUM_DECIMAL_SEPARATOR_FIELD
, 2, 3},
2335 {UNUM_CURRENCY_FIELD
, 3, 10},
2337 verifyAffixesAndPaddingInt32(
2343 expectedAttributes
);
2345 precision
.fMin
.setFracDigitCount(2);
2347 NumberFormat2Test_Attributes expectedAttributes
[] = {
2348 {UNUM_INTEGER_FIELD
, 0, 1},
2349 {UNUM_DECIMAL_SEPARATOR_FIELD
, 1, 2},
2350 {UNUM_FRACTION_FIELD
, 2, 4},
2351 {UNUM_CURRENCY_FIELD
, 4, 12},
2353 verifyAffixesAndPaddingInt32(
2359 expectedAttributes
);
2363 void NumberFormat2Test::TestPluralsAndRounding() {
2364 UErrorCode status
= U_ZERO_ERROR
;
2365 DecimalFormatSymbols
symbols("en", status
);
2366 if (!assertSuccess("", status
)) {
2369 DigitFormatter
formatter(symbols
);
2370 DigitGrouping grouping
;
2371 FixedPrecision precision
;
2372 precision
.fSignificant
.setMax(3);
2373 DigitFormatterOptions options
;
2375 vf
.prepareFixedDecimalFormatting(
2381 DigitAffixesAndPadding aap
;
2382 // Set up for plural currencies.
2383 aap
.fNegativePrefix
.append("-", UNUM_SIGN_FIELD
);
2386 part
.setVariant("one", " Dollar", status
);
2387 part
.setVariant("other", " Dollars", status
);
2388 aap
.fPositiveSuffix
.append(part
, UNUM_CURRENCY_FIELD
, status
);
2390 aap
.fNegativeSuffix
= aap
.fPositiveSuffix
;
2392 LocalPointer
<PluralRules
> rules(PluralRules::forLocale("en", status
));
2393 if (!assertSuccess("", status
)) {
2398 verifyAffixesAndPadding(
2408 verifyAffixesAndPadding(
2418 verifyAffixesAndPadding(
2426 precision
.fSignificant
.setMin(2);
2429 verifyAffixesAndPadding(
2439 verifyAffixesAndPadding(
2447 precision
.fSignificant
.setMin(0);
2449 digits
.set(-79.214);
2450 verifyAffixesAndPadding(
2458 // No more sig digits just max fractions
2459 precision
.fSignificant
.setMax(0);
2460 precision
.fMax
.setFracDigitCount(4);
2462 digits
.set(79.213562);
2463 verifyAffixesAndPadding(
2475 void NumberFormat2Test::TestPluralsAndRoundingScientific() {
2476 UErrorCode status
= U_ZERO_ERROR
;
2477 DecimalFormatSymbols
symbols("en", status
);
2478 if (!assertSuccess("", status
)) {
2481 DigitFormatter
formatter(symbols
);
2482 ScientificPrecision precision
;
2483 precision
.fMantissa
.fSignificant
.setMax(4);
2484 SciFormatterOptions options
;
2486 vf
.prepareScientificFormatting(
2491 DigitAffixesAndPadding aap
;
2492 aap
.fNegativePrefix
.append("-", UNUM_SIGN_FIELD
);
2495 part
.setVariant("one", " Meter", status
);
2496 part
.setVariant("other", " Meters", status
);
2497 aap
.fPositiveSuffix
.append(part
, UNUM_FIELD_COUNT
, status
);
2499 aap
.fNegativeSuffix
= aap
.fPositiveSuffix
;
2500 LocalPointer
<PluralRules
> rules(PluralRules::forLocale("en", status
));
2501 if (!assertSuccess("", status
)) {
2505 digits
.set(0.99996);
2506 NumberFormat2Test_Attributes expectedAttributes
[] = {
2507 {UNUM_INTEGER_FIELD
, 0, 1},
2508 {UNUM_EXPONENT_SYMBOL_FIELD
, 1, 2},
2509 {UNUM_EXPONENT_FIELD
, 2, 3},
2511 verifyAffixesAndPadding(
2517 expectedAttributes
);
2519 options
.fMantissa
.fAlwaysShowDecimal
= TRUE
;
2521 digits
.set(0.99996);
2522 NumberFormat2Test_Attributes expectedAttributes
[] = {
2523 {UNUM_INTEGER_FIELD
, 0, 1},
2524 {UNUM_DECIMAL_SEPARATOR_FIELD
, 1, 2},
2525 {UNUM_EXPONENT_SYMBOL_FIELD
, 2, 3},
2526 {UNUM_EXPONENT_FIELD
, 3, 4},
2528 verifyAffixesAndPadding(
2534 expectedAttributes
);
2537 digits
.set(-299792458.0);
2538 NumberFormat2Test_Attributes expectedAttributes
[] = {
2539 {UNUM_SIGN_FIELD
, 0, 1},
2540 {UNUM_INTEGER_FIELD
, 1, 2},
2541 {UNUM_DECIMAL_SEPARATOR_FIELD
, 2, 3},
2542 {UNUM_FRACTION_FIELD
, 3, 6},
2543 {UNUM_EXPONENT_SYMBOL_FIELD
, 6, 7},
2544 {UNUM_EXPONENT_FIELD
, 7, 8},
2546 verifyAffixesAndPadding(
2552 expectedAttributes
);
2554 precision
.fMantissa
.fSignificant
.setMin(4);
2555 options
.fExponent
.fAlwaysShowSign
= TRUE
;
2556 precision
.fMinExponentDigits
= 3;
2559 NumberFormat2Test_Attributes expectedAttributes
[] = {
2560 {UNUM_INTEGER_FIELD
, 0, 1},
2561 {UNUM_DECIMAL_SEPARATOR_FIELD
, 1, 2},
2562 {UNUM_FRACTION_FIELD
, 2, 5},
2563 {UNUM_EXPONENT_SYMBOL_FIELD
, 5, 6},
2564 {UNUM_EXPONENT_SIGN_FIELD
, 6, 7},
2565 {UNUM_EXPONENT_FIELD
, 7, 10},
2567 verifyAffixesAndPadding(
2568 "3.000E+000 Meters",
2573 expectedAttributes
);
2575 precision
.fMantissa
.fMax
.setIntDigitCount(3);
2577 digits
.set(0.00025001);
2578 NumberFormat2Test_Attributes expectedAttributes
[] = {
2579 {UNUM_INTEGER_FIELD
, 0, 3},
2580 {UNUM_DECIMAL_SEPARATOR_FIELD
, 3, 4},
2581 {UNUM_FRACTION_FIELD
, 4, 5},
2582 {UNUM_EXPONENT_SYMBOL_FIELD
, 5, 6},
2583 {UNUM_EXPONENT_SIGN_FIELD
, 6, 7},
2584 {UNUM_EXPONENT_FIELD
, 7, 10},
2586 verifyAffixesAndPadding(
2587 "250.0E-006 Meters",
2592 expectedAttributes
);
2595 digits
.set(0.0000025001);
2596 NumberFormat2Test_Attributes expectedAttributes
[] = {
2597 {UNUM_INTEGER_FIELD
, 0, 1},
2598 {UNUM_DECIMAL_SEPARATOR_FIELD
, 1, 2},
2599 {UNUM_FRACTION_FIELD
, 2, 5},
2600 {UNUM_EXPONENT_SYMBOL_FIELD
, 5, 6},
2601 {UNUM_EXPONENT_SIGN_FIELD
, 6, 7},
2602 {UNUM_EXPONENT_FIELD
, 7, 10},
2604 verifyAffixesAndPadding(
2605 "2.500E-006 Meters",
2610 expectedAttributes
);
2612 precision
.fMantissa
.fMax
.setFracDigitCount(1);
2614 digits
.set(0.0000025499);
2615 NumberFormat2Test_Attributes expectedAttributes
[] = {
2616 {UNUM_INTEGER_FIELD
, 0, 1},
2617 {UNUM_DECIMAL_SEPARATOR_FIELD
, 1, 2},
2618 {UNUM_FRACTION_FIELD
, 2, 3},
2619 {UNUM_EXPONENT_SYMBOL_FIELD
, 3, 4},
2620 {UNUM_EXPONENT_SIGN_FIELD
, 4, 5},
2621 {UNUM_EXPONENT_FIELD
, 5, 8},
2623 verifyAffixesAndPadding(
2629 expectedAttributes
);
2631 precision
.fMantissa
.fMax
.setIntDigitCount(1);
2632 precision
.fMantissa
.fMax
.setFracDigitCount(2);
2634 digits
.set((int32_t)299792458);
2635 verifyAffixesAndPadding(
2643 // clear significant digits
2644 precision
.fMantissa
.fSignificant
.setMin(0);
2645 precision
.fMantissa
.fSignificant
.setMax(0);
2647 // set int and fraction digits
2648 precision
.fMantissa
.fMin
.setFracDigitCount(2);
2649 precision
.fMantissa
.fMax
.setFracDigitCount(4);
2650 precision
.fMantissa
.fMin
.setIntDigitCount(2);
2651 precision
.fMantissa
.fMax
.setIntDigitCount(3);
2653 digits
.set(-0.0000025300001);
2654 verifyAffixesAndPadding(
2655 "-253.00E-008 Meters",
2663 digits
.set(-0.0000025300006);
2664 verifyAffixesAndPadding(
2665 "-253.0001E-008 Meters",
2673 digits
.set(-0.000025300006);
2674 verifyAffixesAndPadding(
2675 "-25.30E-006 Meters",
2685 void NumberFormat2Test::TestRoundingIncrement() {
2686 UErrorCode status
= U_ZERO_ERROR
;
2687 DecimalFormatSymbols
symbols("en", status
);
2688 if (U_FAILURE(status
)) {
2689 dataerrln("Error creating DecimalFormatSymbols - %s", u_errorName(status
));
2692 DigitFormatter
formatter(symbols
);
2693 ScientificPrecision precision
;
2694 SciFormatterOptions options
;
2695 precision
.fMantissa
.fRoundingIncrement
.set(0.25);
2696 precision
.fMantissa
.fSignificant
.setMax(4);
2697 DigitGrouping grouping
;
2701 vf
.prepareFixedDecimalFormatting(
2704 precision
.fMantissa
,
2707 DigitAffixesAndPadding aap
;
2708 aap
.fNegativePrefix
.append("-", UNUM_SIGN_FIELD
);
2711 verifyAffixesAndPadding(
2720 verifyAffixesAndPadding(
2729 verifyAffixesAndPadding(
2736 precision
.fMantissa
.fMin
.setFracDigitCount(2);
2739 verifyAffixesAndPadding(
2747 digits
.set(-639.65);
2748 verifyAffixesAndPadding(
2756 precision
.fMantissa
.fMin
.setIntDigitCount(2);
2757 // Scientific notation
2758 vf
.prepareScientificFormatting(
2763 digits
.set(-6396.5);
2764 verifyAffixesAndPadding(
2772 digits
.set(-0.00092374);
2773 verifyAffixesAndPadding(
2780 precision
.fMantissa
.fMax
.setIntDigitCount(3);
2782 digits
.set(-0.00092374);
2783 verifyAffixesAndPadding(
2792 void NumberFormat2Test::TestToPatternScientific11648() {
2794 UErrorCode status = U_ZERO_ERROR;
2796 DecimalFormat2 fmt(en, "0.00", status);
2797 fmt.setScientificNotation(TRUE);
2798 UnicodeString pattern;
2799 // Fails, produces "0.00E"
2800 assertEquals("", "0.00E0", fmt.toPattern(pattern));
2801 DecimalFormat fmt2(pattern, status);
2802 // Fails, bad pattern.
2803 assertSuccess("", status);
2807 void NumberFormat2Test::verifyAffixesAndPadding(
2808 const UnicodeString
&expected
,
2809 const DigitAffixesAndPadding
&aaf
,
2811 const ValueFormatter
&vf
,
2812 const PluralRules
*optPluralRules
,
2813 const NumberFormat2Test_Attributes
*expectedAttributes
) {
2814 UnicodeString appendTo
;
2815 NumberFormat2Test_FieldPositionHandler handler
;
2816 UErrorCode status
= U_ZERO_ERROR
;
2827 if (!assertSuccess("", status
)) {
2830 if (expectedAttributes
!= NULL
) {
2831 verifyAttributes(expectedAttributes
, handler
.attributes
);
2835 void NumberFormat2Test::verifyAffixesAndPaddingInt32(
2836 const UnicodeString
&expected
,
2837 const DigitAffixesAndPadding
&aaf
,
2839 const ValueFormatter
&vf
,
2840 const PluralRules
*optPluralRules
,
2841 const NumberFormat2Test_Attributes
*expectedAttributes
) {
2842 UnicodeString appendTo
;
2843 NumberFormat2Test_FieldPositionHandler handler
;
2844 UErrorCode status
= U_ZERO_ERROR
;
2855 if (!assertSuccess("", status
)) {
2858 if (expectedAttributes
!= NULL
) {
2859 verifyAttributes(expectedAttributes
, handler
.attributes
);
2863 verifyAffixesAndPadding(
2864 expected
, aaf
, digits
, vf
, optPluralRules
, expectedAttributes
);
2867 void NumberFormat2Test::verifyAffix(
2868 const UnicodeString
&expected
,
2869 const DigitAffix
&affix
,
2870 const NumberFormat2Test_Attributes
*expectedAttributes
) {
2871 UnicodeString appendTo
;
2872 NumberFormat2Test_FieldPositionHandler handler
;
2875 expected
.unescape(),
2876 affix
.format(handler
, appendTo
));
2877 if (expectedAttributes
!= NULL
) {
2878 verifyAttributes(expectedAttributes
, handler
.attributes
);
2882 // Right now only works for positive values.
2883 void NumberFormat2Test::verifyDigitList(
2884 const UnicodeString
&expected
,
2885 const DigitList
&digits
) {
2886 DigitFormatter formatter
;
2887 DigitGrouping grouping
;
2888 VisibleDigits visibleDigits
;
2889 FixedPrecision precision
;
2890 precision
.fMin
.setIntDigitCount(0);
2891 DigitFormatterOptions options
;
2892 UErrorCode status
= U_ZERO_ERROR
;
2893 DigitList
dlCopy(digits
);
2894 precision
.initVisibleDigits(
2895 dlCopy
, visibleDigits
, status
);
2896 if (!assertSuccess("", status
)) {
2899 verifyDigitFormatter(
2908 void NumberFormat2Test::verifyVisibleDigits(
2909 const UnicodeString
&expected
,
2911 const VisibleDigits
&digits
) {
2912 DigitFormatter formatter
;
2913 DigitGrouping grouping
;
2914 DigitFormatterOptions options
;
2915 verifyDigitFormatter(
2922 if (digits
.isNegative() != bNegative
) {
2923 errln(expected
+ ": Wrong sign.");
2925 if (digits
.isNaN() || digits
.isInfinite()) {
2926 errln(expected
+ ": Require real value.");
2930 void NumberFormat2Test::verifyVisibleDigitsWithExponent(
2931 const UnicodeString
&expected
,
2933 const VisibleDigitsWithExponent
&digits
) {
2934 DigitFormatter formatter
;
2935 SciFormatterOptions options
;
2942 if (digits
.isNegative() != bNegative
) {
2943 errln(expected
+ ": Wrong sign.");
2945 if (digits
.isNaN() || digits
.isInfinite()) {
2946 errln(expected
+ ": Require real value.");
2950 void NumberFormat2Test::verifySciFormatter(
2951 const UnicodeString
&expected
,
2952 const DigitFormatter
&formatter
,
2953 const VisibleDigitsWithExponent
&digits
,
2954 const SciFormatterOptions
&options
,
2955 const NumberFormat2Test_Attributes
*expectedAttributes
) {
2958 expected
.countChar32(),
2959 formatter
.countChar32(digits
, options
));
2960 UnicodeString appendTo
;
2961 NumberFormat2Test_FieldPositionHandler handler
;
2970 if (expectedAttributes
!= NULL
) {
2971 verifyAttributes(expectedAttributes
, handler
.attributes
);
2975 void NumberFormat2Test::verifyPositiveIntDigitFormatter(
2976 const UnicodeString
&expected
,
2977 const DigitFormatter
&formatter
,
2981 const NumberFormat2Test_Attributes
*expectedAttributes
) {
2982 IntDigitCountRange
range(minDigits
, maxDigits
);
2983 UnicodeString appendTo
;
2984 NumberFormat2Test_FieldPositionHandler handler
;
2988 formatter
.formatPositiveInt32(
2993 if (expectedAttributes
!= NULL
) {
2994 verifyAttributes(expectedAttributes
, handler
.attributes
);
2998 void NumberFormat2Test::verifyDigitFormatter(
2999 const UnicodeString
&expected
,
3000 const DigitFormatter
&formatter
,
3001 const VisibleDigits
&digits
,
3002 const DigitGrouping
&grouping
,
3003 const DigitFormatterOptions
&options
,
3004 const NumberFormat2Test_Attributes
*expectedAttributes
) {
3007 expected
.countChar32(),
3008 formatter
.countChar32(digits
, grouping
, options
));
3009 UnicodeString appendTo
;
3010 NumberFormat2Test_FieldPositionHandler handler
;
3020 if (expectedAttributes
!= NULL
) {
3021 verifyAttributes(expectedAttributes
, handler
.attributes
);
3025 void NumberFormat2Test::verifySmallIntFormatter(
3026 const UnicodeString
&expected
,
3027 int32_t positiveValue
,
3029 int32_t maxDigits
) {
3030 IntDigitCountRange
range(minDigits
, maxDigits
);
3031 if (!SmallIntFormatter::canFormat(positiveValue
, range
)) {
3032 UnicodeString actual
;
3033 assertEquals("", expected
, actual
);
3036 UnicodeString actual
;
3037 assertEquals("", expected
, SmallIntFormatter::format(positiveValue
, range
, actual
));
3040 void NumberFormat2Test::verifyAttributes(
3041 const NumberFormat2Test_Attributes
*expected
,
3042 const NumberFormat2Test_Attributes
*actual
) {
3044 while (expected
[idx
].spos
!= -1 && actual
[idx
].spos
!= -1) {
3045 assertEquals("id", expected
[idx
].id
, actual
[idx
].id
);
3046 assertEquals("spos", expected
[idx
].spos
, actual
[idx
].spos
);
3047 assertEquals("epos", expected
[idx
].epos
, actual
[idx
].epos
);
3051 "expected and actual not same length",
3056 void NumberFormat2Test::verifyIntValue(
3057 int64_t expected
, const VisibleDigits
&digits
) {
3058 double unusedSource
;
3063 UBool unusedHasIntValue
;
3064 digits
.getFixedDecimal(
3065 unusedSource
, intValue
, unusedF
,
3066 unusedT
, unusedV
, unusedHasIntValue
);
3067 assertEquals("", expected
, intValue
);
3070 void NumberFormat2Test::verifySource(
3071 double expected
, const VisibleDigits
&digits
) {
3073 int64_t unusedIntValue
;
3077 UBool unusedHasIntValue
;
3078 digits
.getFixedDecimal(
3079 source
, unusedIntValue
, unusedF
,
3080 unusedT
, unusedV
, unusedHasIntValue
);
3081 if (expected
!= source
) {
3082 errln("Expected %f, got %f instead", expected
, source
);
3086 extern IntlTest
*createNumberFormat2Test() {
3087 return new NumberFormat2Test();
3090 #endif /* !UCONFIG_NO_FORMATTING */