]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_checked_cpp_int.cpp
1 ///////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
7 // Compare arithmetic results using fixed_int to GMP results.
11 # define _SCL_SECURE_NO_WARNINGS
14 #include <boost/multiprecision/cpp_int.hpp>
17 template <class Number
>
20 using namespace boost::multiprecision
;
21 typedef Number test_type
;
23 if(std::numeric_limits
<test_type
>::is_bounded
)
25 test_type val
= (std::numeric_limits
<test_type
>::max
)();
26 #ifndef BOOST_NO_EXCEPTIONS
27 BOOST_CHECK_THROW(++val
, std::overflow_error
);
28 val
= (std::numeric_limits
<test_type
>::max
)();
29 BOOST_CHECK_THROW(test_type(1 + val
), std::overflow_error
);
30 BOOST_CHECK_THROW(test_type(val
+ 1), std::overflow_error
);
31 BOOST_CHECK_THROW(test_type(2 * val
), std::overflow_error
);
34 BOOST_CHECK_THROW(test_type(2 * val
), std::overflow_error
);
36 if(std::numeric_limits
<test_type
>::is_signed
)
38 val
= (std::numeric_limits
<test_type
>::min
)();
39 BOOST_CHECK_THROW(--val
, std::overflow_error
);
40 val
= (std::numeric_limits
<test_type
>::min
)();
41 BOOST_CHECK_THROW(test_type(val
- 1), std::overflow_error
);
42 BOOST_CHECK_THROW(test_type(2 * val
), std::overflow_error
);
45 BOOST_CHECK_THROW(test_type(2 * val
), std::overflow_error
);
49 val
= (std::numeric_limits
<test_type
>::min
)();
50 BOOST_CHECK_THROW(--val
, std::range_error
);
51 val
= (std::numeric_limits
<test_type
>::min
)();
52 BOOST_CHECK_THROW(test_type(val
- 1), std::range_error
);
57 #ifndef BOOST_NO_EXCEPTIONS
58 if(std::numeric_limits
<test_type
>::is_signed
)
62 BOOST_CHECK_THROW(test_type(a
| b
), std::range_error
);
63 BOOST_CHECK_THROW(test_type(a
& b
), std::range_error
);
64 BOOST_CHECK_THROW(test_type(a
^ b
), std::range_error
);
68 // Constructing from a negative value is not allowed:
69 BOOST_CHECK_THROW(test_type(-2), std::range_error
);
70 BOOST_CHECK_THROW(test_type("-2"), std::range_error
);
72 if(std::numeric_limits
<test_type
>::digits
< std::numeric_limits
<long long>::digits
)
74 long long llm
= (std::numeric_limits
<long long>::max
)();
76 BOOST_CHECK_THROW(t
= llm
, std::range_error
);
77 BOOST_CHECK_THROW(t
= static_cast<test_type
>(llm
), std::range_error
);
78 unsigned long long ullm
= (std::numeric_limits
<unsigned long long>::max
)();
79 BOOST_CHECK_THROW(t
= ullm
, std::range_error
);
80 BOOST_CHECK_THROW(t
= static_cast<test_type
>(ullm
), std::range_error
);
82 static const checked_uint512_t big
= (std::numeric_limits
<checked_uint512_t
>::max
)();
83 BOOST_CHECK_THROW(t
= static_cast<test_type
>(big
), std::range_error
);
88 BOOST_CHECK_THROW(test_type("12A"), std::runtime_error
);
89 BOOST_CHECK_THROW(test_type("0658"), std::runtime_error
);
91 if(std::numeric_limits
<test_type
>::is_signed
)
93 BOOST_CHECK_THROW(test_type(-2).str(0, std::ios_base::hex
), std::runtime_error
);
94 BOOST_CHECK_THROW(test_type(-2).str(0, std::ios_base::oct
), std::runtime_error
);
101 using namespace boost::multiprecision
;
103 test
<number
<cpp_int_backend
<0, 0, signed_magnitude
, checked
> > >();
104 test
<checked_int512_t
>();
105 test
<checked_uint512_t
>();
106 test
<number
<cpp_int_backend
<32, 32, signed_magnitude
, checked
, void> > >();
107 test
<number
<cpp_int_backend
<32, 32, unsigned_magnitude
, checked
, void> > >();
110 // We also need to test type with "odd" bit counts in order to ensure full code coverage:
112 test
<number
<cpp_int_backend
<528, 528, signed_magnitude
, checked
, void> > >();
113 test
<number
<cpp_int_backend
<528, 528, unsigned_magnitude
, checked
, void> > >();
114 test
<number
<cpp_int_backend
<48, 48, signed_magnitude
, checked
, void> > >();
115 test
<number
<cpp_int_backend
<48, 48, unsigned_magnitude
, checked
, void> > >();
116 return boost::report_errors();