test_signed_zero.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2006 Johan Rade
  2. // Copyright 2011 Paul A. Bristow To incorporate into Boost.Math
  3. // Copyright 2012 Paul A. Bristow with new tests.
  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)
  7. #ifdef _MSC_VER
  8. # pragma warning(disable : 4127) // Expression is constant.
  9. #endif
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  13. #include "s_.ipp" // To create test strings like std::basic_string<CharType> s = S_("0 -0");
  14. #include <iomanip>
  15. #include <locale>
  16. #include <sstream>
  17. #include <ostream>
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <limits>
  21. #include <iostream>
  22. namespace {
  23. // Using an anonymous namespace resolves ambiguities on platforms
  24. // with fpclassify etc functions at global scope.
  25. using namespace boost::math;
  26. using boost::math::signbit;
  27. using boost::math::changesign;
  28. using boost::math::isnan;
  29. //------------------------------------------------------------------------------
  30. template<class CharType, class ValType> void signed_zero_test_impl();
  31. // Loopback tests using all built-in char and floating-point types.
  32. BOOST_AUTO_TEST_CASE(signed_zero_test)
  33. {
  34. std::cout
  35. << "BuildInfo:" << '\n'
  36. << " platform " << BOOST_PLATFORM << '\n'
  37. << " compiler " << BOOST_COMPILER << '\n'
  38. << " STL " << BOOST_STDLIB << '\n'
  39. << " Boost version " << BOOST_VERSION/100000 << "."
  40. << BOOST_VERSION/100 % 1000 << "."
  41. << BOOST_VERSION % 100
  42. << std::endl;
  43. signed_zero_test_impl<char, float>();
  44. signed_zero_test_impl<char, double>();
  45. signed_zero_test_impl<char, long double>();
  46. signed_zero_test_impl<wchar_t, float>();
  47. signed_zero_test_impl<wchar_t, double>();
  48. signed_zero_test_impl<wchar_t, long double>();
  49. }
  50. template<class CharType, class ValType> void signed_zero_test_impl()
  51. {
  52. if (signbit(static_cast<CharType>(-1e-6f) / (std::numeric_limits<CharType>::max)()) != -0)
  53. {
  54. BOOST_TEST_MESSAGE("Signed zero is not supported on this platform!");
  55. return;
  56. }
  57. std::locale old_locale;
  58. std::locale tmp_locale(
  59. old_locale, new nonfinite_num_put<CharType>(signed_zero));
  60. std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
  61. std::basic_stringstream<CharType> ss;
  62. ss.imbue(new_locale);
  63. std::basic_string<CharType> null = S_("");
  64. std::basic_string<CharType> s1 = S_("123");
  65. ss << s1 << std::endl;
  66. ss.str(null);
  67. BOOST_CHECK(ss.str() == null); //
  68. ValType a1 = static_cast<ValType>(0); // zero.
  69. ValType a2 = (changesign)(static_cast<ValType>(0)); // negative signed zero.
  70. BOOST_CHECK(!(signbit)(a1)); //
  71. BOOST_CHECK((signbit)(a2));
  72. ss << a1 << ' ' << a2;
  73. std::basic_string<CharType> s = S_("0 -0"); // Expected.
  74. BOOST_CHECK(ss.str() == s);
  75. ValType b1, b2;
  76. ss >> b1 >> b2; // Read back in.
  77. BOOST_CHECK(b1 == a1);
  78. BOOST_CHECK(b2 == a2);
  79. BOOST_CHECK(!(signbit)(b1));
  80. BOOST_CHECK((signbit)(b2));
  81. BOOST_CHECK(ss.rdstate() == std::ios_base::eofbit);
  82. } // template<class CharType, class ValType> void signed_zero_test_impl()
  83. // Checking output of types char using first default & then using signed_zero flag.
  84. #define CHECKOUT(manips, expected)\
  85. {\
  86. {\
  87. std::locale old_locale;\
  88. std::locale tmp_locale(old_locale, new nonfinite_num_put<char>(0));\
  89. std::locale new_locale(tmp_locale, new nonfinite_num_get<char>);\
  90. std::ostringstream ss;\
  91. ss.imbue(new_locale);\
  92. ss << manips;\
  93. std::basic_string<char> s = S_(expected);\
  94. BOOST_CHECK_EQUAL(ss.str(), s);\
  95. }\
  96. {\
  97. std::locale old_locale;\
  98. std::locale tmp_locale(old_locale, new nonfinite_num_put<char>(signed_zero));\
  99. std::locale new_locale(tmp_locale, new nonfinite_num_get<char>);\
  100. std::ostringstream ss;\
  101. ss.imbue(new_locale);\
  102. ss << manips;\
  103. std::basic_string<char> s = S_(expected);\
  104. BOOST_CHECK_EQUAL(ss.str(), s);\
  105. }\
  106. }\
  107. BOOST_AUTO_TEST_CASE(misc_output_tests)
  108. { // Tests of output using a variety of output options.
  109. //
  110. // STD libraries don't all format zeros the same,
  111. // so figure out what the library-specific formatting is
  112. // and then make sure that our facet produces the same...
  113. //
  114. bool precision_after = false; // Prints N digits after the point rather than N digits total
  115. bool triple_exponent = false; // Has 3 digits in the exponent rather than 2.
  116. std::stringstream ss;
  117. ss << std::showpoint << std::setprecision(6) << 0.0;
  118. if(ss.str().size() == 8)
  119. precision_after = true;
  120. ss.str("");
  121. ss << std::scientific << 0.0;
  122. triple_exponent = ss.str().size() - ss.str().find_first_of('e') == 5;
  123. // Positive zero.
  124. CHECKOUT(0, "0"); // integer zero.
  125. CHECKOUT(0., "0"); // double zero.
  126. CHECKOUT(std::setw(2) << 0., " 0");
  127. CHECKOUT(std::setw(4) << 0., " 0");
  128. CHECKOUT(std::right << std::setw(4) << 0., " 0");
  129. CHECKOUT(std::left << std::setw(4) << 0., "0 ");
  130. CHECKOUT(std::setw(4) << std::setfill('*') << 0., "***0");
  131. CHECKOUT(std::setw(4) << std::internal << std::setfill('*') << 0., "***0"); // left adjust sign and right adjust value.
  132. CHECKOUT(std::showpos << std::setw(4) << std::internal << std::setfill('*') << 0., "+**0"); // left adjust sign and right adjust value.
  133. if(precision_after)
  134. {
  135. // BOOST_STDLIB == ("Dinkumware standard library version" BOOST_STRINGIZE(_CPPLIB_VER)) )
  136. CHECKOUT(std::showpoint << 0., "0.000000"); // std::setprecision(6)
  137. CHECKOUT(std::setprecision(2) << std::showpoint << 0., "0.00");
  138. }
  139. else
  140. {
  141. CHECKOUT(std::showpoint << 0., "0.00000"); // std::setprecision(6)
  142. CHECKOUT(std::setprecision(2) << std::showpoint << 0., "0.0");
  143. }
  144. CHECKOUT(std::fixed << std::setw(5) << std::setfill('0') << std::setprecision(2) << 0., "00.00");
  145. CHECKOUT(std::fixed << std::setw(6) << std::setfill('0') << std::setprecision(2) << 0., "000.00");
  146. CHECKOUT(std::fixed << std::setw(6) << std::setfill('0') << std::setprecision(3) << 0., "00.000");
  147. CHECKOUT(std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(3) << 0., "*0.000");
  148. CHECKOUT(std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(2) << std::left << 0.0, "0.00**");
  149. CHECKOUT(std::showpos << 0., "+0");
  150. CHECKOUT(std::showpos << std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(2) << std::left << 0.0, "+0.00*");
  151. if(triple_exponent)
  152. {
  153. CHECKOUT(std::scientific << std::showpoint << std::setw(10) << std::setfill('*') << std::setprecision(1) << std::left << 0., "0.0e+000**");
  154. }
  155. else
  156. {
  157. CHECKOUT(std::scientific << std::showpoint << std::setw(10) << std::setfill('*') << std::setprecision(1) << std::left << 0., "0.0e+00***");
  158. }
  159. CHECKOUT(std::fixed << std::showpoint << std::setw(6) << std::setfill('*') << std::setprecision(3) << std::left << 0., "0.000*");
  160. double nz = (changesign)(static_cast<double>(0)); // negative signed zero.
  161. CHECKOUT(nz, "-0");
  162. // CHECKOUT(std::defaultfloat << nz, "-0"); Only for C++11
  163. CHECKOUT(std::showpos << nz, "-0"); // Ignore showpos because is negative.
  164. CHECKOUT(std::setw(2) << nz, "-0");
  165. CHECKOUT(std::setw(4) << nz, " -0");
  166. CHECKOUT(std::right << std::setw(4) << nz, " -0");
  167. CHECKOUT(std::left << std::setw(4) << nz, "-0 ");
  168. CHECKOUT(std::setw(4) << std::setfill('*') << nz, "**-0");
  169. CHECKOUT(std::setw(4) << std::internal << std::setfill('*') << nz, "-**0"); // Use std::internal to left adjust sign and right adjust value.
  170. CHECKOUT(std::showpos << std::setw(4) << std::internal << std::setfill('*') << nz, "-**0");
  171. CHECKOUT(std::fixed << std::setw(5) << std::setfill('0') << std::setprecision(2) << 0., "00.00");
  172. CHECKOUT(std::fixed << std::setw(6) << std::setfill('0') << std::setprecision(2) << 0., "000.00");
  173. CHECKOUT(std::fixed << std::setw(6) << std::setfill('0') << std::setprecision(3) << 0., "00.000");
  174. CHECKOUT(std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(3) << 0., "*0.000");
  175. CHECKOUT(std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(2) << std::left << 0.0, "0.00**");
  176. CHECKOUT(std::setprecision(2) << nz, "-0"); // No showpoint, so no decimal point nor trailing zeros.
  177. if(precision_after)
  178. {
  179. CHECKOUT(std::setprecision(2) << std::showpoint << nz, "-0.00"); // or "-0.0"
  180. CHECKOUT(std::setw(1) << std::setprecision(3) << std::showpoint << nz, "-0.000"); // Not enough width for precision overflows width. or "-0.00"
  181. }
  182. else
  183. {
  184. CHECKOUT(std::setprecision(2) << std::showpoint << nz, "-0.0"); // or "-0.00"
  185. CHECKOUT(std::setw(1) << std::setprecision(3) << std::showpoint << nz, "-0.00"); // Not enough width for precision overflows width. or "-0.000"
  186. }
  187. if(triple_exponent)
  188. {
  189. CHECKOUT(std::scientific << std::showpoint << std::setw(10) << std::setfill('*') << std::setprecision(1) << std::left << nz, "-0.0e+000*"); // -0.0e+00**
  190. }
  191. else
  192. {
  193. CHECKOUT(std::scientific << std::showpoint << std::setw(10) << std::setfill('*') << std::setprecision(1) << std::left << nz, "-0.0e+00**"); // -0.0e+000*
  194. }
  195. CHECKOUT(std::fixed << std::showpoint << std::setw(6) << std::setfill('*') << std::setprecision(3) << std::left << 0., "0.000*");
  196. // Non zero values.
  197. CHECKOUT(std::showpos << std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(2) << std::left << 42., "+42.00");
  198. CHECKOUT(std::showpos << std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(2) << std::left << 4.2, "+4.20*");
  199. CHECKOUT(std::showpos << std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(2) << std::left << 1.22, "+1.22*");
  200. CHECKOUT(std::showpos << std::fixed << std::setw(6) << std::setfill('*') << std::setprecision(2) << std::left << 0.12, "+0.12*");
  201. CHECKOUT(std::setprecision(4) << std::showpoint << 1.2, "1.200");
  202. }
  203. } // anonymous namespace
  204. /*
  205. Output:
  206. test_signed_zero.cpp
  207. Running 2 test cases...
  208. Platform: Win32
  209. Compiler: Microsoft Visual C++ version 10.0
  210. STL : Dinkumware standard library version 520
  211. Boost : 1.49.0
  212. Entering test suite "Master Test Suite"
  213. Entering test case "signed_zero_test"
  214. Leaving test case "signed_zero_test"; testing time: 2ms
  215. Entering test case "misc_output_tests"
  216. Leaving test case "misc_output_tests"; testing time: 15ms
  217. Leaving test suite "Master Test Suite"
  218. *** No errors detected
  219. */