]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
51004dcb A |
3 | /* |
4 | ******************************************************************************* | |
2ca993e8 | 5 | * Copyright (C) 1997-2015, International Business Machines Corporation and * |
51004dcb A |
6 | * others. All Rights Reserved. * |
7 | ******************************************************************************* | |
8 | * | |
9 | * File COMPACTDECIMALFORMATTEST.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/compactdecimalformat.h" | |
21 | #include "unicode/unum.h" | |
b331163b | 22 | #include "cmemory.h" |
51004dcb A |
23 | |
24 | typedef struct ExpectedResult { | |
25 | double value; | |
26 | const char *expected; | |
27 | } ExpectedResult; | |
28 | ||
29 | static const char *kShortStr = "Short"; | |
30 | static const char *kLongStr = "Long"; | |
31 | ||
32 | static ExpectedResult kEnglishShort[] = { | |
57a6839d | 33 | {0.0, "0"}, |
51004dcb A |
34 | {0.17, "0.17"}, |
35 | {1.0, "1"}, | |
36 | {1234.0, "1.2K"}, | |
37 | {12345.0, "12K"}, | |
38 | {123456.0, "120K"}, | |
39 | {1234567.0, "1.2M"}, | |
40 | {12345678.0, "12M"}, | |
41 | {123456789.0, "120M"}, | |
42 | {1.23456789E9, "1.2B"}, | |
43 | {1.23456789E10, "12B"}, | |
44 | {1.23456789E11, "120B"}, | |
45 | {1.23456789E12, "1.2T"}, | |
46 | {1.23456789E13, "12T"}, | |
47 | {1.23456789E14, "120T"}, | |
48 | {1.23456789E15, "1200T"}}; | |
49 | ||
50 | static ExpectedResult kSerbianShort[] = { | |
b331163b A |
51 | {1234.0, "1,2\\u00a0\\u0445\\u0438\\u0459."}, |
52 | {12345.0, "12\\u00a0\\u0445\\u0438\\u0459."}, | |
53 | {20789.0, "21\\u00a0\\u0445\\u0438\\u0459."}, | |
54 | {123456.0, "120\\u00a0\\u0445\\u0438\\u0459."}, | |
55 | {1234567.0, "1,2\\u00A0\\u043C\\u0438\\u043B."}, | |
56 | {12345678.0, "12\\u00A0\\u043C\\u0438\\u043B."}, | |
57 | {123456789.0, "120\\u00A0\\u043C\\u0438\\u043B."}, | |
58 | {1.23456789E9, "1,2\\u00A0\\u043C\\u043B\\u0440\\u0434."}, | |
59 | {1.23456789E10, "12\\u00A0\\u043C\\u043B\\u0440\\u0434."}, | |
60 | {1.23456789E11, "120\\u00A0\\u043C\\u043B\\u0440\\u0434."}, | |
61 | {1.23456789E12, "1,2\\u00A0\\u0431\\u0438\\u043B."}, | |
62 | {1.23456789E13, "12\\u00A0\\u0431\\u0438\\u043B."}, | |
63 | {1.23456789E14, "120\\u00A0\\u0431\\u0438\\u043B."}, | |
64 | {1.23456789E15, "1200\\u00A0\\u0431\\u0438\\u043B."}}; | |
51004dcb A |
65 | |
66 | static ExpectedResult kSerbianLong[] = { | |
57a6839d A |
67 | {1234.0, "1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few |
68 | {12345.0, "12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other | |
69 | {21789.0, "22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few | |
70 | {123456.0, "120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other | |
71 | {999999.0, "1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"}, // 10^6 one | |
72 | {1234567.0, "1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 few | |
73 | {12345678.0, "12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 other | |
74 | {123456789.0, "120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6 other | |
75 | {1.23456789E9, "1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, // 10^9 few | |
76 | {1.23456789E10, "12 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, // 10^9 other | |
77 | {2.08901234E10, "21 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0430"}, // 10^9 one | |
78 | {2.18901234E10, "22 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, // 10^9 few | |
79 | {1.23456789E11, "120 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, // 10^9 other | |
f3c0d7a5 A |
80 | {1.23456789E12, "1,2 \\u0431\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^12 few |
81 | {1.23456789E13, "12 \\u0431\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^12 other | |
82 | {1.23456789E14, "120 \\u0431\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^12 other | |
83 | {1.23456789E15, "1200 \\u0431\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}}; // 10^12 other | |
51004dcb A |
84 | |
85 | static ExpectedResult kSerbianLongNegative[] = { | |
57a6839d | 86 | {-1234.0, "-1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, |
51004dcb A |
87 | {-12345.0, "-12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, |
88 | {-21789.0, "-22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, | |
89 | {-123456.0, "-120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, | |
90 | {-999999.0, "-1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"}, | |
91 | {-1234567.0, "-1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, | |
92 | {-12345678.0, "-12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, | |
93 | {-123456789.0, "-120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, | |
57a6839d | 94 | {-1.23456789E9, "-1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, |
51004dcb A |
95 | {-1.23456789E10, "-12 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, |
96 | {-2.08901234E10, "-21 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0430"}, | |
97 | {-2.18901234E10, "-22 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"}, | |
98 | {-1.23456789E11, "-120 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0438"}, | |
f3c0d7a5 A |
99 | {-1.23456789E12, "-1,2 \\u0431\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, |
100 | {-1.23456789E13, "-12 \\u0431\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, | |
101 | {-1.23456789E14, "-120 \\u0431\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, | |
102 | {-1.23456789E15, "-1200 \\u0431\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}}; | |
51004dcb A |
103 | |
104 | static ExpectedResult kJapaneseShort[] = { | |
2ca993e8 | 105 | {1234.0, "1200"}, |
51004dcb A |
106 | {12345.0, "1.2\\u4E07"}, |
107 | {123456.0, "12\\u4E07"}, | |
108 | {1234567.0, "120\\u4E07"}, | |
109 | {12345678.0, "1200\\u4E07"}, | |
110 | {123456789.0, "1.2\\u5104"}, | |
111 | {1.23456789E9, "12\\u5104"}, | |
112 | {1.23456789E10, "120\\u5104"}, | |
113 | {1.23456789E11, "1200\\u5104"}, | |
114 | {1.23456789E12, "1.2\\u5146"}, | |
115 | {1.23456789E13, "12\\u5146"}, | |
116 | {1.23456789E14, "120\\u5146"}}; | |
117 | ||
118 | static ExpectedResult kSwahiliShort[] = { | |
119 | {1234.0, "elfu\\u00a01.2"}, | |
120 | {12345.0, "elfu\\u00a012"}, | |
b331163b | 121 | {123456.0, "elfu\\u00a0120"}, |
51004dcb A |
122 | {1234567.0, "M1.2"}, |
123 | {12345678.0, "M12"}, | |
124 | {123456789.0, "M120"}, | |
125 | {1.23456789E9, "B1.2"}, | |
126 | {1.23456789E10, "B12"}, | |
127 | {1.23456789E11, "B120"}, | |
128 | {1.23456789E12, "T1.2"}, | |
129 | {1.23456789E13, "T12"}, | |
130 | {1.23456789E15, "T1200"}}; | |
131 | ||
132 | static ExpectedResult kCsShort[] = { | |
133 | {1000.0, "1\\u00a0tis."}, | |
134 | {1500.0, "1,5\\u00a0tis."}, | |
135 | {5000.0, "5\\u00a0tis."}, | |
136 | {23000.0, "23\\u00a0tis."}, | |
137 | {127123.0, "130\\u00a0tis."}, | |
138 | {1271234.0, "1,3\\u00a0mil."}, | |
139 | {12712345.0, "13\\u00a0mil."}, | |
140 | {127123456.0, "130\\u00a0mil."}, | |
141 | {1.27123456E9, "1,3\\u00a0mld."}, | |
142 | {1.27123456E10, "13\\u00a0mld."}, | |
143 | {1.27123456E11, "130\\u00a0mld."}, | |
144 | {1.27123456E12, "1,3\\u00a0bil."}, | |
145 | {1.27123456E13, "13\\u00a0bil."}, | |
146 | {1.27123456E14, "130\\u00a0bil."}}; | |
147 | ||
148 | static ExpectedResult kSkLong[] = { | |
149 | {1000.0, "1 tis\\u00edc"}, | |
2ca993e8 A |
150 | {1572.0, "1,6 tis\\u00edca"}, |
151 | {5184.0, "5,2 tis\\u00edca"}}; | |
51004dcb A |
152 | |
153 | static ExpectedResult kSwahiliShortNegative[] = { | |
154 | {-1234.0, "elfu\\u00a0-1.2"}, | |
155 | {-12345.0, "elfu\\u00a0-12"}, | |
b331163b | 156 | {-123456.0, "elfu\\u00a0-120"}, |
51004dcb A |
157 | {-1234567.0, "M-1.2"}, |
158 | {-12345678.0, "M-12"}, | |
159 | {-123456789.0, "M-120"}, | |
160 | {-1.23456789E9, "B-1.2"}, | |
161 | {-1.23456789E10, "B-12"}, | |
162 | {-1.23456789E11, "B-120"}, | |
163 | {-1.23456789E12, "T-1.2"}, | |
164 | {-1.23456789E13, "T-12"}, | |
165 | {-1.23456789E15, "T-1200"}}; | |
166 | ||
167 | static ExpectedResult kArabicLong[] = { | |
f3c0d7a5 | 168 | {-5300.0, "\\u061C-\\u0665\\u066B\\u0663 \\u0623\\u0644\\u0641"}}; |
51004dcb | 169 | |
0f5d89e8 A |
170 | static ExpectedResult kChineseCurrencyTestData[] = { |
171 | {1.0, "\\uFFE51"}, | |
172 | {12.0, "\\uFFE512"}, | |
173 | {123.0, "\\uFFE5120"}, | |
174 | {1234.0, "\\uFFE51200"}, | |
175 | {12345.0, "\\uFFE51.2\\u4E07"}, | |
176 | {123456.0, "\\uFFE512\\u4E07"}, | |
177 | {1234567.0, "\\uFFE5120\\u4E07"}, | |
178 | {12345678.0, "\\uFFE51200\\u4E07"}, | |
179 | {123456789.0, "\\uFFE51.2\\u4EBF"}, | |
180 | {1234567890.0, "\\uFFE512\\u4EBF"}, | |
181 | {12345678901.0, "\\uFFE5120\\u4EBF"}, | |
182 | {123456789012.0, "\\uFFE51200\\u4EBF"}, | |
183 | {1234567890123.0, "\\uFFE51.2\\u5146"}, | |
184 | {12345678901234.0, "\\uFFE512\\u5146"}, | |
185 | {123456789012345.0, "\\uFFE5120\\u5146"}, | |
186 | }; | |
187 | static ExpectedResult kGermanCurrencyTestData[] = { | |
188 | {1.0, u8"1\\u00A0\\u20AC"}, | |
189 | {12.0, u8"12\\u00A0\\u20AC"}, | |
190 | {123.0, u8"120\\u00A0\\u20AC"}, | |
191 | {1234.0, u8"1200\\u00A0\\u20AC"}, | |
192 | {12345.0, u8"12.000\\u00A0\\u20AC"}, | |
193 | {123456.0, u8"120.000\\u00A0\\u20AC"}, | |
194 | {1234567.0, u8"1,2\\u00A0Mio.\\u00A0\\u20AC"}, | |
195 | {12345678.0, u8"12\\u00A0Mio.\\u00A0\\u20AC"}, | |
196 | {123456789.0, u8"120\\u00A0Mio.\\u00A0\\u20AC"}, | |
197 | {1234567890.0, u8"1,2\\u00A0Mrd.\\u00A0\\u20AC"}, | |
198 | {12345678901.0, u8"12\\u00A0Mrd.\\u00A0\\u20AC"}, | |
199 | {123456789012.0, u8"120\\u00A0Mrd.\\u00A0\\u20AC"}, | |
200 | {1234567890123.0, u8"1,2\\u00A0Bio.\\u00A0\\u20AC"}, | |
201 | {12345678901234.0, u8"12\\u00A0Bio.\\u00A0\\u20AC"}, | |
202 | {123456789012345.0, u8"120\\u00A0Bio.\\u00A0\\u20AC"}, | |
203 | }; | |
204 | static ExpectedResult kEnglishCurrencyTestData[] = { | |
205 | {1.0, u8"$1"}, | |
206 | {12.0, u8"$12"}, | |
207 | {123.0, u8"$120"}, | |
208 | {1234.0, u8"$1.2K"}, | |
209 | {12345.0, u8"$12K"}, | |
210 | {123456.0, u8"$120K"}, | |
211 | {1234567.0, u8"$1.2M"}, | |
212 | {12345678.0, u8"$12M"}, | |
213 | {123456789.0, u8"$120M"}, | |
214 | {1234567890.0, u8"$1.2B"}, | |
215 | {12345678901.0, u8"$12B"}, | |
216 | {123456789012.0, u8"$120B"}, | |
217 | {1234567890123.0, u8"$1.2T"}, | |
218 | {12345678901234.0, u8"$12T"}, | |
219 | {123456789012345.0, u8"$120T"}, | |
220 | }; | |
221 | ||
51004dcb A |
222 | |
223 | class CompactDecimalFormatTest : public IntlTest { | |
224 | public: | |
225 | CompactDecimalFormatTest() { | |
226 | } | |
227 | ||
228 | void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0); | |
229 | private: | |
230 | void TestEnglishShort(); | |
231 | void TestSerbianShort(); | |
232 | void TestSerbianLong(); | |
233 | void TestSerbianLongNegative(); | |
234 | void TestJapaneseShort(); | |
235 | void TestSwahiliShort(); | |
236 | void TestCsShort(); | |
237 | void TestSkLong(); | |
238 | void TestSwahiliShortNegative(); | |
0f5d89e8 A |
239 | void TestEnglishCurrency(); |
240 | void TestGermanCurrency(); | |
241 | void TestChineseCurrency(); | |
51004dcb A |
242 | void TestArabicLong(); |
243 | void TestFieldPosition(); | |
0f5d89e8 | 244 | void TestDefaultSignificantDigits(); |
2ca993e8 | 245 | void TestAPIVariants(); |
0f5d89e8 A |
246 | void TestBug12975(); |
247 | ||
51004dcb A |
248 | void CheckLocale( |
249 | const Locale& locale, UNumberCompactStyle style, | |
0f5d89e8 A |
250 | const ExpectedResult* expectedResults, int32_t expectedResultLength); |
251 | void CheckLocaleWithCurrency(const Locale& locale, UNumberCompactStyle style, const UChar* currency, | |
252 | const ExpectedResult* expectedResults, int32_t expectedResultLength); | |
51004dcb A |
253 | void CheckExpectedResult( |
254 | const CompactDecimalFormat* cdf, const ExpectedResult* expectedResult, | |
255 | const char* description); | |
256 | CompactDecimalFormat* createCDFInstance(const Locale& locale, UNumberCompactStyle style, UErrorCode& status); | |
257 | static const char *StyleStr(UNumberCompactStyle style); | |
258 | }; | |
259 | ||
260 | void CompactDecimalFormatTest::runIndexedTest( | |
261 | int32_t index, UBool exec, const char *&name, char *) { | |
262 | if (exec) { | |
263 | logln("TestSuite CompactDecimalFormatTest: "); | |
264 | } | |
265 | TESTCASE_AUTO_BEGIN; | |
266 | TESTCASE_AUTO(TestEnglishShort); | |
267 | TESTCASE_AUTO(TestSerbianShort); | |
268 | TESTCASE_AUTO(TestSerbianLong); | |
269 | TESTCASE_AUTO(TestSerbianLongNegative); | |
270 | TESTCASE_AUTO(TestJapaneseShort); | |
271 | TESTCASE_AUTO(TestSwahiliShort); | |
0f5d89e8 A |
272 | //TESTCASE_AUTO(TestEnglishCurrency); |
273 | //TESTCASE_AUTO(TestGermanCurrency); | |
274 | //TESTCASE_AUTO(TestChineseCurrency); | |
51004dcb A |
275 | TESTCASE_AUTO(TestCsShort); |
276 | TESTCASE_AUTO(TestSkLong); | |
277 | TESTCASE_AUTO(TestSwahiliShortNegative); | |
278 | TESTCASE_AUTO(TestArabicLong); | |
279 | TESTCASE_AUTO(TestFieldPosition); | |
0f5d89e8 | 280 | TESTCASE_AUTO(TestDefaultSignificantDigits); |
2ca993e8 | 281 | TESTCASE_AUTO(TestAPIVariants); |
0f5d89e8 | 282 | //TESTCASE_AUTO(TestBug12975); |
51004dcb A |
283 | TESTCASE_AUTO_END; |
284 | } | |
285 | ||
286 | void CompactDecimalFormatTest::TestEnglishShort() { | |
b331163b | 287 | CheckLocale("en", UNUM_SHORT, kEnglishShort, UPRV_LENGTHOF(kEnglishShort)); |
51004dcb A |
288 | } |
289 | ||
290 | void CompactDecimalFormatTest::TestSerbianShort() { | |
b331163b | 291 | CheckLocale("sr", UNUM_SHORT, kSerbianShort, UPRV_LENGTHOF(kSerbianShort)); |
51004dcb A |
292 | } |
293 | ||
294 | void CompactDecimalFormatTest::TestSerbianLong() { | |
b331163b | 295 | CheckLocale("sr", UNUM_LONG, kSerbianLong, UPRV_LENGTHOF(kSerbianLong)); |
51004dcb A |
296 | } |
297 | ||
298 | void CompactDecimalFormatTest::TestSerbianLongNegative() { | |
b331163b | 299 | CheckLocale("sr", UNUM_LONG, kSerbianLongNegative, UPRV_LENGTHOF(kSerbianLongNegative)); |
51004dcb A |
300 | } |
301 | ||
302 | void CompactDecimalFormatTest::TestJapaneseShort() { | |
b331163b | 303 | CheckLocale(Locale::getJapan(), UNUM_SHORT, kJapaneseShort, UPRV_LENGTHOF(kJapaneseShort)); |
51004dcb A |
304 | } |
305 | ||
306 | void CompactDecimalFormatTest::TestSwahiliShort() { | |
b331163b | 307 | CheckLocale("sw", UNUM_SHORT, kSwahiliShort, UPRV_LENGTHOF(kSwahiliShort)); |
51004dcb A |
308 | } |
309 | ||
0f5d89e8 A |
310 | void CompactDecimalFormatTest::TestEnglishCurrency() { |
311 | CheckLocaleWithCurrency( | |
312 | "en", UNUM_SHORT, u"USD", kEnglishCurrencyTestData, UPRV_LENGTHOF(kEnglishCurrencyTestData)); | |
313 | } | |
314 | ||
315 | void CompactDecimalFormatTest::TestGermanCurrency() { | |
316 | CheckLocaleWithCurrency( | |
317 | "de", UNUM_SHORT, u"EUR", kGermanCurrencyTestData, UPRV_LENGTHOF(kGermanCurrencyTestData)); | |
318 | } | |
319 | ||
320 | void CompactDecimalFormatTest::TestChineseCurrency() { | |
321 | CheckLocaleWithCurrency( | |
322 | "zh", UNUM_SHORT, u"CNY", kChineseCurrencyTestData, UPRV_LENGTHOF(kChineseCurrencyTestData)); | |
323 | } | |
324 | ||
51004dcb A |
325 | void CompactDecimalFormatTest::TestFieldPosition() { |
326 | // Swahili uses prefixes which forces offsets in field position to change | |
327 | UErrorCode status = U_ZERO_ERROR; | |
328 | LocalPointer<CompactDecimalFormat> cdf(createCDFInstance("sw", UNUM_SHORT, status)); | |
329 | if (U_FAILURE(status)) { | |
330 | dataerrln("Unable to create format object - %s", u_errorName(status)); | |
331 | return; | |
332 | } | |
333 | FieldPosition fp(UNUM_INTEGER_FIELD); | |
334 | UnicodeString result; | |
335 | cdf->format(1234567.0, result, fp); | |
336 | UnicodeString subString = result.tempSubString(fp.getBeginIndex(), fp.getEndIndex() - fp.getBeginIndex()); | |
337 | if (subString != UnicodeString("1", -1, US_INV)) { | |
338 | errln(UnicodeString("Expected 1, got ") + subString); | |
339 | } | |
340 | } | |
341 | ||
342 | void CompactDecimalFormatTest::TestCsShort() { | |
b331163b | 343 | CheckLocale("cs", UNUM_SHORT, kCsShort, UPRV_LENGTHOF(kCsShort)); |
51004dcb A |
344 | } |
345 | ||
346 | void CompactDecimalFormatTest::TestSkLong() { | |
347 | // In CLDR we have: | |
348 | // 1000 { | |
349 | // few{"0"} | |
350 | // one{"0"} | |
351 | // other{"0"} | |
b331163b | 352 | CheckLocale("sk", UNUM_LONG, kSkLong, UPRV_LENGTHOF(kSkLong)); |
51004dcb A |
353 | } |
354 | ||
355 | void CompactDecimalFormatTest::TestSwahiliShortNegative() { | |
b331163b | 356 | CheckLocale("sw", UNUM_SHORT, kSwahiliShortNegative, UPRV_LENGTHOF(kSwahiliShortNegative)); |
51004dcb A |
357 | } |
358 | ||
359 | void CompactDecimalFormatTest::TestArabicLong() { | |
0f5d89e8 | 360 | CheckLocale("ar-EG", UNUM_LONG, kArabicLong, UPRV_LENGTHOF(kArabicLong)); |
51004dcb A |
361 | } |
362 | ||
0f5d89e8 | 363 | void CompactDecimalFormatTest::TestDefaultSignificantDigits() { |
51004dcb A |
364 | UErrorCode status = U_ZERO_ERROR; |
365 | LocalPointer<CompactDecimalFormat> cdf(CompactDecimalFormat::createInstance("en", UNUM_SHORT, status)); | |
366 | if (U_FAILURE(status)) { | |
367 | dataerrln("Unable to create format object - %s", u_errorName(status)); | |
368 | return; | |
369 | } | |
0f5d89e8 | 370 | // We expect 3 significant digits by default (ICU 61 behavior) |
51004dcb | 371 | UnicodeString actual; |
0f5d89e8 A |
372 | assertEquals("Default significant digits", u"123K", cdf->format(123456, actual.remove())); |
373 | assertEquals("Default significant digits", u"12.3K", cdf->format(12345, actual.remove())); | |
374 | assertEquals("Default significant digits", u"1.23K", cdf->format(1234, actual.remove())); | |
375 | assertEquals("Default significant digits", u"123", cdf->format(123, actual.remove())); | |
51004dcb A |
376 | } |
377 | ||
2ca993e8 A |
378 | void CompactDecimalFormatTest::TestAPIVariants() { |
379 | UErrorCode status = U_ZERO_ERROR; | |
380 | LocalPointer<CompactDecimalFormat> cdf(CompactDecimalFormat::createInstance("en", UNUM_SHORT, status)); | |
381 | if (U_FAILURE(status)) { | |
382 | dataerrln("Unable to create format object - %s", u_errorName(status)); | |
383 | return; | |
384 | } | |
385 | UnicodeString actual; | |
386 | FieldPosition pos; | |
387 | FieldPositionIterator posIter; | |
388 | UnicodeString expected("123K", -1, US_INV); | |
389 | pos.setField(UNUM_INTEGER_FIELD); | |
390 | ||
391 | actual.remove(); | |
392 | pos.setBeginIndex(0); | |
393 | pos.setEndIndex(0); | |
394 | cdf->format((double)123456.0, actual, pos); | |
395 | if (actual != expected || pos.getEndIndex() != 3) { | |
396 | errln(UnicodeString("Fail format(double,UnicodeString&,FieldPosition&): Expected: \"") + expected + "\", pos 3; " + | |
397 | "Got: \"" + actual + "\", pos " + pos.getEndIndex()); | |
398 | } | |
399 | ||
400 | actual.remove(); | |
401 | pos.setBeginIndex(0); | |
402 | pos.setEndIndex(0); | |
403 | status = U_ZERO_ERROR; | |
404 | cdf->format((double)123456.0, actual, pos, status); | |
405 | if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) { | |
406 | errln(UnicodeString("Fail format(double,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", pos 3, status U_ZERO_ERROR; " + | |
407 | "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status)); | |
408 | } | |
409 | ||
410 | actual.remove(); | |
0f5d89e8 A |
411 | pos.setBeginIndex(0); |
412 | pos.setEndIndex(0); | |
2ca993e8 A |
413 | status = U_ZERO_ERROR; |
414 | cdf->format((double)123456.0, actual, &posIter, status); | |
415 | if (status != U_UNSUPPORTED_ERROR) { | |
416 | errln(UnicodeString("Fail format(double,UnicodeString&,FieldPositionIterator*,UErrorCode&): Expected status U_UNSUPPORTED_ERROR;") + | |
417 | "Got status " + u_errorName(status)); | |
418 | } | |
419 | ||
420 | actual.remove(); | |
421 | pos.setBeginIndex(0); | |
422 | pos.setEndIndex(0); | |
423 | cdf->format((int32_t)123456, actual, pos); | |
424 | if (actual != expected || pos.getEndIndex() != 3) { | |
425 | errln(UnicodeString("Fail format(int32_t,UnicodeString&,FieldPosition&): Expected: \"") + expected + "\", pos 3; " + | |
426 | "Got: \"" + actual + "\", pos " + pos.getEndIndex()); | |
427 | } | |
428 | ||
429 | actual.remove(); | |
430 | pos.setBeginIndex(0); | |
431 | pos.setEndIndex(0); | |
432 | status = U_ZERO_ERROR; | |
433 | cdf->format((int32_t)123456, actual, pos, status); | |
434 | if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) { | |
435 | errln(UnicodeString("Fail format(int32_t,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", pos 3, status U_ZERO_ERROR; " + | |
436 | "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status)); | |
437 | } | |
438 | ||
439 | actual.remove(); | |
0f5d89e8 A |
440 | pos.setBeginIndex(0); |
441 | pos.setEndIndex(0); | |
2ca993e8 A |
442 | status = U_ZERO_ERROR; |
443 | cdf->format((int32_t)123456, actual, &posIter, status); | |
444 | if (status != U_UNSUPPORTED_ERROR) { | |
445 | errln(UnicodeString("Fail format(int32_t,UnicodeString&,FieldPositionIterator*,UErrorCode&): Expected status U_UNSUPPORTED_ERROR;") + | |
446 | "Got status " + u_errorName(status)); | |
447 | } | |
448 | ||
449 | actual.remove(); | |
450 | pos.setBeginIndex(0); | |
451 | pos.setEndIndex(0); | |
452 | cdf->format((int64_t)123456, actual, pos); | |
453 | if (actual != expected || pos.getEndIndex() != 3) { | |
454 | errln(UnicodeString("Fail format(int64_t,UnicodeString&,FieldPosition&): Expected: \"") + expected + "\", pos 3; " + | |
455 | "Got: \"" + actual + "\", pos " + pos.getEndIndex()); | |
456 | } | |
457 | ||
458 | actual.remove(); | |
459 | pos.setBeginIndex(0); | |
460 | pos.setEndIndex(0); | |
461 | status = U_ZERO_ERROR; | |
462 | cdf->format((int64_t)123456, actual, pos, status); | |
463 | if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) { | |
464 | errln(UnicodeString("Fail format(int64_t,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", pos 3, status U_ZERO_ERROR; " + | |
465 | "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status)); | |
466 | } | |
467 | ||
468 | actual.remove(); | |
0f5d89e8 A |
469 | pos.setBeginIndex(0); |
470 | pos.setEndIndex(0); | |
2ca993e8 A |
471 | status = U_ZERO_ERROR; |
472 | cdf->format((int64_t)123456, actual, &posIter, status); | |
473 | if (status != U_UNSUPPORTED_ERROR) { | |
474 | errln(UnicodeString("Fail format(int64_t,UnicodeString&,FieldPositionIterator*,UErrorCode&): Expected status U_UNSUPPORTED_ERROR;") + | |
475 | "Got status " + u_errorName(status)); | |
476 | } | |
477 | ||
478 | } | |
479 | ||
0f5d89e8 A |
480 | void CompactDecimalFormatTest::TestBug12975() { |
481 | IcuTestErrorCode status(*this, "TestBug12975"); | |
482 | Locale locale("it"); | |
483 | LocalPointer<CompactDecimalFormat> cdf(CompactDecimalFormat::createInstance(locale, UNUM_SHORT, status)); | |
484 | if (assertSuccess("", status, true, __FILE__, __LINE__)) { | |
485 | UnicodeString resultCdf; | |
486 | cdf->format(120000, resultCdf); | |
487 | LocalPointer<DecimalFormat> df((DecimalFormat*) DecimalFormat::createInstance(locale, status)); | |
488 | UnicodeString resultDefault; | |
489 | df->format(120000, resultDefault); | |
490 | assertEquals("CompactDecimalFormat should use default pattern when compact pattern is unavailable", | |
491 | resultDefault, resultCdf); | |
492 | } | |
493 | } | |
494 | ||
495 | ||
496 | // End test cases. Helpers: | |
497 | ||
51004dcb A |
498 | void CompactDecimalFormatTest::CheckLocale(const Locale& locale, UNumberCompactStyle style, const ExpectedResult* expectedResults, int32_t expectedResultLength) { |
499 | UErrorCode status = U_ZERO_ERROR; | |
500 | LocalPointer<CompactDecimalFormat> cdf(createCDFInstance(locale, style, status)); | |
501 | if (U_FAILURE(status)) { | |
502 | dataerrln("Unable to create format object - %s", u_errorName(status)); | |
503 | return; | |
504 | } | |
505 | char description[256]; | |
506 | sprintf(description,"%s - %s", locale.getName(), StyleStr(style)); | |
507 | for (int32_t i = 0; i < expectedResultLength; i++) { | |
508 | CheckExpectedResult(cdf.getAlias(), &expectedResults[i], description); | |
509 | } | |
510 | } | |
511 | ||
0f5d89e8 A |
512 | void CompactDecimalFormatTest::CheckLocaleWithCurrency(const Locale& locale, UNumberCompactStyle style, |
513 | const UChar* currency, | |
514 | const ExpectedResult* expectedResults, | |
515 | int32_t expectedResultLength) { | |
516 | UErrorCode status = U_ZERO_ERROR; | |
517 | LocalPointer<CompactDecimalFormat> cdf(createCDFInstance(locale, style, status)); | |
518 | if (U_FAILURE(status)) { | |
519 | dataerrln("Unable to create format object - %s", u_errorName(status)); | |
520 | return; | |
521 | } | |
522 | cdf->setCurrency(currency, status); | |
523 | assertSuccess("Failed to set currency", status); | |
524 | char description[256]; | |
525 | sprintf(description,"%s - %s", locale.getName(), StyleStr(style)); | |
526 | for (int32_t i = 0; i < expectedResultLength; i++) { | |
527 | CheckExpectedResult(cdf.getAlias(), &expectedResults[i], description); | |
528 | } | |
529 | } | |
530 | ||
51004dcb A |
531 | void CompactDecimalFormatTest::CheckExpectedResult( |
532 | const CompactDecimalFormat* cdf, const ExpectedResult* expectedResult, const char* description) { | |
533 | UnicodeString actual; | |
534 | cdf->format(expectedResult->value, actual); | |
535 | UnicodeString expected(expectedResult->expected, -1, US_INV); | |
536 | expected = expected.unescape(); | |
537 | if (actual != expected) { | |
538 | errln(UnicodeString("Fail: Expected: ") + expected | |
539 | + UnicodeString(" Got: ") + actual | |
540 | + UnicodeString(" for: ") + UnicodeString(description)); | |
541 | } | |
542 | } | |
543 | ||
544 | CompactDecimalFormat* | |
545 | CompactDecimalFormatTest::createCDFInstance(const Locale& locale, UNumberCompactStyle style, UErrorCode& status) { | |
546 | CompactDecimalFormat* result = CompactDecimalFormat::createInstance(locale, style, status); | |
547 | if (U_FAILURE(status)) { | |
548 | return NULL; | |
549 | } | |
550 | // All tests are written for two significant digits, so we explicitly set here | |
551 | // in case default significant digits change. | |
552 | result->setMaximumSignificantDigits(2); | |
553 | return result; | |
554 | } | |
555 | ||
556 | const char *CompactDecimalFormatTest::StyleStr(UNumberCompactStyle style) { | |
557 | if (style == UNUM_SHORT) { | |
558 | return kShortStr; | |
559 | } | |
560 | return kLongStr; | |
561 | } | |
562 | ||
563 | extern IntlTest *createCompactDecimalFormatTest() { | |
564 | return new CompactDecimalFormatTest(); | |
565 | } | |
566 | ||
567 | #endif |