Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Assertion.h
Go to the documentation of this file.
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Assertion.h (C) 2000-2025 */
9/* */
10/* Set of assertions used for unit tests. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_ASSERTION_H
13#define ARCANE_CORE_ASSERTION_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/TraceInfo.h"
18#include "arcane/utils/Numeric.h"
20#include "arcane/core/ArcaneException.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27class IParallelMng;
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32/*!
33 * \file Assertion.h
34 *
35 * This file contains the assertions used for unit tests.
36 */
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
41/*!
42 * \brief Base class for assertions in unit tests.
43 */
44class ARCANE_CORE_EXPORT Assertion
45{
46 private:
47
48 void _checkAssertion(bool is_error, const TraceInfo& where,
49 const String& expected, const String& actual, IParallelMng* pm);
50
51 public:
52
53 void fail(const TraceInfo& where)
54 {
55 throw AssertionException(where);
56 }
57
58 //! Throws an AssertException if \a condition is false.
59 void assertTrue(const TraceInfo& where, bool condition, IParallelMng* pm = nullptr)
60 {
61 bool is_error = (!condition);
62 _checkAssertion(is_error, where, "true", "false", pm);
63 }
64
65 //! Throws an AssertException if \a condition is true.
66 void assertFalse(const TraceInfo& where, bool condition, IParallelMng* pm = nullptr)
67 {
68 bool is_error = (condition);
69 _checkAssertion(is_error, where, "false", "true", pm);
70 }
71
72 /*!
73 * Overload for character strings. This allows comparing
74 * String with 'const char*' for example.
75 */
76 void assertEqual(const TraceInfo& where, const String& expected,
77 const String& actual, IParallelMng* pm = nullptr)
78 {
79 bool is_error = (expected != actual);
80 _checkAssertion(is_error, where, expected, actual, pm);
81 }
82
83 template <typename T>
84 void assertEqual(const TraceInfo& where, const T& expected, const T& actual, IParallelMng* pm = nullptr)
85 {
86 // using the == operator for numeric types and not !=
87 bool is_error = (!(expected == actual));
88 _checkAssertion(is_error, where, String::fromNumber(expected), String::fromNumber(actual), pm);
89 }
90
91 template <typename T>
92 void assertNearlyEqual(const TraceInfo& where, const T& expected,
93 const T& actual, IParallelMng* pm = nullptr)
94 {
95 bool is_error = (!math::isNearlyEqual(expected, actual));
96 _checkAssertion(is_error, where, String::fromNumber(expected), String::fromNumber(actual), pm);
97 }
98
99 template <typename T>
100 void assertNearlyZero(const TraceInfo& where, const T& actual, IParallelMng* pm = nullptr)
101 {
102 bool is_error = (!math::isNearlyZero(actual));
103 _checkAssertion(is_error, where, "0", String::fromNumber(actual), pm);
104 }
105
106 template <typename T>
107 void assertNearlyEqualWithEpsilon(const TraceInfo& where, const T& expected,
108 const T& actual, const T& epsilon, IParallelMng* pm = nullptr)
109 {
110 bool is_error = (!math::isNearlyEqualWithEpsilon(expected, actual, epsilon));
111 _checkAssertion(is_error, where, String::fromNumber(expected), String::fromNumber(actual), pm);
112 }
113
114 template <typename T>
115 void assertNearlyZeroWithEpsilon(const TraceInfo& where, const T& actual,
116 const T& epsilon, IParallelMng* pm = nullptr)
117 {
118 bool is_error = (!math::isNearlyZeroWithEpsilon(actual, epsilon));
119 _checkAssertion(is_error, where, "0", String::fromNumber(actual), pm);
120 }
121};
122
123#define FAIL fail(A_FUNCINFO)
124
125/*!
126 * \brief Checks that \a condition is true.
127 */
128#define ASSERT_TRUE(condition) \
129 assertTrue(A_FUNCINFO, condition)
130
131/*!
132 * \brief Checks in parallel that \a condition is true.
133 */
134#define PARALLEL_ASSERT_TRUE(condition, parallel_mng) \
135 assertTrue(A_FUNCINFO, condition, parallel_mng)
136
137/*!
138 * \brief Checks that \a condition is false.
139 */
140#define ASSERT_FALSE(condition) \
141 assertFalse(A_FUNCINFO, condition)
142
143/*!
144 * \brief Checks that \a condition is false.
145 */
146#define PARALLEL_ASSERT_FALSE(condition, parallel_mng) \
147 assertFalse(A_FUNCINFO, condition, parallel_mng)
148
149/*!
150 * \brief Checks that \a expected and \a actual are equal.
151 * The comparison is done via the type's ==() operator.
152 */
153#define ASSERT_EQUAL(expected, actual) \
154 assertEqual(A_FUNCINFO, expected, actual)
155
156/*!
157 * \brief Checks that \a expected and \a actual are equal.
158 * The comparison is done via the type's ==() operator.
159 */
160#define PARALLEL_ASSERT_EQUAL(expected, actual, parallel_mng) \
161 assertEqual(A_FUNCINFO, expected, actual, parallel_mng)
162
163/*!
164 * \brief Checks that \a expected and \a actual are nearly equal.
165 * \sa math::isNearlyEqual()
166 */
167#define ASSERT_NEARLY_EQUAL(expected, actual) \
168 assertNearlyEqual(A_FUNCINFO, expected, actual)
169
170/*!
171 * \brief Checks that \a expected and \a actual are nearly equal.
172 * \sa math::isNearlyEqual()
173 */
174#define PARALLEL_ASSERT_NEARLY_EQUAL(expected, actual, parallel_mng) \
175 assertNearlyEqual(A_FUNCINFO, expected, actual, parallel_mng)
176
177/*!
178 * \brief Checks that \a actual is almost equal to zero
179 * using the standard epsilon
180 * \sa math::isNearlyZero()
181 */
182#define ASSERT_NEARLY_ZERO(actual) \
183 assertNearlyZero(A_FUNCINFO, actual)
184
185/*!
186 * \brief Checks that \a actual is almost equal to zero
187 * using the standard epsilon
188 * \sa math::isNearlyZero()
189 */
190#define PARALLEL_ASSERT_NEARLY_ZERO(actual, parallel_mng) \
191 assertNearlyZero(A_FUNCINFO, actual, parallel_mng)
192
193/*!
194 * \brief Checks that \a expected and \a actual are nearly equal.
195 * \sa math::isNearlyEqualWithEpsilon()
196 */
197#define ASSERT_NEARLY_EQUAL_EPSILON(expected, actual, epsilon) \
198 assertNearlyEqualWithEpsilon(A_FUNCINFO, expected, actual, epsilon)
199
200/*!
201 * \brief Checks that \a expected and \a actual are nearly equal.
202 * \sa math::isNearlyEqualWithEpsilon()
203 */
204#define PARALLEL_ASSERT_NEARLY_EQUAL_EPSILON(expected, actual, epsilon, parallel_mng) \
205 assertNearlyEqualWithEpsilon(A_FUNCINFO, expected, actual, epsilon, parallel_mng)
206
207/*!
208 * \brief Checks that \a actual is almost equal to zero
209 * using the specified epsilon \a epsilon
210 * \sa math::isNearlyZero()
211 */
212#define ASSERT_NEARLY_ZERO_EPSILON(actual, epsilon) \
213 assertNearlyZeroWithEpsilon(A_FUNCINFO, actual, epsilon)
214
215/*!
216 * \brief Checks that \a actual is almost equal to zero
217 * using the specified epsilon \a epsilon
218 * \sa math::isNearlyZero()
219 */
220#define PARALLEL_ASSERT_NEARLY_ZERO_EPSILON(actual, epsilon, parallel_mng) \
221 assertNearlyZeroWithEpsilon(A_FUNCINFO, actual, epsilon, parallel_mng)
222
223/*!
224 * \brief Checks that \a expected and \a actual are equal.
225 * The comparison is done via the type's ==() operator.
226 * \deprecated Use ASSERT_EQUAL() (without the S)
227 */
228#define ASSERT_EQUALS(expected, actual) \
229 assertEqual(A_FUNCINFO, expected, actual)
230
231/*!
232 * \brief Checks that \a expected and \a actual are nearly equal.
233 * \sa math::isNearlyEqual()
234 * \deprecated Use ASSERT_NEARLY_EQUAL() (without the S)
235 */
236#define ASSERT_NEARLY_EQUALS(expected, actual) \
237 assertNearlyEqual(A_FUNCINFO, expected, actual)
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
242} // End namespace Arcane
243
244/*---------------------------------------------------------------------------*/
245/*---------------------------------------------------------------------------*/
246
247#endif
Declarations of Arcane's general types.
Base class for assertions in unit tests.
Definition Assertion.h:45
void assertTrue(const TraceInfo &where, bool condition, IParallelMng *pm=nullptr)
Throws an AssertException if condition is false.
Definition Assertion.h:59
void assertFalse(const TraceInfo &where, bool condition, IParallelMng *pm=nullptr)
Throws an AssertException if condition is true.
Definition Assertion.h:66
void assertEqual(const TraceInfo &where, const String &expected, const String &actual, IParallelMng *pm=nullptr)
Definition Assertion.h:76
Interface of the parallelism manager for a subdomain.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --