Skip to content

Commit

Permalink
Fix create_datatype<bool>().
Browse files Browse the repository at this point in the history
Create the specialization from `create_datatype<bool>` and adds the
tests to check that raw arrays of bool are bitwise compatible with the
enum that's serialized.
  • Loading branch information
1uc committed Nov 27, 2023
1 parent 90f8fd6 commit defe171
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/highfive/bits/H5DataType_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,12 @@ inline DataType create_and_check_datatype() {

} // namespace HighFive
HIGHFIVE_REGISTER_TYPE(HighFive::details::Boolean, HighFive::create_enum_boolean)

namespace HighFive {

template <>
inline DataType create_datatype<bool>() {
return create_datatype<HighFive::details::Boolean>();
}

}
48 changes: 48 additions & 0 deletions tests/unit/tests_high_five_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <random>
#include <string>
#include <typeinfo>
#include <type_traits>
#include <vector>

#include <catch2/catch_test_macros.hpp>
Expand Down Expand Up @@ -2946,6 +2947,53 @@ TEST_CASE("HighFiveReadType") {
CHECK(t4 == t3);
}


TEST_CASE("DirectWriteBool") {
SECTION("Basic compatibility") {
using IntType = typename std::underlying_type<details::Boolean>::type;
CHECK(sizeof(bool) == sizeof(details::Boolean));
CHECK(true == static_cast<IntType>(details::Boolean::HighFiveTrue));
CHECK(false == static_cast<IntType>(details::Boolean::HighFiveFalse));
}

auto file = File("rw_bool_from_ptr.h5", File::Truncate);

size_t n = 4;
bool * expected = new bool[n];
bool * actual = new bool[n];

for(size_t i = 0; i < 4; ++i) {
expected[i] = i % 2 == 0;
}

auto dataspace = DataSpace{n};
auto datatype = create_datatype<bool>();

SECTION("WriteReadCycleAttribute") {
auto attr = file.createAttribute("attr", dataspace, datatype);
attr.write_raw(expected);
attr.read(actual);

for(size_t i = 0; i < n; ++i) {
REQUIRE(expected[i] == actual[i]);
}
}

SECTION("WriteReadCycleDataSet") {
auto dset = file.createAttribute("dset", dataspace, datatype);
dset.write_raw(expected);
dset.read(actual);

for(size_t i = 0; i < n; ++i) {
REQUIRE(expected[i] == actual[i]);
}
}

delete[] expected;
delete[] actual;
}


class ForwardToAttribute {
public:
ForwardToAttribute(const HighFive::File& file)
Expand Down

0 comments on commit defe171

Please sign in to comment.