Debian Codegraph @

packages/teuchos/core/test/ScalarTraits/float128.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "Teuchos_Array.hpp"
11 #include "Teuchos_ScalarTraits.hpp" // operator<< and operator>> overloads
12 #include "Teuchos_Tuple.hpp"
13 #include "Teuchos_UnitTestHarness.hpp"
14
15 namespace { // (anonymous)
16
17 using std::endl;
18
19 TEUCHOS_UNIT_TEST( Float128, OutputStreamOp )
20 {
21 Teuchos::OSTab tab0 (out);
22
23 #ifdef HAVE_TEUCHOSCORE_QUADMATH
24 out << "Test operator<< (std::ostream&, __float128)" << endl;
25
26 __float128 x = 1.0;
27 __float128 y = strtoflt128 ("1.111112222233333", NULL);
28 // __float128 has enough digits to represent this exactly, but
29 // double precision would round.
30 __float128 z = strtoflt128 ("1.111112222233333444445555566666", NULL);
31
32 // FIXME (mfh 04 Sep 2015) The results of printing could depend on
33 // the locale. This works fine for the default locale on my system.
34 {
35 std::ostringstream os;
36 os << x;
37 TEST_EQUALITY_CONST( os.str (), "1.000000000000000000000000000000e+00" );
38 }
39 {
40 std::ostringstream os;
41 os << y;
42 TEST_EQUALITY_CONST( os.str (), "1.111112222233333000000000000000e+00" );
43 }
44 {
45 std::ostringstream os;
46 os << z;
47 TEST_EQUALITY_CONST( os.str (), "1.111112222233333444445555566666e+00" );
48 }
49
50 // Test that operator<< (std::ostream&, __float128) works. The
51 // operator<< overload MUST be defined in the std namespace. If
52 // defined in the global namespace, the compiler will have trouble
53 // with the TEST_COMPARE_ARRAYS expression below.
54
55 out << "Test chaining operator<<:" << endl
56 << "z = " << z << ", it really does." << endl;
57
58 // Test op<<, but make the __float128 arguments entries of an array.
59 // (Make sure that this compiles without "ambiguous overload"
60 // errors.)
61 __float128 a[3];
62 a[0] = x;
63 a[1] = y;
64 a[2] = z;
65 out << "Testing chaining operator<< with array entries:" << endl
66 << "a[0] = " << a[0] << ", a[1] = " << a[1] << ", a[2] = " << a[2]
67 << endl;
68
69 Teuchos::Array<__float128> arrayOfFloat128 (1);
70 Teuchos::Tuple<__float128, 1> tupleOfFloat128;
71 arrayOfFloat128[0] = z;
72 tupleOfFloat128[0] = z;
73 Teuchos::ArrayView<__float128> arrayViewOfFloat128 = arrayOfFloat128 ();
74 // mfh 04 Sep 2015: If operator<< (std::ostream&, __float128) is
75 // defined in the global namespace, instead of in the std namespace,
76 // the TEST_COMPARE_ARRAYS expression will fail to compile. GCC
77 // 5.2.0 complains about the following line of
78 // Teuchos::compareArrays (in
79 // teuchos/core/src/Teuchos_TestingHelpers.hpp):
80 //
81 // out << "\nError, "<<a1_name<<"["<<i<<"] = "<<a1[i]<<" == "
82 // << a2_name<<"["<<i<<"] = "<<a2[i]<<": failed!\n";
83 //
84 // where a1 is a Teuchos::ArrayView<__float128> and a2 is a
85 // Teuchos::Tuple<__float128, 1>. The compiler claims an ambiguous
86 // overload. It has something to do with compareArrays (and
87 // analogous functions in that file) being templated on Array types,
88 // as I don't see that when I directly imitate what that line of
89 // code does (see below).
90
91 TEST_COMPARE_ARRAYS( arrayViewOfFloat128, tupleOfFloat128 );
92
93 std::string s1 ("Some string");
94 out << "Hello there! \"" << s1 << "\" is a string that doesn't mean anything "
95 "on its own, but just makes this line of code more like the line of code "
96 "that doesn't compile. arrayViewOfFloat128[0] = " << arrayViewOfFloat128[0]
97 << " and tupleOfFloat128[0] = " << tupleOfFloat128[0] << "." << endl;
98
99 // Test that operator>> (std::istream&, __float128&) works.
100 {
101 // Use enough digits in the example to catch "cheats" that
102 // truncate to double.
103 const std::string z_str ("1.111112222233333444445555566666");
104 std::istringstream is (z_str);
105 __float128 z_copy = 0.0;
106 is >> z_copy;
107 TEST_EQUALITY_CONST( z, z_copy );
108 }
109
110 #else
111 out << "This test only makes sense to run with libquadmath enabled." << endl;
112 #endif // HAVE_TEUCHOSCORE_QUADMATH
113 }
114
115 } // namespace (anonymous)