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, 2012-2024. |
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/core/lightweight_test.hpp> |
21 | #include <boost/range/iterator_range.hpp> |
22 | |
23 | #include <cstdlib> |
24 | |
25 | #include "escape_struct.hpp" |
26 | |
27 | #ifndef BOOST_NO_EXCEPTIONS |
28 | #error "This test must be compiled with -DBOOST_NO_EXCEPTIONS" |
29 | #endif |
30 | |
31 | namespace boost { |
32 | |
33 | BOOST_NORETURN void throw_exception(std::exception const & ) { |
34 | static int state = 0; |
35 | ++ state; |
36 | |
37 | EscapeStruct v(""); |
38 | switch(state) { |
39 | case 1: |
40 | lexical_cast<char>(arg: v); // should call boost::throw_exception |
41 | std::exit(status: 1); |
42 | case 2: |
43 | lexical_cast<unsigned char>(arg: v); // should call boost::throw_exception |
44 | std::exit(status: 2); |
45 | } |
46 | std::exit(status: boost::report_errors()); |
47 | } |
48 | |
49 | } |
50 | |
51 | void test_exceptions_off() { |
52 | using namespace boost; |
53 | EscapeStruct v(""); |
54 | |
55 | v = lexical_cast<EscapeStruct>(arg: 100); |
56 | BOOST_TEST_EQ(lexical_cast<int>(v), 100); |
57 | BOOST_TEST_EQ(lexical_cast<unsigned int>(v), 100u); |
58 | |
59 | v = lexical_cast<EscapeStruct>(arg: 0.0); |
60 | BOOST_TEST_EQ(lexical_cast<double>(v), 0.0); |
61 | |
62 | BOOST_TEST_EQ(lexical_cast<short>(100), 100); |
63 | BOOST_TEST_EQ(lexical_cast<float>(0.0), 0.0); |
64 | |
65 | lexical_cast<short>(arg: 700000); // should call boost::throw_exception |
66 | BOOST_TEST(false); |
67 | } |
68 | |
69 | int main() { |
70 | test_exceptions_off(); |
71 | |
72 | return boost::report_errors(); |
73 | } |
74 | |
75 |