1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2015, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "dcfmapts.h"
#include "unicode/currpinf.h"
#include "unicode/dcfmtsym.h"
#include "unicode/decimfmt.h"
#include "unicode/fmtable.h"
#include "unicode/localpointer.h"
#include "unicode/parseerr.h"
#include "unicode/stringpiece.h"
#include "putilimp.h"
#include "plurrule_impl.h"
#include "number_decimalquantity.h"
#include <stdio.h>
// This is an API test, not a unit test. It doesn't test very many cases, and doesn't
// try to test the full functionality. It just calls each function in the class and
// verifies that it works on a basic level.
void IntlTestDecimalFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
if (exec) logln(UnicodeString("TestSuite DecimalFormatAPI"));
switch (index) {
case 0: name = "DecimalFormat API test";
if (exec) {
logln(UnicodeString("DecimalFormat API test---")); logln(UnicodeString(""));
UErrorCode status = U_ZERO_ERROR;
Locale saveLocale;
Locale::setDefault(Locale::getEnglish(), status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: Could not set default locale, test may not give correct results"));
}
testAPI(/*par*/);
Locale::setDefault(saveLocale, status);
}
break;
case 1: name = "Rounding test";
if(exec) {
logln(UnicodeString("DecimalFormat Rounding test---"));
testRounding(/*par*/);
}
break;
case 2: name = "Test6354";
if(exec) {
logln(UnicodeString("DecimalFormat Rounding Increment test---"));
testRoundingInc(/*par*/);
}
break;
case 3: name = "TestCurrencyPluralInfo";
if(exec) {
logln(UnicodeString("CurrencyPluralInfo API test---"));
TestCurrencyPluralInfo();
}
break;
case 4: name = "TestScale";
if(exec) {
logln(UnicodeString("Scale test---"));
TestScale();
}
break;
case 5: name = "TestFixedDecimal";
if(exec) {
logln(UnicodeString("TestFixedDecimal ---"));
TestFixedDecimal();
}
break;
case 6: name = "TestBadFastpath";
if(exec) {
logln(UnicodeString("TestBadFastpath ---"));
TestBadFastpath();
}
break;
case 7: name = "TestRequiredDecimalPoint";
if(exec) {
logln(UnicodeString("TestRequiredDecimalPoint ---"));
TestRequiredDecimalPoint();
}
break;
case 8: name = "testErrorCode";
if(exec) {
logln(UnicodeString("testErrorCode ---"));
testErrorCode();
}
break;
case 9: name = "testInvalidObject";
if(exec) {
logln(UnicodeString("testInvalidObject ---"));
testInvalidObject();
}
break;
default: name = ""; break;
}
}
/**
* This test checks various generic API methods in DecimalFormat to achieve 100%
* API coverage.
*/
void IntlTestDecimalFormatAPI::testAPI(/*char *par*/)
{
UErrorCode status = U_ZERO_ERROR;
// ======= Test constructors
logln(UnicodeString("Testing DecimalFormat constructors"));
DecimalFormat def(status);
if(U_FAILURE(status)) {
errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
return;
}
// bug 10864
status = U_ZERO_ERROR;
DecimalFormat noGrouping("###0.##", status);
assertEquals("Grouping size should be 0 for no grouping.", 0, noGrouping.getGroupingSize());
noGrouping.setGroupingUsed(true);
assertEquals("Grouping size should still be 0.", 0, noGrouping.getGroupingSize());
// end bug 10864
// bug 13442 comment 14
status = U_ZERO_ERROR;
{
DecimalFormat df("0", {"en", status}, status);
UnicodeString result;
assertEquals("pat 0: ", 0, df.getGroupingSize());
assertEquals("pat 0: ", false, df.isGroupingUsed());
df.setGroupingUsed(false);
assertEquals("pat 0 then disabled: ", 0, df.getGroupingSize());
assertEquals("pat 0 then disabled: ", u"1111", df.format(1111, result.remove()));
df.setGroupingUsed(true);
assertEquals("pat 0 then enabled: ", 0, df.getGroupingSize());
assertEquals("pat 0 then enabled: ", u"1111", df.format(1111, result.remove()));
}
{
DecimalFormat df("#,##0", {"en", status}, status);
UnicodeString result;
assertEquals("pat #,##0: ", 3, df.getGroupingSize());
assertEquals("pat #,##0: ", true, df.isGroupingUsed());
df.setGroupingUsed(false);
assertEquals("pat #,##0 then disabled: ", 3, df.getGroupingSize());
assertEquals("pat #,##0 then disabled: ", u"1111", df.format(1111, result.remove()));
df.setGroupingUsed(true);
assertEquals("pat #,##0 then enabled: ", 3, df.getGroupingSize());
assertEquals("pat #,##0 then enabled: ", u"1,111", df.format(1111, result.remove()));
}
// end bug 13442 comment 14
status = U_ZERO_ERROR;
const UnicodeString pattern("#,##0.# FF");
DecimalFormat pat(pattern, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: Could not create DecimalFormat (pattern)"));
return;
}
status = U_ZERO_ERROR;
DecimalFormatSymbols *symbols = new DecimalFormatSymbols(Locale::getFrench(), status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: Could not create DecimalFormatSymbols (French)"));
return;
}
status = U_ZERO_ERROR;
DecimalFormat cust1(pattern, *symbols, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: Could not create DecimalFormat (pattern, symbols)"));
}
// NOTE: The test where you pass "symbols" as a pointer has to come second-- the DecimalFormat
// object is _adopting_ this object, meaning it's unavailable for use by this test (e.g.,
// to pass to another DecimalFormat) after the call to the DecimalFormat constructor.
// The call above, where we're passing it by reference, doesn't take ownership of the
// symbols object, so we can reuse it here.
status = U_ZERO_ERROR;
DecimalFormat cust2(pattern, symbols, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: Could not create DecimalFormat (pattern, symbols*)"));
}
DecimalFormat copy(pat);
// ======= Test clone(), assignment, and equality
logln(UnicodeString("Testing clone(), assignment and equality operators"));
if( ! (copy == pat) || copy != pat) {
errln(UnicodeString("ERROR: Copy constructor or == failed"));
}
copy = cust1;
if(copy != cust1) {
errln(UnicodeString("ERROR: Assignment (or !=) failed"));
}
Format *clone = def.clone();
if( ! (*clone == def) ) {
errln(UnicodeString("ERROR: Clone() failed"));
}
delete clone;
// ======= Test various format() methods
logln(UnicodeString("Testing various format() methods"));
double d = -10456.0037;
int32_t l = 100000000;
Formattable fD(d);
Formattable fL(l);
UnicodeString res1, res2, res3, res4;
FieldPosition pos1(FieldPosition::DONT_CARE), pos2(FieldPosition::DONT_CARE), pos3(FieldPosition::DONT_CARE), pos4(FieldPosition::DONT_CARE);
res1 = def.format(d, res1, pos1);
logln(UnicodeString("") + static_cast<int32_t>(d) + " formatted to " + res1);
res2 = pat.format(l, res2, pos2);
logln(UnicodeString("") + l + " formatted to " + res2);
status = U_ZERO_ERROR;
res3 = cust1.format(fD, res3, pos3, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: format(Formattable [double]) failed"));
}
logln(UnicodeString("") + static_cast<int32_t>(fD.getDouble()) + " formatted to " + res3);
status = U_ZERO_ERROR;
res4 = cust2.format(fL, res4, pos4, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: format(Formattable [long]) failed"));
}
logln(UnicodeString("") + fL.getLong() + " formatted to " + res4);
// ======= Test parse()
logln(UnicodeString("Testing parse()"));
UnicodeString text("-10,456.0037");
Formattable result1, result2;
ParsePosition pos(0);
UnicodeString patt("#,##0.#");
status = U_ZERO_ERROR;
pat.applyPattern(patt, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: applyPattern() failed"));
}
pat.parse(text, result1, pos);
if(result1.getType() != Formattable::kDouble && result1.getDouble() != d) {
errln(UnicodeString("ERROR: Roundtrip failed (via parse()) for ") + text);
}
logln(text + " parsed into " + static_cast<int32_t>(result1.getDouble()));
status = U_ZERO_ERROR;
pat.parse(text, result2, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: parse() failed"));
}
if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) {
errln(UnicodeString("ERROR: Roundtrip failed (via parse()) for ") + text);
}
logln(text + " parsed into " + static_cast<int32_t>(result2.getDouble()));
// ======= Test getters and setters
logln(UnicodeString("Testing getters and setters"));
const DecimalFormatSymbols *syms = pat.getDecimalFormatSymbols();
DecimalFormatSymbols *newSyms = new DecimalFormatSymbols(*syms);
def.setDecimalFormatSymbols(*newSyms);
def.adoptDecimalFormatSymbols(newSyms); // don't use newSyms after this
if( *(pat.getDecimalFormatSymbols()) != *(def.getDecimalFormatSymbols())) {
errln(UnicodeString("ERROR: adopt or set DecimalFormatSymbols() failed"));
}
UnicodeString posPrefix;
pat.setPositivePrefix("+");
posPrefix = pat.getPositivePrefix(posPrefix);
logln(UnicodeString("Positive prefix (should be +): ") + posPrefix);
if(posPrefix != "+") {
errln(UnicodeString("ERROR: setPositivePrefix() failed"));
}
UnicodeString negPrefix;
pat.setNegativePrefix("-");
negPrefix = pat.getNegativePrefix(negPrefix);
logln(UnicodeString("Negative prefix (should be -): ") + negPrefix);
if(negPrefix != "-") {
errln(UnicodeString("ERROR: setNegativePrefix() failed"));
}
UnicodeString posSuffix;
pat.setPositiveSuffix("_");
posSuffix = pat.getPositiveSuffix(posSuffix);
logln(UnicodeString("Positive suffix (should be _): ") + posSuffix);
if(posSuffix != "_") {
errln(UnicodeString("ERROR: setPositiveSuffix() failed"));
}
UnicodeString negSuffix;
pat.setNegativeSuffix("~");
negSuffix = pat.getNegativeSuffix(negSuffix);
logln(UnicodeString("Negative suffix (should be ~): ") + negSuffix);
if(negSuffix != "~") {
errln(UnicodeString("ERROR: setNegativeSuffix() failed"));
}
int32_t multiplier = 0;
pat.setMultiplier(8);
multiplier = pat.getMultiplier();
logln(UnicodeString("Multiplier (should be 8): ") + multiplier);
if(multiplier != 8) {
errln(UnicodeString("ERROR: setMultiplier() failed"));
}
int32_t groupingSize = 0;
pat.setGroupingSize(2);
groupingSize = pat.getGroupingSize();
logln(UnicodeString("Grouping size (should be 2): ") + groupingSize);
if(groupingSize != 2) {
errln(UnicodeString("ERROR: setGroupingSize() failed"));
}
pat.setDecimalSeparatorAlwaysShown(true);
UBool tf = pat.isDecimalSeparatorAlwaysShown();
logln(UnicodeString("DecimalSeparatorIsAlwaysShown (should be true) is ") + UnicodeString(tf ? "true" : "false"));
if(tf != true) {
errln(UnicodeString("ERROR: setDecimalSeparatorAlwaysShown() failed"));
}
// Added by Ken Liu testing set/isExponentSignAlwaysShown
pat.setExponentSignAlwaysShown(true);
UBool esas = pat.isExponentSignAlwaysShown();
logln(UnicodeString("ExponentSignAlwaysShown (should be true) is ") + UnicodeString(esas ? "true" : "false"));
if(esas != true) {
errln(UnicodeString("ERROR: ExponentSignAlwaysShown() failed"));
}
// Added by Ken Liu testing set/isScientificNotation
pat.setScientificNotation(true);
UBool sn = pat.isScientificNotation();
logln(UnicodeString("isScientificNotation (should be true) is ") + UnicodeString(sn ? "true" : "false"));
if(sn != true) {
errln(UnicodeString("ERROR: setScientificNotation() failed"));
}
// Added by Ken Liu testing set/getMinimumExponentDigits
int8_t MinimumExponentDigits = 0;
pat.setMinimumExponentDigits(2);
MinimumExponentDigits = pat.getMinimumExponentDigits();
logln(UnicodeString("MinimumExponentDigits (should be 2) is ") + MinimumExponentDigits);
if(MinimumExponentDigits != 2) {
errln(UnicodeString("ERROR: setMinimumExponentDigits() failed"));
}
// Added by Ken Liu testing set/getRoundingIncrement
double RoundingIncrement = 0.0;
pat.setRoundingIncrement(2.0);
RoundingIncrement = pat.getRoundingIncrement();
logln(UnicodeString("RoundingIncrement (should be 2.0) is ") + RoundingIncrement);
if(RoundingIncrement != 2.0) {
errln(UnicodeString("ERROR: setRoundingIncrement() failed"));
}
//end of Ken's Adding
UnicodeString funkyPat;
funkyPat = pat.toPattern(funkyPat);
logln(UnicodeString("Pattern is ") + funkyPat);
UnicodeString locPat;
locPat = pat.toLocalizedPattern(locPat);
logln(UnicodeString("Localized pattern is ") + locPat);
// ======= Test applyPattern()
logln(UnicodeString("Testing applyPattern()"));
pat = DecimalFormat(status); // reset
UnicodeString p1("#,##0.0#;(#,##0.0#)");
logln(UnicodeString("Applying pattern ") + p1);
status = U_ZERO_ERROR;
pat.applyPattern(p1, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: applyPattern() failed with ") + static_cast<int32_t>(status));
}
UnicodeString s2;
s2 = pat.toPattern(s2);
logln(UnicodeString("Extracted pattern is ") + s2);
assertEquals("toPattern() result did not match pattern applied", p1, s2);
if(pat.getSecondaryGroupingSize() != 0) {
errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat.getSecondaryGroupingSize());
}
if(pat.getGroupingSize() != 3) {
errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize());
}
UnicodeString p2("#,##,##0.0# FF;(#,##,##0.0# FF)");
logln(UnicodeString("Applying pattern ") + p2);
status = U_ZERO_ERROR;
pat.applyLocalizedPattern(p2, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: applyPattern() failed with ") + static_cast<int32_t>(status));
}
UnicodeString s3;
s3 = pat.toLocalizedPattern(s3);
logln(UnicodeString("Extracted pattern is ") + s3);
assertEquals("toLocalizedPattern() result did not match pattern applied", p2, s3);
status = U_ZERO_ERROR;
UParseError pe;
pat.applyLocalizedPattern(p2, pe, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: applyPattern((with ParseError)) failed with ") + static_cast<int32_t>(status));
}
UnicodeString s4;
s4 = pat.toLocalizedPattern(s3);
logln(UnicodeString("Extracted pattern is ") + s4);
assertEquals("toLocalizedPattern(with ParseErr) result did not match pattern applied", p2, s4);
if(pat.getSecondaryGroupingSize() != 2) {
errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat.getSecondaryGroupingSize());
}
if(pat.getGroupingSize() != 3) {
errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize());
}
// ======= Test getStaticClassID()
logln(UnicodeString("Testing getStaticClassID()"));
status = U_ZERO_ERROR;
NumberFormat *test = new DecimalFormat(status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: Couldn't create a DecimalFormat"));
}
if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
errln(UnicodeString("ERROR: getDynamicClassID() didn't return the expected value"));
}
delete test;
}
void IntlTestDecimalFormatAPI::TestCurrencyPluralInfo(){
UErrorCode status = U_ZERO_ERROR;
LocalPointer<CurrencyPluralInfo>cpi(new CurrencyPluralInfo(status), status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: CurrencyPluralInfo(UErrorCode) could not be created"));
return;
}
CurrencyPluralInfo cpi1 = *cpi;
if(cpi->getDynamicClassID() != CurrencyPluralInfo::getStaticClassID()){
errln(UnicodeString("ERROR: CurrencyPluralInfo::getDynamicClassID() didn't return the expected value"));
}
cpi->setCurrencyPluralPattern("","",status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: CurrencyPluralInfo::setCurrencyPluralPattern"));
}
cpi->setLocale(Locale::getCanada(), status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: CurrencyPluralInfo::setLocale"));
}
cpi->setPluralRules("",status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: CurrencyPluralInfo::setPluralRules"));
}
LocalPointer<DecimalFormat>df(new DecimalFormat(status));
if(U_FAILURE(status)) {
errcheckln(status, "ERROR: Could not create DecimalFormat - %s", u_errorName(status));
return;
}
df->adoptCurrencyPluralInfo(cpi.orphan());
df->getCurrencyPluralInfo();
df->setCurrencyPluralInfo(cpi1);
}
void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
{
UErrorCode status = U_ZERO_ERROR;
double Roundingnumber = 2.55;
double Roundingnumber1 = -2.55;
//+2.55 results -2.55 results
double result[]={ 3.0, -2.0, // kRoundCeiling 0,
2.0, -3.0, // kRoundFloor 1,
2.0, -2.0, // kRoundDown 2,
3.0, -3.0, // kRoundUp 3,
3.0, -3.0, // kRoundHalfEven 4,
3.0, -3.0, // kRoundHalfDown 5,
3.0, -3.0 // kRoundHalfUp 6
};
DecimalFormat pat(status);
if(U_FAILURE(status)) {
errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
return;
}
uint16_t mode;
uint16_t i=0;
UnicodeString message;
UnicodeString resultStr;
for(mode=0;mode < 7;mode++){
pat.setRoundingMode(static_cast<DecimalFormat::ERoundingMode>(mode));
if (pat.getRoundingMode() != static_cast<DecimalFormat::ERoundingMode>(mode)) {
errln(UnicodeString("SetRoundingMode or GetRoundingMode failed for mode=") + mode);
}
//for +2.55 with RoundingIncrement=1.0
pat.setRoundingIncrement(1.0);
pat.format(Roundingnumber, resultStr);
message = UnicodeString("round(") + Roundingnumber + UnicodeString(",") + mode + UnicodeString(",false) with RoundingIncrement=1.0==>");
verify(message, resultStr, result[i++]);
message.remove();
resultStr.remove();
//for -2.55 with RoundingIncrement=1.0
pat.format(Roundingnumber1, resultStr);
message = UnicodeString("round(") + Roundingnumber1 + UnicodeString(",") + mode + UnicodeString(",false) with RoundingIncrement=1.0==>");
verify(message, resultStr, result[i++]);
message.remove();
resultStr.remove();
}
}
void IntlTestDecimalFormatAPI::verify(const UnicodeString& message, const UnicodeString& got, double expected){
logln(UnicodeString(message) + got + UnicodeString(" Expected : ") + expected);
UnicodeString expectedStr("");
expectedStr=expectedStr + expected;
if(got != expectedStr ) {
errln(UnicodeString("ERROR: ") + message + got + UnicodeString(" Expected : ") + expectedStr);
}
}
void IntlTestDecimalFormatAPI::verifyString(const UnicodeString& message, const UnicodeString& got, UnicodeString& expected){
logln(UnicodeString(message) + got + UnicodeString(" Expected : ") + expected);
if(got != expected ) {
errln(UnicodeString("ERROR: ") + message + got + UnicodeString(" Expected : ") + expected);
}
}
void IntlTestDecimalFormatAPI::testRoundingInc(/*char *par*/)
{
UErrorCode status = U_ZERO_ERROR;
DecimalFormat pat(UnicodeString("#,##0.00"),status);
if(U_FAILURE(status)) {
errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
return;
}
// get default rounding increment
double roundingInc = pat.getRoundingIncrement();
if (roundingInc != 0.0) {
errln(UnicodeString("ERROR: Rounding increment not zero"));
return;
}
// With rounding now being handled by decNumber, we no longer
// set a rounding increment to enable non-default mode rounding,
// checking of which was the original point of this test.
// set rounding mode with zero increment. Rounding
// increment should not be set by this operation
pat.setRoundingMode(static_cast<DecimalFormat::ERoundingMode>(0));
roundingInc = pat.getRoundingIncrement();
if (roundingInc != 0.0) {
errln(UnicodeString("ERROR: Rounding increment not zero after setRoundingMode"));
return;
}
}
void IntlTestDecimalFormatAPI::TestScale()
{
typedef struct TestData {
double inputValue;
int inputScale;
const char *expectedOutput;
} TestData;
static TestData testData[] = {
{ 100.0, 3, "100,000" },
{ 10034.0, -2, "100.34" },
{ 0.86, -3, "0.0009" },
{ -0.000455, 1, "-0%" },
{ -0.000555, 1, "-1%" },
{ 0.000455, 1, "0%" },
{ 0.000555, 1, "1%" },
};
UErrorCode status = U_ZERO_ERROR;
DecimalFormat pat(status);
if(U_FAILURE(status)) {
errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
return;
}
UnicodeString message;
UnicodeString resultStr;
UnicodeString exp;
UnicodeString percentPattern("#,##0%");
pat.setMaximumFractionDigits(4);
for(int32_t i=0; i < UPRV_LENGTHOF(testData); i++) {
if ( i > 2 ) {
pat.applyPattern(percentPattern,status);
}
// Test both the attribute and the setter
if (i % 2 == 0) {
pat.setAttribute(UNUM_SCALE, testData[i].inputScale,status);
assertEquals("", testData[i].inputScale, pat.getMultiplierScale());
} else {
pat.setMultiplierScale(testData[i].inputScale);
assertEquals("", testData[i].inputScale, pat.getAttribute(UNUM_SCALE, status));
}
pat.format(testData[i].inputValue, resultStr);
message = UnicodeString("Unexpected output for ") + testData[i].inputValue + UnicodeString(" and scale ") +
testData[i].inputScale + UnicodeString(". Got: ");
exp = testData[i].expectedOutput;
verifyString(message, resultStr, exp);
message.remove();
resultStr.remove();
exp.remove();
}
}
#define ASSERT_EQUAL(expect, actual) UPRV_BLOCK_MACRO_BEGIN { \
/* ICU-20080: Use temporary variables to avoid strange compiler behaviour \
(with the nice side-effect of avoiding repeated function calls too). */ \
auto lhs = (expect); \
auto rhs = (actual); \
char tmp[200]; \
snprintf(tmp, sizeof(tmp), "(%g==%g)", (double)lhs, (double)rhs); \
assertTrue(tmp, (lhs==rhs), false, true, __FILE__, __LINE__); \
} UPRV_BLOCK_MACRO_END
#if defined(_MSC_VER)
// Ignore the noisy warning 4805 (comparisons between int and bool) in the function below as we use the ICU true/false macros
// which are int values, whereas some of the DecimalQuantity methods return C++ bools.
#pragma warning(push)
#pragma warning(disable: 4805)
#endif
void IntlTestDecimalFormatAPI::TestFixedDecimal() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<DecimalFormat> df(new DecimalFormat("###", status), status);
assertSuccess(WHERE, status);
if (status == U_MISSING_RESOURCE_ERROR) {
return;
}
number::impl::DecimalQuantity fd;
df->formatToDecimalQuantity(44, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(44, fd.getPluralOperand(PLURAL_OPERAND_N));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(false, fd.isNegative());
df->formatToDecimalQuantity(-44, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(44, fd.getPluralOperand(PLURAL_OPERAND_N));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(true, fd.isNegative());
df.adoptInsteadAndCheckErrorCode(new DecimalFormat("###.00##", status), status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(123.456, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(3, fd.getPluralOperand(PLURAL_OPERAND_V)); // v
ASSERT_EQUAL(456, fd.getPluralOperand(PLURAL_OPERAND_F)); // f
ASSERT_EQUAL(456, fd.getPluralOperand(PLURAL_OPERAND_T)); // t
ASSERT_EQUAL(123, fd.getPluralOperand(PLURAL_OPERAND_I)); // i
ASSERT_EQUAL(123.456, fd.getPluralOperand(PLURAL_OPERAND_N)); // n
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df->formatToDecimalQuantity(-123.456, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(3, fd.getPluralOperand(PLURAL_OPERAND_V)); // v
ASSERT_EQUAL(456, fd.getPluralOperand(PLURAL_OPERAND_F)); // f
ASSERT_EQUAL(456, fd.getPluralOperand(PLURAL_OPERAND_T)); // t
ASSERT_EQUAL(123, fd.getPluralOperand(PLURAL_OPERAND_I)); // i
ASSERT_EQUAL(123.456, fd.getPluralOperand(PLURAL_OPERAND_N)); // n
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(true, fd.isNegative());
// test max int digits
df->setMaximumIntegerDigits(2);
df->formatToDecimalQuantity(123.456, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(3, fd.getPluralOperand(PLURAL_OPERAND_V)); // v
ASSERT_EQUAL(456, fd.getPluralOperand(PLURAL_OPERAND_F)); // f
ASSERT_EQUAL(456, fd.getPluralOperand(PLURAL_OPERAND_T)); // t
ASSERT_EQUAL(23, fd.getPluralOperand(PLURAL_OPERAND_I)); // i
ASSERT_EQUAL(23.456, fd.getPluralOperand(PLURAL_OPERAND_N)); // n
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df->formatToDecimalQuantity(-123.456, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(3, fd.getPluralOperand(PLURAL_OPERAND_V)); // v
ASSERT_EQUAL(456, fd.getPluralOperand(PLURAL_OPERAND_F)); // f
ASSERT_EQUAL(456, fd.getPluralOperand(PLURAL_OPERAND_T)); // t
ASSERT_EQUAL(23, fd.getPluralOperand(PLURAL_OPERAND_I)); // i
ASSERT_EQUAL(23.456, fd.getPluralOperand(PLURAL_OPERAND_N)); // n
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(true, fd.isNegative());
// test max fraction digits
df->setMaximumIntegerDigits(2000000000);
df->setMaximumFractionDigits(2);
df->formatToDecimalQuantity(123.456, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V)); // v
ASSERT_EQUAL(46, fd.getPluralOperand(PLURAL_OPERAND_F)); // f
ASSERT_EQUAL(46, fd.getPluralOperand(PLURAL_OPERAND_T)); // t
ASSERT_EQUAL(123, fd.getPluralOperand(PLURAL_OPERAND_I)); // i
ASSERT_EQUAL(123.46, fd.getPluralOperand(PLURAL_OPERAND_N)); // n
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df->formatToDecimalQuantity(-123.456, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V)); // v
ASSERT_EQUAL(46, fd.getPluralOperand(PLURAL_OPERAND_F)); // f
ASSERT_EQUAL(46, fd.getPluralOperand(PLURAL_OPERAND_T)); // t
ASSERT_EQUAL(123, fd.getPluralOperand(PLURAL_OPERAND_I)); // i
ASSERT_EQUAL(123.46, fd.getPluralOperand(PLURAL_OPERAND_N)); // n
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(true, fd.isNegative());
// test esoteric rounding
df->setMaximumFractionDigits(6);
df->setRoundingIncrement(7.3);
df->formatToDecimalQuantity(30.0, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V)); // v
ASSERT_EQUAL(20, fd.getPluralOperand(PLURAL_OPERAND_F)); // f
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_T)); // t
ASSERT_EQUAL(29, fd.getPluralOperand(PLURAL_OPERAND_I)); // i
ASSERT_EQUAL(29.2, fd.getPluralOperand(PLURAL_OPERAND_N)); // n
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df->formatToDecimalQuantity(-30.0, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V)); // v
ASSERT_EQUAL(20, fd.getPluralOperand(PLURAL_OPERAND_F)); // f
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_T)); // t
ASSERT_EQUAL(29, fd.getPluralOperand(PLURAL_OPERAND_I)); // i
ASSERT_EQUAL(29.2, fd.getPluralOperand(PLURAL_OPERAND_N)); // n
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(true, fd.isNegative());
df.adoptInsteadAndCheckErrorCode(new DecimalFormat("###", status), status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(123.456, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(123, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(true, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df.adoptInsteadAndCheckErrorCode(new DecimalFormat("###.0", status), status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(123.01, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(1, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(123, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(true, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df.adoptInsteadAndCheckErrorCode(new DecimalFormat("###.0", status), status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(123.06, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(1, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(1, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(1, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(123, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df.adoptInsteadAndCheckErrorCode(new DecimalFormat("@@@@@", status), status); // Significant Digits
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(123, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(123, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(true, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df.adoptInsteadAndCheckErrorCode(new DecimalFormat("@@@@@", status), status); // Significant Digits
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(1.23, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(4, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(2300, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(23, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(1, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
df->formatToDecimalQuantity(uprv_getInfinity(), fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(true, fd.isNaN() || fd.isInfinite());
df->formatToDecimalQuantity(0.0, fd, status);
ASSERT_EQUAL(false, fd.isNaN() || fd.isInfinite());
df->formatToDecimalQuantity(uprv_getNaN(), fd, status);
ASSERT_EQUAL(true, fd.isNaN() || fd.isInfinite());
assertSuccess(WHERE, status);
// Test Big Decimal input.
// 22 digits before and after decimal, will exceed the precision of a double
// and force DecimalFormat::getFixedDecimal() to work with a digit list.
df.adoptInsteadAndCheckErrorCode(
new DecimalFormat("#####################0.00####################", status), status);
assertSuccess(WHERE, status);
Formattable fable("12.34", status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(34, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(34, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(12, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
fable.setDecimalNumber("12.3456789012345678900123456789", status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(22, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(3456789012345678900LL, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(34567890123456789LL, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(12, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
// On field overflow, Integer part is truncated on the left, fraction part on the right.
fable.setDecimalNumber("123456789012345678901234567890.123456789012345678901234567890", status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(22, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(1234567890123456789LL, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(1234567890123456789LL, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(345678901234567890LL, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
// Digits way to the right of the decimal but within the format's precision aren't truncated
fable.setDecimalNumber("1.0000000000000000000012", status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(22, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(12, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(12, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(1, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
// Digits beyond the precision of the format are rounded away
fable.setDecimalNumber("1.000000000000000000000012", status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(1, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(true, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
// Negative numbers come through
fable.setDecimalNumber("-1.0000000000000000000012", status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(22, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(12, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(12, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(1, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(true, fd.isNegative());
// MinFractionDigits from format larger than from number.
fable.setDecimalNumber("1000000000000000000000.3", status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(30, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(3, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
fable.setDecimalNumber("1000000000000000050000.3", status);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(30, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(3, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(50000LL, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(false, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
// Test some int64_t values that are out of the range of a double
fable.setInt64(4503599627370496LL);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(4503599627370496LL, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(true, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
fable.setInt64(4503599627370497LL);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_T));
ASSERT_EQUAL(4503599627370497LL, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(true, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
fable.setInt64(9223372036854775807LL);
assertSuccess(WHERE, status);
df->formatToDecimalQuantity(fable, fd, status);
assertSuccess(WHERE, status);
ASSERT_EQUAL(2, fd.getPluralOperand(PLURAL_OPERAND_V));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_F));
ASSERT_EQUAL(0, fd.getPluralOperand(PLURAL_OPERAND_T));
// note: going through DigitList path to FixedDecimal, which is trimming
// int64_t fields to 18 digits. See ticket Ticket #10374
ASSERT_EQUAL(223372036854775807LL, fd.getPluralOperand(PLURAL_OPERAND_I));
ASSERT_EQUAL(true, fd.hasIntegerValue());
ASSERT_EQUAL(false, fd.isNegative());
}
#if defined(_MSC_VER)
// Re-enable 4805 warnings (comparisons between int and bool).
#pragma warning(pop)
#endif
void IntlTestDecimalFormatAPI::TestBadFastpath() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<DecimalFormat> df(new DecimalFormat("###", status), status);
if (U_FAILURE(status)) {
dataerrln("Error creating new DecimalFormat - %s", u_errorName(status));
return;
}
UnicodeString fmt;
fmt.remove();
assertEquals("Format 1234", "1234", df->format(static_cast<int32_t>(1234), fmt));
df->setGroupingUsed(false);
fmt.remove();
assertEquals("Format 1234", "1234", df->format(static_cast<int32_t>(1234), fmt));
df->setGroupingUsed(true);
df->setGroupingSize(3);
fmt.remove();
assertEquals("Format 1234 w/ grouping", "1,234", df->format(static_cast<int32_t>(1234), fmt));
}
void IntlTestDecimalFormatAPI::TestRequiredDecimalPoint() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString text("99");
Formattable result1;
UnicodeString pat1("##.0000");
UnicodeString pat2("00.0");
LocalPointer<DecimalFormat> df(new DecimalFormat(pat1, status), status);
if (U_FAILURE(status)) {
dataerrln("Error creating new DecimalFormat - %s", u_errorName(status));
return;
}
status = U_ZERO_ERROR;
df->applyPattern(pat1, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: applyPattern() failed"));
}
df->parse(text, result1, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: parse() failed"));
}
df->setDecimalPatternMatchRequired(true);
df->parse(text, result1, status);
if(U_SUCCESS(status)) {
errln(UnicodeString("ERROR: unexpected parse()"));
}
status = U_ZERO_ERROR;
df->applyPattern(pat2, status);
df->setDecimalPatternMatchRequired(false);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: applyPattern(2) failed"));
}
df->parse(text, result1, status);
if(U_FAILURE(status)) {
errln(UnicodeString("ERROR: parse(2) failed - ") + u_errorName(status));
}
df->setDecimalPatternMatchRequired(true);
df->parse(text, result1, status);
if(U_SUCCESS(status)) {
errln(UnicodeString("ERROR: unexpected parse(2)"));
}
}
void IntlTestDecimalFormatAPI::testErrorCode() {
// Try each DecimalFormat constructor with an errorCode set on input,
// Verify no crashes or leaks, and that the errorCode is not altered.
UErrorCode status = U_ZERO_ERROR;
const UnicodeString pattern(u"0.###E0");
UParseError pe;
DecimalFormatSymbols symbols(Locale::getUS(), status);
assertSuccess(WHERE, status);
{
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat df(status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
}
{
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat df(pattern, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
}
{
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat df(pattern, new DecimalFormatSymbols(symbols), status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
}
{
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat df(pattern, new DecimalFormatSymbols(symbols), UNUM_DECIMAL_COMPACT_LONG, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
}
{
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat df(pattern, new DecimalFormatSymbols(symbols), pe, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
}
{
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat df(pattern, symbols ,status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
}
// Try each DecimalFormat method with an error code parameter, verifying that
// an input error is not altered, and that no segmentation faults occur.
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat dfBogus(status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_ZERO_ERROR;
DecimalFormat dfGood(pattern, new DecimalFormatSymbols(symbols), status);
assertSuccess(WHERE, status);
for (DecimalFormat *df: {&dfBogus, &dfGood}) {
status = U_INTERNAL_PROGRAM_ERROR;
df->setAttribute(UNUM_PARSE_INT_ONLY, 0, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->getAttribute(UNUM_MAX_FRACTION_DIGITS, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
UnicodeString dest;
FieldPosition fp;
df->format(1.2, dest, fp, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->format(1.2, dest, nullptr, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->format(static_cast<int32_t>(666), dest, nullptr, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->format(static_cast<int64_t>(666), dest, nullptr, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->format(StringPiece("3.1415926535897932384626"), dest, nullptr, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->applyPattern(pattern, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->applyLocalizedPattern(pattern, pe, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->applyLocalizedPattern(pattern, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->setCurrency(u"USD", status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
df->setCurrencyUsage(UCURR_USAGE_CASH, &status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
}
}
void IntlTestDecimalFormatAPI::testInvalidObject() {
{
UErrorCode status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat dfBogus(status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_ZERO_ERROR;
DecimalFormat dfGood(status);
assertSuccess(WHERE, status);
// An invalid object should not be equal to a valid object.
// This also tests that no segmentation fault occurs in the comparison operator due
// to any dangling/nullptr pointers. (ICU-20381).
assertTrue(WHERE, dfGood != dfBogus);
status = U_MEMORY_ALLOCATION_ERROR;
DecimalFormat dfBogus2(status);
assertEquals(WHERE, U_MEMORY_ALLOCATION_ERROR, status);
// Two invalid objects should not be equal.
// (Also verify that nullptr isn't t dereferenced in the comparison operator.)
assertTrue(WHERE, dfBogus != dfBogus2);
// Verify the comparison operator works for two valid objects.
status = U_ZERO_ERROR;
DecimalFormat dfGood2(status);
assertSuccess(WHERE, status);
assertTrue(WHERE, dfGood == dfGood2);
// Verify that the assignment operator sets the object to an invalid state, and
// that no segmentation fault occurs due to any dangling/nullptr pointers.
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat dfAssignmentBogus = DecimalFormat(status);
// Verify comparison for the assigned object.
assertTrue(WHERE, dfAssignmentBogus != dfGood);
assertTrue(WHERE, dfAssignmentBogus != dfGood2);
assertTrue(WHERE, dfAssignmentBogus != dfBogus);
// Verify that cloning our original invalid object gives nullptr.
auto* dfBogusClone = dfBogus.clone();
assertTrue(WHERE, dfBogusClone == nullptr);
// Verify that cloning our assigned invalid object gives nullptr.
auto* dfBogusClone2 = dfAssignmentBogus.clone();
assertTrue(WHERE, dfBogusClone2 == nullptr);
// Verify copy constructing from an invalid object is also invalid.
DecimalFormat dfCopy(dfBogus);
assertTrue(WHERE, dfCopy != dfGood);
assertTrue(WHERE, dfCopy != dfGood2);
assertTrue(WHERE, dfCopy != dfBogus);
DecimalFormat dfCopyAssign = dfBogus;
assertTrue(WHERE, dfCopyAssign != dfGood);
assertTrue(WHERE, dfCopyAssign != dfGood2);
assertTrue(WHERE, dfCopyAssign != dfBogus);
auto* dfBogusCopyClone1 = dfCopy.clone();
auto* dfBogusCopyClone2 = dfCopyAssign.clone();
assertTrue(WHERE, dfBogusCopyClone1 == nullptr);
assertTrue(WHERE, dfBogusCopyClone2 == nullptr);
}
{
// Try each DecimalFormat class method that lacks an error code parameter, verifying
// we don't crash (segmentation fault) on invalid objects.
UErrorCode status = U_ZERO_ERROR;
const UnicodeString pattern(u"0.###E0");
UParseError pe;
DecimalFormatSymbols symbols(Locale::getUS(), status);
assertSuccess(WHERE, status);
CurrencyPluralInfo currencyPI(status);
assertSuccess(WHERE, status);
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat dfBogus1(status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat dfBogus2(pattern, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat dfBogus3(pattern, new DecimalFormatSymbols(symbols), status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat dfBogus4(pattern, new DecimalFormatSymbols(symbols), UNumberFormatStyle::UNUM_CURRENCY, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
status = U_INTERNAL_PROGRAM_ERROR;
DecimalFormat dfBogus5(pattern, new DecimalFormatSymbols(symbols), pe, status);
assertEquals(WHERE, U_INTERNAL_PROGRAM_ERROR, status);
for (DecimalFormat *df : {&dfBogus1, &dfBogus2, &dfBogus3, &dfBogus4, &dfBogus5})
{
df->setGroupingUsed(true);
df->setParseIntegerOnly(false);
df->setLenient(true);
auto* dfClone = df->clone();
assertTrue(WHERE, dfClone == nullptr);
UnicodeString dest;
FieldPosition fp;
df->format(1.2, dest, fp);
df->format(static_cast<int32_t>(1234), dest, fp);
df->format(static_cast<int64_t>(1234), dest, fp);
UnicodeString text("-1,234.00");
Formattable result;
ParsePosition pos(0);
df->parse(text, result, pos);
CurrencyAmount* ca = df->parseCurrency(text, pos);
assertTrue(WHERE, ca == nullptr);
const DecimalFormatSymbols* dfs = df->getDecimalFormatSymbols();
assertTrue(WHERE, dfs == nullptr);
df->adoptDecimalFormatSymbols(nullptr);
df->setDecimalFormatSymbols(symbols);
const CurrencyPluralInfo* cpi = df->getCurrencyPluralInfo();
assertTrue(WHERE, cpi == nullptr);
df->adoptCurrencyPluralInfo(nullptr);
df->setCurrencyPluralInfo(currencyPI);
UnicodeString prefix("-123");
df->getPositivePrefix(dest);
df->setPositivePrefix(prefix);
df->getNegativePrefix(dest);
df->setNegativePrefix(prefix);
df->getPositiveSuffix(dest);
df->setPositiveSuffix(prefix);
df->getNegativeSuffix(dest);
df->setNegativeSuffix(prefix);
df->isSignAlwaysShown();
df->setSignAlwaysShown(true);
df->getMultiplier();
df->setMultiplier(10);
df->getMultiplierScale();
df->setMultiplierScale(2);
df->getRoundingIncrement();
df->setRoundingIncrement(1.2);
df->getRoundingMode();
df->setRoundingMode(DecimalFormat::ERoundingMode::kRoundDown);
df->getFormatWidth();
df->setFormatWidth(0);
UnicodeString pad(" ");
df->getPadCharacterString();
df->setPadCharacter(pad);
df->getPadPosition();
df->setPadPosition(DecimalFormat::EPadPosition::kPadBeforePrefix);
df->isScientificNotation();
df->setScientificNotation(false);
df->getMinimumExponentDigits();
df->setMinimumExponentDigits(1);
df->isExponentSignAlwaysShown();
df->setExponentSignAlwaysShown(true);
df->getGroupingSize();
df->setGroupingSize(3);
df->getSecondaryGroupingSize();
df->setSecondaryGroupingSize(-1);
df->getMinimumGroupingDigits();
df->setMinimumGroupingDigits(-1);
df->isDecimalSeparatorAlwaysShown();
df->setDecimalSeparatorAlwaysShown(true);
df->isDecimalPatternMatchRequired();
df->setDecimalPatternMatchRequired(false);
df->isParseNoExponent();
df->setParseNoExponent(true);
df->isParseCaseSensitive();
df->setParseCaseSensitive(false);
df->isFormatFailIfMoreThanMaxDigits();
df->setFormatFailIfMoreThanMaxDigits(true);
df->toPattern(dest);
df->toLocalizedPattern(dest);
df->setMaximumIntegerDigits(10);
df->setMinimumIntegerDigits(0);
df->setMaximumFractionDigits(2);
df->setMinimumFractionDigits(0);
df->getMinimumSignificantDigits();
df->setMinimumSignificantDigits(0);
df->getMaximumSignificantDigits();
df->setMaximumSignificantDigits(5);
df->areSignificantDigitsUsed();
df->setSignificantDigitsUsed(true);
df->setCurrency(u"USD");
df->getCurrencyUsage();
const number::LocalizedNumberFormatter* lnf = df->toNumberFormatter(status);
assertEquals("toNumberFormatter should return nullptr",
(int64_t) nullptr, (int64_t) lnf);
// Should not crash when chaining to error code enabled methods on the LNF
#if !defined(__clang__)
// ubsan does not like the following. I have not yet find a good way to do run
// time check of ubsan, so I just skip the following test on clang for now
lnf->formatInt(1, status);
lnf->formatDouble(1.0, status);
lnf->formatDecimal("1", status);
lnf->toFormat(status);
lnf->toSkeleton(status);
lnf->copyErrorTo(status);
#endif
}
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|