From fd180efb6c71f6ed2abd85e1dc374403ca0cd814 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 29 Jul 2024 18:31:43 +0200 Subject: [PATCH] finished tests for const_vector --- .../const_vector_data_augmentation.test.cpp | 158 ++++++++++++------ 1 file changed, 108 insertions(+), 50 deletions(-) diff --git a/test/const_vector/const_vector_data_augmentation.test.cpp b/test/const_vector/const_vector_data_augmentation.test.cpp index 5e6bcb0..4afdada 100644 --- a/test/const_vector/const_vector_data_augmentation.test.cpp +++ b/test/const_vector/const_vector_data_augmentation.test.cpp @@ -1,58 +1,10 @@ #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; - } -}; +#include +#include constexpr test_suite tests = define_tests("Data Augmentation") ("const_vector::clear()", []() constexpr { @@ -295,6 +247,112 @@ constexpr test_suite tests = define_tests("Data Augmentation") return TEST_PASS(); }), 2, int, char, const char *, TestObj); + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + + ("const_vector::push_back()", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + auto value = make_copy(get_test_param()); + + cc::const_vector v; + + if constexpr (!std::is_same_v) { + v.push_back(T()); + } else { + v.push_back(""); + } + v.push_back(value); + v.push_back(force_move(value)); + + ASSERT_THROWS(v.push_back(get_test_param()), std::length_error, ctx); + + if constexpr (!std::is_same_v) { + ASSERT(v[0] == T()); + } else { + ASSERT(v[0] == std::string_view("")); + } + ASSERT((v[1] == get_test_param()), ctx); + ASSERT((v[2] == get_test_param()), ctx); + + ASSERT(v.size() == 3); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + ("const_vector::emplace_back()", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + auto value = make_copy(get_test_param()); + + cc::const_vector v; + + if constexpr (!std::is_same_v) { + ASSERT(&v.emplace_back(T()) == v.begin(), ctx); + } else { + ASSERT(&v.emplace_back("") == v.begin(), ctx); + } + if constexpr (!std::is_same_v) { + ASSERT(&v.emplace_back(value) == std::next(v.begin()), ctx); + } else { + ASSERT(&v.emplace_back(value.x, value.c, value.s) == std::next(v.begin()), ctx); + } + ASSERT(&v.emplace_back(force_move(value)) == std::next(v.begin(), 2), ctx); + + ASSERT_THROWS(v.emplace_back(get_test_param()), std::length_error, ctx); + + if constexpr (!std::is_same_v) { + ASSERT(v[0] == T()); + } else { + ASSERT(v[0] == std::string_view("")); + } + ASSERT((v[1] == get_test_param()), ctx); + ASSERT((v[2] == get_test_param()), ctx); + + ASSERT(v.size() == 3); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + ("const_vector::pop_back()", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + cc::const_vector v(get_test_param()); + + v.pop_back(); + + auto& arr = get_test_param(); + ASSERT(std::ranges::equal(v, arr | std::ranges::views::take(c_arr_len(arr) - 1)), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + + return TEST_PASS(); + }, EvalFlag::RUNTIME_CONSTEVAL) + + ("const_vector::swap()", []() constexpr { + + REPEAT_FOR_TYPES_N(([]() constexpr { + + cc::const_vector va(get_test_param()); + cc::const_vector()> vv(c_arr_len(get_test_param()), get_test_param()); + + va.swap(vv); + + ASSERT(all_equal_to(va, get_test_param())); + ASSERT(va.size() == c_arr_len(get_test_param()), ctx); + ASSERT(std::ranges::equal(vv, get_test_param()), ctx); + + return TEST_PASS(); + }), 2, int, char, const char *, TestObj); + return TEST_PASS(); }, EvalFlag::RUNTIME_CONSTEVAL);