summaryrefslogtreecommitdiffstats
path: root/src/H5Dio.c
diff options
context:
space:
mode:
authorQuincey Koziol <koziol@hdfgroup.org>2007-11-27 21:19:42 (GMT)
committerQuincey Koziol <koziol@hdfgroup.org>2007-11-27 21:19:42 (GMT)
commit6028cd41b8d81d8f099b42efc36a5f236868be5c (patch)
tree2ba7cc4bace16a06baab493858672abaddf36470 /src/H5Dio.c
parentdf3aac7f009aeb8acf650964378251db0a02ca71 (diff)
downloadhdf5-6028cd41b8d81d8f099b42efc36a5f236868be5c.zip
hdf5-6028cd41b8d81d8f099b42efc36a5f236868be5c.tar.gz
hdf5-6028cd41b8d81d8f099b42efc36a5f236868be5c.tar.bz2
[svn-r14295] Description:
- Eliminate some redundant calls to retrieve datatype sizes in chunk read/ write routines. - Change indexed storage "common" B-tree callback user data to avoid copying chunk offset and separate "downward" info from "upward" info. - Cache chunk info (nbytes/filter_mask/address) for last chunk accessed Tested on: FreeBSD/32 6.2 (duty) in debug mode FreeBSD/64 6.2 (liberty) w/C++ & FORTRAN, in debug mode Linux/32 2.6 (kagiso) w/PGI compilers, w/C++ & FORTRAN, w/threadsafe, in debug mode Linux/64-amd64 2.6 (smirom) w/default API=1.6.x, w/C++ & FORTRAN, in production mode Linux/64-ia64 2.6 (cobalt) w/Intel compilers, w/C++ & FORTRAN, in production mode Solaris/32 2.10 (linew) w/deprecated symbols disabled, w/C++ & FORTRAN, w/szip filter, in production mode Mac OS X/32 10.4.10 (amazon) in debug mode Linux/64-ia64 2.4 (tg-login3) w/parallel, w/FORTRAN, in production mode
Diffstat (limited to 'src/H5Dio.c')
-rw-r--r--src/H5Dio.c40
1 files changed, 22 insertions, 18 deletions
diff --git a/src/H5Dio.c b/src/H5Dio.c
index 66ccd66..5950c22 100644
--- a/src/H5Dio.c
+++ b/src/H5Dio.c
@@ -1377,6 +1377,11 @@ H5D_chunk_read(H5D_io_info_t *io_info, hsize_t nelmts,
/* Set dataset storage for I/O info */
io_info->store=&store;
+ /* Compute element sizes */
+ src_type_size = H5T_get_size(dataset->shared->type);
+ dst_type_size = H5T_get_size(mem_type);
+ max_type_size = MAX(src_type_size, dst_type_size);
+
/*
* If there is no type conversion then read directly into the
* application's buffer. This saves at least one mem-to-mem copy.
@@ -1429,13 +1434,13 @@ H5D_chunk_read(H5D_io_info_t *io_info, hsize_t nelmts,
/* Perform the actual read operation */
if((io_info->ops.read)(io_info, chunk_info->chunk_points,
- H5T_get_size(dataset->shared->type), chunk_info->fspace,
+ src_type_size, chunk_info->fspace,
chunk_info->mspace, chunk_addr, chunk, buf) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_READERROR, FAIL, " chunked read failed")
/* Release the cache lock on the chunk. */
if(chunk) {
- accessed_bytes = chunk_info->chunk_points * H5T_get_size(dataset->shared->type);
+ accessed_bytes = chunk_info->chunk_points * src_type_size;
if(H5D_istore_unlock(io_info, FALSE, idx_hint, chunk, accessed_bytes) < 0)
HGOTO_ERROR(H5E_IO, H5E_READERROR, FAIL, "unable to unlock raw data chunk")
} /* end if */
@@ -1450,7 +1455,7 @@ H5D_chunk_read(H5D_io_info_t *io_info, hsize_t nelmts,
#ifdef H5S_DEBUG
H5_timer_end(&(io_info->stats->stats[1].read_timer), &timer);
- io_info->stats->stats[1].read_nbytes += nelmts * H5T_get_size(dataset->shared->type);
+ io_info->stats->stats[1].read_nbytes += nelmts * src_type_size;
io_info->stats->stats[1].read_ncalls++;
#endif
@@ -1464,10 +1469,7 @@ H5D_chunk_read(H5D_io_info_t *io_info, hsize_t nelmts,
if(nelmts==0)
HGOTO_DONE(SUCCEED)
- /* Compute element sizes and other parameters */
- src_type_size = H5T_get_size(dataset->shared->type);
- dst_type_size = H5T_get_size(mem_type);
- max_type_size = MAX(src_type_size, dst_type_size);
+ /* Compute buffer sizes and other parameters */
target_size = dxpl_cache->max_temp_buf;
/* XXX: This could cause a problem if the user sets their buffer size
* to the same size as the default, and then the dataset elements are
@@ -1646,7 +1648,7 @@ H5D_chunk_read(H5D_io_info_t *io_info, hsize_t nelmts,
/* Release the cache lock on the chunk. */
if(chunk) {
- accessed_bytes = chunk_info->chunk_points * H5T_get_size(dataset->shared->type);
+ accessed_bytes = chunk_info->chunk_points * src_type_size;
if(H5D_istore_unlock(io_info, FALSE, idx_hint, chunk, accessed_bytes) < 0)
HGOTO_ERROR(H5E_IO, H5E_READERROR, FAIL, "unable to unlock raw data chunk")
} /* end if */
@@ -1778,6 +1780,11 @@ H5D_chunk_write(H5D_io_info_t *io_info, hsize_t nelmts,
} /* end if */
#endif /* H5_HAVE_PARALLEL */
+ /* Compute element sizes and other parameters */
+ src_type_size = H5T_get_size(mem_type);
+ dst_type_size = H5T_get_size(dataset->shared->type);
+ max_type_size = MAX(src_type_size, dst_type_size);
+
/*
* If there is no type conversion then write directly from the
* application's buffer. This saves at least one mem-to-mem copy.
@@ -1820,7 +1827,7 @@ H5D_chunk_write(H5D_io_info_t *io_info, hsize_t nelmts,
chunk_addr = H5D_istore_get_addr(io_info, &udata);
if(H5D_istore_if_load(io_info, chunk_addr)) {
- accessed_bytes = chunk_info->chunk_points * H5T_get_size(dataset->shared->type);
+ accessed_bytes = chunk_info->chunk_points * dst_type_size;
if(accessed_bytes != dataset->shared->layout.u.chunk.size)
relax = FALSE;
@@ -1831,7 +1838,7 @@ H5D_chunk_write(H5D_io_info_t *io_info, hsize_t nelmts,
/* Perform the actual read operation */
if((io_info->ops.write)(io_info, chunk_info->chunk_points,
- H5T_get_size(dataset->shared->type), chunk_info->fspace,
+ dst_type_size, chunk_info->fspace,
chunk_info->mspace, chunk_addr, chunk, buf) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_READERROR, FAIL, " chunked write failed")
@@ -1851,7 +1858,7 @@ H5D_chunk_write(H5D_io_info_t *io_info, hsize_t nelmts,
#ifdef H5S_DEBUG
H5_timer_end(&(io_info->stats->stats[0].write_timer), &timer);
- io_info->stats->stats[0].write_nbytes += nelmts * H5T_get_size(mem_type);
+ io_info->stats->stats[0].write_nbytes += nelmts * src_type_size;
io_info->stats->stats[0].write_ncalls++;
#endif
@@ -1865,10 +1872,7 @@ H5D_chunk_write(H5D_io_info_t *io_info, hsize_t nelmts,
if(nelmts==0)
HGOTO_DONE(SUCCEED)
- /* Compute element sizes and other parameters */
- src_type_size = H5T_get_size(mem_type);
- dst_type_size = H5T_get_size(dataset->shared->type);
- max_type_size = MAX(src_type_size, dst_type_size);
+ /* Compute buffer sizes and other parameters */
target_size = dxpl_cache->max_temp_buf;
/* XXX: This could cause a problem if the user sets their buffer size
* to the same size as the default, and then the dataset elements are
@@ -1953,11 +1957,11 @@ H5D_chunk_write(H5D_io_info_t *io_info, hsize_t nelmts,
chunk_addr = H5D_istore_get_addr(io_info, &udata);
if(H5D_istore_if_load(io_info, chunk_addr)) {
- accessed_bytes = chunk_info->chunk_points * H5T_get_size(dataset->shared->type);
+ accessed_bytes = chunk_info->chunk_points * dst_type_size;
if(accessed_bytes != dataset->shared->layout.u.chunk.size)
relax=FALSE;
if(relax) {
- accessed_bytes = H5S_GET_SELECT_NPOINTS(chunk_info->mspace)*H5T_get_size(mem_type);
+ accessed_bytes = H5S_GET_SELECT_NPOINTS(chunk_info->mspace) * src_type_size;
if(accessed_bytes != dataset->shared->layout.u.chunk.size)
relax = FALSE;
}
@@ -2055,7 +2059,7 @@ H5D_chunk_write(H5D_io_info_t *io_info, hsize_t nelmts,
/* Release the cache lock on the chunk. */
if(chunk) {
- accessed_bytes = chunk_info->chunk_points * H5T_get_size(dataset->shared->type);
+ accessed_bytes = chunk_info->chunk_points * dst_type_size;
if(H5D_istore_unlock(io_info, TRUE, idx_hint, chunk, accessed_bytes) < 0)
HGOTO_ERROR(H5E_IO, H5E_READERROR, FAIL, "unable to unlock raw data chunk")
} /* end if */
d='n466' href='#n466'>466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Programmer:	Raymond Lu
 *              October 14, 2001
 *
 * Purpose:	Tests the H5Tget_native_type function.
 */

#include "h5test.h"

const char *FILENAME[] = {
        "ntypes",
        NULL
};

#define DIM0    100
#define DIM1    200
#define DIM3    20


int	ipoints2[DIM0][DIM1], icheck2[DIM0][DIM1];
short	spoints2[DIM0][DIM1], scheck2[DIM0][DIM1];
int	ipoints3[DIM0][DIM1][5], icheck3[DIM0][DIM1][5];

#define DSET_ATOMIC_NAME_1	"atomic_type_1"
#define DSET_ATOMIC_NAME_2	"atomic_type_2"
#define DSET_ATOMIC_NAME_3	"atomic_type_3"
#define DSET_ATOMIC_NAME_4	"atomic_type_4"
#define DSET_ATOMIC_NAME_5	"atomic_type_5"
#define DSET_COMPOUND_NAME      "compound_type"
#define DSET_COMPOUND_NAME_2    "compound_type_2"
#define DSET_COMPOUND_NAME_3    "compound_type_3"
#define DSET_COMPOUND_NAME_4    "compound_type_4"
#define DSET_ENUM_NAME	        "enum_type"
#define DSET_ARRAY_NAME	        "array_type"
#define DSET_ARRAY2_NAME	"array_type_2"
#define DSET_VL_NAME	        "vl_type"
#define DSET_VLSTR_NAME         "vlstr_type"
#define DSET_STR_NAME           "str_type"
#define DSET_OPAQUE_NAME        "opaque_type"
#define DSET1_BITFIELD_NAME     "bitfield_type_1"
#define DSET2_BITFIELD_NAME     "bitfield_type_2"

#define SPACE1_DIM1             4
#define SPACE1_RANK             1
#define SPACE2_RANK	        2
#define SPACE2_DIM1	        10
#define SPACE2_DIM2	        10
#define BITFIELD_ENUMB          8


/*-------------------------------------------------------------------------
 * Function:	test_atomic_dtype
 *
 * Purpose:	Test H5Tget_native_type for atomic datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_atomic_dtype(hid_t file)
{
    hid_t	dataset = -1, space = -1;
    hid_t       dtype = -1, native_type = -1;
    int		i, j, n;
    hsize_t	dims[2];
    void       *tmp = NULL;

    TESTING("atomic datatype");

    /* Initialize the dataset */
    for(i = n = 0; i < DIM0; i++)
        for(j = 0; j < DIM1; j++)
            ipoints2[i][j] = n++;

    /* Create the data space */
    dims[0] = DIM0;
    dims[1] = DIM1;
    if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;

    /*------------------- Test data values ------------------------*/
    /* Create the dataset */
    if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_1, H5T_STD_I32BE, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Write the data to the dataset */
    if(H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, ipoints2) < 0)
        TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_ATOMIC_NAME_1, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Verify the datatype retrieved and converted */
    if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_INT))
        TEST_ERROR;
    if(H5Tget_size(native_type) < H5Tget_size(H5T_STD_I32BE))
        TEST_ERROR;
    if(H5T_INTEGER != H5Tget_class(native_type))
        TEST_ERROR;

    /* Read the dataset back.  The temporary buffer is for special platforms
     * like Cray. */
    if(NULL == (tmp = HDmalloc((size_t)(DIM0 * DIM1 * H5Tget_size(native_type)))))
        TEST_ERROR

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
        TEST_ERROR;

    /* Copy data from temporary buffer to destination buffer */
    HDmemcpy(icheck2, tmp, (size_t)(DIM0 * DIM1 * H5Tget_size(native_type)));
    HDfree(tmp);
    tmp = NULL;

    /* Convert to the integer type */
    if(H5Tconvert(native_type, H5T_NATIVE_INT, (DIM0 * DIM1), icheck2, NULL, H5P_DEFAULT) < 0)
        TEST_ERROR;

    /* Check that the values read are the same as the values written */
    for(i = 0; i < DIM0; i++)
        for(j = 0; j < DIM1; j++)
            if(ipoints2[i][j] != icheck2[i][j]) {
                H5_FAILED();
                printf("    Read different values than written.\n");
                printf("    At index %d,%d\n", i, j);
                goto error;
            } /* end if */

    if(H5Dclose(dataset) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    if(H5Tclose(dtype) < 0) TEST_ERROR;

    /*------------------ Test different data types ----------------*/

    /* Create the dataset of H5T_STD_I64LE */
    if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_2, H5T_STD_I64LE, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Verify the datatype retrieved and converted */
    if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_LLONG))
        TEST_ERROR;
    if(H5Tget_size(native_type) < H5Tget_size(H5T_STD_I64LE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(native_type))
        TEST_ERROR;

    if(H5Dclose(dataset) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    if(H5Tclose(dtype) < 0) TEST_ERROR;


    /* Create the dataset of H5T_STD_I8LE */
    if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_3, H5T_STD_I8LE, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_ASCEND)) < 0)
        TEST_ERROR;

    /* Verify the datatype retrieved and converted */
    if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_CHAR))
        TEST_ERROR;
    if(H5Tget_size(native_type) < H5Tget_size(H5T_STD_I8LE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(native_type))
        TEST_ERROR;

    if(H5Dclose(dataset) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    if(H5Tclose(dtype) < 0) TEST_ERROR;


    /* Create the dataset of H5T_IEEE_F32BE */
    if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_4, H5T_IEEE_F32BE, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DESCEND)) < 0)
        TEST_ERROR;

    /* Verify the datatype retrieved and converted */
    if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_FLOAT))
        TEST_ERROR;
    if(H5Tget_size(native_type) < H5Tget_size(H5T_IEEE_F32BE))
        TEST_ERROR;
    if(H5T_FLOAT!=H5Tget_class(native_type))
        TEST_ERROR;

    if(H5Dclose(dataset) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    if(H5Tclose(dtype) < 0) TEST_ERROR;


    /* Create the dataset of H5T_IEEE_F64BE */
    if((dataset = H5Dcreate2(file, DSET_ATOMIC_NAME_5, H5T_IEEE_F64BE, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DESCEND)) < 0)
        TEST_ERROR;

    /* Verify the datatype retrieved and converted */
    if(H5Tget_order(native_type) != H5Tget_order(H5T_NATIVE_DOUBLE))
        TEST_ERROR;
    if(H5Tget_size(native_type) < H5Tget_size(H5T_IEEE_F64BE))
        TEST_ERROR;
    if(H5T_FLOAT != H5Tget_class(native_type))
        TEST_ERROR;

    if(H5Dclose(dataset) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    if(H5Tclose(dtype) < 0) TEST_ERROR;


    /* Close dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;

    PASSED();

    return 0;

error:
    if(tmp)
        HDfree(tmp);

    H5E_BEGIN_TRY {
        H5Dclose(dataset);
        H5Tclose(native_type);
        H5Tclose(dtype);
        H5Sclose(space);
    } H5E_END_TRY;

    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	test_compound_dtype2
 *
 * Purpose:	Test H5Tget_native_type for compound datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_compound_dtype2(hid_t file)
{
    typedef struct s2 {
        short           c2;
        long            l2;
        long long       ll2;
    } s2;
    typedef struct s1 {
        char            c;
        int             i;
        s2              st;
        unsigned long long       l;
    } s1;
    hid_t	dataset = -1, space = -1;
    hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, tid_m = -1,
                tid_m2 = -1, mem_id = -1, nest_mem_id = -1;
    int		i, j, n;
    hsize_t	dims[2];
    s1         *temp_point = NULL, *temp_check = NULL;
    s1 	       *points = NULL, *check = NULL;
    void       *tmp = NULL, *bkg = NULL;

    TESTING("nested compound datatype");

    /* Allocate space for the points & check arrays */
    if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1)))
        TEST_ERROR;
    if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1)))
        TEST_ERROR;

    /* Initialize the dataset */
    for(i = n = 0, temp_point = points; i < DIM0; i++) {
        for(j = 0; j < DIM1; j++, temp_point++) {
            temp_point->c = 't';
            temp_point->i = n++;
            temp_point->st.c2 = (short)(i + j);
            temp_point->st.l2 = (i * 5 + j * 50) * n;
            temp_point->st.ll2 = (i * 10 + j * 100) * n;
            temp_point->l = (unsigned long long)((i * 40 + j * 400) * n);
        } /* end for */
    } /* end for */

    /* Create the data space */
    dims[0] = DIM0;
    dims[1] = DIM1;
    if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;

    /* Create compound datatype for disk storage */
    if((tid2=H5Tcreate(H5T_COMPOUND, sizeof(s2))) < 0) TEST_ERROR;
    if((tid=H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;

    /* Insert and pack members */
    if(H5Tinsert(tid2, "c2", HOFFSET(s2, c2), H5T_STD_I16BE) < 0) TEST_ERROR;
#if H5_SIZEOF_LONG == 4
    if(H5Tinsert(tid2, "l2", HOFFSET(s2, l2), H5T_STD_I32LE) < 0) TEST_ERROR;
#elif H5_SIZEOF_LONG == 8
    if(H5Tinsert(tid2, "l2", HOFFSET(s2, l2), H5T_STD_I64LE) < 0) TEST_ERROR;
#else
#error "Unknown 'long' size"
#endif
#if H5_SIZEOF_LONG_LONG == 4
    if(H5Tinsert(tid2, "ll2", HOFFSET(s2, ll2), H5T_STD_I32BE) < 0) TEST_ERROR;
#elif H5_SIZEOF_LONG_LONG == 8
    if(H5Tinsert(tid2, "ll2", HOFFSET(s2, ll2), H5T_STD_I64BE) < 0) TEST_ERROR;
#else
#error "Unknown 'long long' size"
#endif

    if(H5Tinsert(tid, "c", HOFFSET(s1, c), H5T_STD_U8LE) < 0) TEST_ERROR;
    if(H5Tinsert(tid, "i", HOFFSET(s1, i), H5T_STD_I32LE) < 0) TEST_ERROR;
    if(H5Tinsert(tid, "st", HOFFSET(s1, st), tid2) < 0) TEST_ERROR;
#if H5_SIZEOF_LONG_LONG == 4
    if(H5Tinsert(tid, "l", HOFFSET(s1, l), H5T_STD_U32BE) < 0) TEST_ERROR;
#elif H5_SIZEOF_LONG_LONG == 8
    if(H5Tinsert(tid, "l", HOFFSET(s1, l), H5T_STD_U64BE) < 0) TEST_ERROR;
#else
#error "Unknown 'long long' size"
#endif

    /* Take away the paddings */
    if(H5Tpack(tid) < 0) TEST_ERROR;

    /* Create the dataset */
    if((dataset = H5Dcreate2(file, DSET_COMPOUND_NAME_2, tid, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create compound datatype for memory */
    if((tid_m2 = H5Tcreate(H5T_COMPOUND, sizeof(s2))) < 0) TEST_ERROR;
    if((tid_m = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;

    /* Insert members */
    if(H5Tinsert(tid_m2, "c2", HOFFSET(s2, c2), H5T_NATIVE_SHORT) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m2, "l2", HOFFSET(s2, l2), H5T_NATIVE_LONG) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m2, "ll2", HOFFSET(s2, ll2), H5T_NATIVE_LLONG) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "i", HOFFSET(s1, i), H5T_NATIVE_INT) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "st", HOFFSET(s1, st), tid_m2) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "l", HOFFSET(s1, l), H5T_NATIVE_ULLONG) < 0) TEST_ERROR;

    /* Write the data to the dataset */
    if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
        TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;


    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_COMPOUND_NAME_2, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    if(H5Tequal(native_type, tid_m) != TRUE)
        TEST_ERROR;

    /* Verify the datatype of each field retrieved and converted */
    /* check the char member */
    if((mem_id = H5Tget_member_type(native_type, 0)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_SCHAR))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I8LE))
        TEST_ERROR;
    if(H5T_INTEGER != H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* check the integer member */
    if((mem_id = H5Tget_member_type(native_type, 1)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_INT))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I32LE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* check the unsigned long long member */
    if((mem_id = H5Tget_member_type(native_type, 3)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_ULLONG))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U64BE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* check the nested compound member */
    if((nest_mem_id = H5Tget_member_type(native_type, 2)) < 0)
        TEST_ERROR;

    if((mem_id = H5Tget_member_type(nest_mem_id, 0)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_SHORT))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I16BE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    if((mem_id = H5Tget_member_type(nest_mem_id, 1)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LONG))
        TEST_ERROR;
#if H5_SIZEOF_LONG==4
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I32LE)) TEST_ERROR;
#elif H5_SIZEOF_LONG==8
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64LE)) TEST_ERROR;
#else
#error "Unknown 'long' size"
#endif
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    if((mem_id = H5Tget_member_type(nest_mem_id, 2)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LLONG))
        TEST_ERROR;
