• 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/math/tools/config.hpp>  // BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
24 #include <boost/test/unit_test.hpp>
25 
26 using namespace boost;
27 
28 void test_round_conversion_float();
29 void test_round_conversion_double();
30 void test_round_conversion_long_double();
31 
init_unit_test_suite(int,char * [])32 unit_test::test_suite *init_unit_test_suite(int, char *[])
33 {
34     unit_test::test_suite *suite =
35         BOOST_TEST_SUITE("lexical_cast unit test");
36     suite->add(BOOST_TEST_CASE(&test_round_conversion_float));
37     suite->add(BOOST_TEST_CASE(&test_round_conversion_double));
38     suite->add(BOOST_TEST_CASE(&test_round_conversion_long_double));
39 
40     return suite;
41 }
42 
43 template<class T>
test_round_conversion()44 void test_round_conversion()
45 {
46     T epsilon = std::numeric_limits<T>::epsilon();
47     std::string const epsilon_s = boost::lexical_cast<std::string>(epsilon);
48     BOOST_CHECK(epsilon == lexical_cast<T>(epsilon_s));
49 
50     T max_ = (std::numeric_limits<T>::max)();
51     std::string const max_s = boost::lexical_cast<std::string>(max_);
52     BOOST_CHECK(max_ == lexical_cast<T>(max_s));
53 
54     T min_ = (std::numeric_limits<T>::min)();
55     std::string const min_s = boost::lexical_cast<std::string>(min_);
56     BOOST_CHECK(min_ == lexical_cast<T>(min_s));
57 
58     T max_div137 = max_ / 137;
59     std::string max_div137_s = boost::lexical_cast<std::string>(max_div137);
60     BOOST_CHECK(max_div137 == lexical_cast<T>(max_div137_s));
61 
62     T epsilon_mult137 = epsilon * 137;
63     std::string epsilon_mult137_s(lexical_cast<std::string>(epsilon_mult137));
64     BOOST_CHECK(epsilon_mult137 == lexical_cast<T>(epsilon_mult137_s));
65 
66 }
67 
68 // See bug http://tinyurl.com/vhpvo
69 template<class T>
test_msvc_magic_values()70 void test_msvc_magic_values()
71 {
72     T magic_msvc = 0.00010000433948393407;
73     std::string magic_msvc_s = boost::lexical_cast<std::string>(magic_msvc);
74     BOOST_CHECK(magic_msvc == lexical_cast<T>(magic_msvc_s));
75 }
76 
test_round_conversion_float()77 void test_round_conversion_float()
78 {
79     test_round_conversion<float>();
80 }
81 
test_round_conversion_double()82 void test_round_conversion_double()
83 {
84     test_round_conversion<double>();
85     test_msvc_magic_values<double>();
86 }
87 
test_round_conversion_long_double()88 void test_round_conversion_long_double()
89 {
90 // We do not run tests on compilers and Standard Libraries with poor support of long double
91 #if !defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS) && !defined(__MINGW64__)
92     test_round_conversion<long double>();
93     test_msvc_magic_values<long double>();
94 #endif
95     BOOST_CHECK(true);
96 }
97 
98