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 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852
|
/* $Id: cpl_test.c,v 1.113 2012/03/13 14:52:02 llundin Exp $
*
* This file is part of the ESO Common Pipeline Library
* Copyright (C) 2001-2008 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author: llundin $
* $Date: 2012/03/13 14:52:02 $
* $Revision: 1.113 $
* $Name: cpl-6_1_1 $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include "cpl_test.h"
#include "cpl_init.h"
#include "cpl_errorstate.h"
#include "cpl_memory.h"
#include "cpl_msg.h"
#include "cpl_tools.h"
#include "cpl_stats.h"
#include "cpl_io_fits.h"
#include "cpl_image_io_impl.h"
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
/* Needed for fabs() */
#include <math.h>
/* Needed for cabs() */
#include <complex.h>
#ifdef HAVE_SYS_TIMES_H
/* Used for times() */
#include <sys/times.h>
#include <time.h>
#include <unistd.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
/* Used for gettimeofday() */
#include <sys/time.h>
#endif
/* Needed for CFITSIO_VERSION */
#include <fitsio.h>
#if defined CPL_FFTW_INSTALLED || defined CPL_FFTWF_INSTALLED
/* Needed for fftw_version */
#include <fftw3.h>
#endif
#if defined CPL_WCS_INSTALLED && CPL_WCS_INSTALLED == 1
/* Used for WCSLIB_VERSION */
#include <wcslib/wcslib.h>
#endif
#ifndef inline
#define inline /* inline */
#endif
/*----------------------------------------------------------------------------*/
/**
* @defgroup cpl_test Unit testing functions
*
* This module provides various functions for unit testing.
*
* @par Synopsis:
* @code
* #include "cpl_test.h"
* @endcode
*/
/*----------------------------------------------------------------------------*/
/**@{*/
/*-----------------------------------------------------------------------------
Private variables
-----------------------------------------------------------------------------*/
static cpl_errorstate cleanstate;
static unsigned cpl_test_count = 0;
static unsigned cpl_test_failures = 0;
static const char * cpl_test_report = NULL;
static double cpl_test_time_start;
static double cpl_test_time_one;
static cpl_flops cpl_test_flops_one = 0;
/*-----------------------------------------------------------------------------
Private function prototypes
-----------------------------------------------------------------------------*/
static void cpl_test_reset(cpl_errorstate);
static void cpl_errorstate_dump_debug(unsigned, unsigned, unsigned);
static void cpl_errorstate_dump_info(unsigned, unsigned, unsigned);
static const char * cpl_test_get_description_a(void) CPL_ATTR_CONST;
static const char * cpl_test_get_description_b(void) CPL_ATTR_CONST;
static char * cpl_test_get_description(void) CPL_ATTR_ALLOC;
static void cpl_test_one(int, double, cpl_flops, cpl_errorstate, cpl_boolean,
const char *, cpl_boolean, const char *,
const char *, unsigned) CPL_ATTR_NONNULL;
static void cpl_test_dump_status(void);
static char * cpl_test_fits_file(const char *, const char *) CPL_ATTR_NONNULL;
/*-----------------------------------------------------------------------------
Function codes
-----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/**
@brief Get the process CPU time, when available (from times())
@return The process CPU time in seconds.
@note Will always return 0 if times() is unavailable
Example of usage:
@code
int my_benchmark (void)
{
double cputime, tstop;
const double tstart = cpl_test_get_cputime();
myfunc();
tstop = cpl_test_get_cputime();
cputime = tstop - tstart;
cpl_msg_info(cpl_func, "The call took %g seconds of CPU-time", cputime);
}
@endcode
*/
/*----------------------------------------------------------------------------*/
double cpl_test_get_cputime(void) {
#if defined HAVE_SYS_TIMES_H && defined HAVE_SYSCONF && defined _SC_CLK_TCK
struct tms buf;
(void)times(&buf);
return (double)buf.tms_utime / (double)sysconf(_SC_CLK_TCK);
#else
return 0.0;
#endif
}
/*----------------------------------------------------------------------------*/
/**
@brief Get the process wall-clock time, when available (from gettimeofday())
@return The process wall-clock time in seconds.
@note Will always return 0 if gettimeofday() is unavailable
Example of usage:
@code
int my_benchmark (void)
{
double walltime, tstop;
const double tstart = cpl_test_get_walltime();
myfunc();
tstop = cpl_test_get_walltime();
walltime = tstop - tstart;
cpl_msg_info(cpl_func, "The call took %g seconds of wall-clock time",
walltime);
}
@endcode
*/
/*----------------------------------------------------------------------------*/
double cpl_test_get_walltime(void) {
#if defined HAVE_SYS_TIME_H
struct timeval buf;
return gettimeofday(&buf, 0) ? 0.0
: (double)buf.tv_sec + 1.0e-6 * (double)buf.tv_usec;
#else
return 0.0;
#endif
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Get the FLOPS count.
@return The FLOPS count
@note This function is intended to be used only by the CPL test macros.
*/
/*----------------------------------------------------------------------------*/
inline cpl_flops cpl_test_get_flops(void)
{
return cpl_tools_get_flops();
}
/*----------------------------------------------------------------------------*/
/**
@brief Get the amount of storage [bytes] for the CPL object
@param self The CPL object
@return The size in bytes
@note Passing NULL is allowed and will return zero
Example of usage:
@code
int my_benchmark (void)
{
const size_t storage = cpl_test_get_bytes_vector(mydata);
double walltime, tstop;
const double tstart = cpl_test_get_walltime();
myfunc(mydata);
tstop = cpl_test_get_walltime();
walltime = tstop - tstart;
if (walltime > 0.0) {
cpl_msg_info(cpl_func, "Processing rate: %g",
(double)storage/walltime);
}
}
@endcode
*/
/*----------------------------------------------------------------------------*/
size_t cpl_test_get_bytes_vector(const cpl_vector * self)
{
return self == NULL ? 0
: (size_t)cpl_vector_get_size(self) * sizeof(double);
}
/*----------------------------------------------------------------------------*/
/**
@brief Get the amount of storage [bytes] for the CPL object
@param self The CPL object
@return The size in bytes
@note Passing NULL is allowed and will return zero
@see cpl_test_get_bytes_vector
*/
/*----------------------------------------------------------------------------*/
size_t cpl_test_get_bytes_matrix(const cpl_matrix * self)
{
return self == NULL ? 0
: (size_t)cpl_matrix_get_nrow(self)
* (size_t)cpl_matrix_get_ncol(self) * sizeof(double);
}
/*----------------------------------------------------------------------------*/
/**
@brief Get the amount of storage [bytes] for the CPL object
@param self The CPL object
@return The size in bytes
@note Passing NULL is allowed and will return zero
@see cpl_test_get_bytes_vector
*/
/*----------------------------------------------------------------------------*/
size_t cpl_test_get_bytes_image(const cpl_image * self)
{
return self == NULL ? 0
: (size_t)cpl_image_get_size_x(self)
* (size_t)cpl_image_get_size_y(self)
* cpl_type_get_sizeof(cpl_image_get_type(self));
}
/*----------------------------------------------------------------------------*/
/**
@brief Get the amount of storage [bytes] for the CPL object
@param self The CPL object
@return The size in bytes
@note Passing NULL is allowed and will return zero
@see cpl_test_get_bytes_vector
*/
/*----------------------------------------------------------------------------*/
size_t cpl_test_get_bytes_imagelist(const cpl_imagelist * self)
{
return self == NULL ? 0
: (size_t)cpl_imagelist_get_size(self)
* (size_t)cpl_test_get_bytes_image(cpl_imagelist_get_const(self, 0));
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Initialize CPL and unit-test environment
@param filename __FILE__ of unit-test source code for log-file
@param report The email address for the error message
@param default_level Default level for messaging system
@return void
@see cpl_test_init()
@note This function should only be called by cpl_test_init()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_init_macro(const char * filename, const char * report,
cpl_msg_severity default_level)
{
const char * dotpos;
/* ISO/IEC 9899:1999 (E) 7.5 3:
The value of errno is zero at program startup, but is never set to
zero by any library function. The value of errno may be set to
nonzero by a library function call whether or not there is an error,
provided the use of errno is not documented in the
description of the function in this International Standard.
*/
/* As such it is safe to read errno at this stage,
but a non-zero value has no significance. */
const int errnopre = errno;
int errnoini;
assert(report != NULL);
assert(filename != NULL);
#ifdef _OPENMP
#pragma omp master
#endif
{
cpl_test_report = report;
errno = 0;
cpl_init(CPL_INIT_DEFAULT);
cpl_test_time_start = cpl_test_get_walltime();
cpl_test_time_one = cpl_test_time_start;
errnoini = errno;
errno = 0;
/* Needed on alphaev56 */
if (signal(SIGFPE, SIG_IGN) == SIG_ERR) {
cpl_msg_warning(cpl_func, "Could not install new signal handler "
"(SIG_IGN) for SIGFPE");
}
cleanstate = cpl_errorstate_get();
cpl_msg_set_level(default_level);
cpl_msg_set_level_from_env();
if (filename != NULL && (dotpos = index(filename, '.')) != NULL &&
strlen(dotpos) == 2) {
/* Create a new string, where the ending .? is replaced with .log */
char * logfile = cpl_sprintf("%sog", filename);
logfile[strlen(logfile)-strlen("log")] = 'l';
cpl_msg_set_log_name(logfile);
if (cpl_error_get_code()) {
/* The log-file name could not be set */
cpl_msg_warning(cpl_func, "Ignoring failed setting of "
"log-file:");
cpl_errorstate_dump(cleanstate, CPL_FALSE,
cpl_errorstate_dump_one_warning);
cpl_test_reset(cleanstate);
}
/* Drop .log */
logfile[strlen(logfile)-strlen(".log")] = '\0';
cpl_msg_set_domain(logfile);
cpl_free(logfile);
}
cpl_msg_set_log_level(CPL_MSG_DEBUG);
if (errnopre != 0) {
/* May be useful for debugging - but see the above errno comment */
/* See also DFS04285 */
cpl_msg_debug(cpl_func, "%s() was called with errno=%d: %s (Unless "
"you are debugging code prior to the cpl_init() call "
"you can ignore this message)",
cpl_func, errnopre, strerror(errnopre));
}
if (errnoini != 0) {
/* May be useful for debugging - but see the above errno comment */
cpl_msg_debug(cpl_func, "cpl_init() set errno=%d: %s (Unless "
"you are debugging cpl_init() you can ignore "
"this message)", errnoini, strerror(errnoini));
}
}
#ifdef _OPENMP
/* No thread can start testing before the master is ready */
#pragma omp barrier
#endif
if (cpl_error_get_code() != CPL_ERROR_NONE) {
cpl_errorstate_dump_one(1, 1, 1); /* Dump the most recent error */
assert(cpl_error_get_code() == CPL_ERROR_NONE);
abort();
}
cpl_msg_debug(cpl_func, "sizeof(cpl_size): %u", (unsigned)sizeof(cpl_size));
#ifdef OFF_T
cpl_msg_debug(cpl_func, "sizeof(OFF_T)=%u", (unsigned)sizeof(OFF_T));
#endif
cpl_msg_debug(cpl_func, "%s", cpl_get_description(CPL_DESCRIPTION_DEFAULT));
if (errno != 0) {
cpl_msg_warning(cpl_func, "%s() set errno=%d: %s", cpl_func, errno,
strerror(errno));
errno = 0;
}
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test a given boolean expression
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param expr The integer expression to evaluate
@param fail_on_zero Fail iff the expression is zero
@param expr_txt The integer expression to evaluate as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test()
@note This function should only be called via cpl_test()
@note CPL_FALSE of the boolean is a failure, CPL_TRUE is not
*/
/*----------------------------------------------------------------------------*/
void cpl_test_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre, cpl_size expression,
cpl_boolean fail_on_zero, const char * expr_txt,
const char * function, const char * file, unsigned line)
{
char * message = cpl_sprintf(fail_on_zero ? "(%s) = %" CPL_SIZE_FORMAT :
"(%s) = %" CPL_SIZE_FORMAT " <=> 0",
expr_txt, expression);
const cpl_boolean bool = fail_on_zero
? (expression ? CPL_TRUE : CPL_FALSE)
: (expression ? CPL_FALSE : CPL_TRUE);
cpl_test_one(errnopre, twallpre, flopspre, statepre, bool, message,
CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if a pointer is NULL
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param pointer The pointer to check, side-effects are allowed
@param pointer_string The pointer as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_null
@note This function should only be called from the macro cpl_test_null()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_null_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const void * pointer, const char * pointer_string,
const char * function, const char * file,
unsigned line)
{
char * message = cpl_sprintf("(%s) = %p == NULL", pointer_string,
pointer);
cpl_test_one(errnopre, twallpre, flopspre, statepre,
pointer == NULL ? CPL_TRUE : CPL_FALSE,
message, CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if a pointer is non-NULL
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param pointer The pointer to check, side-effects are allowed
@param pointer_string The pointer as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_nonnull
@note This function should only be called from the macro cpl_test_nonnull()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_nonnull_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const void * pointer, const char * pointer_string,
const char * function, const char * file,
unsigned line)
{
char * message = cpl_sprintf("(%s) = %p != NULL", pointer_string,
pointer);
cpl_test_one(errnopre, twallpre, flopspre, statepre,
pointer != NULL ? CPL_TRUE : CPL_FALSE,
message, CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two integer expressions are equal
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first value in the comparison
@param first_string The first value as a string
@param second The second value in the comparison
@param second_string The second value as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_eq
@note This function should only be called from the macro cpl_test_eq()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_eq_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
cpl_size first, const char * first_string,
cpl_size second, const char * second_string,
const char * function, const char * file, unsigned line)
{
char * message = cpl_sprintf("(%s) = %" CPL_SIZE_FORMAT "; (%s) = %"
CPL_SIZE_FORMAT, first_string, first,
second_string, second);
cpl_test_one(errnopre, twallpre, flopspre, statepre,
first == second ? CPL_TRUE : CPL_FALSE,
message, CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two integer expressions are not equal
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first value in the comparison
@param first_string The first value as a string
@param second The second value in the comparison
@param second_string The second value as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_noneq
@note This function should only be called from the macro cpl_test_noneq()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_noneq_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
cpl_size first, const char * first_string,
cpl_size second, const char * second_string,
const char * function, const char * file,
unsigned line)
{
char * message = cpl_sprintf("(%s) = %" CPL_SIZE_FORMAT "; (%s) = %"
CPL_SIZE_FORMAT,
first_string, first,
second_string, second);
cpl_test_one(errnopre, twallpre, flopspre, statepre,
first != second ? CPL_TRUE : CPL_FALSE,
message, CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two pointer expressions are equal
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first value in the comparison
@param first_string The first value as a string
@param second The second value in the comparison
@param second_string The second value as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_eq_ptr
@note This function should only be called from the macro cpl_test_eq_ptr()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_eq_ptr_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const void * first, const char * first_string,
const void * second, const char * second_string,
const char * function, const char * file,
unsigned line)
{
char * message = cpl_sprintf("(%s) = %p; (%s) = %p",
first_string, first,
second_string, second);
cpl_test_one(errnopre, twallpre, flopspre, statepre,
first == second ? CPL_TRUE : CPL_FALSE,
message, CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two pointer expressions are not equal
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first value in the comparison
@param first_string The first value as a string
@param second The second value in the comparison
@param second_string The second value as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_noneq_ptr
@note This function should only be called from the macro cpl_test_noneq_ptr()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_noneq_ptr_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const void * first, const char * first_string,
const void * second, const char * second_string,
const char * function, const char * file,
unsigned line)
{
char * message = cpl_sprintf("(%s) = %p; (%s) = %p",
first_string, first,
second_string, second);
cpl_test_one(errnopre, twallpre, flopspre, statepre,
first != second ? CPL_TRUE : CPL_FALSE,
message, CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two strings are equal
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first string or NULL of the comparison
@param first_string The first value as a string
@param second The second string or NULL of the comparison
@param second_string The second value as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_eq_string()
@note This function should only be called from cpl_test_eq_string()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_eq_string_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const char * first, const char * first_string,
const char * second, const char * second_string,
const char * function,
const char * file, unsigned line)
{
char * fsquote = first == NULL ? NULL : cpl_sprintf("'%s'", first);
char * ssquote = second == NULL ? NULL : cpl_sprintf("'%s'", second);
const char * fquote = fsquote == NULL ? "NULL" : fsquote;
const char * squote = ssquote == NULL ? "NULL" : ssquote;
char * message = cpl_sprintf("%s = %s; %s = %s", first_string, fquote,
second_string, squote);
cpl_free(fsquote);
cpl_free(ssquote);
cpl_test_one(errnopre, twallpre, flopspre, statepre,
first != NULL && second != NULL && strcmp(first, second) == 0,
message, CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two strings are not equal
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first string or NULL of the comparison
@param first_string The first value as a string
@param second The second string or NULL of the comparison
@param second_string The second value as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_noneq_string()
@note This function should only be called from cpl_test_noneq_string()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_noneq_string_macro(int errnopre, double twallpre,
cpl_flops flopspre, cpl_errorstate statepre,
const char * first, const char * first_string,
const char * second,
const char * second_string,
const char * function,
const char * file, unsigned line)
{
char * fsquote = first == NULL ? NULL : cpl_sprintf("'%s'", first);
char * ssquote = second == NULL ? NULL : cpl_sprintf("'%s'", second);
const char * fquote = fsquote == NULL ? "NULL" : fsquote;
const char * squote = ssquote == NULL ? "NULL" : ssquote;
char * message = cpl_sprintf("%s = %s; %s = %s", first_string, fquote,
second_string, squote);
cpl_free(fsquote);
cpl_free(ssquote);
cpl_test_one(errnopre, twallpre, flopspre, statepre,
first != NULL && second != NULL && strcmp(first, second) != 0,
message, CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if a file is valid FITS using an external verification utility
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param filename The file to verify
@param filename_string The file to verify as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_fits()
@note This function should only be called from cpl_test_fits()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_fits_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const char * filename, const char * filename_string,
const char * function, const char * file,
unsigned line)
{
const char * checker = getenv(CPL_TEST_FITS);
char * message;
cpl_boolean expression;
if (filename == NULL) {
message = cpl_sprintf(CPL_TEST_FITS " unusable on NULL-file "
"%s", filename_string);
expression = CPL_FALSE; /* Unable to do an actual test */
} else if (checker == NULL) {
message = cpl_test_fits_file(filename, filename_string);
if (message != NULL) {
expression = CPL_FALSE; /* File cannot be FITS */
} else {
/* The previous FITS validation is so primitive that its
success is not reported */
message = cpl_sprintf(CPL_TEST_FITS " undefined for file %s='%s', "
"try: export " CPL_TEST_FITS "=fitsverify",
filename_string, filename);
expression = CPL_TRUE; /* Unable to do an actual test */
}
} else {
const char * redir = cpl_msg_get_level() < CPL_MSG_WARNING ? ""
: (cpl_msg_get_level() < CPL_MSG_ERROR ? " > /dev/null"
: " > /dev/null 2>&1");
char * cmd = cpl_sprintf("%s %s %s", checker, filename, redir);
message = cpl_sprintf(CPL_TEST_FITS " on file %s: %s",
filename_string, cmd);
expression = system(cmd) == 0 ? CPL_TRUE : CPL_FALSE;
cpl_free(cmd);
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two CPL masks are equal
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first mask or NULL of the comparison
@param first_mask The first value as a string
@param second The second mask or NULL of the comparison
@param second_mask The second value as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_eq_mask()
@note This function should only be called from cpl_test_eq_mask()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_eq_mask_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const cpl_mask * first, const char * first_string,
const cpl_mask * second, const char * second_string,
const char * function, const char * file,
unsigned line)
{
cpl_errorstate mystate = cpl_errorstate_get();
const cpl_size nx1 = cpl_mask_get_size_x(first);
const cpl_size ny1 = cpl_mask_get_size_y(first);
const cpl_size nx2 = cpl_mask_get_size_x(second);
const cpl_size ny2 = cpl_mask_get_size_y(second);
cpl_boolean expression;
char * message;
if (!cpl_errorstate_is_equal(mystate)) {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s input error",
first_string, second_string);
} else if (nx1 != nx2 || ny1 != ny2) {
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s incompatible input, nx: %"
CPL_SIZE_FORMAT " <=> %" CPL_SIZE_FORMAT ", ny: %"
CPL_SIZE_FORMAT " <=> %" CPL_SIZE_FORMAT,
first_string, second_string,
nx1, nx2, ny1, ny2);
} else if (memcmp(cpl_mask_get_data_const(first),
cpl_mask_get_data_const(second), (size_t)(nx1 * ny1))) {
/* The test has failed, now spend extra time to report why */
cpl_size i;
cpl_size k = 0;
cpl_size n = 0;
const cpl_binary * pbpm1 = cpl_mask_get_data_const(first);
const cpl_binary * pbpm2 = cpl_mask_get_data_const(second);
for (i = 0; i < nx1 * ny1; i++) {
if (pbpm1[i] != pbpm2[i]) {
k = i;
n++;
}
}
assert( n != 0 );
expression = CPL_FALSE;
message
= cpl_sprintf("%s(%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT
") = %u <=> %u = %s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT ") (%" CPL_SIZE_FORMAT " of %"
CPL_SIZE_FORMAT " x %" CPL_SIZE_FORMAT " "
"differ(s))", first_string, 1+k%nx1, 1+k/nx1,
(unsigned)pbpm1[k], (unsigned)pbpm2[k], second_string,
1+k%nx2, 1+k/nx2, n, nx1, ny1);
} else {
expression = CPL_TRUE;
message = cpl_sprintf("%s == %s", first_string, second_string);
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_errorstate_set(mystate);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Evaluate A <= B and update an internal counter if it is not true
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param value The double-precision number to test
@param tolerance The double-precision upper limit to compare against
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_leq()
@note This function should only be called via cpl_test_leq_macro()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_leq_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
double value, const char * value_string,
double tolerance, const char * tolerance_string,
const char * function, const char * file, unsigned line)
{
const cpl_boolean expression
= (value <= tolerance) ? CPL_TRUE : CPL_FALSE;
char * message = cpl_sprintf("%s = %g <= %g = %s", value_string,
value, tolerance, tolerance_string);
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Evaluate A < B and update an internal counter if it is not true
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param value The double-precision number to test
@param tolerance The double-precision upper limit to compare against
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_lt()
@note This function should only be called via cpl_test_leq_macro()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_lt_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
double value, const char * value_string,
double tolerance, const char * tolerance_string,
const char * function, const char * file, unsigned line)
{
const cpl_boolean expression
= (value < tolerance) ? CPL_TRUE : CPL_FALSE;
char * message = cpl_sprintf("%s = %g < %g = %s", value_string,
value, tolerance, tolerance_string);
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two numerical expressions are
within a given (absolute) tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first value in the comparison
@param first_string The first value as a string
@param second The second value in the comparison
@param second_string The second value as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_abs()
@note This function should only be called from the macro cpl_test_abs()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_abs_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
double first, const char *first_string,
double second, const char *second_string,
double tolerance, const char *tolerance_string,
const char *function, const char *file, unsigned line)
{
const cpl_boolean expression
= (fabs(first - second) <= tolerance) ? CPL_TRUE : CPL_FALSE;
char *message = cpl_sprintf("|%s - %s| = |%g - %g| = |%g| <= %g = %s",
first_string, second_string, first,
second, first - second, tolerance,
tolerance_string);
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two complex expressions are
within a given (absolute) tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first value in the comparison
@param first_string The first value as a string
@param second The second value in the comparison
@param second_string The second value as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_abs_complex()
@note This function should only be called from the macro
cpl_test_abs_complex()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_abs_complex_macro(int errnopre, double twallpre,
cpl_flops flopspre, cpl_errorstate statepre,
double complex first,
const char *first_string,
double complex second,
const char *second_string,
double tolerance,
const char *tolerance_string,
const char *function, const char *file,
unsigned line)
{
const cpl_boolean expression
= (cabs(first - second) <= tolerance) ? CPL_TRUE : CPL_FALSE;
char *message =
cpl_sprintf("|%s - %s| = |(%g%+gi) - (%g%+gi)| = "
"|%g%+gi| = %g <= %g = %s",
first_string, second_string, creal(first),
cimag(first), creal(second), cimag(first),
creal(first - second), cimag(first - second),
cabs(first - second),
tolerance, tolerance_string);
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two numerical expressions are
within a given relative tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first value in the comparison
@param first_string The first value as a string
@param second The second value in the comparison
@param second_string The second value as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_rel()
@note This function should only be called from the macro cpl_test_rel()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_rel_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
double first, const char *first_string,
double second, const char *second_string,
double tolerance, const char *tolerance_string,
const char *function, const char *file, unsigned line)
{
char * message = NULL; /* Avoid (false) uninit warnings */
cpl_boolean expression;
if (tolerance < 0.0) {
expression = CPL_FALSE;
message = cpl_sprintf("%s = %g; %s = %g. Negative tolerance %s = %g",
first_string, first,
second_string, second,
tolerance_string, tolerance);
} else if (first == second) {
/* Not needed. Used only for prettier messaging */
expression = CPL_TRUE;
message = cpl_sprintf("%s = %g = %s. (Tolerance %s = %g)",
first_string, first, second_string,
tolerance_string, tolerance);
} else if (first == 0.0) {
/* Not needed. Used only for prettier messaging */
expression = CPL_FALSE;
message = cpl_sprintf("%s = zero; %s = non-zero (%g). (Tolerance "
"%s = %g)", first_string, second_string,
second, tolerance_string, tolerance);
} else if (second == 0.0) {
/* Not needed. Used only for prettier messaging */
expression = CPL_FALSE;
message = cpl_sprintf("%s = non-zero (%g); %s = zero. (Tolerance "
"%s = %g)", first_string, first, second_string,
tolerance_string, tolerance);
} else if (fabs(first) < fabs(second)) {
expression = fabs(first - second) <= tolerance * fabs(first)
? CPL_TRUE : CPL_FALSE;
message = cpl_sprintf("|%s - %s|/|%s| = |%g - %g|/|%g| = |%g|/|%g|"
" <= %g = %s", first_string, second_string,
first_string, first, second, first,
first - second, first, tolerance,
tolerance_string);
} else {
/* assert(fabs(second) < fabs(first)) */
expression = fabs(first - second) <= tolerance * fabs(second)
? CPL_TRUE : CPL_FALSE;
message = cpl_sprintf("|%s - %s|/|%s| = |%g - %g|/|%g| = |%g|/|%g|"
" <= %g = %s", first_string, second_string,
second_string, first, second, second,
first - second, second, tolerance,
tolerance_string);
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two CPL vectors are identical
within a given (absolute) tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first vector in the comparison
@param first_string The first vector as a string
@param second The second vector of identical size in the comparison
@param second_string The second vector as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_vector_abs()
@note This function should only be called from the macro cpl_test_vector_abs()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_vector_abs_macro(int errnopre, double twallpre,
cpl_flops flopspre, cpl_errorstate statepre,
const cpl_vector * first,
const char *first_string,
const cpl_vector * second,
const char *second_string,
double tolerance, const char *tolerance_string,
const char *function, const char *file,
unsigned line)
{
cpl_errorstate mystate = cpl_errorstate_get();
cpl_vector * diff = cpl_vector_duplicate(first);
cpl_boolean expression;
char * message;
(void)cpl_vector_subtract(diff, second);
if (!cpl_errorstate_is_equal(mystate)) {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s (tol=%s) input error:",
first_string, second_string,
tolerance_string);
} else {
const double * pdiff = cpl_vector_get_data_const(diff);
double difval = pdiff[0];
const cpl_size n = cpl_vector_get_size(diff);
cpl_size pos = 0;
cpl_size i;
for (i = 1; i < n; i++) {
if (fabs(pdiff[i]) > fabs(difval)) {
pos = i;
difval = pdiff[i];
}
}
if (cpl_errorstate_is_equal(mystate)) {
const double val1 = cpl_vector_get(first, pos);
const double val2 = cpl_vector_get(second, pos);
expression = (fabs(difval) <= tolerance) ? CPL_TRUE : CPL_FALSE;
message = cpl_sprintf("|%s(%" CPL_SIZE_FORMAT ") - %s(%"
CPL_SIZE_FORMAT ")| = "
"|%g - %g| = |%g| <= %g = %s",
first_string, pos,
second_string, pos,
val1, val2, difval, tolerance,
tolerance_string);
} else {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s (tol=%s) input error:",
first_string, second_string,
tolerance_string);
}
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_errorstate_set(mystate);
cpl_vector_delete(diff);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two CPL matrices are identical
within a given (absolute) tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first matrix in the comparison
@param first_string The first matrix as a string
@param second The second matrix of identical size in the comparison
@param second_string The second matrix as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_matrix_abs()
@note This function should only be called from the macro cpl_test_matrix_abs()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_matrix_abs_macro(int errnopre, double twallpre,
cpl_flops flopspre, cpl_errorstate statepre,
const cpl_matrix * first,
const char *first_string,
const cpl_matrix * second,
const char *second_string,
double tolerance, const char *tolerance_string,
const char *function, const char *file,
unsigned line)
{
cpl_errorstate mystate = cpl_errorstate_get();
cpl_matrix * diff = cpl_matrix_duplicate(first);
cpl_boolean expression;
char * message;
(void)cpl_matrix_subtract(diff, second);
if (!cpl_errorstate_is_equal(mystate)) {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s (tol=%s) input error:",
first_string, second_string,
tolerance_string);
} else {
const double * pdiff = cpl_matrix_get_data_const(diff);
double difval = pdiff[0];
const cpl_size nrow = cpl_matrix_get_nrow(diff);
const cpl_size n = nrow * cpl_matrix_get_ncol(diff);
cpl_size pos = 0;
cpl_size i;
for (i = 1; i < n; i++) {
if (fabs(pdiff[i]) > fabs(difval)) {
pos = i;
difval = pdiff[i];
}
}
if (cpl_errorstate_is_equal(mystate)) {
const cpl_size irow = pos / nrow;
const cpl_size icol = pos % nrow;
const double val1 = cpl_matrix_get(first, irow, icol);
const double val2 = cpl_matrix_get(second, irow, icol);
expression = (fabs(difval) <= tolerance) ? CPL_TRUE : CPL_FALSE;
message = cpl_sprintf("|%s(%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT
") - %s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT ")| = |%g - %g| = "
"|%g| <= %g = %s",
first_string, irow, icol,
second_string, irow, icol,
val1, val2, difval, tolerance,
tolerance_string);
} else {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s (tol=%s) input error:",
first_string, second_string,
tolerance_string);
}
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_errorstate_set(mystate);
cpl_matrix_delete(diff);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two CPL numerical arrays are identical
within a given (absolute) tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first array in the comparison
@param first_string The first array as a string
@param second The second array of identical size in the comparison
@param second_string The second array as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_array_abs()
@note This function should only be called from the macro cpl_test_array_abs()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_array_abs_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const cpl_array * first,
const char *first_string,
const cpl_array * second,
const char *second_string,
double tolerance, const char *tolerance_string,
const char *function, const char *file,
unsigned line)
{
/* Modified from cpl_test_image_abs_macro() */
cpl_errorstate mystate = cpl_errorstate_get();
const cpl_type type1 = cpl_array_get_type(first);
const cpl_type type2 = cpl_array_get_type(second);
#ifdef CPL_SIZE_FORMAT
const cpl_size nbad1 = cpl_array_count_invalid(first);
const cpl_size nbad2 = cpl_array_count_invalid(second);
const cpl_size nx = cpl_array_get_size(first);
#else
const int nbad1 = cpl_array_count_invalid(first);
const int nbad2 = cpl_array_count_invalid(second);
const int nx = cpl_array_get_size(first);
#endif
cpl_array * diff = cpl_array_duplicate(first);
cpl_boolean expression;
char * message;
(void)cpl_array_subtract(diff, second);
if (!cpl_errorstate_is_equal(mystate)) {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
#ifdef CPL_SIZE_FORMAT
message = cpl_sprintf("%s(%" CPL_SIZE_FORMAT ", %s) <=> %s(%"
CPL_SIZE_FORMAT ", %s) (tol=%s) input error:",
first_string, nx, cpl_type_get_name(type1),
second_string, cpl_array_get_size(second),
cpl_type_get_name(type2), tolerance_string);
#else
message = cpl_sprintf("%s(%d, %s) <=> %s(%d, %s) (tol=%s) input error:",
first_string, nx, cpl_type_get_name(type1),
second_string, cpl_array_get_size(second),
cpl_type_get_name(type2), tolerance_string);
#endif
} else if (nbad1 == nbad2 && nbad1 == nx) {
expression = CPL_TRUE;
#ifdef CPL_SIZE_FORMAT
message = cpl_sprintf("%s(%" CPL_SIZE_FORMAT ", %s) <=> %s(%"
CPL_SIZE_FORMAT ", %s) (tol=%s) All elements "
"are bad",
first_string, nx, cpl_type_get_name(type1),
second_string, cpl_array_get_size(second),
cpl_type_get_name(type2), tolerance_string);
#else
message = cpl_sprintf("%s(%d, %s) <=> %s(%d, %s) (tol=%s) "
"All elements are bad", first_string,
nx, cpl_type_get_name(type1),
second_string, cpl_array_get_size(second),
cpl_type_get_name(type2), tolerance_string);
#endif
} else if (cpl_array_count_invalid(diff) == nx) {
expression = CPL_FALSE;
#ifdef CPL_SIZE_FORMAT
message = cpl_sprintf("%s(%" CPL_SIZE_FORMAT ", %s) <=> %s(%"
CPL_SIZE_FORMAT ", %s) (tol=%s) All elements "
"are bad in the first (%" CPL_SIZE_FORMAT
") or second (%" CPL_SIZE_FORMAT "d) array",
first_string, nx, cpl_type_get_name(type1),
second_string, cpl_array_get_size(second),
cpl_type_get_name(type2), tolerance_string,
nbad1, nbad2);
#else
message = cpl_sprintf("%s(%d, %s) <=> %s(%d, %s) (tol=%s) All elements "
"are bad in the first (%d) or second (%d) array",
first_string, nx, cpl_type_get_name(type1),
second_string, cpl_array_get_size(second),
cpl_type_get_name(type2), tolerance_string,
nbad1, nbad2);
#endif
} else {
const double maxdif = cpl_array_get_max(diff);
const double mindif = cpl_array_get_min(diff);
const cpl_boolean is_pos = (maxdif >= -mindif) ? CPL_TRUE : CPL_FALSE;
const double difval = is_pos ? maxdif : mindif;
#ifdef CPL_SIZE_FORMAT
cpl_size posx;
#else
int posx;
#endif
const cpl_error_code error = (is_pos ? cpl_array_get_maxpos
: cpl_array_get_minpos) (diff, &posx);
int is_bad1;
int is_bad2;
const double val1
= (type1 == CPL_TYPE_INT ? (double)cpl_array_get_int(first, posx,
&is_bad1)
: (type1 == CPL_TYPE_FLOAT
? (double)cpl_array_get_float(first, posx, &is_bad1)
: cpl_array_get_double(first, posx, &is_bad1)));
const double val2
= (type2 == CPL_TYPE_INT
? (double)cpl_array_get_int(second, posx, &is_bad2)
: (type2 == CPL_TYPE_FLOAT
? (double)cpl_array_get_float(second, posx, &is_bad2)
: cpl_array_get_double(second, posx, &is_bad2)));
if (!error && cpl_errorstate_is_equal(mystate)) {
const char * rejstr1 = is_bad1 ? " invalid" : " valid";
const char * rejstr2 = is_bad2 ? " invalid" : " valid";
expression = (fabs(difval) <= tolerance) ? CPL_TRUE : CPL_FALSE;
message = cpl_sprintf("|%s(%" CPL_SIZE_FORMAT ",%s, %s) - %s(%"
CPL_SIZE_FORMAT ",%s, %s)| = "
"|%g - %g| = |%g| <= %g = %s",
first_string, posx, rejstr1,
cpl_type_get_name(type1), second_string,
posx, rejstr2, cpl_type_get_name(type2),
val1, val2, difval, tolerance,
tolerance_string);
} else {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s (tol=%s) input error:",
first_string, second_string,
tolerance_string);
}
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
if (!expression && cpl_errorstate_is_equal(mystate)) {
cpl_msg_warning(cpl_func, "Structure of the compared arrays:");
if (cpl_msg_get_level() < CPL_MSG_ERROR) {
cpl_array_dump_structure(first, stderr);
cpl_array_dump_structure(second, stderr);
}
}
cpl_errorstate_set(mystate);
cpl_array_delete(diff);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two CPL images are identical
within a given (absolute) tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first image in the comparison
@param first_string The first image as a string
@param second The second image of identical size in the comparison
@param second_string The second image as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_image_abs()
@note This function should only be called from the macro cpl_test_image_abs()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_image_abs_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
const cpl_image * first,
const char *first_string,
const cpl_image * second,
const char *second_string,
double tolerance, const char *tolerance_string,
const char *function, const char *file,
unsigned line)
{
cpl_errorstate mystate = cpl_errorstate_get();
const char * stype1 = cpl_type_get_name(cpl_image_get_type(first));
const char * stype2 = cpl_type_get_name(cpl_image_get_type(second));
const cpl_size nbad1 = cpl_image_count_rejected(first);
const cpl_size nbad2 = cpl_image_count_rejected(second);
const cpl_size nx = cpl_image_get_size_x(first);
const cpl_size ny = cpl_image_get_size_y(first);
cpl_image * cdiff = cpl_image_subtract_create(first, second);
cpl_image * diff = (cpl_image_get_type(cdiff) & CPL_TYPE_COMPLEX) ?
cpl_image_extract_mod(cdiff) : cdiff;
cpl_boolean expression;
char * message;
if (!cpl_errorstate_is_equal(mystate)) {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s(%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT
", %s) <=> %s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT ", %s) (tol=%s) input error:",
first_string, nx, ny, stype1, second_string,
cpl_image_get_size_x(second),
cpl_image_get_size_y(second), stype2,
tolerance_string);
} else if (nbad1 == nbad2 && nbad1 == nx * ny) {
expression = CPL_TRUE;
message = cpl_sprintf("%s(%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT
", %s) <=> %s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT ", %s) (tol=%s) All pixels "
"are bad",
first_string, nx, ny, stype1, second_string,
cpl_image_get_size_x(second),
cpl_image_get_size_y(second), stype2,
tolerance_string);
} else if (cpl_image_count_rejected(diff) == nx * ny) {
expression = CPL_FALSE;
message = cpl_sprintf("%s(%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT
", %s) <=> %s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT ", %s) (tol=%s) All pixels "
"are bad in the first (%" CPL_SIZE_FORMAT
") or second (%" CPL_SIZE_FORMAT ") image",
first_string, nx, ny, stype1,
second_string, cpl_image_get_size_x(second),
cpl_image_get_size_y(second), stype2,
tolerance_string, nbad1, nbad2);
} else {
cpl_stats * stats = cpl_stats_new_from_image(diff, CPL_STATS_MIN
| CPL_STATS_MAX
| CPL_STATS_MINPOS
| CPL_STATS_MAXPOS);
const double maxdif = cpl_stats_get_max(stats);
const double mindif = cpl_stats_get_min(stats);
const cpl_boolean is_pos = (maxdif >= -mindif) ? CPL_TRUE : CPL_FALSE;
const double difval = is_pos ? maxdif : mindif;
const cpl_size posx
= is_pos ? cpl_stats_get_max_x(stats) : cpl_stats_get_min_x(stats);
const cpl_size posy
= is_pos ? cpl_stats_get_max_y(stats) : cpl_stats_get_min_y(stats);
int is_bad1;
int is_bad2;
if (cpl_errorstate_is_equal(mystate)) {
expression = (fabs(difval) <= tolerance) ? CPL_TRUE : CPL_FALSE;
if (cpl_image_get_type(cdiff) & CPL_TYPE_COMPLEX) {
const double complex val1 =
cpl_image_get_complex(first, posx, posy, &is_bad1);
const double complex val2 =
(cpl_image_get_type(second) & CPL_TYPE_COMPLEX)
? cpl_image_get_complex(second, posx, posy, &is_bad2)
: cpl_image_get(second, posx, posy, &is_bad2);
const char * rejstr1 = is_bad1 ? " bad" : " not bad";
const char * rejstr2 = is_bad2 ? " bad" : " not bad";
message = cpl_sprintf("|%s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT
",%s, %s) - %s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT ",%s, %s)| = "
"|%g - %g + I (%g - %g) | = |%g| "
"<= %g = %s",
first_string, posx, posy, rejstr1, stype1,
second_string, posx, posy, rejstr2,
stype2, creal(val1), creal(val2),
cimag(val1), cimag(val2), difval,
tolerance, tolerance_string);
} else {
const double val1 = cpl_image_get(first, posx, posy, &is_bad1);
const double val2 = cpl_image_get(second, posx, posy, &is_bad2);
const char * rejstr1 = is_bad1 ? " bad" : " not bad";
const char * rejstr2 = is_bad2 ? " bad" : " not bad";
message = cpl_sprintf("|%s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT
",%s, %s) - %s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT ",%s, %s)| = "
"|%g - %g| = |%g| <= %g = %s",
first_string, posx, posy, rejstr1, stype1,
second_string, posx, posy, rejstr2,
stype2, val1, val2, difval, tolerance,
tolerance_string);
}
if (!expression) cpl_stats_dump(stats, CPL_STATS_MIN
| CPL_STATS_MAX
| CPL_STATS_MINPOS
| CPL_STATS_MAXPOS, stderr);
} else {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s(%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT
", %s) <=> %s(%" CPL_SIZE_FORMAT ",%"
CPL_SIZE_FORMAT ", %s) (tol=%s) input error:",
first_string, nx, ny, stype1,
second_string, cpl_image_get_size_x(second),
cpl_image_get_size_y(second), stype2,
tolerance_string);
}
cpl_stats_delete(stats);
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
if (!expression && cpl_errorstate_is_equal(mystate)) {
cpl_msg_warning(cpl_func, "Structure of the compared images:");
if (cpl_msg_get_level() < CPL_MSG_ERROR) {
cpl_image_dump_structure(first, stderr);
cpl_image_dump_structure(second, stderr);
}
}
cpl_errorstate_set(mystate);
cpl_image_delete(cdiff);
if (diff != cdiff) cpl_image_delete(diff);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two CPL imagelists are identical
within a given (absolute) tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first imagelist in the comparison
@param first_string The first imagelist as a string
@param second The second list of identical size in the comparison
@param second_string The second imagelist as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_imagelist_abs()
@note This function should only be called from cpl_test_imagelist_abs()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_imagelist_abs_macro(int errnopre, double twallpre,
cpl_flops flopspre, cpl_errorstate statepre,
const cpl_imagelist * first,
const char *first_string,
const cpl_imagelist * second,
const char *second_string,
double tolerance,
const char *tolerance_string,
const char *function, const char *file,
unsigned line)
{
cpl_errorstate mystate = cpl_errorstate_get();
const cpl_size sz1 = cpl_imagelist_get_size(first);
const cpl_size sz2 = cpl_imagelist_get_size(second);
cpl_boolean expression;
char * message = NULL;
if (!cpl_errorstate_is_equal(mystate)) {
cpl_error_set(cpl_func, cpl_error_get_code());
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s (tol=%s) input error:",
first_string, second_string,
tolerance_string);
} else if (sz1 != sz2) {
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s (tol=%s) imagelist list sizes differ: "
"%" CPL_SIZE_FORMAT " <=> %" CPL_SIZE_FORMAT,
first_string, second_string,
tolerance_string, sz1, sz2);
} else {
const unsigned failures = cpl_test_failures;
cpl_size i;
message = cpl_sprintf("|%s(%" CPL_SIZE_FORMAT ") - %s(%" CPL_SIZE_FORMAT
")| <= %g = %s", first_string, sz1,
second_string, sz2,
tolerance, tolerance_string);
for (i = 0; i < sz1; i++) {
const cpl_image * img1 = cpl_imagelist_get_const(first, i);
const cpl_image * img2 = cpl_imagelist_get_const(second, i);
char * img1string = cpl_sprintf("image %" CPL_SIZE_FORMAT
" in first list", 1+i);
char * img2string = cpl_sprintf("image %" CPL_SIZE_FORMAT
" in second list", 1+i);
cpl_test_image_abs_macro(errnopre, twallpre, flopspre, statepre,
img1, img1string, img2, img2string,
tolerance, tolerance_string, function,
file, line);
cpl_free(img1string);
cpl_free(img2string);
}
expression = failures == cpl_test_failures ? CPL_TRUE : CPL_FALSE;
cpl_test_failures = failures; /* Count as only one test ! */
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
if (!expression && cpl_errorstate_is_equal(mystate)) {
cpl_msg_warning(cpl_func, "Structure of the compared imagelists:");
if (cpl_msg_get_level() < CPL_MSG_ERROR) {
cpl_imagelist_dump_structure(first, stderr);
cpl_imagelist_dump_structure(second, stderr);
}
}
cpl_errorstate_set(mystate);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two CPL polynomials are identical
within a given (absolute) tolerance
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first polynomial in the comparison
@param first_string The first polynomial as a string
@param second The second polynomial in the comparison
@param second_string The second polynomial as a string
@param tolerance A non-negative tolerance
@param tolerance_string The tolerance as a string
@param function function name
@param file filename
@param line line number
@see cpl_test_polynomial_abs()
@note This function should only be called from the macro
cpl_test_polynomial_abs()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_polynomial_abs_macro(int errnopre, double twallpre,
cpl_flops flopspre, cpl_errorstate statepre,
const cpl_polynomial * first,
const char *first_string,
const cpl_polynomial * second,
const char *second_string,
double tolerance,
const char *tolerance_string,
const char *function, const char *file,
unsigned line)
{
cpl_errorstate mystate = cpl_errorstate_get();
cpl_boolean expression = cpl_polynomial_compare(first, second, tolerance)
? CPL_FALSE : CPL_TRUE;
char * message;
if (cpl_errorstate_is_equal(mystate)) {
cpl_error_set(cpl_func, cpl_error_get_code());
message = cpl_sprintf("|%s - %s| <= %g = %s",
first_string, second_string,
tolerance, tolerance_string);
} else {
expression = CPL_FALSE;
message = cpl_sprintf("%s <=> %s (tol=%s) input error:",
first_string, second_string,
tolerance_string);
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, expression, message,
CPL_FALSE, function, file, line);
cpl_errorstate_set(mystate);
cpl_free(message);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test and reset the CPL error code
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param errorstate The expected CPL error code (incl. CPL_ERROR_NONE)
@param errorstate_string The CPL error code as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_errorstate
@note This function should only be called from the macro
cpl_test_errorstate()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_errorstate_macro(int errnopre, double twallpre,
cpl_flops flopspre, cpl_errorstate statepre,
cpl_errorstate errorstate,
const char * errorstate_string,
const char * function, const char * file,
unsigned line)
{
/* FIXME: Improve message */
char * message = cpl_sprintf("%s <=> %d (%s)",
errorstate_string,
cpl_error_get_code(),
cpl_error_get_message());
cpl_test_one(errnopre, twallpre, flopspre, statepre,
cpl_errorstate_is_equal(errorstate) ? CPL_TRUE : CPL_FALSE,
message, CPL_TRUE, function, file, line);
cpl_free(message);
cpl_test_reset(errorstate);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test and reset the CPL error code
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param error The expected CPL error code (incl. CPL_ERROR_NONE)
@param error_string The CPL error code as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_error
@note This function should only be called from the macro cpl_test_error()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_error_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
cpl_error_code error, const char * error_string,
const char * function, const char * file,
unsigned line)
{
char * message = cpl_sprintf("(%s) = %d (%s) <=> %d (%s)",
error_string, error,
cpl_error_get_message_default(error),
cpl_error_get_code(),
cpl_error_get_message());
cpl_test_one(errnopre, twallpre, flopspre, statepre,
cpl_error_get_code() == error ? CPL_TRUE : CPL_FALSE,
message, CPL_TRUE, function, file, line);
cpl_free(message);
cpl_test_reset(cleanstate);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if two CPL error expressions are equal, also to the CPL error code
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param first The first value in the comparison
@param first_string The first value as a string
@param second The second value in the comparison
@param second_string The second value as a string
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_error
@note This function should only be called from the macro cpl_test_eq_error()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_eq_error_macro(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre,
cpl_error_code first, const char * first_string,
cpl_error_code second, const char * second_string,
const char * function, const char * file,
unsigned line)
{
char * message = cpl_sprintf("(%s) = %d (%s) <=> (%s) = %d (%s) "
"<=> %d (%s)", first_string, first,
cpl_error_get_message_default(first),
second_string, second,
cpl_error_get_message_default(second),
cpl_error_get_code(),
cpl_error_get_message_default
(cpl_error_get_code()));
cpl_test_one(errnopre, twallpre, flopspre, statepre,
(first == second && cpl_error_get_code() == first)
? CPL_TRUE : CPL_FALSE,
message, CPL_TRUE, function, file, line);
cpl_free(message);
cpl_test_reset(cleanstate);
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test if the memory system is empty
@param errnopre errno prior to expression evaluation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param function cpl_func
@param file __FILE__
@param line __LINE__
@see cpl_test_memory_is_empty
@note This function should only be called from the macro
cpl_test_memory_is_empty()
*/
/*----------------------------------------------------------------------------*/
void cpl_test_memory_is_empty_macro(int errnopre, double twallpre,
cpl_flops flopspre, cpl_errorstate statepre,
const char * function, const char * file,
unsigned line)
{
const char * message;
cpl_boolean ok;
if (cpl_memory_is_empty() == -1) {
message = "CPL memory system is empty (not testable)";
ok = CPL_TRUE;
} else {
message = "CPL memory system is empty";
ok = cpl_memory_is_empty() == 0 ? CPL_FALSE : CPL_TRUE;
}
cpl_test_one(errnopre, twallpre, flopspre, statepre, ok, message, CPL_FALSE,
function, file, line);
if (!ok) {
cpl_msg_indent_more();
cpl_memory_dump();
cpl_msg_indent_less();
}
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Finalize CPL and unit-testing environment and report any failures
@param nfail The number of failures counted apart from cpl_test() et al.
@return @em EXIT_SUCCESS iff the CPL errorstate is clean
@note This function should be used for the final return from a unit test
@see cpl_test_init()
nfail should normally be zero, but may be set to a positive number when it
is necessary to ensure a failure.
nfail should only be negative in the unit test of the unit-test functions
themselves.
Example of usage:
@code
int main (void)
{
cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
cpl_test(myfunc(&p));
cpl_test(p != NULL);
return cpl_test_end(0);
}
@endcode
*/
/*----------------------------------------------------------------------------*/
int cpl_test_end(cpl_size nfail)
{
const int errnopre = errno;
const cpl_flops nflops = cpl_tools_get_flops();
cpl_boolean ok = CPL_TRUE;
const cpl_size mfail = nfail + (cpl_size)cpl_test_failures;
const double cpl_test_elap = cpl_test_get_walltime() - cpl_test_time_start;
#if defined HAVE_SYS_TIMES_H && defined _SC_CLK_TCK && defined HAVE_SYSCONF
struct tms buf;
const clock_t clocks = times(&buf);
const double cputime =(double)buf.tms_utime
/ (double)sysconf(_SC_CLK_TCK);
const double systime =(double)buf.tms_stime
/ (double)sysconf(_SC_CLK_TCK);
const double chcputime =(double)buf.tms_cutime
/ (double)sysconf(_SC_CLK_TCK);
const double chsystime =(double)buf.tms_cstime
/ (double)sysconf(_SC_CLK_TCK);
errno = 0;
cpl_msg_debug(cpl_func, "Sizeof(clock_t): %u", (unsigned)sizeof(clocks));
cpl_msg_debug(cpl_func, "sysconf(_SC_CLK_TCK): %u",
(unsigned)sysconf(_SC_CLK_TCK));
cpl_msg_info(cpl_func, "User time to test [s]: %g", cputime);
cpl_msg_info(cpl_func, "System time to test [s]: %g", systime);
cpl_msg_debug(cpl_func, "Child user time to test [s]: %g", chcputime);
cpl_msg_debug(cpl_func, "Child system time to test [s]: %g", chsystime);
#else
errno = 0;
#endif
/* Need to close files here, to deallocate */
cpl_test_zero(cpl_io_fits_end());
if (cpl_test_elap > 0.0) {
cpl_msg_info(cpl_func, "Actual time to test [s]: %g",
cpl_test_elap);
cpl_msg_info(cpl_func, "The computational speed during this test "
"[MFLOP/s]: %g", 1e-6*(double)nflops/cpl_test_elap);
} else {
cpl_msg_info(cpl_func, "Number of MFLOPs in this test: %g",
1e-6*(double)nflops);
}
if (errnopre != 0) {
cpl_msg_warning(cpl_func, "%s() was called with errno=%d: %s",
cpl_func, errnopre, strerror(errnopre));
}
/* Make sure that the failure is written */
if (cpl_msg_get_level() == CPL_MSG_OFF) cpl_msg_set_level(CPL_MSG_ERROR);
if (cpl_error_get_code() != CPL_ERROR_NONE) {
ok = CPL_FALSE;
cpl_msg_error(cpl_func, "The CPL errorstate was set by the unit "
"test(s)");
cpl_msg_indent_more();
cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
cpl_msg_indent_less();
}
if (mfail > 0) {
ok = CPL_FALSE;
cpl_msg_error(cpl_func, "%" CPL_SIZE_FORMAT " of %u test(s) failed",
mfail, cpl_test_count);
} else if (mfail < 0) {
ok = CPL_FALSE;
/* This special case is only foreseen to be reached by
the unit test of the CPL unit test module */
cpl_msg_error(cpl_func, "%u of %u test(s) failed, %" CPL_SIZE_FORMAT
" less than the expected %" CPL_SIZE_FORMAT
" failure(s)", cpl_test_failures, cpl_test_count,
-mfail, -nfail);
} else {
cpl_msg_info(cpl_func, "All %u test(s) succeeded", cpl_test_count);
}
if (!cpl_memory_is_empty()) {
ok = CPL_FALSE;
cpl_msg_error(cpl_func, "Memory leak detected:");
cpl_msg_indent_more();
cpl_memory_dump();
cpl_msg_indent_less();
} else if (cpl_msg_get_level() <= CPL_MSG_DEBUG) {
cpl_memory_dump();
}
if (!ok) {
char * desc = cpl_test_get_description();
cpl_msg_error(cpl_func, "This failure may indicate a bug in the tested "
"code");
cpl_msg_error(cpl_func, "You can contribute to the improvement of the "
"software by emailing the logfile '%s' and the configure "
"logfile 'config.log' to %s", cpl_msg_get_log_name(),
cpl_test_report ? cpl_test_report : PACKAGE_BUGREPORT);
cpl_msg_error(cpl_func, "System specifics:\n%s", desc);
cpl_free(desc);
}
cpl_test_dump_status();
if (errno != 0) {
cpl_msg_warning(cpl_func, "%s() set errno=%d: %s", cpl_func, errno,
strerror(errno));
errno = 0;
}
cpl_end();
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
/**@}*/
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Dump the CPL errorstate and reset it
@param self The errorstate to reset to
@return void
*/
/*----------------------------------------------------------------------------*/
static void cpl_test_reset(cpl_errorstate self)
{
if (!cpl_errorstate_is_equal(self)) {
cpl_errorstate_dump(self, CPL_FALSE, cpl_errorstate_dump_debug);
cpl_errorstate_set(self);
}
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Test an expression and update an internal counter if it fails
@param errnopre errno prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param flopspre FLOP count prior to expression evaluation
@param twallpre Wall clock time prior to expression calculation
@param statepre CPL errorstate prior to expression evaluation
@param expression The expression to test (CPL_FALSE means failure)
@param message The text message associated with the expression
@param function function name
@param file filename
@param line line number
The CPL FLOP count, the CPL errorstate and errno may have changed prior to
or during to the expression evaluation.
Success or failure,
any CPL errorstate change prior to expression evaluation,
any CPL errorstate change during expression evaluation,
any errno change prior to expression evaluation,
any errno change during expression evaluation.
*/
/*----------------------------------------------------------------------------*/
static void cpl_test_one(int errnopre, double twallpre, cpl_flops flopspre,
cpl_errorstate statepre, cpl_boolean expression,
const char *message, cpl_boolean expect_error,
const char *function, const char *file, unsigned line)
{
const int myerrno = errno; /* Local copy, in case errno changes in here */
char * errnopre_txt = errnopre == 0 ? NULL :
cpl_sprintf(" Prior to this test errno=%d: %s.", errnopre,
strerror(errnopre));
char * myerrno_txt = myerrno == errnopre ? NULL :
cpl_sprintf(" This test set errno=%d: %s.", myerrno,
strerror(myerrno));
const char * errnopre_msg = errnopre_txt == NULL ? "" : errnopre_txt;
const char * myerrno_msg = myerrno_txt == NULL ? "" : myerrno_txt;
const char * error_msg = cpl_errorstate_is_equal(cleanstate) ? "" :
(expect_error ?
(cpl_errorstate_is_equal(statepre) ? ""
: "CPL error(s) set during this test.") :
(cpl_errorstate_is_equal(statepre) ?
" CPL error(s) set prior to this test." :
(statepre == cleanstate ? " CPL error(s) set during this test." :
" CPL error(s) set prior to and during this test.")));
char * flopprev_txt = NULL;
char * floptest_txt = NULL;
assert(message != NULL);
assert(function != NULL);
assert(file != NULL);
errno = 0;
#ifdef _OPENMP
#pragma omp atomic
#endif
cpl_test_count++;
#ifdef _OPENMP
#pragma omp master
#endif
{
/* If two cpl_tests would be permitted to enter concurrently here,
then the reported FLOP rate would be meaningless */
const double cpl_test_time_now = cpl_test_get_walltime();
const double cpl_test_time_prev = cpl_test_time_one;
const cpl_flops cpl_test_flops_now = cpl_tools_get_flops();
const cpl_flops cpl_test_flops_prev = cpl_test_flops_one;
cpl_test_time_one = cpl_test_time_now;
cpl_test_flops_one = cpl_test_flops_now;
if (flopspre > cpl_test_flops_prev) {
const double cpl_test_time_between = twallpre - cpl_test_time_prev;
const cpl_flops cpl_test_flops_between = flopspre
- cpl_test_flops_prev;
if (cpl_test_time_between > 0.0) {
flopprev_txt = cpl_sprintf(" (%g FLOPs after the previous "
"test and prior to this one at "
"[MFLOP/s]: %g).",
(double)cpl_test_flops_between,
1e-6*(double)cpl_test_flops_between
/cpl_test_time_between);
} else {
flopprev_txt = cpl_sprintf(" (%g FLOPs after the previous test "
"and prior to this one).",
(double)cpl_test_flops_between);
}
}
if (cpl_test_flops_now > flopspre) {
const double cpl_test_time_during = cpl_test_time_now - twallpre;
const cpl_flops cpl_test_flops_during = cpl_test_flops_now
- flopspre;
if (cpl_test_time_during > 0.0) {
floptest_txt = cpl_sprintf(" (%g FLOPs during this test at "
"[MFLOP/s]: %g).",
(double)cpl_test_flops_during,
1e-6*(double)cpl_test_flops_during
/cpl_test_time_during);
} else {
floptest_txt = cpl_sprintf(" (%g FLOPs during this test).",
(double)cpl_test_flops_during);
}
}
}
if (flopprev_txt == NULL) flopprev_txt = cpl_strdup("");
if (floptest_txt == NULL) floptest_txt = cpl_strdup("");
if (expression) {
cpl_boolean has_error = error_msg[0] ? CPL_TRUE : CPL_FALSE;
(has_error ? cpl_msg_info : cpl_msg_debug)
(function, "Test %u OK at %s:%u: %s.%s%s%s%s%s",
cpl_test_count, file, line, message,
error_msg, errnopre_msg, myerrno_msg, flopprev_txt, floptest_txt);
cpl_errorstate_dump(cleanstate, CPL_FALSE, has_error
? cpl_errorstate_dump_info
: cpl_errorstate_dump_debug);
} else {
cpl_msg_error(function, "Test %u failed at %s:%u: %s.%s%s%s%s%s",
cpl_test_count, file, line, message, error_msg,
errnopre_msg, myerrno_msg, flopprev_txt, floptest_txt);
cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
#ifdef _OPENMP
#pragma omp atomic
#endif
cpl_test_failures++;
}
cpl_free(errnopre_txt);
cpl_free(myerrno_txt);
cpl_free(flopprev_txt);
cpl_free(floptest_txt);
if (errno != 0) {
cpl_msg_debug(cpl_func, "%s() set errno=%d: %s", cpl_func, errno,
strerror(errno));
errno = 0;
}
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Dump a single CPL error at debug messaging level
@param self The number of the current error to be dumped
@param first The number of the first error to be dumped
@param last The number of the last error to be dumped
@return void
@see cpl_errorstate_dump_one
*/
/*----------------------------------------------------------------------------*/
static void cpl_errorstate_dump_debug(unsigned self, unsigned first,
unsigned last)
{
const cpl_boolean is_reverse = first > last ? CPL_TRUE : CPL_FALSE;
const unsigned newest = is_reverse ? first : last;
const unsigned oldest = is_reverse ? last : first;
const char * revmsg = is_reverse ? " in reverse order" : "";
assert( oldest <= self );
assert( newest >= self );
if (newest == 0) {
cpl_msg_debug(cpl_func, "No error(s) to dump");
assert( oldest == 0);
} else {
assert( oldest > 0);
assert( newest >= oldest);
if (self == first) {
if (oldest == 1) {
cpl_msg_debug(cpl_func, "Dumping all %u error(s)%s:", newest,
revmsg);
} else {
cpl_msg_debug(cpl_func, "Dumping the %u most recent error(s) "
"out of a total of %u errors%s:",
newest - oldest + 1, newest, revmsg);
}
cpl_msg_indent_more();
}
cpl_msg_debug(cpl_func, "[%u/%u] '%s' (%u) at %s", self, newest,
cpl_error_get_message(), cpl_error_get_code(),
cpl_error_get_where());
if (self == last) cpl_msg_indent_less();
}
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Dump a single CPL error at debug messaging level
@param self The number of the current error to be dumped
@param first The number of the first error to be dumped
@param last The number of the last error to be dumped
@return void
@see cpl_errorstate_dump_debug
*/
/*----------------------------------------------------------------------------*/
static void cpl_errorstate_dump_info(unsigned self, unsigned first,
unsigned last)
{
const cpl_boolean is_reverse = first > last ? CPL_TRUE : CPL_FALSE;
const unsigned newest = is_reverse ? first : last;
const unsigned oldest = is_reverse ? last : first;
const char * revmsg = is_reverse ? " in reverse order" : "";
assert( oldest <= self );
assert( newest >= self );
if (newest == 0) {
cpl_msg_info(cpl_func, "No error(s) to dump");
assert( oldest == 0);
} else {
assert( oldest > 0);
assert( newest >= oldest);
if (self == first) {
if (oldest == 1) {
cpl_msg_info(cpl_func, "Dumping all %u error(s)%s:", newest,
revmsg);
} else {
cpl_msg_info(cpl_func, "Dumping the %u most recent error(s) "
"out of a total of %u errors%s:",
newest - oldest + 1, newest, revmsg);
}
cpl_msg_indent_more();
}
cpl_msg_info(cpl_func, "[%u/%u] '%s' (%u) at %s", self, newest,
cpl_error_get_message(), cpl_error_get_code(),
cpl_error_get_where());
if (self == last) cpl_msg_indent_less();
}
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief First half of a string useful in error-reporting
@return Pointer to a string literal useful in error-reporting
*/
/*----------------------------------------------------------------------------*/
static const char * cpl_test_get_description_a(void)
{
return "CPL version: " PACKAGE_VERSION
#if defined CPL_SIZE_BITS && CPL_SIZE_BITS == 32
" (32-bit cpl_size)"
#else
" (64-bit cpl_size)"
#endif
"\n"
#ifdef CFITSIO_VERSION
"CFITSIO version: " CPL_STRINGIFY(CFITSIO_VERSION) "\n"
#elif defined _FITSIO_H
"CFITSIO version is less than 3.0\n"
#endif
#if defined WCSLIB_VERSION
"WCSLIB version: " CPL_STRINGIFY(WCSLIB_VERSION) "\n"
#elif defined CPL_WCS_INSTALLED && CPL_WCS_INSTALLED == 1
"WCSLIB installation is detected\n"
#else
"WCSLIB installation is not detected\n"
#endif
;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Second half of a string useful in error-reporting
@return Pointer to a string literal useful in error-reporting
*/
/*----------------------------------------------------------------------------*/
static const char * cpl_test_get_description_b(void)
{
return
#ifdef _OPENMP
CPL_XSTRINGIFY(_OPENMP) ": " CPL_STRINGIFY(_OPENMP) "\n"
#endif
#ifdef OFF_T
"OFF_T is defined as " CPL_STRINGIFY(OFF_T) "\n"
#else
"OFF_T is not defined\n"
#endif
#if defined WORDS_BIGENDIAN && WORDS_BIGENDIAN == 1
"This platform is big-endian\n"
#else
"This platform is not big-endian\n"
#endif
#ifdef __DATE__
"Compile date: " __DATE__ "\n"
#endif
#ifdef __TIME__
"Compile time: " __TIME__ "\n"
#endif
#ifdef __STDC__
CPL_XSTRINGIFY(__STDC__) ": " CPL_STRINGIFY(__STDC__) "\n"
#endif
#ifdef __STDC_VERSION__
CPL_XSTRINGIFY(__STDC_VERSION__) ": "
CPL_STRINGIFY(__STDC_VERSION__) "\n"
#endif
#ifdef __STDC_HOSTED__
CPL_XSTRINGIFY(__STDC_HOSTED__) ": " CPL_STRINGIFY(__STDC_HOSTED__) "\n"
#endif
#ifdef __STDC_IEC_559__
CPL_XSTRINGIFY(__STDC_IEC_559__) ": "
CPL_STRINGIFY(__STDC_IEC_559__) "\n"
#endif
#ifdef __STDC_IEC_559_COMPLEX__
CPL_XSTRINGIFY(__STDC_IEC_559_COMPLEX__) ": "
CPL_STRINGIFY(__STDC_IEC_559_COMPLEX__) "\n"
#endif
#ifdef __STRICT_ANSI__
/* gcc and Sun Studio 12.1 supports this */
CPL_XSTRINGIFY(__STRICT_ANSI__) ": " CPL_STRINGIFY(__STRICT_ANSI__) "\n"
#endif
#ifdef __GNUC__
"gcc version (major number): " CPL_STRINGIFY(__GNUC__) "\n"
#ifdef __GNUC_MINOR__
"gcc version (minor number): " CPL_STRINGIFY(__GNUC_MINOR__) "\n"
#endif
#ifdef __GNUC_PATCHLEVEL__
"gcc version (patch level): " CPL_STRINGIFY(__GNUC_PATCHLEVEL__) "\n"
#endif
#ifdef __VERSION__
"Compiler version: " __VERSION__ "\n"
#endif
#ifdef __LP64__
CPL_XSTRINGIFY(__LP64__) ": " CPL_STRINGIFY(__LP64__) "\n"
#endif
#ifdef __PIC__
CPL_XSTRINGIFY(__PIC__) ": " CPL_STRINGIFY(__PIC__) "\n"
#endif
#ifdef __OPTIMIZE__
CPL_XSTRINGIFY(__OPTIMIZE__) ": " CPL_STRINGIFY(__OPTIMIZE__) "\n"
#endif
#ifdef __TIMESTAMP__
"Last modification of " __FILE__ ": " __TIMESTAMP__ "\n"
#endif
#endif
;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief A string useful in error-reporting
@return Pointer to a string literal useful in error-reporting
*/
/*----------------------------------------------------------------------------*/
static char * cpl_test_get_description(void)
{
return cpl_sprintf("%s"
#if defined CPL_FFTW_INSTALLED && defined CPL_FFTWF_INSTALLED
"FFTW version (single and double precision): %s\n%s",
cpl_test_get_description_a(),
fftw_version,
#elif defined CPL_FFTW_INSTALLED
"FFTW version (double precision only): %s\n%s",
cpl_test_get_description_a(),
fftw_version,
#elif defined CPL_FFTWF_INSTALLED
"FFTW version (single precision only): %s\n%s",
cpl_test_get_description_a(),
fftwf_version,
#else
"FFTW installation is not detected\n%s",
cpl_test_get_description_a(),
#endif
cpl_test_get_description_b());
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Dump various process information
*/
/*----------------------------------------------------------------------------*/
static void cpl_test_dump_status(void)
{
#if defined HAVE_GETPID && defined CPL_TEST_DUMP_STATUS
const pid_t pid = getpid();
char * file = cpl_sprintf("/proc/%u/status", (const unsigned)pid);
FILE * stream = fopen(file, "r");
if (stream != NULL) {
char line[CPL_MAX_MSG_LENGTH];
while (fgets(line, CPL_MAX_MSG_LENGTH, stream) != NULL) {
/* Ignore newline */
char * retpos = memchr(line, '\n', CPL_MAX_MSG_LENGTH);
if (retpos != NULL) *retpos = 0;
cpl_msg_debug(cpl_func, "%s", line);
}
fclose(stream);
}
cpl_free(file);
#endif
return;
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Some simple FITS validation
@param filename The filename
@param filename_string The filename as a string
@note No input validation !
*/
/*----------------------------------------------------------------------------*/
static char * cpl_test_fits_file(const char * filename,
const char * filename_string)
{
char * self = NULL;
#ifdef HAVE_SYS_STAT_H
struct stat buf;
const int error = stat(filename, &buf);
if (error) {
self = cpl_sprintf("%s => %s stat() returned %d: %s",
filename_string, filename, error, strerror(errno));
} else {
const off_t size = buf.st_size;
if (size == 0) {
self = cpl_sprintf("%s => %s has zero size", filename_string,
filename);
} else {
const off_t rem = size % 2880;
if (rem != 0) {
self = cpl_sprintf("%s => %s has illegal size=%luB with %uB "
"in excess of the 2880B-blocks",
filename_string, filename,
(long unsigned)size, (unsigned)rem);
}
}
}
#endif
return self;
}
|