• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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=0) override;
235 private:
236     void TestEnglishShort();
237     void TestSerbianShort();
238     void TestSerbianLong();
239     void TestSerbianLongNegative();
240     void TestJapaneseShort();
241     void TestSwahiliShort();
242     void TestCsShort();
243     void TestSkLong();
244     void TestSwahiliShortNegative();
245     void TestEnglishCurrency();
246     void TestGermanCurrency();
247     void TestChineseCurrency();
248     void TestArabicLong();
249     void TestFieldPosition();
250     void TestDefaultSignificantDigits();
251     void TestAPIVariants();
252     void TestBug12975();
253 
254     void CheckLocale(
255         const Locale& locale, UNumberCompactStyle style,
256         const ExpectedResult* expectedResults, int32_t expectedResultLength);
257     void CheckLocaleWithCurrency(const Locale& locale, UNumberCompactStyle style, const UChar* currency,
258                                  const ExpectedResult* expectedResults, int32_t expectedResultLength);
259     void CheckExpectedResult(
260         const CompactDecimalFormat* cdf, const ExpectedResult* expectedResult,
261         const char* description);
262     CompactDecimalFormat* createCDFInstance(const Locale& locale, UNumberCompactStyle style, UErrorCode& status);
263     static const char *StyleStr(UNumberCompactStyle style);
264 };
265 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)266 void CompactDecimalFormatTest::runIndexedTest(
267     int32_t index, UBool exec, const char *&name, char *) {
268   if (exec) {
269     logln("TestSuite CompactDecimalFormatTest: ");
270   }
271   TESTCASE_AUTO_BEGIN;
272   TESTCASE_AUTO(TestEnglishShort);
273   TESTCASE_AUTO(TestSerbianShort);
274   TESTCASE_AUTO(TestSerbianLong);
275   TESTCASE_AUTO(TestSerbianLongNegative);
276   TESTCASE_AUTO(TestJapaneseShort);
277   TESTCASE_AUTO(TestSwahiliShort);
278   TESTCASE_AUTO(TestEnglishCurrency);
279   TESTCASE_AUTO(TestGermanCurrency);
280   TESTCASE_AUTO(TestChineseCurrency);
281   TESTCASE_AUTO(TestCsShort);
282   TESTCASE_AUTO(TestSkLong);
283   TESTCASE_AUTO(TestSwahiliShortNegative);
284   TESTCASE_AUTO(TestArabicLong);
285   TESTCASE_AUTO(TestFieldPosition);
286   TESTCASE_AUTO(TestDefaultSignificantDigits);
287   TESTCASE_AUTO(TestAPIVariants);
288   TESTCASE_AUTO(TestBug12975);
289   TESTCASE_AUTO_END;
290 }
291 
TestEnglishShort()292 void CompactDecimalFormatTest::TestEnglishShort() {
293   CheckLocale("en", UNUM_SHORT, kEnglishShort, UPRV_LENGTHOF(kEnglishShort));
294 }
295 
TestSerbianShort()296 void CompactDecimalFormatTest::TestSerbianShort() {
297   CheckLocale("sr", UNUM_SHORT, kSerbianShort, UPRV_LENGTHOF(kSerbianShort));
298 }
299 
TestSerbianLong()300 void CompactDecimalFormatTest::TestSerbianLong() {
301   CheckLocale("sr", UNUM_LONG, kSerbianLong, UPRV_LENGTHOF(kSerbianLong));
302 }
303 
TestSerbianLongNegative()304 void CompactDecimalFormatTest::TestSerbianLongNegative() {
305   CheckLocale("sr", UNUM_LONG, kSerbianLongNegative, UPRV_LENGTHOF(kSerbianLongNegative));
306 }
307 
TestJapaneseShort()308 void CompactDecimalFormatTest::TestJapaneseShort() {
309   CheckLocale(Locale::getJapan(), UNUM_SHORT, kJapaneseShort, UPRV_LENGTHOF(kJapaneseShort));
310 }
311 
TestSwahiliShort()312 void CompactDecimalFormatTest::TestSwahiliShort() {
313   CheckLocale("sw", UNUM_SHORT, kSwahiliShort, UPRV_LENGTHOF(kSwahiliShort));
314 }
315 
TestEnglishCurrency()316 void CompactDecimalFormatTest::TestEnglishCurrency() {
317     CheckLocaleWithCurrency(
318             "en", UNUM_SHORT, u"USD", kEnglishCurrencyTestData, UPRV_LENGTHOF(kEnglishCurrencyTestData));
319 }
320 
TestGermanCurrency()321 void CompactDecimalFormatTest::TestGermanCurrency() {
322     CheckLocaleWithCurrency(
323             "de", UNUM_SHORT, u"EUR", kGermanCurrencyTestData, UPRV_LENGTHOF(kGermanCurrencyTestData));
324 }
325 
TestChineseCurrency()326 void CompactDecimalFormatTest::TestChineseCurrency() {
327     CheckLocaleWithCurrency(
328             "zh", UNUM_SHORT, u"CNY", kChineseCurrencyTestData, UPRV_LENGTHOF(kChineseCurrencyTestData));
329 }
330 
TestFieldPosition()331 void CompactDecimalFormatTest::TestFieldPosition() {
332   // Swahili uses prefixes which forces offsets in field position to change
333   UErrorCode status = U_ZERO_ERROR;
334   LocalPointer<CompactDecimalFormat> cdf(createCDFInstance("sw", UNUM_SHORT, status));
335   if (U_FAILURE(status)) {
336     dataerrln("Unable to create format object - %s", u_errorName(status));
337     return;
338   }
339   FieldPosition fp(UNUM_INTEGER_FIELD);
340   UnicodeString result;
341   cdf->format(1234567.0, result, fp);
342   UnicodeString subString = result.tempSubString(fp.getBeginIndex(), fp.getEndIndex() - fp.getBeginIndex());
343   if (subString != UnicodeString("1", -1, US_INV)) {
344     errln(UnicodeString("Expected 1, got ") + subString);
345   }
346 }
347 
TestCsShort()348 void CompactDecimalFormatTest::TestCsShort() {
349   CheckLocale("cs", UNUM_SHORT, kCsShort, UPRV_LENGTHOF(kCsShort));
350 }
351 
TestSkLong()352 void CompactDecimalFormatTest::TestSkLong() {
353   // In CLDR we have:
354   // 1000 {
355   //   few{"0"}
356   //   one{"0"}
357   //   other{"0"}
358   CheckLocale("sk", UNUM_LONG, kSkLong, UPRV_LENGTHOF(kSkLong));
359 }
360 
TestSwahiliShortNegative()361 void CompactDecimalFormatTest::TestSwahiliShortNegative() {
362   CheckLocale("sw", UNUM_SHORT, kSwahiliShortNegative, UPRV_LENGTHOF(kSwahiliShortNegative));
363 }
364 
TestArabicLong()365 void CompactDecimalFormatTest::TestArabicLong() {
366   CheckLocale("ar-EG", UNUM_LONG, kArabicLong, UPRV_LENGTHOF(kArabicLong));
367 }
368 
TestDefaultSignificantDigits()369 void CompactDecimalFormatTest::TestDefaultSignificantDigits() {
370   UErrorCode status = U_ZERO_ERROR;
371   LocalPointer<CompactDecimalFormat> cdf(CompactDecimalFormat::createInstance("en", UNUM_SHORT, status));
372   if (U_FAILURE(status)) {
373     dataerrln("Unable to create format object - %s", u_errorName(status));
374     return;
375   }
376   // We are expecting two significant digits for compact formats with one or two zeros,
377   // and rounded to the unit for compact formats with three or more zeros.
378   UnicodeString actual;
379   assertEquals("Default significant digits", u"123K", cdf->format(123456, actual.remove()));
380   assertEquals("Default significant digits", u"12K", cdf->format(12345, actual.remove()));
381   assertEquals("Default significant digits", u"1.2K", cdf->format(1234, actual.remove()));
382   assertEquals("Default significant digits", u"123", cdf->format(123, actual.remove()));
383 }
384 
TestAPIVariants()385 void CompactDecimalFormatTest::TestAPIVariants() {
386   UErrorCode status = U_ZERO_ERROR;
387   LocalPointer<CompactDecimalFormat> cdf(CompactDecimalFormat::createInstance("en", UNUM_SHORT, status));
388   if (U_FAILURE(status)) {
389     dataerrln("Unable to create format object - %s", u_errorName(status));
390     return;
391   }
392   UnicodeString actual;
393   FieldPosition pos;
394   FieldPositionIterator posIter;
395   UnicodeString expected("123K", -1, US_INV);
396   pos.setField(UNUM_INTEGER_FIELD);
397 
398   actual.remove();
399   pos.setBeginIndex(0);
400   pos.setEndIndex(0);
401   cdf->format((double)123456.0, actual, pos);
402   if (actual != expected || pos.getEndIndex() != 3) {
403     errln(UnicodeString("Fail format(double,UnicodeString&,FieldPosition&): Expected: \"") + expected + "\", pos 3; " +
404                                                                            "Got: \"" + actual + "\", pos " + pos.getEndIndex());
405   }
406 
407   actual.remove();
408   pos.setBeginIndex(0);
409   pos.setEndIndex(0);
410   status = U_ZERO_ERROR;
411   cdf->format((double)123456.0, actual, pos, status);
412   if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) {
413     errln(UnicodeString("Fail format(double,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", pos 3, status U_ZERO_ERROR; " +
414                                                               "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status));
415   }
416 
417   actual.remove();
418   pos.setBeginIndex(0);
419   pos.setEndIndex(0);
420   status = U_ZERO_ERROR;
421   cdf->format((double)123456.0, actual, &posIter, status);
422   posIter.next(pos);
423   if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) {
424     errln(UnicodeString("Fail format(int32_t,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", first pos 3, status U_ZERO_ERROR; " +
425           "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status));
426   }
427 
428   actual.remove();
429   pos.setBeginIndex(0);
430   pos.setEndIndex(0);
431   cdf->format((int32_t)123456, actual, pos);
432   if (actual != expected || pos.getEndIndex() != 3) {
433     errln(UnicodeString("Fail format(int32_t,UnicodeString&,FieldPosition&): Expected: \"") + expected + "\", pos 3; " +
434                                                                            "Got: \"" + actual + "\", pos " + pos.getEndIndex());
435   }
436 
437   actual.remove();
438   pos.setBeginIndex(0);
439   pos.setEndIndex(0);
440   status = U_ZERO_ERROR;
441   cdf->format((int32_t)123456, actual, pos, status);
442   if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) {
443     errln(UnicodeString("Fail format(int32_t,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", pos 3, status U_ZERO_ERROR; " +
444                                                               "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status));
445   }
446 
447   actual.remove();
448   pos.setBeginIndex(0);
449   pos.setEndIndex(0);
450   status = U_ZERO_ERROR;
451   cdf->format((int32_t)123456, actual, &posIter, status);
452   posIter.next(pos);
453   if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) {
454     errln(UnicodeString("Fail format(int32_t,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", first pos 3, status U_ZERO_ERROR; " +
455           "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status));
456   }
457 
458   actual.remove();
459   pos.setBeginIndex(0);
460   pos.setEndIndex(0);
461   cdf->format((int64_t)123456, actual, pos);
462   if (actual != expected || pos.getEndIndex() != 3) {
463     errln(UnicodeString("Fail format(int64_t,UnicodeString&,FieldPosition&): Expected: \"") + expected + "\", pos 3; " +
464                                                                            "Got: \"" + actual + "\", pos " + pos.getEndIndex());
465   }
466 
467   actual.remove();
468   pos.setBeginIndex(0);
469   pos.setEndIndex(0);
470   status = U_ZERO_ERROR;
471   cdf->format((int64_t)123456, actual, pos, status);
472   if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) {
473     errln(UnicodeString("Fail format(int64_t,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", pos 3, status U_ZERO_ERROR; " +
474                                                               "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status));
475   }
476 
477   actual.remove();
478   pos.setBeginIndex(0);
479   pos.setEndIndex(0);
480   status = U_ZERO_ERROR;
481   cdf->format((int64_t)123456, actual, &posIter, status);
482   posIter.next(pos);
483   if (actual != expected || pos.getEndIndex() != 3 || status != U_ZERO_ERROR) {
484     errln(UnicodeString("Fail format(int32_t,UnicodeString&,FieldPosition&,UErrorCode&): Expected: \"") + expected + "\", first pos 3, status U_ZERO_ERROR; " +
485           "Got: \"" + actual + "\", pos " + pos.getEndIndex() + ", status " + u_errorName(status));
486   }
487 
488 }
489 
TestBug12975()490 void CompactDecimalFormatTest::TestBug12975() {
491 	IcuTestErrorCode status(*this, "TestBug12975");
492     Locale locale("it");
493     LocalPointer<CompactDecimalFormat> cdf(CompactDecimalFormat::createInstance(locale, UNUM_SHORT, status));
494     if (assertSuccess("", status, true, __FILE__, __LINE__)) {
495         UnicodeString resultCdf;
496         cdf->format(12000, resultCdf);
497         LocalPointer<DecimalFormat> df((DecimalFormat*) DecimalFormat::createInstance(locale, status));
498         UnicodeString resultDefault;
499         df->format(12000, resultDefault);
500         assertEquals("CompactDecimalFormat should use default pattern when compact pattern is unavailable",
501                      resultDefault, resultCdf);
502     }
503 }
504 
505 
506 // End test cases. Helpers:
507 
CheckLocale(const Locale & locale,UNumberCompactStyle style,const ExpectedResult * expectedResults,int32_t expectedResultLength)508 void CompactDecimalFormatTest::CheckLocale(const Locale& locale, UNumberCompactStyle style, const ExpectedResult* expectedResults, int32_t expectedResultLength) {
509   UErrorCode status = U_ZERO_ERROR;
510   LocalPointer<CompactDecimalFormat> cdf(createCDFInstance(locale, style, status));
511   if (U_FAILURE(status)) {
512     dataerrln("Unable to create format object - %s", u_errorName(status));
513     return;
514   }
515   char description[256];
516   sprintf(description,"%s - %s", locale.getName(), StyleStr(style));
517   for (int32_t i = 0; i < expectedResultLength; i++) {
518     CheckExpectedResult(cdf.getAlias(), &expectedResults[i], description);
519   }
520 }
521 
CheckLocaleWithCurrency(const Locale & locale,UNumberCompactStyle style,const UChar * currency,const ExpectedResult * expectedResults,int32_t expectedResultLength)522 void CompactDecimalFormatTest::CheckLocaleWithCurrency(const Locale& locale, UNumberCompactStyle style,
523                                                        const UChar* currency,
524                                                        const ExpectedResult* expectedResults,
525                                                        int32_t expectedResultLength) {
526     UErrorCode status = U_ZERO_ERROR;
527     LocalPointer<CompactDecimalFormat> cdf(createCDFInstance(locale, style, status));
528     if (U_FAILURE(status)) {
529         dataerrln("Unable to create format object - %s", u_errorName(status));
530         return;
531     }
532     cdf->setCurrency(currency, status);
533     assertSuccess("Failed to set currency", status);
534     char description[256];
535     sprintf(description,"%s - %s", locale.getName(), StyleStr(style));
536     for (int32_t i = 0; i < expectedResultLength; i++) {
537         CheckExpectedResult(cdf.getAlias(), &expectedResults[i], description);
538     }
539 }
540 
CheckExpectedResult(const CompactDecimalFormat * cdf,const ExpectedResult * expectedResult,const char * description)541 void CompactDecimalFormatTest::CheckExpectedResult(
542     const CompactDecimalFormat* cdf, const ExpectedResult* expectedResult, const char* description) {
543   UnicodeString actual;
544   cdf->format(expectedResult->value, actual);
545   UnicodeString expected(expectedResult->expected, -1, US_INV);
546   expected = expected.unescape();
547   if (actual != expected) {
548     errln(UnicodeString("Fail: Expected: ") + expected
549           + UnicodeString(" Got: ") + actual
550           + UnicodeString(" for: ") + UnicodeString(description));
551   }
552 }
553 
554 CompactDecimalFormat*
createCDFInstance(const Locale & locale,UNumberCompactStyle style,UErrorCode & status)555 CompactDecimalFormatTest::createCDFInstance(const Locale& locale, UNumberCompactStyle style, UErrorCode& status) {
556   CompactDecimalFormat* result = CompactDecimalFormat::createInstance(locale, style, status);
557   if (U_FAILURE(status)) {
558     return NULL;
559   }
560   // All tests are written for two significant digits, so we explicitly set here
561   // in case default significant digits change.
562   result->setMaximumSignificantDigits(2);
563   return result;
564 }
565 
StyleStr(UNumberCompactStyle style)566 const char *CompactDecimalFormatTest::StyleStr(UNumberCompactStyle style) {
567   if (style == UNUM_SHORT) {
568     return kShortStr;
569   }
570   return kLongStr;
571 }
572 
createCompactDecimalFormatTest()573 extern IntlTest *createCompactDecimalFormatTest() {
574   return new CompactDecimalFormatTest();
575 }
576 
577 #endif
578