Skip to content

Commit

Permalink
hdf5Write unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Jan 30, 2023
1 parent 3462e11 commit 18ad906
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion modules/c++/hdf5.lite/unittests/test_hdf5write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

#include "hdf5/lite/Write.h"
#include "hdf5/lite/HDF5Exception.h"
#include "hdf5/lite/Info.h"
#include "hdf5/lite/Read.h"

static std::filesystem::path find_unittest_file(const std::filesystem::path& name)
{
Expand All @@ -41,12 +43,51 @@ static std::filesystem::path find_unittest_file(const std::filesystem::path& nam
TEST_CASE(test_hdf5Create)
{
static const auto path_ = find_unittest_file("example.h5");
static const auto path = path_.parent_path() / "TEST_myfile_TMP.h5";
static const auto path = path_.parent_path() / "TEST_hdf5Create_TMP.h5";

// https://www.mathworks.com/help/matlab/ref/h5write.html
hdf5::lite::createFile<float>(path, "/DS1", {10, 20});
}

TEST_CASE(test_hdf5Write)
{
static const auto path_ = find_unittest_file("example.h5");
static const auto path = path_.parent_path() / "TEST_hdf5Write_TMP.h5";

const types::RowCol<size_t> dims{10, 20};
std::vector<double> data_(dims.area());
const hdf5::lite::SpanRC<double> data(data_.data(), dims);
double d = 0.0;
for (size_t r = 0; r<dims.row; r++)
{
for (size_t c = 0; c < dims.col; c++)
{
data(r, c) = d++;
}
}

// https://www.mathworks.com/help/matlab/ref/h5write.html
hdf5::lite::createFile(path, "/DS1", data);
hdf5::lite::writeFile(path, "/DS1", data);

// Be sure we can read the file just written
const auto info = hdf5::lite::dataSetInfo(path, "/DS1");
TEST_ASSERT_EQ(path.string(), info.filename);
TEST_ASSERT_EQ("DS1", info.name);
TEST_ASSERT(info.dataType.h5Class == hdf5::lite::Class::Float);

std::vector<double> result;
const auto rc = hdf5::lite::readFile(path, "/DS1", result);
TEST_ASSERT(rc.dims() == dims);
TEST_ASSERT_EQ(dims.area(), result.size());
for (size_t i = 0; i < result.size(); i++)
{
const auto expected = static_cast<double>(i);
TEST_ASSERT_ALMOST_EQ(result[i], expected);
}
}

TEST_MAIN(
TEST_CHECK(test_hdf5Create);
TEST_CHECK(test_hdf5Write);
)

0 comments on commit 18ad906

Please sign in to comment.