Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
shamtest.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
18#include "details/Test.hpp"
19
24namespace shamtest {
25
30 namespace details {
31
36 inline std::vector<Test> static_init_vec_tests{};
37
43
49 inline explicit TestStaticInit(Test t) {
50 static_init_vec_tests.push_back(std::move(t));
51 }
52 };
53
58 extern TestResult current_test;
59
60 } // namespace details
61
63 struct TestConfig {
64
67
69 bool full_output = false;
70
72 bool output_tex = true;
73
75 std::optional<std::string> json_output = {};
76
77 bool run_long_tests = false;
78 bool run_unittest = true;
79 bool run_validation = true;
80 bool run_benchmark = false;
81
82 std::optional<std::string> run_only = {};
83 };
84
93 int run_all_tests(int argc, char *argv[], TestConfig cfg);
94
96 void gen_test_list(std::string_view outfile);
97
106
115
118
119} // namespace shamtest
120
131#define TestStart(type, name, func_name, node_cnt) \
132 void test_func_##func_name(); \
133 void (*test_func_ptr_##func_name)() = test_func_##func_name; \
134 shamtest::details::TestStaticInit test_class_obj_##func_name( \
135 shamtest::details::Test{type, name, node_cnt, test_func_ptr_##func_name}); \
136 void test_func_##func_name()
137
148#define TEX_REPORT(src) shamtest::details::current_test.tex_output += src;
149
150#define STDSTRINGIFY(x) std::string(#x)
151
153// Assert macros
155
156// Note : the do-while are here to enforce the presence of a semicolumn after the call to the macros
157
158namespace shamtest::details {
165 inline std::string format_assert_name(std::string s) {
166 if (s == "") {
167 return "";
168 }
169 return "\"" + s + "\" : ";
170 }
171} // namespace shamtest::details
172
183#define REQUIRE_NAMED(name, a) \
184 do { \
185 using namespace shamtest::details; \
186 bool eval = a; \
187 std::string assert_name = format_assert_name(name) + #a; \
188 if (eval) { \
189 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
190 } else { \
191 shamtest::asserts().assert_bool_with_log( \
192 assert_name, \
193 eval, \
194 STDSTRINGIFY(a) + " evaluated to false\n\n" \
195 + " -> location : " + SourceLocation{}.format_one_line()); \
196 } \
197 } while (0)
198
208#define REQUIRE_EQUAL_CUSTOM_COMP_NAMED(name, _a, _b, comp) \
209 do { \
210 auto _______a = _a; \
211 auto _______b = _b; \
212 using namespace shamtest::details; \
213 bool eval = comp(_______a, _______b); \
214 std::string assert_name = format_assert_name(name) + #_a " == " #_b; \
215 if (eval) { \
216 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
217 } else { \
218 shamtest::asserts().assert_bool_with_log( \
219 assert_name, \
220 eval, \
221 assert_name + " evaluated to false\n\n" + " -> " #_a \
222 + shambase::format(" = {}", _______a) + "\n" + " -> " #_b \
223 + shambase::format(" = {}", _______b) + "\n" \
224 + " -> location : " + SourceLocation{}.format_one_line()); \
225 } \
226 } while (0)
227
237#define REQUIRE_EQUAL_NAMED(name, a, b) \
238 REQUIRE_EQUAL_CUSTOM_COMP_NAMED(name, a, b, [](const auto &p1, const auto &p2) { \
239 return p1 == p2; \
240 })
241
251#define REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED(name, _a, _b, prec, dist) \
252 do { \
253 auto a = _a; \
254 auto b = _b; \
255 using namespace shamtest::details; \
256 bool eval = dist((a) - (b)) < prec; \
257 std::string assert_name \
258 = format_assert_name(name) + #dist "(" #_a ") - (" #_b ") < " #prec; \
259 if (eval) { \
260 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
261 } else { \
262 shamtest::asserts().assert_bool_with_log( \
263 assert_name, \
264 eval, \
265 assert_name + " evaluated to false\n\n" + shambase::format(" -> " #_a " = {}", a) \
266 + "\n" + shambase::format(" -> " #_b " = {}", b) + "\n" \
267 + shambase::format(" -> " #prec " = {}", prec) + "\n" \
268 + " -> location : " + SourceLocation{}.format_one_line()); \
269 } \
270 } while (0)
271
280#define REQUIRE_FLOAT_EQUAL_NAMED(name, a, b, prec) \
281 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED(name, a, b, prec, std::abs)
282
292#define REQUIRE(a) REQUIRE_NAMED("", a)
293
302#define REQUIRE_EQUAL_CUSTOM_COMP(a, b, comp) REQUIRE_EQUAL_CUSTOM_COMP_NAMED("", a, b, comp)
303
312#define REQUIRE_EQUAL(a, b) REQUIRE_EQUAL_NAMED("", a, b)
313
323#define REQUIRE_FLOAT_EQUAL_CUSTOM_DIST(name, a, b, prec, dist) \
324 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED("", a, b, prec, dist)
325
334#define REQUIRE_FLOAT_EQUAL(a, b, prec) \
335 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED("", a, b, prec, std::abs)
336
348#define REQUIRE_EXCEPTION_THROW(call, exception_type) \
349 do { \
350 try { \
351 /* Try to call the function that is expected to throw */ \
352 call; \
353 /* If no exception is thrown, assert that the test failed */ \
354 shamtest::asserts().assert_bool_with_log( \
355 #exception_type " was not thrown", \
356 false, \
357 "Expected throw of type " #exception_type ", but nothing was thrown\n" \
358 " -> location : " \
359 + SourceLocation{}.format_one_line()); \
360 } catch (const exception_type &ex) { \
361 /* If wanted exception is thrown, assert that the test pass */ \
362 shamtest::asserts().assert_bool_with_log( \
363 "Found wanted throw of type " #exception_type, true, ""); \
364 } catch (const std::exception &e) { \
365 /* If another exception type is thrown, assert that the test failed */ \
366 shamtest::asserts().assert_bool_with_log( \
367 #exception_type " was not thrown", \
368 false, \
369 "Expected throw of type " #exception_type ", but got " + std::string(e.what()) \
370 + "\n" + " -> location : " + SourceLocation{}.format_one_line()); \
371 } catch (...) { \
372 /* If an unknown exception is thrown, assert that the test failed */ \
373 shamtest::asserts().assert_bool_with_log( \
374 #exception_type " was not thrown", \
375 false, \
376 "Expected throw of type " #exception_type ", but got unknown exception\n" \
377 " -> location : " \
378 + SourceLocation{}.format_one_line()); \
379 } \
380 } while (0)
Namespace for internal details of the logs module.
implementation details of the test library
Definition DataNode.hpp:23
std::string format_assert_name(std::string s)
Format a string that is an assert name.
Definition shamtest.hpp:165
TestResult current_test
Current test being run.
Definition Test.cpp:22
std::vector< Test > static_init_vec_tests
Static init vector containing the list of all the tests in the code see : programming guide : Static ...
Definition shamtest.hpp:36
namespace containing stuff related to the test library
Definition DataNode.hpp:23
std::string & test_tex_out()
Get current tex output from a test.
Definition shamtest.hpp:117
void gen_test_list(std::string_view outfile)
output test list to a file
shamtest::details::TestDataList & test_data()
current test data
Definition shamtest.hpp:112
shamtest::details::TestAssertList & asserts()
current test asserts
Definition shamtest.hpp:103
int run_all_tests(int argc, char *argv[], TestConfig cfg)
run all the tests
Configuration of the test runner.
Definition shamtest.hpp:63
std::optional< std::string > run_only
Run only regex to select tests.
Definition shamtest.hpp:82
bool run_long_tests
run also long tests
Definition shamtest.hpp:77
bool run_benchmark
run benchmarks
Definition shamtest.hpp:80
bool output_tex
Should output a tex report.
Definition shamtest.hpp:72
bool run_unittest
run unittests
Definition shamtest.hpp:78
bool full_output
Should display all logs including all asserts.
Definition shamtest.hpp:69
std::optional< std::string > json_output
Should output a json report.
Definition shamtest.hpp:75
bool print_test_list_exit
Should print test list and then exit.
Definition shamtest.hpp:66
bool run_validation
run validation tests
Definition shamtest.hpp:79
Class to hold the list of assertion related to a test.
List of data generated by a test.
helper class to statically register tests
Definition shamtest.hpp:42
TestStaticInit(Test t)
This constructor register the given arguments into static_init_vec_tests
Definition shamtest.hpp:49
Informations about a test.
Definition Test.hpp:26