lexical_cast_loopback_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Unit test for boost::lexical_cast.
  2. //
  3. // See http://www.boost.org for most recent version, including documentation.
  4. //
  5. // Copyright Alexander Nasonov, 2006.
  6. //
  7. // Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
  10. //
  11. // Test round-tripping conversion FPT -> string -> FPT,
  12. // where FPT is Floating Point Type.
  13. #include <boost/config.hpp>
  14. #if defined(__INTEL_COMPILER)
  15. #pragma warning(disable: 193 383 488 981 1418 1419)
  16. #elif defined(BOOST_MSVC)
  17. #pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
  18. #endif
  19. #include <boost/lexical_cast.hpp>
  20. #include <boost/test/unit_test.hpp>
  21. using namespace boost;
  22. void test_round_conversion_float();
  23. void test_round_conversion_double();
  24. void test_round_conversion_long_double();
  25. unit_test::test_suite *init_unit_test_suite(int, char *[])
  26. {
  27. unit_test::test_suite *suite =
  28. BOOST_TEST_SUITE("lexical_cast unit test");
  29. suite->add(BOOST_TEST_CASE(&test_round_conversion_float));
  30. suite->add(BOOST_TEST_CASE(&test_round_conversion_double));
  31. suite->add(BOOST_TEST_CASE(&test_round_conversion_long_double));
  32. return suite;
  33. }
  34. template<class T>
  35. void test_round_conversion()
  36. {
  37. T epsilon = std::numeric_limits<T>::epsilon();
  38. std::string const epsilon_s = boost::lexical_cast<std::string>(epsilon);
  39. BOOST_CHECK(epsilon == lexical_cast<T>(epsilon_s));
  40. T max_ = (std::numeric_limits<T>::max)();
  41. std::string const max_s = boost::lexical_cast<std::string>(max_);
  42. BOOST_CHECK(max_ == lexical_cast<T>(max_s));
  43. T min_ = (std::numeric_limits<T>::min)();
  44. std::string const min_s = boost::lexical_cast<std::string>(min_);
  45. BOOST_CHECK(min_ == lexical_cast<T>(min_s));
  46. T max_div137 = max_ / 137;
  47. std::string max_div137_s = boost::lexical_cast<std::string>(max_div137);
  48. BOOST_CHECK(max_div137 == lexical_cast<T>(max_div137_s));
  49. T epsilon_mult137 = epsilon * 137;
  50. std::string epsilon_mult137_s(lexical_cast<std::string>(epsilon_mult137));
  51. BOOST_CHECK(epsilon_mult137 == lexical_cast<T>(epsilon_mult137_s));
  52. }
  53. // See bug http://tinyurl.com/vhpvo
  54. template<class T>
  55. void test_msvc_magic_values()
  56. {
  57. T magic_msvc = 0.00010000433948393407;
  58. std::string magic_msvc_s = boost::lexical_cast<std::string>(magic_msvc);
  59. BOOST_CHECK(magic_msvc == lexical_cast<T>(magic_msvc_s));
  60. }
  61. void test_round_conversion_float()
  62. {
  63. test_round_conversion<float>();
  64. }
  65. void test_round_conversion_double()
  66. {
  67. test_round_conversion<double>();
  68. test_msvc_magic_values<double>();
  69. }
  70. void test_round_conversion_long_double()
  71. {
  72. // We do not run tests on compilers with bugs
  73. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  74. test_round_conversion<long double>();
  75. test_msvc_magic_values<long double>();
  76. #endif
  77. BOOST_CHECK(true);
  78. }