From 856c76375e6c38c386550234d594a65e7bad2596 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 23 Jul 2024 01:49:09 +0200 Subject: [PATCH] added type hints for ret_val --- test/common_helper/test.hpp | 47 +++++++++++++++++++++++++++++------ test/const_vector/test_args.h | 3 +++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/test/common_helper/test.hpp b/test/common_helper/test.hpp index b30d6fa..cee1430 100644 --- a/test/common_helper/test.hpp +++ b/test/common_helper/test.hpp @@ -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 + #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()), "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::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 } +#define TEST_PASS_TYPE(type_hint) ret_val_s { "", ReturnCode::PASSED, nullptr, to_type_hint_str::value } +#define TEST_PASS_MSG_TYPE(msg, type_hint) ret_val_s { "", ReturnCode::PASSED, msg, to_type_hint_str::value } +#define TEST_SKIP_TYPE(type_hint) ret_val_s { "", ReturnCode::SKIPPED, nullptr, to_type_hint_str::value } -#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()), "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(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 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 + 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 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 @@ -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; diff --git a/test/const_vector/test_args.h b/test/const_vector/test_args.h index 71eb556..2dbc908 100644 --- a/test/const_vector/test_args.h +++ b/test/const_vector/test_args.h @@ -4,6 +4,8 @@ #include #include +#include + #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 {