2 *******************************************************************************
3 * Copyright (C) 1996-2010, 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) \
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() {
82 // this is the expected output for 123.45, with no '<' in it.
84 0x05de, 0x05d0, 0x05d4, 0x0020,
85 0x05e2, 0x05e9, 0x05e8, 0x05d9, 0x05dd, 0x0020,
86 0x05d5, 0x05e9, 0x05dc, 0x05d5, 0x05e9, 0x0020,
87 0x05e0, 0x05e7, 0x05d5, 0x05d3, 0x05d4, 0x0020,
88 0x05d0, 0x05e8, 0x05d1, 0x05e2, 0x0020,
89 0x05d7, 0x05de, 0x05e9, 0x0000,
92 0x05DE, 0x05D0, 0x05D4, 0x0020,
93 0x05E2, 0x05E9, 0x05E8, 0x05D9, 0x05DD, 0x0020,
94 0x05D5, 0x05E9, 0x05DC, 0x05D5, 0x05E9, 0x0020,
95 0x05E0, 0x05E7, 0x05D5, 0x05D3, 0x05D4, 0x0020,
96 0x05D0, 0x05E4, 0x05E1, 0x0020,
97 0x05D0, 0x05E4, 0x05E1, 0x0020,
98 0x05D0, 0x05E8, 0x05D1, 0x05E2, 0x0020,
99 0x05D7, 0x05DE, 0x05E9, 0x0000,
101 UErrorCode status
= U_ZERO_ERROR
;
102 RuleBasedNumberFormat
* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT
, "he_IL", status
);
103 if (status
== U_MISSING_RESOURCE_ERROR
|| status
== U_FILE_ACCESS_ERROR
) {
104 errcheckln(status
, "Failed in constructing RuleBasedNumberFormat - %s", u_errorName(status
));
108 UnicodeString result
;
109 Formattable parseResult
;
112 UnicodeString
expected(text1
);
113 formatter
->format(123.45, result
);
114 if (result
!= expected
) {
115 errln((UnicodeString
)"expected '" + TestUtility::hex(expected
) + "'\nbut got: '" + TestUtility::hex(result
) + "'");
117 // formatter->parse(result, parseResult, pp);
118 // if (parseResult.getDouble() != 123.45) {
119 // errln("expected 123.45 but got: %g", parseResult.getDouble());
124 UnicodeString
expected(text2
);
126 formatter
->format(123.0045, result
);
127 if (result
!= expected
) {
128 errln((UnicodeString
)"expected '" + TestUtility::hex(expected
) + "'\nbut got: '" + TestUtility::hex(result
) + "'");
131 // formatter->parse(result, parseResult, pp);
132 // if (parseResult.getDouble() != 123.0045) {
133 // errln("expected 123.0045 but got: %g", parseResult.getDouble());
141 IntlTestRBNF::TestAPI() {
142 // This test goes through the APIs that were not tested before.
143 // These tests are too small to have separate test classes/functions
145 UErrorCode status
= U_ZERO_ERROR
;
146 RuleBasedNumberFormat
* formatter
147 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
148 if (status
== U_MISSING_RESOURCE_ERROR
|| status
== U_FILE_ACCESS_ERROR
) {
149 dataerrln("Unable to create formatter. - %s", u_errorName(status
));
154 logln("RBNF API test starting");
157 logln("Testing Clone");
158 RuleBasedNumberFormat
* rbnfClone
= (RuleBasedNumberFormat
*)formatter
->clone();
159 if(rbnfClone
!= NULL
) {
160 if(!(*rbnfClone
== *formatter
)) {
161 errln("Clone should be semantically equivalent to the original!");
165 errln("Cloning failed!");
171 logln("Testing assignment operator");
172 RuleBasedNumberFormat
assignResult(URBNF_SPELLOUT
, Locale("es", "ES", ""), status
);
173 assignResult
= *formatter
;
174 if(!(assignResult
== *formatter
)) {
175 errln("Assignment result should be semantically equivalent to the original!");
179 // test rule constructor
181 logln("Testing rule constructor");
182 LocalUResourceBundlePointer
en(ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
"rbnf", "en", &status
));
183 if(U_FAILURE(status
)) {
184 errln("Unable to access resource bundle with data!");
188 LocalUResourceBundlePointer
rbnfRules(ures_getByKey(en
.getAlias(), "RBNFRules", NULL
, &status
));
189 LocalUResourceBundlePointer
ruleSets(ures_getByKey(rbnfRules
.getAlias(), "SpelloutRules", NULL
, &status
));
191 while (ures_hasNext(ruleSets
.getAlias())) {
192 const UChar
* currentString
= ures_getNextString(ruleSets
.getAlias(), &len
, NULL
, &status
);
194 desc
.append(currentString
);
197 const UChar
*spelloutRules
= desc
.getTerminatedBuffer();
199 if(U_FAILURE(status
) || ruleLen
== 0 || spelloutRules
== NULL
) {
200 errln("Unable to access the rules string!");
203 RuleBasedNumberFormat
ruleCtorResult(spelloutRules
, Locale::getUS(), perror
, status
);
204 if(!(ruleCtorResult
== *formatter
)) {
205 errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
208 // Jitterbug 4452, for coverage
209 RuleBasedNumberFormat
nf(spelloutRules
, (UnicodeString
)"", Locale::getUS(), perror
, status
);
210 if(!(nf
== *formatter
)) {
211 errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
219 logln("Testing getRules function");
220 UnicodeString rules
= formatter
->getRules();
222 RuleBasedNumberFormat
fromRulesResult(rules
, Locale::getUS(), perror
, status
);
224 if(!(fromRulesResult
== *formatter
)) {
225 errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!");
231 logln("Testing copy constructor");
232 RuleBasedNumberFormat
copyCtorResult(*formatter
);
233 if(!(copyCtorResult
== *formatter
)) {
234 errln("Copy constructor result result should be semantically equivalent to the original!");
238 #if !UCONFIG_NO_COLLATION
240 #define NUMERIC_STRINGS_NOT_PARSEABLE 1 // ticket/8224
242 // test ruleset names
244 logln("Testing getNumberOfRuleSetNames, getRuleSetName and format using rule set names");
245 int32_t noOfRuleSetNames
= formatter
->getNumberOfRuleSetNames();
246 if(noOfRuleSetNames
== 0) {
247 errln("Number of rule set names should be more than zero");
249 UnicodeString ruleSetName
;
251 int32_t intFormatNum
= 34567;
252 double doubleFormatNum
= 893411.234;
253 logln("number of rule set names is %i", noOfRuleSetNames
);
254 for(i
= 0; i
< noOfRuleSetNames
; i
++) {
255 FieldPosition pos1
, pos2
;
256 UnicodeString intFormatResult
, doubleFormatResult
;
257 Formattable intParseResult
, doubleParseResult
;
258 #if NUMERIC_STRINGS_NOT_PARSEABLE
259 UBool parseDoubleNonLenientOK
= TRUE
;
260 UBool parseDoubleLenientOK
= TRUE
;
263 ruleSetName
= formatter
->getRuleSetName(i
);
264 log("Rule set name %i is ", i
);
266 logln(". Format results are: ");
267 intFormatResult
= formatter
->format(intFormatNum
, ruleSetName
, intFormatResult
, pos1
, status
);
268 doubleFormatResult
= formatter
->format(doubleFormatNum
, ruleSetName
, doubleFormatResult
, pos2
, status
);
269 if(U_FAILURE(status
)) {
270 errln("Format using a rule set failed");
273 logln(intFormatResult
);
274 logln(doubleFormatResult
);
276 #if NUMERIC_STRINGS_NOT_PARSEABLE
277 // "spellout-numbering-year" ruleSet produces (above) a numeric string using:
278 // "x.x: =#,###0.#=;"
279 // which will not parse (below) - we believe this is CORRECT behavior, as found in ICU 4.0 (see ticket/8224).
280 // Note this numeric string "89,3411.2" will not even parse with Lenient = TRUE because
281 // the NumberFormat (used as last-resort) in NFSubstitution::doParse fails.
282 UnicodeString numberingYear
= UNICODE_STRING_SIMPLE("spellout-numbering-year");
284 // "spellout-ordinal" and "spellout-ordinal-verbose" ruleSets produce (above) a numeric string using:
285 // "x.x: =#,##0.#=;" -> "893,411.2"
286 // which will not parse (below) with Lenient = FALSE, but does parse with Lenient = TRUE because
287 // NFSubstitution::doParse will succeed when using NumberFormat as last-resort.
288 UnicodeString ordinal
= UNICODE_STRING_SIMPLE("spellout-ordinal");
290 // RuleSets other than spellout-numbering-year and spellout-ordinalXXX produce fully spelled out text above
291 // which is fully parseable.
292 parseDoubleLenientOK
= ( ruleSetName
.indexOf(numberingYear
) == -1 );
293 parseDoubleNonLenientOK
= ( ruleSetName
.indexOf(numberingYear
) == -1 && ruleSetName
.indexOf(ordinal
) == -1 );
296 formatter
->setLenient(TRUE
);
297 formatter
->parse(intFormatResult
, intParseResult
, status
);
298 formatter
->parse(doubleFormatResult
, doubleParseResult
, status
);
300 logln("Parse results for lenient = TRUE, %i, %f", intParseResult
.getLong(), doubleParseResult
.getDouble());
302 #if NUMERIC_STRINGS_NOT_PARSEABLE
303 if((!parseDoubleLenientOK
) && (status
== U_INVALID_FORMAT_ERROR
)) {
304 status
= U_USING_FALLBACK_WARNING
;
305 logln("Clearing expected U_INVALID_FORMAT_ERROR during parsing");
309 formatter
->setLenient(FALSE
);
310 formatter
->parse(intFormatResult
, intParseResult
, status
);
311 formatter
->parse(doubleFormatResult
, doubleParseResult
, status
);
313 logln("Parse results for lenient = FALSE, %i, %f", intParseResult
.getLong(), doubleParseResult
.getDouble());
315 #if NUMERIC_STRINGS_NOT_PARSEABLE
316 if((!parseDoubleNonLenientOK
) && (status
== U_INVALID_FORMAT_ERROR
)) {
317 status
= U_USING_FALLBACK_WARNING
;
318 logln("Clearing expected U_INVALID_FORMAT_ERROR during parsing");
322 if(U_FAILURE(status
)) {
323 errln("Error during parsing");
326 intFormatResult
= formatter
->format(intFormatNum
, "BLABLA", intFormatResult
, pos1
, status
);
327 if(U_SUCCESS(status
)) {
328 errln("Using invalid rule set name should have failed");
331 status
= U_ZERO_ERROR
;
332 doubleFormatResult
= formatter
->format(doubleFormatNum
, "TRUC", doubleFormatResult
, pos2
, status
);
333 if(U_SUCCESS(status
)) {
334 errln("Using invalid rule set name should have failed");
337 status
= U_ZERO_ERROR
;
339 status
= U_ZERO_ERROR
;
344 UnicodeString
expected("four point five","");
345 logln("Testing format(double)");
346 UnicodeString result
;
347 formatter
->format(4.5,result
);
348 if(result
!= expected
) {
349 errln("Formatted 4.5, expected " + expected
+ " got " + result
);
351 logln("Formatted 4.5, expected " + expected
+ " got " + result
);
355 formatter
->format((int32_t)4,result
);
356 if(result
!= expected
) {
357 errln("Formatted 4, expected " + expected
+ " got " + result
);
359 logln("Formatted 4, expected " + expected
+ " got " + result
);
364 formatter
->format((int64_t)4, result
, pos
, status
= U_ZERO_ERROR
);
365 if(result
!= expected
) {
366 errln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
368 logln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
371 //Jitterbug 4452, for coverage
374 formatter
->format((int64_t)4, formatter
->getRuleSetName(0), result
, pos2
, status
= U_ZERO_ERROR
);
375 if(result
!= expected
) {
376 errln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
378 logln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
382 logln("Cleaning up");
386 void IntlTestRBNF::TestFractionalRuleSet()
388 UnicodeString
fracRules(
390 // this rule formats the number if it's 1 or more. It formats
391 // the integral part using a DecimalFormat ("#,##0" puts
392 // thousands separators in the right places) and the fractional
393 // part using %%frac. If there is no fractional part, it
394 // just shows the integral part.
395 " x.0: <#,##0<[ >%%frac>];\n"
396 // this rule formats the number if it's between 0 and 1. It
397 // shows only the fractional part (0.5 shows up as "1/2," not
400 // the fraction rule set. This works the same way as the one in the
401 // preceding example: We multiply the fractional part of the number
402 // being formatted by each rule's base value and use the rule that
403 // produces the result closest to 0 (or the first rule that produces 0).
404 // Since we only provide rules for the numbers from 2 to 10, we know
405 // we'll get a fraction with a denominator between 2 and 10.
406 // "<0<" causes the numerator of the fraction to be formatted
420 int len
= fracRules
.length();
422 for (int i
= 0; i
< len
; ++i
) {
423 UChar ch
= fracRules
.charAt(i
);
425 change
= 2; // change ok
426 } else if (ch
== ':') {
427 change
= 1; // change, but once we hit a non-space char, don't change
428 } else if (ch
== ' ') {
430 fracRules
.setCharAt(i
, (UChar
)0x200e);
439 UErrorCode status
= U_ZERO_ERROR
;
441 RuleBasedNumberFormat
formatter(fracRules
, Locale::getEnglish(), perror
, status
);
442 if (U_FAILURE(status
)) {
443 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
445 static const char* const testData
[][2] = {
458 { "3.125", "3 1/8" },
459 { "4.1428", "4 1/7" },
460 { "5.1667", "5 1/6" },
463 { "8.333", "8 1/3" },
468 { "1.2856", "1 2/7" },
471 doTest(&formatter
, testData
, FALSE
); // exact values aren't parsable from fractions
476 #define LLAssert(a) \
477 if (!(a)) errln("FAIL: " #a)
479 void IntlTestRBNF::TestLLongConstructors()
481 logln("Testing constructors");
483 // constant (shouldn't really be public)
484 LLAssert(llong(llong::kD32
).asDouble() == llong::kD32
);
486 // internal constructor (shouldn't really be public)
487 LLAssert(llong(0, 1).asDouble() == 1);
488 LLAssert(llong(1, 0).asDouble() == llong::kD32
);
489 LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1);
491 // public empty constructor
492 LLAssert(llong().asDouble() == 0);
494 // public int32_t constructor
495 LLAssert(llong((int32_t)0).asInt() == (int32_t)0);
496 LLAssert(llong((int32_t)1).asInt() == (int32_t)1);
497 LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1);
498 LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff);
499 LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1);
500 LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000);
502 // public int16_t constructor
503 LLAssert(llong((int16_t)0).asInt() == (int16_t)0);
504 LLAssert(llong((int16_t)1).asInt() == (int16_t)1);
505 LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1);
506 LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff);
507 LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff);
508 LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000);
510 // public int8_t constructor
511 LLAssert(llong((int8_t)0).asInt() == (int8_t)0);
512 LLAssert(llong((int8_t)1).asInt() == (int8_t)1);
513 LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1);
514 LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f);
515 LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff);
516 LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80);
518 // public uint16_t constructor
519 LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0);
520 LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1);
521 LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1);
522 LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff);
523 LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff);
524 LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000);
526 // public uint32_t constructor
527 LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0);
528 LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1);
529 LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1);
530 LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff);
531 LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1);
532 LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
534 // public double constructor
535 LLAssert(llong((double)0).asDouble() == (double)0);
536 LLAssert(llong((double)1).asDouble() == (double)1);
537 LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff);
538 LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000);
539 LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001);
541 // can't access uprv_maxmantissa, so fake it
542 double maxmantissa
= (llong((int32_t)1) << 40).asDouble();
543 LLAssert(llong(maxmantissa
).asDouble() == maxmantissa
);
544 LLAssert(llong(-maxmantissa
).asDouble() == -maxmantissa
);
547 LLAssert(llong(llong(0, 1)).asDouble() == 1);
548 LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32
);
549 LLAssert(llong(llong(-1, (uint32_t)-1)).asDouble() == -1);
551 // asInt - test unsigned to signed narrowing conversion
552 LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff);
553 LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000);
555 // asUInt - test signed to unsigned narrowing conversion
556 LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1);
557 LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
559 // asDouble already tested
563 void IntlTestRBNF::TestLLongSimpleOperators()
565 logln("Testing simple operators");
568 LLAssert(llong() == llong(0, 0));
569 LLAssert(llong(1,0) == llong(1, 0));
570 LLAssert(llong(0,1) == llong(0, 1));
573 LLAssert(llong(1,0) != llong(1,1));
574 LLAssert(llong(0,1) != llong(1,1));
575 LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff));
578 LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff)));
581 LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1)));
584 LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff)));
585 LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1)));
588 LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1)));
589 LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1)));
592 LLAssert(llong(1, 1) > llong(1, 0));
593 LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff));
594 LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0));
595 LLAssert(llong(1, 0) > llong(0, 0x7fffffff));
596 LLAssert(llong(1, 0) > llong(0, 0xffffffff));
597 LLAssert(llong(0, 0) > llong(0x80000000, 1));
600 LLAssert(llong(1, 0) < llong(1, 1));
601 LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000));
602 LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1));
603 LLAssert(llong(0, 0x7fffffff) < llong(1, 0));
604 LLAssert(llong(0, 0xffffffff) < llong(1, 0));
605 LLAssert(llong(0x80000000, 1) < llong(0, 0));
608 LLAssert(llong(1, 1) >= llong(1, 0));
609 LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff));
610 LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0));
611 LLAssert(llong(1, 0) >= llong(0, 0x7fffffff));
612 LLAssert(llong(1, 0) >= llong(0, 0xffffffff));
613 LLAssert(llong(0, 0) >= llong(0x80000000, 1));
614 LLAssert(llong() >= llong(0, 0));
615 LLAssert(llong(1,0) >= llong(1, 0));
616 LLAssert(llong(0,1) >= llong(0, 1));
619 LLAssert(llong(1, 0) <= llong(1, 1));
620 LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000));
621 LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1));
622 LLAssert(llong(0, 0x7fffffff) <= llong(1, 0));
623 LLAssert(llong(0, 0xffffffff) <= llong(1, 0));
624 LLAssert(llong(0x80000000, 1) <= llong(0, 0));
625 LLAssert(llong() <= llong(0, 0));
626 LLAssert(llong(1,0) <= llong(1, 0));
627 LLAssert(llong(0,1) <= llong(0, 1));
630 LLAssert(llong() == (int32_t)0);
631 LLAssert(llong(0,1) == (int32_t)1);
634 LLAssert(llong(1,0) != (int32_t)0);
635 LLAssert(llong(0,1) != (int32_t)2);
636 LLAssert(llong(0,0xffffffff) != (int32_t)-1);
638 llong
negOne(0xffffffff, 0xffffffff);
641 LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff);
642 LLAssert(negOne
> (int32_t)-2);
643 LLAssert(llong(1, 0) > (int32_t)0x7fffffff);
644 LLAssert(llong(0, 0) > (int32_t)-1);
647 LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff);
648 LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1);
651 LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff);
652 LLAssert(negOne
>= (int32_t)-2);
653 LLAssert(llong(1, 0) >= (int32_t)0x7fffffff);
654 LLAssert(llong(0, 0) >= (int32_t)-1);
655 LLAssert(llong() >= (int32_t)0);
656 LLAssert(llong(0,1) >= (int32_t)1);
659 LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff);
660 LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1);
661 LLAssert(llong() <= (int32_t)0);
662 LLAssert(llong(0,1) <= (int32_t)1);
665 LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1);
668 LLAssert((llong(1, 1) <<= 0) == llong(1, 1));
669 LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000));
670 LLAssert((llong(1, 1) <<= 32) == llong(1, 0));
671 LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0));
672 LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used
673 LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used
676 LLAssert((llong((int32_t)1) << 5).asUInt() == 32);
678 // operator >>= (sign extended)
679 LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc));
680 LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde));
681 LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff));
682 LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000));
683 LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
684 LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used
686 // operator >> sign extended)
687 LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde));
689 // ushr (right shift without sign extension)
690 LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc));
691 LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde));
692 LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1));
693 LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000));
694 LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
695 LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used
698 LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
701 LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
704 LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
707 LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
710 LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
713 LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
716 LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa));
719 LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
722 LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
725 LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
727 // operator&=(uint32)
728 LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
730 // operator|=(uint32)
731 LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
733 // operator^=(uint32)
734 LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
737 LLAssert(llong(1, 0) == ++llong(0,0xffffffff));
740 LLAssert(llong(0,0xffffffff) == --llong(1, 0));
744 llong
n(0, 0xffffffff);
745 LLAssert(llong(0, 0xffffffff) == n
++);
746 LLAssert(llong(1, 0) == n
);
752 LLAssert(llong(1, 0) == n
--);
753 LLAssert(llong(0, 0xffffffff) == n
);
757 LLAssert(llong(0, 0) == -llong(0, 0));
758 LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1));
759 LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff));
760 LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1));
761 LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow
766 LLAssert((n
-= llong(0, 1)) == llong(0xffffffff, 0xffffffff));
767 LLAssert(n
== llong(0xffffffff, 0xffffffff));
770 LLAssert((n
-= llong(0, 1)) == llong(0, 0xffffffff));
771 LLAssert(n
== llong(0, 0xffffffff));
777 LLAssert((n
- llong(0, 1)) == llong(0xffffffff, 0xffffffff));
778 LLAssert(n
== llong(0, 0));
781 LLAssert((n
- llong(0, 1)) == llong(0, 0xffffffff));
782 LLAssert(n
== llong(1, 0));
787 llong
n(0xffffffff, 0xffffffff);
788 LLAssert((n
+= llong(0, 1)) == llong(0, 0));
789 LLAssert(n
== llong(0, 0));
791 n
= llong(0, 0xffffffff);
792 LLAssert((n
+= llong(0, 1)) == llong(1, 0));
793 LLAssert(n
== llong(1, 0));
798 llong
n(0xffffffff, 0xffffffff);
799 LLAssert((n
+ llong(0, 1)) == llong(0, 0));
800 LLAssert(n
== llong(0xffffffff, 0xffffffff));
802 n
= llong(0, 0xffffffff);
803 LLAssert((n
+ llong(0, 1)) == llong(1, 0));
804 LLAssert(n
== llong(0, 0xffffffff));
809 void IntlTestRBNF::TestLLong()
811 logln("Starting TestLLong");
813 TestLLongConstructors();
815 TestLLongSimpleOperators();
817 logln("Testing operator*=, operator*");
819 // operator*=, operator*
820 // small and large values, positive, &NEGative, zero
821 // also test commutivity
824 const llong
ONE(0, 1);
825 const llong
NEG_ONE((int32_t)-1);
826 const llong
THREE(0, 3);
827 const llong
NEG_THREE((int32_t)-3);
828 const llong
TWO_TO_16(0, 0x10000);
829 const llong NEG_TWO_TO_16
= -TWO_TO_16
;
830 const llong
TWO_TO_32(1, 0);
831 const llong NEG_TWO_TO_32
= -TWO_TO_32
;
833 const llong
NINE(0, 9);
834 const llong NEG_NINE
= -NINE
;
836 const llong
TWO_TO_16X3(0, 0x00030000);
837 const llong NEG_TWO_TO_16X3
= -TWO_TO_16X3
;
839 const llong
TWO_TO_32X3(3, 0);
840 const llong NEG_TWO_TO_32X3
= -TWO_TO_32X3
;
842 const llong
TWO_TO_48(0x10000, 0);
843 const llong NEG_TWO_TO_48
= -TWO_TO_48
;
845 const int32_t VALUE_WIDTH
= 9;
846 const llong
* values
[VALUE_WIDTH
] = {
847 &ZERO
, &ONE
, &NEG_ONE
, &THREE
, &NEG_THREE
, &TWO_TO_16
, &NEG_TWO_TO_16
, &TWO_TO_32
, &NEG_TWO_TO_32
850 const llong
* answers
[VALUE_WIDTH
*VALUE_WIDTH
] = {
851 &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
,
852 &ZERO
, &ONE
, &NEG_ONE
, &THREE
, &NEG_THREE
, &TWO_TO_16
, &NEG_TWO_TO_16
, &TWO_TO_32
, &NEG_TWO_TO_32
,
853 &ZERO
, &NEG_ONE
, &ONE
, &NEG_THREE
, &THREE
, &NEG_TWO_TO_16
, &TWO_TO_16
, &NEG_TWO_TO_32
, &TWO_TO_32
,
854 &ZERO
, &THREE
, &NEG_THREE
, &NINE
, &NEG_NINE
, &TWO_TO_16X3
, &NEG_TWO_TO_16X3
, &TWO_TO_32X3
, &NEG_TWO_TO_32X3
,
855 &ZERO
, &NEG_THREE
, &THREE
, &NEG_NINE
, &NINE
, &NEG_TWO_TO_16X3
, &TWO_TO_16X3
, &NEG_TWO_TO_32X3
, &TWO_TO_32X3
,
856 &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
,
857 &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
,
858 &ZERO
, &TWO_TO_32
, &NEG_TWO_TO_32
, &TWO_TO_32X3
, &NEG_TWO_TO_32X3
, &TWO_TO_48
, &NEG_TWO_TO_48
, &ZERO
, &ZERO
,
859 &ZERO
, &NEG_TWO_TO_32
, &TWO_TO_32
, &NEG_TWO_TO_32X3
, &TWO_TO_32X3
, &NEG_TWO_TO_48
, &TWO_TO_48
, &ZERO
, &ZERO
862 for (int i
= 0; i
< VALUE_WIDTH
; ++i
) {
863 for (int j
= 0; j
< VALUE_WIDTH
; ++j
) {
864 llong lhs
= *values
[i
];
865 llong rhs
= *values
[j
];
866 llong ans
= *answers
[i
*VALUE_WIDTH
+ j
];
870 LLAssert((n
*= rhs
) == ans
);
874 LLAssert((n
* rhs
) == ans
);
880 logln("Testing operator/=, operator/");
881 // operator/=, operator/
882 // test num = 0, div = 0, pos/neg, > 2^32, div > num
885 const llong
ONE(0, 1);
886 const llong NEG_ONE
= -ONE
;
887 const llong
MAX(0x7fffffff, 0xffffffff);
888 const llong
MIN(0x80000000, 0);
889 const llong
TWO(0, 2);
890 const llong NEG_TWO
= -TWO
;
891 const llong
FIVE(0, 5);
892 const llong NEG_FIVE
= -FIVE
;
893 const llong
TWO_TO_32(1, 0);
894 const llong NEG_TWO_TO_32
= -TWO_TO_32
;
895 const llong TWO_TO_32d5
= llong(TWO_TO_32
.asDouble()/5.0);
896 const llong NEG_TWO_TO_32d5
= -TWO_TO_32d5
;
897 const llong TWO_TO_32X5
= TWO_TO_32
* FIVE
;
898 const llong NEG_TWO_TO_32X5
= -TWO_TO_32X5
;
900 const llong
* tuples
[] = { // lhs, rhs, ans
903 &NEG_ONE
, &ZERO
, &MIN
,
905 &ONE
, &NEG_ONE
, &NEG_ONE
,
906 &NEG_ONE
, &ONE
, &NEG_ONE
,
907 &NEG_ONE
, &NEG_ONE
, &ONE
,
909 &FIVE
, &NEG_TWO
, &NEG_TWO
,
910 &NEG_FIVE
, &TWO
, &NEG_TWO
,
911 &NEG_FIVE
, &NEG_TWO
, &TWO
,
913 &TWO
, &NEG_FIVE
, &ZERO
,
914 &NEG_TWO
, &FIVE
, &ZERO
,
915 &NEG_TWO
, &NEG_FIVE
, &ZERO
,
916 &TWO_TO_32
, &TWO_TO_32
, &ONE
,
917 &TWO_TO_32
, &NEG_TWO_TO_32
, &NEG_ONE
,
918 &NEG_TWO_TO_32
, &TWO_TO_32
, &NEG_ONE
,
919 &NEG_TWO_TO_32
, &NEG_TWO_TO_32
, &ONE
,
920 &TWO_TO_32
, &FIVE
, &TWO_TO_32d5
,
921 &TWO_TO_32
, &NEG_FIVE
, &NEG_TWO_TO_32d5
,
922 &NEG_TWO_TO_32
, &FIVE
, &NEG_TWO_TO_32d5
,
923 &NEG_TWO_TO_32
, &NEG_FIVE
, &TWO_TO_32d5
,
924 &TWO_TO_32X5
, &FIVE
, &TWO_TO_32
,
925 &TWO_TO_32X5
, &NEG_FIVE
, &NEG_TWO_TO_32
,
926 &NEG_TWO_TO_32X5
, &FIVE
, &NEG_TWO_TO_32
,
927 &NEG_TWO_TO_32X5
, &NEG_FIVE
, &TWO_TO_32
,
928 &TWO_TO_32X5
, &TWO_TO_32
, &FIVE
,
929 &TWO_TO_32X5
, &NEG_TWO_TO_32
, &NEG_FIVE
,
930 &NEG_TWO_TO_32X5
, &NEG_TWO_TO_32
, &FIVE
,
931 &NEG_TWO_TO_32X5
, &TWO_TO_32
, &NEG_FIVE
933 const int TUPLE_WIDTH
= 3;
934 const int TUPLE_COUNT
= (int)(sizeof(tuples
)/sizeof(tuples
[0]))/TUPLE_WIDTH
;
935 for (int i
= 0; i
< TUPLE_COUNT
; ++i
) {
936 const llong lhs
= *tuples
[i
*TUPLE_WIDTH
+0];
937 const llong rhs
= *tuples
[i
*TUPLE_WIDTH
+1];
938 const llong ans
= *tuples
[i
*TUPLE_WIDTH
+2];
941 if (!((n
/= rhs
) == ans
)) {
942 errln("fail: (n /= rhs) == ans");
947 LLAssert((n
/ rhs
) == ans
);
952 logln("Testing operator%%=, operator%%");
953 //operator%=, operator%
956 const llong
ONE(0, 1);
957 const llong
TWO(0, 2);
958 const llong
THREE(0,3);
959 const llong
FOUR(0, 4);
960 const llong
FIVE(0, 5);
961 const llong
SIX(0, 6);
963 const llong NEG_ONE
= -ONE
;
964 const llong NEG_TWO
= -TWO
;
965 const llong NEG_THREE
= -THREE
;
966 const llong NEG_FOUR
= -FOUR
;
967 const llong NEG_FIVE
= -FIVE
;
968 const llong NEG_SIX
= -SIX
;
970 const llong
NINETY_NINE(0, 99);
971 const llong
HUNDRED(0, 100);
972 const llong
HUNDRED_ONE(0, 101);
974 const llong
BIG(0x12345678, 0x9abcdef0);
975 const llong
BIG_FIVE(BIG
* FIVE
);
976 const llong BIG_FIVEm1
= BIG_FIVE
- ONE
;
977 const llong BIG_FIVEp1
= BIG_FIVE
+ ONE
;
979 const llong
* tuples
[] = {
983 &THREE
, &FIVE
, &THREE
,
987 &ZERO
, &NEG_FIVE
, &ZERO
,
988 &ONE
, &NEG_FIVE
, &ONE
,
989 &TWO
, &NEG_FIVE
, &TWO
,
990 &THREE
, &NEG_FIVE
, &THREE
,
991 &FOUR
, &NEG_FIVE
, &FOUR
,
992 &FIVE
, &NEG_FIVE
, &ZERO
,
993 &SIX
, &NEG_FIVE
, &ONE
,
994 &NEG_ONE
, &FIVE
, &NEG_ONE
,
995 &NEG_TWO
, &FIVE
, &NEG_TWO
,
996 &NEG_THREE
, &FIVE
, &NEG_THREE
,
997 &NEG_FOUR
, &FIVE
, &NEG_FOUR
,
998 &NEG_FIVE
, &FIVE
, &ZERO
,
999 &NEG_SIX
, &FIVE
, &NEG_ONE
,
1000 &NEG_ONE
, &NEG_FIVE
, &NEG_ONE
,
1001 &NEG_TWO
, &NEG_FIVE
, &NEG_TWO
,
1002 &NEG_THREE
, &NEG_FIVE
, &NEG_THREE
,
1003 &NEG_FOUR
, &NEG_FIVE
, &NEG_FOUR
,
1004 &NEG_FIVE
, &NEG_FIVE
, &ZERO
,
1005 &NEG_SIX
, &NEG_FIVE
, &NEG_ONE
,
1006 &NINETY_NINE
, &FIVE
, &FOUR
,
1007 &HUNDRED
, &FIVE
, &ZERO
,
1008 &HUNDRED_ONE
, &FIVE
, &ONE
,
1009 &BIG_FIVEm1
, &FIVE
, &FOUR
,
1010 &BIG_FIVE
, &FIVE
, &ZERO
,
1011 &BIG_FIVEp1
, &FIVE
, &ONE
1013 const int TUPLE_WIDTH
= 3;
1014 const int TUPLE_COUNT
= (int)(sizeof(tuples
)/sizeof(tuples
[0]))/TUPLE_WIDTH
;
1015 for (int i
= 0; i
< TUPLE_COUNT
; ++i
) {
1016 const llong lhs
= *tuples
[i
*TUPLE_WIDTH
+0];
1017 const llong rhs
= *tuples
[i
*TUPLE_WIDTH
+1];
1018 const llong ans
= *tuples
[i
*TUPLE_WIDTH
+2];
1021 if (!((n
%= rhs
) == ans
)) {
1022 errln("fail: (n %= rhs) == ans");
1027 LLAssert((n
% rhs
) == ans
);
1032 logln("Testing pow");
1034 LLAssert(llong(0, 0).pow(0) == llong(0, 0));
1035 LLAssert(llong(0, 0).pow(2) == llong(0, 0));
1036 LLAssert(llong(0, 2).pow(0) == llong(0, 1));
1037 LLAssert(llong(0, 2).pow(2) == llong(0, 4));
1038 LLAssert(llong(0, 2).pow(32) == llong(1, 0));
1039 LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5));
1043 const llong
n(0xffffffff,0xffffffff);
1044 LLAssert(n
.abs() == llong(0, 1));
1048 logln("Testing atoll");
1050 const char empty
[] = "";
1051 const char zero
[] = "0";
1052 const char neg_one
[] = "-1";
1053 const char neg_12345
[] = "-12345";
1054 const char big1
[] = "123456789abcdef0";
1055 const char big2
[] = "fFfFfFfFfFfFfFfF";
1056 LLAssert(llong::atoll(empty
) == llong(0, 0));
1057 LLAssert(llong::atoll(zero
) == llong(0, 0));
1058 LLAssert(llong::atoll(neg_one
) == llong(0xffffffff, 0xffffffff));
1059 LLAssert(llong::atoll(neg_12345
) == -llong(0, 12345));
1060 LLAssert(llong::atoll(big1
, 16) == llong(0x12345678, 0x9abcdef0));
1061 LLAssert(llong::atoll(big2
, 16) == llong(0xffffffff, 0xffffffff));
1065 const UChar uempty
[] = { 0 };
1066 const UChar uzero
[] = { 0x30, 0 };
1067 const UChar uneg_one
[] = { 0x2d, 0x31, 0 };
1068 const UChar uneg_12345
[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 };
1069 const UChar ubig1
[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 };
1070 const UChar ubig2
[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 };
1071 LLAssert(llong::utoll(uempty
) == llong(0, 0));
1072 LLAssert(llong::utoll(uzero
) == llong(0, 0));
1073 LLAssert(llong::utoll(uneg_one
) == llong(0xffffffff, 0xffffffff));
1074 LLAssert(llong::utoll(uneg_12345
) == -llong(0, 12345));
1075 LLAssert(llong::utoll(ubig1
, 16) == llong(0x12345678, 0x9abcdef0));
1076 LLAssert(llong::utoll(ubig2
, 16) == llong(0xffffffff, 0xffffffff));
1079 logln("Testing lltoa");
1082 char buf
[64]; // ascii
1083 LLAssert((llong(0, 0).lltoa(buf
, (uint32_t)sizeof(buf
)) == 1) && (strcmp(buf
, zero
) == 0));
1084 LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf
, (uint32_t)sizeof(buf
)) == 2) && (strcmp(buf
, neg_one
) == 0));
1085 LLAssert(((-llong(0, 12345)).lltoa(buf
, (uint32_t)sizeof(buf
)) == 6) && (strcmp(buf
, neg_12345
) == 0));
1086 LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf
, (uint32_t)sizeof(buf
), 16) == 16) && (strcmp(buf
, big1
) == 0));
1090 logln("Testing u_lltoa");
1094 LLAssert((llong(0, 0).lltou(buf
, (uint32_t)sizeof(buf
)) == 1) && (u_strcmp(buf
, uzero
) == 0));
1095 LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf
, (uint32_t)sizeof(buf
)) == 2) && (u_strcmp(buf
, uneg_one
) == 0));
1096 LLAssert(((-llong(0, 12345)).lltou(buf
, (uint32_t)sizeof(buf
)) == 6) && (u_strcmp(buf
, uneg_12345
) == 0));
1097 LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf
, (uint32_t)sizeof(buf
), 16) == 16) && (u_strcmp(buf
, ubig1
) == 0));
1105 IntlTestRBNF::TestEnglishSpellout()
1107 UErrorCode status
= U_ZERO_ERROR
;
1108 RuleBasedNumberFormat
* formatter
1109 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
1110 if (U_FAILURE(status
)) {
1111 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1113 static const char* const testData
[][2] = {
1116 { "15", "fifteen" },
1118 { "23", "twenty-three" },
1119 { "73", "seventy-three" },
1120 { "88", "eighty-eight" },
1121 { "100", "one hundred" },
1122 { "106", "one hundred six" },
1123 { "127", "one hundred twenty-seven" },
1124 { "200", "two hundred" },
1125 { "579", "five hundred seventy-nine" },
1126 { "1,000", "one thousand" },
1127 { "2,000", "two thousand" },
1128 { "3,004", "three thousand four" },
1129 { "4,567", "four thousand five hundred sixty-seven" },
1130 { "15,943", "fifteen thousand nine hundred forty-three" },
1131 { "2,345,678", "two million three hundred forty-five thousand six hundred seventy-eight" },
1132 { "-36", "minus thirty-six" },
1133 { "234.567", "two hundred thirty-four point five six seven" },
1137 doTest(formatter
, testData
, TRUE
);
1139 #if !UCONFIG_NO_COLLATION
1140 formatter
->setLenient(TRUE
);
1141 static const char* lpTestData
[][2] = {
1142 { "fifty-7", "57" },
1143 { " fifty-7", "57" },
1144 { " fifty-7", "57" },
1145 { "2 thousand six HUNDRED fifty-7", "2,657" },
1146 { "fifteen hundred and zero", "1,500" },
1147 { "FOurhundred thiRTY six", "436" },
1150 doLenientParseTest(formatter
, lpTestData
);
1157 IntlTestRBNF::TestOrdinalAbbreviations()
1159 UErrorCode status
= U_ZERO_ERROR
;
1160 RuleBasedNumberFormat
* formatter
1161 = new RuleBasedNumberFormat(URBNF_ORDINAL
, Locale::getUS(), status
);
1163 if (U_FAILURE(status
)) {
1164 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1166 static const char* const testData
[][2] = {
1183 { "12,345", "12,345th" },
1187 doTest(formatter
, testData
, FALSE
);
1193 IntlTestRBNF::TestDurations()
1195 UErrorCode status
= U_ZERO_ERROR
;
1196 RuleBasedNumberFormat
* formatter
1197 = new RuleBasedNumberFormat(URBNF_DURATION
, Locale::getUS(), status
);
1199 if (U_FAILURE(status
)) {
1200 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1202 static const char* const testData
[][2] = {
1203 { "3,600", "1:00:00" }, //move me and I fail
1206 { "24", "24 sec." },
1211 // { "3,600", "1:00:00" },
1212 { "3,740", "1:02:20" },
1213 { "10,293", "2:51:33" },
1217 doTest(formatter
, testData
, TRUE
);
1219 #if !UCONFIG_NO_COLLATION
1220 formatter
->setLenient(TRUE
);
1221 static const char* lpTestData
[][2] = {
1222 { "2-51-33", "10,293" },
1225 doLenientParseTest(formatter
, lpTestData
);
1232 IntlTestRBNF::TestSpanishSpellout()
1234 UErrorCode status
= U_ZERO_ERROR
;
1235 RuleBasedNumberFormat
* formatter
1236 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("es", "ES", ""), status
);
1238 if (U_FAILURE(status
)) {
1239 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1241 static const char* const testData
[][2] = {
1244 { "16", "diecis\\u00e9is" },
1246 { "24", "veinticuatro" },
1247 { "26", "veintis\\u00e9is" },
1248 { "73", "setenta y tres" },
1249 { "88", "ochenta y ocho" },
1251 { "106", "ciento seis" },
1252 { "127", "ciento veintisiete" },
1253 { "200", "doscientos" },
1254 { "579", "quinientos setenta y nueve" },
1256 { "2,000", "dos mil" },
1257 { "3,004", "tres mil cuatro" },
1258 { "4,567", "cuatro mil quinientos sesenta y siete" },
1259 { "15,943", "quince mil novecientos cuarenta y tres" },
1260 { "2,345,678", "dos millones trescientos cuarenta y cinco mil seiscientos setenta y ocho"},
1261 { "-36", "menos treinta y seis" },
1262 { "234.567", "doscientos treinta y cuatro coma cinco seis siete" },
1266 doTest(formatter
, testData
, TRUE
);
1272 IntlTestRBNF::TestFrenchSpellout()
1274 UErrorCode status
= U_ZERO_ERROR
;
1275 RuleBasedNumberFormat
* formatter
1276 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getFrance(), status
);
1278 if (U_FAILURE(status
)) {
1279 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1281 static const char* const testData
[][2] = {
1285 { "21", "vingt-et-un" },
1286 { "23", "vingt-trois" },
1287 { "62", "soixante-deux" },
1288 { "70", "soixante-dix" },
1289 { "71", "soixante-et-onze" },
1290 { "73", "soixante-treize" },
1291 { "80", "quatre-vingts" },
1292 { "88", "quatre-vingt-huit" },
1294 { "106", "cent-six" },
1295 { "127", "cent-vingt-sept" },
1296 { "200", "deux-cents" },
1297 { "579", "cinq-cent-soixante-dix-neuf" },
1298 { "1,000", "mille" },
1299 { "1,123", "mille-cent-vingt-trois" },
1300 { "1,594", "mille-cinq-cent-quatre-vingt-quatorze" },
1301 { "2,000", "deux-mille" },
1302 { "3,004", "trois-mille-quatre" },
1303 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1304 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1305 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-soixante-dix-huit" },
1306 { "-36", "moins trente-six" },
1307 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
1311 doTest(formatter
, testData
, TRUE
);
1313 #if !UCONFIG_NO_COLLATION
1314 formatter
->setLenient(TRUE
);
1315 static const char* lpTestData
[][2] = {
1316 { "trente-et-un", "31" },
1317 { "un cent quatre vingt dix huit", "198" },
1320 doLenientParseTest(formatter
, lpTestData
);
1326 static const char* const swissFrenchTestData
[][2] = {
1330 { "21", "vingt-et-un" },
1331 { "23", "vingt-trois" },
1332 { "62", "soixante-deux" },
1333 { "70", "septante" },
1334 { "71", "septante-et-un" },
1335 { "73", "septante-trois" },
1336 { "80", "huitante" },
1337 { "88", "huitante-huit" },
1339 { "106", "cent-six" },
1340 { "127", "cent-vingt-sept" },
1341 { "200", "deux-cents" },
1342 { "579", "cinq-cent-septante-neuf" },
1343 { "1,000", "mille" },
1344 { "1,123", "mille-cent-vingt-trois" },
1345 { "1,594", "mille-cinq-cent-nonante-quatre" },
1346 { "2,000", "deux-mille" },
1347 { "3,004", "trois-mille-quatre" },
1348 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1349 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1350 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-septante-huit" },
1351 { "-36", "moins trente-six" },
1352 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
1357 IntlTestRBNF::TestSwissFrenchSpellout()
1359 UErrorCode status
= U_ZERO_ERROR
;
1360 RuleBasedNumberFormat
* formatter
1361 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "CH", ""), status
);
1363 if (U_FAILURE(status
)) {
1364 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1366 doTest(formatter
, swissFrenchTestData
, TRUE
);
1371 static const char* const belgianFrenchTestData
[][2] = {
1375 { "21", "vingt-et-un" },
1376 { "23", "vingt-trois" },
1377 { "62", "soixante-deux" },
1378 { "70", "septante" },
1379 { "71", "septante-et-un" },
1380 { "73", "septante-trois" },
1381 { "80", "quatre-vingts" },
1382 { "88", "quatre-vingt-huit" },
1383 { "90", "nonante" },
1384 { "91", "nonante-et-un" },
1385 { "95", "nonante-cinq" },
1387 { "106", "cent-six" },
1388 { "127", "cent-vingt-sept" },
1389 { "200", "deux-cents" },
1390 { "579", "cinq-cent-septante-neuf" },
1391 { "1,000", "mille" },
1392 { "1,123", "mille-cent-vingt-trois" },
1393 { "1,594", "mille-cinq-cent-nonante-quatre" },
1394 { "2,000", "deux-mille" },
1395 { "3,004", "trois-mille-quatre" },
1396 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1397 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1398 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-septante-huit" },
1399 { "-36", "moins trente-six" },
1400 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
1406 IntlTestRBNF::TestBelgianFrenchSpellout()
1408 UErrorCode status
= U_ZERO_ERROR
;
1409 RuleBasedNumberFormat
* formatter
1410 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "BE", ""), status
);
1412 if (U_FAILURE(status
)) {
1413 errcheckln(status
, "rbnf status: 0x%x (%s)\n", status
, u_errorName(status
));
1414 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1416 // Belgian french should match Swiss french.
1417 doTest(formatter
, belgianFrenchTestData
, TRUE
);
1423 IntlTestRBNF::TestItalianSpellout()
1425 UErrorCode status
= U_ZERO_ERROR
;
1426 RuleBasedNumberFormat
* formatter
1427 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getItalian(), status
);
1429 if (U_FAILURE(status
)) {
1430 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1432 static const char* const testData
[][2] = {
1434 { "15", "quindici" },
1436 { "23", "venti\\u00ADtr\\u00E9" },
1437 { "73", "settanta\\u00ADtr\\u00E9" },
1438 { "88", "ottant\\u00ADotto" },
1440 { "101", "cent\\u00ADuno" },
1441 { "103", "cento\\u00ADtr\\u00E9" },
1442 { "106", "cento\\u00ADsei" },
1443 { "108", "cent\\u00ADotto" },
1444 { "127", "cento\\u00ADventi\\u00ADsette" },
1445 { "181", "cent\\u00ADottant\\u00ADuno" },
1446 { "200", "due\\u00ADcento" },
1447 { "579", "cinque\\u00ADcento\\u00ADsettanta\\u00ADnove" },
1448 { "1,000", "mille" },
1449 { "2,000", "due\\u00ADmila" },
1450 { "3,004", "tre\\u00ADmila\\u00ADquattro" },
1451 { "4,567", "quattro\\u00ADmila\\u00ADcinque\\u00ADcento\\u00ADsessanta\\u00ADsette" },
1452 { "15,943", "quindici\\u00ADmila\\u00ADnove\\u00ADcento\\u00ADquaranta\\u00ADtr\\u00E9" },
1453 { "-36", "meno trenta\\u00ADsei" },
1454 { "234.567", "due\\u00ADcento\\u00ADtrenta\\u00ADquattro virgola cinque sei sette" },
1458 doTest(formatter
, testData
, TRUE
);
1464 IntlTestRBNF::TestPortugueseSpellout()
1466 UErrorCode status
= U_ZERO_ERROR
;
1467 RuleBasedNumberFormat
* formatter
1468 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("pt","BR",""), status
);
1470 if (U_FAILURE(status
)) {
1471 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1473 static const char* const testData
[][2] = {
1477 { "23", "vinte e tr\\u00EAs" },
1478 { "73", "setenta e tr\\u00EAs" },
1479 { "88", "oitenta e oito" },
1481 { "106", "cento e seis" },
1482 { "108", "cento e oito" },
1483 { "127", "cento e vinte e sete" },
1484 { "181", "cento e oitenta e um" },
1485 { "200", "duzcentos" },
1486 { "579", "quinhentos e setenta e nove" },
1488 { "2,000", "dois mil" },
1489 { "3,004", "tr\\u00EAs mil e quatro" },
1490 { "4,567", "quatro mil e quinhentos e sessenta e sete" },
1491 { "15,943", "quinze mil e novecentos e quarenta e tr\\u00EAs" },
1492 { "-36", "menos trinta e seis" },
1493 { "234.567", "duzcentos e trinta e quatro v\\u00EDrgula cinco seis sete" },
1497 doTest(formatter
, testData
, TRUE
);
1502 IntlTestRBNF::TestGermanSpellout()
1504 UErrorCode status
= U_ZERO_ERROR
;
1505 RuleBasedNumberFormat
* formatter
1506 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getGermany(), status
);
1508 if (U_FAILURE(status
)) {
1509 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1511 static const char* const testData
[][2] = {
1513 { "15", "f\\u00fcnfzehn" },
1514 { "20", "zwanzig" },
1515 { "23", "drei\\u00ADund\\u00ADzwanzig" },
1516 { "73", "drei\\u00ADund\\u00ADsiebzig" },
1517 { "88", "acht\\u00ADund\\u00ADachtzig" },
1518 { "100", "ein\\u00ADhundert" },
1519 { "106", "ein\\u00ADhundert\\u00ADsechs" },
1520 { "127", "ein\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADzwanzig" },
1521 { "200", "zwei\\u00ADhundert" },
1522 { "579", "f\\u00fcnf\\u00ADhundert\\u00ADneun\\u00ADund\\u00ADsiebzig" },
1523 { "1,000", "ein\\u00ADtausend" },
1524 { "2,000", "zwei\\u00ADtausend" },
1525 { "3,004", "drei\\u00ADtausend\\u00ADvier" },
1526 { "4,567", "vier\\u00ADtausend\\u00ADf\\u00fcnf\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADsechzig" },
1527 { "15,943", "f\\u00fcnfzehn\\u00ADtausend\\u00ADneun\\u00ADhundert\\u00ADdrei\\u00ADund\\u00ADvierzig" },
1528 { "2,345,678", "zwei Millionen drei\\u00ADhundert\\u00ADf\\u00fcnf\\u00ADund\\u00ADvierzig\\u00ADtausend\\u00ADsechs\\u00ADhundert\\u00ADacht\\u00ADund\\u00ADsiebzig" },
1532 doTest(formatter
, testData
, TRUE
);
1534 #if !UCONFIG_NO_COLLATION
1535 formatter
->setLenient(TRUE
);
1536 static const char* lpTestData
[][2] = {
1537 { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
1540 doLenientParseTest(formatter
, lpTestData
);
1547 IntlTestRBNF::TestThaiSpellout()
1549 UErrorCode status
= U_ZERO_ERROR
;
1550 RuleBasedNumberFormat
* formatter
1551 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("th"), status
);
1553 if (U_FAILURE(status
)) {
1554 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1556 static const char* const testData
[][2] = {
1557 { "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" },
1558 { "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1559 { "10", "\\u0e2a\\u0e34\\u0e1a" },
1560 { "11", "\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1561 { "21", "\\u0e22\\u0e35\\u0e48\\u200b\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1562 { "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e23\\u0e49\\u0e2d\\u0e22\\u200b\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1563 { "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e08\\u0e38\\u0e14\\u200b\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" },
1567 doTest(formatter
, testData
, TRUE
);
1573 IntlTestRBNF::TestSwedishSpellout()
1575 UErrorCode status
= U_ZERO_ERROR
;
1576 RuleBasedNumberFormat
* formatter
1577 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("sv"), status
);
1579 if (U_FAILURE(status
)) {
1580 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1582 static const char* testDataDefault
[][2] = {
1583 { "101", "ett\\u00adhundra\\u00adett" },
1584 { "123", "ett\\u00adhundra\\u00adtjugo\\u00adtre" },
1585 { "1,001", "et\\u00adtusen ett" },
1586 { "1,100", "et\\u00adtusen ett\\u00adhundra" },
1587 { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1588 { "1,234", "et\\u00adtusen tv\\u00e5\\u00adhundra\\u00adtrettio\\u00adfyra" },
1589 { "10,001", "tio\\u00adtusen ett" },
1590 { "11,000", "elva\\u00adtusen" },
1591 { "12,000", "tolv\\u00adtusen" },
1592 { "20,000", "tjugo\\u00adtusen" },
1593 { "21,000", "tjugo\\u00adet\\u00adtusen" },
1594 { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
1595 { "200,000", "tv\\u00e5\\u00adhundra\\u00adtusen" },
1596 { "201,000", "tv\\u00e5\\u00adhundra\\u00adet\\u00adtusen" },
1597 { "200,200", "tv\\u00e5\\u00adhundra\\u00adtusen tv\\u00e5\\u00adhundra" },
1598 { "2,002,000", "tv\\u00e5 miljoner tv\\u00e5\\u00adtusen" },
1599 { "12,345,678", "tolv miljoner tre\\u00adhundra\\u00adfyrtio\\u00adfem\\u00adtusen sex\\u00adhundra\\u00adsjuttio\\u00ad\\u00e5tta" },
1600 { "123,456.789", "ett\\u00adhundra\\u00adtjugo\\u00adtre\\u00adtusen fyra\\u00adhundra\\u00adfemtio\\u00adsex komma sju \\u00e5tta nio" },
1601 { "-12,345.678", "minus tolv\\u00adtusen tre\\u00adhundra\\u00adfyrtio\\u00adfem komma sex sju \\u00e5tta" },
1604 doTest(formatter
, testDataDefault
, TRUE
);
1606 static const char* testDataNeutrum
[][2] = {
1607 { "101", "ett\\u00adhundra\\u00aden" },
1608 { "1,001", "ettusen en" },
1609 { "1,101", "ettusen ett\\u00adhundra\\u00aden" },
1610 { "10,001", "tio\\u00adtusen en" },
1611 { "21,001", "tjugo\\u00aden\\u00adtusen en" },
1615 formatter
->setDefaultRuleSet("%spellout-cardinal-neutre", status
);
1616 if (U_SUCCESS(status
)) {
1617 logln(" testing spellout-cardinal-neutre rules");
1618 doTest(formatter
, testDataNeutrum
, TRUE
);
1621 errln("Can't test spellout-cardinal-neutre rules");
1624 static const char* testDataYear
[][2] = {
1625 { "101", "ett\\u00adhundra\\u00adett" },
1626 { "900", "nio\\u00adhundra" },
1627 { "1,001", "et\\u00adtusen ett" },
1628 { "1,100", "elva\\u00adhundra" },
1629 { "1,101", "elva\\u00adhundra\\u00adett" },
1630 { "1,234", "tolv\\u00adhundra\\u00adtrettio\\u00adfyra" },
1631 { "2,001", "tjugo\\u00adhundra\\u00adett" },
1632 { "10,001", "tio\\u00adtusen ett" },
1636 formatter
->setDefaultRuleSet("%spellout-numbering-year", status
);
1637 if (U_SUCCESS(status
)) {
1638 logln("testing year rules");
1639 doTest(formatter
, testDataYear
, TRUE
);
1642 errln("Can't test year rules");
1650 IntlTestRBNF::TestSmallValues()
1652 UErrorCode status
= U_ZERO_ERROR
;
1653 RuleBasedNumberFormat
* formatter
1654 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("en_US"), status
);
1656 if (U_FAILURE(status
)) {
1657 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1659 static const char* const testDataDefault
[][2] = {
1660 { "0.001", "zero point zero zero one" },
1661 { "0.0001", "zero point zero zero zero one" },
1662 { "0.00001", "zero point zero zero zero zero one" },
1663 { "0.000001", "zero point zero zero zero zero zero one" },
1664 { "0.0000001", "zero point zero zero zero zero zero zero one" },
1665 { "0.00000001", "zero point zero zero zero zero zero zero zero one" },
1666 { "0.000000001", "zero point zero zero zero zero zero zero zero zero one" },
1667 { "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" },
1668 { "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" },
1669 { "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" },
1670 { "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" },
1671 { "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1672 { "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1673 { "10,000,000.001", "ten million point zero zero one" },
1674 { "10,000,000.0001", "ten million point zero zero zero one" },
1675 { "10,000,000.00001", "ten million point zero zero zero zero one" },
1676 { "10,000,000.000001", "ten million point zero zero zero zero zero one" },
1677 { "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" },
1678 // { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" },
1679 // { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" },
1680 { "10,000,000", "ten million" },
1681 // { "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" },
1682 // { "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" },
1683 // { "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" },
1684 { "1,234,567.7654321", "one million two hundred thirty-four thousand five hundred sixty-seven point seven six five four three two one" },
1685 { "123,456.654321", "one hundred twenty-three thousand four hundred fifty-six point six five four three two one" },
1686 { "12,345.54321", "twelve thousand three hundred forty-five point five four three two one" },
1687 { "1,234.4321", "one thousand two hundred thirty-four point four three two one" },
1688 { "123.321", "one hundred twenty-three point three two one" },
1689 { "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" },
1690 { "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" },
1694 doTest(formatter
, testDataDefault
, TRUE
);
1701 IntlTestRBNF::TestLocalizations(void)
1704 UnicodeString
rules("%main:0:no;1:some;100:a lot;1000:tons;\n"
1705 "%other:0:nada;1:yah, some;100:plenty;1000:more'n you'll ever need");
1707 UErrorCode status
= U_ZERO_ERROR
;
1709 RuleBasedNumberFormat
formatter(rules
, perror
, status
);
1710 if (U_FAILURE(status
)) {
1711 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1714 static const char* const testData
[][2] = {
1716 { "5", "yah, some" },
1717 { "423", "plenty" },
1718 { "12345", "more'n you'll ever need" },
1721 doTest(&formatter
, testData
, FALSE
);
1725 UnicodeString
loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>");
1726 static const char* const testData
[][2] = {
1730 { "12345", "tons" },
1733 RuleBasedNumberFormat
formatter0(rules
, loc
, perror
, status
);
1734 if (U_FAILURE(status
)) {
1735 errln("failed to build second formatter");
1737 doTest(&formatter0
, testData
, FALSE
);
1740 // exercise localization info
1741 Locale
locale0("en__VALLEY@turkey=gobblegobble");
1742 Locale
locale1("de_DE_FOO");
1743 Locale
locale2("ja_JP");
1744 UnicodeString name
= formatter0
.getRuleSetName(0);
1745 if ( formatter0
.getRuleSetDisplayName(0, locale0
) == "Main"
1746 && formatter0
.getRuleSetDisplayName(0, locale1
) == "das Main"
1747 && formatter0
.getRuleSetDisplayName(0, locale2
) == "%main"
1748 && formatter0
.getRuleSetDisplayName(name
, locale0
) == "Main"
1749 && formatter0
.getRuleSetDisplayName(name
, locale1
) == "das Main"
1750 && formatter0
.getRuleSetDisplayName(name
, locale2
) == "%main"){
1751 logln("getRuleSetDisplayName tested");
1753 errln("failed to getRuleSetDisplayName");
1757 for (i
= 0; i
< formatter0
.getNumberOfRuleSetDisplayNameLocales(); ++i
) {
1758 Locale locale
= formatter0
.getRuleSetDisplayNameLocale(i
, status
);
1759 if (U_SUCCESS(status
)) {
1760 for (int j
= 0; j
< formatter0
.getNumberOfRuleSetNames(); ++j
) {
1761 UnicodeString name
= formatter0
.getRuleSetName(j
);
1762 UnicodeString lname
= formatter0
.getRuleSetDisplayName(j
, locale
);
1763 UnicodeString msg
= locale
.getName();
1776 static const char* goodLocs
[] = {
1777 "", // zero-length ok, same as providing no localization data
1778 "<<>>", // no public rule sets ok
1779 "<<%main>>", // no localizations ok
1780 "<<%main,>,<en, Main,>>", // comma before close angle ok
1781 "<<%main>,<en, ',<>\" '>>", // quotes everything until next quote
1782 "<<%main>,<'en', \"it's ok\">>", // double quotes work too
1783 " \n <\n <\n %main\n >\n , \t <\t en\t , \tfoo \t\t > \n\n > \n ", // rule whitespace ok
1785 int32_t goodLocsLen
= sizeof(goodLocs
)/sizeof(goodLocs
[0]);
1787 static const char* badLocs
[] = {
1788 " ", // non-zero length
1789 "<>", // empty array
1790 "<", // unclosed outer array
1791 "<<", // unclosed inner array
1792 "<<,>>", // unexpected comma
1793 "<<''>>", // empty string
1794 " x<<%main>>", // first non space char not open angle bracket
1795 "<%main>", // missing inner array
1796 "<<%main %other>>", // elements missing separating commma (spaces must be quoted)
1797 "<<%main><en, Main>>", // arrays missing separating comma
1798 "<<%main>,<en, main, foo>>", // too many elements in locale data
1799 "<<%main>,<en>>", // too few elements in locale data
1800 "<<<%main>>>", // unexpected open angle
1801 "<<%main<>>>", // unexpected open angle
1802 "<<%main, %other>,<en,,>>", // implicit empty strings
1803 "<<%main>,<en,''>>", // empty string
1804 "<<%main>, < en, '>>", // unterminated quote
1805 "<<%main>, < en, \"<>>", // unterminated quote
1806 "<<%main\">>", // quote in string
1807 "<<%main'>>", // quote in string
1808 "<<%main<>>", // open angle in string
1809 "<<%main>> x", // extra non-space text at end
1812 int32_t badLocsLen
= sizeof(badLocs
)/sizeof(badLocs
[0]);
1814 for (i
= 0; i
< goodLocsLen
; ++i
) {
1815 logln("[%d] '%s'", i
, goodLocs
[i
]);
1816 UErrorCode status
= U_ZERO_ERROR
;
1817 UnicodeString
loc(goodLocs
[i
]);
1818 RuleBasedNumberFormat
fmt(rules
, loc
, perror
, status
);
1819 if (U_FAILURE(status
)) {
1820 errln("Failed parse of good localization string: '%s'", goodLocs
[i
]);
1824 for (i
= 0; i
< badLocsLen
; ++i
) {
1825 logln("[%d] '%s'", i
, badLocs
[i
]);
1826 UErrorCode status
= U_ZERO_ERROR
;
1827 UnicodeString
loc(badLocs
[i
]);
1828 RuleBasedNumberFormat
fmt(rules
, loc
, perror
, status
);
1829 if (U_SUCCESS(status
)) {
1830 errln("Successful parse of bad localization string: '%s'", badLocs
[i
]);
1838 IntlTestRBNF::TestAllLocales()
1840 const char* names
[] = {
1845 double numbers
[] = {45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111};
1847 // RBNF parse is extremely slow when lenient option is enabled.
1848 // For non-exhaustive mode, we only test a few locales.
1849 const char* parseLocales
[] = {"en_US", "nl_NL", "be", NULL
};
1853 const Locale
* locales
= Locale::getAvailableLocales(count
);
1854 for (int i
= 0; i
< count
; ++i
) {
1855 const Locale
* loc
= &locales
[i
];
1856 UBool testParse
= TRUE
;
1859 for (int k
= 0; parseLocales
[k
] != NULL
; k
++) {
1860 if (strcmp(loc
->getLanguage(), parseLocales
[k
]) == 0) {
1867 for (int j
= 0; j
< 3; ++j
) {
1868 UErrorCode status
= U_ZERO_ERROR
;
1869 RuleBasedNumberFormat
* f
= new RuleBasedNumberFormat((URBNFRuleSetTag
)j
, *loc
, status
);
1870 if (U_FAILURE(status
)) {
1871 errln(UnicodeString(loc
->getName()) + names
[j
]
1872 + "ERROR could not instantiate -> " + u_errorName(status
));
1875 #if !UCONFIG_NO_COLLATION
1876 for (unsigned int numidx
= 0; numidx
< sizeof(numbers
)/sizeof(double); numidx
++) {
1877 double n
= numbers
[numidx
];
1881 logln(UnicodeString(loc
->getName()) + names
[j
]
1882 + "success: " + n
+ " -> " + str
);
1885 // We do not validate the result in this test case,
1886 // because there are cases which do not round trip by design.
1890 status
= U_ZERO_ERROR
;
1891 f
->setLenient(FALSE
);
1892 f
->parse(str
, num
, status
);
1893 if (U_FAILURE(status
)) {
1894 //TODO: We need to fix parse problems - see #6895 / #6896
1895 if (status
== U_INVALID_FORMAT_ERROR
) {
1896 logln(UnicodeString(loc
->getName()) + names
[j
]
1897 + "WARNING could not parse '" + str
+ "' -> " + u_errorName(status
));
1899 errln(UnicodeString(loc
->getName()) + names
[j
]
1900 + "ERROR could not parse '" + str
+ "' -> " + u_errorName(status
));
1904 status
= U_ZERO_ERROR
;
1905 f
->setLenient(TRUE
);
1906 f
->parse(str
, num
, status
);
1907 if (U_FAILURE(status
)) {
1908 //TODO: We need to fix parse problems - see #6895 / #6896
1909 if (status
== U_INVALID_FORMAT_ERROR
) {
1910 logln(UnicodeString(loc
->getName()) + names
[j
]
1911 + "WARNING could not parse(lenient) '" + str
+ "' -> " + u_errorName(status
));
1913 errln(UnicodeString(loc
->getName()) + names
[j
]
1914 + "ERROR could not parse(lenient) '" + str
+ "' -> " + u_errorName(status
));
1926 IntlTestRBNF::TestMultiplierSubstitution(void) {
1927 UnicodeString
rules("=#,##0=;1,000,000: <##0.###< million;");
1928 UErrorCode status
= U_ZERO_ERROR
;
1929 UParseError parse_error
;
1930 RuleBasedNumberFormat
*rbnf
=
1931 new RuleBasedNumberFormat(rules
, Locale::getUS(), parse_error
, status
);
1932 if (U_SUCCESS(status
)) {
1935 double n
= 1234000.0;
1936 rbnf
->format(n
, res
, pos
);
1939 UnicodeString expected
= UNICODE_STRING_SIMPLE("1.234 million");
1940 if (expected
!= res
) {
1941 UnicodeString msg
= "Expected: ";
1942 msg
.append(expected
);
1943 msg
.append(" but got ");
1951 IntlTestRBNF::doTest(RuleBasedNumberFormat
* formatter
, const char* const testData
[][2], UBool testParsing
)
1953 // man, error reporting would be easier with printf-style syntax for unicode string and formattable
1955 UErrorCode status
= U_ZERO_ERROR
;
1956 DecimalFormatSymbols
dfs("en", status
);
1957 // NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
1958 DecimalFormat
decFmt("#,###.################", dfs
, status
);
1959 if (U_FAILURE(status
)) {
1960 errcheckln(status
, "FAIL: could not create NumberFormat - %s", u_errorName(status
));
1962 for (int i
= 0; testData
[i
][0]; ++i
) {
1963 const char* numString
= testData
[i
][0];
1964 const char* expectedWords
= testData
[i
][1];
1966 log("[%i] %s = ", i
, numString
);
1967 Formattable expectedNumber
;
1968 decFmt
.parse(numString
, expectedNumber
, status
);
1969 if (U_FAILURE(status
)) {
1970 errln("FAIL: decFmt could not parse %s", numString
);
1973 UnicodeString actualString
;
1975 formatter
->format(expectedNumber
, actualString
/* , pos*/, status
);
1976 if (U_FAILURE(status
)) {
1977 UnicodeString msg
= "Fail: formatter could not format ";
1978 decFmt
.format(expectedNumber
, msg
, status
);
1982 UnicodeString expectedString
= UnicodeString(expectedWords
, -1, US_INV
).unescape();
1983 if (actualString
!= expectedString
) {
1984 UnicodeString msg
= "FAIL: check failed for ";
1985 decFmt
.format(expectedNumber
, msg
, status
);
1986 msg
.append(", expected ");
1987 msg
.append(expectedString
);
1988 msg
.append(" but got ");
1989 msg
.append(actualString
);
1993 logln(actualString
);
1995 Formattable parsedNumber
;
1996 formatter
->parse(actualString
, parsedNumber
, status
);
1997 if (U_FAILURE(status
)) {
1998 UnicodeString msg
= "FAIL: formatter could not parse ";
1999 msg
.append(actualString
);
2000 msg
.append(" status code: " );
2001 msg
.append(u_errorName(status
));
2005 if (parsedNumber
!= expectedNumber
) {
2006 UnicodeString msg
= "FAIL: parse failed for ";
2007 msg
.append(actualString
);
2008 msg
.append(", expected ");
2009 decFmt
.format(expectedNumber
, msg
, status
);
2010 msg
.append(", but got ");
2011 decFmt
.format(parsedNumber
, msg
, status
);
2025 IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat
* formatter
, const char* testData
[][2])
2027 UErrorCode status
= U_ZERO_ERROR
;
2028 NumberFormat
* decFmt
= NumberFormat::createInstance(Locale::getUS(), status
);
2029 if (U_FAILURE(status
)) {
2030 errcheckln(status
, "FAIL: could not create NumberFormat - %s", u_errorName(status
));
2032 for (int i
= 0; testData
[i
][0]; ++i
) {
2033 const char* spelledNumber
= testData
[i
][0]; // spelled-out number
2034 const char* asciiUSNumber
= testData
[i
][1]; // number as ascii digits formatted for US locale
2036 UnicodeString spelledNumberString
= UnicodeString(spelledNumber
).unescape();
2037 Formattable actualNumber
;
2038 formatter
->parse(spelledNumberString
, actualNumber
, status
);
2039 if (U_FAILURE(status
)) {
2040 UnicodeString msg
= "FAIL: formatter could not parse ";
2041 msg
.append(spelledNumberString
);
2045 // I changed the logic of this test somewhat from Java-- instead of comparing the
2046 // strings, I compare the Formattables. Hmmm, but the Formattables don't compare,
2047 // so change it back.
2049 UnicodeString asciiUSNumberString
= asciiUSNumber
;
2050 Formattable expectedNumber
;
2051 decFmt
->parse(asciiUSNumberString
, expectedNumber
, status
);
2052 if (U_FAILURE(status
)) {
2053 UnicodeString msg
= "FAIL: decFmt could not parse ";
2054 msg
.append(asciiUSNumberString
);
2058 UnicodeString actualNumberString
;
2059 UnicodeString expectedNumberString
;
2060 decFmt
->format(actualNumber
, actualNumberString
, status
);
2061 decFmt
->format(expectedNumber
, expectedNumberString
, status
);
2062 if (actualNumberString
!= expectedNumberString
) {
2063 UnicodeString msg
= "FAIL: parsing";
2064 msg
.append(asciiUSNumberString
);
2066 msg
.append(" lenient parse failed for ");
2067 msg
.append(spelledNumberString
);
2068 msg
.append(", expected ");
2069 msg
.append(expectedNumberString
);
2070 msg
.append(", but got ");
2071 msg
.append(actualNumberString
);
2086 IntlTestRBNF::TestRBNFDisabled() {
2087 errln("*** RBNF currently disabled on this platform ***\n");
2093 #endif /* #if !UCONFIG_NO_FORMATTING */