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