added type hints for ret_val

This commit is contained in:
Patrick 2024-07-23 01:49:09 +02:00
parent b10b6049c8
commit 856c76375e
2 changed files with 43 additions and 7 deletions

View File

@ -17,18 +17,32 @@
#define _EXPAND_MACRO(s) _EXPAND_STR(s)
#define _LOCATION " (at " _EXPAND_MACRO(__FILE__) ":" _EXPAND_MACRO(__LINE__) ")"
#define ADD_TYPE_HINT(type) template <> constexpr const char* const to_type_hint_str::value<type> = #type
#define TEST_FAIL(msg) ret_val_s { "", ReturnCode::FAILED, msg }
#define TEST_PASS() ret_val_s { "", ReturnCode::PASSED, nullptr }
#define TEST_PASS_MSG(msg) ret_val_s { "", ReturnCode::PASSED, msg }
#define TEST_SKIP() ret_val_s { "", ReturnCode::SKIPPED, nullptr }
#define TEST_SKIP() ret_val_s { "", ReturnCode::SKIPPED, nullptr}
#define ASSERT(condition) ASSERT_MSG(condition, #condition "" _LOCATION)
#define ASSERT_MSG(condition, msg) { if (!(condition)) return TEST_FAIL(msg); } static_assert(true, "")
#define ASSERT_ALL_EQ(first, last, eq) ASSERT_MSG(std::all_of(first, last, all_eq_arr_elem_test_func(eq)), "Not all elements in (" #first ", " #last ") equal " #eq "" _LOCATION)
#define ASSERT_C_ARR_EQ(first, last, eq) ASSERT_MSG(std::equal(first, last, eq, all_eq_arr_arr_test_func<decltype(eq)>()), "Elements in (" #first ", " #last ") and " #eq " differ" _LOCATION)
#define ASSERT_ITER_EQ(first, last, eq) ASSERT_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)
#define TEST_FAIL_TYPE(msg, type_hint) ret_val_s { "", ReturnCode::FAILED, msg, to_type_hint_str::value<type_hint> }
#define TEST_PASS_TYPE(type_hint) ret_val_s { "", ReturnCode::PASSED, nullptr, to_type_hint_str::value<type_hint> }
#define TEST_PASS_MSG_TYPE(msg, type_hint) ret_val_s { "", ReturnCode::PASSED, msg, to_type_hint_str::value<type_hint> }
#define TEST_SKIP_TYPE(type_hint) ret_val_s { "", ReturnCode::SKIPPED, nullptr, to_type_hint_str::value<type_hint> }
#define ASSERT_THROWS(operation, exception_type) if not consteval { try { operation; ASSERT_MSG(false, #operation " did not throw " #exception_type _LOCATION); } catch (exception_type &e) {} } static_assert(true, "")
#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(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_THROWS(operation, exception_type, type) if not consteval { try { operation; ASSERT_TYPE_MSG(false, #operation " did not throw " #exception_type _LOCATION, type); } catch (exception_type &e) {} } static_assert(true, "")
#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) {
@ -52,6 +66,18 @@ enum class EvalFlag { RUNTIME, CONSTEVAL, RUNTIME_CONSTEVAL };
enum class ReturnCode { FAILED = -1, PASSED = 0, SKIPPED = 1, NOT_EVALUATED = 2 };
struct to_type_hint_str {
template<typename T>
static constexpr const char *value = nullptr;
};
ADD_TYPE_HINT(bool);
ADD_TYPE_HINT(int);
ADD_TYPE_HINT(char);
ADD_TYPE_HINT(const char *);
ADD_TYPE_HINT(float);
ADD_TYPE_HINT(double);
template<typename Suite>
struct quick_test_def;
@ -62,6 +88,7 @@ struct ret_val_s {
const char *test_name = "";
ReturnCode val = ReturnCode::FAILED;
const char *msg = "";
const char *type_hint = nullptr;
};
template<std::size_t Nr>
@ -153,6 +180,9 @@ class test_suite {
if (ret.msg != nullptr) {
std::cout << "\n\t" << ret.msg;
}
if (ret.type_hint != nullptr) {
std::cout << "\n\t" << "with type '" << ret.type_hint << "'";
}
std::cout << std::endl;
ret_vals[i][0] = ret.val;
@ -164,6 +194,9 @@ class test_suite {
if (ret.msg != nullptr) {
std::cout << "\n\t" << ret.msg;
}
if (ret.type_hint != nullptr) {
std::cout << "\n\t" << "with type '" << ret.type_hint << "'";
}
std::cout << std::endl;
ret_vals[i][1] = ret.val;

View File

@ -4,6 +4,8 @@
#include <array>
#include <initializer_list>
#include <test.hpp>
#define C_ARR_LEN(arr) (sizeof(arr)/sizeof(arr[0]))
struct TestStruct {
@ -13,6 +15,7 @@ struct TestStruct {
constexpr bool operator==(const TestStruct &other) const { return x == other.x && c == other.c && std::string_view(s) == other.s; }
};
ADD_TYPE_HINT(TestStruct);
struct test_defs {