cosmotool/sample/testHDF5.cpp

66 lines
1.5 KiB
C++
Raw Normal View History

#include <iostream>
#include <H5Cpp.h>
#include "hdf5_array.hpp"
using namespace std;
int main()
{
typedef boost::multi_array<float, 2> array_type;
typedef boost::multi_array<float, 3> array3_type;
typedef boost::multi_array<std::complex<double>, 2> arrayc_type;
typedef array_type::index index;
H5::H5File f("test.h5", H5F_ACC_TRUNC);
H5::Group g = f.createGroup("test_group");
array_type A(boost::extents[2][3]);
array_type B;
array3_type C(boost::extents[2][3][4]);
arrayc_type D, E;
int values = 0;
for (index i = 0; i != 2; i++)
for (index j = 0; j != 3; j++)
A[i][j] = values++;
CosmoTool::hdf5_write_array(g, "test_data", A);
CosmoTool::hdf5_read_array(g, "test_data", B);
int verify = 0;
for (index i = 0; i != 2; i++)
for (index j = 0; j != 3; j++)
if (B[i][j] != verify++) {
std::cout << "Invalid array content" << endl;
abort();
}
try
{
CosmoTool::hdf5_read_array(g, "test_data", C);
std::cout << "Did not throw InvalidDimensions" << endl;
abort();
}
catch (const CosmoTool::InvalidDimensions&)
{}
D.resize(boost::extents[2][3]);
D = A;
CosmoTool::hdf5_write_array(g, "test_data_c", D);
CosmoTool::hdf5_read_array(g, "test_data_c", E);
verify = 0;
for (index i = 0; i != 2; i++)
for (index j = 0; j != 3; j++)
if (E[i][j].real() != verify++) {
std::cout << "Invalid array content" << endl;
abort();
}
return 0;
}