Merge remote-tracking branch 'origin/master'
# Conflicts: # test/common_helper/test_define_test.hpp # test/const_vector/const_vector_constructor.test.cpp
This commit is contained in:
commit
40d4608477
|
|
@ -0,0 +1,161 @@
|
||||||
|
//
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
constexpr test_def_func_base(const char * name, Func func) : name(name), func(func) {}
|
||||||
|
constexpr 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()() { return std::apply(func, std::make_tuple()); }
|
||||||
|
|
||||||
|
consteval auto run_consteval() { 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;
|
||||||
|
|
||||||
|
constexpr test_definition(const char *name, Func func, Args ...args)
|
||||||
|
: test_def_func_base<Func>(name, func), args(std::make_tuple(args...)) {}
|
||||||
|
|
||||||
|
constexpr 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()() override { return std::apply(base::func, args); }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct test_suite_base {
|
||||||
|
const char *name;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename ...TestDefs>
|
||||||
|
requires (sizeof...(TestDefs) == 0 || (std::derived_from<TestDefs, test_def_func_base<typename TestDefs::FuncType>> && ...))
|
||||||
|
class test_suite : public test_suite_base {
|
||||||
|
|
||||||
|
public:
|
||||||
|
static constexpr std::size_t TEST_NR = sizeof...(TestDefs);
|
||||||
|
|
||||||
|
constexpr test_suite(const char *name, std::tuple<TestDefs...> tests) : test_suite_base(name), tests(tests) {}
|
||||||
|
|
||||||
|
|
||||||
|
ret_val<TEST_NR> run() const {
|
||||||
|
|
||||||
|
constexpr auto is = std::make_index_sequence<TEST_NR>();
|
||||||
|
|
||||||
|
std::cout << "--------------\n";
|
||||||
|
|
||||||
|
constexpr auto test_arr = expand_tuple(tests, is);
|
||||||
|
constexpr auto const_test_arr = test_arr
|
||||||
|
| std::views::enumerate
|
||||||
|
| std::views::filter([](auto e) consteval { return noexcept(e.run_consteval()); });
|
||||||
|
|
||||||
|
for (auto [i, test_ref] : std::ranges::views::enumerate(test_arr)) {
|
||||||
|
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) {
|
||||||
|
//cret = run_consteval_test<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret_val<TEST_NR> ret = { name };
|
||||||
|
//std::ranges::move(v, ret.vals.begin());
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr std::tuple<std::reference_wrapper<TestDefs>...> tests;
|
||||||
|
|
||||||
|
template<std::size_t ...Is>
|
||||||
|
static constexpr auto expand_tuple(const auto &tests, std::index_sequence<Is...>) {
|
||||||
|
return std::array { (std::reference_wrapper(std::get<Is>(tests)), ...) };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<auto Test, typename Func>
|
||||||
|
requires (std::derived_from<decltype(Test), test_def_func_base<Func>>)
|
||||||
|
static consteval ret_val_s run_consteval_test() {
|
||||||
|
return Test();
|
||||||
|
}
|
||||||
|
|
||||||
|
friend class quick_test_def<test_suite<TestDefs...>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Suite>
|
||||||
|
struct quick_test_def {
|
||||||
|
Suite current;
|
||||||
|
|
||||||
|
template<std::invocable Func, typename ...Args>
|
||||||
|
constexpr auto operator()(const char *name, Func func, Args... args) {
|
||||||
|
return operator()(name, func, EvalFlag::RUNTIME, std::forward(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::invocable Func, typename ...Args>
|
||||||
|
constexpr auto operator()(const char *name, Func func, EvalFlag evalFlag, Args... args) {
|
||||||
|
auto test = test_definition(name, func, evalFlag, args...);
|
||||||
|
auto new_suite = test_suite(current.name, std::tuple_cat(current.tests, std::make_tuple(test)));
|
||||||
|
return quick_test_def<decltype(new_suite)> { new_suite };
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr operator Suite() {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename ...TestDefs>
|
||||||
|
test_suite(quick_test_def<test_suite<TestDefs...>>) -> test_suite<TestDefs...>;
|
||||||
|
|
||||||
|
constexpr auto define_tests(const char *name) {
|
||||||
|
return quick_test_def { test_suite<>{ name, std::make_tuple() } };
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //CONST_CONTAINER_TEST_DEFINE_TEST_HPP_
|
||||||
Loading…
Reference in New Issue