RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
RSTest.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <string_view>
5#include <source_location>
6#include <vector>
7#include <cassert>
8#include <exception>
9#include <chrono>
10
13
14namespace RST::RSTester {
15
16 constexpr const char* COLOR_GREEN = "\033[32m";
17 constexpr const char* COLOR_RED = "\033[31m";
18 constexpr const char* COLOR_YELLOW = "\033[33m";
19
20 constexpr const char* COLOR_BOLD_GREEN = "\033[1;32m";
21 constexpr const char* COLOR_BOLD_RED = "\033[1;31m";
22 constexpr const char* COLOR_BOLD_YELLOW = "\033[1;33m";
23 constexpr const char* COLOR_BOLD_CYAN = "\033[1;36m";
24 constexpr const char* COLOR_BOLD_WHITE = "\033[1;37m";
25 constexpr const char* COLOR_RESET = "\033[0m";
26
27 struct TestAbortException : public std::exception {};
28
29 template <typename T>
30 constexpr std::string tryToStr(const T& t){
31 if constexpr(requires{ (std::ostringstream() << t).str(); }){
32 return (std::ostringstream() << std::boolalpha << t).str();
33 } else {
34 return "<obj>";
35 }
36 }
37
38 struct info_t {
39 std::string_view expr = "";
40 std::source_location sl = std::source_location::current();
41 bool result = false;
42 std::string opt_lhs = "";
43 std::string opt_rhs = "";
44 };
45
46 template <class A>
47 struct check_t {
48 A a;
49 info_t& i;
50 #define CHECK_T_COMP_OP(OP) \
51 template <class B> \
52 info_t operator OP(B&& b) && { \
53 i.opt_lhs = tryToStr(a); \
54 i.opt_rhs = tryToStr(b); \
55 i.result = (a OP b); \
56 return std::move(i); \
57 } static_assert(true)
58 CHECK_T_COMP_OP(==);
59 CHECK_T_COMP_OP(!=);
60 CHECK_T_COMP_OP(> );
61 CHECK_T_COMP_OP(>=);
62 CHECK_T_COMP_OP(< );
63 CHECK_T_COMP_OP(<=);
64 #undef CHECK_T_COMP_OP
65 operator info_t() && {
66 i.opt_lhs = tryToStr(a);
67 i.result = a;
68 return std::move(i);
69 }
70 };
71
72 struct destruct_t {
73 info_t i;
74 template <class A>
75 check_t<A> operator<(A&& a) && {
76 return check_t<A>{std::forward<A>(a), i};
77 }
78 };
79
80 #define DECOMPOSE_CMP_OP(expr, lit) RST::RSTester::destruct_t{RST::RSTester::info_t{lit ""}} < expr
81
82 inline void checkInternal(info_t i, std::string_view additional_info = ""){
83 (void)additional_info;
84 if (!i.result) {
85 std::cerr << " " << i.sl.file_name() << ":" << i.sl.line() << " -> "
86 << COLOR_RED << "FAILED\n" << COLOR_RESET
87 << " Expression: " << i.expr << "\n"
88 << " Values: " << i.opt_lhs << " and " << i.opt_rhs << "\n";
89 throw TestAbortException();
90 }
91 }
92
94 #define CHECK(e, ...) RST::RSTester::checkInternal(DECOMPOSE_CMP_OP(e, #e) __VA_OPT__(,) __VA_ARGS__)
95
96 struct testfunction{
97 using function_sig = void();
98 function_sig& function;
99 std::string_view name;
100 };
101
102 class TestRunner {
103 public:
104 static TestRunner& getInstance() {
105 static TestRunner instance;
106 return instance;
107 }
108
109 int addTest(testfunction test) {
110 _tests.push_back(test);
111 return 0;
112 }
113
114 int runAllTests() {
115 auto time_start_global = std::chrono::high_resolution_clock::now();
116
117 std::cout << COLOR_BOLD_YELLOW << "[==========] " << COLOR_RESET << "Running " << _tests.size() << " tests.\n";
118 int failed_count = 0;
119 int total_tests = 0;
120
121 for (const auto& t : _tests) {
122 std::cout << COLOR_BOLD_CYAN << "\n[ RUN ] " << COLOR_RESET << COLOR_BOLD_WHITE << t.name << "\n" << COLOR_RESET;
123 bool passed = true;
124 auto time_start_test = std::chrono::high_resolution_clock::now();
125
126 try {
127 total_tests++;
128 t.function();
129 } catch (const TestAbortException&) {
130 std::cout << " -> Test aborted due to fatal failure.\n";
131 failed_count++;
132 passed = false;
133 } catch (const std::exception& e) {
134 std::cerr << " -> Unhandled exception: " << e.what() << "\n";
135 failed_count++;
136 passed = false;
137 }
138
139 auto time_end_test = std::chrono::high_resolution_clock::now();
140 auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_end_test - time_start_test).count();
141
142 if (passed) {
143 std::cout << COLOR_GREEN << "[ OK ] " << COLOR_RESET << t.name << " (" << duration_ms << " ms)\n";
144 } else {
145 std::cout << COLOR_RED << "[ TEST FAILED ] " << COLOR_RESET << t.name << " (" << duration_ms << " ms)\n";
146 }
147 }
148 auto time_end_global = std::chrono::high_resolution_clock::now();
149 auto total_duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_end_global - time_start_global).count();
150
151 std::cout << COLOR_BOLD_YELLOW << "\n[==========] " << COLOR_RESET << "Test execution finished. (" << total_duration_ms << " ms total)\n";
152 std::cout << COLOR_BOLD_CYAN << "[ RESULT ] " << COLOR_RESET << total_tests - failed_count << "/" << total_tests << " tests passed (" << ((total_tests > 0) ? ((total_tests - failed_count) * 100 / total_tests) : 0) << "%)\n\n";
153
154
155 if (failed_count > 0) {
156 std::cout << COLOR_BOLD_RED << "[ FAILED ] " << COLOR_RESET << COLOR_BOLD_WHITE << failed_count << " tests failed.\n" << COLOR_RESET;
157 } else {
158 std::cout << COLOR_BOLD_GREEN << "[ PASSED ] " << COLOR_RESET << COLOR_BOLD_WHITE << "All tests passed!\n" << COLOR_RESET;
159 }
160 return failed_count;
161 }
162 private:
163 TestRunner() = default;
164 std::vector<testfunction> _tests = {};
165 };
166
167 #define CONCAT_IMPL(x, y) x##y
168 #define CONCAT(x, y) CONCAT_IMPL(x, y)
170 #define TEST_CASE(name) \
171 static void name(); \
172 static int CONCAT(impl, __LINE__) = RST::RSTester::TestRunner::getInstance().addTest({name, #name}); \
173 static void name()
174
176 #define RUN_ALL_TESTS() RST::RSTester::TestRunner::getInstance().runAllTests()
177}