1 // Unit test for boost::lexical_cast.
3 // See http://www.boost.org for most recent version, including documentation.
5 // Copyright Alexander Nasonov, 2006.
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).
11 // Test round-tripping conversion FPT -> string -> FPT,
12 // where FPT is Floating Point Type.
14 #include <boost/config.hpp>
16 #if defined(__INTEL_COMPILER)
17 #pragma warning(disable: 193 383 488 981 1418 1419)
18 #elif defined(BOOST_MSVC)
19 #pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
22 #include <boost/lexical_cast.hpp>
23 #include <boost/test/unit_test.hpp>
25 using namespace boost
;
27 void test_round_conversion_float();
28 void test_round_conversion_double();
29 void test_round_conversion_long_double();
31 unit_test::test_suite
*init_unit_test_suite(int, char *[])
33 unit_test::test_suite
*suite
=
34 BOOST_TEST_SUITE("lexical_cast unit test");
35 suite
->add(BOOST_TEST_CASE(&test_round_conversion_float
));
36 suite
->add(BOOST_TEST_CASE(&test_round_conversion_double
));
37 suite
->add(BOOST_TEST_CASE(&test_round_conversion_long_double
));
43 void test_round_conversion()
45 T epsilon
= std::numeric_limits
<T
>::epsilon();
46 std::string
const epsilon_s
= boost::lexical_cast
<std::string
>(epsilon
);
47 BOOST_CHECK(epsilon
== lexical_cast
<T
>(epsilon_s
));
49 T max_
= (std::numeric_limits
<T
>::max
)();
50 std::string
const max_s
= boost::lexical_cast
<std::string
>(max_
);
51 BOOST_CHECK(max_
== lexical_cast
<T
>(max_s
));
53 T min_
= (std::numeric_limits
<T
>::min
)();
54 std::string
const min_s
= boost::lexical_cast
<std::string
>(min_
);
55 BOOST_CHECK(min_
== lexical_cast
<T
>(min_s
));
57 T max_div137
= max_
/ 137;
58 std::string max_div137_s
= boost::lexical_cast
<std::string
>(max_div137
);
59 BOOST_CHECK(max_div137
== lexical_cast
<T
>(max_div137_s
));
61 T epsilon_mult137
= epsilon
* 137;
62 std::string
epsilon_mult137_s(lexical_cast
<std::string
>(epsilon_mult137
));
63 BOOST_CHECK(epsilon_mult137
== lexical_cast
<T
>(epsilon_mult137_s
));
67 // See bug http://tinyurl.com/vhpvo
69 void test_msvc_magic_values()
71 T magic_msvc
= 0.00010000433948393407;
72 std::string magic_msvc_s
= boost::lexical_cast
<std::string
>(magic_msvc
);
73 BOOST_CHECK(magic_msvc
== lexical_cast
<T
>(magic_msvc_s
));
76 void test_round_conversion_float()
78 test_round_conversion
<float>();
81 void test_round_conversion_double()
83 test_round_conversion
<double>();
84 test_msvc_magic_values
<double>();
87 void test_round_conversion_long_double()
89 // We do not run tests on compilers with bugs
90 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
91 test_round_conversion
<long double>();
92 test_msvc_magic_values
<long double>();