• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 #include <boost/config.hpp>
15 
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)
20 #endif
21 
22 #include <boost/lexical_cast.hpp>
23 #include <boost/test/unit_test.hpp>
24 
25 using namespace boost;
26 
27 void test_round_conversion_float();
28 void test_round_conversion_double();
29 void test_round_conversion_long_double();
30 
init_unit_test_suite(int,char * [])31 unit_test::test_suite *init_unit_test_suite(int, char *[])
32 {
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));
38 
39     return suite;
40 }
41 
42 template<class T>
test_round_conversion()43 void test_round_conversion()
44 {
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));
48 
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));
52 
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));
56 
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));
60 
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));
64 
65 }
66 
67 // See bug http://tinyurl.com/vhpvo
68 template<class T>
test_msvc_magic_values()69 void test_msvc_magic_values()
70 {
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));
74 }
75 
test_round_conversion_float()76 void test_round_conversion_float()
77 {
78     test_round_conversion<float>();
79 }
80 
test_round_conversion_double()81 void test_round_conversion_double()
82 {
83     test_round_conversion<double>();
84     test_msvc_magic_values<double>();
85 }
86 
test_round_conversion_long_double()87 void test_round_conversion_long_double()
88 {
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>();
93 #endif
94     BOOST_CHECK(true);
95 }
96 
97