]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/numeric/conversion/test/numeric_cast_test.cpp
f94393cb193513ba1d87524e19960ced91c2dae7
1 // boost utility cast test program -----------------------------------------//
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)
7 // See http://www.boost.org for most recent version including documentation.
10 // 28 Set 04 taken from the old cast library (Fernando Cacciola)
14 #include <cfloat> // for DBL_MAX (Peter Schmid)
16 #include <boost/numeric/conversion/cast.hpp>
18 #include "boost/test/minimal.hpp"
20 # if SCHAR_MAX == LONG_MAX
21 # error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
24 using namespace boost
;
27 int test_main( int argc
, char * argv
[] )
31 cout
<< "NDEBUG is defined\n";
33 cout
<< "NDEBUG is not defined\n";
36 cout
<< "\nBeginning tests...\n";
38 // test implicit_cast and numeric_cast -------------------------------------//
40 // tests which should succeed
42 long small_negative_value
= -1;
43 long large_value
= LONG_MAX
;
44 long large_negative_value
= LONG_MIN
;
47 c
= large_value
; // see if compiler generates warning
49 c
= numeric_cast
<signed char>( small_value
);
50 BOOST_CHECK( c
== 1 );
52 c
= numeric_cast
<signed char>( small_value
);
53 BOOST_CHECK( c
== 1 );
55 c
= numeric_cast
<signed char>( small_negative_value
);
56 BOOST_CHECK( c
== -1 );
58 // These tests courtesy of Joe R NWP Swatosh<joe.r.swatosh@usace.army.mil>
59 BOOST_CHECK( 0.0f
== numeric_cast
<float>( 0.0 ) );
60 BOOST_CHECK( 0.0 == numeric_cast
<double>( 0.0 ) );
62 // tests which should result in errors being detected
64 bool caught_exception
= false;
65 try { c
= numeric_cast
<signed char>( large_value
); }
66 catch ( numeric::bad_numeric_cast
)
67 { cout
<<"caught bad_numeric_cast #1\n"; caught_exception
= true; }
68 BOOST_CHECK ( caught_exception
);
70 caught_exception
= false;
71 try { c
= numeric_cast
<signed char>( large_negative_value
); }
72 catch ( numeric::bad_numeric_cast
)
73 { cout
<<"caught bad_numeric_cast #2\n"; caught_exception
= true; }
74 BOOST_CHECK ( caught_exception
);
77 caught_exception
= false;
78 try { ul
= numeric_cast
<unsigned long>( large_negative_value
); }
79 catch ( numeric::bad_numeric_cast
)
80 { cout
<<"caught bad_numeric_cast #3\n"; caught_exception
= true; }
81 BOOST_CHECK ( caught_exception
);
83 caught_exception
= false;
84 try { ul
= numeric_cast
<unsigned long>( small_negative_value
); }
85 catch ( numeric::bad_numeric_cast
)
86 { cout
<<"caught bad_numeric_cast #4\n"; caught_exception
= true; }
87 BOOST_CHECK ( caught_exception
);
89 caught_exception
= false;
90 try { numeric_cast
<int>( DBL_MAX
); }
91 catch ( numeric::bad_numeric_cast
)
92 { cout
<<"caught bad_numeric_cast #5\n"; caught_exception
= true; }
93 BOOST_CHECK ( caught_exception
);