145 lines
4.4 KiB
C++
145 lines
4.4 KiB
C++
//
|
|
// Created by Patrick Maschek on 08/04/2024.
|
|
//
|
|
|
|
#ifndef CONST_CONTAINER_TEST_DEFINE_TEST_HPP_
|
|
#define CONST_CONTAINER_TEST_DEFINE_TEST_HPP_
|
|
|
|
#include <array>
|
|
#include <concepts>
|
|
#include <iostream>
|
|
#include <ranges>
|
|
#include <tuple>
|
|
|
|
#include "test_ret_val.h"
|
|
|
|
template<typename Suite>
|
|
struct quick_test_def;
|
|
|
|
enum class EvalFlag { RUNTIME, CONSTEVAL, RUNTIME_CONSTEVAL };
|
|
|
|
struct test_def_base {};
|
|
|
|
template<std::invocable Func>
|
|
struct test_def_func_base : public test_def_base {
|
|
using FuncType = Func;
|
|
|
|
const char *name;
|
|
Func func;
|
|
EvalFlag evalFlag = EvalFlag::RUNTIME;
|
|
|
|
consteval test_def_func_base(const char * name, Func func) : name(name), func(func) {}
|
|
consteval test_def_func_base(const char * name, Func func, EvalFlag evalFlag)
|
|
: name(name), func(func), evalFlag(evalFlag) {}
|
|
|
|
virtual constexpr std::invoke_result_t<Func> operator()() const { return std::apply(func, std::make_tuple()); }
|
|
|
|
consteval auto run_consteval() const { return operator()(); }
|
|
};
|
|
|
|
template<std::invocable Func, typename ...Args>
|
|
struct test_definition : public test_def_func_base<Func> {
|
|
using base = test_def_func_base<Func>;
|
|
static constexpr std::size_t ARG_SIZE = sizeof...(Args);
|
|
|
|
std::tuple<Args...> args;
|
|
|
|
consteval test_definition(const char *name, Func func, Args ...args)
|
|
: test_def_func_base<Func>(name, func), args(std::make_tuple(args...)) {}
|
|
|
|
consteval test_definition(const char *name, Func func, EvalFlag evalFlag, Args ...args)
|
|
: test_def_func_base<Func>(name, func, evalFlag), args(std::make_tuple(args...)) {}
|
|
|
|
constexpr std::invoke_result_t<Func, Args...> operator()() const override { return std::apply(base::func, args); }
|
|
};
|
|
|
|
|
|
template<typename ...TestDefs>
|
|
requires (sizeof...(TestDefs) == 0 || (std::derived_from<TestDefs, test_def_func_base<typename TestDefs::FuncType>> && ...))
|
|
struct test_suite {
|
|
|
|
template<typename NewTest>
|
|
using with_added_test_t = test_suite<TestDefs..., NewTest>;
|
|
|
|
static constexpr std::size_t TEST_NR = sizeof...(TestDefs);
|
|
|
|
const char *name;
|
|
std::tuple<TestDefs...> tests;
|
|
|
|
consteval test_suite(const char *name, std::tuple<TestDefs...> tests) : name(name), tests(tests) {}
|
|
|
|
template<std::size_t ...Is>
|
|
[[nodiscard]] constexpr auto get_tests() const {
|
|
return get_tests_delegate(std::make_index_sequence<TEST_NR>());
|
|
}
|
|
|
|
template<std::size_t ...Is>
|
|
[[nodiscard]] constexpr auto get_tests_delegate(std::index_sequence<Is...>) const {
|
|
return std::array { (std::reference_wrapper(std::get<Is>(tests)), ...) };
|
|
}
|
|
};
|
|
|
|
template<typename Suite>
|
|
struct quick_test_def {
|
|
Suite current;
|
|
|
|
template<std::invocable Func, typename ...Args>
|
|
consteval auto operator()(const char *name, Func func, EvalFlag evalFlag, Args... args) const {
|
|
auto test = test_definition(name, func, evalFlag, args...);
|
|
auto suite = test_suite(name, std::tuple_cat(current.tests, std::make_tuple(test)));
|
|
return quick_test_def<decltype(suite)> { .current = suite };
|
|
}
|
|
|
|
constexpr operator Suite() const {
|
|
return current;
|
|
}
|
|
};
|
|
|
|
template<typename ...TestDefs>
|
|
test_suite(quick_test_def<test_suite<TestDefs...>>) -> test_suite<TestDefs...>;
|
|
|
|
consteval auto define_tests(const char *name) {
|
|
return quick_test_def { test_suite<>{ name, std::make_tuple() } };
|
|
}
|
|
|
|
template<std::invocable Test>
|
|
requires (std::derived_from<Test, test_def_func_base<typename Test::FuncType>>)
|
|
static consteval ret_val_s run_consteval_test(const Test& test) {
|
|
return test();
|
|
}
|
|
|
|
template<typename ...TestDefs>
|
|
void evaluate_tests(const test_suite<TestDefs...>& tests) {
|
|
|
|
for (auto [i, test_ref] : std::ranges::views::enumerate(tests.get_tests())) {
|
|
auto test = test_ref.get();
|
|
|
|
std::cout << "Running Test: \"" << test.name << "\"\n";
|
|
if (test.evalFlag == EvalFlag::RUNTIME || test.evalFlag == EvalFlag::RUNTIME_CONSTEVAL) {
|
|
ret_val_s ret = test();
|
|
|
|
std::cout << "Result of Runtime Evaluation of Test \"" << ret.test_name << "\" (number: " << i
|
|
<< "): "
|
|
<< (ret.val == ReturnCode::PASSED ? "PASSED" : "FAILED") << "\n"
|
|
<< "\t" << ret.msg << "\n";
|
|
}
|
|
|
|
std::optional<ret_val_s> cret;
|
|
|
|
if (test.evalFlag == EvalFlag::CONSTEVAL || test.evalFlag == EvalFlag::RUNTIME_CONSTEVAL) {
|
|
test.run_consteval();
|
|
}
|
|
|
|
if (cret.has_value()) {
|
|
auto ret = cret.value();
|
|
std::cout << "Result of Consteval Evaluation of Test \"" << ret.test_name << "\" (number: " << i
|
|
<< "): "
|
|
<< (ret.val == ReturnCode::PASSED ? "PASSED" : "FAILED") << "\n"
|
|
<< "\t" << ret.msg << "\n";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#endif //CONST_CONTAINER_TEST_DEFINE_TEST_HPP_
|