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
17
19#include "details/Test.hpp"
20
25namespace shamtest {
26
31 namespace details {
32
37 inline std::vector<Test> static_init_vec_tests{};
38
44
50 inline explicit TestStaticInit(Test t) {
51 static_init_vec_tests.push_back(std::move(t));
52 }
53 };
54
59 extern TestResult current_test;
60
61 } // namespace details
62
64 struct TestConfig {
65
68
70 bool full_output = false;
71
73 bool output_tex = true;
74
76 std::optional<std::string> json_output = {};
77
78 bool run_long_tests = false;
79 bool run_unittest = true;
80 bool run_validation = true;
81 bool run_benchmark = false;
82
83 std::optional<std::string> run_only = {};
84 };
85
94 int run_all_tests(int argc, char *argv[], TestConfig cfg);
95
97 void gen_test_list(std::string_view outfile);
98
107
116
118 inline std::string &test_tex_out() { return shamtest::details::current_test.tex_output; }
119
120} // namespace shamtest
121
122#define ANY_WORLD_SIZE -1
123
124#define _internal_new_test(type, name, func_name, func_ptr, class_name, node_cnt) \
125 static void func_name(); \
126 static void (*func_ptr)() = func_name; \
127 static shamtest::details::TestStaticInit class_name( \
128 shamtest::details::Test{type, name, node_cnt, func_ptr}); \
129 static void func_name()
130
141#define NEW_TEST(type, name, node_cnt) \
142 _internal_new_test( \
143 type, \
144 name, \
145 __shamrock_unique_name(test_func_), \
146 __shamrock_unique_name(test_func_ptr_), \
147 __shamrock_unique_name(test_class_), \
148 node_cnt)
149
160#define TEX_REPORT(src) shamtest::details::current_test.tex_output += src;
161
162#define STDSTRINGIFY(x) std::string(#x)
163
165// Assert macros
167
168// Note : the do-while are here to enforce the presence of a semicolumn after the call to the macros
169
170namespace shamtest::details {
175 * Otherwise, returns the string with " | " appended
176 */
177 inline std::string format_assert_name(std::string s) {
178 if (s == "") {
179 return "";
180 }
181 return "\"" + s + "\" : ";
182 }
183} // namespace shamtest::details
184
193 * \endcode
194 */
195#define REQUIRE_NAMED(name, a) \
196 do { \
197 using namespace shamtest::details; \
198 bool eval = a; \
199 std::string assert_name = format_assert_name(name) + #a; \
200 if (eval) { \
201 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
202 } else { \
203 shamtest::asserts().assert_bool_with_log( \
204 assert_name, \
205 eval, \
206 STDSTRINGIFY(a) + " evaluated to false\n\n" \
207 + " -> location : " + SourceLocation{}.format_one_line()); \
208 } \
209 } while (0)
210
218 * \endcode
219 */
220#define REQUIRE_EQUAL_CUSTOM_COMP_NAMED(name, _a, _b, comp) \
221 do { \
222 auto _______a = _a; \
223 auto _______b = _b; \
224 using namespace shamtest::details; \
225 bool eval = comp(_______a, _______b); \
226 std::string assert_name = format_assert_name(name) + #_a " == " #_b; \
227 if (eval) { \
228 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
229 } else { \
230 shamtest::asserts().assert_bool_with_log( \
231 assert_name, \
232 eval, \
233 assert_name + " evaluated to false\n\n" + " -> " #_a \
234 + shambase::format(" = {}", _______a) + "\n" + " -> " #_b \
235 + shambase::format(" = {}", _______b) + "\n" \
236 + " -> location : " + SourceLocation{}.format_one_line()); \
237 } \
238 } while (0)
239
247 * \endcode
248 */
249#define REQUIRE_EQUAL_NAMED(name, a, b) \
250 REQUIRE_EQUAL_CUSTOM_COMP_NAMED(name, a, b, [](const auto &p1, const auto &p2) { \
251 return p1 == p2; \
252 })
253
261 * \endcode
262 */
263#define REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED(name, _a, _b, prec, dist) \
264 do { \
265 auto a = _a; \
266 auto b = _b; \
267 using namespace shamtest::details; \
268 bool eval = dist((a) - (b)) < prec; \
269 std::string assert_name \
270 = format_assert_name(name) + #dist "(" #_a ") - (" #_b ") < " #prec; \
271 if (eval) { \
272 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
273 } else { \
274 shamtest::asserts().assert_bool_with_log( \
275 assert_name, \
276 eval, \
277 assert_name + " evaluated to false\n\n" + shambase::format(" -> " #_a " = {}", a) \
278 + "\n" + shambase::format(" -> " #_b " = {}", b) + "\n" \
279 + shambase::format(" -> " #prec " = {}", prec) + "\n" \
280 + " -> location : " + SourceLocation{}.format_one_line()); \
281 } \
282 } while (0)
283
290 * \endcode
291 */
292#define REQUIRE_FLOAT_EQUAL_NAMED(name, a, b, prec) \
293 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED(name, a, b, prec, std::abs)
294
304#define REQUIRE(a) REQUIRE_NAMED("", a)
305
314#define REQUIRE_EQUAL_CUSTOM_COMP(a, b, comp) REQUIRE_EQUAL_CUSTOM_COMP_NAMED("", a, b, comp)
315
324#define REQUIRE_EQUAL(a, b) REQUIRE_EQUAL_NAMED("", a, b)
325
333 * \endcode
334 */
335#define REQUIRE_FLOAT_EQUAL_CUSTOM_DIST(name, a, b, prec, dist) \
336 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED("", a, b, prec, dist)
337
344 * \endcode
345 */
346#define REQUIRE_FLOAT_EQUAL(a, b, prec) \
347 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED("", a, b, prec, std::abs)
348
358 * @param exception_type Exception type that is expected to be thrown
359 */
360#define REQUIRE_EXCEPTION_THROW(call, exception_type) \
361 do { \
362 try { \
363 /* Try to call the function that is expected to throw */ \
364 call; \
365 /* If no exception 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 nothing was thrown\n" \
370 " -> location : " \
371 + SourceLocation{}.format_one_line()); \
372 } catch (const exception_type &ex) { \
373 /* If wanted exception is thrown, assert that the test pass */ \
374 shamtest::asserts().assert_bool_with_log( \
375 "Found wanted throw of type " #exception_type, true, ""); \
376 } catch (const std::exception &e) { \
377 /* If another exception type is thrown, assert that the test failed */ \
378 shamtest::asserts().assert_bool_with_log( \
379 #exception_type " was not thrown", \
380 false, \
381 "Expected throw of type " #exception_type ", but got " + std::string(e.what()) \
382 + "\n" + " -> location : " + SourceLocation{}.format_one_line()); \
383 } catch (...) { \
384 /* If an unknown exception is thrown, assert that the test failed */ \
385 shamtest::asserts().assert_bool_with_log( \
386 #exception_type " was not thrown", \
387 false, \
388 "Expected throw of type " #exception_type ", but got unknown exception\n" \
389 " -> location : " \
390 + SourceLocation{}.format_one_line()); \
391 } \
392 } while (0)
implementation details of the test library
Definition DataNode.cpp:20
std::string format_assert_name(std::string s)
Format a string that is an assert name.
Definition shamtest.hpp:175
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:37
namespace containing stuff related to the test library
Definition DataNode.cpp:20
std::string & test_tex_out()
Get current tex output from a test.
Definition shamtest.hpp:118
void gen_test_list(std::string_view outfile)
output test list to a file
Definition shamtest.cpp:586
shamtest::details::TestDataList & test_data()
current test data
Definition shamtest.hpp:113
shamtest::details::TestAssertList & asserts()
current test asserts
Definition shamtest.hpp:104
int run_all_tests(int argc, char *argv[], TestConfig cfg)
run all the tests
Definition shamtest.cpp:486
Configuration of the test runner.
Definition shamtest.hpp:64
std::optional< std::string > run_only
Run only regex to select tests.
Definition shamtest.hpp:83
bool run_long_tests
run also long tests
Definition shamtest.hpp:78
bool run_benchmark
run benchmarks
Definition shamtest.hpp:81
bool output_tex
Should output a tex report.
Definition shamtest.hpp:73
bool run_unittest
run unittests
Definition shamtest.hpp:79
bool full_output
Should display all logs including all asserts.
Definition shamtest.hpp:70
std::optional< std::string > json_output
Should output a json report.
Definition shamtest.hpp:76
bool print_test_list_exit
Should print test list and then exit.
Definition shamtest.hpp:67
bool run_validation
run validation tests
Definition shamtest.hpp:80
Class to hold the list of assertion related to a test.
List of data generated by a test.
TestStaticInit(Test t)
This constructor register the given arguments into static_init_vec_tests.
Definition shamtest.hpp:50
Informations about a test.
Definition Test.hpp:26
Provides macros for generating unique identifiers at compile time.