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