1 // Unit test for boost::lexical_cast.
3 // See http://www.boost.org for most recent version, including documentation.
5 // Copyright Antony Polukhin, 2012.
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).
11 #include <boost/config.hpp>
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)
19 #include <boost/lexical_cast.hpp>
20 #include <boost/test/unit_test.hpp>
21 #include <boost/range/iterator_range.hpp>
23 #ifndef BOOST_NO_EXCEPTIONS
24 #error "This test must be compiled with -DBOOST_NO_EXCEPTIONS"
27 bool g_was_exception = false;
31 void throw_exception(std::exception const & ) {
32 g_was_exception = true;
37 using namespace boost;
43 Escape(const std::string& s)
50 inline std::ostream& operator<< (std::ostream& o, const Escape& rhs)
55 inline std::istream& operator>> (std::istream& i, Escape& rhs)
60 void test_exceptions_off()
64 g_was_exception = false;
65 lexical_cast<char>(v);
66 BOOST_CHECK(g_was_exception);
68 g_was_exception = false;
69 lexical_cast<unsigned char>(v);
70 BOOST_CHECK(g_was_exception);
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);
76 v = lexical_cast<Escape>(0.0);
77 BOOST_CHECK_EQUAL(lexical_cast<double>(v), 0.0);
79 BOOST_CHECK_EQUAL(lexical_cast<short>(100), 100);
80 BOOST_CHECK_EQUAL(lexical_cast<float>(0.0), 0.0);
82 g_was_exception = false;
83 lexical_cast<short>(700000);
84 BOOST_CHECK(g_was_exception);
87 unit_test::test_suite *init_unit_test_suite(int, char *[])
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));