Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
TestAssertList.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 "shambase/string.hpp"
20#include "TestAssert.hpp"
21#include "shambackends/sycl.hpp"
22#include <utility>
23
24namespace shamtest::details {
25
28
30 std::vector<TestAssert> asserts;
31
32 // define member function here
33 // to register asserts
34
35 inline void assert_bool_with_log(std::string assert_name, bool v, std::string log) {
36 asserts.push_back(
37 TestAssert{.value = v, .name = std::move(assert_name), .comment = std::move(log)});
38 }
39
41 inline static std::string gen_comment(std::string s, SourceLocation loc) {
42 return s + "\n" + loc.format_multiline();
43 }
44
46 [[deprecated("Please use the supplied testing macros instead")]]
47 inline void assert_bool(
48 std::string assert_name, bool v, SourceLocation loc = SourceLocation{}) {
49
50 asserts.push_back(
52 .value = v,
53 .name = std::move(assert_name),
54 .comment = "failed assert location : " + loc.format_one_line()});
55 }
56
58 template<class T1, class T2>
59 [[deprecated("Please use the supplied testing macros instead")]]
60 inline void assert_equal(
61 std::string assert_name, T1 a, T2 b, SourceLocation loc = SourceLocation{}) {
62
63 bool t = a == b;
64 std::string comment = "";
65
66 if (!t) {
67 comment = "left=" + std::to_string(a) + " right=" + std::to_string(b);
68 }
69
70 asserts.push_back(
71 TestAssert{
72 .value = t,
73 .name = std::move(assert_name),
74 .comment = gen_comment(comment, loc)});
75 }
76
78 template<class Acca, class Accb>
79 [[deprecated("Please use the supplied testing macros instead")]]
80 inline void assert_equal_array(
81 std::string assert_name,
82 Acca &acc_a,
83 Accb &acc_b,
84 u32 len,
86
87 bool t = true;
88 std::string comment = "";
89
90 for (u32 i = 0; i < len; i++) {
91 t = t && (acc_a[i] == acc_b[i]);
92 }
93
94 if (!t) {
95 comment += "left : \n";
96 comment += shambase::format_array(acc_a, len, 16, "{} ");
97 comment += "right : \n";
98 comment += shambase::format_array(acc_b, len, 16, "{} ");
99 }
100
101 asserts.push_back(
102 TestAssert{
103 .value = t,
104 .name = std::move(assert_name),
105 .comment = gen_comment(comment, loc)});
106 }
107
117 [[deprecated("Please use the supplied testing macros instead")]]
119 std::string assert_name, f64 a, f64 b, f64 eps, SourceLocation loc = SourceLocation{}) {
120 f64 diff = sycl::fabs(a - b);
121
122 bool t = diff < eps;
123 std::string comment = "";
124
125 if (!t) {
126 comment = "left=" + std::to_string(a) + " right=" + std::to_string(b)
127 + " diff=" + std::to_string(diff);
128 }
129
130 asserts.push_back(
131 TestAssert{
132 .value = t,
133 .name = std::move(assert_name),
134 .comment = gen_comment(comment, loc)});
135 }
136
139 std::string assert_name,
140 bool v,
141 std::string comment,
143 asserts.push_back(
145 .value = v,
146 .name = std::move(assert_name),
147 .comment = gen_comment(std::move(comment), loc)});
148 }
149
151 std::string serialize_json();
153 void serialize(std::basic_stringstream<byte> &stream);
155 static TestAssertList deserialize(std::basic_stringstream<byte> &reader);
156
158 inline u32 get_assert_count() { return asserts.size(); }
159
162 u32 cnt = 0;
163 for (TestAssert &a : asserts) {
164 if (a.value) {
165 cnt++;
166 }
167 }
168 return cnt;
169 }
170 };
171} // namespace shamtest::details
Source location utility.
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::string format_array(const It &iter, u32 len, u32 column_count, fmt::format_string< Tformat... > fmt)
Format an array of elements into a string.
Definition string.hpp:54
implementation details of the test library
Definition DataNode.cpp:20
shamtest::details::TestAssertList & asserts()
current test asserts
Definition shamtest.hpp:104
provide information about the source location
std::string format_multiline() const
format the location in multiple lines
Class to hold the list of assertion related to a test.
static TestAssertList deserialize(std::basic_stringstream< byte > &reader)
DeSerialize the assertion from binary format.
void assert_float_equal(std::string assert_name, f64 a, f64 b, f64 eps, SourceLocation loc=SourceLocation{})
Add an assertion testing a floating point equality up to precision eps.
u32 get_assert_success_count()
Get the number of successful assertions.
void assert_equal(std::string assert_name, T1 a, T2 b, SourceLocation loc=SourceLocation{})
Test for an equality.
void serialize(std::basic_stringstream< byte > &stream)
Serialize the assertion in binary format.
u32 get_assert_count()
Get number of assertion in the list.
std::string serialize_json()
Serialize the assertion in JSON.
void assert_bool(std::string assert_name, bool v, SourceLocation loc=SourceLocation{})
Test if the supplied boolean is true.
static std::string gen_comment(std::string s, SourceLocation loc)
Append the source location to the the supplied string to generate a comment.
std::vector< TestAssert > asserts
List of assertion held by the class.
void assert_add_comment(std::string assert_name, bool v, std::string comment, SourceLocation loc=SourceLocation{})
add an assertion with a comment
void assert_equal_array(std::string assert_name, Acca &acc_a, Accb &acc_b, u32 len, SourceLocation loc=SourceLocation{})
Assert equal on an array of values.