Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create_datatype<bool>(). #869

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/highfive/bits/H5DataType_misc.hpp
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>();
}

} // namespace HighFive
48 changes: 48 additions & 0 deletions tests/unit/tests_high_five_base.cpp
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 < n; ++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