Arcane  v3.16.8.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Assertion.h
Aller à la documentation de ce fichier.
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* Ensemble d'assertions utilisées pour les tests unitaires. */
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/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
41class ARCANE_CORE_EXPORT Assertion
42{
43 private:
44
45 void _checkAssertion(bool is_error,const TraceInfo& where,
46 const String& expected, const String& actual, IParallelMng* pm);
47
48 public:
49
50 void fail(const TraceInfo& where)
51 {
52 throw AssertionException(where);
53 }
54
56 void assertTrue(const TraceInfo& where, bool condition, IParallelMng* pm = nullptr)
57 {
58 bool is_error = (!condition);
59 _checkAssertion(is_error, where, "true", "false", pm);
60 }
61
63 void assertFalse(const TraceInfo& where, bool condition, IParallelMng* pm = nullptr)
64 {
65 bool is_error = (condition);
66 _checkAssertion(is_error, where, "false", "true", pm);
67 }
68
73 void assertEqual(const TraceInfo& where, const String& expected,
74 const String& actual, IParallelMng* pm = nullptr)
75 {
76 bool is_error = (expected != actual);
77 _checkAssertion(is_error,where,expected,actual,pm);
78 }
79
80 template<typename T>
81 void assertEqual(const TraceInfo& where, const T& expected, const T& actual, IParallelMng* pm = nullptr)
82 {
83 // utilisation de l'operateur == pour les types numeriques et non !=
84 bool is_error = (! (expected == actual));
85 _checkAssertion(is_error,where,String::fromNumber(expected),String::fromNumber(actual),pm);
86 }
87
88 template<typename T>
89 void assertNearlyEqual(const TraceInfo& where, const T& expected,
90 const T& actual, IParallelMng* pm = nullptr)
91 {
92 bool is_error = (!math::isNearlyEqual(expected,actual));
93 _checkAssertion(is_error,where,String::fromNumber(expected),String::fromNumber(actual),pm);
94 }
95
96 template<typename T>
97 void assertNearlyZero(const TraceInfo& where, const T& actual, IParallelMng* pm = nullptr)
98 {
99 bool is_error = (!math::isNearlyZero(actual));
100 _checkAssertion(is_error, where, "0", String::fromNumber(actual),pm);
101 }
102
103 template<typename T>
104 void assertNearlyEqualWithEpsilon(const TraceInfo& where, const T& expected,
105 const T& actual,const T& epsilon, IParallelMng* pm = nullptr)
106 {
107 bool is_error = (!math::isNearlyEqualWithEpsilon(expected,actual,epsilon));
108 _checkAssertion(is_error, where, String::fromNumber(expected), String::fromNumber(actual), pm);
109 }
110
111 template<typename T>
112 void assertNearlyZeroWithEpsilon(const TraceInfo& where, const T& actual,
113 const T& epsilon, IParallelMng* pm = nullptr)
114 {
115 bool is_error = (!math::isNearlyZeroWithEpsilon(actual,epsilon));
116 _checkAssertion(is_error, where, "0", String::fromNumber(actual), pm);
117 }
118};
119
120#define FAIL fail(A_FUNCINFO)
121
125#define ASSERT_TRUE(condition) \
126assertTrue(A_FUNCINFO, condition)
127
131#define PARALLEL_ASSERT_TRUE(condition, parallel_mng) \
132 assertTrue(A_FUNCINFO, condition, parallel_mng)
133
137#define ASSERT_FALSE(condition) \
138assertFalse(A_FUNCINFO, condition)
139
143#define PARALLEL_ASSERT_FALSE(condition, parallel_mng) \
144 assertFalse(A_FUNCINFO, condition, parallel_mng)
145
150#define ASSERT_EQUAL(expected, actual) \
151assertEqual(A_FUNCINFO, expected, actual)
152
157#define PARALLEL_ASSERT_EQUAL(expected, actual, parallel_mng) \
158 assertEqual(A_FUNCINFO, expected, actual, parallel_mng)
159
164#define ASSERT_NEARLY_EQUAL(expected, actual) \
165assertNearlyEqual(A_FUNCINFO, expected, actual)
166
171#define PARALLEL_ASSERT_NEARLY_EQUAL(expected, actual, parallel_mng) \
172 assertNearlyEqual(A_FUNCINFO, expected, actual, parallel_mng)
173
179#define ASSERT_NEARLY_ZERO(actual) \
180assertNearlyZero(A_FUNCINFO, actual)
181
187#define PARALLEL_ASSERT_NEARLY_ZERO(actual, parallel_mng) \
188 assertNearlyZero(A_FUNCINFO, actual, parallel_mng)
189
194#define ASSERT_NEARLY_EQUAL_EPSILON(expected, actual, epsilon) \
195assertNearlyEqualWithEpsilon(A_FUNCINFO, expected, actual, epsilon)
196
201#define PARALLEL_ASSERT_NEARLY_EQUAL_EPSILON(expected, actual, epsilon, parallel_mng) \
202 assertNearlyEqualWithEpsilon(A_FUNCINFO, expected, actual, epsilon, parallel_mng)
203
209#define ASSERT_NEARLY_ZERO_EPSILON(actual,epsilon) \
210assertNearlyZeroWithEpsilon(A_FUNCINFO, actual, epsilon)
211
217#define PARALLEL_ASSERT_NEARLY_ZERO_EPSILON(actual,epsilon, parallel_mng) \
218 assertNearlyZeroWithEpsilon(A_FUNCINFO, actual, epsilon, parallel_mng)
219
225#define ASSERT_EQUALS(expected, actual) \
226assertEqual(A_FUNCINFO, expected, actual)
227
233#define ASSERT_NEARLY_EQUALS(expected, actual) \
234assertNearlyEqual(A_FUNCINFO, expected, actual)
235
236/*---------------------------------------------------------------------------*/
237/*---------------------------------------------------------------------------*/
238
239} // End namespace Arcane
240
241/*---------------------------------------------------------------------------*/
242/*---------------------------------------------------------------------------*/
243
244#endif
Déclarations des types généraux de Arcane.
Exception dans une assertion.
Classe de base pour assertions dans les tests unitaires.
Definition Assertion.h:42
void assertTrue(const TraceInfo &where, bool condition, IParallelMng *pm=nullptr)
Lance une exception AssertException si condition est faux.
Definition Assertion.h:56
void assertFalse(const TraceInfo &where, bool condition, IParallelMng *pm=nullptr)
Lance une exception AssertException si condition est vrai.
Definition Assertion.h:63
void assertEqual(const TraceInfo &where, const String &expected, const String &actual, IParallelMng *pm=nullptr)
Definition Assertion.h:73
Interface du gestionnaire de parallélisme pour un sous-domaine.
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-