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
19#include "shambase/string.hpp"
20#include "TestAssert.hpp"
21#include "shambackends/sycl.hpp"
22
23namespace shamtest::details {
24
27
29 std::vector<TestAssert> asserts;
30
31 // define member function here
32 // to register asserts
33
34 inline void assert_bool_with_log(std::string assert_name, bool v, std::string log) {
35 asserts.push_back(TestAssert{v, std::move(assert_name), std::move(log)});
36 }
37
39 inline static std::string gen_comment(std::string s, SourceLocation loc) {
40 return s + "\n" + loc.format_multiline();
41 }
42
44 [[deprecated("Please use the supplied testing macros instead")]]
45 inline void assert_bool(
46 std::string assert_name, bool v, SourceLocation loc = SourceLocation{}) {
47
48 asserts.push_back(
50 v,
51 std::move(assert_name),
52 "failed assert location : " + loc.format_one_line()});
53 }
54
56 template<class T1, class T2>
57 [[deprecated("Please use the supplied testing macros instead")]]
58 inline void assert_equal(
59 std::string assert_name, T1 a, T2 b, SourceLocation loc = SourceLocation{}) {
60
61 bool t = a == b;
62 std::string comment = "";
63
64 if (!t) {
65 comment = "left=" + std::to_string(a) + " right=" + std::to_string(b);
66 }
67
68 asserts.push_back(TestAssert{t, std::move(assert_name), gen_comment(comment, loc)});
69 }
70
72 template<class Acca, class Accb>
73 [[deprecated("Please use the supplied testing macros instead")]]
74 inline void assert_equal_array(
75 std::string assert_name,
76 Acca &acc_a,
77 Accb &acc_b,
78 u32 len,
80
81 bool t = true;
82 std::string comment = "";
83
84 for (u32 i = 0; i < len; i++) {
85 t = t && (acc_a[i] == acc_b[i]);
86 }
87
88 if (!t) {
89 comment += "left : \n";
90 comment += shambase::format_array(acc_a, len, 16, "{} ");
91 comment += "right : \n";
92 comment += shambase::format_array(acc_b, len, 16, "{} ");
93 }
94
95 asserts.push_back(TestAssert{t, std::move(assert_name), gen_comment(comment, loc)});
96 }
97
107 [[deprecated("Please use the supplied testing macros instead")]]
109 std::string assert_name, f64 a, f64 b, f64 eps, SourceLocation loc = SourceLocation{}) {
110 f64 diff = sycl::fabs(a - b);
111
112 bool t = diff < eps;
113 std::string comment = "";
114
115 if (!t) {
116 comment = "left=" + std::to_string(a) + " right=" + std::to_string(b)
117 + " diff=" + std::to_string(diff);
118 }
119
120 asserts.push_back(TestAssert{t, std::move(assert_name), gen_comment(comment, loc)});
121 }
122
125 std::string assert_name,
126 bool v,
127 std::string comment,
129 asserts.push_back(TestAssert{v, std::move(assert_name), gen_comment(comment, loc)});
130 }
131
133 std::string serialize_json();
135 void serialize(std::basic_stringstream<byte> &stream);
137 static TestAssertList deserialize(std::basic_stringstream<byte> &reader);
138
140 inline u32 get_assert_count() { return asserts.size(); }
141
144 u32 cnt = 0;
145 for (TestAssert &a : asserts) {
146 if (a.value) {
147 cnt++;
148 }
149 }
150 return cnt;
151 }
152 };
153} // 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:111
implementation details of the test library
Definition DataNode.hpp:23
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.