2 *******************************************************************************
3 * Copyright (C) 1996-2015, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/umachine.h"
16 #include "unicode/tblcoll.h"
17 #include "unicode/coleitr.h"
18 #include "unicode/ures.h"
19 #include "unicode/ustring.h"
20 #include "unicode/decimfmt.h"
21 #include "unicode/udata.h"
26 // import com.ibm.text.RuleBasedNumberFormat;
27 // import com.ibm.test.TestFmwk;
29 // import java.util.Locale;
30 // import java.text.NumberFormat;
32 // current macro not in icu1.8.1
33 #define TESTCASE(id,test) \
43 void IntlTestRBNF::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/)
45 if (exec
) logln("TestSuite RuleBasedNumberFormat");
48 TESTCASE(0, TestEnglishSpellout
);
49 TESTCASE(1, TestOrdinalAbbreviations
);
50 TESTCASE(2, TestDurations
);
51 TESTCASE(3, TestSpanishSpellout
);
52 TESTCASE(4, TestFrenchSpellout
);
53 TESTCASE(5, TestSwissFrenchSpellout
);
54 TESTCASE(6, TestItalianSpellout
);
55 TESTCASE(7, TestGermanSpellout
);
56 TESTCASE(8, TestThaiSpellout
);
58 TESTCASE(10, TestFractionalRuleSet
);
59 TESTCASE(11, TestSwedishSpellout
);
60 TESTCASE(12, TestBelgianFrenchSpellout
);
61 TESTCASE(13, TestSmallValues
);
62 TESTCASE(14, TestLocalizations
);
63 TESTCASE(15, TestAllLocales
);
64 TESTCASE(16, TestHebrewFraction
);
65 TESTCASE(17, TestPortugueseSpellout
);
66 TESTCASE(18, TestMultiplierSubstitution
);
67 TESTCASE(19, TestSetDecimalFormatSymbols
);
68 TESTCASE(20, TestPluralRules
);
69 TESTCASE(21, TestMultiplePluralRules
);
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");
388 * Perform a simple spot check on the parsing going into an infinite loop for alternate rules.
390 void IntlTestRBNF::TestMultiplePluralRules() {
391 // This is trying to model the feminine form, but don't worry about the details too much.
392 // We're trying to test the plural rules where there are different prefixes.
393 UnicodeString
rules("%spellout-cardinal-feminine-genitive:"
396 "1000: << $(cardinal,one{thousand}few{thousanF}other{thousanO})$[ >>];"
397 "%spellout-cardinal-feminine:"
400 "1000: << $(cardinal,one{thousand}few{thousanF}other{thousanO})$[ >>];");
401 UErrorCode status
= U_ZERO_ERROR
;
403 RuleBasedNumberFormat
formatter(rules
, Locale("ru"), pError
, status
);
405 UnicodeString resultStr
;
408 if (U_FAILURE(status
)) {
409 dataerrln("Unable to create formatter - %s", u_errorName(status
));
413 formatter
.parse(formatter
.format(1000.0, resultStr
, pos
, status
), result
, status
);
414 if (1000 != result
.getLong() || resultStr
!= UNICODE_STRING_SIMPLE("one thousand")) {
415 errln("RuleBasedNumberFormat did not return the correct value. Got: %d", result
.getLong());
419 formatter
.parse(formatter
.format(1000.0, UnicodeString("%spellout-cardinal-feminine-genitive"), resultStr
, pos
, status
), result
, status
);
420 if (1000 != result
.getLong() || resultStr
!= UNICODE_STRING_SIMPLE("ono thousand")) {
421 errln("RuleBasedNumberFormat(cardinal-feminine-genitive) did not return the correct value. Got: %d", result
.getLong());
425 formatter
.parse(formatter
.format(1000.0, UnicodeString("%spellout-cardinal-feminine"), resultStr
, pos
, status
), result
, status
);
426 if (1000 != result
.getLong() || resultStr
!= UNICODE_STRING_SIMPLE("one thousand")) {
427 errln("RuleBasedNumberFormat(spellout-cardinal-feminine) did not return the correct value. Got: %d", result
.getLong());
432 void IntlTestRBNF::TestFractionalRuleSet()
434 UnicodeString
fracRules(
436 // this rule formats the number if it's 1 or more. It formats
437 // the integral part using a DecimalFormat ("#,##0" puts
438 // thousands separators in the right places) and the fractional
439 // part using %%frac. If there is no fractional part, it
440 // just shows the integral part.
441 " x.0: <#,##0<[ >%%frac>];\n"
442 // this rule formats the number if it's between 0 and 1. It
443 // shows only the fractional part (0.5 shows up as "1/2," not
446 // the fraction rule set. This works the same way as the one in the
447 // preceding example: We multiply the fractional part of the number
448 // being formatted by each rule's base value and use the rule that
449 // produces the result closest to 0 (or the first rule that produces 0).
450 // Since we only provide rules for the numbers from 2 to 10, we know
451 // we'll get a fraction with a denominator between 2 and 10.
452 // "<0<" causes the numerator of the fraction to be formatted
466 int len
= fracRules
.length();
468 for (int i
= 0; i
< len
; ++i
) {
469 UChar ch
= fracRules
.charAt(i
);
471 change
= 2; // change ok
472 } else if (ch
== ':') {
473 change
= 1; // change, but once we hit a non-space char, don't change
474 } else if (ch
== ' ') {
476 fracRules
.setCharAt(i
, (UChar
)0x200e);
485 UErrorCode status
= U_ZERO_ERROR
;
487 RuleBasedNumberFormat
formatter(fracRules
, Locale::getEnglish(), perror
, status
);
488 if (U_FAILURE(status
)) {
489 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
491 static const char* const testData
[][2] = {
504 { "3.125", "3 1/8" },
505 { "4.1428", "4 1/7" },
506 { "5.1667", "5 1/6" },
509 { "8.333", "8 1/3" },
514 { "1.2856", "1 2/7" },
517 doTest(&formatter
, testData
, FALSE
); // exact values aren't parsable from fractions
522 #define LLAssert(a) \
523 if (!(a)) errln("FAIL: " #a)
525 void IntlTestRBNF::TestLLongConstructors()
527 logln("Testing constructors");
529 // constant (shouldn't really be public)
530 LLAssert(llong(llong::kD32
).asDouble() == llong::kD32
);
532 // internal constructor (shouldn't really be public)
533 LLAssert(llong(0, 1).asDouble() == 1);
534 LLAssert(llong(1, 0).asDouble() == llong::kD32
);
535 LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1);
537 // public empty constructor
538 LLAssert(llong().asDouble() == 0);
540 // public int32_t constructor
541 LLAssert(llong((int32_t)0).asInt() == (int32_t)0);
542 LLAssert(llong((int32_t)1).asInt() == (int32_t)1);
543 LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1);
544 LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff);
545 LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1);
546 LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000);
548 // public int16_t constructor
549 LLAssert(llong((int16_t)0).asInt() == (int16_t)0);
550 LLAssert(llong((int16_t)1).asInt() == (int16_t)1);
551 LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1);
552 LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff);
553 LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff);
554 LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000);
556 // public int8_t constructor
557 LLAssert(llong((int8_t)0).asInt() == (int8_t)0);
558 LLAssert(llong((int8_t)1).asInt() == (int8_t)1);
559 LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1);
560 LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f);
561 LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff);
562 LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80);
564 // public uint16_t constructor
565 LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0);
566 LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1);
567 LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1);
568 LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff);
569 LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff);
570 LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000);
572 // public uint32_t constructor
573 LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0);
574 LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1);
575 LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1);
576 LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff);
577 LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1);
578 LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
580 // public double constructor
581 LLAssert(llong((double)0).asDouble() == (double)0);
582 LLAssert(llong((double)1).asDouble() == (double)1);
583 LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff);
584 LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000);
585 LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001);
587 // can't access uprv_maxmantissa, so fake it
588 double maxmantissa
= (llong((int32_t)1) << 40).asDouble();
589 LLAssert(llong(maxmantissa
).asDouble() == maxmantissa
);
590 LLAssert(llong(-maxmantissa
).asDouble() == -maxmantissa
);
593 LLAssert(llong(llong(0, 1)).asDouble() == 1);
594 LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32
);
595 LLAssert(llong(llong(-1, (uint32_t)-1)).asDouble() == -1);
597 // asInt - test unsigned to signed narrowing conversion
598 LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff);
599 LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000);
601 // asUInt - test signed to unsigned narrowing conversion
602 LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1);
603 LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
605 // asDouble already tested
609 void IntlTestRBNF::TestLLongSimpleOperators()
611 logln("Testing simple operators");
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,1) != llong(1,1));
621 LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff));
624 LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff)));
627 LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1)));
630 LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff)));
631 LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1)));
634 LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1)));
635 LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1)));
638 LLAssert(llong(1, 1) > llong(1, 0));
639 LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff));
640 LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0));
641 LLAssert(llong(1, 0) > llong(0, 0x7fffffff));
642 LLAssert(llong(1, 0) > llong(0, 0xffffffff));
643 LLAssert(llong(0, 0) > llong(0x80000000, 1));
646 LLAssert(llong(1, 0) < llong(1, 1));
647 LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000));
648 LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1));
649 LLAssert(llong(0, 0x7fffffff) < llong(1, 0));
650 LLAssert(llong(0, 0xffffffff) < llong(1, 0));
651 LLAssert(llong(0x80000000, 1) < llong(0, 0));
654 LLAssert(llong(1, 1) >= llong(1, 0));
655 LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff));
656 LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0));
657 LLAssert(llong(1, 0) >= llong(0, 0x7fffffff));
658 LLAssert(llong(1, 0) >= llong(0, 0xffffffff));
659 LLAssert(llong(0, 0) >= llong(0x80000000, 1));
660 LLAssert(llong() >= llong(0, 0));
661 LLAssert(llong(1,0) >= llong(1, 0));
662 LLAssert(llong(0,1) >= llong(0, 1));
665 LLAssert(llong(1, 0) <= llong(1, 1));
666 LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000));
667 LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1));
668 LLAssert(llong(0, 0x7fffffff) <= llong(1, 0));
669 LLAssert(llong(0, 0xffffffff) <= llong(1, 0));
670 LLAssert(llong(0x80000000, 1) <= llong(0, 0));
671 LLAssert(llong() <= llong(0, 0));
672 LLAssert(llong(1,0) <= llong(1, 0));
673 LLAssert(llong(0,1) <= llong(0, 1));
676 LLAssert(llong() == (int32_t)0);
677 LLAssert(llong(0,1) == (int32_t)1);
680 LLAssert(llong(1,0) != (int32_t)0);
681 LLAssert(llong(0,1) != (int32_t)2);
682 LLAssert(llong(0,0xffffffff) != (int32_t)-1);
684 llong
negOne(0xffffffff, 0xffffffff);
687 LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff);
688 LLAssert(negOne
> (int32_t)-2);
689 LLAssert(llong(1, 0) > (int32_t)0x7fffffff);
690 LLAssert(llong(0, 0) > (int32_t)-1);
693 LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff);
694 LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1);
697 LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff);
698 LLAssert(negOne
>= (int32_t)-2);
699 LLAssert(llong(1, 0) >= (int32_t)0x7fffffff);
700 LLAssert(llong(0, 0) >= (int32_t)-1);
701 LLAssert(llong() >= (int32_t)0);
702 LLAssert(llong(0,1) >= (int32_t)1);
705 LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff);
706 LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1);
707 LLAssert(llong() <= (int32_t)0);
708 LLAssert(llong(0,1) <= (int32_t)1);
711 LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1);
714 LLAssert((llong(1, 1) <<= 0) == llong(1, 1));
715 LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000));
716 LLAssert((llong(1, 1) <<= 32) == llong(1, 0));
717 LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0));
718 LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used
719 LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used
722 LLAssert((llong((int32_t)1) << 5).asUInt() == 32);
724 // operator >>= (sign extended)
725 LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc));
726 LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde));
727 LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff));
728 LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000));
729 LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
730 LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used
732 // operator >> sign extended)
733 LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde));
735 // ushr (right shift without sign extension)
736 LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc));
737 LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde));
738 LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1));
739 LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000));
740 LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
741 LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used
744 LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
747 LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
750 LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
753 LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
756 LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
759 LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
762 LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa));
765 LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
768 LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
771 LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
773 // operator&=(uint32)
774 LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
776 // operator|=(uint32)
777 LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
779 // operator^=(uint32)
780 LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
783 LLAssert(llong(1, 0) == ++llong(0,0xffffffff));
786 LLAssert(llong(0,0xffffffff) == --llong(1, 0));
790 llong
n(0, 0xffffffff);
791 LLAssert(llong(0, 0xffffffff) == n
++);
792 LLAssert(llong(1, 0) == n
);
798 LLAssert(llong(1, 0) == n
--);
799 LLAssert(llong(0, 0xffffffff) == n
);
803 LLAssert(llong(0, 0) == -llong(0, 0));
804 LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1));
805 LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff));
806 LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1));
807 LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow
812 LLAssert((n
-= llong(0, 1)) == llong(0xffffffff, 0xffffffff));
813 LLAssert(n
== llong(0xffffffff, 0xffffffff));
816 LLAssert((n
-= llong(0, 1)) == llong(0, 0xffffffff));
817 LLAssert(n
== llong(0, 0xffffffff));
823 LLAssert((n
- llong(0, 1)) == llong(0xffffffff, 0xffffffff));
824 LLAssert(n
== llong(0, 0));
827 LLAssert((n
- llong(0, 1)) == llong(0, 0xffffffff));
828 LLAssert(n
== llong(1, 0));
833 llong
n(0xffffffff, 0xffffffff);
834 LLAssert((n
+= llong(0, 1)) == llong(0, 0));
835 LLAssert(n
== llong(0, 0));
837 n
= llong(0, 0xffffffff);
838 LLAssert((n
+= llong(0, 1)) == llong(1, 0));
839 LLAssert(n
== llong(1, 0));
844 llong
n(0xffffffff, 0xffffffff);
845 LLAssert((n
+ llong(0, 1)) == llong(0, 0));
846 LLAssert(n
== llong(0xffffffff, 0xffffffff));
848 n
= llong(0, 0xffffffff);
849 LLAssert((n
+ llong(0, 1)) == llong(1, 0));
850 LLAssert(n
== llong(0, 0xffffffff));
855 void IntlTestRBNF::TestLLong()
857 logln("Starting TestLLong");
859 TestLLongConstructors();
861 TestLLongSimpleOperators();
863 logln("Testing operator*=, operator*");
865 // operator*=, operator*
866 // small and large values, positive, &NEGative, zero
867 // also test commutivity
870 const llong
ONE(0, 1);
871 const llong
NEG_ONE((int32_t)-1);
872 const llong
THREE(0, 3);
873 const llong
NEG_THREE((int32_t)-3);
874 const llong
TWO_TO_16(0, 0x10000);
875 const llong NEG_TWO_TO_16
= -TWO_TO_16
;
876 const llong
TWO_TO_32(1, 0);
877 const llong NEG_TWO_TO_32
= -TWO_TO_32
;
879 const llong
NINE(0, 9);
880 const llong NEG_NINE
= -NINE
;
882 const llong
TWO_TO_16X3(0, 0x00030000);
883 const llong NEG_TWO_TO_16X3
= -TWO_TO_16X3
;
885 const llong
TWO_TO_32X3(3, 0);
886 const llong NEG_TWO_TO_32X3
= -TWO_TO_32X3
;
888 const llong
TWO_TO_48(0x10000, 0);
889 const llong NEG_TWO_TO_48
= -TWO_TO_48
;
891 const int32_t VALUE_WIDTH
= 9;
892 const llong
* values
[VALUE_WIDTH
] = {
893 &ZERO
, &ONE
, &NEG_ONE
, &THREE
, &NEG_THREE
, &TWO_TO_16
, &NEG_TWO_TO_16
, &TWO_TO_32
, &NEG_TWO_TO_32
896 const llong
* answers
[VALUE_WIDTH
*VALUE_WIDTH
] = {
897 &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
, &ZERO
,
898 &ZERO
, &ONE
, &NEG_ONE
, &THREE
, &NEG_THREE
, &TWO_TO_16
, &NEG_TWO_TO_16
, &TWO_TO_32
, &NEG_TWO_TO_32
,
899 &ZERO
, &NEG_ONE
, &ONE
, &NEG_THREE
, &THREE
, &NEG_TWO_TO_16
, &TWO_TO_16
, &NEG_TWO_TO_32
, &TWO_TO_32
,
900 &ZERO
, &THREE
, &NEG_THREE
, &NINE
, &NEG_NINE
, &TWO_TO_16X3
, &NEG_TWO_TO_16X3
, &TWO_TO_32X3
, &NEG_TWO_TO_32X3
,
901 &ZERO
, &NEG_THREE
, &THREE
, &NEG_NINE
, &NINE
, &NEG_TWO_TO_16X3
, &TWO_TO_16X3
, &NEG_TWO_TO_32X3
, &TWO_TO_32X3
,
902 &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
,
903 &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
,
904 &ZERO
, &TWO_TO_32
, &NEG_TWO_TO_32
, &TWO_TO_32X3
, &NEG_TWO_TO_32X3
, &TWO_TO_48
, &NEG_TWO_TO_48
, &ZERO
, &ZERO
,
905 &ZERO
, &NEG_TWO_TO_32
, &TWO_TO_32
, &NEG_TWO_TO_32X3
, &TWO_TO_32X3
, &NEG_TWO_TO_48
, &TWO_TO_48
, &ZERO
, &ZERO
908 for (int i
= 0; i
< VALUE_WIDTH
; ++i
) {
909 for (int j
= 0; j
< VALUE_WIDTH
; ++j
) {
910 llong lhs
= *values
[i
];
911 llong rhs
= *values
[j
];
912 llong ans
= *answers
[i
*VALUE_WIDTH
+ j
];
916 LLAssert((n
*= rhs
) == ans
);
920 LLAssert((n
* rhs
) == ans
);
926 logln("Testing operator/=, operator/");
927 // operator/=, operator/
928 // test num = 0, div = 0, pos/neg, > 2^32, div > num
931 const llong
ONE(0, 1);
932 const llong NEG_ONE
= -ONE
;
933 const llong
MAX(0x7fffffff, 0xffffffff);
934 const llong
MIN(0x80000000, 0);
935 const llong
TWO(0, 2);
936 const llong NEG_TWO
= -TWO
;
937 const llong
FIVE(0, 5);
938 const llong NEG_FIVE
= -FIVE
;
939 const llong
TWO_TO_32(1, 0);
940 const llong NEG_TWO_TO_32
= -TWO_TO_32
;
941 const llong TWO_TO_32d5
= llong(TWO_TO_32
.asDouble()/5.0);
942 const llong NEG_TWO_TO_32d5
= -TWO_TO_32d5
;
943 const llong TWO_TO_32X5
= TWO_TO_32
* FIVE
;
944 const llong NEG_TWO_TO_32X5
= -TWO_TO_32X5
;
946 const llong
* tuples
[] = { // lhs, rhs, ans
949 &NEG_ONE
, &ZERO
, &MIN
,
951 &ONE
, &NEG_ONE
, &NEG_ONE
,
952 &NEG_ONE
, &ONE
, &NEG_ONE
,
953 &NEG_ONE
, &NEG_ONE
, &ONE
,
955 &FIVE
, &NEG_TWO
, &NEG_TWO
,
956 &NEG_FIVE
, &TWO
, &NEG_TWO
,
957 &NEG_FIVE
, &NEG_TWO
, &TWO
,
959 &TWO
, &NEG_FIVE
, &ZERO
,
960 &NEG_TWO
, &FIVE
, &ZERO
,
961 &NEG_TWO
, &NEG_FIVE
, &ZERO
,
962 &TWO_TO_32
, &TWO_TO_32
, &ONE
,
963 &TWO_TO_32
, &NEG_TWO_TO_32
, &NEG_ONE
,
964 &NEG_TWO_TO_32
, &TWO_TO_32
, &NEG_ONE
,
965 &NEG_TWO_TO_32
, &NEG_TWO_TO_32
, &ONE
,
966 &TWO_TO_32
, &FIVE
, &TWO_TO_32d5
,
967 &TWO_TO_32
, &NEG_FIVE
, &NEG_TWO_TO_32d5
,
968 &NEG_TWO_TO_32
, &FIVE
, &NEG_TWO_TO_32d5
,
969 &NEG_TWO_TO_32
, &NEG_FIVE
, &TWO_TO_32d5
,
970 &TWO_TO_32X5
, &FIVE
, &TWO_TO_32
,
971 &TWO_TO_32X5
, &NEG_FIVE
, &NEG_TWO_TO_32
,
972 &NEG_TWO_TO_32X5
, &FIVE
, &NEG_TWO_TO_32
,
973 &NEG_TWO_TO_32X5
, &NEG_FIVE
, &TWO_TO_32
,
974 &TWO_TO_32X5
, &TWO_TO_32
, &FIVE
,
975 &TWO_TO_32X5
, &NEG_TWO_TO_32
, &NEG_FIVE
,
976 &NEG_TWO_TO_32X5
, &NEG_TWO_TO_32
, &FIVE
,
977 &NEG_TWO_TO_32X5
, &TWO_TO_32
, &NEG_FIVE
979 const int TUPLE_WIDTH
= 3;
980 const int TUPLE_COUNT
= (int)(sizeof(tuples
)/sizeof(tuples
[0]))/TUPLE_WIDTH
;
981 for (int i
= 0; i
< TUPLE_COUNT
; ++i
) {
982 const llong lhs
= *tuples
[i
*TUPLE_WIDTH
+0];
983 const llong rhs
= *tuples
[i
*TUPLE_WIDTH
+1];
984 const llong ans
= *tuples
[i
*TUPLE_WIDTH
+2];
987 if (!((n
/= rhs
) == ans
)) {
988 errln("fail: (n /= rhs) == ans");
993 LLAssert((n
/ rhs
) == ans
);
998 logln("Testing operator%%=, operator%%");
999 //operator%=, operator%
1002 const llong
ONE(0, 1);
1003 const llong
TWO(0, 2);
1004 const llong
THREE(0,3);
1005 const llong
FOUR(0, 4);
1006 const llong
FIVE(0, 5);
1007 const llong
SIX(0, 6);
1009 const llong NEG_ONE
= -ONE
;
1010 const llong NEG_TWO
= -TWO
;
1011 const llong NEG_THREE
= -THREE
;
1012 const llong NEG_FOUR
= -FOUR
;
1013 const llong NEG_FIVE
= -FIVE
;
1014 const llong NEG_SIX
= -SIX
;
1016 const llong
NINETY_NINE(0, 99);
1017 const llong
HUNDRED(0, 100);
1018 const llong
HUNDRED_ONE(0, 101);
1020 const llong
BIG(0x12345678, 0x9abcdef0);
1021 const llong
BIG_FIVE(BIG
* FIVE
);
1022 const llong BIG_FIVEm1
= BIG_FIVE
- ONE
;
1023 const llong BIG_FIVEp1
= BIG_FIVE
+ ONE
;
1025 const llong
* tuples
[] = {
1026 &ZERO
, &FIVE
, &ZERO
,
1029 &THREE
, &FIVE
, &THREE
,
1030 &FOUR
, &FIVE
, &FOUR
,
1031 &FIVE
, &FIVE
, &ZERO
,
1033 &ZERO
, &NEG_FIVE
, &ZERO
,
1034 &ONE
, &NEG_FIVE
, &ONE
,
1035 &TWO
, &NEG_FIVE
, &TWO
,
1036 &THREE
, &NEG_FIVE
, &THREE
,
1037 &FOUR
, &NEG_FIVE
, &FOUR
,
1038 &FIVE
, &NEG_FIVE
, &ZERO
,
1039 &SIX
, &NEG_FIVE
, &ONE
,
1040 &NEG_ONE
, &FIVE
, &NEG_ONE
,
1041 &NEG_TWO
, &FIVE
, &NEG_TWO
,
1042 &NEG_THREE
, &FIVE
, &NEG_THREE
,
1043 &NEG_FOUR
, &FIVE
, &NEG_FOUR
,
1044 &NEG_FIVE
, &FIVE
, &ZERO
,
1045 &NEG_SIX
, &FIVE
, &NEG_ONE
,
1046 &NEG_ONE
, &NEG_FIVE
, &NEG_ONE
,
1047 &NEG_TWO
, &NEG_FIVE
, &NEG_TWO
,
1048 &NEG_THREE
, &NEG_FIVE
, &NEG_THREE
,
1049 &NEG_FOUR
, &NEG_FIVE
, &NEG_FOUR
,
1050 &NEG_FIVE
, &NEG_FIVE
, &ZERO
,
1051 &NEG_SIX
, &NEG_FIVE
, &NEG_ONE
,
1052 &NINETY_NINE
, &FIVE
, &FOUR
,
1053 &HUNDRED
, &FIVE
, &ZERO
,
1054 &HUNDRED_ONE
, &FIVE
, &ONE
,
1055 &BIG_FIVEm1
, &FIVE
, &FOUR
,
1056 &BIG_FIVE
, &FIVE
, &ZERO
,
1057 &BIG_FIVEp1
, &FIVE
, &ONE
1059 const int TUPLE_WIDTH
= 3;
1060 const int TUPLE_COUNT
= (int)(sizeof(tuples
)/sizeof(tuples
[0]))/TUPLE_WIDTH
;
1061 for (int i
= 0; i
< TUPLE_COUNT
; ++i
) {
1062 const llong lhs
= *tuples
[i
*TUPLE_WIDTH
+0];
1063 const llong rhs
= *tuples
[i
*TUPLE_WIDTH
+1];
1064 const llong ans
= *tuples
[i
*TUPLE_WIDTH
+2];
1067 if (!((n
%= rhs
) == ans
)) {
1068 errln("fail: (n %= rhs) == ans");
1073 LLAssert((n
% rhs
) == ans
);
1078 logln("Testing pow");
1080 LLAssert(llong(0, 0).pow(0) == llong(0, 0));
1081 LLAssert(llong(0, 0).pow(2) == llong(0, 0));
1082 LLAssert(llong(0, 2).pow(0) == llong(0, 1));
1083 LLAssert(llong(0, 2).pow(2) == llong(0, 4));
1084 LLAssert(llong(0, 2).pow(32) == llong(1, 0));
1085 LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5));
1089 const llong
n(0xffffffff,0xffffffff);
1090 LLAssert(n
.abs() == llong(0, 1));
1094 logln("Testing atoll");
1096 const char empty
[] = "";
1097 const char zero
[] = "0";
1098 const char neg_one
[] = "-1";
1099 const char neg_12345
[] = "-12345";
1100 const char big1
[] = "123456789abcdef0";
1101 const char big2
[] = "fFfFfFfFfFfFfFfF";
1102 LLAssert(llong::atoll(empty
) == llong(0, 0));
1103 LLAssert(llong::atoll(zero
) == llong(0, 0));
1104 LLAssert(llong::atoll(neg_one
) == llong(0xffffffff, 0xffffffff));
1105 LLAssert(llong::atoll(neg_12345
) == -llong(0, 12345));
1106 LLAssert(llong::atoll(big1
, 16) == llong(0x12345678, 0x9abcdef0));
1107 LLAssert(llong::atoll(big2
, 16) == llong(0xffffffff, 0xffffffff));
1111 const UChar uempty
[] = { 0 };
1112 const UChar uzero
[] = { 0x30, 0 };
1113 const UChar uneg_one
[] = { 0x2d, 0x31, 0 };
1114 const UChar uneg_12345
[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 };
1115 const UChar ubig1
[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 };
1116 const UChar ubig2
[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 };
1117 LLAssert(llong::utoll(uempty
) == llong(0, 0));
1118 LLAssert(llong::utoll(uzero
) == llong(0, 0));
1119 LLAssert(llong::utoll(uneg_one
) == llong(0xffffffff, 0xffffffff));
1120 LLAssert(llong::utoll(uneg_12345
) == -llong(0, 12345));
1121 LLAssert(llong::utoll(ubig1
, 16) == llong(0x12345678, 0x9abcdef0));
1122 LLAssert(llong::utoll(ubig2
, 16) == llong(0xffffffff, 0xffffffff));
1125 logln("Testing lltoa");
1128 char buf
[64]; // ascii
1129 LLAssert((llong(0, 0).lltoa(buf
, (uint32_t)sizeof(buf
)) == 1) && (strcmp(buf
, zero
) == 0));
1130 LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf
, (uint32_t)sizeof(buf
)) == 2) && (strcmp(buf
, neg_one
) == 0));
1131 LLAssert(((-llong(0, 12345)).lltoa(buf
, (uint32_t)sizeof(buf
)) == 6) && (strcmp(buf
, neg_12345
) == 0));
1132 LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf
, (uint32_t)sizeof(buf
), 16) == 16) && (strcmp(buf
, big1
) == 0));
1136 logln("Testing u_lltoa");
1140 LLAssert((llong(0, 0).lltou(buf
, (uint32_t)sizeof(buf
)) == 1) && (u_strcmp(buf
, uzero
) == 0));
1141 LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf
, (uint32_t)sizeof(buf
)) == 2) && (u_strcmp(buf
, uneg_one
) == 0));
1142 LLAssert(((-llong(0, 12345)).lltou(buf
, (uint32_t)sizeof(buf
)) == 6) && (u_strcmp(buf
, uneg_12345
) == 0));
1143 LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf
, (uint32_t)sizeof(buf
), 16) == 16) && (u_strcmp(buf
, ubig1
) == 0));
1151 IntlTestRBNF::TestEnglishSpellout()
1153 UErrorCode status
= U_ZERO_ERROR
;
1154 RuleBasedNumberFormat
* formatter
1155 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getUS(), status
);
1156 if (U_FAILURE(status
)) {
1157 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1159 static const char* const testData
[][2] = {
1162 { "15", "fifteen" },
1164 { "23", "twenty-three" },
1165 { "73", "seventy-three" },
1166 { "88", "eighty-eight" },
1167 { "100", "one hundred" },
1168 { "106", "one hundred six" },
1169 { "127", "one hundred twenty-seven" },
1170 { "200", "two hundred" },
1171 { "579", "five hundred seventy-nine" },
1172 { "1,000", "one thousand" },
1173 { "2,000", "two thousand" },
1174 { "3,004", "three thousand four" },
1175 { "4,567", "four thousand five hundred sixty-seven" },
1176 { "15,943", "fifteen thousand nine hundred forty-three" },
1177 { "2,345,678", "two million three hundred forty-five thousand six hundred seventy-eight" },
1178 { "-36", "minus thirty-six" },
1179 { "234.567", "two hundred thirty-four point five six seven" },
1183 doTest(formatter
, testData
, TRUE
);
1185 #if !UCONFIG_NO_COLLATION
1186 if( !logKnownIssue("9503") ) {
1187 formatter
->setLenient(TRUE
);
1188 static const char* lpTestData
[][2] = {
1189 { "fifty-7", "57" },
1190 { " fifty-7", "57" },
1191 { " fifty-7", "57" },
1192 { "2 thousand six HUNDRED fifty-7", "2,657" },
1193 { "fifteen hundred and zero", "1,500" },
1194 { "FOurhundred thiRTY six", "436" },
1197 doLenientParseTest(formatter
, lpTestData
);
1205 IntlTestRBNF::TestOrdinalAbbreviations()
1207 UErrorCode status
= U_ZERO_ERROR
;
1208 RuleBasedNumberFormat
* formatter
1209 = new RuleBasedNumberFormat(URBNF_ORDINAL
, Locale::getUS(), status
);
1211 if (U_FAILURE(status
)) {
1212 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1214 static const char* const testData
[][2] = {
1231 { "12,345", "12,345th" },
1235 doTest(formatter
, testData
, FALSE
);
1241 IntlTestRBNF::TestDurations()
1243 UErrorCode status
= U_ZERO_ERROR
;
1244 RuleBasedNumberFormat
* formatter
1245 = new RuleBasedNumberFormat(URBNF_DURATION
, Locale::getUS(), status
);
1247 if (U_FAILURE(status
)) {
1248 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1250 static const char* const testData
[][2] = {
1251 { "3,600", "1:00:00" }, //move me and I fail
1254 { "24", "24 sec." },
1259 // { "3,600", "1:00:00" },
1260 { "3,740", "1:02:20" },
1261 { "10,293", "2:51:33" },
1265 doTest(formatter
, testData
, TRUE
);
1267 #if !UCONFIG_NO_COLLATION
1268 formatter
->setLenient(TRUE
);
1269 static const char* lpTestData
[][2] = {
1270 { "2-51-33", "10,293" },
1273 doLenientParseTest(formatter
, lpTestData
);
1280 IntlTestRBNF::TestSpanishSpellout()
1282 UErrorCode status
= U_ZERO_ERROR
;
1283 RuleBasedNumberFormat
* formatter
1284 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("es", "ES", ""), status
);
1286 if (U_FAILURE(status
)) {
1287 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1289 static const char* const testData
[][2] = {
1292 { "16", "diecis\\u00e9is" },
1294 { "24", "veinticuatro" },
1295 { "26", "veintis\\u00e9is" },
1296 { "73", "setenta y tres" },
1297 { "88", "ochenta y ocho" },
1299 { "106", "ciento seis" },
1300 { "127", "ciento veintisiete" },
1301 { "200", "doscientos" },
1302 { "579", "quinientos setenta y nueve" },
1304 { "2,000", "dos mil" },
1305 { "3,004", "tres mil cuatro" },
1306 { "4,567", "cuatro mil quinientos sesenta y siete" },
1307 { "15,943", "quince mil novecientos cuarenta y tres" },
1308 { "2,345,678", "dos millones trescientos cuarenta y cinco mil seiscientos setenta y ocho"},
1309 { "-36", "menos treinta y seis" },
1310 { "234.567", "doscientos treinta y cuatro coma cinco seis siete" },
1314 doTest(formatter
, testData
, TRUE
);
1320 IntlTestRBNF::TestFrenchSpellout()
1322 UErrorCode status
= U_ZERO_ERROR
;
1323 RuleBasedNumberFormat
* formatter
1324 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getFrance(), status
);
1326 if (U_FAILURE(status
)) {
1327 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1329 static const char* const testData
[][2] = {
1333 { "21", "vingt-et-un" },
1334 { "23", "vingt-trois" },
1335 { "62", "soixante-deux" },
1336 { "70", "soixante-dix" },
1337 { "71", "soixante-et-onze" },
1338 { "73", "soixante-treize" },
1339 { "80", "quatre-vingts" },
1340 { "88", "quatre-vingt-huit" },
1342 { "106", "cent six" },
1343 { "127", "cent vingt-sept" },
1344 { "200", "deux cents" },
1345 { "579", "cinq cent soixante-dix-neuf" },
1346 { "1,000", "mille" },
1347 { "1,123", "mille cent vingt-trois" },
1348 { "1,594", "mille cinq cent quatre-vingt-quatorze" },
1349 { "2,000", "deux mille" },
1350 { "3,004", "trois mille quatre" },
1351 { "4,567", "quatre mille cinq cent soixante-sept" },
1352 { "15,943", "quinze mille neuf cent quarante-trois" },
1353 { "2,345,678", "deux millions trois cent quarante-cinq mille six cent soixante-dix-huit" },
1354 { "-36", "moins trente-six" },
1355 { "234.567", "deux cent trente-quatre virgule cinq six sept" },
1359 doTest(formatter
, testData
, TRUE
);
1361 #if !UCONFIG_NO_COLLATION
1362 formatter
->setLenient(TRUE
);
1363 static const char* lpTestData
[][2] = {
1364 { "trente-et-un", "31" },
1365 { "un cent quatre vingt dix huit", "198" },
1368 doLenientParseTest(formatter
, lpTestData
);
1374 static const char* const swissFrenchTestData
[][2] = {
1378 { "21", "vingt-et-un" },
1379 { "23", "vingt-trois" },
1380 { "62", "soixante-deux" },
1381 { "70", "septante" },
1382 { "71", "septante-et-un" },
1383 { "73", "septante-trois" },
1384 { "80", "huitante" },
1385 { "88", "huitante-huit" },
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" },
1405 IntlTestRBNF::TestSwissFrenchSpellout()
1407 UErrorCode status
= U_ZERO_ERROR
;
1408 RuleBasedNumberFormat
* formatter
1409 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "CH", ""), status
);
1411 if (U_FAILURE(status
)) {
1412 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1414 doTest(formatter
, swissFrenchTestData
, TRUE
);
1419 static const char* const belgianFrenchTestData
[][2] = {
1423 { "21", "vingt-et-un" },
1424 { "23", "vingt-trois" },
1425 { "62", "soixante-deux" },
1426 { "70", "septante" },
1427 { "71", "septante-et-un" },
1428 { "73", "septante-trois" },
1429 { "80", "quatre-vingts" },
1430 { "88", "quatre-vingt huit" },
1431 { "90", "nonante" },
1432 { "91", "nonante-et-un" },
1433 { "95", "nonante-cinq" },
1435 { "106", "cent six" },
1436 { "127", "cent vingt-sept" },
1437 { "200", "deux cents" },
1438 { "579", "cinq cent septante-neuf" },
1439 { "1,000", "mille" },
1440 { "1,123", "mille cent vingt-trois" },
1441 { "1,594", "mille cinq cent nonante-quatre" },
1442 { "2,000", "deux mille" },
1443 { "3,004", "trois mille quatre" },
1444 { "4,567", "quatre mille cinq cent soixante-sept" },
1445 { "15,943", "quinze mille neuf cent quarante-trois" },
1446 { "2,345,678", "deux millions trois cent quarante-cinq mille six cent septante-huit" },
1447 { "-36", "moins trente-six" },
1448 { "234.567", "deux cent trente-quatre virgule cinq six sept" },
1454 IntlTestRBNF::TestBelgianFrenchSpellout()
1456 UErrorCode status
= U_ZERO_ERROR
;
1457 RuleBasedNumberFormat
* formatter
1458 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("fr", "BE", ""), status
);
1460 if (U_FAILURE(status
)) {
1461 errcheckln(status
, "rbnf status: 0x%x (%s)\n", status
, u_errorName(status
));
1462 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1464 // Belgian french should match Swiss french.
1465 doTest(formatter
, belgianFrenchTestData
, TRUE
);
1471 IntlTestRBNF::TestItalianSpellout()
1473 UErrorCode status
= U_ZERO_ERROR
;
1474 RuleBasedNumberFormat
* formatter
1475 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getItalian(), status
);
1477 if (U_FAILURE(status
)) {
1478 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1480 static const char* const testData
[][2] = {
1482 { "15", "quindici" },
1484 { "23", "venti\\u00ADtr\\u00E9" },
1485 { "73", "settanta\\u00ADtr\\u00E9" },
1486 { "88", "ottant\\u00ADotto" },
1488 { "101", "cento\\u00ADuno" },
1489 { "103", "cento\\u00ADtr\\u00E9" },
1490 { "106", "cento\\u00ADsei" },
1491 { "108", "cent\\u00ADotto" },
1492 { "127", "cento\\u00ADventi\\u00ADsette" },
1493 { "181", "cent\\u00ADottant\\u00ADuno" },
1494 { "200", "due\\u00ADcento" },
1495 { "579", "cinque\\u00ADcento\\u00ADsettanta\\u00ADnove" },
1496 { "1,000", "mille" },
1497 { "2,000", "due\\u00ADmila" },
1498 { "3,004", "tre\\u00ADmila\\u00ADquattro" },
1499 { "4,567", "quattro\\u00ADmila\\u00ADcinque\\u00ADcento\\u00ADsessanta\\u00ADsette" },
1500 { "15,943", "quindici\\u00ADmila\\u00ADnove\\u00ADcento\\u00ADquaranta\\u00ADtr\\u00E9" },
1501 { "-36", "meno trenta\\u00ADsei" },
1502 { "234.567", "due\\u00ADcento\\u00ADtrenta\\u00ADquattro virgola cinque sei sette" },
1506 doTest(formatter
, testData
, TRUE
);
1512 IntlTestRBNF::TestPortugueseSpellout()
1514 UErrorCode status
= U_ZERO_ERROR
;
1515 RuleBasedNumberFormat
* formatter
1516 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("pt","BR",""), status
);
1518 if (U_FAILURE(status
)) {
1519 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1521 static const char* const testData
[][2] = {
1525 { "23", "vinte e tr\\u00EAs" },
1526 { "73", "setenta e tr\\u00EAs" },
1527 { "88", "oitenta e oito" },
1529 { "106", "cento e seis" },
1530 { "108", "cento e oito" },
1531 { "127", "cento e vinte e sete" },
1532 { "181", "cento e oitenta e um" },
1533 { "200", "duzentos" },
1534 { "579", "quinhentos e setenta e nove" },
1536 { "2,000", "dois mil" },
1537 { "3,004", "tr\\u00EAs mil e quatro" },
1538 { "4,567", "quatro mil e quinhentos e sessenta e sete" },
1539 { "15,943", "quinze mil e novecentos e quarenta e tr\\u00EAs" },
1540 { "-36", "menos trinta e seis" },
1541 { "234.567", "duzentos e trinta e quatro v\\u00EDrgula cinco seis sete" },
1545 doTest(formatter
, testData
, TRUE
);
1550 IntlTestRBNF::TestGermanSpellout()
1552 UErrorCode status
= U_ZERO_ERROR
;
1553 RuleBasedNumberFormat
* formatter
1554 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale::getGermany(), status
);
1556 if (U_FAILURE(status
)) {
1557 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1559 static const char* const testData
[][2] = {
1561 { "15", "f\\u00fcnfzehn" },
1562 { "20", "zwanzig" },
1563 { "23", "drei\\u00ADund\\u00ADzwanzig" },
1564 { "73", "drei\\u00ADund\\u00ADsiebzig" },
1565 { "88", "acht\\u00ADund\\u00ADachtzig" },
1566 { "100", "ein\\u00ADhundert" },
1567 { "106", "ein\\u00ADhundert\\u00ADsechs" },
1568 { "127", "ein\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADzwanzig" },
1569 { "200", "zwei\\u00ADhundert" },
1570 { "579", "f\\u00fcnf\\u00ADhundert\\u00ADneun\\u00ADund\\u00ADsiebzig" },
1571 { "1,000", "ein\\u00ADtausend" },
1572 { "2,000", "zwei\\u00ADtausend" },
1573 { "3,004", "drei\\u00ADtausend\\u00ADvier" },
1574 { "4,567", "vier\\u00ADtausend\\u00ADf\\u00fcnf\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADsechzig" },
1575 { "15,943", "f\\u00fcnfzehn\\u00ADtausend\\u00ADneun\\u00ADhundert\\u00ADdrei\\u00ADund\\u00ADvierzig" },
1576 { "2,345,678", "zwei Millionen drei\\u00ADhundert\\u00ADf\\u00fcnf\\u00ADund\\u00ADvierzig\\u00ADtausend\\u00ADsechs\\u00ADhundert\\u00ADacht\\u00ADund\\u00ADsiebzig" },
1580 doTest(formatter
, testData
, TRUE
);
1582 #if !UCONFIG_NO_COLLATION
1583 formatter
->setLenient(TRUE
);
1584 static const char* lpTestData
[][2] = {
1585 { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
1588 doLenientParseTest(formatter
, lpTestData
);
1595 IntlTestRBNF::TestThaiSpellout()
1597 UErrorCode status
= U_ZERO_ERROR
;
1598 RuleBasedNumberFormat
* formatter
1599 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("th"), status
);
1601 if (U_FAILURE(status
)) {
1602 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1604 static const char* const testData
[][2] = {
1605 { "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" },
1606 { "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1607 { "10", "\\u0e2a\\u0e34\\u0e1a" },
1608 { "11", "\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1609 { "21", "\\u0e22\\u0e35\\u0e48\\u200b\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1610 { "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e23\\u0e49\\u0e2d\\u0e22\\u200b\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1611 { "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e08\\u0e38\\u0e14\\u200b\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" },
1615 doTest(formatter
, testData
, TRUE
);
1621 IntlTestRBNF::TestSwedishSpellout()
1623 UErrorCode status
= U_ZERO_ERROR
;
1624 RuleBasedNumberFormat
* formatter
1625 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("sv"), status
);
1627 if (U_FAILURE(status
)) {
1628 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1630 static const char* testDataDefault
[][2] = {
1631 { "101", "ett\\u00adhundra\\u00adett" },
1632 { "123", "ett\\u00adhundra\\u00adtjugo\\u00adtre" },
1633 { "1,001", "et\\u00adtusen ett" },
1634 { "1,100", "et\\u00adtusen ett\\u00adhundra" },
1635 { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1636 { "1,234", "et\\u00adtusen tv\\u00e5\\u00adhundra\\u00adtrettio\\u00adfyra" },
1637 { "10,001", "tio\\u00adtusen ett" },
1638 { "11,000", "elva\\u00adtusen" },
1639 { "12,000", "tolv\\u00adtusen" },
1640 { "20,000", "tjugo\\u00adtusen" },
1641 { "21,000", "tjugo\\u00adet\\u00adtusen" },
1642 { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
1643 { "200,000", "tv\\u00e5\\u00adhundra\\u00adtusen" },
1644 { "201,000", "tv\\u00e5\\u00adhundra\\u00adet\\u00adtusen" },
1645 { "200,200", "tv\\u00e5\\u00adhundra\\u00adtusen tv\\u00e5\\u00adhundra" },
1646 { "2,002,000", "tv\\u00e5 miljoner tv\\u00e5\\u00adtusen" },
1647 { "12,345,678", "tolv miljoner tre\\u00adhundra\\u00adfyrtio\\u00adfem\\u00adtusen sex\\u00adhundra\\u00adsjuttio\\u00ad\\u00e5tta" },
1648 { "123,456.789", "ett\\u00adhundra\\u00adtjugo\\u00adtre\\u00adtusen fyra\\u00adhundra\\u00adfemtio\\u00adsex komma sju \\u00e5tta nio" },
1649 { "-12,345.678", "minus tolv\\u00adtusen tre\\u00adhundra\\u00adfyrtio\\u00adfem komma sex sju \\u00e5tta" },
1652 doTest(formatter
, testDataDefault
, TRUE
);
1654 static const char* testDataNeutrum
[][2] = {
1655 { "101", "ett\\u00adhundra\\u00adett" },
1656 { "1,001", "et\\u00adtusen ett" },
1657 { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1658 { "10,001", "tio\\u00adtusen ett" },
1659 { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
1663 formatter
->setDefaultRuleSet("%spellout-cardinal-neuter", status
);
1664 if (U_SUCCESS(status
)) {
1665 logln(" testing spellout-cardinal-neuter rules");
1666 doTest(formatter
, testDataNeutrum
, TRUE
);
1669 errln("Can't test spellout-cardinal-neuter rules");
1672 static const char* testDataYear
[][2] = {
1673 { "101", "ett\\u00adhundra\\u00adett" },
1674 { "900", "nio\\u00adhundra" },
1675 { "1,001", "et\\u00adtusen ett" },
1676 { "1,100", "elva\\u00adhundra" },
1677 { "1,101", "elva\\u00adhundra\\u00adett" },
1678 { "1,234", "tolv\\u00adhundra\\u00adtrettio\\u00adfyra" },
1679 { "2,001", "tjugo\\u00adhundra\\u00adett" },
1680 { "10,001", "tio\\u00adtusen ett" },
1684 status
= U_ZERO_ERROR
;
1685 formatter
->setDefaultRuleSet("%spellout-numbering-year", status
);
1686 if (U_SUCCESS(status
)) {
1687 logln("testing year rules");
1688 doTest(formatter
, testDataYear
, TRUE
);
1691 errln("Can't test year rules");
1699 IntlTestRBNF::TestSmallValues()
1701 UErrorCode status
= U_ZERO_ERROR
;
1702 RuleBasedNumberFormat
* formatter
1703 = new RuleBasedNumberFormat(URBNF_SPELLOUT
, Locale("en_US"), status
);
1705 if (U_FAILURE(status
)) {
1706 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1708 static const char* const testDataDefault
[][2] = {
1709 { "0.001", "zero point zero zero one" },
1710 { "0.0001", "zero point zero zero zero one" },
1711 { "0.00001", "zero point zero zero zero zero one" },
1712 { "0.000001", "zero point zero zero zero zero zero one" },
1713 { "0.0000001", "zero point zero zero zero zero zero zero one" },
1714 { "0.00000001", "zero point zero zero zero zero zero zero zero one" },
1715 { "0.000000001", "zero point zero zero zero zero zero zero zero zero one" },
1716 { "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" },
1717 { "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" },
1718 { "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" },
1719 { "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" },
1720 { "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1721 { "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1722 { "10,000,000.001", "ten million point zero zero one" },
1723 { "10,000,000.0001", "ten million point zero zero zero one" },
1724 { "10,000,000.00001", "ten million point zero zero zero zero one" },
1725 { "10,000,000.000001", "ten million point zero zero zero zero zero one" },
1726 { "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" },
1727 // { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" },
1728 // { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" },
1729 { "10,000,000", "ten million" },
1730 // { "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" },
1731 // { "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" },
1732 // { "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" },
1733 { "1,234,567.7654321", "one million two hundred thirty-four thousand five hundred sixty-seven point seven six five four three two one" },
1734 { "123,456.654321", "one hundred twenty-three thousand four hundred fifty-six point six five four three two one" },
1735 { "12,345.54321", "twelve thousand three hundred forty-five point five four three two one" },
1736 { "1,234.4321", "one thousand two hundred thirty-four point four three two one" },
1737 { "123.321", "one hundred twenty-three point three two one" },
1738 { "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" },
1739 { "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" },
1743 doTest(formatter
, testDataDefault
, TRUE
);
1750 IntlTestRBNF::TestLocalizations(void)
1753 UnicodeString
rules("%main:0:no;1:some;100:a lot;1000:tons;\n"
1754 "%other:0:nada;1:yah, some;100:plenty;1000:more'n you'll ever need");
1756 UErrorCode status
= U_ZERO_ERROR
;
1758 RuleBasedNumberFormat
formatter(rules
, perror
, status
);
1759 if (U_FAILURE(status
)) {
1760 errcheckln(status
, "FAIL: could not construct formatter - %s", u_errorName(status
));
1763 static const char* const testData
[][2] = {
1765 { "5", "yah, some" },
1766 { "423", "plenty" },
1767 { "12345", "more'n you'll ever need" },
1770 doTest(&formatter
, testData
, FALSE
);
1774 UnicodeString
loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>");
1775 static const char* const testData
[][2] = {
1779 { "12345", "tons" },
1782 RuleBasedNumberFormat
formatter0(rules
, loc
, perror
, status
);
1783 if (U_FAILURE(status
)) {
1784 errln("failed to build second formatter");
1786 doTest(&formatter0
, testData
, FALSE
);
1789 // exercise localization info
1790 Locale
locale0("en__VALLEY@turkey=gobblegobble");
1791 Locale
locale1("de_DE_FOO");
1792 Locale
locale2("ja_JP");
1793 UnicodeString name
= formatter0
.getRuleSetName(0);
1794 if ( formatter0
.getRuleSetDisplayName(0, locale0
) == "Main"
1795 && formatter0
.getRuleSetDisplayName(0, locale1
) == "das Main"
1796 && formatter0
.getRuleSetDisplayName(0, locale2
) == "%main"
1797 && formatter0
.getRuleSetDisplayName(name
, locale0
) == "Main"
1798 && formatter0
.getRuleSetDisplayName(name
, locale1
) == "das Main"
1799 && formatter0
.getRuleSetDisplayName(name
, locale2
) == "%main"){
1800 logln("getRuleSetDisplayName tested");
1802 errln("failed to getRuleSetDisplayName");
1806 for (i
= 0; i
< formatter0
.getNumberOfRuleSetDisplayNameLocales(); ++i
) {
1807 Locale locale
= formatter0
.getRuleSetDisplayNameLocale(i
, status
);
1808 if (U_SUCCESS(status
)) {
1809 for (int j
= 0; j
< formatter0
.getNumberOfRuleSetNames(); ++j
) {
1810 UnicodeString name
= formatter0
.getRuleSetName(j
);
1811 UnicodeString lname
= formatter0
.getRuleSetDisplayName(j
, locale
);
1812 UnicodeString msg
= locale
.getName();
1825 static const char* goodLocs
[] = {
1826 "", // zero-length ok, same as providing no localization data
1827 "<<>>", // no public rule sets ok
1828 "<<%main>>", // no localizations ok
1829 "<<%main,>,<en, Main,>>", // comma before close angle ok
1830 "<<%main>,<en, ',<>\" '>>", // quotes everything until next quote
1831 "<<%main>,<'en', \"it's ok\">>", // double quotes work too
1832 " \n <\n <\n %main\n >\n , \t <\t en\t , \tfoo \t\t > \n\n > \n ", // Pattern_White_Space ok
1834 int32_t goodLocsLen
= sizeof(goodLocs
)/sizeof(goodLocs
[0]);
1836 static const char* badLocs
[] = {
1837 " ", // non-zero length
1838 "<>", // empty array
1839 "<", // unclosed outer array
1840 "<<", // unclosed inner array
1841 "<<,>>", // unexpected comma
1842 "<<''>>", // empty string
1843 " x<<%main>>", // first non space char not open angle bracket
1844 "<%main>", // missing inner array
1845 "<<%main %other>>", // elements missing separating commma (spaces must be quoted)
1846 "<<%main><en, Main>>", // arrays missing separating comma
1847 "<<%main>,<en, main, foo>>", // too many elements in locale data
1848 "<<%main>,<en>>", // too few elements in locale data
1849 "<<<%main>>>", // unexpected open angle
1850 "<<%main<>>>", // unexpected open angle
1851 "<<%main, %other>,<en,,>>", // implicit empty strings
1852 "<<%main>,<en,''>>", // empty string
1853 "<<%main>, < en, '>>", // unterminated quote
1854 "<<%main>, < en, \"<>>", // unterminated quote
1855 "<<%main\">>", // quote in string
1856 "<<%main'>>", // quote in string
1857 "<<%main<>>", // open angle in string
1858 "<<%main>> x", // extra non-space text at end
1861 int32_t badLocsLen
= sizeof(badLocs
)/sizeof(badLocs
[0]);
1863 for (i
= 0; i
< goodLocsLen
; ++i
) {
1864 logln("[%d] '%s'", i
, goodLocs
[i
]);
1865 UErrorCode status
= U_ZERO_ERROR
;
1866 UnicodeString
loc(goodLocs
[i
]);
1867 RuleBasedNumberFormat
fmt(rules
, loc
, perror
, status
);
1868 if (U_FAILURE(status
)) {
1869 errln("Failed parse of good localization string: '%s'", goodLocs
[i
]);
1873 for (i
= 0; i
< badLocsLen
; ++i
) {
1874 logln("[%d] '%s'", i
, badLocs
[i
]);
1875 UErrorCode status
= U_ZERO_ERROR
;
1876 UnicodeString
loc(badLocs
[i
]);
1877 RuleBasedNumberFormat
fmt(rules
, loc
, perror
, status
);
1878 if (U_SUCCESS(status
)) {
1879 errln("Successful parse of bad localization string: '%s'", badLocs
[i
]);
1887 IntlTestRBNF::TestAllLocales()
1889 const char* names
[] = {
1892 // " (duration) " // This is English only, and it's not really supported in CLDR anymore.
1894 double numbers
[] = {45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111};
1897 const Locale
* locales
= Locale::getAvailableLocales(count
);
1898 for (int i
= 0; i
< count
; ++i
) {
1899 const Locale
* loc
= &locales
[i
];
1901 for (int j
= 0; j
< 2; ++j
) {
1902 UErrorCode status
= U_ZERO_ERROR
;
1903 RuleBasedNumberFormat
* f
= new RuleBasedNumberFormat((URBNFRuleSetTag
)j
, *loc
, status
);
1905 if (status
== U_USING_DEFAULT_WARNING
|| status
== U_USING_FALLBACK_WARNING
) {
1910 if (U_FAILURE(status
)) {
1911 errln(UnicodeString(loc
->getName()) + names
[j
]
1912 + "ERROR could not instantiate -> " + u_errorName(status
));
1915 #if !UCONFIG_NO_COLLATION
1916 for (unsigned int numidx
= 0; numidx
< sizeof(numbers
)/sizeof(double); numidx
++) {
1917 double n
= numbers
[numidx
];
1922 logln(UnicodeString(loc
->getName()) + names
[j
]
1923 + "success: " + n
+ " -> " + str
);
1926 // We do not validate the result in this test case,
1927 // because there are cases which do not round trip by design.
1931 status
= U_ZERO_ERROR
;
1932 f
->setLenient(FALSE
);
1933 f
->parse(str
, num
, status
);
1934 if (U_FAILURE(status
)) {
1935 errln(UnicodeString(loc
->getName()) + names
[j
]
1936 + "ERROR could not parse '" + str
+ "' -> " + u_errorName(status
));
1938 // We only check the spellout. The behavior is undefined for numbers < 1 and fractional numbers.
1940 if (num
.getType() == Formattable::kLong
&& num
.getLong() != n
) {
1941 errln(UnicodeString(loc
->getName()) + names
[j
]
1942 + UnicodeString("ERROR could not roundtrip ") + n
1943 + UnicodeString(" -> ") + str
+ UnicodeString(" -> ") + num
.getLong());
1945 else if (num
.getType() == Formattable::kDouble
&& (int64_t)(num
.getDouble() * 1000) != (int64_t)(n
*1000)) {
1946 // The epsilon difference is too high.
1947 errln(UnicodeString(loc
->getName()) + names
[j
]
1948 + UnicodeString("ERROR could not roundtrip ") + n
1949 + UnicodeString(" -> ") + str
+ UnicodeString(" -> ") + num
.getDouble());
1952 if (!quick
&& !logKnownIssue("9503") ) {
1954 status
= U_ZERO_ERROR
;
1955 f
->setLenient(TRUE
);
1956 f
->parse(str
, num
, status
);
1957 if (U_FAILURE(status
)) {
1958 errln(UnicodeString(loc
->getName()) + names
[j
]
1959 + "ERROR could not parse(lenient) '" + str
+ "' -> " + u_errorName(status
));
1961 // We only check the spellout. The behavior is undefined for numbers < 1 and fractional numbers.
1963 if (num
.getType() == Formattable::kLong
&& num
.getLong() != n
) {
1964 errln(UnicodeString(loc
->getName()) + names
[j
]
1965 + UnicodeString("ERROR could not roundtrip ") + n
1966 + UnicodeString(" -> ") + str
+ UnicodeString(" -> ") + num
.getLong());
1968 else if (num
.getType() == Formattable::kDouble
&& (int64_t)(num
.getDouble() * 1000) != (int64_t)(n
*1000)) {
1969 // The epsilon difference is too high.
1970 errln(UnicodeString(loc
->getName()) + names
[j
]
1971 + UnicodeString("ERROR could not roundtrip ") + n
1972 + UnicodeString(" -> ") + str
+ UnicodeString(" -> ") + num
.getDouble());
1984 IntlTestRBNF::TestMultiplierSubstitution(void) {
1985 UnicodeString
rules("=#,##0=;1,000,000: <##0.###< million;");
1986 UErrorCode status
= U_ZERO_ERROR
;
1987 UParseError parse_error
;
1988 RuleBasedNumberFormat
*rbnf
=
1989 new RuleBasedNumberFormat(rules
, Locale::getUS(), parse_error
, status
);
1990 if (U_SUCCESS(status
)) {
1993 double n
= 1234000.0;
1994 rbnf
->format(n
, res
, pos
);
1997 UnicodeString
expected(UNICODE_STRING_SIMPLE("1.234 million"));
1998 if (expected
!= res
) {
1999 UnicodeString msg
= "Expected: ";
2000 msg
.append(expected
);
2001 msg
.append(" but got ");
2009 IntlTestRBNF::TestSetDecimalFormatSymbols() {
2010 UErrorCode status
= U_ZERO_ERROR
;
2012 RuleBasedNumberFormat
rbnf(URBNF_ORDINAL
, Locale::getEnglish(), status
);
2013 if (U_FAILURE(status
)) {
2014 dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status
)));
2018 DecimalFormatSymbols
dfs(Locale::getEnglish(), status
);
2019 if (U_FAILURE(status
)) {
2020 errln("Unable to create DecimalFormatSymbols - " + UnicodeString(u_errorName(status
)));
2024 UnicodeString expected
[] = {
2025 UnicodeString("1,001st"),
2026 UnicodeString("1&001st")
2029 double number
= 1001;
2031 UnicodeString result
;
2033 rbnf
.format(number
, result
);
2034 if (result
!= expected
[0]) {
2035 errln("Format Error - Got: " + result
+ " Expected: " + expected
[0]);
2040 /* Set new symbol for testing */
2041 dfs
.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol
, UnicodeString("&"), TRUE
);
2042 rbnf
.setDecimalFormatSymbols(dfs
);
2044 rbnf
.format(number
, result
);
2045 if (result
!= expected
[1]) {
2046 errln("Format Error - Got: " + result
+ " Expected: " + expected
[1]);
2050 void IntlTestRBNF::TestPluralRules() {
2051 UErrorCode status
= U_ZERO_ERROR
;
2052 UnicodeString
enRules("%digits-ordinal:-x: ->>;0: =#,##0=$(ordinal,one{st}two{nd}few{rd}other{th})$;");
2053 UParseError parseError
;
2054 RuleBasedNumberFormat
enFormatter(enRules
, Locale::getEnglish(), parseError
, status
);
2055 if (U_FAILURE(status
)) {
2056 dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status
)));
2059 const char* const enTestData
[][2] = {
2075 doTest(&enFormatter
, enTestData
, TRUE
);
2077 // This is trying to model the feminine form, but don't worry about the details too much.
2078 // We're trying to test the plural rules.
2079 UnicodeString
ruRules("%spellout-numbering:"
2110 "100: hundred[ >>];"
2111 "200: << hundred[ >>];"
2112 "300: << hundreds[ >>];"
2113 "500: << hundredss[ >>];"
2114 "1000: << $(cardinal,one{thousand}few{thousands}other{thousandss})$[ >>];"
2115 "1000000: << $(cardinal,one{million}few{millions}other{millionss})$[ >>];");
2116 RuleBasedNumberFormat
ruFormatter(ruRules
, Locale("ru"), parseError
, status
);
2117 const char* const ruTestData
[][2] = {
2119 { "100", "hundred" },
2120 { "125", "hundred twenty-five" },
2121 { "399", "three hundreds ninety-nine" },
2122 { "1,000", "one thousand" },
2123 { "1,001", "one thousand one" },
2124 { "2,000", "two thousands" },
2125 { "2,001", "two thousands one" },
2126 { "2,002", "two thousands two" },
2127 { "3,333", "three thousands three hundreds thirty-three" },
2128 { "5,000", "five thousandss" },
2129 { "11,000", "eleven thousandss" },
2130 { "21,000", "twenty-one thousand" },
2131 { "22,000", "twenty-two thousands" },
2132 { "25,001", "twenty-five thousandss one" },
2136 if (U_FAILURE(status
)) {
2137 errln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status
)));
2140 doTest(&ruFormatter
, ruTestData
, TRUE
);
2142 // Make sure there are no divide by 0 errors.
2143 UnicodeString result
;
2144 RuleBasedNumberFormat(ruRules
, Locale("ru"), parseError
, status
).format(21000, result
);
2145 if (result
.compare(UNICODE_STRING_SIMPLE("twenty-one thousand")) != 0) {
2146 errln("Got " + result
+ " for 21000");
2152 IntlTestRBNF::doTest(RuleBasedNumberFormat
* formatter
, const char* const testData
[][2], UBool testParsing
)
2154 // man, error reporting would be easier with printf-style syntax for unicode string and formattable
2156 UErrorCode status
= U_ZERO_ERROR
;
2157 DecimalFormatSymbols
dfs("en", status
);
2158 // NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
2159 DecimalFormat
decFmt("#,###.################", dfs
, status
);
2160 if (U_FAILURE(status
)) {
2161 errcheckln(status
, "FAIL: could not create NumberFormat - %s", u_errorName(status
));
2163 for (int i
= 0; testData
[i
][0]; ++i
) {
2164 const char* numString
= testData
[i
][0];
2165 const char* expectedWords
= testData
[i
][1];
2167 log("[%i] %s = ", i
, numString
);
2168 Formattable expectedNumber
;
2169 decFmt
.parse(numString
, expectedNumber
, status
);
2170 if (U_FAILURE(status
)) {
2171 errln("FAIL: decFmt could not parse %s", numString
);
2174 UnicodeString actualString
;
2176 formatter
->format(expectedNumber
, actualString
/* , pos*/, status
);
2177 if (U_FAILURE(status
)) {
2178 UnicodeString msg
= "Fail: formatter could not format ";
2179 decFmt
.format(expectedNumber
, msg
, status
);
2183 UnicodeString expectedString
= UnicodeString(expectedWords
, -1, US_INV
).unescape();
2184 if (actualString
!= expectedString
) {
2185 UnicodeString msg
= "FAIL: check failed for ";
2186 decFmt
.format(expectedNumber
, msg
, status
);
2187 msg
.append(", expected ");
2188 msg
.append(expectedString
);
2189 msg
.append(" but got ");
2190 msg
.append(actualString
);
2194 logln(actualString
);
2196 Formattable parsedNumber
;
2197 formatter
->parse(actualString
, parsedNumber
, status
);
2198 if (U_FAILURE(status
)) {
2199 UnicodeString msg
= "FAIL: formatter could not parse ";
2200 msg
.append(actualString
);
2201 msg
.append(" status code: " );
2202 msg
.append(u_errorName(status
));
2206 if (parsedNumber
!= expectedNumber
) {
2207 UnicodeString msg
= "FAIL: parse failed for ";
2208 msg
.append(actualString
);
2209 msg
.append(", expected ");
2210 decFmt
.format(expectedNumber
, msg
, status
);
2211 msg
.append(", but got ");
2212 decFmt
.format(parsedNumber
, msg
, status
);
2226 IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat
* formatter
, const char* testData
[][2])
2228 UErrorCode status
= U_ZERO_ERROR
;
2229 NumberFormat
* decFmt
= NumberFormat::createInstance(Locale::getUS(), status
);
2230 if (U_FAILURE(status
)) {
2231 errcheckln(status
, "FAIL: could not create NumberFormat - %s", u_errorName(status
));
2233 for (int i
= 0; testData
[i
][0]; ++i
) {
2234 const char* spelledNumber
= testData
[i
][0]; // spelled-out number
2235 const char* asciiUSNumber
= testData
[i
][1]; // number as ascii digits formatted for US locale
2237 UnicodeString spelledNumberString
= UnicodeString(spelledNumber
).unescape();
2238 Formattable actualNumber
;
2239 formatter
->parse(spelledNumberString
, actualNumber
, status
);
2240 if (U_FAILURE(status
)) {
2241 UnicodeString msg
= "FAIL: formatter could not parse ";
2242 msg
.append(spelledNumberString
);
2246 // I changed the logic of this test somewhat from Java-- instead of comparing the
2247 // strings, I compare the Formattables. Hmmm, but the Formattables don't compare,
2248 // so change it back.
2250 UnicodeString asciiUSNumberString
= asciiUSNumber
;
2251 Formattable expectedNumber
;
2252 decFmt
->parse(asciiUSNumberString
, expectedNumber
, status
);
2253 if (U_FAILURE(status
)) {
2254 UnicodeString msg
= "FAIL: decFmt could not parse ";
2255 msg
.append(asciiUSNumberString
);
2259 UnicodeString actualNumberString
;
2260 UnicodeString expectedNumberString
;
2261 decFmt
->format(actualNumber
, actualNumberString
, status
);
2262 decFmt
->format(expectedNumber
, expectedNumberString
, status
);
2263 if (actualNumberString
!= expectedNumberString
) {
2264 UnicodeString msg
= "FAIL: parsing";
2265 msg
.append(asciiUSNumberString
);
2267 msg
.append(" lenient parse failed for ");
2268 msg
.append(spelledNumberString
);
2269 msg
.append(", expected ");
2270 msg
.append(expectedNumberString
);
2271 msg
.append(", but got ");
2272 msg
.append(actualNumberString
);
2287 IntlTestRBNF::TestRBNFDisabled() {
2288 errln("*** RBNF currently disabled on this platform ***\n");
2294 #endif /* #if !UCONFIG_NO_FORMATTING */