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