2 *******************************************************************************
3 * Copyright (C) 1996-2000, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/fmtable.h"
15 #include "math.h" // fabs
17 // current macro not in icu1.8.1
18 #define TESTCASE(id,test) \
23 logln((UnicodeString)""); \
28 void RbnfRoundTripTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/)
30 if (exec
) logln("TestSuite RuleBasedNumberFormatRT");
33 TESTCASE(0, TestEnglishSpelloutRT
);
34 TESTCASE(1, TestDurationsRT
);
35 TESTCASE(2, TestSpanishSpelloutRT
);
36 TESTCASE(3, TestFrenchSpelloutRT
);
37 TESTCASE(4, TestSwissFrenchSpelloutRT
);
38 TESTCASE(5, TestItalianSpelloutRT
);
39 TESTCASE(6, TestGermanSpelloutRT
);
40 TESTCASE(7, TestSwedishSpelloutRT
);
41 TESTCASE(8, TestDutchSpelloutRT
);
42 TESTCASE(9, TestJapaneseSpelloutRT
);
43 TESTCASE(10, TestRussianSpelloutRT
);
44 TESTCASE(11, TestGreekSpelloutRT
);
46 TESTCASE(0, TestRBNFDisabled
);
57 * Perform an exhaustive round-trip test on the English spellout rules
60 RbnfRoundTripTest::TestEnglishSpelloutRT()
62 UErrorCode status
= U_ZERO_ERROR
;
63 RuleBasedNumberFormat
* formatter
64 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
66 if (U_FAILURE(status
)) {
67 errln("failed to construct formatter");
69 doTest(formatter
, -12345678, 12345678);
75 * Perform an exhaustive round-trip test on the duration-formatting rules
78 RbnfRoundTripTest::TestDurationsRT()
80 UErrorCode status
= U_ZERO_ERROR
;
81 RuleBasedNumberFormat
* formatter
82 = new RuleBasedNumberFormat(URBNF_DURATION
, Locale::getUS(), status
);
84 if (U_FAILURE(status
)) {
85 errln("failed to construct formatter");
87 doTest(formatter
, 0, 12345678);
93 * Perform an exhaustive round-trip test on the Spanish spellout rules
96 RbnfRoundTripTest::TestSpanishSpelloutRT()
98 UErrorCode status
= U_ZERO_ERROR
;
99 RuleBasedNumberFormat
* formatter
100 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("es", "es"), status
);
102 if (U_FAILURE(status
)) {
103 errln("failed to construct formatter");
105 doTest(formatter
, -12345678, 12345678);
111 * Perform an exhaustive round-trip test on the French spellout rules
114 RbnfRoundTripTest::TestFrenchSpelloutRT()
116 UErrorCode status
= U_ZERO_ERROR
;
117 RuleBasedNumberFormat
* formatter
118 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getFrance(), status
);
120 if (U_FAILURE(status
)) {
121 errln("failed to construct formatter");
123 doTest(formatter
, -12345678, 12345678);
129 * Perform an exhaustive round-trip test on the Swiss French spellout rules
132 RbnfRoundTripTest::TestSwissFrenchSpelloutRT()
134 UErrorCode status
= U_ZERO_ERROR
;
135 RuleBasedNumberFormat
* formatter
136 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "CH"), status
);
138 if (U_FAILURE(status
)) {
139 errln("failed to construct formatter");
141 doTest(formatter
, -12345678, 12345678);
147 * Perform an exhaustive round-trip test on the Italian spellout rules
150 RbnfRoundTripTest::TestItalianSpelloutRT()
152 UErrorCode status
= U_ZERO_ERROR
;
153 RuleBasedNumberFormat
* formatter
154 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getItalian(), status
);
156 if (U_FAILURE(status
)) {
157 errln("failed to construct formatter");
159 doTest(formatter
, -999999, 999999);
165 * Perform an exhaustive round-trip test on the German spellout rules
168 RbnfRoundTripTest::TestGermanSpelloutRT()
170 UErrorCode status
= U_ZERO_ERROR
;
171 RuleBasedNumberFormat
* formatter
172 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getGermany(), status
);
174 if (U_FAILURE(status
)) {
175 errln("failed to construct formatter");
177 doTest(formatter
, 0, 12345678);
183 * Perform an exhaustive round-trip test on the Swedish spellout rules
186 RbnfRoundTripTest::TestSwedishSpelloutRT()
188 UErrorCode status
= U_ZERO_ERROR
;
189 RuleBasedNumberFormat
* formatter
190 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("sv", "SE"), status
);
192 if (U_FAILURE(status
)) {
193 errln("failed to construct formatter");
195 doTest(formatter
, 0, 12345678);
201 * Perform an exhaustive round-trip test on the Dutch spellout rules
204 RbnfRoundTripTest::TestDutchSpelloutRT()
206 UErrorCode status
= U_ZERO_ERROR
;
207 RuleBasedNumberFormat
* formatter
208 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("nl", "NL"), status
);
210 if (U_FAILURE(status
)) {
211 errln("failed to construct formatter");
213 doTest(formatter
, -12345678, 12345678);
219 * Perform an exhaustive round-trip test on the Japanese spellout rules
222 RbnfRoundTripTest::TestJapaneseSpelloutRT()
224 UErrorCode status
= U_ZERO_ERROR
;
225 RuleBasedNumberFormat
* formatter
226 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getJapan(), status
);
228 if (U_FAILURE(status
)) {
229 errln("failed to construct formatter");
231 doTest(formatter
, 0, 12345678);
237 * Perform an exhaustive round-trip test on the Russian spellout rules
240 RbnfRoundTripTest::TestRussianSpelloutRT()
242 UErrorCode status
= U_ZERO_ERROR
;
243 RuleBasedNumberFormat
* formatter
244 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("ru", "RU"), status
);
246 if (U_FAILURE(status
)) {
247 errln("failed to construct formatter");
249 doTest(formatter
, 0, 12345678);
255 * Perform an exhaustive round-trip test on the Greek spellout rules
258 RbnfRoundTripTest::TestGreekSpelloutRT()
260 UErrorCode status
= U_ZERO_ERROR
;
261 RuleBasedNumberFormat
* formatter
262 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("el", "GR"), status
);
264 if (U_FAILURE(status
)) {
265 errln("failed to construct formatter");
267 doTest(formatter
, 0, 12345678);
273 RbnfRoundTripTest::doTest(const RuleBasedNumberFormat
* formatter
,
280 double increment
= 1;
281 for (double i
= lowLimit
; i
<= highLimit
; i
+= increment
) {
282 if (count
% 1000 == 0) {
283 sprintf(buf
, "%.12g", i
);
289 else if (fabs(i
) < 500000)
294 UnicodeString formatResult
;
295 formatter
->format(i
, formatResult
);
296 UErrorCode status
= U_ZERO_ERROR
;
297 Formattable parseResult
;
298 formatter
->parse(formatResult
, parseResult
, status
);
299 if (U_FAILURE(status
)) {
300 sprintf(buf
, "Round-trip status failure: %.12g, status: %d", i
, status
);
304 double rt
= (parseResult
.getType() == Formattable::kDouble
) ?
305 parseResult
.getDouble() :
306 (double)parseResult
.getLong();
309 sprintf(buf
, "Round-trip failed: %.12g -> %.12g", i
, rt
);
321 UnicodeString formatResult
;
322 formatter
->format(d
, formatResult
);
323 UErrorCode status
= U_ZERO_ERROR
;
324 Formattable parseResult
;
325 formatter
->parse(formatResult
, parseResult
, status
);
326 if (U_FAILURE(status
)) {
327 sprintf(buf
, "Round-trip status failure: %.12g, status: %d", d
, status
);
331 double rt
= (parseResult
.getType() == Formattable::kDouble
) ?
332 parseResult
.getDouble() :
333 (double)parseResult
.getLong();
337 sprintf(buf
, "Round-trip failed: %.12g -> ", d
);
339 msg
.append(formatResult
);
340 sprintf(buf
, " -> %.12g", rt
);
356 RbnfRoundTripTest::TestRBNFDisabled() {
357 errln("*** RBNF currently disabled on this platform ***\n");
363 #endif /* #if !UCONFIG_NO_FORMATTING */