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