updated equal funcs of asserts

This commit is contained in:
Patrick 2024-07-24 17:42:27 +02:00
parent 96554418e0
commit 13113ce484
1 changed files with 46 additions and 21 deletions

View File

@ -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<decltype(eq)>()), "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<std::iterator_traits<decltype(eq)::iterator>::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<decltype(eq)>()), "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<std::iterator_traits<decltype(eq)::iterator>::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<typename T>
constexpr auto all_eq_arr_elem_test_func(T&& eq) {
return [=] (auto&& e) constexpr { return e == eq; };
}
template<const char *>
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<T, std::string_view>;
template<std::input_iterator I, std::sentinel_for<I> 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<typename T>
constexpr auto all_eq_arr_arr_test_func() {
return [] (auto&& a, auto&& b) constexpr { return a == b; };
template<std::input_iterator I, std::sentinel_for<I> 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<const char *>
constexpr auto all_eq_arr_arr_test_func() {
return [] (auto&& a, auto&& b) constexpr { return std::string_view(a) == b; };
template<std::input_iterator I, std::sentinel_for<I> S, std::ranges::range R>
requires StringLike<std::ranges::range_value_t<R>>
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<std::input_iterator I, std::sentinel_for<I> 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<std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2>
requires StringLike<typename std::iterator_traits<I2>::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<std::input_iterator I1, std::sentinel_for<I1> 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 };