1 // Copyright (c) 2006 Johan Rade
2 // Copyright (c) 2011 Paul A. Bristow comments
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 /*!
8 \file
9 \brief Legacy (non-C99) tests of the nonfinite num facets.
10
11 \detail legacy_test outputs using nonfinite_num_put facet
12 with legacy flag, and reads back in using nonfinite_num_ facet,
13 and checks loopback OK.
14
15 Also checks that output of infinity, -infinity and NaN are as expected,
16 including the 'legacy' "1.#IND", "1.#QNAN", "1.#SNAN" representations
17 (was used by MSVC but now all represented on output by "1.#QNAN")
18 and qnan snan nanq nans (used by other systems)
19 excluding C99 specification "nan -nan nan -nan" and "inf -inf".
20 */
21
22 #ifdef _MSC_VER
23 # pragma warning(disable : 4702)
24 #endif
25
26 #include <iomanip>
27 #include <locale>
28 #include <sstream>
29
30 #define BOOST_TEST_MAIN
31 #include <boost/test/unit_test.hpp>
32
33 //#include "almost_equal.hpp"
34 //#include "S_.hpp"
35
36 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
37
38 namespace {
39
40 // The anonymous namespace resolves ambiguities on platforms
41 // with fpclassify etc functions declared at global scope.
42
43 using namespace boost::math;
44 using boost::math::signbit;
45 using boost::math::changesign;
46 using boost::math::isnan;
47
48 //------------------------------------------------------------------------------
49
50 void legacy_test_inf();
51 void legacy_test_nan();
52
BOOST_AUTO_TEST_CASE(legacy_test)53 BOOST_AUTO_TEST_CASE(legacy_test)
54 {
55 legacy_test_inf();
56 legacy_test_nan();
57 }
58
59 //------------------------------------------------------------------------------
60
61 template<class CharType, class ValType> void legacy_test_inf_impl();
62
legacy_test_inf()63 void legacy_test_inf()
64 {
65 legacy_test_inf_impl<char, float>();
66 legacy_test_inf_impl<char, double>();
67 legacy_test_inf_impl<char, long double>();
68 legacy_test_inf_impl<wchar_t, float>();
69 legacy_test_inf_impl<wchar_t, double>();
70 legacy_test_inf_impl<wchar_t, long double>();
71 }
72
legacy_test_inf_impl()73 template<class CharType, class ValType> void legacy_test_inf_impl()
74 {
75 std::locale old_locale;
76 std::locale new_locale(old_locale, new nonfinite_num_get<CharType>(legacy));
77
78 std::basic_stringstream<CharType> ss;
79 ss.imbue(new_locale);
80
81 ValType a1 = std::numeric_limits<ValType>::infinity();
82 ValType a2 = -std::numeric_limits<ValType>::infinity();
83 ss << a1 << ' ' << a2;
84
85 ss << " 1.#INF";
86
87 ValType b1, b2, b3;
88 ss >> b1 >> b2 >> b3;
89
90 BOOST_CHECK(b1 == a1);
91 BOOST_CHECK(b2 == a2);
92 BOOST_CHECK(b3 == std::numeric_limits<ValType>::infinity());
93 BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
94 }
95
96 //------------------------------------------------------------------------------
97
98 template<class CharType, class ValType> void legacy_test_nan_impl();
99
legacy_test_nan()100 void legacy_test_nan()
101 {
102 legacy_test_nan_impl<char, float>();
103 legacy_test_nan_impl<char, double>();
104 legacy_test_nan_impl<char, long double>();
105 legacy_test_nan_impl<wchar_t, float>();
106 legacy_test_nan_impl<wchar_t, double>();
107 legacy_test_nan_impl<wchar_t, long double>();
108 }
109
legacy_test_nan_impl()110 template<class CharType, class ValType> void legacy_test_nan_impl()
111 {
112 std::locale old_locale;
113 std::locale new_locale(old_locale, new nonfinite_num_get<CharType>(legacy));
114
115 std::basic_stringstream<CharType> ss;
116 ss.imbue(new_locale);
117
118 ValType a1 = std::numeric_limits<ValType>::quiet_NaN();
119 ValType a2 = -std::numeric_limits<ValType>::quiet_NaN();
120 ValType a3 = std::numeric_limits<ValType>::signaling_NaN();
121 ValType a4 = -std::numeric_limits<ValType>::signaling_NaN();
122 ss << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4;
123
124 ss << " qnan snan nanq nans 1.#IND 1.#QNAN 1.#SNAN";
125
126 ValType b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11;
127 ss >> b1 >> b2 >> b3 >> b4 >> b5 >> b6 >> b7 >> b8 >> b9 >> b10 >> b11;
128
129 // std::cout << b11 << std::endl; // Confirms that legacy
130 // IND, SNAN and QNAN are considered the same,
131 // and both output the legacy string "1.#QNAN".
132
133 BOOST_CHECK((isnan)(b1));
134 BOOST_CHECK((isnan)(b2));
135 BOOST_CHECK((isnan)(b3));
136 BOOST_CHECK((isnan)(b4));
137 BOOST_CHECK((isnan)(b5));
138 BOOST_CHECK((isnan)(b6));
139 BOOST_CHECK((isnan)(b7));
140 BOOST_CHECK((isnan)(b8));
141 BOOST_CHECK((isnan)(b9));
142 BOOST_CHECK((isnan)(b10));
143 BOOST_CHECK((isnan)(b11)); // Johan V3 1.#SNAN failed on MSVC 10.
144 // Change in nonfinite_num_facet.hpp Paul A. Bristow 11 Apr 11 makes work OK.
145 /*
146 // These tests fail on platforms, such as gcc,
147 // that use the same representation of +nan and -nan.
148
149 BOOST_CHECK(!(signbit)(b1));
150 BOOST_CHECK((signbit)(b2));
151 BOOST_CHECK(!(signbit)(b3));
152 BOOST_CHECK((signbit)(b4));
153 */
154 BOOST_CHECK(!(signbit)(b5));
155 BOOST_CHECK(!(signbit)(b6));
156 BOOST_CHECK(!(signbit)(b7));
157 BOOST_CHECK(!(signbit)(b8));
158 BOOST_CHECK(!(signbit)(b9));
159 BOOST_CHECK(!(signbit)(b10));
160 BOOST_CHECK(!(signbit)(b11)); // Johan V3 1.#SNAN failed MSVC 10.
161
162 BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit); // Fails if SNAN test fails.
163 }
164
165 //------------------------------------------------------------------------------
166
167 } // anonymous namespace
168
169 /*
170
171 Output:
172
173 legacy_test.vcxproj -> J:\Cpp\fp_facet\fp_facet\Debug\legacy_test.exe
174 Running 1 test case...
175 1.#QNAN
176 1.#QNAN
177 1.#QNAN
178 1.#QNAN
179 1.#QNAN
180 1.#QNAN
181
182 *** No errors detected
183
184
185 */
186