Arcane  v4.1.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestValueConvert.cc
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#include <gtest/gtest.h>
9
10#include "arcane/utils/ValueConvert.h"
11#include "arcane/utils/internal/ValueConvertInternal.h"
12
13#include "arccore/base/internal/ConvertInternal.h"
14
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18using namespace Arcane;
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29template <typename T> void _checkBad(const String& s)
30{
31 T x = {};
32 bool is_bad = builtInGetValue(x, s);
33 std::cout << "CheckBad S=" << s << " X=" << x << " is_bad?=" << is_bad << "\n";
34 ASSERT_TRUE(is_bad);
35}
36
37void _checkDouble(const String& s, double expected_x)
38{
39 Real x = 0;
40 bool is_bad = builtInGetValue(x, s);
41 std::cout << "S=" << s << " X=" << x << " is_bad?=" << is_bad << "\n";
42 ASSERT_FALSE(is_bad);
43 ASSERT_EQ(x, expected_x);
44}
45
46void _checkNaN(const String& s)
47{
48 Real x = 0;
49 bool is_bad = builtInGetValue(x, s);
50 std::cout << "S=" << s << " X=" << x << " is_bad?=" << is_bad << "\n";
51 ASSERT_FALSE(is_bad);
52 ASSERT_TRUE(std::isnan(x));
53}
54
60void _checkValidDouble(double value, double expected_value)
61{
62 // Pour NaN, on ne peut pas faire la comparaison.
63 if (std::isnan(expected_value)) {
64 ASSERT_TRUE(std::isnan(value)) << "value " << value << " is not 'nan'";
65 return;
66 }
67 ASSERT_EQ(value, expected_value);
68}
69
70void _checkReal2(const String& s, Real2 expected_v)
71{
72 Real2 v = {};
73 bool is_bad = builtInGetValue(v, s);
74 std::cout << "S=" << s << " Real2=" << v << " is_bad?=" << is_bad << "\n";
75 ASSERT_FALSE(is_bad) << "Can not convert '" << s << "' to Real2";
76 bool do_test = false;
77 _checkValidDouble(v.x, expected_v.x);
78 _checkValidDouble(v.y, expected_v.y);
79 if (do_test) {
80 ASSERT_EQ(v, expected_v);
81 }
82}
83
84void _checkReal3(const String& s, Real3 expected_v)
85{
86 std::cout << "Real3Compare s='" << s << "'\n";
87 Real3 v = {};
88 bool is_bad = builtInGetValue(v, s);
89 std::cout << "S=" << s << " Real3=" << v << " is_bad?=" << is_bad << "\n";
90 ASSERT_FALSE(is_bad) << "Can not convert '" << s << "' to Real3";
91 bool do_test = false;
92 _checkValidDouble(v.x, expected_v.x);
93 _checkValidDouble(v.y, expected_v.y);
94 _checkValidDouble(v.z, expected_v.z);
95 if (do_test) {
96 ASSERT_EQ(v, expected_v);
97 }
98}
99
100void _testDoubleConvert(bool use_from_chars)
101{
102
103 {
104 // TODO: tester les autres conversions
105 String s = "25e3";
106 Int32 x = 0;
107 bool is_bad = builtInGetValue(x, s);
108 std::cout << "S=" << s << " X=" << x << " is_bad?=" << is_bad << "\n";
109 ASSERT_TRUE(is_bad);
110 }
111 // Avec la version 'from_chars', convertir une chaîne vide est une erreur
112 // mais pas avec la version historique.
113 if (use_from_chars)
114 _checkBad<double>("");
115 _checkDouble("-0x1.81e03f705857bp-16", -2.3e-05);
116 _checkDouble("0x1.81e03f705857bp-16", 2.3e-05);
117 _checkDouble("+0x1.81e03f705857bp-16", 2.3e-05);
118 _checkDouble(" +1.23e42", 1.23e42);
119 _checkDouble(" -1.23e42", -1.23e42);
120 if (!use_from_chars) {
121 _checkDouble("+1.23e42", 1.23e42);
122 }
123
124 _checkBad<double>("d2");
125 _checkBad<double>("2.3w");
126
127 {
128 Real inf_x = std::numeric_limits<Real>::infinity();
129 _checkDouble("inf", inf_x);
130 _checkDouble(" INF", inf_x);
131 _checkDouble(" infinity", inf_x);
132 _checkDouble("INFINITY", inf_x);
133 }
134 {
135 Real minus_inf_x = -std::numeric_limits<Real>::infinity();
136 _checkDouble("-inf", minus_inf_x);
137 _checkDouble(" -INF", minus_inf_x);
138 _checkDouble(" -infinity", minus_inf_x);
139 _checkDouble("-INFINITY", minus_inf_x);
140 }
141
142 {
143 _checkNaN("nan");
144 _checkNaN(" NAN");
145 _checkNaN("NaN");
146 _checkNaN(" nAN");
147 }
148 {
149 String s3 = "23123.132e123";
150 Real total = 0.0;
151 Int32 nb_iter = 1000000 * 10;
152 nb_iter = 1;
153 for (Int32 i = 0; i < nb_iter; ++i) {
154 Real v = {};
155 builtInGetValue(v, s3);
156 total += v;
157 }
158 std::cout << "Total=" << total << "\n";
159 }
160}
161
162void _testReal2Convert(bool use_same_that_real)
163{
165 Real v_nan = std::numeric_limits<double>::quiet_NaN();
166 _checkReal2("2.3e1 -1.2", Real2(2.3e1, -1.2));
167 if (use_same_that_real) {
168 _checkReal2("-1.3 nan", Real2(-1.3, v_nan));
169 _checkReal2(" 2.3 -1.3", Real2(2.3, -1.3));
170 }
171 _checkBad<Real2>("2.3 1.2w");
172 _checkBad<Real2>("2.3x");
173 _checkBad<Real2>(" y2.3 1.2");
174}
175
176void _testReal3Convert(bool use_same_that_real)
177{
179 Real v_nan = std::numeric_limits<double>::quiet_NaN();
180 Real v_inf = std::numeric_limits<double>::infinity();
181 _checkReal3("2.3e1 -1.2 1.5", Real3(2.3e1, -1.2, 1.5));
182 if (use_same_that_real) {
183 _checkReal3("-1.3 nan 4.6", Real3(-1.3, v_nan, 4.6));
184 _checkReal3("1.3 4.2 inf", Real3(1.3, 4.2, v_inf));
185 _checkReal3("-2.1\t -1.5 1.0e5", Real3(-2.1, -1.5, 1.0e5));
186 _checkReal3(" -2.1 -1.5 1.0e5", Real3(-2.1, -1.5, 1.0e5));
187 //_checkReal3("-2.1 -1.5 +1.0e5", Real3(-2.1, -1.5, 1.0e5));
188 }
189 _checkBad<Real3>("2.3 1.2w");
190 _checkBad<Real3>("2.3x");
191 _checkBad<Real3>("2.3 1.2");
192 _checkBad<Real3>("2.3 -1.2ee2 4.5");
193 _checkBad<Real3>("z2.3 -1.2e2 -2323.3");
194 _checkBad<Real3>("2.3 -1.2e2 -2323.3x");
195}
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
200} // namespace
201
202/*---------------------------------------------------------------------------*/
203/*---------------------------------------------------------------------------*/
204
205TEST(ValueConvert, Basic)
206{
207 std::cout << "TEST_ValueConvert Basic\n";
209
211 _testDoubleConvert(true);
212 _testReal2Convert(true);
213 _testReal2Convert(false);
214 _testReal3Convert(true);
215 _testReal3Convert(false);
216
218 _testDoubleConvert(false);
219 _testReal2Convert(true);
220 _testReal2Convert(false);
221 _testReal3Convert(true);
222 _testReal3Convert(false);
223}
224
225TEST(ValueConvert, TryParse)
226{
227 {
228 String s2;
230 ASSERT_FALSE(v.has_value());
231 }
232
233 {
234 String s2;
236 ASSERT_TRUE(v.has_value());
237 ASSERT_EQ(v, 4);
238 }
239
240 {
241 String s2("2.3");
243 ASSERT_EQ(v, 2.3);
244 }
245
246 {
247 String s2("2.3w");
249 ASSERT_FALSE(v.has_value());
250 }
251}
252
253/*---------------------------------------------------------------------------*/
254/*---------------------------------------------------------------------------*/
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
Definition Assertion.h:137
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Definition Assertion.h:125
static void setUseFromChars(bool v)
Indique si on utilise 'std::from_chars' pour convertir les chaînes de caractères en un type numérique...
static void setUseSameConvertForAllReal(bool v)
static void setVerbosity(Int32 v)
Positionne le niveau de verbosité pour les fonctions de conversion.
Classe template pour convertir un type.
Classe gérant un vecteur de réel de dimension 2.
Definition Real2.h:121
Classe gérant un vecteur de réel de dimension 3.
Definition Real3.h:132
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Real y
deuxième composante du couple
Definition Real2.h:35
Real x
première composante du couple
Definition Real2.h:34
Real y
deuxième composante du triplet
Definition Real3.h:36
Real z
troisième composante du triplet
Definition Real3.h:37
Real x
première composante du triplet
Definition Real3.h:35