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