]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
374ca955 | 3 | * Copyright (C) 1996-2004, International Business Machines Corporation and * |
b75a7d8f A |
4 | * others. All Rights Reserved. * |
5 | ******************************************************************************* | |
6 | */ | |
7 | ||
8 | #include "unicode/utypes.h" | |
9 | ||
10 | #if !UCONFIG_NO_FORMATTING | |
11 | ||
12 | #include "itrbnf.h" | |
13 | ||
14 | #include "unicode/umachine.h" | |
15 | ||
16 | #include "unicode/tblcoll.h" | |
17 | #include "unicode/coleitr.h" | |
18 | #include "unicode/ures.h" | |
19 | #include "unicode/ustring.h" | |
374ca955 A |
20 | #include "unicode/decimfmt.h" |
21 | #include "unicode/udata.h" | |
b75a7d8f A |
22 | //#include "llong.h" |
23 | ||
24 | #include <string.h> | |
25 | ||
26 | // import com.ibm.text.RuleBasedNumberFormat; | |
27 | // import com.ibm.test.TestFmwk; | |
28 | ||
29 | // import java.util.Locale; | |
30 | // import java.text.NumberFormat; | |
31 | ||
32 | // current macro not in icu1.8.1 | |
33 | #define TESTCASE(id,test) \ | |
34 | case id: \ | |
35 | name = #test; \ | |
36 | if (exec) { \ | |
37 | logln(#test "---"); \ | |
38 | logln((UnicodeString)""); \ | |
39 | test(); \ | |
40 | } \ | |
41 | break | |
42 | ||
43 | void IntlTestRBNF::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) | |
44 | { | |
45 | if (exec) logln("TestSuite RuleBasedNumberFormat"); | |
46 | switch (index) { | |
47 | #if U_HAVE_RBNF | |
374ca955 A |
48 | TESTCASE(0, TestEnglishSpellout); |
49 | TESTCASE(1, TestOrdinalAbbreviations); | |
50 | TESTCASE(2, TestDurations); | |
51 | TESTCASE(3, TestSpanishSpellout); | |
52 | TESTCASE(4, TestFrenchSpellout); | |
53 | TESTCASE(5, TestSwissFrenchSpellout); | |
54 | TESTCASE(6, TestItalianSpellout); | |
55 | TESTCASE(7, TestGermanSpellout); | |
56 | TESTCASE(8, TestThaiSpellout); | |
57 | TESTCASE(9, TestAPI); | |
58 | TESTCASE(10, TestFractionalRuleSet); | |
59 | TESTCASE(11, TestSwedishSpellout); | |
60 | TESTCASE(12, TestBelgianFrenchSpellout); | |
61 | TESTCASE(13, TestSmallValues); | |
62 | TESTCASE(14, TestLocalizations); | |
b75a7d8f | 63 | #else |
374ca955 | 64 | TESTCASE(0, TestRBNFDisabled); |
b75a7d8f A |
65 | #endif |
66 | default: | |
374ca955 A |
67 | name = ""; |
68 | break; | |
b75a7d8f A |
69 | } |
70 | } | |
71 | ||
72 | #if U_HAVE_RBNF | |
73 | ||
74 | void | |
75 | IntlTestRBNF::TestAPI() { | |
76 | // This test goes through the APIs that were not tested before. | |
77 | // These tests are too small to have separate test classes/functions | |
78 | ||
79 | UErrorCode status = U_ZERO_ERROR; | |
80 | RuleBasedNumberFormat* formatter | |
81 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status); | |
82 | ||
83 | logln("RBNF API test starting"); | |
84 | // test clone | |
85 | { | |
86 | logln("Testing Clone"); | |
87 | RuleBasedNumberFormat* rbnfClone = (RuleBasedNumberFormat *)formatter->clone(); | |
88 | if(rbnfClone != NULL) { | |
89 | if(!(*rbnfClone == *formatter)) { | |
90 | errln("Clone should be semantically equivalent to the original!"); | |
91 | } | |
92 | delete rbnfClone; | |
93 | } else { | |
94 | errln("Cloning failed!"); | |
95 | } | |
96 | } | |
97 | ||
98 | // test assignment | |
99 | { | |
100 | logln("Testing assignment operator"); | |
101 | RuleBasedNumberFormat assignResult(URBNF_SPELLOUT, Locale("es", "ES", ""), status); | |
102 | assignResult = *formatter; | |
103 | if(!(assignResult == *formatter)) { | |
104 | errln("Assignment result should be semantically equivalent to the original!"); | |
105 | } | |
106 | } | |
107 | ||
108 | // test rule constructor | |
109 | { | |
110 | logln("Testing rule constructor"); | |
374ca955 | 111 | UResourceBundle *en = ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "rbnf", "en", &status); |
b75a7d8f A |
112 | if(U_FAILURE(status)) { |
113 | errln("Unable to access resource bundle with data!"); | |
114 | } else { | |
115 | int32_t ruleLen = 0; | |
116 | const UChar *spelloutRules = ures_getStringByKey(en, "SpelloutRules", &ruleLen, &status); | |
117 | if(U_FAILURE(status) || ruleLen == 0 || spelloutRules == NULL) { | |
118 | errln("Unable to access the rules string!"); | |
119 | } else { | |
120 | UParseError perror; | |
121 | RuleBasedNumberFormat ruleCtorResult(spelloutRules, Locale::getUS(), perror, status); | |
122 | if(!(ruleCtorResult == *formatter)) { | |
123 | errln("Formatter constructed from the original rules should be semantically equivalent to the original!"); | |
124 | } | |
125 | } | |
126 | ures_close(en); | |
127 | } | |
128 | } | |
129 | ||
130 | // test getRules | |
131 | { | |
132 | logln("Testing getRules function"); | |
133 | UnicodeString rules = formatter->getRules(); | |
134 | UParseError perror; | |
135 | RuleBasedNumberFormat fromRulesResult(rules, Locale::getUS(), perror, status); | |
136 | ||
137 | if(!(fromRulesResult == *formatter)) { | |
138 | errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!"); | |
139 | } | |
140 | } | |
141 | ||
142 | ||
143 | { | |
144 | logln("Testing copy constructor"); | |
145 | RuleBasedNumberFormat copyCtorResult(*formatter); | |
146 | if(!(copyCtorResult == *formatter)) { | |
147 | errln("Copy constructor result result should be semantically equivalent to the original!"); | |
148 | } | |
149 | } | |
150 | ||
151 | #if !UCONFIG_NO_COLLATION | |
152 | // test ruleset names | |
153 | { | |
154 | logln("Testing getNumberOfRuleSetNames, getRuleSetName and format using rule set names"); | |
155 | int32_t noOfRuleSetNames = formatter->getNumberOfRuleSetNames(); | |
156 | if(noOfRuleSetNames == 0) { | |
157 | errln("Number of rule set names should be more than zero"); | |
158 | } | |
159 | UnicodeString ruleSetName; | |
160 | int32_t i = 0; | |
161 | int32_t intFormatNum = 34567; | |
162 | double doubleFormatNum = 893411.234; | |
163 | logln("number of rule set names is %i", noOfRuleSetNames); | |
164 | for(i = 0; i < noOfRuleSetNames; i++) { | |
165 | FieldPosition pos1, pos2; | |
166 | UnicodeString intFormatResult, doubleFormatResult; | |
167 | Formattable intParseResult, doubleParseResult; | |
168 | ||
169 | ruleSetName = formatter->getRuleSetName(i); | |
170 | log("Rule set name %i is ", i); | |
171 | log(ruleSetName); | |
172 | logln(". Format results are: "); | |
173 | intFormatResult = formatter->format(intFormatNum, ruleSetName, intFormatResult, pos1, status); | |
174 | doubleFormatResult = formatter->format(doubleFormatNum, ruleSetName, doubleFormatResult, pos2, status); | |
175 | if(U_FAILURE(status)) { | |
176 | errln("Format using a rule set failed"); | |
177 | break; | |
178 | } | |
179 | logln(intFormatResult); | |
180 | logln(doubleFormatResult); | |
181 | formatter->setLenient(TRUE); | |
182 | formatter->parse(intFormatResult, intParseResult, status); | |
183 | formatter->parse(doubleFormatResult, doubleParseResult, status); | |
184 | ||
185 | logln("Parse results for lenient = TRUE, %i, %f", intParseResult.getLong(), doubleParseResult.getDouble()); | |
186 | ||
187 | formatter->setLenient(FALSE); | |
188 | formatter->parse(intFormatResult, intParseResult, status); | |
189 | formatter->parse(doubleFormatResult, doubleParseResult, status); | |
190 | ||
191 | logln("Parse results for lenient = FALSE, %i, %f", intParseResult.getLong(), doubleParseResult.getDouble()); | |
192 | ||
193 | if(U_FAILURE(status)) { | |
194 | errln("Error during parsing"); | |
195 | } | |
196 | ||
197 | intFormatResult = formatter->format(intFormatNum, "BLABLA", intFormatResult, pos1, status); | |
198 | if(U_SUCCESS(status)) { | |
199 | errln("Using invalid rule set name should have failed"); | |
200 | break; | |
201 | } | |
202 | status = U_ZERO_ERROR; | |
203 | doubleFormatResult = formatter->format(doubleFormatNum, "TRUC", doubleFormatResult, pos2, status); | |
204 | if(U_SUCCESS(status)) { | |
205 | errln("Using invalid rule set name should have failed"); | |
206 | break; | |
207 | } | |
208 | status = U_ZERO_ERROR; | |
209 | } | |
210 | status = U_ZERO_ERROR; | |
211 | } | |
212 | #endif | |
213 | ||
214 | // test API | |
215 | UnicodeString expected("four point five",""); | |
216 | logln("Testing format(double)"); | |
217 | UnicodeString result; | |
218 | formatter->format(4.5,result); | |
219 | if(result != expected) { | |
220 | errln("Formatted 4.5, expected " + expected + " got " + result); | |
221 | } else { | |
222 | logln("Formatted 4.5, expected " + expected + " got " + result); | |
223 | } | |
224 | result.remove(); | |
225 | expected = "four"; | |
226 | formatter->format((int32_t)4,result); | |
227 | if(result != expected) { | |
228 | errln("Formatted 4, expected " + expected + " got " + result); | |
229 | } else { | |
230 | logln("Formatted 4, expected " + expected + " got " + result); | |
231 | } | |
232 | ||
233 | ||
234 | // clean up | |
235 | logln("Cleaning up"); | |
236 | delete formatter; | |
237 | } | |
238 | ||
239 | void IntlTestRBNF::TestFractionalRuleSet() | |
240 | { | |
241 | UnicodeString fracRules( | |
242 | "%main:\n" | |
243 | // this rule formats the number if it's 1 or more. It formats | |
244 | // the integral part using a DecimalFormat ("#,##0" puts | |
245 | // thousands separators in the right places) and the fractional | |
246 | // part using %%frac. If there is no fractional part, it | |
247 | // just shows the integral part. | |
248 | " x.0: <#,##0<[ >%%frac>];\n" | |
249 | // this rule formats the number if it's between 0 and 1. It | |
250 | // shows only the fractional part (0.5 shows up as "1/2," not | |
251 | // "0 1/2") | |
252 | " 0.x: >%%frac>;\n" | |
253 | // the fraction rule set. This works the same way as the one in the | |
254 | // preceding example: We multiply the fractional part of the number | |
255 | // being formatted by each rule's base value and use the rule that | |
256 | // produces the result closest to 0 (or the first rule that produces 0). | |
257 | // Since we only provide rules for the numbers from 2 to 10, we know | |
258 | // we'll get a fraction with a denominator between 2 and 10. | |
259 | // "<0<" causes the numerator of the fraction to be formatted | |
260 | // using numerals | |
261 | "%%frac:\n" | |
262 | " 2: 1/2;\n" | |
263 | " 3: <0</3;\n" | |
264 | " 4: <0</4;\n" | |
265 | " 5: <0</5;\n" | |
266 | " 6: <0</6;\n" | |
267 | " 7: <0</7;\n" | |
268 | " 8: <0</8;\n" | |
269 | " 9: <0</9;\n" | |
270 | " 10: <0</10;\n"); | |
271 | ||
272 | // mondo hack | |
273 | int len = fracRules.length(); | |
274 | int change = 2; | |
275 | for (int i = 0; i < len; ++i) { | |
276 | UChar ch = fracRules.charAt(i); | |
277 | if (ch == '\n') { | |
278 | change = 2; // change ok | |
279 | } else if (ch == ':') { | |
280 | change = 1; // change, but once we hit a non-space char, don't change | |
281 | } else if (ch == ' ') { | |
282 | if (change != 0) { | |
283 | fracRules.setCharAt(i, (UChar)0x200e); | |
284 | } | |
285 | } else { | |
286 | if (change == 1) { | |
287 | change = 0; | |
288 | } | |
289 | } | |
290 | } | |
291 | ||
292 | UErrorCode status = U_ZERO_ERROR; | |
293 | UParseError perror; | |
294 | RuleBasedNumberFormat formatter(fracRules, Locale::getEnglish(), perror, status); | |
295 | if (U_FAILURE(status)) { | |
296 | errln("FAIL: could not construct formatter"); | |
297 | } else { | |
298 | static const char* testData[][2] = { | |
299 | { "0", "0" }, | |
300 | { ".1", "1/10" }, | |
301 | { ".11", "1/9" }, | |
302 | { ".125", "1/8" }, | |
303 | { ".1428", "1/7" }, | |
304 | { ".1667", "1/6" }, | |
305 | { ".2", "1/5" }, | |
306 | { ".25", "1/4" }, | |
307 | { ".333", "1/3" }, | |
308 | { ".5", "1/2" }, | |
309 | { "1.1", "1 1/10" }, | |
310 | { "2.11", "2 1/9" }, | |
311 | { "3.125", "3 1/8" }, | |
312 | { "4.1428", "4 1/7" }, | |
313 | { "5.1667", "5 1/6" }, | |
314 | { "6.2", "6 1/5" }, | |
315 | { "7.25", "7 1/4" }, | |
316 | { "8.333", "8 1/3" }, | |
317 | { "9.5", "9 1/2" }, | |
318 | { ".2222", "2/9" }, | |
319 | { ".4444", "4/9" }, | |
320 | { ".5555", "5/9" }, | |
321 | { "1.2856", "1 2/7" }, | |
322 | { NULL, NULL } | |
323 | }; | |
324 | doTest(&formatter, testData, FALSE); // exact values aren't parsable from fractions | |
325 | } | |
326 | } | |
327 | ||
328 | #if 0 | |
329 | #define LLAssert(a) \ | |
330 | if (!(a)) errln("FAIL: " #a) | |
331 | ||
332 | void IntlTestRBNF::TestLLongConstructors() | |
333 | { | |
334 | logln("Testing constructors"); | |
335 | ||
336 | // constant (shouldn't really be public) | |
337 | LLAssert(llong(llong::kD32).asDouble() == llong::kD32); | |
338 | ||
339 | // internal constructor (shouldn't really be public) | |
340 | LLAssert(llong(0, 1).asDouble() == 1); | |
341 | LLAssert(llong(1, 0).asDouble() == llong::kD32); | |
342 | LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1); | |
343 | ||
344 | // public empty constructor | |
345 | LLAssert(llong().asDouble() == 0); | |
346 | ||
347 | // public int32_t constructor | |
348 | LLAssert(llong((int32_t)0).asInt() == (int32_t)0); | |
349 | LLAssert(llong((int32_t)1).asInt() == (int32_t)1); | |
350 | LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1); | |
351 | LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff); | |
352 | LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1); | |
353 | LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000); | |
354 | ||
355 | // public int16_t constructor | |
356 | LLAssert(llong((int16_t)0).asInt() == (int16_t)0); | |
357 | LLAssert(llong((int16_t)1).asInt() == (int16_t)1); | |
358 | LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1); | |
359 | LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff); | |
360 | LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff); | |
361 | LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000); | |
362 | ||
363 | // public int8_t constructor | |
364 | LLAssert(llong((int8_t)0).asInt() == (int8_t)0); | |
365 | LLAssert(llong((int8_t)1).asInt() == (int8_t)1); | |
366 | LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1); | |
367 | LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f); | |
368 | LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff); | |
369 | LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80); | |
370 | ||
371 | // public uint16_t constructor | |
372 | LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0); | |
373 | LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1); | |
374 | LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1); | |
375 | LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff); | |
376 | LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff); | |
377 | LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000); | |
378 | ||
379 | // public uint32_t constructor | |
380 | LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0); | |
381 | LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1); | |
382 | LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1); | |
383 | LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff); | |
384 | LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1); | |
385 | LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000); | |
386 | ||
387 | // public double constructor | |
388 | LLAssert(llong((double)0).asDouble() == (double)0); | |
389 | LLAssert(llong((double)1).asDouble() == (double)1); | |
390 | LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff); | |
391 | LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000); | |
392 | LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001); | |
393 | ||
394 | // can't access uprv_maxmantissa, so fake it | |
395 | double maxmantissa = (llong((int32_t)1) << 40).asDouble(); | |
396 | LLAssert(llong(maxmantissa).asDouble() == maxmantissa); | |
397 | LLAssert(llong(-maxmantissa).asDouble() == -maxmantissa); | |
398 | ||
399 | // copy constructor | |
400 | LLAssert(llong(llong(0, 1)).asDouble() == 1); | |
401 | LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32); | |
402 | LLAssert(llong(llong(-1, (uint32_t)-1)).asDouble() == -1); | |
403 | ||
404 | // asInt - test unsigned to signed narrowing conversion | |
405 | LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff); | |
406 | LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000); | |
407 | ||
408 | // asUInt - test signed to unsigned narrowing conversion | |
409 | LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1); | |
410 | LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000); | |
411 | ||
412 | // asDouble already tested | |
413 | ||
414 | } | |
415 | ||
416 | void IntlTestRBNF::TestLLongSimpleOperators() | |
417 | { | |
418 | logln("Testing simple operators"); | |
419 | ||
420 | // operator== | |
421 | LLAssert(llong() == llong(0, 0)); | |
422 | LLAssert(llong(1,0) == llong(1, 0)); | |
423 | LLAssert(llong(0,1) == llong(0, 1)); | |
424 | ||
425 | // operator!= | |
426 | LLAssert(llong(1,0) != llong(1,1)); | |
427 | LLAssert(llong(0,1) != llong(1,1)); | |
428 | LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff)); | |
429 | ||
430 | // unsigned > | |
431 | LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff))); | |
432 | ||
433 | // unsigned < | |
434 | LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1))); | |
435 | ||
436 | // unsigned >= | |
437 | LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff))); | |
438 | LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1))); | |
439 | ||
440 | // unsigned <= | |
441 | LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1))); | |
442 | LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1))); | |
443 | ||
444 | // operator> | |
445 | LLAssert(llong(1, 1) > llong(1, 0)); | |
446 | LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff)); | |
447 | LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0)); | |
448 | LLAssert(llong(1, 0) > llong(0, 0x7fffffff)); | |
449 | LLAssert(llong(1, 0) > llong(0, 0xffffffff)); | |
450 | LLAssert(llong(0, 0) > llong(0x80000000, 1)); | |
451 | ||
452 | // operator< | |
453 | LLAssert(llong(1, 0) < llong(1, 1)); | |
454 | LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000)); | |
455 | LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1)); | |
456 | LLAssert(llong(0, 0x7fffffff) < llong(1, 0)); | |
457 | LLAssert(llong(0, 0xffffffff) < llong(1, 0)); | |
458 | LLAssert(llong(0x80000000, 1) < llong(0, 0)); | |
459 | ||
460 | // operator>= | |
461 | LLAssert(llong(1, 1) >= llong(1, 0)); | |
462 | LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff)); | |
463 | LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0)); | |
464 | LLAssert(llong(1, 0) >= llong(0, 0x7fffffff)); | |
465 | LLAssert(llong(1, 0) >= llong(0, 0xffffffff)); | |
466 | LLAssert(llong(0, 0) >= llong(0x80000000, 1)); | |
467 | LLAssert(llong() >= llong(0, 0)); | |
468 | LLAssert(llong(1,0) >= llong(1, 0)); | |
469 | LLAssert(llong(0,1) >= llong(0, 1)); | |
470 | ||
471 | // operator<= | |
472 | LLAssert(llong(1, 0) <= llong(1, 1)); | |
473 | LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000)); | |
474 | LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1)); | |
475 | LLAssert(llong(0, 0x7fffffff) <= llong(1, 0)); | |
476 | LLAssert(llong(0, 0xffffffff) <= llong(1, 0)); | |
477 | LLAssert(llong(0x80000000, 1) <= llong(0, 0)); | |
478 | LLAssert(llong() <= llong(0, 0)); | |
479 | LLAssert(llong(1,0) <= llong(1, 0)); | |
480 | LLAssert(llong(0,1) <= llong(0, 1)); | |
481 | ||
482 | // operator==(int32) | |
483 | LLAssert(llong() == (int32_t)0); | |
484 | LLAssert(llong(0,1) == (int32_t)1); | |
485 | ||
486 | // operator!=(int32) | |
487 | LLAssert(llong(1,0) != (int32_t)0); | |
488 | LLAssert(llong(0,1) != (int32_t)2); | |
489 | LLAssert(llong(0,0xffffffff) != (int32_t)-1); | |
490 | ||
491 | llong negOne(0xffffffff, 0xffffffff); | |
492 | ||
493 | // operator>(int32) | |
494 | LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff); | |
495 | LLAssert(negOne > (int32_t)-2); | |
496 | LLAssert(llong(1, 0) > (int32_t)0x7fffffff); | |
497 | LLAssert(llong(0, 0) > (int32_t)-1); | |
498 | ||
499 | // operator<(int32) | |
500 | LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff); | |
501 | LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1); | |
502 | ||
503 | // operator>=(int32) | |
504 | LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff); | |
505 | LLAssert(negOne >= (int32_t)-2); | |
506 | LLAssert(llong(1, 0) >= (int32_t)0x7fffffff); | |
507 | LLAssert(llong(0, 0) >= (int32_t)-1); | |
508 | LLAssert(llong() >= (int32_t)0); | |
509 | LLAssert(llong(0,1) >= (int32_t)1); | |
510 | ||
511 | // operator<=(int32) | |
512 | LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff); | |
513 | LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1); | |
514 | LLAssert(llong() <= (int32_t)0); | |
515 | LLAssert(llong(0,1) <= (int32_t)1); | |
516 | ||
517 | // operator= | |
518 | LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1); | |
519 | ||
520 | // operator <<= | |
521 | LLAssert((llong(1, 1) <<= 0) == llong(1, 1)); | |
522 | LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000)); | |
523 | LLAssert((llong(1, 1) <<= 32) == llong(1, 0)); | |
524 | LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0)); | |
525 | LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used | |
526 | LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used | |
527 | ||
528 | // operator << | |
529 | LLAssert((llong((int32_t)1) << 5).asUInt() == 32); | |
530 | ||
531 | // operator >>= (sign extended) | |
532 | LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc)); | |
533 | LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde)); | |
534 | LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff)); | |
535 | LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000)); | |
536 | LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used | |
537 | LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used | |
538 | ||
539 | // operator >> sign extended) | |
540 | LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde)); | |
541 | ||
542 | // ushr (right shift without sign extension) | |
543 | LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc)); | |
544 | LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde)); | |
545 | LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1)); | |
546 | LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000)); | |
547 | LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used | |
548 | LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used | |
549 | ||
550 | // operator&(llong) | |
551 | LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000)); | |
552 | ||
553 | // operator|(llong) | |
554 | LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff)); | |
555 | ||
556 | // operator^(llong) | |
557 | LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff)); | |
558 | ||
559 | // operator&(uint32) | |
560 | LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000)); | |
561 | ||
562 | // operator|(uint32) | |
563 | LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff)); | |
564 | ||
565 | // operator^(uint32) | |
566 | LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff)); | |
567 | ||
568 | // operator~ | |
569 | LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa)); | |
570 | ||
571 | // operator&=(llong) | |
572 | LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000)); | |
573 | ||
574 | // operator|=(llong) | |
575 | LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff)); | |
576 | ||
577 | // operator^=(llong) | |
578 | LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff)); | |
579 | ||
580 | // operator&=(uint32) | |
581 | LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000)); | |
582 | ||
583 | // operator|=(uint32) | |
584 | LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff)); | |
585 | ||
586 | // operator^=(uint32) | |
587 | LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff)); | |
588 | ||
589 | // prefix inc | |
590 | LLAssert(llong(1, 0) == ++llong(0,0xffffffff)); | |
591 | ||
592 | // prefix dec | |
593 | LLAssert(llong(0,0xffffffff) == --llong(1, 0)); | |
594 | ||
595 | // postfix inc | |
596 | { | |
597 | llong n(0, 0xffffffff); | |
598 | LLAssert(llong(0, 0xffffffff) == n++); | |
599 | LLAssert(llong(1, 0) == n); | |
600 | } | |
601 | ||
602 | // postfix dec | |
603 | { | |
604 | llong n(1, 0); | |
605 | LLAssert(llong(1, 0) == n--); | |
606 | LLAssert(llong(0, 0xffffffff) == n); | |
607 | } | |
608 | ||
609 | // unary minus | |
610 | LLAssert(llong(0, 0) == -llong(0, 0)); | |
611 | LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1)); | |
612 | LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff)); | |
613 | LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1)); | |
614 | LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow | |
615 | ||
616 | // operator-= | |
617 | { | |
618 | llong n; | |
619 | LLAssert((n -= llong(0, 1)) == llong(0xffffffff, 0xffffffff)); | |
620 | LLAssert(n == llong(0xffffffff, 0xffffffff)); | |
621 | ||
622 | n = llong(1, 0); | |
623 | LLAssert((n -= llong(0, 1)) == llong(0, 0xffffffff)); | |
624 | LLAssert(n == llong(0, 0xffffffff)); | |
625 | } | |
626 | ||
627 | // operator- | |
628 | { | |
629 | llong n; | |
630 | LLAssert((n - llong(0, 1)) == llong(0xffffffff, 0xffffffff)); | |
631 | LLAssert(n == llong(0, 0)); | |
632 | ||
633 | n = llong(1, 0); | |
634 | LLAssert((n - llong(0, 1)) == llong(0, 0xffffffff)); | |
635 | LLAssert(n == llong(1, 0)); | |
636 | } | |
637 | ||
638 | // operator+= | |
639 | { | |
640 | llong n(0xffffffff, 0xffffffff); | |
641 | LLAssert((n += llong(0, 1)) == llong(0, 0)); | |
642 | LLAssert(n == llong(0, 0)); | |
643 | ||
644 | n = llong(0, 0xffffffff); | |
645 | LLAssert((n += llong(0, 1)) == llong(1, 0)); | |
646 | LLAssert(n == llong(1, 0)); | |
647 | } | |
648 | ||
649 | // operator+ | |
650 | { | |
651 | llong n(0xffffffff, 0xffffffff); | |
652 | LLAssert((n + llong(0, 1)) == llong(0, 0)); | |
653 | LLAssert(n == llong(0xffffffff, 0xffffffff)); | |
654 | ||
655 | n = llong(0, 0xffffffff); | |
656 | LLAssert((n + llong(0, 1)) == llong(1, 0)); | |
657 | LLAssert(n == llong(0, 0xffffffff)); | |
658 | } | |
659 | ||
660 | } | |
661 | ||
662 | void IntlTestRBNF::TestLLong() | |
663 | { | |
664 | logln("Starting TestLLong"); | |
665 | ||
666 | TestLLongConstructors(); | |
667 | ||
668 | TestLLongSimpleOperators(); | |
669 | ||
670 | logln("Testing operator*=, operator*"); | |
671 | ||
672 | // operator*=, operator* | |
673 | // small and large values, positive, &NEGative, zero | |
674 | // also test commutivity | |
675 | { | |
676 | const llong ZERO; | |
677 | const llong ONE(0, 1); | |
678 | const llong NEG_ONE((int32_t)-1); | |
679 | const llong THREE(0, 3); | |
680 | const llong NEG_THREE((int32_t)-3); | |
681 | const llong TWO_TO_16(0, 0x10000); | |
682 | const llong NEG_TWO_TO_16 = -TWO_TO_16; | |
683 | const llong TWO_TO_32(1, 0); | |
684 | const llong NEG_TWO_TO_32 = -TWO_TO_32; | |
685 | ||
686 | const llong NINE(0, 9); | |
687 | const llong NEG_NINE = -NINE; | |
688 | ||
689 | const llong TWO_TO_16X3(0, 0x00030000); | |
690 | const llong NEG_TWO_TO_16X3 = -TWO_TO_16X3; | |
691 | ||
692 | const llong TWO_TO_32X3(3, 0); | |
693 | const llong NEG_TWO_TO_32X3 = -TWO_TO_32X3; | |
694 | ||
695 | const llong TWO_TO_48(0x10000, 0); | |
696 | const llong NEG_TWO_TO_48 = -TWO_TO_48; | |
697 | ||
698 | const int32_t VALUE_WIDTH = 9; | |
699 | const llong* values[VALUE_WIDTH] = { | |
700 | &ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32 | |
701 | }; | |
702 | ||
703 | const llong* answers[VALUE_WIDTH*VALUE_WIDTH] = { | |
704 | &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, | |
705 | &ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32, | |
706 | &ZERO, &NEG_ONE, &ONE, &NEG_THREE, &THREE, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_32, &TWO_TO_32, | |
707 | &ZERO, &THREE, &NEG_THREE, &NINE, &NEG_NINE, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32X3, &NEG_TWO_TO_32X3, | |
708 | &ZERO, &NEG_THREE, &THREE, &NEG_NINE, &NINE, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32X3, &TWO_TO_32X3, | |
709 | &ZERO, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_48, &NEG_TWO_TO_48, | |
710 | &ZERO, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_48, &TWO_TO_48, | |
711 | &ZERO, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_32X3, &NEG_TWO_TO_32X3, &TWO_TO_48, &NEG_TWO_TO_48, &ZERO, &ZERO, | |
712 | &ZERO, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_32X3, &TWO_TO_32X3, &NEG_TWO_TO_48, &TWO_TO_48, &ZERO, &ZERO | |
713 | }; | |
714 | ||
715 | for (int i = 0; i < VALUE_WIDTH; ++i) { | |
716 | for (int j = 0; j < VALUE_WIDTH; ++j) { | |
717 | llong lhs = *values[i]; | |
718 | llong rhs = *values[j]; | |
719 | llong ans = *answers[i*VALUE_WIDTH + j]; | |
720 | ||
721 | llong n = lhs; | |
722 | ||
723 | LLAssert((n *= rhs) == ans); | |
724 | LLAssert(n == ans); | |
725 | ||
726 | n = lhs; | |
727 | LLAssert((n * rhs) == ans); | |
728 | LLAssert(n == lhs); | |
729 | } | |
730 | } | |
731 | } | |
732 | ||
733 | logln("Testing operator/=, operator/"); | |
734 | // operator/=, operator/ | |
735 | // test num = 0, div = 0, pos/neg, > 2^32, div > num | |
736 | { | |
737 | const llong ZERO; | |
738 | const llong ONE(0, 1); | |
739 | const llong NEG_ONE = -ONE; | |
740 | const llong MAX(0x7fffffff, 0xffffffff); | |
741 | const llong MIN(0x80000000, 0); | |
742 | const llong TWO(0, 2); | |
743 | const llong NEG_TWO = -TWO; | |
744 | const llong FIVE(0, 5); | |
745 | const llong NEG_FIVE = -FIVE; | |
746 | const llong TWO_TO_32(1, 0); | |
747 | const llong NEG_TWO_TO_32 = -TWO_TO_32; | |
748 | const llong TWO_TO_32d5 = llong(TWO_TO_32.asDouble()/5.0); | |
749 | const llong NEG_TWO_TO_32d5 = -TWO_TO_32d5; | |
750 | const llong TWO_TO_32X5 = TWO_TO_32 * FIVE; | |
751 | const llong NEG_TWO_TO_32X5 = -TWO_TO_32X5; | |
752 | ||
753 | const llong* tuples[] = { // lhs, rhs, ans | |
754 | &ZERO, &ZERO, &ZERO, | |
755 | &ONE, &ZERO,&MAX, | |
756 | &NEG_ONE, &ZERO, &MIN, | |
757 | &ONE, &ONE, &ONE, | |
758 | &ONE, &NEG_ONE, &NEG_ONE, | |
759 | &NEG_ONE, &ONE, &NEG_ONE, | |
760 | &NEG_ONE, &NEG_ONE, &ONE, | |
761 | &FIVE, &TWO, &TWO, | |
762 | &FIVE, &NEG_TWO, &NEG_TWO, | |
763 | &NEG_FIVE, &TWO, &NEG_TWO, | |
764 | &NEG_FIVE, &NEG_TWO, &TWO, | |
765 | &TWO, &FIVE, &ZERO, | |
766 | &TWO, &NEG_FIVE, &ZERO, | |
767 | &NEG_TWO, &FIVE, &ZERO, | |
768 | &NEG_TWO, &NEG_FIVE, &ZERO, | |
769 | &TWO_TO_32, &TWO_TO_32, &ONE, | |
770 | &TWO_TO_32, &NEG_TWO_TO_32, &NEG_ONE, | |
771 | &NEG_TWO_TO_32, &TWO_TO_32, &NEG_ONE, | |
772 | &NEG_TWO_TO_32, &NEG_TWO_TO_32, &ONE, | |
773 | &TWO_TO_32, &FIVE, &TWO_TO_32d5, | |
774 | &TWO_TO_32, &NEG_FIVE, &NEG_TWO_TO_32d5, | |
775 | &NEG_TWO_TO_32, &FIVE, &NEG_TWO_TO_32d5, | |
776 | &NEG_TWO_TO_32, &NEG_FIVE, &TWO_TO_32d5, | |
777 | &TWO_TO_32X5, &FIVE, &TWO_TO_32, | |
778 | &TWO_TO_32X5, &NEG_FIVE, &NEG_TWO_TO_32, | |
779 | &NEG_TWO_TO_32X5, &FIVE, &NEG_TWO_TO_32, | |
780 | &NEG_TWO_TO_32X5, &NEG_FIVE, &TWO_TO_32, | |
781 | &TWO_TO_32X5, &TWO_TO_32, &FIVE, | |
782 | &TWO_TO_32X5, &NEG_TWO_TO_32, &NEG_FIVE, | |
783 | &NEG_TWO_TO_32X5, &NEG_TWO_TO_32, &FIVE, | |
784 | &NEG_TWO_TO_32X5, &TWO_TO_32, &NEG_FIVE | |
785 | }; | |
786 | const int TUPLE_WIDTH = 3; | |
787 | const int TUPLE_COUNT = (int)(sizeof(tuples)/sizeof(tuples[0]))/TUPLE_WIDTH; | |
788 | for (int i = 0; i < TUPLE_COUNT; ++i) { | |
789 | const llong lhs = *tuples[i*TUPLE_WIDTH+0]; | |
790 | const llong rhs = *tuples[i*TUPLE_WIDTH+1]; | |
791 | const llong ans = *tuples[i*TUPLE_WIDTH+2]; | |
792 | ||
793 | llong n = lhs; | |
794 | if (!((n /= rhs) == ans)) { | |
795 | errln("fail: (n /= rhs) == ans"); | |
796 | } | |
797 | LLAssert(n == ans); | |
798 | ||
799 | n = lhs; | |
800 | LLAssert((n / rhs) == ans); | |
801 | LLAssert(n == lhs); | |
802 | } | |
803 | } | |
804 | ||
805 | logln("Testing operator%%=, operator%%"); | |
806 | //operator%=, operator% | |
807 | { | |
808 | const llong ZERO; | |
809 | const llong ONE(0, 1); | |
810 | const llong TWO(0, 2); | |
811 | const llong THREE(0,3); | |
812 | const llong FOUR(0, 4); | |
813 | const llong FIVE(0, 5); | |
814 | const llong SIX(0, 6); | |
815 | ||
816 | const llong NEG_ONE = -ONE; | |
817 | const llong NEG_TWO = -TWO; | |
818 | const llong NEG_THREE = -THREE; | |
819 | const llong NEG_FOUR = -FOUR; | |
820 | const llong NEG_FIVE = -FIVE; | |
821 | const llong NEG_SIX = -SIX; | |
822 | ||
823 | const llong NINETY_NINE(0, 99); | |
824 | const llong HUNDRED(0, 100); | |
825 | const llong HUNDRED_ONE(0, 101); | |
826 | ||
827 | const llong BIG(0x12345678, 0x9abcdef0); | |
828 | const llong BIG_FIVE(BIG * FIVE); | |
829 | const llong BIG_FIVEm1 = BIG_FIVE - ONE; | |
830 | const llong BIG_FIVEp1 = BIG_FIVE + ONE; | |
831 | ||
832 | const llong* tuples[] = { | |
833 | &ZERO, &FIVE, &ZERO, | |
834 | &ONE, &FIVE, &ONE, | |
835 | &TWO, &FIVE, &TWO, | |
836 | &THREE, &FIVE, &THREE, | |
837 | &FOUR, &FIVE, &FOUR, | |
838 | &FIVE, &FIVE, &ZERO, | |
839 | &SIX, &FIVE, &ONE, | |
840 | &ZERO, &NEG_FIVE, &ZERO, | |
841 | &ONE, &NEG_FIVE, &ONE, | |
842 | &TWO, &NEG_FIVE, &TWO, | |
843 | &THREE, &NEG_FIVE, &THREE, | |
844 | &FOUR, &NEG_FIVE, &FOUR, | |
845 | &FIVE, &NEG_FIVE, &ZERO, | |
846 | &SIX, &NEG_FIVE, &ONE, | |
847 | &NEG_ONE, &FIVE, &NEG_ONE, | |
848 | &NEG_TWO, &FIVE, &NEG_TWO, | |
849 | &NEG_THREE, &FIVE, &NEG_THREE, | |
850 | &NEG_FOUR, &FIVE, &NEG_FOUR, | |
851 | &NEG_FIVE, &FIVE, &ZERO, | |
852 | &NEG_SIX, &FIVE, &NEG_ONE, | |
853 | &NEG_ONE, &NEG_FIVE, &NEG_ONE, | |
854 | &NEG_TWO, &NEG_FIVE, &NEG_TWO, | |
855 | &NEG_THREE, &NEG_FIVE, &NEG_THREE, | |
856 | &NEG_FOUR, &NEG_FIVE, &NEG_FOUR, | |
857 | &NEG_FIVE, &NEG_FIVE, &ZERO, | |
858 | &NEG_SIX, &NEG_FIVE, &NEG_ONE, | |
859 | &NINETY_NINE, &FIVE, &FOUR, | |
860 | &HUNDRED, &FIVE, &ZERO, | |
861 | &HUNDRED_ONE, &FIVE, &ONE, | |
862 | &BIG_FIVEm1, &FIVE, &FOUR, | |
863 | &BIG_FIVE, &FIVE, &ZERO, | |
864 | &BIG_FIVEp1, &FIVE, &ONE | |
865 | }; | |
866 | const int TUPLE_WIDTH = 3; | |
867 | const int TUPLE_COUNT = (int)(sizeof(tuples)/sizeof(tuples[0]))/TUPLE_WIDTH; | |
868 | for (int i = 0; i < TUPLE_COUNT; ++i) { | |
869 | const llong lhs = *tuples[i*TUPLE_WIDTH+0]; | |
870 | const llong rhs = *tuples[i*TUPLE_WIDTH+1]; | |
871 | const llong ans = *tuples[i*TUPLE_WIDTH+2]; | |
872 | ||
873 | llong n = lhs; | |
874 | if (!((n %= rhs) == ans)) { | |
875 | errln("fail: (n %= rhs) == ans"); | |
876 | } | |
877 | LLAssert(n == ans); | |
878 | ||
879 | n = lhs; | |
880 | LLAssert((n % rhs) == ans); | |
881 | LLAssert(n == lhs); | |
882 | } | |
883 | } | |
884 | ||
885 | logln("Testing pow"); | |
886 | // pow | |
887 | LLAssert(llong(0, 0).pow(0) == llong(0, 0)); | |
888 | LLAssert(llong(0, 0).pow(2) == llong(0, 0)); | |
889 | LLAssert(llong(0, 2).pow(0) == llong(0, 1)); | |
890 | LLAssert(llong(0, 2).pow(2) == llong(0, 4)); | |
891 | LLAssert(llong(0, 2).pow(32) == llong(1, 0)); | |
892 | LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5)); | |
893 | ||
894 | // absolute value | |
895 | { | |
896 | const llong n(0xffffffff,0xffffffff); | |
897 | LLAssert(n.abs() == llong(0, 1)); | |
898 | } | |
899 | ||
900 | #ifdef RBNF_DEBUG | |
901 | logln("Testing atoll"); | |
902 | // atoll | |
903 | const char empty[] = ""; | |
904 | const char zero[] = "0"; | |
905 | const char neg_one[] = "-1"; | |
906 | const char neg_12345[] = "-12345"; | |
907 | const char big1[] = "123456789abcdef0"; | |
908 | const char big2[] = "fFfFfFfFfFfFfFfF"; | |
909 | LLAssert(llong::atoll(empty) == llong(0, 0)); | |
910 | LLAssert(llong::atoll(zero) == llong(0, 0)); | |
911 | LLAssert(llong::atoll(neg_one) == llong(0xffffffff, 0xffffffff)); | |
912 | LLAssert(llong::atoll(neg_12345) == -llong(0, 12345)); | |
913 | LLAssert(llong::atoll(big1, 16) == llong(0x12345678, 0x9abcdef0)); | |
914 | LLAssert(llong::atoll(big2, 16) == llong(0xffffffff, 0xffffffff)); | |
915 | #endif | |
916 | ||
917 | // u_atoll | |
918 | const UChar uempty[] = { 0 }; | |
919 | const UChar uzero[] = { 0x30, 0 }; | |
920 | const UChar uneg_one[] = { 0x2d, 0x31, 0 }; | |
921 | const UChar uneg_12345[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 }; | |
922 | const UChar ubig1[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 }; | |
923 | const UChar ubig2[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 }; | |
924 | LLAssert(llong::utoll(uempty) == llong(0, 0)); | |
925 | LLAssert(llong::utoll(uzero) == llong(0, 0)); | |
926 | LLAssert(llong::utoll(uneg_one) == llong(0xffffffff, 0xffffffff)); | |
927 | LLAssert(llong::utoll(uneg_12345) == -llong(0, 12345)); | |
928 | LLAssert(llong::utoll(ubig1, 16) == llong(0x12345678, 0x9abcdef0)); | |
929 | LLAssert(llong::utoll(ubig2, 16) == llong(0xffffffff, 0xffffffff)); | |
930 | ||
931 | #ifdef RBNF_DEBUG | |
932 | logln("Testing lltoa"); | |
933 | // lltoa | |
934 | { | |
935 | char buf[64]; // ascii | |
936 | LLAssert((llong(0, 0).lltoa(buf, (uint32_t)sizeof(buf)) == 1) && (strcmp(buf, zero) == 0)); | |
937 | LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf, (uint32_t)sizeof(buf)) == 2) && (strcmp(buf, neg_one) == 0)); | |
938 | LLAssert(((-llong(0, 12345)).lltoa(buf, (uint32_t)sizeof(buf)) == 6) && (strcmp(buf, neg_12345) == 0)); | |
939 | LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf, (uint32_t)sizeof(buf), 16) == 16) && (strcmp(buf, big1) == 0)); | |
940 | } | |
941 | #endif | |
942 | ||
943 | logln("Testing u_lltoa"); | |
944 | // u_lltoa | |
945 | { | |
946 | UChar buf[64]; | |
947 | LLAssert((llong(0, 0).lltou(buf, (uint32_t)sizeof(buf)) == 1) && (u_strcmp(buf, uzero) == 0)); | |
948 | LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf, (uint32_t)sizeof(buf)) == 2) && (u_strcmp(buf, uneg_one) == 0)); | |
949 | LLAssert(((-llong(0, 12345)).lltou(buf, (uint32_t)sizeof(buf)) == 6) && (u_strcmp(buf, uneg_12345) == 0)); | |
950 | LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf, (uint32_t)sizeof(buf), 16) == 16) && (u_strcmp(buf, ubig1) == 0)); | |
951 | } | |
952 | } | |
953 | ||
954 | /* if 0 */ | |
955 | #endif | |
956 | ||
957 | void | |
958 | IntlTestRBNF::TestEnglishSpellout() | |
959 | { | |
960 | UErrorCode status = U_ZERO_ERROR; | |
961 | RuleBasedNumberFormat* formatter | |
962 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status); | |
963 | ||
964 | if (U_FAILURE(status)) { | |
965 | errln("FAIL: could not construct formatter"); | |
966 | } else { | |
967 | static const char* testData[][2] = { | |
968 | { "1", "one" }, | |
969 | { "2", "two" }, | |
970 | { "15", "fifteen" }, | |
971 | { "20", "twenty" }, | |
972 | { "23", "twenty-three" }, | |
973 | { "73", "seventy-three" }, | |
974 | { "88", "eighty-eight" }, | |
975 | { "100", "one hundred" }, | |
976 | { "106", "one hundred and six" }, | |
977 | { "127", "one hundred and twenty-seven" }, | |
978 | { "200", "two hundred" }, | |
979 | { "579", "five hundred and seventy-nine" }, | |
980 | { "1,000", "one thousand" }, | |
981 | { "2,000", "two thousand" }, | |
982 | { "3,004", "three thousand and four" }, | |
983 | { "4,567", "four thousand five hundred and sixty-seven" }, | |
984 | { "15,943", "fifteen thousand nine hundred and forty-three" }, | |
985 | { "2,345,678", "two million, three hundred and forty-five thousand, six hundred and seventy-eight" }, | |
986 | { "-36", "minus thirty-six" }, | |
987 | { "234.567", "two hundred and thirty-four point five six seven" }, | |
988 | { NULL, NULL} | |
989 | }; | |
990 | ||
991 | doTest(formatter, testData, TRUE); | |
992 | ||
993 | #if !UCONFIG_NO_COLLATION | |
994 | formatter->setLenient(TRUE); | |
995 | static const char* lpTestData[][2] = { | |
996 | { "fifty-7", "57" }, | |
997 | { " fifty-7", "57" }, | |
998 | { " fifty-7", "57" }, | |
999 | { "2 thousand six HUNDRED fifty-7", "2,657" }, | |
1000 | { "fifteen hundred and zero", "1,500" }, | |
1001 | { "FOurhundred thiRTY six", "436" }, | |
1002 | { NULL, NULL} | |
1003 | }; | |
1004 | doLenientParseTest(formatter, lpTestData); | |
1005 | #endif | |
1006 | } | |
1007 | delete formatter; | |
1008 | } | |
1009 | ||
1010 | void | |
1011 | IntlTestRBNF::TestOrdinalAbbreviations() | |
1012 | { | |
1013 | UErrorCode status = U_ZERO_ERROR; | |
1014 | RuleBasedNumberFormat* formatter | |
1015 | = new RuleBasedNumberFormat(URBNF_ORDINAL, Locale::getUS(), status); | |
1016 | ||
1017 | if (U_FAILURE(status)) { | |
1018 | errln("FAIL: could not construct formatter"); | |
1019 | } else { | |
1020 | static const char* testData[][2] = { | |
1021 | { "1", "1st" }, | |
1022 | { "2", "2nd" }, | |
1023 | { "3", "3rd" }, | |
1024 | { "4", "4th" }, | |
1025 | { "7", "7th" }, | |
1026 | { "10", "10th" }, | |
1027 | { "11", "11th" }, | |
1028 | { "13", "13th" }, | |
1029 | { "20", "20th" }, | |
1030 | { "21", "21st" }, | |
1031 | { "22", "22nd" }, | |
1032 | { "23", "23rd" }, | |
1033 | { "24", "24th" }, | |
1034 | { "33", "33rd" }, | |
1035 | { "102", "102nd" }, | |
1036 | { "312", "312th" }, | |
1037 | { "12,345", "12,345th" }, | |
1038 | { NULL, NULL} | |
1039 | }; | |
1040 | ||
1041 | doTest(formatter, testData, FALSE); | |
1042 | } | |
1043 | delete formatter; | |
1044 | } | |
1045 | ||
1046 | void | |
1047 | IntlTestRBNF::TestDurations() | |
1048 | { | |
1049 | UErrorCode status = U_ZERO_ERROR; | |
1050 | RuleBasedNumberFormat* formatter | |
1051 | = new RuleBasedNumberFormat(URBNF_DURATION, Locale::getUS(), status); | |
1052 | ||
1053 | if (U_FAILURE(status)) { | |
1054 | errln("FAIL: could not construct formatter"); | |
1055 | } else { | |
1056 | static const char* testData[][2] = { | |
1057 | { "3,600", "1:00:00" }, //move me and I fail | |
1058 | { "0", "0 sec." }, | |
1059 | { "1", "1 sec." }, | |
1060 | { "24", "24 sec." }, | |
1061 | { "60", "1:00" }, | |
1062 | { "73", "1:13" }, | |
1063 | { "145", "2:25" }, | |
1064 | { "666", "11:06" }, | |
1065 | // { "3,600", "1:00:00" }, | |
1066 | { "3,740", "1:02:20" }, | |
1067 | { "10,293", "2:51:33" }, | |
1068 | { NULL, NULL} | |
1069 | }; | |
1070 | ||
1071 | doTest(formatter, testData, TRUE); | |
1072 | ||
1073 | #if !UCONFIG_NO_COLLATION | |
1074 | formatter->setLenient(TRUE); | |
1075 | static const char* lpTestData[][2] = { | |
1076 | { "2-51-33", "10,293" }, | |
1077 | { NULL, NULL} | |
1078 | }; | |
1079 | doLenientParseTest(formatter, lpTestData); | |
1080 | #endif | |
1081 | } | |
1082 | delete formatter; | |
1083 | } | |
1084 | ||
1085 | void | |
1086 | IntlTestRBNF::TestSpanishSpellout() | |
1087 | { | |
1088 | UErrorCode status = U_ZERO_ERROR; | |
1089 | RuleBasedNumberFormat* formatter | |
1090 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("es", "ES", ""), status); | |
1091 | ||
1092 | if (U_FAILURE(status)) { | |
1093 | errln("FAIL: could not construct formatter"); | |
1094 | } else { | |
1095 | static const char* testData[][2] = { | |
1096 | { "1", "uno" }, | |
1097 | { "6", "seis" }, | |
1098 | { "16", "diecis\\u00e9is" }, | |
1099 | { "20", "veinte" }, | |
1100 | { "24", "veinticuatro" }, | |
1101 | { "26", "veintis\\u00e9is" }, | |
1102 | { "73", "setenta y tres" }, | |
1103 | { "88", "ochenta y ocho" }, | |
1104 | { "100", "cien" }, | |
1105 | { "106", "ciento seis" }, | |
1106 | { "127", "ciento veintisiete" }, | |
1107 | { "200", "doscientos" }, | |
1108 | { "579", "quinientos setenta y nueve" }, | |
1109 | { "1,000", "mil" }, | |
1110 | { "2,000", "dos mil" }, | |
1111 | { "3,004", "tres mil cuatro" }, | |
1112 | { "4,567", "cuatro mil quinientos sesenta y siete" }, | |
1113 | { "15,943", "quince mil novecientos cuarenta y tres" }, | |
1114 | { "2,345,678", "dos mill\\u00f3n trescientos cuarenta y cinco mil seiscientos setenta y ocho"}, | |
1115 | { "-36", "menos treinta y seis" }, | |
1116 | { "234.567", "doscientos treinta y cuatro punto cinco seis siete" }, | |
1117 | { NULL, NULL} | |
1118 | }; | |
1119 | ||
1120 | doTest(formatter, testData, TRUE); | |
1121 | } | |
1122 | delete formatter; | |
1123 | } | |
1124 | ||
1125 | void | |
1126 | IntlTestRBNF::TestFrenchSpellout() | |
1127 | { | |
1128 | UErrorCode status = U_ZERO_ERROR; | |
1129 | RuleBasedNumberFormat* formatter | |
1130 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getFrance(), status); | |
1131 | ||
1132 | if (U_FAILURE(status)) { | |
1133 | errln("FAIL: could not construct formatter"); | |
1134 | } else { | |
1135 | static const char* testData[][2] = { | |
1136 | { "1", "un" }, | |
1137 | { "15", "quinze" }, | |
1138 | { "20", "vingt" }, | |
1139 | { "21", "vingt-et-un" }, | |
1140 | { "23", "vingt-trois" }, | |
1141 | { "62", "soixante-deux" }, | |
1142 | { "70", "soixante-dix" }, | |
1143 | { "71", "soixante et onze" }, | |
1144 | { "73", "soixante-treize" }, | |
1145 | { "80", "quatre-vingts" }, | |
1146 | { "88", "quatre-vingt-huit" }, | |
1147 | { "100", "cent" }, | |
1148 | { "106", "cent six" }, | |
1149 | { "127", "cent vingt-sept" }, | |
1150 | { "200", "deux cents" }, | |
1151 | { "579", "cinq cents soixante-dix-neuf" }, | |
1152 | { "1,000", "mille" }, | |
1153 | { "1,123", "onze cents vingt-trois" }, | |
1154 | { "1,594", "mille cinq cents quatre-vingt-quatorze" }, | |
1155 | { "2,000", "deux mille" }, | |
1156 | { "3,004", "trois mille quatre" }, | |
1157 | { "4,567", "quatre mille cinq cents soixante-sept" }, | |
1158 | { "15,943", "quinze mille neuf cents quarante-trois" }, | |
1159 | { "2,345,678", "deux million trois cents quarante-cinq mille six cents soixante-dix-huit" }, | |
1160 | { "-36", "moins trente-six" }, | |
1161 | { "234.567", "deux cents trente-quatre virgule cinq six sept" }, | |
1162 | { NULL, NULL} | |
1163 | }; | |
1164 | ||
1165 | doTest(formatter, testData, TRUE); | |
1166 | ||
1167 | #if !UCONFIG_NO_COLLATION | |
1168 | formatter->setLenient(TRUE); | |
1169 | static const char* lpTestData[][2] = { | |
1170 | { "trente-un", "31" }, | |
1171 | { "un cents quatre vingt dix huit", "198" }, | |
1172 | { NULL, NULL} | |
1173 | }; | |
1174 | doLenientParseTest(formatter, lpTestData); | |
1175 | #endif | |
1176 | } | |
1177 | delete formatter; | |
1178 | } | |
1179 | ||
1180 | static const char* swissFrenchTestData[][2] = { | |
374ca955 A |
1181 | { "1", "un" }, |
1182 | { "15", "quinze" }, | |
1183 | { "20", "vingt" }, | |
1184 | { "21", "vingt-et-un" }, | |
1185 | { "23", "vingt-trois" }, | |
1186 | { "62", "soixante-deux" }, | |
1187 | { "70", "septante" }, | |
1188 | { "71", "septante-et-un" }, | |
1189 | { "73", "septante-trois" }, | |
1190 | { "80", "huitante" }, | |
1191 | { "88", "huitante-huit" }, | |
1192 | { "100", "cent" }, | |
1193 | { "106", "cent six" }, | |
1194 | { "127", "cent vingt-sept" }, | |
1195 | { "200", "deux cents" }, | |
1196 | { "579", "cinq cents septante-neuf" }, | |
1197 | { "1,000", "mille" }, | |
1198 | { "1,123", "onze cents vingt-trois" }, | |
1199 | { "1,594", "mille cinq cents nonante-quatre" }, | |
1200 | { "2,000", "deux mille" }, | |
1201 | { "3,004", "trois mille quatre" }, | |
1202 | { "4,567", "quatre mille cinq cents soixante-sept" }, | |
1203 | { "15,943", "quinze mille neuf cents quarante-trois" }, | |
1204 | { "2,345,678", "deux million trois cents quarante-cinq mille six cents septante-huit" }, | |
1205 | { "-36", "moins trente-six" }, | |
1206 | { "234.567", "deux cents trente-quatre virgule cinq six sept" }, | |
1207 | { NULL, NULL} | |
b75a7d8f A |
1208 | }; |
1209 | ||
1210 | void | |
1211 | IntlTestRBNF::TestSwissFrenchSpellout() | |
1212 | { | |
1213 | UErrorCode status = U_ZERO_ERROR; | |
1214 | RuleBasedNumberFormat* formatter | |
1215 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "CH", ""), status); | |
1216 | ||
1217 | if (U_FAILURE(status)) { | |
1218 | errln("FAIL: could not construct formatter"); | |
1219 | } else { | |
1220 | doTest(formatter, swissFrenchTestData, TRUE); | |
1221 | } | |
1222 | delete formatter; | |
1223 | } | |
1224 | ||
1225 | void | |
1226 | IntlTestRBNF::TestBelgianFrenchSpellout() | |
1227 | { | |
1228 | UErrorCode status = U_ZERO_ERROR; | |
1229 | RuleBasedNumberFormat* formatter | |
1230 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "BE", ""), status); | |
1231 | ||
1232 | if (U_FAILURE(status)) { | |
374ca955 | 1233 | errln("rbnf status: 0x%x (%s)\n", status, u_errorName(status)); |
b75a7d8f A |
1234 | errln("FAIL: could not construct formatter"); |
1235 | } else { | |
374ca955 | 1236 | // Belgian french should match Swiss french. |
b75a7d8f A |
1237 | doTest(formatter, swissFrenchTestData, TRUE); |
1238 | } | |
1239 | delete formatter; | |
1240 | } | |
1241 | ||
1242 | void | |
1243 | IntlTestRBNF::TestItalianSpellout() | |
1244 | { | |
1245 | UErrorCode status = U_ZERO_ERROR; | |
1246 | RuleBasedNumberFormat* formatter | |
1247 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getItalian(), status); | |
1248 | ||
1249 | if (U_FAILURE(status)) { | |
1250 | errln("FAIL: could not construct formatter"); | |
1251 | } else { | |
1252 | static const char* testData[][2] = { | |
1253 | { "1", "uno" }, | |
1254 | { "15", "quindici" }, | |
1255 | { "20", "venti" }, | |
1256 | { "23", "ventitre" }, | |
1257 | { "73", "settantatre" }, | |
1258 | { "88", "ottantotto" }, | |
1259 | { "100", "cento" }, | |
1260 | { "106", "centosei" }, | |
1261 | { "108", "centotto" }, | |
1262 | { "127", "centoventisette" }, | |
1263 | { "181", "centottantuno" }, | |
1264 | { "200", "duecento" }, | |
1265 | { "579", "cinquecentosettantanove" }, | |
1266 | { "1,000", "mille" }, | |
1267 | { "2,000", "duemila" }, | |
1268 | { "3,004", "tremilaquattro" }, | |
1269 | { "4,567", "quattromilacinquecentosessantasette" }, | |
1270 | { "15,943", "quindicimilanovecentoquarantatre" }, | |
1271 | { "-36", "meno trentisei" }, | |
1272 | { "234.567", "duecentotrentiquattro virgola cinque sei sette" }, | |
1273 | { NULL, NULL} | |
1274 | }; | |
1275 | ||
1276 | doTest(formatter, testData, TRUE); | |
1277 | } | |
1278 | delete formatter; | |
1279 | } | |
1280 | ||
1281 | void | |
1282 | IntlTestRBNF::TestGermanSpellout() | |
1283 | { | |
1284 | UErrorCode status = U_ZERO_ERROR; | |
1285 | RuleBasedNumberFormat* formatter | |
1286 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getGermany(), status); | |
1287 | ||
1288 | if (U_FAILURE(status)) { | |
1289 | errln("FAIL: could not construct formatter"); | |
1290 | } else { | |
1291 | static const char* testData[][2] = { | |
1292 | { "1", "eins" }, | |
1293 | { "15", "f\\u00fcnfzehn" }, | |
1294 | { "20", "zwanzig" }, | |
1295 | { "23", "dreiundzwanzig" }, | |
1296 | { "73", "dreiundsiebzig" }, | |
1297 | { "88", "achtundachtzig" }, | |
1298 | { "100", "hundert" }, | |
1299 | { "106", "hundertsechs" }, | |
1300 | { "127", "hundertsiebenundzwanzig" }, | |
1301 | { "200", "zweihundert" }, | |
1302 | { "579", "f\\u00fcnfhundertneunundsiebzig" }, | |
1303 | { "1,000", "tausend" }, | |
1304 | { "2,000", "zweitausend" }, | |
1305 | { "3,004", "dreitausendvier" }, | |
1306 | { "4,567", "viertausendf\\u00fcnfhundertsiebenundsechzig" }, | |
1307 | { "15,943", "f\\u00fcnfzehntausendneunhundertdreiundvierzig" }, | |
1308 | { "2,345,678", "zwei Millionen dreihundertf\\u00fcnfundvierzigtausendsechshundertachtundsiebzig" }, | |
1309 | { NULL, NULL} | |
1310 | }; | |
1311 | ||
1312 | doTest(formatter, testData, TRUE); | |
1313 | ||
1314 | #if !UCONFIG_NO_COLLATION | |
1315 | formatter->setLenient(TRUE); | |
1316 | static const char* lpTestData[][2] = { | |
1317 | { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" }, | |
1318 | { NULL, NULL} | |
1319 | }; | |
1320 | doLenientParseTest(formatter, lpTestData); | |
1321 | #endif | |
1322 | } | |
1323 | delete formatter; | |
1324 | } | |
1325 | ||
1326 | void | |
1327 | IntlTestRBNF::TestThaiSpellout() | |
1328 | { | |
1329 | UErrorCode status = U_ZERO_ERROR; | |
1330 | RuleBasedNumberFormat* formatter | |
1331 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("th"), status); | |
1332 | ||
1333 | if (U_FAILURE(status)) { | |
1334 | errln("FAIL: could not construct formatter"); | |
1335 | } else { | |
1336 | static const char* testData[][2] = { | |
1337 | { "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" }, | |
1338 | { "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" }, | |
1339 | { "10", "\\u0e2a\\u0e34\\u0e1a" }, | |
1340 | { "11", "\\u0e2a\\u0e34\\u0e1a\\u0e40\\u0e2d\\u0e47\\u0e14" }, | |
1341 | { "21", "\\u0e22\\u0e35\\u0e48\\u0e2a\\u0e34\\u0e1a\\u0e40\\u0e2d\\u0e47\\u0e14" }, | |
1342 | { "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u0e23\\u0e49\\u0e2d\\u0e22\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" }, | |
1343 | { "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u0e08\\u0e38\\u0e14\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" }, | |
1344 | { NULL, NULL} | |
1345 | }; | |
1346 | ||
1347 | doTest(formatter, testData, TRUE); | |
1348 | } | |
1349 | delete formatter; | |
1350 | } | |
1351 | ||
1352 | void | |
1353 | IntlTestRBNF::TestSwedishSpellout() | |
1354 | { | |
1355 | UErrorCode status = U_ZERO_ERROR; | |
1356 | RuleBasedNumberFormat* formatter | |
1357 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("sv"), status); | |
1358 | ||
1359 | if (U_FAILURE(status)) { | |
1360 | errln("FAIL: could not construct formatter"); | |
1361 | } else { | |
1362 | static const char* testDataDefault[][2] = { | |
1363 | { "101", "etthundra\\u00aden" }, | |
1364 | { "123", "etthundra\\u00adtjugotre" }, | |
1365 | { "1,001", "ettusen en" }, | |
1366 | { "1,100", "ettusen etthundra" }, | |
1367 | { "1,101", "ettusen etthundra\\u00aden" }, | |
1368 | { "1,234", "ettusen tv\\u00e5hundra\\u00adtrettiofyra" }, | |
1369 | { "10,001", "tio\\u00adtusen en" }, | |
1370 | { "11,000", "elva\\u00adtusen" }, | |
1371 | { "12,000", "tolv\\u00adtusen" }, | |
1372 | { "20,000", "tjugo\\u00adtusen" }, | |
1373 | { "21,000", "tjugoen\\u00adtusen" }, | |
1374 | { "21,001", "tjugoen\\u00adtusen en" }, | |
1375 | { "200,000", "tv\\u00e5hundra\\u00adtusen" }, | |
1376 | { "201,000", "tv\\u00e5hundra\\u00aden\\u00adtusen" }, | |
1377 | { "200,200", "tv\\u00e5hundra\\u00adtusen tv\\u00e5hundra" }, | |
1378 | { "2,002,000", "tv\\u00e5 miljoner tv\\u00e5\\u00adtusen" }, | |
1379 | { "12,345,678", "tolv miljoner trehundra\\u00adfyrtiofem\\u00adtusen sexhundra\\u00adsjuttio\\u00e5tta" }, | |
1380 | { "123,456.789", "etthundra\\u00adtjugotre\\u00adtusen fyrahundra\\u00adfemtiosex komma sju \\u00e5tta nio" }, | |
1381 | { "-12,345.678", "minus tolv\\u00adtusen trehundra\\u00adfyrtiofem komma sex sju \\u00e5tta" }, | |
1382 | { NULL, NULL } | |
1383 | }; | |
1384 | doTest(formatter, testDataDefault, TRUE); | |
1385 | ||
1386 | static const char* testDataNeutrum[][2] = { | |
1387 | { "101", "etthundra\\u00adett" }, | |
1388 | { "1,001", "ettusen ett" }, | |
1389 | { "1,101", "ettusen etthundra\\u00adett" }, | |
1390 | { "10,001", "tio\\u00adtusen ett" }, | |
1391 | { "21,001", "tjugoen\\u00adtusen ett" }, | |
1392 | { NULL, NULL } | |
1393 | }; | |
1394 | ||
1395 | formatter->setDefaultRuleSet("%neutrum", status); | |
1396 | if (U_SUCCESS(status)) { | |
1397 | logln("testing neutrum rules"); | |
1398 | doTest(formatter, testDataNeutrum, TRUE); | |
1399 | } | |
1400 | else { | |
1401 | errln("Can't test neutrum rules"); | |
1402 | } | |
1403 | ||
1404 | static const char* testDataYear[][2] = { | |
1405 | { "101", "etthundra\\u00adett" }, | |
1406 | { "900", "niohundra" }, | |
1407 | { "1,001", "tiohundra\\u00adett" }, | |
1408 | { "1,100", "elvahundra" }, | |
1409 | { "1,101", "elvahundra\\u00adett" }, | |
1410 | { "1,234", "tolvhundra\\u00adtrettiofyra" }, | |
1411 | { "2,001", "tjugohundra\\u00adett" }, | |
1412 | { "10,001", "tio\\u00adtusen ett" }, | |
1413 | { NULL, NULL } | |
1414 | }; | |
1415 | ||
1416 | formatter->setDefaultRuleSet("%year", status); | |
1417 | if (U_SUCCESS(status)) { | |
1418 | logln("testing year rules"); | |
1419 | doTest(formatter, testDataYear, TRUE); | |
1420 | } | |
1421 | else { | |
1422 | errln("Can't test year rules"); | |
1423 | } | |
1424 | ||
1425 | } | |
1426 | delete formatter; | |
1427 | } | |
1428 | ||
374ca955 A |
1429 | void |
1430 | IntlTestRBNF::TestSmallValues() | |
1431 | { | |
1432 | UErrorCode status = U_ZERO_ERROR; | |
1433 | RuleBasedNumberFormat* formatter | |
1434 | = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("en_US"), status); | |
1435 | ||
1436 | if (U_FAILURE(status)) { | |
1437 | errln("FAIL: could not construct formatter"); | |
1438 | } else { | |
1439 | static const char* testDataDefault[][2] = { | |
1440 | { "0.001", "zero point zero zero one" }, | |
1441 | { "0.0001", "zero point zero zero zero one" }, | |
1442 | { "0.00001", "zero point zero zero zero zero one" }, | |
1443 | { "0.000001", "zero point zero zero zero zero zero one" }, | |
1444 | { "0.0000001", "zero point zero zero zero zero zero zero one" }, | |
1445 | { "0.00000001", "zero point zero zero zero zero zero zero zero one" }, | |
1446 | { "0.000000001", "zero point zero zero zero zero zero zero zero zero one" }, | |
1447 | { "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" }, | |
1448 | { "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" }, | |
1449 | { "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" }, | |
1450 | { "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" }, | |
1451 | { "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" }, | |
1452 | { "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" }, | |
1453 | { "10,000,000.001", "ten million point zero zero one" }, | |
1454 | { "10,000,000.0001", "ten million point zero zero zero one" }, | |
1455 | { "10,000,000.00001", "ten million point zero zero zero zero one" }, | |
1456 | { "10,000,000.000001", "ten million point zero zero zero zero zero one" }, | |
1457 | { "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" }, | |
1458 | // { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" }, | |
1459 | // { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" }, | |
1460 | { "10,000,000", "ten million" }, | |
1461 | // { "1,234,567,890.0987654", "one billion, two hundred and thirty-four million, five hundred and sixty-seven thousand, eight hundred and ninety point zero nine eight seven six five four" }, | |
1462 | // { "123,456,789.9876543", "one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine point nine eight seven six five four three" }, | |
1463 | // { "12,345,678.87654321", "twelve million, three hundred and forty-five thousand, six hundred and seventy-eight point eight seven six five four three two one" }, | |
1464 | { "1,234,567.7654321", "one million, two hundred and thirty-four thousand, five hundred and sixty-seven point seven six five four three two one" }, | |
1465 | { "123,456.654321", "one hundred and twenty-three thousand, four hundred and fifty-six point six five four three two one" }, | |
1466 | { "12,345.54321", "twelve thousand three hundred and forty-five point five four three two one" }, | |
1467 | { "1,234.4321", "one thousand two hundred and thirty-four point four three two one" }, | |
1468 | { "123.321", "one hundred and twenty-three point three two one" }, | |
1469 | { "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" }, | |
1470 | { "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" }, | |
1471 | { NULL, NULL } | |
1472 | }; | |
1473 | ||
1474 | doTest(formatter, testDataDefault, TRUE); | |
1475 | ||
1476 | delete formatter; | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | void | |
1481 | IntlTestRBNF::TestLocalizations(void) | |
1482 | { | |
1483 | int i; | |
1484 | UnicodeString rules("%main:0:no;1:some;100:a lot;1000:tons;\n" | |
1485 | "%other:0:nada;1:yah, some;100:plenty;1000:more'n you'll ever need"); | |
1486 | ||
1487 | UErrorCode status = U_ZERO_ERROR; | |
1488 | UParseError perror; | |
1489 | RuleBasedNumberFormat formatter(rules, perror, status); | |
1490 | if (U_FAILURE(status)) { | |
1491 | errln("FAIL: could not construct formatter"); | |
1492 | } else { | |
1493 | { | |
1494 | static const char* testData[][2] = { | |
1495 | { "0", "nada" }, | |
1496 | { "5", "yah, some" }, | |
1497 | { "423", "plenty" }, | |
1498 | { "12345", "more'n you'll ever need" }, | |
1499 | { NULL, NULL } | |
1500 | }; | |
1501 | doTest(&formatter, testData, FALSE); | |
1502 | } | |
1503 | ||
1504 | { | |
1505 | UnicodeString loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>"); | |
1506 | static const char* testData[][2] = { | |
1507 | { "0", "no" }, | |
1508 | { "5", "some" }, | |
1509 | { "423", "a lot" }, | |
1510 | { "12345", "tons" }, | |
1511 | { NULL, NULL } | |
1512 | }; | |
1513 | RuleBasedNumberFormat formatter0(rules, loc, perror, status); | |
1514 | if (U_FAILURE(status)) { | |
1515 | errln("failed to build second formatter"); | |
1516 | } else { | |
1517 | doTest(&formatter0, testData, FALSE); | |
1518 | ||
1519 | { | |
1520 | // exercise localization info | |
1521 | Locale locale0("en__VALLEY@turkey=gobblegobble"); | |
1522 | Locale locale1("de_DE_FOO"); | |
1523 | Locale locale2("ja_JP"); | |
1524 | logln(formatter0.getRuleSetDisplayName(0, locale0)); | |
1525 | logln(formatter0.getRuleSetDisplayName(0, locale1)); | |
1526 | logln(formatter0.getRuleSetDisplayName(0, locale2)); | |
1527 | // TODO: check against intended result | |
1528 | } | |
1529 | ||
1530 | for (i = 0; i < formatter0.getNumberOfRuleSetDisplayNameLocales(); ++i) { | |
1531 | Locale locale = formatter0.getRuleSetDisplayNameLocale(i, status); | |
1532 | if (U_SUCCESS(status)) { | |
1533 | for (int j = 0; j < formatter0.getNumberOfRuleSetNames(); ++j) { | |
1534 | UnicodeString name = formatter0.getRuleSetName(j); | |
1535 | UnicodeString lname = formatter0.getRuleSetDisplayName(j, locale); | |
1536 | UnicodeString msg = locale.getName(); | |
1537 | msg.append(": "); | |
1538 | msg.append(name); | |
1539 | msg.append(" = "); | |
1540 | msg.append(lname); | |
1541 | logln(msg); | |
1542 | } | |
1543 | } | |
1544 | } | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | { | |
1549 | static const char* goodLocs[] = { | |
1550 | "", // zero-length ok, same as providing no localization data | |
1551 | "<<>>", // no public rule sets ok | |
1552 | "<<%main>>", // no localizations ok | |
1553 | "<<%main,>,<en, Main,>>", // comma before close angle ok | |
1554 | "<<%main>,<en, ',<>\" '>>", // quotes everything until next quote | |
1555 | "<<%main>,<'en', \"it's ok\">>", // double quotes work too | |
1556 | " \n <\n <\n %main\n >\n , \t <\t en\t , \tfoo \t\t > \n\n > \n ", // rule whitespace ok | |
1557 | }; | |
1558 | int32_t goodLocsLen = sizeof(goodLocs)/sizeof(goodLocs[0]); | |
1559 | ||
1560 | static const char* badLocs[] = { | |
1561 | " ", // non-zero length | |
1562 | "<>", // empty array | |
1563 | "<", // unclosed outer array | |
1564 | "<<", // unclosed inner array | |
1565 | "<<,>>", // unexpected comma | |
1566 | "<<''>>", // empty string | |
1567 | " x<<%main>>", // first non space char not open angle bracket | |
1568 | "<%main>", // missing inner array | |
1569 | "<<%main %other>>", // elements missing separating commma (spaces must be quoted) | |
1570 | "<<%main><en, Main>>", // arrays missing separating comma | |
1571 | "<<%main>,<en, main, foo>>", // too many elements in locale data | |
1572 | "<<%main>,<en>>", // too few elements in locale data | |
1573 | "<<<%main>>>", // unexpected open angle | |
1574 | "<<%main<>>>", // unexpected open angle | |
1575 | "<<%main, %other>,<en,,>>", // implicit empty strings | |
1576 | "<<%main>,<en,''>>", // empty string | |
1577 | "<<%main>, < en, '>>", // unterminated quote | |
1578 | "<<%main>, < en, \"<>>", // unterminated quote | |
1579 | "<<%main\">>", // quote in string | |
1580 | "<<%main'>>", // quote in string | |
1581 | "<<%main<>>", // open angle in string | |
1582 | "<<%main>> x", // extra non-space text at end | |
1583 | ||
1584 | }; | |
1585 | int32_t badLocsLen = sizeof(badLocs)/sizeof(badLocs[0]); | |
1586 | ||
1587 | for (i = 0; i < goodLocsLen; ++i) { | |
1588 | logln("[%d] '%s'", i, goodLocs[i]); | |
1589 | UErrorCode status = U_ZERO_ERROR; | |
1590 | UnicodeString loc(goodLocs[i]); | |
1591 | RuleBasedNumberFormat fmt(rules, loc, perror, status); | |
1592 | if (U_FAILURE(status)) { | |
1593 | errln("Failed parse of good localization string: '%s'", goodLocs[i]); | |
1594 | } | |
1595 | } | |
1596 | ||
1597 | for (i = 0; i < badLocsLen; ++i) { | |
1598 | logln("[%d] '%s'", i, badLocs[i]); | |
1599 | UErrorCode status = U_ZERO_ERROR; | |
1600 | UnicodeString loc(badLocs[i]); | |
1601 | RuleBasedNumberFormat fmt(rules, loc, perror, status); | |
1602 | if (U_SUCCESS(status)) { | |
1603 | errln("Successful parse of bad localization string: '%s'", badLocs[i]); | |
1604 | } | |
1605 | } | |
1606 | } | |
1607 | } | |
1608 | } | |
b75a7d8f A |
1609 | |
1610 | void | |
1611 | IntlTestRBNF::doTest(RuleBasedNumberFormat* formatter, const char* testData[][2], UBool testParsing) | |
1612 | { | |
1613 | // man, error reporting would be easier with printf-style syntax for unicode string and formattable | |
1614 | ||
1615 | UErrorCode status = U_ZERO_ERROR; | |
374ca955 A |
1616 | // NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status); |
1617 | NumberFormat* decFmt = new DecimalFormat("#,###.################", status); | |
b75a7d8f A |
1618 | if (U_FAILURE(status)) { |
1619 | errln("FAIL: could not create NumberFormat"); | |
1620 | } else { | |
1621 | for (int i = 0; testData[i][0]; ++i) { | |
1622 | const char* numString = testData[i][0]; | |
1623 | const char* expectedWords = testData[i][1]; | |
1624 | ||
374ca955 | 1625 | log("[%i] %s = ", i, numString); |
b75a7d8f A |
1626 | Formattable expectedNumber; |
1627 | decFmt->parse(numString, expectedNumber, status); | |
1628 | if (U_FAILURE(status)) { | |
1629 | errln("FAIL: decFmt could not parse %s", numString); | |
1630 | break; | |
1631 | } else { | |
1632 | UnicodeString actualString; | |
1633 | FieldPosition pos; | |
1634 | formatter->format(expectedNumber, actualString/* , pos*/, status); | |
1635 | if (U_FAILURE(status)) { | |
1636 | UnicodeString msg = "Fail: formatter could not format "; | |
1637 | decFmt->format(expectedNumber, msg, status); | |
1638 | errln(msg); | |
1639 | break; | |
1640 | } else { | |
1641 | UnicodeString expectedString = UnicodeString(expectedWords).unescape(); | |
1642 | if (actualString != expectedString) { | |
1643 | UnicodeString msg = "FAIL: check failed for "; | |
1644 | decFmt->format(expectedNumber, msg, status); | |
1645 | msg.append(", expected "); | |
1646 | msg.append(expectedString); | |
1647 | msg.append(" but got "); | |
1648 | msg.append(actualString); | |
1649 | errln(msg); | |
1650 | break; | |
374ca955 A |
1651 | } else { |
1652 | logln(actualString); | |
1653 | if (testParsing) { | |
1654 | Formattable parsedNumber; | |
1655 | formatter->parse(actualString, parsedNumber, status); | |
1656 | if (U_FAILURE(status)) { | |
1657 | UnicodeString msg = "FAIL: formatter could not parse "; | |
b75a7d8f | 1658 | msg.append(actualString); |
374ca955 A |
1659 | msg.append(" status code: " ); |
1660 | msg.append(u_errorName(status)); | |
b75a7d8f A |
1661 | errln(msg); |
1662 | break; | |
374ca955 A |
1663 | } else { |
1664 | if (parsedNumber != expectedNumber) { | |
1665 | UnicodeString msg = "FAIL: parse failed for "; | |
1666 | msg.append(actualString); | |
1667 | msg.append(", expected "); | |
1668 | decFmt->format(expectedNumber, msg, status); | |
1669 | msg.append(", but got "); | |
1670 | decFmt->format(parsedNumber, msg, status); | |
1671 | errln(msg); | |
1672 | break; | |
1673 | } | |
b75a7d8f A |
1674 | } |
1675 | } | |
1676 | } | |
1677 | } | |
1678 | } | |
1679 | } | |
1680 | delete decFmt; | |
1681 | } | |
1682 | } | |
1683 | ||
1684 | void | |
1685 | IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat* formatter, const char* testData[][2]) | |
1686 | { | |
1687 | UErrorCode status = U_ZERO_ERROR; | |
1688 | NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status); | |
1689 | if (U_FAILURE(status)) { | |
1690 | errln("FAIL: could not create NumberFormat"); | |
1691 | } else { | |
1692 | for (int i = 0; testData[i][0]; ++i) { | |
1693 | const char* spelledNumber = testData[i][0]; // spelled-out number | |
1694 | const char* asciiUSNumber = testData[i][1]; // number as ascii digits formatted for US locale | |
1695 | ||
1696 | UnicodeString spelledNumberString = UnicodeString(spelledNumber).unescape(); | |
1697 | Formattable actualNumber; | |
1698 | formatter->parse(spelledNumberString, actualNumber, status); | |
1699 | if (U_FAILURE(status)) { | |
1700 | UnicodeString msg = "FAIL: formatter could not parse "; | |
1701 | msg.append(spelledNumberString); | |
1702 | errln(msg); | |
1703 | break; | |
1704 | } else { | |
1705 | // I changed the logic of this test somewhat from Java-- instead of comparing the | |
1706 | // strings, I compare the Formattables. Hmmm, but the Formattables don't compare, | |
1707 | // so change it back. | |
1708 | ||
1709 | UnicodeString asciiUSNumberString = asciiUSNumber; | |
1710 | Formattable expectedNumber; | |
1711 | decFmt->parse(asciiUSNumberString, expectedNumber, status); | |
1712 | if (U_FAILURE(status)) { | |
1713 | UnicodeString msg = "FAIL: decFmt could not parse "; | |
1714 | msg.append(asciiUSNumberString); | |
1715 | errln(msg); | |
1716 | break; | |
1717 | } else { | |
1718 | UnicodeString actualNumberString; | |
1719 | UnicodeString expectedNumberString; | |
1720 | decFmt->format(actualNumber, actualNumberString, status); | |
1721 | decFmt->format(expectedNumber, expectedNumberString, status); | |
1722 | if (actualNumberString != expectedNumberString) { | |
1723 | UnicodeString msg = "FAIL: parsing"; | |
1724 | msg.append(asciiUSNumberString); | |
1725 | msg.append("\n"); | |
1726 | msg.append(" lenient parse failed for "); | |
1727 | msg.append(spelledNumberString); | |
1728 | msg.append(", expected "); | |
1729 | msg.append(expectedNumberString); | |
1730 | msg.append(", but got "); | |
1731 | msg.append(actualNumberString); | |
1732 | errln(msg); | |
1733 | break; | |
1734 | } | |
1735 | } | |
1736 | } | |
1737 | } | |
1738 | delete decFmt; | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | /* U_HAVE_RBNF */ | |
1743 | #else | |
1744 | ||
1745 | void | |
1746 | IntlTestRBNF::TestRBNFDisabled() { | |
1747 | errln("*** RBNF currently disabled on this platform ***\n"); | |
1748 | } | |
1749 | ||
1750 | /* U_HAVE_RBNF */ | |
1751 | #endif | |
1752 | ||
1753 | #endif /* #if !UCONFIG_NO_FORMATTING */ |