]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/numeric/conversion/test/numeric_cast_test.cpp
f94393cb193513ba1d87524e19960ced91c2dae7
[ceph.git] / ceph / src / boost / libs / numeric / conversion / test / numeric_cast_test.cpp
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
18 #include "boost/test/minimal.hpp"
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
27 int test_main( int argc, char * argv[] )
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 );
50 BOOST_CHECK( c == 1 );
51 c = 0;
52 c = numeric_cast<signed char>( small_value );
53 BOOST_CHECK( c == 1 );
54 c = 0;
55 c = numeric_cast<signed char>( small_negative_value );
56 BOOST_CHECK( c == -1 );
57
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 ) );
61
62 // tests which should result in errors being detected
63
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 );
69
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 );
75
76 unsigned long ul;
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 );
82
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 );
88
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 );
94
95 return 0 ;
96
97 } // main