]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_nonfinite_trap.cpp
1 // Copyright (c) 2006 Johan Rade
2 // Copyright (c) 2011 Paul A. Bristow To incorporate into Boost.Math
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // test_nonfinite_trap.cpp
11 # pragma warning(disable : 4702)
14 #define BOOST_TEST_MAIN
16 #include <boost/test/unit_test.hpp>
17 #include "almost_equal.ipp" // Similar to BOOST_CLOSE_FRACTION.
18 #include "s_.ipp" // To create test strings like std::basic_string<CharType> s = S_("0 -0");
19 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
26 // Using an anonymous namespace resolves ambiguities on platforms
27 // with fpclassify etc functions at global scope.
29 using namespace boost::math
;
30 using boost::math::signbit
;
31 using boost::math::changesign
;
32 using boost::math::isnan
;
34 //------------------------------------------------------------------------------
36 void trap_test_finite();
40 BOOST_AUTO_TEST_CASE(trap_test
)
47 //------------------------------------------------------------------------------
49 template<class CharType
, class ValType
> void trap_test_finite_impl();
51 void trap_test_finite()
53 trap_test_finite_impl
<char, float>();
54 trap_test_finite_impl
<char, double>();
55 trap_test_finite_impl
<char, long double>();
56 trap_test_finite_impl
<wchar_t, float>();
57 trap_test_finite_impl
<wchar_t, double>();
58 trap_test_finite_impl
<wchar_t, long double>();
61 template<class CharType
, class ValType
> void trap_test_finite_impl()
63 std::locale old_locale
;
64 std::locale
tmp_locale(old_locale
,
65 new nonfinite_num_put
<CharType
>(trap_infinity
| trap_nan
));
66 std::locale
new_locale(tmp_locale
,
67 new nonfinite_num_get
<CharType
>(trap_infinity
| trap_nan
));
69 std::basic_stringstream
<CharType
> ss
;
72 ValType a1
= (ValType
)1.2;
73 ValType a2
= (ValType
)-3.5;
74 ValType a3
= (std::numeric_limits
<ValType
>::max
)();
75 ValType a4
= -(std::numeric_limits
<ValType
>::max
)();
76 ss
<< a1
<< ' ' << a2
<< ' ' << a3
<< ' ' << a4
;
78 ValType b1
, b2
, b3
, b4
;
79 ss
>> b1
>> b2
>> b3
>> b4
;
81 BOOST_CHECK(almost_equal(b1
, a1
));
82 BOOST_CHECK(almost_equal(b2
, a2
));
83 BOOST_CHECK(almost_equal(b3
, a3
));
84 BOOST_CHECK(almost_equal(b4
, a4
));
85 BOOST_CHECK(b3
!= std::numeric_limits
<ValType
>::infinity());
86 BOOST_CHECK(b4
!= -std::numeric_limits
<ValType
>::infinity());
87 BOOST_CHECK(ss
.rdstate() == std::ios_base::eofbit
);
95 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
98 //------------------------------------------------------------------------------
100 template<class CharType
, class ValType
> void trap_test_inf_impl();
101 template<class CharType
, class ValType
> void trap_test_put_inf_impl();
102 template<class CharType
, class ValType
> void trap_test_get_inf_impl();
106 trap_test_inf_impl
<char, float>();
107 trap_test_inf_impl
<char, double>();
108 trap_test_inf_impl
<char, long double>();
109 trap_test_inf_impl
<wchar_t, float>();
110 trap_test_inf_impl
<wchar_t, double>();
111 trap_test_inf_impl
<wchar_t, long double>();
114 template<class CharType
, class ValType
> void trap_test_inf_impl()
116 trap_test_put_inf_impl
<CharType
, ValType
>();
117 trap_test_get_inf_impl
<CharType
, ValType
>();
120 template<class CharType
, class ValType
> void trap_test_put_inf_impl()
122 std::locale old_locale
;
123 std::locale
new_locale(old_locale
,
124 new nonfinite_num_put
<CharType
>(trap_infinity
));
126 std::basic_stringstream
<CharType
> ss
;
127 ss
.imbue(new_locale
);
129 ValType a1
= std::numeric_limits
<ValType
>::infinity();
131 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
132 || ss
.rdstate() == std::ios_base::badbit
);
135 ValType a2
= -std::numeric_limits
<ValType
>::infinity();
137 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
138 || ss
.rdstate() == std::ios_base::badbit
);
141 template<class CharType
, class ValType
> void trap_test_get_inf_impl()
143 std::locale old_locale
;
144 std::locale
tmp_locale(old_locale
, new nonfinite_num_put
<CharType
>);
145 std::locale
new_locale(tmp_locale
,
146 new nonfinite_num_get
<CharType
>(trap_infinity
));
148 std::basic_stringstream
<CharType
> ss
;
149 ss
.imbue(new_locale
);
151 ValType a1
= std::numeric_limits
<ValType
>::infinity();
155 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
160 ValType a2
= -std::numeric_limits
<ValType
>::infinity();
164 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
167 //------------------------------------------------------------------------------
169 template<class CharType
, class ValType
> void trap_test_nan_impl();
170 template<class CharType
, class ValType
> void trap_test_put_nan_impl();
171 template<class CharType
, class ValType
> void trap_test_get_nan_impl();
175 trap_test_nan_impl
<char, float>();
176 trap_test_nan_impl
<char, double>();
177 trap_test_nan_impl
<char, long double>();
178 trap_test_nan_impl
<wchar_t, float>();
179 trap_test_nan_impl
<wchar_t, double>();
180 trap_test_nan_impl
<wchar_t, long double>();
183 template<class CharType
, class ValType
> void trap_test_nan_impl()
185 trap_test_put_nan_impl
<CharType
, ValType
>();
186 trap_test_get_nan_impl
<CharType
, ValType
>();
189 template<class CharType
, class ValType
> void trap_test_put_nan_impl()
191 std::locale old_locale
;
192 std::locale
new_locale(old_locale
,
193 new nonfinite_num_put
<CharType
>(trap_nan
));
195 std::basic_stringstream
<CharType
> ss
;
196 ss
.imbue(new_locale
);
198 ValType a1
= std::numeric_limits
<ValType
>::quiet_NaN();
200 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
201 || ss
.rdstate() == std::ios_base::badbit
);
204 ValType a2
= std::numeric_limits
<ValType
>::signaling_NaN();
206 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
207 || ss
.rdstate() == std::ios_base::badbit
);
210 template<class CharType
, class ValType
> void trap_test_get_nan_impl()
212 std::locale old_locale
;
213 std::locale
tmp_locale(old_locale
, new nonfinite_num_put
<CharType
>);
214 std::locale
new_locale(tmp_locale
,
215 new nonfinite_num_get
<CharType
>(trap_nan
));
217 std::basic_stringstream
<CharType
> ss
;
218 ss
.imbue(new_locale
);
220 ValType a1
= std::numeric_limits
<ValType
>::quiet_NaN();
224 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
229 ValType a2
= std::numeric_limits
<ValType
>::signaling_NaN();
233 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
236 //------------------------------------------------------------------------------
238 } // anonymous namespace