Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_lexical_cast.cpp
1
2 // Copyright (c) 2006 Johan Rade
3
4 // Copyright (c) 2011 Paul A. Bristow incorporated Boost.Math
5
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt
8 // or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10 //#ifdef _MSC_VER
11 //#   pragma warning(disable : 4127 4511 4512 4701 4702)
12 //#endif
13
14 #define BOOST_TEST_MAIN
15
16 #include <limits>
17 #include <locale>
18 #include <string>
19 #include <boost/lexical_cast.hpp>
20 #include <boost/test/unit_test.hpp>
21
22 #include <boost/math/special_functions/nonfinite_num_facets.hpp>
23 #include <boost/math/special_functions/sign.hpp>
24 #include <boost/math/special_functions/fpclassify.hpp>
25 #include "almost_equal.ipp"
26 #include "s_.ipp"
27
28 namespace {
29
30 // the anonymous namespace resolves ambiguities on platforms
31 // with fpclassify etc functions at global scope
32
33 using boost::lexical_cast;
34
35 using namespace boost::math;
36 using boost::math::signbit;
37 using boost::math::changesign;
38 using boost::math::isnan;
39
40 //------------------------------------------------------------------------------
41
42 template<class CharType, class ValType> void lexical_cast_test_impl();
43
44 BOOST_AUTO_TEST_CASE(lexical_cast_test)
45 {
46     lexical_cast_test_impl<char, float>();
47     lexical_cast_test_impl<char, double>();
48     lexical_cast_test_impl<char, long double>();
49     lexical_cast_test_impl<wchar_t, float>();
50     lexical_cast_test_impl<wchar_t, double>();
51     lexical_cast_test_impl<wchar_t, long double>();
52 }
53
54 template<class CharType, class ValType> void lexical_cast_test_impl()
55 {
56     if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
57        return;
58     if((std::numeric_limits<ValType>::has_quiet_NaN == 0) || (std::numeric_limits<ValType>::quiet_NaN() == 0))
59        return;
60
61     std::locale old_locale;
62     std::locale tmp_locale(old_locale,
63         new nonfinite_num_put<CharType>(signed_zero));
64     std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
65     std::locale::global(new_locale);
66
67     ValType a1 = static_cast<ValType>(0);
68     ValType a2 = static_cast<ValType>(13);
69     ValType a3 = std::numeric_limits<ValType>::infinity();
70     ValType a4 = std::numeric_limits<ValType>::quiet_NaN();
71     ValType a5 = std::numeric_limits<ValType>::signaling_NaN();
72     ValType a6 = (changesign)(static_cast<ValType>(0));
73     ValType a7 = static_cast<ValType>(-57);
74     ValType a8 = -std::numeric_limits<ValType>::infinity();
75     ValType a9 = (changesign)(std::numeric_limits<ValType>::quiet_NaN()); // -NaN
76     ValType a10 = (changesign)(std::numeric_limits<ValType>::signaling_NaN()); // -NaN
77
78     std::basic_string<CharType> s1 = S_("0");
79     std::basic_string<CharType> s2 = S_("13");
80     std::basic_string<CharType> s3 = S_("inf");
81     std::basic_string<CharType> s4 = S_("nan");
82     std::basic_string<CharType> s5 = S_("nan");
83     std::basic_string<CharType> s6 = S_("-0");
84     std::basic_string<CharType> s7 = S_("-57");
85     std::basic_string<CharType> s8 = S_("-inf");
86     std::basic_string<CharType> s9 = S_("-nan");
87     std::basic_string<CharType> s10 = S_("-nan");
88
89     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a1) == s1);
90     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a2) == s2);
91     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a3) == s3);
92     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a4) == s4);
93     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a5) == s5);
94     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a6) == s6);
95     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a7) == s7);
96     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a8) == s8);
97     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a9) == s9);
98     BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a10) == s10);
99
100     BOOST_CHECK(lexical_cast<ValType>(s1) == a1);
101     BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s1)));
102     BOOST_CHECK(lexical_cast<ValType>(s2) == a2);
103     BOOST_CHECK(lexical_cast<ValType>(s3) == a3);
104     BOOST_CHECK((isnan)(lexical_cast<ValType>(s4)));
105     BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s4)));
106     BOOST_CHECK((isnan)(lexical_cast<ValType>(s5)));
107     BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s5)));
108     BOOST_CHECK(lexical_cast<ValType>(a6) == a6);
109     BOOST_CHECK((signbit)(lexical_cast<ValType>(s6)));
110     BOOST_CHECK(lexical_cast<ValType>(s7) == a7);
111     BOOST_CHECK(lexical_cast<ValType>(s8) == a8);
112     BOOST_CHECK((isnan)(lexical_cast<ValType>(s9)));
113     BOOST_CHECK((signbit)(lexical_cast<ValType>(s9)));
114     BOOST_CHECK((isnan)(lexical_cast<ValType>(s10)));
115     BOOST_CHECK((signbit)(lexical_cast<ValType>(s10)));
116
117     std::locale::global(old_locale);
118 }
119
120 //------------------------------------------------------------------------------
121
122 }   // anonymous namespace