Arcane  4.1.12.0
Developer 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
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
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
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
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
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
128#define ASSERT_TRUE(condition) \
129 assertTrue(A_FUNCINFO, condition)
130
134#define PARALLEL_ASSERT_TRUE(condition, parallel_mng) \
135 assertTrue(A_FUNCINFO, condition, parallel_mng)
136
140#define ASSERT_FALSE(condition) \
141 assertFalse(A_FUNCINFO, condition)
142
146#define PARALLEL_ASSERT_FALSE(condition, parallel_mng) \
147 assertFalse(A_FUNCINFO, condition, parallel_mng)
148
153#define ASSERT_EQUAL(expected, actual) \
154 assertEqual(A_FUNCINFO, expected, actual)
155
160#define PARALLEL_ASSERT_EQUAL(expected, actual, parallel_mng) \
161 assertEqual(A_FUNCINFO, expected, actual, parallel_mng)
162
167#define ASSERT_NEARLY_EQUAL(expected, actual) \
168 assertNearlyEqual(A_FUNCINFO, expected, actual)
169
174#define PARALLEL_ASSERT_NEARLY_EQUAL(expected, actual, parallel_mng) \
175 assertNearlyEqual(A_FUNCINFO, expected, actual, parallel_mng)
176
182#define ASSERT_NEARLY_ZERO(actual) \
183 assertNearlyZero(A_FUNCINFO, actual)
184
190#define PARALLEL_ASSERT_NEARLY_ZERO(actual, parallel_mng) \
191 assertNearlyZero(A_FUNCINFO, actual, parallel_mng)
192
197#define ASSERT_NEARLY_EQUAL_EPSILON(expected, actual, epsilon) \
198 assertNearlyEqualWithEpsilon(A_FUNCINFO, expected, actual, epsilon)
199
204#define PARALLEL_ASSERT_NEARLY_EQUAL_EPSILON(expected, actual, epsilon, parallel_mng) \
205 assertNearlyEqualWithEpsilon(A_FUNCINFO, expected, actual, epsilon, parallel_mng)
206
212#define ASSERT_NEARLY_ZERO_EPSILON(actual, epsilon) \
213 assertNearlyZeroWithEpsilon(A_FUNCINFO, actual, epsilon)
214
220#define PARALLEL_ASSERT_NEARLY_ZERO_EPSILON(actual, epsilon, parallel_mng) \
221 assertNearlyZeroWithEpsilon(A_FUNCINFO, actual, epsilon, parallel_mng)
222
228#define ASSERT_EQUALS(expected, actual) \
229 assertEqual(A_FUNCINFO, expected, actual)
230
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.
Exception in an assertion.
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 --