]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/numfmtspectest.cpp
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / numfmtspectest.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b331163b
A
3/*
4*******************************************************************************
2ca993e8 5* Copyright (C) 2014-2015, International Business Machines Corporation and *
b331163b
A
6* others. All Rights Reserved. *
7*******************************************************************************
8*
9* File numfmtspectest.cpp
10*
11*******************************************************************************
12*/
13#include <stdio.h>
14#include <stdlib.h>
15
16#include "intltest.h"
17
18#if !UCONFIG_NO_FORMATTING
19
20#include "unicode/localpointer.h"
21#include "unicode/decimfmt.h"
22#include "unicode/dtfmtsym.h"
23#include "uassert.h"
24
25static const UChar kJPY[] = {0x4A, 0x50, 0x59};
26
27static void fixNonBreakingSpace(UnicodeString &str) {
28 for (int32_t i = 0; i < str.length(); ++i) {
29 if (str[i] == 0xa0) {
30 str.setCharAt(i, 0x20);
31 }
32 }
33}
34
35static NumberFormat *nfWithPattern(const char *pattern) {
36 UnicodeString upattern(pattern, -1, US_INV);
37 upattern = upattern.unescape();
38 UErrorCode status = U_ZERO_ERROR;
39 DecimalFormat *result = new DecimalFormat(
40 upattern, new DecimalFormatSymbols("fr", status), status);
2ca993e8
A
41 if (U_FAILURE(status)) {
42 return NULL;
43 }
44
b331163b
A
45 return result;
46}
47
48static UnicodeString format(double d, const NumberFormat &fmt) {
49 UnicodeString result;
50 fmt.format(d, result);
51 fixNonBreakingSpace(result);
52 return result;
53}
54
55class NumberFormatSpecificationTest : public IntlTest {
56public:
57 NumberFormatSpecificationTest() {
58 }
59 void TestBasicPatterns();
60 void TestNfSetters();
61 void TestRounding();
62 void TestSignificantDigits();
63 void TestScientificNotation();
64 void TestPercent();
65 void TestPerMilli();
66 void TestPadding();
67 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
68private:
69 void assertPatternFr(
70 const char *expected, double x, const char *pattern, UBool possibleDataError=FALSE);
71
72};
73
74void NumberFormatSpecificationTest::runIndexedTest(
75 int32_t index, UBool exec, const char *&name, char *) {
76 if (exec) {
77 logln("TestSuite NumberFormatSpecificationTest: ");
78 }
79 TESTCASE_AUTO_BEGIN;
80 TESTCASE_AUTO(TestBasicPatterns);
81 TESTCASE_AUTO(TestNfSetters);
82 TESTCASE_AUTO(TestRounding);
83 TESTCASE_AUTO(TestSignificantDigits);
84 TESTCASE_AUTO(TestScientificNotation);
85 TESTCASE_AUTO(TestPercent);
86 TESTCASE_AUTO(TestPerMilli);
87 TESTCASE_AUTO(TestPadding);
88 TESTCASE_AUTO_END;
89}
90
91void NumberFormatSpecificationTest::TestBasicPatterns() {
3d1f044b 92 assertPatternFr("1\\u202F234,57", 1234.567, "#,##0.##", TRUE);
b331163b
A
93 assertPatternFr("1234,57", 1234.567, "0.##", TRUE);
94 assertPatternFr("1235", 1234.567, "0", TRUE);
3d1f044b 95 assertPatternFr("1\\u202F234,567", 1234.567, "#,##0.###", TRUE);
b331163b
A
96 assertPatternFr("1234,567", 1234.567, "###0.#####", TRUE);
97 assertPatternFr("1234,5670", 1234.567, "###0.0000#", TRUE);
98 assertPatternFr("01234,5670", 1234.567, "00000.0000", TRUE);
3d1f044b 99 assertPatternFr("1\\u202F234,57 \\u20ac", 1234.567, "#,##0.00 \\u00a4", TRUE);
b331163b
A
100}
101
102void NumberFormatSpecificationTest::TestNfSetters() {
103 LocalPointer<NumberFormat> nf(nfWithPattern("#,##0.##"));
2ca993e8
A
104 if (nf == NULL) {
105 dataerrln("Error creating NumberFormat");
106 return;
107 }
b331163b
A
108 nf->setMaximumIntegerDigits(5);
109 nf->setMinimumIntegerDigits(4);
3d1f044b
A
110 assertEquals("", u"34\u202F567,89", format(1234567.89, *nf), TRUE);
111 assertEquals("", u"0\u202F034,56", format(34.56, *nf), TRUE);
b331163b
A
112}
113
114void NumberFormatSpecificationTest::TestRounding() {
115 assertPatternFr("1,0", 1.25, "0.5", TRUE);
116 assertPatternFr("2,0", 1.75, "0.5", TRUE);
117 assertPatternFr("-1,0", -1.25, "0.5", TRUE);
118 assertPatternFr("-02,0", -1.75, "00.5", TRUE);
119 assertPatternFr("0", 2.0, "4", TRUE);
120 assertPatternFr("8", 6.0, "4", TRUE);
121 assertPatternFr("8", 10.0, "4", TRUE);
122 assertPatternFr("99,90", 99.0, "2.70", TRUE);
123 assertPatternFr("273,00", 272.0, "2.73", TRUE);
3d1f044b 124 assertPatternFr("1\\u202F03,60", 104.0, "#,#3.70", TRUE);
b331163b
A
125}
126
127void NumberFormatSpecificationTest::TestSignificantDigits() {
128 assertPatternFr("1230", 1234.0, "@@@", TRUE);
3d1f044b
A
129 assertPatternFr("1\\u202F234", 1234.0, "@,@@@", TRUE);
130 assertPatternFr("1\\u202F235\\u202F000", 1234567.0, "@,@@@", TRUE);
131 assertPatternFr("1\\u202F234\\u202F567", 1234567.0, "@@@@,@@@", TRUE);
132 assertPatternFr("12\\u202F34\\u202F567,00", 1234567.0, "@@@@,@@,@@@", TRUE);
133 assertPatternFr("12\\u202F34\\u202F567,0", 1234567.0, "@@@@,@@,@@#", TRUE);
134 assertPatternFr("12\\u202F34\\u202F567", 1234567.0, "@@@@,@@,@##", TRUE);
135 assertPatternFr("12\\u202F34\\u202F567", 1234567.001, "@@@@,@@,@##", TRUE);
136 assertPatternFr("12\\u202F34\\u202F567", 1234567.001, "@@@@,@@,###", TRUE);
137 assertPatternFr("1\\u202F200", 1234.0, "#,#@@", TRUE);
b331163b
A
138}
139
140void NumberFormatSpecificationTest::TestScientificNotation() {
141 assertPatternFr("1,23E4", 12345.0, "0.00E0", TRUE);
142 assertPatternFr("123,00E2", 12300.0, "000.00E0", TRUE);
143 assertPatternFr("123,0E2", 12300.0, "000.0#E0", TRUE);
144 assertPatternFr("123,0E2", 12300.1, "000.0#E0", TRUE);
145 assertPatternFr("123,01E2", 12301.0, "000.0#E0", TRUE);
146 assertPatternFr("123,01E+02", 12301.0, "000.0#E+00", TRUE);
147 assertPatternFr("12,3E3", 12345.0, "##0.00E0", TRUE);
148 assertPatternFr("12,300E3", 12300.1, "##0.0000E0", TRUE);
149 assertPatternFr("12,30E3", 12300.1, "##0.000#E0", TRUE);
150 assertPatternFr("12,301E3", 12301.0, "##0.000#E0", TRUE);
3d1f044b 151 assertPatternFr("1,25E4", 12301.2, "0.05E0");
b331163b
A
152 assertPatternFr("170,0E-3", 0.17, "##0.000#E0", TRUE);
153
154}
155
156void NumberFormatSpecificationTest::TestPercent() {
157 assertPatternFr("57,3%", 0.573, "0.0%", TRUE);
158 assertPatternFr("%57,3", 0.573, "%0.0", TRUE);
159 assertPatternFr("p%p57,3", 0.573, "p%p0.0", TRUE);
160 assertPatternFr("p%p0,6", 0.573, "p'%'p0.0", TRUE);
161 assertPatternFr("%3,260", 0.0326, "%@@@@", TRUE);
3d1f044b
A
162 assertPatternFr("%1\\u202F540", 15.43, "%#,@@@", TRUE);
163 assertPatternFr("%1\\u202F656,4", 16.55, "%#,##4.1", TRUE);
b331163b
A
164 assertPatternFr("%16,3E3", 162.55, "%##0.00E0", TRUE);
165}
166
167void NumberFormatSpecificationTest::TestPerMilli() {
168 assertPatternFr("573,0\\u2030", 0.573, "0.0\\u2030", TRUE);
169 assertPatternFr("\\u2030573,0", 0.573, "\\u20300.0", TRUE);
170 assertPatternFr("p\\u2030p573,0", 0.573, "p\\u2030p0.0", TRUE);
171 assertPatternFr("p\\u2030p0,6", 0.573, "p'\\u2030'p0.0", TRUE);
172 assertPatternFr("\\u203032,60", 0.0326, "\\u2030@@@@", TRUE);
3d1f044b
A
173 assertPatternFr("\\u203015\\u202F400", 15.43, "\\u2030#,@@@", TRUE);
174 assertPatternFr("\\u203016\\u202F551,7", 16.55, "\\u2030#,##4.1", TRUE);
b331163b
A
175 assertPatternFr("\\u2030163E3", 162.55, "\\u2030##0.00E0", TRUE);
176}
177
178void NumberFormatSpecificationTest::TestPadding() {
3d1f044b
A
179 assertPatternFr("$***1\\u202F234", 1234, "$**####,##0", TRUE);
180 assertPatternFr("xxx$1\\u202F234", 1234, "*x$####,##0", TRUE);
181 assertPatternFr("1\\u202F234xxx$", 1234, "####,##0*x$", TRUE);
182 assertPatternFr("1\\u202F234$xxx", 1234, "####,##0$*x", TRUE);
183 assertPatternFr("ne1\\u202F234nx", -1234, "####,##0$*x;ne#n", TRUE);
184 assertPatternFr("n1\\u202F234*xx", -1234, "####,##0$*x;n#'*'", TRUE);
b331163b 185 assertPatternFr("yyyy%432,6", 4.33, "*y%4.2######", TRUE);
3d1f044b
A
186 assertPatternFr("EUR *433,00", 433.0, "\\u00a4\\u00a4 **####0.00");
187 assertPatternFr("EUR *433,00", 433.0, "\\u00a4\\u00a4 **#######0");
b331163b
A
188 {
189 UnicodeString upattern("\\u00a4\\u00a4 **#######0", -1, US_INV);
190 upattern = upattern.unescape();
191 UErrorCode status = U_ZERO_ERROR;
192 UnicodeString result;
193 DecimalFormat fmt(
194 upattern, new DecimalFormatSymbols("fr", status), status);
2ca993e8
A
195 if (U_FAILURE(status)) {
196 dataerrln("Error creating DecimalFormat - %s", u_errorName(status));
197 } else {
198 fmt.setCurrency(kJPY);
199 fmt.format(433.22, result);
200 assertSuccess("", status);
201 assertEquals("", "JPY ****433", result, TRUE);
202 }
b331163b
A
203 }
204 {
205 UnicodeString upattern(
206 "\\u00a4\\u00a4 **#######0;\\u00a4\\u00a4 (#)", -1, US_INV);
207 upattern = upattern.unescape();
208 UErrorCode status = U_ZERO_ERROR;
209 UnicodeString result;
210 DecimalFormat fmt(
211 upattern,
212 new DecimalFormatSymbols("en_US", status),
213 status);
2ca993e8
A
214 if (U_FAILURE(status)) {
215 dataerrln("Error creating DecimalFormat - %s", u_errorName(status));
216 } else {
217 fmt.format(-433.22, result);
218 assertSuccess("", status);
219 assertEquals("", "USD (433.22)", result, TRUE);
220 }
b331163b
A
221 }
222 const char *paddedSciPattern = "QU**00.#####E0";
223 assertPatternFr("QU***43,3E-1", 4.33, paddedSciPattern, TRUE);
224 {
225 UErrorCode status = U_ZERO_ERROR;
226 DecimalFormatSymbols *sym = new DecimalFormatSymbols("fr", status);
227 sym->setSymbol(DecimalFormatSymbols::kExponentialSymbol, "EE");
228 DecimalFormat fmt(
229 paddedSciPattern,
230 sym,
231 status);
2ca993e8
A
232 if (U_FAILURE(status)) {
233 dataerrln("Error creating DecimalFormat - %s", u_errorName(status));
234 } else {
235 UnicodeString result;
236 fmt.format(4.33, result);
237 assertSuccess("", status);
238 assertEquals("", "QU**43,3EE-1", result, TRUE);
239 }
b331163b
A
240 }
241 // padding cannot work as intended with scientific notation.
242 assertPatternFr("QU**43,32E-1", 4.332, paddedSciPattern, TRUE);
243}
244
245void NumberFormatSpecificationTest::assertPatternFr(
246 const char *expected,
247 double x,
248 const char *pattern,
249 UBool possibleDataError) {
250 UnicodeString upattern(pattern, -1, US_INV);
251 UnicodeString uexpected(expected, -1, US_INV);
252 upattern = upattern.unescape();
253 uexpected = uexpected.unescape();
254 UErrorCode status = U_ZERO_ERROR;
255 UnicodeString result;
256 DecimalFormat fmt(
257 upattern, new DecimalFormatSymbols("fr_FR", status), status);
2ca993e8
A
258 if (U_FAILURE(status)) {
259 dataerrln("Error creating DecimalFormatSymbols - %s", u_errorName(status));
260 return;
261 }
b331163b
A
262 fmt.format(x, result);
263 fixNonBreakingSpace(result);
264 assertSuccess("", status);
265 assertEquals("", uexpected, result, possibleDataError);
266}
267
268extern IntlTest *createNumberFormatSpecificationTest() {
269 return new NumberFormatSpecificationTest();
270}
271
272#endif