#if H5_SIZEOF_LONG_LONG==4
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I32LE)) TEST_ERROR;
#elif H5_SIZEOF_LONG_LONG==8
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64LE)) TEST_ERROR;
#else
#error "Unknown 'long long' size"
#endif
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* Read the dataset back.  Temporary buffer is for special platforms like
     * Cray */
    if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
        TEST_ERROR
    if(NULL == (bkg = HDcalloc(sizeof(s1), DIM0 * DIM1)))
        TEST_ERROR;

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
        TEST_ERROR;

    HDmemcpy(check, tmp, DIM0*DIM1*H5Tget_size(native_type));
    HDfree(tmp);
    tmp = NULL;

    if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), check, bkg, H5P_DEFAULT) < 0)
        TEST_ERROR;

    HDfree(bkg);
    bkg = NULL;

    /* Check that the values read are the same as the values written */
    for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++) {
        for(j = 0; j < DIM1; j++, temp_point++, temp_check++) {
            if(temp_point->c != temp_check->c ||
                    temp_point->i != temp_check->i ||
                    temp_point->st.c2 != temp_check->st.c2 ||
                    temp_point->st.l2 != temp_check->st.l2 ||
                    temp_point->st.ll2 != temp_check->st.ll2 ||
                    temp_point->l != temp_check->l ) {
                H5_FAILED();
                printf("    Read different values than written.\n");
                printf("    At index %d,%d\n", i, j);
                goto error;
            } /* end if */
        } /* end for */
    } /* end for */

    /* Close temporary datatypes */
    if(H5Tclose(tid2) < 0) TEST_ERROR;
    if(H5Tclose(tid) < 0) TEST_ERROR;
    if(H5Tclose(tid_m2) < 0) TEST_ERROR;

    /* Close HDF5 objects */
    H5Dclose(dataset);
    H5Tclose(dtype);
    H5Tclose(native_type);
    H5Tclose(tid_m);

    /* Free memory for test data */
    HDfree(points);
    HDfree(check);

    PASSED();
    return 0;

