From 8454e80177dc41905688ca0493b1ee97fc968226 Mon Sep 17 00:00:00 2001 From: Dimitri Vlachos Date: Mon, 27 Jan 2025 11:13:32 +0000 Subject: [PATCH] Add unit test for h5 filewriter --- tests/CMakeLists.txt | 4 ++++ tests/test_write_h5_array.cxx | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/test_write_h5_array.cxx diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2de1601..1216b17 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,7 +5,11 @@ target_link_libraries(test_make_models GTest::gtest_main dx2 Eigen3::Eigen nlohm add_executable(test_read_h5_array test_read_h5_array.cxx) target_link_libraries(test_read_h5_array GTest::gtest_main dx2 hdf5::hdf5) +add_executable(test_write_h5_array test_write_h5_array.cxx) +target_link_libraries(test_write_h5_array GTest::gtest_main dx2 hdf5::hdf5) + include(GoogleTest) gtest_discover_tests(test_make_models PROPERTIES LABELS dx2tests) gtest_discover_tests(test_read_h5_array WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tests" PROPERTIES LABELS dx2tests) +gtest_discover_tests(test_write_h5_array WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tests" PROPERTIES LABELS dx2tests) diff --git a/tests/test_write_h5_array.cxx b/tests/test_write_h5_array.cxx new file mode 100644 index 0000000..a5146bf --- /dev/null +++ b/tests/test_write_h5_array.cxx @@ -0,0 +1,43 @@ +#include +#include +#include
+#include
+#include + +// Test writing a 3D vector dataset to an HDF5 file and verifying the written +// content +TEST(ExampleTests, WriteArrayTest) { + // Define the test file path + std::filesystem::path cwd = std::filesystem::current_path(); + std::string test_file_path = cwd.generic_string(); + test_file_path.append("/data/test_write.h5"); + + // Prepare test data (3D vectors) + std::vector> xyzobs_test_data = { + {100.1, 200.2, 300.3}, {400.4, 500.5, 600.6}, {700.7, 800.8, 900.9}}; + + // Call the function to write the test data to the HDF5 file + std::string dataset_name = "xyzobs.px.value"; + std::string group_path = "processing"; + + write_xyzobs_to_h5_file(test_file_path, group_path, dataset_name, + xyzobs_test_data); + + // Read the written data back from the HDF5 file + std::string full_dataset_path = "/dials/processing/group_0/xyzobs.px.value"; + std::vector read_xyzobs = + read_array_from_h5_file(test_file_path, full_dataset_path); + + // Validate the size of the read data + ASSERT_EQ(read_xyzobs.size(), xyzobs_test_data.size() * 3); + + // Validate the content of the read data + for (std::size_t i = 0; i < xyzobs_test_data.size(); ++i) { + EXPECT_DOUBLE_EQ(read_xyzobs[i * 3 + 0], xyzobs_test_data[i][0]); + EXPECT_DOUBLE_EQ(read_xyzobs[i * 3 + 1], xyzobs_test_data[i][1]); + EXPECT_DOUBLE_EQ(read_xyzobs[i * 3 + 2], xyzobs_test_data[i][2]); + } + + // Clean up test file after successful run (comment out to keep the test file) + std::filesystem::remove(test_file_path); +}