Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TestCxx20.cc
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#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} // namespace
44
45TEST(TestCxx20, Concept)
46{
47 Int32 a = 12;
48 Int32 b = -48;
49 ASSERT_EQ(_testAdd(a, b), (a + b));
50}
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
55namespace
56{
57
58double _doTestDouble(double value)
59{
60 std::cout << "ValueDirectStream=" << value << "\n";
61 std::ostringstream ostr;
62 ostr << value << "\n";
63 std::string str0 = ostr.str();
64 std::cout << "O_STR=" << str0;
65
66 {
67 std::istringstream istr(str0);
68 double v = -1.0;
69 std::cout << "IS_GOOD?=" << istr.good() << "\n";
70 istr >> std::ws >> v;
71 std::cout << "IS_GOOD?=" << istr.good() << "\n";
72 std::cout << "V=" << v << "\n";
73 char* str_end = nullptr;
74 double v2 = std::strtod(str0.data(), &str_end);
75 std::cout << "ReadWith 'strtod' =" << v2 << "\n";
76 }
77
78 double result = {};
79 {
80 auto [ptr, ec] = std::from_chars(str0.data(), str0.data() + str0.length(), result);
81 if (ec == std::errc())
82 std::cout << "Result: " << result << ", ptr -> " << (ptr - str0.data()) << '\n';
83 else if (ec == std::errc::invalid_argument)
84 std::cout << "This is not a number.\n";
85 else if (ec == std::errc::result_out_of_range)
86 std::cout << "This number is larger than an int.\n";
87 }
88 return result;
89}
90
91} // namespace
92
93TEST(TestFromChars, Real)
94{
95 std::cout << "TEST_ValueConvert 'Real' \n";
96 double d_inf = std::numeric_limits<double>::infinity();
97 double d_nan = std::numeric_limits<double>::quiet_NaN();
98 std::cout << "Infinity=" << d_inf << "\n";
99 std::cout << "NaN=" << d_nan << "\n";
100
101 double d_t0 = 1.2345;
102 std::cout << "** Test: " << d_t0 << "\n";
103 double r_t0 = _doTestDouble(d_t0);
104 ASSERT_EQ(r_t0, d_t0);
105
106 std::cout << "** Test: Infinity\n";
107 double r_inf = _doTestDouble(d_inf);
108 ASSERT_EQ(r_inf, d_inf);
109
110 std::cout << "** Test: NaN\n";
111 double r_nan = _doTestDouble(d_nan);
112 ASSERT_TRUE(std::isnan(r_nan));
113}
114
115/*---------------------------------------------------------------------------*/
116/*---------------------------------------------------------------------------*/
#define ASSERT_TRUE(condition)
Checks that condition is true.
Definition Assertion.h:128
@ Atomic
Uses atomic operations between blocks.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --