2 *******************************************************************************
3 * Copyright (C) 1996-2012, 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
);
69 TESTCASE(19, TestSetDecimalFormatSymbols
);
71 TESTCASE(0, TestRBNFDisabled
);
81 void IntlTestRBNF::TestHebrewFraction() {
83 // this is the expected output for 123.45, with no '<' in it.
85 0x05de, 0x05d0, 0x05d4, 0x0020,
86 0x05e2, 0x05e9, 0x05e8, 0x05d9, 0x05dd, 0x0020,
87 0x05d5, 0x05e9, 0x05dc, 0x05d5, 0x05e9, 0x0020,
88 0x05e0, 0x05e7, 0x05d5, 0x05d3, 0x05d4, 0x0020,
89 0x05d0, 0x05e8, 0x05d1, 0x05e2, 0x0020,
90 0x05d7, 0x05de, 0x05e9, 0x0000,
93 0x05DE, 0x05D0, 0x05D4, 0x0020,
94 0x05E2, 0x05E9, 0x05E8, 0x05D9, 0x05DD, 0x0020,
95 0x05D5, 0x05E9, 0x05DC, 0x05D5, 0x05E9, 0x0020,
96 0x05E0, 0x05E7, 0x05D5, 0x05D3, 0x05D4, 0x0020,
97 0x05D0, 0x05E4, 0x05E1, 0x0020,
98 0x05D0, 0x05E4, 0x05E1, 0x0020,
99 0x05D0, 0x05E8, 0x05D1, 0x05E2, 0x0020,
100 0x05D7, 0x05DE, 0x05E9, 0x0000,
102 UErrorCode status
= U_ZERO_ERROR
;
103 RuleBasedNumberFormat
* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT
, "he_IL", status
);
104 if (status
== U_MISSING_RESOURCE_ERROR
|| status
== U_FILE_ACCESS_ERROR
) {
105 errcheckln(status
, "Failed in constructing RuleBasedNumberFormat - %s", u_errorName(status
));
109 UnicodeString result
;
110 Formattable parseResult
;
113 UnicodeString
expected(text1
);
114 formatter
->format(123.45, result
);
115 if (result
!= expected
) {
116 errln((UnicodeString
)"expected '" + TestUtility::hex(expected
) + "'\nbut got: '" + TestUtility::hex(result
) + "'");
118 // formatter->parse(result, parseResult, pp);
119 // if (parseResult.getDouble() != 123.45) {
120 // errln("expected 123.45 but got: %g", parseResult.getDouble());
125 UnicodeString
expected(text2
);
127 formatter
->format(123.0045, result
);
128 if (result
!= expected
) {
129 errln((UnicodeString
)"expected '" + TestUtility::hex(expected
) + "'\nbut got: '" + TestUtility::hex(result
) + "'");
132 // formatter->parse(result, parseResult, pp);
133 // if (parseResult.getDouble() != 123.0045) {
134 // errln("expected 123.0045 but got: %g", parseResult.getDouble());
142 IntlTestRBNF::TestAPI() {
143 // This test goes through the APIs that were not tested before.
144 // These tests are too small to have separate test classes/functions
146 UErrorCode status
= U_ZERO_ERROR
;
147 RuleBasedNumberFormat
* formatter
148 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
149 if (status
== U_MISSING_RESOURCE_ERROR
|| status
== U_FILE_ACCESS_ERROR
) {
150 dataerrln("Unable to create formatter. - %s", u_errorName(status
));
155 logln("RBNF API test starting");
158 logln("Testing Clone");
159 RuleBasedNumberFormat
* rbnfClone
= (RuleBasedNumberFormat
*)formatter
->clone();
160 if(rbnfClone
!= NULL
) {
161 if(!(*rbnfClone
== *formatter
)) {
162 errln("Clone should be semantically equivalent to the original!");
166 errln("Cloning failed!");
172 logln("Testing assignment operator");
173 RuleBasedNumberFormat
assignResult(URBNF_SPELLOUT
, Locale("es", "ES", ""), status
);
174 assignResult
= *formatter
;
175 if(!(assignResult
== *formatter
)) {
176 errln("Assignment result should be semantically equivalent to the original!");
180 // test rule constructor
182 logln("Testing rule constructor");
183 LocalUResourceBundlePointer
en(ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
"rbnf", "en", &status
));
184 if(U_FAILURE(status
)) {
185 errln("Unable to access resource bundle with data!");
189 LocalUResourceBundlePointer
rbnfRules(ures_getByKey(en
.getAlias(), "RBNFRules", NULL
, &status
));
190 LocalUResourceBundlePointer
ruleSets(ures_getByKey(rbnfRules
.getAlias(), "SpelloutRules", NULL
, &status
));
192 while (ures_hasNext(ruleSets
.getAlias())) {
193 const UChar
* currentString
= ures_getNextString(ruleSets
.getAlias(), &len
, NULL
, &status
);
195 desc
.append(currentString
);
198 const UChar
*spelloutRules
= desc
.getTerminatedBuffer();
200 if(U_FAILURE(status
) || ruleLen
== 0 || spelloutRules
== NULL
) {
201 errln("Unable to access the rules string!");
204 RuleBasedNumberFormat
ruleCtorResult(spelloutRules
, Locale::getUS(), perror
, status
);
205 if(!(ruleCtorResult
== *formatter
)) {
206 errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
209 // Jitterbug 4452, for coverage
210 RuleBasedNumberFormat
nf(spelloutRules
, (UnicodeString
)"", Locale::getUS(), perror
, status
);
211 if(!(nf
== *formatter
)) {
212 errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
220 logln("Testing getRules function");
221 UnicodeString rules
= formatter
->getRules();
223 RuleBasedNumberFormat
fromRulesResult(rules
, Locale::getUS(), perror
, status
);
225 if(!(fromRulesResult
== *formatter
)) {
226 errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!");
232 logln("Testing copy constructor");
233 RuleBasedNumberFormat
copyCtorResult(*formatter
);
234 if(!(copyCtorResult
== *formatter
)) {
235 errln("Copy constructor result result should be semantically equivalent to the original!");
239 #if !UCONFIG_NO_COLLATION
241 #define NUMERIC_STRINGS_NOT_PARSEABLE 1 // ticket/8224
243 // test ruleset names
245 logln("Testing getNumberOfRuleSetNames, getRuleSetName and format using rule set names");
246 int32_t noOfRuleSetNames
= formatter
->getNumberOfRuleSetNames();
247 if(noOfRuleSetNames
== 0) {
248 errln("Number of rule set names should be more than zero");
250 UnicodeString ruleSetName
;
252 int32_t intFormatNum
= 34567;
253 double doubleFormatNum
= 893411.234;
254 logln("number of rule set names is %i", noOfRuleSetNames
);
255 for(i
= 0; i
< noOfRuleSetNames
; i
++) {
256 FieldPosition pos1
, pos2
;
257 UnicodeString intFormatResult
, doubleFormatResult
;
258 Formattable intParseResult
, doubleParseResult
;
259 #if NUMERIC_STRINGS_NOT_PARSEABLE
260 UBool parseDoubleNonLenientOK
= TRUE
;
261 UBool parseDoubleLenientOK
= TRUE
;
264 ruleSetName
= formatter
->getRuleSetName(i
);
265 log("Rule set name %i is ", i
);
267 logln(". Format results are: ");
268 intFormatResult
= formatter
->format(intFormatNum
, ruleSetName
, intFormatResult
, pos1
, status
);
269 doubleFormatResult
= formatter
->format(doubleFormatNum
, ruleSetName
, doubleFormatResult
, pos2
, status
);
270 if(U_FAILURE(status
)) {
271 errln("Format using a rule set failed");
274 logln(intFormatResult
);
275 logln(doubleFormatResult
);
277 #if NUMERIC_STRINGS_NOT_PARSEABLE
278 // "spellout-numbering-year" ruleSet produces (above) a numeric string using:
279 // "x.x: =#,###0.#=;"
280 // which will not parse (below) - we believe this is CORRECT behavior, as found in ICU 4.0 (see ticket/8224).
281 // Note this numeric string "89,3411.2" will not even parse with Lenient = TRUE because
282 // the NumberFormat (used as last-resort) in NFSubstitution::doParse fails.
283 UnicodeString numberingYear
= UNICODE_STRING_SIMPLE("spellout-numbering-year");
285 // "spellout-ordinal" and "spellout-ordinal-verbose" ruleSets produce (above) a numeric string using:
286 // "x.x: =#,##0.#=;" -> "893,411.2"
287 // which will not parse (below) with Lenient = FALSE, but does parse with Lenient = TRUE because
288 // NFSubstitution::doParse will succeed when using NumberFormat as last-resort.
289 UnicodeString ordinal
= UNICODE_STRING_SIMPLE("spellout-ordinal");
291 // RuleSets other than spellout-numbering-year and spellout-ordinalXXX produce fully spelled out text above
292 // which is fully parseable.
293 parseDoubleLenientOK
= ( ruleSetName
.indexOf(numberingYear
) == -1 );
294 parseDoubleNonLenientOK
= ( ruleSetName
.indexOf(numberingYear
) == -1 && ruleSetName
.indexOf(ordinal
) == -1 );
297 formatter
->setLenient(TRUE
);
298 formatter
->parse(intFormatResult
, intParseResult
, status
);
299 formatter
->parse(doubleFormatResult
, doubleParseResult
, status
);
301 logln("Parse results for lenient = TRUE, %i, %f", intParseResult
.getLong(), doubleParseResult
.getDouble());
303 #if NUMERIC_STRINGS_NOT_PARSEABLE
304 if((!parseDoubleLenientOK
) && (status
== U_INVALID_FORMAT_ERROR
)) {
305 status
= U_USING_FALLBACK_WARNING
;
306 logln("Clearing expected U_INVALID_FORMAT_ERROR during parsing");
310 formatter
->setLenient(FALSE
);
311 formatter
->parse(intFormatResult
, intParseResult
, status
);
312 formatter
->parse(doubleFormatResult
, doubleParseResult
, status
);
314 logln("Parse results for lenient = FALSE, %i, %f", intParseResult
.getLong(), doubleParseResult
.getDouble());
316 #if NUMERIC_STRINGS_NOT_PARSEABLE
317 if((!parseDoubleNonLenientOK
) && (status
== U_INVALID_FORMAT_ERROR
)) {
318 status
= U_USING_FALLBACK_WARNING
;
319 logln("Clearing expected U_INVALID_FORMAT_ERROR during parsing");
323 if(U_FAILURE(status
)) {
324 errln("Error during parsing");
327 intFormatResult
= formatter
->format(intFormatNum
, "BLABLA", intFormatResult
, pos1
, status
);
328 if(U_SUCCESS(status
)) {
329 errln("Using invalid rule set name should have failed");
332 status
= U_ZERO_ERROR
;
333 doubleFormatResult
= formatter
->format(doubleFormatNum
, "TRUC", doubleFormatResult
, pos2
, status
);
334 if(U_SUCCESS(status
)) {
335 errln("Using invalid rule set name should have failed");
338 status
= U_ZERO_ERROR
;
340 status
= U_ZERO_ERROR
;
345 UnicodeString
expected("four point five","");
346 logln("Testing format(double)");
347 UnicodeString result
;
348 formatter
->format(4.5,result
);
349 if(result
!= expected
) {
350 errln("Formatted 4.5, expected " + expected
+ " got " + result
);
352 logln("Formatted 4.5, expected " + expected
+ " got " + result
);
356 formatter
->format((int32_t)4,result
);
357 if(result
!= expected
) {
358 errln("Formatted 4, expected " + expected
+ " got " + result
);
360 logln("Formatted 4, expected " + expected
+ " got " + result
);
365 formatter
->format((int64_t)4, result
, pos
, status
= U_ZERO_ERROR
);
366 if(result
!= expected
) {
367 errln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
369 logln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
372 //Jitterbug 4452, for coverage
375 formatter
->format((int64_t)4, formatter
->getRuleSetName(0), result
, pos2
, status
= U_ZERO_ERROR
);
376 if(result
!= expected
) {
377 errln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
379 logln("Formatted 4 int64_t, expected " + expected
+ " got " + result
);
383 logln("Cleaning up");
387 void IntlTestRBNF::TestFractionalRuleSet()
389 UnicodeString
fracRules(
391 // this rule formats the number if it's 1 or more. It formats
392 // the integral part using a DecimalFormat ("#,##0" puts
393 // thousands separators in the right places) and the fractional
394 // part using %%frac. If there is no fractional part, it
395 // just shows the integral part.
396 " x.0: <#,##0<[ >%%frac>];\n"
397 // this rule formats the number if it's between 0 and 1. It
398 // shows only the fractional part (0.5 shows up as "1/2," not
401 // the fraction rule set. This works the same way as the one in the
402 // preceding example: We multiply the fractional part of the number
403 // being formatted by each rule's base value and use the rule that
404 // produces the result closest to 0 (or the first rule that produces 0).
405 // Since we only provide rules for the numbers from 2 to 10, we know
406 // we'll get a fraction with a denominator between 2 and 10.
407 // "<0<" causes the numerator of the fraction to be formatted
421 int len
= fracRules
.length();
423 for (int i
= 0; i
< len
; ++i
) {
424 UChar ch
= fracRules
.charAt(i
);
426 change
= 2; // change ok
427 } else if (ch
== ':') {
428 change
= 1; // change, but once we hit a non-space char, don't change
429 } else if (ch
== ' ') {
431 fracRules
.setCharAt(i
, (UChar
)0x200e);
440 UErrorCode status
= U_ZERO_ERROR
;
442 RuleBasedNumberFormat
formatter(fracRules
, Locale::getEnglish(), perror
, status
);
443 if (U_FAILURE(status
)) {
444 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
446 static const char* const testData
[][2] = {
459 { "3.125", "3 1/8" },
460 { "4.1428", "4 1/7" },
461 { "5.1667", "5 1/6" },
464 { "8.333", "8 1/3" },
469 { "1.2856", "1 2/7" },
472 doTest(&formatter
, testData
, FALSE
); // exact values aren't parsable from fractions
477 #define LLAssert(a) \
478 if (!(a)) errln("FAIL: " #a)
480 void IntlTestRBNF::TestLLongConstructors()
482 logln("Testing constructors");
484 // constant (shouldn't really be public)
485 LLAssert(llong(llong::kD32
).asDouble() == llong::kD32
);
487 // internal constructor (shouldn't really be public)
488 LLAssert(llong(0, 1).asDouble() == 1);
489 LLAssert(llong(1, 0).asDouble() == llong::kD32
);
490 LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1);
492 // public empty constructor
493 LLAssert(llong().asDouble() == 0);
495 // public int32_t constructor
496 LLAssert(llong((int32_t)0).asInt() == (int32_t)0);
497 LLAssert(llong((int32_t)1).asInt() == (int32_t)1);
498 LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1);
499 LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff);
500 LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1);
501 LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000);
503 // public int16_t constructor
504 LLAssert(llong((int16_t)0).asInt() == (int16_t)0);
505 LLAssert(llong((int16_t)1).asInt() == (int16_t)1);
506 LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1);
507 LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff);
508 LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff);
509 LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000);
511 // public int8_t constructor
512 LLAssert(llong((int8_t)0).asInt() == (int8_t)0);
513 LLAssert(llong((int8_t)1).asInt() == (int8_t)1);
514 LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1);
515 LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f);
516 LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff);
517 LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80);
519 // public uint16_t constructor
520 LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0);
521 LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1);
522 LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1);
523 LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff);
524 LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff);
525 LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000);
527 // public uint32_t constructor
528 LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0);
529 LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1);
530 LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1);
531 LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff);
532 LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1);
533 LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
535 // public double constructor
536 LLAssert(llong((double)0).asDouble() == (double)0);
537 LLAssert(llong((double)1).asDouble() == (double)1);
538 LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff);
539 LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000);
540 LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001);
542 // can't access uprv_maxmantissa, so fake it
543 double maxmantissa
= (llong((int32_t)1) << 40).asDouble();
544 LLAssert(llong(maxmantissa
).asDouble() == maxmantissa
);
545 LLAssert(llong(-maxmantissa
).asDouble() == -maxmantissa
);
548 LLAssert(llong(llong(0, 1)).asDouble() == 1);
549 LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32
);
550 LLAssert(llong(llong(-1, (uint32_t)-1)).asDouble() == -1);
552 // asInt - test unsigned to signed narrowing conversion
553 LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff);
554 LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000);
556 // asUInt - test signed to unsigned narrowing conversion
557 LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1);
558 LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
560 // asDouble already tested
564 void IntlTestRBNF::TestLLongSimpleOperators()
566 logln("Testing simple operators");
569 LLAssert(llong() == llong(0, 0));
570 LLAssert(llong(1,0) == llong(1, 0));
571 LLAssert(llong(0,1) == llong(0, 1));
574 LLAssert(llong(1,0) != llong(1,1));
575 LLAssert(llong(0,1) != llong(1,1));
576 LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff));
579 LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff)));
582 LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1)));
585 LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff)));
586 LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1)));
589 LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1)));
590 LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1)));
593 LLAssert(llong(1, 1) > llong(1, 0));
594 LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff));
595 LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0));
596 LLAssert(llong(1, 0) > llong(0, 0x7fffffff));
597 LLAssert(llong(1, 0) > llong(0, 0xffffffff));
598 LLAssert(llong(0, 0) > llong(0x80000000, 1));
601 LLAssert(llong(1, 0) < llong(1, 1));
602 LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000));
603 LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1));
604 LLAssert(llong(0, 0x7fffffff) < llong(1, 0));
605 LLAssert(llong(0, 0xffffffff) < llong(1, 0));
606 LLAssert(llong(0x80000000, 1) < llong(0, 0));
609 LLAssert(llong(1, 1) >= llong(1, 0));
610 LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff));
611 LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0));
612 LLAssert(llong(1, 0) >= llong(0, 0x7fffffff));
613 LLAssert(llong(1, 0) >= llong(0, 0xffffffff));
614 LLAssert(llong(0, 0) >= llong(0x80000000, 1));
615 LLAssert(llong() >= llong(0, 0));
616 LLAssert(llong(1,0) >= llong(1, 0));
617 LLAssert(llong(0,1) >= llong(0, 1));
620 LLAssert(llong(1, 0) <= llong(1, 1));
621 LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000));
622 LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1));
623 LLAssert(llong(0, 0x7fffffff) <= llong(1, 0));
624 LLAssert(llong(0, 0xffffffff) <= llong(1, 0));
625 LLAssert(llong(0x80000000, 1) <= llong(0, 0));
626 LLAssert(llong() <= llong(0, 0));
627 LLAssert(llong(1,0) <= llong(1, 0));
628 LLAssert(llong(0,1) <= llong(0, 1));
631 LLAssert(llong() == (int32_t)0);
632 LLAssert(llong(0,1) == (int32_t)1);
635 LLAssert(llong(1,0) != (int32_t)0);
636 LLAssert(llong(0,1) != (int32_t)2);
637 LLAssert(llong(0,0xffffffff) != (int32_t)-1);
639 llong
negOne(0xffffffff, 0xffffffff);
642 LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff);
643 LLAssert(negOne
> (int32_t)-2);
644 LLAssert(llong(1, 0) > (int32_t)0x7fffffff);
645 LLAssert(llong(0, 0) > (int32_t)-1);
648 LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff);
649 LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1);
652 LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff);
653 LLAssert(negOne
>= (int32_t)-2);
654 LLAssert(llong(1, 0) >= (int32_t)0x7fffffff);
655 LLAssert(llong(0, 0) >= (int32_t)-1);
656 LLAssert(llong() >= (int32_t)0);
657 LLAssert(llong(0,1) >= (int32_t)1);
660 LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff);
661 LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1);
662 LLAssert(llong() <= (int32_t)0);
663 LLAssert(llong(0,1) <= (int32_t)1);
666 LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1);
669 LLAssert((llong(1, 1) <<= 0) == llong(1, 1));
670 LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000));
671 LLAssert((llong(1, 1) <<= 32) == llong(1, 0));
672 LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0));
673 LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used
674 LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used
677 LLAssert((llong((int32_t)1) << 5).asUInt() == 32);
679 // operator >>= (sign extended)
680 LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc));
681 LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde));
682 LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff));
683 LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000));
684 LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
685 LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used
687 // operator >> sign extended)
688 LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde));
690 // ushr (right shift without sign extension)
691 LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc));
692 LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde));
693 LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1));
694 LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000));
695 LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
696 LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used
699 LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
702 LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
705 LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
708 LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
711 LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
714 LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
717 LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa));
720 LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
723 LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
726 LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
728 // operator&=(uint32)
729 LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
731 // operator|=(uint32)
732 LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
734 // operator^=(uint32)
735 LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
738 LLAssert(llong(1, 0) == ++llong(0,0xffffffff));
741 LLAssert(llong(0,0xffffffff) == --llong(1, 0));
745 llong
n(0, 0xffffffff);
746 LLAssert(llong(0, 0xffffffff) == n
++);
747 LLAssert(llong(1, 0) == n
);
753 LLAssert(llong(1, 0) == n
--);
754 LLAssert(llong(0, 0xffffffff) == n
);
758 LLAssert(llong(0, 0) == -llong(0, 0));
759 LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1));
760 LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff));
761 LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1));
762 LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow
767 LLAssert((n
-= llong(0, 1)) == llong(0xffffffff, 0xffffffff));
768 LLAssert(n
== llong(0xffffffff, 0xffffffff));
771 LLAssert((n
-= llong(0, 1)) == llong(0, 0xffffffff));
772 LLAssert(n
== llong(0, 0xffffffff));
778 LLAssert((n
- llong(0, 1)) == llong(0xffffffff, 0xffffffff));
779 LLAssert(n
== llong(0, 0));
782 LLAssert((n
- llong(0, 1)) == llong(0, 0xffffffff));
783 LLAssert(n
== llong(1, 0));
788 llong
n(0xffffffff, 0xffffffff);
789 LLAssert((n
+= llong(0, 1)) == llong(0, 0));
790 LLAssert(n
== llong(0, 0));
792 n
= llong(0, 0xffffffff);
793 LLAssert((n
+= llong(0, 1)) == llong(1, 0));
794 LLAssert(n
== llong(1, 0));
799 llong
n(0xffffffff, 0xffffffff);
800 LLAssert((n
+ llong(0, 1)) == llong(0, 0));
801 LLAssert(n
== llong(0xffffffff, 0xffffffff));
803 n
= llong(0, 0xffffffff);
804 LLAssert((n
+ llong(0, 1)) == llong(1, 0));
805 LLAssert(n
== llong(0, 0xffffffff));
810 void IntlTestRBNF::TestLLong()
812 logln("Starting TestLLong");
814 TestLLongConstructors();
816 TestLLongSimpleOperators();
818 logln("Testing operator*=, operator*");
820 // operator*=, operator*
821 // small and large values, positive, &NEGative, zero
822 // also test commutivity
825 const llong
ONE(0, 1);
826 const llong
NEG_ONE((int32_t)-1);
827 const llong
THREE(0, 3);
828 const llong
NEG_THREE((int32_t)-3);
829 const llong
TWO_TO_16(0, 0x10000);
830 const llong NEG_TWO_TO_16
= -TWO_TO_16
;
831 const llong
TWO_TO_32(1, 0);
832 const llong NEG_TWO_TO_32
= -TWO_TO_32
;
834 const llong
NINE(0, 9);
835 const llong NEG_NINE
= -NINE
;
837 const llong
TWO_TO_16X3(0, 0x00030000);
838 const llong NEG_TWO_TO_16X3
= -TWO_TO_16X3
;
840 const llong
TWO_TO_32X3(3, 0);
841 const llong NEG_TWO_TO_32X3
= -TWO_TO_32X3
;
843 const llong
TWO_TO_48(0x10000, 0);
844 const llong NEG_TWO_TO_48
= -TWO_TO_48
;
846 const int32_t VALUE_WIDTH
= 9;
847 const llong
* values
[VALUE_WIDTH
] = {
848 &ZERO
, &ONE
, &NEG_ONE
, &THREE
, &NEG_THREE
, &TWO_TO_16
, &NEG_TWO_TO_16
, &TWO_TO_32
, &NEG_TWO_TO_32
851 const llong
* answers
[VALUE_WIDTH
*VALUE_WIDTH
] = {
852 &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
,
853 &ZERO
, &ONE
, &NEG_ONE
, &THREE
, &NEG_THREE
, &TWO_TO_16
, &NEG_TWO_TO_16
, &TWO_TO_32
, &NEG_TWO_TO_32
,
854 &ZERO
, &NEG_ONE
, &ONE
, &NEG_THREE
, &THREE
, &NEG_TWO_TO_16
, &TWO_TO_16
, &NEG_TWO_TO_32
, &TWO_TO_32
,
855 &ZERO
, &THREE
, &NEG_THREE
, &NINE
, &NEG_NINE
, &TWO_TO_16X3
, &NEG_TWO_TO_16X3
, &TWO_TO_32X3
, &NEG_TWO_TO_32X3
,
856 &ZERO
, &NEG_THREE
, &THREE
, &NEG_NINE
, &NINE
, &NEG_TWO_TO_16X3
, &TWO_TO_16X3
, &NEG_TWO_TO_32X3
, &TWO_TO_32X3
,
857 &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
,
858 &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
,
859 &ZERO
, &TWO_TO_32
, &NEG_TWO_TO_32
, &TWO_TO_32X3
, &NEG_TWO_TO_32X3
, &TWO_TO_48
, &NEG_TWO_TO_48
, &ZERO
, &ZERO
,
860 &ZERO
, &NEG_TWO_TO_32
, &TWO_TO_32
, &NEG_TWO_TO_32X3
, &TWO_TO_32X3
, &NEG_TWO_TO_48
, &TWO_TO_48
, &ZERO
, &ZERO
863 for (int i
= 0; i
< VALUE_WIDTH
; ++i
) {
864 for (int j
= 0; j
< VALUE_WIDTH
; ++j
) {
865 llong lhs
= *values
[i
];
866 llong rhs
= *values
[j
];
867 llong ans
= *answers
[i
*VALUE_WIDTH
+ j
];
871 LLAssert((n
*= rhs
) == ans
);
875 LLAssert((n
* rhs
) == ans
);
881 logln("Testing operator/=, operator/");
882 // operator/=, operator/
883 // test num = 0, div = 0, pos/neg, > 2^32, div > num
886 const llong
ONE(0, 1);
887 const llong NEG_ONE
= -ONE
;
888 const llong
MAX(0x7fffffff, 0xffffffff);
889 const llong
MIN(0x80000000, 0);
890 const llong
TWO(0, 2);
891 const llong NEG_TWO
= -TWO
;
892 const llong
FIVE(0, 5);
893 const llong NEG_FIVE
= -FIVE
;
894 const llong
TWO_TO_32(1, 0);
895 const llong NEG_TWO_TO_32
= -TWO_TO_32
;
896 const llong TWO_TO_32d5
= llong(TWO_TO_32
.asDouble()/5.0);
897 const llong NEG_TWO_TO_32d5
= -TWO_TO_32d5
;
898 const llong TWO_TO_32X5
= TWO_TO_32
* FIVE
;
899 const llong NEG_TWO_TO_32X5
= -TWO_TO_32X5
;
901 const llong
* tuples
[] = { // lhs, rhs, ans
904 &NEG_ONE
, &ZERO
, &MIN
,
906 &ONE
, &NEG_ONE
, &NEG_ONE
,
907 &NEG_ONE
, &ONE
, &NEG_ONE
,
908 &NEG_ONE
, &NEG_ONE
, &ONE
,
910 &FIVE
, &NEG_TWO
, &NEG_TWO
,
911 &NEG_FIVE
, &TWO
, &NEG_TWO
,
912 &NEG_FIVE
, &NEG_TWO
, &TWO
,
914 &TWO
, &NEG_FIVE
, &ZERO
,
915 &NEG_TWO
, &FIVE
, &ZERO
,
916 &NEG_TWO
, &NEG_FIVE
, &ZERO
,
917 &TWO_TO_32
, &TWO_TO_32
, &ONE
,
918 &TWO_TO_32
, &NEG_TWO_TO_32
, &NEG_ONE
,
919 &NEG_TWO_TO_32
, &TWO_TO_32
, &NEG_ONE
,
920 &NEG_TWO_TO_32
, &NEG_TWO_TO_32
, &ONE
,
921 &TWO_TO_32
, &FIVE
, &TWO_TO_32d5
,
922 &TWO_TO_32
, &NEG_FIVE
, &NEG_TWO_TO_32d5
,
923 &NEG_TWO_TO_32
, &FIVE
, &NEG_TWO_TO_32d5
,
924 &NEG_TWO_TO_32
, &NEG_FIVE
, &TWO_TO_32d5
,
925 &TWO_TO_32X5
, &FIVE
, &TWO_TO_32
,
926 &TWO_TO_32X5
, &NEG_FIVE
, &NEG_TWO_TO_32
,
927 &NEG_TWO_TO_32X5
, &FIVE
, &NEG_TWO_TO_32
,
928 &NEG_TWO_TO_32X5
, &NEG_FIVE
, &TWO_TO_32
,
929 &TWO_TO_32X5
, &TWO_TO_32
, &FIVE
,
930 &TWO_TO_32X5
, &NEG_TWO_TO_32
, &NEG_FIVE
,
931 &NEG_TWO_TO_32X5
, &NEG_TWO_TO_32
, &FIVE
,
932 &NEG_TWO_TO_32X5
, &TWO_TO_32
, &NEG_FIVE
934 const int TUPLE_WIDTH
= 3;
935 const int TUPLE_COUNT
= (int)(sizeof(tuples
)/sizeof(tuples
[0]))/TUPLE_WIDTH
;
936 for (int i
= 0; i
< TUPLE_COUNT
; ++i
) {
937 const llong lhs
= *tuples
[i
*TUPLE_WIDTH
+0];
938 const llong rhs
= *tuples
[i
*TUPLE_WIDTH
+1];
939 const llong ans
= *tuples
[i
*TUPLE_WIDTH
+2];
942 if (!((n
/= rhs
) == ans
)) {
943 errln("fail: (n /= rhs) == ans");
948 LLAssert((n
/ rhs
) == ans
);
953 logln("Testing operator%%=, operator%%");
954 //operator%=, operator%
957 const llong
ONE(0, 1);
958 const llong
TWO(0, 2);
959 const llong
THREE(0,3);
960 const llong
FOUR(0, 4);
961 const llong
FIVE(0, 5);
962 const llong
SIX(0, 6);
964 const llong NEG_ONE
= -ONE
;
965 const llong NEG_TWO
= -TWO
;
966 const llong NEG_THREE
= -THREE
;
967 const llong NEG_FOUR
= -FOUR
;
968 const llong NEG_FIVE
= -FIVE
;
969 const llong NEG_SIX
= -SIX
;
971 const llong
NINETY_NINE(0, 99);
972 const llong
HUNDRED(0, 100);
973 const llong
HUNDRED_ONE(0, 101);
975 const llong
BIG(0x12345678, 0x9abcdef0);
976 const llong
BIG_FIVE(BIG
* FIVE
);
977 const llong BIG_FIVEm1
= BIG_FIVE
- ONE
;
978 const llong BIG_FIVEp1
= BIG_FIVE
+ ONE
;
980 const llong
* tuples
[] = {
984 &THREE
, &FIVE
, &THREE
,
988 &ZERO
, &NEG_FIVE
, &ZERO
,
989 &ONE
, &NEG_FIVE
, &ONE
,
990 &TWO
, &NEG_FIVE
, &TWO
,
991 &THREE
, &NEG_FIVE
, &THREE
,
992 &FOUR
, &NEG_FIVE
, &FOUR
,
993 &FIVE
, &NEG_FIVE
, &ZERO
,
994 &SIX
, &NEG_FIVE
, &ONE
,
995 &NEG_ONE
, &FIVE
, &NEG_ONE
,
996 &NEG_TWO
, &FIVE
, &NEG_TWO
,
997 &NEG_THREE
, &FIVE
, &NEG_THREE
,
998 &NEG_FOUR
, &FIVE
, &NEG_FOUR
,
999 &NEG_FIVE
, &FIVE
, &ZERO
,
1000 &NEG_SIX
, &FIVE
, &NEG_ONE
,
1001 &NEG_ONE
, &NEG_FIVE
, &NEG_ONE
,
1002 &NEG_TWO
, &NEG_FIVE
, &NEG_TWO
,
1003 &NEG_THREE
, &NEG_FIVE
, &NEG_THREE
,
1004 &NEG_FOUR
, &NEG_FIVE
, &NEG_FOUR
,
1005 &NEG_FIVE
, &NEG_FIVE
, &ZERO
,
1006 &NEG_SIX
, &NEG_FIVE
, &NEG_ONE
,
1007 &NINETY_NINE
, &FIVE
, &FOUR
,
1008 &HUNDRED
, &FIVE
, &ZERO
,
1009 &HUNDRED_ONE
, &FIVE
, &ONE
,
1010 &BIG_FIVEm1
, &FIVE
, &FOUR
,
1011 &BIG_FIVE
, &FIVE
, &ZERO
,
1012 &BIG_FIVEp1
, &FIVE
, &ONE
1014 const int TUPLE_WIDTH
= 3;
1015 const int TUPLE_COUNT
= (int)(sizeof(tuples
)/sizeof(tuples
[0]))/TUPLE_WIDTH
;
1016 for (int i
= 0; i
< TUPLE_COUNT
; ++i
) {
1017 const llong lhs
= *tuples
[i
*TUPLE_WIDTH
+0];
1018 const llong rhs
= *tuples
[i
*TUPLE_WIDTH
+1];
1019 const llong ans
= *tuples
[i
*TUPLE_WIDTH
+2];
1022 if (!((n
%= rhs
) == ans
)) {
1023 errln("fail: (n %= rhs) == ans");
1028 LLAssert((n
% rhs
) == ans
);
1033 logln("Testing pow");
1035 LLAssert(llong(0, 0).pow(0) == llong(0, 0));
1036 LLAssert(llong(0, 0).pow(2) == llong(0, 0));
1037 LLAssert(llong(0, 2).pow(0) == llong(0, 1));
1038 LLAssert(llong(0, 2).pow(2) == llong(0, 4));
1039 LLAssert(llong(0, 2).pow(32) == llong(1, 0));
1040 LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5));
1044 const llong
n(0xffffffff,0xffffffff);
1045 LLAssert(n
.abs() == llong(0, 1));
1049 logln("Testing atoll");
1051 const char empty
[] = "";
1052 const char zero
[] = "0";
1053 const char neg_one
[] = "-1";
1054 const char neg_12345
[] = "-12345";
1055 const char big1
[] = "123456789abcdef0";
1056 const char big2
[] = "fFfFfFfFfFfFfFfF";
1057 LLAssert(llong::atoll(empty
) == llong(0, 0));
1058 LLAssert(llong::atoll(zero
) == llong(0, 0));
1059 LLAssert(llong::atoll(neg_one
) == llong(0xffffffff, 0xffffffff));
1060 LLAssert(llong::atoll(neg_12345
) == -llong(0, 12345));
1061 LLAssert(llong::atoll(big1
, 16) == llong(0x12345678, 0x9abcdef0));
1062 LLAssert(llong::atoll(big2
, 16) == llong(0xffffffff, 0xffffffff));
1066 const UChar uempty
[] = { 0 };
1067 const UChar uzero
[] = { 0x30, 0 };
1068 const UChar uneg_one
[] = { 0x2d, 0x31, 0 };
1069 const UChar uneg_12345
[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 };
1070 const UChar ubig1
[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 };
1071 const UChar ubig2
[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 };
1072 LLAssert(llong::utoll(uempty
) == llong(0, 0));
1073 LLAssert(llong::utoll(uzero
) == llong(0, 0));
1074 LLAssert(llong::utoll(uneg_one
) == llong(0xffffffff, 0xffffffff));
1075 LLAssert(llong::utoll(uneg_12345
) == -llong(0, 12345));
1076 LLAssert(llong::utoll(ubig1
, 16) == llong(0x12345678, 0x9abcdef0));
1077 LLAssert(llong::utoll(ubig2
, 16) == llong(0xffffffff, 0xffffffff));
1080 logln("Testing lltoa");
1083 char buf
[64]; // ascii
1084 LLAssert((llong(0, 0).lltoa(buf
, (uint32_t)sizeof(buf
)) == 1) && (strcmp(buf
, zero
) == 0));
1085 LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf
, (uint32_t)sizeof(buf
)) == 2) && (strcmp(buf
, neg_one
) == 0));
1086 LLAssert(((-llong(0, 12345)).lltoa(buf
, (uint32_t)sizeof(buf
)) == 6) && (strcmp(buf
, neg_12345
) == 0));
1087 LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf
, (uint32_t)sizeof(buf
), 16) == 16) && (strcmp(buf
, big1
) == 0));
1091 logln("Testing u_lltoa");
1095 LLAssert((llong(0, 0).lltou(buf
, (uint32_t)sizeof(buf
)) == 1) && (u_strcmp(buf
, uzero
) == 0));
1096 LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf
, (uint32_t)sizeof(buf
)) == 2) && (u_strcmp(buf
, uneg_one
) == 0));
1097 LLAssert(((-llong(0, 12345)).lltou(buf
, (uint32_t)sizeof(buf
)) == 6) && (u_strcmp(buf
, uneg_12345
) == 0));
1098 LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf
, (uint32_t)sizeof(buf
), 16) == 16) && (u_strcmp(buf
, ubig1
) == 0));
1106 IntlTestRBNF::TestEnglishSpellout()
1108 UErrorCode status
= U_ZERO_ERROR
;
1109 RuleBasedNumberFormat
* formatter
1110 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
1111 if (U_FAILURE(status
)) {
1112 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1114 static const char* const testData
[][2] = {
1117 { "15", "fifteen" },
1119 { "23", "twenty-three" },
1120 { "73", "seventy-three" },
1121 { "88", "eighty-eight" },
1122 { "100", "one hundred" },
1123 { "106", "one hundred six" },
1124 { "127", "one hundred twenty-seven" },
1125 { "200", "two hundred" },
1126 { "579", "five hundred seventy-nine" },
1127 { "1,000", "one thousand" },
1128 { "2,000", "two thousand" },
1129 { "3,004", "three thousand four" },
1130 { "4,567", "four thousand five hundred sixty-seven" },
1131 { "15,943", "fifteen thousand nine hundred forty-three" },
1132 { "2,345,678", "two million three hundred forty-five thousand six hundred seventy-eight" },
1133 { "-36", "minus thirty-six" },
1134 { "234.567", "two hundred thirty-four point five six seven" },
1138 doTest(formatter
, testData
, TRUE
);
1140 #if !UCONFIG_NO_COLLATION
1141 formatter
->setLenient(TRUE
);
1142 static const char* lpTestData
[][2] = {
1143 { "fifty-7", "57" },
1144 { " fifty-7", "57" },
1145 { " fifty-7", "57" },
1146 { "2 thousand six HUNDRED fifty-7", "2,657" },
1147 { "fifteen hundred and zero", "1,500" },
1148 { "FOurhundred thiRTY six", "436" },
1151 doLenientParseTest(formatter
, lpTestData
);
1158 IntlTestRBNF::TestOrdinalAbbreviations()
1160 UErrorCode status
= U_ZERO_ERROR
;
1161 RuleBasedNumberFormat
* formatter
1162 = new RuleBasedNumberFormat(URBNF_ORDINAL
, Locale::getUS(), status
);
1164 if (U_FAILURE(status
)) {
1165 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1167 static const char* const testData
[][2] = {
1184 { "12,345", "12,345th" },
1188 doTest(formatter
, testData
, FALSE
);
1194 IntlTestRBNF::TestDurations()
1196 UErrorCode status
= U_ZERO_ERROR
;
1197 RuleBasedNumberFormat
* formatter
1198 = new RuleBasedNumberFormat(URBNF_DURATION
, Locale::getUS(), status
);
1200 if (U_FAILURE(status
)) {
1201 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1203 static const char* const testData
[][2] = {
1204 { "3,600", "1:00:00" }, //move me and I fail
1207 { "24", "24 sec." },
1212 // { "3,600", "1:00:00" },
1213 { "3,740", "1:02:20" },
1214 { "10,293", "2:51:33" },
1218 doTest(formatter
, testData
, TRUE
);
1220 #if !UCONFIG_NO_COLLATION
1221 formatter
->setLenient(TRUE
);
1222 static const char* lpTestData
[][2] = {
1223 { "2-51-33", "10,293" },
1226 doLenientParseTest(formatter
, lpTestData
);
1233 IntlTestRBNF::TestSpanishSpellout()
1235 UErrorCode status
= U_ZERO_ERROR
;
1236 RuleBasedNumberFormat
* formatter
1237 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("es", "ES", ""), status
);
1239 if (U_FAILURE(status
)) {
1240 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1242 static const char* const testData
[][2] = {
1245 { "16", "diecis\\u00e9is" },
1247 { "24", "veinticuatro" },
1248 { "26", "veintis\\u00e9is" },
1249 { "73", "setenta y tres" },
1250 { "88", "ochenta y ocho" },
1252 { "106", "ciento seis" },
1253 { "127", "ciento veintisiete" },
1254 { "200", "doscientos" },
1255 { "579", "quinientos setenta y nueve" },
1257 { "2,000", "dos mil" },
1258 { "3,004", "tres mil cuatro" },
1259 { "4,567", "cuatro mil quinientos sesenta y siete" },
1260 { "15,943", "quince mil novecientos cuarenta y tres" },
1261 { "2,345,678", "dos millones trescientos cuarenta y cinco mil seiscientos setenta y ocho"},
1262 { "-36", "menos treinta y seis" },
1263 { "234.567", "doscientos treinta y cuatro coma cinco seis siete" },
1267 doTest(formatter
, testData
, TRUE
);
1273 IntlTestRBNF::TestFrenchSpellout()
1275 UErrorCode status
= U_ZERO_ERROR
;
1276 RuleBasedNumberFormat
* formatter
1277 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getFrance(), status
);
1279 if (U_FAILURE(status
)) {
1280 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1282 static const char* const testData
[][2] = {
1286 { "21", "vingt-et-un" },
1287 { "23", "vingt-trois" },
1288 { "62", "soixante-deux" },
1289 { "70", "soixante-dix" },
1290 { "71", "soixante-et-onze" },
1291 { "73", "soixante-treize" },
1292 { "80", "quatre-vingts" },
1293 { "88", "quatre-vingt-huit" },
1295 { "106", "cent-six" },
1296 { "127", "cent-vingt-sept" },
1297 { "200", "deux-cents" },
1298 { "579", "cinq-cent-soixante-dix-neuf" },
1299 { "1,000", "mille" },
1300 { "1,123", "mille-cent-vingt-trois" },
1301 { "1,594", "mille-cinq-cent-quatre-vingt-quatorze" },
1302 { "2,000", "deux-mille" },
1303 { "3,004", "trois-mille-quatre" },
1304 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1305 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1306 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-soixante-dix-huit" },
1307 { "-36", "moins trente-six" },
1308 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
1312 doTest(formatter
, testData
, TRUE
);
1314 #if !UCONFIG_NO_COLLATION
1315 formatter
->setLenient(TRUE
);
1316 static const char* lpTestData
[][2] = {
1317 { "trente-et-un", "31" },
1318 { "un cent quatre vingt dix huit", "198" },
1321 doLenientParseTest(formatter
, lpTestData
);
1327 static const char* const swissFrenchTestData
[][2] = {
1331 { "21", "vingt-et-un" },
1332 { "23", "vingt-trois" },
1333 { "62", "soixante-deux" },
1334 { "70", "septante" },
1335 { "71", "septante-et-un" },
1336 { "73", "septante-trois" },
1337 { "80", "huitante" },
1338 { "88", "huitante-huit" },
1340 { "106", "cent-six" },
1341 { "127", "cent-vingt-sept" },
1342 { "200", "deux-cents" },
1343 { "579", "cinq-cent-septante-neuf" },
1344 { "1,000", "mille" },
1345 { "1,123", "mille-cent-vingt-trois" },
1346 { "1,594", "mille-cinq-cent-nonante-quatre" },
1347 { "2,000", "deux-mille" },
1348 { "3,004", "trois-mille-quatre" },
1349 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1350 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1351 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-septante-huit" },
1352 { "-36", "moins trente-six" },
1353 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
1358 IntlTestRBNF::TestSwissFrenchSpellout()
1360 UErrorCode status
= U_ZERO_ERROR
;
1361 RuleBasedNumberFormat
* formatter
1362 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "CH", ""), status
);
1364 if (U_FAILURE(status
)) {
1365 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1367 doTest(formatter
, swissFrenchTestData
, TRUE
);
1372 static const char* const belgianFrenchTestData
[][2] = {
1376 { "21", "vingt-et-un" },
1377 { "23", "vingt-trois" },
1378 { "62", "soixante-deux" },
1379 { "70", "septante" },
1380 { "71", "septante-et-un" },
1381 { "73", "septante-trois" },
1382 { "80", "quatre-vingts" },
1383 { "88", "quatre-vingt-huit" },
1384 { "90", "nonante" },
1385 { "91", "nonante-et-un" },
1386 { "95", "nonante-cinq" },
1388 { "106", "cent-six" },
1389 { "127", "cent-vingt-sept" },
1390 { "200", "deux-cents" },
1391 { "579", "cinq-cent-septante-neuf" },
1392 { "1,000", "mille" },
1393 { "1,123", "mille-cent-vingt-trois" },
1394 { "1,594", "mille-cinq-cent-nonante-quatre" },
1395 { "2,000", "deux-mille" },
1396 { "3,004", "trois-mille-quatre" },
1397 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1398 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1399 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-septante-huit" },
1400 { "-36", "moins trente-six" },
1401 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
1407 IntlTestRBNF::TestBelgianFrenchSpellout()
1409 UErrorCode status
= U_ZERO_ERROR
;
1410 RuleBasedNumberFormat
* formatter
1411 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "BE", ""), status
);
1413 if (U_FAILURE(status
)) {
1414 errcheckln(status
, "rbnf status: 0x%x (%s)\n", status
, u_errorName(status
));
1415 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1417 // Belgian french should match Swiss french.
1418 doTest(formatter
, belgianFrenchTestData
, TRUE
);
1424 IntlTestRBNF::TestItalianSpellout()
1426 UErrorCode status
= U_ZERO_ERROR
;
1427 RuleBasedNumberFormat
* formatter
1428 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getItalian(), status
);
1430 if (U_FAILURE(status
)) {
1431 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1433 static const char* const testData
[][2] = {
1435 { "15", "quindici" },
1437 { "23", "venti\\u00ADtr\\u00E9" },
1438 { "73", "settanta\\u00ADtr\\u00E9" },
1439 { "88", "ottant\\u00ADotto" },
1441 { "101", "cento\\u00ADuno" },
1442 { "103", "cento\\u00ADtr\\u00E9" },
1443 { "106", "cento\\u00ADsei" },
1444 { "108", "cent\\u00ADotto" },
1445 { "127", "cento\\u00ADventi\\u00ADsette" },
1446 { "181", "cent\\u00ADottant\\u00ADuno" },
1447 { "200", "due\\u00ADcento" },
1448 { "579", "cinque\\u00ADcento\\u00ADsettanta\\u00ADnove" },
1449 { "1,000", "mille" },
1450 { "2,000", "due\\u00ADmila" },
1451 { "3,004", "tre\\u00ADmila\\u00ADquattro" },
1452 { "4,567", "quattro\\u00ADmila\\u00ADcinque\\u00ADcento\\u00ADsessanta\\u00ADsette" },
1453 { "15,943", "quindici\\u00ADmila\\u00ADnove\\u00ADcento\\u00ADquaranta\\u00ADtr\\u00E9" },
1454 { "-36", "meno trenta\\u00ADsei" },
1455 { "234.567", "due\\u00ADcento\\u00ADtrenta\\u00ADquattro virgola cinque sei sette" },
1459 doTest(formatter
, testData
, TRUE
);
1465 IntlTestRBNF::TestPortugueseSpellout()
1467 UErrorCode status
= U_ZERO_ERROR
;
1468 RuleBasedNumberFormat
* formatter
1469 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("pt","BR",""), status
);
1471 if (U_FAILURE(status
)) {
1472 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1474 static const char* const testData
[][2] = {
1478 { "23", "vinte e tr\\u00EAs" },
1479 { "73", "setenta e tr\\u00EAs" },
1480 { "88", "oitenta e oito" },
1482 { "106", "cento e seis" },
1483 { "108", "cento e oito" },
1484 { "127", "cento e vinte e sete" },
1485 { "181", "cento e oitenta e um" },
1486 { "200", "duzentos" },
1487 { "579", "quinhentos e setenta e nove" },
1489 { "2,000", "dois mil" },
1490 { "3,004", "tr\\u00EAs mil e quatro" },
1491 { "4,567", "quatro mil e quinhentos e sessenta e sete" },
1492 { "15,943", "quinze mil e novecentos e quarenta e tr\\u00EAs" },
1493 { "-36", "menos trinta e seis" },
1494 { "234.567", "duzentos e trinta e quatro v\\u00EDrgula cinco seis sete" },
1498 doTest(formatter
, testData
, TRUE
);
1503 IntlTestRBNF::TestGermanSpellout()
1505 UErrorCode status
= U_ZERO_ERROR
;
1506 RuleBasedNumberFormat
* formatter
1507 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getGermany(), status
);
1509 if (U_FAILURE(status
)) {
1510 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1512 static const char* const testData
[][2] = {
1514 { "15", "f\\u00fcnfzehn" },
1515 { "20", "zwanzig" },
1516 { "23", "drei\\u00ADund\\u00ADzwanzig" },
1517 { "73", "drei\\u00ADund\\u00ADsiebzig" },
1518 { "88", "acht\\u00ADund\\u00ADachtzig" },
1519 { "100", "ein\\u00ADhundert" },
1520 { "106", "ein\\u00ADhundert\\u00ADsechs" },
1521 { "127", "ein\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADzwanzig" },
1522 { "200", "zwei\\u00ADhundert" },
1523 { "579", "f\\u00fcnf\\u00ADhundert\\u00ADneun\\u00ADund\\u00ADsiebzig" },
1524 { "1,000", "ein\\u00ADtausend" },
1525 { "2,000", "zwei\\u00ADtausend" },
1526 { "3,004", "drei\\u00ADtausend\\u00ADvier" },
1527 { "4,567", "vier\\u00ADtausend\\u00ADf\\u00fcnf\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADsechzig" },
1528 { "15,943", "f\\u00fcnfzehn\\u00ADtausend\\u00ADneun\\u00ADhundert\\u00ADdrei\\u00ADund\\u00ADvierzig" },
1529 { "2,345,678", "zwei Millionen drei\\u00ADhundert\\u00ADf\\u00fcnf\\u00ADund\\u00ADvierzig\\u00ADtausend\\u00ADsechs\\u00ADhundert\\u00ADacht\\u00ADund\\u00ADsiebzig" },
1533 doTest(formatter
, testData
, TRUE
);
1535 #if !UCONFIG_NO_COLLATION
1536 formatter
->setLenient(TRUE
);
1537 static const char* lpTestData
[][2] = {
1538 { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
1541 doLenientParseTest(formatter
, lpTestData
);
1548 IntlTestRBNF::TestThaiSpellout()
1550 UErrorCode status
= U_ZERO_ERROR
;
1551 RuleBasedNumberFormat
* formatter
1552 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("th"), status
);
1554 if (U_FAILURE(status
)) {
1555 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1557 static const char* const testData
[][2] = {
1558 { "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" },
1559 { "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1560 { "10", "\\u0e2a\\u0e34\\u0e1a" },
1561 { "11", "\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1562 { "21", "\\u0e22\\u0e35\\u0e48\\u200b\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1563 { "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e23\\u0e49\\u0e2d\\u0e22\\u200b\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1564 { "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e08\\u0e38\\u0e14\\u200b\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" },
1568 doTest(formatter
, testData
, TRUE
);
1574 IntlTestRBNF::TestSwedishSpellout()
1576 UErrorCode status
= U_ZERO_ERROR
;
1577 RuleBasedNumberFormat
* formatter
1578 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("sv"), status
);
1580 if (U_FAILURE(status
)) {
1581 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1583 static const char* testDataDefault
[][2] = {
1584 { "101", "ett\\u00adhundra\\u00adett" },
1585 { "123", "ett\\u00adhundra\\u00adtjugo\\u00adtre" },
1586 { "1,001", "et\\u00adtusen ett" },
1587 { "1,100", "et\\u00adtusen ett\\u00adhundra" },
1588 { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1589 { "1,234", "et\\u00adtusen tv\\u00e5\\u00adhundra\\u00adtrettio\\u00adfyra" },
1590 { "10,001", "tio\\u00adtusen ett" },
1591 { "11,000", "elva\\u00adtusen" },
1592 { "12,000", "tolv\\u00adtusen" },
1593 { "20,000", "tjugo\\u00adtusen" },
1594 { "21,000", "tjugo\\u00adet\\u00adtusen" },
1595 { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
1596 { "200,000", "tv\\u00e5\\u00adhundra\\u00adtusen" },
1597 { "201,000", "tv\\u00e5\\u00adhundra\\u00adet\\u00adtusen" },
1598 { "200,200", "tv\\u00e5\\u00adhundra\\u00adtusen tv\\u00e5\\u00adhundra" },
1599 { "2,002,000", "tv\\u00e5 miljoner tv\\u00e5\\u00adtusen" },
1600 { "12,345,678", "tolv miljoner tre\\u00adhundra\\u00adfyrtio\\u00adfem\\u00adtusen sex\\u00adhundra\\u00adsjuttio\\u00ad\\u00e5tta" },
1601 { "123,456.789", "ett\\u00adhundra\\u00adtjugo\\u00adtre\\u00adtusen fyra\\u00adhundra\\u00adfemtio\\u00adsex komma sju \\u00e5tta nio" },
1602 { "-12,345.678", "minus tolv\\u00adtusen tre\\u00adhundra\\u00adfyrtio\\u00adfem komma sex sju \\u00e5tta" },
1605 doTest(formatter
, testDataDefault
, TRUE
);
1607 static const char* testDataNeutrum
[][2] = {
1608 { "101", "ett\\u00adhundra\\u00adett" },
1609 { "1,001", "et\\u00adtusen ett" },
1610 { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1611 { "10,001", "tio\\u00adtusen ett" },
1612 { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
1616 formatter
->setDefaultRuleSet("%spellout-cardinal-neuter", status
);
1617 if (U_SUCCESS(status
)) {
1618 logln(" testing spellout-cardinal-neuter rules");
1619 doTest(formatter
, testDataNeutrum
, TRUE
);
1622 errln("Can't test spellout-cardinal-neuter rules");
1625 static const char* testDataYear
[][2] = {
1626 { "101", "ett\\u00adhundra\\u00adett" },
1627 { "900", "nio\\u00adhundra" },
1628 { "1,001", "et\\u00adtusen ett" },
1629 { "1,100", "elva\\u00adhundra" },
1630 { "1,101", "elva\\u00adhundra\\u00adett" },
1631 { "1,234", "tolv\\u00adhundra\\u00adtrettio\\u00adfyra" },
1632 { "2,001", "tjugo\\u00adhundra\\u00adett" },
1633 { "10,001", "tio\\u00adtusen ett" },
1637 status
= U_ZERO_ERROR
;
1638 formatter
->setDefaultRuleSet("%spellout-numbering-year", status
);
1639 if (U_SUCCESS(status
)) {
1640 logln("testing year rules");
1641 doTest(formatter
, testDataYear
, TRUE
);
1644 errln("Can't test year rules");
1652 IntlTestRBNF::TestSmallValues()
1654 UErrorCode status
= U_ZERO_ERROR
;
1655 RuleBasedNumberFormat
* formatter
1656 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("en_US"), status
);
1658 if (U_FAILURE(status
)) {
1659 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1661 static const char* const testDataDefault
[][2] = {
1662 { "0.001", "zero point zero zero one" },
1663 { "0.0001", "zero point zero zero zero one" },
1664 { "0.00001", "zero point zero zero zero zero one" },
1665 { "0.000001", "zero point zero zero zero zero zero one" },
1666 { "0.0000001", "zero point zero zero zero zero zero zero one" },
1667 { "0.00000001", "zero point zero zero zero zero zero zero zero one" },
1668 { "0.000000001", "zero point zero zero zero zero zero zero zero zero one" },
1669 { "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" },
1670 { "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" },
1671 { "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" },
1672 { "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" },
1673 { "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1674 { "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1675 { "10,000,000.001", "ten million point zero zero one" },
1676 { "10,000,000.0001", "ten million point zero zero zero one" },
1677 { "10,000,000.00001", "ten million point zero zero zero zero one" },
1678 { "10,000,000.000001", "ten million point zero zero zero zero zero one" },
1679 { "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" },
1680 // { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" },
1681 // { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" },
1682 { "10,000,000", "ten million" },
1683 // { "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" },
1684 // { "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" },
1685 // { "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" },
1686 { "1,234,567.7654321", "one million two hundred thirty-four thousand five hundred sixty-seven point seven six five four three two one" },
1687 { "123,456.654321", "one hundred twenty-three thousand four hundred fifty-six point six five four three two one" },
1688 { "12,345.54321", "twelve thousand three hundred forty-five point five four three two one" },
1689 { "1,234.4321", "one thousand two hundred thirty-four point four three two one" },
1690 { "123.321", "one hundred twenty-three point three two one" },
1691 { "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" },
1692 { "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" },
1696 doTest(formatter
, testDataDefault
, TRUE
);
1703 IntlTestRBNF::TestLocalizations(void)
1706 UnicodeString
rules("%main:0:no;1:some;100:a lot;1000:tons;\n"
1707 "%other:0:nada;1:yah, some;100:plenty;1000:more'n you'll ever need");
1709 UErrorCode status
= U_ZERO_ERROR
;
1711 RuleBasedNumberFormat
formatter(rules
, perror
, status
);
1712 if (U_FAILURE(status
)) {
1713 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1716 static const char* const testData
[][2] = {
1718 { "5", "yah, some" },
1719 { "423", "plenty" },
1720 { "12345", "more'n you'll ever need" },
1723 doTest(&formatter
, testData
, FALSE
);
1727 UnicodeString
loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>");
1728 static const char* const testData
[][2] = {
1732 { "12345", "tons" },
1735 RuleBasedNumberFormat
formatter0(rules
, loc
, perror
, status
);
1736 if (U_FAILURE(status
)) {
1737 errln("failed to build second formatter");
1739 doTest(&formatter0
, testData
, FALSE
);
1742 // exercise localization info
1743 Locale
locale0("en__VALLEY@turkey=gobblegobble");
1744 Locale
locale1("de_DE_FOO");
1745 Locale
locale2("ja_JP");
1746 UnicodeString name
= formatter0
.getRuleSetName(0);
1747 if ( formatter0
.getRuleSetDisplayName(0, locale0
) == "Main"
1748 && formatter0
.getRuleSetDisplayName(0, locale1
) == "das Main"
1749 && formatter0
.getRuleSetDisplayName(0, locale2
) == "%main"
1750 && formatter0
.getRuleSetDisplayName(name
, locale0
) == "Main"
1751 && formatter0
.getRuleSetDisplayName(name
, locale1
) == "das Main"
1752 && formatter0
.getRuleSetDisplayName(name
, locale2
) == "%main"){
1753 logln("getRuleSetDisplayName tested");
1755 errln("failed to getRuleSetDisplayName");
1759 for (i
= 0; i
< formatter0
.getNumberOfRuleSetDisplayNameLocales(); ++i
) {
1760 Locale locale
= formatter0
.getRuleSetDisplayNameLocale(i
, status
);
1761 if (U_SUCCESS(status
)) {
1762 for (int j
= 0; j
< formatter0
.getNumberOfRuleSetNames(); ++j
) {
1763 UnicodeString name
= formatter0
.getRuleSetName(j
);
1764 UnicodeString lname
= formatter0
.getRuleSetDisplayName(j
, locale
);
1765 UnicodeString msg
= locale
.getName();
1778 static const char* goodLocs
[] = {
1779 "", // zero-length ok, same as providing no localization data
1780 "<<>>", // no public rule sets ok
1781 "<<%main>>", // no localizations ok
1782 "<<%main,>,<en, Main,>>", // comma before close angle ok
1783 "<<%main>,<en, ',<>\" '>>", // quotes everything until next quote
1784 "<<%main>,<'en', \"it's ok\">>", // double quotes work too
1785 " \n <\n <\n %main\n >\n , \t <\t en\t , \tfoo \t\t > \n\n > \n ", // Pattern_White_Space ok
1787 int32_t goodLocsLen
= sizeof(goodLocs
)/sizeof(goodLocs
[0]);
1789 static const char* badLocs
[] = {
1790 " ", // non-zero length
1791 "<>", // empty array
1792 "<", // unclosed outer array
1793 "<<", // unclosed inner array
1794 "<<,>>", // unexpected comma
1795 "<<''>>", // empty string
1796 " x<<%main>>", // first non space char not open angle bracket
1797 "<%main>", // missing inner array
1798 "<<%main %other>>", // elements missing separating commma (spaces must be quoted)
1799 "<<%main><en, Main>>", // arrays missing separating comma
1800 "<<%main>,<en, main, foo>>", // too many elements in locale data
1801 "<<%main>,<en>>", // too few elements in locale data
1802 "<<<%main>>>", // unexpected open angle
1803 "<<%main<>>>", // unexpected open angle
1804 "<<%main, %other>,<en,,>>", // implicit empty strings
1805 "<<%main>,<en,''>>", // empty string
1806 "<<%main>, < en, '>>", // unterminated quote
1807 "<<%main>, < en, \"<>>", // unterminated quote
1808 "<<%main\">>", // quote in string
1809 "<<%main'>>", // quote in string
1810 "<<%main<>>", // open angle in string
1811 "<<%main>> x", // extra non-space text at end
1814 int32_t badLocsLen
= sizeof(badLocs
)/sizeof(badLocs
[0]);
1816 for (i
= 0; i
< goodLocsLen
; ++i
) {
1817 logln("[%d] '%s'", i
, goodLocs
[i
]);
1818 UErrorCode status
= U_ZERO_ERROR
;
1819 UnicodeString
loc(goodLocs
[i
]);
1820 RuleBasedNumberFormat
fmt(rules
, loc
, perror
, status
);
1821 if (U_FAILURE(status
)) {
1822 errln("Failed parse of good localization string: '%s'", goodLocs
[i
]);
1826 for (i
= 0; i
< badLocsLen
; ++i
) {
1827 logln("[%d] '%s'", i
, badLocs
[i
]);
1828 UErrorCode status
= U_ZERO_ERROR
;
1829 UnicodeString
loc(badLocs
[i
]);
1830 RuleBasedNumberFormat
fmt(rules
, loc
, perror
, status
);
1831 if (U_SUCCESS(status
)) {
1832 errln("Successful parse of bad localization string: '%s'", badLocs
[i
]);
1840 IntlTestRBNF::TestAllLocales()
1842 const char* names
[] = {
1847 double numbers
[] = {45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111};
1849 // RBNF parse is extremely slow when lenient option is enabled.
1850 // For non-exhaustive mode, we only test a few locales.
1851 const char* parseLocales
[] = {"en_US", "nl_NL", "be", NULL
};
1855 const Locale
* locales
= Locale::getAvailableLocales(count
);
1856 for (int i
= 0; i
< count
; ++i
) {
1857 const Locale
* loc
= &locales
[i
];
1858 UBool testParse
= TRUE
;
1861 for (int k
= 0; parseLocales
[k
] != NULL
; k
++) {
1862 if (strcmp(loc
->getLanguage(), parseLocales
[k
]) == 0) {
1869 for (int j
= 0; j
< 3; ++j
) {
1870 UErrorCode status
= U_ZERO_ERROR
;
1871 RuleBasedNumberFormat
* f
= new RuleBasedNumberFormat((URBNFRuleSetTag
)j
, *loc
, status
);
1872 if (U_FAILURE(status
)) {
1873 errln(UnicodeString(loc
->getName()) + names
[j
]
1874 + "ERROR could not instantiate -> " + u_errorName(status
));
1877 #if !UCONFIG_NO_COLLATION
1878 for (unsigned int numidx
= 0; numidx
< sizeof(numbers
)/sizeof(double); numidx
++) {
1879 double n
= numbers
[numidx
];
1883 logln(UnicodeString(loc
->getName()) + names
[j
]
1884 + "success: " + n
+ " -> " + str
);
1887 // We do not validate the result in this test case,
1888 // because there are cases which do not round trip by design.
1892 status
= U_ZERO_ERROR
;
1893 f
->setLenient(FALSE
);
1894 f
->parse(str
, num
, status
);
1895 if (U_FAILURE(status
)) {
1896 //TODO: We need to fix parse problems - see #6895 / #6896
1897 if (status
== U_INVALID_FORMAT_ERROR
) {
1898 logln(UnicodeString(loc
->getName()) + names
[j
]
1899 + "WARNING could not parse '" + str
+ "' -> " + u_errorName(status
));
1901 errln(UnicodeString(loc
->getName()) + names
[j
]
1902 + "ERROR could not parse '" + str
+ "' -> " + u_errorName(status
));
1906 status
= U_ZERO_ERROR
;
1907 f
->setLenient(TRUE
);
1908 f
->parse(str
, num
, status
);
1909 if (U_FAILURE(status
)) {
1910 //TODO: We need to fix parse problems - see #6895 / #6896
1911 if (status
== U_INVALID_FORMAT_ERROR
) {
1912 logln(UnicodeString(loc
->getName()) + names
[j
]
1913 + "WARNING could not parse(lenient) '" + str
+ "' -> " + u_errorName(status
));
1915 errln(UnicodeString(loc
->getName()) + names
[j
]
1916 + "ERROR could not parse(lenient) '" + str
+ "' -> " + u_errorName(status
));
1928 IntlTestRBNF::TestMultiplierSubstitution(void) {
1929 UnicodeString
rules("=#,##0=;1,000,000: <##0.###< million;");
1930 UErrorCode status
= U_ZERO_ERROR
;
1931 UParseError parse_error
;
1932 RuleBasedNumberFormat
*rbnf
=
1933 new RuleBasedNumberFormat(rules
, Locale::getUS(), parse_error
, status
);
1934 if (U_SUCCESS(status
)) {
1937 double n
= 1234000.0;
1938 rbnf
->format(n
, res
, pos
);
1941 UnicodeString expected
= UNICODE_STRING_SIMPLE("1.234 million");
1942 if (expected
!= res
) {
1943 UnicodeString msg
= "Expected: ";
1944 msg
.append(expected
);
1945 msg
.append(" but got ");
1953 IntlTestRBNF::TestSetDecimalFormatSymbols() {
1954 UErrorCode status
= U_ZERO_ERROR
;
1956 RuleBasedNumberFormat
rbnf(URBNF_ORDINAL
, Locale::getEnglish(), status
);
1957 if (U_FAILURE(status
)) {
1958 dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status
)));
1962 DecimalFormatSymbols
dfs(Locale::getEnglish(), status
);
1963 if (U_FAILURE(status
)) {
1964 errln("Unable to create DecimalFormatSymbols - " + UnicodeString(u_errorName(status
)));
1968 UnicodeString expected
[] = {
1969 UnicodeString("1,001st"),
1970 UnicodeString("1&001st")
1973 double number
= 1001;
1975 UnicodeString result
;
1977 rbnf
.format(number
, result
);
1978 if (result
!= expected
[0]) {
1979 errln("Format Error - Got: " + result
+ " Expected: " + expected
[0]);
1984 /* Set new symbol for testing */
1985 dfs
.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
, UnicodeString("&"), TRUE
);
1986 rbnf
.setDecimalFormatSymbols(dfs
);
1988 rbnf
.format(number
, result
);
1989 if (result
!= expected
[1]) {
1990 errln("Format Error - Got: " + result
+ " Expected: " + expected
[1]);
1996 IntlTestRBNF::doTest(RuleBasedNumberFormat
* formatter
, const char* const testData
[][2], UBool testParsing
)
1998 // man, error reporting would be easier with printf-style syntax for unicode string and formattable
2000 UErrorCode status
= U_ZERO_ERROR
;
2001 DecimalFormatSymbols
dfs("en", status
);
2002 // NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
2003 DecimalFormat
decFmt("#,###.################", dfs
, status
);
2004 if (U_FAILURE(status
)) {
2005 errcheckln(status
, "FAIL: could not create NumberFormat - %s", u_errorName(status
));
2007 for (int i
= 0; testData
[i
][0]; ++i
) {
2008 const char* numString
= testData
[i
][0];
2009 const char* expectedWords
= testData
[i
][1];
2011 log("[%i] %s = ", i
, numString
);
2012 Formattable expectedNumber
;
2013 decFmt
.parse(numString
, expectedNumber
, status
);
2014 if (U_FAILURE(status
)) {
2015 errln("FAIL: decFmt could not parse %s", numString
);
2018 UnicodeString actualString
;
2020 formatter
->format(expectedNumber
, actualString
/* , pos*/, status
);
2021 if (U_FAILURE(status
)) {
2022 UnicodeString msg
= "Fail: formatter could not format ";
2023 decFmt
.format(expectedNumber
, msg
, status
);
2027 UnicodeString expectedString
= UnicodeString(expectedWords
, -1, US_INV
).unescape();
2028 if (actualString
!= expectedString
) {
2029 UnicodeString msg
= "FAIL: check failed for ";
2030 decFmt
.format(expectedNumber
, msg
, status
);
2031 msg
.append(", expected ");
2032 msg
.append(expectedString
);
2033 msg
.append(" but got ");
2034 msg
.append(actualString
);
2038 logln(actualString
);
2040 Formattable parsedNumber
;
2041 formatter
->parse(actualString
, parsedNumber
, status
);
2042 if (U_FAILURE(status
)) {
2043 UnicodeString msg
= "FAIL: formatter could not parse ";
2044 msg
.append(actualString
);
2045 msg
.append(" status code: " );
2046 msg
.append(u_errorName(status
));
2050 if (parsedNumber
!= expectedNumber
) {
2051 UnicodeString msg
= "FAIL: parse failed for ";
2052 msg
.append(actualString
);
2053 msg
.append(", expected ");
2054 decFmt
.format(expectedNumber
, msg
, status
);
2055 msg
.append(", but got ");
2056 decFmt
.format(parsedNumber
, msg
, status
);
2070 IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat
* formatter
, const char* testData
[][2])
2072 UErrorCode status
= U_ZERO_ERROR
;
2073 NumberFormat
* decFmt
= NumberFormat::createInstance(Locale::getUS(), status
);
2074 if (U_FAILURE(status
)) {
2075 errcheckln(status
, "FAIL: could not create NumberFormat - %s", u_errorName(status
));
2077 for (int i
= 0; testData
[i
][0]; ++i
) {
2078 const char* spelledNumber
= testData
[i
][0]; // spelled-out number
2079 const char* asciiUSNumber
= testData
[i
][1]; // number as ascii digits formatted for US locale
2081 UnicodeString spelledNumberString
= UnicodeString(spelledNumber
).unescape();
2082 Formattable actualNumber
;
2083 formatter
->parse(spelledNumberString
, actualNumber
, status
);
2084 if (U_FAILURE(status
)) {
2085 UnicodeString msg
= "FAIL: formatter could not parse ";
2086 msg
.append(spelledNumberString
);
2090 // I changed the logic of this test somewhat from Java-- instead of comparing the
2091 // strings, I compare the Formattables. Hmmm, but the Formattables don't compare,
2092 // so change it back.
2094 UnicodeString asciiUSNumberString
= asciiUSNumber
;
2095 Formattable expectedNumber
;
2096 decFmt
->parse(asciiUSNumberString
, expectedNumber
, status
);
2097 if (U_FAILURE(status
)) {
2098 UnicodeString msg
= "FAIL: decFmt could not parse ";
2099 msg
.append(asciiUSNumberString
);
2103 UnicodeString actualNumberString
;
2104 UnicodeString expectedNumberString
;
2105 decFmt
->format(actualNumber
, actualNumberString
, status
);
2106 decFmt
->format(expectedNumber
, expectedNumberString
, status
);
2107 if (actualNumberString
!= expectedNumberString
) {
2108 UnicodeString msg
= "FAIL: parsing";
2109 msg
.append(asciiUSNumberString
);
2111 msg
.append(" lenient parse failed for ");
2112 msg
.append(spelledNumberString
);
2113 msg
.append(", expected ");
2114 msg
.append(expectedNumberString
);
2115 msg
.append(", but got ");
2116 msg
.append(actualNumberString
);
2131 IntlTestRBNF::TestRBNFDisabled() {
2132 errln("*** RBNF currently disabled on this platform ***\n");
2138 #endif /* #if !UCONFIG_NO_FORMATTING */