From 0a3dfa2bde6496ddf87c94f9922babbcd016ab25 Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Sun, 26 Nov 2023 14:05:59 +0100 Subject: [PATCH 1/2] Fix `create_datatype()`. Create the specialization from `create_datatype` and adds the tests to check that raw arrays of bool are bitwise compatible with the enum that's serialized. --- include/highfive/bits/H5DataType_misc.hpp | 9 +++++ tests/unit/tests_high_five_base.cpp | 48 +++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/highfive/bits/H5DataType_misc.hpp b/include/highfive/bits/H5DataType_misc.hpp index 27f863d3d..e579d611d 100644 --- a/include/highfive/bits/H5DataType_misc.hpp +++ b/include/highfive/bits/H5DataType_misc.hpp @@ -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() { + return create_datatype(); +} + +} // namespace HighFive diff --git a/tests/unit/tests_high_five_base.cpp b/tests/unit/tests_high_five_base.cpp index 4108de06d..c89805118 100644 --- a/tests/unit/tests_high_five_base.cpp +++ b/tests/unit/tests_high_five_base.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -2946,6 +2947,53 @@ TEST_CASE("HighFiveReadType") { CHECK(t4 == t3); } + +TEST_CASE("DirectWriteBool") { + SECTION("Basic compatibility") { + using IntType = typename std::underlying_type::type; + CHECK(sizeof(bool) == sizeof(details::Boolean)); + CHECK(true == static_cast(details::Boolean::HighFiveTrue)); + CHECK(false == static_cast(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(); + + 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) From a391f2b7786b8f3a8a5068f9d3c51677da89bacc Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Mon, 27 Nov 2023 09:56:29 +0100 Subject: [PATCH 2/2] Fixup hardcoded value. --- tests/unit/tests_high_five_base.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/tests_high_five_base.cpp b/tests/unit/tests_high_five_base.cpp index c89805118..0f125c290 100644 --- a/tests/unit/tests_high_five_base.cpp +++ b/tests/unit/tests_high_five_base.cpp @@ -2962,7 +2962,7 @@ TEST_CASE("DirectWriteBool") { bool* expected = new bool[n]; bool* actual = new bool[n]; - for (size_t i = 0; i < 4; ++i) { + for (size_t i = 0; i < n; ++i) { expected[i] = i % 2 == 0; }