Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / lexical_cast / test / lexical_cast_no_exceptions_test.cpp
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.
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 #ifndef BOOST_NO_EXCEPTIONS
24 #error "This test must be compiled with -DBOOST_NO_EXCEPTIONS"
25 #endif
26
27 bool g_was_exception = false;
28
29 namespace boost {
30
31 void throw_exception(std::exception const & ) {
32     g_was_exception = true;
33 }
34
35 }
36
37 using namespace boost;
38
39
40 struct Escape
41 {
42     Escape(){}
43     Escape(const std::string& s)
44         : str_(s)
45     {}
46
47     std::string str_;
48 };
49
50 inline std::ostream& operator<< (std::ostream& o, const Escape& rhs)
51 {
52     return o << rhs.str_;
53 }
54
55 inline std::istream& operator>> (std::istream& i, Escape& rhs)
56 {
57     return i >> rhs.str_;
58 }
59
60 void test_exceptions_off()
61 {
62     Escape v("");
63
64     g_was_exception = false;
65     lexical_cast<char>(v);
66     BOOST_CHECK(g_was_exception);
67
68     g_was_exception = false;
69     lexical_cast<unsigned char>(v);
70     BOOST_CHECK(g_was_exception);
71
72     v = lexical_cast<Escape>(100);
73     BOOST_CHECK_EQUAL(lexical_cast<int>(v), 100);
74     BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(v), 100u);
75
76     v = lexical_cast<Escape>(0.0);
77     BOOST_CHECK_EQUAL(lexical_cast<double>(v), 0.0);
78
79     BOOST_CHECK_EQUAL(lexical_cast<short>(100), 100);
80     BOOST_CHECK_EQUAL(lexical_cast<float>(0.0), 0.0);
81
82     g_was_exception = false;
83     lexical_cast<short>(700000);
84     BOOST_CHECK(g_was_exception);
85 }
86
87 unit_test::test_suite *init_unit_test_suite(int, char *[])
88 {
89     unit_test::test_suite *suite =
90         BOOST_TEST_SUITE("lexical_cast. Testing with BOOST_NO_EXCEPTIONS");
91     suite->add(BOOST_TEST_CASE(&test_exceptions_off));
92
93     return suite;
94 }
95