Skip to content

Commit

Permalink
Add unit test for h5 filewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrivlachos committed Jan 27, 2025
1 parent bd88e82 commit 8454e80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
43 changes: 43 additions & 0 deletions tests/test_write_h5_array.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <filesystem>
#include <gtest/gtest.h>
#include <h5/h5read_processed.h>
#include <h5/h5write.h>
#include <vector>

// 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<std::array<double, 3>> 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<double> read_xyzobs =
read_array_from_h5_file<double>(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);
}

0 comments on commit 8454e80

Please sign in to comment.