]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/itrbnf.cpp
ICU-511.27.tar.gz
[apple/icu.git] / icuSources / test / intltest / itrbnf.cpp
CommitLineData
b75a7d8f
A
1/*
2 *******************************************************************************
4388f060 3 * Copyright (C) 1996-2012, 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
1141 formatter->setLenient(TRUE);
1142 static const char* lpTestData[][2] = {
1143 { "fifty-7", "57" },
1144 { " fifty-7", "57" },
1145 { " fifty-7", "57" },
1146 { "2 thousand six HUNDRED fifty-7", "2,657" },
1147 { "fifteen hundred and zero", "1,500" },
1148 { "FOurhundred thiRTY six", "436" },
1149 { NULL, NULL}
1150 };
1151 doLenientParseTest(formatter, lpTestData);
1152#endif
1153 }
1154 delete formatter;
1155}
1156
1157void
1158IntlTestRBNF::TestOrdinalAbbreviations()
1159{
1160 UErrorCode status = U_ZERO_ERROR;
1161 RuleBasedNumberFormat* formatter
1162 = new RuleBasedNumberFormat(URBNF_ORDINAL, Locale::getUS(), status);
1163
1164 if (U_FAILURE(status)) {
729e4ab9 1165 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f 1166 } else {
46f4442e 1167 static const char* const testData[][2] = {
b75a7d8f
A
1168 { "1", "1st" },
1169 { "2", "2nd" },
1170 { "3", "3rd" },
1171 { "4", "4th" },
1172 { "7", "7th" },
1173 { "10", "10th" },
1174 { "11", "11th" },
1175 { "13", "13th" },
1176 { "20", "20th" },
1177 { "21", "21st" },
1178 { "22", "22nd" },
1179 { "23", "23rd" },
1180 { "24", "24th" },
1181 { "33", "33rd" },
1182 { "102", "102nd" },
1183 { "312", "312th" },
1184 { "12,345", "12,345th" },
1185 { NULL, NULL}
1186 };
1187
1188 doTest(formatter, testData, FALSE);
1189 }
1190 delete formatter;
1191}
1192
1193void
1194IntlTestRBNF::TestDurations()
1195{
1196 UErrorCode status = U_ZERO_ERROR;
1197 RuleBasedNumberFormat* formatter
1198 = new RuleBasedNumberFormat(URBNF_DURATION, Locale::getUS(), status);
1199
1200 if (U_FAILURE(status)) {
729e4ab9 1201 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f 1202 } else {
46f4442e 1203 static const char* const testData[][2] = {
b75a7d8f
A
1204 { "3,600", "1:00:00" }, //move me and I fail
1205 { "0", "0 sec." },
1206 { "1", "1 sec." },
1207 { "24", "24 sec." },
1208 { "60", "1:00" },
1209 { "73", "1:13" },
1210 { "145", "2:25" },
1211 { "666", "11:06" },
1212 // { "3,600", "1:00:00" },
1213 { "3,740", "1:02:20" },
1214 { "10,293", "2:51:33" },
1215 { NULL, NULL}
1216 };
1217
1218 doTest(formatter, testData, TRUE);
1219
1220#if !UCONFIG_NO_COLLATION
1221 formatter->setLenient(TRUE);
1222 static const char* lpTestData[][2] = {
1223 { "2-51-33", "10,293" },
1224 { NULL, NULL}
1225 };
1226 doLenientParseTest(formatter, lpTestData);
1227#endif
1228 }
1229 delete formatter;
1230}
1231
1232void
1233IntlTestRBNF::TestSpanishSpellout()
1234{
1235 UErrorCode status = U_ZERO_ERROR;
1236 RuleBasedNumberFormat* formatter
1237 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("es", "ES", ""), status);
1238
1239 if (U_FAILURE(status)) {
729e4ab9 1240 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f 1241 } else {
46f4442e 1242 static const char* const testData[][2] = {
b75a7d8f
A
1243 { "1", "uno" },
1244 { "6", "seis" },
1245 { "16", "diecis\\u00e9is" },
1246 { "20", "veinte" },
1247 { "24", "veinticuatro" },
1248 { "26", "veintis\\u00e9is" },
1249 { "73", "setenta y tres" },
1250 { "88", "ochenta y ocho" },
1251 { "100", "cien" },
1252 { "106", "ciento seis" },
1253 { "127", "ciento veintisiete" },
1254 { "200", "doscientos" },
1255 { "579", "quinientos setenta y nueve" },
1256 { "1,000", "mil" },
1257 { "2,000", "dos mil" },
1258 { "3,004", "tres mil cuatro" },
1259 { "4,567", "cuatro mil quinientos sesenta y siete" },
1260 { "15,943", "quince mil novecientos cuarenta y tres" },
729e4ab9 1261 { "2,345,678", "dos millones trescientos cuarenta y cinco mil seiscientos setenta y ocho"},
b75a7d8f 1262 { "-36", "menos treinta y seis" },
729e4ab9 1263 { "234.567", "doscientos treinta y cuatro coma cinco seis siete" },
b75a7d8f
A
1264 { NULL, NULL}
1265 };
1266
1267 doTest(formatter, testData, TRUE);
1268 }
1269 delete formatter;
1270}
1271
1272void
1273IntlTestRBNF::TestFrenchSpellout()
1274{
1275 UErrorCode status = U_ZERO_ERROR;
1276 RuleBasedNumberFormat* formatter
1277 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getFrance(), status);
1278
1279 if (U_FAILURE(status)) {
729e4ab9 1280 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f 1281 } else {
46f4442e 1282 static const char* const testData[][2] = {
b75a7d8f
A
1283 { "1", "un" },
1284 { "15", "quinze" },
1285 { "20", "vingt" },
1286 { "21", "vingt-et-un" },
1287 { "23", "vingt-trois" },
1288 { "62", "soixante-deux" },
1289 { "70", "soixante-dix" },
729e4ab9 1290 { "71", "soixante-et-onze" },
b75a7d8f
A
1291 { "73", "soixante-treize" },
1292 { "80", "quatre-vingts" },
1293 { "88", "quatre-vingt-huit" },
1294 { "100", "cent" },
729e4ab9
A
1295 { "106", "cent-six" },
1296 { "127", "cent-vingt-sept" },
1297 { "200", "deux-cents" },
1298 { "579", "cinq-cent-soixante-dix-neuf" },
b75a7d8f 1299 { "1,000", "mille" },
729e4ab9
A
1300 { "1,123", "mille-cent-vingt-trois" },
1301 { "1,594", "mille-cinq-cent-quatre-vingt-quatorze" },
1302 { "2,000", "deux-mille" },
1303 { "3,004", "trois-mille-quatre" },
1304 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1305 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1306 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-soixante-dix-huit" },
b75a7d8f 1307 { "-36", "moins trente-six" },
729e4ab9 1308 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
b75a7d8f
A
1309 { NULL, NULL}
1310 };
1311
1312 doTest(formatter, testData, TRUE);
1313
1314#if !UCONFIG_NO_COLLATION
1315 formatter->setLenient(TRUE);
1316 static const char* lpTestData[][2] = {
729e4ab9
A
1317 { "trente-et-un", "31" },
1318 { "un cent quatre vingt dix huit", "198" },
b75a7d8f
A
1319 { NULL, NULL}
1320 };
1321 doLenientParseTest(formatter, lpTestData);
1322#endif
1323 }
1324 delete formatter;
1325}
1326
46f4442e 1327static const char* const swissFrenchTestData[][2] = {
374ca955
A
1328 { "1", "un" },
1329 { "15", "quinze" },
1330 { "20", "vingt" },
1331 { "21", "vingt-et-un" },
1332 { "23", "vingt-trois" },
1333 { "62", "soixante-deux" },
1334 { "70", "septante" },
1335 { "71", "septante-et-un" },
1336 { "73", "septante-trois" },
1337 { "80", "huitante" },
1338 { "88", "huitante-huit" },
1339 { "100", "cent" },
729e4ab9
A
1340 { "106", "cent-six" },
1341 { "127", "cent-vingt-sept" },
1342 { "200", "deux-cents" },
1343 { "579", "cinq-cent-septante-neuf" },
374ca955 1344 { "1,000", "mille" },
729e4ab9
A
1345 { "1,123", "mille-cent-vingt-trois" },
1346 { "1,594", "mille-cinq-cent-nonante-quatre" },
1347 { "2,000", "deux-mille" },
1348 { "3,004", "trois-mille-quatre" },
1349 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1350 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1351 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-septante-huit" },
374ca955 1352 { "-36", "moins trente-six" },
729e4ab9 1353 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
374ca955 1354 { NULL, NULL}
b75a7d8f
A
1355};
1356
1357void
1358IntlTestRBNF::TestSwissFrenchSpellout()
1359{
1360 UErrorCode status = U_ZERO_ERROR;
1361 RuleBasedNumberFormat* formatter
1362 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "CH", ""), status);
1363
1364 if (U_FAILURE(status)) {
729e4ab9 1365 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f
A
1366 } else {
1367 doTest(formatter, swissFrenchTestData, TRUE);
1368 }
1369 delete formatter;
1370}
1371
729e4ab9
A
1372static const char* const belgianFrenchTestData[][2] = {
1373 { "1", "un" },
1374 { "15", "quinze" },
1375 { "20", "vingt" },
1376 { "21", "vingt-et-un" },
1377 { "23", "vingt-trois" },
1378 { "62", "soixante-deux" },
1379 { "70", "septante" },
1380 { "71", "septante-et-un" },
1381 { "73", "septante-trois" },
1382 { "80", "quatre-vingts" },
1383 { "88", "quatre-vingt-huit" },
1384 { "90", "nonante" },
1385 { "91", "nonante-et-un" },
1386 { "95", "nonante-cinq" },
1387 { "100", "cent" },
1388 { "106", "cent-six" },
1389 { "127", "cent-vingt-sept" },
1390 { "200", "deux-cents" },
1391 { "579", "cinq-cent-septante-neuf" },
1392 { "1,000", "mille" },
1393 { "1,123", "mille-cent-vingt-trois" },
1394 { "1,594", "mille-cinq-cent-nonante-quatre" },
1395 { "2,000", "deux-mille" },
1396 { "3,004", "trois-mille-quatre" },
1397 { "4,567", "quatre-mille-cinq-cent-soixante-sept" },
1398 { "15,943", "quinze-mille-neuf-cent-quarante-trois" },
1399 { "2,345,678", "deux millions trois-cent-quarante-cinq-mille-six-cent-septante-huit" },
1400 { "-36", "moins trente-six" },
1401 { "234.567", "deux-cent-trente-quatre virgule cinq six sept" },
1402 { NULL, NULL}
1403};
1404
1405
b75a7d8f
A
1406void
1407IntlTestRBNF::TestBelgianFrenchSpellout()
1408{
1409 UErrorCode status = U_ZERO_ERROR;
1410 RuleBasedNumberFormat* formatter
1411 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "BE", ""), status);
1412
1413 if (U_FAILURE(status)) {
729e4ab9
A
1414 errcheckln(status, "rbnf status: 0x%x (%s)\n", status, u_errorName(status));
1415 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f 1416 } else {
374ca955 1417 // Belgian french should match Swiss french.
729e4ab9 1418 doTest(formatter, belgianFrenchTestData, TRUE);
b75a7d8f
A
1419 }
1420 delete formatter;
1421}
1422
1423void
1424IntlTestRBNF::TestItalianSpellout()
1425{
1426 UErrorCode status = U_ZERO_ERROR;
1427 RuleBasedNumberFormat* formatter
1428 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getItalian(), status);
1429
1430 if (U_FAILURE(status)) {
729e4ab9 1431 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f 1432 } else {
46f4442e 1433 static const char* const testData[][2] = {
b75a7d8f
A
1434 { "1", "uno" },
1435 { "15", "quindici" },
1436 { "20", "venti" },
729e4ab9
A
1437 { "23", "venti\\u00ADtr\\u00E9" },
1438 { "73", "settanta\\u00ADtr\\u00E9" },
1439 { "88", "ottant\\u00ADotto" },
b75a7d8f 1440 { "100", "cento" },
51004dcb 1441 { "101", "cento\\u00ADuno" },
729e4ab9
A
1442 { "103", "cento\\u00ADtr\\u00E9" },
1443 { "106", "cento\\u00ADsei" },
1444 { "108", "cent\\u00ADotto" },
1445 { "127", "cento\\u00ADventi\\u00ADsette" },
1446 { "181", "cent\\u00ADottant\\u00ADuno" },
1447 { "200", "due\\u00ADcento" },
1448 { "579", "cinque\\u00ADcento\\u00ADsettanta\\u00ADnove" },
b75a7d8f 1449 { "1,000", "mille" },
729e4ab9
A
1450 { "2,000", "due\\u00ADmila" },
1451 { "3,004", "tre\\u00ADmila\\u00ADquattro" },
1452 { "4,567", "quattro\\u00ADmila\\u00ADcinque\\u00ADcento\\u00ADsessanta\\u00ADsette" },
1453 { "15,943", "quindici\\u00ADmila\\u00ADnove\\u00ADcento\\u00ADquaranta\\u00ADtr\\u00E9" },
1454 { "-36", "meno trenta\\u00ADsei" },
1455 { "234.567", "due\\u00ADcento\\u00ADtrenta\\u00ADquattro virgola cinque sei sette" },
b75a7d8f
A
1456 { NULL, NULL}
1457 };
1458
1459 doTest(formatter, testData, TRUE);
1460 }
1461 delete formatter;
1462}
1463
46f4442e
A
1464void
1465IntlTestRBNF::TestPortugueseSpellout()
1466{
1467 UErrorCode status = U_ZERO_ERROR;
1468 RuleBasedNumberFormat* formatter
1469 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("pt","BR",""), status);
1470
1471 if (U_FAILURE(status)) {
729e4ab9 1472 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
46f4442e
A
1473 } else {
1474 static const char* const testData[][2] = {
1475 { "1", "um" },
1476 { "15", "quinze" },
1477 { "20", "vinte" },
1478 { "23", "vinte e tr\\u00EAs" },
1479 { "73", "setenta e tr\\u00EAs" },
1480 { "88", "oitenta e oito" },
1481 { "100", "cem" },
1482 { "106", "cento e seis" },
1483 { "108", "cento e oito" },
1484 { "127", "cento e vinte e sete" },
1485 { "181", "cento e oitenta e um" },
51004dcb 1486 { "200", "duzentos" },
46f4442e
A
1487 { "579", "quinhentos e setenta e nove" },
1488 { "1,000", "mil" },
1489 { "2,000", "dois mil" },
1490 { "3,004", "tr\\u00EAs mil e quatro" },
729e4ab9
A
1491 { "4,567", "quatro mil e quinhentos e sessenta e sete" },
1492 { "15,943", "quinze mil e novecentos e quarenta e tr\\u00EAs" },
46f4442e 1493 { "-36", "menos trinta e seis" },
51004dcb 1494 { "234.567", "duzentos e trinta e quatro v\\u00EDrgula cinco seis sete" },
46f4442e
A
1495 { NULL, NULL}
1496 };
1497
1498 doTest(formatter, testData, TRUE);
1499 }
1500 delete formatter;
1501}
b75a7d8f
A
1502void
1503IntlTestRBNF::TestGermanSpellout()
1504{
1505 UErrorCode status = U_ZERO_ERROR;
1506 RuleBasedNumberFormat* formatter
1507 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getGermany(), status);
1508
1509 if (U_FAILURE(status)) {
729e4ab9 1510 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f 1511 } else {
46f4442e 1512 static const char* const testData[][2] = {
b75a7d8f
A
1513 { "1", "eins" },
1514 { "15", "f\\u00fcnfzehn" },
1515 { "20", "zwanzig" },
729e4ab9
A
1516 { "23", "drei\\u00ADund\\u00ADzwanzig" },
1517 { "73", "drei\\u00ADund\\u00ADsiebzig" },
1518 { "88", "acht\\u00ADund\\u00ADachtzig" },
1519 { "100", "ein\\u00ADhundert" },
1520 { "106", "ein\\u00ADhundert\\u00ADsechs" },
1521 { "127", "ein\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADzwanzig" },
1522 { "200", "zwei\\u00ADhundert" },
1523 { "579", "f\\u00fcnf\\u00ADhundert\\u00ADneun\\u00ADund\\u00ADsiebzig" },
1524 { "1,000", "ein\\u00ADtausend" },
1525 { "2,000", "zwei\\u00ADtausend" },
1526 { "3,004", "drei\\u00ADtausend\\u00ADvier" },
1527 { "4,567", "vier\\u00ADtausend\\u00ADf\\u00fcnf\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADsechzig" },
1528 { "15,943", "f\\u00fcnfzehn\\u00ADtausend\\u00ADneun\\u00ADhundert\\u00ADdrei\\u00ADund\\u00ADvierzig" },
1529 { "2,345,678", "zwei Millionen drei\\u00ADhundert\\u00ADf\\u00fcnf\\u00ADund\\u00ADvierzig\\u00ADtausend\\u00ADsechs\\u00ADhundert\\u00ADacht\\u00ADund\\u00ADsiebzig" },
b75a7d8f
A
1530 { NULL, NULL}
1531 };
1532
1533 doTest(formatter, testData, TRUE);
1534
1535#if !UCONFIG_NO_COLLATION
1536 formatter->setLenient(TRUE);
1537 static const char* lpTestData[][2] = {
1538 { "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
1539 { NULL, NULL}
1540 };
1541 doLenientParseTest(formatter, lpTestData);
1542#endif
1543 }
1544 delete formatter;
1545}
1546
1547void
1548IntlTestRBNF::TestThaiSpellout()
1549{
1550 UErrorCode status = U_ZERO_ERROR;
1551 RuleBasedNumberFormat* formatter
1552 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("th"), status);
1553
1554 if (U_FAILURE(status)) {
729e4ab9 1555 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f 1556 } else {
46f4442e 1557 static const char* const testData[][2] = {
b75a7d8f
A
1558 { "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" },
1559 { "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1560 { "10", "\\u0e2a\\u0e34\\u0e1a" },
729e4ab9
A
1561 { "11", "\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1562 { "21", "\\u0e22\\u0e35\\u0e48\\u200b\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
1563 { "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e23\\u0e49\\u0e2d\\u0e22\\u200b\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1564 { "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e08\\u0e38\\u0e14\\u200b\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" },
b75a7d8f
A
1565 { NULL, NULL}
1566 };
1567
1568 doTest(formatter, testData, TRUE);
1569 }
1570 delete formatter;
1571}
1572
1573void
1574IntlTestRBNF::TestSwedishSpellout()
1575{
1576 UErrorCode status = U_ZERO_ERROR;
1577 RuleBasedNumberFormat* formatter
1578 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("sv"), status);
1579
1580 if (U_FAILURE(status)) {
729e4ab9 1581 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
b75a7d8f
A
1582 } else {
1583 static const char* testDataDefault[][2] = {
729e4ab9
A
1584 { "101", "ett\\u00adhundra\\u00adett" },
1585 { "123", "ett\\u00adhundra\\u00adtjugo\\u00adtre" },
1586 { "1,001", "et\\u00adtusen ett" },
1587 { "1,100", "et\\u00adtusen ett\\u00adhundra" },
1588 { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1589 { "1,234", "et\\u00adtusen tv\\u00e5\\u00adhundra\\u00adtrettio\\u00adfyra" },
1590 { "10,001", "tio\\u00adtusen ett" },
b75a7d8f
A
1591 { "11,000", "elva\\u00adtusen" },
1592 { "12,000", "tolv\\u00adtusen" },
1593 { "20,000", "tjugo\\u00adtusen" },
729e4ab9
A
1594 { "21,000", "tjugo\\u00adet\\u00adtusen" },
1595 { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
1596 { "200,000", "tv\\u00e5\\u00adhundra\\u00adtusen" },
1597 { "201,000", "tv\\u00e5\\u00adhundra\\u00adet\\u00adtusen" },
1598 { "200,200", "tv\\u00e5\\u00adhundra\\u00adtusen tv\\u00e5\\u00adhundra" },
b75a7d8f 1599 { "2,002,000", "tv\\u00e5 miljoner tv\\u00e5\\u00adtusen" },
729e4ab9
A
1600 { "12,345,678", "tolv miljoner tre\\u00adhundra\\u00adfyrtio\\u00adfem\\u00adtusen sex\\u00adhundra\\u00adsjuttio\\u00ad\\u00e5tta" },
1601 { "123,456.789", "ett\\u00adhundra\\u00adtjugo\\u00adtre\\u00adtusen fyra\\u00adhundra\\u00adfemtio\\u00adsex komma sju \\u00e5tta nio" },
1602 { "-12,345.678", "minus tolv\\u00adtusen tre\\u00adhundra\\u00adfyrtio\\u00adfem komma sex sju \\u00e5tta" },
b75a7d8f
A
1603 { NULL, NULL }
1604 };
1605 doTest(formatter, testDataDefault, TRUE);
1606
729e4ab9 1607 static const char* testDataNeutrum[][2] = {
51004dcb
A
1608 { "101", "ett\\u00adhundra\\u00adett" },
1609 { "1,001", "et\\u00adtusen ett" },
1610 { "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
1611 { "10,001", "tio\\u00adtusen ett" },
1612 { "21,001", "tjugo\\u00adet\\u00adtusen ett" },
729e4ab9
A
1613 { NULL, NULL }
1614 };
1615
51004dcb 1616 formatter->setDefaultRuleSet("%spellout-cardinal-neuter", status);
729e4ab9 1617 if (U_SUCCESS(status)) {
51004dcb 1618 logln(" testing spellout-cardinal-neuter rules");
729e4ab9
A
1619 doTest(formatter, testDataNeutrum, TRUE);
1620 }
1621 else {
51004dcb 1622 errln("Can't test spellout-cardinal-neuter rules");
729e4ab9 1623 }
b75a7d8f
A
1624
1625 static const char* testDataYear[][2] = {
729e4ab9
A
1626 { "101", "ett\\u00adhundra\\u00adett" },
1627 { "900", "nio\\u00adhundra" },
1628 { "1,001", "et\\u00adtusen ett" },
1629 { "1,100", "elva\\u00adhundra" },
1630 { "1,101", "elva\\u00adhundra\\u00adett" },
1631 { "1,234", "tolv\\u00adhundra\\u00adtrettio\\u00adfyra" },
1632 { "2,001", "tjugo\\u00adhundra\\u00adett" },
b75a7d8f
A
1633 { "10,001", "tio\\u00adtusen ett" },
1634 { NULL, NULL }
1635 };
1636
51004dcb 1637 status = U_ZERO_ERROR;
729e4ab9 1638 formatter->setDefaultRuleSet("%spellout-numbering-year", status);
b75a7d8f
A
1639 if (U_SUCCESS(status)) {
1640 logln("testing year rules");
1641 doTest(formatter, testDataYear, TRUE);
1642 }
1643 else {
1644 errln("Can't test year rules");
1645 }
1646
1647 }
1648 delete formatter;
1649}
1650
374ca955
A
1651void
1652IntlTestRBNF::TestSmallValues()
1653{
1654 UErrorCode status = U_ZERO_ERROR;
1655 RuleBasedNumberFormat* formatter
1656 = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("en_US"), status);
1657
1658 if (U_FAILURE(status)) {
729e4ab9 1659 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
374ca955 1660 } else {
46f4442e 1661 static const char* const testDataDefault[][2] = {
374ca955
A
1662 { "0.001", "zero point zero zero one" },
1663 { "0.0001", "zero point zero zero zero one" },
1664 { "0.00001", "zero point zero zero zero zero one" },
1665 { "0.000001", "zero point zero zero zero zero zero one" },
1666 { "0.0000001", "zero point zero zero zero zero zero zero one" },
1667 { "0.00000001", "zero point zero zero zero zero zero zero zero one" },
1668 { "0.000000001", "zero point zero zero zero zero zero zero zero zero one" },
1669 { "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" },
1670 { "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" },
1671 { "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" },
1672 { "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" },
1673 { "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1674 { "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
1675 { "10,000,000.001", "ten million point zero zero one" },
1676 { "10,000,000.0001", "ten million point zero zero zero one" },
1677 { "10,000,000.00001", "ten million point zero zero zero zero one" },
1678 { "10,000,000.000001", "ten million point zero zero zero zero zero one" },
1679 { "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" },
1680// { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" },
1681// { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" },
1682 { "10,000,000", "ten million" },
1683// { "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" },
1684// { "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" },
1685// { "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
1686 { "1,234,567.7654321", "one million two hundred thirty-four thousand five hundred sixty-seven point seven six five four three two one" },
1687 { "123,456.654321", "one hundred twenty-three thousand four hundred fifty-six point six five four three two one" },
1688 { "12,345.54321", "twelve thousand three hundred forty-five point five four three two one" },
1689 { "1,234.4321", "one thousand two hundred thirty-four point four three two one" },
1690 { "123.321", "one hundred twenty-three point three two one" },
374ca955
A
1691 { "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" },
1692 { "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" },
46f4442e
A
1693 { NULL, NULL }
1694 };
374ca955
A
1695
1696 doTest(formatter, testDataDefault, TRUE);
1697
46f4442e 1698 delete formatter;
374ca955
A
1699 }
1700}
1701
1702void
1703IntlTestRBNF::TestLocalizations(void)
1704{
1705 int i;
1706 UnicodeString rules("%main:0:no;1:some;100:a lot;1000:tons;\n"
1707 "%other:0:nada;1:yah, some;100:plenty;1000:more'n you'll ever need");
1708
1709 UErrorCode status = U_ZERO_ERROR;
1710 UParseError perror;
1711 RuleBasedNumberFormat formatter(rules, perror, status);
1712 if (U_FAILURE(status)) {
729e4ab9 1713 errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
374ca955
A
1714 } else {
1715 {
46f4442e 1716 static const char* const testData[][2] = {
374ca955
A
1717 { "0", "nada" },
1718 { "5", "yah, some" },
1719 { "423", "plenty" },
1720 { "12345", "more'n you'll ever need" },
1721 { NULL, NULL }
1722 };
1723 doTest(&formatter, testData, FALSE);
1724 }
1725
1726 {
1727 UnicodeString loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>");
46f4442e 1728 static const char* const testData[][2] = {
374ca955
A
1729 { "0", "no" },
1730 { "5", "some" },
1731 { "423", "a lot" },
1732 { "12345", "tons" },
1733 { NULL, NULL }
1734 };
1735 RuleBasedNumberFormat formatter0(rules, loc, perror, status);
1736 if (U_FAILURE(status)) {
1737 errln("failed to build second formatter");
1738 } else {
1739 doTest(&formatter0, testData, FALSE);
1740
1741 {
1742 // exercise localization info
1743 Locale locale0("en__VALLEY@turkey=gobblegobble");
1744 Locale locale1("de_DE_FOO");
1745 Locale locale2("ja_JP");
73c04bcf
A
1746 UnicodeString name = formatter0.getRuleSetName(0);
1747 if ( formatter0.getRuleSetDisplayName(0, locale0) == "Main"
1748 && formatter0.getRuleSetDisplayName(0, locale1) == "das Main"
1749 && formatter0.getRuleSetDisplayName(0, locale2) == "%main"
1750 && formatter0.getRuleSetDisplayName(name, locale0) == "Main"
1751 && formatter0.getRuleSetDisplayName(name, locale1) == "das Main"
1752 && formatter0.getRuleSetDisplayName(name, locale2) == "%main"){
1753 logln("getRuleSetDisplayName tested");
1754 }else {
1755 errln("failed to getRuleSetDisplayName");
1756 }
374ca955
A
1757 }
1758
1759 for (i = 0; i < formatter0.getNumberOfRuleSetDisplayNameLocales(); ++i) {
1760 Locale locale = formatter0.getRuleSetDisplayNameLocale(i, status);
1761 if (U_SUCCESS(status)) {
1762 for (int j = 0; j < formatter0.getNumberOfRuleSetNames(); ++j) {
1763 UnicodeString name = formatter0.getRuleSetName(j);
1764 UnicodeString lname = formatter0.getRuleSetDisplayName(j, locale);
1765 UnicodeString msg = locale.getName();
1766 msg.append(": ");
1767 msg.append(name);
1768 msg.append(" = ");
1769 msg.append(lname);
1770 logln(msg);
1771 }
1772 }
1773 }
1774 }
1775 }
1776
1777 {
1778 static const char* goodLocs[] = {
1779 "", // zero-length ok, same as providing no localization data
1780 "<<>>", // no public rule sets ok
1781 "<<%main>>", // no localizations ok
1782 "<<%main,>,<en, Main,>>", // comma before close angle ok
1783 "<<%main>,<en, ',<>\" '>>", // quotes everything until next quote
1784 "<<%main>,<'en', \"it's ok\">>", // double quotes work too
4388f060 1785 " \n <\n <\n %main\n >\n , \t <\t en\t , \tfoo \t\t > \n\n > \n ", // Pattern_White_Space ok
374ca955
A
1786 };
1787 int32_t goodLocsLen = sizeof(goodLocs)/sizeof(goodLocs[0]);
1788
1789 static const char* badLocs[] = {
1790 " ", // non-zero length
1791 "<>", // empty array
1792 "<", // unclosed outer array
1793 "<<", // unclosed inner array
1794 "<<,>>", // unexpected comma
1795 "<<''>>", // empty string
1796 " x<<%main>>", // first non space char not open angle bracket
1797 "<%main>", // missing inner array
1798 "<<%main %other>>", // elements missing separating commma (spaces must be quoted)
1799 "<<%main><en, Main>>", // arrays missing separating comma
1800 "<<%main>,<en, main, foo>>", // too many elements in locale data
1801 "<<%main>,<en>>", // too few elements in locale data
1802 "<<<%main>>>", // unexpected open angle
1803 "<<%main<>>>", // unexpected open angle
1804 "<<%main, %other>,<en,,>>", // implicit empty strings
1805 "<<%main>,<en,''>>", // empty string
1806 "<<%main>, < en, '>>", // unterminated quote
1807 "<<%main>, < en, \"<>>", // unterminated quote
1808 "<<%main\">>", // quote in string
1809 "<<%main'>>", // quote in string
1810 "<<%main<>>", // open angle in string
1811 "<<%main>> x", // extra non-space text at end
1812
1813 };
1814 int32_t badLocsLen = sizeof(badLocs)/sizeof(badLocs[0]);
1815
1816 for (i = 0; i < goodLocsLen; ++i) {
1817 logln("[%d] '%s'", i, goodLocs[i]);
1818 UErrorCode status = U_ZERO_ERROR;
1819 UnicodeString loc(goodLocs[i]);
1820 RuleBasedNumberFormat fmt(rules, loc, perror, status);
1821 if (U_FAILURE(status)) {
1822 errln("Failed parse of good localization string: '%s'", goodLocs[i]);
1823 }
1824 }
1825
1826 for (i = 0; i < badLocsLen; ++i) {
1827 logln("[%d] '%s'", i, badLocs[i]);
1828 UErrorCode status = U_ZERO_ERROR;
1829 UnicodeString loc(badLocs[i]);
1830 RuleBasedNumberFormat fmt(rules, loc, perror, status);
1831 if (U_SUCCESS(status)) {
1832 errln("Successful parse of bad localization string: '%s'", badLocs[i]);
1833 }
1834 }
1835 }
1836 }
1837}
b75a7d8f 1838
73c04bcf
A
1839void
1840IntlTestRBNF::TestAllLocales()
1841{
729e4ab9
A
1842 const char* names[] = {
1843 " (spellout) ",
1844 " (ordinal) ",
1845 " (duration) "
1846 };
1847 double numbers[] = {45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111};
1848
1849 // RBNF parse is extremely slow when lenient option is enabled.
1850 // For non-exhaustive mode, we only test a few locales.
1851 const char* parseLocales[] = {"en_US", "nl_NL", "be", NULL};
1852
1853
1854 int32_t count = 0;
1855 const Locale* locales = Locale::getAvailableLocales(count);
1856 for (int i = 0; i < count; ++i) {
1857 const Locale* loc = &locales[i];
1858 UBool testParse = TRUE;
1859 if (quick) {
1860 testParse = FALSE;
1861 for (int k = 0; parseLocales[k] != NULL; k++) {
1862 if (strcmp(loc->getLanguage(), parseLocales[k]) == 0) {
1863 testParse = TRUE;
1864 break;
1865 }
1866 }
1867 }
1868
1869 for (int j = 0; j < 3; ++j) {
1870 UErrorCode status = U_ZERO_ERROR;
1871 RuleBasedNumberFormat* f = new RuleBasedNumberFormat((URBNFRuleSetTag)j, *loc, status);
1872 if (U_FAILURE(status)) {
1873 errln(UnicodeString(loc->getName()) + names[j]
1874 + "ERROR could not instantiate -> " + u_errorName(status));
1875 continue;
1876 }
1877#if !UCONFIG_NO_COLLATION
1878 for (unsigned int numidx = 0; numidx < sizeof(numbers)/sizeof(double); numidx++) {
1879 double n = numbers[numidx];
1880 UnicodeString str;
1881 f->format(n, str);
1882
1883 logln(UnicodeString(loc->getName()) + names[j]
1884 + "success: " + n + " -> " + str);
1885
1886 if (testParse) {
1887 // We do not validate the result in this test case,
1888 // because there are cases which do not round trip by design.
1889 Formattable num;
1890
1891 // regular parse
1892 status = U_ZERO_ERROR;
1893 f->setLenient(FALSE);
1894 f->parse(str, num, status);
1895 if (U_FAILURE(status)) {
1896 //TODO: We need to fix parse problems - see #6895 / #6896
1897 if (status == U_INVALID_FORMAT_ERROR) {
1898 logln(UnicodeString(loc->getName()) + names[j]
1899 + "WARNING could not parse '" + str + "' -> " + u_errorName(status));
1900 } else {
1901 errln(UnicodeString(loc->getName()) + names[j]
1902 + "ERROR could not parse '" + str + "' -> " + u_errorName(status));
1903 }
1904 }
1905 // lenient parse
1906 status = U_ZERO_ERROR;
1907 f->setLenient(TRUE);
1908 f->parse(str, num, status);
1909 if (U_FAILURE(status)) {
1910 //TODO: We need to fix parse problems - see #6895 / #6896
1911 if (status == U_INVALID_FORMAT_ERROR) {
1912 logln(UnicodeString(loc->getName()) + names[j]
1913 + "WARNING could not parse(lenient) '" + str + "' -> " + u_errorName(status));
1914 } else {
1915 errln(UnicodeString(loc->getName()) + names[j]
1916 + "ERROR could not parse(lenient) '" + str + "' -> " + u_errorName(status));
1917 }
1918 }
1919 }
1920 }
1921#endif
1922 delete f;
1923 }
73c04bcf 1924 }
73c04bcf
A
1925}
1926
b75a7d8f 1927void
46f4442e
A
1928IntlTestRBNF::TestMultiplierSubstitution(void) {
1929 UnicodeString rules("=#,##0=;1,000,000: <##0.###< million;");
1930 UErrorCode status = U_ZERO_ERROR;
1931 UParseError parse_error;
1932 RuleBasedNumberFormat *rbnf =
1933 new RuleBasedNumberFormat(rules, Locale::getUS(), parse_error, status);
1934 if (U_SUCCESS(status)) {
1935 UnicodeString res;
1936 FieldPosition pos;
1937 double n = 1234000.0;
1938 rbnf->format(n, res, pos);
1939 delete rbnf;
1940
1941 UnicodeString expected = UNICODE_STRING_SIMPLE("1.234 million");
1942 if (expected != res) {
1943 UnicodeString msg = "Expected: ";
1944 msg.append(expected);
1945 msg.append(" but got ");
1946 msg.append(res);
1947 errln(msg);
1948 }
1949 }
1950}
1951
4388f060
A
1952void
1953IntlTestRBNF::TestSetDecimalFormatSymbols() {
1954 UErrorCode status = U_ZERO_ERROR;
1955
1956 RuleBasedNumberFormat rbnf(URBNF_ORDINAL, Locale::getEnglish(), status);
1957 if (U_FAILURE(status)) {
1958 dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
1959 return;
1960 }
1961
1962 DecimalFormatSymbols dfs(Locale::getEnglish(), status);
1963 if (U_FAILURE(status)) {
1964 errln("Unable to create DecimalFormatSymbols - " + UnicodeString(u_errorName(status)));
1965 return;
1966 }
1967
1968 UnicodeString expected[] = {
1969 UnicodeString("1,001st"),
1970 UnicodeString("1&001st")
1971 };
1972
1973 double number = 1001;
1974
1975 UnicodeString result;
1976
1977 rbnf.format(number, result);
1978 if (result != expected[0]) {
1979 errln("Format Error - Got: " + result + " Expected: " + expected[0]);
1980 }
1981
1982 result.remove();
1983
1984 /* Set new symbol for testing */
1985 dfs.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, UnicodeString("&"), TRUE);
1986 rbnf.setDecimalFormatSymbols(dfs);
1987
1988 rbnf.format(number, result);
1989 if (result != expected[1]) {
1990 errln("Format Error - Got: " + result + " Expected: " + expected[1]);
1991 }
1992}
1993
1994
46f4442e
A
1995void
1996IntlTestRBNF::doTest(RuleBasedNumberFormat* formatter, const char* const testData[][2], UBool testParsing)
b75a7d8f
A
1997{
1998 // man, error reporting would be easier with printf-style syntax for unicode string and formattable
1999
2000 UErrorCode status = U_ZERO_ERROR;
73c04bcf 2001 DecimalFormatSymbols dfs("en", status);
374ca955 2002 // NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
73c04bcf 2003 DecimalFormat decFmt("#,###.################", dfs, status);
b75a7d8f 2004 if (U_FAILURE(status)) {
729e4ab9 2005 errcheckln(status, "FAIL: could not create NumberFormat - %s", u_errorName(status));
b75a7d8f
A
2006 } else {
2007 for (int i = 0; testData[i][0]; ++i) {
2008 const char* numString = testData[i][0];
2009 const char* expectedWords = testData[i][1];
2010
374ca955 2011 log("[%i] %s = ", i, numString);
b75a7d8f 2012 Formattable expectedNumber;
73c04bcf 2013 decFmt.parse(numString, expectedNumber, status);
b75a7d8f
A
2014 if (U_FAILURE(status)) {
2015 errln("FAIL: decFmt could not parse %s", numString);
2016 break;
2017 } else {
2018 UnicodeString actualString;
2019 FieldPosition pos;
2020 formatter->format(expectedNumber, actualString/* , pos*/, status);
2021 if (U_FAILURE(status)) {
2022 UnicodeString msg = "Fail: formatter could not format ";
73c04bcf 2023 decFmt.format(expectedNumber, msg, status);
b75a7d8f
A
2024 errln(msg);
2025 break;
2026 } else {
46f4442e 2027 UnicodeString expectedString = UnicodeString(expectedWords, -1, US_INV).unescape();
b75a7d8f
A
2028 if (actualString != expectedString) {
2029 UnicodeString msg = "FAIL: check failed for ";
73c04bcf 2030 decFmt.format(expectedNumber, msg, status);
b75a7d8f
A
2031 msg.append(", expected ");
2032 msg.append(expectedString);
2033 msg.append(" but got ");
2034 msg.append(actualString);
2035 errln(msg);
2036 break;
374ca955
A
2037 } else {
2038 logln(actualString);
2039 if (testParsing) {
2040 Formattable parsedNumber;
2041 formatter->parse(actualString, parsedNumber, status);
2042 if (U_FAILURE(status)) {
2043 UnicodeString msg = "FAIL: formatter could not parse ";
b75a7d8f 2044 msg.append(actualString);
374ca955
A
2045 msg.append(" status code: " );
2046 msg.append(u_errorName(status));
b75a7d8f
A
2047 errln(msg);
2048 break;
374ca955
A
2049 } else {
2050 if (parsedNumber != expectedNumber) {
2051 UnicodeString msg = "FAIL: parse failed for ";
2052 msg.append(actualString);
2053 msg.append(", expected ");
73c04bcf 2054 decFmt.format(expectedNumber, msg, status);
374ca955 2055 msg.append(", but got ");
73c04bcf 2056 decFmt.format(parsedNumber, msg, status);
374ca955
A
2057 errln(msg);
2058 break;
2059 }
b75a7d8f
A
2060 }
2061 }
2062 }
2063 }
2064 }
2065 }
b75a7d8f
A
2066 }
2067}
2068
2069void
2070IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat* formatter, const char* testData[][2])
2071{
2072 UErrorCode status = U_ZERO_ERROR;
2073 NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
2074 if (U_FAILURE(status)) {
729e4ab9 2075 errcheckln(status, "FAIL: could not create NumberFormat - %s", u_errorName(status));
b75a7d8f
A
2076 } else {
2077 for (int i = 0; testData[i][0]; ++i) {
2078 const char* spelledNumber = testData[i][0]; // spelled-out number
2079 const char* asciiUSNumber = testData[i][1]; // number as ascii digits formatted for US locale
2080
2081 UnicodeString spelledNumberString = UnicodeString(spelledNumber).unescape();
2082 Formattable actualNumber;
2083 formatter->parse(spelledNumberString, actualNumber, status);
2084 if (U_FAILURE(status)) {
2085 UnicodeString msg = "FAIL: formatter could not parse ";
2086 msg.append(spelledNumberString);
2087 errln(msg);
2088 break;
2089 } else {
2090 // I changed the logic of this test somewhat from Java-- instead of comparing the
2091 // strings, I compare the Formattables. Hmmm, but the Formattables don't compare,
2092 // so change it back.
2093
2094 UnicodeString asciiUSNumberString = asciiUSNumber;
2095 Formattable expectedNumber;
2096 decFmt->parse(asciiUSNumberString, expectedNumber, status);
2097 if (U_FAILURE(status)) {
2098 UnicodeString msg = "FAIL: decFmt could not parse ";
2099 msg.append(asciiUSNumberString);
2100 errln(msg);
2101 break;
2102 } else {
2103 UnicodeString actualNumberString;
2104 UnicodeString expectedNumberString;
2105 decFmt->format(actualNumber, actualNumberString, status);
2106 decFmt->format(expectedNumber, expectedNumberString, status);
2107 if (actualNumberString != expectedNumberString) {
2108 UnicodeString msg = "FAIL: parsing";
2109 msg.append(asciiUSNumberString);
2110 msg.append("\n");
2111 msg.append(" lenient parse failed for ");
2112 msg.append(spelledNumberString);
2113 msg.append(", expected ");
2114 msg.append(expectedNumberString);
2115 msg.append(", but got ");
2116 msg.append(actualNumberString);
2117 errln(msg);
2118 break;
2119 }
2120 }
2121 }
2122 }
2123 delete decFmt;
2124 }
2125}
2126
2127/* U_HAVE_RBNF */
2128#else
2129
2130void
2131IntlTestRBNF::TestRBNFDisabled() {
2132 errln("*** RBNF currently disabled on this platform ***\n");
2133}
2134
2135/* U_HAVE_RBNF */
2136#endif
2137
2138#endif /* #if !UCONFIG_NO_FORMATTING */