]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/math/test/test_cstdfloat.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / math / test / test_cstdfloat.cpp
CommitLineData
7c673cae
FG
1// Copyright Christopher Kormanyos 2014.
2// Copyright John Maddock 2014.
3// Copyright Paul A. Bristow 2014.
4
5// Use, modification and distribution are subject to the
6// Boost Software License, Version 1.0. (See accompanying file
7// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9#include <cmath>
10#include <complex>
11#include <limits>
12#include <iostream>
13#include <iomanip>
14#include <sstream>
15#include <string>
16#include <boost/math/cstdfloat/cstdfloat_types.hpp>
17
18#ifdef _MSC_VER
19# pragma warning(disable : 4127) // conditional expression is constant.
20# pragma warning(disable : 4512) // assignment operator could not be generated.
21# pragma warning(disable : 4996) // use -D_SCL_SECURE_NO_WARNINGS.
22#endif
23
24#define BOOST_TEST_MAIN
25#include <boost/test/unit_test.hpp> // Boost.Test
26#include <boost/test/floating_point_comparison.hpp>
27
28//
29// DESCRIPTION:
30// ~~~~~~~~~~~~
31//
32// This file tests the implementation of floating-point typedefs having
33// specified widths, as implemented in <boost/cstdfloat.hpp> and described
34// in N3626 (proposed for C++14).
35
36// For more information on <boost/cstdfloat.hpp> and the corresponding
37// proposal of "Floating-Point Typedefs Having Specified Widths",
38// see: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3626.pdf
39
40// The tests:
41//
42// Perform sanity checks on boost::float16_t, boost::float32_t,
43// boost::float64_t, boost::float80_t, and boost::float128_t when
44// these types are available. In the sanity checks, we verify the
45// formal behavior of the types and the macros for creating literal
46// floating-point constants.
47//
48// An extended check is included for boost::float128_t. This checks
49// the functionality of <cmath>, I/O stream operations, and <complex>
50// functions for boost::float128_t.
51
52#define TEST_CSTDFLOAT_SANITY_CHECK(the_digits) \
53void sanity_check_##the_digits##_func() \
54{ \
55 typedef boost::float##the_digits##_t float_type; \
56 \
57 BOOST_CONSTEXPR_OR_CONST int my_digits10 = std::numeric_limits<float_type>::digits10; \
58 \
59 { \
60 BOOST_CONSTEXPR_OR_CONST float_type x = \
61 BOOST_FLOAT##the_digits##_C(0.33333333333333333333333333333333333333333); \
62 std::stringstream ss; \
63 ss << std::setprecision(my_digits10 - 1) \
64 << x; \
65 std::string str = "0."; \
66 str += std::string(std::string::size_type(my_digits10 - 1), char('3')); \
67 BOOST_CHECK_EQUAL( ss.str(), str ); \
68 } \
69 { \
70 BOOST_CONSTEXPR_OR_CONST float_type x = \
71 BOOST_FLOAT##the_digits##_C(0.66666666666666666666666666666666666666666); \
72 std::stringstream ss; \
73 ss << std::setprecision(my_digits10 - 1) \
74 << x; \
75 std::string str = "0."; \
76 str += std::string(std::string::size_type(my_digits10 - 2), char('6')); \
77 str += "7"; \
78 BOOST_CHECK_EQUAL( ss.str(), str ); \
79 } \
80 { \
81 const float_type x = BOOST_FLOAT##the_digits##_C(1.0) / test_cstdfloat::zero; \
82 const bool the_inf_test = ( std::numeric_limits<float_type>::has_infinity \
83 && (x == std::numeric_limits<float_type>::infinity())); \
84 BOOST_CHECK_EQUAL( the_inf_test, true ); \
85 } \
86 { \
87 using std::sqrt; \
88 const float_type x = sqrt(float_type(test_cstdfloat::minus_one)); \
89 const bool the_nan_test = ( std::numeric_limits<float_type>::has_quiet_NaN \
90 && (x != x)); \
91 BOOST_CHECK_EQUAL( the_nan_test, true ); \
92 } \
93 { \
94 const bool the_lim_test = \
95 (std::numeric_limits<boost::floatmax_t>::digits >= std::numeric_limits<float_type>::digits); \
96 BOOST_CHECK_EQUAL( the_lim_test, true ); \
97 } \
98}
99
100namespace test_cstdfloat
101{
102 int zero;
103 int minus_one;
104
105 #if defined(BOOST_FLOATMAX_C)
106 BOOST_CONSTEXPR_OR_CONST int has_floatmax_t = 1;
107 #else
108 BOOST_CONSTEXPR_OR_CONST int has_floatmax_t = 0;
109 #endif
110
111 #if defined(BOOST_FLOAT16_C)
112 TEST_CSTDFLOAT_SANITY_CHECK(16)
113 #endif
114
115 #if defined(BOOST_FLOAT32_C)
116 TEST_CSTDFLOAT_SANITY_CHECK(32)
117 #endif
118
119 #if defined(BOOST_FLOAT64_C)
120 TEST_CSTDFLOAT_SANITY_CHECK(64)
121 #endif
122
123 #if defined(BOOST_FLOAT80_C)
124 TEST_CSTDFLOAT_SANITY_CHECK(80)
125 #endif
126
127 #if defined(BOOST_FLOAT128_C)
128 TEST_CSTDFLOAT_SANITY_CHECK(128)
129
130 void extend_check_128_func()
131 {
132 }
133 #endif // defined (BOOST_FLOAT128_C)
134}
135
136BOOST_AUTO_TEST_CASE(test_main)
137{
138 test_cstdfloat::zero = 0;
139 test_cstdfloat::minus_one = -1;
140
141 // Perform basic sanity checks that verify both the existence of the proper
142 // floating-point literal macros as well as the correct digit handling
143 // for a given floating-point typedef having specified width.
144
145 BOOST_CHECK_EQUAL( test_cstdfloat::has_floatmax_t, 1 );
146
147 #if defined(BOOST_FLOAT16_C)
148 test_cstdfloat::sanity_check_16_func();
149 #endif
150
151 #if defined(BOOST_FLOAT32_C)
152 test_cstdfloat::sanity_check_32_func();
153 #endif
154
155 #if defined(BOOST_FLOAT64_C)
156 test_cstdfloat::sanity_check_64_func();
157 #endif
158
159 #if defined(BOOST_FLOAT80_C)
160 test_cstdfloat::sanity_check_80_func();
161 #endif
162
163 #if defined(BOOST_FLOAT128_C)
164 test_cstdfloat::sanity_check_128_func();
165
166 // Perform an extended check of boost::float128_t including
167 // a variety of functions from the C++ standard library.
168 test_cstdfloat::extend_check_128_func();
169 #endif // defined (BOOST_FLOAT128_C)
170}