Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Oct 5, 2024
1 parent b2cc936 commit 2d6cc05
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions inc/mkn/kul/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ template <typename T>
std::vector<T, Allocator<T>>& as_super(std::vector<T, NonConstructingAllocator<T>>& v) {
return *reinterpret_cast<std::vector<T, Allocator<T>>*>(&v);
}
template <typename T>
std::vector<T, Allocator<T>> const& as_super(std::vector<T, NonConstructingAllocator<T>> const& v) {
return *reinterpret_cast<std::vector<T, Allocator<T>> const*>(&v);
}

} // namespace mkn::kul

Expand Down
20 changes: 15 additions & 5 deletions test/test/no_construct_alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <array>
#include <cstddef>
#include <algorithm>
#include <stdexcept>

template <std::size_t size = 8>
Expand All @@ -17,9 +18,7 @@ struct S {
}

bool operator==(S const& that) const {
for (std::size_t i = 0; i < size; i++)
if (vars[i] != that.vars[i]) return false;
return true;
return std::equal(vars.begin(), vars.end(), that.vars.begin());
}

bool operator!=(S const& that) const { return !(*this == that); }
Expand All @@ -43,6 +42,15 @@ auto copy_operator_equal(V const& v) {
return out;
}

template <typename V>
auto copy_operator_equal_super(V const& v) {
KUL_DBG_FUNC_ENTER;
V out;
out.reserve(v.capacity());
mkn::kul::as_super(out) = mkn::kul::as_super(v);
return out;
}

template <typename V>
auto copy_manual(V const& v) {
KUL_DBG_FUNC_ENTER;
Expand All @@ -56,7 +64,7 @@ auto copy_manual(V const& v) {
template <typename V>
auto make_vector(std::size_t const& size) {
KUL_DBG_FUNC_ENTER;
return V{size};
return V(size);
}
template <typename V0, typename V1>
auto make_vector_from(V1 const& v1) {
Expand All @@ -68,7 +76,7 @@ auto make_vector_from(V1 const& v1) {

template <typename T>
void do_compare() {
constexpr static std::size_t N = 2e6;
constexpr static std::size_t N = 2000000;
auto const std_vec = make_vector<std::vector<T>>(N);
auto const std_vec2 = make_vector_from<std::vector<T>>(std_vec);
auto const no_construct_vec = make_vector_from<mkn::kul::NonConstructingVector<T>>(std_vec);
Expand All @@ -78,11 +86,13 @@ void do_compare() {
auto const v1 = copy_construct(no_construct_vec);
auto const v2 = copy_manual(std_vec);
auto const v3 = copy_manual(no_construct_vec);
auto const v4 = copy_operator_equal_super(no_construct_vec);

if (v0 != std_vec) throw std::runtime_error("FAIL 0");
if (v0 == v1) throw std::runtime_error("FAIL 1"); // :(
if (v0 != v2) throw std::runtime_error("FAIL 2");
if (v0 != v3) throw std::runtime_error("FAIL 3");
if (v0 != v4) throw std::runtime_error("FAIL 4");
}

TEST(NoConstructAllocator, copies) { do_compare<S<8>>(); }

0 comments on commit 2d6cc05

Please sign in to comment.