Skip to content

Commit

Permalink
added mutation test
Browse files Browse the repository at this point in the history
  • Loading branch information
Cfretz244 committed May 25, 2023
1 parent 3b10d98 commit 6e73872
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
45 changes: 45 additions & 0 deletions tests/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,52 @@ TEST_CASE("move-only properties", "varvec tests") {

auto moved = std::move(vec);
validate(moved);
moved.pop_back();
moved.pop_back();
moved.pop_back();
moved.pop_back();
REQUIRE(moved.empty());
REQUIRE(moved.begin() == moved.end());
};
asserts(varvec::meta::identity<movable_vector> {});
asserts(varvec::meta::identity<dynamic_movable_vector> {});
}

TEST_CASE("mutation", "varvec tests") {
auto asserts = [] <class V> (varvec::meta::identity<V>) {
using val = typename V::value_type;
V vec;
vec.push_back(true);
vec.push_back(5);
vec.push_back((float) 3.5);
vec.push_back("hello world");

vec.visit_at(3, varvec::meta::overload {
[] (std::string& msg) { msg = "hello life"; },
[] (auto&) { REQUIRE(false); }
});
REQUIRE(std::get<std::string>(vec[3]) == "hello life");

vec.visit_at(2, varvec::meta::overload {
[] (float& msg) { msg = 42.0; },
[] (auto&) { REQUIRE(false); }
});
REQUIRE(std::get<float>(vec[2]) == 42.0);

vec.visit_at(1, varvec::meta::overload {
[] (int& msg) { msg = 1337; },
[] (auto&) { REQUIRE(false); }
});
REQUIRE(std::get<int>(vec[1]) == 1337);

vec.visit_at(0, varvec::meta::overload {
[] (bool& msg) { msg = false; },
[] (auto&) { REQUIRE(false); }
});
REQUIRE(std::get<bool>(vec[0]) == 0);
};
asserts(varvec::meta::identity<copyable_vector> {});
asserts(varvec::meta::identity<movable_vector> {});
asserts(varvec::meta::identity<dynamic_vector> {});
asserts(varvec::meta::identity<dynamic_movable_vector> {});
}
14 changes: 7 additions & 7 deletions varvec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
#include <functional>
#include <type_traits>

#define DIRTY_MACRO_DECLARE_OPERATOR(op) \
friend bool operator op (variable_iterator const& lhs, variable_iterator const& rhs) { \
return lhs.idx op rhs.idx; \
}

namespace varvec::meta {

template <class T>
Expand Down Expand Up @@ -79,7 +74,7 @@ namespace varvec::meta {

template <class T, class... Ts>
struct simulated_overload_resolution_impl<T, Ts...> : simulated_overload_resolution_impl<Ts...> {
// I don't really understand why this needs to be here,
// XXX: I don't really understand why this needs to be here,
// but clang won't eat it otherwise.
template <class U>
using array_type = U[1];
Expand Down Expand Up @@ -121,6 +116,7 @@ namespace varvec::meta {
using fuzzy_type_match_t =
typename decltype(simulated_overload_resolution_impl<Ts...> {}(std::declval<T>(), std::declval<T>()))::type;

// Redirect move-only types to pointers
template <class T>
constexpr auto copyable_type_for() {
if constexpr (std::copyable<T>) {
Expand Down Expand Up @@ -494,13 +490,17 @@ namespace varvec::storage {
}

uint8_t* resize(size_t newsize) {
assert(newsize >= bytes);

// Align some storage
std::unique_ptr<uint8_t[], void (*) (uint8_t*) noexcept> newdata {
new (std::align_val_t(max_alignment)) uint8_t[newsize], aligned_delete
};

// Strong exception guarantee. Don't throw from moves
if constexpr (std::is_nothrow_move_constructible_v<Variant>) {
if constexpr (std::is_trivially_copyable_v<Variant>) {
memcpy(newdata.get(), data.get(), bytes);
} else if constexpr (std::is_nothrow_move_constructible_v<Variant>) {
move_storage<Variant>(count, meta, newdata.get(), data.get());
} else {
copy_storage<Variant>(count, meta, newdata.get(), data.get());
Expand Down

0 comments on commit 6e73872

Please sign in to comment.