1 // Distributed under the Boost Software License, Version 1.0.
2 // (See accompanying file LICENSE_1_0.txt
3 // or copy at http://www.boost.org/LICENSE_1_0.txt)
5 // Copyright (c) 2006 Johan Rade
6 // Copyright (c) 2011 Paul A. Bristow comments
7 // Copyright (c) 2011 John Maddock
10 \brief Basic tests of the nonfinite num facets.
12 \detail If has_infinity and has_nan, then
13 basic_test outputs using nonfinite_num_put facet
14 and reads back in using nonfinite_num_ facet,
15 and checks loopback OK.
17 Also checks that output of infinity, -infinity and NaN are as expected,
18 using C99 specification "nan -nan nan -nan" and "inf -inf".
19 Also includes a few combinations of display manipulators
20 (left, right, internal, showpos)
21 and checks that can input C99 infinity and NaN too.
26 # pragma warning(disable : 4702) // Unreachable code.
33 #define BOOST_TEST_MAIN
35 #include <boost/test/unit_test.hpp>
37 #include "almost_equal.ipp"
40 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
43 { // The anonymous namespace resolves ambiguities on
44 // platforms with fpclassify etc functions at global scope.
46 using namespace boost::math;
47 using boost::math::signbit;
48 using boost::math::changesign;
49 using boost::math::isnan;
51 //------------------------------------------------------------------------------
53 void basic_test_finite();
54 void basic_test_inf();
55 void basic_test_nan();
56 void basic_test_format();
58 BOOST_AUTO_TEST_CASE(basic_test)
66 //------------------------------------------------------------------------------
68 template<class CharType, class ValType> void basic_test_finite_impl();
70 void basic_test_finite()
72 basic_test_finite_impl<char, float>();
73 basic_test_finite_impl<char, double>();
74 basic_test_finite_impl<char, long double>();
75 basic_test_finite_impl<wchar_t, float>();
76 basic_test_finite_impl<wchar_t, double>();
77 basic_test_finite_impl<wchar_t, long double>();
80 template<class CharType, class ValType> void basic_test_finite_impl()
82 if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
85 std::locale old_locale;
86 std::locale tmp_locale(old_locale, new nonfinite_num_put<CharType>);
87 std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
89 std::basic_stringstream<CharType> ss;
92 ValType a1 = (ValType)1.2;
93 ValType a2 = (ValType)-3.5;
94 ValType a3 = (std::numeric_limits<ValType>::max)();
95 ValType a4 = -(std::numeric_limits<ValType>::max)();
96 ss << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4;
98 ValType b1, b2, b3, b4;
99 ss >> b1 >> b2 >> b3 >> b4;
101 BOOST_CHECK(almost_equal(b1, a1));
102 BOOST_CHECK(almost_equal(b2, a2));
103 BOOST_CHECK(almost_equal(b3, a3));
104 BOOST_CHECK(almost_equal(b4, a4));
105 BOOST_CHECK(b3 != std::numeric_limits<ValType>::infinity());
106 BOOST_CHECK(b4 != -std::numeric_limits<ValType>::infinity());
107 BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
115 BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
118 //------------------------------------------------------------------------------
120 template<class CharType, class ValType> void basic_test_inf_impl();
122 void basic_test_inf()
124 basic_test_inf_impl<char, float>();
125 basic_test_inf_impl<char, double>();
126 basic_test_inf_impl<char, long double>();
127 basic_test_inf_impl<wchar_t, float>();
128 basic_test_inf_impl<wchar_t, double>();
129 basic_test_inf_impl<wchar_t, long double>();
132 template<class CharType, class ValType> void basic_test_inf_impl()
134 if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
137 std::locale old_locale;
138 std::locale tmp_locale(old_locale, new nonfinite_num_put<CharType>);
139 std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
141 std::basic_stringstream<CharType> ss;
142 ss.imbue(new_locale);
144 ValType a1 = std::numeric_limits<ValType>::infinity();
145 ValType a2 = -std::numeric_limits<ValType>::infinity();
147 BOOST_CHECK((boost::math::isinf)(a1));
148 BOOST_CHECK((boost::math::isinf)(a2));
150 ss << a1 << ' ' << a2;
152 std::basic_string<CharType> s = S_("inf -inf");
153 BOOST_CHECK(ss.str() == s);
155 ss << " infinity"; // Alternative C99 representation of infinity.
162 BOOST_CHECK(b1 == a1);
163 BOOST_CHECK(b2 == a2);
164 BOOST_CHECK(b3 == std::numeric_limits<ValType>::infinity());
165 BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
168 //------------------------------------------------------------------------------
170 template<class CharType, class ValType> void basic_test_nan_impl();
172 void basic_test_nan()
174 basic_test_nan_impl<char, float>();
175 basic_test_nan_impl<char, double>();
176 basic_test_nan_impl<char, long double>();
177 basic_test_nan_impl<wchar_t, float>();
178 basic_test_nan_impl<wchar_t, double>();
179 basic_test_nan_impl<wchar_t, long double>();
182 template<class CharType, class ValType> void basic_test_nan_impl()
184 if((std::numeric_limits<ValType>::has_quiet_NaN == 0) || (std::numeric_limits<ValType>::quiet_NaN() == 0))
187 std::locale old_locale;
188 std::locale tmp_locale(old_locale, new nonfinite_num_put<CharType>);
189 std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
191 std::basic_stringstream<CharType> ss;
192 ss.imbue(new_locale);
194 ValType a1 = std::numeric_limits<ValType>::quiet_NaN();
195 ValType a2 = (boost::math::changesign)(std::numeric_limits<ValType>::quiet_NaN());
196 ValType a3 = std::numeric_limits<ValType>::signaling_NaN();
197 ValType a4 = (boost::math::changesign)(std::numeric_limits<ValType>::signaling_NaN());
198 ss << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4;
200 BOOST_CHECK((boost::math::isnan)(a1) && (boost::math::isnan)(a2) && (boost::math::isnan)(a3) && (boost::math::isnan)(a4));
202 std::basic_string<CharType> s = S_("nan -nan nan -nan");
203 BOOST_CHECK(ss.str() == s);
205 // Alternative C99 representation of NaN.
208 ValType b1, b2, b3, b4, b5;
209 ss >> b1 >> b2 >> b3 >> b4 >> b5;
211 BOOST_CHECK((isnan)(b1));
212 BOOST_CHECK((isnan)(b2));
213 BOOST_CHECK((isnan)(b3));
214 BOOST_CHECK((isnan)(b4));
215 BOOST_CHECK((isnan)(b5));
217 BOOST_CHECK(!(signbit)(b1));
218 BOOST_CHECK((signbit)(b2));
219 BOOST_CHECK(!(signbit)(b3));
220 BOOST_CHECK((signbit)(b4));
221 BOOST_CHECK(!(signbit)(b5));
223 BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
226 //------------------------------------------------------------------------------
228 template<class CharType, class ValType> void basic_test_format_impl();
230 void basic_test_format()
232 basic_test_format_impl<char, float>();
233 basic_test_format_impl<char, double>();
234 basic_test_format_impl<char, long double>();
235 basic_test_format_impl<wchar_t, float>();
236 basic_test_format_impl<wchar_t, double>();
237 basic_test_format_impl<wchar_t, long double>();
240 template<class CharType, class ValType> void basic_test_format_impl()
242 if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
245 std::locale old_locale;
246 std::locale tmp_locale(old_locale, new nonfinite_num_put<CharType>);
247 std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
249 std::basic_stringstream<CharType> ss;
250 ss.imbue(new_locale);
252 ValType a = std::numeric_limits<ValType>::infinity();
254 BOOST_CHECK((boost::math::isinf)(a));
256 ss << std::setw(6) << a; // Expect right justified in field of six, so 3 leading spaces.
258 ss << std::setw(2) << a; // Too narrow for "inf", but should still be "inf".
260 ss << std::left << std::setw(5) << a; // 5 - 3 leaves two trailing spaces.
262 ss << std::showpos << std::internal << std::setw(7) << a; // 3 internal spaces between + and "inf".
264 ss << std::uppercase << std::right << std::setw(6) << a; // still showpos, so "space, space, +INF".
266 std::basic_string<CharType> s = S_(" inf|inf|inf |+ inf| +INF");
267 BOOST_CHECK(ss.str() == s);
270 //------------------------------------------------------------------------------
272 } // anonymous namespace