OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * Copyright (C) 1997-2013, International Business Machines Corporation and * | 3 * Copyright (C) 1997-2014, International Business Machines Corporation and * |
4 * others. All Rights Reserved. * | 4 * others. All Rights Reserved. * |
5 ******************************************************************************* | 5 ******************************************************************************* |
6 * | 6 * |
7 * File COMPACTDECIMALFORMATTEST.CPP | 7 * File COMPACTDECIMALFORMATTEST.CPP |
8 * | 8 * |
9 ******************************************************************************** | 9 ******************************************************************************** |
10 */ | 10 */ |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 | 13 |
14 #include "intltest.h" | 14 #include "intltest.h" |
15 | 15 |
16 #if !UCONFIG_NO_FORMATTING | 16 #if !UCONFIG_NO_FORMATTING |
17 | 17 |
18 #include "unicode/compactdecimalformat.h" | 18 #include "unicode/compactdecimalformat.h" |
19 #include "unicode/unum.h" | 19 #include "unicode/unum.h" |
20 | 20 #include "cmemory.h" |
21 #define LENGTHOF(array) (int32_t)(sizeof(array) / sizeof((array)[0])) | |
22 | 21 |
23 typedef struct ExpectedResult { | 22 typedef struct ExpectedResult { |
24 double value; | 23 double value; |
25 const char *expected; | 24 const char *expected; |
26 } ExpectedResult; | 25 } ExpectedResult; |
27 | 26 |
28 static const char *kShortStr = "Short"; | 27 static const char *kShortStr = "Short"; |
29 static const char *kLongStr = "Long"; | 28 static const char *kLongStr = "Long"; |
30 | 29 |
31 static ExpectedResult kEnglishShort[] = { | 30 static ExpectedResult kEnglishShort[] = { |
32 {0.0, "0"}, | 31 {0.0, "0"}, |
33 {0.17, "0.17"}, | 32 {0.17, "0.17"}, |
34 {1.0, "1"}, | 33 {1.0, "1"}, |
35 {1234.0, "1.2K"}, | 34 {1234.0, "1.2K"}, |
36 {12345.0, "12K"}, | 35 {12345.0, "12K"}, |
37 {123456.0, "120K"}, | 36 {123456.0, "120K"}, |
38 {1234567.0, "1.2M"}, | 37 {1234567.0, "1.2M"}, |
39 {12345678.0, "12M"}, | 38 {12345678.0, "12M"}, |
40 {123456789.0, "120M"}, | 39 {123456789.0, "120M"}, |
41 {1.23456789E9, "1.2B"}, | 40 {1.23456789E9, "1.2B"}, |
42 {1.23456789E10, "12B"}, | 41 {1.23456789E10, "12B"}, |
43 {1.23456789E11, "120B"}, | 42 {1.23456789E11, "120B"}, |
44 {1.23456789E12, "1.2T"}, | 43 {1.23456789E12, "1.2T"}, |
45 {1.23456789E13, "12T"}, | 44 {1.23456789E13, "12T"}, |
46 {1.23456789E14, "120T"}, | 45 {1.23456789E14, "120T"}, |
47 {1.23456789E15, "1200T"}}; | 46 {1.23456789E15, "1200T"}}; |
48 | 47 |
49 static ExpectedResult kSerbianShort[] = { | 48 static ExpectedResult kSerbianShort[] = { |
50 {1234.0, "1200"}, | 49 {1234.0, "1,2\\u00a0\\u0445\\u0438\\u0459."}, |
51 {12345.0, "12\\u00a0\\u0445\\u0438\\u0459"}, | 50 {12345.0, "12\\u00a0\\u0445\\u0438\\u0459."}, |
52 {20789.0, "21\\u00a0\\u0445\\u0438\\u0459"}, | 51 {20789.0, "21\\u00a0\\u0445\\u0438\\u0459."}, |
53 {123456.0, "120\\u00a0\\u0445\\u0438\\u0459"}, | 52 {123456.0, "120\\u00a0\\u0445\\u0438\\u0459."}, |
54 {1234567.0, "1,2\\u00A0\\u043C\\u0438\\u043B"}, | 53 {1234567.0, "1,2\\u00A0\\u043C\\u0438\\u043B."}, |
55 {12345678.0, "12\\u00A0\\u043C\\u0438\\u043B"}, | 54 {12345678.0, "12\\u00A0\\u043C\\u0438\\u043B."}, |
56 {123456789.0, "120\\u00A0\\u043C\\u0438\\u043B"}, | 55 {123456789.0, "120\\u00A0\\u043C\\u0438\\u043B."}, |
57 {1.23456789E9, "1,2\\u00A0\\u043C\\u043B\\u0440\\u0434"}, | 56 {1.23456789E9, "1,2\\u00A0\\u043C\\u043B\\u0440\\u0434."}, |
58 {1.23456789E10, "12\\u00A0\\u043C\\u043B\\u0440\\u0434"}, | 57 {1.23456789E10, "12\\u00A0\\u043C\\u043B\\u0440\\u0434."}, |
59 {1.23456789E11, "120\\u00A0\\u043C\\u043B\\u0440\\u0434"}, | 58 {1.23456789E11, "120\\u00A0\\u043C\\u043B\\u0440\\u0434."}, |
60 {1.23456789E12, "1,2\\u00A0\\u0431\\u0438\\u043B"}, | 59 {1.23456789E12, "1,2\\u00A0\\u0431\\u0438\\u043B."}, |
61 {1.23456789E13, "12\\u00A0\\u0431\\u0438\\u043B"}, | 60 {1.23456789E13, "12\\u00A0\\u0431\\u0438\\u043B."}, |
62 {1.23456789E14, "120\\u00A0\\u0431\\u0438\\u043B"}, | 61 {1.23456789E14, "120\\u00A0\\u0431\\u0438\\u043B."}, |
63 {1.23456789E15, "1200\\u00A0\\u0431\\u0438\\u043B"}}; | 62 {1.23456789E15, "1200\\u00A0\\u0431\\u0438\\u043B."}}; |
64 | 63 |
65 static ExpectedResult kSerbianLong[] = { | 64 static ExpectedResult kSerbianLong[] = { |
66 {1234.0, "1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few | 65 {1234.0, "1,2 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few |
67 {12345.0, "12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other | 66 {12345.0, "12 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other |
68 {21789.0, "22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few | 67 {21789.0, "22 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0435"}, // 10^3 few |
69 {123456.0, "120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other | 68 {123456.0, "120 \\u0445\\u0438\\u0459\\u0430\\u0434\\u0430"}, // 10^3 other |
70 {999999.0, "1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"}, // 10^6 one | 69 {999999.0, "1 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D"}, // 10^6 one |
71 {1234567.0, "1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6
few | 70 {1234567.0, "1,2 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6
few |
72 {12345678.0, "12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6
other | 71 {12345678.0, "12 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^6
other |
73 {123456789.0, "120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^
6 other | 72 {123456789.0, "120 \\u043C\\u0438\\u043B\\u0438\\u043E\\u043D\\u0430"}, // 10^
6 other |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 {1.23456789E9, "12\\u5104"}, | 109 {1.23456789E9, "12\\u5104"}, |
111 {1.23456789E10, "120\\u5104"}, | 110 {1.23456789E10, "120\\u5104"}, |
112 {1.23456789E11, "1200\\u5104"}, | 111 {1.23456789E11, "1200\\u5104"}, |
113 {1.23456789E12, "1.2\\u5146"}, | 112 {1.23456789E12, "1.2\\u5146"}, |
114 {1.23456789E13, "12\\u5146"}, | 113 {1.23456789E13, "12\\u5146"}, |
115 {1.23456789E14, "120\\u5146"}}; | 114 {1.23456789E14, "120\\u5146"}}; |
116 | 115 |
117 static ExpectedResult kSwahiliShort[] = { | 116 static ExpectedResult kSwahiliShort[] = { |
118 {1234.0, "elfu\\u00a01.2"}, | 117 {1234.0, "elfu\\u00a01.2"}, |
119 {12345.0, "elfu\\u00a012"}, | 118 {12345.0, "elfu\\u00a012"}, |
120 {123456.0, "laki1.2"}, | 119 {123456.0, "elfu\\u00a0120"}, |
121 {1234567.0, "M1.2"}, | 120 {1234567.0, "M1.2"}, |
122 {12345678.0, "M12"}, | 121 {12345678.0, "M12"}, |
123 {123456789.0, "M120"}, | 122 {123456789.0, "M120"}, |
124 {1.23456789E9, "B1.2"}, | 123 {1.23456789E9, "B1.2"}, |
125 {1.23456789E10, "B12"}, | 124 {1.23456789E10, "B12"}, |
126 {1.23456789E11, "B120"}, | 125 {1.23456789E11, "B120"}, |
127 {1.23456789E12, "T1.2"}, | 126 {1.23456789E12, "T1.2"}, |
128 {1.23456789E13, "T12"}, | 127 {1.23456789E13, "T12"}, |
129 {1.23456789E15, "T1200"}}; | 128 {1.23456789E15, "T1200"}}; |
130 | 129 |
(...skipping 14 matching lines...) Expand all Loading... |
145 {1.27123456E14, "130\\u00a0bil."}}; | 144 {1.27123456E14, "130\\u00a0bil."}}; |
146 | 145 |
147 static ExpectedResult kSkLong[] = { | 146 static ExpectedResult kSkLong[] = { |
148 {1000.0, "1 tis\\u00edc"}, | 147 {1000.0, "1 tis\\u00edc"}, |
149 {1572.0, "1,6 tis\\u00edc"}, | 148 {1572.0, "1,6 tis\\u00edc"}, |
150 {5184.0, "5,2 tis\\u00edc"}}; | 149 {5184.0, "5,2 tis\\u00edc"}}; |
151 | 150 |
152 static ExpectedResult kSwahiliShortNegative[] = { | 151 static ExpectedResult kSwahiliShortNegative[] = { |
153 {-1234.0, "elfu\\u00a0-1.2"}, | 152 {-1234.0, "elfu\\u00a0-1.2"}, |
154 {-12345.0, "elfu\\u00a0-12"}, | 153 {-12345.0, "elfu\\u00a0-12"}, |
155 {-123456.0, "laki-1.2"}, | 154 {-123456.0, "elfu\\u00a0-120"}, |
156 {-1234567.0, "M-1.2"}, | 155 {-1234567.0, "M-1.2"}, |
157 {-12345678.0, "M-12"}, | 156 {-12345678.0, "M-12"}, |
158 {-123456789.0, "M-120"}, | 157 {-123456789.0, "M-120"}, |
159 {-1.23456789E9, "B-1.2"}, | 158 {-1.23456789E9, "B-1.2"}, |
160 {-1.23456789E10, "B-12"}, | 159 {-1.23456789E10, "B-12"}, |
161 {-1.23456789E11, "B-120"}, | 160 {-1.23456789E11, "B-120"}, |
162 {-1.23456789E12, "T-1.2"}, | 161 {-1.23456789E12, "T-1.2"}, |
163 {-1.23456789E13, "T-12"}, | 162 {-1.23456789E13, "T-12"}, |
164 {-1.23456789E15, "T-1200"}}; | 163 {-1.23456789E15, "T-1200"}}; |
165 | 164 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 TESTCASE_AUTO(TestCsShort); | 210 TESTCASE_AUTO(TestCsShort); |
212 TESTCASE_AUTO(TestSkLong); | 211 TESTCASE_AUTO(TestSkLong); |
213 TESTCASE_AUTO(TestSwahiliShortNegative); | 212 TESTCASE_AUTO(TestSwahiliShortNegative); |
214 TESTCASE_AUTO(TestArabicLong); | 213 TESTCASE_AUTO(TestArabicLong); |
215 TESTCASE_AUTO(TestFieldPosition); | 214 TESTCASE_AUTO(TestFieldPosition); |
216 TESTCASE_AUTO(TestSignificantDigits); | 215 TESTCASE_AUTO(TestSignificantDigits); |
217 TESTCASE_AUTO_END; | 216 TESTCASE_AUTO_END; |
218 } | 217 } |
219 | 218 |
220 void CompactDecimalFormatTest::TestEnglishShort() { | 219 void CompactDecimalFormatTest::TestEnglishShort() { |
221 CheckLocale("en", UNUM_SHORT, kEnglishShort, LENGTHOF(kEnglishShort)); | 220 CheckLocale("en", UNUM_SHORT, kEnglishShort, UPRV_LENGTHOF(kEnglishShort)); |
222 } | 221 } |
223 | 222 |
224 void CompactDecimalFormatTest::TestSerbianShort() { | 223 void CompactDecimalFormatTest::TestSerbianShort() { |
225 CheckLocale("sr", UNUM_SHORT, kSerbianShort, LENGTHOF(kSerbianShort)); | 224 CheckLocale("sr", UNUM_SHORT, kSerbianShort, UPRV_LENGTHOF(kSerbianShort)); |
226 } | 225 } |
227 | 226 |
228 void CompactDecimalFormatTest::TestSerbianLong() { | 227 void CompactDecimalFormatTest::TestSerbianLong() { |
229 CheckLocale("sr", UNUM_LONG, kSerbianLong, LENGTHOF(kSerbianLong)); | 228 CheckLocale("sr", UNUM_LONG, kSerbianLong, UPRV_LENGTHOF(kSerbianLong)); |
230 } | 229 } |
231 | 230 |
232 void CompactDecimalFormatTest::TestSerbianLongNegative() { | 231 void CompactDecimalFormatTest::TestSerbianLongNegative() { |
233 CheckLocale("sr", UNUM_LONG, kSerbianLongNegative, LENGTHOF(kSerbianLongNegati
ve)); | 232 CheckLocale("sr", UNUM_LONG, kSerbianLongNegative, UPRV_LENGTHOF(kSerbianLongN
egative)); |
234 } | 233 } |
235 | 234 |
236 void CompactDecimalFormatTest::TestJapaneseShort() { | 235 void CompactDecimalFormatTest::TestJapaneseShort() { |
237 CheckLocale(Locale::getJapan(), UNUM_SHORT, kJapaneseShort, LENGTHOF(kJapanese
Short)); | 236 CheckLocale(Locale::getJapan(), UNUM_SHORT, kJapaneseShort, UPRV_LENGTHOF(kJap
aneseShort)); |
238 } | 237 } |
239 | 238 |
240 void CompactDecimalFormatTest::TestSwahiliShort() { | 239 void CompactDecimalFormatTest::TestSwahiliShort() { |
241 CheckLocale("sw", UNUM_SHORT, kSwahiliShort, LENGTHOF(kSwahiliShort)); | 240 CheckLocale("sw", UNUM_SHORT, kSwahiliShort, UPRV_LENGTHOF(kSwahiliShort)); |
242 } | 241 } |
243 | 242 |
244 void CompactDecimalFormatTest::TestFieldPosition() { | 243 void CompactDecimalFormatTest::TestFieldPosition() { |
245 // Swahili uses prefixes which forces offsets in field position to change | 244 // Swahili uses prefixes which forces offsets in field position to change |
246 UErrorCode status = U_ZERO_ERROR; | 245 UErrorCode status = U_ZERO_ERROR; |
247 LocalPointer<CompactDecimalFormat> cdf(createCDFInstance("sw", UNUM_SHORT, sta
tus)); | 246 LocalPointer<CompactDecimalFormat> cdf(createCDFInstance("sw", UNUM_SHORT, sta
tus)); |
248 if (U_FAILURE(status)) { | 247 if (U_FAILURE(status)) { |
249 dataerrln("Unable to create format object - %s", u_errorName(status)); | 248 dataerrln("Unable to create format object - %s", u_errorName(status)); |
250 return; | 249 return; |
251 } | 250 } |
252 FieldPosition fp(UNUM_INTEGER_FIELD); | 251 FieldPosition fp(UNUM_INTEGER_FIELD); |
253 UnicodeString result; | 252 UnicodeString result; |
254 cdf->format(1234567.0, result, fp); | 253 cdf->format(1234567.0, result, fp); |
255 UnicodeString subString = result.tempSubString(fp.getBeginIndex(), fp.getEndIn
dex() - fp.getBeginIndex()); | 254 UnicodeString subString = result.tempSubString(fp.getBeginIndex(), fp.getEndIn
dex() - fp.getBeginIndex()); |
256 if (subString != UnicodeString("1", -1, US_INV)) { | 255 if (subString != UnicodeString("1", -1, US_INV)) { |
257 errln(UnicodeString("Expected 1, got ") + subString); | 256 errln(UnicodeString("Expected 1, got ") + subString); |
258 } | 257 } |
259 } | 258 } |
260 | 259 |
261 void CompactDecimalFormatTest::TestCsShort() { | 260 void CompactDecimalFormatTest::TestCsShort() { |
262 CheckLocale("cs", UNUM_SHORT, kCsShort, LENGTHOF(kCsShort)); | 261 CheckLocale("cs", UNUM_SHORT, kCsShort, UPRV_LENGTHOF(kCsShort)); |
263 } | 262 } |
264 | 263 |
265 void CompactDecimalFormatTest::TestSkLong() { | 264 void CompactDecimalFormatTest::TestSkLong() { |
266 // In CLDR we have: | 265 // In CLDR we have: |
267 // 1000 { | 266 // 1000 { |
268 // few{"0"} | 267 // few{"0"} |
269 // one{"0"} | 268 // one{"0"} |
270 // other{"0"} | 269 // other{"0"} |
271 CheckLocale("sk", UNUM_LONG, kSkLong, LENGTHOF(kSkLong)); | 270 CheckLocale("sk", UNUM_LONG, kSkLong, UPRV_LENGTHOF(kSkLong)); |
272 } | 271 } |
273 | 272 |
274 void CompactDecimalFormatTest::TestSwahiliShortNegative() { | 273 void CompactDecimalFormatTest::TestSwahiliShortNegative() { |
275 CheckLocale("sw", UNUM_SHORT, kSwahiliShortNegative, LENGTHOF(kSwahiliShortNeg
ative)); | 274 CheckLocale("sw", UNUM_SHORT, kSwahiliShortNegative, UPRV_LENGTHOF(kSwahiliSho
rtNegative)); |
276 } | 275 } |
277 | 276 |
278 void CompactDecimalFormatTest::TestArabicLong() { | 277 void CompactDecimalFormatTest::TestArabicLong() { |
279 CheckLocale("ar", UNUM_LONG, kArabicLong, LENGTHOF(kArabicLong)); | 278 CheckLocale("ar", UNUM_LONG, kArabicLong, UPRV_LENGTHOF(kArabicLong)); |
280 } | 279 } |
281 | 280 |
282 void CompactDecimalFormatTest::TestSignificantDigits() { | 281 void CompactDecimalFormatTest::TestSignificantDigits() { |
283 UErrorCode status = U_ZERO_ERROR; | 282 UErrorCode status = U_ZERO_ERROR; |
284 LocalPointer<CompactDecimalFormat> cdf(CompactDecimalFormat::createInstance("e
n", UNUM_SHORT, status)); | 283 LocalPointer<CompactDecimalFormat> cdf(CompactDecimalFormat::createInstance("e
n", UNUM_SHORT, status)); |
285 if (U_FAILURE(status)) { | 284 if (U_FAILURE(status)) { |
286 dataerrln("Unable to create format object - %s", u_errorName(status)); | 285 dataerrln("Unable to create format object - %s", u_errorName(status)); |
287 return; | 286 return; |
288 } | 287 } |
289 UnicodeString actual; | 288 UnicodeString actual; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 return kShortStr; | 338 return kShortStr; |
340 } | 339 } |
341 return kLongStr; | 340 return kLongStr; |
342 } | 341 } |
343 | 342 |
344 extern IntlTest *createCompactDecimalFormatTest() { | 343 extern IntlTest *createCompactDecimalFormatTest() { |
345 return new CompactDecimalFormatTest(); | 344 return new CompactDecimalFormatTest(); |
346 } | 345 } |
347 | 346 |
348 #endif | 347 #endif |
OLD | NEW |