From 13113ce484fadf3756ea3512f423e89d959cb323 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 24 Jul 2024 17:42:27 +0200 Subject: [PATCH] updated equal funcs of asserts --- test/common_helper/test.hpp | 67 +++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/test/common_helper/test.hpp b/test/common_helper/test.hpp index f3d8a30..4fb43d9 100644 --- a/test/common_helper/test.hpp +++ b/test/common_helper/test.hpp @@ -36,42 +36,67 @@ #define ASSERT_TYPE_NUM(condition, type, num) ASSERT_TYPE_NUM_MSG(condition, #condition "" _LOCATION, type, num) #define ASSERT_TYPE_NUM_MSG(condition, msg, type, num) { if (!(condition)) return TEST_FAIL_TYPE_NUM(msg, type, num); } static_assert(true, "") -#define ASSERT_TYPE_NUM_ALL_EQ(first, last, eq, type, num) ASSERT_TYPE_NUM_MSG(std::all_of(first, last, all_eq_arr_elem_test_func(eq)), "Not all elements in (" #first ", " #last ") equal " #eq "" _LOCATION, type, num) -#define ASSERT_TYPE_NUM_C_ARR_EQ(first, last, eq, type, num) ASSERT_TYPE_NUM_MSG(std::equal(first, last, eq, all_eq_arr_arr_test_func()), "Elements in (" #first ", " #last ") and " #eq " differ" _LOCATION, type, num) -#define ASSERT_TYPE_NUM_ITER_EQ(first, last, eq, type, num) ASSERT_TYPE_NUM_MSG(std::equal(first, last, (eq).begin(), all_eq_arr_arr_test_func::value_type>()), "Elements in (" #first ", " #last ") and " #eq " differ" _LOCATION, type, num) +#define ASSERT_TYPE_NUM_ALL_EQ(first, last, eq, type, num) ASSERT_TYPE_NUM_MSG(all_eq_to(first, last, eq), "Not all elements in (" #first ", " #last ") equal " #eq "" _LOCATION, type, num) +#define ASSERT_TYPE_NUM_C_ARR_EQ(first, last, eq, type, num) ASSERT_TYPE_NUM_MSG(equal_to(first, last, eq), "Elements in (" #first ", " #last ") and " #eq " differ" _LOCATION, type, num) -#define ASSERT_TYPE(condition, type) ASSERT_TYPE_MSG(condition, #condition "" _LOCATION, type) -#define ASSERT_TYPE_MSG(condition, msg, type) { if (!(condition)) return TEST_FAIL_TYPE(msg, type); } static_assert(true, "") -#define ASSERT_TYPE_ALL_EQ(first, last, eq, type) ASSERT_TYPE_MSG(std::all_of(first, last, all_eq_arr_elem_test_func(eq)), "Not all elements in (" #first ", " #last ") equal " #eq "" _LOCATION, type) -#define ASSERT_TYPE_C_ARR_EQ(first, last, eq, type) ASSERT_TYPE_MSG(std::equal(first, last, eq, all_eq_arr_arr_test_func()), "Elements in (" #first ", " #last ") and " #eq " differ" _LOCATION, type) -#define ASSERT_TYPE_ITER_EQ(first, last, eq, type) ASSERT_TYPE_MSG(std::equal(first, last, (eq).begin(), all_eq_arr_arr_test_func::value_type>()), "Elements in (" #first ", " #last ") and " #eq " differ" _LOCATION, type) +#define ASSERT_TYPE(condition, type) ASSERT_TYPE_NUM(condition, type, -1ULL) +#define ASSERT_TYPE_MSG(condition, msg, type) ASSERT_TYPE_NUM_MSG(condition, msg, type -1ULL) +#define ASSERT_TYPE_ALL_EQ(first, last, eq, type) ASSERT_TYPE_NUM_ALL_EQ(first, last, eq, type, -1ULL) +#define ASSERT_TYPE_C_ARR_EQ(first, last, eq, type) ASSERT_TYPE_NUM_C_ARR_EQ(first, last, eq, type, -1ULL) #define ASSERT(condition) ASSERT_TYPE(condition, std::nullptr_t) #define ASSERT_MSG(condition, msg) ASSERT_TYPE_MSG(condition, msg, std::nullptr_t) #define ASSERT_ALL_EQ(first, last, eq) ASSERT_TYPE_ALL_EQ(first, last, eq, std::nullptr_t) #define ASSERT_C_ARR_EQ(first, last, eq) ASSERT_TYPE_C_ARR_EQ(first, last, eq, std::nullptr_t) -#define ASSERT_ITER_EQ(first, last, eq) ASSERT_TYPE_ITER_EQ(first, last, eq, std::nullptr_t) #define ASSERT_TYPE_NUM_THROWS(operation, exception_type, type, num) if not consteval { try { operation; ASSERT_TYPE_NUM_MSG(false, #operation " did not throw " #exception_type _LOCATION, type, num); } catch (exception_type &e) {} } static_assert(true, "") #define ASSERT_TYPE_THROWS(operation, exception_type, type) ASSERT_TYPE_NUM_THROWS(operation, exception_type, type, -1ULL) #define ASSERT_THROWS(operation, exception_type) ASSERT_TYPE_THROWS(operation, exception_type, std::nullptr_t) template -constexpr auto all_eq_arr_elem_test_func(T&& eq) { - return [=] (auto&& e) constexpr { return e == eq; }; -} -template -constexpr auto all_eq_arr_elem_test_func(const char* eq) { - return [=] (auto&& e) constexpr { return std::string_view(e) == eq; }; +concept StringLike = std::is_convertible_v; + +template S, StringLike T> +constexpr bool all_eq_to(I first, S last, T t) { + auto r1 = std::ranges::subrange(first, last); + auto r2 = std::ranges::views::repeat(std::string_view(t), std::ranges::distance(first, last)); + return std::ranges::equal(r1, r2); } -template -constexpr auto all_eq_arr_arr_test_func() { - return [] (auto&& a, auto&& b) constexpr { return a == b; }; +template S, typename T> +constexpr bool all_eq_to(I first, S last, T t) { + auto r1 = std::ranges::subrange(first, last); + auto r2 = std::ranges::views::repeat(t, std::ranges::distance(first, last)); + return std::ranges::equal(r1, r2); } -template -constexpr auto all_eq_arr_arr_test_func() { - return [] (auto&& a, auto&& b) constexpr { return std::string_view(a) == b; }; + +template S, std::ranges::range R> + requires StringLike> +constexpr bool equal_to(I first, S last, R r) { + auto r1 = std::ranges::subrange(first, last); + auto r2 = r | std::ranges::views::transform([](auto&& e) constexpr { return std::string_view(e); }); + return std::ranges::equal(r1, r2); +} + +template S, std::ranges::range R> +constexpr bool equal_to(I first, S last, R r) { + return std::ranges::equal(std::ranges::subrange(first, last), r); +} + +template S1, std::input_iterator I2> + requires StringLike::value_type> +constexpr bool equal_to(I1 first1, S1 last1, I2 first2) { + auto r1 = std::ranges::subrange(first1, last1); + auto r2 = std::ranges::subrange(first2, std::next(first2, std::distance(first1, last1))) + | std::ranges::views::transform([](auto&& e) constexpr { return std::string_view(e); }); + return std::ranges::equal(r1, r2); +} + +template S1, std::input_iterator I2> +constexpr bool equal_to(I1 first1, S1 last1, I2 first2) { + auto r1 = std::ranges::subrange(first1, last1); + auto r2 = std::ranges::subrange(first2, std::next(first2, std::distance(first1, last1))); + return std::ranges::equal(r1, r2); } enum class EvalFlag { RUNTIME, CONSTEVAL, RUNTIME_CONSTEVAL };