OLD | NEW |
1 /******************************************************************** | 1 /******************************************************************** |
2 * Copyright (c) 2008-2013, International Business Machines Corporation and | 2 * Copyright (c) 2008-2014, International Business Machines Corporation and |
3 * others. All Rights Reserved. | 3 * others. All Rights Reserved. |
4 ********************************************************************/ | 4 ********************************************************************/ |
5 | 5 |
6 #include "unicode/utypes.h" | 6 #include "unicode/utypes.h" |
7 | 7 |
8 #if !UCONFIG_NO_FORMATTING | 8 #if !UCONFIG_NO_FORMATTING |
9 | 9 |
| 10 #include "unicode/decimfmt.h" |
10 #include "unicode/tmunit.h" | 11 #include "unicode/tmunit.h" |
11 #include "unicode/tmutamt.h" | 12 #include "unicode/tmutamt.h" |
12 #include "unicode/tmutfmt.h" | 13 #include "unicode/tmutfmt.h" |
13 #include "tufmtts.h" | 14 #include "tufmtts.h" |
| 15 #include "cmemory.h" |
14 #include "unicode/ustring.h" | 16 #include "unicode/ustring.h" |
15 | 17 |
16 //TODO: put as compilation flag | 18 //TODO: put as compilation flag |
17 //#define TUFMTTS_DEBUG 1 | 19 //#define TUFMTTS_DEBUG 1 |
18 | 20 |
19 #ifdef TUFMTTS_DEBUG | 21 #ifdef TUFMTTS_DEBUG |
20 #include <iostream> | 22 #include <iostream> |
21 #endif | 23 #endif |
22 | 24 |
23 void TimeUnitTest::runIndexedTest( int32_t index, UBool exec, const char* &name,
char* /*par*/ ) { | 25 void TimeUnitTest::runIndexedTest( int32_t index, UBool exec, const char* &name,
char* /*par*/ ) { |
24 if (exec) logln("TestSuite TimeUnitTest"); | 26 if (exec) logln("TestSuite TimeUnitTest"); |
25 switch (index) { | 27 switch (index) { |
26 TESTCASE(0, testBasic); | 28 TESTCASE(0, testBasic); |
27 TESTCASE(1, testAPI); | 29 TESTCASE(1, testAPI); |
28 TESTCASE(2, testGreekWithFallback); | 30 TESTCASE(2, testGreekWithFallback); |
29 TESTCASE(3, testGreekWithSanitization); | 31 TESTCASE(3, testGreekWithSanitization); |
| 32 TESTCASE(4, test10219Plurals); |
30 default: name = ""; break; | 33 default: name = ""; break; |
31 } | 34 } |
32 } | 35 } |
33 | 36 |
| 37 // This function is more lenient than equals operator as it considers integer 3
hours and |
| 38 // double 3.0 hours to be equal |
| 39 static UBool tmaEqual(const TimeUnitAmount& left, const TimeUnitAmount& right) { |
| 40 if (left.getTimeUnitField() != right.getTimeUnitField()) { |
| 41 return FALSE; |
| 42 } |
| 43 UErrorCode status = U_ZERO_ERROR; |
| 44 if (!left.getNumber().isNumeric() || !right.getNumber().isNumeric()) { |
| 45 return FALSE; |
| 46 } |
| 47 UBool result = left.getNumber().getDouble(status) == right.getNumber().getDo
uble(status); |
| 48 if (U_FAILURE(status)) { |
| 49 return FALSE; |
| 50 } |
| 51 return result; |
| 52 } |
| 53 |
34 /** | 54 /** |
35 * Test basic | 55 * Test basic |
36 */ | 56 */ |
37 void TimeUnitTest::testBasic() { | 57 void TimeUnitTest::testBasic() { |
38 const char* locales[] = {"en", "sl", "fr", "zh", "ar", "ru", "zh_Hant", "pa"
}; | 58 const char* locales[] = {"en", "sl", "fr", "zh", "ar", "ru", "zh_Hant", "pa"
}; |
39 for ( unsigned int locIndex = 0; | 59 for ( unsigned int locIndex = 0; |
40 locIndex < sizeof(locales)/sizeof(locales[0]); | 60 locIndex < sizeof(locales)/sizeof(locales[0]); |
41 ++locIndex ) { | 61 ++locIndex ) { |
42 UErrorCode status = U_ZERO_ERROR; | 62 UErrorCode status = U_ZERO_ERROR; |
43 Locale loc(locales[locIndex]); | 63 Locale loc(locales[locIndex]); |
(...skipping 27 matching lines...) Expand all Loading... |
71 formatted = ((Format*)formats[style])->format(formattable, forma
tted, status); | 91 formatted = ((Format*)formats[style])->format(formattable, forma
tted, status); |
72 if (!assertSuccess("format()", status)) return; | 92 if (!assertSuccess("format()", status)) return; |
73 #ifdef TUFMTTS_DEBUG | 93 #ifdef TUFMTTS_DEBUG |
74 char formatResult[1000]; | 94 char formatResult[1000]; |
75 formatted.extract(0, formatted.length(), formatResult, "UTF-8"); | 95 formatted.extract(0, formatted.length(), formatResult, "UTF-8"); |
76 std::cout << "format result: " << formatResult << "\n"; | 96 std::cout << "format result: " << formatResult << "\n"; |
77 #endif | 97 #endif |
78 Formattable result; | 98 Formattable result; |
79 ((Format*)formats[style])->parseObject(formatted, result, status
); | 99 ((Format*)formats[style])->parseObject(formatted, result, status
); |
80 if (!assertSuccess("parseObject()", status)) return; | 100 if (!assertSuccess("parseObject()", status)) return; |
81 if (result != formattable) { | 101 if (!tmaEqual(*((TimeUnitAmount *)result.getObject()), *((TimeUn
itAmount *) formattable.getObject()))) { |
82 dataerrln("No round trip: "); | 102 dataerrln("No round trip: "); |
83 } | 103 } |
84 // other style parsing | 104 // other style parsing |
85 Formattable result_1; | 105 Formattable result_1; |
86 ((Format*)formats[1-style])->parseObject(formatted, result_1, st
atus); | 106 ((Format*)formats[1-style])->parseObject(formatted, result_1, st
atus); |
87 if (!assertSuccess("parseObject()", status)) return; | 107 if (!assertSuccess("parseObject()", status)) return; |
88 if (result_1 != formattable) { | 108 if (!tmaEqual(*((TimeUnitAmount *)result_1.getObject()), *((Time
UnitAmount *) formattable.getObject()))) { |
89 dataerrln("No round trip: "); | 109 dataerrln("No round trip: "); |
90 } | 110 } |
91 } | 111 } |
92 } | 112 } |
93 } | 113 } |
94 delete formats[UTMUTFMT_FULL_STYLE]; | 114 delete formats[UTMUTFMT_FULL_STYLE]; |
95 delete formats[UTMUTFMT_ABBREVIATED_STYLE]; | 115 delete formats[UTMUTFMT_ABBREVIATED_STYLE]; |
96 delete[] formats; | 116 delete[] formats; |
97 } | 117 } |
98 } | 118 } |
(...skipping 11 matching lines...) Expand all Loading... |
110 TimeUnit fourth = third; | 130 TimeUnit fourth = third; |
111 | 131 |
112 assertTrue("orig and clone are equal", (*tmunit == *another)); | 132 assertTrue("orig and clone are equal", (*tmunit == *another)); |
113 assertTrue("copied and assigned are equal", (third == fourth)); | 133 assertTrue("copied and assigned are equal", (third == fourth)); |
114 | 134 |
115 TimeUnit* tmunit_m = TimeUnit::createInstance(TimeUnit::UTIMEUNIT_MONTH, sta
tus); | 135 TimeUnit* tmunit_m = TimeUnit::createInstance(TimeUnit::UTIMEUNIT_MONTH, sta
tus); |
116 assertTrue("year != month", (*tmunit != *tmunit_m)); | 136 assertTrue("year != month", (*tmunit != *tmunit_m)); |
117 | 137 |
118 TimeUnit::UTimeUnitFields field = tmunit_m->getTimeUnitField(); | 138 TimeUnit::UTimeUnitFields field = tmunit_m->getTimeUnitField(); |
119 assertTrue("field of month time unit is month", (field == TimeUnit::UTIMEUNI
T_MONTH)); | 139 assertTrue("field of month time unit is month", (field == TimeUnit::UTIMEUNI
T_MONTH)); |
120 | 140 |
| 141 //===== Interoperability with MeasureUnit ====== |
| 142 MeasureUnit **ptrs = new MeasureUnit *[TimeUnit::UTIMEUNIT_FIELD_COUNT]; |
| 143 |
| 144 ptrs[TimeUnit::UTIMEUNIT_YEAR] = MeasureUnit::createYear(status); |
| 145 ptrs[TimeUnit::UTIMEUNIT_MONTH] = MeasureUnit::createMonth(status); |
| 146 ptrs[TimeUnit::UTIMEUNIT_DAY] = MeasureUnit::createDay(status); |
| 147 ptrs[TimeUnit::UTIMEUNIT_WEEK] = MeasureUnit::createWeek(status); |
| 148 ptrs[TimeUnit::UTIMEUNIT_HOUR] = MeasureUnit::createHour(status); |
| 149 ptrs[TimeUnit::UTIMEUNIT_MINUTE] = MeasureUnit::createMinute(status); |
| 150 ptrs[TimeUnit::UTIMEUNIT_SECOND] = MeasureUnit::createSecond(status); |
| 151 if (!assertSuccess("TimeUnit::createInstance", status)) return; |
| 152 |
| 153 for (TimeUnit::UTimeUnitFields j = TimeUnit::UTIMEUNIT_YEAR; |
| 154 j < TimeUnit::UTIMEUNIT_FIELD_COUNT; |
| 155 j = (TimeUnit::UTimeUnitFields)(j+1)) { |
| 156 MeasureUnit *ptr = TimeUnit::createInstance(j, status); |
| 157 if (!assertSuccess("TimeUnit::createInstance", status)) return; |
| 158 // We have to convert *ptr to a MeasureUnit or else == will fail over |
| 159 // differing types (TimeUnit vs. MeasureUnit). |
| 160 assertTrue( |
| 161 "Time unit should be equal to corresponding MeasureUnit", |
| 162 MeasureUnit(*ptr) == *ptrs[j]); |
| 163 delete ptr; |
| 164 } |
121 delete tmunit; | 165 delete tmunit; |
122 delete another; | 166 delete another; |
123 delete tmunit_m; | 167 delete tmunit_m; |
| 168 for (int i = 0; i < TimeUnit::UTIMEUNIT_FIELD_COUNT; ++i) { |
| 169 delete ptrs[i]; |
| 170 } |
| 171 delete [] ptrs; |
| 172 |
124 // | 173 // |
125 //================= TimeUnitAmount ================= | 174 //================= TimeUnitAmount ================= |
126 | 175 |
127 Formattable formattable((int32_t)2); | 176 Formattable formattable((int32_t)2); |
128 TimeUnitAmount tma_long(formattable, TimeUnit::UTIMEUNIT_DAY, status); | 177 TimeUnitAmount tma_long(formattable, TimeUnit::UTIMEUNIT_DAY, status); |
129 if (!assertSuccess("TimeUnitAmount(formattable...)", status)) return; | 178 if (!assertSuccess("TimeUnitAmount(formattable...)", status)) return; |
130 | 179 |
131 formattable.setDouble(2); | 180 formattable.setDouble(2); |
132 TimeUnitAmount tma_double(formattable, TimeUnit::UTIMEUNIT_DAY, status); | 181 TimeUnitAmount tma_double(formattable, TimeUnit::UTIMEUNIT_DAY, status); |
133 if (!assertSuccess("TimeUnitAmount(formattable...)", status)) return; | 182 if (!assertSuccess("TimeUnitAmount(formattable...)", status)) return; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 268 |
220 const UChar oneSecond[] = {0x0031, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4, 0
x03b5, 0x03c1, 0x03cc, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03bf, 0}; | 269 const UChar oneSecond[] = {0x0031, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4, 0
x03b5, 0x03c1, 0x03cc, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03bf, 0}; |
221 const UChar oneSecondShort[] = {0x0031, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03
c4, 0x002e, 0}; | 270 const UChar oneSecondShort[] = {0x0031, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03
c4, 0x002e, 0}; |
222 const UChar oneMinute[] = {0x0031, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0
x03cc, 0}; | 271 const UChar oneMinute[] = {0x0031, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0
x03cc, 0}; |
223 const UChar oneMinuteShort[] = {0x0031, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0x00
2e, 0}; | 272 const UChar oneMinuteShort[] = {0x0031, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0x00
2e, 0}; |
224 const UChar oneHour[] = {0x0031, 0x0020, 0x03ce, 0x03c1, 0x03b1, 0}; | 273 const UChar oneHour[] = {0x0031, 0x0020, 0x03ce, 0x03c1, 0x03b1, 0}; |
225 const UChar oneDay[] = {0x0031, 0x0020, 0x03b7, 0x03bc, 0x03ad, 0x03c1, 0x03
b1, 0}; | 274 const UChar oneDay[] = {0x0031, 0x0020, 0x03b7, 0x03bc, 0x03ad, 0x03c1, 0x03
b1, 0}; |
226 const UChar oneMonth[] = {0x0031, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x03b1, 0x
03c2, 0}; | 275 const UChar oneMonth[] = {0x0031, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x03b1, 0x
03c2, 0}; |
227 const UChar oneMonthShort[] = {0x0031, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x002
e, 0}; | 276 const UChar oneMonthShort[] = {0x0031, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x002
e, 0}; |
228 const UChar oneYear[] = {0x0031, 0x0020, 0x03ad, 0x03c4, 0x03bf, 0x03c2, 0}; | 277 const UChar oneYear[] = {0x0031, 0x0020, 0x03ad, 0x03c4, 0x03bf, 0x03c2, 0}; |
| 278 const UChar oneYearShort[] = {0x0031, 0x0020, 0x03ad, 0x03c4, 0x002e, 0}; |
229 const UChar sevenSeconds[] = {0x0037, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4
, 0x03b5, 0x03c1, 0x03cc, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03b1, 0}; | 279 const UChar sevenSeconds[] = {0x0037, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4
, 0x03b5, 0x03c1, 0x03cc, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03b1, 0}; |
230 const UChar sevenSecondsShort[] = {0x0037, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0
x03c4, 0x002e, 0}; | 280 const UChar sevenSecondsShort[] = {0x0037, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0
x03c4, 0x002e, 0}; |
231 const UChar sevenMinutes[] = {0x0037, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0x03c4
, 0x03ac, 0}; | 281 const UChar sevenMinutes[] = {0x0037, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0x03c4
, 0x03ac, 0}; |
232 const UChar sevenMinutesShort[] = {0x0037, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0
x002e, 0}; | 282 const UChar sevenMinutesShort[] = {0x0037, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0
x002e, 0}; |
233 const UChar sevenHours[] = {0x0037, 0x0020, 0x03ce, 0x03c1, 0x03b5, 0x03c2,
0}; | 283 const UChar sevenHours[] = {0x0037, 0x0020, 0x03ce, 0x03c1, 0x03b5, 0x03c2,
0}; |
| 284 const UChar sevenHoursShort[] = {0x0037, 0x0020, 0x03ce, 0x03c1, 0x002e, 0}; |
234 const UChar sevenDays[] = {0x0037, 0x0020, 0x03b7, 0x03bc, 0x03ad, 0x03c1, 0
x03b5, 0x03c2, 0}; | 285 const UChar sevenDays[] = {0x0037, 0x0020, 0x03b7, 0x03bc, 0x03ad, 0x03c1, 0
x03b5, 0x03c2, 0}; |
235 const UChar sevenMonths[] = {0x0037, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x03b5,
0x3c2, 0}; | 286 const UChar sevenMonths[] = {0x0037, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x03b5,
0x3c2, 0}; |
236 const UChar sevenMonthsShort[] = {0x0037, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x
002e, 0}; | 287 const UChar sevenMonthsShort[] = {0x0037, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x
002e, 0}; |
237 const UChar sevenYears[] = {0x0037, 0x0020, 0x03ad, 0x03c4, 0x03b7, 0}; | 288 const UChar sevenYears[] = {0x0037, 0x0020, 0x03ad, 0x03c4, 0x03b7, 0}; |
| 289 const UChar sevenYearsShort[] = {0x0037, 0x0020, 0x03ad, 0x03c4, 0x002e, 0}; |
238 | 290 |
239 const UnicodeString oneSecondStr(oneSecond); | 291 const UnicodeString oneSecondStr(oneSecond); |
240 const UnicodeString oneSecondShortStr(oneSecondShort); | 292 const UnicodeString oneSecondShortStr(oneSecondShort); |
241 const UnicodeString oneMinuteStr(oneMinute); | 293 const UnicodeString oneMinuteStr(oneMinute); |
242 const UnicodeString oneMinuteShortStr(oneMinuteShort); | 294 const UnicodeString oneMinuteShortStr(oneMinuteShort); |
243 const UnicodeString oneHourStr(oneHour); | 295 const UnicodeString oneHourStr(oneHour); |
244 const UnicodeString oneDayStr(oneDay); | 296 const UnicodeString oneDayStr(oneDay); |
245 const UnicodeString oneMonthStr(oneMonth); | 297 const UnicodeString oneMonthStr(oneMonth); |
246 const UnicodeString oneMonthShortStr(oneMonthShort); | 298 const UnicodeString oneMonthShortStr(oneMonthShort); |
247 const UnicodeString oneYearStr(oneYear); | 299 const UnicodeString oneYearStr(oneYear); |
| 300 const UnicodeString oneYearShortStr(oneYearShort); |
248 const UnicodeString sevenSecondsStr(sevenSeconds); | 301 const UnicodeString sevenSecondsStr(sevenSeconds); |
249 const UnicodeString sevenSecondsShortStr(sevenSecondsShort); | 302 const UnicodeString sevenSecondsShortStr(sevenSecondsShort); |
250 const UnicodeString sevenMinutesStr(sevenMinutes); | 303 const UnicodeString sevenMinutesStr(sevenMinutes); |
251 const UnicodeString sevenMinutesShortStr(sevenMinutesShort); | 304 const UnicodeString sevenMinutesShortStr(sevenMinutesShort); |
252 const UnicodeString sevenHoursStr(sevenHours); | 305 const UnicodeString sevenHoursStr(sevenHours); |
| 306 const UnicodeString sevenHoursShortStr(sevenHoursShort); |
253 const UnicodeString sevenDaysStr(sevenDays); | 307 const UnicodeString sevenDaysStr(sevenDays); |
254 const UnicodeString sevenMonthsStr(sevenMonths); | 308 const UnicodeString sevenMonthsStr(sevenMonths); |
255 const UnicodeString sevenMonthsShortStr(sevenMonthsShort); | 309 const UnicodeString sevenMonthsShortStr(sevenMonthsShort); |
256 const UnicodeString sevenYearsStr(sevenYears); | 310 const UnicodeString sevenYearsStr(sevenYears); |
| 311 const UnicodeString sevenYearsShortStr(sevenYearsShort); |
257 | 312 |
258 const UnicodeString expected[] = {oneSecondStr, oneMinuteStr, oneHourStr, on
eDayStr, oneMonthStr, oneYearStr, | 313 const UnicodeString expected[] = { |
259 oneSecondShortStr, oneMinuteShortStr, oneHourStr,
oneDayStr, oneMonthShortStr, oneYearStr, | 314 oneSecondStr, oneMinuteStr, oneHourStr, oneDayStr, oneMonthStr, oneY
earStr, |
260 sevenSecondsStr, sevenMinutesStr, sevenHoursStr, s
evenDaysStr, sevenMonthsStr, sevenYearsStr, | 315 oneSecondShortStr, oneMinuteShortStr, oneHourStr, oneDayStr, oneMont
hShortStr, oneYearShortStr, |
261 sevenSecondsShortStr, sevenMinutesShortStr, sevenH
oursStr, sevenDaysStr, sevenMonthsShortStr, sevenYearsStr, | 316 sevenSecondsStr, sevenMinutesStr, sevenHoursStr, sevenDaysStr, seven
MonthsStr, sevenYearsStr, |
262 oneSecondStr, oneMinuteStr, oneHourStr, oneDayStr,
oneMonthStr, oneYearStr, | 317 sevenSecondsShortStr, sevenMinutesShortStr, sevenHoursShortStr, seve
nDaysStr, sevenMonthsShortStr, sevenYearsShortStr, |
263 oneSecondShortStr, oneMinuteShortStr, oneHourStr,
oneDayStr, oneMonthShortStr, oneYearStr, | 318 |
264 sevenSecondsStr, sevenMinutesStr, sevenHoursStr, s
evenDaysStr, sevenMonthsStr, sevenYearsStr, | 319 oneSecondStr, oneMinuteStr, oneHourStr, oneDayStr, oneMonthStr, oneY
earStr, |
265 sevenSecondsShortStr, sevenMinutesShortStr, sevenH
oursStr, sevenDaysStr, sevenMonthsShortStr, sevenYearsStr}; | 320 oneSecondShortStr, oneMinuteShortStr, oneHourStr, oneDayStr, oneMont
hShortStr, oneYearShortStr, |
| 321 sevenSecondsStr, sevenMinutesStr, sevenHoursStr, sevenDaysStr, seven
MonthsStr, sevenYearsStr, |
| 322 sevenSecondsShortStr, sevenMinutesShortStr, sevenHoursShortStr, seve
nDaysStr, sevenMonthsShortStr, sevenYearsShortStr}; |
266 | 323 |
267 int counter = 0; | 324 int counter = 0; |
268 for ( unsigned int locIndex = 0; | 325 for ( unsigned int locIndex = 0; |
269 locIndex < sizeof(locales)/sizeof(locales[0]); | 326 locIndex < sizeof(locales)/sizeof(locales[0]); |
270 ++locIndex ) { | 327 ++locIndex ) { |
271 | 328 |
272 Locale l = Locale::createFromName(locales[locIndex]); | 329 Locale l = Locale::createFromName(locales[locIndex]); |
273 | 330 |
274 for ( unsigned int numberIndex = 0; | 331 for ( unsigned int numberIndex = 0; |
275 numberIndex < sizeof(numbers)/sizeof(int); | 332 numberIndex < sizeof(numbers)/sizeof(int); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 | 404 |
348 TimeUnitFormat* timeUnitFormat = new TimeUnitFormat(elLoc, status); | 405 TimeUnitFormat* timeUnitFormat = new TimeUnitFormat(elLoc, status); |
349 if (!assertSuccess("TimeUnitFormat::TimeUnitFormat for el locale", status))
return; | 406 if (!assertSuccess("TimeUnitFormat::TimeUnitFormat for el locale", status))
return; |
350 | 407 |
351 timeUnitFormat->setNumberFormat(*numberFmt, status); | 408 timeUnitFormat->setNumberFormat(*numberFmt, status); |
352 | 409 |
353 delete numberFmt; | 410 delete numberFmt; |
354 delete timeUnitFormat; | 411 delete timeUnitFormat; |
355 } | 412 } |
356 | 413 |
| 414 void TimeUnitTest::test10219Plurals() { |
| 415 Locale usLocale("en_US"); |
| 416 double values[2] = {1.588, 1.011}; |
| 417 UnicodeString expected[2][3] = { |
| 418 {"1 minute", "1.5 minutes", "1.58 minutes"}, |
| 419 {"1 minute", "1.0 minutes", "1.01 minutes"} |
| 420 }; |
| 421 UErrorCode status = U_ZERO_ERROR; |
| 422 TimeUnitFormat tuf(usLocale, status); |
| 423 if (U_FAILURE(status)) { |
| 424 dataerrln("generating TimeUnitFormat Object failed: %s", u_errorName(sta
tus)); |
| 425 return; |
| 426 } |
| 427 LocalPointer<DecimalFormat> nf((DecimalFormat *) NumberFormat::createInstanc
e(usLocale, status)); |
| 428 if (U_FAILURE(status)) { |
| 429 dataerrln("generating NumberFormat Object failed: %s", u_errorName(statu
s)); |
| 430 return; |
| 431 } |
| 432 for (int32_t j = 0; j < UPRV_LENGTHOF(values); ++j) { |
| 433 for (int32_t i = 0; i < UPRV_LENGTHOF(expected[j]); ++i) { |
| 434 nf->setMinimumFractionDigits(i); |
| 435 nf->setMaximumFractionDigits(i); |
| 436 nf->setRoundingMode(DecimalFormat::kRoundDown); |
| 437 tuf.setNumberFormat(*nf, status); |
| 438 if (U_FAILURE(status)) { |
| 439 dataerrln("setting NumberFormat failed: %s", u_errorName(status)
); |
| 440 return; |
| 441 } |
| 442 UnicodeString actual; |
| 443 Formattable fmt; |
| 444 LocalPointer<TimeUnitAmount> tamt(new TimeUnitAmount(values[j], Time
Unit::UTIMEUNIT_MINUTE, status)); |
| 445 if (U_FAILURE(status)) { |
| 446 dataerrln("generating TimeUnitAmount Object failed: %s", u_error
Name(status)); |
| 447 return; |
| 448 } |
| 449 fmt.adoptObject(tamt.orphan()); |
| 450 tuf.format(fmt, actual, status); |
| 451 if (U_FAILURE(status)) { |
| 452 dataerrln("Actual formatting failed: %s", u_errorName(status)); |
| 453 return; |
| 454 } |
| 455 if (expected[j][i] != actual) { |
| 456 errln("Expected " + expected[j][i] + ", got " + actual); |
| 457 } |
| 458 } |
| 459 } |
| 460 |
| 461 // test parsing |
| 462 Formattable result; |
| 463 ParsePosition pos; |
| 464 UnicodeString formattedString = "1 minutes"; |
| 465 tuf.parseObject(formattedString, result, pos); |
| 466 if (formattedString.length() != pos.getIndex()) { |
| 467 errln("Expect parsing to go all the way to the end of the string."); |
| 468 } |
| 469 } |
| 470 |
357 #endif | 471 #endif |
OLD | NEW |