test_lexical_cast.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2006 Johan Rade
  2. // Copyright (c) 2011 Paul A. Bristow incorporated Boost.Math
  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. //#ifdef _MSC_VER
  7. //# pragma warning(disable : 4127 4511 4512 4701 4702)
  8. //#endif
  9. #define BOOST_TEST_MAIN
  10. #include <limits>
  11. #include <locale>
  12. #include <string>
  13. #include <boost/lexical_cast.hpp>
  14. #include <boost/test/unit_test.hpp>
  15. #include <boost/math/special_functions/nonfinite_num_facets.hpp>
  16. #include <boost/math/special_functions/sign.hpp>
  17. #include <boost/math/special_functions/fpclassify.hpp>
  18. #include "almost_equal.ipp"
  19. #include "s_.ipp"
  20. namespace {
  21. // the anonymous namespace resolves ambiguities on platforms
  22. // with fpclassify etc functions at global scope
  23. using boost::lexical_cast;
  24. using namespace boost::math;
  25. using boost::math::signbit;
  26. using boost::math::changesign;
  27. using boost::math::isnan;
  28. //------------------------------------------------------------------------------
  29. template<class CharType, class ValType> void lexical_cast_test_impl();
  30. BOOST_AUTO_TEST_CASE(lexical_cast_test)
  31. {
  32. lexical_cast_test_impl<char, float>();
  33. lexical_cast_test_impl<char, double>();
  34. lexical_cast_test_impl<char, long double>();
  35. lexical_cast_test_impl<wchar_t, float>();
  36. lexical_cast_test_impl<wchar_t, double>();
  37. lexical_cast_test_impl<wchar_t, long double>();
  38. }
  39. template<class CharType, class ValType> void lexical_cast_test_impl()
  40. {
  41. if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
  42. return;
  43. if((std::numeric_limits<ValType>::has_quiet_NaN == 0) || (std::numeric_limits<ValType>::quiet_NaN() == 0))
  44. return;
  45. std::locale old_locale;
  46. std::locale tmp_locale(old_locale,
  47. new nonfinite_num_put<CharType>(signed_zero));
  48. std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
  49. std::locale::global(new_locale);
  50. ValType a1 = static_cast<ValType>(0);
  51. ValType a2 = static_cast<ValType>(13);
  52. ValType a3 = std::numeric_limits<ValType>::infinity();
  53. ValType a4 = std::numeric_limits<ValType>::quiet_NaN();
  54. ValType a5 = std::numeric_limits<ValType>::signaling_NaN();
  55. ValType a6 = (changesign)(static_cast<ValType>(0));
  56. ValType a7 = static_cast<ValType>(-57);
  57. ValType a8 = -std::numeric_limits<ValType>::infinity();
  58. ValType a9 = (changesign)(std::numeric_limits<ValType>::quiet_NaN()); // -NaN
  59. ValType a10 = (changesign)(std::numeric_limits<ValType>::signaling_NaN()); // -NaN
  60. std::basic_string<CharType> s1 = S_("0");
  61. std::basic_string<CharType> s2 = S_("13");
  62. std::basic_string<CharType> s3 = S_("inf");
  63. std::basic_string<CharType> s4 = S_("nan");
  64. std::basic_string<CharType> s5 = S_("nan");
  65. std::basic_string<CharType> s6 = S_("-0");
  66. std::basic_string<CharType> s7 = S_("-57");
  67. std::basic_string<CharType> s8 = S_("-inf");
  68. std::basic_string<CharType> s9 = S_("-nan");
  69. std::basic_string<CharType> s10 = S_("-nan");
  70. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a1) == s1);
  71. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a2) == s2);
  72. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a3) == s3);
  73. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a4) == s4);
  74. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a5) == s5);
  75. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a6) == s6);
  76. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a7) == s7);
  77. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a8) == s8);
  78. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a9) == s9);
  79. BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a10) == s10);
  80. BOOST_CHECK(lexical_cast<ValType>(s1) == a1);
  81. BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s1)));
  82. BOOST_CHECK(lexical_cast<ValType>(s2) == a2);
  83. BOOST_CHECK(lexical_cast<ValType>(s3) == a3);
  84. BOOST_CHECK((isnan)(lexical_cast<ValType>(s4)));
  85. BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s4)));
  86. BOOST_CHECK((isnan)(lexical_cast<ValType>(s5)));
  87. BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s5)));
  88. BOOST_CHECK(lexical_cast<ValType>(a6) == a6);
  89. BOOST_CHECK((signbit)(lexical_cast<ValType>(s6)));
  90. BOOST_CHECK(lexical_cast<ValType>(s7) == a7);
  91. BOOST_CHECK(lexical_cast<ValType>(s8) == a8);
  92. BOOST_CHECK((isnan)(lexical_cast<ValType>(s9)));
  93. BOOST_CHECK((signbit)(lexical_cast<ValType>(s9)));
  94. BOOST_CHECK((isnan)(lexical_cast<ValType>(s10)));
  95. BOOST_CHECK((signbit)(lexical_cast<ValType>(s10)));
  96. std::locale::global(old_locale);
  97. }
  98. //------------------------------------------------------------------------------
  99. } // anonymous namespace