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 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2007-2014, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************************
* File PLURULTS.cpp
*
********************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "unicode/localpointer.h"
#include "unicode/plurrule.h"
#include "unicode/stringpiece.h"
#include "unicode/numberformatter.h"
#include "unicode/numberrangeformatter.h"
#include "cmemory.h"
#include "cstr.h"
#include "plurrule_impl.h"
#include "plurults.h"
#include "uhash.h"
#include "number_decimalquantity.h"
using icu::number::impl::DecimalQuantity;
using namespace icu::number;
void setupResult(const int32_t testSource[], char result[], int32_t* max);
UBool checkEqual(const PluralRules &test, char *result, int32_t max);
UBool testEquality(const PluralRules &test);
// 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 PluralRulesTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
if (exec) logln("TestSuite PluralRulesAPI");
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(testAPI);
// TESTCASE_AUTO(testGetUniqueKeywordValue);
TESTCASE_AUTO(testGetSamples);
TESTCASE_AUTO(testGetDecimalQuantitySamples);
TESTCASE_AUTO(testGetOrAddSamplesFromString);
TESTCASE_AUTO(testGetOrAddSamplesFromStringCompactNotation);
TESTCASE_AUTO(testSamplesWithExponent);
TESTCASE_AUTO(testSamplesWithCompactNotation);
TESTCASE_AUTO(testWithin);
TESTCASE_AUTO(testGetAllKeywordValues);
TESTCASE_AUTO(testScientificPluralKeyword);
TESTCASE_AUTO(testCompactDecimalPluralKeyword);
TESTCASE_AUTO(testDoubleValue);
TESTCASE_AUTO(testLongValue);
TESTCASE_AUTO(testOrdinal);
TESTCASE_AUTO(testSelect);
TESTCASE_AUTO(testSelectRange);
TESTCASE_AUTO(testAvailableLocales);
TESTCASE_AUTO(testParseErrors);
TESTCASE_AUTO(testFixedDecimal);
TESTCASE_AUTO(testSelectTrailingZeros);
TESTCASE_AUTO(testLocaleExtension);
TESTCASE_AUTO(testDoubleEqualSign);
TESTCASE_AUTO(test22638LongNumberValue);
TESTCASE_AUTO_END;
}
// Quick and dirty class for putting UnicodeStrings in char * messages.
// TODO: something like this should be generally available.
class US {
private:
char *buf;
public:
US(const UnicodeString &us) {
int32_t bufLen = us.extract(static_cast<int32_t>(0), us.length(), (char*)nullptr, static_cast<uint32_t>(0)) + 1;
buf = static_cast<char*>(uprv_malloc(bufLen));
us.extract(0, us.length(), buf, bufLen); }
const char *cstr() {return buf;}
~US() { uprv_free(buf);}
};
#define PLURAL_TEST_NUM 18
/**
* Test various generic API methods of PluralRules for API coverage.
*/
void PluralRulesTest::testAPI(/*char *par*/)
{
UnicodeString pluralTestData[PLURAL_TEST_NUM] = {
UNICODE_STRING_SIMPLE("a: n is 1"),
UNICODE_STRING_SIMPLE("a: n mod 10 is 2"),
UNICODE_STRING_SIMPLE("a: n is not 1"),
UNICODE_STRING_SIMPLE("a: n mod 3 is not 1"),
UNICODE_STRING_SIMPLE("a: n in 2..5"),
UNICODE_STRING_SIMPLE("a: n within 2..5"),
UNICODE_STRING_SIMPLE("a: n not in 2..5"),
UNICODE_STRING_SIMPLE("a: n not within 2..5"),
UNICODE_STRING_SIMPLE("a: n mod 10 in 2..5"),
UNICODE_STRING_SIMPLE("a: n mod 10 within 2..5"),
UNICODE_STRING_SIMPLE("a: n mod 10 is 2 and n is not 12"),
UNICODE_STRING_SIMPLE("a: n mod 10 in 2..3 or n mod 10 is 5"),
UNICODE_STRING_SIMPLE("a: n mod 10 within 2..3 or n mod 10 is 5"),
UNICODE_STRING_SIMPLE("a: n is 1 or n is 4 or n is 23"),
UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n in 1..11"),
UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n within 1..11"),
UNICODE_STRING_SIMPLE("a: n mod 2 is 1 or n mod 5 is 1 and n is not 6"),
"",
};
static const int32_t pluralTestResult[PLURAL_TEST_NUM][30] = {
{1, 0},
{2,12,22, 0},
{0,2,3,4,5,0},
{0,2,3,5,6,8,9,0},
{2,3,4,5,0},
{2,3,4,5,0},
{0,1,6,7,8, 0},
{0,1,6,7,8, 0},
{2,3,4,5,12,13,14,15,22,23,24,25,0},
{2,3,4,5,12,13,14,15,22,23,24,25,0},
{2,22,32,42,0},
{2,3,5,12,13,15,22,23,25,0},
{2,3,5,12,13,15,22,23,25,0},
{1,4,23,0},
{1,5,7,9,11,0},
{1,5,7,9,11,0},
{1,3,5,7,9,11,13,15,16,0},
};
UErrorCode status = U_ZERO_ERROR;
// ======= Test constructors
logln("Testing PluralRules constructors");
logln("\n start default locale test case ..\n");
PluralRules defRule(status);
LocalPointer<PluralRules> test(new PluralRules(status), status);
if(U_FAILURE(status)) {
dataerrln("ERROR: Could not create PluralRules (default) - exiting");
return;
}
LocalPointer<PluralRules> newEnPlural(test->forLocale(Locale::getEnglish(), status), status);
if(U_FAILURE(status)) {
dataerrln("ERROR: Could not create PluralRules (English) - exiting");
return;
}
// ======= Test clone, assignment operator && == operator.
LocalPointer<PluralRules> dupRule(defRule.clone());
if (dupRule==nullptr) {
errln("ERROR: clone plural rules test failed!");
return;
} else {
if ( *dupRule != defRule ) {
errln("ERROR: clone plural rules test failed!");
}
}
*dupRule = *newEnPlural;
if (dupRule!=nullptr) {
if ( *dupRule != *newEnPlural ) {
errln("ERROR: clone plural rules test failed!");
}
}
// ======= Test empty plural rules
logln("Testing Simple PluralRules");
LocalPointer<PluralRules> empRule(test->createRules(UNICODE_STRING_SIMPLE("a:n"), status));
UnicodeString key;
for (int32_t i=0; i<10; ++i) {
key = empRule->select(i);
if ( key.charAt(0)!= 0x61 ) { // 'a'
errln("ERROR: empty plural rules test failed! - exiting");
}
}
// ======= Test simple plural rules
logln("Testing Simple PluralRules");
char result[100];
int32_t max;
for (int32_t i=0; i<PLURAL_TEST_NUM-1; ++i) {
LocalPointer<PluralRules> newRules(test->createRules(pluralTestData[i], status));
setupResult(pluralTestResult[i], result, &max);
if ( !checkEqual(*newRules, result, max) ) {
errln("ERROR: simple plural rules failed! - exiting");
return;
}
}
// ======= Test complex plural rules
logln("Testing Complex PluralRules");
// TODO: the complex test data is hard coded. It's better to implement
// a parser to parse the test data.
UnicodeString complexRule = UNICODE_STRING_SIMPLE("a: n in 2..5; b: n in 5..8; c: n mod 2 is 1");
UnicodeString complexRule2 = UNICODE_STRING_SIMPLE("a: n within 2..5; b: n within 5..8; c: n mod 2 is 1");
char cRuleResult[] =
{
0x6F, // 'o'
0x63, // 'c'
0x61, // 'a'
0x61, // 'a'
0x61, // 'a'
0x61, // 'a'
0x62, // 'b'
0x62, // 'b'
0x62, // 'b'
0x63, // 'c'
0x6F, // 'o'
0x63 // 'c'
};
LocalPointer<PluralRules> newRules(test->createRules(complexRule, status));
if ( !checkEqual(*newRules, cRuleResult, 12) ) {
errln("ERROR: complex plural rules failed! - exiting");
return;
}
newRules.adoptInstead(test->createRules(complexRule2, status));
if ( !checkEqual(*newRules, cRuleResult, 12) ) {
errln("ERROR: complex plural rules failed! - exiting");
return;
}
// ======= Test decimal fractions plural rules
UnicodeString decimalRule= UNICODE_STRING_SIMPLE("a: n not in 0..100;");
UnicodeString KEYWORD_A = UNICODE_STRING_SIMPLE("a");
status = U_ZERO_ERROR;
newRules.adoptInstead(test->createRules(decimalRule, status));
if (U_FAILURE(status)) {
dataerrln("ERROR: Could not create PluralRules for testing fractions - exiting");
return;
}
double fData[] = {-101, -100, -1, -0.0, 0, 0.1, 1, 1.999, 2.0, 100, 100.001, 1.39e188 };
bool isKeywordA[] = {true, false, false, false, false, true, false, true, false, false, true, true };
for (int32_t i=0; i<UPRV_LENGTHOF(fData); i++) {
if ((newRules->select(fData[i])== KEYWORD_A) != isKeywordA[i]) {
errln("File %s, Line %d, ERROR: plural rules for decimal fractions test failed!\n"
" number = %g, expected %s", __FILE__, __LINE__, fData[i], isKeywordA[i]?"true":"false");
}
}
// ======= Test Equality
logln("Testing Equality of PluralRules");
if ( !testEquality(*test) ) {
errln("ERROR: complex plural rules failed! - exiting");
return;
}
// ======= Test getStaticClassID()
logln("Testing getStaticClassID()");
if(test->getDynamicClassID() != PluralRules::getStaticClassID()) {
errln("ERROR: getDynamicClassID() didn't return the expected value");
}
// ====== Test fallback to parent locale
LocalPointer<PluralRules> en_UK(test->forLocale(Locale::getUK(), status));
LocalPointer<PluralRules> en(test->forLocale(Locale::getEnglish(), status));
if (en_UK.isValid() && en.isValid()) {
if ( *en_UK != *en ) {
errln("ERROR: test locale fallback failed!");
}
}
LocalPointer<PluralRules> zh_Hant(test->forLocale(Locale::getTaiwan(), status));
LocalPointer<PluralRules> zh(test->forLocale(Locale::getChinese(), status));
if (zh_Hant.isValid() && zh.isValid()) {
if ( *zh_Hant != *zh ) {
errln("ERROR: test locale fallback failed!");
}
}
}
void setupResult(const int32_t testSource[], char result[], int32_t* max) {
int32_t i=0;
int32_t curIndex=0;
do {
while (curIndex < testSource[i]) {
result[curIndex++]=0x6F; //'o' other
}
result[curIndex++]=0x61; // 'a'
} while(testSource[++i]>0);
*max=curIndex;
}
UBool checkEqual(const PluralRules &test, char *result, int32_t max) {
UnicodeString key;
UBool isEqual = true;
for (int32_t i=0; i<max; ++i) {
key= test.select(i);
if ( key.charAt(0)!=result[i] ) {
isEqual = false;
}
}
return isEqual;
}
static const int32_t MAX_EQ_ROW = 2;
static const int32_t MAX_EQ_COL = 5;
UBool testEquality(const PluralRules &test) {
UnicodeString testEquRules[MAX_EQ_ROW][MAX_EQ_COL] = {
{ UNICODE_STRING_SIMPLE("a: n in 2..3"),
UNICODE_STRING_SIMPLE("a: n is 2 or n is 3"),
UNICODE_STRING_SIMPLE( "a:n is 3 and n in 2..5 or n is 2"),
"",
},
{ UNICODE_STRING_SIMPLE("a: n is 12; b:n mod 10 in 2..3"),
UNICODE_STRING_SIMPLE("b: n mod 10 in 2..3 and n is not 12; a: n in 12..12"),
UNICODE_STRING_SIMPLE("b: n is 13; a: n in 12..13; b: n mod 10 is 2 or n mod 10 is 3"),
"",
}
};
UErrorCode status = U_ZERO_ERROR;
UnicodeString key[MAX_EQ_COL];
UBool ret=true;
for (int32_t i=0; i<MAX_EQ_ROW; ++i) {
PluralRules* rules[MAX_EQ_COL];
for (int32_t j=0; j<MAX_EQ_COL; ++j) {
rules[j]=nullptr;
}
int32_t totalRules=0;
while((totalRules<MAX_EQ_COL) && (testEquRules[i][totalRules].length()>0) ) {
rules[totalRules]=test.createRules(testEquRules[i][totalRules], status);
totalRules++;
}
for (int32_t n=0; n<300 && ret ; ++n) {
for(int32_t j=0; j<totalRules;++j) {
key[j] = rules[j]->select(n);
}
for(int32_t j=0; j<totalRules-1;++j) {
if (key[j]!=key[j+1]) {
ret= false;
break;
}
}
}
for (int32_t j=0; j<MAX_EQ_COL; ++j) {
if (rules[j]!=nullptr) {
delete rules[j];
}
}
}
return ret;
}
void
PluralRulesTest::assertRuleValue(const UnicodeString& rule, double expected) {
assertRuleKeyValue("a:" + rule, "a", expected);
}
void
PluralRulesTest::assertRuleKeyValue(const UnicodeString& rule,
const UnicodeString& key, double expected) {
UErrorCode status = U_ZERO_ERROR;
PluralRules *pr = PluralRules::createRules(rule, status);
double result = pr->getUniqueKeywordValue(key);
delete pr;
if (expected != result) {
errln("expected %g but got %g", expected, result);
}
}
// TODO: UniqueKeywordValue() is not currently supported.
// If it never will be, this test code should be removed.
void PluralRulesTest::testGetUniqueKeywordValue() {
assertRuleValue("n is 1", 1);
assertRuleValue("n in 2..2", 2);
assertRuleValue("n within 2..2", 2);
assertRuleValue("n in 3..4", UPLRULES_NO_UNIQUE_VALUE);
assertRuleValue("n within 3..4", UPLRULES_NO_UNIQUE_VALUE);
assertRuleValue("n is 2 or n is 2", 2);
assertRuleValue("n is 2 and n is 2", 2);
assertRuleValue("n is 2 or n is 3", UPLRULES_NO_UNIQUE_VALUE);
assertRuleValue("n is 2 and n is 3", UPLRULES_NO_UNIQUE_VALUE);
assertRuleValue("n is 2 or n in 2..3", UPLRULES_NO_UNIQUE_VALUE);
assertRuleValue("n is 2 and n in 2..3", 2);
assertRuleKeyValue("a: n is 1", "not_defined", UPLRULES_NO_UNIQUE_VALUE); // key not defined
assertRuleKeyValue("a: n is 1", "other", UPLRULES_NO_UNIQUE_VALUE); // key matches default rule
}
/**
* Using the double API for getting plural samples, assert all samples match the keyword
* they are listed under, for all locales.
*
* Specifically, iterate over all locales, get plural rules for the locale, iterate over every rule,
* then iterate over every sample in the rule, parse sample to a number (double), use that number
* as an input to .select() for the rules object, and assert the actual return plural keyword matches
* what we expect based on the plural rule string.
*/
void PluralRulesTest::testGetSamples() {
// no get functional equivalent API in ICU4C, so just
// test every locale...
UErrorCode status = U_ZERO_ERROR;
int32_t numLocales;
const Locale* locales = Locale::getAvailableLocales(numLocales);
double values[1000];
for (int32_t i = 0; U_SUCCESS(status) && i < numLocales; ++i) {
LocalPointer<PluralRules> rules(PluralRules::forLocale(locales[i], status));
if (U_FAILURE(status)) {
break;
}
LocalPointer<StringEnumeration> keywords(rules->getKeywords(status));
if (U_FAILURE(status)) {
break;
}
const UnicodeString* keyword;
while (nullptr != (keyword = keywords->snext(status))) {
int32_t count = rules->getSamples(*keyword, values, UPRV_LENGTHOF(values), status);
if (U_FAILURE(status)) {
errln(UnicodeString(u"getSamples() failed for locale ") +
locales[i].getName() +
UnicodeString(u", keyword ") + *keyword);
continue;
}
if (count == 0) {
// TODO: Lots of these.
// errln(UnicodeString(u"no samples for keyword ") + *keyword + UnicodeString(u" in locale ") + locales[i].getName() );
}
if (count > UPRV_LENGTHOF(values)) {
errln(UnicodeString(u"getSamples()=") + count +
UnicodeString(u", too many values, for locale ") +
locales[i].getName() +
UnicodeString(u", keyword ") + *keyword);
count = UPRV_LENGTHOF(values);
}
for (int32_t j = 0; j < count; ++j) {
if (values[j] == UPLRULES_NO_UNIQUE_VALUE) {
errln("got 'no unique value' among values");
} else {
UnicodeString resultKeyword = rules->select(values[j]);
// if (strcmp(locales[i].getName(), "uk") == 0) { // Debug only.
// std::cout << " uk " << US(resultKeyword).cstr() << " " << values[j] << std::endl;
// }
if (*keyword != resultKeyword) {
errln("file %s, line %d, Locale %s, sample for keyword \"%s\": %g, select(%g) returns keyword \"%s\"",
__FILE__, __LINE__, locales[i].getName(), US(*keyword).cstr(), values[j], values[j], US(resultKeyword).cstr());
}
}
}
}
}
}
/**
* Using the DecimalQuantity API for getting plural samples, assert all samples match the keyword
* they are listed under, for all locales.
*
* Specifically, iterate over all locales, get plural rules for the locale, iterate over every rule,
* then iterate over every sample in the rule, parse sample to a number (DecimalQuantity), use that number
* as an input to .select() for the rules object, and assert the actual return plural keyword matches
* what we expect based on the plural rule string.
*/
void PluralRulesTest::testGetDecimalQuantitySamples() {
// no get functional equivalent API in ICU4C, so just
// test every locale...
UErrorCode status = U_ZERO_ERROR;
int32_t numLocales;
const Locale* locales = Locale::getAvailableLocales(numLocales);
DecimalQuantity values[1000];
for (int32_t i = 0; U_SUCCESS(status) && i < numLocales; ++i) {
LocalPointer<PluralRules> rules(PluralRules::forLocale(locales[i], status));
if (U_FAILURE(status)) {
break;
}
LocalPointer<StringEnumeration> keywords(rules->getKeywords(status));
if (U_FAILURE(status)) {
break;
}
const UnicodeString* keyword;
while (nullptr != (keyword = keywords->snext(status))) {
int32_t count = rules->getSamples(*keyword, values, UPRV_LENGTHOF(values), status);
if (U_FAILURE(status)) {
errln(UnicodeString(u"getSamples() failed for locale ") +
locales[i].getName() +
UnicodeString(u", keyword ") + *keyword);
continue;
}
if (count == 0) {
// TODO: Lots of these.
// errln(UnicodeString(u"no samples for keyword ") + *keyword + UnicodeString(u" in locale ") + locales[i].getName() );
}
if (count > UPRV_LENGTHOF(values)) {
errln(UnicodeString(u"getSamples()=") + count +
UnicodeString(u", too many values, for locale ") +
locales[i].getName() +
UnicodeString(u", keyword ") + *keyword);
count = UPRV_LENGTHOF(values);
}
for (int32_t j = 0; j < count; ++j) {
if (values[j] == UPLRULES_NO_UNIQUE_VALUE_DECIMAL(status)) {
errln("got 'no unique value' among values");
} else {
if (U_FAILURE(status)){
errln(UnicodeString(u"getSamples() failed for sample ") +
values[j].toExponentString() +
UnicodeString(u", keyword ") + *keyword);
continue;
}
UnicodeString resultKeyword = rules->select(values[j]);
// if (strcmp(locales[i].getName(), "uk") == 0) { // Debug only.
// std::cout << " uk " << US(resultKeyword).cstr() << " " << values[j] << std::endl;
// }
if (*keyword != resultKeyword) {
errln("file %s, line %d, Locale %s, sample for keyword \"%s\": %s, select(%s) returns keyword \"%s\"",
__FILE__, __LINE__, locales[i].getName(), US(*keyword).cstr(),
US(values[j].toExponentString()).cstr(), US(values[j].toExponentString()).cstr(),
US(resultKeyword).cstr());
}
}
}
}
}
}
/**
* Test addSamples (Java) / getSamplesFromString (C++) to ensure the expansion of plural rule sample range
* expands to a sequence of sample numbers that is incremented as the right scale.
*
* Do this for numbers with fractional digits but no exponent.
*/
void PluralRulesTest::testGetOrAddSamplesFromString() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString description(u"testkeyword: e != 0 @decimal 2.0c6~4.0c6, …");
LocalPointer<PluralRules> rules(PluralRules::createRules(description, status));
if (U_FAILURE(status)) {
errln("Couldn't create plural rules from a string, with error = %s", u_errorName(status));
return;
}
LocalPointer<StringEnumeration> keywords(rules->getKeywords(status));
if (U_FAILURE(status)) {
errln("Couldn't get keywords from a parsed rules object, with error = %s", u_errorName(status));
return;
}
DecimalQuantity values[1000];
const UnicodeString keyword(u"testkeyword");
int32_t count = rules->getSamples(keyword, values, UPRV_LENGTHOF(values), status);
if (U_FAILURE(status)) {
errln(UnicodeString(u"getSamples() failed for plural rule keyword ") + keyword);
return;
}
UnicodeString expDqStrs[] = {
u"2.0c6", u"2.1c6", u"2.2c6", u"2.3c6", u"2.4c6", u"2.5c6", u"2.6c6", u"2.7c6", u"2.8c6", u"2.9c6",
u"3.0c6", u"3.1c6", u"3.2c6", u"3.3c6", u"3.4c6", u"3.5c6", u"3.6c6", u"3.7c6", u"3.8c6", u"3.9c6",
u"4.0c6"
};
assertEquals(u"Number of parsed samples from test string incorrect", 21, count);
for (int i = 0; i < count; i++) {
UnicodeString expDqStr = expDqStrs[i];
DecimalQuantity sample = values[i];
UnicodeString sampleStr = sample.toExponentString();
assertEquals(u"Expansion of sample range to sequence of sample values should increment at the right scale",
expDqStr, sampleStr);
}
}
/**
* Test addSamples (Java) / getSamplesFromString (C++) to ensure the expansion of plural rule sample range
* expands to a sequence of sample numbers that is incremented as the right scale.
*
* Do this for numbers written in a notation that has an exponent, for which the number is an
* integer (also as defined in the UTS 35 spec for the plural operands) but whose representation
* has fractional digits in the significand written before the exponent.
*/
void PluralRulesTest::testGetOrAddSamplesFromStringCompactNotation() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString description(u"testkeyword: e != 0 @decimal 2.0~4.0, …");
LocalPointer<PluralRules> rules(PluralRules::createRules(description, status));
if (U_FAILURE(status)) {
errln("Couldn't create plural rules from a string, with error = %s", u_errorName(status));
return;
}
LocalPointer<StringEnumeration> keywords(rules->getKeywords(status));
if (U_FAILURE(status)) {
errln("Couldn't get keywords from a parsed rules object, with error = %s", u_errorName(status));
return;
}
DecimalQuantity values[1000];
const UnicodeString keyword(u"testkeyword");
int32_t count = rules->getSamples(keyword, values, UPRV_LENGTHOF(values), status);
if (U_FAILURE(status)) {
errln(UnicodeString(u"getSamples() failed for plural rule keyword ") + keyword);
return;
}
UnicodeString expDqStrs[] = {
u"2.0", u"2.1", u"2.2", u"2.3", u"2.4", u"2.5", u"2.6", u"2.7", u"2.8", u"2.9",
u"3.0", u"3.1", u"3.2", u"3.3", u"3.4", u"3.5", u"3.6", u"3.7", u"3.8", u"3.9",
u"4.0"
};
assertEquals(u"Number of parsed samples from test string incorrect", 21, count);
for (int i = 0; i < count; i++) {
UnicodeString expDqStr = expDqStrs[i];
DecimalQuantity sample = values[i];
UnicodeString sampleStr = sample.toExponentString();
assertEquals(u"Expansion of sample range to sequence of sample values should increment at the right scale",
expDqStr, sampleStr);
}
}
/**
* This test is for the support of X.YeZ scientific notation of numbers in
* the plural sample string.
*/
void PluralRulesTest::testSamplesWithExponent() {
// integer samples
UErrorCode status = U_ZERO_ERROR;
UnicodeString description(
u"one: i = 0,1 @integer 0, 1, 1e5 @decimal 0.0~1.5, 1.1e5; "
u"many: e = 0 and i != 0 and i % 1000000 = 0 and v = 0 or e != 0..5"
u" @integer 1000000, 2e6, 3e6, 4e6, 5e6, 6e6, 7e6, … @decimal 2.1e6, 3.1e6, 4.1e6, 5.1e6, 6.1e6, 7.1e6, …; "
u"other: @integer 2~17, 100, 1000, 10000, 100000, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, …"
u" @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1e5, 3.1e5, 4.1e5, 5.1e5, 6.1e5, 7.1e5, …"
);
LocalPointer<PluralRules> test(PluralRules::createRules(description, status));
if (U_FAILURE(status)) {
errln("Couldn't create plural rules from a string using exponent notation, with error = %s", u_errorName(status));
return;
}
checkNewSamples(description, test, u"one", u"@integer 0, 1, 1e5", DecimalQuantity::fromExponentString(u"0", status));
checkNewSamples(description, test, u"many", u"@integer 1000000, 2e6, 3e6, 4e6, 5e6, 6e6, 7e6, …", DecimalQuantity::fromExponentString(u"1000000", status));
checkNewSamples(description, test, u"other", u"@integer 2~17, 100, 1000, 10000, 100000, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, …", DecimalQuantity::fromExponentString(u"2", status));
// decimal samples
status = U_ZERO_ERROR;
UnicodeString description2(
u"one: i = 0,1 @decimal 0.0~1.5, 1.1e5; "
u"many: e = 0 and i != 0 and i % 1000000 = 0 and v = 0 or e != 0..5"
u" @decimal 2.1e6, 3.1e6, 4.1e6, 5.1e6, 6.1e6, 7.1e6, …; "
u"other: @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1e5, 3.1e5, 4.1e5, 5.1e5, 6.1e5, 7.1e5, …"
);
LocalPointer<PluralRules> test2(PluralRules::createRules(description2, status));
if (U_FAILURE(status)) {
errln("Couldn't create plural rules from a string using exponent notation, with error = %s", u_errorName(status));
return;
}
checkNewSamples(description2, test2, u"one", u"@decimal 0.0~1.5, 1.1e5", DecimalQuantity::fromExponentString(u"0.0", status));
checkNewSamples(description2, test2, u"many", u"@decimal 2.1e6, 3.1e6, 4.1e6, 5.1e6, 6.1e6, 7.1e6, …", DecimalQuantity::fromExponentString(u"2.1c6", status));
checkNewSamples(description2, test2, u"other", u"@decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1e5, 3.1e5, 4.1e5, 5.1e5, 6.1e5, 7.1e5, …", DecimalQuantity::fromExponentString(u"2.0", status));
}
/**
* This test is for the support of X.YcZ compact notation of numbers in
* the plural sample string.
*/
void PluralRulesTest::testSamplesWithCompactNotation() {
// integer samples
UErrorCode status = U_ZERO_ERROR;
UnicodeString description(
u"one: i = 0,1 @integer 0, 1, 1c5 @decimal 0.0~1.5, 1.1c5; "
u"many: c = 0 and i != 0 and i % 1000000 = 0 and v = 0 or c != 0..5"
u" @integer 1000000, 2c6, 3c6, 4c6, 5c6, 6c6, 7c6, … @decimal 2.1c6, 3.1c6, 4.1c6, 5.1c6, 6.1c6, 7.1c6, …; "
u"other: @integer 2~17, 100, 1000, 10000, 100000, 2c5, 3c5, 4c5, 5c5, 6c5, 7c5, …"
u" @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1c5, 3.1c5, 4.1c5, 5.1c5, 6.1c5, 7.1c5, …"
);
LocalPointer<PluralRules> test(PluralRules::createRules(description, status));
if (U_FAILURE(status)) {
errln("Couldn't create plural rules from a string using exponent notation, with error = %s", u_errorName(status));
return;
}
checkNewSamples(description, test, u"one", u"@integer 0, 1, 1c5", DecimalQuantity::fromExponentString(u"0", status));
checkNewSamples(description, test, u"many", u"@integer 1000000, 2c6, 3c6, 4c6, 5c6, 6c6, 7c6, …", DecimalQuantity::fromExponentString(u"1000000", status));
checkNewSamples(description, test, u"other", u"@integer 2~17, 100, 1000, 10000, 100000, 2c5, 3c5, 4c5, 5c5, 6c5, 7c5, …", DecimalQuantity::fromExponentString(u"2", status));
// decimal samples
status = U_ZERO_ERROR;
UnicodeString description2(
u"one: i = 0,1 @decimal 0.0~1.5, 1.1c5; "
u"many: c = 0 and i != 0 and i % 1000000 = 0 and v = 0 or c != 0..5"
u" @decimal 2.1c6, 3.1c6, 4.1c6, 5.1c6, 6.1c6, 7.1c6, …; "
u"other: @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1c5, 3.1c5, 4.1c5, 5.1c5, 6.1c5, 7.1c5, …"
);
LocalPointer<PluralRules> test2(PluralRules::createRules(description2, status));
if (U_FAILURE(status)) {
errln("Couldn't create plural rules from a string using exponent notation, with error = %s", u_errorName(status));
return;
}
checkNewSamples(description2, test2, u"one", u"@decimal 0.0~1.5, 1.1c5", DecimalQuantity::fromExponentString(u"0.0", status));
checkNewSamples(description2, test2, u"many", u"@decimal 2.1c6, 3.1c6, 4.1c6, 5.1c6, 6.1c6, 7.1c6, …", DecimalQuantity::fromExponentString(u"2.1c6", status));
checkNewSamples(description2, test2, u"other", u"@decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1c5, 3.1c5, 4.1c5, 5.1c5, 6.1c5, 7.1c5, …", DecimalQuantity::fromExponentString(u"2.0", status));
}
void PluralRulesTest::checkNewSamples(
UnicodeString description,
const LocalPointer<PluralRules> &test,
UnicodeString keyword,
UnicodeString samplesString,
DecimalQuantity firstInRange) {
UErrorCode status = U_ZERO_ERROR;
DecimalQuantity samples[1000];
test->getSamples(keyword, samples, UPRV_LENGTHOF(samples), status);
if (U_FAILURE(status)) {
errln("Couldn't retrieve plural samples, with error = %s", u_errorName(status));
return;
}
DecimalQuantity actualFirstSample = samples[0];
if (!(firstInRange == actualFirstSample)) {
CStr descCstr(description);
CStr samplesCstr(samplesString);
char errMsg[1000];
snprintf(errMsg, sizeof(errMsg), "First parsed sample FixedDecimal not equal to expected for samples: %s in rule string: %s\n", descCstr(), samplesCstr());
errln(errMsg);
}
}
void PluralRulesTest::testWithin() {
// goes to show you what lack of testing will do.
// of course, this has been broken for two years and no one has noticed...
UErrorCode status = U_ZERO_ERROR;
PluralRules *rules = PluralRules::createRules("a: n mod 10 in 5..8", status);
if (!rules) {
errln("couldn't instantiate rules");
return;
}
UnicodeString keyword = rules->select(static_cast<int32_t>(26));
if (keyword != "a") {
errln("expected 'a' for 26 but didn't get it.");
}
keyword = rules->select(26.5);
if (keyword != "other") {
errln("expected 'other' for 26.5 but didn't get it.");
}
delete rules;
}
void
PluralRulesTest::testGetAllKeywordValues() {
const char* data[] = {
"a: n in 2..5", "a: 2,3,4,5; other: null; b:",
"a: n not in 2..5", "a: null; other: null",
"a: n within 2..5", "a: null; other: null",
"a: n not within 2..5", "a: null; other: null",
"a: n in 2..5 or n within 6..8", "a: null", // ignore 'other' here on out, always null
"a: n in 2..5 and n within 6..8", "a:",
"a: n in 2..5 and n within 5..8", "a: 5",
"a: n within 2..5 and n within 6..8", "a:", // our sampling catches these
"a: n within 2..5 and n within 5..8", "a: 5", // ''
"a: n within 1..2 and n within 2..3 or n within 3..4 and n within 4..5", "a: 2,4",
"a: n within 1..2 and n within 2..3 or n within 3..4 and n within 4..5 "
"or n within 5..6 and n within 6..7", "a: null", // but not this...
"a: n mod 3 is 0", "a: null",
"a: n mod 3 is 0 and n within 1..2", "a:",
"a: n mod 3 is 0 and n within 0..5", "a: 0,3",
"a: n mod 3 is 0 and n within 0..6", "a: null", // similarly with mod, we don't catch...
"a: n mod 3 is 0 and n in 3..12", "a: 3,6,9,12",
nullptr
};
for (int i = 0; data[i] != nullptr; i += 2) {
UErrorCode status = U_ZERO_ERROR;
UnicodeString ruleDescription(data[i], -1, US_INV);
const char* result = data[i+1];
logln("[%d] %s", i >> 1, data[i]);
PluralRules *p = PluralRules::createRules(ruleDescription, status);
if (p == nullptr || U_FAILURE(status)) {
errln("file %s, line %d: could not create rules from '%s'\n"
" ErrorCode: %s\n",
__FILE__, __LINE__, data[i], u_errorName(status));
continue;
}
// TODO: fix samples implementation, re-enable test.
(void)result;
#if 0
const char* rp = result;
while (*rp) {
while (*rp == ' ') ++rp;
if (!rp) {
break;
}
const char* ep = rp;
while (*ep && *ep != ':') ++ep;
status = U_ZERO_ERROR;
UnicodeString keyword(rp, ep - rp, US_INV);
double samples[4]; // no test above should have more samples than 4
int32_t count = p->getAllKeywordValues(keyword, &samples[0], 4, status);
if (U_FAILURE(status)) {
errln("error getting samples for %s", rp);
break;
}
if (count > 4) {
errln("count > 4 for keyword %s", rp);
count = 4;
}
if (*ep) {
++ep; // skip colon
while (*ep && *ep == ' ') ++ep; // and spaces
}
UBool ok = true;
if (count == -1) {
if (*ep != 'n') {
errln("expected values for keyword %s but got -1 (%s)", rp, ep);
ok = false;
}
} else if (*ep == 'n') {
errln("expected count of -1, got %d, for keyword %s (%s)", count, rp, ep);
ok = false;
}
// We'll cheat a bit here. The samples happened to be in order and so are our
// expected values, so we'll just test in order until a failure. If the
// implementation changes to return samples in an arbitrary order, this test
// must change. There's no actual restriction on the order of the samples.
for (int j = 0; ok && j < count; ++j ) { // we've verified count < 4
double val = samples[j];
if (*ep == 0 || *ep == ';') {
errln("got unexpected value[%d]: %g", j, val);
ok = false;
break;
}
char* xp;
double expectedVal = strtod(ep, &xp);
if (xp == ep) {
// internal error
errln("yikes!");
ok = false;
break;
}
ep = xp;
if (expectedVal != val) {
errln("expected %g but got %g", expectedVal, val);
ok = false;
break;
}
if (*ep == ',') ++ep;
}
if (ok && count != -1) {
if (!(*ep == 0 || *ep == ';')) {
errln("file: %s, line %d, didn't get expected value: %s", __FILE__, __LINE__, ep);
ok = false;
}
}
while (*ep && *ep != ';') ++ep;
if (*ep == ';') ++ep;
rp = ep;
}
#endif
delete p;
}
}
// For the time being, the compact notation exponent operand `c` is an alias
// for the scientific exponent operand `e` and compact notation.
/**
* Test the proper plural rule keyword selection given an input number that is
* already formatted into scientific notation. This exercises the `e` plural operand
* for the formatted number.
*/
void
PluralRulesTest::testScientificPluralKeyword() {
IcuTestErrorCode errorCode(*this, "testScientificPluralKeyword");
LocalPointer<PluralRules> rules(PluralRules::createRules(
u"one: i = 0,1 @integer 0, 1 @decimal 0.0~1.5; "
u"many: e = 0 and i % 1000000 = 0 and v = 0 or e != 0 .. 5; "
u"other: @integer 2~17, 100, 1000, 10000, 100000, 1000000, "
u" @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …", errorCode));
if (U_FAILURE(errorCode)) {
errln("Couldn't instantiate plurals rules from string, with error = %s", u_errorName(errorCode));
return;
}
const char* localeName = "fr-FR";
Locale locale = Locale::createFromName(localeName);
struct TestCase {
const char16_t* skeleton;
const int input;
const char16_t* expectedFormattedOutput;
const char16_t* expectedPluralRuleKeyword;
} cases[] = {
// unlocalized formatter skeleton, input, string output, plural rule keyword
{u"", 0, u"0", u"one"},
{u"scientific", 0, u"0", u"one"},
{u"", 1, u"1", u"one"},
{u"scientific", 1, u"1", u"one"},
{u"", 2, u"2", u"other"},
{u"scientific", 2, u"2", u"other"},
{u"", 1000000, u"1 000 000", u"many"},
{u"scientific", 1000000, u"1 million", u"many"},
{u"", 1000001, u"1 000 001", u"other"},
{u"scientific", 1000001, u"1 million", u"many"},
{u"", 120000, u"1 200 000", u"other"},
{u"scientific", 1200000, u"1,2 millions", u"many"},
{u"", 1200001, u"1 200 001", u"other"},
{u"scientific", 1200001, u"1,2 millions", u"many"},
{u"", 2000000, u"2 000 000", u"many"},
{u"scientific", 2000000, u"2 millions", u"many"},
};
for (const auto& cas : cases) {
const char16_t* skeleton = cas.skeleton;
const int input = cas.input;
const char16_t* expectedPluralRuleKeyword = cas.expectedPluralRuleKeyword;
UnicodeString actualPluralRuleKeyword =
getPluralKeyword(rules, locale, input, skeleton);
UnicodeString message(UnicodeString(localeName) + u" " + DoubleToUnicodeString(input));
assertEquals(message, expectedPluralRuleKeyword, actualPluralRuleKeyword);
}
}
/**
* Test the proper plural rule keyword selection given an input number that is
* already formatted into compact notation. This exercises the `c` plural operand
* for the formatted number.
*/
void
PluralRulesTest::testCompactDecimalPluralKeyword() {
IcuTestErrorCode errorCode(*this, "testCompactDecimalPluralKeyword");
LocalPointer<PluralRules> rules(PluralRules::createRules(
u"one: i = 0,1 @integer 0, 1 @decimal 0.0~1.5; "
u"many: c = 0 and i % 1000000 = 0 and v = 0 or c != 0 .. 5; "
u"other: @integer 2~17, 100, 1000, 10000, 100000, 1000000, "
u" @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …", errorCode));
if (U_FAILURE(errorCode)) {
errln("Couldn't instantiate plurals rules from string, with error = %s", u_errorName(errorCode));
return;
}
const char* localeName = "fr-FR";
Locale locale = Locale::createFromName(localeName);
struct TestCase {
const char16_t* skeleton;
const int input;
const char16_t* expectedFormattedOutput;
const char16_t* expectedPluralRuleKeyword;
} cases[] = {
// unlocalized formatter skeleton, input, string output, plural rule keyword
{u"", 0, u"0", u"one"},
{u"compact-long", 0, u"0", u"one"},
{u"", 1, u"1", u"one"},
{u"compact-long", 1, u"1", u"one"},
{u"", 2, u"2", u"other"},
{u"compact-long", 2, u"2", u"other"},
{u"", 1000000, u"1 000 000", u"many"},
{u"compact-long", 1000000, u"1 million", u"many"},
{u"", 1000001, u"1 000 001", u"other"},
{u"compact-long", 1000001, u"1 million", u"many"},
{u"", 120000, u"1 200 000", u"other"},
{u"compact-long", 1200000, u"1,2 millions", u"many"},
{u"", 1200001, u"1 200 001", u"other"},
{u"compact-long", 1200001, u"1,2 millions", u"many"},
{u"", 2000000, u"2 000 000", u"many"},
{u"compact-long", 2000000, u"2 millions", u"many"},
};
for (const auto& cas : cases) {
const char16_t* skeleton = cas.skeleton;
const int input = cas.input;
const char16_t* expectedPluralRuleKeyword = cas.expectedPluralRuleKeyword;
UnicodeString actualPluralRuleKeyword =
getPluralKeyword(rules, locale, input, skeleton);
UnicodeString message(UnicodeString(localeName) + u" " + DoubleToUnicodeString(input));
assertEquals(message, expectedPluralRuleKeyword, actualPluralRuleKeyword);
}
}
void
PluralRulesTest::testDoubleValue() {
IcuTestErrorCode errorCode(*this, "testDoubleValue");
struct IntTestCase {
const int64_t inputNum;
const double expVal;
} intCases[] = {
{-101, -101.0},
{-100, -100.0},
{-1, -1.0},
{0, 0.0},
{1, 1.0},
{100, 100.0}
};
for (const auto& cas : intCases) {
const int64_t inputNum = cas.inputNum;
const double expVal = cas.expVal;
FixedDecimal fd(static_cast<double>(inputNum));
UnicodeString message(u"FixedDecimal::doubleValue() for" + Int64ToUnicodeString(inputNum));
assertEquals(message, expVal, fd.doubleValue());
}
struct DoubleTestCase {
const double inputNum;
const double expVal;
} dblCases[] = {
{-0.0, -0.0},
{0.1, 0.1},
{1.999, 1.999},
{2.0, 2.0},
{100.001, 100.001}
};
for (const auto & cas : dblCases) {
const double inputNum = cas.inputNum;
const double expVal = cas.expVal;
FixedDecimal fd(inputNum);
UnicodeString message(u"FixedDecimal::doubleValue() for" + DoubleToUnicodeString(inputNum));
assertEquals(message, expVal, fd.doubleValue());
}
}
void
PluralRulesTest::test22638LongNumberValue() {
IcuTestErrorCode errorCode(*this, "test22638LongNumberValue");
LocalPointer<PluralRules> pr(PluralRules::createRules(
u"g:c%4422322222232222222222232222222322222223222222232222222322222223"
u"2222222322222232222222322222223222232222222222222322222223222222",
errorCode));
errorCode.expectErrorAndReset(U_UNEXPECTED_TOKEN);
}
void
PluralRulesTest::testLongValue() {
IcuTestErrorCode errorCode(*this, "testLongValue");
struct IntTestCase {
const int64_t inputNum;
const int64_t expVal;
} intCases[] = {
{-101, 101},
{-100, 100},
{-1, 1},
{0, 0},
{1, 1},
{100, 100}
};
for (const auto& cas : intCases) {
const int64_t inputNum = cas.inputNum;
const int64_t expVal = cas.expVal;
FixedDecimal fd(static_cast<double>(inputNum));
UnicodeString message(u"FixedDecimal::longValue() for" + Int64ToUnicodeString(inputNum));
assertEquals(message, expVal, fd.longValue());
}
struct DoubleTestCase {
const double inputNum;
const int64_t expVal;
} dblCases[] = {
{-0.0, 0},
{0.1, 0},
{1.999, 1},
{2.0, 2},
{100.001, 100}
};
for (const auto & cas : dblCases) {
const double inputNum = cas.inputNum;
const int64_t expVal = cas.expVal;
FixedDecimal fd(static_cast<double>(inputNum));
UnicodeString message(u"FixedDecimal::longValue() for" + DoubleToUnicodeString(inputNum));
assertEquals(message, expVal, fd.longValue());
}
}
UnicodeString PluralRulesTest::getPluralKeyword(const LocalPointer<PluralRules> &rules, Locale locale, double number, const char16_t* skeleton) {
IcuTestErrorCode errorCode(*this, "getPluralKeyword");
UnlocalizedNumberFormatter ulnf = NumberFormatter::forSkeleton(skeleton, errorCode);
if (errorCode.errIfFailureAndReset("PluralRules::getPluralKeyword(<PluralRules>, <locale>, %d, %s) failed", number, skeleton)) {
return nullptr;
}
LocalizedNumberFormatter formatter = ulnf.locale(locale);
const FormattedNumber fn = formatter.formatDouble(number, errorCode);
if (errorCode.errIfFailureAndReset("NumberFormatter::formatDouble(%d) failed", number)) {
return nullptr;
}
UnicodeString pluralKeyword = rules->select(fn, errorCode);
if (errorCode.errIfFailureAndReset("PluralRules->select(FormattedNumber of %d) failed", number)) {
return nullptr;
}
return pluralKeyword;
}
void PluralRulesTest::testOrdinal() {
IcuTestErrorCode errorCode(*this, "testOrdinal");
LocalPointer<PluralRules> pr(PluralRules::forLocale("en", UPLURAL_TYPE_ORDINAL, errorCode));
if (errorCode.errIfFailureAndReset("PluralRules::forLocale(en, UPLURAL_TYPE_ORDINAL) failed")) {
return;
}
UnicodeString keyword = pr->select(2.);
if (keyword != UNICODE_STRING("two", 3)) {
dataerrln("PluralRules(en-ordinal).select(2) failed");
}
}
static const char * END_MARK = "999.999"; // Mark end of varargs data.
void PluralRulesTest::checkSelect(const LocalPointer<PluralRules> &rules, UErrorCode &status,
int32_t line, const char *keyword, ...) {
// The varargs parameters are a const char* strings, each being a decimal number.
// The formatting of the numbers as strings is significant, e.g.
// the difference between "2" and "2.0" can affect which rule matches (which keyword is selected).
// Note: rules parameter is a LocalPointer reference rather than a PluralRules * to avoid having
// to write getAlias() at every (numerous) call site.
if (U_FAILURE(status)) {
errln("file %s, line %d, ICU error status: %s.", __FILE__, line, u_errorName(status));
status = U_ZERO_ERROR;
return;
}
if (rules == nullptr) {
errln("file %s, line %d: rules pointer is nullptr", __FILE__, line);
return;
}
va_list ap;
va_start(ap, keyword);
for (;;) {
const char *num = va_arg(ap, const char *);
if (strcmp(num, END_MARK) == 0) {
break;
}
// DigitList is a convenient way to parse the decimal number string and get a double.
DecimalQuantity dl;
dl.setToDecNumber(StringPiece(num), status);
if (U_FAILURE(status)) {
errln("file %s, line %d, ICU error status: %s.", __FILE__, line, u_errorName(status));
status = U_ZERO_ERROR;
continue;
}
double numDbl = dl.toDouble();
const char *decimalPoint = strchr(num, '.');
int fractionDigitCount = decimalPoint == nullptr ? 0 : static_cast<int>((num + strlen(num) - 1) - decimalPoint);
int fractionDigits = fractionDigitCount == 0 ? 0 : atoi(decimalPoint + 1);
FixedDecimal ni(numDbl, fractionDigitCount, fractionDigits);
UnicodeString actualKeyword = rules->select(ni);
if (actualKeyword != UnicodeString(keyword)) {
errln("file %s, line %d, select(%s) returned incorrect keyword. Expected %s, got %s",
__FILE__, line, num, keyword, US(actualKeyword).cstr());
}
}
va_end(ap);
}
void PluralRulesTest::testSelect() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<PluralRules> pr(PluralRules::createRules("s: n in 1,3,4,6", status));
checkSelect(pr, status, __LINE__, "s", "1.0", "3.0", "4.0", "6.0", END_MARK);
checkSelect(pr, status, __LINE__, "other", "0.0", "2.0", "3.1", "7.0", END_MARK);
pr.adoptInstead(PluralRules::createRules("s: n not in 1,3,4,6", status));
checkSelect(pr, status, __LINE__, "other", "1.0", "3.0", "4.0", "6.0", END_MARK);
checkSelect(pr, status, __LINE__, "s", "0.0", "2.0", "3.1", "7.0", END_MARK);
pr.adoptInstead(PluralRules::createRules("r: n in 1..4, 7..10, 14 .. 17;"
"s: n is 29;", status));
checkSelect(pr, status, __LINE__, "r", "1.0", "3.0", "7.0", "8.0", "10.0", "14.0", "17.0", END_MARK);
checkSelect(pr, status, __LINE__, "s", "29.0", END_MARK);
checkSelect(pr, status, __LINE__, "other", "28.0", "29.1", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 1; b: n mod 100 is 0 ", status));
checkSelect(pr, status, __LINE__, "a", "1", "11", "41", "101", "301.00", END_MARK);
checkSelect(pr, status, __LINE__, "b", "0", "100", "200.0", "300.", "1000", "1100", "110000", END_MARK);
checkSelect(pr, status, __LINE__, "other", "0.01", "1.01", "0.99", "2", "3", "99", "102", END_MARK);
// Rules that end with or without a ';' and with or without trailing spaces.
// (There was a rule parser bug here with these.)
pr.adoptInstead(PluralRules::createRules("a: n is 1", status));
checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n is 1 ", status));
checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n is 1;", status));
checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n is 1 ; ", status));
checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
// First match when rules for different keywords are not disjoint.
// Also try spacing variations around ':' and '..'
pr.adoptInstead(PluralRules::createRules("c: n in 5..15; b : n in 1..10 ;a:n in 10 .. 20", status));
checkSelect(pr, status, __LINE__, "a", "20", END_MARK);
checkSelect(pr, status, __LINE__, "b", "1", END_MARK);
checkSelect(pr, status, __LINE__, "c", "10", END_MARK);
checkSelect(pr, status, __LINE__, "other", "0", "21", "10.1", END_MARK);
// in vs within
pr.adoptInstead(PluralRules::createRules("a: n in 2..10; b: n within 8..15", status));
checkSelect(pr, status, __LINE__, "a", "2", "8", "10", END_MARK);
checkSelect(pr, status, __LINE__, "b", "8.01", "9.5", "11", "14.99", "15", END_MARK);
checkSelect(pr, status, __LINE__, "other", "1", "7.7", "15.01", "16", END_MARK);
// OR and AND chains.
pr.adoptInstead(PluralRules::createRules("a: n in 2..10 and n in 4..12 and n not in 5..7", status));
checkSelect(pr, status, __LINE__, "a", "4", "8", "9", "10", END_MARK);
checkSelect(pr, status, __LINE__, "other", "2", "3", "5", "7", "11", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n is 2 or n is 5 or n in 7..11 and n in 11..13", status));
checkSelect(pr, status, __LINE__, "a", "2", "5", "11", END_MARK);
checkSelect(pr, status, __LINE__, "other", "3", "4", "6", "8", "10", "12", "13", END_MARK);
// Number attributes -
// n: the number itself
// i: integer digits
// f: visible fraction digits
// t: f with trailing zeros removed.
// v: number of visible fraction digits
// j: = n if there are no visible fraction digits
// != anything if there are visible fraction digits
pr.adoptInstead(PluralRules::createRules("a: i is 123", status));
checkSelect(pr, status, __LINE__, "a", "123", "123.0", "123.1", "0123.99", END_MARK);
checkSelect(pr, status, __LINE__, "other", "124", "122.0", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: f is 120", status));
checkSelect(pr, status, __LINE__, "a", "1.120", "0.120", "11123.120", "0123.120", END_MARK);
checkSelect(pr, status, __LINE__, "other", "1.121", "122.1200", "1.12", "120", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: t is 12", status));
checkSelect(pr, status, __LINE__, "a", "1.120", "0.12", "11123.12000", "0123.1200000", END_MARK);
checkSelect(pr, status, __LINE__, "other", "1.121", "122.1200001", "1.11", "12", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: v is 3", status));
checkSelect(pr, status, __LINE__, "a", "1.120", "0.000", "11123.100", "0123.124", ".666", END_MARK);
checkSelect(pr, status, __LINE__, "other", "1.1212", "122.12", "1.1", "122", "0.0000", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: v is 0 and i is 123", status));
checkSelect(pr, status, __LINE__, "a", "123", "123.", END_MARK);
checkSelect(pr, status, __LINE__, "other", "123.0", "123.1", "123.123", "0.123", END_MARK);
// The reserved words from the rule syntax will also function as keywords.
pr.adoptInstead(PluralRules::createRules("a: n is 21; n: n is 22; i: n is 23; f: n is 24;"
"t: n is 25; v: n is 26; w: n is 27; j: n is 28"
, status));
checkSelect(pr, status, __LINE__, "other", "20", "29", END_MARK);
checkSelect(pr, status, __LINE__, "a", "21", END_MARK);
checkSelect(pr, status, __LINE__, "n", "22", END_MARK);
checkSelect(pr, status, __LINE__, "i", "23", END_MARK);
checkSelect(pr, status, __LINE__, "f", "24", END_MARK);
checkSelect(pr, status, __LINE__, "t", "25", END_MARK);
checkSelect(pr, status, __LINE__, "v", "26", END_MARK);
checkSelect(pr, status, __LINE__, "w", "27", END_MARK);
checkSelect(pr, status, __LINE__, "j", "28", END_MARK);
pr.adoptInstead(PluralRules::createRules("not: n=31; and: n=32; or: n=33; mod: n=34;"
"in: n=35; within: n=36;is:n=37"
, status));
checkSelect(pr, status, __LINE__, "other", "30", "39", END_MARK);
checkSelect(pr, status, __LINE__, "not", "31", END_MARK);
checkSelect(pr, status, __LINE__, "and", "32", END_MARK);
checkSelect(pr, status, __LINE__, "or", "33", END_MARK);
checkSelect(pr, status, __LINE__, "mod", "34", END_MARK);
checkSelect(pr, status, __LINE__, "in", "35", END_MARK);
checkSelect(pr, status, __LINE__, "within", "36", END_MARK);
checkSelect(pr, status, __LINE__, "is", "37", END_MARK);
// Test cases from ICU4J PluralRulesTest.parseTestData
pr.adoptInstead(PluralRules::createRules("a: n is 1", status));
checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 2", status));
checkSelect(pr, status, __LINE__, "a", "2", "12", "22", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n is not 1", status));
checkSelect(pr, status, __LINE__, "a", "0", "2", "3", "4", "5", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 3 is not 1", status));
checkSelect(pr, status, __LINE__, "a", "0", "2", "3", "5", "6", "8", "9", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n in 2..5", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n within 2..5", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n not in 2..5", status));
checkSelect(pr, status, __LINE__, "a", "0", "1", "6", "7", "8", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n not within 2..5", status));
checkSelect(pr, status, __LINE__, "a", "0", "1", "6", "7", "8", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2..5", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "12", "13", "14", "15", "22", "23", "24", "25", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 10 within 2..5", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "12", "13", "14", "15", "22", "23", "24", "25", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 2 and n is not 12", status));
checkSelect(pr, status, __LINE__, "a", "2", "22", "32", "42", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2..3 or n mod 10 is 5", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "12", "13", "15", "22", "23", "25", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 10 within 2..3 or n mod 10 is 5", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "12", "13", "15", "22", "23", "25", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n is 1 or n is 4 or n is 23", status));
checkSelect(pr, status, __LINE__, "a", "1", "4", "23", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 and n is not 3 and n in 1..11", status));
checkSelect(pr, status, __LINE__, "a", "1", "5", "7", "9", "11", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 and n is not 3 and n within 1..11", status));
checkSelect(pr, status, __LINE__, "a", "1", "5", "7", "9", "11", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 or n mod 5 is 1 and n is not 6", status));
checkSelect(pr, status, __LINE__, "a", "1", "3", "5", "7", "9", "11", "13", "15", "16", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n in 2..5; b: n in 5..8; c: n mod 2 is 1", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
checkSelect(pr, status, __LINE__, "b", "6", "7", "8", END_MARK);
checkSelect(pr, status, __LINE__, "c", "1", "9", "11", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n within 2..5; b: n within 5..8; c: n mod 2 is 1", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
checkSelect(pr, status, __LINE__, "b", "6", "7", "8", END_MARK);
checkSelect(pr, status, __LINE__, "c", "1", "9", "11", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n in 2, 4..6; b: n within 7..9,11..12,20", status));
checkSelect(pr, status, __LINE__, "a", "2", "4", "5", "6", END_MARK);
checkSelect(pr, status, __LINE__, "b", "7", "8", "9", "11", "12", "20", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n in 2..8, 12 and n not in 4..6", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "7", "8", "12", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2, 3,5..7 and n is not 12", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "6", "7", "13", "15", "16", "17", END_MARK);
pr.adoptInstead(PluralRules::createRules("a: n in 2..6, 3..7", status));
checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "6", "7", END_MARK);
// Extended Syntax, with '=', '!=' and '%' operators.
pr.adoptInstead(PluralRules::createRules("a: n = 1..8 and n!= 2,3,4,5", status));
checkSelect(pr, status, __LINE__, "a", "1", "6", "7", "8", END_MARK);
checkSelect(pr, status, __LINE__, "other", "0", "2", "3", "4", "5", "9", END_MARK);
pr.adoptInstead(PluralRules::createRules("a:n % 10 != 1", status));
checkSelect(pr, status, __LINE__, "a", "2", "6", "7", "8", END_MARK);
checkSelect(pr, status, __LINE__, "other", "1", "21", "211", "91", END_MARK);
}
void PluralRulesTest::testSelectRange() {
IcuTestErrorCode status(*this, "testSelectRange");
int32_t d1 = 102;
int32_t d2 = 201;
Locale locale("sl");
// Locale sl has interesting data: one + two => few
auto range = NumberRangeFormatter::withLocale(locale).formatFormattableRange(d1, d2, status);
auto rules = LocalPointer<PluralRules>(PluralRules::forLocale(locale, status), status);
if (status.errIfFailureAndReset()) {
return;
}
// For testing: get plural form of first and second numbers
auto a = NumberFormatter::withLocale(locale).formatDouble(d1, status);
auto b = NumberFormatter::withLocale(locale).formatDouble(d2, status);
assertEquals("First plural", u"two", rules->select(a, status));
assertEquals("Second plural", u"one", rules->select(b, status));
// Check the range plural now:
auto form = rules->select(range, status);
assertEquals("Range plural", u"few", form);
// Test after copying:
PluralRules copy(*rules);
form = copy.select(range, status);
assertEquals("Range plural after copying", u"few", form);
// Test when plural ranges data is unavailable:
auto bare = LocalPointer<PluralRules>(
PluralRules::createRules(u"a: i = 0,1", status), status);
if (status.errIfFailureAndReset()) { return; }
form = bare->select(range, status);
status.expectErrorAndReset(U_UNSUPPORTED_ERROR);
// However, they should not set an error when no data is available for a language.
auto xyz = LocalPointer<PluralRules>(
PluralRules::forLocale("xyz", status));
form = xyz->select(range, status);
assertEquals("Fallback form", u"other", form);
}
void PluralRulesTest::testAvailableLocales() {
// Hash set of (char *) strings.
UErrorCode status = U_ZERO_ERROR;
UHashtable *localeSet = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, uhash_compareLong, &status);
uhash_setKeyDeleter(localeSet, uprv_deleteUObject);
if (U_FAILURE(status)) {
errln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
return;
}
// Check that each locale returned by the iterator is unique.
StringEnumeration *localesEnum = PluralRules::getAvailableLocales(status);
int localeCount = 0;
for (;;) {
const char *locale = localesEnum->next(nullptr, status);
if (U_FAILURE(status)) {
dataerrln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
return;
}
if (locale == nullptr) {
break;
}
localeCount++;
int32_t oldVal = uhash_puti(localeSet, new UnicodeString(locale), 1, &status);
if (oldVal != 0) {
errln("file %s, line %d: locale %s was seen before.", __FILE__, __LINE__, locale);
}
}
// Reset the iterator, verify that we get the same count.
localesEnum->reset(status);
int32_t localeCount2 = 0;
while (localesEnum->next(nullptr, status) != nullptr) {
if (U_FAILURE(status)) {
errln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
break;
}
localeCount2++;
}
if (localeCount != localeCount2) {
errln("file %s, line %d: locale counts differ. They are (%d, %d)",
__FILE__, __LINE__, localeCount, localeCount2);
}
// Instantiate plural rules for each available locale.
localesEnum->reset(status);
for (;;) {
status = U_ZERO_ERROR;
const char *localeName = localesEnum->next(nullptr, status);
if (U_FAILURE(status)) {
errln("file %s, line %d: Error status = %s, locale = %s",
__FILE__, __LINE__, u_errorName(status), localeName);
return;
}
if (localeName == nullptr) {
break;
}
Locale locale = Locale::createFromName(localeName);
PluralRules *pr = PluralRules::forLocale(locale, status);
if (U_FAILURE(status)) {
errln("file %s, line %d: Error %s creating plural rules for locale %s",
__FILE__, __LINE__, u_errorName(status), localeName);
continue;
}
if (pr == nullptr) {
errln("file %s, line %d: Null plural rules for locale %s", __FILE__, __LINE__, localeName);
continue;
}
// Pump some numbers through the plural rules. Can't check for correct results,
// mostly this to tickle any asserts or crashes that may be lurking.
for (double n=0; n<120.0; n+=0.5) {
UnicodeString keyword = pr->select(n);
if (keyword.length() == 0) {
errln("file %s, line %d, empty keyword for n = %g, locale %s",
__FILE__, __LINE__, n, localeName);
}
}
delete pr;
}
uhash_close(localeSet);
delete localesEnum;
}
void PluralRulesTest::testParseErrors() {
// Test rules with syntax errors.
// Creation of PluralRules from them should fail.
static const char *testCases[] = {
"a: n mod 10, is 1",
"a: q is 13",
"a n is 13",
"a: n is 13,",
"a: n is 13, 15, b: n is 4",
"a: n is 1, 3, 4.. ",
"a: n within 5..4",
"A: n is 13", // Uppercase keywords not allowed.
"a: n ! = 3", // spaces in != operator
"a: n = not 3", // '=' not exact equivalent of 'is'
"a: n ! in 3..4", // '!' not exact equivalent of 'not'
"a: n % 37 ! in 3..4"
};
for (int i=0; i<UPRV_LENGTHOF(testCases); i++) {
const char *rules = testCases[i];
UErrorCode status = U_ZERO_ERROR;
PluralRules *pr = PluralRules::createRules(UnicodeString(rules), status);
if (U_SUCCESS(status)) {
errln("file %s, line %d, expected failure with \"%s\".", __FILE__, __LINE__, rules);
}
if (pr != nullptr) {
errln("file %s, line %d, expected nullptr. Rules: \"%s\"", __FILE__, __LINE__, rules);
}
}
}
void PluralRulesTest::testFixedDecimal() {
struct DoubleTestCase {
double n;
int32_t fractionDigitCount;
int64_t fractionDigits;
};
// Check that the internal functions for extracting the decimal fraction digits from
// a double value are working.
static DoubleTestCase testCases[] = {
{1.0, 0, 0},
{123456.0, 0, 0},
{1.1, 1, 1},
{1.23, 2, 23},
{1.234, 3, 234},
{1.2345, 4, 2345},
{1.23456, 5, 23456},
{.1234, 4, 1234},
{.01234, 5, 1234},
{.001234, 6, 1234},
{.0001234, 7, 1234},
{100.1234, 4, 1234},
{100.01234, 5, 1234},
{100.001234, 6, 1234},
{100.0001234, 7, 1234}
};
for (int i=0; i<UPRV_LENGTHOF(testCases); ++i) {
DoubleTestCase &tc = testCases[i];
int32_t numFractionDigits = FixedDecimal::decimals(tc.n);
if (numFractionDigits != tc.fractionDigitCount) {
errln("file %s, line %d: decimals(%g) expected %d, actual %d",
__FILE__, __LINE__, tc.n, tc.fractionDigitCount, numFractionDigits);
continue;
}
int64_t actualFractionDigits = FixedDecimal::getFractionalDigits(tc.n, numFractionDigits);
if (actualFractionDigits != tc.fractionDigits) {
errln("file %s, line %d: getFractionDigits(%g, %d): expected %ld, got %ld",
__FILE__, __LINE__, tc.n, numFractionDigits, tc.fractionDigits, actualFractionDigits);
}
}
}
void PluralRulesTest::testSelectTrailingZeros() {
IcuTestErrorCode status(*this, "testSelectTrailingZeros");
number::UnlocalizedNumberFormatter unf = number::NumberFormatter::with()
.precision(number::Precision::fixedFraction(2));
struct TestCase {
const char* localeName;
const char16_t* expectedDoubleKeyword;
const char16_t* expectedFormattedKeyword;
double number;
} cases[] = {
{"bs", u"few", u"other", 5.2}, // 5.2 => two, but 5.20 => other
{"si", u"one", u"one", 0.0},
{"si", u"one", u"one", 1.0},
{"si", u"one", u"other", 0.1}, // 0.1 => one, but 0.10 => other
{"si", u"one", u"one", 0.01}, // 0.01 => one
{"hsb", u"few", u"few", 1.03}, // (f % 100 == 3) => few
{"hsb", u"few", u"other", 1.3}, // 1.3 => few, but 1.30 => other
};
for (const auto& cas : cases) {
UnicodeString message(UnicodeString(cas.localeName) + u" " + DoubleToUnicodeString(cas.number));
status.setScope(message);
Locale locale(cas.localeName);
LocalPointer<PluralRules> rules(PluralRules::forLocale(locale, status));
if (U_FAILURE(status)) {
dataerrln("Failed to create PluralRules by PluralRules::forLocale(%s): %s\n",
cas.localeName, u_errorName(status));
return;
}
assertEquals(message, cas.expectedDoubleKeyword, rules->select(cas.number));
number::FormattedNumber fn = unf.locale(locale).formatDouble(cas.number, status);
assertEquals(message, cas.expectedFormattedKeyword, rules->select(fn, status));
status.errIfFailureAndReset();
}
}
void PluralRulesTest::compareLocaleResults(const char* loc1, const char* loc2, const char* loc3) {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<PluralRules> rules1(PluralRules::forLocale(loc1, status));
LocalPointer<PluralRules> rules2(PluralRules::forLocale(loc2, status));
LocalPointer<PluralRules> rules3(PluralRules::forLocale(loc3, status));
if (U_FAILURE(status)) {
dataerrln("Failed to create PluralRules for one of %s, %s, %s: %s\n", loc1, loc2, loc3, u_errorName(status));
return;
}
for (int32_t value = 0; value <= 12; value++) {
UnicodeString result1 = rules1->select(value);
UnicodeString result2 = rules2->select(value);
UnicodeString result3 = rules3->select(value);
if (result1 != result2 || result1 != result3) {
errln("PluralRules.select(%d) does not return the same values for %s, %s, %s\n", value, loc1, loc2, loc3);
}
}
}
void PluralRulesTest::testDoubleEqualSign() {
IcuTestErrorCode errorCode(*this, "testDoubleEqualSign");
// ICU-22626
// Two '=' in the rul should not leak.
LocalPointer<PluralRules> rules(
PluralRules::createRules(u"e:c=2=", errorCode), errorCode);
errorCode.expectErrorAndReset(U_UNEXPECTED_TOKEN);
}
void PluralRulesTest::testLocaleExtension() {
IcuTestErrorCode errorCode(*this, "testLocaleExtension");
LocalPointer<PluralRules> rules(PluralRules::forLocale("pt@calendar=gregorian", errorCode));
if (errorCode.errIfFailureAndReset("PluralRules::forLocale()")) { return; }
UnicodeString key = rules->select(1);
assertEquals("pt@calendar=gregorian select(1)", u"one", key);
compareLocaleResults("ar", "ar_SA", "ar_SA@calendar=gregorian");
compareLocaleResults("ru", "ru_UA", "ru-u-cu-RUB");
compareLocaleResults("fr", "fr_CH", "fr@ms=uksystem");
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|