Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
ValueChecker.h
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/* ValueChecker.h (C) 2000-2024 */
9/* */
10/* Checking the validity of certain values. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_VALUECHECKER_H
13#define ARCANE_UTILS_VALUECHECKER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arcane/utils/OStringStream.h"
19#include "arcane/utils/TraceInfo.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
41class ARCANE_UTILS_EXPORT ValueChecker
42{
43 public:
44
45 ValueChecker(const TraceInfo& ti)
46 : m_trace_info(ti)
47 , m_nb_error(0)
48 , m_throw_on_error(true)
49 {}
50
51 public:
52
56 template <typename T1, typename T2, typename X = std::is_convertible<T2, T1>>
57 void areEqual(const T1& value, const T2& expected_value, const String& message)
58 {
59 if (value != expected_value) {
60 _addError(String::format("{0} value={1} expected={2}", message, value, expected_value));
61 }
62 }
63
68 template <typename T1, typename T2,
69 typename ValueType = typename T1::value_type,
70 typename X1 = std::is_convertible<T1, Span<const ValueType>>,
71 typename X2 = std::is_convertible<T1, Span<const ValueType>>>
72 void areEqualArray(const T1& x_values, const T2& x_expected_values,
73 const String& message)
74 {
75 auto values = static_cast<Span<const ValueType>>(x_values);
76 auto expected_values = static_cast<Span<const ValueType>>(x_expected_values);
77
78 Int64 nb_value = values.size();
79 Int64 nb_expected = expected_values.size();
80 if (nb_value != nb_expected) {
81 _addError(String::format("{0} bad array size n={1} expected={2}",
82 message, nb_value, nb_expected));
83 // Do not compare array elements if sizes
84 // are different.
85 return;
86 }
87
88 for (Int64 i = 0; i < nb_value; ++i) {
89 const ValueType& v = values[i];
90 const ValueType& e = expected_values[i];
91 if (v != e) {
92 _addError(String::format("{0} index={1} value={2} expected={3}", message, i, v, e));
93 }
94 }
95 }
96
101 template <typename T>
102 void areEqualArray(Span2<const T> values, Span2<const T> expected_values,
103 const String& message)
104 {
105 Int64 nb_value = values.dim1Size();
106 Int64 nb_expected = expected_values.dim1Size();
107 if (nb_value != nb_expected) {
108 _addError(String::format("{0} bad array size n={1} expected={2}",
109 message, nb_value, nb_expected));
110 // Do not compare array elements if sizes
111 // are different.
112 return;
113 }
114
115 for (Int64 i = 0; i < nb_value; ++i)
116 areEqualArray(values[i], expected_values[i], message);
117 }
118
123 template <typename T>
124 void areEqualArray(SmallSpan2<T> values, SmallSpan2<T> expected_values,
125 const String& message)
126 {
127 return areEqualArray(Span2<const T>(values), Span2<const T>(expected_values), message);
128 }
129
133 void throwIfError();
134
136 void setThrowOnError(bool v)
137 {
138 m_throw_on_error = v;
139 }
140
142 bool throwOnError() const { return m_throw_on_error; }
143
145 Integer nbError() const { return m_nb_error; }
146
147 private:
148
149 TraceInfo m_trace_info;
150 Integer m_nb_error;
151 OStringStream m_ostr;
152 String m_last_error_str;
153 bool m_throw_on_error;
154
155 private:
156
157 void _addError(const String& message);
158};
159
160/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163} // End namespace Arcane
164
165/*---------------------------------------------------------------------------*/
166/*---------------------------------------------------------------------------*/
167
168#endif
Declarations of types used in Arcane.
Output stream linked to a String.
View for a 2D array whose size is an 'Int32'.
Definition Span2.h:249
constexpr __host__ __device__ SizeType dim1Size() const
Number of elements in the first dimension.
Definition Span2.h:120
View for a 2D array whose size is an 'Int64'.
Definition Span2.h:324
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
View of an array of elements of type T.
Definition Span.h:635
void areEqual(const T1 &value, const T2 &expected_value, const String &message)
void areEqualArray(const T1 &x_values, const T2 &x_expected_values, const String &message)
Checks that the two arrays values and expected_values have the same values.
void areEqualArray(SmallSpan2< T > values, SmallSpan2< T > expected_values, const String &message)
Checks that the two arrays values and expected_values have the same values.
bool throwOnError() const
Indicates whether an exception is thrown in case of an error.
void areEqualArray(Span2< const T > values, Span2< const T > expected_values, const String &message)
Checks that the two 2D arrays values and expected_values have the same values.
Integer nbError() const
Number of errors.
void setThrowOnError(bool v)
Indicates whether an exception is thrown in case of an error.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.