]>
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, 2012-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 | ||
20effc67 TL |
23 | #include "escape_struct.hpp" |
24 | ||
7c673cae FG |
25 | using namespace boost; |
26 | ||
27 | // Testing compilation and some basic usage with BOOST_NO_STD_LOCALE | |
28 | // Tests are mainly copyied from lexical_cast_empty_input_test.cpp (something | |
29 | // new added to test_empty_3) | |
30 | ||
31 | #ifndef BOOST_NO_STD_LOCALE | |
32 | #error "This test must be compiled with -DBOOST_NO_STD_LOCALE" | |
33 | #endif | |
34 | ||
35 | ||
36 | template <class T> | |
37 | void do_test_on_empty_input(T& v) | |
38 | { | |
39 | BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast); | |
40 | BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast); | |
41 | BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast); | |
42 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
43 | BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast); | |
44 | #endif | |
45 | BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast); | |
46 | BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast); | |
47 | #if defined(BOOST_HAS_LONG_LONG) | |
48 | BOOST_CHECK_THROW(lexical_cast<boost::ulong_long_type>(v), bad_lexical_cast); | |
49 | BOOST_CHECK_THROW(lexical_cast<boost::long_long_type>(v), bad_lexical_cast); | |
50 | #elif defined(BOOST_HAS_MS_INT64) | |
51 | BOOST_CHECK_THROW(lexical_cast<unsigned __int64>(v), bad_lexical_cast); | |
52 | BOOST_CHECK_THROW(lexical_cast<__int64>(v), bad_lexical_cast); | |
53 | #endif | |
54 | } | |
55 | ||
56 | void test_empty_1() | |
57 | { | |
58 | boost::iterator_range<char*> v; | |
59 | do_test_on_empty_input(v); | |
60 | BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string()); | |
61 | BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast); | |
62 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast); | |
63 | BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast); | |
64 | ||
65 | boost::iterator_range<const char*> cv; | |
66 | do_test_on_empty_input(cv); | |
67 | BOOST_CHECK_EQUAL(lexical_cast<std::string>(cv), std::string()); | |
68 | BOOST_CHECK_THROW(lexical_cast<char>(cv), bad_lexical_cast); | |
69 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(cv), bad_lexical_cast); | |
70 | BOOST_CHECK_THROW(lexical_cast<signed char>(cv), bad_lexical_cast); | |
71 | ||
72 | const boost::iterator_range<const char*> ccv; | |
73 | do_test_on_empty_input(ccv); | |
74 | BOOST_CHECK_EQUAL(lexical_cast<std::string>(ccv), std::string()); | |
75 | BOOST_CHECK_THROW(lexical_cast<char>(ccv), bad_lexical_cast); | |
76 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(ccv), bad_lexical_cast); | |
77 | BOOST_CHECK_THROW(lexical_cast<signed char>(ccv), bad_lexical_cast); | |
78 | } | |
79 | ||
80 | void test_empty_2() | |
81 | { | |
82 | std::string v; | |
83 | do_test_on_empty_input(v); | |
84 | BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast); | |
85 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast); | |
86 | BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast); | |
87 | } | |
88 | ||
7c673cae FG |
89 | void test_empty_3() |
90 | { | |
20effc67 | 91 | EscapeStruct v(""); |
7c673cae FG |
92 | do_test_on_empty_input(v); |
93 | ||
94 | BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast); | |
95 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast); | |
96 | BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast); | |
97 | ||
20effc67 | 98 | v = lexical_cast<EscapeStruct>(100); |
7c673cae FG |
99 | BOOST_CHECK_EQUAL(lexical_cast<int>(v), 100); |
100 | BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(v), 100u); | |
101 | ||
20effc67 | 102 | v = lexical_cast<EscapeStruct>(0.0); |
7c673cae FG |
103 | BOOST_CHECK_EQUAL(lexical_cast<double>(v), 0.0); |
104 | } | |
105 | ||
106 | namespace std { | |
107 | inline std::ostream & operator<<(std::ostream & out, const std::vector<long> & v) | |
108 | { | |
109 | std::ostream_iterator<long> it(out); | |
110 | std::copy(v.begin(), v.end(), it); | |
f67539c2 | 111 | BOOST_CHECK(out); |
7c673cae FG |
112 | return out; |
113 | } | |
114 | } | |
115 | ||
116 | void test_empty_4() | |
117 | { | |
118 | std::vector<long> v; | |
119 | do_test_on_empty_input(v); | |
120 | BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast); | |
121 | BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast); | |
122 | BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast); | |
123 | } | |
124 | ||
125 | ||
126 | struct my_string { | |
127 | friend std::ostream &operator<<(std::ostream& sout, my_string const&/* st*/) { | |
128 | return sout << ""; | |
129 | } | |
130 | }; | |
131 | ||
132 | void test_empty_5() | |
133 | { | |
134 | my_string st; | |
135 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(st), std::string());; | |
136 | } | |
137 | ||
138 | unit_test::test_suite *init_unit_test_suite(int, char *[]) | |
139 | { | |
140 | unit_test::test_suite *suite = | |
141 | BOOST_TEST_SUITE("lexical_cast. Testing with BOOST_NO_STD_LOCALE"); | |
142 | suite->add(BOOST_TEST_CASE(&test_empty_1)); | |
143 | suite->add(BOOST_TEST_CASE(&test_empty_2)); | |
144 | suite->add(BOOST_TEST_CASE(&test_empty_3)); | |
145 | suite->add(BOOST_TEST_CASE(&test_empty_4)); | |
146 | suite->add(BOOST_TEST_CASE(&test_empty_5)); | |
147 | ||
148 | return suite; | |
149 | } | |
150 |