error:
    if(tmp)
        HDfree(tmp);
    if(bkg)
        HDfree(bkg);
    if(points)
        HDfree(points);
    if(check)
        HDfree(check);

    H5E_BEGIN_TRY {
        H5Tclose(tid);
        H5Tclose(tid2);
        H5Tclose(tid_m);
        H5Tclose(tid_m2);
        H5Sclose(space);
        H5Tclose(mem_id);
        H5Tclose(nest_mem_id);
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
    } H5E_END_TRY;

    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	test_compound_dtype
 *
 * Purpose:	Test H5Tget_native_type for compound datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_compound_dtype(hid_t file)
{
    typedef struct {
        char            c;
        unsigned int    i;
        long long       l;
    } s1;
    hid_t	dataset = -1, space = -1;
    hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, mem_id = -1;
    int		i, j, n;
    hsize_t	dims[2];
    s1         *temp_point = NULL;
    s1         *temp_check = NULL;
    s1         *points = NULL;
    s1         *check = NULL;
    void       *tmp = NULL;
    void       *bkg = NULL;

    TESTING("compound datatype");

    /* Allocate space for the points & check arrays */
    if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1)))
        TEST_ERROR;
    if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1)))
        TEST_ERROR;

    /* Initialize the dataset */
    for(i = n = 0, temp_point = points; i < DIM0; i++)
        for(j = 0; j < DIM1; j++, temp_point++) {
            temp_point->c = 't';
            temp_point->i = (unsigned int)(n++);
            temp_point->l = (i * 10 + j * 100) * n;
        } /* end for */

    /* Create the data space */
    dims[0] = DIM0;
    dims[1] = DIM1;
    if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;

    /* Create compound datatype for disk storage */
    if((tid = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;

    /* Insert members */
    if(H5Tinsert(tid, "c", 0, H5T_STD_U8LE) < 0) TEST_ERROR;
    if(H5Tinsert(tid, "i", 1, H5T_STD_U32LE) < 0) TEST_ERROR;
    if(H5Tinsert(tid, "l", 5, H5T_STD_I64BE) < 0) TEST_ERROR;

    /* Create the dataset */
    if((dataset = H5Dcreate2(file, DSET_COMPOUND_NAME, tid, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create compound datatype for datatype in memory */
    if((tid2 = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
    if(H5Tinsert(tid2, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
    if(H5Tinsert(tid2, "i", HOFFSET(s1, i), H5T_NATIVE_UINT) < 0) TEST_ERROR;
    if(H5Tinsert(tid2, "l", HOFFSET(s1, l), H5T_NATIVE_LLONG) < 0) TEST_ERROR;

    /* Write the data to the dataset */
    if(H5Dwrite(dataset, tid2, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
        TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;


    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_COMPOUND_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    if(H5Tequal(native_type, tid2) != TRUE)
        TEST_ERROR;

    /* Verify the datatype of each field retrieved and converted */
    if((mem_id = H5Tget_member_type(native_type, 0)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_UCHAR))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U8LE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    if((mem_id = H5Tget_member_type(native_type, 1)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_UINT))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U32LE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    if((mem_id = H5Tget_member_type(native_type, 2)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LLONG))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64BE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* Read the dataset back.  Temporary buffer is for special platforms like
     * Cray */
    if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
        TEST_ERROR
    if(NULL == (bkg = HDcalloc(sizeof(s1), DIM0 * DIM1)))
        TEST_ERROR

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
        TEST_ERROR;

    HDmemcpy(check, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
    HDfree(tmp);
    tmp = NULL;

    if(H5Tconvert(native_type, tid2, (DIM0 * DIM1), check, bkg, H5P_DEFAULT) < 0)
        TEST_ERROR;

    HDfree(bkg);
    bkg = NULL;

    /* Check that the values read are the same as the values written */
    for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++)
        for(j = 0; j < DIM1; j++, temp_point++, temp_check++)
            if(temp_point->c != temp_check->c ||
                    temp_point->i != temp_check->i ||
                    temp_point->l != temp_check->l ) {
                H5_FAILED();
                printf("    Read different values than written.\n");
                printf("    At index %d,%d\n", i, j);
                goto error;
            } /* end if */

    /* Close datatype */
    if(H5Tclose(tid) < 0) TEST_ERROR;

    H5Dclose(dataset);
    H5Tclose(dtype);
    H5Tclose(native_type);
    H5Tclose(tid2);

    /* Free memory for test data */
    HDfree(points);
    HDfree(check);

    PASSED();
    return 0;

error:
    /* Free memory for test data */
    if(tmp)
        HDfree(tmp);
    if(bkg)
        HDfree(bkg);
    if(points)
        HDfree(points);
    if(check)
        HDfree(check);

    H5E_BEGIN_TRY {
        H5Tclose(tid);
        H5Sclose(space);
        H5Tclose(mem_id);
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(tid2);
    } H5E_END_TRY;

    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	test_compound_dtype3
 *
 * Purpose:	Test H5Tget_native_type for compound datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_compound_dtype3(hid_t file)
{
    typedef struct {
        char            c;
        int             a[5];
        long long       l;
    } s1;
    hid_t	dataset = -1, space = -1;
    hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, tid_m = -1,
                tid_m2 = -1, mem_id = -1, nest_mem_id = -1;
    hsize_t     array_dims[1] = {5};
    int		i, j, k, n;
    hsize_t	dims[2];
    s1         *temp_point = NULL, *temp_check = NULL;
    s1 	       *points = NULL, *check = NULL;
    void       *tmp = NULL, *bkg = NULL;

    TESTING("compound datatype with array as field");

    /* Allocate space for the points & check arrays */
    if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1)))
        TEST_ERROR;
    if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1)))
        TEST_ERROR;

    /* Initialize the dataset */
    for(i = n = 0, temp_point = points; i < DIM0; i++)
        for(j = 0; j < DIM1; j++, temp_point++) {
            temp_point->c = 't';
            temp_point->l = (i * 10 + j * 100) * n;
            for(k = 0; k < 5; k++)
                (temp_point->a)[k] = n++;
        } /* end for */

    /* Create the data space */
    dims[0] = DIM0;
    dims[1] = DIM1;
    if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;

    /* Create array datatype */
    if((tid2 = H5Tarray_create2(H5T_STD_I32LE, 1, array_dims)) < 0) TEST_ERROR;

    /* Create compound datatype for disk storage */
    if((tid = H5Tcreate(H5T_COMPOUND, 29)) < 0) TEST_ERROR;

    /* Insert members */
    if(H5Tinsert(tid, "c", 0, H5T_STD_U8LE) < 0) TEST_ERROR;
    if(H5Tinsert(tid, "a", 1, tid2) < 0) TEST_ERROR;
    if(H5Tinsert(tid, "l", 21, H5T_STD_I64BE) < 0) TEST_ERROR;

    /* Create the dataset */
    if((dataset = H5Dcreate2(file, DSET_COMPOUND_NAME_3, tid, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create array datatype */
    if((tid_m2 = H5Tarray_create2(H5T_NATIVE_INT, 1, array_dims)) < 0) TEST_ERROR;

    /* Create compound datatype for datatype in memory */
    if((tid_m = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "a", HOFFSET(s1, a), tid_m2) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "l", HOFFSET(s1, l), H5T_NATIVE_LLONG) < 0) TEST_ERROR;

    /* Write the data to the dataset */
    if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
        TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid) < 0) TEST_ERROR;
    if(H5Tclose(tid2) < 0) TEST_ERROR;

    /* Close dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;


    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_COMPOUND_NAME_3, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Verify the datatype of each field retrieved and converted */
    /* check the char member */
    if((mem_id = H5Tget_member_type(native_type, 0)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_UCHAR))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U8LE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* check the array member */
    if((mem_id = H5Tget_member_type(native_type, 1)) < 0)
        TEST_ERROR;
    if(H5T_ARRAY!=H5Tget_class(mem_id))
        TEST_ERROR;
    if((nest_mem_id = H5Tget_super(mem_id)) < 0)
        TEST_ERROR;
    if(H5Tget_order(nest_mem_id) != H5Tget_order(H5T_NATIVE_INT))
        TEST_ERROR;
    if(H5Tget_size(nest_mem_id) < H5Tget_size(H5T_STD_I32LE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(nest_mem_id))
        TEST_ERROR;
    H5Tclose(nest_mem_id);
    H5Tclose(mem_id);

    /* check the long long member */
    if((mem_id = H5Tget_member_type(native_type, 2)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LLONG))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64BE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* Read the dataset back.  Temporary buffer is for special platforms like
     * Cray */
    if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
        TEST_ERROR;
    if(NULL == (bkg = HDcalloc(sizeof(s1), DIM0 * DIM1)))
        TEST_ERROR;

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
        TEST_ERROR;

    HDmemcpy(check, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
    HDfree(tmp);
    tmp = NULL;

    if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), check, bkg, H5P_DEFAULT) < 0)
        TEST_ERROR;

    HDfree(bkg);
    bkg = NULL;

    /* Check that the values read are the same as the values written */
    for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++)
        for(j = 0; j < DIM1; j++, temp_point++, temp_check++) {
            if(temp_point->c != temp_check->c ||
                    temp_point->l != temp_check->l ) {
                H5_FAILED();
                printf("    Read different values than written.\n");
                printf("    At index %d,%d\n", i, j);
                goto error;
            } /* end if */

            for(k = 0; k < 5; k++) {
                if(temp_point->a[k] != temp_check->a[k]) {
                    H5_FAILED();
                    printf("    Read different values than written.\n");
                    printf("    At index %d,%d,%d\n", i, j, k);
                    goto error;
                } /* end if */
            } /* end for */
        } /* end for */

    H5Dclose(dataset);
    H5Tclose(dtype);
    H5Tclose(native_type);
    H5Tclose(tid_m);
    H5Tclose(tid_m2);

    /* Free memory for test data */
    HDfree(points);
    HDfree(check);

    PASSED();
    return 0;

error:
    /* Free memory for test data */
    if(tmp)
        HDfree(tmp);
    if(bkg)
        HDfree(bkg);
    if(points)
        HDfree(points);
    if(check)
        HDfree(check);

    H5E_BEGIN_TRY {
        H5Sclose(space);
        H5Tclose(mem_id);
        H5Tclose(nest_mem_id);
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(tid);
        H5Tclose(tid2);
        H5Tclose(tid_m);
        H5Tclose(tid_m2);
    } H5E_END_TRY;

    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	test_compound_opaque
 *
 * Purpose:	Test H5Tget_native_type for compound datatype with opaque field
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Quincey Koziol
 *		January 31, 2004
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_compound_opaque(hid_t file)
{
    typedef struct {
        char            c;
        unsigned char   o[5];
        long long       l;
    } s1;
    hid_t	dataset = -1, space = -1;
    hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, tid_m = -1,
                mem_id = -1;
    int		i, j, k, n;
    hsize_t	dims[2];
    s1         *temp_point = NULL, *temp_check = NULL;
    s1 	       *points = NULL, *check = NULL;
    void       *tmp = NULL, *bkg = NULL;

    TESTING("compound datatype with opaque field");

    /* Allocate space for the points & check arrays */
    if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1)))
        TEST_ERROR;
    if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1)))
        TEST_ERROR;

    /* Initialize the dataset */
    for(i = n = 0, temp_point = points; i < DIM0; i++)
        for(j = 0; j < DIM1; j++, temp_point++) {
            temp_point->c = 't';
            temp_point->l = (i * 10 + j * 100) * n;
            for(k = 0; k < 5; k++)
                (temp_point->o)[k] = (unsigned char)(n++);
        } /* end for */

    /* Create the data space */
    dims[0] = DIM0;
    dims[1] = DIM1;
    if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;

    /* Create opaque datatype */
    if((tid2 = H5Tcreate(H5T_OPAQUE, sizeof(temp_point->o))) < 0) TEST_ERROR;
    if(H5Tset_tag(tid2, "testing opaque field") < 0) TEST_ERROR;

    /* Create compound datatype for disk storage */
    if((tid = H5Tcreate(H5T_COMPOUND, 14)) < 0) TEST_ERROR;

    /* Insert members */
    if(H5Tinsert(tid, "c", 0, H5T_STD_U8LE) < 0) TEST_ERROR;
    if(H5Tinsert(tid, "o", 1, tid2) < 0) TEST_ERROR;
    if(H5Tinsert(tid, "l", 6, H5T_STD_I64BE) < 0) TEST_ERROR;

    /* Create the dataset */
    if((dataset = H5Dcreate2(file, DSET_COMPOUND_NAME_4, tid, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create compound datatype for datatype in memory */
    if((tid_m = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "o", HOFFSET(s1, o), tid2) < 0) TEST_ERROR;
    if(H5Tinsert(tid_m, "l", HOFFSET(s1, l), H5T_NATIVE_LLONG) < 0) TEST_ERROR;

    /* Write the data to the dataset */
    if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
        TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid) < 0) TEST_ERROR;
    if(H5Tclose(tid2) < 0) TEST_ERROR;

    /* Close dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;


    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_COMPOUND_NAME_4, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Verify the datatype of each field retrieved and converted */
    /* check the char member */
    if((mem_id = H5Tget_member_type(native_type, 0)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_UCHAR))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_U8LE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* check the array member */
    if((mem_id = H5Tget_member_type(native_type, 1)) < 0)
        TEST_ERROR;
    if(H5T_OPAQUE!=H5Tget_class(mem_id))
        TEST_ERROR;
    if(H5Tget_size(mem_id) != sizeof(temp_point->o))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* check the long long member */
    if((mem_id = H5Tget_member_type(native_type, 2)) < 0)
        TEST_ERROR;
    if(H5Tget_order(mem_id) != H5Tget_order(H5T_NATIVE_LLONG))
        TEST_ERROR;
    if(H5Tget_size(mem_id) < H5Tget_size(H5T_STD_I64BE))
        TEST_ERROR;
    if(H5T_INTEGER!=H5Tget_class(mem_id))
        TEST_ERROR;
    H5Tclose(mem_id);

    /* Read the dataset back.  Temporary buffer is for special platforms like
     * Cray */
    if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
        TEST_ERROR;
    if(NULL == (bkg = HDcalloc(sizeof(s1), DIM0 * DIM1)))
        TEST_ERROR;

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
        TEST_ERROR;

    HDmemcpy(check, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
    HDfree(tmp);
    tmp = NULL;

    if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), check, bkg, H5P_DEFAULT) < 0)
        TEST_ERROR;

    HDfree(bkg);
    bkg = NULL;

    /* Check that the values read are the same as the values written */
    for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++)
        for(j = 0; j < DIM1; j++, temp_point++, temp_check++) {
            if(temp_point->c != temp_check->c ||
                    temp_point->l != temp_check->l ) {
                H5_FAILED();
                printf("    Read different values than written.\n");
                printf("    At index %d,%d\n", i, j);
                goto error;
            } /* end if */

            for(k = 0; k < 5; k++) {
                if(temp_point->o[k] != temp_check->o[k]) {
                    H5_FAILED();
                    printf("    Read different values than written.\n");
                    printf("    At index %d,%d,%d\n", i, j, k);
                    goto error;
                } /* end if */
            } /* end for */
        } /* end for */

    H5Dclose(dataset);
    H5Tclose(dtype);
    H5Tclose(native_type);
    H5Tclose(tid_m);

    /* Free memory for test data */
    HDfree(points);
    HDfree(check);

    PASSED();
    return 0;

