]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Unit test for boost::lexical_cast. |
2 | // | |
3 | // See http://www.boost.org for most recent version, including documentation. | |
4 | // | |
f67539c2 | 5 | // Copyright Antony Polukhin, 2011-2020. |
7c673cae FG |
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 | using namespace boost; | |
24 | ||
25 | #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) | |
26 | #define BOOST_LCAST_NO_WCHAR_T | |
27 | #endif | |
28 | ||
29 | template <class T> | |
30 | void do_test_on_empty_input(T& v) | |
31 | { | |
32 | BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast); | |
33 | BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast); | |
34 | BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast); | |
35 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
36 | BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast); | |
37 | #endif | |
38 | BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast); | |
39 | BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast); | |
40 | #if defined(BOOST_HAS_LONG_LONG) | |
41 | BOOST_CHECK_THROW(lexical_cast<boost::ulong_long_type>(v), bad_lexical_cast); | |
42 | BOOST_CHECK_THROW(lexical_cast<boost::long_long_type>(v), bad_lexical_cast); | |
43 | #elif defined(BOOST_HAS_MS_INT64) | |
44 | BOOST_CHECK_THROW(lexical_cast<unsigned __int64>(v), bad_lexical_cast); | |
45 | BOOST_CHECK_THROW(lexical_cast<__int64>(v), bad_lexical_cast); | |
46 | #endif | |
47 | } | |
48 | ||
49 | void test_empty_iterator_range() | |
50 | { | |
51 | ||
52 | boost::iterator_range<char*> v; | |
53 | do_test_on_empty_input(v); | |
54 | BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string()); | |
55 | BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast); | |
56 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast); | |
57 | BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast); | |
58 | ||
59 | boost::iterator_range<const char*> cv; | |
60 | do_test_on_empty_input(cv); | |
61 | BOOST_CHECK_EQUAL(lexical_cast<std::string>(cv), std::string()); | |
62 | BOOST_CHECK_THROW(lexical_cast<char>(cv), bad_lexical_cast); | |
63 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(cv), bad_lexical_cast); | |
64 | BOOST_CHECK_THROW(lexical_cast<signed char>(cv), bad_lexical_cast); | |
65 | ||
66 | const boost::iterator_range<const char*> ccv; | |
67 | do_test_on_empty_input(ccv); | |
68 | BOOST_CHECK_EQUAL(lexical_cast<std::string>(ccv), std::string()); | |
69 | BOOST_CHECK_THROW(lexical_cast<char>(ccv), bad_lexical_cast); | |
70 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(ccv), bad_lexical_cast); | |
71 | BOOST_CHECK_THROW(lexical_cast<signed char>(ccv), bad_lexical_cast); | |
72 | } | |
73 | ||
74 | void test_empty_string() | |
75 | { | |
76 | std::string v; | |
77 | do_test_on_empty_input(v); | |
78 | BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast); | |
79 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast); | |
80 | BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast); | |
81 | ||
82 | #ifndef BOOST_LCAST_NO_WCHAR_T | |
83 | std::wstring vw; | |
84 | do_test_on_empty_input(vw); | |
85 | BOOST_CHECK_THROW(lexical_cast<wchar_t>(vw), bad_lexical_cast); | |
86 | #endif | |
87 | ||
88 | // Currently, no compiler and STL library fully support char16_t and char32_t | |
89 | //#ifndef BOOST_NO_CXX11_CHAR16_T | |
90 | // std::basic_string<char16_t> v16w; | |
91 | // do_test_on_empty_input(v16w); | |
92 | // BOOST_CHECK_THROW(lexical_cast<char16_t>(v16w), bad_lexical_cast); | |
93 | //#endif | |
94 | //#ifndef BOOST_NO_CXX11_CHAR32_T | |
95 | // std::basic_string<char32_t> v32w; | |
96 | // do_test_on_empty_input(v32w); | |
97 | // BOOST_CHECK_THROW(lexical_cast<char32_t>(v32w), bad_lexical_cast); | |
98 | //#endif | |
99 | } | |
100 | ||
101 | struct Escape | |
102 | { | |
103 | Escape(const std::string& s) | |
104 | : str_(s) | |
105 | {} | |
106 | ||
107 | std::string str_; | |
108 | }; | |
109 | ||
110 | inline std::ostream& operator<< (std::ostream& o, const Escape& rhs) | |
111 | { | |
112 | return o << rhs.str_; | |
113 | } | |
114 | ||
115 | void test_empty_user_class() | |
116 | { | |
117 | Escape v(""); | |
118 | do_test_on_empty_input(v); | |
119 | BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast); | |
120 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast); | |
121 | BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast); | |
122 | } | |
123 | ||
124 | namespace std { | |
125 | inline std::ostream & operator<<(std::ostream & out, const std::vector<long> & v) | |
126 | { | |
127 | std::ostream_iterator<long> it(out); | |
128 | std::copy(v.begin(), v.end(), it); | |
f67539c2 | 129 | BOOST_CHECK(out); |
7c673cae FG |
130 | return out; |
131 | } | |
132 | } | |
133 | ||
134 | void test_empty_vector() | |
135 | { | |
136 | std::vector<long> v; | |
137 | do_test_on_empty_input(v); | |
138 | BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast); | |
139 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast); | |
140 | BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast); | |
141 | } | |
142 | ||
143 | ||
144 | struct my_string { | |
145 | friend std::ostream &operator<<(std::ostream& sout, my_string const&/* st*/) { | |
146 | return sout << ""; | |
147 | } | |
148 | }; | |
149 | ||
150 | void test_empty_zero_terminated_string() | |
151 | { | |
152 | my_string st; | |
153 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(st), std::string());; | |
154 | } | |
155 | ||
156 | unit_test::test_suite *init_unit_test_suite(int, char *[]) | |
157 | { | |
158 | unit_test::test_suite *suite = | |
159 | BOOST_TEST_SUITE("lexical_cast. Empty input unit test"); | |
160 | suite->add(BOOST_TEST_CASE(&test_empty_iterator_range)); | |
161 | suite->add(BOOST_TEST_CASE(&test_empty_string)); | |
162 | suite->add(BOOST_TEST_CASE(&test_empty_user_class)); | |
163 | suite->add(BOOST_TEST_CASE(&test_empty_vector)); | |
164 | suite->add(BOOST_TEST_CASE(&test_empty_zero_terminated_string)); | |
165 | ||
166 | return suite; | |
167 | } |