Skip to content

Commit

Permalink
Add/test is_std_vector/is_pmr_vector, add if_byte_insertable test.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jul 7, 2024
1 parent 67cd335 commit 2f52802
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
14 changes: 14 additions & 0 deletions include/bitcoin/system/typelets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ struct is_std_array_t<std_array<Type, Size>> : std::true_type {};
template<typename Type>
constexpr bool is_std_array = is_std_array_t<Type>::value;

template<typename>
struct is_std_vector_t : std::false_type {};
template<typename Type>
struct is_std_vector_t<std::vector<Type>> : std::true_type {};
template<typename Type>
constexpr bool is_std_vector = is_std_vector_t<Type>::value;

template<typename>
struct is_pmr_vector_t : std::false_type {};
template<typename Type>
struct is_pmr_vector_t<std_vector<Type>> : std::true_type {};
template<typename Type>
constexpr bool is_pmr_vector = is_pmr_vector_t<Type>::value;

template <typename Type, std::enable_if_t<is_std_array<std::decay_t<Type>>, bool> = true>
constexpr size_t array_count = std::tuple_size_v<std::decay_t<Type>>;

Expand Down
1 change: 1 addition & 0 deletions test/constraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ static_assert(is_defined<if_integral_array<std_array<uint8_t, 0>>>);
////static_assert(!is_defined<if_integral_array<uint8_t>>);

static_assert(is_defined<if_byte_insertable<std::string>>);
static_assert(is_defined<if_byte_insertable<std::vector<uint8_t>>>);
static_assert(is_defined<if_byte_insertable<std_vector<uint8_t>>>);
////static_assert(is_defined<if_byte_insertable<std_array<uint8_t, 42>>>);
////static_assert(!is_defined<if_byte_insertable<std::u32string>>);
Expand Down
31 changes: 20 additions & 11 deletions test/typelets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,30 @@ static_assert(is_same_type<std::decay_t<uint8_t&>, uint8_t>);
static_assert(is_same_type<const volatile uint8_t&, uint8_t>);
static_assert(is_same_type<std::decay_t<const volatile uint8_t&>, uint8_t>);

static_assert(is_std_array<std_array<uint8_t, 0>>);
static_assert(is_std_array<std_array<base, 0>>);
static_assert(is_std_array<std_array<uint8_t, 42>>);
static_assert(is_std_array<std::array<uint8_t, 42>>);
static_assert(!is_std_array<std::vector<uint8_t>>);
static_assert(!is_std_array<std_vector<uint8_t>>);
static_assert(!is_std_array<uint8_t>);
static_assert(is_same_type<decltype(is_std_array<std_array<uint8_t, 0>>), const bool>);
static_assert(is_same_type<decltype(is_std_array<const volatile std_array<uint8_t, 0>&>), const bool>);

static_assert(is_std_vector<std::vector<uint8_t>>);
static_assert(!is_std_vector<std_vector<uint8_t>>);
static_assert(!is_std_vector<std_array<uint8_t, 42>>);
static_assert(!is_std_vector<std::array<uint8_t, 42>>);
static_assert(!is_std_vector<uint8_t>);
static_assert(is_same_type<decltype(is_std_vector<std::vector<uint8_t>>), const bool>);
static_assert(is_same_type<decltype(is_std_vector<const volatile std::vector<uint8_t>&>), const bool>);

static_assert(is_pmr_vector<std_vector<uint8_t>>);
static_assert(!is_pmr_vector<std::vector<uint8_t>>);
static_assert(!is_pmr_vector<std_array<uint8_t, 42>>);
static_assert(!is_pmr_vector<std::array<uint8_t, 42>>);
static_assert(!is_pmr_vector<uint8_t>);
static_assert(is_same_type<decltype(is_pmr_vector<std_vector<uint8_t>>), const bool>);
static_assert(is_same_type<decltype(is_pmr_vector<const volatile std_vector<uint8_t>&>), const bool>);

static_assert(array_count<std_array<uint8_t, 42>> == 42);
static_assert(array_count<std_array<std_array<uint32_t, 42>, 24>> == 24);
static_assert(array_count<const volatile std_array<std_array<uint32_t, 42>, 24>&> == 24);
Expand Down Expand Up @@ -352,15 +370,6 @@ static_assert(size_of<std_array<const volatile std_array<std_array<uint32_t, 42>
static_assert(size_of<std_array<std_array<const volatile std_array<uint32_t, 42>, 24>, 8>>() == sizeof(uint32_t) * 42 * 24 * 8);
static_assert(size_of<const volatile std_array<const volatile std_array<const volatile std_array<uint32_t, 42>, 24>, 8>&>() == sizeof(uint32_t) * 42 * 24 * 8);

// std_array fails on "negative subscript"
////static_assert(size_of<std_array<uint8_t, max_size_t>>() == sizeof(uint8_t) * max_size_t);
////static_assert(size_of<std_array<uint16_t, max_size_t>>() == sizeof(uint16_t) * max_size_t);

// multiply overlow
////static_assert(size_of<std_array<uint16_t, to_half(max_size_t)>>() == sizeof(uint16_t) * to_half(max_size_t));

// TODO: This is not currently restricted (is_std_array || !is_std_array).

static_assert(is_uintx<uint5_t>);
static_assert(is_uintx<uint11_t>);
static_assert(is_uintx<uint48_t>);
Expand Down

0 comments on commit 2f52802

Please sign in to comment.