1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 1996-2009, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
16 #include "unicode/fmtable.h"
17 #include <math.h> // fabs
20 // current macro not in icu1.8.1
21 #define TESTCASE(id,test) \
31 void RbnfRoundTripTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/)
33 if (exec
) logln("TestSuite RuleBasedNumberFormatRT");
36 TESTCASE(0, TestEnglishSpelloutRT
);
37 TESTCASE(1, TestDurationsRT
);
38 TESTCASE(2, TestSpanishSpelloutRT
);
39 TESTCASE(3, TestFrenchSpelloutRT
);
40 TESTCASE(4, TestSwissFrenchSpelloutRT
);
41 TESTCASE(5, TestItalianSpelloutRT
);
42 TESTCASE(6, TestGermanSpelloutRT
);
43 TESTCASE(7, TestSwedishSpelloutRT
);
44 TESTCASE(8, TestDutchSpelloutRT
);
45 TESTCASE(9, TestJapaneseSpelloutRT
);
46 TESTCASE(10, TestRussianSpelloutRT
);
47 TESTCASE(11, TestPortugueseSpelloutRT
);
49 TESTCASE(0, TestRBNFDisabled
);
60 * Perform an exhaustive round-trip test on the English spellout rules
63 RbnfRoundTripTest::TestEnglishSpelloutRT()
65 UErrorCode status
= U_ZERO_ERROR
;
66 RuleBasedNumberFormat
* formatter
67 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
69 if (U_FAILURE(status
)) {
70 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
72 doTest(formatter
, -12345678, 12345678);
78 * Perform an exhaustive round-trip test on the duration-formatting rules
81 RbnfRoundTripTest::TestDurationsRT()
83 UErrorCode status
= U_ZERO_ERROR
;
84 RuleBasedNumberFormat
* formatter
85 = new RuleBasedNumberFormat(URBNF_DURATION
, Locale::getUS(), status
);
87 if (U_FAILURE(status
)) {
88 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
90 doTest(formatter
, 0, 12345678);
96 * Perform an exhaustive round-trip test on the Spanish spellout rules
99 RbnfRoundTripTest::TestSpanishSpelloutRT()
101 UErrorCode status
= U_ZERO_ERROR
;
102 RuleBasedNumberFormat
* formatter
103 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("es", "es"), status
);
105 if (U_FAILURE(status
)) {
106 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
108 doTest(formatter
, -12345678, 12345678);
114 * Perform an exhaustive round-trip test on the French spellout rules
117 RbnfRoundTripTest::TestFrenchSpelloutRT()
119 UErrorCode status
= U_ZERO_ERROR
;
120 RuleBasedNumberFormat
* formatter
121 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getFrance(), status
);
123 if (U_FAILURE(status
)) {
124 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
126 doTest(formatter
, -12345678, 12345678);
132 * Perform an exhaustive round-trip test on the Swiss French spellout rules
135 RbnfRoundTripTest::TestSwissFrenchSpelloutRT()
137 UErrorCode status
= U_ZERO_ERROR
;
138 RuleBasedNumberFormat
* formatter
139 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "CH"), status
);
141 if (U_FAILURE(status
)) {
142 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
144 doTest(formatter
, -12345678, 12345678);
150 * Perform an exhaustive round-trip test on the Italian spellout rules
153 RbnfRoundTripTest::TestItalianSpelloutRT()
155 UErrorCode status
= U_ZERO_ERROR
;
156 RuleBasedNumberFormat
* formatter
157 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getItalian(), status
);
159 if (U_FAILURE(status
)) {
160 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
162 doTest(formatter
, -999999, 999999);
168 * Perform an exhaustive round-trip test on the German spellout rules
171 RbnfRoundTripTest::TestGermanSpelloutRT()
173 UErrorCode status
= U_ZERO_ERROR
;
174 RuleBasedNumberFormat
* formatter
175 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getGermany(), status
);
177 if (U_FAILURE(status
)) {
178 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
180 doTest(formatter
, 0, 12345678);
186 * Perform an exhaustive round-trip test on the Swedish spellout rules
189 RbnfRoundTripTest::TestSwedishSpelloutRT()
191 UErrorCode status
= U_ZERO_ERROR
;
192 RuleBasedNumberFormat
* formatter
193 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("sv", "SE"), status
);
195 if (U_FAILURE(status
)) {
196 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
198 doTest(formatter
, 0, 12345678);
204 * Perform an exhaustive round-trip test on the Dutch spellout rules
207 RbnfRoundTripTest::TestDutchSpelloutRT()
209 UErrorCode status
= U_ZERO_ERROR
;
210 RuleBasedNumberFormat
* formatter
211 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("nl", "NL"), status
);
213 if (U_FAILURE(status
)) {
214 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
216 doTest(formatter
, -12345678, 12345678);
222 * Perform an exhaustive round-trip test on the Japanese spellout rules
225 RbnfRoundTripTest::TestJapaneseSpelloutRT()
227 UErrorCode status
= U_ZERO_ERROR
;
228 RuleBasedNumberFormat
* formatter
229 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getJapan(), status
);
231 if (U_FAILURE(status
)) {
232 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
234 doTest(formatter
, 0, 12345678);
240 * Perform an exhaustive round-trip test on the Russian spellout rules
243 RbnfRoundTripTest::TestRussianSpelloutRT()
245 UErrorCode status
= U_ZERO_ERROR
;
246 RuleBasedNumberFormat
* formatter
247 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("ru", "RU"), status
);
249 if (U_FAILURE(status
)) {
250 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
252 doTest(formatter
, 0, 12345678);
258 * Perform an exhaustive round-trip test on the Portuguese spellout rules
261 RbnfRoundTripTest::TestPortugueseSpelloutRT()
263 UErrorCode status
= U_ZERO_ERROR
;
264 RuleBasedNumberFormat
* formatter
265 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("pt", "BR"), status
);
267 if (U_FAILURE(status
)) {
268 errcheckln(status
, "failed to construct formatter - %s", u_errorName(status
));
270 doTest(formatter
, -12345678, 12345678);
276 RbnfRoundTripTest::doTest(const RuleBasedNumberFormat
* formatter
,
283 double increment
= 1;
284 for (double i
= lowLimit
; i
<= highLimit
; i
+= increment
) {
285 if (count
% 1000 == 0) {
286 sprintf(buf
, "%.12g", i
);
292 else if (fabs(i
) < 500000)
297 UnicodeString formatResult
;
298 formatter
->format(i
, formatResult
);
299 UErrorCode status
= U_ZERO_ERROR
;
300 Formattable parseResult
;
301 formatter
->parse(formatResult
, parseResult
, status
);
302 if (U_FAILURE(status
)) {
303 sprintf(buf
, "Round-trip status failure: %.12g, status: %d", i
, status
);
307 double rt
= (parseResult
.getType() == Formattable::kDouble
) ?
308 parseResult
.getDouble() :
309 (double)parseResult
.getLong();
312 sprintf(buf
, "Round-trip failed: %.12g -> %.12g", i
, rt
);
324 UnicodeString formatResult
;
325 formatter
->format(d
, formatResult
);
326 UErrorCode status
= U_ZERO_ERROR
;
327 Formattable parseResult
;
328 formatter
->parse(formatResult
, parseResult
, status
);
329 if (U_FAILURE(status
)) {
330 sprintf(buf
, "Round-trip status failure: %.12g, status: %d", d
, status
);
334 double rt
= (parseResult
.getType() == Formattable::kDouble
) ?
335 parseResult
.getDouble() :
336 (double)parseResult
.getLong();
340 sprintf(buf
, "Round-trip failed: %.12g -> ", d
);
342 msg
.append(formatResult
);
343 sprintf(buf
, " -> %.12g", rt
);
359 RbnfRoundTripTest::TestRBNFDisabled() {
360 errln("*** RBNF currently disabled on this platform ***\n");
366 #endif /* #if !UCONFIG_NO_FORMATTING */