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 factored out numeric_cast<> test (Fernando Cacciola) |
---|
11 | // 20 Jan 01 removed use of <limits> for portability to raw GCC (David Abrahams) |
---|
12 | // 28 Jun 00 implicit_cast removed (Beman Dawes) |
---|
13 | // 30 Aug 99 value_cast replaced by numeric_cast |
---|
14 | // 3 Aug 99 Initial Version |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | #include <climits> |
---|
18 | #include <cfloat> // for DBL_MAX (Peter Schmid) |
---|
19 | #include <boost/cast.hpp> |
---|
20 | |
---|
21 | #include "boost/test/minimal.hpp" |
---|
22 | |
---|
23 | # if SCHAR_MAX == LONG_MAX |
---|
24 | # error "This test program doesn't work if SCHAR_MAX == LONG_MAX" |
---|
25 | # endif |
---|
26 | |
---|
27 | using namespace boost; |
---|
28 | using std::cout; |
---|
29 | |
---|
30 | |
---|
31 | int test_main( int argc, char * argv[] ) |
---|
32 | { |
---|
33 | |
---|
34 | # ifdef NDEBUG |
---|
35 | cout << "NDEBUG is defined\n"; |
---|
36 | # else |
---|
37 | cout << "NDEBUG is not defined\n"; |
---|
38 | # endif |
---|
39 | |
---|
40 | cout << "\nBeginning tests...\n"; |
---|
41 | |
---|
42 | // test implicit_cast and numeric_cast -------------------------------------// |
---|
43 | |
---|
44 | // tests which should succeed |
---|
45 | long small_value = 1; |
---|
46 | long small_negative_value = -1; |
---|
47 | long large_value = LONG_MAX; |
---|
48 | long large_negative_value = LONG_MIN; |
---|
49 | signed char c = 0; |
---|
50 | |
---|
51 | c = large_value; // see if compiler generates warning |
---|
52 | |
---|
53 | c = numeric_cast<signed char>( small_value ); |
---|
54 | BOOST_CHECK( c == 1 ); |
---|
55 | c = 0; |
---|
56 | c = numeric_cast<signed char>( small_value ); |
---|
57 | BOOST_CHECK( c == 1 ); |
---|
58 | c = 0; |
---|
59 | c = numeric_cast<signed char>( small_negative_value ); |
---|
60 | BOOST_CHECK( c == -1 ); |
---|
61 | |
---|
62 | // These tests courtesy of Joe R NWP Swatosh<joe.r.swatosh@usace.army.mil> |
---|
63 | BOOST_CHECK( 0.0f == numeric_cast<float>( 0.0 ) ); |
---|
64 | BOOST_CHECK( 0.0 == numeric_cast<double>( 0.0 ) ); |
---|
65 | |
---|
66 | // tests which should result in errors being detected |
---|
67 | |
---|
68 | bool caught_exception = false; |
---|
69 | try { c = numeric_cast<signed char>( large_value ); } |
---|
70 | catch (bad_numeric_cast) |
---|
71 | { cout<<"caught bad_numeric_cast #1\n"; caught_exception = true; } |
---|
72 | BOOST_CHECK ( caught_exception ); |
---|
73 | |
---|
74 | caught_exception = false; |
---|
75 | try { c = numeric_cast<signed char>( large_negative_value ); } |
---|
76 | catch (bad_numeric_cast) |
---|
77 | { cout<<"caught bad_numeric_cast #2\n"; caught_exception = true; } |
---|
78 | BOOST_CHECK ( caught_exception ); |
---|
79 | |
---|
80 | unsigned long ul; |
---|
81 | caught_exception = false; |
---|
82 | try { ul = numeric_cast<unsigned long>( large_negative_value ); } |
---|
83 | catch (bad_numeric_cast) |
---|
84 | { cout<<"caught bad_numeric_cast #3\n"; caught_exception = true; } |
---|
85 | BOOST_CHECK ( caught_exception ); |
---|
86 | |
---|
87 | caught_exception = false; |
---|
88 | try { ul = numeric_cast<unsigned long>( small_negative_value ); } |
---|
89 | catch (bad_numeric_cast) |
---|
90 | { cout<<"caught bad_numeric_cast #4\n"; caught_exception = true; } |
---|
91 | BOOST_CHECK ( caught_exception ); |
---|
92 | |
---|
93 | caught_exception = false; |
---|
94 | try { numeric_cast<int>( DBL_MAX ); } |
---|
95 | catch (bad_numeric_cast) |
---|
96 | { cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; } |
---|
97 | BOOST_CHECK ( caught_exception ); |
---|
98 | |
---|
99 | return 0 ; |
---|
100 | } |
---|