]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_nonfinite_io.cpp
2 // Copyright 2011 Paul A. Bristow
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 : 4127) // Expression is constant.
14 #define BOOST_TEST_MAIN
16 #include <boost/test/auto_unit_test.hpp>
17 #include <libs/math/test/almost_equal.ipp> // Similar to BOOST_CLOSE_FRACTION.
18 #include <libs/math/test/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>
27 // Using an anonymous namespace resolves ambiguities on platforms
28 // with fpclassify etc functions at global scope.
30 using namespace boost::math
;
31 using boost::math::signbit
;
32 using boost::math::changesign
;
33 using (boost::math::isnan
)(;
35 //------------------------------------------------------------------------------
36 // Test nonfinite_num_put and nonfinite_num_get facets by checking
37 // loopback (output and re-input) of a few values,
38 // but using all the built-in char and floating-point types.
39 // Only the default output is used but various ostream options are tested seperately below.
40 // Finite, infinite and NaN values (positive and negative) are used for the test.
42 void trap_test_finite();
46 BOOST_AUTO_TEST_CASE(trap_test
)
53 //------------------------------------------------------------------------------
55 template<class CharType
, class ValType
> void trap_test_finite_impl();
57 void trap_test_finite()
58 { // Test finite using all the built-in char and floating-point types.
59 trap_test_finite_impl
<char, float>();
60 trap_test_finite_impl
<char, double>();
61 trap_test_finite_impl
<char, long double>();
62 trap_test_finite_impl
<wchar_t, float>();
63 trap_test_finite_impl
<wchar_t, double>();
64 trap_test_finite_impl
<wchar_t, long double>();
67 template<class CharType
, class ValType
> void trap_test_finite_impl()
69 std::locale old_locale
;
70 std::locale
tmp_locale(old_locale
,
71 new nonfinite_num_put
<CharType
>(trap_infinity
| trap_nan
));
72 std::locale
new_locale(tmp_locale
,
73 new nonfinite_num_get
<CharType
>(trap_infinity
| trap_nan
));
75 std::basic_stringstream
<CharType
> ss
;
78 ValType a1
= (ValType
)1.2;
79 ValType a2
= (ValType
)-3.5;
80 ValType a3
= (std::numeric_limits
<ValType
>::max
)();
81 ValType a4
= -(std::numeric_limits
<ValType
>::max
)();
82 ss
<< a1
<< ' ' << a2
<< ' ' << a3
<< ' ' << a4
; // 1.2, -3.5, max, -max
84 ValType b1
, b2
, b3
, b4
;
85 ss
>> b1
>> b2
>> b3
>> b4
;
87 BOOST_CHECK(almost_equal(b1
, a1
));
88 BOOST_CHECK(almost_equal(b2
, a2
));
89 BOOST_CHECK(almost_equal(b3
, a3
));
90 BOOST_CHECK(almost_equal(b4
, a4
));
91 BOOST_CHECK(b3
!= std::numeric_limits
<ValType
>::infinity());
92 BOOST_CHECK(b4
!= -std::numeric_limits
<ValType
>::infinity());
93 BOOST_CHECK(ss
.rdstate() == std::ios_base::eofbit
);
101 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
104 //------------------------------------------------------------------------------
106 template<class CharType
, class ValType
> void trap_test_inf_impl();
107 template<class CharType
, class ValType
> void trap_test_put_inf_impl();
108 template<class CharType
, class ValType
> void trap_test_get_inf_impl();
111 { // Test infinity using all the built-in char and floating-point types.
112 trap_test_inf_impl
<char, float>();
113 trap_test_inf_impl
<char, double>();
114 trap_test_inf_impl
<char, long double>();
115 trap_test_inf_impl
<wchar_t, float>();
116 trap_test_inf_impl
<wchar_t, double>();
117 trap_test_inf_impl
<wchar_t, long double>();
120 template<class CharType
, class ValType
> void trap_test_inf_impl()
122 trap_test_put_inf_impl
<CharType
, ValType
>();
123 trap_test_get_inf_impl
<CharType
, ValType
>();
126 template<class CharType
, class ValType
> void trap_test_put_inf_impl()
128 std::locale old_locale
;
129 std::locale
new_locale(old_locale
,
130 new nonfinite_num_put
<CharType
>(trap_infinity
));
132 std::basic_stringstream
<CharType
> ss
;
133 ss
.imbue(new_locale
);
135 ValType a1
= std::numeric_limits
<ValType
>::infinity();
137 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
138 || ss
.rdstate() == std::ios_base::badbit
);
141 ValType a2
= -std::numeric_limits
<ValType
>::infinity();
143 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
144 || ss
.rdstate() == std::ios_base::badbit
);
147 template<class CharType
, class ValType
> void trap_test_get_inf_impl()
149 std::locale old_locale
;
150 std::locale
tmp_locale(old_locale
, new nonfinite_num_put
<CharType
>);
151 std::locale
new_locale(tmp_locale
,
152 new nonfinite_num_get
<CharType
>(trap_infinity
));
154 std::basic_stringstream
<CharType
> ss
;
155 ss
.imbue(new_locale
);
157 ValType a1
= std::numeric_limits
<ValType
>::infinity();
161 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
166 ValType a2
= -std::numeric_limits
<ValType
>::infinity();
170 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
173 //------------------------------------------------------------------------------
175 template<class CharType
, class ValType
> void trap_test_nan_impl();
176 template<class CharType
, class ValType
> void trap_test_put_nan_impl();
177 template<class CharType
, class ValType
> void trap_test_get_nan_impl();
180 { // Test NaN using all the built-in char and floating-point types.
181 trap_test_nan_impl
<char, float>();
182 trap_test_nan_impl
<char, double>();
183 trap_test_nan_impl
<char, long double>();
184 trap_test_nan_impl
<wchar_t, float>();
185 trap_test_nan_impl
<wchar_t, double>();
186 trap_test_nan_impl
<wchar_t, long double>();
189 template<class CharType
, class ValType
> void trap_test_nan_impl()
191 trap_test_put_nan_impl
<CharType
, ValType
>();
192 trap_test_get_nan_impl
<CharType
, ValType
>();
195 template<class CharType
, class ValType
> void trap_test_put_nan_impl()
197 std::locale old_locale
;
198 std::locale
new_locale(old_locale
,
199 new nonfinite_num_put
<CharType
>(trap_nan
));
201 std::basic_stringstream
<CharType
> ss
;
202 ss
.imbue(new_locale
);
204 ValType a1
= std::numeric_limits
<ValType
>::quiet_NaN();
206 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
207 || ss
.rdstate() == std::ios_base::badbit
);
210 ValType a2
= std::numeric_limits
<ValType
>::signaling_NaN();
212 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
213 || ss
.rdstate() == std::ios_base::badbit
);
216 template<class CharType
, class ValType
> void trap_test_get_nan_impl()
218 std::locale old_locale
;
219 std::locale
tmp_locale(old_locale
, new nonfinite_num_put
<CharType
>);
220 std::locale
new_locale(tmp_locale
,
221 new nonfinite_num_get
<CharType
>(trap_nan
));
223 std::basic_stringstream
<CharType
> ss
;
224 ss
.imbue(new_locale
);
226 ValType a1
= std::numeric_limits
<ValType
>::quiet_NaN();
230 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
235 ValType a2
= std::numeric_limits
<ValType
>::signaling_NaN();
239 BOOST_CHECK(ss
.rdstate() == std::ios_base::failbit
);
242 //------------------------------------------------------------------------------
244 // Test a selection of stream output options comparing result with expected string.
245 // Only uses CharType = char and ValType = double.
246 // Other types have already been tested above.
248 #define CHECKOUT(manips, expected)\
250 std::locale old_locale;\
251 std::locale tmp_locale(old_locale, new nonfinite_num_put<char>(0)); /* default flags. */\
252 std::locale new_locale(tmp_locale, new nonfinite_num_get<char>);\
253 std::ostringstream ss;\
254 ss.imbue(new_locale);\
256 std::basic_string<char> s = S_(expected);\
257 BOOST_CHECK_EQUAL(ss.str(), s);\
260 BOOST_AUTO_TEST_CASE(check_trap_nan)
261 { // Check that with trap_nan set, it really does throw exception.
262 std::locale old_locale
;
263 std::locale
tmp_locale(old_locale
, new nonfinite_num_put
<char>(trap_nan
));
264 std::locale
new_locale(tmp_locale
, new nonfinite_num_get
<char>);
265 std::ostringstream os
;
266 os
.imbue(new_locale
);
267 os
.exceptions(std::ios_base::badbit
| std::ios_base::failbit
); // Enable throwing exceptions.
268 double nan
= std::numeric_limits
<double>::quiet_NaN();
269 BOOST_MATH_CHECK_THROW((os
<< nan
), std::runtime_error
);
270 // warning : in "check_trap_nan": exception std::runtime_error is expected
271 } // BOOST_AUTO_TEST_CASE(check_trap_nan)
273 BOOST_AUTO_TEST_CASE(check_trap_inf
)
274 { // Check that with trap_nan set, it really does throw exception.
275 std::locale old_locale
;
276 std::locale
tmp_locale(old_locale
, new nonfinite_num_put
<char>(trap_infinity
));
277 std::locale
new_locale(tmp_locale
, new nonfinite_num_get
<char>);
278 std::ostringstream os
;
279 os
.imbue(new_locale
);
280 os
.exceptions(std::ios_base::badbit
| std::ios_base::failbit
); // Enable throwing exceptions.
281 double inf
= std::numeric_limits
<double>::infinity();
282 BOOST_MATH_CHECK_THROW((os
<< inf
), std::runtime_error
);
283 // warning : in "check_trap_inf": exception std::runtime_error is expected.
285 } // BOOST_AUTO_TEST_CASE(check_trap_nan_inf)
287 BOOST_AUTO_TEST_CASE(output_tests
)
290 CHECKOUT(0, "0"); // integer zero.
291 CHECKOUT(0., "0"); // double zero.
293 double nan
= std::numeric_limits
<double>::quiet_NaN();
294 double inf
= std::numeric_limits
<double>::infinity();
296 CHECKOUT(inf
, "inf"); // infinity.
297 CHECKOUT(-inf
, "-inf"); // infinity.
298 CHECKOUT(std::showpos
<< inf
, "+inf"); // infinity.
300 CHECKOUT(std::setw(6) << std::showpos
<< inf
, " +inf"); // infinity.
301 CHECKOUT(std::right
<< std::setw(6) << std::showpos
<< inf
, " +inf"); // infinity.
302 CHECKOUT(std::left
<< std::setw(6) << std::showpos
<< inf
, "+inf "); // infinity.
303 CHECKOUT(std::left
<< std::setw(6) << std::setprecision(6) << inf
, "inf "); // infinity.
304 CHECKOUT(std::left
<< std::setw(6) << std::setfill('*') << std::setprecision(6) << inf
, "inf***"); // infinity.
305 CHECKOUT(std::right
<< std::setw(6) << std::setfill('*') << std::setprecision(6) << inf
, "***inf"); // infinity.
306 CHECKOUT(std::internal
<< std::setw(6) << std::showpos
<< inf
, "+ inf"); // infinity.
307 CHECKOUT(std::internal
<< std::setw(6) << std::setfill('*') << std::showpos
<< inf
, "+**inf"); // infinity.
308 CHECKOUT(std::internal
<< std::setw(6) << std::setfill('*') << std::showpos
<< -inf
, "-**inf"); // infinity.
310 CHECKOUT(nan
, "nan"); // nan
311 CHECKOUT(std::setw(1) << nan
, "nan"); // nan, even if width was too small.
312 CHECKOUT(std::setprecision(10) << nan
, "nan"); // setprecision has no effect.
313 } // BOOST_AUTO_TEST_CASE(output_tests)
315 } // anonymous namespace
321 test_nonfinite_io.cpp
322 test_nonfinite_io.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Debug\test_nonfinite_io.exe
323 Running 4 test cases...
325 Compiler: Microsoft Visual C++ version 10.0
326 STL : Dinkumware standard library version 520
328 Entering test suite "Master Test Suite"
329 Entering test case "trap_test"
330 Leaving test case "trap_test"; testing time: 7ms
331 Entering test case "check_trap_nan"
332 Leaving test case "check_trap_nan"
333 Entering test case "check_trap_inf"
334 Leaving test case "check_trap_inf"; testing time: 1ms
335 Entering test case "output_tests"
336 Leaving test case "output_tests"; testing time: 3ms
337 Leaving test suite "Master Test Suite"
339 *** No errors detected