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