]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // boost utility cast test program -----------------------------------------// |
2 | ||
3 | // (C) Copyright Beman Dawes, Dave Abrahams 1999. Distributed under the Boost | |
4 | // Software License, Version 1.0. (See accompanying file | |
5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | // See http://www.boost.org for most recent version including documentation. | |
8 | ||
9 | // Revision History | |
10 | // 28 Set 04 taken from the old cast library (Fernando Cacciola) | |
11 | ||
12 | #include <iostream> | |
13 | #include <climits> | |
14 | #include <cfloat> // for DBL_MAX (Peter Schmid) | |
15 | ||
16 | #include <boost/numeric/conversion/cast.hpp> | |
17 | ||
1e59de90 | 18 | #include <boost/core/lightweight_test.hpp> |
7c673cae FG |
19 | |
20 | # if SCHAR_MAX == LONG_MAX | |
21 | # error "This test program doesn't work if SCHAR_MAX == LONG_MAX" | |
22 | # endif | |
23 | ||
24 | using namespace boost; | |
25 | using std::cout; | |
26 | ||
1e59de90 | 27 | int main() |
7c673cae FG |
28 | { |
29 | ||
30 | # ifdef NDEBUG | |
31 | cout << "NDEBUG is defined\n"; | |
32 | # else | |
33 | cout << "NDEBUG is not defined\n"; | |
34 | # endif | |
35 | ||
36 | cout << "\nBeginning tests...\n"; | |
37 | ||
38 | // test implicit_cast and numeric_cast -------------------------------------// | |
39 | ||
40 | // tests which should succeed | |
41 | long small_value = 1; | |
42 | long small_negative_value = -1; | |
43 | long large_value = LONG_MAX; | |
44 | long large_negative_value = LONG_MIN; | |
45 | signed char c = 0; | |
46 | ||
47 | c = large_value; // see if compiler generates warning | |
48 | ||
49 | c = numeric_cast<signed char>( small_value ); | |
1e59de90 | 50 | BOOST_TEST( c == 1 ); |
7c673cae FG |
51 | c = 0; |
52 | c = numeric_cast<signed char>( small_value ); | |
1e59de90 | 53 | BOOST_TEST( c == 1 ); |
7c673cae FG |
54 | c = 0; |
55 | c = numeric_cast<signed char>( small_negative_value ); | |
1e59de90 | 56 | BOOST_TEST( c == -1 ); |
7c673cae FG |
57 | |
58 | // These tests courtesy of Joe R NWP Swatosh<joe.r.swatosh@usace.army.mil> | |
1e59de90 TL |
59 | BOOST_TEST( 0.0f == numeric_cast<float>( 0.0 ) ); |
60 | BOOST_TEST( 0.0 == numeric_cast<double>( 0.0 ) ); | |
7c673cae FG |
61 | |
62 | // tests which should result in errors being detected | |
63 | ||
1e59de90 TL |
64 | BOOST_TEST_THROWS( numeric_cast<signed char>(large_value), |
65 | numeric::bad_numeric_cast ); | |
66 | ||
67 | BOOST_TEST_THROWS( numeric_cast<signed char>(large_negative_value), | |
68 | numeric::bad_numeric_cast ); | |
69 | ||
70 | BOOST_TEST_THROWS( numeric_cast<signed char>(large_negative_value), | |
71 | numeric::bad_numeric_cast ); | |
72 | ||
73 | BOOST_TEST_THROWS( numeric_cast<unsigned long>(small_negative_value), | |
74 | numeric::bad_numeric_cast ); | |
75 | ||
76 | BOOST_TEST_THROWS( numeric_cast<int>(DBL_MAX), numeric::bad_numeric_cast ); | |
77 | ||
78 | return boost::report_errors() ; | |
7c673cae FG |
79 | |
80 | } // main |