test_GSky.cpp 96.1 KB
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
/***************************************************************************
 *                  test_GSky.cpp - Test sky module                        *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2010-2021 by Juergen Knoedlseder                         *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *  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 3 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, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
 ***************************************************************************/
/**
 * @file test_GSky.cpp
 * @brief Testing of sky module.
 * @author Juergen Knoedlseder
 */

/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>                           // cout, cerr
#include <stdexcept>                          // std::exception
#include <cstdlib>                            // for system
#include "test_GSky.hpp"
#include "GTools.hpp"


/* __ Namespaces _________________________________________________________ */

/* __ Globals ____________________________________________________________ */

/* __ Debug definitions __________________________________________________ */
#define G_WCS_FORTH_BACK_PIXEL_DEBUG
#define G_WCS_COPY_DEBUG

/* __ Constants __________________________________________________________ */
const std::string datadir         = std::getenv("TEST_DATA");
const std::string sky_region_circ = datadir + "/test_circle_region.reg";
const std::string sky_region_rect = datadir + "/test_rect_region.reg";
const std::string sky_region_map  = datadir + "/test_map_region.fits";
const std::string sky_map         = datadir + "/cena_lobes_parkes.fits";


/***********************************************************************//**
 * @brief Set parameters and tests
 ***************************************************************************/
void TestGSky::set(void){

    // Test name
    name("GSky");

    // Append tests
    append(static_cast<pfunction>(&TestGSky::test_GWcs),
           "Test GWcs");
    append(static_cast<pfunction>(&TestGSky::test_GSkyDir),
           "Test GSkyDir");
    append(static_cast<pfunction>(&TestGSky::test_GSkyDirs),
           "Test GSkyDirs");
    append(static_cast<pfunction>(&TestGSky::test_GSkyPixel),
           "Test GSkyPixel");
    append(static_cast<pfunction>(&TestGSky::test_GSkyMap_healpix_construct),
           "Test Healpix GSkyMap constructors");
    append(static_cast<pfunction>(&TestGSky::test_GSkyMap_healpix_io),
           "Test Healpix GSkyMap I/O");
    append(static_cast<pfunction>(&TestGSky::test_GSkyMap_wcs_construct),
           "Test WCS GSkyMap constructors");
    append(static_cast<pfunction>(&TestGSky::test_GSkyMap_wcs_io),
           "Test WCS GSkyMap I/O");
    append(static_cast<pfunction>(&TestGSky::test_GSkyMap),
           "Test GSkyMap");
    append(static_cast<pfunction>(&TestGSky::test_GSkyMap_io),
           "Test GSkyMap I/O");
    append(static_cast<pfunction>(&TestGSky::test_GSkyRegions),
           "Test GSkyRegions");
    append(static_cast<pfunction>(&TestGSky::test_GSkyRegionCircle),
           "Test GSkyRegionCircle");
    append(static_cast<pfunction>(&TestGSky::test_GSkyRegionRectangle),
           "Test GSkyRegionRectangle");
    append(static_cast<pfunction>(&TestGSky::test_GSkyRegionMap),
           "Test GSkyRegionMap");
    append(static_cast<pfunction>(&TestGSky::test_GHorizDir),
           "Test GHorizDir");

    // Return
    return;
}


/***********************************************************************//**
 * @brief Clone test suite
 *
 * @return Pointer to deep copy of test suite.
 ***************************************************************************/
TestGSky* TestGSky::clone(void) const
{
    // Clone test suite
    return new TestGSky(*this);
}


/***********************************************************************//**
 * @brief Test consistency of forward and background transformations
 *
 * @param[in] wcs WCS object
 * @param[in] nx Number of points in X
 * @param[in] ny Number of points in Y
 * @param[in] crpix1 Reference pixel in X
 * @param[in] crpix2 Reference pixel in Y
 ***************************************************************************/
double TestGSky::wcs_forth_back_pixel(GWcs*   wcs,
                                      int     nx,
                                      int     ny,
                                      double& crpix1,
                                      double& crpix2)
{
    // Initialise maximal distance
    double dist_max = 0.0;

    // Loop over x pixels
    for (int ix = -nx; ix <= nx; ++ix) {

        // Set x value
        double x = double(ix) + crpix1;

        // Skip pixels outside valid range (they are not expected to
        // transform bijectively)
        if (x < 0 || x >= nx) {
            continue;
        }

        // Loop over y pixels
        for (int iy = -ny; iy <= ny; ++iy) {

            // Set y value
            double y = double(iy) + crpix2;

            // Skip pixels outside valid range (they are not expected to
            // transform bijectively)
            if (y < 0 || y >= ny) {
                continue;
            }

            // Set sky pixel
            GSkyPixel pix_in(x,y);

            // Forth: Transform to world
            GSkyDir dir = wcs->pix2dir(pix_in);

            // Back: Transform to pixel
            GSkyPixel pix_out = wcs->dir2pix(dir);

            // Compute distance
            double dx      = pix_in.x()-pix_out.x();
            double dy      = pix_in.y()-pix_out.y();
            double dist    = sqrt(dx*dx+dy*dy);

            // Debug option: Dump discrepancies
            #if defined(G_WCS_FORTH_BACK_PIXEL_DEBUG)
            if (dist > 0.001) {
                std::cout << std::endl;
                std::cout << wcs->code() << ":";
                std::cout << " dist=" << dist;
                std::cout << " dx=" << dx;
                std::cout << " dy=" << dy;
                std::cout << " pix_in=" << pix_in;
                std::cout << " pix_out=" << pix_out;
                std::cout << " (x,y)=(" << x << "," << y << ")";
                std::cout << " dir=" << dir;
            }
            #endif

            // Store maximum distance
            if (dist > dist_max) {
                dist_max = dist;
            }

        } // endfor: y pixels
    } // endfor: x pixels

    // Return
    return dist_max;
}


/***********************************************************************//**
 * @brief Test consistency of copy
 *
 * @param[in] wcs WCS object
 * @param[in] nx Number of points in X
 * @param[in] ny Number of points in Y
 * @param[in] crpix1 Reference pixel in X
 * @param[in] crpix2 Reference pixel in Y
 ***************************************************************************/
double TestGSky::wcs_copy(GWcs*   wcs,
                          int     nx,
                          int     ny,
                          double& crpix1,
                          double& crpix2)
{
    // Make a copy using clone
    GWcs* cpy = wcs->clone();

    // Initialise maximal angle and distance
    double angle_max = 0.0;
    double dist_max  = 0.0;

    // Loop over x pixels
    for (int ix = -nx; ix <= nx; ++ix) {

        // Set x value
        double x = double(ix) + crpix1;

        // Skip pixels outside valid range (they are not expected to
        // transform bijectively)
        if (x < 0 || x >= nx) {
            continue;
        }

        // Loop over y pixels
        for (int iy = -ny; iy <= ny; ++iy) {

            // Set y value
            double y = double(iy) + crpix2;

            // Skip pixels outside valid range (they are not expected to
            // transform bijectively)
            if (y < 0 || y >= ny) {
                continue;
            }

            // Set sky pixel
            GSkyPixel pix_in(x,y);

            // Transform to world
            GSkyDir dir1 = wcs->pix2dir(pix_in);
            GSkyDir dir2 = cpy->pix2dir(pix_in);
            double angle = dir1.dist_deg(dir2);
            if (angle > angle_max) {
                angle_max = angle;
            }

            // Debug option: Dump discrepancies
            #if defined(G_WCS_COPY_DEBUG)
            if (angle > 0.001) {
                std::cout << std::endl;
                std::cout << "angle=" << angle;
                std::cout << " dir1=" << dir1;
                std::cout << " dir2=" << dir2;
            }
            #endif

            // Transform to pixel
            GSkyPixel pix_out1 = wcs->dir2pix(dir1);
            GSkyPixel pix_out2 = cpy->dir2pix(dir2);

            // Compute distance
            double dx   = pix_out1.x()-pix_out2.x();
            double dy   = pix_out1.y()-pix_out2.y();
            double dist = std::sqrt(dx*dx+dy*dy);
            if (dist > dist_max) {
                dist_max = dist;
            }

            // Debug option: Dump discrepancies
            #if defined(G_WCS_COPY_DEBUG)
            if (dist > 0.001) {
                std::cout << std::endl;
                std::cout << "dist=" << dist;
                std::cout << " dx=" << dx;
                std::cout << " dy=" << dy;
                std::cout << " pix_out1=" << pix_out1;
                std::cout << " pix_out2=" << pix_out2;
                std::cout << " dir1=" << dir1;
                std::cout << " dir2=" << dir2;
            }
            #endif

        } // endfor: y pixels
    } // endfor: x pixels

    // Delete copy
    delete cpy;

    // Return
    return ((dist_max > angle_max) ? dist_max : angle_max);
}


/***************************************************************************
 * @brief Test GSkyDir class
 ***************************************************************************/
void TestGSky::test_GSkyDir(void)
{
    // Test void constructor
    GSkyDir dir1;
    test_value(dir1.ra_deg(),  0.0, "Right Ascension of empty sky direction");
    test_value(dir1.dec_deg(), 0.0, "Declination of empty sky direction");
    test_value(dir1.l_deg(),   0.0, "Longitude of empty sky direction");
    test_value(dir1.b_deg(),   0.0, "Latitude of empty sky direction");

    // Test Crab sky direction
    GSkyDir dir2;
    dir2.radec_deg(83.633083, 22.0145);
    test_value(dir2.ra_deg(),  83.633083, "Right Ascension of Crab");
    test_value(dir2.dec_deg(), 22.0145,   "Declination of Crab");
    test_value(dir2.l_deg(),   184.55745, "Longitude of Crab");
    test_value(dir2.b_deg(),    -5.78436, "Latitude of Crab");

    // Test precess() method
    dir2.precess(2000.0, 1950.0);
    test_value(dir2.ra_deg(),  82.88086799, "Right Ascension of Crab (epoch 1950)");
    test_value(dir2.dec_deg(), 21.98181049, "Declination of Crab (epoch 1950)");
    dir2.precess(1950.0, 2000.0);
    test_value(dir2.ra_deg(),  83.633083, "Right Ascension of Crab");
    test_value(dir2.dec_deg(), 22.0145,   "Declination of Crab");

    // Test posang() and posang_deg() methods
    dir1.radec_deg(0.0,  0.0);
    dir2.radec_deg(0.0, 10.0);
    test_value(dir1.posang(dir2), 0.0, "1. Position angle posang()");
    test_value(dir1.posang(dir2, "CEL"), 0.0, "2. Position angle posang(CEL)");
    test_value(dir1.posang(dir2, "GAL"), 0.4098000, "3. Position angle posang(GAL)");
    test_value(dir1.posang_deg(dir2), 0.0, "4. Position angle posang_deg()");
    test_value(dir1.posang_deg(dir2, "CEL"), 0.0, "5. Position angle posang_deg(CEL)");
    test_value(dir1.posang_deg(dir2, "GAL"), 23.47981, "6. Position angle posang_deg(GAL)");
    dir2.radec_deg(90.0, 0.0);
    test_value(dir1.posang(dir2), 1.5707963, "7. Position angle posang()");
    test_value(dir1.posang(dir2, "CEL"), 1.5707963, "8. Position angle posang(CEL)");
    test_value(dir1.posang(dir2, "GAL"), 1.9805963, "9. Position angle posang(GAL)");
    test_value(dir1.posang_deg(dir2), 90.0, "10. Position angle posang_deg()");
    test_value(dir1.posang_deg(dir2, "CEL"), 90.0, "11. Position angle posang_deg(CEL)");
    test_value(dir1.posang_deg(dir2, "GAL"), 113.47981, "12. Position angle posang_deg(GAL)");
    dir1.lb_deg(0.0,  0.0);
    dir2.lb_deg(0.0, 10.0);
    test_value(dir1.posang(dir2), -1.0227397, "13. Position angle posang()");
    test_value(dir1.posang(dir2, "CEL"), -1.0227397, "14. Position angle posang(CEL)");
    test_value(dir1.posang(dir2, "GAL"), 0.0, "15. Position angle posang(GAL)");
    test_value(dir1.posang_deg(dir2), -58.5986663, "16. Position angle posang_deg()");
    test_value(dir1.posang_deg(dir2, "CEL"), -58.5986663, "17. Position angle posang_deg(CEL)");
    test_value(dir1.posang_deg(dir2, "GAL"), 0.0, "18. Position angle posang_deg(GAL)");
    dir2.lb_deg(90.0, 0.0);
    test_value(dir1.posang(dir2), 0.5480567, "19. Position angle posang()");
    test_value(dir1.posang(dir2, "CEL"), 0.5480567, "20. Position angle posang(CEL)");
    test_value(dir1.posang(dir2, "GAL"), 1.5707963, "21. Position angle posang(GAL)");
    test_value(dir1.posang_deg(dir2), 31.4013337, "22. Position angle posang_deg()");
    test_value(dir1.posang_deg(dir2, "CEL"), 31.4013337, "23. Position angle posang_deg(CEL)");
    test_value(dir1.posang_deg(dir2, "GAL"), 90.0, "24. Position angle posang_deg(GAL)");

    // Return
    return;
}


