From fb22b549605b4b97372f01f78370f59af0d704c5 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 24 Jul 2024 19:01:45 +0200 Subject: [PATCH] added check whether consteval results are valid --- test/common_helper/test.hpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/test/common_helper/test.hpp b/test/common_helper/test.hpp index 4fb43d9..87709fd 100644 --- a/test/common_helper/test.hpp +++ b/test/common_helper/test.hpp @@ -124,7 +124,7 @@ inline std::ostream& operator<<(std::ostream& os, ReturnCode rc); struct ret_val_s { const char *test_name = ""; ReturnCode val = ReturnCode::FAILED; - const char *msg = ""; + const char *msg = nullptr; const char *type_hint = nullptr; std::size_t param_nr = -1; }; @@ -183,6 +183,26 @@ class test_definition_impl : public test_definition { ret_val_s _c_res; }; +class _has_been_const_evaluated_test : public test_definition { + public: + constexpr _has_been_const_evaluated_test() + : _res(_name, ReturnCode::FAILED, "Could not be evaluated at compile time") { + if consteval { + _res = ret_val_s { _name, ReturnCode::PASSED }; + } + } + + [[nodiscard]] constexpr ret_val_s evaluate() const override { return _res; } + + [[nodiscard]] const char *name() const override { return _name; } + [[nodiscard]] EvalFlag evalFlag() const override { return EvalFlag::CONSTEVAL; } + [[nodiscard]] const ret_val_s &c_res() const override { return _res; } + + private: + const char *_name = "Constant evaluation check"; + ret_val_s _res; +}; + template requires (sizeof...(TestDefs) == 0 || (std::derived_from && ...)) class test_suite { @@ -190,7 +210,8 @@ class test_suite { public: static constexpr std::size_t TEST_NR = sizeof...(TestDefs); - constexpr test_suite(const char *name, std::tuple tests) : _name(name), _tests(tests) {} + constexpr test_suite(const char *name, std::tuple tests) + : _name(name), _tests(tests), _c_test(_has_been_const_evaluated_test()) {} int run() const { @@ -267,7 +288,8 @@ class test_suite { << correct << "/" << num_tests << " tests evaluated correctly" << "\n" << failed << "/" << num_tests << " tests failed" << "\n" << full_skipped << "/" << num_tests << " tests skipped" << "\n" - << part_skipped << "/" << num_tests << " tests have been partially skipped" << "\n"; + << part_skipped << "/" << num_tests << " tests have been partially skipped" << "\n" + << (_c_test.c_res().val == ReturnCode::PASSED ? "Results of constant evaluation are valid" : "---Results of constant evaluation are invalid---") << "\n"; return -failed; } @@ -275,6 +297,7 @@ class test_suite { private: const char *_name; std::tuple _tests; + _has_been_const_evaluated_test _c_test; template static constexpr auto expand_test_tuple(const auto &tests, std::index_sequence) {