]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
3 | * Copyright (C) 1996-2000, International Business Machines Corporation and * | |
4 | * others. All Rights Reserved. * | |
5 | ******************************************************************************* | |
6 | */ | |
7 | ||
8 | #include "unicode/utypes.h" | |
9 | ||
10 | #if !UCONFIG_NO_FORMATTING | |
11 | ||
12 | #include "itrbnfrt.h" | |
13 | ||
14 | #include "unicode/fmtable.h" | |
15 | #include "math.h" // fabs | |
16 | ||
17 | // current macro not in icu1.8.1 | |
18 | #define TESTCASE(id,test) \ | |
19 | case id: \ | |
20 | name = #test; \ | |
21 | if (exec) { \ | |
22 | logln(#test "---"); \ | |
23 | logln((UnicodeString)""); \ | |
24 | test(); \ | |
25 | } \ | |
26 | break | |
27 | ||
28 | void RbnfRoundTripTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) | |
29 | { | |
30 | if (exec) logln("TestSuite RuleBasedNumberFormatRT"); | |
31 | switch (index) { | |
32 | #if U_HAVE_RBNF | |
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); | |
45 | #else | |
46 | TESTCASE(0, TestRBNFDisabled); | |
47 | #endif | |
48 | default: | |
49 | name = ""; | |
50 | break; | |
51 | } | |
52 | } | |
53 | ||
54 | #if U_HAVE_RBNF | |
55 | ||
56 | /** | |
57 | * Perform an exhaustive round-trip test on the English spellout rules | |
58 | */ | |
59 | void | |
60 | RbnfRoundTripTest::TestEnglishSpelloutRT() | |
61 | { | |
62 | UErrorCode status = U_ZERO_ERROR; | |
63 | RuleBasedNumberFormat* formatter | |
64 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status); | |
65 | ||
66 | if (U_FAILURE(status)) { | |
67 | errln("failed to construct formatter"); | |
68 | } else { | |
69 | doTest(formatter, -12345678, 12345678); | |
70 | } | |
71 | delete formatter; | |
72 | } | |
73 | ||
74 | /** | |
75 | * Perform an exhaustive round-trip test on the duration-formatting rules | |
76 | */ | |
77 | void | |
78 | RbnfRoundTripTest::TestDurationsRT() | |
79 | { | |
80 | UErrorCode status = U_ZERO_ERROR; | |
81 | RuleBasedNumberFormat* formatter | |
82 | = new RuleBasedNumberFormat(URBNF_DURATION, Locale::getUS(), status); | |
83 | ||
84 | if (U_FAILURE(status)) { | |
85 | errln("failed to construct formatter"); | |
86 | } else { | |
87 | doTest(formatter, 0, 12345678); | |
88 | } | |
89 | delete formatter; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Perform an exhaustive round-trip test on the Spanish spellout rules | |
94 | */ | |
95 | void | |
96 | RbnfRoundTripTest::TestSpanishSpelloutRT() | |
97 | { | |
98 | UErrorCode status = U_ZERO_ERROR; | |
99 | RuleBasedNumberFormat* formatter | |
100 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("es", "es"), status); | |
101 | ||
102 | if (U_FAILURE(status)) { | |
103 | errln("failed to construct formatter"); | |
104 | } else { | |
105 | doTest(formatter, -12345678, 12345678); | |
106 | } | |
107 | delete formatter; | |
108 | } | |
109 | ||
110 | /** | |
111 | * Perform an exhaustive round-trip test on the French spellout rules | |
112 | */ | |
113 | void | |
114 | RbnfRoundTripTest::TestFrenchSpelloutRT() | |
115 | { | |
116 | UErrorCode status = U_ZERO_ERROR; | |
117 | RuleBasedNumberFormat* formatter | |
118 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getFrance(), status); | |
119 | ||
120 | if (U_FAILURE(status)) { | |
121 | errln("failed to construct formatter"); | |
122 | } else { | |
123 | doTest(formatter, -12345678, 12345678); | |
124 | } | |
125 | delete formatter; | |
126 | } | |
127 | ||
128 | /** | |
129 | * Perform an exhaustive round-trip test on the Swiss French spellout rules | |
130 | */ | |
131 | void | |
132 | RbnfRoundTripTest::TestSwissFrenchSpelloutRT() | |
133 | { | |
134 | UErrorCode status = U_ZERO_ERROR; | |
135 | RuleBasedNumberFormat* formatter | |
136 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "CH"), status); | |
137 | ||
138 | if (U_FAILURE(status)) { | |
139 | errln("failed to construct formatter"); | |
140 | } else { | |
141 | doTest(formatter, -12345678, 12345678); | |
142 | } | |
143 | delete formatter; | |
144 | } | |
145 | ||
146 | /** | |
147 | * Perform an exhaustive round-trip test on the Italian spellout rules | |
148 | */ | |
149 | void | |
150 | RbnfRoundTripTest::TestItalianSpelloutRT() | |
151 | { | |
152 | UErrorCode status = U_ZERO_ERROR; | |
153 | RuleBasedNumberFormat* formatter | |
154 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getItalian(), status); | |
155 | ||
156 | if (U_FAILURE(status)) { | |
157 | errln("failed to construct formatter"); | |
158 | } else { | |
159 | doTest(formatter, -999999, 999999); | |
160 | } | |
161 | delete formatter; | |
162 | } | |
163 | ||
164 | /** | |
165 | * Perform an exhaustive round-trip test on the German spellout rules | |
166 | */ | |
167 | void | |
168 | RbnfRoundTripTest::TestGermanSpelloutRT() | |
169 | { | |
170 | UErrorCode status = U_ZERO_ERROR; | |
171 | RuleBasedNumberFormat* formatter | |
172 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getGermany(), status); | |
173 | ||
174 | if (U_FAILURE(status)) { | |
175 | errln("failed to construct formatter"); | |
176 | } else { | |
177 | doTest(formatter, 0, 12345678); | |
178 | } | |
179 | delete formatter; | |
180 | } | |
181 | ||
182 | /** | |
183 | * Perform an exhaustive round-trip test on the Swedish spellout rules | |
184 | */ | |
185 | void | |
186 | RbnfRoundTripTest::TestSwedishSpelloutRT() | |
187 | { | |
188 | UErrorCode status = U_ZERO_ERROR; | |
189 | RuleBasedNumberFormat* formatter | |
190 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("sv", "SE"), status); | |
191 | ||
192 | if (U_FAILURE(status)) { | |
193 | errln("failed to construct formatter"); | |
194 | } else { | |
195 | doTest(formatter, 0, 12345678); | |
196 | } | |
197 | delete formatter; | |
198 | } | |
199 | ||
200 | /** | |
201 | * Perform an exhaustive round-trip test on the Dutch spellout rules | |
202 | */ | |
203 | void | |
204 | RbnfRoundTripTest::TestDutchSpelloutRT() | |
205 | { | |
206 | UErrorCode status = U_ZERO_ERROR; | |
207 | RuleBasedNumberFormat* formatter | |
208 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("nl", "NL"), status); | |
209 | ||
210 | if (U_FAILURE(status)) { | |
211 | errln("failed to construct formatter"); | |
212 | } else { | |
213 | doTest(formatter, -12345678, 12345678); | |
214 | } | |
215 | delete formatter; | |
216 | } | |
217 | ||
218 | /** | |
219 | * Perform an exhaustive round-trip test on the Japanese spellout rules | |
220 | */ | |
221 | void | |
222 | RbnfRoundTripTest::TestJapaneseSpelloutRT() | |
223 | { | |
224 | UErrorCode status = U_ZERO_ERROR; | |
225 | RuleBasedNumberFormat* formatter | |
226 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getJapan(), status); | |
227 | ||
228 | if (U_FAILURE(status)) { | |
229 | errln("failed to construct formatter"); | |
230 | } else { | |
231 | doTest(formatter, 0, 12345678); | |
232 | } | |
233 | delete formatter; | |
234 | } | |
235 | ||
236 | /** | |
237 | * Perform an exhaustive round-trip test on the Russian spellout rules | |
238 | */ | |
239 | void | |
240 | RbnfRoundTripTest::TestRussianSpelloutRT() | |
241 | { | |
242 | UErrorCode status = U_ZERO_ERROR; | |
243 | RuleBasedNumberFormat* formatter | |
244 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("ru", "RU"), status); | |
245 | ||
246 | if (U_FAILURE(status)) { | |
247 | errln("failed to construct formatter"); | |
248 | } else { | |
249 | doTest(formatter, 0, 12345678); | |
250 | } | |
251 | delete formatter; | |
252 | } | |
253 | ||
254 | /** | |
255 | * Perform an exhaustive round-trip test on the Greek spellout rules | |
256 | */ | |
257 | void | |
258 | RbnfRoundTripTest::TestGreekSpelloutRT() | |
259 | { | |
260 | UErrorCode status = U_ZERO_ERROR; | |
261 | RuleBasedNumberFormat* formatter | |
262 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("el", "GR"), status); | |
263 | ||
264 | if (U_FAILURE(status)) { | |
265 | errln("failed to construct formatter"); | |
266 | } else { | |
267 | doTest(formatter, 0, 12345678); | |
268 | } | |
269 | delete formatter; | |
270 | } | |
271 | ||
272 | void | |
273 | RbnfRoundTripTest::doTest(const RuleBasedNumberFormat* formatter, | |
274 | double lowLimit, | |
275 | double highLimit) | |
276 | { | |
277 | char buf[128]; | |
278 | ||
279 | uint32_t count = 0; | |
280 | double increment = 1; | |
281 | for (double i = lowLimit; i <= highLimit; i += increment) { | |
282 | if (count % 1000 == 0) { | |
283 | sprintf(buf, "%.12g", i); | |
284 | logln(buf); | |
285 | } | |
286 | ||
287 | if (fabs(i) < 5000) | |
288 | increment = 1; | |
289 | else if (fabs(i) < 500000) | |
290 | increment = 2737; | |
291 | else | |
292 | increment = 267437; | |
293 | ||
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); | |
301 | errln(buf); | |
302 | return; | |
303 | } else { | |
304 | double rt = (parseResult.getType() == Formattable::kDouble) ? | |
305 | parseResult.getDouble() : | |
306 | (double)parseResult.getLong(); | |
307 | ||
308 | if (rt != i) { | |
309 | sprintf(buf, "Round-trip failed: %.12g -> %.12g", i, rt); | |
310 | errln(buf); | |
311 | return; | |
312 | } | |
313 | } | |
314 | ||
315 | ++count; | |
316 | } | |
317 | ||
318 | if (lowLimit < 0) { | |
319 | double d = 1.234; | |
320 | while (d < 1000) { | |
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); | |
328 | errln(buf); | |
329 | return; | |
330 | } else { | |
331 | double rt = (parseResult.getType() == Formattable::kDouble) ? | |
332 | parseResult.getDouble() : | |
333 | (double)parseResult.getLong(); | |
334 | ||
335 | if (rt != d) { | |
336 | UnicodeString msg; | |
337 | sprintf(buf, "Round-trip failed: %.12g -> ", d); | |
338 | msg.append(buf); | |
339 | msg.append(formatResult); | |
340 | sprintf(buf, " -> %.12g", rt); | |
341 | msg.append(buf); | |
342 | errln(msg); | |
343 | return; | |
344 | } | |
345 | } | |
346 | ||
347 | d *= 10; | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
352 | /* U_HAVE_RBNF */ | |
353 | #else | |
354 | ||
355 | void | |
356 | RbnfRoundTripTest::TestRBNFDisabled() { | |
357 | errln("*** RBNF currently disabled on this platform ***\n"); | |
358 | } | |
359 | ||
360 | /* U_HAVE_RBNF */ | |
361 | #endif | |
362 | ||
363 | #endif /* #if !UCONFIG_NO_FORMATTING */ |