From e331f6c4e2a91f4b03795834f0e9e52163fe0592 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 15 Jan 2023 01:26:01 +0100 Subject: [PATCH] first hdf5 test --- src/opt/hdf5/include/cnorxz_hdf5.h | 4 ++ src/opt/hdf5/lib/h5_file.cc | 2 +- src/opt/hdf5/tests/CMakeLists.txt | 2 +- src/opt/hdf5/tests/h5_basic_unit_test.cc | 62 +++++++++++++++++++++++- 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/opt/hdf5/include/cnorxz_hdf5.h diff --git a/src/opt/hdf5/include/cnorxz_hdf5.h b/src/opt/hdf5/include/cnorxz_hdf5.h new file mode 100644 index 0000000..b4de2fc --- /dev/null +++ b/src/opt/hdf5/include/cnorxz_hdf5.h @@ -0,0 +1,4 @@ + +#include "h5_content_base.h" +#include "h5_file.h" +#include "h5_group.h" diff --git a/src/opt/hdf5/lib/h5_file.cc b/src/opt/hdf5/lib/h5_file.cc index a520be8..ba19f89 100644 --- a/src/opt/hdf5/lib/h5_file.cc +++ b/src/opt/hdf5/lib/h5_file.cc @@ -13,7 +13,7 @@ namespace CNORXZ Int ex = this->exists(); const String fn = this->filename(); CXZ_ASSERT( ex != 2, "tried to open non-h5 file '" << fn << "'" ); - if(this->ro()){ + if(mRo){ CXZ_ASSERT( ex == 1, "could not open file as read-only: '" << fn << "' does not exist'"); mId = H5Fopen( fn.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); diff --git a/src/opt/hdf5/tests/CMakeLists.txt b/src/opt/hdf5/tests/CMakeLists.txt index 41d0808..d96d301 100644 --- a/src/opt/hdf5/tests/CMakeLists.txt +++ b/src/opt/hdf5/tests/CMakeLists.txt @@ -8,4 +8,4 @@ add_definitions(-DTEST_FILE_BASE=${TEST_FILE_BASE}) add_executable(h5basic h5_basic_unit_test.cc) add_dependencies(h5basic cnorxz cnorxzhdf5) target_link_libraries(h5basic ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${HDF5_LIBS} cnorxz cnorxzhdf5) -add_test(NAME h5basic COMMAND rm -f ${TEST_FILE_BASE}* ; h5basic) +add_test(NAME h5basic COMMAND h5basic) diff --git a/src/opt/hdf5/tests/h5_basic_unit_test.cc b/src/opt/hdf5/tests/h5_basic_unit_test.cc index 8c49f6a..3681496 100644 --- a/src/opt/hdf5/tests/h5_basic_unit_test.cc +++ b/src/opt/hdf5/tests/h5_basic_unit_test.cc @@ -1,8 +1,66 @@ -// check no file -// check no hdf5 file +#include +#include +#include + +#include "gtest/gtest.h" + +#include "cnorxz_hdf5.h" + +namespace +{ + using namespace CNORXZ; + using namespace CNORXZ::hdf5; + + class NoFile_Test : public ::testing::Test + { + protected: + + NoFile_Test() + { + mNoFileName = "no_file.h5"; + mWrongFileName = "just_txt.h5"; + std::fstream fs(mWrongFileName, std::ios::out); + fs << "This file is not a hdf5 file" << std::endl; + fs.close(); + } + + String mNoFileName; + String mWrongFileName; + }; + + class Group_Test : public ::testing::Test + { + protected: + + Group_Test() + { + + } + }; + + TEST_F(NoFile_Test, NoFile) + { + auto l = [&](){ return std::make_shared(mNoFileName, true); }; + EXPECT_THROW(l(), std::runtime_error); + } + + TEST_F(NoFile_Test, NoH5Format) + { + auto l1 = [&](){ return std::make_shared(mWrongFileName, true); }; + auto l2 = [&](){ return std::make_shared(mWrongFileName, false); }; + EXPECT_THROW(l1(), std::runtime_error); + EXPECT_THROW(l2(), std::runtime_error); + } +} + // check write to new file // check read from that file // check write to existing file // check again read from that file +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}