Skip to content

Commit

Permalink
Add HDF5IO::readAttribute unit test for invalid and double attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
oruebel committed Dec 28, 2024
1 parent fe2c63e commit 2b26343
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/io/hdf5/HDF5IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,10 @@ AQNWB::IO::DataBlockGeneric HDF5IO::readAttribute(
// Read the attribute
auto attributePtr = this->getAttribute(dataPath);
if (attributePtr == nullptr) {
throw std::invalid_argument("attributePtr is null");
throw std::invalid_argument(
"HDF5IO::readAttribute, attribute does not exist.");
}

H5::Attribute& attribute = *attributePtr;
H5::DataType dataType = attribute.getDataType();

Expand Down
25 changes: 25 additions & 0 deletions tests/testHDF5IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,19 @@ TEST_CASE("readAttribute", "[hdf5io]")
}
}

SECTION("read double attribute")
{
const double writeData = 2.718;
hdf5io.createAttribute(
BaseDataType::F64, &writeData, "/data", "doubleAttribute");
auto readDataGeneric = hdf5io.readAttribute("/data/doubleAttribute");
auto readData = IO::DataBlock<double>::fromGeneric(readDataGeneric);

REQUIRE(readData.shape.empty()); // Scalar attribute
REQUIRE(readData.data.size() == 1);
REQUIRE(readData.data[0] == Catch::Approx(writeData).epsilon(0.001));
}

SECTION("read string array attribute")
{
const std::vector<std::string> writeData = {"str1", "str2", "str3"};
Expand Down Expand Up @@ -1020,6 +1033,18 @@ TEST_CASE("readAttribute", "[hdf5io]")
H5Gclose(ref_group_id);
}

SECTION("read non-existent attribute")
{
REQUIRE_THROWS_AS(hdf5io.readAttribute("/data/nonExistentAttribute"),
std::invalid_argument);
}

SECTION("read attribute from invalid path")
{
REQUIRE_THROWS_AS(hdf5io.readAttribute("/invalidPath/attribute"),
std::invalid_argument);
}

// close file
hdf5io.close();
}

0 comments on commit 2b26343

Please sign in to comment.