Skip to content

Commit

Permalink
test const 0 capacity variants
Browse files Browse the repository at this point in the history
  • Loading branch information
wusatosi committed Nov 19, 2024
1 parent 548a282 commit c9f2d6e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
11 changes: 7 additions & 4 deletions include/beman/inplace_vector/inplace_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ using inplace_vector_internal_size_type =

// array based storage is used so that we can satisfy constexpr requirement
//
// Selecting this storage type implies: std::is_trivial_v<T>
// Selecting this storage type implies: std::is_trivial_v<T> or Capacity = 0
template <typename T, std::size_t Capacity>
struct inplace_vector_array_based_storage {
std::array<T, Capacity> elems;
using array_type = If<!std::is_const_v<T>, std::array<T, Capacity>,
const std::array<std::remove_const_t<T>, Capacity>>;

array_type elems{};

constexpr T *begin() { return elems.data(); }
constexpr const T *begin() const { return elems.data(); }
Expand All @@ -57,7 +60,7 @@ struct inplace_vector_array_based_storage {
// byte array based storage is used for non-constexpr environment, where default
// initialization may not be available.
//
// Selecting this storage type implies: !std::is_trivial_v<T>
// Selecting this storage type implies: !std::is_trivial_v<T> and Capacity != 0
template <typename T, std::size_t Capacity>
struct inplace_vector_bytes_based_storage {
alignas(T) unsigned char elems[Capacity * sizeof(T)];
Expand All @@ -74,7 +77,7 @@ struct inplace_vector_destruct_base {
using size_type = std::size_t;
using internal_size_type = inplace_vector_internal_size_type<Capacity>;
using internal_storage_type =
std::conditional_t<std::is_trivial_v<T>,
std::conditional_t<std::is_trivial_v<T> or Capacity == 0,
inplace_vector_array_based_storage<T, Capacity>,
inplace_vector_bytes_based_storage<T, Capacity>>;

Expand Down
66 changes: 35 additions & 31 deletions src/beman/inplace_vector/tests/constexpr.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,55 @@ struct NonTrivial {
};
static_assert(!std::is_trivial_v<NonTrivial>);

static_assert(std::invoke([]() {
inplace_vector<int, 0> vec;
template <typename T> constexpr bool test_empty_vec(T &vec) {

// sizes
S_ASSERT(vec.max_size() == 0);
S_ASSERT(vec.capacity() == 0);
S_ASSERT(vec.size() == 0);
S_ASSERT(vec.empty());
// sizes
S_ASSERT(vec.max_size() == 0);
S_ASSERT(vec.capacity() == 0);
S_ASSERT(vec.size() == 0);
S_ASSERT(vec.empty());

// itr
S_ASSERT(vec.begin() == vec.end());
S_ASSERT(vec.cbegin() == vec.cend());
S_ASSERT(vec.rbegin() == vec.rend());
S_ASSERT(vec.crbegin() == vec.crend());
// itr
S_ASSERT(vec.begin() == vec.end());
S_ASSERT(vec.cbegin() == vec.cend());
S_ASSERT(vec.rbegin() == vec.rend());
S_ASSERT(vec.crbegin() == vec.crend());

// push_back
S_ASSERT(vec.try_push_back(0) == nullptr);
S_ASSERT(vec.try_emplace_back(0) == nullptr);
// push_back
S_ASSERT(vec.try_push_back({}) == nullptr);
S_ASSERT(vec.try_emplace_back() == nullptr);

return true;
}

static_assert(std::invoke([]() {
inplace_vector<int, 0> vec;
test_empty_vec(vec);
return true;
}),
"0 capacity Trivial type");

static_assert(std::invoke([]() {
inplace_vector<NonTrivial, 0> vec;

// sizes
S_ASSERT(vec.max_size() == 0);
S_ASSERT(vec.capacity() == 0);
S_ASSERT(vec.size() == 0);
S_ASSERT(vec.empty());

// itr
S_ASSERT(vec.begin() == vec.end());
S_ASSERT(vec.cbegin() == vec.cend());
S_ASSERT(vec.rbegin() == vec.rend());
S_ASSERT(vec.crbegin() == vec.crend());

// push_back
S_ASSERT(vec.try_push_back({}) == nullptr);
inplace_vector<const int, 0> vec;
test_empty_vec(vec);
return true;
}),
"0 capacity Trivial const type");

static_assert(std::invoke([]() {
inplace_vector<NonTrivial, 0> vec;
test_empty_vec(vec);
return true;
}),
"0 capacity Non-trivial type");

static_assert(std::invoke([]() {
inplace_vector<const NonTrivial, 0> vec;
test_empty_vec(vec);
return true;
}),
"0 capacity Non-trivial const type");

static_assert(std::invoke([]() {
// sizes
{
Expand Down

0 comments on commit c9f2d6e

Please sign in to comment.