Arcane  v3.15.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestCxx20.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/ArcaneCxx20.h"
11
12#include <atomic>
13#include <charconv>
14#include <iostream>
15#include <limits>
16#include <cmath>
17
18/*---------------------------------------------------------------------------*/
19/*---------------------------------------------------------------------------*/
20
21using namespace Arcane;
22
23template <class T>
24concept integral = std::is_integral_v<T>;
25
26TEST(TestCxx20,Atomic)
27{
28 Int32 x = 25;
29 std::atomic_ref<Int32> ax(x);
30 ax.fetch_add(32);
31 ASSERT_EQ(x,57);
32}
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace
38{
39template<integral DataType> DataType _testAdd(DataType a,DataType b)
40{
41 return a+b;
42}
43}
44
46{
47 Int32 a = 12;
48 Int32 b = -48;
49 ASSERT_EQ(_testAdd(a,b),(a+b));
50}
51
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56namespace
57{
58
59double _doTestDouble(double value)
60{
61 std::cout << "ValueDirectStream=" << value << "\n";
62 std::ostringstream ostr;
63 ostr << value << "\n";
64 std::string str0 = ostr.str();
65 std::cout << "O_STR=" << str0;
66
67 {
68 std::istringstream istr(str0);
69 double v = -1.0;
70 std::cout << "IS_GOOD?=" << istr.good() << "\n";
71 istr >> std::ws >> v;
72 std::cout << "IS_GOOD?=" << istr.good() << "\n";
73 std::cout << "V=" << v << "\n";
74 char* str_end = nullptr;
75 double v2 = std::strtod(str0.data(), &str_end);
76 std::cout << "ReadWith 'strtod' =" << v2 << "\n";
77 }
78
79 double result = {};
80 {
81 auto [ptr, ec] = std::from_chars(str0.data(), str0.data() + str0.length(), result);
82 if (ec == std::errc())
83 std::cout << "Result: " << result << ", ptr -> " << (ptr - str0.data()) << '\n';
84 else if (ec == std::errc::invalid_argument)
85 std::cout << "This is not a number.\n";
86 else if (ec == std::errc::result_out_of_range)
87 std::cout << "This number is larger than an int.\n";
88 }
89 return result;
90}
91
92} // namespace
93
95{
96 std::cout << "TEST_ValueConvert 'Real' \n";
97 double d_inf = std::numeric_limits<double>::infinity();
98 double d_nan = std::numeric_limits<double>::quiet_NaN();
99 std::cout << "Infinity=" << d_inf << "\n";
100 std::cout << "NaN=" << d_nan << "\n";
101
102 double d_t0 = 1.2345;
103 std::cout << "** Test: " << d_t0 << "\n";
104 double r_t0 = _doTestDouble(d_t0);
106
107 std::cout << "** Test: Infinity\n";
108 double r_inf = _doTestDouble(d_inf);
110
111 std::cout << "** Test: NaN\n";
112 double r_nan = _doTestDouble(d_nan);
113 ASSERT_TRUE(std::isnan(r_nan));
114}
115
116/*---------------------------------------------------------------------------*/
117/*---------------------------------------------------------------------------*/
#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
Espace de nom pour l'utilisation des accélérateurs.
@ Atomic
Utilise des opérations atomiques entre les blocs.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-