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