diff --git a/src/opt/hdf5/include/h5_group.h b/src/opt/hdf5/include/h5_group.h index 79daeff..a13cd02 100644 --- a/src/opt/hdf5/include/h5_group.h +++ b/src/opt/hdf5/include/h5_group.h @@ -24,6 +24,8 @@ namespace CNORXZ virtual Int exists() const; + const ContentPtr& get(const String& name) const; + Sptr getGroup(const String& name) const; const MArray& get() const; Group& addGroup(const String& name); @@ -39,7 +41,6 @@ namespace CNORXZ void mkCont(); }; - } } diff --git a/src/opt/hdf5/lib/h5_group.cc b/src/opt/hdf5/lib/h5_group.cc index ec130e6..3690c36 100644 --- a/src/opt/hdf5/lib/h5_group.cc +++ b/src/opt/hdf5/lib/h5_group.cc @@ -71,8 +71,26 @@ namespace CNORXZ return H5Lexists(mParent->id(), mName.c_str(), H5P_DEFAULT) != 0 ? 1 : 0; } + const ContentPtr& Group::get(const String& name) const + { + CXZ_ASSERT(this->isOpen(), "tried to get content of closed group"); + auto dvec = [](const String& n) { return Vector({DType(n)}); }; + auto i = mCont.begin(); + i.at(dvec(name)); + return *i; + } + + Sptr Group::getGroup(const String& name) const + { + auto group = this->get(name); + CXZ_ASSERT(group->type() == ContentType::GROUP, + "element '" << name << "' is not of type GROUP"); + return std::dynamic_pointer_cast( group ); + } + const MArray& Group::get() const { + CXZ_ASSERT(this->isOpen(), "tried to get content of closed group"); return mCont; } diff --git a/src/opt/hdf5/tests/h5_basic_unit_test.cc b/src/opt/hdf5/tests/h5_basic_unit_test.cc index 2ddeb6a..1399651 100644 --- a/src/opt/hdf5/tests/h5_basic_unit_test.cc +++ b/src/opt/hdf5/tests/h5_basic_unit_test.cc @@ -12,6 +12,8 @@ namespace { using namespace CNORXZ; using namespace CNORXZ::hdf5; + + static const String testh5file = "test_file.h5"; class NoFile_Test : public ::testing::Test { @@ -36,14 +38,28 @@ namespace Group_Test() { - mFileName = "test_file.h5"; + mFileName = testh5file; mGrps = { "gr1", "gr2" }; + mFs = URangeFactory(Vector({"field1","second","real"})).create(); + Vector> v + ( { {0, -6, 3.141}, + {3, -8, 0.789}, + {34, 4, 10.009}, + {2, -777, -9.77}, + {321, 0, -0.003} + } ); + RangePtr rs = CRangeFactory(v.size()).create(); + mTabA = MArray>(rs, std::move(v)); } String mFileName; Vector mGrps; + + RangePtr mFs; + MArray> mTabA; }; + TEST_F(NoFile_Test, NoFile) { File f(mNoFileName, true); @@ -67,15 +83,18 @@ namespace h5f.addGroup("gr1"); h5f.addGroup("gr2"); EXPECT_EQ(h5f.get().size(), 2u); - - // move to separate test: - RangePtr rs = CRangeFactory(2).create(); - RangePtr fs = URangeFactory(Vector({"field1","second","real"})).create(); - Vector> v({ {0, -6, 3.141}, {3, -8, 0.789} }); - MArray> a(rs, std::move(v)); - std::dynamic_pointer_cast(h5f.get().data()[0])->addTable("tab1", a, fs); + h5f.close(); } + TEST_F(Group_Test, CreateTable) + { + File h5f(mFileName, false); + h5f.open(); + h5f.getGroup("gr1")->open().addTable("tab1", mTabA, mFs); + h5f.getGroup("gr2")->open().addTable("tab1", mTabA, mFs); + h5f.close(); + } + TEST_F(Group_Test, Read) { File h5f(mFileName, true);