16 #define GETTEXT_DOMAIN "wesnoth-test"
21 #include <boost/test/unit_test.hpp>
29 #pragma warning(disable:4702)
32 #define LEXICAL_CAST_DEBUG
49 char,
signed char,
unsigned char,
50 short, int, long,
long long,
51 unsigned short,
unsigned int,
unsigned long,
unsigned long long
66 <<
"Expected " << result <<
'\n';
73 template<
typename Test,
typename... Types>
74 constexpr
bool contains_type(std::tuple<Types...>)
76 return (std::is_same_v<Test, Types> || ...);
81 #define TEST_CASE(type_send, initializer) \
83 type_send val = initializer value; \
85 BOOST_CHECK_EXCEPTION( \
86 lexical_cast<std::string>(val), const char*, validate); \
94 result =
"specialized - To std::string - From integral (pointer)";
123 result =
"specialized - To signed - From (const) char*";
125 const char* value =
"test";
126 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
128 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
129 const_cast<char*
>(value)),
const char*,
validate);
131 result =
"specialized - To signed - From std::string";
133 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
134 std::string(value)),
const char*,
validate);
139 result =
"specialized - To long long - From (const) char*";
141 const char* value =
"test";
142 BOOST_CHECK_EXCEPTION(lexical_cast<long long>(
144 BOOST_CHECK_EXCEPTION(lexical_cast<long long>(
145 const_cast<char*
>(value)),
const char*,
validate);
147 result =
"specialized - To long long - From std::string";
149 BOOST_CHECK_EXCEPTION(lexical_cast<long long>(
150 std::string(value)),
const char*,
validate);
162 result =
"specialized - To unsigned - From (const) char*";
164 const char* value =
"test";
165 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
167 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
168 const_cast<char*
>(value)),
const char*,
validate);
170 result =
"specialized - To unsigned - From std::string";
172 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
173 std::string(value)),
const char*,
validate);
179 result =
"specialized - To unsigned long long - From (const) char*";
181 const char* value =
"test";
182 BOOST_CHECK_EXCEPTION(lexical_cast<unsigned long long>(
184 BOOST_CHECK_EXCEPTION(lexical_cast<unsigned long long>(
185 const_cast<char*
>(value)),
const char*,
validate);
187 result =
"specialized - To unsigned long long - From std::string";
189 BOOST_CHECK_EXCEPTION(lexical_cast<unsigned long long>(
190 std::string(value)),
const char*,
validate);
195 result =
"specialized - To bool - From (const) char*";
197 const char* value =
"test";
198 BOOST_CHECK_EXCEPTION(lexical_cast<bool>(
200 BOOST_CHECK_EXCEPTION(lexical_cast<bool>(
201 const_cast<char*
>(value)),
const char*,
validate);
203 result =
"specialized - To bool - From std::string";
205 BOOST_CHECK_EXCEPTION(lexical_cast<bool>(
206 std::string(value)),
const char*,
validate);
217 result =
"specialized - To floating point - From (const) char*";
219 const char* value =
"test";
220 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
222 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
223 const_cast<char*
>(value)),
const char*,
validate);
225 result =
"specialized - To floating point - From std::string";
227 BOOST_CHECK_EXCEPTION(lexical_cast<T>(
228 std::string(value)),
const char*,
validate);
235 BOOST_CHECK_EQUAL(lexical_cast<std::string>(
true),
"1");
236 BOOST_CHECK_EQUAL(lexical_cast<std::string>(
false),
"0");
238 BOOST_CHECK_EQUAL(lexical_cast<std::string>(1),
"1");
239 BOOST_CHECK_EQUAL(lexical_cast<std::string>(1u),
"1");
241 BOOST_CHECK_EQUAL(lexical_cast<std::string>(1.2f),
"1.2");
242 BOOST_CHECK_EQUAL(lexical_cast<std::string>(1.2),
"1.2");
244 BOOST_CHECK_EQUAL(lexical_cast<int>(
"1"), 1);
245 BOOST_CHECK_EQUAL(lexical_cast<int>(
"-1"), -1);
246 BOOST_CHECK_EQUAL(lexical_cast<unsigned>(
"1"), 1);
247 BOOST_CHECK_EQUAL(lexical_cast<double>(
"1.2"), 1.2);
250 std::string a =
"01234567890123456789";
251 BOOST_CHECK_EQUAL(lexical_cast<long long>(a), 1234567890123456789ll);
253 BOOST_CHECK_EQUAL(lexical_cast<double>(a), 1.23456789012345678e18);
254 BOOST_CHECK_EQUAL(lexical_cast_default<long long>(a, 0ll), 1234567890123456789ll);
255 BOOST_CHECK_EQUAL(lexical_cast_default<int>(a, 0), 0);
256 BOOST_CHECK_EQUAL(lexical_cast_default<double>(a, 0.0), 1.23456789012345678e18);
258 std::string
b =
"99999999999999999999";
261 BOOST_CHECK_EQUAL(lexical_cast<double>(
b), 1e20);
262 BOOST_CHECK_EQUAL(lexical_cast_default<long long>(
b, 0ll), 0ll);
263 BOOST_CHECK_EQUAL(lexical_cast_default<int>(
b, 0), 0);
264 BOOST_CHECK_EQUAL(lexical_cast_default<double>(
b, 0.0), 1e20);
static void validate(boost::any &v, const std::vector< std::string > &values, two_strings *, int)
New lexcical_cast header.
Standard logging facilities (interface).
std::tuple< signed char, short, int, long > test_lexical_cast_signed_types
std::tuple< unsigned char, unsigned short, unsigned int, unsigned long > test_lexical_cast_unsigned_types
std::tuple< float, double, long double > test_nomatch_types
BOOST_AUTO_TEST_CASE(test_lexical_cast_long_long)
std::tuple< bool, char, signed char, unsigned char, short, int, long, long long, unsigned short, unsigned int, unsigned long, unsigned long long > test_match_types
decltype(std::tuple_cat(test_nomatch_types{}, test_match_types{})) test_types
std::tuple< float, double, long double > test_lexical_cast_floating_point_types
BOOST_AUTO_TEST_CASE_TEMPLATE(test_lexical_cast_throw, T, test_types)
Thrown when a lexical_cast fails.
#define TEST_CASE(type_send, initializer)
BOOST_AUTO_TEST_CASE(test_lexical_cast_result)