]>
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 | // | |
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/config.hpp> | |
16 | #include <boost/lexical_cast.hpp> | |
17 | #include <boost/cstdint.hpp> | |
18 | #include <boost/test/unit_test.hpp> | |
19 | ||
20 | #include <string> | |
21 | ||
b32b8144 FG |
22 | #ifdef BOOST_MSVC |
23 | # pragma warning(disable: 4127) // conditional expression is constant | |
24 | #endif | |
25 | ||
7c673cae FG |
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_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast); | |
41 | s[s.size()-1] += static_cast<CharT>(9); // '0' -> '9' | |
42 | BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast); | |
43 | ||
b32b8144 | 44 | if (limits::is_signed) |
7c673cae | 45 | { |
b32b8144 FG |
46 | std::basic_ostringstream<CharT> o2; |
47 | o2 << (limits::min)() << zero; | |
48 | s = o2.str(); | |
7c673cae FG |
49 | BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast); |
50 | s[s.size()-1] += static_cast<CharT>(9); // '0' -> '9' | |
51 | BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast); | |
52 | } | |
53 | } | |
54 | ||
55 | void test_vc8_bug() | |
56 | { | |
57 | test_too_long_number<boost::intmax_t>('0'); | |
58 | test_too_long_number<boost::uintmax_t>('0'); | |
59 | #if !defined(BOOST_LCAST_NO_WCHAR_T) | |
60 | test_too_long_number<boost::intmax_t>(L'0'); | |
61 | test_too_long_number<boost::uintmax_t>(L'0'); | |
62 | #endif | |
63 | } | |
64 | ||
65 | unit_test::test_suite *init_unit_test_suite(int, char *[]) | |
66 | { | |
67 | unit_test::test_suite *suite = | |
68 | BOOST_TEST_SUITE("lexical_cast vc8 bug unit test"); | |
69 | suite->add(BOOST_TEST_CASE(test_vc8_bug)); | |
70 | return suite; | |
71 | } |