error:
    /* Free memory for test data */
    if(tmp)
        HDfree(tmp);
    if(bkg)
        HDfree(bkg);
    if(points)
        HDfree(points);
    if(check)
        HDfree(check);

    H5E_BEGIN_TRY {
        H5Sclose(space);
        H5Tclose(mem_id);
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(tid);
        H5Tclose(tid2);
        H5Tclose(tid_m);
    } H5E_END_TRY;

    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	test_enum_dtype
 *
 * Purpose:	Test H5Tget_native_type for enumerate datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_enum_dtype(hid_t file)
{
    hid_t	dataset = -1, space = -1;
    hid_t       tid = -1, tid_m = -1, dtype = -1, native_type = -1;
    int		i, j, n;
    hsize_t	dims[2];
    void        *tmp = NULL;
    short        colors[8];
    unsigned char sub_colors[16];
    const char  *mname[] = { "RED",
                            "GREEN",
                            "BLUE",
                            "YELLOW",
                            "PINK",
                            "PURPLE",
                            "ORANGE",
                            "WHITE" };

    TESTING("enum datatype");

    /* Initialize the dataset */
    for(i = 0; i < DIM0; i++)
        for(j = 0, n = 0; j < DIM1; j++, n++)
            spoints2[i][j] = (short)((i * 10 + j * 100 + n) % 8);

    /* Create the data space */
    dims[0] = DIM0;
    dims[1] = DIM1;
    if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR;

    /* Construct enum type based on native type */
    if((tid = H5Tenum_create(H5T_STD_I16LE)) < 0) TEST_ERROR;

    for(i = 0; i < 8; i++) {
        sub_colors[i * 2] = (unsigned char)i;
        sub_colors[i * 2 + 1] = 0;
        if(H5Tenum_insert(tid, mname[i], &(sub_colors[i*2])) < 0) TEST_ERROR;
    } /* end for */

    /* Create the dataset */
    if((dataset = H5Dcreate2(file, DSET_ENUM_NAME, tid, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Construct enum type based on native type in memory */
    if((tid_m = H5Tenum_create(H5T_NATIVE_SHORT)) < 0) TEST_ERROR;

    for(i = 0; i < 8; i++) {
        colors[i] = (short)i;
        if(H5Tenum_insert(tid_m, mname[i], &(colors[i])) < 0) TEST_ERROR;
    } /* end for */

    /* Write the data to the dataset */
    if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, spoints2) < 0)
        TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid) < 0) TEST_ERROR;

    /* Close dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;

    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_ENUM_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Read the dataset back.  Temporary buffer is for special platforms like
     * Cray */
    if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
        TEST_ERROR

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
        TEST_ERROR;

    HDmemcpy(scheck2, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
    HDfree(tmp);
    tmp = NULL;

    if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), scheck2, NULL, H5P_DEFAULT) < 0)
        TEST_ERROR;

    /* Check that the values read are the same as the values written */
    for(i = 0; i < DIM0; i++)
        for(j = 0; j < DIM1; j++)
            if(spoints2[i][j] != scheck2[i][j]) {
                H5_FAILED();
                printf("    Read different values than written.\n");
                printf("    At index %d,%d\n", i, j);
                printf(" spoints2[i][j]=%hd, scheck2[i][j]=%hd\n", spoints2[i][j],
                        scheck2[i][j]);
                goto error;
            } /* end if */

    H5Dclose(dataset);
    H5Tclose(dtype);
    H5Tclose(native_type);
    H5Tclose(tid_m);
    PASSED();
    return 0;

error:
    /* Free memory for test data */
    if(tmp)
        HDfree(tmp);

    H5E_BEGIN_TRY {
        H5Sclose(space);
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(tid);
        H5Tclose(tid_m);
    } H5E_END_TRY;

    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	test_array_dtype
 *
 * Purpose:	Test H5Tget_native_type for array datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_array_dtype(hid_t file)
{
    typedef struct {
        char    c;
        int     i;
        long long l;
    } s1;
    hid_t	dataset = -1, space = -1;
    hid_t       dtype = -1, native_type = -1, tid = -1, tid2 = -1, tid3 = -1, tid_m = -1;
    int		i, j, k, n;
    hsize_t	space_dims[2], array_dims[1]={5};
    s1         *temp_point = NULL, *temp_check = NULL;
    s1 	       *points = NULL, *check = NULL;
    void       *tmp = NULL;

    TESTING("array of compound datatype");

    /* Allocate space for the points & check arrays */
    if(NULL == (points = (s1 *)HDmalloc(sizeof(s1) * DIM0 * DIM1 * 5)))
        TEST_ERROR;
    if(NULL == (check = (s1 *)HDcalloc(sizeof(s1), DIM0 * DIM1 * 5)))
        TEST_ERROR;

    /* Initialize the dataset */
    for(i = n = 0, temp_point = points; i < DIM0; i++)
        for(j = 0; j < DIM1; j++)
            for(k = 0; k < 5; k++,temp_point++) {
                temp_point->c= 't';
                temp_point->i= n++;
                temp_point->l= (i * 10 + j * 100) * n;
            } /* end for */

    /* Create the data space */
    space_dims[0] = DIM0;
    space_dims[1] = DIM1;
    if((space = H5Screate_simple(2, space_dims, NULL)) < 0) TEST_ERROR;

    /* Create compound datatype for disk storage */
    if((tid2 = H5Tcreate(H5T_COMPOUND, 13)) < 0) TEST_ERROR;

    /* Insert members */
    if(H5Tinsert(tid2, "c", 0, H5T_STD_U8BE) < 0) TEST_ERROR;
    if(H5Tinsert(tid2, "i", 1, H5T_STD_U32LE) < 0) TEST_ERROR;
    if(H5Tinsert(tid2, "l", 5, H5T_STD_I64BE) < 0) TEST_ERROR;

    /* Create array datatype for disk storage */
    if((tid = H5Tarray_create2(tid2, 1, array_dims)) < 0) TEST_ERROR;

    /* Create the dataset */
    if((dataset = H5Dcreate2(file, DSET_ARRAY_NAME, tid, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create compound datatype for datatype in memory */
    if((tid3 = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) TEST_ERROR;
    if(H5Tinsert(tid3, "c", HOFFSET(s1, c), H5T_NATIVE_UCHAR) < 0) TEST_ERROR;
    if(H5Tinsert(tid3, "i", HOFFSET(s1, i), H5T_NATIVE_UINT) < 0) TEST_ERROR;
    if(H5Tinsert(tid3, "l", HOFFSET(s1, l), H5T_NATIVE_LLONG) < 0) TEST_ERROR;

    /* Create array datatype for memory */
    if((tid_m = H5Tarray_create2(tid3, 1, array_dims)) < 0) TEST_ERROR;

    /* Write the data to the dataset */
    if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0)
        TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid) < 0) TEST_ERROR;
    if(H5Tclose(tid2) < 0) TEST_ERROR;

    /* Close dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;

    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_ARRAY_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Read the dataset back. Temporary buffer is for special platforms like
     * Cray */
    if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
        TEST_ERROR

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
        TEST_ERROR;

    HDmemcpy(check, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
    HDfree(tmp);
    tmp = NULL;

    if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), check, NULL, H5P_DEFAULT) < 0)
        TEST_ERROR;

    /* Check that the values read are the same as the values written */
    for(i = 0, temp_point = points, temp_check = check; i < DIM0; i++)
        for(j = 0; j < DIM1; j++)
            for(k = 0; k < 5; k++, temp_point++, temp_check++)
                if(temp_point->c != temp_check->c ||
                        temp_point->i != temp_check->i ||
                        temp_point->l != temp_check->l ) {
                    H5_FAILED();
                    printf("    Read different values than written.\n");
                    printf("    At index %d,%d\n", i, j);
                    goto error;
                } /* end if */

    /* Close HDF5 objects */
    if(H5Dclose(dataset)) TEST_ERROR;
    if(H5Tclose(native_type)) TEST_ERROR;
    if(H5Tclose(dtype)) TEST_ERROR;
    if(H5Tclose(tid_m) < 0) TEST_ERROR;
    if(H5Tclose(tid3) < 0) TEST_ERROR;

    /* Free memory for test data */
    HDfree(points);
    HDfree(check);

    PASSED();
    return 0;

