Arcane  v3.15.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/*---------------------------------------------------------------------------*/
14/*---------------------------------------------------------------------------*/
15
16using namespace Arcane;
17
18namespace
19{
20
21void _checkBadDouble(const String& s)
22{
23 Real x = 0;
24 bool is_bad = builtInGetValue(x, s);
25 std::cout << "S=" << s << " X=" << x << " is_bad?=" << is_bad << "\n";
27}
28
29void _checkDouble(const String& s, double expected_x)
30{
31 Real x = 0;
32 bool is_bad = builtInGetValue(x, s);
33 std::cout << "S=" << s << " X=" << x << " is_bad?=" << is_bad << "\n";
36}
37
38void _checkNaN(const String& s)
39{
40 Real x = 0;
41 bool is_bad = builtInGetValue(x, s);
42 std::cout << "S=" << s << " X=" << x << " is_bad?=" << is_bad << "\n";
44 ASSERT_TRUE(std::isnan(x));
45}
46
48{
49
50 {
51 // TODO: tester les autres conversions
52 String s = "25e3";
53 Int32 x = 0;
54 bool is_bad = builtInGetValue(x, s);
55 std::cout << "S=" << s << " X=" << x << " is_bad?=" << is_bad << "\n";
57 }
58 // Avec la version 'from_chars', convertir une chaîne vide est une erreur
59 // mais pas avec la version historique.
62 _checkDouble("-0x1.81e03f705857bp-16", -2.3e-05);
63 _checkDouble("0x1.81e03f705857bp-16", 2.3e-05);
64
65 {
66 Real inf_x = std::numeric_limits<Real>::infinity();
67 _checkDouble("inf", inf_x);
68 _checkDouble("INF", inf_x);
69 _checkDouble("infinity", inf_x);
70 _checkDouble("INFINITY", inf_x);
71 }
72 {
73 Real minus_inf_x = -std::numeric_limits<Real>::infinity();
76 _checkDouble("-infinity", minus_inf_x);
77 _checkDouble("-INFINITY", minus_inf_x);
78 }
79
80 {
81 _checkNaN("nan");
82 _checkNaN("NAN");
83 _checkNaN("NaN");
84 _checkNaN("nAN");
85 }
86 {
87 String s3 = "23123.132e123";
88 Real total = 0.0;
89 Int32 nb_iter = 1000000 * 10;
90 nb_iter = 1;
91 for (Int32 i = 0; i < nb_iter; ++i) {
92 Real v = {};
93 builtInGetValue(v, s3);
94 total += v;
95 }
96 std::cout << "Total=" << total << "\n";
97 }
98}
99
100} // namespace
101
102TEST(ValueConvert, Basic)
103{
104 std::cout << "TEST_ValueConvert Basic\n";
105 impl::arcaneSetValueConvertVerbosity(1);
106
107#if defined(ARCANE_HAS_CXX20)
108 impl::arcaneSetIsValueConvertUseFromChars(true);
109 _testDoubleConvert(true);
110#endif
111
112 impl::arcaneSetIsValueConvertUseFromChars(false);
113 _testDoubleConvert(false);
114}
115
117{
118 {
119 String s2;
121 ASSERT_FALSE(v.has_value());
122 }
123
124 {
125 String s2;
127 ASSERT_TRUE(v.has_value());
128 ASSERT_EQ(v, 4);
129 }
130
131 {
132 String s2("2.3");
134 ASSERT_EQ(v, 2.3);
135 }
136
137 {
138 String s2("2.3w");
140 ASSERT_FALSE(v.has_value());
141 }
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
Definition Assertion.h:138
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Definition Assertion.h:126
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-