2 *******************************************************************************
3 * Copyright (C) 1996-2004, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/umachine.h"
16 #include "unicode/tblcoll.h"
17 #include "unicode/coleitr.h"
18 #include "unicode/ures.h"
19 #include "unicode/ustring.h"
20 #include "unicode/decimfmt.h"
21 #include "unicode/udata.h"
26 // import com.ibm.text.RuleBasedNumberFormat;
27 // import com.ibm.test.TestFmwk;
29 // import java.util.Locale;
30 // import java.text.NumberFormat;
32 // current macro not in icu1.8.1
33 #define TESTCASE(id,test) \
38 logln((UnicodeString)""); \
43 void IntlTestRBNF::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/)
45 if (exec
) logln("TestSuite RuleBasedNumberFormat");
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
);
58 TESTCASE(10, TestFractionalRuleSet
);
59 TESTCASE(11, TestSwedishSpellout
);
60 TESTCASE(12, TestBelgianFrenchSpellout
);
61 TESTCASE(13, TestSmallValues
);
62 TESTCASE(14, TestLocalizations
);
64 TESTCASE(0, TestRBNFDisabled
);
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
79 UErrorCode status
= U_ZERO_ERROR
;
80 RuleBasedNumberFormat
* formatter
81 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
83 logln("RBNF API test starting");
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!");
94 errln("Cloning failed!");
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!");
108 // test rule constructor
110 logln("Testing rule constructor");
111 UResourceBundle
*en
= ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
"rbnf", "en", &status
);
112 if(U_FAILURE(status
)) {
113 errln("Unable to access resource bundle with data!");
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!");
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!");
132 logln("Testing getRules function");
133 UnicodeString rules
= formatter
->getRules();
135 RuleBasedNumberFormat
fromRulesResult(rules
, Locale::getUS(), perror
, status
);
137 if(!(fromRulesResult
== *formatter
)) {
138 errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!");
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!");
151 #if !UCONFIG_NO_COLLATION
152 // test ruleset names
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");
159 UnicodeString ruleSetName
;
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
;
169 ruleSetName
= formatter
->getRuleSetName(i
);
170 log("Rule set name %i is ", i
);
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");
179 logln(intFormatResult
);
180 logln(doubleFormatResult
);
181 formatter
->setLenient(TRUE
);
182 formatter
->parse(intFormatResult
, intParseResult
, status
);
183 formatter
->parse(doubleFormatResult
, doubleParseResult
, status
);
185 logln("Parse results for lenient = TRUE, %i, %f", intParseResult
.getLong(), doubleParseResult
.getDouble());
187 formatter
->setLenient(FALSE
);
188 formatter
->parse(intFormatResult
, intParseResult
, status
);
189 formatter
->parse(doubleFormatResult
, doubleParseResult
, status
);
191 logln("Parse results for lenient = FALSE, %i, %f", intParseResult
.getLong(), doubleParseResult
.getDouble());
193 if(U_FAILURE(status
)) {
194 errln("Error during parsing");
197 intFormatResult
= formatter
->format(intFormatNum
, "BLABLA", intFormatResult
, pos1
, status
);
198 if(U_SUCCESS(status
)) {
199 errln("Using invalid rule set name should have failed");
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");
208 status
= U_ZERO_ERROR
;
210 status
= U_ZERO_ERROR
;
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
);
222 logln("Formatted 4.5, expected " + expected
+ " got " + result
);
226 formatter
->format((int32_t)4,result
);
227 if(result
!= expected
) {
228 errln("Formatted 4, expected " + expected
+ " got " + result
);
230 logln("Formatted 4, expected " + expected
+ " got " + result
);
235 logln("Cleaning up");
239 void IntlTestRBNF::TestFractionalRuleSet()
241 UnicodeString
fracRules(
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
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
273 int len
= fracRules
.length();
275 for (int i
= 0; i
< len
; ++i
) {
276 UChar ch
= fracRules
.charAt(i
);
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
== ' ') {
283 fracRules
.setCharAt(i
, (UChar
)0x200e);
292 UErrorCode status
= U_ZERO_ERROR
;
294 RuleBasedNumberFormat
formatter(fracRules
, Locale::getEnglish(), perror
, status
);
295 if (U_FAILURE(status
)) {
296 errln("FAIL: could not construct formatter");
298 static const char* testData
[][2] = {
311 { "3.125", "3 1/8" },
312 { "4.1428", "4 1/7" },
313 { "5.1667", "5 1/6" },
316 { "8.333", "8 1/3" },
321 { "1.2856", "1 2/7" },
324 doTest(&formatter
, testData
, FALSE
); // exact values aren't parsable from fractions
329 #define LLAssert(a) \
330 if (!(a)) errln("FAIL: " #a)
332 void IntlTestRBNF::TestLLongConstructors()
334 logln("Testing constructors");
336 // constant (shouldn't really be public)
337 LLAssert(llong(llong::kD32
).asDouble() == llong::kD32
);
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);
344 // public empty constructor
345 LLAssert(llong().asDouble() == 0);
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);
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);
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);
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);
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);
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);
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
);
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);
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);
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);
412 // asDouble already tested
416 void IntlTestRBNF::TestLLongSimpleOperators()
418 logln("Testing simple operators");
421 LLAssert(llong() == llong(0, 0));
422 LLAssert(llong(1,0) == llong(1, 0));
423 LLAssert(llong(0,1) == llong(0, 1));
426 LLAssert(llong(1,0) != llong(1,1));
427 LLAssert(llong(0,1) != llong(1,1));
428 LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff));
431 LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff)));
434 LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1)));
437 LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff)));
438 LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1)));
441 LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1)));
442 LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1)));
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));
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));
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));
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));
483 LLAssert(llong() == (int32_t)0);
484 LLAssert(llong(0,1) == (int32_t)1);
487 LLAssert(llong(1,0) != (int32_t)0);
488 LLAssert(llong(0,1) != (int32_t)2);
489 LLAssert(llong(0,0xffffffff) != (int32_t)-1);
491 llong
negOne(0xffffffff, 0xffffffff);
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);
500 LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff);
501 LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1);
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);
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);
518 LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1);
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
529 LLAssert((llong((int32_t)1) << 5).asUInt() == 32);
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
539 // operator >> sign extended)
540 LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde));
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
551 LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
554 LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
557 LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
560 LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
563 LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
566 LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
569 LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa));
572 LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
575 LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
578 LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
580 // operator&=(uint32)
581 LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
583 // operator|=(uint32)
584 LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
586 // operator^=(uint32)
587 LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
590 LLAssert(llong(1, 0) == ++llong(0,0xffffffff));
593 LLAssert(llong(0,0xffffffff) == --llong(1, 0));
597 llong
n(0, 0xffffffff);
598 LLAssert(llong(0, 0xffffffff) == n
++);
599 LLAssert(llong(1, 0) == n
);
605 LLAssert(llong(1, 0) == n
--);
606 LLAssert(llong(0, 0xffffffff) == n
);
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
619 LLAssert((n
-= llong(0, 1)) == llong(0xffffffff, 0xffffffff));
620 LLAssert(n
== llong(0xffffffff, 0xffffffff));
623 LLAssert((n
-= llong(0, 1)) == llong(0, 0xffffffff));
624 LLAssert(n
== llong(0, 0xffffffff));
630 LLAssert((n
- llong(0, 1)) == llong(0xffffffff, 0xffffffff));
631 LLAssert(n
== llong(0, 0));
634 LLAssert((n
- llong(0, 1)) == llong(0, 0xffffffff));
635 LLAssert(n
== llong(1, 0));
640 llong
n(0xffffffff, 0xffffffff);
641 LLAssert((n
+= llong(0, 1)) == llong(0, 0));
642 LLAssert(n
== llong(0, 0));
644 n
= llong(0, 0xffffffff);
645 LLAssert((n
+= llong(0, 1)) == llong(1, 0));
646 LLAssert(n
== llong(1, 0));
651 llong
n(0xffffffff, 0xffffffff);
652 LLAssert((n
+ llong(0, 1)) == llong(0, 0));
653 LLAssert(n
== llong(0xffffffff, 0xffffffff));
655 n
= llong(0, 0xffffffff);
656 LLAssert((n
+ llong(0, 1)) == llong(1, 0));
657 LLAssert(n
== llong(0, 0xffffffff));
662 void IntlTestRBNF::TestLLong()
664 logln("Starting TestLLong");
666 TestLLongConstructors();
668 TestLLongSimpleOperators();
670 logln("Testing operator*=, operator*");
672 // operator*=, operator*
673 // small and large values, positive, &NEGative, zero
674 // also test commutivity
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
;
686 const llong
NINE(0, 9);
687 const llong NEG_NINE
= -NINE
;
689 const llong
TWO_TO_16X3(0, 0x00030000);
690 const llong NEG_TWO_TO_16X3
= -TWO_TO_16X3
;
692 const llong
TWO_TO_32X3(3, 0);
693 const llong NEG_TWO_TO_32X3
= -TWO_TO_32X3
;
695 const llong
TWO_TO_48(0x10000, 0);
696 const llong NEG_TWO_TO_48
= -TWO_TO_48
;
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
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
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
];
723 LLAssert((n
*= rhs
) == ans
);
727 LLAssert((n
* rhs
) == ans
);
733 logln("Testing operator/=, operator/");
734 // operator/=, operator/
735 // test num = 0, div = 0, pos/neg, > 2^32, div > num
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
;
753 const llong
* tuples
[] = { // lhs, rhs, ans
756 &NEG_ONE
, &ZERO
, &MIN
,
758 &ONE
, &NEG_ONE
, &NEG_ONE
,
759 &NEG_ONE
, &ONE
, &NEG_ONE
,
760 &NEG_ONE
, &NEG_ONE
, &ONE
,
762 &FIVE
, &NEG_TWO
, &NEG_TWO
,
763 &NEG_FIVE
, &TWO
, &NEG_TWO
,
764 &NEG_FIVE
, &NEG_TWO
, &TWO
,
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
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];
794 if (!((n
/= rhs
) == ans
)) {
795 errln("fail: (n /= rhs) == ans");
800 LLAssert((n
/ rhs
) == ans
);
805 logln("Testing operator%%=, operator%%");
806 //operator%=, operator%
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);
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
;
823 const llong
NINETY_NINE(0, 99);
824 const llong
HUNDRED(0, 100);
825 const llong
HUNDRED_ONE(0, 101);
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
;
832 const llong
* tuples
[] = {
836 &THREE
, &FIVE
, &THREE
,
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
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];
874 if (!((n
%= rhs
) == ans
)) {
875 errln("fail: (n %= rhs) == ans");
880 LLAssert((n
% rhs
) == ans
);
885 logln("Testing 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));
896 const llong
n(0xffffffff,0xffffffff);
897 LLAssert(n
.abs() == llong(0, 1));
901 logln("Testing 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));
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));
932 logln("Testing lltoa");
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));
943 logln("Testing u_lltoa");
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));
958 IntlTestRBNF::TestEnglishSpellout()
960 UErrorCode status
= U_ZERO_ERROR
;
961 RuleBasedNumberFormat
* formatter
962 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
964 if (U_FAILURE(status
)) {
965 errln("FAIL: could not construct formatter");
967 static const char* testData
[][2] = {
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" },
991 doTest(formatter
, testData
, TRUE
);
993 #if !UCONFIG_NO_COLLATION
994 formatter
->setLenient(TRUE
);
995 static const char* lpTestData
[][2] = {
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" },
1004 doLenientParseTest(formatter
, lpTestData
);
1011 IntlTestRBNF::TestOrdinalAbbreviations()
1013 UErrorCode status
= U_ZERO_ERROR
;
1014 RuleBasedNumberFormat
* formatter
1015 = new RuleBasedNumberFormat(URBNF_ORDINAL
, Locale::getUS(), status
);
1017 if (U_FAILURE(status
)) {
1018 errln("FAIL: could not construct formatter");
1020 static const char* testData
[][2] = {
1037 { "12,345", "12,345th" },
1041 doTest(formatter
, testData
, FALSE
);
1047 IntlTestRBNF::TestDurations()
1049 UErrorCode status
= U_ZERO_ERROR
;
1050 RuleBasedNumberFormat
* formatter
1051 = new RuleBasedNumberFormat(URBNF_DURATION
, Locale::getUS(), status
);
1053 if (U_FAILURE(status
)) {
1054 errln("FAIL: could not construct formatter");
1056 static const char* testData
[][2] = {
1057 { "3,600", "1:00:00" }, //move me and I fail
1060 { "24", "24 sec." },
1065 // { "3,600", "1:00:00" },
1066 { "3,740", "1:02:20" },
1067 { "10,293", "2:51:33" },
1071 doTest(formatter
, testData
, TRUE
);
1073 #if !UCONFIG_NO_COLLATION
1074 formatter
->setLenient(TRUE
);
1075 static const char* lpTestData
[][2] = {
1076 { "2-51-33", "10,293" },
1079 doLenientParseTest(formatter
, lpTestData
);
1086 IntlTestRBNF::TestSpanishSpellout()
1088 UErrorCode status
= U_ZERO_ERROR
;
1089 RuleBasedNumberFormat
* formatter
1090 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("es", "ES", ""), status
);
1092 if (U_FAILURE(status
)) {
1093 errln("FAIL: could not construct formatter");
1095 static const char* testData
[][2] = {
1098 { "16", "diecis\\u00e9is" },
1100 { "24", "veinticuatro" },
1101 { "26", "veintis\\u00e9is" },
1102 { "73", "setenta y tres" },
1103 { "88", "ochenta y ocho" },
1105 { "106", "ciento seis" },
1106 { "127", "ciento veintisiete" },
1107 { "200", "doscientos" },
1108 { "579", "quinientos setenta y nueve" },
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" },
1120 doTest(formatter
, testData
, TRUE
);
1126 IntlTestRBNF::TestFrenchSpellout()
1128 UErrorCode status
= U_ZERO_ERROR
;
1129 RuleBasedNumberFormat
* formatter
1130 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getFrance(), status
);
1132 if (U_FAILURE(status
)) {
1133 errln("FAIL: could not construct formatter");
1135 static const char* testData
[][2] = {
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" },
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" },
1165 doTest(formatter
, testData
, TRUE
);
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" },
1174 doLenientParseTest(formatter
, lpTestData
);
1180 static const char* swissFrenchTestData
[][2] = {
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" },
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" },
1211 IntlTestRBNF::TestSwissFrenchSpellout()
1213 UErrorCode status
= U_ZERO_ERROR
;
1214 RuleBasedNumberFormat
* formatter
1215 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "CH", ""), status
);
1217 if (U_FAILURE(status
)) {
1218 errln("FAIL: could not construct formatter");
1220 doTest(formatter
, swissFrenchTestData
, TRUE
);
1226 IntlTestRBNF::TestBelgianFrenchSpellout()
1228 UErrorCode status
= U_ZERO_ERROR
;
1229 RuleBasedNumberFormat
* formatter
1230 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "BE", ""), status
);
1232 if (U_FAILURE(status
)) {
1233 errln("rbnf status: 0x%x (%s)\n", status
, u_errorName(status
));
1234 errln("FAIL: could not construct formatter");
1236 // Belgian french should match Swiss french.
1237 doTest(formatter
, swissFrenchTestData
, TRUE
);
1243 IntlTestRBNF::TestItalianSpellout()
1245 UErrorCode status
= U_ZERO_ERROR
;
1246 RuleBasedNumberFormat
* formatter
1247 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getItalian(), status
);
1249 if (U_FAILURE(status
)) {
1250 errln("FAIL: could not construct formatter");
1252 static const char* testData
[][2] = {
1254 { "15", "quindici" },
1256 { "23", "ventitre" },
1257 { "73", "settantatre" },
1258 { "88", "ottantotto" },
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" },
1276 doTest(formatter
, testData
, TRUE
);
1282 IntlTestRBNF::TestGermanSpellout()
1284 UErrorCode status
= U_ZERO_ERROR
;
1285 RuleBasedNumberFormat
* formatter
1286 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getGermany(), status
);
1288 if (U_FAILURE(status
)) {
1289 errln("FAIL: could not construct formatter");
1291 static const char* testData
[][2] = {
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" },
1312 doTest(formatter
, testData
, TRUE
);
1314 #if !UCONFIG_NO_COLLATION
1315 formatter
->setLenient(TRUE
);
1316 static const char* lpTestData
[][2] = {
1317 { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
1320 doLenientParseTest(formatter
, lpTestData
);
1327 IntlTestRBNF::TestThaiSpellout()
1329 UErrorCode status
= U_ZERO_ERROR
;
1330 RuleBasedNumberFormat
* formatter
1331 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("th"), status
);
1333 if (U_FAILURE(status
)) {
1334 errln("FAIL: could not construct formatter");
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" },
1347 doTest(formatter
, testData
, TRUE
);
1353 IntlTestRBNF::TestSwedishSpellout()
1355 UErrorCode status
= U_ZERO_ERROR
;
1356 RuleBasedNumberFormat
* formatter
1357 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("sv"), status
);
1359 if (U_FAILURE(status
)) {
1360 errln("FAIL: could not construct formatter");
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" },
1384 doTest(formatter
, testDataDefault
, TRUE
);
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" },
1395 formatter
->setDefaultRuleSet("%neutrum", status
);
1396 if (U_SUCCESS(status
)) {
1397 logln("testing neutrum rules");
1398 doTest(formatter
, testDataNeutrum
, TRUE
);
1401 errln("Can't test neutrum rules");
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" },
1416 formatter
->setDefaultRuleSet("%year", status
);
1417 if (U_SUCCESS(status
)) {
1418 logln("testing year rules");
1419 doTest(formatter
, testDataYear
, TRUE
);
1422 errln("Can't test year rules");
1430 IntlTestRBNF::TestSmallValues()
1432 UErrorCode status
= U_ZERO_ERROR
;
1433 RuleBasedNumberFormat
* formatter
1434 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("en_US"), status
);
1436 if (U_FAILURE(status
)) {
1437 errln("FAIL: could not construct formatter");
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" },
1474 doTest(formatter
, testDataDefault
, TRUE
);
1481 IntlTestRBNF::TestLocalizations(void)
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");
1487 UErrorCode status
= U_ZERO_ERROR
;
1489 RuleBasedNumberFormat
formatter(rules
, perror
, status
);
1490 if (U_FAILURE(status
)) {
1491 errln("FAIL: could not construct formatter");
1494 static const char* testData
[][2] = {
1496 { "5", "yah, some" },
1497 { "423", "plenty" },
1498 { "12345", "more'n you'll ever need" },
1501 doTest(&formatter
, testData
, FALSE
);
1505 UnicodeString
loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>");
1506 static const char* testData
[][2] = {
1510 { "12345", "tons" },
1513 RuleBasedNumberFormat
formatter0(rules
, loc
, perror
, status
);
1514 if (U_FAILURE(status
)) {
1515 errln("failed to build second formatter");
1517 doTest(&formatter0
, testData
, FALSE
);
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
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();
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
1558 int32_t goodLocsLen
= sizeof(goodLocs
)/sizeof(goodLocs
[0]);
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
1585 int32_t badLocsLen
= sizeof(badLocs
)/sizeof(badLocs
[0]);
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
]);
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
]);
1611 IntlTestRBNF::doTest(RuleBasedNumberFormat
* formatter
, const char* testData
[][2], UBool testParsing
)
1613 // man, error reporting would be easier with printf-style syntax for unicode string and formattable
1615 UErrorCode status
= U_ZERO_ERROR
;
1616 // NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
1617 NumberFormat
* decFmt
= new DecimalFormat("#,###.################", status
);
1618 if (U_FAILURE(status
)) {
1619 errln("FAIL: could not create NumberFormat");
1621 for (int i
= 0; testData
[i
][0]; ++i
) {
1622 const char* numString
= testData
[i
][0];
1623 const char* expectedWords
= testData
[i
][1];
1625 log("[%i] %s = ", i
, numString
);
1626 Formattable expectedNumber
;
1627 decFmt
->parse(numString
, expectedNumber
, status
);
1628 if (U_FAILURE(status
)) {
1629 errln("FAIL: decFmt could not parse %s", numString
);
1632 UnicodeString actualString
;
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
);
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
);
1652 logln(actualString
);
1654 Formattable parsedNumber
;
1655 formatter
->parse(actualString
, parsedNumber
, status
);
1656 if (U_FAILURE(status
)) {
1657 UnicodeString msg
= "FAIL: formatter could not parse ";
1658 msg
.append(actualString
);
1659 msg
.append(" status code: " );
1660 msg
.append(u_errorName(status
));
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
);
1685 IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat
* formatter
, const char* testData
[][2])
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");
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
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
);
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.
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
);
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
);
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
);
1746 IntlTestRBNF::TestRBNFDisabled() {
1747 errln("*** RBNF currently disabled on this platform ***\n");
1753 #endif /* #if !UCONFIG_NO_FORMATTING */