2 *******************************************************************************
3 * Copyright (C) 1996-2009, 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
18 // current macro not in icu1.8.1
19 #define TESTCASE(id,test) \
29 void RbnfRoundTripTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/)
31 if (exec
) logln("TestSuite RuleBasedNumberFormatRT");
34 TESTCASE(0, TestEnglishSpelloutRT
);
35 TESTCASE(1, TestDurationsRT
);
36 TESTCASE(2, TestSpanishSpelloutRT
);
37 TESTCASE(3, TestFrenchSpelloutRT
);
38 TESTCASE(4, TestSwissFrenchSpelloutRT
);
39 TESTCASE(5, TestItalianSpelloutRT
);
40 TESTCASE(6, TestGermanSpelloutRT
);
41 TESTCASE(7, TestSwedishSpelloutRT
);
42 TESTCASE(8, TestDutchSpelloutRT
);
43 TESTCASE(9, TestJapaneseSpelloutRT
);
44 TESTCASE(10, TestRussianSpelloutRT
);
45 TESTCASE(11, TestPortugueseSpelloutRT
);
47 TESTCASE(0, TestRBNFDisabled
);
58 * Perform an exhaustive round-trip test on the English spellout rules
61 RbnfRoundTripTest::TestEnglishSpelloutRT()
63 UErrorCode status
= U_ZERO_ERROR
;
64 RuleBasedNumberFormat
* formatter
65 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
67 if (U_FAILURE(status
)) {
68 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
70 doTest(formatter
, -12345678, 12345678);
76 * Perform an exhaustive round-trip test on the duration-formatting rules
79 RbnfRoundTripTest::TestDurationsRT()
81 UErrorCode status
= U_ZERO_ERROR
;
82 RuleBasedNumberFormat
* formatter
83 = new RuleBasedNumberFormat(URBNF_DURATION
, Locale::getUS(), status
);
85 if (U_FAILURE(status
)) {
86 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
88 doTest(formatter
, 0, 12345678);
94 * Perform an exhaustive round-trip test on the Spanish spellout rules
97 RbnfRoundTripTest::TestSpanishSpelloutRT()
99 UErrorCode status
= U_ZERO_ERROR
;
100 RuleBasedNumberFormat
* formatter
101 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("es", "es"), status
);
103 if (U_FAILURE(status
)) {
104 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
106 doTest(formatter
, -12345678, 12345678);
112 * Perform an exhaustive round-trip test on the French spellout rules
115 RbnfRoundTripTest::TestFrenchSpelloutRT()
117 UErrorCode status
= U_ZERO_ERROR
;
118 RuleBasedNumberFormat
* formatter
119 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getFrance(), status
);
121 if (U_FAILURE(status
)) {
122 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
124 doTest(formatter
, -12345678, 12345678);
130 * Perform an exhaustive round-trip test on the Swiss French spellout rules
133 RbnfRoundTripTest::TestSwissFrenchSpelloutRT()
135 UErrorCode status
= U_ZERO_ERROR
;
136 RuleBasedNumberFormat
* formatter
137 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "CH"), status
);
139 if (U_FAILURE(status
)) {
140 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
142 doTest(formatter
, -12345678, 12345678);
148 * Perform an exhaustive round-trip test on the Italian spellout rules
151 RbnfRoundTripTest::TestItalianSpelloutRT()
153 UErrorCode status
= U_ZERO_ERROR
;
154 RuleBasedNumberFormat
* formatter
155 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getItalian(), status
);
157 if (U_FAILURE(status
)) {
158 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
160 doTest(formatter
, -999999, 999999);
166 * Perform an exhaustive round-trip test on the German spellout rules
169 RbnfRoundTripTest::TestGermanSpelloutRT()
171 UErrorCode status
= U_ZERO_ERROR
;
172 RuleBasedNumberFormat
* formatter
173 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getGermany(), status
);
175 if (U_FAILURE(status
)) {
176 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
178 doTest(formatter
, 0, 12345678);
184 * Perform an exhaustive round-trip test on the Swedish spellout rules
187 RbnfRoundTripTest::TestSwedishSpelloutRT()
189 UErrorCode status
= U_ZERO_ERROR
;
190 RuleBasedNumberFormat
* formatter
191 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("sv", "SE"), status
);
193 if (U_FAILURE(status
)) {
194 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
196 doTest(formatter
, 0, 12345678);
202 * Perform an exhaustive round-trip test on the Dutch spellout rules
205 RbnfRoundTripTest::TestDutchSpelloutRT()
207 UErrorCode status
= U_ZERO_ERROR
;
208 RuleBasedNumberFormat
* formatter
209 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("nl", "NL"), status
);
211 if (U_FAILURE(status
)) {
212 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
214 doTest(formatter
, -12345678, 12345678);
220 * Perform an exhaustive round-trip test on the Japanese spellout rules
223 RbnfRoundTripTest::TestJapaneseSpelloutRT()
225 UErrorCode status
= U_ZERO_ERROR
;
226 RuleBasedNumberFormat
* formatter
227 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getJapan(), status
);
229 if (U_FAILURE(status
)) {
230 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
232 doTest(formatter
, 0, 12345678);
238 * Perform an exhaustive round-trip test on the Russian spellout rules
241 RbnfRoundTripTest::TestRussianSpelloutRT()
243 UErrorCode status
= U_ZERO_ERROR
;
244 RuleBasedNumberFormat
* formatter
245 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("ru", "RU"), status
);
247 if (U_FAILURE(status
)) {
248 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
250 doTest(formatter
, 0, 12345678);
256 * Perform an exhaustive round-trip test on the Portuguese spellout rules
259 RbnfRoundTripTest::TestPortugueseSpelloutRT()
261 UErrorCode status
= U_ZERO_ERROR
;
262 RuleBasedNumberFormat
* formatter
263 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("pt", "BR"), status
);
265 if (U_FAILURE(status
)) {
266 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
268 doTest(formatter
, -12345678, 12345678);
274 RbnfRoundTripTest::doTest(const RuleBasedNumberFormat
* formatter
,
281 double increment
= 1;
282 for (double i
= lowLimit
; i
<= highLimit
; i
+= increment
) {
283 if (count
% 1000 == 0) {
284 sprintf(buf
, "%.12g", i
);
290 else if (fabs(i
) < 500000)
295 UnicodeString formatResult
;
296 formatter
->format(i
, formatResult
);
297 UErrorCode status
= U_ZERO_ERROR
;
298 Formattable parseResult
;
299 formatter
->parse(formatResult
, parseResult
, status
);
300 if (U_FAILURE(status
)) {
301 sprintf(buf
, "Round-trip status failure: %.12g, status: %d", i
, status
);
305 double rt
= (parseResult
.getType() == Formattable::kDouble
) ?
306 parseResult
.getDouble() :
307 (double)parseResult
.getLong();
310 sprintf(buf
, "Round-trip failed: %.12g -> %.12g", i
, rt
);
322 UnicodeString formatResult
;
323 formatter
->format(d
, formatResult
);
324 UErrorCode status
= U_ZERO_ERROR
;
325 Formattable parseResult
;
326 formatter
->parse(formatResult
, parseResult
, status
);
327 if (U_FAILURE(status
)) {
328 sprintf(buf
, "Round-trip status failure: %.12g, status: %d", d
, status
);
332 double rt
= (parseResult
.getType() == Formattable::kDouble
) ?
333 parseResult
.getDouble() :
334 (double)parseResult
.getLong();
338 sprintf(buf
, "Round-trip failed: %.12g -> ", d
);
340 msg
.append(formatResult
);
341 sprintf(buf
, " -> %.12g", rt
);
357 RbnfRoundTripTest::TestRBNFDisabled() {
358 errln("*** RBNF currently disabled on this platform ***\n");
364 #endif /* #if !UCONFIG_NO_FORMATTING */