• 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 Antony Polukhin, 2011-2020.
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 #include <boost/config.hpp>
12 
13 #if defined(__INTEL_COMPILER)
14 #pragma warning(disable: 193 383 488 981 1418 1419)
15 #elif defined(BOOST_MSVC)
16 #pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
17 #endif
18 
19 #include <boost/lexical_cast.hpp>
20 #include <boost/test/unit_test.hpp>
21 #include <boost/range/iterator_range.hpp>
22 
23 #include "escape_struct.hpp"
24 
25 using namespace boost;
26 
27 #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
28 #define BOOST_LCAST_NO_WCHAR_T
29 #endif
30 
31 template <class T>
do_test_on_empty_input(T & v)32 void do_test_on_empty_input(T& v)
33 {
34     BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast);
35     BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast);
36     BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast);
37 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
38     BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast);
39 #endif
40     BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast);
41     BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast);
42 #if defined(BOOST_HAS_LONG_LONG)
43     BOOST_CHECK_THROW(lexical_cast<boost::ulong_long_type>(v), bad_lexical_cast);
44     BOOST_CHECK_THROW(lexical_cast<boost::long_long_type>(v), bad_lexical_cast);
45 #elif defined(BOOST_HAS_MS_INT64)
46     BOOST_CHECK_THROW(lexical_cast<unsigned __int64>(v), bad_lexical_cast);
47     BOOST_CHECK_THROW(lexical_cast<__int64>(v), bad_lexical_cast);
48 #endif
49 }
50 
test_empty_iterator_range()51 void test_empty_iterator_range()
52 {
53 
54     boost::iterator_range<char*> v;
55     do_test_on_empty_input(v);
56     BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string());
57     BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
58     BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
59     BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
60 
61     boost::iterator_range<const char*> cv;
62     do_test_on_empty_input(cv);
63     BOOST_CHECK_EQUAL(lexical_cast<std::string>(cv), std::string());
64     BOOST_CHECK_THROW(lexical_cast<char>(cv), bad_lexical_cast);
65     BOOST_CHECK_THROW(lexical_cast<unsigned char>(cv), bad_lexical_cast);
66     BOOST_CHECK_THROW(lexical_cast<signed char>(cv), bad_lexical_cast);
67 
68     const boost::iterator_range<const char*> ccv;
69     do_test_on_empty_input(ccv);
70     BOOST_CHECK_EQUAL(lexical_cast<std::string>(ccv), std::string());
71     BOOST_CHECK_THROW(lexical_cast<char>(ccv), bad_lexical_cast);
72     BOOST_CHECK_THROW(lexical_cast<unsigned char>(ccv), bad_lexical_cast);
73     BOOST_CHECK_THROW(lexical_cast<signed char>(ccv), bad_lexical_cast);
74 }
75 
test_empty_string()76 void test_empty_string()
77 {
78     std::string v;
79     do_test_on_empty_input(v);
80     BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
81     BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
82     BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
83 
84 #ifndef BOOST_LCAST_NO_WCHAR_T
85     std::wstring vw;
86     do_test_on_empty_input(vw);
87     BOOST_CHECK_THROW(lexical_cast<wchar_t>(vw), bad_lexical_cast);
88 #endif
89 
90 // Currently, no compiler and STL library fully support char16_t and char32_t
91 //#ifndef BOOST_NO_CXX11_CHAR16_T
92 //    std::basic_string<char16_t> v16w;
93 //    do_test_on_empty_input(v16w);
94 //    BOOST_CHECK_THROW(lexical_cast<char16_t>(v16w), bad_lexical_cast);
95 //#endif
96 //#ifndef BOOST_NO_CXX11_CHAR32_T
97 //    std::basic_string<char32_t> v32w;
98 //    do_test_on_empty_input(v32w);
99 //    BOOST_CHECK_THROW(lexical_cast<char32_t>(v32w), bad_lexical_cast);
100 //#endif
101 }
102 
test_empty_user_class()103 void test_empty_user_class()
104 {
105     EscapeStruct v("");
106     do_test_on_empty_input(v);
107     BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
108     BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
109     BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
110 }
111 
112 namespace std {
operator <<(std::ostream & out,const std::vector<long> & v)113 inline std::ostream & operator<<(std::ostream & out, const std::vector<long> & v)
114 {
115     std::ostream_iterator<long> it(out);
116     std::copy(v.begin(), v.end(), it);
117     BOOST_CHECK(out);
118     return out;
119 }
120 }
121 
test_empty_vector()122 void test_empty_vector()
123 {
124     std::vector<long> v;
125     do_test_on_empty_input(v);
126     BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
127     BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
128     BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
129 }
130 
131 
132 struct my_string {
operator <<(std::ostream & sout,my_string const &)133     friend std::ostream &operator<<(std::ostream& sout, my_string const&/* st*/) {
134             return sout << "";
135     }
136 };
137 
test_empty_zero_terminated_string()138 void test_empty_zero_terminated_string()
139 {
140     my_string st;
141     BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(st), std::string());;
142 }
143 
init_unit_test_suite(int,char * [])144 unit_test::test_suite *init_unit_test_suite(int, char *[])
145 {
146     unit_test::test_suite *suite =
147         BOOST_TEST_SUITE("lexical_cast. Empty input unit test");
148     suite->add(BOOST_TEST_CASE(&test_empty_iterator_range));
149     suite->add(BOOST_TEST_CASE(&test_empty_string));
150     suite->add(BOOST_TEST_CASE(&test_empty_user_class));
151     suite->add(BOOST_TEST_CASE(&test_empty_vector));
152     suite->add(BOOST_TEST_CASE(&test_empty_zero_terminated_string));
153 
154     return suite;
155 }
156