1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2015, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 
8 #include "unicode/utypes.h"
9 
10 #if !UCONFIG_NO_FORMATTING
11 
12 #include "itrbnf.h"
13 
14 #include "unicode/umachine.h"
15 
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"
22 #include "putilimp.h"
23 #include "testutil.h"
24 
25 #include <string.h>
26 
27 // import com.ibm.text.RuleBasedNumberFormat;
28 // import com.ibm.test.TestFmwk;
29 
30 // import java.util.Locale;
31 // import java.text.NumberFormat;
32 
33 // current macro not in icu1.8.1
34 #define TESTCASE(id,test)             \
35     case id:                          \
36         name = #test;                 \
37         if (exec) {                   \
38             logln(#test "---");       \
39             logln();                  \
40             test();                   \
41         }                             \
42         break
43 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)44 void IntlTestRBNF::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/)
45 {
46     if (exec) logln("TestSuite RuleBasedNumberFormat");
47     switch (index) {
48 #if U_HAVE_RBNF
49         TESTCASE(0, TestEnglishSpellout);
50         TESTCASE(1, TestOrdinalAbbreviations);
51         TESTCASE(2, TestDurations);
52         TESTCASE(3, TestSpanishSpellout);
53         TESTCASE(4, TestFrenchSpellout);
54         TESTCASE(5, TestSwissFrenchSpellout);
55         TESTCASE(6, TestItalianSpellout);
56         TESTCASE(7, TestGermanSpellout);
57         TESTCASE(8, TestThaiSpellout);
58         TESTCASE(9, TestAPI);
59         TESTCASE(10, TestFractionalRuleSet);
60         TESTCASE(11, TestSwedishSpellout);
61         TESTCASE(12, TestBelgianFrenchSpellout);
62         TESTCASE(13, TestSmallValues);
63         TESTCASE(14, TestLocalizations);
64         TESTCASE(15, TestAllLocales);
65         TESTCASE(16, TestHebrewFraction);
66         TESTCASE(17, TestPortugueseSpellout);
67         TESTCASE(18, TestMultiplierSubstitution);
68         TESTCASE(19, TestSetDecimalFormatSymbols);
69         TESTCASE(20, TestPluralRules);
70         TESTCASE(21, TestMultiplePluralRules);
71         TESTCASE(22, TestInfinityNaN);
72         TESTCASE(23, TestVariableDecimalPoint);
73 #else
74         TESTCASE(0, TestRBNFDisabled);
75 #endif
76     default:
77         name = "";
78         break;
79     }
80 }
81 
82 #if U_HAVE_RBNF
83 
TestHebrewFraction()84 void IntlTestRBNF::TestHebrewFraction() {
85 
86     // this is the expected output for 123.45, with no '<' in it.
87     UChar text1[] = {
88         0x05de, 0x05d0, 0x05d4, 0x0020,
89         0x05e2, 0x05e9, 0x05e8, 0x05d9, 0x05dd, 0x0020,
90         0x05d5, 0x05e9, 0x05dc, 0x05d5, 0x05e9, 0x0020,
91         0x05e0, 0x05e7, 0x05d5, 0x05d3, 0x05d4, 0x0020,
92         0x05d0, 0x05e8, 0x05d1, 0x05e2, 0x0020,
93         0x05d7, 0x05de, 0x05e9, 0x0000,
94     };
95     UChar text2[] = {
96         0x05DE, 0x05D0, 0x05D4, 0x0020,
97         0x05E2, 0x05E9, 0x05E8, 0x05D9, 0x05DD, 0x0020,
98         0x05D5, 0x05E9, 0x05DC, 0x05D5, 0x05E9, 0x0020,
99         0x05E0, 0x05E7, 0x05D5, 0x05D3, 0x05D4, 0x0020,
100         0x05D0, 0x05E4, 0x05E1, 0x0020,
101         0x05D0, 0x05E4, 0x05E1, 0x0020,
102         0x05D0, 0x05E8, 0x05D1, 0x05E2, 0x0020,
103         0x05D7, 0x05DE, 0x05E9, 0x0000,
104     };
105     UErrorCode status = U_ZERO_ERROR;
106     RuleBasedNumberFormat* formatter = new RuleBasedNumberFormat(URBNF_SPELLOUT, "he_IL", status);
107     if (status == U_MISSING_RESOURCE_ERROR || status == U_FILE_ACCESS_ERROR) {
108         errcheckln(status, "Failed in constructing RuleBasedNumberFormat - %s", u_errorName(status));
109         delete formatter;
110         return;
111     }
112     UnicodeString result;
113     Formattable parseResult;
114     ParsePosition pp(0);
115     {
116         UnicodeString expected(text1);
117         formatter->format(123.45, result);
118         if (result != expected) {
119             errln((UnicodeString)"expected '" + TestUtility::hex(expected) + "'\nbut got: '" + TestUtility::hex(result) + "'");
120         } else {
121 //            formatter->parse(result, parseResult, pp);
122 //            if (parseResult.getDouble() != 123.45) {
123 //                errln("expected 123.45 but got: %g", parseResult.getDouble());
124 //            }
125         }
126     }
127     {
128         UnicodeString expected(text2);
129         result.remove();
130         formatter->format(123.0045, result);
131         if (result != expected) {
132             errln((UnicodeString)"expected '" + TestUtility::hex(expected) + "'\nbut got: '" + TestUtility::hex(result) + "'");
133         } else {
134             pp.setIndex(0);
135 //            formatter->parse(result, parseResult, pp);
136 //            if (parseResult.getDouble() != 123.0045) {
137 //                errln("expected 123.0045 but got: %g", parseResult.getDouble());
138 //            }
139         }
140     }
141     delete formatter;
142 }
143 
144 void
TestAPI()145 IntlTestRBNF::TestAPI() {
146   // This test goes through the APIs that were not tested before.
147   // These tests are too small to have separate test classes/functions
148 
149   UErrorCode status = U_ZERO_ERROR;
150   RuleBasedNumberFormat* formatter
151       = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status);
152   if (status == U_MISSING_RESOURCE_ERROR || status == U_FILE_ACCESS_ERROR) {
153     dataerrln("Unable to create formatter. - %s", u_errorName(status));
154     delete formatter;
155     return;
156   }
157 
158   logln("RBNF API test starting");
159   // test clone
160   {
161     logln("Testing Clone");
162     RuleBasedNumberFormat* rbnfClone = (RuleBasedNumberFormat *)formatter->clone();
163     if(rbnfClone != NULL) {
164       if(!(*rbnfClone == *formatter)) {
165         errln("Clone should be semantically equivalent to the original!");
166       }
167       delete rbnfClone;
168     } else {
169       errln("Cloning failed!");
170     }
171   }
172 
173   // test assignment
174   {
175     logln("Testing assignment operator");
176     RuleBasedNumberFormat assignResult(URBNF_SPELLOUT, Locale("es", "ES", ""), status);
177     assignResult = *formatter;
178     if(!(assignResult == *formatter)) {
179       errln("Assignment result should be semantically equivalent to the original!");
180     }
181   }
182 
183   // test rule constructor
184   {
185     logln("Testing rule constructor");
186     LocalUResourceBundlePointer en(ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "rbnf", "en", &status));
187     if(U_FAILURE(status)) {
188       errln("Unable to access resource bundle with data!");
189     } else {
190       int32_t ruleLen = 0;
191       int32_t len = 0;
192       LocalUResourceBundlePointer rbnfRules(ures_getByKey(en.getAlias(), "RBNFRules", NULL, &status));
193       LocalUResourceBundlePointer ruleSets(ures_getByKey(rbnfRules.getAlias(), "SpelloutRules", NULL, &status));
194       UnicodeString desc;
195       while (ures_hasNext(ruleSets.getAlias())) {
196            const UChar* currentString = ures_getNextString(ruleSets.getAlias(), &len, NULL, &status);
197            ruleLen += len;
198            desc.append(currentString);
199       }
200 
201       const UChar *spelloutRules = desc.getTerminatedBuffer();
202 
203       if(U_FAILURE(status) || ruleLen == 0 || spelloutRules == NULL) {
204         errln("Unable to access the rules string!");
205       } else {
206         UParseError perror;
207         RuleBasedNumberFormat ruleCtorResult(spelloutRules, Locale::getUS(), perror, status);
208         if(!(ruleCtorResult == *formatter)) {
209           errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
210         }
211 
212         // Jitterbug 4452, for coverage
213         RuleBasedNumberFormat nf(spelloutRules, (UnicodeString)"", Locale::getUS(), perror, status);
214         if(!(nf == *formatter)) {
215           errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
216         }
217       }
218     }
219   }
220 
221   // test getRules
222   {
223     logln("Testing getRules function");
224     UnicodeString rules = formatter->getRules();
225     UParseError perror;
226     RuleBasedNumberFormat fromRulesResult(rules, Locale::getUS(), perror, status);
227 
228     if(!(fromRulesResult == *formatter)) {
229       errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!");
230     }
231   }
232 
233 
234   {
235     logln("Testing copy constructor");
236     RuleBasedNumberFormat copyCtorResult(*formatter);
237     if(!(copyCtorResult == *formatter)) {
238       errln("Copy constructor result result should be semantically equivalent to the original!");
239     }
240   }
241 
242 #if !UCONFIG_NO_COLLATION
243   // test ruleset names
244   {
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");
249     }
250     UnicodeString ruleSetName;
251     int32_t i = 0;
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 
260       ruleSetName = formatter->getRuleSetName(i);
261       log("Rule set name %i is ", i);
262       log(ruleSetName);
263       logln(". Format results are: ");
264       intFormatResult = formatter->format(intFormatNum, ruleSetName, intFormatResult, pos1, status);
265       doubleFormatResult = formatter->format(doubleFormatNum, ruleSetName, doubleFormatResult, pos2, status);
266       if(U_FAILURE(status)) {
267         errln("Format using a rule set failed");
268         break;
269       }
270       logln(intFormatResult);
271       logln(doubleFormatResult);
272       formatter->setLenient(TRUE);
273       formatter->parse(intFormatResult, intParseResult, status);
274       formatter->parse(doubleFormatResult, doubleParseResult, status);
275 
276       logln("Parse results for lenient = TRUE, %i, %f", intParseResult.getLong(), doubleParseResult.getDouble());
277 
278       formatter->setLenient(FALSE);
279       formatter->parse(intFormatResult, intParseResult, status);
280       formatter->parse(doubleFormatResult, doubleParseResult, status);
281 
282       logln("Parse results for lenient = FALSE, %i, %f", intParseResult.getLong(), doubleParseResult.getDouble());
283 
284       if(U_FAILURE(status)) {
285         errln("Error during parsing");
286       }
287 
288       intFormatResult = formatter->format(intFormatNum, "BLABLA", intFormatResult, pos1, status);
289       if(U_SUCCESS(status)) {
290         errln("Using invalid rule set name should have failed");
291         break;
292       }
293       status = U_ZERO_ERROR;
294       doubleFormatResult = formatter->format(doubleFormatNum, "TRUC", doubleFormatResult, pos2, status);
295       if(U_SUCCESS(status)) {
296         errln("Using invalid rule set name should have failed");
297         break;
298       }
299       status = U_ZERO_ERROR;
300     }
301     status = U_ZERO_ERROR;
302   }
303 #endif
304 
305   // test API
306   UnicodeString expected("four point five","");
307   logln("Testing format(double)");
308   UnicodeString result;
309   formatter->format(4.5,result);
310   if(result != expected) {
311       errln("Formatted 4.5, expected " + expected + " got " + result);
312   } else {
313       logln("Formatted 4.5, expected " + expected + " got " + result);
314   }
315   result.remove();
316   expected = "four";
317   formatter->format((int32_t)4,result);
318   if(result != expected) {
319       errln("Formatted 4, expected " + expected + " got " + result);
320   } else {
321       logln("Formatted 4, expected " + expected + " got " + result);
322   }
323 
324   result.remove();
325   FieldPosition pos;
326   formatter->format((int64_t)4, result, pos, status = U_ZERO_ERROR);
327   if(result != expected) {
328       errln("Formatted 4 int64_t, expected " + expected + " got " + result);
329   } else {
330       logln("Formatted 4 int64_t, expected " + expected + " got " + result);
331   }
332 
333   //Jitterbug 4452, for coverage
334   result.remove();
335   FieldPosition pos2;
336   formatter->format((int64_t)4, formatter->getRuleSetName(0), result, pos2, status = U_ZERO_ERROR);
337   if(result != expected) {
338       errln("Formatted 4 int64_t, expected " + expected + " got " + result);
339   } else {
340       logln("Formatted 4 int64_t, expected " + expected + " got " + result);
341   }
342 
343   // clean up
344   logln("Cleaning up");
345   delete formatter;
346 }
347 
348 /**
349  * Perform a simple spot check on the parsing going into an infinite loop for alternate rules.
350  */
TestMultiplePluralRules()351 void IntlTestRBNF::TestMultiplePluralRules() {
352     // This is trying to model the feminine form, but don't worry about the details too much.
353     // We're trying to test the plural rules where there are different prefixes.
354     UnicodeString rules("%spellout-cardinal-feminine-genitive:"
355                 "0: zero;"
356                 "1: ono;"
357                 "2: two;"
358                 "1000: << $(cardinal,one{thousand}few{thousanF}other{thousanO})$[ >>];"
359                 "%spellout-cardinal-feminine:"
360                 "x.x: [<< $(cardinal,one{singleton}other{plurality})$ ]>%%fractions>;"
361                 "0: zero;"
362                 "1: one;"
363                 "2: two;"
364                 "1000: << $(cardinal,one{thousand}few{thousanF}other{thousanO})$[ >>];"
365                 "%%fractions:"
366                 "10: <%spellout-cardinal-feminine< $(cardinal,one{oneth}other{tenth})$;"
367                 "100: <%spellout-cardinal-feminine< $(cardinal,one{1hundredth}other{hundredth})$;");
368     UErrorCode status = U_ZERO_ERROR;
369     UParseError pError;
370     RuleBasedNumberFormat formatter(rules, Locale("ru"), pError, status);
371     Formattable result;
372     UnicodeString resultStr;
373     FieldPosition pos;
374 
375     if (U_FAILURE(status)) {
376         dataerrln("Unable to create formatter - %s", u_errorName(status));
377         return;
378     }
379 
380     formatter.parse(formatter.format(1000.0, resultStr, pos, status), result, status);
381     if (1000 != result.getLong() || resultStr != UNICODE_STRING_SIMPLE("one thousand")) {
382         errln("RuleBasedNumberFormat did not return the correct value. Got: %d", result.getLong());
383         errln(resultStr);
384     }
385     resultStr.remove();
386     formatter.parse(formatter.format(1000.0, UnicodeString("%spellout-cardinal-feminine-genitive"), resultStr, pos, status), result, status);
387     if (1000 != result.getLong() || resultStr != UNICODE_STRING_SIMPLE("ono thousand")) {
388         errln("RuleBasedNumberFormat(cardinal-feminine-genitive) did not return the correct value. Got: %d", result.getLong());
389         errln(resultStr);
390     }
391     resultStr.remove();
392     formatter.parse(formatter.format(1000.0, UnicodeString("%spellout-cardinal-feminine"), resultStr, pos, status), result, status);
393     if (1000 != result.getLong() || resultStr != UNICODE_STRING_SIMPLE("one thousand")) {
394         errln("RuleBasedNumberFormat(spellout-cardinal-feminine) did not return the correct value. Got: %d", result.getLong());
395         errln(resultStr);
396     }
397     static const char* const testData[][2] = {
398         { "0", "zero" },
399         { "1", "one" },
400         { "2", "two" },
401         { "0.1", "one oneth" },
402         { "0.2", "two tenth" },
403         { "1.1", "one singleton one oneth" },
404         { "1.2", "one singleton two tenth" },
405         { "2.1", "two plurality one oneth" },
406         { "2.2", "two plurality two tenth" },
407         { "0.01", "one 1hundredth" },
408         { "0.02", "two hundredth" },
409         { NULL, NULL }
410     };
411     doTest(&formatter, testData, TRUE);
412 }
413 
TestFractionalRuleSet()414 void IntlTestRBNF::TestFractionalRuleSet()
415 {
416     UnicodeString fracRules(
417         "%main:\n"
418                // this rule formats the number if it's 1 or more.  It formats
419                // the integral part using a DecimalFormat ("#,##0" puts
420                // thousands separators in the right places) and the fractional
421                // part using %%frac.  If there is no fractional part, it
422                // just shows the integral part.
423         "    x.0: <#,##0<[ >%%frac>];\n"
424                // this rule formats the number if it's between 0 and 1.  It
425                // shows only the fractional part (0.5 shows up as "1/2," not
426                // "0 1/2")
427         "    0.x: >%%frac>;\n"
428         // the fraction rule set.  This works the same way as the one in the
429         // preceding example: We multiply the fractional part of the number
430         // being formatted by each rule's base value and use the rule that
431         // produces the result closest to 0 (or the first rule that produces 0).
432         // Since we only provide rules for the numbers from 2 to 10, we know
433         // we'll get a fraction with a denominator between 2 and 10.
434         // "<0<" causes the numerator of the fraction to be formatted
435         // using numerals
436         "%%frac:\n"
437         "    2: 1/2;\n"
438         "    3: <0</3;\n"
439         "    4: <0</4;\n"
440         "    5: <0</5;\n"
441         "    6: <0</6;\n"
442         "    7: <0</7;\n"
443         "    8: <0</8;\n"
444         "    9: <0</9;\n"
445         "   10: <0</10;\n");
446 
447     // mondo hack
448     int len = fracRules.length();
449     int change = 2;
450     for (int i = 0; i < len; ++i) {
451         UChar ch = fracRules.charAt(i);
452         if (ch == '\n') {
453             change = 2; // change ok
454         } else if (ch == ':') {
455             change = 1; // change, but once we hit a non-space char, don't change
456         } else if (ch == ' ') {
457             if (change != 0) {
458                 fracRules.setCharAt(i, (UChar)0x200e);
459             }
460         } else {
461             if (change == 1) {
462                 change = 0;
463             }
464         }
465     }
466 
467     UErrorCode status = U_ZERO_ERROR;
468     UParseError perror;
469     RuleBasedNumberFormat formatter(fracRules, Locale::getEnglish(), perror, status);
470     if (U_FAILURE(status)) {
471         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
472     } else {
473         static const char* const testData[][2] = {
474             { "0", "0" },
475             { ".1", "1/10" },
476             { ".11", "1/9" },
477             { ".125", "1/8" },
478             { ".1428", "1/7" },
479             { ".1667", "1/6" },
480             { ".2", "1/5" },
481             { ".25", "1/4" },
482             { ".333", "1/3" },
483             { ".5", "1/2" },
484             { "1.1", "1 1/10" },
485             { "2.11", "2 1/9" },
486             { "3.125", "3 1/8" },
487             { "4.1428", "4 1/7" },
488             { "5.1667", "5 1/6" },
489             { "6.2", "6 1/5" },
490             { "7.25", "7 1/4" },
491             { "8.333", "8 1/3" },
492             { "9.5", "9 1/2" },
493             { ".2222", "2/9" },
494             { ".4444", "4/9" },
495             { ".5555", "5/9" },
496             { "1.2856", "1 2/7" },
497             { NULL, NULL }
498         };
499         doTest(&formatter, testData, FALSE); // exact values aren't parsable from fractions
500     }
501 }
502 
503 #if 0
504 #define LLAssert(a) \
505   if (!(a)) errln("FAIL: " #a)
506 
507 void IntlTestRBNF::TestLLongConstructors()
508 {
509     logln("Testing constructors");
510 
511     // constant (shouldn't really be public)
512     LLAssert(llong(llong::kD32).asDouble() == llong::kD32);
513 
514     // internal constructor (shouldn't really be public)
515     LLAssert(llong(0, 1).asDouble() == 1);
516     LLAssert(llong(1, 0).asDouble() == llong::kD32);
517     LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1);
518 
519     // public empty constructor
520     LLAssert(llong().asDouble() == 0);
521 
522     // public int32_t constructor
523     LLAssert(llong((int32_t)0).asInt() == (int32_t)0);
524     LLAssert(llong((int32_t)1).asInt() == (int32_t)1);
525     LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1);
526     LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff);
527     LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1);
528     LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000);
529 
530     // public int16_t constructor
531     LLAssert(llong((int16_t)0).asInt() == (int16_t)0);
532     LLAssert(llong((int16_t)1).asInt() == (int16_t)1);
533     LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1);
534     LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff);
535     LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff);
536     LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000);
537 
538     // public int8_t constructor
539     LLAssert(llong((int8_t)0).asInt() == (int8_t)0);
540     LLAssert(llong((int8_t)1).asInt() == (int8_t)1);
541     LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1);
542     LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f);
543     LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff);
544     LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80);
545 
546     // public uint16_t constructor
547     LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0);
548     LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1);
549     LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1);
550     LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff);
551     LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff);
552     LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000);
553 
554     // public uint32_t constructor
555     LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0);
556     LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1);
557     LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1);
558     LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff);
559     LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1);
560     LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
561 
562     // public double constructor
563     LLAssert(llong((double)0).asDouble() == (double)0);
564     LLAssert(llong((double)1).asDouble() == (double)1);
565     LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff);
566     LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000);
567     LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001);
568 
569     // can't access uprv_maxmantissa, so fake it
570     double maxmantissa = (llong((int32_t)1) << 40).asDouble();
571     LLAssert(llong(maxmantissa).asDouble() == maxmantissa);
572     LLAssert(llong(-maxmantissa).asDouble() == -maxmantissa);
573 
574     // copy constructor
575     LLAssert(llong(llong(0, 1)).asDouble() == 1);
576     LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32);
577     LLAssert(llong(llong(-1, (uint32_t)-1)).asDouble() == -1);
578 
579     // asInt - test unsigned to signed narrowing conversion
580     LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff);
581     LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000);
582 
583     // asUInt - test signed to unsigned narrowing conversion
584     LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1);
585     LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
586 
587     // asDouble already tested
588 
589 }
590 
591 void IntlTestRBNF::TestLLongSimpleOperators()
592 {
593     logln("Testing simple operators");
594 
595     // operator==
596     LLAssert(llong() == llong(0, 0));
597     LLAssert(llong(1,0) == llong(1, 0));
598     LLAssert(llong(0,1) == llong(0, 1));
599 
600     // operator!=
601     LLAssert(llong(1,0) != llong(1,1));
602     LLAssert(llong(0,1) != llong(1,1));
603     LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff));
604 
605     // unsigned >
606     LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff)));
607 
608     // unsigned <
609     LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1)));
610 
611     // unsigned >=
612     LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff)));
613     LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1)));
614 
615     // unsigned <=
616     LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1)));
617     LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1)));
618 
619     // operator>
620     LLAssert(llong(1, 1) > llong(1, 0));
621     LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff));
622     LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0));
623     LLAssert(llong(1, 0) > llong(0, 0x7fffffff));
624     LLAssert(llong(1, 0) > llong(0, 0xffffffff));
625     LLAssert(llong(0, 0) > llong(0x80000000, 1));
626 
627     // operator<
628     LLAssert(llong(1, 0) < llong(1, 1));
629     LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000));
630     LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1));
631     LLAssert(llong(0, 0x7fffffff) < llong(1, 0));
632     LLAssert(llong(0, 0xffffffff) < llong(1, 0));
633     LLAssert(llong(0x80000000, 1) < llong(0, 0));
634 
635     // operator>=
636     LLAssert(llong(1, 1) >= llong(1, 0));
637     LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff));
638     LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0));
639     LLAssert(llong(1, 0) >= llong(0, 0x7fffffff));
640     LLAssert(llong(1, 0) >= llong(0, 0xffffffff));
641     LLAssert(llong(0, 0) >= llong(0x80000000, 1));
642     LLAssert(llong() >= llong(0, 0));
643     LLAssert(llong(1,0) >= llong(1, 0));
644     LLAssert(llong(0,1) >= llong(0, 1));
645 
646     // operator<=
647     LLAssert(llong(1, 0) <= llong(1, 1));
648     LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000));
649     LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1));
650     LLAssert(llong(0, 0x7fffffff) <= llong(1, 0));
651     LLAssert(llong(0, 0xffffffff) <= llong(1, 0));
652     LLAssert(llong(0x80000000, 1) <= llong(0, 0));
653     LLAssert(llong() <= llong(0, 0));
654     LLAssert(llong(1,0) <= llong(1, 0));
655     LLAssert(llong(0,1) <= llong(0, 1));
656 
657     // operator==(int32)
658     LLAssert(llong() == (int32_t)0);
659     LLAssert(llong(0,1) == (int32_t)1);
660 
661     // operator!=(int32)
662     LLAssert(llong(1,0) != (int32_t)0);
663     LLAssert(llong(0,1) != (int32_t)2);
664     LLAssert(llong(0,0xffffffff) != (int32_t)-1);
665 
666     llong negOne(0xffffffff, 0xffffffff);
667 
668     // operator>(int32)
669     LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff);
670     LLAssert(negOne > (int32_t)-2);
671     LLAssert(llong(1, 0) > (int32_t)0x7fffffff);
672     LLAssert(llong(0, 0) > (int32_t)-1);
673 
674     // operator<(int32)
675     LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff);
676     LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1);
677 
678     // operator>=(int32)
679     LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff);
680     LLAssert(negOne >= (int32_t)-2);
681     LLAssert(llong(1, 0) >= (int32_t)0x7fffffff);
682     LLAssert(llong(0, 0) >= (int32_t)-1);
683     LLAssert(llong() >= (int32_t)0);
684     LLAssert(llong(0,1) >= (int32_t)1);
685 
686     // operator<=(int32)
687     LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff);
688     LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1);
689     LLAssert(llong() <= (int32_t)0);
690     LLAssert(llong(0,1) <= (int32_t)1);
691 
692     // operator=
693     LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1);
694 
695     // operator <<=
696     LLAssert((llong(1, 1) <<= 0) ==  llong(1, 1));
697     LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000));
698     LLAssert((llong(1, 1) <<= 32) == llong(1, 0));
699     LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0));
700     LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used
701     LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used
702 
703     // operator <<
704     LLAssert((llong((int32_t)1) << 5).asUInt() == 32);
705 
706     // operator >>= (sign extended)
707     LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc));
708     LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde));
709     LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff));
710     LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000));
711     LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
712     LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used
713 
714     // operator >> sign extended)
715     LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde));
716 
717     // ushr (right shift without sign extension)
718     LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc));
719     LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde));
720     LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1));
721     LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000));
722     LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
723     LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used
724 
725     // operator&(llong)
726     LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
727 
728     // operator|(llong)
729     LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
730 
731     // operator^(llong)
732     LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
733 
734     // operator&(uint32)
735     LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
736 
737     // operator|(uint32)
738     LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
739 
740     // operator^(uint32)
741     LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
742 
743     // operator~
744     LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa));
745 
746     // operator&=(llong)
747     LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
748 
749     // operator|=(llong)
750     LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
751 
752     // operator^=(llong)
753     LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
754 
755     // operator&=(uint32)
756     LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
757 
758     // operator|=(uint32)
759     LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
760 
761     // operator^=(uint32)
762     LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
763 
764     // prefix inc
765     LLAssert(llong(1, 0) == ++llong(0,0xffffffff));
766 
767     // prefix dec
768     LLAssert(llong(0,0xffffffff) == --llong(1, 0));
769 
770     // postfix inc
771     {
772         llong n(0, 0xffffffff);
773         LLAssert(llong(0, 0xffffffff) == n++);
774         LLAssert(llong(1, 0) == n);
775     }
776 
777     // postfix dec
778     {
779         llong n(1, 0);
780         LLAssert(llong(1, 0) == n--);
781         LLAssert(llong(0, 0xffffffff) == n);
782     }
783 
784     // unary minus
785     LLAssert(llong(0, 0) == -llong(0, 0));
786     LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1));
787     LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff));
788     LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1));
789     LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow
790 
791     // operator-=
792     {
793         llong n;
794         LLAssert((n -= llong(0, 1)) == llong(0xffffffff, 0xffffffff));
795         LLAssert(n == llong(0xffffffff, 0xffffffff));
796 
797         n = llong(1, 0);
798         LLAssert((n -= llong(0, 1)) == llong(0, 0xffffffff));
799         LLAssert(n == llong(0, 0xffffffff));
800     }
801 
802     // operator-
803     {
804         llong n;
805         LLAssert((n - llong(0, 1)) == llong(0xffffffff, 0xffffffff));
806         LLAssert(n == llong(0, 0));
807 
808         n = llong(1, 0);
809         LLAssert((n - llong(0, 1)) == llong(0, 0xffffffff));
810         LLAssert(n == llong(1, 0));
811     }
812 
813     // operator+=
814     {
815         llong n(0xffffffff, 0xffffffff);
816         LLAssert((n += llong(0, 1)) == llong(0, 0));
817         LLAssert(n == llong(0, 0));
818 
819         n = llong(0, 0xffffffff);
820         LLAssert((n += llong(0, 1)) == llong(1, 0));
821         LLAssert(n == llong(1, 0));
822     }
823 
824     // operator+
825     {
826         llong n(0xffffffff, 0xffffffff);
827         LLAssert((n + llong(0, 1)) == llong(0, 0));
828         LLAssert(n == llong(0xffffffff, 0xffffffff));
829 
830         n = llong(0, 0xffffffff);
831         LLAssert((n + llong(0, 1)) == llong(1, 0));
832         LLAssert(n == llong(0, 0xffffffff));
833     }
834 
835 }
836 
837 void IntlTestRBNF::TestLLong()
838 {
839     logln("Starting TestLLong");
840 
841     TestLLongConstructors();
842 
843     TestLLongSimpleOperators();
844 
845     logln("Testing operator*=, operator*");
846 
847     // operator*=, operator*
848     // small and large values, positive, &NEGative, zero
849     // also test commutivity
850     {
851         const llong ZERO;
852         const llong ONE(0, 1);
853         const llong NEG_ONE((int32_t)-1);
854         const llong THREE(0, 3);
855         const llong NEG_THREE((int32_t)-3);
856         const llong TWO_TO_16(0, 0x10000);
857         const llong NEG_TWO_TO_16 = -TWO_TO_16;
858         const llong TWO_TO_32(1, 0);
859         const llong NEG_TWO_TO_32 = -TWO_TO_32;
860 
861         const llong NINE(0, 9);
862         const llong NEG_NINE = -NINE;
863 
864         const llong TWO_TO_16X3(0, 0x00030000);
865         const llong NEG_TWO_TO_16X3 = -TWO_TO_16X3;
866 
867         const llong TWO_TO_32X3(3, 0);
868         const llong NEG_TWO_TO_32X3 = -TWO_TO_32X3;
869 
870         const llong TWO_TO_48(0x10000, 0);
871         const llong NEG_TWO_TO_48 = -TWO_TO_48;
872 
873         const int32_t VALUE_WIDTH = 9;
874         const llong* values[VALUE_WIDTH] = {
875             &ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32
876         };
877 
878         const llong* answers[VALUE_WIDTH*VALUE_WIDTH] = {
879             &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO,
880             &ZERO, &ONE,  &NEG_ONE, &THREE, &NEG_THREE,  &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32,
881             &ZERO, &NEG_ONE, &ONE, &NEG_THREE, &THREE, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_32, &TWO_TO_32,
882             &ZERO, &THREE, &NEG_THREE, &NINE, &NEG_NINE, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32X3, &NEG_TWO_TO_32X3,
883             &ZERO, &NEG_THREE, &THREE, &NEG_NINE, &NINE, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32X3, &TWO_TO_32X3,
884             &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,
885             &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,
886             &ZERO, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_32X3, &NEG_TWO_TO_32X3, &TWO_TO_48, &NEG_TWO_TO_48, &ZERO, &ZERO,
887             &ZERO, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_32X3, &TWO_TO_32X3, &NEG_TWO_TO_48, &TWO_TO_48, &ZERO, &ZERO
888         };
889 
890         for (int i = 0; i < VALUE_WIDTH; ++i) {
891             for (int j = 0; j < VALUE_WIDTH; ++j) {
892                 llong lhs = *values[i];
893                 llong rhs = *values[j];
894                 llong ans = *answers[i*VALUE_WIDTH + j];
895 
896                 llong n = lhs;
897 
898                 LLAssert((n *= rhs) == ans);
899                 LLAssert(n == ans);
900 
901                 n = lhs;
902                 LLAssert((n * rhs) == ans);
903                 LLAssert(n == lhs);
904             }
905         }
906     }
907 
908     logln("Testing operator/=, operator/");
909     // operator/=, operator/
910     // test num = 0, div = 0, pos/neg, > 2^32, div > num
911     {
912         const llong ZERO;
913         const llong ONE(0, 1);
914         const llong NEG_ONE = -ONE;
915         const llong MAX(0x7fffffff, 0xffffffff);
916         const llong MIN(0x80000000, 0);
917         const llong TWO(0, 2);
918         const llong NEG_TWO = -TWO;
919         const llong FIVE(0, 5);
920         const llong NEG_FIVE = -FIVE;
921         const llong TWO_TO_32(1, 0);
922         const llong NEG_TWO_TO_32 = -TWO_TO_32;
923         const llong TWO_TO_32d5 = llong(TWO_TO_32.asDouble()/5.0);
924         const llong NEG_TWO_TO_32d5 = -TWO_TO_32d5;
925         const llong TWO_TO_32X5 = TWO_TO_32 * FIVE;
926         const llong NEG_TWO_TO_32X5 = -TWO_TO_32X5;
927 
928         const llong* tuples[] = { // lhs, rhs, ans
929             &ZERO, &ZERO, &ZERO,
930             &ONE, &ZERO,&MAX,
931             &NEG_ONE, &ZERO, &MIN,
932             &ONE, &ONE, &ONE,
933             &ONE, &NEG_ONE, &NEG_ONE,
934             &NEG_ONE, &ONE, &NEG_ONE,
935             &NEG_ONE, &NEG_ONE, &ONE,
936             &FIVE, &TWO, &TWO,
937             &FIVE, &NEG_TWO, &NEG_TWO,
938             &NEG_FIVE, &TWO, &NEG_TWO,
939             &NEG_FIVE, &NEG_TWO, &TWO,
940             &TWO, &FIVE, &ZERO,
941             &TWO, &NEG_FIVE, &ZERO,
942             &NEG_TWO, &FIVE, &ZERO,
943             &NEG_TWO, &NEG_FIVE, &ZERO,
944             &TWO_TO_32, &TWO_TO_32, &ONE,
945             &TWO_TO_32, &NEG_TWO_TO_32, &NEG_ONE,
946             &NEG_TWO_TO_32, &TWO_TO_32, &NEG_ONE,
947             &NEG_TWO_TO_32, &NEG_TWO_TO_32, &ONE,
948             &TWO_TO_32, &FIVE, &TWO_TO_32d5,
949             &TWO_TO_32, &NEG_FIVE, &NEG_TWO_TO_32d5,
950             &NEG_TWO_TO_32, &FIVE, &NEG_TWO_TO_32d5,
951             &NEG_TWO_TO_32, &NEG_FIVE, &TWO_TO_32d5,
952             &TWO_TO_32X5, &FIVE, &TWO_TO_32,
953             &TWO_TO_32X5, &NEG_FIVE, &NEG_TWO_TO_32,
954             &NEG_TWO_TO_32X5, &FIVE, &NEG_TWO_TO_32,
955             &NEG_TWO_TO_32X5, &NEG_FIVE, &TWO_TO_32,
956             &TWO_TO_32X5, &TWO_TO_32, &FIVE,
957             &TWO_TO_32X5, &NEG_TWO_TO_32, &NEG_FIVE,
958             &NEG_TWO_TO_32X5, &NEG_TWO_TO_32, &FIVE,
959             &NEG_TWO_TO_32X5, &TWO_TO_32, &NEG_FIVE
960         };
961         const int TUPLE_WIDTH = 3;
962         const int TUPLE_COUNT = (int)(sizeof(tuples)/sizeof(tuples[0]))/TUPLE_WIDTH;
963         for (int i = 0; i < TUPLE_COUNT; ++i) {
964             const llong lhs = *tuples[i*TUPLE_WIDTH+0];
965             const llong rhs = *tuples[i*TUPLE_WIDTH+1];
966             const llong ans = *tuples[i*TUPLE_WIDTH+2];
967 
968             llong n = lhs;
969             if (!((n /= rhs) == ans)) {
970                 errln("fail: (n /= rhs) == ans");
971             }
972             LLAssert(n == ans);
973 
974             n = lhs;
975             LLAssert((n / rhs) == ans);
976             LLAssert(n == lhs);
977         }
978     }
979 
980     logln("Testing operator%%=, operator%%");
981     //operator%=, operator%
982     {
983         const llong ZERO;
984         const llong ONE(0, 1);
985         const llong TWO(0, 2);
986         const llong THREE(0,3);
987         const llong FOUR(0, 4);
988         const llong FIVE(0, 5);
989         const llong SIX(0, 6);
990 
991         const llong NEG_ONE = -ONE;
992         const llong NEG_TWO = -TWO;
993         const llong NEG_THREE = -THREE;
994         const llong NEG_FOUR = -FOUR;
995         const llong NEG_FIVE = -FIVE;
996         const llong NEG_SIX = -SIX;
997 
998         const llong NINETY_NINE(0, 99);
999         const llong HUNDRED(0, 100);
1000         const llong HUNDRED_ONE(0, 101);
1001 
1002         const llong BIG(0x12345678, 0x9abcdef0);
1003         const llong BIG_FIVE(BIG * FIVE);
1004         const llong BIG_FIVEm1 = BIG_FIVE - ONE;
1005         const llong BIG_FIVEp1 = BIG_FIVE + ONE;
1006 
1007         const llong* tuples[] = {
1008             &ZERO, &FIVE, &ZERO,
1009             &ONE, &FIVE, &ONE,
1010             &TWO, &FIVE, &TWO,
1011             &THREE, &FIVE, &THREE,
1012             &FOUR, &FIVE, &FOUR,
1013             &FIVE, &FIVE, &ZERO,
1014             &SIX, &FIVE, &ONE,
1015             &ZERO, &NEG_FIVE, &ZERO,
1016             &ONE, &NEG_FIVE, &ONE,
1017             &TWO, &NEG_FIVE, &TWO,
1018             &THREE, &NEG_FIVE, &THREE,
1019             &FOUR, &NEG_FIVE, &FOUR,
1020             &FIVE, &NEG_FIVE, &ZERO,
1021             &SIX, &NEG_FIVE, &ONE,
1022             &NEG_ONE, &FIVE, &NEG_ONE,
1023             &NEG_TWO, &FIVE, &NEG_TWO,
1024             &NEG_THREE, &FIVE, &NEG_THREE,
1025             &NEG_FOUR, &FIVE, &NEG_FOUR,
1026             &NEG_FIVE, &FIVE, &ZERO,
1027             &NEG_SIX, &FIVE, &NEG_ONE,
1028             &NEG_ONE, &NEG_FIVE, &NEG_ONE,
1029             &NEG_TWO, &NEG_FIVE, &NEG_TWO,
1030             &NEG_THREE, &NEG_FIVE, &NEG_THREE,
1031             &NEG_FOUR, &NEG_FIVE, &NEG_FOUR,
1032             &NEG_FIVE, &NEG_FIVE, &ZERO,
1033             &NEG_SIX, &NEG_FIVE, &NEG_ONE,
1034             &NINETY_NINE, &FIVE, &FOUR,
1035             &HUNDRED, &FIVE, &ZERO,
1036             &HUNDRED_ONE, &FIVE, &ONE,
1037             &BIG_FIVEm1, &FIVE, &FOUR,
1038             &BIG_FIVE, &FIVE, &ZERO,
1039             &BIG_FIVEp1, &FIVE, &ONE
1040         };
1041         const int TUPLE_WIDTH = 3;
1042         const int TUPLE_COUNT = (int)(sizeof(tuples)/sizeof(tuples[0]))/TUPLE_WIDTH;
1043         for (int i = 0; i < TUPLE_COUNT; ++i) {
1044             const llong lhs = *tuples[i*TUPLE_WIDTH+0];
1045             const llong rhs = *tuples[i*TUPLE_WIDTH+1];
1046             const llong ans = *tuples[i*TUPLE_WIDTH+2];
1047 
1048             llong n = lhs;
1049             if (!((n %= rhs) == ans)) {
1050                 errln("fail: (n %= rhs) == ans");
1051             }
1052             LLAssert(n == ans);
1053 
1054             n = lhs;
1055             LLAssert((n % rhs) == ans);
1056             LLAssert(n == lhs);
1057         }
1058     }
1059 
1060     logln("Testing pow");
1061     // pow
1062     LLAssert(llong(0, 0).pow(0) == llong(0, 0));
1063     LLAssert(llong(0, 0).pow(2) == llong(0, 0));
1064     LLAssert(llong(0, 2).pow(0) == llong(0, 1));
1065     LLAssert(llong(0, 2).pow(2) == llong(0, 4));
1066     LLAssert(llong(0, 2).pow(32) == llong(1, 0));
1067     LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5));
1068 
1069     // absolute value
1070     {
1071         const llong n(0xffffffff,0xffffffff);
1072         LLAssert(n.abs() == llong(0, 1));
1073     }
1074 
1075 #ifdef RBNF_DEBUG
1076     logln("Testing atoll");
1077     // atoll
1078     const char empty[] = "";
1079     const char zero[] = "0";
1080     const char neg_one[] = "-1";
1081     const char neg_12345[] = "-12345";
1082     const char big1[] = "123456789abcdef0";
1083     const char big2[] = "fFfFfFfFfFfFfFfF";
1084     LLAssert(llong::atoll(empty) == llong(0, 0));
1085     LLAssert(llong::atoll(zero) == llong(0, 0));
1086     LLAssert(llong::atoll(neg_one) == llong(0xffffffff, 0xffffffff));
1087     LLAssert(llong::atoll(neg_12345) == -llong(0, 12345));
1088     LLAssert(llong::atoll(big1, 16) == llong(0x12345678, 0x9abcdef0));
1089     LLAssert(llong::atoll(big2, 16) == llong(0xffffffff, 0xffffffff));
1090 #endif
1091 
1092     // u_atoll
1093     const UChar uempty[] = { 0 };
1094     const UChar uzero[] = { 0x30, 0 };
1095     const UChar uneg_one[] = { 0x2d, 0x31, 0 };
1096     const UChar uneg_12345[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 };
1097     const UChar ubig1[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 };
1098     const UChar ubig2[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 };
1099     LLAssert(llong::utoll(uempty) == llong(0, 0));
1100     LLAssert(llong::utoll(uzero) == llong(0, 0));
1101     LLAssert(llong::utoll(uneg_one) == llong(0xffffffff, 0xffffffff));
1102     LLAssert(llong::utoll(uneg_12345) == -llong(0, 12345));
1103     LLAssert(llong::utoll(ubig1, 16) == llong(0x12345678, 0x9abcdef0));
1104     LLAssert(llong::utoll(ubig2, 16) == llong(0xffffffff, 0xffffffff));
1105 
1106 #ifdef RBNF_DEBUG
1107     logln("Testing lltoa");
1108     // lltoa
1109     {
1110         char buf[64]; // ascii
1111         LLAssert((llong(0, 0).lltoa(buf, (uint32_t)sizeof(buf)) == 1) && (strcmp(buf, zero) == 0));
1112         LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf, (uint32_t)sizeof(buf)) == 2) && (strcmp(buf, neg_one) == 0));
1113         LLAssert(((-llong(0, 12345)).lltoa(buf, (uint32_t)sizeof(buf)) == 6) && (strcmp(buf, neg_12345) == 0));
1114         LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf, (uint32_t)sizeof(buf), 16) == 16) && (strcmp(buf, big1) == 0));
1115     }
1116 #endif
1117 
1118     logln("Testing u_lltoa");
1119     // u_lltoa
1120     {
1121         UChar buf[64];
1122         LLAssert((llong(0, 0).lltou(buf, (uint32_t)sizeof(buf)) == 1) && (u_strcmp(buf, uzero) == 0));
1123         LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf, (uint32_t)sizeof(buf)) == 2) && (u_strcmp(buf, uneg_one) == 0));
1124         LLAssert(((-llong(0, 12345)).lltou(buf, (uint32_t)sizeof(buf)) == 6) && (u_strcmp(buf, uneg_12345) == 0));
1125         LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf, (uint32_t)sizeof(buf), 16) == 16) && (u_strcmp(buf, ubig1) == 0));
1126     }
1127 }
1128 
1129 /* if 0 */
1130 #endif
1131 
1132 void
TestEnglishSpellout()1133 IntlTestRBNF::TestEnglishSpellout()
1134 {
1135     UErrorCode status = U_ZERO_ERROR;
1136     RuleBasedNumberFormat* formatter
1137         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status);
1138     if (U_FAILURE(status)) {
1139         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1140     } else {
1141         static const char* const testData[][2] = {
1142             { "1", "one" },
1143             { "2", "two" },
1144             { "15", "fifteen" },
1145             { "20", "twenty" },
1146             { "23", "twenty-three" },
1147             { "73", "seventy-three" },
1148             { "88", "eighty-eight" },
1149             { "100", "one hundred" },
1150             { "106", "one hundred six" },
1151             { "127", "one hundred twenty-seven" },
1152             { "200", "two hundred" },
1153             { "579", "five hundred seventy-nine" },
1154             { "1,000", "one thousand" },
1155             { "2,000", "two thousand" },
1156             { "3,004", "three thousand four" },
1157             { "4,567", "four thousand five hundred sixty-seven" },
1158             { "15,943", "fifteen thousand nine hundred forty-three" },
1159             { "2,345,678", "two million three hundred forty-five thousand six hundred seventy-eight" },
1160             { "-36", "minus thirty-six" },
1161             { "234.567", "two hundred thirty-four point five six seven" },
1162             { NULL, NULL}
1163         };
1164 
1165         doTest(formatter, testData, TRUE);
1166 
1167 #if !UCONFIG_NO_COLLATION
1168         if( !logKnownIssue("9503") ) {
1169           formatter->setLenient(TRUE);
1170           static const char* lpTestData[][2] = {
1171             { "fifty-7", "57" },
1172             { " fifty-7", "57" },
1173             { "  fifty-7", "57" },
1174             { "2 thousand six    HUNDRED fifty-7", "2,657" },
1175             { "fifteen hundred and zero", "1,500" },
1176             { "FOurhundred     thiRTY six", "436" },
1177             { NULL, NULL}
1178           };
1179           doLenientParseTest(formatter, lpTestData);
1180         }
1181 #endif
1182     }
1183     delete formatter;
1184 }
1185 
1186 void
TestOrdinalAbbreviations()1187 IntlTestRBNF::TestOrdinalAbbreviations()
1188 {
1189     UErrorCode status = U_ZERO_ERROR;
1190     RuleBasedNumberFormat* formatter
1191         = new RuleBasedNumberFormat(URBNF_ORDINAL, Locale::getUS(), status);
1192 
1193     if (U_FAILURE(status)) {
1194         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1195     } else {
1196         static const char* const testData[][2] = {
1197             { "1", "1st" },
1198             { "2", "2nd" },
1199             { "3", "3rd" },
1200             { "4", "4th" },
1201             { "7", "7th" },
1202             { "10", "10th" },
1203             { "11", "11th" },
1204             { "13", "13th" },
1205             { "20", "20th" },
1206             { "21", "21st" },
1207             { "22", "22nd" },
1208             { "23", "23rd" },
1209             { "24", "24th" },
1210             { "33", "33rd" },
1211             { "102", "102nd" },
1212             { "312", "312th" },
1213             { "12,345", "12,345th" },
1214             { NULL, NULL}
1215         };
1216 
1217         doTest(formatter, testData, FALSE);
1218     }
1219     delete formatter;
1220 }
1221 
1222 void
TestDurations()1223 IntlTestRBNF::TestDurations()
1224 {
1225     UErrorCode status = U_ZERO_ERROR;
1226     RuleBasedNumberFormat* formatter
1227         = new RuleBasedNumberFormat(URBNF_DURATION, Locale::getUS(), status);
1228 
1229     if (U_FAILURE(status)) {
1230         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1231     } else {
1232         static const char* const testData[][2] = {
1233             { "3,600", "1:00:00" },     //move me and I fail
1234             { "0", "0 sec." },
1235             { "1", "1 sec." },
1236             { "24", "24 sec." },
1237             { "60", "1:00" },
1238             { "73", "1:13" },
1239             { "145", "2:25" },
1240             { "666", "11:06" },
1241             //            { "3,600", "1:00:00" },
1242             { "3,740", "1:02:20" },
1243             { "10,293", "2:51:33" },
1244             { NULL, NULL}
1245         };
1246 
1247         doTest(formatter, testData, TRUE);
1248 
1249 #if !UCONFIG_NO_COLLATION
1250         formatter->setLenient(TRUE);
1251         static const char* lpTestData[][2] = {
1252             { "2-51-33", "10,293" },
1253             { NULL, NULL}
1254         };
1255         doLenientParseTest(formatter, lpTestData);
1256 #endif
1257     }
1258     delete formatter;
1259 }
1260 
1261 void
TestSpanishSpellout()1262 IntlTestRBNF::TestSpanishSpellout()
1263 {
1264     UErrorCode status = U_ZERO_ERROR;
1265     RuleBasedNumberFormat* formatter
1266         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("es", "ES", ""), status);
1267 
1268     if (U_FAILURE(status)) {
1269         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1270     } else {
1271         static const char* const testData[][2] = {
1272             { "1", "uno" },
1273             { "6", "seis" },
1274             { "16", "diecis\\u00e9is" },
1275             { "20", "veinte" },
1276             { "24", "veinticuatro" },
1277             { "26", "veintis\\u00e9is" },
1278             { "73", "setenta y tres" },
1279             { "88", "ochenta y ocho" },
1280             { "100", "cien" },
1281             { "106", "ciento seis" },
1282             { "127", "ciento veintisiete" },
1283             { "200", "doscientos" },
1284             { "579", "quinientos setenta y nueve" },
1285             { "1,000", "mil" },
1286             { "2,000", "dos mil" },
1287             { "3,004", "tres mil cuatro" },
1288             { "4,567", "cuatro mil quinientos sesenta y siete" },
1289             { "15,943", "quince mil novecientos cuarenta y tres" },
1290             { "2,345,678", "dos millones trescientos cuarenta y cinco mil seiscientos setenta y ocho"},
1291             { "-36", "menos treinta y seis" },
1292             { "234.567", "doscientos treinta y cuatro coma cinco seis siete" },
1293             { NULL, NULL}
1294         };
1295 
1296         doTest(formatter, testData, TRUE);
1297     }
1298     delete formatter;
1299 }
1300 
1301 void
TestFrenchSpellout()1302 IntlTestRBNF::TestFrenchSpellout()
1303 {
1304     UErrorCode status = U_ZERO_ERROR;
1305     RuleBasedNumberFormat* formatter
1306         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getFrance(), status);
1307 
1308     if (U_FAILURE(status)) {
1309         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1310     } else {
1311         static const char* const testData[][2] = {
1312             { "1", "un" },
1313             { "15", "quinze" },
1314             { "20", "vingt" },
1315             { "21", "vingt-et-un" },
1316             { "23", "vingt-trois" },
1317             { "62", "soixante-deux" },
1318             { "70", "soixante-dix" },
1319             { "71", "soixante-et-onze" },
1320             { "73", "soixante-treize" },
1321             { "80", "quatre-vingts" },
1322             { "88", "quatre-vingt-huit" },
1323             { "100", "cent" },
1324             { "106", "cent six" },
1325             { "127", "cent vingt-sept" },
1326             { "200", "deux cents" },
1327             { "579", "cinq cent soixante-dix-neuf" },
1328             { "1,000", "mille" },
1329             { "1,123", "mille cent vingt-trois" },
1330             { "1,594", "mille cinq cent quatre-vingt-quatorze" },
1331             { "2,000", "deux mille" },
1332             { "3,004", "trois mille quatre" },
1333             { "4,567", "quatre mille cinq cent soixante-sept" },
1334             { "15,943", "quinze mille neuf cent quarante-trois" },
1335             { "2,345,678", "deux millions trois cent quarante-cinq mille six cent soixante-dix-huit" },
1336             { "-36", "moins trente-six" },
1337             { "234.567", "deux cent trente-quatre virgule cinq six sept" },
1338             { NULL, NULL}
1339         };
1340 
1341         doTest(formatter, testData, TRUE);
1342 
1343 #if !UCONFIG_NO_COLLATION
1344         formatter->setLenient(TRUE);
1345         static const char* lpTestData[][2] = {
1346             { "trente-et-un", "31" },
1347             { "un cent quatre vingt dix huit", "198" },
1348             { NULL, NULL}
1349         };
1350         doLenientParseTest(formatter, lpTestData);
1351 #endif
1352     }
1353     delete formatter;
1354 }
1355 
1356 static const char* const swissFrenchTestData[][2] = {
1357     { "1", "un" },
1358     { "15", "quinze" },
1359     { "20", "vingt" },
1360     { "21", "vingt-et-un" },
1361     { "23", "vingt-trois" },
1362     { "62", "soixante-deux" },
1363     { "70", "septante" },
1364     { "71", "septante-et-un" },
1365     { "73", "septante-trois" },
1366     { "80", "huitante" },
1367     { "88", "huitante-huit" },
1368     { "100", "cent" },
1369     { "106", "cent six" },
1370     { "127", "cent vingt-sept" },
1371     { "200", "deux cents" },
1372     { "579", "cinq cent septante-neuf" },
1373     { "1,000", "mille" },
1374     { "1,123", "mille cent vingt-trois" },
1375     { "1,594", "mille cinq cent nonante-quatre" },
1376     { "2,000", "deux mille" },
1377     { "3,004", "trois mille quatre" },
1378     { "4,567", "quatre mille cinq cent soixante-sept" },
1379     { "15,943", "quinze mille neuf cent quarante-trois" },
1380     { "2,345,678", "deux millions trois cent quarante-cinq mille six cent septante-huit" },
1381     { "-36", "moins trente-six" },
1382     { "234.567", "deux cent trente-quatre virgule cinq six sept" },
1383     { NULL, NULL}
1384 };
1385 
1386 void
TestSwissFrenchSpellout()1387 IntlTestRBNF::TestSwissFrenchSpellout()
1388 {
1389     UErrorCode status = U_ZERO_ERROR;
1390     RuleBasedNumberFormat* formatter
1391         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "CH", ""), status);
1392 
1393     if (U_FAILURE(status)) {
1394         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1395     } else {
1396         doTest(formatter, swissFrenchTestData, TRUE);
1397     }
1398     delete formatter;
1399 }
1400 
1401 static const char* const belgianFrenchTestData[][2] = {
1402     { "1", "un" },
1403     { "15", "quinze" },
1404     { "20", "vingt" },
1405     { "21", "vingt-et-un" },
1406     { "23", "vingt-trois" },
1407     { "62", "soixante-deux" },
1408     { "70", "septante" },
1409     { "71", "septante-et-un" },
1410     { "73", "septante-trois" },
1411     { "80", "quatre-vingts" },
1412     { "88", "quatre-vingt huit" },
1413     { "90", "nonante" },
1414     { "91", "nonante-et-un" },
1415     { "95", "nonante-cinq" },
1416     { "100", "cent" },
1417     { "106", "cent six" },
1418     { "127", "cent vingt-sept" },
1419     { "200", "deux cents" },
1420     { "579", "cinq cent septante-neuf" },
1421     { "1,000", "mille" },
1422     { "1,123", "mille cent vingt-trois" },
1423     { "1,594", "mille cinq cent nonante-quatre" },
1424     { "2,000", "deux mille" },
1425     { "3,004", "trois mille quatre" },
1426     { "4,567", "quatre mille cinq cent soixante-sept" },
1427     { "15,943", "quinze mille neuf cent quarante-trois" },
1428     { "2,345,678", "deux millions trois cent quarante-cinq mille six cent septante-huit" },
1429     { "-36", "moins trente-six" },
1430     { "234.567", "deux cent trente-quatre virgule cinq six sept" },
1431     { NULL, NULL}
1432 };
1433 
1434 
1435 void
TestBelgianFrenchSpellout()1436 IntlTestRBNF::TestBelgianFrenchSpellout()
1437 {
1438     UErrorCode status = U_ZERO_ERROR;
1439     RuleBasedNumberFormat* formatter
1440         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "BE", ""), status);
1441 
1442     if (U_FAILURE(status)) {
1443         errcheckln(status, "rbnf status: 0x%x (%s)\n", status, u_errorName(status));
1444         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1445     } else {
1446         // Belgian french should match Swiss french.
1447         doTest(formatter, belgianFrenchTestData, TRUE);
1448     }
1449     delete formatter;
1450 }
1451 
1452 void
TestItalianSpellout()1453 IntlTestRBNF::TestItalianSpellout()
1454 {
1455     UErrorCode status = U_ZERO_ERROR;
1456     RuleBasedNumberFormat* formatter
1457         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getItalian(), status);
1458 
1459     if (U_FAILURE(status)) {
1460         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1461     } else {
1462         static const char* const testData[][2] = {
1463             { "1", "uno" },
1464             { "15", "quindici" },
1465             { "20", "venti" },
1466             { "23", "venti\\u00ADtr\\u00E9" },
1467             { "73", "settanta\\u00ADtr\\u00E9" },
1468             { "88", "ottant\\u00ADotto" },
1469             { "100", "cento" },
1470             { "101", "cento\\u00ADuno" },
1471             { "103", "cento\\u00ADtr\\u00E9" },
1472             { "106", "cento\\u00ADsei" },
1473             { "108", "cent\\u00ADotto" },
1474             { "127", "cento\\u00ADventi\\u00ADsette" },
1475             { "181", "cent\\u00ADottant\\u00ADuno" },
1476             { "200", "due\\u00ADcento" },
1477             { "579", "cinque\\u00ADcento\\u00ADsettanta\\u00ADnove" },
1478             { "1,000", "mille" },
1479             { "2,000", "due\\u00ADmila" },
1480             { "3,004", "tre\\u00ADmila\\u00ADquattro" },
1481             { "4,567", "quattro\\u00ADmila\\u00ADcinque\\u00ADcento\\u00ADsessanta\\u00ADsette" },
1482             { "15,943", "quindici\\u00ADmila\\u00ADnove\\u00ADcento\\u00ADquaranta\\u00ADtr\\u00E9" },
1483             { "-36", "meno trenta\\u00ADsei" },
1484             { "234.567", "due\\u00ADcento\\u00ADtrenta\\u00ADquattro virgola cinque sei sette" },
1485             { NULL, NULL}
1486         };
1487 
1488         doTest(formatter, testData, TRUE);
1489     }
1490     delete formatter;
1491 }
1492 
1493 void
TestPortugueseSpellout()1494 IntlTestRBNF::TestPortugueseSpellout()
1495 {
1496     UErrorCode status = U_ZERO_ERROR;
1497     RuleBasedNumberFormat* formatter
1498         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("pt","BR",""), status);
1499 
1500     if (U_FAILURE(status)) {
1501         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1502     } else {
1503         static const char* const testData[][2] = {
1504             { "1", "um" },
1505             { "15", "quinze" },
1506             { "20", "vinte" },
1507             { "23", "vinte e tr\\u00EAs" },
1508             { "73", "setenta e tr\\u00EAs" },
1509             { "88", "oitenta e oito" },
1510             { "100", "cem" },
1511             { "106", "cento e seis" },
1512             { "108", "cento e oito" },
1513             { "127", "cento e vinte e sete" },
1514             { "181", "cento e oitenta e um" },
1515             { "200", "duzentos" },
1516             { "579", "quinhentos e setenta e nove" },
1517             { "1,000", "mil" },
1518             { "2,000", "dois mil" },
1519             { "3,004", "tr\\u00EAs mil e quatro" },
1520             { "4,567", "quatro mil e quinhentos e sessenta e sete" },
1521             { "15,943", "quinze mil e novecentos e quarenta e tr\\u00EAs" },
1522             { "-36", "menos trinta e seis" },
1523             { "234.567", "duzentos e trinta e quatro v\\u00EDrgula cinco seis sete" },
1524             { NULL, NULL}
1525         };
1526 
1527         doTest(formatter, testData, TRUE);
1528     }
1529     delete formatter;
1530 }
1531 void
TestGermanSpellout()1532 IntlTestRBNF::TestGermanSpellout()
1533 {
1534     UErrorCode status = U_ZERO_ERROR;
1535     RuleBasedNumberFormat* formatter
1536         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getGermany(), status);
1537 
1538     if (U_FAILURE(status)) {
1539         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1540     } else {
1541         static const char* const testData[][2] = {
1542             { "1", "eins" },
1543             { "15", "f\\u00fcnfzehn" },
1544             { "20", "zwanzig" },
1545             { "23", "drei\\u00ADund\\u00ADzwanzig" },
1546             { "73", "drei\\u00ADund\\u00ADsiebzig" },
1547             { "88", "acht\\u00ADund\\u00ADachtzig" },
1548             { "100", "ein\\u00ADhundert" },
1549             { "106", "ein\\u00ADhundert\\u00ADsechs" },
1550             { "127", "ein\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADzwanzig" },
1551             { "200", "zwei\\u00ADhundert" },
1552             { "579", "f\\u00fcnf\\u00ADhundert\\u00ADneun\\u00ADund\\u00ADsiebzig" },
1553             { "1,000", "ein\\u00ADtausend" },
1554             { "2,000", "zwei\\u00ADtausend" },
1555             { "3,004", "drei\\u00ADtausend\\u00ADvier" },
1556             { "4,567", "vier\\u00ADtausend\\u00ADf\\u00fcnf\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADsechzig" },
1557             { "15,943", "f\\u00fcnfzehn\\u00ADtausend\\u00ADneun\\u00ADhundert\\u00ADdrei\\u00ADund\\u00ADvierzig" },
1558             { "2,345,678", "zwei Millionen drei\\u00ADhundert\\u00ADf\\u00fcnf\\u00ADund\\u00ADvierzig\\u00ADtausend\\u00ADsechs\\u00ADhundert\\u00ADacht\\u00ADund\\u00ADsiebzig" },
1559             { NULL, NULL}
1560         };
1561 
1562         doTest(formatter, testData, TRUE);
1563 
1564 #if !UCONFIG_NO_COLLATION
1565         formatter->setLenient(TRUE);
1566         static const char* lpTestData[][2] = {
1567             { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
1568             { NULL, NULL}
1569         };
1570         doLenientParseTest(formatter, lpTestData);
1571 #endif
1572     }
1573     delete formatter;
1574 }
1575 
1576 void
TestThaiSpellout()1577 IntlTestRBNF::TestThaiSpellout()
1578 {
1579     UErrorCode status = U_ZERO_ERROR;
1580     RuleBasedNumberFormat* formatter
1581         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("th"), status);
1582 
1583     if (U_FAILURE(status)) {
1584         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1585     } else {
1586         static const char* const testData[][2] = {
1587             { "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" },
1588             { "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1589             { "10", "\\u0e2a\\u0e34\\u0e1a" },
1590             { "11", "\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1591             { "21", "\\u0e22\\u0e35\\u0e48\\u200b\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1592             { "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e23\\u0e49\\u0e2d\\u0e22\\u200b\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1593             { "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e08\\u0e38\\u0e14\\u200b\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" },
1594             { NULL, NULL}
1595         };
1596 
1597         doTest(formatter, testData, TRUE);
1598     }
1599     delete formatter;
1600 }
1601 
1602 void
TestSwedishSpellout()1603 IntlTestRBNF::TestSwedishSpellout()
1604 {
1605     UErrorCode status = U_ZERO_ERROR;
1606     RuleBasedNumberFormat* formatter
1607         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("sv"), status);
1608 
1609     if (U_FAILURE(status)) {
1610         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1611     } else {
1612         static const char* testDataDefault[][2] = {
1613             { "101", "ett\\u00adhundra\\u00adett" },
1614             { "123", "ett\\u00adhundra\\u00adtjugo\\u00adtre" },
1615             { "1,001", "et\\u00adtusen ett" },
1616             { "1,100", "et\\u00adtusen ett\\u00adhundra" },
1617             { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1618             { "1,234", "et\\u00adtusen tv\\u00e5\\u00adhundra\\u00adtrettio\\u00adfyra" },
1619             { "10,001", "tio\\u00adtusen ett" },
1620             { "11,000", "elva\\u00adtusen" },
1621             { "12,000", "tolv\\u00adtusen" },
1622             { "20,000", "tjugo\\u00adtusen" },
1623             { "21,000", "tjugo\\u00adet\\u00adtusen" },
1624             { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
1625             { "200,000", "tv\\u00e5\\u00adhundra\\u00adtusen" },
1626             { "201,000", "tv\\u00e5\\u00adhundra\\u00adet\\u00adtusen" },
1627             { "200,200", "tv\\u00e5\\u00adhundra\\u00adtusen tv\\u00e5\\u00adhundra" },
1628             { "2,002,000", "tv\\u00e5 miljoner tv\\u00e5\\u00adtusen" },
1629             { "12,345,678", "tolv miljoner tre\\u00adhundra\\u00adfyrtio\\u00adfem\\u00adtusen sex\\u00adhundra\\u00adsjuttio\\u00ad\\u00e5tta" },
1630             { "123,456.789", "ett\\u00adhundra\\u00adtjugo\\u00adtre\\u00adtusen fyra\\u00adhundra\\u00adfemtio\\u00adsex komma sju \\u00e5tta nio" },
1631             { "-12,345.678", "minus tolv\\u00adtusen tre\\u00adhundra\\u00adfyrtio\\u00adfem komma sex sju \\u00e5tta" },
1632             { NULL, NULL }
1633         };
1634         doTest(formatter, testDataDefault, TRUE);
1635 
1636           static const char* testDataNeutrum[][2] = {
1637               { "101", "ett\\u00adhundra\\u00adett" },
1638               { "1,001", "et\\u00adtusen ett" },
1639               { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1640               { "10,001", "tio\\u00adtusen ett" },
1641               { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
1642               { NULL, NULL }
1643           };
1644 
1645           formatter->setDefaultRuleSet("%spellout-cardinal-neuter", status);
1646           if (U_SUCCESS(status)) {
1647           logln("        testing spellout-cardinal-neuter rules");
1648           doTest(formatter, testDataNeutrum, TRUE);
1649           }
1650           else {
1651           errln("Can't test spellout-cardinal-neuter rules");
1652           }
1653 
1654         static const char* testDataYear[][2] = {
1655             { "101", "ett\\u00adhundra\\u00adett" },
1656             { "900", "nio\\u00adhundra" },
1657             { "1,001", "et\\u00adtusen ett" },
1658             { "1,100", "elva\\u00adhundra" },
1659             { "1,101", "elva\\u00adhundra\\u00adett" },
1660             { "1,234", "tolv\\u00adhundra\\u00adtrettio\\u00adfyra" },
1661             { "2,001", "tjugo\\u00adhundra\\u00adett" },
1662             { "10,001", "tio\\u00adtusen ett" },
1663             { NULL, NULL }
1664         };
1665 
1666         status = U_ZERO_ERROR;
1667         formatter->setDefaultRuleSet("%spellout-numbering-year", status);
1668         if (U_SUCCESS(status)) {
1669             logln("testing year rules");
1670             doTest(formatter, testDataYear, TRUE);
1671         }
1672         else {
1673             errln("Can't test year rules");
1674         }
1675 
1676     }
1677     delete formatter;
1678 }
1679 
1680 void
TestSmallValues()1681 IntlTestRBNF::TestSmallValues()
1682 {
1683     UErrorCode status = U_ZERO_ERROR;
1684     RuleBasedNumberFormat* formatter
1685         = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("en_US"), status);
1686 
1687     if (U_FAILURE(status)) {
1688         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1689     } else {
1690         static const char* const testDataDefault[][2] = {
1691         { "0.001", "zero point zero zero one" },
1692         { "0.0001", "zero point zero zero zero one" },
1693         { "0.00001", "zero point zero zero zero zero one" },
1694         { "0.000001", "zero point zero zero zero zero zero one" },
1695         { "0.0000001", "zero point zero zero zero zero zero zero one" },
1696         { "0.00000001", "zero point zero zero zero zero zero zero zero one" },
1697         { "0.000000001", "zero point zero zero zero zero zero zero zero zero one" },
1698         { "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" },
1699         { "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" },
1700         { "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" },
1701         { "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" },
1702         { "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1703         { "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1704         { "10,000,000.001", "ten million point zero zero one" },
1705         { "10,000,000.0001", "ten million point zero zero zero one" },
1706         { "10,000,000.00001", "ten million point zero zero zero zero one" },
1707         { "10,000,000.000001", "ten million point zero zero zero zero zero one" },
1708         { "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" },
1709 //        { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" },
1710 //        { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" },
1711         { "10,000,000", "ten million" },
1712 //        { "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" },
1713 //        { "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" },
1714 //        { "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" },
1715         { "1,234,567.7654321", "one million two hundred thirty-four thousand five hundred sixty-seven point seven six five four three two one" },
1716         { "123,456.654321", "one hundred twenty-three thousand four hundred fifty-six point six five four three two one" },
1717         { "12,345.54321", "twelve thousand three hundred forty-five point five four three two one" },
1718         { "1,234.4321", "one thousand two hundred thirty-four point four three two one" },
1719         { "123.321", "one hundred twenty-three point three two one" },
1720         { "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" },
1721         { "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" },
1722         { NULL, NULL }
1723         };
1724 
1725         doTest(formatter, testDataDefault, TRUE);
1726 
1727         delete formatter;
1728     }
1729 }
1730 
1731 void
TestLocalizations(void)1732 IntlTestRBNF::TestLocalizations(void)
1733 {
1734     int i;
1735     UnicodeString rules("%main:0:no;1:some;100:a lot;1000:tons;\n"
1736         "%other:0:nada;1:yah, some;100:plenty;1000:more'n you'll ever need");
1737 
1738     UErrorCode status = U_ZERO_ERROR;
1739     UParseError perror;
1740     RuleBasedNumberFormat formatter(rules, perror, status);
1741     if (U_FAILURE(status)) {
1742         errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
1743     } else {
1744         {
1745             static const char* const testData[][2] = {
1746                 { "0", "nada" },
1747                 { "5", "yah, some" },
1748                 { "423", "plenty" },
1749                 { "12345", "more'n you'll ever need" },
1750                 { NULL, NULL }
1751             };
1752             doTest(&formatter, testData, FALSE);
1753         }
1754 
1755         {
1756             UnicodeString loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>");
1757             static const char* const testData[][2] = {
1758                 { "0", "no" },
1759                 { "5", "some" },
1760                 { "423", "a lot" },
1761                 { "12345", "tons" },
1762                 { NULL, NULL }
1763             };
1764             RuleBasedNumberFormat formatter0(rules, loc, perror, status);
1765             if (U_FAILURE(status)) {
1766                 errln("failed to build second formatter");
1767             } else {
1768                 doTest(&formatter0, testData, FALSE);
1769 
1770                 {
1771                 // exercise localization info
1772                     Locale locale0("en__VALLEY@turkey=gobblegobble");
1773                     Locale locale1("de_DE_FOO");
1774                     Locale locale2("ja_JP");
1775                     UnicodeString name = formatter0.getRuleSetName(0);
1776                     if ( formatter0.getRuleSetDisplayName(0, locale0) == "Main"
1777                       && formatter0.getRuleSetDisplayName(0, locale1) == "das Main"
1778                       && formatter0.getRuleSetDisplayName(0, locale2) == "%main"
1779                       && formatter0.getRuleSetDisplayName(name, locale0) == "Main"
1780                       && formatter0.getRuleSetDisplayName(name, locale1) == "das Main"
1781                       && formatter0.getRuleSetDisplayName(name, locale2) == "%main"){
1782                           logln("getRuleSetDisplayName tested");
1783                     }else {
1784                         errln("failed to getRuleSetDisplayName");
1785                     }
1786                 }
1787 
1788                 for (i = 0; i < formatter0.getNumberOfRuleSetDisplayNameLocales(); ++i) {
1789                     Locale locale = formatter0.getRuleSetDisplayNameLocale(i, status);
1790                     if (U_SUCCESS(status)) {
1791                         for (int j = 0; j < formatter0.getNumberOfRuleSetNames(); ++j) {
1792                             UnicodeString name = formatter0.getRuleSetName(j);
1793                             UnicodeString lname = formatter0.getRuleSetDisplayName(j, locale);
1794                             UnicodeString msg = locale.getName();
1795                             msg.append(": ");
1796                             msg.append(name);
1797                             msg.append(" = ");
1798                             msg.append(lname);
1799                             logln(msg);
1800                         }
1801                     }
1802                 }
1803             }
1804         }
1805 
1806         {
1807             static const char* goodLocs[] = {
1808                 "", // zero-length ok, same as providing no localization data
1809                 "<<>>", // no public rule sets ok
1810                 "<<%main>>", // no localizations ok
1811                 "<<%main,>,<en, Main,>>", // comma before close angle ok
1812                 "<<%main>,<en, ',<>\" '>>", // quotes everything until next quote
1813                 "<<%main>,<'en', \"it's ok\">>", // double quotes work too
1814                 "  \n <\n  <\n  %main\n  >\n  , \t <\t   en\t  ,  \tfoo \t\t > \n\n >  \n ", // Pattern_White_Space ok
1815            };
1816             int32_t goodLocsLen = sizeof(goodLocs)/sizeof(goodLocs[0]);
1817 
1818             static const char* badLocs[] = {
1819                 " ", // non-zero length
1820                 "<>", // empty array
1821                 "<", // unclosed outer array
1822                 "<<", // unclosed inner array
1823                 "<<,>>", // unexpected comma
1824                 "<<''>>", // empty string
1825                 "  x<<%main>>", // first non space char not open angle bracket
1826                 "<%main>", // missing inner array
1827                 "<<%main %other>>", // elements missing separating commma (spaces must be quoted)
1828                 "<<%main><en, Main>>", // arrays missing separating comma
1829                 "<<%main>,<en, main, foo>>", // too many elements in locale data
1830                 "<<%main>,<en>>", // too few elements in locale data
1831                 "<<<%main>>>", // unexpected open angle
1832                 "<<%main<>>>", // unexpected open angle
1833                 "<<%main, %other>,<en,,>>", // implicit empty strings
1834                 "<<%main>,<en,''>>", // empty string
1835                 "<<%main>, < en, '>>", // unterminated quote
1836                 "<<%main>, < en, \"<>>", // unterminated quote
1837                 "<<%main\">>", // quote in string
1838                 "<<%main'>>", // quote in string
1839                 "<<%main<>>", // open angle in string
1840                 "<<%main>> x", // extra non-space text at end
1841 
1842             };
1843             int32_t badLocsLen = sizeof(badLocs)/sizeof(badLocs[0]);
1844 
1845             for (i = 0; i < goodLocsLen; ++i) {
1846                 logln("[%d] '%s'", i, goodLocs[i]);
1847                 UErrorCode status = U_ZERO_ERROR;
1848                 UnicodeString loc(goodLocs[i]);
1849                 RuleBasedNumberFormat fmt(rules, loc, perror, status);
1850                 if (U_FAILURE(status)) {
1851                     errln("Failed parse of good localization string: '%s'", goodLocs[i]);
1852                 }
1853             }
1854 
1855             for (i = 0; i < badLocsLen; ++i) {
1856                 logln("[%d] '%s'", i, badLocs[i]);
1857                 UErrorCode status = U_ZERO_ERROR;
1858                 UnicodeString loc(badLocs[i]);
1859                 RuleBasedNumberFormat fmt(rules, loc, perror, status);
1860                 if (U_SUCCESS(status)) {
1861                     errln("Successful parse of bad localization string: '%s'", badLocs[i]);
1862                 }
1863             }
1864         }
1865     }
1866 }
1867 
1868 void
TestAllLocales()1869 IntlTestRBNF::TestAllLocales()
1870 {
1871     const char* names[] = {
1872         " (spellout) ",
1873         " (ordinal)  "
1874         // " (duration) " // This is English only, and it's not really supported in CLDR anymore.
1875     };
1876     double numbers[] = {45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111};
1877 
1878     int32_t count = 0;
1879     const Locale* locales = Locale::getAvailableLocales(count);
1880     for (int i = 0; i < count; ++i) {
1881         const Locale* loc = &locales[i];
1882 
1883         for (int j = 0; j < 2; ++j) {
1884             UErrorCode status = U_ZERO_ERROR;
1885             RuleBasedNumberFormat* f = new RuleBasedNumberFormat((URBNFRuleSetTag)j, *loc, status);
1886 
1887             if (status == U_USING_DEFAULT_WARNING || status == U_USING_FALLBACK_WARNING) {
1888                 // Skip it.
1889                 delete f;
1890                 break;
1891             }
1892             if (U_FAILURE(status)) {
1893                 errln(UnicodeString(loc->getName()) + names[j]
1894                     + "ERROR could not instantiate -> " + u_errorName(status));
1895                 continue;
1896             }
1897 #if !UCONFIG_NO_COLLATION
1898             for (unsigned int numidx = 0; numidx < sizeof(numbers)/sizeof(double); numidx++) {
1899                 double n = numbers[numidx];
1900                 UnicodeString str;
1901                 f->format(n, str);
1902 
1903                 if (verbose) {
1904                     logln(UnicodeString(loc->getName()) + names[j]
1905                         + "success: " + n + " -> " + str);
1906                 }
1907 
1908                 // We do not validate the result in this test case,
1909                 // because there are cases which do not round trip by design.
1910                 Formattable num;
1911 
1912                 // regular parse
1913                 status = U_ZERO_ERROR;
1914                 f->setLenient(FALSE);
1915                 f->parse(str, num, status);
1916                 if (U_FAILURE(status)) {
1917                     errln(UnicodeString(loc->getName()) + names[j]
1918                         + "ERROR could not parse '" + str + "' -> " + u_errorName(status));
1919                 }
1920                 // We only check the spellout. The behavior is undefined for numbers < 1 and fractional numbers.
1921                 if (j == 0) {
1922                     if (num.getType() == Formattable::kLong && num.getLong() != n) {
1923                         errln(UnicodeString(loc->getName()) + names[j]
1924                             + UnicodeString("ERROR could not roundtrip ") + n
1925                             + UnicodeString(" -> ") + str + UnicodeString(" -> ") + num.getLong());
1926                     }
1927                     else if (num.getType() == Formattable::kDouble && (int64_t)(num.getDouble() * 1000) != (int64_t)(n*1000)) {
1928                         // The epsilon difference is too high.
1929                         errln(UnicodeString(loc->getName()) + names[j]
1930                             + UnicodeString("ERROR could not roundtrip ") + n
1931                             + UnicodeString(" -> ") + str + UnicodeString(" -> ") + num.getDouble());
1932                     }
1933                 }
1934                 if (!quick && !logKnownIssue("9503") ) {
1935                     // lenient parse
1936                     status = U_ZERO_ERROR;
1937                     f->setLenient(TRUE);
1938                     f->parse(str, num, status);
1939                     if (U_FAILURE(status)) {
1940                         errln(UnicodeString(loc->getName()) + names[j]
1941                             + "ERROR could not parse(lenient) '" + str + "' -> " + u_errorName(status));
1942                     }
1943                     // We only check the spellout. The behavior is undefined for numbers < 1 and fractional numbers.
1944                     if (j == 0) {
1945                         if (num.getType() == Formattable::kLong && num.getLong() != n) {
1946                             errln(UnicodeString(loc->getName()) + names[j]
1947                                 + UnicodeString("ERROR could not roundtrip ") + n
1948                                 + UnicodeString(" -> ") + str + UnicodeString(" -> ") + num.getLong());
1949                         }
1950                         else if (num.getType() == Formattable::kDouble && (int64_t)(num.getDouble() * 1000) != (int64_t)(n*1000)) {
1951                             // The epsilon difference is too high.
1952                             errln(UnicodeString(loc->getName()) + names[j]
1953                                 + UnicodeString("ERROR could not roundtrip ") + n
1954                                 + UnicodeString(" -> ") + str + UnicodeString(" -> ") + num.getDouble());
1955                         }
1956                     }
1957                 }
1958             }
1959 #endif
1960             delete f;
1961         }
1962     }
1963 }
1964 
1965 void
TestMultiplierSubstitution(void)1966 IntlTestRBNF::TestMultiplierSubstitution(void) {
1967     UnicodeString rules("=#,##0=;1,000,000: <##0.###< million;");
1968     UErrorCode status = U_ZERO_ERROR;
1969     UParseError parse_error;
1970     RuleBasedNumberFormat *rbnf =
1971         new RuleBasedNumberFormat(rules, Locale::getUS(), parse_error, status);
1972     if (U_SUCCESS(status)) {
1973         UnicodeString res;
1974         FieldPosition pos;
1975         double n = 1234000.0;
1976         rbnf->format(n, res, pos);
1977         delete rbnf;
1978 
1979         UnicodeString expected(UNICODE_STRING_SIMPLE("1.234 million"));
1980         if (expected != res) {
1981             UnicodeString msg = "Expected: ";
1982             msg.append(expected);
1983             msg.append(" but got ");
1984             msg.append(res);
1985             errln(msg);
1986         }
1987     }
1988 }
1989 
1990 void
TestSetDecimalFormatSymbols()1991 IntlTestRBNF::TestSetDecimalFormatSymbols() {
1992     UErrorCode status = U_ZERO_ERROR;
1993 
1994     RuleBasedNumberFormat rbnf(URBNF_ORDINAL, Locale::getEnglish(), status);
1995     if (U_FAILURE(status)) {
1996         dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
1997         return;
1998     }
1999 
2000     DecimalFormatSymbols dfs(Locale::getEnglish(), status);
2001     if (U_FAILURE(status)) {
2002         errln("Unable to create DecimalFormatSymbols - " + UnicodeString(u_errorName(status)));
2003         return;
2004     }
2005 
2006     UnicodeString expected[] = {
2007             UnicodeString("1,001st"),
2008             UnicodeString("1&001st")
2009     };
2010 
2011     double number = 1001;
2012 
2013     UnicodeString result;
2014 
2015     rbnf.format(number, result);
2016     if (result != expected[0]) {
2017         errln("Format Error - Got: " + result + " Expected: " + expected[0]);
2018     }
2019 
2020     result.remove();
2021 
2022     /* Set new symbol for testing */
2023     dfs.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, UnicodeString("&"), TRUE);
2024     rbnf.setDecimalFormatSymbols(dfs);
2025 
2026     rbnf.format(number, result);
2027     if (result != expected[1]) {
2028         errln("Format Error - Got: " + result + " Expected: " + expected[1]);
2029     }
2030 }
2031 
TestPluralRules()2032 void IntlTestRBNF::TestPluralRules() {
2033     UErrorCode status = U_ZERO_ERROR;
2034     UnicodeString enRules("%digits-ordinal:-x: ->>;0: =#,##0=$(ordinal,one{st}two{nd}few{rd}other{th})$;");
2035     UParseError parseError;
2036     RuleBasedNumberFormat enFormatter(enRules, Locale::getEnglish(), parseError, status);
2037     if (U_FAILURE(status)) {
2038         dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
2039         return;
2040     }
2041     const char* const enTestData[][2] = {
2042             { "1", "1st" },
2043             { "2", "2nd" },
2044             { "3", "3rd" },
2045             { "4", "4th" },
2046             { "11", "11th" },
2047             { "12", "12th" },
2048             { "13", "13th" },
2049             { "14", "14th" },
2050             { "21", "21st" },
2051             { "22", "22nd" },
2052             { "23", "23rd" },
2053             { "24", "24th" },
2054             { NULL, NULL }
2055     };
2056 
2057     doTest(&enFormatter, enTestData, TRUE);
2058 
2059     // This is trying to model the feminine form, but don't worry about the details too much.
2060     // We're trying to test the plural rules.
2061     UnicodeString ruRules("%spellout-numbering:"
2062             "-x: minus >>;"
2063             "x.x: << point >>;"
2064             "0: zero;"
2065             "1: one;"
2066             "2: two;"
2067             "3: three;"
2068             "4: four;"
2069             "5: five;"
2070             "6: six;"
2071             "7: seven;"
2072             "8: eight;"
2073             "9: nine;"
2074             "10: ten;"
2075             "11: eleven;"
2076             "12: twelve;"
2077             "13: thirteen;"
2078             "14: fourteen;"
2079             "15: fifteen;"
2080             "16: sixteen;"
2081             "17: seventeen;"
2082             "18: eighteen;"
2083             "19: nineteen;"
2084             "20: twenty[->>];"
2085             "30: thirty[->>];"
2086             "40: forty[->>];"
2087             "50: fifty[->>];"
2088             "60: sixty[->>];"
2089             "70: seventy[->>];"
2090             "80: eighty[->>];"
2091             "90: ninety[->>];"
2092             "100: hundred[ >>];"
2093             "200: << hundred[ >>];"
2094             "300: << hundreds[ >>];"
2095             "500: << hundredss[ >>];"
2096             "1000: << $(cardinal,one{thousand}few{thousands}other{thousandss})$[ >>];"
2097             "1000000: << $(cardinal,one{million}few{millions}other{millionss})$[ >>];");
2098     RuleBasedNumberFormat ruFormatter(ruRules, Locale("ru"), parseError, status);
2099     const char* const ruTestData[][2] = {
2100             { "1", "one" },
2101             { "100", "hundred" },
2102             { "125", "hundred twenty-five" },
2103             { "399", "three hundreds ninety-nine" },
2104             { "1,000", "one thousand" },
2105             { "1,001", "one thousand one" },
2106             { "2,000", "two thousands" },
2107             { "2,001", "two thousands one" },
2108             { "2,002", "two thousands two" },
2109             { "3,333", "three thousands three hundreds thirty-three" },
2110             { "5,000", "five thousandss" },
2111             { "11,000", "eleven thousandss" },
2112             { "21,000", "twenty-one thousand" },
2113             { "22,000", "twenty-two thousands" },
2114             { "25,001", "twenty-five thousandss one" },
2115             { NULL, NULL }
2116     };
2117 
2118     if (U_FAILURE(status)) {
2119         errln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
2120         return;
2121     }
2122     doTest(&ruFormatter, ruTestData, TRUE);
2123 
2124     // Make sure there are no divide by 0 errors.
2125     UnicodeString result;
2126     RuleBasedNumberFormat(ruRules, Locale("ru"), parseError, status).format(21000, result);
2127     if (result.compare(UNICODE_STRING_SIMPLE("twenty-one thousand")) != 0) {
2128         errln("Got " + result + " for 21000");
2129     }
2130 
2131 }
2132 
TestInfinityNaN()2133 void IntlTestRBNF::TestInfinityNaN() {
2134     UErrorCode status = U_ZERO_ERROR;
2135     UParseError parseError;
2136     UnicodeString enRules("%default:"
2137             "-x: minus >>;"
2138             "Inf: infinite;"
2139             "NaN: not a number;"
2140             "0: =#,##0=;");
2141     RuleBasedNumberFormat enFormatter(enRules, Locale::getEnglish(), parseError, status);
2142     const char * const enTestData[][2] = {
2143             {"1", "1"},
2144             {"\\u221E", "infinite"},
2145             {"-\\u221E", "minus infinite"},
2146             {"NaN", "not a number"},
2147             { NULL, NULL }
2148     };
2149     if (U_FAILURE(status)) {
2150         dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
2151         return;
2152     }
2153 
2154     doTest(&enFormatter, enTestData, true);
2155 
2156     // Test the default behavior when the rules are undefined.
2157     UnicodeString enRules2("%default:"
2158             "-x: ->>;"
2159             "0: =#,##0=;");
2160     RuleBasedNumberFormat enFormatter2(enRules2, Locale::getEnglish(), parseError, status);
2161     if (U_FAILURE(status)) {
2162         errln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
2163         return;
2164     }
2165     const char * const enDefaultTestData[][2] = {
2166             {"1", "1"},
2167             {"\\u221E", "\\u221E"},
2168             {"-\\u221E", "-\\u221E"},
2169             {"NaN", "NaN"},
2170             { NULL, NULL }
2171     };
2172 
2173     doTest(&enFormatter2, enDefaultTestData, true);
2174 }
2175 
TestVariableDecimalPoint()2176 void IntlTestRBNF::TestVariableDecimalPoint() {
2177     UErrorCode status = U_ZERO_ERROR;
2178     UParseError parseError;
2179     UnicodeString enRules("%spellout-numbering:"
2180             "-x: minus >>;"
2181             "x.x: << point >>;"
2182             "x,x: << comma >>;"
2183             "0.x: xpoint >>;"
2184             "0,x: xcomma >>;"
2185             "0: zero;"
2186             "1: one;"
2187             "2: two;"
2188             "3: three;"
2189             "4: four;"
2190             "5: five;"
2191             "6: six;"
2192             "7: seven;"
2193             "8: eight;"
2194             "9: nine;");
2195     RuleBasedNumberFormat enFormatter(enRules, Locale::getEnglish(), parseError, status);
2196     const char * const enTestPointData[][2] = {
2197             {"1.1", "one point one"},
2198             {"1.23", "one point two three"},
2199             {"0.4", "xpoint four"},
2200             { NULL, NULL }
2201     };
2202     if (U_FAILURE(status)) {
2203         dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
2204         return;
2205     }
2206     doTest(&enFormatter, enTestPointData, true);
2207 
2208     DecimalFormatSymbols decimalFormatSymbols(Locale::getEnglish(), status);
2209     decimalFormatSymbols.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, UNICODE_STRING_SIMPLE(","));
2210     enFormatter.setDecimalFormatSymbols(decimalFormatSymbols);
2211     const char * const enTestCommaData[][2] = {
2212             {"1.1", "one comma one"},
2213             {"1.23", "one comma two three"},
2214             {"0.4", "xcomma four"},
2215             { NULL, NULL }
2216     };
2217     doTest(&enFormatter, enTestCommaData, true);
2218 }
2219 
2220 void
doTest(RuleBasedNumberFormat * formatter,const char * const testData[][2],UBool testParsing)2221 IntlTestRBNF::doTest(RuleBasedNumberFormat* formatter, const char* const testData[][2], UBool testParsing)
2222 {
2223   // man, error reporting would be easier with printf-style syntax for unicode string and formattable
2224 
2225     UErrorCode status = U_ZERO_ERROR;
2226     DecimalFormatSymbols dfs("en", status);
2227     // NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
2228     DecimalFormat decFmt("#,###.################", dfs, status);
2229     if (U_FAILURE(status)) {
2230         errcheckln(status, "FAIL: could not create NumberFormat - %s", u_errorName(status));
2231     } else {
2232         for (int i = 0; testData[i][0]; ++i) {
2233             const char* numString = testData[i][0];
2234             const char* expectedWords = testData[i][1];
2235 
2236             log("[%i] %s = ", i, numString);
2237             Formattable expectedNumber;
2238             UnicodeString escapedNumString = UnicodeString(numString, -1, US_INV).unescape();
2239             decFmt.parse(escapedNumString, expectedNumber, status);
2240             if (U_FAILURE(status)) {
2241                 errln("FAIL: decFmt could not parse %s", numString);
2242                 break;
2243             } else {
2244                 UnicodeString actualString;
2245                 FieldPosition pos;
2246                 formatter->format(expectedNumber, actualString/* , pos*/, status);
2247                 if (U_FAILURE(status)) {
2248                     UnicodeString msg = "Fail: formatter could not format ";
2249                     decFmt.format(expectedNumber, msg, status);
2250                     errln(msg);
2251                     break;
2252                 } else {
2253                     UnicodeString expectedString = UnicodeString(expectedWords, -1, US_INV).unescape();
2254                     if (actualString != expectedString) {
2255                         UnicodeString msg = "FAIL: check failed for ";
2256                         decFmt.format(expectedNumber, msg, status);
2257                         msg.append(", expected ");
2258                         msg.append(expectedString);
2259                         msg.append(" but got ");
2260                         msg.append(actualString);
2261                         errln(msg);
2262                         break;
2263                     } else {
2264                         logln(actualString);
2265                         if (testParsing) {
2266                             Formattable parsedNumber;
2267                             formatter->parse(actualString, parsedNumber, status);
2268                             if (U_FAILURE(status)) {
2269                                 UnicodeString msg = "FAIL: formatter could not parse ";
2270                                 msg.append(actualString);
2271                                 msg.append(" status code: " );
2272                                 msg.append(u_errorName(status));
2273                                 errln(msg);
2274                                 break;
2275                             } else {
2276                                 if (parsedNumber != expectedNumber
2277                                     && (!uprv_isNaN(parsedNumber.getDouble()) || !uprv_isNaN(expectedNumber.getDouble())))
2278                                 {
2279                                     UnicodeString msg = "FAIL: parse failed for ";
2280                                     msg.append(actualString);
2281                                     msg.append(", expected ");
2282                                     decFmt.format(expectedNumber, msg, status);
2283                                     msg.append(", but got ");
2284                                     decFmt.format(parsedNumber, msg, status);
2285                                     errln(msg);
2286                                     break;
2287                                 }
2288                             }
2289                         }
2290                     }
2291                 }
2292             }
2293         }
2294     }
2295 }
2296 
2297 void
doLenientParseTest(RuleBasedNumberFormat * formatter,const char * testData[][2])2298 IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat* formatter, const char* testData[][2])
2299 {
2300     UErrorCode status = U_ZERO_ERROR;
2301     NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
2302     if (U_FAILURE(status)) {
2303         errcheckln(status, "FAIL: could not create NumberFormat - %s", u_errorName(status));
2304     } else {
2305         for (int i = 0; testData[i][0]; ++i) {
2306             const char* spelledNumber = testData[i][0]; // spelled-out number
2307             const char* asciiUSNumber = testData[i][1]; // number as ascii digits formatted for US locale
2308 
2309             UnicodeString spelledNumberString = UnicodeString(spelledNumber).unescape();
2310             Formattable actualNumber;
2311             formatter->parse(spelledNumberString, actualNumber, status);
2312             if (U_FAILURE(status)) {
2313                 UnicodeString msg = "FAIL: formatter could not parse ";
2314                 msg.append(spelledNumberString);
2315                 errln(msg);
2316                 break;
2317             } else {
2318                 // I changed the logic of this test somewhat from Java-- instead of comparing the
2319                 // strings, I compare the Formattables.  Hmmm, but the Formattables don't compare,
2320                 // so change it back.
2321 
2322                 UnicodeString asciiUSNumberString = asciiUSNumber;
2323                 Formattable expectedNumber;
2324                 decFmt->parse(asciiUSNumberString, expectedNumber, status);
2325                 if (U_FAILURE(status)) {
2326                     UnicodeString msg = "FAIL: decFmt could not parse ";
2327                     msg.append(asciiUSNumberString);
2328                     errln(msg);
2329                     break;
2330                 } else {
2331                     UnicodeString actualNumberString;
2332                     UnicodeString expectedNumberString;
2333                     decFmt->format(actualNumber, actualNumberString, status);
2334                     decFmt->format(expectedNumber, expectedNumberString, status);
2335                     if (actualNumberString != expectedNumberString) {
2336                         UnicodeString msg = "FAIL: parsing";
2337                         msg.append(asciiUSNumberString);
2338                         msg.append("\n");
2339                         msg.append("  lenient parse failed for ");
2340                         msg.append(spelledNumberString);
2341                         msg.append(", expected ");
2342                         msg.append(expectedNumberString);
2343                         msg.append(", but got ");
2344                         msg.append(actualNumberString);
2345                         errln(msg);
2346                         break;
2347                     }
2348                 }
2349             }
2350         }
2351         delete decFmt;
2352     }
2353 }
2354 
2355 /* U_HAVE_RBNF */
2356 #else
2357 
2358 void
TestRBNFDisabled()2359 IntlTestRBNF::TestRBNFDisabled() {
2360     errln("*** RBNF currently disabled on this platform ***\n");
2361 }
2362 
2363 /* U_HAVE_RBNF */
2364 #endif
2365 
2366 #endif /* #if !UCONFIG_NO_FORMATTING */
2367