From 5e1bda2715f0b2e8e4bfca5f1090cb2bae630048 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 29 Jul 2024 16:32:28 +0200 Subject: [PATCH] added data augmentation tests for clear and insert --- test/const_vector/CMakeLists.txt | 5 + .../const_vector_data_augmentation.test.cpp | 248 ++++++++++++++++++ 2 files changed, 253 insertions(+) create mode 100644 test/const_vector/const_vector_data_augmentation.test.cpp diff --git a/test/const_vector/CMakeLists.txt b/test/const_vector/CMakeLists.txt index 53352a5..c1889fe 100644 --- a/test/const_vector/CMakeLists.txt +++ b/test/const_vector/CMakeLists.txt @@ -16,3 +16,8 @@ add_executable(test_const_vector_data_access const_vector_data_access.test.cpp test_params.h) target_link_libraries(test_const_vector_data_access const_container test_common) add_test(NAME "const_vector data access" COMMAND test_const_vector_data_access) + +add_executable(test_const_vector_data_augmentation const_vector_data_augmentation.test.cpp + test_params.h) +target_link_libraries(test_const_vector_data_augmentation const_container test_common) +add_test(NAME "const_vector data augmentation" COMMAND test_const_vector_data_augmentation) diff --git a/test/const_vector/const_vector_data_augmentation.test.cpp b/test/const_vector/const_vector_data_augmentation.test.cpp new file mode 100644 index 0000000..cb542e5 --- /dev/null +++ b/test/const_vector/const_vector_data_augmentation.test.cpp @@ -0,0 +1,248 @@ +#include + +#include +#include +#include +#include "test_params.h" + +#include +#include +#include + +template +struct std::formatter { + template + constexpr ParseContext::iterator parse(ParseContext& ctx) { + auto it = ctx.begin(); + while (it != ctx.end() && *it != '}') + ++it; + return it; + } + template + constexpr FmtContext::iterator format(const R& range, FmtContext& ctx) const { + std::ostringstream out; + out << '['; + + auto it = std::ranges::begin(range); + for (; it != std::next(std::ranges::begin(range), std::ranges::size(range) - 1); ++it) { + out << std::format("{}, ", *it); + } + out << std::format("{}]", *it); + + return std::ranges::copy(std::move(out).str(), ctx.out()).out; + } +}; + +template +struct std::formatter { + template + constexpr ParseContext::iterator parse(ParseContext &ctx) + { + auto it = ctx.begin(); + while (it != ctx.end() && *it != '}') + ++it; + return it; + } + + template + constexpr FmtContext::iterator format(const TestObj &obj, FmtContext &ctx) const + { + std::ostringstream out; + out << '(' << obj.x << ", '" << obj.c << "', \"" << obj.s << "\")"; + + return std::ranges::copy(std::move(out).str(), ctx.out()).out; + } +}; + +constexpr test_suite tests = define_tests("Data Augmentation") + ("const_vector::clear()", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + cc::const_vector v(get_test_param()); + + v.clear(); + + ASSERT(v.empty(), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + ("const_vector::insert(const_iter pos, const T& value)", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + cc::const_vector()) + 3> v(get_test_param()); + + auto val_it = v.insert(std::next(v.begin()), get_test_param()); + v.insert(val_it, *v.begin()); + v.insert(v.end(), get_test_param()); + + ASSERT_THROWS((v.insert(v.end(), get_test_param())), std::length_error, ctx); + + std::vector comparable; + std::ranges::copy(get_test_param(), std::back_inserter(comparable)); + + auto comparable_it = comparable.insert(std::next(comparable.begin()), get_test_param()); + comparable.insert(comparable_it, *comparable.begin()); + comparable.insert(comparable.end(), get_test_param()); + + ASSERT(ranges_equal(v, comparable), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + ("const_vector::insert(const_iter pos, T&& value)", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + auto value1 = make_copy(get_test_param()); + auto value2 = make_copy(get_test_param()[0]); + auto value3 = make_copy(get_test_param()); + auto value4 = make_copy(get_test_param()); + + auto value1_comp = make_copy(get_test_param()); + auto value2_comp = make_copy(get_test_param()[0]); + auto value3_comp = make_copy(get_test_param()); + + cc::const_vector()) + 3> v(get_test_param()); + + auto val_it = v.insert(std::next(v.begin()), force_move(value1)); + v.insert(val_it, force_move(value2)); + v.insert(v.end(), force_move(value3)); + + ASSERT_THROWS((v.insert(v.end(), force_move(value4))), std::length_error, ctx); + ASSERT_MSG((value4 == get_test_param()), "Value has been changed by failed move insert", ctx); + + std::vector comparable; + std::ranges::copy(get_test_param(), std::back_inserter(comparable)); + + auto comparable_it = comparable.insert(std::next(comparable.begin()), force_move(value1_comp)); + comparable.insert(comparable_it, force_move(value2_comp)); + comparable.insert(comparable.end(), force_move(value3_comp)); + + ASSERT(ranges_equal(v, comparable), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + ("const_vector::insert(const_iter pos, size_type count, const T &value)", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + cc::const_vector()) + 9> v(get_test_param()); + + auto it = v.insert(std::next(v.begin()), 3, get_test_param()); + v.insert(it, 3, get_test_param()[0]); + v.insert(v.end(), 3, get_test_param()); + + ASSERT_THROWS((v.insert(v.end(), 1, get_test_param())), std::length_error, ctx); + ASSERT_NOTHROW((v.insert(v.end(), 0, get_test_param())), std::length_error, ctx); + + std::vector comparable; + std::ranges::copy(get_test_param(), std::back_inserter(comparable)); + + auto cit = comparable.insert(std::next(comparable.begin()), 3, get_test_param()); + comparable.insert(cit, 3, get_test_param()[0]); + comparable.insert(comparable.end(), 3, get_test_param()); + + ASSERT(ranges_equal(v, comparable), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + ("const_vector::insert(const_iter pos, InputIt first, InputIt last)", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + std::vector src; + std::ranges::copy(get_test_param(), std::back_inserter(src)); + + cc::const_vector()) * 3> v(get_test_param()); + + auto it = v.insert(std::next(v.begin()), std::begin(get_test_param()), std::end(get_test_param())); + v.insert(it, src.begin(), std::next(src.begin() + src.size() / 2)); + v.insert(v.end(), std::next(src.begin() + src.size() / 2), src.end()); + + ASSERT_THROWS((v.insert(v.end(), src.begin(), src.end())), std::length_error, ctx); + ASSERT_NOTHROW((v.insert(v.end(), src.begin(), src.begin())), std::length_error, ctx); + + ASSERT(std::ranges::equal(src, get_test_param())); + + std::vector comparable; + std::ranges::copy(get_test_param(), std::back_inserter(comparable)); + + auto cit = comparable.insert(std::next(comparable.begin()), std::begin(get_test_param()), std::end(get_test_param())); + comparable.insert(cit, src.begin(), std::next(src.begin() + src.size() / 2)); + comparable.insert(comparable.end(), std::next(src.begin() + src.size() / 2), src.end()); + + ASSERT(ranges_equal(v, comparable), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + ("const_vector::insert(const_iter pos, std::initializer_list values)", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + cc::const_vector().size() * 4> v(get_test_param()); + + auto it = v.insert(std::next(v.begin()), get_test_param()); + v.insert(it, get_test_param()); + v.insert(v.end(), get_test_param()); + + ASSERT_THROWS((v.insert(v.end(), get_test_param())), std::length_error, ctx); + + std::vector comparable; + std::ranges::copy(get_test_param(), std::back_inserter(comparable)); + + auto cit = comparable.insert(std::next(comparable.begin()), get_test_param()); + comparable.insert(cit, get_test_param()); + comparable.insert(comparable.end(), get_test_param()); + + ASSERT(ranges_equal(v, comparable), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + + ("const_vector::emplace(const_iterator pos, Args &&... args)", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + cc::const_vector().size() * 4> v(get_test_param()); + + auto it = v.emplace(); + + ASSERT_THROWS((v.insert(v.end(), get_test_param())), std::length_error, ctx); + + std::vector comparable; + std::ranges::copy(get_test_param(), std::back_inserter(comparable)); + + auto cit = comparable.insert(std::next(comparable.begin()), get_test_param()); + comparable.insert(cit, get_test_param()); + comparable.insert(comparable.end(), get_test_param()); + + ASSERT(ranges_equal(v, comparable), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL); + +int main() { + return tests.run(); +} \ No newline at end of file