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 | // Copyright Antony Polukhin, 2023-2024. |
7 | // |
8 | // Distributed under the Boost |
9 | // Software License, Version 1.0. (See accompanying file |
10 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). |
11 | // |
12 | // Test round-tripping conversion FPT -> string -> FPT, |
13 | // where FPT is Floating Point Type. |
14 | |
15 | #include <boost/lexical_cast.hpp> |
16 | |
17 | #include <boost/core/lightweight_test.hpp> |
18 | |
19 | #if (defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__NetBSD__) \ |
20 | || (defined(__hppa) && !defined(__OpenBSD__)) || (defined(__NO_LONG_DOUBLE_MATH) && (DBL_MANT_DIG != LDBL_MANT_DIG))) \ |
21 | || defined(__MINGW64__) |
22 | # define BOOST_LEXICAL_CAST_NO_LONG_DOUBLE_MATH_FUNCTIONS |
23 | #endif |
24 | |
25 | template<class T> |
26 | void test_round_conversion() |
27 | { |
28 | T epsilon = std::numeric_limits<T>::epsilon(); |
29 | std::string const epsilon_s = boost::lexical_cast<std::string>(epsilon); |
30 | BOOST_TEST(epsilon == boost::lexical_cast<T>(epsilon_s)); |
31 | |
32 | T max_ = (std::numeric_limits<T>::max)(); |
33 | std::string const max_s = boost::lexical_cast<std::string>(max_); |
34 | BOOST_TEST(max_ == boost::lexical_cast<T>(max_s)); |
35 | |
36 | T min_ = (std::numeric_limits<T>::min)(); |
37 | std::string const min_s = boost::lexical_cast<std::string>(min_); |
38 | BOOST_TEST(min_ == boost::lexical_cast<T>(min_s)); |
39 | |
40 | T max_div137 = max_ / 137; |
41 | std::string max_div137_s = boost::lexical_cast<std::string>(max_div137); |
42 | BOOST_TEST(max_div137 == boost::lexical_cast<T>(max_div137_s)); |
43 | |
44 | T epsilon_mult137 = epsilon * 137; |
45 | std::string epsilon_mult137_s(boost::lexical_cast<std::string>(epsilon_mult137)); |
46 | BOOST_TEST(epsilon_mult137 == boost::lexical_cast<T>(epsilon_mult137_s)); |
47 | |
48 | } |
49 | |
50 | // See bug http://tinyurl.com/vhpvo |
51 | template<class T> |
52 | void test_msvc_magic_values() |
53 | { |
54 | T magic_msvc = 0.00010000433948393407; |
55 | std::string magic_msvc_s = boost::lexical_cast<std::string>(magic_msvc); |
56 | BOOST_TEST(magic_msvc == boost::lexical_cast<T>(magic_msvc_s)); |
57 | } |
58 | |
59 | void test_round_conversion_float() |
60 | { |
61 | test_round_conversion<float>(); |
62 | } |
63 | |
64 | void test_round_conversion_double() |
65 | { |
66 | test_round_conversion<double>(); |
67 | test_msvc_magic_values<double>(); |
68 | } |
69 | |
70 | void test_round_conversion_long_double() |
71 | { |
72 | // We do not run tests on compilers and Standard Libraries with poor support of long double |
73 | #if !defined(BOOST_LEXICAL_CAST_NO_LONG_DOUBLE_MATH_FUNCTIONS) |
74 | test_round_conversion<long double>(); |
75 | test_msvc_magic_values<long double>(); |
76 | #endif |
77 | BOOST_TEST(true); |
78 | } |
79 | |
80 | int main() |
81 | { |
82 | test_round_conversion_float(); |
83 | test_round_conversion_double(); |
84 | test_round_conversion_long_double(); |
85 | |
86 | return boost::report_errors(); |
87 | } |
88 | |
89 |