2 *******************************************************************************
3 * Copyright (C) 1996-2008, 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"
28 // import com.ibm.text.RuleBasedNumberFormat;
29 // import com.ibm.test.TestFmwk;
31 // import java.util.Locale;
32 // import java.text.NumberFormat;
34 // current macro not in icu1.8.1
35 #define TESTCASE(id,test) \
40 logln((UnicodeString)""); \
45 void IntlTestRBNF::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/)
47 if (exec
) logln("TestSuite RuleBasedNumberFormat");
50 TESTCASE(0, TestEnglishSpellout
);
51 TESTCASE(1, TestOrdinalAbbreviations
);
52 TESTCASE(2, TestDurations
);
53 TESTCASE(3, TestSpanishSpellout
);
54 TESTCASE(4, TestFrenchSpellout
);
55 TESTCASE(5, TestSwissFrenchSpellout
);
56 TESTCASE(6, TestItalianSpellout
);
57 TESTCASE(7, TestGermanSpellout
);
58 TESTCASE(8, TestThaiSpellout
);
60 TESTCASE(10, TestFractionalRuleSet
);
61 TESTCASE(11, TestSwedishSpellout
);
62 TESTCASE(12, TestBelgianFrenchSpellout
);
63 TESTCASE(13, TestSmallValues
);
64 TESTCASE(14, TestLocalizations
);
65 TESTCASE(15, TestAllLocales
);
66 TESTCASE(16, TestHebrewFraction
);
67 TESTCASE(17, TestPortugueseSpellout
);
68 TESTCASE(18, TestMultiplierSubstitution
);
70 TESTCASE(0, TestRBNFDisabled
);
80 void IntlTestRBNF::TestHebrewFraction() {
81 // this is the expected output for 123.45, with no '<' in it.
83 0x05de, 0x05d0, 0x05d4, 0x0020,
84 0x05e2, 0x05e9, 0x05e8, 0x05d9, 0x05dd, 0x0020,
85 0x05d5, 0x05e9, 0x05dc, 0x05d5, 0x05e9, 0x0020,
86 0x05e0, 0x05e7, 0x05d5, 0x05d3, 0x05d4, 0x0020,
87 0x05d0, 0x05e8, 0x05d1, 0x05e2, 0x05d9, 0x05dd, 0x0020,
88 0x05d5, 0x05d7, 0x05de, 0x05e9, 0x0000,
91 0x05DE, 0x05D0, 0x05D4, 0x0020,
92 0x05E2, 0x05E9, 0x05E8, 0x05D9, 0x05DD, 0x0020,
93 0x05D5, 0x05E9, 0x05DC, 0x05D5, 0x05E9, 0x0020,
94 0x05E0, 0x05E7, 0x05D5, 0x05D3, 0x05D4, 0x0020,
95 0x05D0, 0x05E4, 0x05E1, 0x0020,
96 0x05D0, 0x05E4, 0x05E1, 0x0020,
97 0x05D0, 0x05E8, 0x05D1, 0x05E2, 0x05D9, 0x05DD, 0x0020,
98 0x05D5, 0x05D7, 0x05DE, 0x05E9, 0x0000,
100 UErrorCode status
= U_ZERO_ERROR
;
101 RuleBasedNumberFormat
* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT
, "he_IL", status
);
102 UnicodeString result
;
103 Formattable parseResult
;
106 UnicodeString
expected(text1
);
107 formatter
->format(123.45, result
);
108 if (result
!= expected
) {
109 errln((UnicodeString
)"expected '" + TestUtility::hex(expected
) + "'\nbut got: '" + TestUtility::hex(result
) + "'");
111 formatter
->parse(result
, parseResult
, pp
);
112 if (parseResult
.getDouble() != 123.45) {
113 errln("expected 123.45 but got: %g", parseResult
.getDouble());
118 UnicodeString
expected(text2
);
120 formatter
->format(123.0045, result
);
121 if (result
!= expected
) {
122 errln((UnicodeString
)"expected '" + TestUtility::hex(expected
) + "'\nbut got: '" + TestUtility::hex(result
) + "'");
125 formatter
->parse(result
, parseResult
, pp
);
126 if (parseResult
.getDouble() != 123.0045) {
127 errln("expected 123.0045 but got: %g", parseResult
.getDouble());
135 IntlTestRBNF::TestAPI() {
136 // This test goes through the APIs that were not tested before.
137 // These tests are too small to have separate test classes/functions
139 UErrorCode status
= U_ZERO_ERROR
;
140 RuleBasedNumberFormat
* formatter
141 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
143 logln("RBNF API test starting");
146 logln("Testing Clone");
147 RuleBasedNumberFormat
* rbnfClone
= (RuleBasedNumberFormat
*)formatter
->clone();
148 if(rbnfClone
!= NULL
) {
149 if(!(*rbnfClone
== *formatter
)) {
150 errln("Clone should be semantically equivalent to the original!");
154 errln("Cloning failed!");
160 logln("Testing assignment operator");
161 RuleBasedNumberFormat
assignResult(URBNF_SPELLOUT
, Locale("es", "ES", ""), status
);
162 assignResult
= *formatter
;
163 if(!(assignResult
== *formatter
)) {
164 errln("Assignment result should be semantically equivalent to the original!");
168 // test rule constructor
170 logln("Testing rule constructor");
171 UResourceBundle
*en
= ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
"rbnf", "en", &status
);
172 if(U_FAILURE(status
)) {
173 errln("Unable to access resource bundle with data!");
176 const UChar
*spelloutRules
= ures_getStringByKey(en
, "SpelloutRules", &ruleLen
, &status
);
177 if(U_FAILURE(status
) || ruleLen
== 0 || spelloutRules
== NULL
) {
178 errln("Unable to access the rules string!");
181 RuleBasedNumberFormat
ruleCtorResult(spelloutRules
, Locale::getUS(), perror
, status
);
182 if(!(ruleCtorResult
== *formatter
)) {
183 errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
186 // Jitterbug 4452, for coverage
187 RuleBasedNumberFormat
nf(spelloutRules
, (UnicodeString
)"", Locale::getUS(), perror
, status
);
188 if(!(nf
== *formatter
)) {
189 errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
198 logln("Testing getRules function");
199 UnicodeString rules
= formatter
->getRules();
201 RuleBasedNumberFormat
fromRulesResult(rules
, Locale::getUS(), perror
, status
);
203 if(!(fromRulesResult
== *formatter
)) {
204 errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!");
210 logln("Testing copy constructor");
211 RuleBasedNumberFormat
copyCtorResult(*formatter
);
212 if(!(copyCtorResult
== *formatter
)) {
213 errln("Copy constructor result result should be semantically equivalent to the original!");
217 #if !UCONFIG_NO_COLLATION
218 // test ruleset names
220 logln("Testing getNumberOfRuleSetNames, getRuleSetName and format using rule set names");
221 int32_t noOfRuleSetNames
= formatter
->getNumberOfRuleSetNames();
222 if(noOfRuleSetNames
== 0) {
223 errln("Number of rule set names should be more than zero");
225 UnicodeString ruleSetName
;
227 int32_t intFormatNum
= 34567;
228 double doubleFormatNum
= 893411.234;
229 logln("number of rule set names is %i", noOfRuleSetNames
);
230 for(i
= 0; i
< noOfRuleSetNames
; i
++) {
231 FieldPosition pos1
, pos2
;
232 UnicodeString intFormatResult
, doubleFormatResult
;
233 Formattable intParseResult
, doubleParseResult
;
235 ruleSetName
= formatter
->getRuleSetName(i
);
236 log("Rule set name %i is ", i
);
238 logln(". Format results are: ");
239 intFormatResult
= formatter
->format(intFormatNum
, ruleSetName
, intFormatResult
, pos1
, status
);
240 doubleFormatResult
= formatter
->format(doubleFormatNum
, ruleSetName
, doubleFormatResult
, pos2
, status
);
241 if(U_FAILURE(status
)) {
242 errln("Format using a rule set failed");
245 logln(intFormatResult
);
246 logln(doubleFormatResult
);
247 formatter
->setLenient(TRUE
);
248 formatter
->parse(intFormatResult
, intParseResult
, status
);
249 formatter
->parse(doubleFormatResult
, doubleParseResult
, status
);
251 logln("Parse results for lenient = TRUE, %i, %f", intParseResult
.getLong(), doubleParseResult
.getDouble());
253 formatter
->setLenient(FALSE
);
254 formatter
->parse(intFormatResult
, intParseResult
, status
);
255 formatter
->parse(doubleFormatResult
, doubleParseResult
, status
);
257 logln("Parse results for lenient = FALSE, %i, %f", intParseResult
.getLong(), doubleParseResult
.getDouble());
259 if(U_FAILURE(status
)) {
260 errln("Error during parsing");
263 intFormatResult
= formatter
->format(intFormatNum
, "BLABLA", intFormatResult
, pos1
, status
);
264 if(U_SUCCESS(status
)) {
265 errln("Using invalid rule set name should have failed");
268 status
= U_ZERO_ERROR
;
269 doubleFormatResult
= formatter
->format(doubleFormatNum
, "TRUC", doubleFormatResult
, pos2
, status
);
270 if(U_SUCCESS(status
)) {
271 errln("Using invalid rule set name should have failed");
274 status
= U_ZERO_ERROR
;
276 status
= U_ZERO_ERROR
;
281 UnicodeString
expected("four point five","");
282 logln("Testing format(double)");
283 UnicodeString result
;
284 formatter
->format(4.5,result
);
285 if(result
!= expected
) {
286 errln("Formatted 4.5, expected " + expected
+ " got " + result
);
288 logln("Formatted 4.5, expected " + expected
+ " got " + result
);
292 formatter
->format((int32_t)4,result
);
293 if(result
!= expected
) {
294 errln("Formatted 4, expected " + expected
+ " got " + result
);
296 logln("Formatted 4, expected " + expected
+ " got " + result
);
301 formatter
->format((int64_t)4, result
, pos
, status
= U_ZERO_ERROR
);
302 if(result
!= expected
) {
303 errln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
305 logln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
308 //Jitterbug 4452, for coverage
311 formatter
->format((int64_t)4, formatter
->getRuleSetName(0), result
, pos2
, status
= U_ZERO_ERROR
);
312 if(result
!= expected
) {
313 errln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
315 logln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
319 logln("Cleaning up");
323 void IntlTestRBNF::TestFractionalRuleSet()
325 UnicodeString
fracRules(
327 // this rule formats the number if it's 1 or more. It formats
328 // the integral part using a DecimalFormat ("#,##0" puts
329 // thousands separators in the right places) and the fractional
330 // part using %%frac. If there is no fractional part, it
331 // just shows the integral part.
332 " x.0: <#,##0<[ >%%frac>];\n"
333 // this rule formats the number if it's between 0 and 1. It
334 // shows only the fractional part (0.5 shows up as "1/2," not
337 // the fraction rule set. This works the same way as the one in the
338 // preceding example: We multiply the fractional part of the number
339 // being formatted by each rule's base value and use the rule that
340 // produces the result closest to 0 (or the first rule that produces 0).
341 // Since we only provide rules for the numbers from 2 to 10, we know
342 // we'll get a fraction with a denominator between 2 and 10.
343 // "<0<" causes the numerator of the fraction to be formatted
357 int len
= fracRules
.length();
359 for (int i
= 0; i
< len
; ++i
) {
360 UChar ch
= fracRules
.charAt(i
);
362 change
= 2; // change ok
363 } else if (ch
== ':') {
364 change
= 1; // change, but once we hit a non-space char, don't change
365 } else if (ch
== ' ') {
367 fracRules
.setCharAt(i
, (UChar
)0x200e);
376 UErrorCode status
= U_ZERO_ERROR
;
378 RuleBasedNumberFormat
formatter(fracRules
, Locale::getEnglish(), perror
, status
);
379 if (U_FAILURE(status
)) {
380 errln("FAIL: could not construct formatter");
382 static const char* const testData
[][2] = {
395 { "3.125", "3 1/8" },
396 { "4.1428", "4 1/7" },
397 { "5.1667", "5 1/6" },
400 { "8.333", "8 1/3" },
405 { "1.2856", "1 2/7" },
408 doTest(&formatter
, testData
, FALSE
); // exact values aren't parsable from fractions
413 #define LLAssert(a) \
414 if (!(a)) errln("FAIL: " #a)
416 void IntlTestRBNF::TestLLongConstructors()
418 logln("Testing constructors");
420 // constant (shouldn't really be public)
421 LLAssert(llong(llong::kD32
).asDouble() == llong::kD32
);
423 // internal constructor (shouldn't really be public)
424 LLAssert(llong(0, 1).asDouble() == 1);
425 LLAssert(llong(1, 0).asDouble() == llong::kD32
);
426 LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1);
428 // public empty constructor
429 LLAssert(llong().asDouble() == 0);
431 // public int32_t constructor
432 LLAssert(llong((int32_t)0).asInt() == (int32_t)0);
433 LLAssert(llong((int32_t)1).asInt() == (int32_t)1);
434 LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1);
435 LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff);
436 LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1);
437 LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000);
439 // public int16_t constructor
440 LLAssert(llong((int16_t)0).asInt() == (int16_t)0);
441 LLAssert(llong((int16_t)1).asInt() == (int16_t)1);
442 LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1);
443 LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff);
444 LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff);
445 LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000);
447 // public int8_t constructor
448 LLAssert(llong((int8_t)0).asInt() == (int8_t)0);
449 LLAssert(llong((int8_t)1).asInt() == (int8_t)1);
450 LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1);
451 LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f);
452 LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff);
453 LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80);
455 // public uint16_t constructor
456 LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0);
457 LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1);
458 LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1);
459 LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff);
460 LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff);
461 LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000);
463 // public uint32_t constructor
464 LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0);
465 LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1);
466 LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1);
467 LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff);
468 LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1);
469 LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
471 // public double constructor
472 LLAssert(llong((double)0).asDouble() == (double)0);
473 LLAssert(llong((double)1).asDouble() == (double)1);
474 LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff);
475 LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000);
476 LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001);
478 // can't access uprv_maxmantissa, so fake it
479 double maxmantissa
= (llong((int32_t)1) << 40).asDouble();
480 LLAssert(llong(maxmantissa
).asDouble() == maxmantissa
);
481 LLAssert(llong(-maxmantissa
).asDouble() == -maxmantissa
);
484 LLAssert(llong(llong(0, 1)).asDouble() == 1);
485 LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32
);
486 LLAssert(llong(llong(-1, (uint32_t)-1)).asDouble() == -1);
488 // asInt - test unsigned to signed narrowing conversion
489 LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff);
490 LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000);
492 // asUInt - test signed to unsigned narrowing conversion
493 LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1);
494 LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
496 // asDouble already tested
500 void IntlTestRBNF::TestLLongSimpleOperators()
502 logln("Testing simple operators");
505 LLAssert(llong() == llong(0, 0));
506 LLAssert(llong(1,0) == llong(1, 0));
507 LLAssert(llong(0,1) == llong(0, 1));
510 LLAssert(llong(1,0) != llong(1,1));
511 LLAssert(llong(0,1) != llong(1,1));
512 LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff));
515 LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff)));
518 LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1)));
521 LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff)));
522 LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1)));
525 LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1)));
526 LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1)));
529 LLAssert(llong(1, 1) > llong(1, 0));
530 LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff));
531 LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0));
532 LLAssert(llong(1, 0) > llong(0, 0x7fffffff));
533 LLAssert(llong(1, 0) > llong(0, 0xffffffff));
534 LLAssert(llong(0, 0) > llong(0x80000000, 1));
537 LLAssert(llong(1, 0) < llong(1, 1));
538 LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000));
539 LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1));
540 LLAssert(llong(0, 0x7fffffff) < llong(1, 0));
541 LLAssert(llong(0, 0xffffffff) < llong(1, 0));
542 LLAssert(llong(0x80000000, 1) < llong(0, 0));
545 LLAssert(llong(1, 1) >= llong(1, 0));
546 LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff));
547 LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0));
548 LLAssert(llong(1, 0) >= llong(0, 0x7fffffff));
549 LLAssert(llong(1, 0) >= llong(0, 0xffffffff));
550 LLAssert(llong(0, 0) >= llong(0x80000000, 1));
551 LLAssert(llong() >= llong(0, 0));
552 LLAssert(llong(1,0) >= llong(1, 0));
553 LLAssert(llong(0,1) >= llong(0, 1));
556 LLAssert(llong(1, 0) <= llong(1, 1));
557 LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000));
558 LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1));
559 LLAssert(llong(0, 0x7fffffff) <= llong(1, 0));
560 LLAssert(llong(0, 0xffffffff) <= llong(1, 0));
561 LLAssert(llong(0x80000000, 1) <= llong(0, 0));
562 LLAssert(llong() <= llong(0, 0));
563 LLAssert(llong(1,0) <= llong(1, 0));
564 LLAssert(llong(0,1) <= llong(0, 1));
567 LLAssert(llong() == (int32_t)0);
568 LLAssert(llong(0,1) == (int32_t)1);
571 LLAssert(llong(1,0) != (int32_t)0);
572 LLAssert(llong(0,1) != (int32_t)2);
573 LLAssert(llong(0,0xffffffff) != (int32_t)-1);
575 llong
negOne(0xffffffff, 0xffffffff);
578 LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff);
579 LLAssert(negOne
> (int32_t)-2);
580 LLAssert(llong(1, 0) > (int32_t)0x7fffffff);
581 LLAssert(llong(0, 0) > (int32_t)-1);
584 LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff);
585 LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1);
588 LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff);
589 LLAssert(negOne
>= (int32_t)-2);
590 LLAssert(llong(1, 0) >= (int32_t)0x7fffffff);
591 LLAssert(llong(0, 0) >= (int32_t)-1);
592 LLAssert(llong() >= (int32_t)0);
593 LLAssert(llong(0,1) >= (int32_t)1);
596 LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff);
597 LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1);
598 LLAssert(llong() <= (int32_t)0);
599 LLAssert(llong(0,1) <= (int32_t)1);
602 LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1);
605 LLAssert((llong(1, 1) <<= 0) == llong(1, 1));
606 LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000));
607 LLAssert((llong(1, 1) <<= 32) == llong(1, 0));
608 LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0));
609 LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used
610 LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used
613 LLAssert((llong((int32_t)1) << 5).asUInt() == 32);
615 // operator >>= (sign extended)
616 LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc));
617 LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde));
618 LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff));
619 LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000));
620 LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
621 LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used
623 // operator >> sign extended)
624 LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde));
626 // ushr (right shift without sign extension)
627 LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc));
628 LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde));
629 LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1));
630 LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000));
631 LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
632 LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used
635 LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
638 LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
641 LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
644 LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
647 LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
650 LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
653 LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa));
656 LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
659 LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
662 LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
664 // operator&=(uint32)
665 LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
667 // operator|=(uint32)
668 LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
670 // operator^=(uint32)
671 LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
674 LLAssert(llong(1, 0) == ++llong(0,0xffffffff));
677 LLAssert(llong(0,0xffffffff) == --llong(1, 0));
681 llong
n(0, 0xffffffff);
682 LLAssert(llong(0, 0xffffffff) == n
++);
683 LLAssert(llong(1, 0) == n
);
689 LLAssert(llong(1, 0) == n
--);
690 LLAssert(llong(0, 0xffffffff) == n
);
694 LLAssert(llong(0, 0) == -llong(0, 0));
695 LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1));
696 LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff));
697 LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1));
698 LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow
703 LLAssert((n
-= llong(0, 1)) == llong(0xffffffff, 0xffffffff));
704 LLAssert(n
== llong(0xffffffff, 0xffffffff));
707 LLAssert((n
-= llong(0, 1)) == llong(0, 0xffffffff));
708 LLAssert(n
== llong(0, 0xffffffff));
714 LLAssert((n
- llong(0, 1)) == llong(0xffffffff, 0xffffffff));
715 LLAssert(n
== llong(0, 0));
718 LLAssert((n
- llong(0, 1)) == llong(0, 0xffffffff));
719 LLAssert(n
== llong(1, 0));
724 llong
n(0xffffffff, 0xffffffff);
725 LLAssert((n
+= llong(0, 1)) == llong(0, 0));
726 LLAssert(n
== llong(0, 0));
728 n
= llong(0, 0xffffffff);
729 LLAssert((n
+= llong(0, 1)) == llong(1, 0));
730 LLAssert(n
== llong(1, 0));
735 llong
n(0xffffffff, 0xffffffff);
736 LLAssert((n
+ llong(0, 1)) == llong(0, 0));
737 LLAssert(n
== llong(0xffffffff, 0xffffffff));
739 n
= llong(0, 0xffffffff);
740 LLAssert((n
+ llong(0, 1)) == llong(1, 0));
741 LLAssert(n
== llong(0, 0xffffffff));
746 void IntlTestRBNF::TestLLong()
748 logln("Starting TestLLong");
750 TestLLongConstructors();
752 TestLLongSimpleOperators();
754 logln("Testing operator*=, operator*");
756 // operator*=, operator*
757 // small and large values, positive, &NEGative, zero
758 // also test commutivity
761 const llong
ONE(0, 1);
762 const llong
NEG_ONE((int32_t)-1);
763 const llong
THREE(0, 3);
764 const llong
NEG_THREE((int32_t)-3);
765 const llong
TWO_TO_16(0, 0x10000);
766 const llong NEG_TWO_TO_16
= -TWO_TO_16
;
767 const llong
TWO_TO_32(1, 0);
768 const llong NEG_TWO_TO_32
= -TWO_TO_32
;
770 const llong
NINE(0, 9);
771 const llong NEG_NINE
= -NINE
;
773 const llong
TWO_TO_16X3(0, 0x00030000);
774 const llong NEG_TWO_TO_16X3
= -TWO_TO_16X3
;
776 const llong
TWO_TO_32X3(3, 0);
777 const llong NEG_TWO_TO_32X3
= -TWO_TO_32X3
;
779 const llong
TWO_TO_48(0x10000, 0);
780 const llong NEG_TWO_TO_48
= -TWO_TO_48
;
782 const int32_t VALUE_WIDTH
= 9;
783 const llong
* values
[VALUE_WIDTH
] = {
784 &ZERO
, &ONE
, &NEG_ONE
, &THREE
, &NEG_THREE
, &TWO_TO_16
, &NEG_TWO_TO_16
, &TWO_TO_32
, &NEG_TWO_TO_32
787 const llong
* answers
[VALUE_WIDTH
*VALUE_WIDTH
] = {
788 &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
,
789 &ZERO
, &ONE
, &NEG_ONE
, &THREE
, &NEG_THREE
, &TWO_TO_16
, &NEG_TWO_TO_16
, &TWO_TO_32
, &NEG_TWO_TO_32
,
790 &ZERO
, &NEG_ONE
, &ONE
, &NEG_THREE
, &THREE
, &NEG_TWO_TO_16
, &TWO_TO_16
, &NEG_TWO_TO_32
, &TWO_TO_32
,
791 &ZERO
, &THREE
, &NEG_THREE
, &NINE
, &NEG_NINE
, &TWO_TO_16X3
, &NEG_TWO_TO_16X3
, &TWO_TO_32X3
, &NEG_TWO_TO_32X3
,
792 &ZERO
, &NEG_THREE
, &THREE
, &NEG_NINE
, &NINE
, &NEG_TWO_TO_16X3
, &TWO_TO_16X3
, &NEG_TWO_TO_32X3
, &TWO_TO_32X3
,
793 &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
,
794 &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
,
795 &ZERO
, &TWO_TO_32
, &NEG_TWO_TO_32
, &TWO_TO_32X3
, &NEG_TWO_TO_32X3
, &TWO_TO_48
, &NEG_TWO_TO_48
, &ZERO
, &ZERO
,
796 &ZERO
, &NEG_TWO_TO_32
, &TWO_TO_32
, &NEG_TWO_TO_32X3
, &TWO_TO_32X3
, &NEG_TWO_TO_48
, &TWO_TO_48
, &ZERO
, &ZERO
799 for (int i
= 0; i
< VALUE_WIDTH
; ++i
) {
800 for (int j
= 0; j
< VALUE_WIDTH
; ++j
) {
801 llong lhs
= *values
[i
];
802 llong rhs
= *values
[j
];
803 llong ans
= *answers
[i
*VALUE_WIDTH
+ j
];
807 LLAssert((n
*= rhs
) == ans
);
811 LLAssert((n
* rhs
) == ans
);
817 logln("Testing operator/=, operator/");
818 // operator/=, operator/
819 // test num = 0, div = 0, pos/neg, > 2^32, div > num
822 const llong
ONE(0, 1);
823 const llong NEG_ONE
= -ONE
;
824 const llong
MAX(0x7fffffff, 0xffffffff);
825 const llong
MIN(0x80000000, 0);
826 const llong
TWO(0, 2);
827 const llong NEG_TWO
= -TWO
;
828 const llong
FIVE(0, 5);
829 const llong NEG_FIVE
= -FIVE
;
830 const llong
TWO_TO_32(1, 0);
831 const llong NEG_TWO_TO_32
= -TWO_TO_32
;
832 const llong TWO_TO_32d5
= llong(TWO_TO_32
.asDouble()/5.0);
833 const llong NEG_TWO_TO_32d5
= -TWO_TO_32d5
;
834 const llong TWO_TO_32X5
= TWO_TO_32
* FIVE
;
835 const llong NEG_TWO_TO_32X5
= -TWO_TO_32X5
;
837 const llong
* tuples
[] = { // lhs, rhs, ans
840 &NEG_ONE
, &ZERO
, &MIN
,
842 &ONE
, &NEG_ONE
, &NEG_ONE
,
843 &NEG_ONE
, &ONE
, &NEG_ONE
,
844 &NEG_ONE
, &NEG_ONE
, &ONE
,
846 &FIVE
, &NEG_TWO
, &NEG_TWO
,
847 &NEG_FIVE
, &TWO
, &NEG_TWO
,
848 &NEG_FIVE
, &NEG_TWO
, &TWO
,
850 &TWO
, &NEG_FIVE
, &ZERO
,
851 &NEG_TWO
, &FIVE
, &ZERO
,
852 &NEG_TWO
, &NEG_FIVE
, &ZERO
,
853 &TWO_TO_32
, &TWO_TO_32
, &ONE
,
854 &TWO_TO_32
, &NEG_TWO_TO_32
, &NEG_ONE
,
855 &NEG_TWO_TO_32
, &TWO_TO_32
, &NEG_ONE
,
856 &NEG_TWO_TO_32
, &NEG_TWO_TO_32
, &ONE
,
857 &TWO_TO_32
, &FIVE
, &TWO_TO_32d5
,
858 &TWO_TO_32
, &NEG_FIVE
, &NEG_TWO_TO_32d5
,
859 &NEG_TWO_TO_32
, &FIVE
, &NEG_TWO_TO_32d5
,
860 &NEG_TWO_TO_32
, &NEG_FIVE
, &TWO_TO_32d5
,
861 &TWO_TO_32X5
, &FIVE
, &TWO_TO_32
,
862 &TWO_TO_32X5
, &NEG_FIVE
, &NEG_TWO_TO_32
,
863 &NEG_TWO_TO_32X5
, &FIVE
, &NEG_TWO_TO_32
,
864 &NEG_TWO_TO_32X5
, &NEG_FIVE
, &TWO_TO_32
,
865 &TWO_TO_32X5
, &TWO_TO_32
, &FIVE
,
866 &TWO_TO_32X5
, &NEG_TWO_TO_32
, &NEG_FIVE
,
867 &NEG_TWO_TO_32X5
, &NEG_TWO_TO_32
, &FIVE
,
868 &NEG_TWO_TO_32X5
, &TWO_TO_32
, &NEG_FIVE
870 const int TUPLE_WIDTH
= 3;
871 const int TUPLE_COUNT
= (int)(sizeof(tuples
)/sizeof(tuples
[0]))/TUPLE_WIDTH
;
872 for (int i
= 0; i
< TUPLE_COUNT
; ++i
) {
873 const llong lhs
= *tuples
[i
*TUPLE_WIDTH
+0];
874 const llong rhs
= *tuples
[i
*TUPLE_WIDTH
+1];
875 const llong ans
= *tuples
[i
*TUPLE_WIDTH
+2];
878 if (!((n
/= rhs
) == ans
)) {
879 errln("fail: (n /= rhs) == ans");
884 LLAssert((n
/ rhs
) == ans
);
889 logln("Testing operator%%=, operator%%");
890 //operator%=, operator%
893 const llong
ONE(0, 1);
894 const llong
TWO(0, 2);
895 const llong
THREE(0,3);
896 const llong
FOUR(0, 4);
897 const llong
FIVE(0, 5);
898 const llong
SIX(0, 6);
900 const llong NEG_ONE
= -ONE
;
901 const llong NEG_TWO
= -TWO
;
902 const llong NEG_THREE
= -THREE
;
903 const llong NEG_FOUR
= -FOUR
;
904 const llong NEG_FIVE
= -FIVE
;
905 const llong NEG_SIX
= -SIX
;
907 const llong
NINETY_NINE(0, 99);
908 const llong
HUNDRED(0, 100);
909 const llong
HUNDRED_ONE(0, 101);
911 const llong
BIG(0x12345678, 0x9abcdef0);
912 const llong
BIG_FIVE(BIG
* FIVE
);
913 const llong BIG_FIVEm1
= BIG_FIVE
- ONE
;
914 const llong BIG_FIVEp1
= BIG_FIVE
+ ONE
;
916 const llong
* tuples
[] = {
920 &THREE
, &FIVE
, &THREE
,
924 &ZERO
, &NEG_FIVE
, &ZERO
,
925 &ONE
, &NEG_FIVE
, &ONE
,
926 &TWO
, &NEG_FIVE
, &TWO
,
927 &THREE
, &NEG_FIVE
, &THREE
,
928 &FOUR
, &NEG_FIVE
, &FOUR
,
929 &FIVE
, &NEG_FIVE
, &ZERO
,
930 &SIX
, &NEG_FIVE
, &ONE
,
931 &NEG_ONE
, &FIVE
, &NEG_ONE
,
932 &NEG_TWO
, &FIVE
, &NEG_TWO
,
933 &NEG_THREE
, &FIVE
, &NEG_THREE
,
934 &NEG_FOUR
, &FIVE
, &NEG_FOUR
,
935 &NEG_FIVE
, &FIVE
, &ZERO
,
936 &NEG_SIX
, &FIVE
, &NEG_ONE
,
937 &NEG_ONE
, &NEG_FIVE
, &NEG_ONE
,
938 &NEG_TWO
, &NEG_FIVE
, &NEG_TWO
,
939 &NEG_THREE
, &NEG_FIVE
, &NEG_THREE
,
940 &NEG_FOUR
, &NEG_FIVE
, &NEG_FOUR
,
941 &NEG_FIVE
, &NEG_FIVE
, &ZERO
,
942 &NEG_SIX
, &NEG_FIVE
, &NEG_ONE
,
943 &NINETY_NINE
, &FIVE
, &FOUR
,
944 &HUNDRED
, &FIVE
, &ZERO
,
945 &HUNDRED_ONE
, &FIVE
, &ONE
,
946 &BIG_FIVEm1
, &FIVE
, &FOUR
,
947 &BIG_FIVE
, &FIVE
, &ZERO
,
948 &BIG_FIVEp1
, &FIVE
, &ONE
950 const int TUPLE_WIDTH
= 3;
951 const int TUPLE_COUNT
= (int)(sizeof(tuples
)/sizeof(tuples
[0]))/TUPLE_WIDTH
;
952 for (int i
= 0; i
< TUPLE_COUNT
; ++i
) {
953 const llong lhs
= *tuples
[i
*TUPLE_WIDTH
+0];
954 const llong rhs
= *tuples
[i
*TUPLE_WIDTH
+1];
955 const llong ans
= *tuples
[i
*TUPLE_WIDTH
+2];
958 if (!((n
%= rhs
) == ans
)) {
959 errln("fail: (n %= rhs) == ans");
964 LLAssert((n
% rhs
) == ans
);
969 logln("Testing pow");
971 LLAssert(llong(0, 0).pow(0) == llong(0, 0));
972 LLAssert(llong(0, 0).pow(2) == llong(0, 0));
973 LLAssert(llong(0, 2).pow(0) == llong(0, 1));
974 LLAssert(llong(0, 2).pow(2) == llong(0, 4));
975 LLAssert(llong(0, 2).pow(32) == llong(1, 0));
976 LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5));
980 const llong
n(0xffffffff,0xffffffff);
981 LLAssert(n
.abs() == llong(0, 1));
985 logln("Testing atoll");
987 const char empty
[] = "";
988 const char zero
[] = "0";
989 const char neg_one
[] = "-1";
990 const char neg_12345
[] = "-12345";
991 const char big1
[] = "123456789abcdef0";
992 const char big2
[] = "fFfFfFfFfFfFfFfF";
993 LLAssert(llong::atoll(empty
) == llong(0, 0));
994 LLAssert(llong::atoll(zero
) == llong(0, 0));
995 LLAssert(llong::atoll(neg_one
) == llong(0xffffffff, 0xffffffff));
996 LLAssert(llong::atoll(neg_12345
) == -llong(0, 12345));
997 LLAssert(llong::atoll(big1
, 16) == llong(0x12345678, 0x9abcdef0));
998 LLAssert(llong::atoll(big2
, 16) == llong(0xffffffff, 0xffffffff));
1002 const UChar uempty
[] = { 0 };
1003 const UChar uzero
[] = { 0x30, 0 };
1004 const UChar uneg_one
[] = { 0x2d, 0x31, 0 };
1005 const UChar uneg_12345
[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 };
1006 const UChar ubig1
[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 };
1007 const UChar ubig2
[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 };
1008 LLAssert(llong::utoll(uempty
) == llong(0, 0));
1009 LLAssert(llong::utoll(uzero
) == llong(0, 0));
1010 LLAssert(llong::utoll(uneg_one
) == llong(0xffffffff, 0xffffffff));
1011 LLAssert(llong::utoll(uneg_12345
) == -llong(0, 12345));
1012 LLAssert(llong::utoll(ubig1
, 16) == llong(0x12345678, 0x9abcdef0));
1013 LLAssert(llong::utoll(ubig2
, 16) == llong(0xffffffff, 0xffffffff));
1016 logln("Testing lltoa");
1019 char buf
[64]; // ascii
1020 LLAssert((llong(0, 0).lltoa(buf
, (uint32_t)sizeof(buf
)) == 1) && (strcmp(buf
, zero
) == 0));
1021 LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf
, (uint32_t)sizeof(buf
)) == 2) && (strcmp(buf
, neg_one
) == 0));
1022 LLAssert(((-llong(0, 12345)).lltoa(buf
, (uint32_t)sizeof(buf
)) == 6) && (strcmp(buf
, neg_12345
) == 0));
1023 LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf
, (uint32_t)sizeof(buf
), 16) == 16) && (strcmp(buf
, big1
) == 0));
1027 logln("Testing u_lltoa");
1031 LLAssert((llong(0, 0).lltou(buf
, (uint32_t)sizeof(buf
)) == 1) && (u_strcmp(buf
, uzero
) == 0));
1032 LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf
, (uint32_t)sizeof(buf
)) == 2) && (u_strcmp(buf
, uneg_one
) == 0));
1033 LLAssert(((-llong(0, 12345)).lltou(buf
, (uint32_t)sizeof(buf
)) == 6) && (u_strcmp(buf
, uneg_12345
) == 0));
1034 LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf
, (uint32_t)sizeof(buf
), 16) == 16) && (u_strcmp(buf
, ubig1
) == 0));
1042 IntlTestRBNF::TestEnglishSpellout()
1044 UErrorCode status
= U_ZERO_ERROR
;
1045 RuleBasedNumberFormat
* formatter
1046 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
1048 if (U_FAILURE(status
)) {
1049 errln("FAIL: could not construct formatter");
1051 static const char* const testData
[][2] = {
1054 { "15", "fifteen" },
1056 { "23", "twenty-three" },
1057 { "73", "seventy-three" },
1058 { "88", "eighty-eight" },
1059 { "100", "one hundred" },
1060 { "106", "one hundred and six" },
1061 { "127", "one hundred and twenty-seven" },
1062 { "200", "two hundred" },
1063 { "579", "five hundred and seventy-nine" },
1064 { "1,000", "one thousand" },
1065 { "2,000", "two thousand" },
1066 { "3,004", "three thousand and four" },
1067 { "4,567", "four thousand five hundred and sixty-seven" },
1068 { "15,943", "fifteen thousand nine hundred and forty-three" },
1069 { "2,345,678", "two million, three hundred and forty-five thousand, six hundred and seventy-eight" },
1070 { "-36", "minus thirty-six" },
1071 { "234.567", "two hundred and thirty-four point five six seven" },
1075 doTest(formatter
, testData
, TRUE
);
1077 #if !UCONFIG_NO_COLLATION
1078 formatter
->setLenient(TRUE
);
1079 static const char* lpTestData
[][2] = {
1080 { "fifty-7", "57" },
1081 { " fifty-7", "57" },
1082 { " fifty-7", "57" },
1083 { "2 thousand six HUNDRED fifty-7", "2,657" },
1084 { "fifteen hundred and zero", "1,500" },
1085 { "FOurhundred thiRTY six", "436" },
1088 doLenientParseTest(formatter
, lpTestData
);
1095 IntlTestRBNF::TestOrdinalAbbreviations()
1097 UErrorCode status
= U_ZERO_ERROR
;
1098 RuleBasedNumberFormat
* formatter
1099 = new RuleBasedNumberFormat(URBNF_ORDINAL
, Locale::getUS(), status
);
1101 if (U_FAILURE(status
)) {
1102 errln("FAIL: could not construct formatter");
1104 static const char* const testData
[][2] = {
1121 { "12,345", "12,345th" },
1125 doTest(formatter
, testData
, FALSE
);
1131 IntlTestRBNF::TestDurations()
1133 UErrorCode status
= U_ZERO_ERROR
;
1134 RuleBasedNumberFormat
* formatter
1135 = new RuleBasedNumberFormat(URBNF_DURATION
, Locale::getUS(), status
);
1137 if (U_FAILURE(status
)) {
1138 errln("FAIL: could not construct formatter");
1140 static const char* const testData
[][2] = {
1141 { "3,600", "1:00:00" }, //move me and I fail
1144 { "24", "24 sec." },
1149 // { "3,600", "1:00:00" },
1150 { "3,740", "1:02:20" },
1151 { "10,293", "2:51:33" },
1155 doTest(formatter
, testData
, TRUE
);
1157 #if !UCONFIG_NO_COLLATION
1158 formatter
->setLenient(TRUE
);
1159 static const char* lpTestData
[][2] = {
1160 { "2-51-33", "10,293" },
1163 doLenientParseTest(formatter
, lpTestData
);
1170 IntlTestRBNF::TestSpanishSpellout()
1172 UErrorCode status
= U_ZERO_ERROR
;
1173 RuleBasedNumberFormat
* formatter
1174 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("es", "ES", ""), status
);
1176 if (U_FAILURE(status
)) {
1177 errln("FAIL: could not construct formatter");
1179 static const char* const testData
[][2] = {
1182 { "16", "diecis\\u00e9is" },
1184 { "24", "veinticuatro" },
1185 { "26", "veintis\\u00e9is" },
1186 { "73", "setenta y tres" },
1187 { "88", "ochenta y ocho" },
1189 { "106", "ciento seis" },
1190 { "127", "ciento veintisiete" },
1191 { "200", "doscientos" },
1192 { "579", "quinientos setenta y nueve" },
1194 { "2,000", "dos mil" },
1195 { "3,004", "tres mil cuatro" },
1196 { "4,567", "cuatro mil quinientos sesenta y siete" },
1197 { "15,943", "quince mil novecientos cuarenta y tres" },
1198 { "2,345,678", "dos mill\\u00f3n trescientos cuarenta y cinco mil seiscientos setenta y ocho"},
1199 { "-36", "menos treinta y seis" },
1200 { "234.567", "doscientos treinta y cuatro punto cinco seis siete" },
1204 doTest(formatter
, testData
, TRUE
);
1210 IntlTestRBNF::TestFrenchSpellout()
1212 UErrorCode status
= U_ZERO_ERROR
;
1213 RuleBasedNumberFormat
* formatter
1214 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getFrance(), status
);
1216 if (U_FAILURE(status
)) {
1217 errln("FAIL: could not construct formatter");
1219 static const char* const testData
[][2] = {
1223 { "21", "vingt-et-un" },
1224 { "23", "vingt-trois" },
1225 { "62", "soixante-deux" },
1226 { "70", "soixante-dix" },
1227 { "71", "soixante et onze" },
1228 { "73", "soixante-treize" },
1229 { "80", "quatre-vingts" },
1230 { "88", "quatre-vingt-huit" },
1232 { "106", "cent six" },
1233 { "127", "cent vingt-sept" },
1234 { "200", "deux cents" },
1235 { "579", "cinq cents soixante-dix-neuf" },
1236 { "1,000", "mille" },
1237 { "1,123", "onze cents vingt-trois" },
1238 { "1,594", "mille cinq cents quatre-vingt-quatorze" },
1239 { "2,000", "deux mille" },
1240 { "3,004", "trois mille quatre" },
1241 { "4,567", "quatre mille cinq cents soixante-sept" },
1242 { "15,943", "quinze mille neuf cents quarante-trois" },
1243 { "2,345,678", "deux million trois cents quarante-cinq mille six cents soixante-dix-huit" },
1244 { "-36", "moins trente-six" },
1245 { "234.567", "deux cents trente-quatre virgule cinq six sept" },
1249 doTest(formatter
, testData
, TRUE
);
1251 #if !UCONFIG_NO_COLLATION
1252 formatter
->setLenient(TRUE
);
1253 static const char* lpTestData
[][2] = {
1254 { "trente-un", "31" },
1255 { "un cents quatre vingt dix huit", "198" },
1258 doLenientParseTest(formatter
, lpTestData
);
1264 static const char* const swissFrenchTestData
[][2] = {
1268 { "21", "vingt-et-un" },
1269 { "23", "vingt-trois" },
1270 { "62", "soixante-deux" },
1271 { "70", "septante" },
1272 { "71", "septante-et-un" },
1273 { "73", "septante-trois" },
1274 { "80", "huitante" },
1275 { "88", "huitante-huit" },
1277 { "106", "cent six" },
1278 { "127", "cent vingt-sept" },
1279 { "200", "deux cents" },
1280 { "579", "cinq cents septante-neuf" },
1281 { "1,000", "mille" },
1282 { "1,123", "onze cents vingt-trois" },
1283 { "1,594", "mille cinq cents nonante-quatre" },
1284 { "2,000", "deux mille" },
1285 { "3,004", "trois mille quatre" },
1286 { "4,567", "quatre mille cinq cents soixante-sept" },
1287 { "15,943", "quinze mille neuf cents quarante-trois" },
1288 { "2,345,678", "deux million trois cents quarante-cinq mille six cents septante-huit" },
1289 { "-36", "moins trente-six" },
1290 { "234.567", "deux cents trente-quatre virgule cinq six sept" },
1295 IntlTestRBNF::TestSwissFrenchSpellout()
1297 UErrorCode status
= U_ZERO_ERROR
;
1298 RuleBasedNumberFormat
* formatter
1299 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "CH", ""), status
);
1301 if (U_FAILURE(status
)) {
1302 errln("FAIL: could not construct formatter");
1304 doTest(formatter
, swissFrenchTestData
, TRUE
);
1310 IntlTestRBNF::TestBelgianFrenchSpellout()
1312 UErrorCode status
= U_ZERO_ERROR
;
1313 RuleBasedNumberFormat
* formatter
1314 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "BE", ""), status
);
1316 if (U_FAILURE(status
)) {
1317 errln("rbnf status: 0x%x (%s)\n", status
, u_errorName(status
));
1318 errln("FAIL: could not construct formatter");
1320 // Belgian french should match Swiss french.
1321 doTest(formatter
, swissFrenchTestData
, TRUE
);
1327 IntlTestRBNF::TestItalianSpellout()
1329 UErrorCode status
= U_ZERO_ERROR
;
1330 RuleBasedNumberFormat
* formatter
1331 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getItalian(), status
);
1333 if (U_FAILURE(status
)) {
1334 errln("FAIL: could not construct formatter");
1336 static const char* const testData
[][2] = {
1338 { "15", "quindici" },
1340 { "23", "ventitr\\u00E9" },
1341 { "73", "settantatr\\u00E9" },
1342 { "88", "ottantotto" },
1344 { "106", "centosei" },
1345 { "108", "centotto" },
1346 { "127", "centoventisette" },
1347 { "181", "centottantuno" },
1348 { "200", "duecento" },
1349 { "579", "cinquecentosettantanove" },
1350 { "1,000", "mille" },
1351 { "2,000", "duemila" },
1352 { "3,004", "tremilaquattro" },
1353 { "4,567", "quattromilacinquecentosessantasette" },
1354 { "15,943", "quindicimilanovecentoquarantatr\\u00E9" },
1355 { "-36", "meno trentasei" },
1356 { "234.567", "duecentotrentaquattro virgola cinque sei sette" },
1360 doTest(formatter
, testData
, TRUE
);
1366 IntlTestRBNF::TestPortugueseSpellout()
1368 UErrorCode status
= U_ZERO_ERROR
;
1369 RuleBasedNumberFormat
* formatter
1370 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("pt","BR",""), status
);
1372 if (U_FAILURE(status
)) {
1373 errln("FAIL: could not construct formatter");
1375 static const char* const testData
[][2] = {
1379 { "23", "vinte e tr\\u00EAs" },
1380 { "73", "setenta e tr\\u00EAs" },
1381 { "88", "oitenta e oito" },
1383 { "106", "cento e seis" },
1384 { "108", "cento e oito" },
1385 { "127", "cento e vinte e sete" },
1386 { "181", "cento e oitenta e um" },
1387 { "200", "duzcentos" },
1388 { "579", "quinhentos e setenta e nove" },
1390 { "2,000", "dois mil" },
1391 { "3,004", "tr\\u00EAs mil e quatro" },
1392 { "4,567", "quatro mil quinhentos e sessenta e sete" },
1393 { "15,943", "quinze mil novecentos e quarenta e tr\\u00EAs" },
1394 { "-36", "menos trinta e seis" },
1395 { "234.567", "duzcentos e trinta e quatro ponto cinco seis sete" },
1399 doTest(formatter
, testData
, TRUE
);
1404 IntlTestRBNF::TestGermanSpellout()
1406 UErrorCode status
= U_ZERO_ERROR
;
1407 RuleBasedNumberFormat
* formatter
1408 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getGermany(), status
);
1410 if (U_FAILURE(status
)) {
1411 errln("FAIL: could not construct formatter");
1413 static const char* const testData
[][2] = {
1415 { "15", "f\\u00fcnfzehn" },
1416 { "20", "zwanzig" },
1417 { "23", "dreiundzwanzig" },
1418 { "73", "dreiundsiebzig" },
1419 { "88", "achtundachtzig" },
1420 { "100", "hundert" },
1421 { "106", "hundertsechs" },
1422 { "127", "hundertsiebenundzwanzig" },
1423 { "200", "zweihundert" },
1424 { "579", "f\\u00fcnfhundertneunundsiebzig" },
1425 { "1,000", "tausend" },
1426 { "2,000", "zweitausend" },
1427 { "3,004", "dreitausendvier" },
1428 { "4,567", "viertausendf\\u00fcnfhundertsiebenundsechzig" },
1429 { "15,943", "f\\u00fcnfzehntausendneunhundertdreiundvierzig" },
1430 { "2,345,678", "zwei Millionen dreihundertf\\u00fcnfundvierzigtausendsechshundertachtundsiebzig" },
1434 doTest(formatter
, testData
, TRUE
);
1436 #if !UCONFIG_NO_COLLATION
1437 formatter
->setLenient(TRUE
);
1438 static const char* lpTestData
[][2] = {
1439 { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
1442 doLenientParseTest(formatter
, lpTestData
);
1449 IntlTestRBNF::TestThaiSpellout()
1451 UErrorCode status
= U_ZERO_ERROR
;
1452 RuleBasedNumberFormat
* formatter
1453 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("th"), status
);
1455 if (U_FAILURE(status
)) {
1456 errln("FAIL: could not construct formatter");
1458 static const char* const testData
[][2] = {
1459 { "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" },
1460 { "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1461 { "10", "\\u0e2a\\u0e34\\u0e1a" },
1462 { "11", "\\u0e2a\\u0e34\\u0e1a\\u0e40\\u0e2d\\u0e47\\u0e14" },
1463 { "21", "\\u0e22\\u0e35\\u0e48\\u0e2a\\u0e34\\u0e1a\\u0e40\\u0e2d\\u0e47\\u0e14" },
1464 { "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u0e23\\u0e49\\u0e2d\\u0e22\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1465 { "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u0e08\\u0e38\\u0e14\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" },
1469 doTest(formatter
, testData
, TRUE
);
1475 IntlTestRBNF::TestSwedishSpellout()
1477 UErrorCode status
= U_ZERO_ERROR
;
1478 RuleBasedNumberFormat
* formatter
1479 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("sv"), status
);
1481 if (U_FAILURE(status
)) {
1482 errln("FAIL: could not construct formatter");
1484 static const char* testDataDefault
[][2] = {
1485 { "101", "etthundra\\u00aden" },
1486 { "123", "etthundra\\u00adtjugotre" },
1487 { "1,001", "ettusen en" },
1488 { "1,100", "ettusen etthundra" },
1489 { "1,101", "ettusen etthundra\\u00aden" },
1490 { "1,234", "ettusen tv\\u00e5hundra\\u00adtrettiofyra" },
1491 { "10,001", "tio\\u00adtusen en" },
1492 { "11,000", "elva\\u00adtusen" },
1493 { "12,000", "tolv\\u00adtusen" },
1494 { "20,000", "tjugo\\u00adtusen" },
1495 { "21,000", "tjugoen\\u00adtusen" },
1496 { "21,001", "tjugoen\\u00adtusen en" },
1497 { "200,000", "tv\\u00e5hundra\\u00adtusen" },
1498 { "201,000", "tv\\u00e5hundra\\u00aden\\u00adtusen" },
1499 { "200,200", "tv\\u00e5hundra\\u00adtusen tv\\u00e5hundra" },
1500 { "2,002,000", "tv\\u00e5 miljoner tv\\u00e5\\u00adtusen" },
1501 { "12,345,678", "tolv miljoner trehundra\\u00adfyrtiofem\\u00adtusen sexhundra\\u00adsjuttio\\u00e5tta" },
1502 { "123,456.789", "etthundra\\u00adtjugotre\\u00adtusen fyrahundra\\u00adfemtiosex komma sju \\u00e5tta nio" },
1503 { "-12,345.678", "minus tolv\\u00adtusen trehundra\\u00adfyrtiofem komma sex sju \\u00e5tta" },
1506 doTest(formatter
, testDataDefault
, TRUE
);
1508 static const char* testDataNeutrum
[][2] = {
1509 { "101", "etthundra\\u00adett" },
1510 { "1,001", "ettusen ett" },
1511 { "1,101", "ettusen etthundra\\u00adett" },
1512 { "10,001", "tio\\u00adtusen ett" },
1513 { "21,001", "tjugoen\\u00adtusen ett" },
1517 formatter
->setDefaultRuleSet("%neutrum", status
);
1518 if (U_SUCCESS(status
)) {
1519 logln("testing neutrum rules");
1520 doTest(formatter
, testDataNeutrum
, TRUE
);
1523 errln("Can't test neutrum rules");
1526 static const char* testDataYear
[][2] = {
1527 { "101", "etthundra\\u00adett" },
1528 { "900", "niohundra" },
1529 { "1,001", "tiohundra\\u00adett" },
1530 { "1,100", "elvahundra" },
1531 { "1,101", "elvahundra\\u00adett" },
1532 { "1,234", "tolvhundra\\u00adtrettiofyra" },
1533 { "2,001", "tjugohundra\\u00adett" },
1534 { "10,001", "tio\\u00adtusen ett" },
1538 formatter
->setDefaultRuleSet("%year", status
);
1539 if (U_SUCCESS(status
)) {
1540 logln("testing year rules");
1541 doTest(formatter
, testDataYear
, TRUE
);
1544 errln("Can't test year rules");
1552 IntlTestRBNF::TestSmallValues()
1554 UErrorCode status
= U_ZERO_ERROR
;
1555 RuleBasedNumberFormat
* formatter
1556 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("en_US"), status
);
1558 if (U_FAILURE(status
)) {
1559 errln("FAIL: could not construct formatter");
1561 static const char* const testDataDefault
[][2] = {
1562 { "0.001", "zero point zero zero one" },
1563 { "0.0001", "zero point zero zero zero one" },
1564 { "0.00001", "zero point zero zero zero zero one" },
1565 { "0.000001", "zero point zero zero zero zero zero one" },
1566 { "0.0000001", "zero point zero zero zero zero zero zero one" },
1567 { "0.00000001", "zero point zero zero zero zero zero zero zero one" },
1568 { "0.000000001", "zero point zero zero zero zero zero zero zero zero one" },
1569 { "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" },
1570 { "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" },
1571 { "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" },
1572 { "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" },
1573 { "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1574 { "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1575 { "10,000,000.001", "ten million point zero zero one" },
1576 { "10,000,000.0001", "ten million point zero zero zero one" },
1577 { "10,000,000.00001", "ten million point zero zero zero zero one" },
1578 { "10,000,000.000001", "ten million point zero zero zero zero zero one" },
1579 { "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" },
1580 // { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" },
1581 // { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" },
1582 { "10,000,000", "ten million" },
1583 // { "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" },
1584 // { "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" },
1585 // { "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" },
1586 { "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" },
1587 { "123,456.654321", "one hundred and twenty-three thousand, four hundred and fifty-six point six five four three two one" },
1588 { "12,345.54321", "twelve thousand three hundred and forty-five point five four three two one" },
1589 { "1,234.4321", "one thousand two hundred and thirty-four point four three two one" },
1590 { "123.321", "one hundred and twenty-three point three two one" },
1591 { "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" },
1592 { "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" },
1596 doTest(formatter
, testDataDefault
, TRUE
);
1603 IntlTestRBNF::TestLocalizations(void)
1606 UnicodeString
rules("%main:0:no;1:some;100:a lot;1000:tons;\n"
1607 "%other:0:nada;1:yah, some;100:plenty;1000:more'n you'll ever need");
1609 UErrorCode status
= U_ZERO_ERROR
;
1611 RuleBasedNumberFormat
formatter(rules
, perror
, status
);
1612 if (U_FAILURE(status
)) {
1613 errln("FAIL: could not construct formatter");
1616 static const char* const testData
[][2] = {
1618 { "5", "yah, some" },
1619 { "423", "plenty" },
1620 { "12345", "more'n you'll ever need" },
1623 doTest(&formatter
, testData
, FALSE
);
1627 UnicodeString
loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>");
1628 static const char* const testData
[][2] = {
1632 { "12345", "tons" },
1635 RuleBasedNumberFormat
formatter0(rules
, loc
, perror
, status
);
1636 if (U_FAILURE(status
)) {
1637 errln("failed to build second formatter");
1639 doTest(&formatter0
, testData
, FALSE
);
1642 // exercise localization info
1643 Locale
locale0("en__VALLEY@turkey=gobblegobble");
1644 Locale
locale1("de_DE_FOO");
1645 Locale
locale2("ja_JP");
1646 UnicodeString name
= formatter0
.getRuleSetName(0);
1647 if ( formatter0
.getRuleSetDisplayName(0, locale0
) == "Main"
1648 && formatter0
.getRuleSetDisplayName(0, locale1
) == "das Main"
1649 && formatter0
.getRuleSetDisplayName(0, locale2
) == "%main"
1650 && formatter0
.getRuleSetDisplayName(name
, locale0
) == "Main"
1651 && formatter0
.getRuleSetDisplayName(name
, locale1
) == "das Main"
1652 && formatter0
.getRuleSetDisplayName(name
, locale2
) == "%main"){
1653 logln("getRuleSetDisplayName tested");
1655 errln("failed to getRuleSetDisplayName");
1659 for (i
= 0; i
< formatter0
.getNumberOfRuleSetDisplayNameLocales(); ++i
) {
1660 Locale locale
= formatter0
.getRuleSetDisplayNameLocale(i
, status
);
1661 if (U_SUCCESS(status
)) {
1662 for (int j
= 0; j
< formatter0
.getNumberOfRuleSetNames(); ++j
) {
1663 UnicodeString name
= formatter0
.getRuleSetName(j
);
1664 UnicodeString lname
= formatter0
.getRuleSetDisplayName(j
, locale
);
1665 UnicodeString msg
= locale
.getName();
1678 static const char* goodLocs
[] = {
1679 "", // zero-length ok, same as providing no localization data
1680 "<<>>", // no public rule sets ok
1681 "<<%main>>", // no localizations ok
1682 "<<%main,>,<en, Main,>>", // comma before close angle ok
1683 "<<%main>,<en, ',<>\" '>>", // quotes everything until next quote
1684 "<<%main>,<'en', \"it's ok\">>", // double quotes work too
1685 " \n <\n <\n %main\n >\n , \t <\t en\t , \tfoo \t\t > \n\n > \n ", // rule whitespace ok
1687 int32_t goodLocsLen
= sizeof(goodLocs
)/sizeof(goodLocs
[0]);
1689 static const char* badLocs
[] = {
1690 " ", // non-zero length
1691 "<>", // empty array
1692 "<", // unclosed outer array
1693 "<<", // unclosed inner array
1694 "<<,>>", // unexpected comma
1695 "<<''>>", // empty string
1696 " x<<%main>>", // first non space char not open angle bracket
1697 "<%main>", // missing inner array
1698 "<<%main %other>>", // elements missing separating commma (spaces must be quoted)
1699 "<<%main><en, Main>>", // arrays missing separating comma
1700 "<<%main>,<en, main, foo>>", // too many elements in locale data
1701 "<<%main>,<en>>", // too few elements in locale data
1702 "<<<%main>>>", // unexpected open angle
1703 "<<%main<>>>", // unexpected open angle
1704 "<<%main, %other>,<en,,>>", // implicit empty strings
1705 "<<%main>,<en,''>>", // empty string
1706 "<<%main>, < en, '>>", // unterminated quote
1707 "<<%main>, < en, \"<>>", // unterminated quote
1708 "<<%main\">>", // quote in string
1709 "<<%main'>>", // quote in string
1710 "<<%main<>>", // open angle in string
1711 "<<%main>> x", // extra non-space text at end
1714 int32_t badLocsLen
= sizeof(badLocs
)/sizeof(badLocs
[0]);
1716 for (i
= 0; i
< goodLocsLen
; ++i
) {
1717 logln("[%d] '%s'", i
, goodLocs
[i
]);
1718 UErrorCode status
= U_ZERO_ERROR
;
1719 UnicodeString
loc(goodLocs
[i
]);
1720 RuleBasedNumberFormat
fmt(rules
, loc
, perror
, status
);
1721 if (U_FAILURE(status
)) {
1722 errln("Failed parse of good localization string: '%s'", goodLocs
[i
]);
1726 for (i
= 0; i
< badLocsLen
; ++i
) {
1727 logln("[%d] '%s'", i
, badLocs
[i
]);
1728 UErrorCode status
= U_ZERO_ERROR
;
1729 UnicodeString
loc(badLocs
[i
]);
1730 RuleBasedNumberFormat
fmt(rules
, loc
, perror
, status
);
1731 if (U_SUCCESS(status
)) {
1732 errln("Successful parse of bad localization string: '%s'", badLocs
[i
]);
1740 IntlTestRBNF::TestAllLocales()
1742 const char* names
[] = {
1748 const Locale
* locales
= Locale::getAvailableLocales(count
);
1749 for (int i
= 0; i
< count
; ++i
) {
1750 const Locale
* loc
= &locales
[i
];
1751 for (int j
= 0; j
< 3; ++j
) {
1752 UErrorCode status
= U_ZERO_ERROR
;
1753 RuleBasedNumberFormat
* f
= new RuleBasedNumberFormat((URBNFRuleSetTag
)j
, *loc
, status
);
1754 if (U_SUCCESS(status
)) {
1760 logln(UnicodeString(loc
->getName()) + UnicodeString(names
[j
])
1761 + UnicodeString("success: 45.678 -> ") + str
);
1763 errln(UnicodeString(loc
->getName()) + UnicodeString(names
[j
])
1764 + UnicodeString("ERROR could not instantiate -> ") + UnicodeString(u_errorName(status
)));
1771 IntlTestRBNF::TestMultiplierSubstitution(void) {
1772 UnicodeString
rules("=#,##0=;1,000,000: <##0.###< million;");
1773 UErrorCode status
= U_ZERO_ERROR
;
1774 UParseError parse_error
;
1775 RuleBasedNumberFormat
*rbnf
=
1776 new RuleBasedNumberFormat(rules
, Locale::getUS(), parse_error
, status
);
1777 if (U_SUCCESS(status
)) {
1780 double n
= 1234000.0;
1781 rbnf
->format(n
, res
, pos
);
1784 UnicodeString expected
= UNICODE_STRING_SIMPLE("1.234 million");
1785 if (expected
!= res
) {
1786 UnicodeString msg
= "Expected: ";
1787 msg
.append(expected
);
1788 msg
.append(" but got ");
1796 IntlTestRBNF::doTest(RuleBasedNumberFormat
* formatter
, const char* const testData
[][2], UBool testParsing
)
1798 // man, error reporting would be easier with printf-style syntax for unicode string and formattable
1800 UErrorCode status
= U_ZERO_ERROR
;
1801 DecimalFormatSymbols
dfs("en", status
);
1802 // NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
1803 DecimalFormat
decFmt("#,###.################", dfs
, status
);
1804 if (U_FAILURE(status
)) {
1805 errln("FAIL: could not create NumberFormat");
1807 for (int i
= 0; testData
[i
][0]; ++i
) {
1808 const char* numString
= testData
[i
][0];
1809 const char* expectedWords
= testData
[i
][1];
1811 log("[%i] %s = ", i
, numString
);
1812 Formattable expectedNumber
;
1813 decFmt
.parse(numString
, expectedNumber
, status
);
1814 if (U_FAILURE(status
)) {
1815 errln("FAIL: decFmt could not parse %s", numString
);
1818 UnicodeString actualString
;
1820 formatter
->format(expectedNumber
, actualString
/* , pos*/, status
);
1821 if (U_FAILURE(status
)) {
1822 UnicodeString msg
= "Fail: formatter could not format ";
1823 decFmt
.format(expectedNumber
, msg
, status
);
1827 UnicodeString expectedString
= UnicodeString(expectedWords
, -1, US_INV
).unescape();
1828 if (actualString
!= expectedString
) {
1829 UnicodeString msg
= "FAIL: check failed for ";
1830 decFmt
.format(expectedNumber
, msg
, status
);
1831 msg
.append(", expected ");
1832 msg
.append(expectedString
);
1833 msg
.append(" but got ");
1834 msg
.append(actualString
);
1838 logln(actualString
);
1840 Formattable parsedNumber
;
1841 formatter
->parse(actualString
, parsedNumber
, status
);
1842 if (U_FAILURE(status
)) {
1843 UnicodeString msg
= "FAIL: formatter could not parse ";
1844 msg
.append(actualString
);
1845 msg
.append(" status code: " );
1846 msg
.append(u_errorName(status
));
1850 if (parsedNumber
!= expectedNumber
) {
1851 UnicodeString msg
= "FAIL: parse failed for ";
1852 msg
.append(actualString
);
1853 msg
.append(", expected ");
1854 decFmt
.format(expectedNumber
, msg
, status
);
1855 msg
.append(", but got ");
1856 decFmt
.format(parsedNumber
, msg
, status
);
1870 IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat
* formatter
, const char* testData
[][2])
1872 UErrorCode status
= U_ZERO_ERROR
;
1873 NumberFormat
* decFmt
= NumberFormat::createInstance(Locale::getUS(), status
);
1874 if (U_FAILURE(status
)) {
1875 errln("FAIL: could not create NumberFormat");
1877 for (int i
= 0; testData
[i
][0]; ++i
) {
1878 const char* spelledNumber
= testData
[i
][0]; // spelled-out number
1879 const char* asciiUSNumber
= testData
[i
][1]; // number as ascii digits formatted for US locale
1881 UnicodeString spelledNumberString
= UnicodeString(spelledNumber
).unescape();
1882 Formattable actualNumber
;
1883 formatter
->parse(spelledNumberString
, actualNumber
, status
);
1884 if (U_FAILURE(status
)) {
1885 UnicodeString msg
= "FAIL: formatter could not parse ";
1886 msg
.append(spelledNumberString
);
1890 // I changed the logic of this test somewhat from Java-- instead of comparing the
1891 // strings, I compare the Formattables. Hmmm, but the Formattables don't compare,
1892 // so change it back.
1894 UnicodeString asciiUSNumberString
= asciiUSNumber
;
1895 Formattable expectedNumber
;
1896 decFmt
->parse(asciiUSNumberString
, expectedNumber
, status
);
1897 if (U_FAILURE(status
)) {
1898 UnicodeString msg
= "FAIL: decFmt could not parse ";
1899 msg
.append(asciiUSNumberString
);
1903 UnicodeString actualNumberString
;
1904 UnicodeString expectedNumberString
;
1905 decFmt
->format(actualNumber
, actualNumberString
, status
);
1906 decFmt
->format(expectedNumber
, expectedNumberString
, status
);
1907 if (actualNumberString
!= expectedNumberString
) {
1908 UnicodeString msg
= "FAIL: parsing";
1909 msg
.append(asciiUSNumberString
);
1911 msg
.append(" lenient parse failed for ");
1912 msg
.append(spelledNumberString
);
1913 msg
.append(", expected ");
1914 msg
.append(expectedNumberString
);
1915 msg
.append(", but got ");
1916 msg
.append(actualNumberString
);
1931 IntlTestRBNF::TestRBNFDisabled() {
1932 errln("*** RBNF currently disabled on this platform ***\n");
1938 #endif /* #if !UCONFIG_NO_FORMATTING */