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
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/********************************************************************************
*
* File CDATTST.C
*
* Modification History:
* Name Description
* Madhu Katragadda Creation
*********************************************************************************
*/
/* C API TEST FOR DATE FORMAT */
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/uloc.h"
#include "unicode/udat.h"
#include "unicode/udatpg.h"
#include "unicode/ucal.h"
#include "unicode/unum.h"
#include "unicode/ustring.h"
#include "unicode/ufieldpositer.h"
#include "cintltst.h"
#include "cdattst.h"
#include "cformtst.h"
#include "cstring.h"
#include "cmemory.h"
#include <math.h>
#include <stdbool.h>
#include <stdio.h> // for sprintf()
static void TestExtremeDates(void);
static void TestAllLocales(void);
static void TestRelativeCrash(void);
static void TestContext(void);
static void TestCalendarDateParse(void);
static void TestParseErrorReturnValue(void);
static void TestFormatForFields(void);
static void TestForceGannenNumbering(void);
static void TestMapDateToCalFields(void);
static void TestNarrowQuarters(void);
static void TestExtraneousCharacters(void);
static void TestParseTooStrict(void);
static void TestHourCycle(void);
static void TestLocaleNameCrash(void);
void addDateForTest(TestNode** root);
#define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x)
void addDateForTest(TestNode** root)
{
TESTCASE(TestDateFormat);
TESTCASE(TestRelativeDateFormat);
TESTCASE(TestSymbols);
TESTCASE(TestDateFormatCalendar);
TESTCASE(TestExtremeDates);
TESTCASE(TestAllLocales);
TESTCASE(TestRelativeCrash);
TESTCASE(TestContext);
TESTCASE(TestCalendarDateParse);
TESTCASE(TestOverrideNumberFormat);
TESTCASE(TestParseErrorReturnValue);
TESTCASE(TestFormatForFields);
TESTCASE(TestForceGannenNumbering);
TESTCASE(TestMapDateToCalFields);
TESTCASE(TestNarrowQuarters);
TESTCASE(TestExtraneousCharacters);
TESTCASE(TestParseTooStrict);
TESTCASE(TestHourCycle);
TESTCASE(TestLocaleNameCrash);
}
/* Testing the DateFormat API */
static void TestDateFormat(void)
{
UDateFormat *def, *fr, *it, *de, *def1, *fr_pat;
UDateFormat *any;
UDateFormat *copy;
UErrorCode status = U_ZERO_ERROR;
UChar* result = NULL;
const UCalendar *cal;
const UNumberFormat *numformat1, *numformat2;
UNumberFormat *adoptNF;
UChar temp[80];
int32_t numlocales;
UDate d1;
int i;
int32_t resultlength;
int32_t resultlengthneeded;
int32_t parsepos;
UDate d = 837039928046.0;
double num = -10456.37;
/*const char* str="yyyy.MM.dd G 'at' hh:mm:ss z";
const char t[]="2/3/76 2:50 AM";*/
/*Testing udat_open() to open a dateformat */
ctest_setTimeZone(NULL, &status);
log_verbose("\nTesting udat_open() with various parameters\n");
fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL,0, NULL, 0,&status);
if(U_FAILURE(status))
{
log_data_err("FAIL: error in creating the dateformat using full time style with french locale -> %s (Are you missing data?)\n",
myErrorName(status) );
return;
}
/* this is supposed to open default date format, but later on it treats it like it is "en_US"
- very bad if you try to run the tests on machine where default locale is NOT "en_US" */
/* def = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0, &status); */
def = udat_open(UDAT_SHORT, UDAT_SHORT, "en_US", NULL, 0,NULL, 0, &status);
if(U_FAILURE(status))
{
log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n",
myErrorName(status) );
return;
}
it = udat_open(UDAT_DEFAULT, UDAT_MEDIUM, "it_IT", NULL, 0, NULL, 0,&status);
if(U_FAILURE(status))
{
log_err("FAIL: error in creating the dateformat using medium date style with italian locale\n %s\n",
myErrorName(status) );
return;
}
de = udat_open(UDAT_LONG, UDAT_LONG, "de_DE", NULL, 0, NULL, 0,&status);
if(U_FAILURE(status))
{
log_err("FAIL: error in creating the dateformat using long time and date styles with german locale\n %s\n",
myErrorName(status));
return;
}
/*creating a default dateformat */
def1 = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0,NULL, 0, &status);
if(U_FAILURE(status))
{
log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n",
myErrorName(status) );
return;
}
/*Testing udat_getAvailable() and udat_countAvailable()*/
log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
numlocales=udat_countAvailable();
/* use something sensible w/o hardcoding the count */
if(numlocales < 0)
log_data_err("FAIL: error in countAvailable\n");
log_verbose("The number of locales for which date/time formatting patterns are available is %d\n", numlocales);
for(i=0;i<numlocales;i++) {
UErrorCode subStatus = U_ZERO_ERROR;
log_verbose("Testing open of %s\n", udat_getAvailable(i));
any = udat_open(UDAT_SHORT, UDAT_SHORT, udat_getAvailable(i), NULL ,0, NULL, 0, &subStatus);
if(U_FAILURE(subStatus)) {
log_data_err("FAIL: date format %s (getAvailable(%d)) is not instantiable: %s\n", udat_getAvailable(i), i, u_errorName(subStatus));
}
udat_close(any);
}
/*Testing udat_clone()*/
log_verbose("\nTesting the udat_clone() function of date format\n");
copy=udat_clone(def, &status);
if(U_FAILURE(status)){
log_err("Error in creating the clone using udat_clone: %s\n", myErrorName(status) );
}
/*if(def != copy)
log_err("Error in udat_clone");*/ /*how should i check for equality???? */
/*Testing udat_format()*/
log_verbose("\nTesting the udat_format() function of date format\n");
u_strcpy(temp, u"7/10/96, 4:05\u202FPM");
/*format using def */
resultlength=0;
resultlengthneeded=udat_format(def, d, NULL, resultlength, NULL, &status);
if(status==U_BUFFER_OVERFLOW_ERROR)
{
status=U_ZERO_ERROR;
resultlength=resultlengthneeded+1;
if(result != NULL) {
free(result);
result = NULL;
}
result=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_format(def, d, result, resultlength, NULL, &status);
}
if(U_FAILURE(status) || !result)
{
log_err("FAIL: Error in formatting using udat_format(.....) %s\n", myErrorName(status) );
return;
}
else
log_verbose("PASS: formatting successful\n");
if(u_strcmp(result, temp)==0)
log_verbose("PASS: Date Format for US locale successful using udat_format()\n");
else {
char xbuf[2048];
char gbuf[2048];
u_austrcpy(xbuf, temp);
u_austrcpy(gbuf, result);
log_err("FAIL: Date Format for US locale failed using udat_format() - expected %s got %s\n", xbuf, gbuf);
}
/*format using fr */
u_unescape("10 juil. 1996, 16:05:28 heure d\\u2019\\u00E9t\\u00E9 du Pacifique nord-am\\u00E9ricain", temp, 80);
if(result != NULL) {
free(result);
result = NULL;
}
result=myDateFormat(fr, d);
if(u_strcmp(result, temp)==0) {
log_verbose("PASS: Date Format for french locale successful using udat_format()\n");
} else {
char xbuf[2048];
char gbuf[2048];
u_austrcpy(xbuf, temp);
u_austrcpy(gbuf, result);
log_data_err("FAIL: Date Format for french locale failed using udat_format() - expected %s got %s\n", xbuf, gbuf);
}
/*format using it */
u_uastrcpy(temp, "10 lug 1996, 16:05:28");
{
UChar *fmtted;
char g[100];
char x[100];
fmtted = myDateFormat(it,d);
u_austrcpy(g, fmtted);
u_austrcpy(x, temp);
if(u_strcmp(fmtted, temp)==0) {
log_verbose("PASS: Date Format for italian locale successful uisng udat_format() - wanted %s, got %s\n", x, g);
} else {
log_data_err("FAIL: Date Format for italian locale failed using udat_format() - wanted %s, got %s\n", x, g);
}
}
/*Testing parsing using udat_parse()*/
log_verbose("\nTesting parsing using udat_parse()\n");
u_strcpy(temp, u"2/3/76, 2:50\u202FAM");
parsepos=0;
status=U_ZERO_ERROR;
d1=udat_parse(def, temp, u_strlen(temp), &parsepos, &status);
if(U_FAILURE(status))
{
log_err("FAIL: Error in parsing using udat_parse(.....) %s\n", myErrorName(status) );
}
else
log_verbose("PASS: parsing successful\n");
/*format it back and check for equality */
if(u_strcmp(myDateFormat(def, d1),temp)!=0)
log_err("FAIL: error in parsing\n");
/*Testing parsing using udat_parse()*/
log_verbose("\nTesting parsing using udat_parse()\n");
u_uastrcpy(temp,"2/Don't parse this part");
status=U_ZERO_ERROR;
d1=udat_parse(def, temp, u_strlen(temp), NULL, &status);
if(status != U_PARSE_ERROR)
{
log_err("FAIL: udat_parse(\"bad string\") passed when it should have failed\n");
}
else
log_verbose("PASS: parsing successful\n");
/*Testing udat_openPattern() */
status=U_ZERO_ERROR;
log_verbose("\nTesting the udat_openPattern with a specified pattern\n");
/*for french locale */
fr_pat=udat_open(UDAT_PATTERN, UDAT_PATTERN,"fr_FR",NULL,0,temp, u_strlen(temp), &status);
if(U_FAILURE(status))
{
log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n",
myErrorName(status) );
}
else
log_verbose("PASS: creating dateformat using udat_openPattern() successful\n");
/*Testing applyPattern and toPattern */
log_verbose("\nTesting applyPattern and toPattern()\n");
udat_applyPattern(def1, false, temp, u_strlen(temp));
log_verbose("Extracting the pattern\n");
resultlength=0;
resultlengthneeded=udat_toPattern(def1, false, NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR)
{
status=U_ZERO_ERROR;
resultlength=resultlengthneeded + 1;
result=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_toPattern(def1, false, result, resultlength, &status);
}
if(U_FAILURE(status))
{
log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
myErrorName(status) );
}
if(u_strcmp(result, temp)!=0)
log_err("FAIL: Error in extracting the pattern\n");
else
log_verbose("PASS: applyPattern and toPattern work fine\n");
if(result != NULL) {
free(result);
result = NULL;
}
/*Testing getter and setter functions*/
/*isLenient and setLenient()*/
log_verbose("\nTesting the isLenient and setLenient properties\n");
udat_setLenient(fr, udat_isLenient(it));
if(udat_isLenient(fr) != udat_isLenient(it))
log_err("ERROR: setLenient() failed\n");
else
log_verbose("PASS: setLenient() successful\n");
/*Test get2DigitYearStart set2DigitYearStart */
log_verbose("\nTesting the get and set 2DigitYearStart properties\n");
d1= udat_get2DigitYearStart(fr_pat,&status);
if(U_FAILURE(status)) {
log_err("ERROR: udat_get2DigitYearStart failed %s\n", myErrorName(status) );
}
status = U_ZERO_ERROR;
udat_set2DigitYearStart(def1 ,d1, &status);
if(U_FAILURE(status)) {
log_err("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) );
}
if(udat_get2DigitYearStart(fr_pat, &status) != udat_get2DigitYearStart(def1, &status))
log_err("FAIL: error in set2DigitYearStart\n");
else
log_verbose("PASS: set2DigitYearStart successful\n");
/*try setting it to another value */
udat_set2DigitYearStart(de, 2000.0, &status);
if(U_FAILURE(status)){
log_verbose("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) );
}
if(udat_get2DigitYearStart(de, &status) != 2000)
log_err("FAIL: error in set2DigitYearStart\n");
else
log_verbose("PASS: set2DigitYearStart successful\n");
/*Test getNumberFormat() and setNumberFormat() */
log_verbose("\nTesting the get and set NumberFormat properties of date format\n");
numformat1=udat_getNumberFormat(fr_pat);
udat_setNumberFormat(def1, numformat1);
numformat2=udat_getNumberFormat(def1);
if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0)
log_err("FAIL: error in setNumberFormat or getNumberFormat()\n");
else
log_verbose("PASS:setNumberFormat and getNumberFormat successful\n");
/*Test getNumberFormat() and adoptNumberFormat() */
log_verbose("\nTesting the get and adopt NumberFormat properties of date format\n");
adoptNF= unum_open(UNUM_DEFAULT, NULL, 0, NULL, NULL, &status);
udat_adoptNumberFormat(def1, adoptNF);
numformat2=udat_getNumberFormat(def1);
if(u_strcmp(myNumformat(adoptNF, num), myNumformat(numformat2, num)) !=0)
log_err("FAIL: error in adoptNumberFormat or getNumberFormat()\n");
else
log_verbose("PASS:adoptNumberFormat and getNumberFormat successful\n");
/*try setting the number format to another format */
numformat1=udat_getNumberFormat(def);
udat_setNumberFormat(def1, numformat1);
numformat2=udat_getNumberFormat(def1);
if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0)
log_err("FAIL: error in setNumberFormat or getNumberFormat()\n");
else
log_verbose("PASS: setNumberFormat and getNumberFormat successful\n");
/*Test getCalendar and setCalendar*/
log_verbose("\nTesting the udat_getCalendar() and udat_setCalendar() properties\n");
cal=udat_getCalendar(fr_pat);
udat_setCalendar(def1, cal);
if(!ucal_equivalentTo(udat_getCalendar(fr_pat), udat_getCalendar(def1)))
log_err("FAIL: Error in setting and getting the calendar\n");
else
log_verbose("PASS: getting and setting calendar successful\n");
if(result!=NULL) {
free(result);
}
/*Closing the UDateForamt */
udat_close(def);
udat_close(fr);
udat_close(it);
udat_close(de);
udat_close(def1);
udat_close(fr_pat);
udat_close(copy);
ctest_resetTimeZone();
}
/*
Test combined relative date formatting (relative date + non-relative time).
This is a bit tricky since we can't have static test data for comparison, the
relative date formatting is relative to the time the tests are run. We generate
the data for comparison dynamically. However, the tests could fail if they are
run right at midnight Pacific time and the call to ucal_getNow() is before midnight
while the calls to udat_format are after midnight or span midnight.
*/
static const UDate dayInterval = 24.0*60.0*60.0*1000.0;
static const UChar trdfZone[] = { 0x0055, 0x0053, 0x002F, 0x0050, 0x0061, 0x0063, 0x0069, 0x0066, 0x0069, 0x0063, 0 }; /* US/Pacific */
static const char trdfLocale[] = "en_US";
static const UChar minutesPatn[] = { 0x006D, 0x006D, 0 }; /* "mm" */
static const UChar monthLongPatn[] = { 0x004D, 0x004D, 0x004D, 0x004D, 0 }; /* "MMMM" */
static const UChar monthMediumPatn[] = { 0x004D, 0x004D, 0x004D, 0 }; /* "MMM" */
static const UChar monthShortPatn[] = { 0x004D, 0 }; /* "M" */
static const UDateFormatStyle dateStylesList[] = { UDAT_FULL, UDAT_LONG, UDAT_MEDIUM, UDAT_SHORT, UDAT_NONE };
static const UChar *monthPatnsList[] = { monthLongPatn, monthLongPatn, monthMediumPatn, monthShortPatn, NULL };
static const UChar newTimePatn[] = { 0x0048, 0x0048, 0x002C, 0x006D, 0x006D, 0 }; /* "HH,mm" */
static const UChar minutesStr[] = { 0x0034, 0x0039, 0 }; /* "49", minutes string to search for in output */
enum { kDateOrTimeOutMax = 96, kDateAndTimeOutMax = 192 };
static const UDate minutesTolerance = 2 * 60.0 * 1000.0;
static const UDate daysTolerance = 2 * 24.0 * 60.0 * 60.0 * 1000.0;
static void TestRelativeDateFormat(void)
{
UDate today = 0.0;
const UDateFormatStyle * stylePtr;
const UChar ** monthPtnPtr;
UErrorCode status = U_ZERO_ERROR;
UCalendar * ucal = ucal_open(trdfZone, -1, trdfLocale, UCAL_GREGORIAN, &status);
if ( U_SUCCESS(status) ) {
int32_t year, month, day;
ucal_setMillis(ucal, ucal_getNow(), &status);
year = ucal_get(ucal, UCAL_YEAR, &status);
month = ucal_get(ucal, UCAL_MONTH, &status);
day = ucal_get(ucal, UCAL_DATE, &status);
ucal_setDateTime(ucal, year, month, day, 18, 49, 0, &status); /* set to today at 18:49:00 */
today = ucal_getMillis(ucal, &status);
ucal_close(ucal);
}
if ( U_FAILURE(status) || today == 0.0 ) {
log_data_err("Generate UDate for a specified time today fails, error %s - (Are you missing data?)\n", myErrorName(status) );
return;
}
for (stylePtr = dateStylesList, monthPtnPtr = monthPatnsList; *stylePtr != UDAT_NONE; ++stylePtr, ++monthPtnPtr) {
UDateFormat* fmtRelDateTime;
UDateFormat* fmtRelDate;
UDateFormat* fmtTime;
int32_t dayOffset, limit;
UFieldPosition fp;
UChar strDateTime[kDateAndTimeOutMax];
UChar strDate[kDateOrTimeOutMax];
UChar strTime[kDateOrTimeOutMax];
UChar * strPtr;
int32_t dtpatLen;
fmtRelDateTime = udat_open(UDAT_SHORT, *stylePtr | UDAT_RELATIVE, trdfLocale, trdfZone, -1, NULL, 0, &status);
if ( U_FAILURE(status) ) {
log_data_err("udat_open timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s (Are you missing data?)\n", *stylePtr, myErrorName(status) );
continue;
}
fmtRelDate = udat_open(UDAT_NONE, *stylePtr | UDAT_RELATIVE, trdfLocale, trdfZone, -1, NULL, 0, &status);
if ( U_FAILURE(status) ) {
log_err("udat_open timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
udat_close(fmtRelDateTime);
continue;
}
fmtTime = udat_open(UDAT_SHORT, UDAT_NONE, trdfLocale, trdfZone, -1, NULL, 0, &status);
if ( U_FAILURE(status) ) {
log_err("udat_open timeStyle SHORT dateStyle NONE fails, error %s\n", myErrorName(status) );
udat_close(fmtRelDateTime);
udat_close(fmtRelDate);
continue;
}
dtpatLen = udat_toPatternRelativeDate(fmtRelDateTime, strDate, kDateAndTimeOutMax, &status);
if ( U_FAILURE(status) ) {
log_err("udat_toPatternRelativeDate timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
status = U_ZERO_ERROR;
} else if ( u_strstr(strDate, *monthPtnPtr) == NULL || dtpatLen != u_strlen(strDate) ) {
log_err("udat_toPatternRelativeDate timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) date pattern incorrect\n", *stylePtr );
}
dtpatLen = udat_toPatternRelativeTime(fmtRelDateTime, strTime, kDateAndTimeOutMax, &status);
if ( U_FAILURE(status) ) {
log_err("udat_toPatternRelativeTime timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
status = U_ZERO_ERROR;
} else if ( u_strstr(strTime, minutesPatn) == NULL || dtpatLen != u_strlen(strTime) ) {
log_err("udat_toPatternRelativeTime timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) time pattern incorrect\n", *stylePtr );
}
dtpatLen = udat_toPattern(fmtRelDateTime, false, strDateTime, kDateAndTimeOutMax, &status);
if ( U_FAILURE(status) ) {
log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
status = U_ZERO_ERROR;
} else if ( u_strstr(strDateTime, strDate) == NULL || u_strstr(strDateTime, strTime) == NULL || dtpatLen != u_strlen(strDateTime) ) {
log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) dateTime pattern incorrect\n", *stylePtr );
}
udat_applyPatternRelative(fmtRelDateTime, strDate, u_strlen(strDate), newTimePatn, u_strlen(newTimePatn), &status);
if ( U_FAILURE(status) ) {
log_err("udat_applyPatternRelative timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
status = U_ZERO_ERROR;
} else {
udat_toPattern(fmtRelDateTime, false, strDateTime, kDateAndTimeOutMax, &status);
if ( U_FAILURE(status) ) {
log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
status = U_ZERO_ERROR;
} else if ( u_strstr(strDateTime, newTimePatn) == NULL ) {
log_err("udat_applyPatternRelative timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) didn't update time pattern\n", *stylePtr );
}
}
udat_applyPatternRelative(fmtRelDateTime, strDate, u_strlen(strDate), strTime, u_strlen(strTime), &status); /* restore original */
fp.field = UDAT_MINUTE_FIELD;
for (dayOffset = -2, limit = 2; dayOffset <= limit; ++dayOffset) {
UDate dateToUse = today + (float)dayOffset*dayInterval;
udat_format(fmtRelDateTime, dateToUse, strDateTime, kDateAndTimeOutMax, &fp, &status);
if ( U_FAILURE(status) ) {
log_err("udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
status = U_ZERO_ERROR;
} else {
int32_t parsePos = 0;
UDate dateResult = udat_parse(fmtRelDateTime, strDateTime, -1, &parsePos, &status);
UDate dateDiff = (dateResult >= dateToUse)? dateResult - dateToUse: dateToUse - dateResult;
if ( U_FAILURE(status) || dateDiff > minutesTolerance ) {
log_err("udat_parse timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s, expect approx %.1f, got %.1f, parsePos %d\n",
*stylePtr, myErrorName(status), dateToUse, dateResult, parsePos );
status = U_ZERO_ERROR;
}
udat_format(fmtRelDate, dateToUse, strDate, kDateOrTimeOutMax, NULL, &status);
if ( U_FAILURE(status) ) {
log_err("udat_format timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) );
status = U_ZERO_ERROR;
} else if ( u_strstr(strDateTime, strDate) == NULL ) {
log_err("relative date string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr );
} else {
parsePos = 0;
dateResult = udat_parse(fmtRelDate, strDate, -1, &parsePos, &status);
dateDiff = (dateResult >= dateToUse)? dateResult - dateToUse: dateToUse - dateResult;
if ( U_FAILURE(status) || dateDiff > daysTolerance ) {
log_err("udat_parse timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s, expect approx %.1f, got %.1f, parsePos %d\n",
*stylePtr, myErrorName(status), dateToUse, dateResult, parsePos );
status = U_ZERO_ERROR;
}
}
udat_format(fmtTime, dateToUse, strTime, kDateOrTimeOutMax, NULL, &status);
if ( U_FAILURE(status) ) {
log_err("udat_format timeStyle SHORT dateStyle NONE fails, error %s\n", myErrorName(status) );
status = U_ZERO_ERROR;
} else if ( u_strstr(strDateTime, strTime) == NULL ) {
log_err("time string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr );
}
strPtr = u_strstr(strDateTime, minutesStr);
if ( strPtr != NULL ) {
int32_t beginIndex = (int32_t)(strPtr - strDateTime);
if ( fp.beginIndex != beginIndex ) {
log_err("UFieldPosition beginIndex %d, expected %d, in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", fp.beginIndex, beginIndex, *stylePtr );
}
} else {
log_err("minutes string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr );
}
}
}
udat_close(fmtRelDateTime);
udat_close(fmtRelDate);
udat_close(fmtTime);
}
}
/*Testing udat_getSymbols() and udat_setSymbols() and udat_countSymbols()*/
static void TestSymbols(void)
{
UDateFormat *def, *fr, *zhChiCal, *esMX, *th;
UErrorCode status = U_ZERO_ERROR;
UChar *value=NULL;
UChar *result = NULL;
int32_t resultlength;
int32_t resultlengthout;
UChar *pattern;
/*creating a dateformat with french locale */
log_verbose("\ncreating a date format with french locale\n");
fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL, 0, NULL, 0, &status);
if(U_FAILURE(status))
{
log_data_err("error in creating the dateformat using full time style with french locale -> %s (Are you missing data?)\n",
myErrorName(status) );
return;
}
/*creating a default dateformat */
log_verbose("\ncreating a date format with default locale\n");
/* this is supposed to open default date format, but later on it treats it like it is "en_US"
- very bad if you try to run the tests on machine where default locale is NOT "en_US" */
/* def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, &status); */
def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status);
if(U_FAILURE(status))
{
log_err("error in creating the dateformat using short date and time style\n %s\n",
myErrorName(status) );
return;
}
/*creating a dateformat with zh locale */
log_verbose("\ncreating a date format with zh locale for chinese calendar\n");
zhChiCal = udat_open(UDAT_NONE, UDAT_FULL, "zh@calendar=chinese", NULL, 0, NULL, 0, &status);
if(U_FAILURE(status))
{
log_data_err("error in creating the dateformat using full date, no time, locale zh@calendar=chinese -> %s (Are you missing data?)\n",
myErrorName(status) );
return;
}
/*creating a dateformat with es_MX locale */
log_verbose("\ncreating a date format with es_MX locale\n");
esMX = udat_open(UDAT_SHORT, UDAT_NONE, "es_MX", NULL, 0, NULL, 0, &status);
if(U_FAILURE(status))
{
log_data_err("error in creating the dateformat using no date, short time, locale es_MX -> %s (Are you missing data?)\n",
myErrorName(status) );
return;
}
/*creating a dateformat with th locale */
log_verbose("\ncreating a date format with th locale\n");
th = udat_open(UDAT_SHORT, UDAT_NONE, "th", NULL, 0, NULL, 0, &status);
if(U_FAILURE(status))
{
log_data_err("error in creating the dateformat using no date, short time, locale th -> %s (Are you missing data?)\n",
myErrorName(status) );
return;
}
/*Testing countSymbols, getSymbols and setSymbols*/
log_verbose("\nTesting countSymbols\n");
/*since the month names has the last string empty and week names are 1 based 1.e first string in the weeknames array is empty */
if(udat_countSymbols(def, UDAT_ERAS)!=2 || udat_countSymbols(def, UDAT_MONTHS)!=12 ||
udat_countSymbols(def, UDAT_SHORT_MONTHS)!=12 || udat_countSymbols(def, UDAT_WEEKDAYS)!=8 ||
udat_countSymbols(def, UDAT_SHORT_WEEKDAYS)!=8 || udat_countSymbols(def, UDAT_AM_PMS)!=2 ||
udat_countSymbols(def, UDAT_QUARTERS) != 4 || udat_countSymbols(def, UDAT_SHORT_QUARTERS) != 4 ||
udat_countSymbols(def, UDAT_LOCALIZED_CHARS)!=1 || udat_countSymbols(def, UDAT_SHORTER_WEEKDAYS)!=8 ||
udat_countSymbols(zhChiCal, UDAT_CYCLIC_YEARS_NARROW)!=60 || udat_countSymbols(zhChiCal, UDAT_ZODIAC_NAMES_NARROW)!=12)
{
log_err("FAIL: error in udat_countSymbols\n");
}
else
log_verbose("PASS: udat_countSymbols() successful\n");
/*testing getSymbols*/
log_verbose("\nTesting getSymbols\n");
pattern=(UChar*)malloc(sizeof(UChar) * 10);
u_uastrcpy(pattern, "jeudi");
resultlength=0;
resultlengthout=udat_getSymbols(fr, UDAT_WEEKDAYS, 5 , NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR)
{
status=U_ZERO_ERROR;
resultlength=resultlengthout+1;
if(result != NULL) {
free(result);
result = NULL;
}
result=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_getSymbols(fr, UDAT_WEEKDAYS, 5, result, resultlength, &status);
}
if(U_FAILURE(status))
{
log_err("FAIL: Error in udat_getSymbols().... %s\n", myErrorName(status) );
}
else
log_verbose("PASS: getSymbols successful\n");
if(u_strcmp(result, pattern)==0)
log_verbose("PASS: getSymbols retrieved the right value\n");
else
log_data_err("FAIL: getSymbols retrieved the wrong value\n");
/*run series of tests to test getsymbols regressively*/
log_verbose("\nTesting getSymbols() regressively\n");
VerifygetSymbols(fr, UDAT_WEEKDAYS, 1, "dimanche");
VerifygetSymbols(def, UDAT_WEEKDAYS, 1, "Sunday");
VerifygetSymbols(fr, UDAT_SHORT_WEEKDAYS, 7, "sam.");
VerifygetSymbols(fr, UDAT_SHORTER_WEEKDAYS, 7, "sa");
VerifygetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Sat");
VerifygetSymbols(def, UDAT_MONTHS, 11, "December");
VerifygetSymbols(def, UDAT_MONTHS, 0, "January");
VerifygetSymbols(fr, UDAT_ERAS, 0, "av. J.-C.");
VerifygetSymbols(def, UDAT_AM_PMS, 0, "AM");
VerifygetSymbols(def, UDAT_AM_PMS, 1, "PM");
VerifygetSymbols(fr, UDAT_SHORT_MONTHS, 0, "janv.");
VerifygetSymbols(def, UDAT_SHORT_MONTHS, 11, "Dec");
VerifygetSymbols(fr, UDAT_QUARTERS, 0, "1er trimestre");
VerifygetSymbols(def, UDAT_QUARTERS, 3, "4th quarter");
VerifygetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "T2");
VerifygetSymbols(def, UDAT_SHORT_QUARTERS, 2, "Q3");
VerifygetSymbols(esMX, UDAT_STANDALONE_NARROW_QUARTERS, 1, "2");
VerifygetSymbols(def, UDAT_NARROW_QUARTERS, 2, "3");
VerifygetSymbols(zhChiCal, UDAT_CYCLIC_YEARS_ABBREVIATED, 0, "\\u7532\\u5B50");
VerifygetSymbols(zhChiCal, UDAT_CYCLIC_YEARS_NARROW, 59, "\\u7678\\u4EA5");
VerifygetSymbols(zhChiCal, UDAT_ZODIAC_NAMES_ABBREVIATED, 0, "\\u9F20");
VerifygetSymbols(zhChiCal, UDAT_ZODIAC_NAMES_WIDE, 11, "\\u732A");
VerifygetSymbols(def, UDAT_AM_PMS_NARROW, 0, "a");
VerifygetSymbols(def, UDAT_AM_PMS_NARROW, 1, "p");
VerifygetSymbols(th, UDAT_AM_PMS_WIDE, 0, "\\u0E01\\u0E48\\u0E2D\\u0E19\\u0E40\\u0E17\\u0E35\\u0E48\\u0E22\\u0E07");
VerifygetSymbols(th, UDAT_AM_PMS_WIDE, 1, "\\u0E2B\\u0E25\\u0E31\\u0E07\\u0E40\\u0E17\\u0E35\\u0E48\\u0E22\\u0E07");
#if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZvcLQqVUOXxrbB:");
#else
VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZvcLQqVUOXxrbB");
#endif
if(result != NULL) {
free(result);
result = NULL;
}
free(pattern);
log_verbose("\nTesting setSymbols\n");
/*applying the pattern so that setSymbolss works */
resultlength=0;
resultlengthout=udat_toPattern(fr, false, NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR)
{
status=U_ZERO_ERROR;
resultlength=resultlengthout + 1;
pattern=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_toPattern(fr, false, pattern, resultlength, &status);
}
if(U_FAILURE(status))
{
log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
myErrorName(status) );
}
udat_applyPattern(def, false, pattern, u_strlen(pattern));
resultlength=0;
resultlengthout=udat_toPattern(def, false, NULL, resultlength,&status);
if(status==U_BUFFER_OVERFLOW_ERROR)
{
status=U_ZERO_ERROR;
resultlength=resultlengthout + 1;
if(result != NULL) {
free(result);
result = NULL;
}
result=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_toPattern(fr, false,result, resultlength, &status);
}
if(U_FAILURE(status))
{
log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
myErrorName(status) );
}
if(u_strcmp(result, pattern)==0)
log_verbose("Pattern applied properly\n");
else
log_err("pattern could not be applied properly\n");
free(pattern);
/*testing set symbols */
resultlength=0;
resultlengthout=udat_getSymbols(fr, UDAT_MONTHS, 11 , NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR){
status=U_ZERO_ERROR;
resultlength=resultlengthout+1;
if(result != NULL) {
free(result);
result = NULL;
}
result=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_getSymbols(fr, UDAT_MONTHS, 11, result, resultlength, &status);
}
if(U_FAILURE(status))
log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) );
resultlength=resultlengthout+1;
udat_setSymbols(def, UDAT_MONTHS, 11, result, resultlength, &status);
if(U_FAILURE(status))
{
log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) );
}
else
log_verbose("PASS: SetSymbols successful\n");
resultlength=0;
resultlengthout=udat_getSymbols(def, UDAT_MONTHS, 11, NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR){
status=U_ZERO_ERROR;
resultlength=resultlengthout+1;
value=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_getSymbols(def, UDAT_MONTHS, 11, value, resultlength, &status);
}
if(U_FAILURE(status))
log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n");
if(u_strcmp(result, value)!=0)
log_data_err("FAIL: Error in setting and getting symbols\n");
else
log_verbose("PASS: setSymbols successful\n");
/*run series of tests to test setSymbols regressively*/
log_verbose("\nTesting setSymbols regressively\n");
VerifysetSymbols(def, UDAT_ERAS, 0, "BeforeChrist");
VerifysetSymbols(def, UDAT_ERA_NAMES, 1, "AnnoDomini");
VerifysetSymbols(def, UDAT_WEEKDAYS, 1, "Sundayweek");
VerifysetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Satweek");
VerifysetSymbols(def, UDAT_NARROW_WEEKDAYS, 4, "M");
VerifysetSymbols(def, UDAT_STANDALONE_WEEKDAYS, 1, "Sonntagweek");
VerifysetSymbols(def, UDAT_STANDALONE_SHORT_WEEKDAYS, 7, "Sams");
VerifysetSymbols(def, UDAT_STANDALONE_NARROW_WEEKDAYS, 4, "V");
VerifysetSymbols(fr, UDAT_MONTHS, 11, "december");
VerifysetSymbols(fr, UDAT_SHORT_MONTHS, 0, "Jan");
VerifysetSymbols(fr, UDAT_NARROW_MONTHS, 1, "R");
VerifysetSymbols(fr, UDAT_STANDALONE_MONTHS, 11, "dezember");
VerifysetSymbols(fr, UDAT_STANDALONE_SHORT_MONTHS, 7, "Aug");
VerifysetSymbols(fr, UDAT_STANDALONE_NARROW_MONTHS, 2, "M");
VerifysetSymbols(fr, UDAT_QUARTERS, 0, "1. Quart");
VerifysetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "QQ2");
VerifysetSymbols(fr, UDAT_NARROW_QUARTERS, 1, "!2");
VerifysetSymbols(fr, UDAT_STANDALONE_QUARTERS, 2, "3rd Quar.");
VerifysetSymbols(fr, UDAT_STANDALONE_SHORT_QUARTERS, 3, "4QQ");
VerifysetSymbols(fr, UDAT_STANDALONE_NARROW_QUARTERS, 3, "!4");
VerifysetSymbols(zhChiCal, UDAT_CYCLIC_YEARS_ABBREVIATED, 1, "yi-chou");
VerifysetSymbols(zhChiCal, UDAT_ZODIAC_NAMES_ABBREVIATED, 1, "Ox");
/*run series of tests to test get and setSymbols regressively*/
log_verbose("\nTesting get and set symbols regressively\n");
VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 1);
VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 7);
VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 1);
VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 7);
VerifygetsetSymbols(fr, def, UDAT_MONTHS, 0);
VerifygetsetSymbols(fr, def, UDAT_SHORT_MONTHS, 0);
VerifygetsetSymbols(fr, def, UDAT_ERAS,1);
VerifygetsetSymbols(fr, def, UDAT_LOCALIZED_CHARS, 0);
VerifygetsetSymbols(fr, def, UDAT_AM_PMS, 1);
/*closing*/
udat_close(fr);
udat_close(def);
udat_close(zhChiCal);
udat_close(esMX);
udat_close(th);
if(result != NULL) {
free(result);
result = NULL;
}
free(value);
}
/**
* Test DateFormat(Calendar) API
*/
static void TestDateFormatCalendar(void) {
UDateFormat *date=0, *time=0, *full=0;
UCalendar *cal=0;
UChar buf[256];
char cbuf[256];
int32_t pos;
UDate when;
UErrorCode ec = U_ZERO_ERROR;
UChar buf1[256];
int32_t len1;
const char *expected;
UChar uExpected[32];
ctest_setTimeZone(NULL, &ec);
/* Create a formatter for date fields. */
date = udat_open(UDAT_NONE, UDAT_SHORT, "en_US", NULL, 0, NULL, 0, &ec);
if (U_FAILURE(ec)) {
log_data_err("FAIL: udat_open(NONE, SHORT, en_US) failed with %s (Are you missing data?)\n",
u_errorName(ec));
goto FAIL;
}
/* Create a formatter for time fields. */
time = udat_open(UDAT_SHORT, UDAT_NONE, "en_US", NULL, 0, NULL, 0, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: udat_open(SHORT, NONE, en_US) failed with %s\n",
u_errorName(ec));
goto FAIL;
}
/* Create a full format for output */
full = udat_open(UDAT_FULL, UDAT_FULL, "en_US", NULL, 0, NULL, 0, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: udat_open(FULL, FULL, en_US) failed with %s\n",
u_errorName(ec));
goto FAIL;
}
/* Create a calendar */
cal = ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: ucal_open(en_US) failed with %s\n",
u_errorName(ec));
goto FAIL;
}
/* Parse the date */
ucal_clear(cal);
u_uastrcpy(buf, "4/5/2001");
pos = 0;
udat_parseCalendar(date, cal, buf, -1, &pos, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: udat_parseCalendar(4/5/2001) failed at %d with %s\n",
pos, u_errorName(ec));
goto FAIL;
}
/* Check if formatCalendar matches the original date */
len1 = udat_formatCalendar(date, cal, buf1, UPRV_LENGTHOF(buf1), NULL, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: udat_formatCalendar(4/5/2001) failed with %s\n",
u_errorName(ec));
goto FAIL;
}
expected = "4/5/01";
u_uastrcpy(uExpected, expected);
if (u_strlen(uExpected) != len1 || u_strncmp(uExpected, buf1, len1) != 0) {
log_err("FAIL: udat_formatCalendar(4/5/2001), expected: %s", expected);
}
/* Parse the time */
u_uastrcpy(buf, "5:45 PM");
pos = 0;
udat_parseCalendar(time, cal, buf, -1, &pos, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: udat_parseCalendar(17:45) failed at %d with %s\n",
pos, u_errorName(ec));
goto FAIL;
}
/* Check if formatCalendar matches the original time */
len1 = udat_formatCalendar(time, cal, buf1, UPRV_LENGTHOF(buf1), NULL, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: udat_formatCalendar(17:45) failed with %s\n",
u_errorName(ec));
goto FAIL;
}
u_strcpy(uExpected, u"5:45\u202FPM");
u_austrcpy(cbuf, uExpected);
if (u_strlen(uExpected) != len1 || u_strncmp(uExpected, buf1, len1) != 0) {
log_err("FAIL: udat_formatCalendar(17:45), expected: %s", cbuf);
}
/* Check result */
when = ucal_getMillis(cal, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: ucal_getMillis() failed with %s\n", u_errorName(ec));
goto FAIL;
}
udat_format(full, when, buf, sizeof(buf), NULL, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: udat_format() failed with %s\n", u_errorName(ec));
goto FAIL;
}
u_austrcpy(cbuf, buf);
/* Thursday, April 5, 2001 5:45:00 PM PDT 986517900000 */
if (when == 986517900000.0) {
log_verbose("Ok: Parsed result: %s\n", cbuf);
} else {
log_err("FAIL: Parsed result: %s, exp 4/5/2001 5:45 PM\n", cbuf);
}
FAIL:
udat_close(date);
udat_close(time);
udat_close(full);
ucal_close(cal);
ctest_resetTimeZone();
}
/**
* Test parsing two digit year against "YY" vs. "YYYY" patterns
*/
static void TestCalendarDateParse(void) {
int32_t result;
UErrorCode ec = U_ZERO_ERROR;
UDateFormat* simpleDateFormat = 0;
int32_t parsePos = 0;
int32_t twoDigitCenturyStart = 75;
int32_t currentTwoDigitYear = 0;
int32_t startCentury = 0;
UCalendar* tempCal = 0;
UCalendar* calendar = 0;
U_STRING_DECL(pattern, "yyyy", 4);
U_STRING_DECL(pattern2, "yy", 2);
U_STRING_DECL(text, "75", 2);
U_STRING_INIT(pattern, "yyyy", 4);
U_STRING_INIT(pattern2, "yy", 2);
U_STRING_INIT(text, "75", 2);
simpleDateFormat = udat_open(UDAT_FULL, UDAT_FULL, "en-GB", 0, 0, 0, 0, &ec);
if (U_FAILURE(ec)) {
log_data_err("udat_open(UDAT_FULL, UDAT_FULL, \"en-GB\", 0, 0, 0, 0, &ec) failed: %s - (Are you missing data?)\n", u_errorName(ec));
return;
}
udat_applyPattern(simpleDateFormat, 0, pattern, u_strlen(pattern));
udat_setLenient(simpleDateFormat, 0);
currentTwoDigitYear = getCurrentYear() % 100;
startCentury = getCurrentYear() - currentTwoDigitYear;
if (twoDigitCenturyStart > currentTwoDigitYear) {
startCentury -= 100;
}
tempCal = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &ec);
ucal_setMillis(tempCal, 0, &ec);
ucal_setDateTime(tempCal, startCentury + twoDigitCenturyStart, UCAL_JANUARY, 1, 0, 0, 0, &ec);
udat_set2DigitYearStart(simpleDateFormat, ucal_getMillis(tempCal, &ec), &ec);
calendar = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &ec);
ucal_setMillis(calendar, 0, &ec);
ucal_setDateTime(calendar, twoDigitCenturyStart, UCAL_JANUARY, 1, 0, 0, 0, &ec);
udat_parseCalendar(simpleDateFormat, calendar, text, u_strlen(text), &parsePos, &ec);
/* Check result */
result = ucal_get(calendar, UCAL_YEAR, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: ucal_get(UCAL_YEAR) failed with %s\n", u_errorName(ec));
goto FAIL;
}
if (result != 75) {
log_err("FAIL: parsed incorrect year: %d\n", result);
goto FAIL;
}
parsePos = 0;
udat_applyPattern(simpleDateFormat, 0, pattern2, u_strlen(pattern2));
udat_parseCalendar(simpleDateFormat, calendar, text, u_strlen(text), &parsePos, &ec);
/* Check result */
result = ucal_get(calendar, UCAL_YEAR, &ec);
if (U_FAILURE(ec)) {
log_err("FAIL: ucal_get(UCAL_YEAR) failed with %s\n", u_errorName(ec));
goto FAIL;
}
if (result != 1975) {
log_err("FAIL: parsed incorrect year: %d\n", result);
goto FAIL;
}
FAIL:
udat_close(simpleDateFormat);
ucal_close(tempCal);
ucal_close(calendar);
}
/*INTERNAL FUNCTIONS USED*/
static int getCurrentYear(void) {
static int currentYear = 0;
if (currentYear == 0) {
UErrorCode status = U_ZERO_ERROR;
UCalendar *cal = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &status);
if (!U_FAILURE(status)) {
/* Get the current year from the default UCalendar */
currentYear = ucal_get(cal, UCAL_YEAR, &status);
ucal_close(cal);
}
}
return currentYear;
}
/* N.B.: use idx instead of index to avoid 'shadow' warnings in strict mode. */
static void VerifygetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t idx, const char* expected)
{
UChar *pattern=NULL;
UErrorCode status = U_ZERO_ERROR;
UChar *result=NULL;
int32_t resultlength, resultlengthout;
int32_t patternSize = (int32_t)strlen(expected) + 1;
pattern=(UChar*)malloc(sizeof(UChar) * patternSize);
u_unescape(expected, pattern, patternSize);
resultlength=0;
resultlengthout=udat_getSymbols(datfor, type, idx , NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR)
{
status=U_ZERO_ERROR;
resultlength=resultlengthout+1;
result=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_getSymbols(datfor, type, idx, result, resultlength, &status);
}
if(U_FAILURE(status))
{
log_err("FAIL: Error in udat_getSymbols()... %s\n", myErrorName(status) );
return;
}
if(u_strcmp(result, pattern)==0)
log_verbose("PASS: getSymbols retrieved the right value\n");
else{
log_data_err("FAIL: getSymbols retrieved the wrong value\n Expected %s Got %s\n", expected,
aescstrdup(result,-1) );
}
free(result);
free(pattern);
}
static void VerifysetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t idx, const char* expected)
{
UChar *result=NULL;
UChar *value=NULL;
int32_t resultlength, resultlengthout;
UErrorCode status = U_ZERO_ERROR;
int32_t valueLen, valueSize = (int32_t)strlen(expected) + 1;
value=(UChar*)malloc(sizeof(UChar) * valueSize);
valueLen = u_unescape(expected, value, valueSize);
udat_setSymbols(datfor, type, idx, value, valueLen, &status);
if(U_FAILURE(status))
{
log_err("FAIL: Error in udat_setSymbols() %s\n", myErrorName(status) );
return;
}
resultlength=0;
resultlengthout=udat_getSymbols(datfor, type, idx, NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR){
status=U_ZERO_ERROR;
resultlength=resultlengthout+1;
result=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_getSymbols(datfor, type, idx, result, resultlength, &status);
}
if(U_FAILURE(status)){
log_err("FAIL: error in retrieving the value using getSymbols after setting it previously\n %s\n",
myErrorName(status) );
return;
}
if(u_strcmp(result, value)!=0){
log_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", expected,
aescstrdup(result,-1) );
}
else
log_verbose("PASS: setSymbols successful\n");
free(value);
free(result);
}
static void VerifygetsetSymbols(UDateFormat* from, UDateFormat* to, UDateFormatSymbolType type, int32_t idx)
{
UChar *result=NULL;
UChar *value=NULL;
int32_t resultlength, resultlengthout;
UErrorCode status = U_ZERO_ERROR;
resultlength=0;
resultlengthout=udat_getSymbols(from, type, idx , NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR){
status=U_ZERO_ERROR;
resultlength=resultlengthout+1;
result=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_getSymbols(from, type, idx, result, resultlength, &status);
}
if(U_FAILURE(status)){
log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) );
return;
}
resultlength=resultlengthout+1;
udat_setSymbols(to, type, idx, result, resultlength, &status);
if(U_FAILURE(status))
{
log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) );
return;
}
resultlength=0;
resultlengthout=udat_getSymbols(to, type, idx, NULL, resultlength, &status);
if(status==U_BUFFER_OVERFLOW_ERROR){
status=U_ZERO_ERROR;
resultlength=resultlengthout+1;
value=(UChar*)malloc(sizeof(UChar) * resultlength);
udat_getSymbols(to, type, idx, value, resultlength, &status);
}
if(U_FAILURE(status)){
log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n %s\n",
myErrorName(status) );
return;
}
if(u_strcmp(result, value)!=0){
log_data_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(result),
austrdup(value) );
}
else
log_verbose("PASS: setSymbols successful\n");
free(value);
free(result);
}
static UChar* myNumformat(const UNumberFormat* numfor, double d)
{
UChar *result2=NULL;
int32_t resultlength, resultlengthneeded;
UErrorCode status = U_ZERO_ERROR;
resultlength=0;
resultlengthneeded=unum_formatDouble(numfor, d, NULL, resultlength, NULL, &status);
if(status==U_BUFFER_OVERFLOW_ERROR)
{
status=U_ZERO_ERROR;
resultlength=resultlengthneeded+1;
/*result2=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /* this leaks */
result2=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/
unum_formatDouble(numfor, d, result2, resultlength, NULL, &status);
}
if(U_FAILURE(status))
{
log_err("FAIL: Error in formatting using unum_format(.....) %s\n", myErrorName(status) );
return 0;
}
return result2;
}
/**
* The search depth for TestExtremeDates. The total number of
* dates that will be tested is (2^EXTREME_DATES_DEPTH) - 1.
*/
#define EXTREME_DATES_DEPTH 8
/**
* Support for TestExtremeDates (below).
*
* Test a single date to see whether udat_format handles it properly.
*/
static UBool _aux1ExtremeDates(UDateFormat* fmt, UDate date,
UChar* buf, int32_t buflen, char* cbuf,
UErrorCode* ec) {
int32_t len = udat_format(fmt, date, buf, buflen, 0, ec);
if (!assertSuccess("udat_format", ec)) return false;
u_austrncpy(cbuf, buf, buflen);
if (len < 4) {
log_err("FAIL: udat_format(%g) => \"%s\"\n", date, cbuf);
} else {
log_verbose("udat_format(%g) => \"%s\"\n", date, cbuf);
}
return true;
}
/**
* Support for TestExtremeDates (below).
*
* Recursively test between 'small' and 'large', up to the depth
* limit specified by EXTREME_DATES_DEPTH.
*/
static UBool _aux2ExtremeDates(UDateFormat* fmt, UDate small, UDate large,
UChar* buf, int32_t buflen, char* cbuf,
int32_t count,
UErrorCode* ec) {
/* Logarithmic midpoint; see below */
UDate mid = (UDate) exp((log(small) + log(large)) / 2);
if (count == EXTREME_DATES_DEPTH) {
return true;
}
return
_aux1ExtremeDates(fmt, mid, buf, buflen, cbuf, ec) &&
_aux2ExtremeDates(fmt, small, mid, buf, buflen, cbuf, count+1, ec) &&
_aux2ExtremeDates(fmt, mid, large, buf, buflen, cbuf, count+1, ec);
}
/**
* http://www.jtcsv.com/cgibin/icu-bugs?findid=3659
*
* For certain large dates, udat_format crashes on MacOS. This test
* attempts to reproduce this problem by doing a recursive logarithmic*
* binary search of a predefined interval (from 'small' to 'large').
*
* The limit of the search is given by EXTREME_DATES_DEPTH, above.
*
* *The search has to be logarithmic, not linear. A linear search of the
* range 0..10^30, for example, will find 0.5*10^30, then 0.25*10^30 and
* 0.75*10^30, etc. A logarithmic search will find 10^15, then 10^7.5
* and 10^22.5, etc.
*/
static void TestExtremeDates(void) {
UDateFormat *fmt;
UErrorCode ec;
UChar buf[256];
char cbuf[256];
const double small = 1000; /* 1 sec */
const double large = 1e+30; /* well beyond usable UDate range */
/* There is no need to test larger values from 1e+30 to 1e+300;
the failures occur around 1e+27, and never above 1e+30. */
ec = U_ZERO_ERROR;
fmt = udat_open(UDAT_LONG, UDAT_LONG, "en_US",
0, 0, 0, 0, &ec);
if (U_FAILURE(ec)) {
log_data_err("FAIL: udat_open (%s) (Are you missing data?)\n", u_errorName(ec));
return;
}
_aux2ExtremeDates(fmt, small, large, buf, UPRV_LENGTHOF(buf), cbuf, 0, &ec);
udat_close(fmt);
}
static void TestAllLocales(void) {
int32_t idx, dateIdx, timeIdx, localeCount;
static const UDateFormatStyle style[] = {
UDAT_FULL, UDAT_LONG, UDAT_MEDIUM, UDAT_SHORT
};
localeCount = uloc_countAvailable();
for (idx = 0; idx < localeCount; idx++) {
for (dateIdx = 0; dateIdx < UPRV_LENGTHOF(style); dateIdx++) {
for (timeIdx = 0; timeIdx < UPRV_LENGTHOF(style); timeIdx++) {
UErrorCode status = U_ZERO_ERROR;
udat_close(udat_open(style[dateIdx], style[timeIdx],
uloc_getAvailable(idx), NULL, 0, NULL, 0, &status));
if (U_FAILURE(status)) {
log_err("FAIL: udat_open(%s) failed with (%s) dateIdx=%d, timeIdx=%d\n",
uloc_getAvailable(idx), u_errorName(status), dateIdx, timeIdx);
}
}
}
}
}
static void TestRelativeCrash(void) {
static const UChar tzName[] = { 0x0055, 0x0053, 0x002F, 0x0050, 0x0061, 0x0063, 0x0069, 0x0066, 0x0069, 0x0063, 0 };
static const UDate aDate = -631152000000.0;
UErrorCode status = U_ZERO_ERROR;
UErrorCode expectStatus = U_ILLEGAL_ARGUMENT_ERROR;
UDateFormat icudf;
icudf = udat_open(UDAT_NONE, UDAT_SHORT_RELATIVE, "en", tzName, -1, NULL, 0, &status);
if ( U_SUCCESS(status) ) {
const char *what = "???";
{
UErrorCode subStatus = U_ZERO_ERROR;
what = "udat_set2DigitYearStart";
log_verbose("Trying %s on a relative date..\n", what);
udat_set2DigitYearStart(icudf, aDate, &subStatus);
if(subStatus == expectStatus) {
log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
} else {
log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
}
}
{
/* clone works polymorphically. try it anyways */
UErrorCode subStatus = U_ZERO_ERROR;
UDateFormat *oth;
what = "clone";
log_verbose("Trying %s on a relative date..\n", what);
oth = udat_clone(icudf, &subStatus);
if(subStatus == U_ZERO_ERROR) {
log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
udat_close(oth); /* ? */
} else {
log_err("FAIL: didn't crash on %s, but got %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
}
}
{
UErrorCode subStatus = U_ZERO_ERROR;
what = "udat_get2DigitYearStart";
log_verbose("Trying %s on a relative date..\n", what);
udat_get2DigitYearStart(icudf, &subStatus);
if(subStatus == expectStatus) {
log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
} else {
log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
}
}
{
/* Now udat_toPattern works for relative date formatters, unless localized is true */
UErrorCode subStatus = U_ZERO_ERROR;
what = "udat_toPattern";
log_verbose("Trying %s on a relative date..\n", what);
udat_toPattern(icudf, true,NULL,0, &subStatus);
if(subStatus == expectStatus) {
log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
} else {
log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
}
}
{
UErrorCode subStatus = U_ZERO_ERROR;
what = "udat_applyPattern";
log_verbose("Trying %s on a relative date..\n", what);
udat_applyPattern(icudf, false,tzName,-1);
subStatus = U_ILLEGAL_ARGUMENT_ERROR; /* what it should be, if this took an errorcode. */
if(subStatus == expectStatus) {
log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
} else {
log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
}
}
{
UChar erabuf[32];
UErrorCode subStatus = U_ZERO_ERROR;
what = "udat_getSymbols";
log_verbose("Trying %s on a relative date..\n", what);
udat_getSymbols(icudf, UDAT_ERAS,0,erabuf,UPRV_LENGTHOF(erabuf), &subStatus);
if(subStatus == U_ZERO_ERROR) {
log_verbose("Success: %s returned %s.\n", what, u_errorName(subStatus));
} else {
log_err("FAIL: didn't crash on %s, but got %s instead of U_ZERO_ERROR.\n", what, u_errorName(subStatus));
}
}
{
UErrorCode subStatus = U_ZERO_ERROR;
UChar symbolValue = 0x0041;
what = "udat_setSymbols";
log_verbose("Trying %s on a relative date..\n", what);
udat_setSymbols(icudf, UDAT_ERAS,0,&symbolValue,1, &subStatus); /* bogus values */
if(subStatus == expectStatus) {
log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
} else {
log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
}
}
{
UErrorCode subStatus = U_ZERO_ERROR;
what = "udat_countSymbols";
log_verbose("Trying %s on a relative date..\n", what);
udat_countSymbols(icudf, UDAT_ERAS);
subStatus = U_ILLEGAL_ARGUMENT_ERROR; /* should have an errorcode. */
if(subStatus == expectStatus) {
log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
} else {
log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
}
}
udat_close(icudf);
} else {
log_data_err("FAIL: err calling udat_open() ->%s (Are you missing data?)\n", u_errorName(status));
}
}
static const UChar skeleton_yMMMM[] = { 0x79,0x4D,0x4D,0x4D,0x4D,0 }; /* "yMMMM"; fr maps to "MMMM y", cs maps to "LLLL y" */
static const UChar july2008_frDefault[] = { 0x6A,0x75,0x69,0x6C,0x6C,0x65,0x74,0x20,0x32,0x30,0x30,0x38,0 }; /* "juillet 2008" */
static const UChar july2008_frTitle[] = { 0x4A,0x75,0x69,0x6C,0x6C,0x65,0x74,0x20,0x32,0x30,0x30,0x38,0 }; /* "Juillet 2008" sentence-begin, standalone */
static const UChar july2008_csDefault[] = { 0x10D,0x65,0x72,0x76,0x65,0x6E,0x65,0x63,0x20,0x32,0x30,0x30,0x38,0 }; /* "c(hacek)ervenec 2008" */
static const UChar july2008_csTitle[] = { 0x10C,0x65,0x72,0x76,0x65,0x6E,0x65,0x63,0x20,0x32,0x30,0x30,0x38,0 }; /* "C(hacek)ervenec 2008" sentence-begin, uiListOrMenu */
typedef struct {
const char * locale;
const UChar * skeleton;
UDisplayContext capitalizationContext;
const UChar * expectedFormat;
} TestContextItem;
static const TestContextItem textContextItems[] = {
{ "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_NONE, july2008_frDefault },
#if !UCONFIG_NO_BREAK_ITERATION
{ "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, july2008_frDefault },
{ "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, july2008_frTitle },
{ "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, july2008_frDefault },
{ "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, july2008_frTitle },
#endif
{ "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_NONE, july2008_csDefault },
#if !UCONFIG_NO_BREAK_ITERATION
{ "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, july2008_csDefault },
{ "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, july2008_csTitle },
{ "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, july2008_csTitle },
{ "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, july2008_csDefault },
#endif
{ NULL, NULL, (UDisplayContext)0, NULL }
};
static const UChar today_enDefault[] = { 0x74,0x6F,0x64,0x61,0x79,0 }; /* "today" */
static const UChar today_enTitle[] = { 0x54,0x6F,0x64,0x61,0x79,0 }; /* "Today" sentence-begin, uiListOrMenu, standalone */
static const UChar yesterday_enDefault[] = { 0x79,0x65,0x73,0x74,0x65,0x72,0x64,0x61,0x79,0 }; /* "yesterday" */
static const UChar yesterday_enTitle[] = { 0x59,0x65,0x73,0x74,0x65,0x72,0x64,0x61,0x79,0 }; /* "Yesterday" sentence-begin, uiListOrMenu, standalone */
static const UChar today_nbDefault[] = { 0x69,0x20,0x64,0x61,0x67,0 }; /* "i dag" */
static const UChar today_nbTitle[] = { 0x49,0x20,0x64,0x61,0x67,0 }; /* "I dag" sentence-begin, standalone */
static const UChar yesterday_nbDefault[] = { 0x69,0x20,0x67,0xE5,0x72,0 };
static const UChar yesterday_nbTitle[] = { 0x49,0x20,0x67,0xE5,0x72,0 };
typedef struct {
const char * locale;
UDisplayContext capitalizationContext;
const UChar * expectedFormatToday;
const UChar * expectedFormatYesterday;
} TestRelativeContextItem;
static const TestRelativeContextItem textContextRelativeItems[] = {
{ "en", UDISPCTX_CAPITALIZATION_NONE, today_enDefault, yesterday_enDefault },
#if !UCONFIG_NO_BREAK_ITERATION
{ "en", UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, today_enDefault, yesterday_enDefault },
{ "en", UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, today_enTitle, yesterday_enTitle },
{ "en", UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, today_enTitle, yesterday_enTitle },
{ "en", UDISPCTX_CAPITALIZATION_FOR_STANDALONE, today_enTitle, yesterday_enTitle },
#endif
{ "nb", UDISPCTX_CAPITALIZATION_NONE, today_nbDefault, yesterday_nbDefault },
#if !UCONFIG_NO_BREAK_ITERATION
{ "nb", UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, today_nbDefault, yesterday_nbDefault },
{ "nb", UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, today_nbTitle, yesterday_nbTitle },
{ "nb", UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, today_nbDefault, yesterday_nbDefault },
{ "nb", UDISPCTX_CAPITALIZATION_FOR_STANDALONE, today_nbTitle, yesterday_nbTitle },
#endif
{ NULL, (UDisplayContext)0, NULL, NULL }
};
static const UChar zoneGMT[] = { 0x47,0x4D,0x54,0 }; /* "GMT" */
static const UDate july022008 = 1215000000000.0;
enum { kUbufMax = 64, kBbufMax = 3*kUbufMax };
static void TestContext(void) {
const TestContextItem* textContextItemPtr;
const TestRelativeContextItem* textRelContextItemPtr;
for (textContextItemPtr = textContextItems; textContextItemPtr->locale != NULL; ++textContextItemPtr) {
UErrorCode status = U_ZERO_ERROR;
UDateTimePatternGenerator* udtpg = udatpg_open(textContextItemPtr->locale, &status);
if ( U_SUCCESS(status) ) {
UChar ubuf[kUbufMax];
int32_t len = udatpg_getBestPattern(udtpg, textContextItemPtr->skeleton, -1, ubuf, kUbufMax, &status);
if ( U_SUCCESS(status) ) {
UDateFormat* udfmt = udat_open(UDAT_PATTERN, UDAT_PATTERN, textContextItemPtr->locale, zoneGMT, -1, ubuf, len, &status);
if ( U_SUCCESS(status) ) {
udat_setContext(udfmt, textContextItemPtr->capitalizationContext, &status);
if ( U_SUCCESS(status) ) {
UDisplayContext getContext;
len = udat_format(udfmt, july022008, ubuf, kUbufMax, NULL, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udat_format for locale %s, capitalizationContext %d, status %s\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) );
status = U_ZERO_ERROR;
} else if (u_strncmp(ubuf, textContextItemPtr->expectedFormat, kUbufMax) != 0) {
char bbuf1[kBbufMax];
char bbuf2[kBbufMax];
log_err("FAIL: udat_format for locale %s, capitalizationContext %d, expected %s, got %s\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext,
u_austrncpy(bbuf1,textContextItemPtr->expectedFormat,kUbufMax), u_austrncpy(bbuf2,ubuf,kUbufMax) );
}
getContext = udat_getContext(udfmt, UDISPCTX_TYPE_CAPITALIZATION, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udat_getContext for locale %s, capitalizationContext %d, status %s\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) );
} else if (getContext != textContextItemPtr->capitalizationContext) {
log_err("FAIL: udat_getContext for locale %s, capitalizationContext %d, got context %d\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, (int)getContext );
}
} else {
log_err("FAIL: udat_setContext for locale %s, capitalizationContext %d, status %s\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) );
}
udat_close(udfmt);
} else {
log_data_err("FAIL: udat_open for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) );
}
} else {
log_err("FAIL: udatpg_getBestPattern for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) );
}
udatpg_close(udtpg);
} else {
log_data_err("FAIL: udatpg_open for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) );
}
}
for (textRelContextItemPtr = textContextRelativeItems; textRelContextItemPtr->locale != NULL; ++textRelContextItemPtr) {
UErrorCode status = U_ZERO_ERROR;
UCalendar* ucal = ucal_open(zoneGMT, -1, "root", UCAL_GREGORIAN, &status);
if ( U_SUCCESS(status) ) {
UDateFormat* udfmt = udat_open(UDAT_NONE, UDAT_LONG_RELATIVE, textRelContextItemPtr->locale, zoneGMT, -1, NULL, 0, &status);
if ( U_SUCCESS(status) ) {
udat_setContext(udfmt, textRelContextItemPtr->capitalizationContext, &status);
if ( U_SUCCESS(status) ) {
UDate yesterday, today = ucal_getNow();
UChar ubuf[kUbufMax];
char bbuf1[kBbufMax];
char bbuf2[kBbufMax];
int32_t len = udat_format(udfmt, today, ubuf, kUbufMax, NULL, &status);
(void)len;
if ( U_FAILURE(status) ) {
log_err("FAIL: udat_format today for locale %s, capitalizationContext %d, status %s\n",
textRelContextItemPtr->locale, (int)textRelContextItemPtr->capitalizationContext, u_errorName(status) );
} else if (u_strncmp(ubuf, textRelContextItemPtr->expectedFormatToday, kUbufMax) != 0) {
log_err("FAIL: udat_format today for locale %s, capitalizationContext %d, expected %s, got %s\n",
textRelContextItemPtr->locale, (int)textRelContextItemPtr->capitalizationContext,
u_austrncpy(bbuf1,textRelContextItemPtr->expectedFormatToday,kUbufMax), u_austrncpy(bbuf2,ubuf,kUbufMax) );
}
status = U_ZERO_ERROR;
ucal_setMillis(ucal, today, &status);
ucal_add(ucal, UCAL_DATE, -1, &status);
yesterday = ucal_getMillis(ucal, &status);
if ( U_SUCCESS(status) ) {
len = udat_format(udfmt, yesterday, ubuf, kUbufMax, NULL, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udat_format yesterday for locale %s, capitalizationContext %d, status %s\n",
textRelContextItemPtr->locale, (int)textRelContextItemPtr->capitalizationContext, u_errorName(status) );
} else if (u_strncmp(ubuf, textRelContextItemPtr->expectedFormatYesterday, kUbufMax) != 0) {
log_err("FAIL: udat_format yesterday for locale %s, capitalizationContext %d, expected %s, got %s\n",
textRelContextItemPtr->locale, (int)textRelContextItemPtr->capitalizationContext,
u_austrncpy(bbuf1,textRelContextItemPtr->expectedFormatYesterday,kUbufMax), u_austrncpy(bbuf2,ubuf,kUbufMax) );
}
}
} else {
log_err("FAIL: udat_setContext relative for locale %s, capitalizationContext %d, status %s\n",
textRelContextItemPtr->locale, (int)textRelContextItemPtr->capitalizationContext, u_errorName(status) );
}
udat_close(udfmt);
} else {
log_data_err("FAIL: udat_open relative for locale %s, status %s\n", textRelContextItemPtr->locale, u_errorName(status) );
}
ucal_close(ucal);
} else {
log_data_err("FAIL: ucal_open for locale root, status %s\n", u_errorName(status) );
}
}
}
// overrideNumberFormat[i][0] is to tell which field to set,
// overrideNumberFormat[i][1] is the expected result
static const char * overrideNumberFormat[][2] = {
{"", "\\u521D\\u4E03 \\u521D\\u4E8C"},
{"d", "07 \\u521D\\u4E8C"},
{"do", "07 \\u521D\\u4E8C"},
{"Md", "\\u521D\\u4E03 \\u521D\\u4E8C"},
{"MdMMd", "\\u521D\\u4E03 \\u521D\\u4E8C"},
{"mixed", "\\u521D\\u4E03 \\u521D\\u4E8C"}
};
static void TestOverrideNumberFormat(void) {
UErrorCode status = U_ZERO_ERROR;
UChar pattern[50];
UChar expected[50];
UChar fields[50];
char bbuf1[kBbufMax];
char bbuf2[kBbufMax];
const char* localeString = "zh@numbers=hanidays";
UDateFormat* fmt;
const UNumberFormat* getter_result;
int32_t i;
u_uastrcpy(fields, "d");
u_uastrcpy(pattern,"MM d");
fmt=udat_open(UDAT_PATTERN, UDAT_PATTERN, "en_US", zoneGMT, -1, pattern, u_strlen(pattern), &status);
if (!assertSuccess("udat_open()", &status)) {
return;
}
// loop 5 times to check getter/setter
for (i = 0; i < 5; i++){
status = U_ZERO_ERROR;
UNumberFormat* overrideFmt;
overrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &status);
assertSuccess("unum_open()", &status);
udat_adoptNumberFormatForFields(fmt, fields, overrideFmt, &status);
overrideFmt = NULL; // no longer valid
assertSuccess("udat_setNumberFormatForField()", &status);
getter_result = udat_getNumberFormatForField(fmt, 0x0064 /*'d'*/);
if(getter_result == NULL) {
log_err("FAIL: udat_getNumberFormatForField did not return a valid pointer\n");
}
}
{
status = U_ZERO_ERROR;
UNumberFormat* overrideFmt;
overrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &status);
assertSuccess("unum_open()", &status);
if (U_SUCCESS(status)) {
udat_setNumberFormat(fmt, overrideFmt); // test the same override NF will not crash
}
unum_close(overrideFmt);
}
udat_close(fmt);
for (i=0; i<UPRV_LENGTHOF(overrideNumberFormat); i++){
status = U_ZERO_ERROR;
UChar ubuf[kUbufMax];
UDateFormat* fmt2;
UNumberFormat* overrideFmt2;
fmt2 =udat_open(UDAT_PATTERN, UDAT_PATTERN,"en_US", zoneGMT, -1, pattern, u_strlen(pattern), &status);
assertSuccess("udat_open() with en_US", &status);
overrideFmt2 = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &status);
assertSuccess("unum_open() in loop", &status);
if (U_FAILURE(status)) {
continue;
}
u_uastrcpy(fields, overrideNumberFormat[i][0]);
u_unescape(overrideNumberFormat[i][1], expected, UPRV_LENGTHOF(expected));
if ( strcmp(overrideNumberFormat[i][0], "") == 0 ) { // use the one w/o field
udat_adoptNumberFormat(fmt2, overrideFmt2);
} else if ( strcmp(overrideNumberFormat[i][0], "mixed") == 0 ) { // set 1 field at first but then full override, both(M & d) should be override
const char* singleLocale = "en@numbers=hebr";
UNumberFormat* singleOverrideFmt;
u_uastrcpy(fields, "d");
singleOverrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, singleLocale, NULL, &status);
assertSuccess("unum_open() in mixed", &status);
udat_adoptNumberFormatForFields(fmt2, fields, singleOverrideFmt, &status);
assertSuccess("udat_setNumberFormatForField() in mixed", &status);
udat_adoptNumberFormat(fmt2, overrideFmt2);
} else if ( strcmp(overrideNumberFormat[i][0], "do") == 0 ) { // o is an invalid field
udat_adoptNumberFormatForFields(fmt2, fields, overrideFmt2, &status);
if(status == U_INVALID_FORMAT_ERROR) {
udat_close(fmt2);
status = U_ZERO_ERROR;
continue;
}
} else {
udat_adoptNumberFormatForFields(fmt2, fields, overrideFmt2, &status);
assertSuccess("udat_setNumberFormatForField() in loop", &status);
}
udat_format(fmt2, july022008, ubuf, kUbufMax, NULL, &status);
assertSuccess("udat_format() july022008", &status);
if (u_strncmp(ubuf, expected, kUbufMax) != 0)
log_err("fail: udat_format for locale, expected %s, got %s\n",
u_austrncpy(bbuf1,expected,kUbufMax), u_austrncpy(bbuf2,ubuf,kUbufMax) );
udat_close(fmt2);
}
}
/*
* Ticket #11523
* udat_parse and udat_parseCalendar should have the same error code when given the same invalid input.
*/
static void TestParseErrorReturnValue(void) {
UErrorCode status = U_ZERO_ERROR;
UErrorCode expectStatus = U_PARSE_ERROR;
UDateFormat* df;
UCalendar* cal;
df = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, NULL, NULL, -1, NULL, -1, &status);
if (!assertSuccessCheck("udat_open()", &status, true)) {
return;
}
cal = ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &status);
if (!assertSuccess("ucal_open()", &status)) {
return;
}
udat_parse(df, NULL, -1, NULL, &status);
if (status != expectStatus) {
log_err("%s should have been returned by udat_parse when given an invalid input, instead got - %s\n", u_errorName(expectStatus), u_errorName(status));
}
status = U_ZERO_ERROR;
udat_parseCalendar(df, cal, NULL, -1, NULL, &status);
if (status != expectStatus) {
log_err("%s should have been returned by udat_parseCalendar when given an invalid input, instead got - %s\n", u_errorName(expectStatus), u_errorName(status));
}
ucal_close(cal);
udat_close(df);
}
/*
* Ticket #11553
* Test new udat_formatForFields, udat_formatCalendarForFields (and UFieldPositionIterator)
*/
static const char localeForFields[] = "en_US";
/* zoneGMT[]defined above */
static const UDate date2015Feb25 = 1424841000000.0; /* Wednesday, February 25, 2015 at 5:10:00 AM GMT */
static const UChar patNoFields[] = { 0x0027, 0x0078, 0x0078, 0x0078, 0x0027, 0 }; /* "'xxx'" */
typedef struct {
int32_t field;
int32_t beginPos;
int32_t endPos;
} FieldsData;
static const FieldsData expectedFields[] = {
{ UDAT_DAY_OF_WEEK_FIELD /* 9*/, 0, 9 },
{ UDAT_MONTH_FIELD /* 2*/, 11, 19 },
{ UDAT_DATE_FIELD /* 3*/, 20, 22 },
{ UDAT_YEAR_FIELD /* 1*/, 24, 28 },
{ UDAT_HOUR1_FIELD /*15*/, 32, 33 },
#if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
{ UDAT_TIME_SEPARATOR_FIELD /*35*/, 33, 34 },
#endif
{ UDAT_MINUTE_FIELD /* 6*/, 34, 36 },
#if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
{ UDAT_TIME_SEPARATOR_FIELD /*35*/, 36, 37 },
#endif
{ UDAT_SECOND_FIELD /* 7*/, 37, 39 },
{ UDAT_AM_PM_FIELD /*14*/, 40, 42 },
{ UDAT_TIMEZONE_FIELD /*17*/, 43, 46 },
{ -1, -1, -1 },
};
enum {kUBufFieldsLen = 128, kBBufFieldsLen = 256 };
static void TestFormatForFields(void) {
UErrorCode status = U_ZERO_ERROR;
UFieldPositionIterator* fpositer = ufieldpositer_open(&status);
if ( U_FAILURE(status) ) {
log_err("ufieldpositer_open fails, status %s\n", u_errorName(status));
} else {
UDateFormat* udfmt = udat_open(UDAT_LONG, UDAT_FULL, localeForFields, zoneGMT, -1, NULL, 0, &status);
UCalendar* ucal = ucal_open(zoneGMT, -1, localeForFields, UCAL_DEFAULT, &status);
if ( U_FAILURE(status) ) {
log_data_err("udat_open or ucal_open fails for locale %s, status %s (Are you missing data?)\n", localeForFields, u_errorName(status));
} else {
int32_t ulen, field, beginPos, endPos;
UChar ubuf[kUBufFieldsLen];
const FieldsData * fptr;
status = U_ZERO_ERROR;
ulen = udat_formatForFields(udfmt, date2015Feb25, ubuf, kUBufFieldsLen, fpositer, &status);
if ( U_FAILURE(status) ) {
log_err("udat_formatForFields fails, status %s\n", u_errorName(status));
} else {
for (fptr = expectedFields; ; fptr++) {
field = ufieldpositer_next(fpositer, &beginPos, &endPos);
if (field != fptr->field || (field >= 0 && (beginPos != fptr->beginPos || endPos != fptr->endPos))) {
if (fptr->field >= 0) {
log_err("udat_formatForFields as \"%s\"; expect field %d range %d-%d, get field %d range %d-%d\n",
aescstrdup(ubuf, ulen), fptr->field, fptr->beginPos, fptr->endPos, field, beginPos, endPos);
} else {
log_err("udat_formatForFields as \"%s\"; expect field < 0, get field %d range %d-%d\n",
aescstrdup(ubuf, ulen), field, beginPos, endPos);
}
break;
}
if (field < 0) {
break;
}
}
}
ucal_setMillis(ucal, date2015Feb25, &status);
status = U_ZERO_ERROR;
ulen = udat_formatCalendarForFields(udfmt, ucal, ubuf, kUBufFieldsLen, fpositer, &status);
if ( U_FAILURE(status) ) {
log_err("udat_formatCalendarForFields fails, status %s\n", u_errorName(status));
} else {
for (fptr = expectedFields; ; fptr++) {
field = ufieldpositer_next(fpositer, &beginPos, &endPos);
if (field != fptr->field || (field >= 0 && (beginPos != fptr->beginPos || endPos != fptr->endPos))) {
if (fptr->field >= 0) {
log_err("udat_formatFudat_formatCalendarForFieldsorFields as \"%s\"; expect field %d range %d-%d, get field %d range %d-%d\n",
aescstrdup(ubuf, ulen), fptr->field, fptr->beginPos, fptr->endPos, field, beginPos, endPos);
} else {
log_err("udat_formatCalendarForFields as \"%s\"; expect field < 0, get field %d range %d-%d\n",
aescstrdup(ubuf, ulen), field, beginPos, endPos);
}
break;
}
if (field < 0) {
break;
}
}
}
udat_applyPattern(udfmt, false, patNoFields, -1);
status = U_ZERO_ERROR;
ulen = udat_formatForFields(udfmt, date2015Feb25, ubuf, kUBufFieldsLen, fpositer, &status);
if ( U_FAILURE(status) ) {
log_err("udat_formatForFields with no-field pattern fails, status %s\n", u_errorName(status));
} else {
field = ufieldpositer_next(fpositer, &beginPos, &endPos);
if (field >= 0) {
log_err("udat_formatForFields with no-field pattern as \"%s\"; expect field < 0, get field %d range %d-%d\n",
aescstrdup(ubuf, ulen), field, beginPos, endPos);
}
}
ucal_close(ucal);
udat_close(udfmt);
}
ufieldpositer_close(fpositer);
}
}
static void TestForceGannenNumbering(void) {
UErrorCode status;
const char* locID = "ja_JP@calendar=japanese";
UDate refDate = 600336000000.0; // 1989 Jan 9 Monday = Heisei 1
const UChar* testSkeleton = u"yMMMd";
// Test Gannen year forcing
status = U_ZERO_ERROR;
UDateTimePatternGenerator* dtpgen = udatpg_open(locID, &status);
if (U_FAILURE(status)) {
log_data_err("Fail in udatpg_open locale %s: %s", locID, u_errorName(status));
} else {
UChar pattern[kUbufMax];
int32_t patlen = udatpg_getBestPattern(dtpgen, testSkeleton, -1, pattern, kUbufMax, &status);
if (U_FAILURE(status)) {
log_data_err("Fail in udatpg_getBestPattern locale %s: %s", locID, u_errorName(status));
} else {
UDateFormat *testFmt = udat_open(UDAT_PATTERN, UDAT_PATTERN, locID, NULL, 0, pattern, patlen, &status);
if (U_FAILURE(status)) {
log_data_err("Fail in udat_open locale %s: %s", locID, u_errorName(status));
} else {
UChar testString[kUbufMax];
int32_t testStrLen = udat_format(testFmt, refDate, testString, kUbufMax, NULL, &status);
if (U_FAILURE(status)) {
log_err("Fail in udat_format locale %s: %s", locID, u_errorName(status));
} else if (testStrLen < 3 || testString[2] != 0x5143) {
char bbuf[kBbufMax];
u_austrncpy(bbuf, testString, testStrLen);
log_err("Formatting year 1 as Gannen, got%s but expected 3rd char to be 0x5143", bbuf);
}
udat_close(testFmt);
}
}
udatpg_close(dtpgen);
}
}
typedef struct {
UChar patternChar; // for future use
UDateFormatField dateField;
UCalendarDateFields calField;
} PatternCharToFieldsItem;
static const PatternCharToFieldsItem patCharToFieldsItems[] = {
{ u'G', UDAT_ERA_FIELD, UCAL_ERA },
{ u'y', UDAT_YEAR_FIELD, UCAL_YEAR },
{ u'Y', UDAT_YEAR_WOY_FIELD, UCAL_YEAR_WOY },
{ u'Q', UDAT_QUARTER_FIELD, UCAL_MONTH },
{ u'H', UDAT_HOUR_OF_DAY0_FIELD, UCAL_HOUR_OF_DAY },
{ u'r', UDAT_RELATED_YEAR_FIELD, UCAL_EXTENDED_YEAR },
{ u'B', UDAT_FLEXIBLE_DAY_PERIOD_FIELD, UCAL_FIELD_COUNT },
{ u'$', UDAT_FIELD_COUNT, UCAL_FIELD_COUNT },
{ 0xFFFF, (UDateFormatField)-1, UCAL_FIELD_COUNT }, // patternChar ignored here
{ (UChar)0, (UDateFormatField)0, (UCalendarDateFields)0 } // terminator
};
static void TestMapDateToCalFields(void){
const PatternCharToFieldsItem* itemPtr;
for ( itemPtr=patCharToFieldsItems; itemPtr->patternChar!=(UChar)0; itemPtr++) {
UCalendarDateFields calField = udat_toCalendarDateField(itemPtr->dateField);
if (calField != itemPtr->calField) {
log_err("for pattern char 0x%04X, dateField %d, expect calField %d and got %d\n",
itemPtr->patternChar, itemPtr->dateField, itemPtr->calField, calField);
}
}
}
static void TestNarrowQuarters(void) {
// Test for rdar://79238094
const UChar* testCases[] = {
u"en_US", u"QQQQ y", u"1st quarter 1970",
u"en_US", u"QQQ y", u"Q1 1970",
u"en_US", u"QQQQQ y", u"1 1970",
u"es_MX", u"QQQQ y", u"1.º trimestre 1970",
u"es_MX", u"QQQ y", u"T1 1970",
u"es_MX", u"QQQQQ y", u"1 1970",
u"en_US", u"qqqq", u"1st quarter",
u"en_US", u"qqq", u"Q1",
u"en_US", u"qqqqq", u"1",
u"es_MX", u"qqqq", u"1.º trimestre",
u"es_MX", u"qqq", u"T1",
u"es_MX", u"qqqqq", u"1",
};
UErrorCode err = U_ZERO_ERROR;
UChar result[100];
UDate parsedDate = 0;
UDate expectedFormatParsedDate = 0;
UDate expectedStandaloneParsedDate = 0;
for (int32_t i = 0; i < UPRV_LENGTHOF(testCases); i += 3) {
const UChar* localeID = testCases[i];
const UChar* pattern = testCases[i + 1];
const UChar* expectedResult = testCases[i + 2];
err = U_ZERO_ERROR;
UDateFormat* df = udat_open(UDAT_PATTERN, UDAT_PATTERN, austrdup(localeID), u"UTC", 0, pattern, -1, &err);
udat_format(df, 0, result, 100, NULL, &err);
if (assertSuccess("Formatting date failed", &err)) {
assertUEquals("Wrong formatting result", expectedResult, result);
}
bool patternIsStandaloneQuarter = u_strchr(pattern, u'q') != NULL;
parsedDate = udat_parse(df, expectedResult, -1, NULL, &err);
if (!patternIsStandaloneQuarter && expectedFormatParsedDate == 0) {
expectedFormatParsedDate = parsedDate;
} else if (patternIsStandaloneQuarter && expectedStandaloneParsedDate == 0) {
expectedStandaloneParsedDate = parsedDate;
}
if (assertSuccess("Parsing date failed", &err)) {
if (patternIsStandaloneQuarter) {
assertDoubleEquals("Wrong parsing result", expectedStandaloneParsedDate, parsedDate);
} else {
assertDoubleEquals("Wrong parsing result", expectedFormatParsedDate, parsedDate);
}
}
udat_close(df);
}
}
static void TestExtraneousCharacters(void) {
// regression test for ICU-21802
UErrorCode err = U_ZERO_ERROR;
UCalendar* cal = ucal_open(u"UTC", -1, "en_US", UCAL_GREGORIAN, &err);
UDateFormat* df = udat_open(UDAT_PATTERN, UDAT_PATTERN, "en_US", u"UTC", -1, u"yyyyMMdd", -1, &err);
if (assertSuccess("Failed to create date formatter and calendar", &err)) {
udat_setLenient(df, false);
udat_parseCalendar(df, cal, u"2021", -1, NULL, &err);
assertTrue("Success parsing '2021'", err == U_PARSE_ERROR);
err = U_ZERO_ERROR;
udat_parseCalendar(df, cal, u"2021-", -1, NULL, &err);
assertTrue("Success parsing '2021-'", err == U_PARSE_ERROR);
}
udat_close(df);
ucal_close(cal);
}
static void TestParseTooStrict(void) {
UErrorCode status = U_ZERO_ERROR;
const char* locale = "en_US";
UDateFormat* df = udat_open(UDAT_PATTERN, UDAT_PATTERN, locale, u"UTC", -1, u"MM/dd/yyyy", -1, &status);
if (U_FAILURE(status)) {
log_data_err("udat_open locale %s pattern MM/dd/yyyy: %s\n", locale, u_errorName(status));
return;
}
UCalendar* cal = ucal_open(u"UTC", -1, locale, UCAL_GREGORIAN, &status);
if (U_FAILURE(status)) {
log_data_err("ucal_open locale %s: %s\n", locale, u_errorName(status));
udat_close(df);
return;
}
ucal_clear(cal);
int32_t ppos = 0;
udat_setLenient(df, false);
udat_parseCalendar(df, cal, u"1/1/2023", -1, &ppos, &status);
if (U_FAILURE(status)) {
log_err("udat_parseCalendar locale %s, 1/1/2023: %s\n", locale, u_errorName(status));
} else if (ppos != 8) {
log_err("udat_parseCalendar locale %s, 1/1/2023: ppos expect 8, get %d\n", locale, ppos);
} else {
UDate parsedDate = ucal_getMillis(cal, &status);
if (U_FAILURE(status)) {
log_err("ucal_getMillis: %s\n", u_errorName(status));
} else if (parsedDate < 1672531200000.0 || parsedDate >= 1672617600000.0) { // check for day stating at UTC 2023-01-01 00:00
log_err("udat_parseCalendar locale %s, 1/1/2023: parsed UDate %.0f out of range\n", locale, parsedDate);
}
}
ucal_close(cal);
udat_close(df);
}
static void TestHourCycle(void) {
static const UDate date = -845601267742; // March 16, 1943 at 3:45 PM
const UChar* testCases[] = {
// test some locales for which we have data
u"en_US", u"Tuesday, March 16, 1943 at 3:45:32 PM",
u"en_CA", u"Tuesday, March 16, 1943 at 3:45:32 p.m.",
u"en_GB", u"Tuesday, 16 March 1943 at 15:45:32",
u"en_AU", u"Tuesday, 16 March 1943 at 3:45:32 pm",
// test a couple locales for which we don't have specific locale files (we should still get the correct hour cycle)
u"en_CO", u"Tuesday, March 16, 1943 at 3:45:32 PM",
u"en_MX", u"Tuesday, March 16, 1943 at 3:45:32 PM",
// test that the rg subtag does the right thing
u"en_US@rg=GBzzzz", u"Tuesday, March 16, 1943 at 15:45:32",
u"en_US@rg=CAzzzz", u"Tuesday, March 16, 1943 at 3:45:32 PM",
u"en_CA@rg=USzzzz", u"Tuesday, March 16, 1943 at 3:45:32 p.m.",
u"en_GB@rg=USzzzz", u"Tuesday, 16 March 1943 at 3:45:32 pm",
u"en_GB@rg=CAzzzz", u"Tuesday, 16 March 1943 at 3:45:32 pm",
u"en_GB@rg=AUzzzz", u"Tuesday, 16 March 1943 at 3:45:32 pm",
// test that the hc ("hours") subtag does the right thing
u"en_US@hours=h23", u"Tuesday, March 16, 1943 at 15:45:32",
u"en_GB@hours=h12", u"Tuesday, 16 March 1943 at 3:45:32 pm",
// test that the rg and hc subtags do the right thing when used together
u"en_US@rg=GBzzzz;hours=h12", u"Tuesday, March 16, 1943 at 3:45:32 PM",
u"en_GB@rg=USzzzz;hours=h23", u"Tuesday, 16 March 1943 at 15:45:32",
};
for (int32_t i = 0; i < UPRV_LENGTHOF(testCases); i += 2) {
char errorMessage[200];
char* locale = austrdup(testCases[i]);
UErrorCode err = U_ZERO_ERROR;
sprintf(errorMessage, "Error creating formatter for %s", locale);
if (assertSuccess(errorMessage, &err)) {
UDateFormat* df = udat_open(UDAT_MEDIUM, UDAT_FULL, austrdup(testCases[i]), u"America/Los_Angeles", -1, NULL, 0, &err);
sprintf(errorMessage, "Error formatting value for %s", locale);
if (assertSuccess(errorMessage, &err)) {
UChar result[100];
udat_format(df, date, result, 100, NULL, &err);
sprintf(errorMessage, "Wrong result for %s", locale);
assertUEquals(errorMessage, testCases[i + 1], result);
udat_close(df);
}
}
}
}
static void TestLocaleNameCrash(void) {
UErrorCode status = U_ZERO_ERROR;
UDateFormat icudf;
icudf = udat_open(UDAT_MEDIUM, UDAT_NONE, "notalanguage", NULL, 0, NULL, 0, &status);
if ( U_SUCCESS(status) ) {
log_verbose("Success: did not crash on udat_open(locale=\"notalanguage\")\n");
} else {
log_err("FAIL: didn't crash on udat_open(locale=\"notalanguage\"), but got %s.\n", u_errorName(status));
}
udat_close(icudf);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|