]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_checked_cpp_int.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / 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_
5
6 //
7 // Compare arithmetic results using fixed_int to GMP results.
8 //
9
10 #ifdef _MSC_VER
11 # define _SCL_SECURE_NO_WARNINGS
12 #endif
13
14 #include <boost/multiprecision/cpp_int.hpp>
15 #include "test.hpp"
16
17 template <class Number>
18 void test()
19 {
20 using namespace boost::multiprecision;
21 typedef Number test_type;
22
23 if(std::numeric_limits<test_type>::is_bounded)
24 {
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);
32 val /= 2;
33 val += 1;
34 BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
35
36 if(std::numeric_limits<test_type>::is_signed)
37 {
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);
43 val /= 2;
44 val -= 1;
45 BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
46 }
47 else
48 {
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);
53 }
54 #endif
55 }
56
57 #ifndef BOOST_NO_EXCEPTIONS
58 if(std::numeric_limits<test_type>::is_signed)
59 {
60 test_type a = -1;
61 test_type b = 1;
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);
65 }
66 else
67 {
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);
71 }
72 if(std::numeric_limits<test_type>::digits < std::numeric_limits<long long>::digits)
73 {
74 long long llm = (std::numeric_limits<long long>::max)();
75 test_type t;
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);
81
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);
84 }
85 //
86 // String errors:
87 //
88 BOOST_CHECK_THROW(test_type("12A"), std::runtime_error);
89 BOOST_CHECK_THROW(test_type("0658"), std::runtime_error);
90
91 if(std::numeric_limits<test_type>::is_signed)
92 {
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);
95 }
96 #endif
97 }
98
99 int main()
100 {
101 using namespace boost::multiprecision;
102
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> > >();
108
109 //
110 // We also need to test type with "odd" bit counts in order to ensure full code coverage:
111 //
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();
117 }
118
119
120