error:
    /* Free memory for test data */
    if(tmp)
        HDfree(tmp);
    if(points)
        HDfree(points);
    if(check)
        HDfree(check);

    H5E_BEGIN_TRY {
        H5Sclose(space);
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(tid);
        H5Tclose(tid2);
        H5Tclose(tid3);
        H5Tclose(tid_m);
    } H5E_END_TRY;

    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	test_array_dtype2
 *
 * Purpose:	Test H5Tget_native_type for array datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_array_dtype2(hid_t file)
{
    hid_t	dataset = -1, space = -1;
    hid_t       dtype = -1, native_type = -1, tid = -1, tid_m = -1;
    int		i, j, k, n;
    hsize_t	space_dims[2], array_dims[1] = {5};
    void       *tmp = NULL;

    TESTING("array of atomic datatype");

    /* Initialize the dataset */
    for(i = n = 0;i < DIM0; i++)
        for(j = 0; j < DIM1; j++)
            for(k = 0; k < 5; k++)
                ipoints3[i][j][k] = n++;

    /* Create the data space */
    space_dims[0] = DIM0;
    space_dims[1] = DIM1;
    if((space = H5Screate_simple(2, space_dims, NULL)) < 0) TEST_ERROR;

    /* Create array datatype for disk storage */
    if((tid = H5Tarray_create2(H5T_STD_I32LE, 1, array_dims)) < 0) TEST_ERROR;

    /* Create the dataset */
    if((dataset = H5Dcreate2(file, DSET_ARRAY2_NAME, tid, space,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create array datatype for memory */
    if((tid_m = H5Tarray_create2(H5T_NATIVE_INT, 1, array_dims)) < 0) TEST_ERROR;

    /* Write the data to the dataset */
    if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, ipoints3) < 0)
        TEST_ERROR;

    /* Close dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid) < 0) TEST_ERROR;

    /* Close dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;

    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_ARRAY2_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Read the dataset back.  Temporary buffer is for special platforms like
     * Cray */
    if(NULL == (tmp = HDmalloc(DIM0 * DIM1 * H5Tget_size(native_type))))
        TEST_ERROR

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
        TEST_ERROR;

    HDmemcpy(icheck3, tmp, DIM0 * DIM1 * H5Tget_size(native_type));
    HDfree(tmp);
    tmp = NULL;

    if(H5Tconvert(native_type, tid_m, (DIM0 * DIM1), icheck3, NULL, H5P_DEFAULT) < 0)
        TEST_ERROR;

    /* Check that the values read are the same as the values written */
    for(i = 0; i < DIM0; i++)
        for(j = 0; j < DIM1; j++)
            for(k = 0; k < 5; k++)
                if(icheck3[i][j][k] != ipoints3[i][j][k]) {
                    H5_FAILED();
                    printf("    Read different values than written.\n");
                    printf("    At index %d,%d\n", i, j);
                    goto error;
                } /* end if */

    /* Close HDF5 objects */
    if(H5Dclose(dataset)) TEST_ERROR;
    if(H5Tclose(native_type)) TEST_ERROR;
    if(H5Tclose(dtype)) TEST_ERROR;
    if(H5Tclose(tid_m) < 0) TEST_ERROR;

    PASSED();
    return 0;

error:
    /* Free memory for test data */
    if(tmp)
        HDfree(tmp);

    H5E_BEGIN_TRY {
        H5Sclose(space);
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(tid);
        H5Tclose(tid_m);
    } H5E_END_TRY;

    return -1;
}


/*-------------------------------------------------------------------------
 * Function:	test_vl_dtype
 *
 * Purpose:	Test H5Tget_native_type for variable length datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_vl_dtype(hid_t file)
{
    hvl_t       wdata[SPACE1_DIM1];   /* Information to write */
    hvl_t       rdata[SPACE1_DIM1];   /* Information read in */
    hvl_t       *t1, *t2;             /* Temporary pointer to VL information */
    hsize_t	dims1[] = {SPACE1_DIM1};
    hid_t	dataset = -1, space = -1;
    hid_t       dtype = -1, native_type = -1, nat_super_type = -1, tid = -1, tid2 = -1, tid_m = -1, tid_m2 = -1;
    size_t	i, j, k;
    void      **tmp = NULL;

    TESTING("variable length datatype");

    /* Allocate and initialize VL data to write */
    for(i = 0; i < SPACE1_DIM1; i++) {
        wdata[i].p = HDmalloc((i + 1) * sizeof(hvl_t));
        if(NULL == wdata[i].p) {
            H5_FAILED();
            printf("    Cannot allocate memory for VL data! i=%u\n",(unsigned)i);
            goto error;
        } /* end if */
        wdata[i].len = i + 1;
        for(t1 = (hvl_t *)wdata[i].p, j = 0; j < (i + 1); j++, t1++) {
            t1->p = HDmalloc((j + 1) * sizeof(unsigned int));
            if(NULL == t1->p) {
                H5_FAILED();
                printf("    Cannot allocate memory for VL data! i=%u, j=%u\n",(unsigned)i,(unsigned)j);
                goto error;
            } /* end if */
            t1->len = j + 1;
            for(k = 0; k < (j + 1); k++)
                ((unsigned int *)t1->p)[k] = (unsigned int)(i * 100 + j * 10 + k);
        } /* end for */
    } /* end for */

    /* Create dataspace for datasets */
    if((space = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0) TEST_ERROR;

    /* Create the base VL type */
    if((tid2 = H5Tvlen_create (H5T_STD_U32LE)) < 0) TEST_ERROR;

    /* Create a VL datatype for disk storage */
    if((tid = H5Tvlen_create(tid2)) < 0) TEST_ERROR

    /* Create a dataset */
    if((dataset = H5Dcreate2(file, DSET_VL_NAME, tid, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create a base VL datatype for memory */
    if((tid_m2 = H5Tvlen_create(H5T_NATIVE_UINT)) < 0) TEST_ERROR;

    /* Create a VL datatype for memory */
    if((tid_m = H5Tvlen_create (tid_m2)) < 0) TEST_ERROR;

    /* Write dataset to disk */
    if(H5Dwrite(dataset, tid_m, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata) < 0) TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid2) < 0) TEST_ERROR;
    if(H5Tclose(tid) < 0) TEST_ERROR;

    /* Open a dataset */
    if((dataset = H5Dopen2(file, DSET_VL_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    /* Get native datatype for dataset */
    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Also get native base type for this nested VL type.  Should be an integer type. */
    if((nat_super_type = H5Tget_super(native_type)) < 0)
        TEST_ERROR;
    if((nat_super_type = H5Tget_super(nat_super_type)) < 0)
        TEST_ERROR;

    /* Read dataset from disk */
    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata) < 0) TEST_ERROR;

    /* Compare data read in */
    for(i = 0; i < SPACE1_DIM1; i++) {
        if(wdata[i].len != rdata[i].len) {
            H5_FAILED();
            printf("    VL data length don't match!, wdata[%d].len=%d, rdata[%d].len=%d\n",(int)i,(int)wdata[i].len,(int)i,(int)rdata[i].len);
            goto error;
        } /* end if */
        for(t1 = (hvl_t *)wdata[i].p, t2 = (hvl_t *)rdata[i].p, j = 0; j < rdata[i].len; j++, t1++, t2++) {
            if(t1->len != t2->len) {
                H5_FAILED();
                printf("    VL data length don't match!, wdata[%d].len=%d, rdata[%d].len=%d\n",(int)i,(int)wdata[i].len,(int)i,(int)rdata[i].len);
                goto error;
            } /* end if */

            /* use temporary buffer to convert datatype.  This is for special
             * platforms like Cray */
            if(NULL == (tmp = (void **)HDmalloc(t2->len * sizeof(unsigned int))))
                TEST_ERROR
            HDmemcpy(tmp, t2->p, t2->len * H5Tget_size(nat_super_type));

            if(H5Tconvert(nat_super_type, H5T_NATIVE_UINT, t2->len, tmp, NULL, H5P_DEFAULT) < 0)
                TEST_ERROR;

            for(k = 0; k < t2->len; k++) {
                if(((unsigned int *)t1->p)[k] != ((unsigned int *)tmp)[k]) {
                    H5_FAILED();
                    printf("    VL data don't match!, wdata[%u].p=%d, rdata[%u].p=%u\n",
                            (unsigned)i, ((unsigned int*)t1->p)[k], (unsigned)i, ((unsigned int*)tmp)[k]);
                    goto error;
                } /* end if */
            } /* end for */

            HDfree(tmp);
            tmp = NULL;
        } /* end for */
    } /* end for */

    /* Reclaim the read VL data */
    if(H5Dvlen_reclaim(native_type, space, H5P_DEFAULT, rdata) < 0) TEST_ERROR;

    /* Reclaim the write VL data */
    if(H5Dvlen_reclaim(native_type, space, H5P_DEFAULT, wdata) < 0) TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    native_type = -1;     /* reset so that error handling can check for VL reclaim */
    if(H5Tclose(dtype) < 0) TEST_ERROR;
    if(H5Tclose(tid_m) < 0) TEST_ERROR;
    if(H5Tclose(tid_m2) < 0) TEST_ERROR;


    /* Close disk dataspace */
    if(H5Sclose(space) < 0) TEST_ERROR;

    PASSED();
    return 0;

error:
    /* Free memory for test data */
    if(tmp)
        HDfree(tmp);

    H5E_BEGIN_TRY {
        if(native_type > 0) {
            H5Dvlen_reclaim(native_type, space, H5P_DEFAULT, rdata);
            H5Dvlen_reclaim(native_type, space, H5P_DEFAULT, wdata);
        } /* end if */
        H5Sclose(space);
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(nat_super_type);
        H5Tclose(tid);
        H5Tclose(tid2);
        H5Tclose(tid_m);
        H5Tclose(tid_m2);
    } H5E_END_TRY;

    return -1;
} /* end test_vl_type() */


/*-------------------------------------------------------------------------
 * Function:	test_vlstr_dtype
 *
 * Purpose:	Test H5Tget_native_type for variable length string datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_vlstr_dtype(hid_t file)
{
    const char *wdata[SPACE1_DIM1]= {
            "Four score and seven years ago our forefathers brought forth on this continent a new nation,",
            "conceived in liberty and dedicated to the proposition that all men are created equal.",
            "Now we are engaged in a great civil war,",
            "testing whether that nation or any nation so conceived and so dedicated can long endure."
    };                                  /* Information to write */
    char       *rdata[SPACE1_DIM1];     /* Information read in */
    hbool_t     rdata_alloc = FALSE;    /* Whether the read data is allocated */
    hid_t	dataset = -1;	        /* Dataset ID			*/
    hid_t	sid1 = -1;              /* Dataspace ID			*/
    hid_t	tid1 = -1, dtype = -1, native_type = -1;     /* Datatype ID			*/
    hsize_t	dims1[] = {SPACE1_DIM1};
    unsigned    i;                          /* counting variable */

    /* Output message about test being performed */
    TESTING("variable length string datatype");

    /* Create dataspace for datasets */
    if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0) TEST_ERROR;

    /* Create a datatype to refer to */
    if((tid1 = H5Tcopy(H5T_C_S1)) < 0) TEST_ERROR;

    if(H5Tset_size(tid1,H5T_VARIABLE) < 0) TEST_ERROR;
    if(H5T_STRING != H5Tget_class(tid1) || !H5Tis_variable_str(tid1))
        TEST_ERROR;

    /* Create a dataset */
    if((dataset = H5Dcreate2(file, DSET_VLSTR_NAME, tid1, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR;

    /* Write dataset to disk */
    if(H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata) < 0) TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Open a dataset */
    if((dataset = H5Dopen2(file, DSET_VLSTR_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    /* Get datatype for dataset */
    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    /* Construct native type */
    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Check if the data type is equal */
    if(!H5Tequal(native_type, tid1))
        TEST_ERROR;

    /* Read dataset from disk */
    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata) < 0) TEST_ERROR;
    rdata_alloc = TRUE;

    /* Compare data read in */
    for(i = 0; i < SPACE1_DIM1; i++) {
        if(HDstrlen(wdata[i]) != HDstrlen(rdata[i])) {
            H5_FAILED();
            printf("    VL data length don't match!, strlen(wdata[%d])=%d, strlen(rdata[%d])=%d\n",
                    (int)i, (int)HDstrlen(wdata[i]), (int)i, (int)HDstrlen(rdata[i]));
            goto error;
        } /* end if */
        if(HDstrcmp(wdata[i], rdata[i]) != 0 ) {
            H5_FAILED();
            printf("    VL data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n",
                    (int)i, wdata[i], (int)i, rdata[i]);
            goto error;
        } /* end if */
    } /* end for */

    /* Close Dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid1) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;

    /* Close disk dataspace */
    if(H5Sclose(sid1) < 0) TEST_ERROR;

    /* Free memory for rdata */
    for(i = 0; i < SPACE1_DIM1; i++)
        HDfree(rdata[i]);
    rdata_alloc = FALSE;

    PASSED();
    return 0;

error:
    if(rdata_alloc) {
        /* Free memory for rdata */
        for(i = 0; i < SPACE1_DIM1; i++)
            HDfree(rdata[i]);
    } /* end if */

    H5E_BEGIN_TRY {
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(sid1);
        H5Tclose(tid1);
    } H5E_END_TRY;

    return -1;
} /* end test_vlstr_dtype() */


/*-------------------------------------------------------------------------
 * Function:	test_str_dtype
 *
 * Purpose:	Test H5Tget_native_type for fixed-length string datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_str_dtype(hid_t file)
{
    const char  wdata[SPACE1_DIM1][4]= {
            "one",
            "two",
            "3rd",
            "4th"
    };                              /* Information to write */
    char        rdata[SPACE1_DIM1][4];  /* Information read in */
    hid_t	dataset = -1;	    /* Dataset ID			*/
    hid_t	sid1 = -1;          /* Dataspace ID			*/
    hid_t	tid1 = -1, dtype = -1, native_type = -1; /* Datatype ID			*/
    hsize_t	dims1[] = {SPACE1_DIM1};
    unsigned    i;                      /* counting variable */

    /* Output message about test being performed */
    TESTING("fixed-length string datatype");

    /* Create dataspace for datasets */
    if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0) TEST_ERROR;

    /* Create a datatype to refer to */
    if((tid1 = H5Tcopy (H5T_C_S1)) < 0) TEST_ERROR;

    if(H5Tset_size(tid1, 4) < 0) TEST_ERROR;
    if(H5T_STRING != H5Tget_class(tid1)) TEST_ERROR;

    /* Create a dataset */
    if((dataset = H5Dcreate2(file, DSET_STR_NAME, tid1, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR;

    /* Write dataset to disk */
    if(H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata) < 0) TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Open a dataset */
    if((dataset = H5Dopen2(file, DSET_STR_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    /* Get datatype for dataset */
    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    /* Construct native type */
    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Check if the data type is equal */
    if(!H5Tequal(native_type, tid1) || H5T_STRING!=H5Tget_class(native_type))
        TEST_ERROR;

    /* Read dataset from disk */
    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata) < 0) TEST_ERROR;

    /* Compare data read in */
    for(i = 0; i < SPACE1_DIM1; i++) {
        if(HDstrlen(wdata[i]) != HDstrlen(rdata[i])) {
            H5_FAILED();
            printf("    data length don't match!, strlen(wdata[%d])=%d, strlen(rdata[%d])=%d\n",
                    (int)i, (int)HDstrlen(wdata[i]), (int)i, (int)HDstrlen(rdata[i]));
            goto error;
        } /* end if */
        if(HDstrcmp(wdata[i], rdata[i]) != 0 ) {
            H5_FAILED();
            printf("    data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n",
                    (int)i, wdata[i], (int)i, rdata[i]);
            goto error;
        } /* end if */
    } /* end for */

    /* Close Dataset */
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid1) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;

    /* Close disk dataspace */
    if(H5Sclose(sid1) < 0) TEST_ERROR;

    PASSED();
    return 0;

error:
    H5E_BEGIN_TRY {
        H5Dclose(dataset);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Tclose(tid1);
        H5Tclose(sid1);
    } H5E_END_TRY;

    return -1;
} /* end test_str_dtype() */


/*-------------------------------------------------------------------------
 * Function:	test_refer_dtype
 *
 * Purpose:	Test H5Tget_native_type for reference datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_refer_dtype(hid_t file)
{
    /* Compound datatype */
    typedef struct s1_t {
        unsigned int a;
        unsigned int b;
        float c;
    } s1_t;

    hid_t	dataset = -1;   /* Dataset ID			*/
    hid_t	group = -1;     /* Group ID             */
    hid_t	sid1 = -1;      /* Dataspace ID			*/
    hid_t	tid1 = -1, dtype = -1, native_type = -1;       /* Datatype ID	*/
    hsize_t	dims1[] = {1};
    H5O_type_t  obj_type;       /* Object type */
    hobj_ref_t *wbuf = NULL,    /* buffer to write to disk */
               *rbuf = NULL;    /* buffer read from disk */

    /* Output message about test being performed */
    TESTING("reference datatype");

    /* Allocate write & read buffers */
    if(NULL == (wbuf = (hobj_ref_t *)HDmalloc(MAX(sizeof(unsigned), sizeof(hobj_ref_t)))))
        TEST_ERROR
    if(NULL == (rbuf = (hobj_ref_t *)HDmalloc(MAX(sizeof(unsigned), sizeof(hobj_ref_t)))))
        TEST_ERROR

    /* Create dataspace for datasets */
    if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0)
        TEST_ERROR;

    /* Create a group */
    if((group = H5Gcreate2(file, "Group1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR

    /* Create a datatype to refer to */
    if((tid1 = H5Tcreate (H5T_COMPOUND, sizeof(s1_t))) < 0)
        TEST_ERROR;

    /* Insert fields */
    if(H5Tinsert (tid1, "a", HOFFSET(s1_t,a), H5T_NATIVE_INT) < 0)
        TEST_ERROR;

    if(H5Tinsert (tid1, "b", HOFFSET(s1_t, b), H5T_NATIVE_INT) < 0)
        TEST_ERROR;

    if(H5Tinsert(tid1, "c", HOFFSET(s1_t, c), H5T_NATIVE_FLOAT) < 0)
        TEST_ERROR;

    /* Save datatype for later */
    if(H5Tcommit2(group, "Datatype1", tid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT) < 0)
        TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid1) < 0)
        TEST_ERROR;

    /* Close group */
    if(H5Gclose(group) < 0)
        TEST_ERROR;

    /* Create a dataset */
    if((dataset = H5Dcreate2(file, "Dataset3", H5T_STD_REF_OBJ, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create reference to named datatype */
    if(H5Rcreate(wbuf, file, "/Group1/Datatype1", H5R_OBJECT, (hid_t)-1) < 0)
        TEST_ERROR;
    if(H5Rget_obj_type2(dataset, H5R_OBJECT, wbuf, &obj_type) < 0)
        TEST_ERROR;
    if(obj_type != H5O_TYPE_NAMED_DATATYPE)
        TEST_ERROR;

    /* Write selection to disk */
    if(H5Dwrite(dataset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf) < 0)
        TEST_ERROR;

    /* Close disk dataspace */
    if(H5Sclose(sid1) < 0)
        TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dataset) < 0)
        TEST_ERROR;

    /* Open the dataset */
    if((dataset = H5Dopen2(file, "/Dataset3", H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Get datatype for dataset */
    if((dtype = H5Dget_type(dataset)) < 0)
        TEST_ERROR;

    /* Construct native type */
    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Check if the data type is equal */
    if(!H5Tequal(native_type, H5T_STD_REF_OBJ))
        TEST_ERROR;

    /* Read selection from disk */
    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rbuf) < 0)
        TEST_ERROR;

    /* Open datatype object */
    if((tid1 = H5Rdereference2(dataset, H5P_DEFAULT, H5R_OBJECT, rbuf)) < 0)
        TEST_ERROR;

    /* Verify correct datatype */
    if(H5Tget_class(tid1) != H5T_COMPOUND)
        TEST_ERROR;

    if(H5Tget_nmembers(tid1)!=3)
        TEST_ERROR;

    /* Close datatype */
    if(H5Tclose(tid1) < 0)
        TEST_ERROR;

    if(H5Tclose(native_type) < 0)
        TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dataset) < 0)
        TEST_ERROR;

    /* Free memory buffers */
    HDfree(wbuf);
    HDfree(rbuf);

    PASSED();

    return 0;

error:
    if(wbuf)
        HDfree(wbuf);
    if(rbuf)
        HDfree(rbuf);

    H5E_BEGIN_TRY {
        H5Sclose(sid1);
        H5Gclose(group);
        H5Tclose(tid1);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Dclose(dataset);
    } H5E_END_TRY;

    return -1;
}   /* test_refer_dtype() */


/*-------------------------------------------------------------------------
 * Function:	test_refer_dtype2
 *
 * Purpose:	Test H5Tget_native_type for reference
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_refer_dtype2(hid_t file)
{
    hid_t           dset1 = -1,      /* Dataset ID			*/
                    dset2 = -1;      /* Dereferenced dataset ID */
    hid_t           sid1 = -1,       /* Dataspace ID	#1		*/
                    sid2 = -1;       /* Dataspace ID	#2		*/
    hid_t           dtype = -1, native_type = -1;
    hsize_t         dims1[] = { 1 }, dims2[] = { SPACE2_DIM1, SPACE2_DIM2 };
    hsize_t         start[SPACE2_RANK];     /* Starting location of hyperslab */
    hsize_t         stride[SPACE2_RANK];    /* Stride of hyperslab */
    hsize_t         count[SPACE2_RANK];     /* Element count of hyperslab */
    hsize_t         block[SPACE2_RANK];     /* Block size of hyperslab */
    hdset_reg_ref_t wbuf,       /* buffer to write to disk */
                    rbuf;       /* buffer read from disk */
    uint8_t        *dwbuf = NULL, /* Buffer for writing numeric data to disk */
                    *drbuf = NULL; /* Buffer for reading numeric data from disk */
    uint8_t        *tu8 = NULL; /* Temporary pointer to uint8 data */
    H5O_type_t      obj_type;   /* Object type */
    int             i;          /* counting variables */

    /* Output message about test being performed */
    TESTING("dataset region reference");

    /* Allocate write & read buffers */
    if(NULL == (dwbuf = (uint8_t *)HDmalloc(sizeof(uint8_t) * SPACE2_DIM1 * SPACE2_DIM2)))
        TEST_ERROR
    if(NULL == (drbuf = (uint8_t *)HDcalloc(sizeof(uint8_t), SPACE2_DIM1 * SPACE2_DIM2)))
        TEST_ERROR

    /* Create dataspace for datasets */
    if((sid2 = H5Screate_simple(SPACE2_RANK, dims2, NULL)) < 0)
        TEST_ERROR;

    /* Create a dataset */
    if((dset2 = H5Dcreate2(file, "Dataset2", H5T_STD_U8LE, sid2, H5P_DEFAULT,
            H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    for(tu8 = dwbuf, i = 0; i < SPACE2_DIM1 * SPACE2_DIM2; i++)
        *tu8++ = (uint8_t)(i * 3);

    /* Write selection to disk */
    if(H5Dwrite(dset2, H5T_STD_U8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, dwbuf) < 0)
        TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dset2) < 0)
        TEST_ERROR;

    /* Create dataspace for the reference dataset */
    if((sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL)) < 0)
        TEST_ERROR;

    /* Create a reference dataset */
    if((dset1 = H5Dcreate2(file, "Dataset1", H5T_STD_REF_DSETREG, sid1,
            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create references */
    /* Select 6x6 hyperslab for first reference */
    start[0] = 2;
    start[1] = 2;
    stride[0] = 1;
    stride[1] = 1;
    count[0] = 1;
    count[1] = 1;
    block[0] = 6;
    block[1] = 6;

    if(H5Sselect_hyperslab(sid2, H5S_SELECT_SET, start, stride, count, block) < 0)
        TEST_ERROR;

    if((int)H5Sget_select_npoints(sid2) != 36)
        TEST_ERROR;

    /* Store first dataset region */
    if(H5Rcreate(&wbuf, file, "/Dataset2", H5R_DATASET_REGION, sid2) < 0)
        TEST_ERROR;
    if(H5Rget_obj_type2(dset1, H5R_DATASET_REGION, &wbuf, &obj_type) < 0)
        TEST_ERROR;
    if(obj_type != H5O_TYPE_DATASET)
        TEST_ERROR;

    /* Write selection to disk */
    if(H5Dwrite(dset1, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, &wbuf) < 0)
        TEST_ERROR;

    /* Close disk dataspace */
    if(H5Sclose(sid1) < 0)
        TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dset1) < 0)
        TEST_ERROR;

    /* Close uint8 dataset dataspace */
    if(H5Sclose(sid2) < 0)
        TEST_ERROR;

    /* Open the dataset */
    if((dset1 = H5Dopen2(file, "/Dataset1", H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Get datatype for dataset */
    if((dtype = H5Dget_type(dset1)) < 0)
        TEST_ERROR;

    /* Construct native type */
    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    /* Check if the data type is equal */
    if(!H5Tequal(native_type, H5T_STD_REF_DSETREG))
        TEST_ERROR;

    /* Read selection from disk */
    if(H5Dread(dset1, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, &rbuf) < 0)
        TEST_ERROR;

    /* Try to open objects */
    if((dset2 = H5Rdereference2(dset1, H5P_DEFAULT, H5R_DATASET_REGION, &rbuf)) < 0)
        TEST_ERROR;

    /* Check what H5Rget_obj_type2 function returns */
    if(H5Rget_obj_type2(dset1, H5R_DATASET_REGION, &rbuf, &obj_type) < 0)
        TEST_ERROR;
    if(obj_type != H5O_TYPE_DATASET)
        TEST_ERROR;

    /* Check information in referenced dataset */
    if((sid1 = H5Dget_space(dset2)) < 0)
        TEST_ERROR;

    if((int)H5Sget_simple_extent_npoints(sid1) != 100)
        TEST_ERROR;

    /* Read from disk */
    if(H5Dread(dset2, H5T_STD_U8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, drbuf) < 0)
        TEST_ERROR;

    for(tu8 = (uint8_t *)drbuf, i = 0; i < (SPACE2_DIM1 * SPACE2_DIM2); i++, tu8++)
        if(*tu8 != (uint8_t)(i * 3))
            TEST_ERROR;

    /* Get the hyperslab selection */
    if((sid2 = H5Rget_region(dset1, H5R_DATASET_REGION, &rbuf)) < 0)
        TEST_ERROR;

    /* Verify correct hyperslab selected */
    if((int)H5Sget_select_npoints(sid2) != 36)
        TEST_ERROR;
    if((int)H5Sget_select_hyper_nblocks(sid2) != 1)
        TEST_ERROR;

    /* Close region space */
    if(H5Sclose(sid2) < 0)
        TEST_ERROR;

    /* Close first space */
    if(H5Sclose(sid1) < 0)
        TEST_ERROR;

    /* Close dereferenced Dataset */
    if(H5Dclose(dset2) < 0)
        TEST_ERROR;

    /* Close Dataset */
    if(H5Dclose(dset1) < 0)
        TEST_ERROR;

    /* Free memory buffers */
    HDfree(dwbuf);
    HDfree(drbuf);

    PASSED();
    return 0;

error:
    /* Free memory buffers */
    if(dwbuf)
        HDfree(dwbuf);
    if(drbuf)
        HDfree(drbuf);

    H5E_BEGIN_TRY {
        H5Sclose(sid2);
        H5Sclose(sid1);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Dclose(dset2);
        H5Dclose(dset1);
    } H5E_END_TRY;

    return -1;
}   /* test_refer_dtype2() */


/*-------------------------------------------------------------------------
 * Function:	test_opaque_dtype
 *
 * Purpose:	Test H5Tget_native_type for opaque datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_opaque_dtype(hid_t file)
{
    hid_t	     type = -1, space = -1, dset = -1;
    hid_t            dataset = -1, dtype = -1, native_type = -1;
    size_t           i;
    unsigned char    wbuf[32], rbuf[32];
    hsize_t	     nelmts;

    TESTING("opaque datatype");

    /* opaque_1 */
    nelmts = sizeof(wbuf);
    if((type = H5Tcreate(H5T_OPAQUE, 1)) < 0) TEST_ERROR;
    if(H5Tset_tag(type, "testing 1-byte opaque type") < 0) TEST_ERROR;
    if((space = H5Screate_simple(1, &nelmts, NULL)) < 0) TEST_ERROR;
    if((dset = H5Dcreate2(file, DSET_OPAQUE_NAME, type, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    for(i = 0; i < sizeof(wbuf); i++)
        wbuf[i] = (unsigned char)0xff ^ (unsigned char)i;

    if(H5Dwrite(dset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf) < 0) TEST_ERROR;
    if(H5Sclose(space) < 0) TEST_ERROR;
    if(H5Dclose(dset) < 0) TEST_ERROR;


    /* Open dataset again to check H5Tget_native_type */
    if((dataset = H5Dopen2(file, DSET_OPAQUE_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0)
        TEST_ERROR;

    if(!H5Tequal(native_type, type)) TEST_ERROR;

    if(H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rbuf) < 0)
        TEST_ERROR;

    for(i = 0; i < sizeof(rbuf); i++)
        if(rbuf[i] != wbuf[i]) {
            H5_FAILED();
            printf("    Read different values than written.\n");
            printf("    At index %u\n", (unsigned)i);
            goto error;
        } /* end if */

    if(H5Tclose(type) < 0) TEST_ERROR;
    if(H5Tclose(dtype) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    if(H5Dclose(dataset) < 0) TEST_ERROR;

    PASSED();
    return 0;

error:
    H5E_BEGIN_TRY {
        H5Sclose(space);
        H5Tclose(type);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Dclose(dset);
        H5Dclose(dataset);
    } H5E_END_TRY;

    return -1;
} /* test_opaque_dtype */


/*-------------------------------------------------------------------------
 * Function:	test_bitfield_dtype
 *
 * Purpose:	Test H5Tget_native_type for bitfield datatype
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *              Raymond Lu
 *              1 December 2009
 *              I added the support for bitfield and changed the test to
 *              compare the data being read back.
 *-------------------------------------------------------------------------
 */
static herr_t
test_bitfield_dtype(hid_t file)
{
    hid_t		type=-1, space=-1, dset1=-1, dset2=-1;
    hid_t               dataset1=-1, dataset2=-1, dtype=-1, native_type=-1;
    size_t		ntype_size, i;
    unsigned char  	wbuf[BITFIELD_ENUMB*sizeof(int)];
    unsigned char       *p=NULL;
    void                *rbuf = NULL;
    unsigned int        intw[BITFIELD_ENUMB], intr[BITFIELD_ENUMB];
    hsize_t		nelmts;

    TESTING("bitfield datatype");

    nelmts = BITFIELD_ENUMB;
    if((type = H5Tcopy(H5T_STD_B32BE)) < 0) TEST_ERROR;

    if((space = H5Screate_simple(1, &nelmts, NULL)) < 0) TEST_ERROR;

    /* Create and write to dataset1 with a unsigned char buffer */
    if((dset1 = H5Dcreate2(file, DSET1_BITFIELD_NAME, type, space, H5P_DEFAULT, H5P_DEFAULT,
        H5P_DEFAULT)) < 0) TEST_ERROR;

    for(i = 0; i < BITFIELD_ENUMB*sizeof(int); i++)
        wbuf[i] = (unsigned char)((unsigned int)0xff ^ (unsigned int)i);

    if(H5Dwrite(dset1, H5T_NATIVE_B32, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf) < 0) TEST_ERROR;
    if(H5Dclose(dset1) < 0) TEST_ERROR;

    /* Create and write to dataset2 with a unsigned int buffer */
    if((dset2 = H5Dcreate2(file, DSET2_BITFIELD_NAME, type, space, H5P_DEFAULT, H5P_DEFAULT,
        H5P_DEFAULT)) < 0) TEST_ERROR;

    for(i = 0; i < BITFIELD_ENUMB; i++)
        intw[i] = (unsigned int)0xff << (unsigned int)((i*8)%32);

    if(H5Dwrite(dset2, H5T_NATIVE_B32, H5S_ALL, H5S_ALL, H5P_DEFAULT, intw) < 0) TEST_ERROR;
    if(H5Dclose(dset2) < 0) TEST_ERROR;
    if(H5Sclose(space) < 0) TEST_ERROR;
    if(H5Tclose(type) < 0) TEST_ERROR;

    /* Open dataset1 again to check H5Tget_native_type */
    if((dataset1 = H5Dopen2(file, DSET1_BITFIELD_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset1)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0) TEST_ERROR;

    if((ntype_size = H5Tget_size(native_type)) == 0) TEST_ERROR;

    rbuf = HDmalloc((size_t)nelmts*ntype_size);

    /* Read the data and compare them */
    if(H5Dread(dataset1, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, rbuf) < 0) TEST_ERROR;

    p = (unsigned char *)rbuf;
    for(i = 0; i < BITFIELD_ENUMB*4; i++) {
        if(*p != wbuf[i]) {
            H5_FAILED();
            printf("    Read different values than written.\n");
            printf("    At index %zu\n", i);
            TEST_ERROR;
        }
        p++;
    }

    if(H5Tclose(dtype) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    if(H5Dclose(dataset1) < 0) TEST_ERROR;
    if(rbuf) HDfree(rbuf);

    /* Open dataset2 again to check H5Tget_native_type */
    if((dataset2 = H5Dopen2(file, DSET2_BITFIELD_NAME, H5P_DEFAULT)) < 0) TEST_ERROR;

    if((dtype = H5Dget_type(dataset2)) < 0) TEST_ERROR;

    if((native_type = H5Tget_native_type(dtype, H5T_DIR_DEFAULT)) < 0) TEST_ERROR;

    /* Read the data and compare them */
    if(H5Dread(dataset2, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, intr) < 0) TEST_ERROR;

    for(i = 0; i < BITFIELD_ENUMB; i++) {
        if(intr[i] != intw[i]) {
            H5_FAILED();
            printf("    Read different values than written.\n");
            printf("    At index %zu\n", i);
            TEST_ERROR;
        }
    }

    if(H5Tclose(dtype) < 0) TEST_ERROR;
    if(H5Tclose(native_type) < 0) TEST_ERROR;
    if(H5Dclose(dataset2) < 0) TEST_ERROR;

    PASSED();
    return 0;

error:
    H5E_BEGIN_TRY {
        H5Sclose(space);
        H5Tclose(type);
        H5Tclose(dtype);
        H5Tclose(native_type);
        H5Dclose(dset1);
        H5Dclose(dset2);
        H5Dclose(dataset1);
        H5Dclose(dataset2);
    } H5E_END_TRY;

    return -1;
} /* test_bitfield_dtype */


/*-------------------------------------------------------------------------
 * Function: test_ninteger
 *
 * Purpose: Test the native integer function; made to check the case
 * like the Cray SV1, where the size of short is 8 but precision is 32
 *
 * Return: Success: 0
 *  Failure: -1
 *
 * Programmer: pvn@ncsa.uiuc.edu
 *  September 3, 2004
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_ninteger(void)
{
    hid_t     fid1 = -1;             /* file ID */
    hid_t     fid2 = -1;             /* file ID */
    hid_t     did1 = -1;             /* dataset ID */
    hid_t     did2 = -1;             /* dataset ID */
    hid_t     sid1 = -1;             /* dataspace ID */
    hid_t     dcpl1 = -1;            /* dataset creation property list ID */
    hid_t     dcpl2 = -1;            /* dataset creation property list ID */
    hid_t     tid1 = -1;             /* file datatype */
    hid_t     tid2 = -1;             /* file datatype */
    hid_t     nid1 = -1;             /* native datatype */
    hid_t     nid2 = -1;             /* native datatype */
    hsize_t   dims[1] = {DIM3};      /* dataspace dimensions */
    hsize_t   nelmts;                /* number of elements in dataset */
    int       rank = 1;              /* rank of dataset */
    int       buf[DIM3];
    int       chk[DIM3];
    int       i;

    TESTING("native integer ");

    for(i = 0; i < DIM3; i++)
        buf[i] = i;

    /*-------------------------------------------------------------------------
     * step1: create a file
     *-------------------------------------------------------------------------
     */
    /* create a file using default properties */
    if((fid1 = H5Fcreate("tstint1.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        FAIL_STACK_ERROR

    /* create a data space */
    if((sid1 = H5Screate_simple(rank, dims, NULL)) < 0) FAIL_STACK_ERROR

    /* create dcpl  */
    if((dcpl1 = H5Pcreate(H5P_DATASET_CREATE)) < 0) FAIL_STACK_ERROR

    /* create a dataset */
    if((did1 = H5Dcreate2(fid1, "dset", H5T_NATIVE_INT, sid1, H5P_DEFAULT, dcpl1, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR

    /* write */
    if(H5Dwrite(did1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) FAIL_STACK_ERROR

    /* close  */
    if(H5Sclose(sid1) < 0) FAIL_STACK_ERROR
    if(H5Pclose(dcpl1) < 0) FAIL_STACK_ERROR
    if(H5Dclose(did1) < 0) FAIL_STACK_ERROR
    if(H5Fclose(fid1) < 0) FAIL_STACK_ERROR

    /*-------------------------------------------------------------------------
     * step 2: open and create another file copying the data from file1
     *-------------------------------------------------------------------------
     */

    /* open */
    if((fid1 = H5Fopen("tstint1.h5", H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR

    /* open dataset */
    if((did1 = H5Dopen2(fid1, "dset", H5P_DEFAULT)) < 0) FAIL_STACK_ERROR

    if((sid1 = H5Dget_space(did1)) < 0) FAIL_STACK_ERROR

    /* get dcpl */
    if((dcpl1 = H5Dget_create_plist(did1)) < 0) FAIL_STACK_ERROR

    /* get file datatype */
    if((tid1 = H5Dget_type(did1)) < 0) FAIL_STACK_ERROR

    /* get native datatype */
    if((nid1 = H5Tget_native_type(tid1, H5T_DIR_DEFAULT)) < 0) FAIL_STACK_ERROR

    /* get size */
    if(H5Tget_size(nid1) == 0) FAIL_STACK_ERROR

    /* get rank */
    if((rank = H5Sget_simple_extent_ndims(sid1)) < 0) FAIL_STACK_ERROR
    HDmemset(dims, 0, sizeof dims);

    /* get dimension */
    if(H5Sget_simple_extent_dims(sid1, dims, NULL) < 0) FAIL_STACK_ERROR
    nelmts = 1;
    for(i = 0; i < rank; i++)
        nelmts *= dims[i];

    /* read */
    if(H5Dread(did1, nid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, chk) < 0) FAIL_STACK_ERROR

    /* create a file using default properties */
    if((fid2 = H5Fcreate("tstint2.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR

    /* create a dataset using the native type */
    if((did2 = H5Dcreate2(fid2, "dset", nid1, sid1, H5P_DEFAULT, dcpl1, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR

    /* write */
    if(H5Dwrite(did2, nid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, chk) < 0) FAIL_STACK_ERROR

    /* get dcpl */
    if((dcpl2 = H5Dget_create_plist(did2)) < 0) FAIL_STACK_ERROR

    /* get file datatype */
    if((tid2 = H5Dget_type(did2)) < 0) FAIL_STACK_ERROR

    /* get native datatype */
    if((nid2 = H5Tget_native_type(tid2, H5T_DIR_DEFAULT)) < 0) FAIL_STACK_ERROR

    /* check */
    if(H5Tget_precision(nid1) != H5Tget_precision(nid2)) {
        printf("    Precision differ.\n");
        TEST_ERROR
    } /* end if */

    /* compare dataset creation property lists */
    if(H5Pequal(dcpl1, dcpl2) <= 0) {
        printf("    Property lists differ.\n");
        TEST_ERROR
    } /* end if */

    /* check */
    for(i = 0; i < DIM3; i++)
        if(buf[i] != chk[i]) {
            H5_FAILED();
            printf("    Read different values than written.\n");
            printf("    At index %d\n", i);
            TEST_ERROR
        } /* end if */

    /* close  */
    if(H5Sclose(sid1) < 0) FAIL_STACK_ERROR
    if(H5Pclose(dcpl1) < 0) FAIL_STACK_ERROR
    if(H5Pclose(dcpl2) < 0) FAIL_STACK_ERROR
    if(H5Tclose(tid1) < 0) FAIL_STACK_ERROR
    if(H5Tclose(tid2) < 0) FAIL_STACK_ERROR
    if(H5Tclose(nid1) < 0) FAIL_STACK_ERROR
    if(H5Tclose(nid2) < 0) FAIL_STACK_ERROR
    if(H5Dclose(did1) < 0) FAIL_STACK_ERROR
    if(H5Dclose(did2) < 0) FAIL_STACK_ERROR
    if(H5Fclose(fid1) < 0) FAIL_STACK_ERROR
    if(H5Fclose(fid2) < 0) FAIL_STACK_ERROR

    PASSED();
    return 0;

error:
    H5E_BEGIN_TRY {
        H5Pclose(dcpl1);
        H5Pclose(dcpl2);
        H5Tclose(tid1);
        H5Tclose(tid2);
        H5Tclose(nid1);
        H5Tclose(nid2);
        H5Dclose(did1);
        H5Dclose(did2);
        H5Sclose(sid1);
        H5Fclose(fid1);
        H5Fclose(fid2);
    } H5E_END_TRY;

    return -1;
} /* end test_ninteger() */


/*-------------------------------------------------------------------------
 * Function:	main
 *
 * Purpose:	Test H5Tget_native_type for different datatype
 *
 * Programmer:	Raymond Lu
 *		October 15, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    hid_t	file, fapl;
    int		nerrors = 0;
    char	filename[1024];

    h5_reset();
    fapl = h5_fileaccess();

    h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
    if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
        goto error;

    nerrors += test_atomic_dtype(file) < 0 	? 1 : 0;
    nerrors += test_compound_dtype(file) < 0 	? 1 : 0;
    nerrors += test_compound_dtype2(file) < 0 	? 1 : 0;
    nerrors += test_compound_dtype3(file) < 0 	? 1 : 0;
    nerrors += test_compound_opaque(file) < 0 	? 1 : 0;
    nerrors += test_enum_dtype(file) < 0 	? 1 : 0;
    nerrors += test_array_dtype(file) < 0 	? 1 : 0;
    nerrors += test_array_dtype2(file) < 0 	? 1 : 0;
    nerrors += test_vl_dtype(file) < 0 	        ? 1 : 0;
    nerrors += test_vlstr_dtype(file) < 0 	? 1 : 0;
    nerrors += test_str_dtype(file) < 0         ? 1 : 0;
    nerrors += test_refer_dtype(file) < 0 	? 1 : 0;
    nerrors += test_refer_dtype2(file) < 0 	? 1 : 0;
    nerrors += test_opaque_dtype(file) < 0 	? 1 : 0;
    nerrors += test_bitfield_dtype(file) < 0 	? 1 : 0;
    nerrors += test_ninteger() < 0              ? 1 : 0;

    if(H5Fclose(file) < 0)
        goto error;

    /* Verify symbol table messages are cached */
    nerrors += (h5_verify_cached_stabs(FILENAME, fapl) < 0 ? 1 : 0);

    if(nerrors)
        goto error;

    printf("All native datatype tests passed.\n");
    h5_cleanup(FILENAME, fapl);

    return 0;

error:
    H5E_BEGIN_TRY {
        H5Fclose(file);
        h5_cleanup(FILENAME, fapl);
    } H5E_END_TRY;

    nerrors = MAX(1, nerrors);
    printf("***** %d DATASET TEST%s FAILED! *****\n",
            nerrors, 1 == nerrors ? "" : "S");

    return 1;
}