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