1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 1997-2015, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
9 * File COMPACTDECIMALFORMATTEST.CPP
11 ********************************************************************************
18 #if !UCONFIG_NO_FORMATTING
20 #include "unicode/compactdecimalformat.h"
21 #include "unicode/unum.h"
24 typedef struct ExpectedResult
{
29 static const char *kShortStr
= "Short";
30 static const char *kLongStr
= "Long";
32 static ExpectedResult kEnglishShort
[] = {
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"}};
50 static ExpectedResult kSerbianShort
[] = {
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."}};
66 static ExpectedResult kSerbianLong
[] = {
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
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
85 static ExpectedResult kSerbianLongNegative
[] = {
86 {-1234.0, "-1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"},
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"},
94 {-1.23456789E9
, "-1,2 \\u043C\\u0438\\u043B\\u0438\\u0458\\u0430\\u0440\\u0434\\u0435"},
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"},
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"}};
104 static ExpectedResult kJapaneseShort
[] = {
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"}};
118 static ExpectedResult kSwahiliShort
[] = {
119 {1234.0, "elfu\\u00a01.2"},
120 {12345.0, "elfu\\u00a012"},
121 {123456.0, "elfu\\u00a0120"},
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"}};
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."}};
148 static ExpectedResult kSkLong
[] = {
149 {1000.0, "1 tis\\u00edc"},
150 {1572.0, "1,6 tis\\u00edca"},
151 {5184.0, "5,2 tis\\u00edca"}};
153 static ExpectedResult kSwahiliShortNegative
[] = {
154 {-1234.0, "elfu\\u00a0-1.2"},
155 {-12345.0, "elfu\\u00a0-12"},
156 {-123456.0, "elfu\\u00a0-120"},
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"}};
167 static ExpectedResult kArabicLong
[] = {
168 {-5300.0, "\\u061C-\\u0665\\u066B\\u0663 \\u0623\\u0644\\u0641"}};
170 static ExpectedResult kChineseCurrencyTestData
[] = {
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"},
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"},
204 static ExpectedResult kEnglishCurrencyTestData
[] = {
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"},
223 class CompactDecimalFormatTest
: public IntlTest
{
225 CompactDecimalFormatTest() {
228 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=0);
230 void TestEnglishShort();
231 void TestSerbianShort();
232 void TestSerbianLong();
233 void TestSerbianLongNegative();
234 void TestJapaneseShort();
235 void TestSwahiliShort();
238 void TestSwahiliShortNegative();
239 void TestEnglishCurrency();
240 void TestGermanCurrency();
241 void TestChineseCurrency();
242 void TestArabicLong();
243 void TestFieldPosition();
244 void TestDefaultSignificantDigits();
245 void TestAPIVariants();
249 const Locale
& locale
, UNumberCompactStyle style
,
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
);
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
);
260 void CompactDecimalFormatTest::runIndexedTest(
261 int32_t index
, UBool exec
, const char *&name
, char *) {
263 logln("TestSuite CompactDecimalFormatTest: ");
266 TESTCASE_AUTO(TestEnglishShort
);
267 TESTCASE_AUTO(TestSerbianShort
);
268 TESTCASE_AUTO(TestSerbianLong
);
269 TESTCASE_AUTO(TestSerbianLongNegative
);
270 TESTCASE_AUTO(TestJapaneseShort
);
271 TESTCASE_AUTO(TestSwahiliShort
);
272 //TESTCASE_AUTO(TestEnglishCurrency);
273 //TESTCASE_AUTO(TestGermanCurrency);
274 //TESTCASE_AUTO(TestChineseCurrency);
275 TESTCASE_AUTO(TestCsShort
);
276 TESTCASE_AUTO(TestSkLong
);
277 TESTCASE_AUTO(TestSwahiliShortNegative
);
278 TESTCASE_AUTO(TestArabicLong
);
279 TESTCASE_AUTO(TestFieldPosition
);
280 TESTCASE_AUTO(TestDefaultSignificantDigits
);
281 TESTCASE_AUTO(TestAPIVariants
);
282 //TESTCASE_AUTO(TestBug12975);
286 void CompactDecimalFormatTest::TestEnglishShort() {
287 CheckLocale("en", UNUM_SHORT
, kEnglishShort
, UPRV_LENGTHOF(kEnglishShort
));
290 void CompactDecimalFormatTest::TestSerbianShort() {
291 CheckLocale("sr", UNUM_SHORT
, kSerbianShort
, UPRV_LENGTHOF(kSerbianShort
));
294 void CompactDecimalFormatTest::TestSerbianLong() {
295 CheckLocale("sr", UNUM_LONG
, kSerbianLong
, UPRV_LENGTHOF(kSerbianLong
));
298 void CompactDecimalFormatTest::TestSerbianLongNegative() {
299 CheckLocale("sr", UNUM_LONG
, kSerbianLongNegative
, UPRV_LENGTHOF(kSerbianLongNegative
));
302 void CompactDecimalFormatTest::TestJapaneseShort() {
303 CheckLocale(Locale::getJapan(), UNUM_SHORT
, kJapaneseShort
, UPRV_LENGTHOF(kJapaneseShort
));
306 void CompactDecimalFormatTest::TestSwahiliShort() {
307 CheckLocale("sw", UNUM_SHORT
, kSwahiliShort
, UPRV_LENGTHOF(kSwahiliShort
));
310 void CompactDecimalFormatTest::TestEnglishCurrency() {
311 CheckLocaleWithCurrency(
312 "en", UNUM_SHORT
, u
"USD", kEnglishCurrencyTestData
, UPRV_LENGTHOF(kEnglishCurrencyTestData
));
315 void CompactDecimalFormatTest::TestGermanCurrency() {
316 CheckLocaleWithCurrency(
317 "de", UNUM_SHORT
, u
"EUR", kGermanCurrencyTestData
, UPRV_LENGTHOF(kGermanCurrencyTestData
));
320 void CompactDecimalFormatTest::TestChineseCurrency() {
321 CheckLocaleWithCurrency(
322 "zh", UNUM_SHORT
, u
"CNY", kChineseCurrencyTestData
, UPRV_LENGTHOF(kChineseCurrencyTestData
));
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
));
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
);
342 void CompactDecimalFormatTest::TestCsShort() {
343 CheckLocale("cs", UNUM_SHORT
, kCsShort
, UPRV_LENGTHOF(kCsShort
));
346 void CompactDecimalFormatTest::TestSkLong() {
352 CheckLocale("sk", UNUM_LONG
, kSkLong
, UPRV_LENGTHOF(kSkLong
));
355 void CompactDecimalFormatTest::TestSwahiliShortNegative() {
356 CheckLocale("sw", UNUM_SHORT
, kSwahiliShortNegative
, UPRV_LENGTHOF(kSwahiliShortNegative
));
359 void CompactDecimalFormatTest::TestArabicLong() {
360 CheckLocale("ar-EG", UNUM_LONG
, kArabicLong
, UPRV_LENGTHOF(kArabicLong
));
363 void CompactDecimalFormatTest::TestDefaultSignificantDigits() {
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
));
370 // We expect 3 significant digits by default (ICU 61 behavior)
371 UnicodeString actual
;
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()));
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
));
385 UnicodeString actual
;
387 FieldPositionIterator posIter
;
388 UnicodeString
expected("123K", -1, US_INV
);
389 pos
.setField(UNUM_INTEGER_FIELD
);
392 pos
.setBeginIndex(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());
401 pos
.setBeginIndex(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
));
411 pos
.setBeginIndex(0);
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
));
421 pos
.setBeginIndex(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());
430 pos
.setBeginIndex(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
));
440 pos
.setBeginIndex(0);
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
));
450 pos
.setBeginIndex(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());
459 pos
.setBeginIndex(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
));
469 pos
.setBeginIndex(0);
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
));
480 void CompactDecimalFormatTest::TestBug12975() {
481 IcuTestErrorCode
status(*this, "TestBug12975");
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
);
496 // End test cases. Helpers:
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
));
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
);
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
));
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
);
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
));
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
)) {
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);
556 const char *CompactDecimalFormatTest::StyleStr(UNumberCompactStyle style
) {
557 if (style
== UNUM_SHORT
) {
563 extern IntlTest
*createCompactDecimalFormatTest() {
564 return new CompactDecimalFormatTest();