test_nonfinite_io.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Copyright 2011 Paul A. Bristow
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // test_nonfinite_trap.cpp
  6. #ifdef _MSC_VER
  7. # pragma warning(disable : 4127) // Expression is constant.
  8. #endif
  9. #define BOOST_TEST_MAIN
  10. #include <boost/test/unit_test.hpp>
  11. #include <libs/math/test/almost_equal.ipp> // Similar to BOOST_CLOSE_FRACTION.
  12. #include <libs/math/test/s_.ipp> // To create test strings like std::basic_string<CharType> s = S_("0 -0");
  13. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  14. #include <locale>
  15. #include <sstream>
  16. #include <iomanip>
  17. namespace {
  18. // Using an anonymous namespace resolves ambiguities on platforms
  19. // with fpclassify etc functions at global scope.
  20. using namespace boost::math;
  21. using boost::math::signbit;
  22. using boost::math::changesign;
  23. using (boost::math::isnan)(;
  24. //------------------------------------------------------------------------------
  25. // Test nonfinite_num_put and nonfinite_num_get facets by checking
  26. // loopback (output and re-input) of a few values,
  27. // but using all the built-in char and floating-point types.
  28. // Only the default output is used but various ostream options are tested seperately below.
  29. // Finite, infinite and NaN values (positive and negative) are used for the test.
  30. void trap_test_finite();
  31. void trap_test_inf();
  32. void trap_test_nan();
  33. BOOST_AUTO_TEST_CASE(trap_test)
  34. {
  35. trap_test_finite();
  36. trap_test_inf();
  37. trap_test_nan();
  38. }
  39. //------------------------------------------------------------------------------
  40. template<class CharType, class ValType> void trap_test_finite_impl();
  41. void trap_test_finite()
  42. { // Test finite using all the built-in char and floating-point types.
  43. trap_test_finite_impl<char, float>();
  44. trap_test_finite_impl<char, double>();
  45. trap_test_finite_impl<char, long double>();
  46. trap_test_finite_impl<wchar_t, float>();
  47. trap_test_finite_impl<wchar_t, double>();
  48. trap_test_finite_impl<wchar_t, long double>();
  49. }
  50. template<class CharType, class ValType> void trap_test_finite_impl()
  51. {
  52. std::locale old_locale;
  53. std::locale tmp_locale(old_locale,
  54. new nonfinite_num_put<CharType>(trap_infinity | trap_nan));
  55. std::locale new_locale(tmp_locale,
  56. new nonfinite_num_get<CharType>(trap_infinity | trap_nan));
  57. std::basic_stringstream<CharType> ss;
  58. ss.imbue(new_locale);
  59. ValType a1 = (ValType)1.2;
  60. ValType a2 = (ValType)-3.5;
  61. ValType a3 = (std::numeric_limits<ValType>::max)();
  62. ValType a4 = -(std::numeric_limits<ValType>::max)();
  63. ss << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4; // 1.2, -3.5, max, -max
  64. ValType b1, b2, b3, b4;
  65. ss >> b1 >> b2 >> b3 >> b4;
  66. BOOST_CHECK(almost_equal(b1, a1));
  67. BOOST_CHECK(almost_equal(b2, a2));
  68. BOOST_CHECK(almost_equal(b3, a3));
  69. BOOST_CHECK(almost_equal(b4, a4));
  70. BOOST_CHECK(b3 != std::numeric_limits<ValType>::infinity());
  71. BOOST_CHECK(b4 != -std::numeric_limits<ValType>::infinity());
  72. BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
  73. ss.clear();
  74. ss.str(S_(""));
  75. ss << "++5";
  76. ValType b5;
  77. ss >> b5;
  78. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
  79. }
  80. //------------------------------------------------------------------------------
  81. template<class CharType, class ValType> void trap_test_inf_impl();
  82. template<class CharType, class ValType> void trap_test_put_inf_impl();
  83. template<class CharType, class ValType> void trap_test_get_inf_impl();
  84. void trap_test_inf()
  85. { // Test infinity using all the built-in char and floating-point types.
  86. trap_test_inf_impl<char, float>();
  87. trap_test_inf_impl<char, double>();
  88. trap_test_inf_impl<char, long double>();
  89. trap_test_inf_impl<wchar_t, float>();
  90. trap_test_inf_impl<wchar_t, double>();
  91. trap_test_inf_impl<wchar_t, long double>();
  92. }
  93. template<class CharType, class ValType> void trap_test_inf_impl()
  94. {
  95. trap_test_put_inf_impl<CharType, ValType>();
  96. trap_test_get_inf_impl<CharType, ValType>();
  97. }
  98. template<class CharType, class ValType> void trap_test_put_inf_impl()
  99. {
  100. std::locale old_locale;
  101. std::locale new_locale(old_locale,
  102. new nonfinite_num_put<CharType>(trap_infinity));
  103. std::basic_stringstream<CharType> ss;
  104. ss.imbue(new_locale);
  105. ValType a1 = std::numeric_limits<ValType>::infinity();
  106. ss << a1;
  107. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit
  108. || ss.rdstate() == std::ios_base::badbit);
  109. ss.clear();
  110. ValType a2 = -std::numeric_limits<ValType>::infinity();
  111. ss << a2;
  112. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit
  113. || ss.rdstate() == std::ios_base::badbit);
  114. }
  115. template<class CharType, class ValType> void trap_test_get_inf_impl()
  116. {
  117. std::locale old_locale;
  118. std::locale tmp_locale(old_locale, new nonfinite_num_put<CharType>);
  119. std::locale new_locale(tmp_locale,
  120. new nonfinite_num_get<CharType>(trap_infinity));
  121. std::basic_stringstream<CharType> ss;
  122. ss.imbue(new_locale);
  123. ValType a1 = std::numeric_limits<ValType>::infinity();
  124. ss << a1;
  125. ValType b1;
  126. ss >> b1;
  127. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
  128. ss.clear();
  129. ss.str(S_(""));
  130. ValType a2 = -std::numeric_limits<ValType>::infinity();
  131. ss << a2;
  132. ValType b2;
  133. ss >> b2;
  134. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
  135. }
  136. //------------------------------------------------------------------------------
  137. template<class CharType, class ValType> void trap_test_nan_impl();
  138. template<class CharType, class ValType> void trap_test_put_nan_impl();
  139. template<class CharType, class ValType> void trap_test_get_nan_impl();
  140. void trap_test_nan()
  141. { // Test NaN using all the built-in char and floating-point types.
  142. trap_test_nan_impl<char, float>();
  143. trap_test_nan_impl<char, double>();
  144. trap_test_nan_impl<char, long double>();
  145. trap_test_nan_impl<wchar_t, float>();
  146. trap_test_nan_impl<wchar_t, double>();
  147. trap_test_nan_impl<wchar_t, long double>();
  148. }
  149. template<class CharType, class ValType> void trap_test_nan_impl()
  150. {
  151. trap_test_put_nan_impl<CharType, ValType>();
  152. trap_test_get_nan_impl<CharType, ValType>();
  153. }
  154. template<class CharType, class ValType> void trap_test_put_nan_impl()
  155. {
  156. std::locale old_locale;
  157. std::locale new_locale(old_locale,
  158. new nonfinite_num_put<CharType>(trap_nan));
  159. std::basic_stringstream<CharType> ss;
  160. ss.imbue(new_locale);
  161. ValType a1 = std::numeric_limits<ValType>::quiet_NaN();
  162. ss << a1;
  163. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit
  164. || ss.rdstate() == std::ios_base::badbit);
  165. ss.clear();
  166. ValType a2 = std::numeric_limits<ValType>::signaling_NaN();
  167. ss << a2;
  168. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit
  169. || ss.rdstate() == std::ios_base::badbit);
  170. }
  171. template<class CharType, class ValType> void trap_test_get_nan_impl()
  172. {
  173. std::locale old_locale;
  174. std::locale tmp_locale(old_locale, new nonfinite_num_put<CharType>);
  175. std::locale new_locale(tmp_locale,
  176. new nonfinite_num_get<CharType>(trap_nan));
  177. std::basic_stringstream<CharType> ss;
  178. ss.imbue(new_locale);
  179. ValType a1 = std::numeric_limits<ValType>::quiet_NaN();
  180. ss << a1;
  181. ValType b1;
  182. ss >> b1;
  183. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
  184. ss.clear();
  185. ss.str(S_(""));
  186. ValType a2 = std::numeric_limits<ValType>::signaling_NaN();
  187. ss << a2;
  188. ValType b2;
  189. ss >> b2;
  190. BOOST_CHECK(ss.rdstate() == std::ios_base::failbit);
  191. }
  192. //------------------------------------------------------------------------------
  193. // Test a selection of stream output options comparing result with expected string.
  194. // Only uses CharType = char and ValType = double.
  195. // Other types have already been tested above.
  196. #define CHECKOUT(manips, expected)\
  197. {\
  198. std::locale old_locale;\
  199. std::locale tmp_locale(old_locale, new nonfinite_num_put<char>(0)); /* default flags. */\
  200. std::locale new_locale(tmp_locale, new nonfinite_num_get<char>);\
  201. std::ostringstream ss;\
  202. ss.imbue(new_locale);\
  203. ss << manips;\
  204. std::basic_string<char> s = S_(expected);\
  205. BOOST_CHECK_EQUAL(ss.str(), s);\
  206. }\
  207. BOOST_AUTO_TEST_CASE(check_trap_nan)
  208. { // Check that with trap_nan set, it really does throw exception.
  209. std::locale old_locale;
  210. std::locale tmp_locale(old_locale, new nonfinite_num_put<char>(trap_nan));
  211. std::locale new_locale(tmp_locale, new nonfinite_num_get<char>);
  212. std::ostringstream os;
  213. os.imbue(new_locale);
  214. os.exceptions(std::ios_base::badbit | std::ios_base::failbit); // Enable throwing exceptions.
  215. double nan = std::numeric_limits<double>::quiet_NaN();
  216. BOOST_MATH_CHECK_THROW((os << nan), std::runtime_error);
  217. // warning : in "check_trap_nan": exception std::runtime_error is expected
  218. } // BOOST_AUTO_TEST_CASE(check_trap_nan)
  219. BOOST_AUTO_TEST_CASE(check_trap_inf)
  220. { // Check that with trap_nan set, it really does throw exception.
  221. std::locale old_locale;
  222. std::locale tmp_locale(old_locale, new nonfinite_num_put<char>(trap_infinity));
  223. std::locale new_locale(tmp_locale, new nonfinite_num_get<char>);
  224. std::ostringstream os;
  225. os.imbue(new_locale);
  226. os.exceptions(std::ios_base::badbit | std::ios_base::failbit); // Enable throwing exceptions.
  227. double inf = std::numeric_limits<double>::infinity();
  228. BOOST_MATH_CHECK_THROW((os << inf), std::runtime_error);
  229. // warning : in "check_trap_inf": exception std::runtime_error is expected.
  230. } // BOOST_AUTO_TEST_CASE(check_trap_nan_inf)
  231. BOOST_AUTO_TEST_CASE(output_tests)
  232. {
  233. // Positive zero.
  234. CHECKOUT(0, "0"); // integer zero.
  235. CHECKOUT(0., "0"); // double zero.
  236. double nan = std::numeric_limits<double>::quiet_NaN();
  237. double inf = std::numeric_limits<double>::infinity();
  238. CHECKOUT(inf, "inf"); // infinity.
  239. CHECKOUT(-inf, "-inf"); // infinity.
  240. CHECKOUT(std::showpos << inf, "+inf"); // infinity.
  241. CHECKOUT(std::setw(6) << std::showpos << inf, " +inf"); // infinity.
  242. CHECKOUT(std::right << std::setw(6) << std::showpos << inf, " +inf"); // infinity.
  243. CHECKOUT(std::left << std::setw(6) << std::showpos << inf, "+inf "); // infinity.
  244. CHECKOUT(std::left << std::setw(6) << std::setprecision(6) << inf, "inf "); // infinity.
  245. CHECKOUT(std::left << std::setw(6) << std::setfill('*') << std::setprecision(6) << inf, "inf***"); // infinity.
  246. CHECKOUT(std::right << std::setw(6) << std::setfill('*') << std::setprecision(6) << inf, "***inf"); // infinity.
  247. CHECKOUT(std::internal<< std::setw(6) << std::showpos << inf, "+ inf"); // infinity.
  248. CHECKOUT(std::internal<< std::setw(6) << std::setfill('*') << std::showpos << inf, "+**inf"); // infinity.
  249. CHECKOUT(std::internal<< std::setw(6) << std::setfill('*') << std::showpos << -inf, "-**inf"); // infinity.
  250. CHECKOUT(nan, "nan"); // nan
  251. CHECKOUT(std::setw(1) << nan, "nan"); // nan, even if width was too small.
  252. CHECKOUT(std::setprecision(10) << nan, "nan"); // setprecision has no effect.
  253. } // BOOST_AUTO_TEST_CASE(output_tests)
  254. } // anonymous namespace
  255. /*
  256. Output:
  257. test_nonfinite_io.cpp
  258. test_nonfinite_io.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Debug\test_nonfinite_io.exe
  259. Running 4 test cases...
  260. Platform: Win32
  261. Compiler: Microsoft Visual C++ version 10.0
  262. STL : Dinkumware standard library version 520
  263. Boost : 1.49.0
  264. Entering test suite "Master Test Suite"
  265. Entering test case "trap_test"
  266. Leaving test case "trap_test"; testing time: 7ms
  267. Entering test case "check_trap_nan"
  268. Leaving test case "check_trap_nan"
  269. Entering test case "check_trap_inf"
  270. Leaving test case "check_trap_inf"; testing time: 1ms
  271. Entering test case "output_tests"
  272. Leaving test case "output_tests"; testing time: 3ms
  273. Leaving test suite "Master Test Suite"
  274. *** No errors detected
  275. */