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