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 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 1996-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "itrbnf.h"
#include "unicode/umachine.h"
#include "unicode/tblcoll.h"
#include "unicode/coleitr.h"
#include "unicode/ures.h"
#include "unicode/ustring.h"
#include "unicode/decimfmt.h"
#include "unicode/udata.h"
#include "cmemory.h"
#include "putilimp.h"
#include "testutil.h"
#include <string.h>
// import com.ibm.text.RuleBasedNumberFormat;
// import com.ibm.test.TestFmwk;
// import java.util.Locale;
// import java.text.NumberFormat;
// current macro not in icu1.8.1
#define TESTCASE(id,test) \
case id: \
name = #test; \
if (exec) { \
logln(#test "---"); \
logln(); \
test(); \
} \
break
void IntlTestRBNF::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/)
{
if (exec) logln("TestSuite RuleBasedNumberFormat");
switch (index) {
#if U_HAVE_RBNF
TESTCASE(0, TestEnglishSpellout);
TESTCASE(1, TestOrdinalAbbreviations);
TESTCASE(2, TestDurations);
TESTCASE(3, TestSpanishSpellout);
TESTCASE(4, TestFrenchSpellout);
TESTCASE(5, TestSwissFrenchSpellout);
TESTCASE(6, TestItalianSpellout);
TESTCASE(7, TestGermanSpellout);
TESTCASE(8, TestThaiSpellout);
TESTCASE(9, TestAPI);
TESTCASE(10, TestFractionalRuleSet);
TESTCASE(11, TestSwedishSpellout);
TESTCASE(12, TestBelgianFrenchSpellout);
TESTCASE(13, TestSmallValues);
TESTCASE(14, TestLocalizations);
TESTCASE(15, TestAllLocales);
TESTCASE(16, TestHebrewFraction);
TESTCASE(17, TestPortugueseSpellout);
TESTCASE(18, TestMultiplierSubstitution);
TESTCASE(19, TestSetDecimalFormatSymbols);
TESTCASE(20, TestPluralRules);
TESTCASE(21, TestMultiplePluralRules);
TESTCASE(22, TestInfinityNaN);
TESTCASE(23, TestVariableDecimalPoint);
TESTCASE(24, TestLargeNumbers);
TESTCASE(25, TestCompactDecimalFormatStyle);
TESTCASE(26, TestParseFailure);
TESTCASE(27, TestMinMaxIntegerDigitsIgnored);
TESTCASE(28, TestNorwegianSpellout);
TESTCASE(29, TestNumberingSystem);
TESTCASE(30, TestDFRounding);
TESTCASE(31, TestMemoryLeak22899);
TESTCASE(32, TestParseRuleDescriptorOverflow23002);
TESTCASE(33, TestInfiniteRecursion);
TESTCASE(34, testOmissionReplacementWithPluralRules);
TESTCASE(35, TestNullDereferenceWRITE23149);
TESTCASE(36, TestNullDereferenceREAD23184);
#else
TESTCASE(0, TestRBNFDisabled);
#endif
default:
name = "";
break;
}
}
#if U_HAVE_RBNF
void IntlTestRBNF::TestHebrewFraction() {
// this is the expected output for 123.45, with no '<' in it.
char16_t text1[] = {
0x05de, 0x05d0, 0x05d4, 0x0020,
0x05e2, 0x05e9, 0x05e8, 0x05d9, 0x05dd, 0x0020,
0x05d5, 0x05e9, 0x05dc, 0x05d5, 0x05e9, 0x0020,
0x05e0, 0x05e7, 0x05d5, 0x05d3, 0x05d4, 0x0020,
0x05d0, 0x05e8, 0x05d1, 0x05e2, 0x0020,
0x05d7, 0x05de, 0x05e9, 0x0000,
};
char16_t text2[] = {
0x05DE, 0x05D0, 0x05D4, 0x0020,
0x05E2, 0x05E9, 0x05E8, 0x05D9, 0x05DD, 0x0020,
0x05D5, 0x05E9, 0x05DC, 0x05D5, 0x05E9, 0x0020,
0x05E0, 0x05E7, 0x05D5, 0x05D3, 0x05D4, 0x0020,
0x05D0, 0x05E4, 0x05E1, 0x0020,
0x05D0, 0x05E4, 0x05E1, 0x0020,
0x05D0, 0x05E8, 0x05D1, 0x05E2, 0x0020,
0x05D7, 0x05DE, 0x05E9, 0x0000,
};
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter = new RuleBasedNumberFormat(URBNF_SPELLOUT, "he_IL", status);
if (status == U_MISSING_RESOURCE_ERROR || status == U_FILE_ACCESS_ERROR) {
errcheckln(status, "Failed in constructing RuleBasedNumberFormat - %s", u_errorName(status));
delete formatter;
return;
}
UnicodeString result;
Formattable parseResult;
ParsePosition pp(0);
{
UnicodeString expected(text1);
formatter->format(123.45, result);
if (result != expected) {
errln(UnicodeString("expected '") + TestUtility::hex(expected) + "'\nbut got: '" + TestUtility::hex(result) + "'");
} else {
// formatter->parse(result, parseResult, pp);
// if (parseResult.getDouble() != 123.45) {
// errln("expected 123.45 but got: %g", parseResult.getDouble());
// }
}
}
{
UnicodeString expected(text2);
result.remove();
formatter->format(123.0045, result);
if (result != expected) {
errln(UnicodeString("expected '") + TestUtility::hex(expected) + "'\nbut got: '" + TestUtility::hex(result) + "'");
} else {
pp.setIndex(0);
// formatter->parse(result, parseResult, pp);
// if (parseResult.getDouble() != 123.0045) {
// errln("expected 123.0045 but got: %g", parseResult.getDouble());
// }
}
}
delete formatter;
}
void
IntlTestRBNF::TestAPI() {
// This test goes through the APIs that were not tested before.
// These tests are too small to have separate test classes/functions
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status);
if (status == U_MISSING_RESOURCE_ERROR || status == U_FILE_ACCESS_ERROR) {
dataerrln("Unable to create formatter. - %s", u_errorName(status));
delete formatter;
return;
}
logln("RBNF API test starting");
// test clone
{
logln("Testing Clone");
RuleBasedNumberFormat* rbnfClone = formatter->clone();
if(rbnfClone != nullptr) {
if(!(*rbnfClone == *formatter)) {
errln("Clone should be semantically equivalent to the original!");
}
delete rbnfClone;
} else {
errln("Cloning failed!");
}
}
// test assignment
{
logln("Testing assignment operator");
RuleBasedNumberFormat assignResult(URBNF_SPELLOUT, Locale("es", "ES", ""), status);
assignResult = *formatter;
if(!(assignResult == *formatter)) {
errln("Assignment result should be semantically equivalent to the original!");
}
}
// test rule constructor
{
logln("Testing rule constructor");
LocalUResourceBundlePointer en(ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "rbnf", "en", &status));
if(U_FAILURE(status)) {
errln("Unable to access resource bundle with data!");
} else {
int32_t ruleLen = 0;
int32_t len = 0;
LocalUResourceBundlePointer rbnfRules(ures_getByKey(en.getAlias(), "RBNFRules", nullptr, &status));
LocalUResourceBundlePointer ruleSets(ures_getByKey(rbnfRules.getAlias(), "SpelloutRules", nullptr, &status));
UnicodeString desc;
while (ures_hasNext(ruleSets.getAlias())) {
const char16_t* currentString = ures_getNextString(ruleSets.getAlias(), &len, nullptr, &status);
ruleLen += len;
desc.append(currentString);
}
const char16_t *spelloutRules = desc.getTerminatedBuffer();
if(U_FAILURE(status) || ruleLen == 0 || spelloutRules == nullptr) {
errln("Unable to access the rules string!");
} else {
UParseError perror;
RuleBasedNumberFormat ruleCtorResult(spelloutRules, Locale::getUS(), perror, status);
if(!(ruleCtorResult == *formatter)) {
errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
}
// Jitterbug 4452, for coverage
RuleBasedNumberFormat nf(spelloutRules, UnicodeString(""), Locale::getUS(), perror, status);
if(!(nf == *formatter)) {
errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
}
}
}
}
// test getRules
{
logln("Testing getRules function");
UnicodeString rules = formatter->getRules();
UParseError perror;
RuleBasedNumberFormat fromRulesResult(rules, Locale::getUS(), perror, status);
if(!(fromRulesResult == *formatter)) {
errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!");
}
}
{
logln("Testing copy constructor");
RuleBasedNumberFormat copyCtorResult(*formatter);
if(!(copyCtorResult == *formatter)) {
errln("Copy constructor result result should be semantically equivalent to the original!");
}
}
#if !UCONFIG_NO_COLLATION
// test ruleset names
{
logln("Testing getNumberOfRuleSetNames, getRuleSetName and format using rule set names");
int32_t noOfRuleSetNames = formatter->getNumberOfRuleSetNames();
if(noOfRuleSetNames == 0) {
errln("Number of rule set names should be more than zero");
}
UnicodeString ruleSetName;
int32_t i = 0;
int32_t intFormatNum = 34567;
double doubleFormatNum = 893411.234;
logln("number of rule set names is %i", noOfRuleSetNames);
for(i = 0; i < noOfRuleSetNames; i++) {
FieldPosition pos1, pos2;
UnicodeString intFormatResult, doubleFormatResult;
Formattable intParseResult, doubleParseResult;
ruleSetName = formatter->getRuleSetName(i);
log("Rule set name %i is ", i);
log(ruleSetName);
logln(". Format results are: ");
intFormatResult = formatter->format(intFormatNum, ruleSetName, intFormatResult, pos1, status);
doubleFormatResult = formatter->format(doubleFormatNum, ruleSetName, doubleFormatResult, pos2, status);
if(U_FAILURE(status)) {
errln("Format using a rule set failed");
break;
}
logln(intFormatResult);
logln(doubleFormatResult);
formatter->setLenient(true);
formatter->parse(intFormatResult, intParseResult, status);
formatter->parse(doubleFormatResult, doubleParseResult, status);
logln("Parse results for lenient = true, %i, %f", intParseResult.getLong(), doubleParseResult.getDouble());
formatter->setLenient(false);
formatter->parse(intFormatResult, intParseResult, status);
formatter->parse(doubleFormatResult, doubleParseResult, status);
logln("Parse results for lenient = false, %i, %f", intParseResult.getLong(), doubleParseResult.getDouble());
if(U_FAILURE(status)) {
errln("Error during parsing");
}
intFormatResult = formatter->format(intFormatNum, "BLABLA", intFormatResult, pos1, status);
if(U_SUCCESS(status)) {
errln("Using invalid rule set name should have failed");
break;
}
status = U_ZERO_ERROR;
doubleFormatResult = formatter->format(doubleFormatNum, "TRUC", doubleFormatResult, pos2, status);
if(U_SUCCESS(status)) {
errln("Using invalid rule set name should have failed");
break;
}
status = U_ZERO_ERROR;
}
status = U_ZERO_ERROR;
}
#endif
// test API
UnicodeString expected("four point five","");
logln("Testing format(double)");
UnicodeString result;
formatter->format(4.5,result);
if(result != expected) {
errln("Formatted 4.5, expected " + expected + " got " + result);
} else {
logln("Formatted 4.5, expected " + expected + " got " + result);
}
result.remove();
expected = "four";
formatter->format(static_cast<int32_t>(4), result);
if(result != expected) {
errln("Formatted 4, expected " + expected + " got " + result);
} else {
logln("Formatted 4, expected " + expected + " got " + result);
}
result.remove();
FieldPosition pos;
formatter->format(static_cast<int64_t>(4), result, pos, status = U_ZERO_ERROR);
if(result != expected) {
errln("Formatted 4 int64_t, expected " + expected + " got " + result);
} else {
logln("Formatted 4 int64_t, expected " + expected + " got " + result);
}
//Jitterbug 4452, for coverage
result.remove();
FieldPosition pos2;
formatter->format(static_cast<int64_t>(4), formatter->getRuleSetName(0), result, pos2, status = U_ZERO_ERROR);
if(result != expected) {
errln("Formatted 4 int64_t, expected " + expected + " got " + result);
} else {
logln("Formatted 4 int64_t, expected " + expected + " got " + result);
}
// clean up
logln("Cleaning up");
delete formatter;
}
/**
* Perform a simple spot check on the parsing going into an infinite loop for alternate rules.
*/
void IntlTestRBNF::TestMultiplePluralRules() {
// This is trying to model the feminine form, but don't worry about the details too much.
// We're trying to test the plural rules where there are different prefixes.
UnicodeString rules("%spellout-cardinal-feminine-genitive:"
"0: zero;"
"1: ono;"
"2: two;"
"1000: << $(cardinal,one{thousand}few{thousanF}other{thousanO})$[ >>];"
"%spellout-cardinal-feminine:"
"x.x: [<< $(cardinal,one{singleton}other{plurality})$ ]>%%fractions>;"
"0: zero;"
"1: one;"
"2: two;"
"1000: << $(cardinal,one{thousand}few{thousanF}other{thousanO})$[ >>];"
"%%fractions:"
"10: <%spellout-cardinal-feminine< $(cardinal,one{oneth}other{tenth})$;"
"100: <%spellout-cardinal-feminine< $(cardinal,one{1hundredth}other{hundredth})$;");
UErrorCode status = U_ZERO_ERROR;
UParseError pError;
RuleBasedNumberFormat formatter(rules, Locale("ru"), pError, status);
Formattable result;
UnicodeString resultStr;
FieldPosition pos;
if (U_FAILURE(status)) {
dataerrln("Unable to create formatter - %s", u_errorName(status));
return;
}
formatter.parse(formatter.format(1000.0, resultStr, pos, status), result, status);
if (1000 != result.getLong() || resultStr != UNICODE_STRING_SIMPLE("one thousand")) {
errln("RuleBasedNumberFormat did not return the correct value. Got: %d", result.getLong());
errln(resultStr);
}
resultStr.remove();
formatter.parse(formatter.format(1000.0, UnicodeString("%spellout-cardinal-feminine-genitive"), resultStr, pos, status), result, status);
if (1000 != result.getLong() || resultStr != UNICODE_STRING_SIMPLE("ono thousand")) {
errln("RuleBasedNumberFormat(cardinal-feminine-genitive) did not return the correct value. Got: %d", result.getLong());
errln(resultStr);
}
resultStr.remove();
formatter.parse(formatter.format(1000.0, UnicodeString("%spellout-cardinal-feminine"), resultStr, pos, status), result, status);
if (1000 != result.getLong() || resultStr != UNICODE_STRING_SIMPLE("one thousand")) {
errln("RuleBasedNumberFormat(spellout-cardinal-feminine) did not return the correct value. Got: %d", result.getLong());
errln(resultStr);
}
static const char* const testData[][2] = {
{ "0", "zero" },
{ "1", "one" },
{ "2", "two" },
{ "0.1", "one oneth" },
{ "0.2", "two tenth" },
{ "1.1", "one singleton one oneth" },
{ "1.2", "one singleton two tenth" },
{ "2.1", "two plurality one oneth" },
{ "2.2", "two plurality two tenth" },
{ "0.01", "one 1hundredth" },
{ "0.02", "two hundredth" },
{ nullptr, nullptr }
};
doTest(&formatter, testData, true);
}
void IntlTestRBNF::TestFractionalRuleSet()
{
UnicodeString fracRules(
"%main:\n"
// this rule formats the number if it's 1 or more. It formats
// the integral part using a DecimalFormat ("#,##0" puts
// thousands separators in the right places) and the fractional
// part using %%frac. If there is no fractional part, it
// just shows the integral part.
" x.0: <#,##0<[ >%%frac>];\n"
// this rule formats the number if it's between 0 and 1. It
// shows only the fractional part (0.5 shows up as "1/2," not
// "0 1/2")
" 0.x: >%%frac>;\n"
// the fraction rule set. This works the same way as the one in the
// preceding example: We multiply the fractional part of the number
// being formatted by each rule's base value and use the rule that
// produces the result closest to 0 (or the first rule that produces 0).
// Since we only provide rules for the numbers from 2 to 10, we know
// we'll get a fraction with a denominator between 2 and 10.
// "<0<" causes the numerator of the fraction to be formatted
// using numerals
"%%frac:\n"
" 2: 1/2;\n"
" 3: <0</3;\n"
" 4: <0</4;\n"
" 5: <0</5;\n"
" 6: <0</6;\n"
" 7: <0</7;\n"
" 8: <0</8;\n"
" 9: <0</9;\n"
" 10: <0</10;\n");
// mondo hack
int len = fracRules.length();
int change = 2;
for (int i = 0; i < len; ++i) {
char16_t ch = fracRules.charAt(i);
if (ch == '\n') {
change = 2; // change ok
} else if (ch == ':') {
change = 1; // change, but once we hit a non-space char, don't change
} else if (ch == ' ') {
if (change != 0) {
fracRules.setCharAt(i, static_cast<char16_t>(0x200e));
}
} else {
if (change == 1) {
change = 0;
}
}
}
UErrorCode status = U_ZERO_ERROR;
UParseError perror;
RuleBasedNumberFormat formatter(fracRules, Locale::getEnglish(), perror, status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "0", "0" },
{ ".1", "1/10" },
{ ".11", "1/9" },
{ ".125", "1/8" },
{ ".1428", "1/7" },
{ ".1667", "1/6" },
{ ".2", "1/5" },
{ ".25", "1/4" },
{ ".333", "1/3" },
{ ".5", "1/2" },
{ "1.1", "1 1/10" },
{ "2.11", "2 1/9" },
{ "3.125", "3 1/8" },
{ "4.1428", "4 1/7" },
{ "5.1667", "5 1/6" },
{ "6.2", "6 1/5" },
{ "7.25", "7 1/4" },
{ "8.333", "8 1/3" },
{ "9.5", "9 1/2" },
{ ".2222", "2/9" },
{ ".4444", "4/9" },
{ ".5555", "5/9" },
{ "1.2856", "1 2/7" },
{ nullptr, nullptr }
};
doTest(&formatter, testData, false); // exact values aren't parsable from fractions
}
}
#if 0
#define LLAssert(a) \
if (!(a)) errln("FAIL: " #a)
void IntlTestRBNF::TestLLongConstructors()
{
logln("Testing constructors");
// constant (shouldn't really be public)
LLAssert(llong(llong::kD32).asDouble() == llong::kD32);
// internal constructor (shouldn't really be public)
LLAssert(llong(0, 1).asDouble() == 1);
LLAssert(llong(1, 0).asDouble() == llong::kD32);
LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1);
// public empty constructor
LLAssert(llong().asDouble() == 0);
// public int32_t constructor
LLAssert(llong((int32_t)0).asInt() == (int32_t)0);
LLAssert(llong((int32_t)1).asInt() == (int32_t)1);
LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1);
LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff);
LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1);
LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000);
// public int16_t constructor
LLAssert(llong((int16_t)0).asInt() == (int16_t)0);
LLAssert(llong((int16_t)1).asInt() == (int16_t)1);
LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1);
LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff);
LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff);
LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000);
// public int8_t constructor
LLAssert(llong((int8_t)0).asInt() == (int8_t)0);
LLAssert(llong((int8_t)1).asInt() == (int8_t)1);
LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1);
LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f);
LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff);
LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80);
// public uint16_t constructor
LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0);
LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1);
LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1);
LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff);
LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff);
LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000);
// public uint32_t constructor
LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0);
LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1);
LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1);
LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff);
LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1);
LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
// public double constructor
LLAssert(llong((double)0).asDouble() == (double)0);
LLAssert(llong((double)1).asDouble() == (double)1);
LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff);
LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000);
LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001);
// can't access uprv_maxmantissa, so fake it
double maxmantissa = (llong((int32_t)1) << 40).asDouble();
LLAssert(llong(maxmantissa).asDouble() == maxmantissa);
LLAssert(llong(-maxmantissa).asDouble() == -maxmantissa);
// copy constructor
LLAssert(llong(llong(0, 1)).asDouble() == 1);
LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32);
LLAssert(llong(llong(-1, (uint32_t)-1)).asDouble() == -1);
// asInt - test unsigned to signed narrowing conversion
LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff);
LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000);
// asUInt - test signed to unsigned narrowing conversion
LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1);
LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
// asDouble already tested
}
void IntlTestRBNF::TestLLongSimpleOperators()
{
logln("Testing simple operators");
// operator==
LLAssert(llong() == llong(0, 0));
LLAssert(llong(1,0) == llong(1, 0));
LLAssert(llong(0,1) == llong(0, 1));
// operator!=
LLAssert(llong(1,0) != llong(1,1));
LLAssert(llong(0,1) != llong(1,1));
LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff));
// unsigned >
LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff)));
// unsigned <
LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1)));
// unsigned >=
LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff)));
LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1)));
// unsigned <=
LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1)));
LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1)));
// operator>
LLAssert(llong(1, 1) > llong(1, 0));
LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff));
LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0));
LLAssert(llong(1, 0) > llong(0, 0x7fffffff));
LLAssert(llong(1, 0) > llong(0, 0xffffffff));
LLAssert(llong(0, 0) > llong(0x80000000, 1));
// operator<
LLAssert(llong(1, 0) < llong(1, 1));
LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000));
LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1));
LLAssert(llong(0, 0x7fffffff) < llong(1, 0));
LLAssert(llong(0, 0xffffffff) < llong(1, 0));
LLAssert(llong(0x80000000, 1) < llong(0, 0));
// operator>=
LLAssert(llong(1, 1) >= llong(1, 0));
LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff));
LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0));
LLAssert(llong(1, 0) >= llong(0, 0x7fffffff));
LLAssert(llong(1, 0) >= llong(0, 0xffffffff));
LLAssert(llong(0, 0) >= llong(0x80000000, 1));
LLAssert(llong() >= llong(0, 0));
LLAssert(llong(1,0) >= llong(1, 0));
LLAssert(llong(0,1) >= llong(0, 1));
// operator<=
LLAssert(llong(1, 0) <= llong(1, 1));
LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000));
LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1));
LLAssert(llong(0, 0x7fffffff) <= llong(1, 0));
LLAssert(llong(0, 0xffffffff) <= llong(1, 0));
LLAssert(llong(0x80000000, 1) <= llong(0, 0));
LLAssert(llong() <= llong(0, 0));
LLAssert(llong(1,0) <= llong(1, 0));
LLAssert(llong(0,1) <= llong(0, 1));
// operator==(int32)
LLAssert(llong() == (int32_t)0);
LLAssert(llong(0,1) == (int32_t)1);
// operator!=(int32)
LLAssert(llong(1,0) != (int32_t)0);
LLAssert(llong(0,1) != (int32_t)2);
LLAssert(llong(0,0xffffffff) != (int32_t)-1);
llong negOne(0xffffffff, 0xffffffff);
// operator>(int32)
LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff);
LLAssert(negOne > (int32_t)-2);
LLAssert(llong(1, 0) > (int32_t)0x7fffffff);
LLAssert(llong(0, 0) > (int32_t)-1);
// operator<(int32)
LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff);
LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1);
// operator>=(int32)
LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff);
LLAssert(negOne >= (int32_t)-2);
LLAssert(llong(1, 0) >= (int32_t)0x7fffffff);
LLAssert(llong(0, 0) >= (int32_t)-1);
LLAssert(llong() >= (int32_t)0);
LLAssert(llong(0,1) >= (int32_t)1);
// operator<=(int32)
LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff);
LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1);
LLAssert(llong() <= (int32_t)0);
LLAssert(llong(0,1) <= (int32_t)1);
// operator=
LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1);
// operator <<=
LLAssert((llong(1, 1) <<= 0) == llong(1, 1));
LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000));
LLAssert((llong(1, 1) <<= 32) == llong(1, 0));
LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0));
LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used
LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used
// operator <<
LLAssert((llong((int32_t)1) << 5).asUInt() == 32);
// operator >>= (sign extended)
LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc));
LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde));
LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff));
LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000));
LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used
// operator >> sign extended)
LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde));
// ushr (right shift without sign extension)
LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc));
LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde));
LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1));
LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000));
LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used
// operator&(llong)
LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
// operator|(llong)
LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
// operator^(llong)
LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
// operator&(uint32)
LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
// operator|(uint32)
LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
// operator^(uint32)
LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
// operator~
LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa));
// operator&=(llong)
LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
// operator|=(llong)
LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
// operator^=(llong)
LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
// operator&=(uint32)
LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
// operator|=(uint32)
LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
// operator^=(uint32)
LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
// prefix inc
LLAssert(llong(1, 0) == ++llong(0,0xffffffff));
// prefix dec
LLAssert(llong(0,0xffffffff) == --llong(1, 0));
// postfix inc
{
llong n(0, 0xffffffff);
LLAssert(llong(0, 0xffffffff) == n++);
LLAssert(llong(1, 0) == n);
}
// postfix dec
{
llong n(1, 0);
LLAssert(llong(1, 0) == n--);
LLAssert(llong(0, 0xffffffff) == n);
}
// unary minus
LLAssert(llong(0, 0) == -llong(0, 0));
LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1));
LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff));
LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1));
LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow
// operator-=
{
llong n;
LLAssert((n -= llong(0, 1)) == llong(0xffffffff, 0xffffffff));
LLAssert(n == llong(0xffffffff, 0xffffffff));
n = llong(1, 0);
LLAssert((n -= llong(0, 1)) == llong(0, 0xffffffff));
LLAssert(n == llong(0, 0xffffffff));
}
// operator-
{
llong n;
LLAssert((n - llong(0, 1)) == llong(0xffffffff, 0xffffffff));
LLAssert(n == llong(0, 0));
n = llong(1, 0);
LLAssert((n - llong(0, 1)) == llong(0, 0xffffffff));
LLAssert(n == llong(1, 0));
}
// operator+=
{
llong n(0xffffffff, 0xffffffff);
LLAssert((n += llong(0, 1)) == llong(0, 0));
LLAssert(n == llong(0, 0));
n = llong(0, 0xffffffff);
LLAssert((n += llong(0, 1)) == llong(1, 0));
LLAssert(n == llong(1, 0));
}
// operator+
{
llong n(0xffffffff, 0xffffffff);
LLAssert((n + llong(0, 1)) == llong(0, 0));
LLAssert(n == llong(0xffffffff, 0xffffffff));
n = llong(0, 0xffffffff);
LLAssert((n + llong(0, 1)) == llong(1, 0));
LLAssert(n == llong(0, 0xffffffff));
}
}
void IntlTestRBNF::TestLLong()
{
logln("Starting TestLLong");
TestLLongConstructors();
TestLLongSimpleOperators();
logln("Testing operator*=, operator*");
// operator*=, operator*
// small and large values, positive, &NEGative, zero
// also test commutivity
{
const llong ZERO;
const llong ONE(0, 1);
const llong NEG_ONE((int32_t)-1);
const llong THREE(0, 3);
const llong NEG_THREE((int32_t)-3);
const llong TWO_TO_16(0, 0x10000);
const llong NEG_TWO_TO_16 = -TWO_TO_16;
const llong TWO_TO_32(1, 0);
const llong NEG_TWO_TO_32 = -TWO_TO_32;
const llong NINE(0, 9);
const llong NEG_NINE = -NINE;
const llong TWO_TO_16X3(0, 0x00030000);
const llong NEG_TWO_TO_16X3 = -TWO_TO_16X3;
const llong TWO_TO_32X3(3, 0);
const llong NEG_TWO_TO_32X3 = -TWO_TO_32X3;
const llong TWO_TO_48(0x10000, 0);
const llong NEG_TWO_TO_48 = -TWO_TO_48;
const int32_t VALUE_WIDTH = 9;
const llong* values[VALUE_WIDTH] = {
&ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32
};
const llong* answers[VALUE_WIDTH*VALUE_WIDTH] = {
&ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO,
&ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32,
&ZERO, &NEG_ONE, &ONE, &NEG_THREE, &THREE, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_32, &TWO_TO_32,
&ZERO, &THREE, &NEG_THREE, &NINE, &NEG_NINE, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32X3, &NEG_TWO_TO_32X3,
&ZERO, &NEG_THREE, &THREE, &NEG_NINE, &NINE, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32X3, &TWO_TO_32X3,
&ZERO, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_48, &NEG_TWO_TO_48,
&ZERO, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_48, &TWO_TO_48,
&ZERO, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_32X3, &NEG_TWO_TO_32X3, &TWO_TO_48, &NEG_TWO_TO_48, &ZERO, &ZERO,
&ZERO, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_32X3, &TWO_TO_32X3, &NEG_TWO_TO_48, &TWO_TO_48, &ZERO, &ZERO
};
for (int i = 0; i < VALUE_WIDTH; ++i) {
for (int j = 0; j < VALUE_WIDTH; ++j) {
llong lhs = *values[i];
llong rhs = *values[j];
llong ans = *answers[i*VALUE_WIDTH + j];
llong n = lhs;
LLAssert((n *= rhs) == ans);
LLAssert(n == ans);
n = lhs;
LLAssert((n * rhs) == ans);
LLAssert(n == lhs);
}
}
}
logln("Testing operator/=, operator/");
// operator/=, operator/
// test num = 0, div = 0, pos/neg, > 2^32, div > num
{
const llong ZERO;
const llong ONE(0, 1);
const llong NEG_ONE = -ONE;
const llong MAX(0x7fffffff, 0xffffffff);
const llong MIN(0x80000000, 0);
const llong TWO(0, 2);
const llong NEG_TWO = -TWO;
const llong FIVE(0, 5);
const llong NEG_FIVE = -FIVE;
const llong TWO_TO_32(1, 0);
const llong NEG_TWO_TO_32 = -TWO_TO_32;
const llong TWO_TO_32d5 = llong(TWO_TO_32.asDouble()/5.0);
const llong NEG_TWO_TO_32d5 = -TWO_TO_32d5;
const llong TWO_TO_32X5 = TWO_TO_32 * FIVE;
const llong NEG_TWO_TO_32X5 = -TWO_TO_32X5;
const llong* tuples[] = { // lhs, rhs, ans
&ZERO, &ZERO, &ZERO,
&ONE, &ZERO,&MAX,
&NEG_ONE, &ZERO, &MIN,
&ONE, &ONE, &ONE,
&ONE, &NEG_ONE, &NEG_ONE,
&NEG_ONE, &ONE, &NEG_ONE,
&NEG_ONE, &NEG_ONE, &ONE,
&FIVE, &TWO, &TWO,
&FIVE, &NEG_TWO, &NEG_TWO,
&NEG_FIVE, &TWO, &NEG_TWO,
&NEG_FIVE, &NEG_TWO, &TWO,
&TWO, &FIVE, &ZERO,
&TWO, &NEG_FIVE, &ZERO,
&NEG_TWO, &FIVE, &ZERO,
&NEG_TWO, &NEG_FIVE, &ZERO,
&TWO_TO_32, &TWO_TO_32, &ONE,
&TWO_TO_32, &NEG_TWO_TO_32, &NEG_ONE,
&NEG_TWO_TO_32, &TWO_TO_32, &NEG_ONE,
&NEG_TWO_TO_32, &NEG_TWO_TO_32, &ONE,
&TWO_TO_32, &FIVE, &TWO_TO_32d5,
&TWO_TO_32, &NEG_FIVE, &NEG_TWO_TO_32d5,
&NEG_TWO_TO_32, &FIVE, &NEG_TWO_TO_32d5,
&NEG_TWO_TO_32, &NEG_FIVE, &TWO_TO_32d5,
&TWO_TO_32X5, &FIVE, &TWO_TO_32,
&TWO_TO_32X5, &NEG_FIVE, &NEG_TWO_TO_32,
&NEG_TWO_TO_32X5, &FIVE, &NEG_TWO_TO_32,
&NEG_TWO_TO_32X5, &NEG_FIVE, &TWO_TO_32,
&TWO_TO_32X5, &TWO_TO_32, &FIVE,
&TWO_TO_32X5, &NEG_TWO_TO_32, &NEG_FIVE,
&NEG_TWO_TO_32X5, &NEG_TWO_TO_32, &FIVE,
&NEG_TWO_TO_32X5, &TWO_TO_32, &NEG_FIVE
};
const int TUPLE_WIDTH = 3;
const int TUPLE_COUNT = UPRV_LENGTHOF(tuples)/TUPLE_WIDTH;
for (int i = 0; i < TUPLE_COUNT; ++i) {
const llong lhs = *tuples[i*TUPLE_WIDTH+0];
const llong rhs = *tuples[i*TUPLE_WIDTH+1];
const llong ans = *tuples[i*TUPLE_WIDTH+2];
llong n = lhs;
if (!((n /= rhs) == ans)) {
errln("fail: (n /= rhs) == ans");
}
LLAssert(n == ans);
n = lhs;
LLAssert((n / rhs) == ans);
LLAssert(n == lhs);
}
}
logln("Testing operator%%=, operator%%");
//operator%=, operator%
{
const llong ZERO;
const llong ONE(0, 1);
const llong TWO(0, 2);
const llong THREE(0,3);
const llong FOUR(0, 4);
const llong FIVE(0, 5);
const llong SIX(0, 6);
const llong NEG_ONE = -ONE;
const llong NEG_TWO = -TWO;
const llong NEG_THREE = -THREE;
const llong NEG_FOUR = -FOUR;
const llong NEG_FIVE = -FIVE;
const llong NEG_SIX = -SIX;
const llong NINETY_NINE(0, 99);
const llong HUNDRED(0, 100);
const llong HUNDRED_ONE(0, 101);
const llong BIG(0x12345678, 0x9abcdef0);
const llong BIG_FIVE(BIG * FIVE);
const llong BIG_FIVEm1 = BIG_FIVE - ONE;
const llong BIG_FIVEp1 = BIG_FIVE + ONE;
const llong* tuples[] = {
&ZERO, &FIVE, &ZERO,
&ONE, &FIVE, &ONE,
&TWO, &FIVE, &TWO,
&THREE, &FIVE, &THREE,
&FOUR, &FIVE, &FOUR,
&FIVE, &FIVE, &ZERO,
&SIX, &FIVE, &ONE,
&ZERO, &NEG_FIVE, &ZERO,
&ONE, &NEG_FIVE, &ONE,
&TWO, &NEG_FIVE, &TWO,
&THREE, &NEG_FIVE, &THREE,
&FOUR, &NEG_FIVE, &FOUR,
&FIVE, &NEG_FIVE, &ZERO,
&SIX, &NEG_FIVE, &ONE,
&NEG_ONE, &FIVE, &NEG_ONE,
&NEG_TWO, &FIVE, &NEG_TWO,
&NEG_THREE, &FIVE, &NEG_THREE,
&NEG_FOUR, &FIVE, &NEG_FOUR,
&NEG_FIVE, &FIVE, &ZERO,
&NEG_SIX, &FIVE, &NEG_ONE,
&NEG_ONE, &NEG_FIVE, &NEG_ONE,
&NEG_TWO, &NEG_FIVE, &NEG_TWO,
&NEG_THREE, &NEG_FIVE, &NEG_THREE,
&NEG_FOUR, &NEG_FIVE, &NEG_FOUR,
&NEG_FIVE, &NEG_FIVE, &ZERO,
&NEG_SIX, &NEG_FIVE, &NEG_ONE,
&NINETY_NINE, &FIVE, &FOUR,
&HUNDRED, &FIVE, &ZERO,
&HUNDRED_ONE, &FIVE, &ONE,
&BIG_FIVEm1, &FIVE, &FOUR,
&BIG_FIVE, &FIVE, &ZERO,
&BIG_FIVEp1, &FIVE, &ONE
};
const int TUPLE_WIDTH = 3;
const int TUPLE_COUNT = UPRV_LENGTHOF(tuples)/TUPLE_WIDTH;
for (int i = 0; i < TUPLE_COUNT; ++i) {
const llong lhs = *tuples[i*TUPLE_WIDTH+0];
const llong rhs = *tuples[i*TUPLE_WIDTH+1];
const llong ans = *tuples[i*TUPLE_WIDTH+2];
llong n = lhs;
if (!((n %= rhs) == ans)) {
errln("fail: (n %= rhs) == ans");
}
LLAssert(n == ans);
n = lhs;
LLAssert((n % rhs) == ans);
LLAssert(n == lhs);
}
}
logln("Testing pow");
// pow
LLAssert(llong(0, 0).pow(0) == llong(0, 0));
LLAssert(llong(0, 0).pow(2) == llong(0, 0));
LLAssert(llong(0, 2).pow(0) == llong(0, 1));
LLAssert(llong(0, 2).pow(2) == llong(0, 4));
LLAssert(llong(0, 2).pow(32) == llong(1, 0));
LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5));
// absolute value
{
const llong n(0xffffffff,0xffffffff);
LLAssert(n.abs() == llong(0, 1));
}
#ifdef RBNF_DEBUG
logln("Testing atoll");
// atoll
const char empty[] = "";
const char zero[] = "0";
const char neg_one[] = "-1";
const char neg_12345[] = "-12345";
const char big1[] = "123456789abcdef0";
const char big2[] = "fFfFfFfFfFfFfFfF";
LLAssert(llong::atoll(empty) == llong(0, 0));
LLAssert(llong::atoll(zero) == llong(0, 0));
LLAssert(llong::atoll(neg_one) == llong(0xffffffff, 0xffffffff));
LLAssert(llong::atoll(neg_12345) == -llong(0, 12345));
LLAssert(llong::atoll(big1, 16) == llong(0x12345678, 0x9abcdef0));
LLAssert(llong::atoll(big2, 16) == llong(0xffffffff, 0xffffffff));
#endif
// u_atoll
const char16_t uempty[] = { 0 };
const char16_t uzero[] = { 0x30, 0 };
const char16_t uneg_one[] = { 0x2d, 0x31, 0 };
const char16_t uneg_12345[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 };
const char16_t ubig1[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 };
const char16_t ubig2[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 };
LLAssert(llong::utoll(uempty) == llong(0, 0));
LLAssert(llong::utoll(uzero) == llong(0, 0));
LLAssert(llong::utoll(uneg_one) == llong(0xffffffff, 0xffffffff));
LLAssert(llong::utoll(uneg_12345) == -llong(0, 12345));
LLAssert(llong::utoll(ubig1, 16) == llong(0x12345678, 0x9abcdef0));
LLAssert(llong::utoll(ubig2, 16) == llong(0xffffffff, 0xffffffff));
#ifdef RBNF_DEBUG
logln("Testing lltoa");
// lltoa
{
char buf[64]; // ascii
LLAssert((llong(0, 0).lltoa(buf, (uint32_t)sizeof(buf)) == 1) && (strcmp(buf, zero) == 0));
LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf, (uint32_t)sizeof(buf)) == 2) && (strcmp(buf, neg_one) == 0));
LLAssert(((-llong(0, 12345)).lltoa(buf, (uint32_t)sizeof(buf)) == 6) && (strcmp(buf, neg_12345) == 0));
LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf, (uint32_t)sizeof(buf), 16) == 16) && (strcmp(buf, big1) == 0));
}
#endif
logln("Testing u_lltoa");
// u_lltoa
{
char16_t buf[64];
LLAssert((llong(0, 0).lltou(buf, (uint32_t)sizeof(buf)) == 1) && (u_strcmp(buf, uzero) == 0));
LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf, (uint32_t)sizeof(buf)) == 2) && (u_strcmp(buf, uneg_one) == 0));
LLAssert(((-llong(0, 12345)).lltou(buf, (uint32_t)sizeof(buf)) == 6) && (u_strcmp(buf, uneg_12345) == 0));
LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf, (uint32_t)sizeof(buf), 16) == 16) && (u_strcmp(buf, ubig1) == 0));
}
}
/* if 0 */
#endif
void
IntlTestRBNF::TestEnglishSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "1", "one" },
{ "2", "two" },
{ "15", "fifteen" },
{ "20", "twenty" },
{ "23", "twenty-three" },
{ "73", "seventy-three" },
{ "88", "eighty-eight" },
{ "100", "one hundred" },
{ "106", "one hundred six" },
{ "127", "one hundred twenty-seven" },
{ "200", "two hundred" },
{ "579", "five hundred seventy-nine" },
{ "1,000", "one thousand" },
{ "2,000", "two thousand" },
{ "3,004", "three thousand four" },
{ "4,567", "four thousand five hundred sixty-seven" },
{ "15,943", "fifteen thousand nine hundred forty-three" },
{ "2,345,678", "two million three hundred forty-five thousand six hundred seventy-eight" },
{ "-36", "minus thirty-six" },
{ "234.567", "two hundred thirty-four point five six seven" },
{ nullptr, nullptr}
};
doTest(formatter, testData, true);
#if !UCONFIG_NO_COLLATION
formatter->setLenient(true);
static const char* lpTestData[][2] = {
{ "fifty-7", "57" },
{ " fifty-7", "57" },
{ " fifty-7", "57" },
{ "2 thousand six HUNDRED fifty-7", "2,657" },
{ "fifteen hundred and zero", "1,500" },
{ "FOurhundred thiRTY six", "436" },
{ nullptr, nullptr}
};
doLenientParseTest(formatter, lpTestData);
#endif
}
delete formatter;
}
void
IntlTestRBNF::TestOrdinalAbbreviations()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_ORDINAL, Locale::getUS(), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "1", "1st" },
{ "2", "2nd" },
{ "3", "3rd" },
{ "4", "4th" },
{ "7", "7th" },
{ "10", "10th" },
{ "11", "11th" },
{ "13", "13th" },
{ "20", "20th" },
{ "21", "21st" },
{ "22", "22nd" },
{ "23", "23rd" },
{ "24", "24th" },
{ "33", "33rd" },
{ "102", "102nd" },
{ "312", "312th" },
{ "12,345", "12,345th" },
{ nullptr, nullptr}
};
doTest(formatter, testData, false);
}
delete formatter;
}
void
IntlTestRBNF::TestDurations()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_DURATION, Locale::getUS(), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "3,600", "1:00:00" }, //move me and I fail
{ "0", "0 sec." },
{ "1", "1 sec." },
{ "24", "24 sec." },
{ "60", "1:00" },
{ "73", "1:13" },
{ "145", "2:25" },
{ "666", "11:06" },
// { "3,600", "1:00:00" },
{ "3,740", "1:02:20" },
{ "10,293", "2:51:33" },
{ nullptr, nullptr}
};
doTest(formatter, testData, true);
static const char* const fractionalTestData[][2] = {
{ "1234", "20:34" },
{ "1234.2", "20:34" },
{ "1234.7", "20:35" },
{ nullptr, nullptr }
};
doTest(formatter, fractionalTestData, false);
#if !UCONFIG_NO_COLLATION
formatter->setLenient(true);
static const char* lpTestData[][2] = {
{ "2-51-33", "10,293" },
{ nullptr, nullptr}
};
doLenientParseTest(formatter, lpTestData);
#endif
}
delete formatter;
}
void IntlTestRBNF::TestDFRounding()
{
// test for ICU-22611
UParseError parseError;
UErrorCode err = U_ZERO_ERROR;
// no decimal places
LocalPointer<RuleBasedNumberFormat> nf0(new RuleBasedNumberFormat(u"1000/1000: <##K<;", Locale::getUS(), parseError, err));
if (U_FAILURE(err)) {
errcheckln(err, "FAIL: could not construct formatter - %s", u_errorName(err));
} else {
static const char* const integerTestData[][2] = {
{ "-1400", "-1K" },
{ "-1900", "-2K" },
{ "1400", "1K" },
{ "1900", "2K" },
{ nullptr, nullptr }
};
doTest(nf0.getAlias(), integerTestData, false);
}
// 1 decimal place
LocalPointer<RuleBasedNumberFormat> nf1(new RuleBasedNumberFormat(u"1000/1000: <##.0K<;", Locale::getUS(), parseError, err));
if (U_FAILURE(err)) {
errcheckln(err, "FAIL: could not construct formatter - %s", u_errorName(err));
} else {
static const char* const oneDecimalPlaceTestData[][2] = {
{ "-1440", "-1.4K" },
{ "1890", "1.9K" },
{ nullptr, nullptr }
};
doTest(nf1.getAlias(), oneDecimalPlaceTestData, false);
}
// with modulus substitution
LocalPointer<RuleBasedNumberFormat> nfMod(new RuleBasedNumberFormat(u"1000/1000: <##<K>##>; -x: ->>;", Locale::getUS(), parseError, err));
if (U_FAILURE(err)) {
errcheckln(err, "FAIL: could not construct formatter - %s", u_errorName(err));
} else {
static const char* const integerTestData[][2] = {
{ "-1400", "-1K400" },
{ "-1900", "-1K900" },
{ "1400", "1K400" },
{ "1900", "1K900" },
{ nullptr, nullptr }
};
doTest(nfMod.getAlias(), integerTestData, false);
}
// no decimal places, but with rounding mode set to ROUND_FLOOR
LocalPointer<RuleBasedNumberFormat> nfFloor(new RuleBasedNumberFormat(u"1000/1000: <##K<;", Locale::getUS(), parseError, err));
nfFloor->setMaximumFractionDigits(0);
nfFloor->setRoundingMode(NumberFormat::kRoundFloor);
if (U_FAILURE(err)) {
errcheckln(err, "FAIL: could not construct formatter - %s", u_errorName(err));
} else {
static const char* const integerTestData[][2] = {
{ "-1400", "-2K" },
{ "-1900", "-2K" },
{ "1400", "1K" },
{ "1900", "1K" },
{ nullptr, nullptr }
};
doTest(nfFloor.getAlias(), integerTestData, false);
}
}
void IntlTestRBNF::TestMemoryLeak22899()
{
UErrorCode status = U_ZERO_ERROR;
UParseError perror;
icu::UnicodeString str(u"0,31,01,30,01,0,01,01,30,01,30,31,01,30,01,30,30,00,01,0:");
icu::RuleBasedNumberFormat rbfmt(str, icu::Locale::getEnglish(), perror, status);
}
void
IntlTestRBNF::TestSpanishSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("es", "ES", ""), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "1", "uno" },
{ "6", "seis" },
{ "16", "diecis\\u00e9is" },
{ "20", "veinte" },
{ "24", "veinticuatro" },
{ "26", "veintis\\u00e9is" },
{ "73", "setenta y tres" },
{ "88", "ochenta y ocho" },
{ "100", "cien" },
{ "106", "ciento seis" },
{ "127", "ciento veintisiete" },
{ "200", "doscientos" },
{ "579", "quinientos setenta y nueve" },
{ "1,000", "mil" },
{ "2,000", "dos mil" },
{ "3,004", "tres mil cuatro" },
{ "4,567", "cuatro mil quinientos sesenta y siete" },
{ "15,943", "quince mil novecientos cuarenta y tres" },
{ "2,345,678", "dos millones trescientos cuarenta y cinco mil seiscientos setenta y ocho"},
{ "-36", "menos treinta y seis" },
{ "234.567", "doscientos treinta y cuatro coma cinco seis siete" },
{ nullptr, nullptr}
};
doTest(formatter, testData, true);
}
delete formatter;
}
void
IntlTestRBNF::TestFrenchSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getFrance(), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "1", "un" },
{ "15", "quinze" },
{ "20", "vingt" },
{ "21", "vingt-et-un" },
{ "23", "vingt-trois" },
{ "62", "soixante-deux" },
{ "70", "soixante-dix" },
{ "71", "soixante-et-onze" },
{ "73", "soixante-treize" },
{ "80", "quatre-vingts" },
{ "88", "quatre-vingt-huit" },
{ "100", "cent" },
{ "106", "cent six" },
{ "127", "cent vingt-sept" },
{ "200", "deux cents" },
{ "579", "cinq cent soixante-dix-neuf" },
{ "1,000", "mille" },
{ "1,123", "mille cent vingt-trois" },
{ "1,594", "mille cinq cent quatre-vingt-quatorze" },
{ "2,000", "deux mille" },
{ "3,004", "trois mille quatre" },
{ "4,567", "quatre mille cinq cent soixante-sept" },
{ "15,943", "quinze mille neuf cent quarante-trois" },
{ "2,345,678", "deux millions trois cent quarante-cinq mille six cent soixante-dix-huit" },
{ "-36", "moins trente-six" },
{ "234.567", "deux cent trente-quatre virgule cinq six sept" },
{ nullptr, nullptr}
};
doTest(formatter, testData, true);
#if !UCONFIG_NO_COLLATION
formatter->setLenient(true);
static const char* lpTestData[][2] = {
{ "trente-et-un", "31" },
{ "un cent quatre vingt dix huit", "198" },
{ nullptr, nullptr}
};
doLenientParseTest(formatter, lpTestData);
#endif
}
delete formatter;
}
static const char* const swissFrenchTestData[][2] = {
{ "1", "un" },
{ "15", "quinze" },
{ "20", "vingt" },
{ "21", "vingt-et-un" },
{ "23", "vingt-trois" },
{ "62", "soixante-deux" },
{ "70", "septante" },
{ "71", "septante-et-un" },
{ "73", "septante-trois" },
{ "80", "huitante" },
{ "88", "huitante-huit" },
{ "100", "cent" },
{ "106", "cent six" },
{ "127", "cent vingt-sept" },
{ "200", "deux cents" },
{ "579", "cinq cent septante-neuf" },
{ "1,000", "mille" },
{ "1,123", "mille cent vingt-trois" },
{ "1,594", "mille cinq cent nonante-quatre" },
{ "2,000", "deux mille" },
{ "3,004", "trois mille quatre" },
{ "4,567", "quatre mille cinq cent soixante-sept" },
{ "15,943", "quinze mille neuf cent quarante-trois" },
{ "2,345,678", "deux millions trois cent quarante-cinq mille six cent septante-huit" },
{ "-36", "moins trente-six" },
{ "234.567", "deux cent trente-quatre virgule cinq six sept" },
{ nullptr, nullptr}
};
void
IntlTestRBNF::TestSwissFrenchSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "CH", ""), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
doTest(formatter, swissFrenchTestData, true);
}
delete formatter;
}
static const char* const belgianFrenchTestData[][2] = {
{ "1", "un" },
{ "15", "quinze" },
{ "20", "vingt" },
{ "21", "vingt-et-un" },
{ "23", "vingt-trois" },
{ "62", "soixante-deux" },
{ "70", "septante" },
{ "71", "septante-et-un" },
{ "73", "septante-trois" },
{ "80", "quatre-vingts" },
{ "88", "quatre-vingt huit" },
{ "90", "nonante" },
{ "91", "nonante-et-un" },
{ "95", "nonante-cinq" },
{ "100", "cent" },
{ "106", "cent six" },
{ "127", "cent vingt-sept" },
{ "200", "deux cents" },
{ "579", "cinq cent septante-neuf" },
{ "1,000", "mille" },
{ "1,123", "mille cent vingt-trois" },
{ "1,594", "mille cinq cent nonante-quatre" },
{ "2,000", "deux mille" },
{ "3,004", "trois mille quatre" },
{ "4,567", "quatre mille cinq cent soixante-sept" },
{ "15,943", "quinze mille neuf cent quarante-trois" },
{ "2,345,678", "deux millions trois cent quarante-cinq mille six cent septante-huit" },
{ "-36", "moins trente-six" },
{ "234.567", "deux cent trente-quatre virgule cinq six sept" },
{ nullptr, nullptr}
};
void
IntlTestRBNF::TestBelgianFrenchSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "BE", ""), status);
if (U_FAILURE(status)) {
errcheckln(status, "rbnf status: 0x%x (%s)\n", status, u_errorName(status));
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
// Belgian french should match Swiss french.
doTest(formatter, belgianFrenchTestData, true);
}
delete formatter;
}
void
IntlTestRBNF::TestItalianSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getItalian(), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "1", "uno" },
{ "15", "quindici" },
{ "20", "venti" },
{ "23", "venti\\u00ADtr\\u00E9" },
{ "73", "settanta\\u00ADtr\\u00E9" },
{ "88", "ottant\\u00ADotto" },
{ "100", "cento" },
{ "101", "cento\\u00ADuno" },
{ "103", "cento\\u00ADtr\\u00E9" },
{ "106", "cento\\u00ADsei" },
{ "108", "cent\\u00ADotto" },
{ "127", "cento\\u00ADventi\\u00ADsette" },
{ "181", "cent\\u00ADottant\\u00ADuno" },
{ "200", "due\\u00ADcento" },
{ "579", "cinque\\u00ADcento\\u00ADsettanta\\u00ADnove" },
{ "1,000", "mille" },
{ "2,000", "due\\u00ADmila" },
{ "3,004", "tre\\u00ADmila\\u00ADquattro" },
{ "4,567", "quattro\\u00ADmila\\u00ADcinque\\u00ADcento\\u00ADsessanta\\u00ADsette" },
{ "15,943", "quindici\\u00ADmila\\u00ADnove\\u00ADcento\\u00ADquaranta\\u00ADtr\\u00E9" },
{ "-36", "meno trenta\\u00ADsei" },
{ "234.567", "due\\u00ADcento\\u00ADtrenta\\u00ADquattro virgola cinque sei sette" },
{ nullptr, nullptr}
};
doTest(formatter, testData, true);
}
delete formatter;
}
void
IntlTestRBNF::TestPortugueseSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("pt","BR",""), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "1", "um" },
{ "15", "quinze" },
{ "20", "vinte" },
{ "23", "vinte e tr\\u00EAs" },
{ "73", "setenta e tr\\u00EAs" },
{ "88", "oitenta e oito" },
{ "100", "cem" },
{ "106", "cento e seis" },
{ "108", "cento e oito" },
{ "127", "cento e vinte e sete" },
{ "181", "cento e oitenta e um" },
{ "200", "duzentos" },
{ "579", "quinhentos e setenta e nove" },
{ "1,000", "mil" },
{ "2,000", "dois mil" },
{ "3,004", "tr\\u00EAs mil e quatro" },
{ "4,567", "quatro mil quinhentos e sessenta e sete" },
{ "15,943", "quinze mil novecentos e quarenta e tr\\u00EAs" },
{ "-36", "menos trinta e seis" },
{ "234.567", "duzentos e trinta e quatro v\\u00EDrgula cinco seis sete" },
{ nullptr, nullptr}
};
doTest(formatter, testData, true);
}
delete formatter;
}
void
IntlTestRBNF::TestGermanSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getGermany(), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "1", "eins" },
{ "15", "f\\u00fcnfzehn" },
{ "20", "zwanzig" },
{ "23", "drei\\u00ADund\\u00ADzwanzig" },
{ "73", "drei\\u00ADund\\u00ADsiebzig" },
{ "88", "acht\\u00ADund\\u00ADachtzig" },
{ "100", "ein\\u00ADhundert" },
{ "106", "ein\\u00ADhundert\\u00ADsechs" },
{ "127", "ein\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADzwanzig" },
{ "200", "zwei\\u00ADhundert" },
{ "579", "f\\u00fcnf\\u00ADhundert\\u00ADneun\\u00ADund\\u00ADsiebzig" },
{ "1,000", "ein\\u00ADtausend" },
{ "1,101", "ein\\u00adtausend\\u00adein\\u00adhundert\\u00adeins" },
{ "2,000", "zwei\\u00ADtausend" },
{ "3,004", "drei\\u00ADtausend\\u00ADvier" },
{ "4,567", "vier\\u00ADtausend\\u00ADf\\u00fcnf\\u00ADhundert\\u00ADsieben\\u00ADund\\u00ADsechzig" },
{ "15,943", "f\\u00fcnfzehn\\u00ADtausend\\u00ADneun\\u00ADhundert\\u00ADdrei\\u00ADund\\u00ADvierzig" },
{ "2,345,678", "zwei Millionen drei\\u00ADhundert\\u00ADf\\u00fcnf\\u00ADund\\u00ADvierzig\\u00ADtausend\\u00ADsechs\\u00ADhundert\\u00ADacht\\u00ADund\\u00ADsiebzig" },
{ nullptr, nullptr}
};
doTest(formatter, testData, true);
#if !UCONFIG_NO_COLLATION
formatter->setLenient(true);
static const char* lpTestData[][2] = {
{ "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
{ nullptr, nullptr}
};
doLenientParseTest(formatter, lpTestData);
#endif
static const char* testDataYear[][2] = {
{ "101", "ein\\u00adhundert\\u00adeins" },
{ "900", "neun\\u00adhundert" },
{ "1,001", "ein\\u00adtausend\\u00adeins" },
{ "1,100", "elf\\u00adhundert" },
{ "1,101", "elf\\u00adhundert\\u00adeins" },
{ "1,234", "zw\\u00f6lf\\u00adhundert\\u00advier\\u00adund\\u00addrei\\u00dfig" },
{ "2,001", "zwei\\u00adtausend\\u00adeins" },
{ "10,001", "zehn\\u00adtausend\\u00adeins" },
{ "-100", "minus ein\\u00adhundert" },
{ "12.34", "12,3" },
{ nullptr, nullptr }
};
status = U_ZERO_ERROR;
formatter->setDefaultRuleSet("%spellout-numbering-year", status);
if (U_SUCCESS(status)) {
logln("testing year rules");
doTest(formatter, testDataYear, false);
}
else {
errln("Can't test year rules");
}
}
delete formatter;
}
void
IntlTestRBNF::TestThaiSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("th"), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testData[][2] = {
{ "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" },
{ "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
{ "10", "\\u0e2a\\u0e34\\u0e1a" },
{ "11", "\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
{ "21", "\\u0e22\\u0e35\\u0e48\\u200b\\u0e2a\\u0e34\\u0e1a\\u200b\\u0e40\\u0e2d\\u0e47\\u0e14" },
{ "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e23\\u0e49\\u0e2d\\u0e22\\u200b\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
{ "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u200b\\u0e08\\u0e38\\u0e14\\u200b\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" },
{ nullptr, nullptr}
};
doTest(formatter, testData, true);
}
delete formatter;
}
void
IntlTestRBNF::TestNorwegianSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* noFormatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("no"), status);
RuleBasedNumberFormat* nbFormatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("nb"), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* testDataDefault[][2] = {
{ "1", "\\u00E9n" },
{ "2", "to" },
{ "3", "tre" },
{ "4", "fire" },
{ "101", "hundre og \\u00E9n" },
{ "123", "hundre og tjue\\u00ADtre" },
{ "1,001", "tusen og \\u00E9n" },
{ "1,100", "tusen hundre" },
{ "6.789", "seks komma sju \\u00E5tte ni" },
{ "-5.678", "minus fem komma seks sju \\u00E5tte" },
{ nullptr, nullptr }
};
doTest(noFormatter, testDataDefault, true);
doTest(nbFormatter, testDataDefault, true);
}
delete nbFormatter;
delete noFormatter;
}
void
IntlTestRBNF::TestSwedishSpellout()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("sv"), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* testDataDefault[][2] = {
{ "101", "ett\\u00adhundra\\u00adett" },
{ "123", "ett\\u00adhundra\\u00adtjugo\\u00adtre" },
{ "1,001", "et\\u00adtusen ett" },
{ "1,100", "et\\u00adtusen ett\\u00adhundra" },
{ "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
{ "1,234", "et\\u00adtusen tv\\u00e5\\u00adhundra\\u00adtrettio\\u00adfyra" },
{ "10,001", "tio\\u00adtusen ett" },
{ "11,000", "elva\\u00adtusen" },
{ "12,000", "tolv\\u00adtusen" },
{ "20,000", "tjugo\\u00adtusen" },
{ "21,000", "tjugo\\u00adet\\u00adtusen" },
{ "21,001", "tjugo\\u00adet\\u00adtusen ett" },
{ "200,000", "tv\\u00e5\\u00adhundra\\u00adtusen" },
{ "201,000", "tv\\u00e5\\u00adhundra\\u00adet\\u00adtusen" },
{ "200,200", "tv\\u00e5\\u00adhundra\\u00adtusen tv\\u00e5\\u00adhundra" },
{ "2,002,000", "tv\\u00e5 miljoner tv\\u00e5\\u00adtusen" },
{ "12,345,678", "tolv miljoner tre\\u00adhundra\\u00adfyrtio\\u00adfem\\u00adtusen sex\\u00adhundra\\u00adsjuttio\\u00ad\\u00e5tta" },
{ "123,456.789", "ett\\u00adhundra\\u00adtjugo\\u00adtre\\u00adtusen fyra\\u00adhundra\\u00adfemtio\\u00adsex komma sju \\u00e5tta nio" },
{ "-12,345.678", "minus tolv\\u00adtusen tre\\u00adhundra\\u00adfyrtio\\u00adfem komma sex sju \\u00e5tta" },
{ nullptr, nullptr }
};
doTest(formatter, testDataDefault, true);
static const char* testDataNeutrum[][2] = {
{ "101", "ett\\u00adhundra\\u00adett" },
{ "1,001", "et\\u00adtusen ett" },
{ "1,101", "et\\u00adtusen ett\\u00adhundra\\u00adett" },
{ "10,001", "tio\\u00adtusen ett" },
{ "21,001", "tjugo\\u00adet\\u00adtusen ett" },
{ nullptr, nullptr }
};
formatter->setDefaultRuleSet("%spellout-cardinal-neuter", status);
if (U_SUCCESS(status)) {
logln(" testing spellout-cardinal-neuter rules");
doTest(formatter, testDataNeutrum, true);
}
else {
errln("Can't test spellout-cardinal-neuter rules");
}
static const char* testDataYear[][2] = {
{ "101", "ett\\u00adhundra\\u00adett" },
{ "900", "nio\\u00adhundra" },
{ "1,001", "et\\u00adtusen ett" },
{ "1,100", "elva\\u00adhundra" },
{ "1,101", "elva\\u00adhundra\\u00adett" },
{ "1,234", "tolv\\u00adhundra\\u00adtrettio\\u00adfyra" },
{ "2,001", "tjugo\\u00adhundra\\u00adett" },
{ "10,001", "tio\\u00adtusen ett" },
{ nullptr, nullptr }
};
status = U_ZERO_ERROR;
formatter->setDefaultRuleSet("%spellout-numbering-year", status);
if (U_SUCCESS(status)) {
logln("testing year rules");
doTest(formatter, testDataYear, true);
}
else {
errln("Can't test year rules");
}
}
delete formatter;
}
void
IntlTestRBNF::TestSmallValues()
{
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* formatter
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("en_US"), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
static const char* const testDataDefault[][2] = {
{ "0.001", "zero point zero zero one" },
{ "0.0001", "zero point zero zero zero one" },
{ "0.00001", "zero point zero zero zero zero one" },
{ "0.000001", "zero point zero zero zero zero zero one" },
{ "0.0000001", "zero point zero zero zero zero zero zero one" },
{ "0.00000001", "zero point zero zero zero zero zero zero zero one" },
{ "0.000000001", "zero point zero zero zero zero zero zero zero zero one" },
{ "0.0000000001", "zero point zero zero zero zero zero zero zero zero zero one" },
{ "0.00000000001", "zero point zero zero zero zero zero zero zero zero zero zero one" },
{ "0.000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero one" },
{ "0.0000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero one" },
{ "0.00000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
{ "0.000000000000001", "zero point zero zero zero zero zero zero zero zero zero zero zero zero zero zero one" },
{ "10,000,000.001", "ten million point zero zero one" },
{ "10,000,000.0001", "ten million point zero zero zero one" },
{ "10,000,000.00001", "ten million point zero zero zero zero one" },
{ "10,000,000.000001", "ten million point zero zero zero zero zero one" },
{ "10,000,000.0000001", "ten million point zero zero zero zero zero zero one" },
// { "10,000,000.00000001", "ten million point zero zero zero zero zero zero zero one" },
// { "10,000,000.000000002", "ten million point zero zero zero zero zero zero zero zero two" },
{ "10,000,000", "ten million" },
// { "1,234,567,890.0987654", "one billion, two hundred and thirty-four million, five hundred and sixty-seven thousand, eight hundred and ninety point zero nine eight seven six five four" },
// { "123,456,789.9876543", "one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine point nine eight seven six five four three" },
// { "12,345,678.87654321", "twelve million, three hundred and forty-five thousand, six hundred and seventy-eight point eight seven six five four three two one" },
{ "1,234,567.7654321", "one million two hundred thirty-four thousand five hundred sixty-seven point seven six five four three two one" },
{ "123,456.654321", "one hundred twenty-three thousand four hundred fifty-six point six five four three two one" },
{ "12,345.54321", "twelve thousand three hundred forty-five point five four three two one" },
{ "1,234.4321", "one thousand two hundred thirty-four point four three two one" },
{ "123.321", "one hundred twenty-three point three two one" },
{ "0.0000000011754944", "zero point zero zero zero zero zero zero zero zero one one seven five four nine four four" },
{ "0.000001175494351", "zero point zero zero zero zero zero one one seven five four nine four three five one" },
{ nullptr, nullptr }
};
doTest(formatter, testDataDefault, true);
delete formatter;
}
}
void
IntlTestRBNF::TestLocalizations()
{
int i;
UnicodeString rules("%main:0:no;1:some;100:a lot;1000:tons;\n"
"%other:0:nada;1:yah, some;100:plenty;1000:more'n you'll ever need");
UErrorCode status = U_ZERO_ERROR;
UParseError perror;
RuleBasedNumberFormat formatter(rules, perror, status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not construct formatter - %s", u_errorName(status));
} else {
{
static const char* const testData[][2] = {
{ "0", "nada" },
{ "5", "yah, some" },
{ "423", "plenty" },
{ "12345", "more'n you'll ever need" },
{ nullptr, nullptr }
};
doTest(&formatter, testData, false);
}
{
UnicodeString loc("<<%main, %other>,<en, Main, Other>,<fr, leMain, leOther>,<de, 'das Main', 'etwas anderes'>>");
static const char* const testData[][2] = {
{ "0", "no" },
{ "5", "some" },
{ "423", "a lot" },
{ "12345", "tons" },
{ nullptr, nullptr }
};
RuleBasedNumberFormat formatter0(rules, loc, perror, status);
if (U_FAILURE(status)) {
errln("failed to build second formatter");
} else {
doTest(&formatter0, testData, false);
{
// exercise localization info
Locale locale0("en__VALLEY@turkey=gobblegobble");
Locale locale1("de_DE_FOO");
Locale locale2("ja_JP");
UnicodeString name = formatter0.getRuleSetName(0);
if ( formatter0.getRuleSetDisplayName(0, locale0) == "Main"
&& formatter0.getRuleSetDisplayName(0, locale1) == "das Main"
&& formatter0.getRuleSetDisplayName(0, locale2) == "%main"
&& formatter0.getRuleSetDisplayName(name, locale0) == "Main"
&& formatter0.getRuleSetDisplayName(name, locale1) == "das Main"
&& formatter0.getRuleSetDisplayName(name, locale2) == "%main"){
logln("getRuleSetDisplayName tested");
}else {
errln("failed to getRuleSetDisplayName");
}
}
for (i = 0; i < formatter0.getNumberOfRuleSetDisplayNameLocales(); ++i) {
Locale locale = formatter0.getRuleSetDisplayNameLocale(i, status);
if (U_SUCCESS(status)) {
for (int j = 0; j < formatter0.getNumberOfRuleSetNames(); ++j) {
UnicodeString name = formatter0.getRuleSetName(j);
UnicodeString lname = formatter0.getRuleSetDisplayName(j, locale);
UnicodeString msg = locale.getName();
msg.append(": ");
msg.append(name);
msg.append(" = ");
msg.append(lname);
logln(msg);
}
}
}
}
}
{
static const char* goodLocs[] = {
"", // zero-length ok, same as providing no localization data
"<<>>", // no public rule sets ok
"<<%main>>", // no localizations ok
"<<%main,>,<en, Main,>>", // comma before close angle ok
"<<%main>,<en, ',<>\" '>>", // quotes everything until next quote
"<<%main>,<'en', \"it's ok\">>", // double quotes work too
" \n <\n <\n %main\n >\n , \t <\t en\t , \tfoo \t\t > \n\n > \n ", // Pattern_White_Space ok
};
int32_t goodLocsLen = UPRV_LENGTHOF(goodLocs);
static const char* badLocs[] = {
" ", // non-zero length
"<>", // empty array
"<", // unclosed outer array
"<<", // unclosed inner array
"<<,>>", // unexpected comma
"<<''>>", // empty string
" x<<%main>>", // first non space char not open angle bracket
"<%main>", // missing inner array
"<<%main %other>>", // elements missing separating comma (spaces must be quoted)
"<<%main><en, Main>>", // arrays missing separating comma
"<<%main>,<en, main, foo>>", // too many elements in locale data
"<<%main>,<en>>", // too few elements in locale data
"<<<%main>>>", // unexpected open angle
"<<%main<>>>", // unexpected open angle
"<<%main, %other>,<en,,>>", // implicit empty strings
"<<%main>,<en,''>>", // empty string
"<<%main>, < en, '>>", // unterminated quote
"<<%main>, < en, \"<>>", // unterminated quote
"<<%main\">>", // quote in string
"<<%main'>>", // quote in string
"<<%main<>>", // open angle in string
"<<%main>> x", // extra non-space text at end
};
int32_t badLocsLen = UPRV_LENGTHOF(badLocs);
for (i = 0; i < goodLocsLen; ++i) {
logln("[%d] '%s'", i, goodLocs[i]);
UErrorCode status = U_ZERO_ERROR;
UnicodeString loc(goodLocs[i]);
RuleBasedNumberFormat fmt(rules, loc, perror, status);
if (U_FAILURE(status)) {
errln("Failed parse of good localization string: '%s'", goodLocs[i]);
}
}
for (i = 0; i < badLocsLen; ++i) {
logln("[%d] '%s'", i, badLocs[i]);
UErrorCode status = U_ZERO_ERROR;
UnicodeString loc(badLocs[i]);
RuleBasedNumberFormat fmt(rules, loc, perror, status);
if (U_SUCCESS(status)) {
errln("Successful parse of bad localization string: '%s'", badLocs[i]);
}
}
}
}
}
void
IntlTestRBNF::TestAllLocales()
{
const char* names[] = {
" (spellout) ",
" (ordinal) "
// " (duration) " // This is English only, and it's not really supported in CLDR anymore.
};
double numbers[] = {45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111};
int32_t count = 0;
const Locale* locales = Locale::getAvailableLocales(count);
for (int i = 0; i < count; ++i) {
const Locale* loc = &locales[i];
for (int j = 0; j < 2; ++j) {
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat* f = new RuleBasedNumberFormat(static_cast<URBNFRuleSetTag>(j), *loc, status);
if (U_FAILURE(status)) {
errln(UnicodeString(loc->getName()) + names[j]
+ "ERROR could not instantiate -> " + u_errorName(status));
continue;
}
Locale actualLocale = f->getLocale(ULOC_ACTUAL_LOCALE, status);
if (actualLocale != *loc) {
// Skip the redundancy
delete f;
break;
}
#if !UCONFIG_NO_COLLATION
for (unsigned int numidx = 0; numidx < UPRV_LENGTHOF(numbers); numidx++) {
double n = numbers[numidx];
UnicodeString str;
f->format(n, str);
if (verbose) {
logln(UnicodeString(loc->getName()) + names[j]
+ "success: " + n + " -> " + str);
}
// We do not validate the result in this test case,
// because there are cases which do not round trip by design.
Formattable num;
// regular parse
status = U_ZERO_ERROR;
f->setLenient(false);
f->parse(str, num, status);
if (U_FAILURE(status)) {
errln(UnicodeString(loc->getName()) + names[j]
+ "ERROR could not parse '" + str + "' -> " + u_errorName(status));
}
// We only check the spellout. The behavior is undefined for numbers < 1 and fractional numbers.
if (j == 0) {
if (num.getType() == Formattable::kLong && num.getLong() != n) {
errln(UnicodeString(loc->getName()) + names[j]
+ UnicodeString("ERROR could not roundtrip ") + n
+ UnicodeString(" -> ") + str + UnicodeString(" -> ") + num.getLong());
}
else if (num.getType() == Formattable::kDouble && static_cast<int64_t>(num.getDouble() * 1000) != static_cast<int64_t>(n * 1000)) {
// The epsilon difference is too high.
errln(UnicodeString(loc->getName()) + names[j]
+ UnicodeString("ERROR could not roundtrip ") + n
+ UnicodeString(" -> ") + str + UnicodeString(" -> ") + num.getDouble());
}
}
// lenient parse
status = U_ZERO_ERROR;
f->setLenient(true);
f->parse(str, num, status);
if (U_FAILURE(status)) {
errln(UnicodeString(loc->getName()) + names[j]
+ "ERROR could not parse(lenient) '" + str + "' -> " + u_errorName(status));
}
// We only check the spellout. The behavior is undefined for numbers < 1 and fractional numbers.
if (j == 0) {
if (num.getType() == Formattable::kLong && num.getLong() != n) {
errln(UnicodeString(loc->getName()) + names[j]
+ UnicodeString("ERROR could not roundtrip ") + n
+ UnicodeString(" -> ") + str + UnicodeString(" -> ") + num.getLong());
}
else if (num.getType() == Formattable::kDouble && static_cast<int64_t>(num.getDouble() * 1000) != static_cast<int64_t>(n * 1000)) {
// The epsilon difference is too high.
errln(UnicodeString(loc->getName()) + names[j]
+ UnicodeString("ERROR could not roundtrip ") + n
+ UnicodeString(" -> ") + str + UnicodeString(" -> ") + num.getDouble());
}
}
}
#endif
delete f;
}
}
}
void
IntlTestRBNF::TestMultiplierSubstitution() {
UnicodeString rules("=#,##0=;1,000,000: <##0.###< million;");
UErrorCode status = U_ZERO_ERROR;
UParseError parse_error;
RuleBasedNumberFormat *rbnf =
new RuleBasedNumberFormat(rules, Locale::getUS(), parse_error, status);
if (U_SUCCESS(status)) {
UnicodeString res;
FieldPosition pos;
double n = 1234000.0;
rbnf->format(n, res, pos);
delete rbnf;
UnicodeString expected(UNICODE_STRING_SIMPLE("1.234 million"));
if (expected != res) {
UnicodeString msg = "Expected: ";
msg.append(expected);
msg.append(" but got ");
msg.append(res);
errln(msg);
}
}
}
void
IntlTestRBNF::TestSetDecimalFormatSymbols() {
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat rbnf(URBNF_ORDINAL, Locale::getEnglish(), status);
if (U_FAILURE(status)) {
dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
return;
}
DecimalFormatSymbols dfs(Locale::getEnglish(), status);
if (U_FAILURE(status)) {
errln("Unable to create DecimalFormatSymbols - " + UnicodeString(u_errorName(status)));
return;
}
UnicodeString expected[] = {
UnicodeString("1,001st"),
UnicodeString("1&001st")
};
double number = 1001;
UnicodeString result;
rbnf.format(number, result);
if (result != expected[0]) {
errln("Format Error - Got: " + result + " Expected: " + expected[0]);
}
result.remove();
/* Set new symbol for testing */
dfs.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, UnicodeString("&"), true);
rbnf.setDecimalFormatSymbols(dfs);
rbnf.format(number, result);
if (result != expected[1]) {
errln("Format Error - Got: " + result + " Expected: " + expected[1]);
}
}
void IntlTestRBNF::TestPluralRules() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString enRules("%digits-ordinal:-x: ->>;0: =#,##0=$(ordinal,one{st}two{nd}few{rd}other{th})$;");
UParseError parseError;
RuleBasedNumberFormat enFormatter(enRules, Locale::getEnglish(), parseError, status);
if (U_FAILURE(status)) {
dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
return;
}
const char* const enTestData[][2] = {
{ "1", "1st" },
{ "2", "2nd" },
{ "3", "3rd" },
{ "4", "4th" },
{ "11", "11th" },
{ "12", "12th" },
{ "13", "13th" },
{ "14", "14th" },
{ "21", "21st" },
{ "22", "22nd" },
{ "23", "23rd" },
{ "24", "24th" },
{ nullptr, nullptr }
};
doTest(&enFormatter, enTestData, true);
// This is trying to model the feminine form, but don't worry about the details too much.
// We're trying to test the plural rules.
UnicodeString ruRules("%spellout-numbering:"
"-x: minus >>;"
"x.x: << point >>;"
"0: zero;"
"1: one;"
"2: two;"
"3: three;"
"4: four;"
"5: five;"
"6: six;"
"7: seven;"
"8: eight;"
"9: nine;"
"10: ten;"
"11: eleven;"
"12: twelve;"
"13: thirteen;"
"14: fourteen;"
"15: fifteen;"
"16: sixteen;"
"17: seventeen;"
"18: eighteen;"
"19: nineteen;"
"20: twenty[->>];"
"30: thirty[->>];"
"40: forty[->>];"
"50: fifty[->>];"
"60: sixty[->>];"
"70: seventy[->>];"
"80: eighty[->>];"
"90: ninety[->>];"
"100: hundred[ >>];"
"200: << hundred[ >>];"
"300: << hundreds[ >>];"
"500: << hundredss[ >>];"
"1000: << $(cardinal,one{thousand}few{thousands}other{thousandss})$[ >>];"
"1000000: << $(cardinal,one{million}few{millions}other{millionss})$[ >>];");
RuleBasedNumberFormat ruFormatter(ruRules, Locale("ru"), parseError, status);
const char* const ruTestData[][2] = {
{ "1", "one" },
{ "100", "hundred" },
{ "125", "hundred twenty-five" },
{ "399", "three hundreds ninety-nine" },
{ "1,000", "one thousand" },
{ "1,001", "one thousand one" },
{ "2,000", "two thousands" },
{ "2,001", "two thousands one" },
{ "2,002", "two thousands two" },
{ "3,333", "three thousands three hundreds thirty-three" },
{ "5,000", "five thousandss" },
{ "11,000", "eleven thousandss" },
{ "21,000", "twenty-one thousand" },
{ "22,000", "twenty-two thousands" },
{ "25,001", "twenty-five thousandss one" },
{ nullptr, nullptr }
};
if (U_FAILURE(status)) {
errln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
return;
}
doTest(&ruFormatter, ruTestData, true);
// Make sure there are no divide by 0 errors.
UnicodeString result;
RuleBasedNumberFormat(ruRules, Locale("ru"), parseError, status).format(static_cast<int32_t>(21000), result);
if (result.compare(UNICODE_STRING_SIMPLE("twenty-one thousand")) != 0) {
errln("Got " + result + " for 21000");
}
}
void IntlTestRBNF::TestInfinityNaN() {
UErrorCode status = U_ZERO_ERROR;
UParseError parseError;
UnicodeString enRules("%default:"
"-x: minus >>;"
"Inf: infinite;"
"NaN: not a number;"
"0: =#,##0=;");
RuleBasedNumberFormat enFormatter(enRules, Locale::getEnglish(), parseError, status);
const char * const enTestData[][2] = {
{"1", "1"},
{"\\u221E", "infinite"},
{"-\\u221E", "minus infinite"},
{"NaN", "not a number"},
{ nullptr, nullptr }
};
if (U_FAILURE(status)) {
dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
return;
}
doTest(&enFormatter, enTestData, true);
// Test the default behavior when the rules are undefined.
UnicodeString enRules2("%default:"
"-x: ->>;"
"0: =#,##0=;");
RuleBasedNumberFormat enFormatter2(enRules2, Locale::getEnglish(), parseError, status);
if (U_FAILURE(status)) {
errln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
return;
}
const char * const enDefaultTestData[][2] = {
{"1", "1"},
{"\\u221E", "\\u221E"},
{"-\\u221E", "-\\u221E"},
{"NaN", "NaN"},
{ nullptr, nullptr }
};
doTest(&enFormatter2, enDefaultTestData, true);
}
void IntlTestRBNF::TestVariableDecimalPoint() {
UErrorCode status = U_ZERO_ERROR;
UParseError parseError;
UnicodeString enRules("%spellout-numbering:"
"-x: minus >>;"
"x.x: << point >>;"
"x,x: << comma >>;"
"0.x: xpoint >>;"
"0,x: xcomma >>;"
"0: zero;"
"1: one;"
"2: two;"
"3: three;"
"4: four;"
"5: five;"
"6: six;"
"7: seven;"
"8: eight;"
"9: nine;");
RuleBasedNumberFormat enFormatter(enRules, Locale::getEnglish(), parseError, status);
const char * const enTestPointData[][2] = {
{"1.1", "one point one"},
{"1.23", "one point two three"},
{"0.4", "xpoint four"},
{ nullptr, nullptr }
};
if (U_FAILURE(status)) {
dataerrln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
return;
}
doTest(&enFormatter, enTestPointData, true);
DecimalFormatSymbols decimalFormatSymbols(Locale::getEnglish(), status);
decimalFormatSymbols.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, UNICODE_STRING_SIMPLE(","));
enFormatter.setDecimalFormatSymbols(decimalFormatSymbols);
const char * const enTestCommaData[][2] = {
{"1.1", "one comma one"},
{"1.23", "one comma two three"},
{"0.4", "xcomma four"},
{ nullptr, nullptr }
};
doTest(&enFormatter, enTestCommaData, true);
}
void IntlTestRBNF::TestLargeNumbers() {
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat rbnf(URBNF_SPELLOUT, Locale::getEnglish(), status);
const char * const enTestFullData[][2] = {
{"-9007199254740991", "minus nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-one"}, // Maximum precision in both a double and a long
{"9007199254740991", "nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-one"}, // Maximum precision in both a double and a long
{"-9007199254740992", "minus nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-two"}, // Only precisely contained in a long
{"9007199254740992", "nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-two"}, // Only precisely contained in a long
{"9999999999999998", "nine quadrillion nine hundred ninety-nine trillion nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-eight"},
{"9999999999999999", "nine quadrillion nine hundred ninety-nine trillion nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine"},
{"999999999999999999", "nine hundred ninety-nine quadrillion nine hundred ninety-nine trillion nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine"},
{"1000000000000000000", "1,000,000,000,000,000,000"}, // The rules don't go to 1 quintillion yet
{"-9223372036854775809", "-9,223,372,036,854,775,809"}, // We've gone beyond 64-bit precision
{"-9223372036854775808", "-9,223,372,036,854,775,808"}, // We've gone beyond +64-bit precision
{"-9223372036854775807", "minus 9,223,372,036,854,775,807"}, // Minimum 64-bit precision
{"-9223372036854775806", "minus 9,223,372,036,854,775,806"}, // Minimum 64-bit precision + 1
{"9223372036854774111", "9,223,372,036,854,774,111"}, // Below 64-bit precision
{"9223372036854774999", "9,223,372,036,854,774,999"}, // Below 64-bit precision
{"9223372036854775000", "9,223,372,036,854,775,000"}, // Below 64-bit precision
{"9223372036854775806", "9,223,372,036,854,775,806"}, // Maximum 64-bit precision - 1
{"9223372036854775807", "9,223,372,036,854,775,807"}, // Maximum 64-bit precision
{"9223372036854775808", "9,223,372,036,854,775,808"}, // We've gone beyond 64-bit precision. This can only be represented with BigDecimal.
{ nullptr, nullptr }
};
doTest(&rbnf, enTestFullData, false);
}
void IntlTestRBNF::TestCompactDecimalFormatStyle() {
UErrorCode status = U_ZERO_ERROR;
UParseError parseError;
// This is not a common use case, but we're testing it anyway.
UnicodeString numberPattern("=###0.#####=;"
"1000: <###0.00< K;"
"1000000: <###0.00< M;"
"1000000000: <###0.00< B;"
"1000000000000: <###0.00< T;"
"1000000000000000: <###0.00< Q;");
RuleBasedNumberFormat rbnf(numberPattern, UnicodeString(), Locale::getEnglish(), parseError, status);
const char * const enTestFullData[][2] = {
{"1000", "1.00 K"},
{"1234", "1.23 K"},
{"999994", "999.99 K"},
{"999995", "1000.00 K"},
{"1000000", "1.00 M"},
{"1200000", "1.20 M"},
{"1200000000", "1.20 B"},
{"1200000000000", "1.20 T"},
{"1200000000000000", "1.20 Q"},
{"4503599627370495", "4.50 Q"},
{"4503599627370496", "4.50 Q"},
{"8990000000000000", "8.99 Q"},
{"9008000000000000", "9.00 Q"}, // Number doesn't precisely fit into a double
{"9456000000000000", "9.00 Q"}, // Number doesn't precisely fit into a double
{"10000000000000000", "10.00 Q"}, // Number doesn't precisely fit into a double
{"9223372036854775807", "9223.00 Q"}, // Maximum 64-bit precision
{"9223372036854775808", "9,223,372,036,854,775,808"}, // We've gone beyond 64-bit precision. This can only be represented with BigDecimal.
{ nullptr, nullptr }
};
doTest(&rbnf, enTestFullData, false);
}
void IntlTestRBNF::TestParseFailure() {
UErrorCode status = U_ZERO_ERROR;
RuleBasedNumberFormat rbnf(URBNF_SPELLOUT, Locale::getJapanese(), status);
static const char16_t* testData[] = {
u"・・・・・・・・・・・・・・・・・・・・・・・・"
};
if (assertSuccess("", status, true, __FILE__, __LINE__)) {
for (int i = 0; i < UPRV_LENGTHOF(testData); ++i) {
UnicodeString spelledNumberString(testData[i]);
Formattable actualNumber;
rbnf.parse(spelledNumberString, actualNumber, status);
if (status != U_INVALID_FORMAT_ERROR) { // I would have expected U_PARSE_ERROR, but NumberFormat::parse gives U_INVALID_FORMAT_ERROR
errln("FAIL: string should be unparseable index=%d %s", i, u_errorName(status));
}
}
}
}
void IntlTestRBNF::TestMinMaxIntegerDigitsIgnored() {
IcuTestErrorCode status(*this, "TestMinMaxIntegerDigitsIgnored");
// NOTE: SimpleDateFormat has an optimization that depends on the fact that min/max integer digits
// do not affect RBNF (see SimpleDateFormat#zeroPaddingNumber).
RuleBasedNumberFormat rbnf(URBNF_SPELLOUT, "en", status);
if (status.isSuccess()) {
rbnf.setMinimumIntegerDigits(2);
rbnf.setMaximumIntegerDigits(3);
UnicodeString result;
rbnf.format(3, result.remove(), status);
assertEquals("Min integer digits are ignored", u"three", result);
rbnf.format(1012, result.remove(), status);
assertEquals("Max integer digits are ignored", u"one thousand twelve", result);
}
}
void
IntlTestRBNF::doTest(RuleBasedNumberFormat* formatter, const char* const testData[][2], UBool testParsing)
{
// man, error reporting would be easier with printf-style syntax for unicode string and formattable
UErrorCode status = U_ZERO_ERROR;
DecimalFormatSymbols dfs("en", status);
// NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
DecimalFormat decFmt("#,###.################", dfs, status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not create NumberFormat - %s", u_errorName(status));
} else {
for (int i = 0; testData[i][0]; ++i) {
const char* numString = testData[i][0];
const char* expectedWords = testData[i][1];
log("[%i] %s = ", i, numString);
Formattable expectedNumber;
UnicodeString escapedNumString = UnicodeString(numString, -1, US_INV).unescape();
decFmt.parse(escapedNumString, expectedNumber, status);
if (U_FAILURE(status)) {
errln("FAIL: decFmt could not parse %s", numString);
break;
} else {
UnicodeString actualString;
FieldPosition pos;
formatter->format(expectedNumber, actualString/* , pos*/, status);
if (U_FAILURE(status)) {
UnicodeString msg = "Fail: formatter could not format ";
decFmt.format(expectedNumber, msg, status);
errln(msg);
break;
} else {
UnicodeString expectedString = UnicodeString(expectedWords, -1, US_INV).unescape();
if (actualString != expectedString) {
UnicodeString msg = "FAIL: check failed for ";
decFmt.format(expectedNumber, msg, status);
msg.append(", expected ");
msg.append(expectedString);
msg.append(" but got ");
msg.append(actualString);
errln(msg);
break;
} else {
logln(actualString);
if (testParsing) {
Formattable parsedNumber;
formatter->parse(actualString, parsedNumber, status);
if (U_FAILURE(status)) {
UnicodeString msg = "FAIL: formatter could not parse ";
msg.append(actualString);
msg.append(" status code: " );
msg.append(u_errorName(status));
errln(msg);
break;
} else {
if (parsedNumber != expectedNumber
&& (!uprv_isNaN(parsedNumber.getDouble()) || !uprv_isNaN(expectedNumber.getDouble())))
{
UnicodeString msg = "FAIL: parse failed for ";
msg.append(actualString);
msg.append(", expected ");
decFmt.format(expectedNumber, msg, status);
msg.append(", but got ");
decFmt.format(parsedNumber, msg, status);
errln(msg);
break;
}
}
}
}
}
}
}
}
}
void
IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat* formatter, const char* testData[][2])
{
UErrorCode status = U_ZERO_ERROR;
NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
if (U_FAILURE(status)) {
errcheckln(status, "FAIL: could not create NumberFormat - %s", u_errorName(status));
} else {
for (int i = 0; testData[i][0]; ++i) {
const char* spelledNumber = testData[i][0]; // spelled-out number
const char* asciiUSNumber = testData[i][1]; // number as ascii digits formatted for US locale
UnicodeString spelledNumberString = UnicodeString(spelledNumber).unescape();
Formattable actualNumber;
formatter->parse(spelledNumberString, actualNumber, status);
if (U_FAILURE(status)) {
UnicodeString msg = "FAIL: formatter could not parse ";
msg.append(spelledNumberString);
errln(msg);
break;
} else {
// I changed the logic of this test somewhat from Java-- instead of comparing the
// strings, I compare the Formattables. Hmmm, but the Formattables don't compare,
// so change it back.
UnicodeString asciiUSNumberString = asciiUSNumber;
Formattable expectedNumber;
decFmt->parse(asciiUSNumberString, expectedNumber, status);
if (U_FAILURE(status)) {
UnicodeString msg = "FAIL: decFmt could not parse ";
msg.append(asciiUSNumberString);
errln(msg);
break;
} else {
UnicodeString actualNumberString;
UnicodeString expectedNumberString;
decFmt->format(actualNumber, actualNumberString, status);
decFmt->format(expectedNumber, expectedNumberString, status);
if (actualNumberString != expectedNumberString) {
UnicodeString msg = "FAIL: parsing";
msg.append(asciiUSNumberString);
msg.append("\n");
msg.append(" lenient parse failed for ");
msg.append(spelledNumberString);
msg.append(", expected ");
msg.append(expectedNumberString);
msg.append(", but got ");
msg.append(actualNumberString);
errln(msg);
break;
}
}
}
}
delete decFmt;
}
}
void
IntlTestRBNF::TestNumberingSystem() {
IcuTestErrorCode err(*this, "TestNumberingSystem");
RuleBasedNumberFormat rbnf(URBNF_NUMBERING_SYSTEM, Locale::getUS(), err);
if (!err.errIfFailureAndReset("Failed to create RBNF with URBNF_NUMBERING_SYSTEM")) {
UnicodeString result;
assertEquals("Wrong result with default rule set", u"123", rbnf.format(123, result, err));
result.remove();
rbnf.setDefaultRuleSet(u"%ethiopic", err);
assertEquals("Wrong result with Ethiopic rule set", u"፻፳፫", rbnf.format(123, result, err));
}
}
void
IntlTestRBNF::TestParseRuleDescriptorOverflow23002() {
UParseError perror;
UErrorCode status = U_ZERO_ERROR;
// Test int64 overflow inside parseRuleDescriptor
UnicodeString testStr(u"0110110/300113001103000113001103000110i/3013033:");
icu::RuleBasedNumberFormat rbfmt(
testStr,
Locale("as"), perror, status);
assertEquals("number too large", U_PARSE_ERROR, status);
}
void
IntlTestRBNF::TestInfiniteRecursion() {
UnicodeString badRules[] = {
">>",
"<<",
"<<<",
">>>",
"%foo: x=%foo=",
"%one: x>%two>; %two: y>%one>;"
};
for (int32_t i = 0; i < UPRV_LENGTHOF(badRules); i++) {
UErrorCode err = U_ZERO_ERROR;
UParseError parseErr;
RuleBasedNumberFormat rbnf(badRules[i], parseErr, err);
if (U_SUCCESS(err)) {
UnicodeString result;
rbnf.format(5, result);
// we don't actually care about the result and the function doesn't return an error code;
// we just want to make sure the function returns
Formattable pResult;
rbnf.parse("foo", pResult, err);
assertTrue("rbnf.parse() didn't return U_INVALID_FORMAT_ERROR!", err == U_INVALID_FORMAT_ERROR);
} else {
// eventually it'd be nice to statically analyze the rules for (at least) the most common
// causes of infinite recursion, in which case we'd end up down here and need to check
// the error code. But for now, we probably won't end up here and don't care if we do
}
}
}
/**
* This test is a little contrived for English, but the grammar is relevant for several languages, including:
* Latin, Germanic, Slavic and Indic.
* It's pretty common, especially for ordinals, to use different words as a magnitude unit and when it's the final word.
* Several languages need grammatical agreement between the final and non-final magnitude unit
* with the numerical quantity before the unit. This test is the equivalent seen in other languages.
*/
void
IntlTestRBNF::testOmissionReplacementWithPluralRules() {
UnicodeString rules("%cardinal:\n"
"-x: minus >>;\n"
"x.x: << point >>;\n"
"Inf: infinite;\n"
"NaN: not a number;\n"
"zero; one; two; three; four; five; six; seven; eight; nine;\n"
"ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen; seventeen; eighteen; nineteen;\n"
"20: twenty[->>];\n"
"30: thirty[->>];\n"
"40: forty[->>];\n"
"50: fifty[->>];\n"
"60: sixty[->>];\n"
"70: seventy[->>];\n"
"80: eighty[->>];\n"
"90: ninety[->>];\n"
"100: << hundred[ >>];\n"
"1000: << thousand[ >>];\n"
"1000000: << million[ >>];\n"
"1000000000: << billion[ >>];\n"
"1000000000000: << trillion[ >>];\n"
"1000000000000000: =#,##0=;\n"
"%ordinal:\n"
"-x: minus >>;\n"
"x.x: =#,##0.#=;\n"
"Inf: infinitieth;\n"
"zeroth; first; second; third; fourth; fifth; sixth; seventh; eighth; ninth;\n"
"tenth; eleventh; twelfth;\n"
"13: =%cardinal=th;\n"
"20: twent[y->>|ieth];\n"
"30: thirt[y->>|ieth];\n"
"40: fort[y->>|ieth];\n"
"50: fift[y->>|ieth];\n"
"60: sixt[y->>|ieth];\n"
"70: sevent[y->>|ieth];\n"
"80: eight[y->>|ieth];\n"
"90: ninet[y->>|ieth];\n"
"100: <%cardinal< [$(cardinal,one{hundred}other{hundreds})$ >>|$(cardinal,one{hundredth}other{hundredths})$];\n"
"1000: <%cardinal< [$(cardinal,one{thousand}other{thousands})$ >>|$(cardinal,one{thousandth}other{thousandths})$];\n"
"1000000: <%cardinal< [$(cardinal,one{million}other{millions})$ >>|$(cardinal,one{millionth}other{millionths})$];\n"
"1000000000: <%cardinal< [$(cardinal,one{billion}other{billions})$ >>|$(cardinal,one{billionth}other{billionths})$];\n"
"1000000000000: <%cardinal< [$(cardinal,one{trillion}other{trillions})$ >>|$(cardinal,one{trillionth}other{trillionths})$];\n"
"1000000000000000: =#,##0=$(ordinal,one{st}two{nd}few{rd}other{th})$;");
UErrorCode status = U_ZERO_ERROR;
UParseError perror;
icu::RuleBasedNumberFormat rbnf(rules, icu::Locale::getEnglish(), perror, status);
const char * const enTestFullData[][2] = {
{"20", "twentieth"},
{"21", "twenty-first"},
{"29", "twenty-ninth"},
{"30", "thirtieth"},
{"31", "thirty-first"},
{"39", "thirty-ninth"},
{"100", "one hundredth"},
{"101", "one hundred first"},
{"200", "two hundredths"},
{"201", "two hundreds first"},
{"300", "three hundredths"},
{"301", "three hundreds first"},
{"1000", "one thousandth"},
{"1001", "one thousand first"},
{"1100", "one thousand one hundredth"},
{"1101", "one thousand one hundred first"},
{"1200", "one thousand two hundredths"},
{"1201", "one thousand two hundreds first"},
{"2000", "two thousandths"},
{"2001", "two thousands first"},
{"2100", "two thousands one hundredth"},
{"2101", "two thousands one hundred first"},
{"8000", "eight thousandths"},
{"8001", "eight thousands first"},
{"888000", "eight hundred eighty-eight thousandths"},
{"888001", "eight hundred eighty-eight thousands first"},
{"888100", "eight hundred eighty-eight thousands one hundredth"},
{"999101", "nine hundred ninety-nine thousands one hundred first"},
{"999200", "nine hundred ninety-nine thousands two hundredths"},
{"999201", "nine hundred ninety-nine thousands two hundreds first"},
{ nullptr, nullptr }
};
doTest(&rbnf, enTestFullData, false);
}
void
IntlTestRBNF::TestNullDereferenceWRITE23149() {
UnicodeString test("x00:><>");
UParseError perror;
UErrorCode status = U_ZERO_ERROR;
// The following call should not crash
icu::RuleBasedNumberFormat rbfmt(test, Locale("en"), perror, status);
}
void
IntlTestRBNF::TestNullDereferenceREAD23184() {
icu::Formattable result;
UParseError perror;
UErrorCode status = U_ZERO_ERROR;
icu::RuleBasedNumberFormat rbfmt(u"x00:>%>>;%:;<0<<", Locale::getUS(), perror, status);
if (U_SUCCESS(status)) {
errln("Construct \"x00:>%%>>;%%:;<0<<\" should get error");
}
status = U_ZERO_ERROR;
icu::RuleBasedNumberFormat rbfmt2(u"x00:>%>>;%;<0<<", Locale::getUS(), perror, status);
if (U_SUCCESS(status)) {
errln("Construct \"x00:>%%>>;%%;<0<<\" should get error");
}
}
/* U_HAVE_RBNF */
#else
void
IntlTestRBNF::TestRBNFDisabled() {
errln("*** RBNF currently disabled on this platform ***\n");
}
/* U_HAVE_RBNF */
#endif
#endif /* #if !UCONFIG_NO_FORMATTING */
|