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#include <optional>
21
26namespace shamtest {
27
32 namespace details {
33
38 inline std::vector<Test> static_init_vec_tests{};
39
45
51 inline explicit TestStaticInit(Test t) {
52 static_init_vec_tests.push_back(std::move(t));
53 }
54 };
55
60 extern TestResult current_test;
61
62 } // namespace details
63
65 struct TestConfig {
66
69
71 bool full_output = false;
72
74 bool output_tex = true;
75
77 std::optional<std::string> json_output = {};
78
79 bool run_long_tests = false;
80 bool run_unittest = true;
81 bool run_validation = true;
82 bool run_benchmark = false;
83
84 std::optional<std::string> run_only = {};
85 };
86
95 int run_all_tests(int argc, char *argv[], TestConfig cfg);
96
98 void gen_test_list(std::string_view outfile);
99
108
117
119 inline std::string &test_tex_out() { return shamtest::details::current_test.tex_output; }
120
121} // namespace shamtest
122
123#define ANY_WORLD_SIZE -1
124
125#define _internal_new_test(type, name, func_name, func_ptr, class_name, node_cnt) \
126 static void func_name(); \
127 static void (*func_ptr)() = func_name; \
128 static shamtest::details::TestStaticInit class_name( \
129 shamtest::details::Test{type, name, node_cnt, func_ptr}); \
130 static void func_name()
131
142#define NEW_TEST(type, name, node_cnt) \
143 _internal_new_test( \
144 type, \
145 name, \
146 __shamrock_unique_name(test_func_), \
147 __shamrock_unique_name(test_func_ptr_), \
148 __shamrock_unique_name(test_class_), \
149 node_cnt)
150
161#define TEX_REPORT(src) shamtest::details::current_test.tex_output += src;
162
163#define STDSTRINGIFY(x) std::string(#x)
164
166// Assert macros
168
169// Note : the do-while are here to enforce the presence of a semicolumn after the call to the macros
170
171namespace shamtest::details {
176 * Otherwise, returns the string with " | " appended
177 */
178 inline std::string format_assert_name(std::string s) {
179 if (s == "") {
180 return "";
181 }
182 return "\"" + s + "\" : ";
183 }
184} // namespace shamtest::details
185
194 * \endcode
195 */
196#define REQUIRE_NAMED(name, a) \
197 do { \
198 using namespace shamtest::details; \
199 bool eval = a; \
200 std::string assert_name = format_assert_name(name) + #a; \
201 if (eval) { \
202 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
203 } else { \
204 shamtest::asserts().assert_bool_with_log( \
205 assert_name, \
206 eval, \
207 STDSTRINGIFY(a) + " evaluated to false\n\n" \
208 + " -> location : " + SourceLocation{}.format_one_line()); \
209 } \
210 } while (0)
211
219 * \endcode
220 */
221#define REQUIRE_EQUAL_CUSTOM_COMP_NAMED(name, _a, _b, comp) \
222 do { \
223 auto _______a = _a; \
224 auto _______b = _b; \
225 using namespace shamtest::details; \
226 bool eval = comp(_______a, _______b); \
227 std::string assert_name = format_assert_name(name) + #_a " == " #_b; \
228 if (eval) { \
229 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
230 } else { \
231 shamtest::asserts().assert_bool_with_log( \
232 assert_name, \
233 eval, \
234 assert_name + " evaluated to false\n\n" + " -> " #_a \
235 + shambase::format(" = {}", _______a) + "\n" + " -> " #_b \
236 + shambase::format(" = {}", _______b) + "\n" \
237 + " -> location : " + SourceLocation{}.format_one_line()); \
238 } \
239 } while (0)
240
248 * \endcode
249 */
250#define REQUIRE_EQUAL_NAMED(name, a, b) \
251 REQUIRE_EQUAL_CUSTOM_COMP_NAMED(name, a, b, [](const auto &p1, const auto &p2) { \
252 return p1 == p2; \
253 })
254
262 * \endcode
263 */
264#define REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED(name, _a, _b, prec, dist) \
265 do { \
266 auto a = _a; \
267 auto b = _b; \
268 using namespace shamtest::details; \
269 bool eval = dist((a) - (b)) < prec; \
270 std::string assert_name \
271 = format_assert_name(name) + #dist "(" #_a ") - (" #_b ") < " #prec; \
272 if (eval) { \
273 shamtest::asserts().assert_bool_with_log(assert_name, eval, ""); \
274 } else { \
275 shamtest::asserts().assert_bool_with_log( \
276 assert_name, \
277 eval, \
278 assert_name + " evaluated to false\n\n" + shambase::format(" -> " #_a " = {}", a) \
279 + "\n" + shambase::format(" -> " #_b " = {}", b) + "\n" \
280 + shambase::format(" -> " #prec " = {}", prec) + "\n" \
281 + " -> location : " + SourceLocation{}.format_one_line()); \
282 } \
283 } while (0)
284
291 * \endcode
292 */
293#define REQUIRE_FLOAT_EQUAL_NAMED(name, a, b, prec) \
294 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED(name, a, b, prec, std::abs)
295
305#define REQUIRE(a) REQUIRE_NAMED("", a)
306
315#define REQUIRE_EQUAL_CUSTOM_COMP(a, b, comp) REQUIRE_EQUAL_CUSTOM_COMP_NAMED("", a, b, comp)
316
325#define REQUIRE_EQUAL(a, b) REQUIRE_EQUAL_NAMED("", a, b)
326
334 * \endcode
335 */
336#define REQUIRE_FLOAT_EQUAL_CUSTOM_DIST(name, a, b, prec, dist) \
337 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED("", a, b, prec, dist)
338
345 * \endcode
346 */
347#define REQUIRE_FLOAT_EQUAL(a, b, prec) \
348 REQUIRE_FLOAT_EQUAL_CUSTOM_DIST_NAMED("", a, b, prec, std::abs)
349
359 * @param exception_type Exception type that is expected to be thrown
360 */
361#define REQUIRE_EXCEPTION_THROW(call, exception_type) \
362 do { \
363 try { \
364 /* Try to call the function that is expected to throw */ \
365 call; \
366 /* If no exception is thrown, assert that the test failed */ \
367 shamtest::asserts().assert_bool_with_log( \
368 #exception_type " was not thrown", \
369 false, \
370 "Expected throw of type " #exception_type ", but nothing was thrown\n" \
371 " -> location : " \
372 + SourceLocation{}.format_one_line()); \
373 } catch (const exception_type &ex) { \
374 /* If wanted exception is thrown, assert that the test pass */ \
375 shamtest::asserts().assert_bool_with_log( \
376 "Found wanted throw of type " #exception_type, true, ""); \
377 } catch (const std::exception &e) { \
378 /* If another exception type is thrown, assert that the test failed */ \
379 shamtest::asserts().assert_bool_with_log( \
380 #exception_type " was not thrown", \
381 false, \
382 "Expected throw of type " #exception_type ", but got " + std::string(e.what()) \
383 + "\n" + " -> location : " + SourceLocation{}.format_one_line()); \
384 } catch (...) { \
385 /* If an unknown exception is thrown, assert that the test failed */ \
386 shamtest::asserts().assert_bool_with_log( \
387 #exception_type " was not thrown", \
388 false, \
389 "Expected throw of type " #exception_type ", but got unknown exception\n" \
390 " -> location : " \
391 + SourceLocation{}.format_one_line()); \
392 } \
393 } 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:176
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:38
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:119
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:114
shamtest::details::TestAssertList & asserts()
current test asserts
Definition shamtest.hpp:105
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:65
std::optional< std::string > run_only
Run only regex to select tests.
Definition shamtest.hpp:84
bool run_long_tests
run also long tests
Definition shamtest.hpp:79
bool run_benchmark
run benchmarks
Definition shamtest.hpp:82
bool output_tex
Should output a tex report.
Definition shamtest.hpp:74
bool run_unittest
run unittests
Definition shamtest.hpp:80
bool full_output
Should display all logs including all asserts.
Definition shamtest.hpp:71
std::optional< std::string > json_output
Should output a json report.
Definition shamtest.hpp:77
bool print_test_list_exit
Should print test list and then exit.
Definition shamtest.hpp:68
bool run_validation
run validation tests
Definition shamtest.hpp:81
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:51
Informations about a test.
Definition Test.hpp:26
Provides macros for generating unique identifiers at compile time.