Skip to content

Commit

Permalink
Improve details::Boolean compatibility checks.
Browse files Browse the repository at this point in the history
Instead of comparing values, use `std::memcmp` to compare bytes.
  • Loading branch information
1uc committed Nov 30, 2023
1 parent 6cc408d commit c6d1194
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions tests/unit/tests_high_five_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
Expand Down Expand Up @@ -2439,10 +2440,24 @@ TEST_CASE("HighFiveReadType") {

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 n_bytes = 2 * sizeof(details::Boolean);

auto* const enum_ptr = (details::Boolean*) malloc(n_bytes);
memset(enum_ptr, 187, n_bytes);
enum_ptr[0] = details::Boolean::HighFiveTrue;
enum_ptr[1] = details::Boolean::HighFiveFalse;

auto* const bool_ptr = (bool*) malloc(n_bytes);
memset(bool_ptr, 68, n_bytes);
bool_ptr[0] = true;
bool_ptr[1] = false;

CHECK(std::memcmp(bool_ptr, enum_ptr, n_bytes) == 0);

free(enum_ptr);
free(bool_ptr);
}

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

0 comments on commit c6d1194

Please sign in to comment.