1 | // Unit test for boost::lexical_cast. |
---|---|
2 | // |
3 | // See http://www.boost.org for most recent version, including documentation. |
4 | // |
5 | // Copyright Alexander Nasonov, 2007. |
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 | // This tests now must pass on vc8, because lexical_cast |
12 | // implementation has changed and it does not use stringstream for casts |
13 | // to integral types |
14 | |
15 | #include <boost/lexical_cast.hpp> |
16 | #include <boost/cstdint.hpp> |
17 | |
18 | #include <boost/core/lightweight_test.hpp> |
19 | |
20 | #include <string> |
21 | |
22 | #ifdef BOOST_MSVC |
23 | # pragma warning(disable: 4127) // conditional expression is constant |
24 | #endif |
25 | |
26 | using namespace boost; |
27 | |
28 | // See also test_conversion_from_string_to_integral(CharT) |
29 | // in libs/conversion/lexical_cast_test.cpp |
30 | template<class T, class CharT> |
31 | void test_too_long_number(CharT zero) |
32 | { |
33 | typedef std::numeric_limits<T> limits; |
34 | |
35 | std::basic_string<CharT> s; |
36 | |
37 | std::basic_ostringstream<CharT> o; |
38 | o << (limits::max)() << zero; |
39 | s = o.str(); |
40 | BOOST_TEST_THROWS(lexical_cast<T>(s), bad_lexical_cast); |
41 | s[s.size()-1] += static_cast<CharT>(9); // '0' -> '9' |
42 | BOOST_TEST_THROWS(lexical_cast<T>(s), bad_lexical_cast); |
43 | |
44 | if (limits::is_signed) |
45 | { |
46 | std::basic_ostringstream<CharT> o2; |
47 | o2 << (limits::min)() << zero; |
48 | s = o2.str(); |
49 | BOOST_TEST_THROWS(lexical_cast<T>(s), bad_lexical_cast); |
50 | s[s.size()-1] += static_cast<CharT>(9); // '0' -> '9' |
51 | BOOST_TEST_THROWS(lexical_cast<T>(s), bad_lexical_cast); |
52 | } |
53 | } |
54 | |
55 | int main() |
56 | { |
57 | test_too_long_number<boost::intmax_t>(zero: '0'); |
58 | test_too_long_number<boost::uintmax_t>(zero: '0'); |
59 | #if !defined(BOOST_LCAST_NO_WCHAR_T) |
60 | test_too_long_number<boost::intmax_t>(zero: L'0'); |
61 | test_too_long_number<boost::uintmax_t>(zero: L'0'); |
62 | #endif |
63 | |
64 | return boost::report_errors(); |
65 | } |
66 |