hdf5: group/table improvements

This commit is contained in:
Christian Zimmermann 2023-01-25 00:30:12 +01:00
parent 58fa19f514
commit 24ea3339f8
3 changed files with 47 additions and 9 deletions

View file

@ -24,6 +24,8 @@ namespace CNORXZ
virtual Int exists() const;
const ContentPtr& get(const String& name) const;
Sptr<Group> getGroup(const String& name) const;
const MArray<ContentPtr>& get() const;
Group& addGroup(const String& name);
@ -39,7 +41,6 @@ namespace CNORXZ
void mkCont();
};
}
}

View file

@ -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>({DType(n)}); };
auto i = mCont.begin();
i.at(dvec(name));
return *i;
}
Sptr<Group> 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>( group );
}
const MArray<ContentPtr>& Group::get() const
{
CXZ_ASSERT(this->isOpen(), "tried to get content of closed group");
return mCont;
}

View file

@ -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<String>(Vector<String>({"field1","second","real"})).create();
Vector<Tuple<SizeT,Int,Double>> 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<Tuple<SizeT,Int,Double>>(rs, std::move(v));
}
String mFileName;
Vector<String> mGrps;
RangePtr mFs;
MArray<Tuple<SizeT,Int,Double>> 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<String>(Vector<String>({"field1","second","real"})).create();
Vector<Tuple<SizeT,Int,Double>> v({ {0, -6, 3.141}, {3, -8, 0.789} });
MArray<Tuple<SizeT,Int,Double>> a(rs, std::move(v));
std::dynamic_pointer_cast<Group>(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);