/***************************************************************************
 * @brief Test GSkyDirs class
 ***************************************************************************/
void TestGSky::test_GSkyDirs(void)
{
    // Manipulate GSkyDirs starting from an empty object
    GSkyDirs dirs;
    test_value(dirs.size(), 0, "GSkyDirs should have zero size.");
    test_assert(dirs.is_empty(), "GSkyDirs should be empty.");

    // Add a sky direction
    dirs.append(GSkyDir());
    test_value(dirs.size(), 1, "GSkyDirs should have 1 sky direction.");
    test_assert(!dirs.is_empty(), "GSkyDirs should not be empty.");

    // Remove sky direction
    dirs.remove(0);
    test_value(dirs.size(), 0, "GSkyDirs should have zero size.");
    test_assert(dirs.is_empty(), "GSkyDirs should be empty.");

    // Append two sky directions
    dirs.append(GSkyDir());
    dirs.append(GSkyDir());
    test_value(dirs.size(), 2, "GSkyDirs should have 2 sky directions.");
    test_assert(!dirs.is_empty(), "GSkyDirs should not be empty.");

    // Clear object
    dirs.clear();
    test_value(dirs.size(), 0, "GSkyDirs should have zero size.");
    test_assert(dirs.is_empty(), "GSkyDirs should be empty.");

    // Insert two sky directions
    dirs.insert(0, GSkyDir());
    dirs.insert(0, GSkyDir());
    test_value(dirs.size(), 2, "GSkyDirs should have 2 sky directions.");
    test_assert(!dirs.is_empty(), "GSkyDirs should not be empty.");

    // Extend sky directios
    dirs.extend(dirs);
    test_value(dirs.size(), 4, "GSkyDirs should have 4 sky directions.");
    test_assert(!dirs.is_empty(), "GSkyDirs should not be empty.");

    // Create 4 sky directions
    dirs.clear();
    for (int i = 0; i < 4; ++i) {
        GSkyDir dir;
        dir.radec_deg(double(i), double(i));
        dirs.append(dir);
    }
    for (int i = 0; i < 4; ++i) {
        test_value(dirs[i].ra_deg(), double(i));
        test_value(dirs[i].dec_deg(), double(i));
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Test GSkyPixel class
 *
 * Test GSkyPixel class.
 ***************************************************************************/
void TestGSky::test_GSkyPixel(void)
{
    // Test void constructor
    GSkyPixel pixel1;
    test_value(pixel1.size(), 0);

    // Test 1D constructor
    GSkyPixel pixel2(41);
    test_assert(pixel2.is_1D(), "Pixel is not 1D");
    test_assert(!pixel2.is_2D(), "Pixel is 2D but it should be 1D");
    test_value(pixel2.size(), 1);
    test_value(pixel2.index(), 41.0);

    // Test 1D constructor
    GSkyPixel pixel3(41.001);
    test_assert(pixel3.is_1D(), "Pixel is not 1D");
    test_assert(!pixel3.is_2D(), "Pixel is 2D but it should be 1D");
    test_value(pixel3.size(), 1);
    test_value(pixel3.index(), 41.001);

    // Test 2D constructor
    GSkyPixel pixel4(41,14);
    test_assert(!pixel4.is_1D(), "Pixel is 1D but it should be 2D");
    test_assert(pixel4.is_2D(), "Pixel is not 2D");
    test_value(pixel4.size(), 2);
    test_value(pixel4.x(), 41.0);
    test_value(pixel4.y(), 14.0);

    // Test 2D constructor
    GSkyPixel pixel5(41.001,14.003);
    test_assert(!pixel5.is_1D(), "Pixel is 1D but it should be 2D");
    test_assert(pixel5.is_2D(), "Pixel is not 2D");
    test_value(pixel5.size(), 2);
    test_value(pixel5.x(), 41.001);
    test_value(pixel5.y(), 14.003);

    // Test 1D indexing
    GSkyPixel pixel6;
    test_value(pixel6.size(), 0);
    pixel6 = 41;
    int index6 = pixel6;
    test_value(index6, 41);
    pixel6 = 41.001;
    index6 = pixel6;
    test_value(index6, 41);
    double dindex6 = pixel6;
    test_value(dindex6, 41.001);

    // Test cloning
    GSkyPixel pixel7;
    test_value(pixel7.size(), 0);
    GSkyPixel* clone = pixel7.clone();
    test_value(clone->size(), 0);
    delete clone;
    pixel7 = 41;
    clone = pixel7.clone();
    test_value(clone->size(), 1);
    int index7 = pixel7;
    test_value(index7, 41);
    delete clone;
    pixel7.x(41.001);
    pixel7.y(14.003);
    clone = pixel7.clone();
    test_value(clone->size(), 2);
    test_value(clone->x(), 41.001);
    test_value(clone->y(), 14.003);
    delete clone;

    // Test equality and inequality operators using pixels from above
    test_assert(pixel1 == pixel1, "Test equality operator for empty pixel");
    test_assert(!(pixel1 != pixel1), "Test inequality operator for empty pixel");
    test_assert(pixel2 == pixel2, "Test equality operator for 1D pixel");
    test_assert(!(pixel2 != pixel2), "Test inequality operator for 1D pixel");
    test_assert(pixel4 == pixel4, "Test equality operator for 2D pixel");
    test_assert(!(pixel4 != pixel4), "Test inequality operator for 2D pixel");
    test_assert(!(pixel2 == pixel4), "Test equality operator for 1D and 2D pixels");
    test_assert(pixel2 != pixel4, "Test inequality operator for 1D and 2D pixels");

    // Return
    return;
}


/***********************************************************************//**
 * @brief Test GWcs projections
 *
 * This method tests all WCS projections that have been registered.
 ***************************************************************************/
void TestGSky::test_GWcs(void)
{
    // Allocate parameters for testing
    double crval1 = 83.63;
    double crval2 = 22.01;
    double crpix1 = 100.5;
    double crpix2 = 100.5;
    double cdelt1 =   0.1;
    double cdelt2 =   0.1;
    int    nx     =   200;
    int    ny     =   200;

    // Loop over all non-HealPix projections
    GWcsRegistry registry;
    for (int i = 0; i < registry.size(); ++i) {

        // Set header
        std::string test_class = "GWcs" + registry.code(i);

        // Perform tests
        test_try("Test "+test_class);
        try {

            // Allocate projection CEL and GAL
            GWcs *cel = dynamic_cast<GWcs*>(registry.alloc(registry.code(i)));
            GWcs *gal = dynamic_cast<GWcs*>(registry.alloc(registry.code(i)));

            // Throw an error if allocation failed
            if (cel == NULL || gal == NULL) {
                std::cout << std::endl
                        << "TEST ERROR: Could not allocate "+test_class+"."
                        << std::endl;
                throw exception_error("Could not allocate"+test_class);
            }

            // Set projections
            test_try("Set projections");
            try {
                cel->set("CEL", crval1, crval2, crpix1, crpix2, cdelt1, cdelt2);
                gal->set("GAL", crval1, crval2, crpix1, crpix2, cdelt1, cdelt2);
                test_try_success();
            }
            catch (std::exception &e) {
                test_try_failure(e);
            }

            // Test forth-back CEL conversion
            test_try("Test forth-back CEL conversion");
            try {
                double tol = 0.0;
                if ((tol = wcs_forth_back_pixel(cel, nx, ny, crpix1, crpix2)) > 1.0e-10) {
                    throw exception_failure("CEL forth-back transformation"
                                            " tolerance 1.0e-10 exceeded: "+
                                            gammalib::str(tol));
                }
                test_try_success();
            }
            catch (std::exception &e) {
                test_try_failure(e);
            }

            // Test forth-back GAL conversion
            test_try("Test forth-back GAL conversion");
            try {
                double tol = 0.0;
                if ((tol = wcs_forth_back_pixel(gal, nx, ny, crpix1, crpix2)) > 1.0e-10) {
                    throw exception_failure("GAL forth-back transformation"
                                            " tolerance 1.0e-10 exceeded: "+
                                            gammalib::str(tol));
                }
                test_try_success();
            }
            catch (std::exception &e) {
                test_try_failure(e);
            }

            // Test CEL copy
            test_try("Test CEL copy");
            try {
                double tol = 0.0;
                if ((tol = wcs_copy(cel, nx, ny, crpix1, crpix2)) > 1.0e-4) {
                    throw exception_failure("CEL copy tolerance 1.0e-4"
                                            " exceeded: "+gammalib::str(tol));
                }
                test_try_success();
            }
            catch (std::exception &e) {
                test_try_failure(e);
            }

            // Test GAL copy
            test_try("Test GAL copy");
            try {
                double tol = 0.0;
                if ((tol = wcs_copy(gal, nx, ny, crpix1, crpix2)) > 1.0e-4) {
                    throw exception_failure("GAL copy tolerance 1.0e-4"
                                            " exceeded: "+gammalib::str(tol));
                }
                test_try_success();
            }
            catch (std::exception &e) {
                test_try_failure(e);
            }

            // Free memory
            delete cel;
            delete gal;

            // Signal success
            test_try_success();
        }
        catch (std::exception &e) {
            test_try_failure(e);
        }

    } // endfor: looped over all non-HealPix projections

    // Exit test
    return;
}


/***************************************************************************
 * @brief GSkyMap_healpix_construct
 ***************************************************************************/
void TestGSky::test_GSkyMap_healpix_construct(void)
{

    // Test void constructor
    GSkyMap empty_map;
    test_assert(empty_map.is_empty(), "Check for empty sky map");
    test_value(empty_map.npix(), 0, "Check for no sky map pixels");
    test_value(empty_map.nx(), 0, "Check for no X-direction map pixels");
    test_value(empty_map.ny(), 0, "Check for no Y-direction map pixels");
    test_value(empty_map.nmaps(), 0, "Check for no sky maps");

    // Test Healpix constructors
    GSkyMap ring1("GAL", 1, "RING", 1);
    test_assert(!ring1.is_empty(), "Check for non-empty sky map");
    test_value(ring1.npix(), 12, "Check for 12 sky map pixels");
    test_value(ring1.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring1.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring1.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring2("GAL", 2, "RING", 1);
    test_assert(!ring2.is_empty(), "Check for non-empty sky map");
    test_value(ring2.npix(), 48, "Check for 48 sky map pixels");
    test_value(ring2.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring2.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring2.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring4("GAL", 4, "RING", 1);
    test_assert(!ring4.is_empty(), "Check for non-empty sky map");
    test_value(ring4.npix(), 192, "Check for 192 sky map pixels");
    test_value(ring4.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring4.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring4.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring8("GAL", 8, "RING", 1);
    test_assert(!ring8.is_empty(), "Check for non-empty sky map");
    test_value(ring8.npix(), 768, "Check for 768 sky map pixels");
    test_value(ring8.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring8.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring8.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring16("GAL", 16, "RING", 1);
    test_assert(!ring16.is_empty(), "Check for non-empty sky map");
    test_value(ring16.npix(), 3072, "Check for 3072 sky map pixels");
    test_value(ring16.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring16.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring16.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring32("GAL", 32, "RING", 1);
    test_assert(!ring32.is_empty(), "Check for non-empty sky map");
    test_value(ring32.npix(), 12288, "Check for 12288 sky map pixels");
    test_value(ring32.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring32.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring32.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring64("GAL", 64, "RING", 1);
    test_assert(!ring64.is_empty(), "Check for non-empty sky map");
    test_value(ring64.npix(), 49152, "Check for 49152 sky map pixels");
    test_value(ring64.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring64.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring64.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring128("GAL", 128, "RING", 1);
    test_assert(!ring128.is_empty(), "Check for non-empty sky map");
    test_value(ring128.npix(), 196608, "Check for 196608 sky map pixels");
    test_value(ring128.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring128.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring128.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring256("GAL", 256, "RING", 1);
    test_assert(!ring256.is_empty(), "Check for non-empty sky map");
    test_value(ring256.npix(), 786432, "Check 786432 XX sky map pixels");
    test_value(ring256.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring256.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring256.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap ring512("GAL", 512, "RING", 1);
    test_assert(!ring512.is_empty(), "Check for non-empty sky map");
    test_value(ring512.npix(), 3145728, "Check for 3145728 sky map pixels");
    test_value(ring512.nx(), 0, "Check for no X-direction map pixels");
    test_value(ring512.ny(), 0, "Check for no Y-direction map pixels");
    test_value(ring512.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest1("GAL", 1, "NEST", 1);
    test_assert(!nest1.is_empty(), "Check for non-empty sky map");
    test_value(nest1.npix(), 12, "Check for 12 sky map pixels");
    test_value(nest1.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest1.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest1.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest2("GAL", 2, "NEST", 1);
    test_assert(!nest2.is_empty(), "Check for non-empty sky map");
    test_value(nest2.npix(), 48, "Check for 48 sky map pixels");
    test_value(nest2.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest2.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest2.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest4("GAL", 4, "NEST", 1);
    test_assert(!nest4.is_empty(), "Check for non-empty sky map");
    test_value(nest4.npix(), 192, "Check for 192 sky map pixels");
    test_value(nest4.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest4.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest4.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest8("GAL", 8, "NEST", 1);
    test_assert(!nest8.is_empty(), "Check for non-empty sky map");
    test_value(nest8.npix(), 768, "Check for 768 sky map pixels");
    test_value(nest8.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest8.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest8.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest16("GAL", 16, "NEST", 1);
    test_assert(!nest16.is_empty(), "Check for non-empty sky map");
    test_value(nest16.npix(), 3072, "Check for 3072 sky map pixels");
    test_value(nest16.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest16.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest16.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest32("GAL", 32, "NEST", 1);
    test_assert(!nest32.is_empty(), "Check for non-empty sky map");
    test_value(nest32.npix(), 12288, "Check for 12288 sky map pixels");
    test_value(nest32.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest32.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest32.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest64("GAL", 64, "NEST", 1);
    test_assert(!nest64.is_empty(), "Check for non-empty sky map");
    test_value(nest64.npix(), 49152, "Check for 49152 sky map pixels");
    test_value(nest64.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest64.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest64.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest128("GAL", 128, "NEST", 1);
    test_assert(!nest128.is_empty(), "Check for non-empty sky map");
    test_value(nest128.npix(), 196608, "Check for 196608 sky map pixels");
    test_value(nest128.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest128.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest128.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest256("GAL", 256, "NEST", 1);
    test_assert(!nest256.is_empty(), "Check for non-empty sky map");
    test_value(nest256.npix(), 786432, "Check for 786432 sky map pixels");
    test_value(nest256.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest256.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest256.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap nest512("GAL", 512, "NEST", 1);
    test_assert(!nest512.is_empty(), "Check for non-empty sky map");
    test_value(nest512.npix(), 3145728, "Check for 3145728 sky map pixels");
    test_value(nest512.nx(), 0, "Check for no X-direction map pixels");
    test_value(nest512.ny(), 0, "Check for no Y-direction map pixels");
    test_value(nest512.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap map1("CEL", 1, "RING", 1);
    test_assert(!map1.is_empty(), "Check for non-empty sky map");
    test_value(map1.npix(), 12, "Check for 12 sky map pixels");
    test_value(map1.nx(), 0, "Check for no X-direction map pixels");
    test_value(map1.ny(), 0, "Check for no Y-direction map pixels");
    test_value(map1.nmaps(), 1, "Check for one sky map");
    //
    GSkyMap map2("CEL", 1, "NESTED", 2);
    test_assert(!map2.is_empty(), "Check for non-empty sky map");
    test_value(map2.npix(), 12, "Check for 12 sky map pixels");
    test_value(map2.nx(), 0, "Check for no X-direction map pixels");
    test_value(map2.ny(), 0, "Check for no Y-direction map pixels");
    test_value(map2.nmaps(), 2, "Check for one sky map");

    // Test Healpix copy constructor
    GSkyMap ring1cpy = ring1;
	test_assert(!ring1cpy.is_empty(), "Check for non-empty sky map");
	test_value(ring1cpy.npix(), 12, "Check for 12 sky map pixels");
	test_value(ring1cpy.nx(), 0, "Check for no X-direction map pixels");
	test_value(ring1cpy.ny(), 0, "Check for no Y-direction map pixels");
	test_value(ring1cpy.nmaps(), 1, "Check for one sky map");

    // Test invalid coordsys in constructor
    test_try("Test invalid coordsys in constructor");
    try {
        GSkyMap map("HOR", 1, "RING", 1);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test invalid nside in constructor
    test_try("Test invalid nside in constructor");
    try {
        GSkyMap map("GAL", 3, "RING", 1);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test invalid ordering in constructor
    test_try("Test invalid ordering in constructor");
    try {
        GSkyMap map("GAL", 2, "SPHERICAL", 1);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }


    // Test invalid nmaps in constructor
    test_try("Test invalid nmaps in constructor");
    try {
        GSkyMap map("GAL", 2, "NEST", 0);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test region circle
    GSkyRegionCircle region = ring1.region_circle();
	test_value(region.centre().ra_deg(), 0.0, "Check R.A. for region_circle()");
	test_value(region.centre().dec_deg(), 0.0, "Check Dec. for region_circle()");
	test_value(region.radius(), 180.0, "Check radius for region_circle()");

    // Exit test
    return;

}


/***************************************************************************
 * @brief GSkyMap_healpix_io
 ***************************************************************************/
void TestGSky::test_GSkyMap_healpix_io(void)
{
    // Set filenames
    const std::string file1 = "test_skymap_hpx_1.fits";

    // Define Healpix map for comparison
    GSkyMap refmap("GAL", 4, "RING", 1);

    // Test Healpix map saving
    test_try("Test Healpix map saving");
    try {
        for (int pix = 0; pix < refmap.npix(); ++pix)
            refmap(pix) = pix+1;
        refmap.save(file1, true);

        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test Healpix map loading
    test_try("Test Healpix map loading");
    try {
        GSkyMap map;
        map.load(file1);
        int diff = 0;
        for (int pix = 0; pix < refmap.npix(); ++pix) {
            if (map(pix) != refmap(pix))
                diff++;
        }
        if (diff > 0) {
            throw exception_error("Loaded file differs from saved file.");
        }

        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test Healpix map instantiation
    test_try("Test Healpix map instantiation");
    try {
        GSkyMap map(file1);
        int diff = 0;
        for (int pix = 0; pix < refmap.npix(); ++pix) {
            if (map(pix) != refmap(pix))
                diff++;
        }
        if (diff > 0) {
            throw exception_error("Loaded file differs from saved file.");
        }
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Exit test
    return;
}


/***************************************************************************
 * @brief GSkyMap_wcs_construct
 ***************************************************************************/
void TestGSky::test_GSkyMap_wcs_construct(void)
{
    // Set precision
    const double eps = 1.0e-5;

    // Test void constructor
    test_try("Test void constructor");
    try {
        GSkyMap map;
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test non-Healpix constructors
    test_try("Test non-Healpix constructors");
    try {
        GSkyMap map1("CAR", "GAL", 0.0, 0.0, 1.0, 1.0, 100, 100);
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test CAR projection
    test_try("Test CAR projection");
    try {
        GSkyMap map1("CAR", "GAL", 138.817, 37.293, 0.521, 0.931, 100, 100);
        GSkyDir dir;
        for (int l = -180; l < 180; ++l) {
            for (int b = -90; b < 90; ++b) {
                dir.lb_deg(double(l),double(b));
                GSkyPixel pixel    = map1.dir2pix(dir);
                GSkyDir   dir_back = map1.pix2dir(pixel);
                double    dist     = dir.dist_deg(dir_back);
                if (dist > eps) {
                    throw exception_failure("Sky direction differs: dir="+
                                            dir.print()+" pixel="+
                                            pixel.print()+" dir_back"+
                                            dir_back.print()+" dist="+
                                            gammalib::str(dist)+" deg");
                }
            }
        }
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test region circle
    GSkyMap          map2("CAR", "CEL", 138.817, 37.293, 0.521, 0.931, 100, 100);
    GSkyRegionCircle region = map2.region_circle();
	test_value(region.centre().ra_deg(), 139.146485, "Check R.A. for region_circle()");
	test_value(region.centre().dec_deg(), 37.758046, "Check Dec. for region_circle()");
	test_value(region.radius(), 52.370103, "Check radius for region_circle()");

    // Exit test
    return;
}


/***************************************************************************
 * @brief GSkyMap_wcs_io
 ***************************************************************************/
void TestGSky::test_GSkyMap_wcs_io(void)
{
    // Set filenames
    const std::string file1 = "test_skymap_wcs_1.fits";
    const std::string file2 = "test_skymap_wcs_2.fits";

    // Define WCS map for comparison
    GSkyMap refmap1("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 100, 100);
    GSkyMap refmap2("CAR", "CEL", 0.0, 0.0, -0.3, 0.3, 200, 200);

    // Test WCS map saving
    test_try("Test WCS map saving");
    try {
        for (int pix = 0; pix < refmap1.npix(); ++pix) {
            refmap1(pix) = pix+1;
        }
        refmap1.save(file1, 1);
        for (int pix = 0; pix < refmap2.npix(); ++pix) {
            refmap2(pix) = pix+1;
        }
        refmap2.save(file2, 1);
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test WCS map loading (1)
    test_try("Test WCS map loading (1)");
    try {
        GSkyMap map;
        map.load(file1);
        int diff = 0;
        for (int pix = 0; pix < refmap1.npix(); ++pix) {
            if (map(pix) != refmap1(pix)) {
                diff++;
            }
        }
        if (diff > 0) {
            throw exception_error("Loaded file "+file1+
                                  " differs from saved file.");
        }
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test WCS map loading (2)
    test_try("Test WCS map loading (2)");
    try {
        GSkyMap map;
        map.load(file2);
        int diff = 0;
        for (int pix = 0; pix < refmap2.npix(); ++pix) {
            if (map(pix) != refmap2(pix)) {
                diff++;
            }
        }
        if (diff > 0) {
            throw exception_error("Loaded file "+file2+
                                  " differs from saved file.");
        }
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Exit test
    return;
}


/***************************************************************************
 * @brief GSkyMap
 *
 * This method tests the GSkyMap class.
 ***************************************************************************/
void TestGSky::test_GSkyMap(void)
{
    // Remove result files
    system("rm -rf test_map_src.fits");
    system("rm -rf test_map_dst.fits");
    system("rm -rf test_map_shape*.fits");

    // Define empty sky map
    GSkyMap empty_map;

    // Test that empty map is indeed empty
	test_assert(empty_map.is_empty(), "Check for empty sky map");
	test_value(empty_map.nmaps(), 0, "Check for no sky maps");
	test_value(empty_map.npix(), 0, "Check number of empty sky map pixels");
	test_value(empty_map.nx(), 0, "Check number of empty sky map X pixels");
	test_value(empty_map.ny(), 0, "Check number of empty sky map X pixels");
	test_value(empty_map.ndim(), 0, "Check empty sky map dimension");
	test_value(empty_map.shape().size(), 0, "Check empty sky map shape");

    // Test that writing, publishing and printing of empty sky map does not
    // lead to a segmentation fault
    empty_map.save("test_empty_map.fits", true);
    empty_map.publish("Empty sky map");
    empty_map.print();

    // Define several maps for comparison
    GSkyMap map_src("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 10, 10, 2);
    GSkyMap map_dst("CAR", "GAL", 0.0, 0.0, -0.1, 0.1, 100, 100, 2);
    GSkyMap map_new("CAR", "GAL", 0.0, 0.0, -0.1, 0.1, 100, 100, 2);

    // Test map dimensions and shape
	test_value(map_src.nmaps(), 2, "Check that sky map contains 2 maps");
	test_value(map_src.ndim(), 1, "Check that sky map has one dimension");
	test_value(map_src.shape().size(), 1, "Check that sky map has a shape size of 1");
	test_value(map_src.shape()[0], 2, "Check that sky map has 2 maps");

    // Fill map pixels
    double total_src = 0.0;
    for (int pix = 0; pix < map_src.npix(); ++pix) {
        for (int k = 0; k < map_src.nmaps(); ++k) {
            map_src(pix,k) = pix+1;
            total_src     += map_src(pix,k);
        }
    }

    // Add pixels to destination map
    map_new  = map_dst + map_src;
    map_dst += map_src;

    // Check total in destination map. Note that the total in the destination
    // map should be 100 times the total in the source map as the operator
    // is expected to perform strict bi-linear interpolation
    double total_dst = 0.0;
    double total_new = 0.0;
    double total_ref = 0.0;
    for (int pix = 0; pix < map_dst.npix(); ++pix) {
        for (int k = 0; k < map_dst.nmaps(); ++k) {
            total_dst += map_dst(pix,k);
            total_new += map_new(pix,k);
            total_ref += map_src(map_dst.pix2dir(pix),k);
        }
    }
    total_dst /= 100.0;
    total_new /= 100.0;
    total_ref /= 100.0;
	test_value(total_dst, total_ref, 1.0e-3, "Test operator+=(GSkyMap)");
	test_value(total_new, total_ref, 1.0e-3, "Test operator+(GSkyMap)");

    // Subtract pixels from destination map
    map_new  = map_dst - map_src;
    map_dst -= map_src;

    // Check total in destination map. Note that the total in the destination
    // map should be zero.
    total_dst = 0.0;
    total_new = 0.0;
    for (int pix = 0; pix < map_dst.npix(); ++pix) {
        for (int k = 0; k < map_dst.nmaps(); ++k) {
            total_dst += map_dst(pix,k);
            total_new += map_dst(pix,k);
        }
    }
	test_value(total_dst, 0.0, 1.0e-3, "Test operator-=(GSkyMap)");
	test_value(total_new, 0.0, 1.0e-3, "Test operator-(GSkyMap)");

    // Check map multiplication
    GSkyMap test_map  = map_src;
    test_map         *= map_src;
    map_new           = map_src * map_src;
    double total_test = 0.0;
    total_ref         = 0.0;
    total_new         = 0.0;
    for (int pix = 0; pix < test_map.npix(); ++pix) {
        for (int k = 0; k < test_map.nmaps(); ++k) {
            total_test += test_map(pix,k);
            total_new  += map_new(pix,k);
            total_ref  += map_src(pix,k) * map_src(pix,k);
        }
    }
	test_value(total_test, total_ref, 1.0e-3, "Test operator*=(GSkyMap)");
	test_value(total_new,  total_ref, 1.0e-3, "Test operator*(GSkyMap)");

    // Check map division
    test_map   = map_src;
    test_map  /= map_src;
    map_new    = map_src / map_src;
    total_test = 0.0;
    total_ref  = 0.0;
    total_new  = 0.0;
    for (int pix = 0; pix < test_map.npix(); ++pix) {
        for (int k = 0; k < test_map.nmaps(); ++k) {
            total_test += test_map(pix,k);
            total_new  += map_new(pix,k);
            total_ref  += map_src(pix,k) / map_src(pix,k);
        }
    }
	test_value(total_test, total_ref, 1.0e-3, "Test operator/=(GSkyMap)");
	test_value(total_new,  total_ref, 1.0e-3, "Test operator/(GSkyMap)");

    // Check map scaling
    test_map   = map_src;
    test_map  *= 3.3;
    total_test = 0.0;
    for (int pix = 0; pix < test_map.npix(); ++pix) {
        for (int k = 0; k < test_map.nmaps(); ++k) {
            total_test += test_map(pix,k);
        }
    }
	test_value(total_test, total_src*3.3, 1.0e-3, "Test operator*=(double)");

    // Check map division
    test_map   = map_src;
    test_map  /= 3.3;
    total_test = 0.0;
    for (int pix = 0; pix < test_map.npix(); ++pix) {
        for (int k = 0; k < test_map.nmaps(); ++k) {
            total_test += test_map(pix,k);
        }
    }
	test_value(total_test, total_src/3.3, 1.0e-3, "Test operator/=(double)");

    // Check map value addition
    test_map   = map_src;
    test_map  += 3.3;
    total_test = 0.0;
    double ref = total_src + 3.3*test_map.npix()*test_map.nmaps();
    for (int pix = 0; pix < test_map.npix(); ++pix) {
        for (int k = 0; k < test_map.nmaps(); ++k) {
            total_test += test_map(pix,k);
        }
    }
	test_value(total_test, ref, 1.0e-3, "Test operator+=(double)");

    // Check map value subtraction
    test_map   = map_src;
    test_map  -= 3.3;
    total_test = 0.0;
    ref        = total_src - 3.3*test_map.npix()*test_map.nmaps();
    for (int pix = 0; pix < test_map.npix(); ++pix) {
        for (int k = 0; k < test_map.nmaps(); ++k) {
            total_test += test_map(pix,k);
        }
    }
	test_value(total_test, ref, 1.0e-3, "Test operator-=(double)");

    // Save maps
    map_src.save("test_map_src.fits", true);
    map_dst.save("test_map_dst.fits", true);

    // Test map stacking
    GSkyMap map_stacked = map_src;
    map_stacked.stack_maps();
    double total_stacked = 0.0;
    for (int pix = 0; pix < map_stacked.npix(); ++pix) {
        total_stacked += map_stacked(pix);
    }
	test_value(total_stacked, total_src, 1.0e-3, "Test stack_maps() method");
	test_value(map_stacked.nmaps(), 1, "Test stack_maps() method");

    // Test total counts computation
    GNdarray counts_spectrum = map_src.counts();
    double total_counts = 0.0;
    for (int i = 0; i < counts_spectrum.size(); ++i) {
        total_counts += counts_spectrum(i);
    }
	test_value(total_counts, total_src, 1.0e-3, "Test counts() method");
	test_value(counts_spectrum.size(), map_src.nmaps(), "Test counts() method");

    // Test total flux computation
    GNdarray flux_spectrum = map_src.flux();
	test_value(flux_spectrum.size(), map_src.nmaps(), "Test flux() method");

    // Test flux in region methods
    GSkyRegionCircle    gc_circle(266.40499, -28.93617, 3.0);
    GSkyRegionRectangle gc_box(266.40499, -28.93617, 4.0, 2.0, -58.59867);
    GSkyRegionMap       gc_map(&gc_box);
    GSkyRegions         gc_regions;
    gc_regions.append(gc_circle);
    gc_regions.append(gc_box);
    test_value(map_src.flux(gc_circle),  0.49208055, "Test flux(GSkyRegionCircle) method");
    test_value(map_src.flux(gc_box),     0.12306231, "Test flux(GSkyRegionRectangle) method");
    test_value(map_src.flux(gc_map),     0.12306231, "Test flux(GSkyRegionMap) method");
    test_value(map_src.flux(gc_regions), 0.49208055, "Test flux(GSkyRegions) method");

    // Test solidangle in region methods
    test_value(map_src.solidangle(gc_circle),  0.0097441693, "Test solidangle(GSkyRegionCircle) method");
    test_value(map_src.solidangle(gc_box),     0.0024368775, "Test solidangle(GSkyRegionRectangle) method");
    test_value(map_src.solidangle(gc_map),     0.0024368775, "Test solidangle(GSkyRegionMap) method");
    test_value(map_src.solidangle(gc_regions), 0.0097441693, "Test solidangle(GSkyRegions) method");

    // Test map number changing
    GSkyMap map_more = map_src;
    map_more.nmaps(4);
    double total_more = 0.0;
    for (int k = 0; k < map_more.nmaps(); ++k) {
        for (int pix = 0; pix < map_more.npix(); ++pix) {
            total_more += map_more(pix,k);
        }
    }
	test_value(total_more, total_src, 1.0e-3, "Test nmaps() method with more maps");
	test_value(map_more.nmaps(), 4, "Test nmaps() method with more maps");
    GSkyMap map_less = map_src;
    map_less.nmaps(1);
    double total_less = 0.0;
    for (int k = 0; k < map_less.nmaps(); ++k) {
        for (int pix = 0; pix < map_less.npix(); ++pix) {
            total_less += map_less(pix,k);
        }
    }
	test_value(total_less, 0.5*total_src, 1.0e-3, "Test nmaps() method with less maps");
	test_value(map_less.nmaps(), 1, "Test nmaps() method with less maps");

    // Test map extraction
    GSkyMap map_extract = map_src.extract(0);
    double total_extract = 0.0;
    for (int k = 0; k < map_extract.nmaps(); ++k) {
        for (int pix = 0; pix < map_extract.npix(); ++pix) {
            total_extract += map_extract(pix,k);
        }
    }
	test_value(total_extract, 0.5*total_src, 1.0e-3, "Test extract() method with 1 map");
	test_value(map_extract.nmaps(), 1, "Test extract() method with 1 map");
    map_extract = map_src.extract(0,2);
    total_extract = 0.0;
    for (int k = 0; k < map_extract.nmaps(); ++k) {
        for (int pix = 0; pix < map_extract.npix(); ++pix) {
            total_extract += map_extract(pix,k);
        }
    }
	test_value(total_extract, total_src, 1.0e-3, "Test extract() method with 2 maps");
	test_value(map_extract.nmaps(), 2, "Test extract() method with 2 maps");

    // Define one more map for shaping manipulation
    GSkyMap map_shape0("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 10, 10);

    // Test map dimensions and shape
	test_value(map_shape0.nmaps(), 1, "Check that sky map contains one map");
	test_value(map_shape0.ndim(), 1, "Check that sky map has one dimension");
	test_value(map_shape0.shape().size(), 1, "Check that sky map has a shape size of 1");
	test_value(map_shape0.shape()[0], 1, "Check that sky map has one map");

    // Save map
    map_shape0.save("test_map_shape0.fits", true);

    // Load map
    GSkyMap map_load_shape0("test_map_shape0.fits");

    // Test map dimensions and shape
	test_value(map_load_shape0.nmaps(), 1, "Check that sky map contains one map");
	test_value(map_load_shape0.ndim(), 1, "Check that sky map has one dimension");
	test_value(map_load_shape0.shape().size(), 1, "Check that sky map has a shape size of 1");
	test_value(map_load_shape0.shape()[0], 1, "Check that sky map has one map");

    // Define one more map for shaping manipulation
    GSkyMap map_shape("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 10, 10, 12);

    // Test initial map dimensions and shape
	test_value(map_shape.nmaps(), 12, "Check that sky map contains 12 maps");
	test_value(map_shape.ndim(), 1, "Check that sky map has one dimension");
	test_value(map_shape.shape().size(), 1, "Check that sky map has a shape size of 1");
	test_value(map_shape.shape()[0], 12, "Check that sky map has 12 maps");

    // Save map
    map_shape.save("test_map_shape1.fits", true);

    // Load map
    GSkyMap map_load_shape1("test_map_shape1.fits");

    // Test loaded map dimensions and shape
	test_value(map_load_shape1.nmaps(), 12, "Check that sky map contains 12 maps");
	test_value(map_load_shape1.ndim(), 1, "Check that sky map has one dimension");
	test_value(map_load_shape1.shape().size(), 1, "Check that sky map has a shape size of 1");
	test_value(map_load_shape1.shape()[0], 12, "Check that sky map has 12 maps");

    // Set new map shape
    map_shape.shape(3,4);

    // Test map dimensions and shape
	test_value(map_shape.nmaps(), 12, "Check that sky map contains 12 maps");
	test_value(map_shape.ndim(), 2, "Check that sky map has two dimensions");
	test_value(map_shape.shape().size(), 2, "Check that sky map has a shape size of 2");
	test_value(map_shape.shape()[0], 3, "Check that sky map has 3 maps in first dimension");
	test_value(map_shape.shape()[1], 4, "Check that sky map has 4 maps in second dimension");

    // Save map
    map_shape.save("test_map_shape2.fits", true);

    // Load map
    GSkyMap map_load_shape2("test_map_shape2.fits");

    // Test map dimensions and shape
	test_value(map_load_shape2.nmaps(), 12, "Check that sky map contains 12 maps");
	test_value(map_load_shape2.ndim(), 2, "Check that sky map has two dimensions");
	test_value(map_load_shape2.shape().size(), 2, "Check that sky map has a shape size of 2");
	test_value(map_load_shape2.shape()[0], 3, "Check that sky map has 3 maps in first dimension");
	test_value(map_load_shape2.shape()[1], 4, "Check that sky map has 4 maps in second dimension");

    // Set new map shape
    map_shape.shape(2,3,2);

    // Test map dimensions and shape
	test_value(map_shape.nmaps(), 12, "Check that sky map contains 12 maps");
	test_value(map_shape.ndim(), 3, "Check that sky map has three dimensions");
	test_value(map_shape.shape().size(), 3, "Check that sky map has a shape size of 3");
	test_value(map_shape.shape()[0], 2, "Check that sky map has 2 maps in first dimension");
	test_value(map_shape.shape()[1], 3, "Check that sky map has 3 maps in second dimension");
	test_value(map_shape.shape()[2], 2, "Check that sky map has 2 maps in third dimension");

    // Save map
    map_shape.save("test_map_shape3.fits", true);

    // Load map
    GSkyMap map_load_shape3("test_map_shape3.fits");

    // Test map dimensions and shape
	test_value(map_load_shape3.nmaps(), 12, "Check that sky map contains 12 maps");
	test_value(map_load_shape3.ndim(), 3, "Check that sky map has three dimensions");
	test_value(map_load_shape3.shape().size(), 3, "Check that sky map has a shape size of 3");
	test_value(map_load_shape3.shape()[0], 2, "Check that sky map has 2 maps in first dimension");
	test_value(map_load_shape3.shape()[1], 3, "Check that sky map has 3 maps in second dimension");
	test_value(map_load_shape3.shape()[2], 2, "Check that sky map has 2 maps in third dimension");

    // Load map for smoothing
    GSkyMap map_smooth(sky_map);

    // Compute sum of sky map for reference
    double ref_smooth = 0.0;
    for (int pix = 0; pix < map_smooth.npix(); ++pix) {
        ref_smooth += map_smooth(pix);
    }

    // Smooth map using DISK kernel
    GSkyMap map_smooth1 = map_smooth;
    map_smooth1.smooth("DISK", 0.2);
    map_smooth1.save("test_map_smooth_disk.fits", true);

    // Compute sum of smoothed sky map
    double sum_smooth1 = 0.0;
    for (int pix = 0; pix < map_smooth1.npix(); ++pix) {
        sum_smooth1 += map_smooth1(pix);
    }
	test_value(sum_smooth1, ref_smooth, "Check DISK smoothing");

    // Smooth map using GAUSSIAN kernel
    GSkyMap map_smooth2 = map_smooth;
    map_smooth2.smooth("GAUSSIAN", 0.2);
    map_smooth2.save("test_map_smooth_gaussian.fits", true);

    // Compute sum of smoothed sky map
    double sum_smooth2 = 0.0;
    for (int pix = 0; pix < map_smooth2.npix(); ++pix) {
        sum_smooth2 += map_smooth2(pix);
    }
	test_value(sum_smooth2, ref_smooth, "Check GAUSSIAN smoothing");

    // Trim the skymap based on pixels
    int startx = 2;
    int stopx  = 5;
    int starty = 2;
    int stopy  = 5;
    test_map   = map_src.extract(startx, stopx, starty, stopy);
    test_value(test_map.npix(), 16, "Check trimmed map pixel count (index)");
    test_value(test_map.nmaps(), map_src.nmaps(), "Check total maps in trimmed map (index)");

    // Check the pixel values
    total_test = 0.0;
    total_src  = 0.0;
    for (int inx = 0; inx < test_map.npix(); ++inx) {
        // Update pixel for source map
        GSkyPixel pix = test_map.inx2pix(inx);
        pix.x(startx + pix.x());
        pix.y(starty + pix.y());

        // Sum over maps
        for (int k = 0; k < test_map.nmaps(); ++k) {
            total_test += test_map(inx,k);
            total_src  += map_src(pix,k);
        }
    }
    test_value(total_test, total_src, "Check Trimmed Map sum (index)");

    // Create a regions map
    GSkyRegionMap exclmap(test_map.extract(1));
    GSkyRegions inclusions;
    inclusions.append(exclmap);

    // Trim the skymap based on the inclusion regions
    test_map = map_src.extract(inclusions);
    test_value(test_map.npix(), 36, "Check trimmed map pixel count (region)");
    test_value(test_map.nmaps(), map_src.nmaps(), "Check total maps in trimmed map (region)");

    // Check the pixel values
    total_test = 0.0;
    total_src  = 0.0;
    for (int inx = 0; inx < test_map.npix(); ++inx) {
        // Update pixel for source map
        GSkyPixel pix = test_map.inx2pix(inx);
        pix.x(startx - 1 + pix.x());
        pix.y(starty - 1 + pix.y());

        // Sum over maps
        for (int k = 0; k < test_map.nmaps(); ++k) {
            total_test += test_map(inx,k);
            total_src  += map_src(pix,k);
        }
    }
    test_value(total_test, total_src, "Check Trimmed Map sum (region)");

    // Define maps for operator testing
    GSkyMap map_op1("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 10, 10, 2);
    GSkyMap map_op2("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 10, 10, 2);
    GSkyMap map_op3("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 12, 12, 2);
    for (int layer = 0; layer < map_op1.nmaps(); ++layer) {
        for (int inx = 0; inx < map_op1.npix(); ++inx) {
            map_op1(inx,layer) = double(inx+map_op1.npix()*layer);
        }
    }
    for (int layer = 0; layer < map_op2.nmaps(); ++layer) {
        for (int inx = 0; inx < map_op2.npix(); ++inx) {
            map_op2(inx,layer) = double(inx+map_op2.npix()*layer);
        }
    }
    for (int layer = 0; layer < map_op3.nmaps(); ++layer) {
        for (int inx = 0; inx < map_op3.npix(); ++inx) {
            map_op3(inx,layer) = double(inx+map_op3.npix()*layer);
        }
    }

    // Test addition
    test_map = map_op1 + map_op2;
    test_value(test_map(45,1), 290.0, "Check addition of identical maps");
    test_map = map_op1 + map_op3;
    test_value(test_map(45,1), 355.0, "Check addition of different maps");

    // Test subtraction
    test_map = map_op1 - map_op2;
    test_value(test_map(45,1), 0.0, "Check subtraction of identical maps");
    test_map = map_op1 - map_op3;
    test_value(test_map(45,1), -65.0, "Check subtraction of different maps");

    // Test multiplication
    test_map = map_op1 * map_op2;
    test_value(test_map(45,1), 21025.0, "Check multiplication of identical maps");
    test_map = map_op1 * map_op3;
    test_value(test_map(45,1), 30450.0, "Check multiplication of different maps");


    // Test division
    test_map = map_op1 / map_op2;
    test_value(test_map(45,1), 1.0, "Check division of identical maps");
    test_map = map_op1 / map_op3;
    test_value(test_map(45,1), 0.6904761905, "Check division of different maps");

    // Exit test
    return;
}


/***************************************************************************
 * @brief GSkyMap
 *
 * This method tests the GSkyMap class input and output. Specifically, the
 * test verifies that saving of a HEALPix and WCS map in the same FITS file
 * is possible, and that upon opening the correct extension is selected.
 ***************************************************************************/
void TestGSky::test_GSkyMap_io(void)
{
    // Set filename
    GFilename filename("test_skymap.fits");
    filename.remove();

    // Create HEALPix and WCS maps and save them to FITS file
    GSkyMap healpix1("GAL", 4, "RING", 1);
    GSkyMap healpix2("GAL", 2, "RING", 1);
    GSkyMap wcs1("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 10, 10);
    GSkyMap wcs2("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 5, 5);
    GSkyMap wcs3("CAR", "GAL", 0.0, 0.0, -1.0, 1.0, 5, 5, 12);
    for (int i = 0; i < healpix1.npix(); ++i) {
        healpix1(i) = double(i+1);
    }
    for (int i = 0; i < healpix2.npix(); ++i) {
        healpix2(i) = 3.0*double(i+1);
    }
    for (int i = 0; i < wcs1.npix(); ++i) {
        wcs1(i) = double(i+1);
    }
    for (int i = 0; i < wcs2.npix(); ++i) {
        wcs2(i) = 3.0*double(i+1);
    }
    for (int k = 0; k < 12; ++k) {
        for (int i = 0; i < wcs3.npix(); ++i) {
            wcs3(i,k) = 3.0*double(i+k+1);
        }
    }
    GFits fits(filename, true);
    healpix1.write(fits, "HEALPIX1");
    wcs1.write(fits, "WCS1");
    healpix2.write(fits, "HEALPIX2");
    wcs2.write(fits, "WCS2");
    wcs3.shape(2,2,3);
    wcs3.write(fits, "WCS3");
    fits.save(true);

    // Load HEALPix map and check content
    GSkyMap map1(filename+"[HEALPIX1]");
	test_value(map1.npix(), healpix1.npix(), "Check number of HEALPix pixels");
    int failures1(0);
    for (int i = 0; i < map1.npix(); ++i) {
        if (std::abs(map1(i)-double(i+1)) > 1.0e-6) {
            failures1++;
        }
    }
	test_value(failures1, 0, "Check number of invalid HEALPix pixel values");

    // Load WCS1 map and check content
    GSkyMap map2(filename+"[WCS1]");
	test_value(map2.npix(), wcs1.npix(), "Check number of WCS pixels");
    int failures2(0);
    for (int i = 0; i < map2.npix(); ++i) {
        if (std::abs(map2(i)-double(i+1)) > 1.0e-6) {
            failures2++;
        }
    }
	test_value(failures2, 0, "Check number of invalid WCS pixel values");

    // Load WCS3 map and check content
    GSkyMap map3(filename+"[WCS3]");
	test_value(map3.npix(),  wcs3.npix(), "Check number of WCS pixels");
	test_value(map3.nmaps(), wcs3.nmaps(), "Check number of WCS maps");
	test_value(map3.ndim(),  wcs3.ndim(), "Check dimension of WCS maps");
    int failures3(0);
    for (int k = 0; k < 12; ++k) {
        for (int i = 0; i < map3.npix(); ++i) {
            if (std::abs(map3(i,k)-3.0*double(i+k+1)) > 1.0e-6) {
                failures3++;
            }
        }
    }
	test_value(failures3, 0, "Check number of invalid WCS pixel values");

    // Exit test
    return;
}


/***************************************************************************
 * @brief Test GSkyRegions class
 ***************************************************************************/
 void TestGSky::test_GSkyRegions(void)
{
    // Allocate regions
    GSkyRegions regions;

    // Test regions loading
    test_try("Test regions loading");
    try {
        regions.load(sky_region_circ);
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Check if region was loaded correctly
    test_value(regions.size(), 5, "5 sky regions expected");
    GSkyRegionCircle* circle = dynamic_cast<GSkyRegionCircle*>(regions[0]);
    test_assert(circle->type() == "Circle", "Region is not a circle");
    test_value(circle->radius(), 10.0, 1.0e-10);
    test_value(circle->ra(), 0.1, 1.0e-10);
    test_value(circle->dec(), -35.6, 1.0e-10);
    test_value(regions[0]->name(), "", "No name for first sky region");
    test_value(regions[1]->name(), "", "Empty name for second sky region");
    test_value(regions[2]->name(), "Test 1", "Name \"Test 1\" for third sky region");
    test_value(regions[3]->name(), "Test 2", "Name \"Test 2\" for fourth sky region");
    test_value(regions[4]->name(), "Test 3", "Name \"Test 3\" for fifth sky region");

    // Test regions saving
    test_try("Test regions saving");
    try {
        regions.save("region.reg");
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test regions reloading
    test_try("Test regions reloading");
    try {
        regions.load("region.reg");
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Check if region was loaded correctly
    test_value(regions.size(), 5, "5 sky regions expected");
    circle = dynamic_cast<GSkyRegionCircle*>(regions[0]);
    test_assert(circle->type() == "Circle", "Region is not a circle");
    test_value(circle->radius(), 10.0, 1.0e-10);
    test_value(circle->ra(), 0.1, 1.0e-10);
    test_value(circle->dec(), -35.6, 1.0e-10);
    test_value(regions[0]->name(), "", "No name for first sky region");
    test_value(regions[1]->name(), "", "Empty name for second sky region");
    test_value(regions[2]->name(), "Test 1", "Name \"Test 1\" for third sky region");
    test_value(regions[3]->name(), "Test 2", "Name \"Test 2\" for fourth sky region");
    test_value(regions[4]->name(), "Test 3", "Name \"Test 3\" for fifth sky region");

    // Exit test
    return;
}


/***************************************************************************
 * @brief Test GSkyRegionCircle
 ***************************************************************************/
void TestGSky::test_GSkyRegionCircle(void)
{
    // Test void constructor
    GSkyRegionCircle circle0;
    test_value(circle0.classname(), "GSkyRegionCircle", "Test classname of void constructor");
    test_value(circle0.type(), "Circle", "Test type of void constructor");
    test_value(circle0.centre().ra_deg(), 0.0, "Test Right Ascension of centre of void constructor");
    test_value(circle0.centre().dec_deg(), 0.0, "Test Declination of centre of void constructor");
    test_value(circle0.ra(), 0.0, "Test Right Ascension of void constructor");
    test_value(circle0.dec(), 0.0, "Test Declination of void constructor");
    test_value(circle0.radius(), 0.0, "Test radius of void constructor");

    // Test sky direction constructor
    GSkyDir dir1;
    dir1.radec_deg(83.6331,22.0145);
    GSkyRegionCircle circle1(dir1, 3.1);
    test_value(circle1.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of sky direction constructor");
    test_value(circle1.centre().dec_deg(), 22.0145, "Test Declination of centre of sky direction constructor");
    test_value(circle1.ra(), 83.6331, "Test Right Ascension of sky direction constructor");
    test_value(circle1.dec(), 22.0145, "Test Declination of sky direction constructor");
    test_value(circle1.radius(), 3.1, "Test radius of sky direction constructor");

    // Test Right Ascension and Declination constructor
    GSkyRegionCircle circle2(83.6331, 22.0145, 3.1);
    test_value(circle2.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of (RA,Dec) constructor");
    test_value(circle2.centre().dec_deg(), 22.0145, "Test Declination of centre of (RA,Dec) constructor");
    test_value(circle2.ra(), 83.6331, "Test Right Ascension of (RA,Dec) constructor");
    test_value(circle2.dec(), 22.0145, "Test Declination of (RA,Dec) constructor");
    test_value(circle2.radius(), 3.1, "Test radius of (RA,Dec) constructor");

    // Test line constructor
    GSkyRegionCircle circle3("fk5;circle(83.6331, 22.0145, 3.1)");
    test_value(circle3.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of line constructor");
    test_value(circle3.centre().dec_deg(), 22.0145, "Test Declination of centre of line constructor");
    test_value(circle3.ra(), 83.6331, "Test Right Ascension of line constructor");
    test_value(circle3.dec(), 22.0145, "Test Declination of line constructor");
    test_value(circle3.radius(), 3.1, "Test radius of line constructor");

    // Test copy constructor
    GSkyRegionCircle circle4(circle3);
    test_value(circle4.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of copy constructor");
    test_value(circle4.centre().dec_deg(), 22.0145, "Test Declination of centre of copy constructor");
    test_value(circle4.ra(), 83.6331, "Test Right Ascension of copy constructor");
    test_value(circle4.dec(), 22.0145, "Test Declination of copy constructor");
    test_value(circle4.radius(), 3.1, "Test radius of copy constructor");

    // Test assignment operator
    GSkyRegionCircle circle5;
    circle5 = circle3;
    test_value(circle5.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of assignment operator");
    test_value(circle5.centre().dec_deg(), 22.0145, "Test Declination of centre of assignment operator");
    test_value(circle5.ra(), 83.6331, "Test Right Ascension of assignment operator");
    test_value(circle5.dec(), 22.0145, "Test Declination of assignment operator");
    test_value(circle5.radius(), 3.1, "Test radius of assignment operator");

    // Test clone method
    GSkyRegionCircle* circle6 = circle3.clone();
    test_value(circle6->centre().ra_deg(), 83.6331, "Test Right Ascension of centre of cloned instance");
    test_value(circle6->centre().dec_deg(), 22.0145, "Test Declination of centre of cloned instance");
    test_value(circle6->ra(), 83.6331, "Test Right Ascension of cloned instance");
    test_value(circle6->dec(), 22.0145, "Test Declination of cloned instance");
    test_value(circle6->radius(), 3.1, "Test radius of cloned instance");
    delete circle6;

    // Test constructor error modes
    test_try("Test invalid radius in sky direction constructor");
    try {
        GSkyRegionCircle circle(dir1, -1.0);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    //
    test_try("Test invalid radius in (RA,Dec) constructor");
    try {
        GSkyRegionCircle circle(83.6331, 22.0145, -1.0);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    //
    test_try("Test invalid radius in line constructor");
    try {
        GSkyRegionCircle circle("fk5;circle(83.6331, 22.0145, -1.0)");
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test assignment methods
    GSkyRegionCircle circle7;
    circle7.radius(3.1);
    test_value(circle7.radius(), 3.1, "Test radius() methods");

    // Test assignment error modes
    test_try("Test invalid radius() assignment");
    try {
        GSkyRegionCircle circle;
        circle.radius(-1.0);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test solid angle computation
    GSkyRegionCircle circle8(83.6331, 22.0145, 3.1);
    double ref = 2.0 * gammalib::pi * (1.0 - std::cos(3.1 * gammalib::deg2rad));
    test_value(circle8.solidangle(), ref, "Test solid angle computation");

    // Test sky direction contains() method
    GSkyRegionCircle circle9(1.0, 1.0, 3.0);
    GSkyDir          test_dir;
    test_dir.radec_deg(1.0, 1.0);
    test_assert(circle9.contains(test_dir), "Test whether (1,1) lies inside circle");
    test_dir.radec_deg(1.0, 3.99999);
    test_assert(circle9.contains(test_dir), "Test whether (1,4) lies inside circle");
    test_dir.radec_deg(1.0, -2.0);
    test_assert(circle9.contains(test_dir), "Test whether (1,-2) lies inside circle");
    test_dir.radec_deg(4.0, 1.0);
    test_assert(circle9.contains(test_dir), "Test whether (4,1) lies inside circle");
    test_dir.radec_deg(4.0, 4.0);
    test_assert(!circle9.contains(test_dir), "Test whether (4,4) lies outside circle");
    test_dir.radec_deg(-4.0, 4.0);
    test_assert(!circle9.contains(test_dir), "Test whether (-4,4) lies outside circle");
    test_dir.radec_deg(4.0, -4.0);
    test_assert(!circle9.contains(test_dir), "Test whether (4,-4) lies outside circle");
    test_dir.radec_deg(-4.0, -4.0);
    test_assert(!circle9.contains(test_dir), "Test whether (-4,-5) lies outside circle");

    // Test region contains() and overlaps() methods for circle
    GSkyRegionCircle circle10(1.0, 1.0, 3.1);
    GSkyRegionCircle circle11(1.0, 7.0, 3.0);
    test_assert(circle9.contains(circle9), "Test whether circle is contained in circle");
    test_assert(!circle9.contains(circle10), "Test whether circle is not contained in circle");
    test_assert(circle9.overlaps(circle10), "Test whether circle overlaps with circle");
    test_assert(!circle9.overlaps(circle11), "Test whether circle does not overlap with circle");

    // Test region contains() and overlaps() methods for rectangles
    GSkyRegionRectangle rect1(1.0, 1.0, 4.24, 4.24, 45.0);
    GSkyRegionRectangle rect2(1.0, 1.0, 4.25, 4.25, 45.0);
    GSkyRegionRectangle rect3(1.0, 7.0, 4.23, 4.23, 45.0);
    test_assert(circle9.contains(rect1), "Test whether rectangle is contained in circle");
    test_assert(!circle9.contains(rect2), "Test whether rectangle is not contained in circle");
    test_assert(circle9.overlaps(rect2), "Test whether circle overlaps with rectangle");
    test_assert(!circle9.overlaps(rect3), "Test whether circle does not overlap with rectangle");

    // Test region contains() and overlaps() methods for maps. The map region
    // is centred at (0,0) and has a radius of 0.3 deg.
    GSkyRegionMap map1(sky_region_map);
    GSkyRegionCircle circle12(0.0, 0.0, 0.31);
    GSkyRegionCircle circle13(0.0, 0.0, 0.29);
    GSkyRegionCircle circle14(0.6, 0.0, 0.3);
    test_assert(circle12.contains(map1), "Test whether map is contained in circle");
    test_assert(!circle13.contains(map1), "Test whether map is not contained in circle");
    test_assert(circle13.overlaps(map1), "Test whether circle overlaps with map");
    test_assert(!circle14.overlaps(map1), "Test whether circle does not overlap with map");

    // Test print() method
    test_assert(!circle9.print().empty(), "Test whether print() method returns something");

    // Test read() and write() methods
    GSkyRegionCircle circle15;
    circle15.read("fk5;circle(83.6331, 22.0145, 3.1)");
    test_value(circle15.centre().ra_deg(), 83.6331, "Test Right Ascension of centre after read()");
    test_value(circle15.centre().dec_deg(), 22.0145, "Test Declination of centre after read()");
    test_value(circle15.ra(), 83.6331, "Test Right Ascension after read()");
    test_value(circle15.dec(), 22.0145, "Test Declination after read()");
    test_value(circle15.radius(), 3.1, "Test radius after read()");
    std::string region = circle15.write();
    circle14.read(region);
    test_value(circle14.centre().ra_deg(), 83.6331, "Test Right Ascension of centre after second read()");
    test_value(circle14.centre().dec_deg(), 22.0145, "Test Declination of centre after second read()");
    test_value(circle14.ra(), 83.6331, "Test Right Ascension after second read()");
    test_value(circle14.dec(), 22.0145, "Test Declination after second read()");
    test_value(circle14.radius(), 3.1, "Test width after second read()");

    // Test clear method
    circle9.clear();
    test_value(circle9.centre().ra_deg(), 0.0, "Test Right Ascension of centre after clear()");
    test_value(circle9.centre().dec_deg(), 0.0, "Test Declination of centre after clear()");
    test_value(circle9.ra(), 0.0, "Test Right Ascension after clear()");
    test_value(circle9.dec(), 0.0, "Test Declination after clear()");
    test_value(circle9.radius(), 0.0, "Test radius after clear()");

    // Test equality and non-equality operators
    test_assert(circle15 == circle15, "Test equality operator for equal circles");
    test_assert(!(circle15 == circle12), "Test equality operator for non-equal circles");
    test_assert(!(circle15 != circle15), "Test for non-equality operator for equal circles");
    test_assert(circle15 != circle12, "Test for non-equality operator for non-equal circles");

	// Exit test
    return;
}


/***************************************************************************
 * @brief GSkyRegionRectangle
 ***************************************************************************/
void TestGSky::test_GSkyRegionRectangle(void)
{
    // Test void constructor
    GSkyRegionRectangle rect0;
    test_value(rect0.classname(), "GSkyRegionRectangle", "Test classname of void constructor");
    test_value(rect0.type(), "Rectangle", "Test type of void constructor");
    test_value(rect0.centre().ra_deg(), 0.0, "Test Right Ascension of centre of void constructor");
    test_value(rect0.centre().dec_deg(), 0.0, "Test Declination of centre of void constructor");
    test_value(rect0.ra(), 0.0, "Test Right Ascension of void constructor");
    test_value(rect0.dec(), 0.0, "Test Declination of void constructor");
    test_value(rect0.width(), 0.0, "Test width of void constructor");
    test_value(rect0.height(), 0.0, "Test height of void constructor");
    test_value(rect0.posang(), 0.0, "Test position angle of void constructor");

    // Test sky direction constructor
    GSkyDir dir1;
    dir1.radec_deg(83.6331,22.0145);
    GSkyRegionRectangle rect1(dir1, 3.1, 2.9, 43.7);
    test_value(rect1.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of sky direction constructor");
    test_value(rect1.centre().dec_deg(), 22.0145, "Test Declination of centre of sky direction constructor");
    test_value(rect1.ra(), 83.6331, "Test Right Ascension of sky direction constructor");
    test_value(rect1.dec(), 22.0145, "Test Declination of sky direction constructor");
    test_value(rect1.width(), 3.1, "Test width of sky direction constructor");
    test_value(rect1.height(), 2.9, "Test height of sky direction constructor");
    test_value(rect1.posang(), 43.7, "Test position angle of sky direction constructor");

    // Test Right Ascension and Declination constructor
    GSkyRegionRectangle rect2(83.6331, 22.0145, 3.1, 2.9, 43.7);
    test_value(rect2.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of (RA,Dec) constructor");
    test_value(rect2.centre().dec_deg(), 22.0145, "Test Declination of centre of (RA,Dec) constructor");
    test_value(rect2.ra(), 83.6331, "Test Right Ascension of (RA,Dec) constructor");
    test_value(rect2.dec(), 22.0145, "Test Declination of (RA,Dec) constructor");
    test_value(rect2.width(), 3.1, "Test width of (RA,Dec) constructor");
    test_value(rect2.height(), 2.9, "Test height of (RA,Dec) constructor");
    test_value(rect2.posang(), 43.7, "Test position angle of (RA,Dec) constructor");

    // Test line constructor
    GSkyRegionRectangle rect3("fk5;box(83.6331, 22.0145, 3.1, 2.9, 43.7)");
    test_value(rect3.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of line constructor");
    test_value(rect3.centre().dec_deg(), 22.0145, "Test Declination of centre of line constructor");
    test_value(rect3.ra(), 83.6331, "Test Right Ascension of line constructor");
    test_value(rect3.dec(), 22.0145, "Test Declination of line constructor");
    test_value(rect3.width(), 3.1, "Test width of line constructor");
    test_value(rect3.height(), 2.9, "Test height of line constructor");
    test_value(rect3.posang(), 43.7, "Test position angle of line constructor");

    // Test copy constructor
    GSkyRegionRectangle rect4(rect3);
    test_value(rect4.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of copy constructor");
    test_value(rect4.centre().dec_deg(), 22.0145, "Test Declination of centre of copy constructor");
    test_value(rect4.ra(), 83.6331, "Test Right Ascension of copy constructor");
    test_value(rect4.dec(), 22.0145, "Test Declination of copy constructor");
    test_value(rect4.width(), 3.1, "Test width of copy constructor");
    test_value(rect4.height(), 2.9, "Test height of copy constructor");
    test_value(rect4.posang(), 43.7, "Test position angle of copy constructor");

    // Test assignment operator
    GSkyRegionRectangle rect5;
    rect5 = rect3;
    test_value(rect5.centre().ra_deg(), 83.6331, "Test Right Ascension of centre of assignment operator");
    test_value(rect5.centre().dec_deg(), 22.0145, "Test Declination of centre of assignment operator");
    test_value(rect5.ra(), 83.6331, "Test Right Ascension of assignment operator");
    test_value(rect5.dec(), 22.0145, "Test Declination of assignment operator");
    test_value(rect5.width(), 3.1, "Test width of assignment operator");
    test_value(rect5.height(), 2.9, "Test height of assignment operator");
    test_value(rect5.posang(), 43.7, "Test position angle of assignment operator");

    // Test clone method
    GSkyRegionRectangle* rect6 = rect3.clone();
    test_value(rect6->centre().ra_deg(), 83.6331, "Test Right Ascension of centre of cloned instance");
    test_value(rect6->centre().dec_deg(), 22.0145, "Test Declination of centre of cloned instance");
    test_value(rect6->ra(), 83.6331, "Test Right Ascension of cloned instance");
    test_value(rect6->dec(), 22.0145, "Test Declination of cloned instance");
    test_value(rect6->width(), 3.1, "Test width of cloned instance");
    test_value(rect6->height(), 2.9, "Test height of cloned instance");
    test_value(rect6->posang(), 43.7, "Test position angle of cloned instance");
    delete rect6;

    // Test constructor error modes
    test_try("Test invalid width in sky direction constructor");
    try {
        GSkyRegionRectangle rect(dir1, -1.0, 2.9, 43.7);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    //
    test_try("Test invalid width in (RA,Dec) constructor");
    try {
        GSkyRegionRectangle rect(83.6331, 22.0145, -1.0, 2.9, 43.7);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    //
    test_try("Test invalid width in line constructor");
    try {
        GSkyRegionRectangle rect("fk5;box(83.6331, 22.0145, -1.0, 2.9, 43.7)");
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    //
    test_try("Test invalid height in sky direction constructor");
    try {
        GSkyRegionRectangle rect(dir1, 3.1, -1.0, 43.7);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    //
    test_try("Test invalid height in (RA,Dec) constructor");
    try {
        GSkyRegionRectangle rect(83.6331, 22.0145, 3.1, -1.0, 43.7);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    //
    test_try("Test invalid height in line constructor");
    try {
        GSkyRegionRectangle rect("fk5;box(83.6331, 22.0145, 3.1, -1.0, 43.7)");
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test assignment methods
    GSkyRegionRectangle rect7;
    rect7.width(3.1);
    rect7.height(2.9);
    rect7.posang(43.7);
    test_value(rect7.width(), 3.1, "Test width() methods");
    test_value(rect7.height(), 2.9, "Test height() methods");
    test_value(rect7.posang(), 43.7, "Test posang() methods");

    // Test assignment error modes
    test_try("Test invalid width() assignment");
    try {
        GSkyRegionRectangle rect;
        rect.width(-1.0);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    test_try("Test invalid height() assignment");
    try {
        GSkyRegionRectangle rect;
        rect.height(-1.0);
        test_try_failure();
    }
    catch (GException::invalid_argument &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test corner() method
    rect7.width(1.0);
    rect7.height(1.0);
    rect7.posang(0.0);
    test_value(rect7.corner(0).ra_deg(),   0.50001269, "Test Right Ascension of corner 0");
    test_value(rect7.corner(0).dec_deg(),  0.49999365, "Test Declination of corner 0");
    test_value(rect7.corner(1).ra_deg(),   0.50001269, "Test Right Ascension of corner 1");
    test_value(rect7.corner(1).dec_deg(), -0.49999365, "Test Declination of corner 1");
    test_value(rect7.corner(2).ra_deg(),  -0.50001269, "Test Right Ascension of corner 2");
    test_value(rect7.corner(2).dec_deg(), -0.49999365, "Test Declination of corner 2");
    test_value(rect7.corner(3).ra_deg(),  -0.50001269, "Test Right Ascension of corner 3");
    test_value(rect7.corner(3).dec_deg(),  0.49999365, "Test Declination of corner 3");

    // Test corner() method error modes
    test_try("Test corner() index < 0");
    try {
        GSkyRegionRectangle rect;
        GSkyDir dir = rect.corner(-1);
        test_try_failure();
    }
    catch (GException::out_of_range &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }
    test_try("Test corner() index > 3");
    try {
        GSkyRegionRectangle rect;
        GSkyDir dir = rect.corner(4);
        test_try_failure();
    }
    catch (GException::out_of_range &e) {
        test_try_success();
    }
    catch (std::exception &e) {
        test_try_failure(e);
    }

    // Test solid angle computation
    GSkyRegionRectangle rect8(83.6331, 22.0145, 3.1, 2.9, 43.7);
    double ref = 3.1*gammalib::deg2rad * 2.9*gammalib::deg2rad;
    test_value(rect8.solidangle(), ref, "Test solid angle computation");

    // Test sky direction contains() method
    GSkyRegionRectangle rect9(1.0, 1.0, 6.0, 10.0, 45.0);
    GSkyDir             test_dir;
    test_dir.radec_deg(-3.0, 0.0);
    test_assert(rect9.contains(test_dir), "Test whether (-3,0) lies inside rectangle");
    test_dir.radec_deg(2.0, 6.0);
    test_assert(rect9.contains(test_dir), "Test whether (2,6) lies inside rectangle");
    test_dir.radec_deg(5.0, 4.0);
    test_assert(rect9.contains(test_dir), "Test whether (5,4) lies inside rectangle");
    test_dir.radec_deg(1.0, -3.0);
    test_assert(rect9.contains(test_dir), "Test whether (1,-3) lies inside rectangle");
    test_dir.radec_deg(-2.0, 3.0);
    test_assert(!rect9.contains(test_dir), "Test whether (-2,3) lies outside rectangle");
    test_dir.radec_deg(3.0, 7.0);
    test_assert(!rect9.contains(test_dir), "Test whether (3,7) lies outside rectangle");
    test_dir.radec_deg(7.0, 3.0);
    test_assert(!rect9.contains(test_dir), "Test whether (7,3) lies outside rectangle");
    test_dir.radec_deg(2.0, -5.0);
    test_assert(!rect9.contains(test_dir), "Test whether (2,-5) lies outside rectangle");
    test_dir.radec_deg(-2.0, -4.0);
    test_assert(!rect9.contains(test_dir), "Test whether (-2,-4) lies outside rectangle");

    // Test whether corners are contained
    test_assert(rect9.contains(rect9.corner(0)), "Test whether corner(0) lies inside rectangle");
    test_assert(rect9.contains(rect9.corner(1)), "Test whether corner(1) lies inside rectangle");
    test_assert(rect9.contains(rect9.corner(2)), "Test whether corner(2) lies inside rectangle");
    test_assert(rect9.contains(rect9.corner(3)), "Test whether corner(3) lies inside rectangle");

    // Test region contains() and overlaps() methods for circle
    GSkyRegionCircle circle1(1.0, 1.0, 3.0);
    GSkyRegionCircle circle2(1.0, 1.0, 3.1);
    GSkyRegionCircle circle3(7.0, 5.0, 0.5);
    test_assert(rect9.contains(circle1), "Test whether circle is contained in rectangle");
    test_assert(!rect9.contains(circle2), "Test whether circle is not contained in rectangle");
    test_assert(rect9.overlaps(circle2), "Test whether circle overlaps with rectangle");
    test_assert(!rect9.overlaps(circle3), "Test whether circle does not overlap with rectangle");

    // Test region contains() and overlaps() methods for rectangles
    GSkyRegionRectangle rect10(1.0,  1.0, 6.1, 10.1, 45.0);
    GSkyRegionRectangle rect11(6.0, -4.0, 6.0, 10.0, 45.0);
    test_assert(rect9.contains(rect9), "Test whether rectangle is contained in rectangle");
    test_assert(!rect9.contains(rect10), "Test whether rectangle is not contained in rectangle");
    test_assert(rect9.overlaps(rect10), "Test whether rectangle overlaps with rectangle");
    test_assert(!rect9.overlaps(rect11), "Test whether rectangle does not overlap with rectangle");

    // Test region contains() and overlaps() methods for maps. The map region
    // is centred at (0,0) and has a radius of 0.3 deg.
    GSkyRegionMap map1(sky_region_map);
    GSkyRegionRectangle rect12(0.0, 0.0, 0.6,  0.6,  45.0);
    GSkyRegionRectangle rect13(0.0, 0.0, 0.59, 0.59, 45.0);
    GSkyRegionRectangle rect14(0.6, 0.0, 0.6,  0.6,   0.0);
    test_assert(rect12.contains(map1), "Test whether map is contained in rectangle");
    test_assert(!rect13.contains(map1), "Test whether map is not contained in rectangle");
    test_assert(rect13.overlaps(map1), "Test whether map overlaps with rectangle");
    test_assert(!rect14.overlaps(map1), "Test whether map does not overlap with rectangle");

    // Test print() method
    test_assert(!rect9.print().empty(), "Test whether print() method returns something");

    // Test read() and write() methods
    GSkyRegionRectangle rect15;
    rect15.read("fk5;box(83.6331, 22.0145, 3.1, 2.9, 43.7)");
    test_value(rect15.centre().ra_deg(), 83.6331, "Test Right Ascension of centre after read()");
    test_value(rect15.centre().dec_deg(), 22.0145, "Test Declination of centre after read()");
    test_value(rect15.ra(), 83.6331, "Test Right Ascension after read()");
    test_value(rect15.dec(), 22.0145, "Test Declination after read()");
    test_value(rect15.width(), 3.1, "Test width after read()");
    test_value(rect15.height(), 2.9, "Test height after read()");
    test_value(rect15.posang(), 43.7, "Test position angle after read()");
    std::string region = rect15.write();
    rect14.read(region);
    test_value(rect14.centre().ra_deg(), 83.6331, "Test Right Ascension of centre after second read()");
    test_value(rect14.centre().dec_deg(), 22.0145, "Test Declination of centre after second read()");
    test_value(rect14.ra(), 83.6331, "Test Right Ascension after second read()");
    test_value(rect14.dec(), 22.0145, "Test Declination after second read()");
    test_value(rect14.width(), 3.1, "Test width after second read()");
    test_value(rect14.height(), 2.9, "Test height after second read()");
    test_value(rect14.posang(), 43.7, "Test position angle after second read()");

    // Test clear method
    rect9.clear();
    test_value(rect9.centre().ra_deg(), 0.0, "Test Right Ascension of centre after clear()");
    test_value(rect9.centre().dec_deg(), 0.0, "Test Declination of centre after clear()");
    test_value(rect9.ra(), 0.0, "Test Right Ascension after clear()");
    test_value(rect9.dec(), 0.0, "Test Declination after clear()");
    test_value(rect9.width(), 0.0, "Test width after clear()");
    test_value(rect9.height(), 0.0, "Test height after clear()");
    test_value(rect9.posang(), 0.0, "Test position angle after clear()");

    // Exit test
    return;
}


/***************************************************************************
 * @brief GSkyRegionMap_construct
 ***************************************************************************/
void TestGSky::test_GSkyRegionMap(void)
{
    // Test void constructor
    GSkyRegionMap map0;
    test_value(map0.classname(), "GSkyRegionMap", "Test classname of void constructor");
    test_value(map0.type(), "Map", "Test type of void constructor");
    test_assert(map0.map().is_empty(), "Test map() method of void constructor");

    // Test filename constructor
    GSkyRegionMap map1(sky_region_map);
    test_assert(!map1.map().is_empty(), "Test map() method of filename constructor");

    // Test skymap constructor
    GSkyMap       skymap(sky_region_map);
    GSkyRegionMap map2(skymap);
    test_assert(!map2.map().is_empty(), "Test map() method of skymap constructor");

    // Test region constructors
    GSkyRegionCircle    circle1(83.6331, 22.0145, 3.1);
    GSkyRegionRectangle rect1(83.6331, 22.0145, 3.1, 2.9, 43.7);
    GSkyRegionMap       map3(&circle1);
    GSkyRegionMap       map4(&rect1);
    test_assert(!map3.map().is_empty(), "Test map() method of circle region constructor");
    test_assert(!map4.map().is_empty(), "Test map() method of rectangular region constructor");

    // Test copy constructor
    GSkyRegionMap map5(map2);
    test_assert(!map5.map().is_empty(), "Test map() method of copy constructor");

    // Test assignment operator
    GSkyRegionMap map6;
    map6 = map5;
    test_assert(!map6.map().is_empty(), "Test map() method of copy constructor");

    // Test clone method
    GSkyRegionMap* map7 = map6.clone();
    test_assert(!map7->map().is_empty(), "Test map() method of copy constructor");
    delete map7;

    // Test constructor error modes

    // Test assignment methods
    GSkyRegionMap map8;
    map8.map(skymap);
    test_assert(!map8.map().is_empty(), "Test map() method");

    // Test solid angle computation
    GSkyRegionMap map9(sky_region_map);
    double ref = 8.72406098e-05 ;
    test_value(map9.solidangle(), ref, "Test solid angle computation");

    // Test sky direction contains() method
    GSkyDir test_dir;
    test_dir.radec_deg(0.0, 0.0);
    test_assert(map9.contains(test_dir), "Test whether (0,0) lies inside map");
    test_dir.radec_deg(0.29, 0.0);
    test_assert(map9.contains(test_dir), "Test whether (0.29,0) lies inside map");
    test_dir.radec_deg(0.0,-0.29);
    test_assert(map9.contains(test_dir), "Test whether (0,-0.29) lies inside circle");
    test_dir.radec_deg(0.0, 0.29);
    test_assert(map9.contains(test_dir), "Test whether (0,0.29) lies inside circle");
    test_dir.radec_deg(0.3, 0.3);
    test_assert(!map9.contains(test_dir), "Test whether (0.3,0.3) lies outside circle");
    test_dir.radec_deg(0.31, 0.0);
    test_assert(!map9.contains(test_dir), "Test whether (0.31,0) lies outside circle");
    test_dir.radec_deg(-0.31, 0.0);
    test_assert(!map9.contains(test_dir), "Test whether (-0.31,0) lies outside circle");

    // Test region contains() and overlaps() methods for circle
    GSkyRegionCircle circle2(0.0, 0.0, 0.28);
    GSkyRegionCircle circle3(0.0, 0.0, 0.31);
    GSkyRegionCircle circle4(0.0, 0.6, 0.3);
    test_assert(map9.contains(circle2), "Test whether circle is contained in map");
    test_assert(!map9.contains(circle3), "Test whether circle is not contained in map");
    test_assert(map9.overlaps(circle3), "Test whether map overlaps with circle");
    test_assert(!map9.overlaps(circle4), "Test whether map does not overlap with circle");

    // Test region contains() and overlaps() methods for rectangles
    GSkyRegionRectangle rect2(0.0, 0.0, 0.42, 0.42, 45.0);
    GSkyRegionRectangle rect3(0.0, 0.0, 0.44, 0.44, 45.0);
    GSkyRegionRectangle rect4(0.0, 6.0, 0.42, 0.42, 45.0);
    test_assert(map9.contains(rect2), "Test whether rectangle is contained in map");
    test_assert(!map9.contains(rect3), "Test whether rectangle is not contained in map");
    test_assert(map9.overlaps(rect3), "Test whether map overlaps with rectangle");
    test_assert(!map9.overlaps(rect4), "Test whether map does not overlap with rectangle");

    // Test region contains() and overlaps() methods for maps
    GSkyRegionMap map_circle2(&circle2);
    GSkyRegionMap map_circle3(&circle3);
    GSkyRegionMap map_circle4(&circle4);
    test_assert(map9.contains(map_circle2), "Test whether map is contained in circle");
    test_assert(!map9.contains(map_circle3), "Test whether map is not contained in circle");
    test_assert(map9.overlaps(map_circle3), "Test whether circle overlaps with map");
    test_assert(!map9.overlaps(map_circle4), "Test whether circle does not overlap with map");

    // Test print() method
    test_assert(!map9.print().empty(), "Test whether print() method returns something");

    // Test read() and write() methods
    // Not implemented

    // Test clear method
    map9.clear();
    test_assert(map9.map().is_empty(), "Test map() method after clear()");

    // Exit test
    return;
}


/***************************************************************************
 * @brief Test GHorizDir class
 ***************************************************************************/
void TestGSky::test_GHorizDir(void)
{

    // Empty horizontal direction
    GHorizDir nulldir;

    // Set position
    GHorizDir altaz;
    double testalt =  45.0;
    double testaz  = 180.0;
    altaz.altaz_deg(testalt, testaz);

    // Retrieve position
    double newalt = altaz.alt_deg();
    double newaz  = altaz.az_deg();

    // Test position
    test_value(newalt, testalt, 1e-10, "Altitude" );
    test_value(newaz,  testaz,  1e-10, "Azimuth" );
    test_value(altaz.zenith_deg(), 90.0-testalt, 1e-10, "Zenith angle");

    // Return
    return;
}


/***************************************************************************
 * @brief Main test function
 ***************************************************************************/
int main(void)
{
    GTestSuites testsuites("GSky");

    // Create a test suite
    TestGSky test;

    // Append to the container
    testsuites.append(test);

    // Run
    bool was_successful = testsuites.run();

    // Save xml report
    testsuites.save("reports/GSky.xml");

    // Return
    return was_successful ? 0:1;
}