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