]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_mixed_cpp_int.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_mixed_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, class BigNumber>
18 void test()
19 {
20 using namespace boost::multiprecision;
21 typedef Number test_type;
22
23 test_type h = (std::numeric_limits<test_type>::max)();
24 test_type l = (std::numeric_limits<test_type>::max)();
25 BigNumber r;
26
27 add(r, h, h);
28 BOOST_CHECK_EQUAL(r, cpp_int(h) + cpp_int(h));
29
30 multiply(r, h, h);
31 BOOST_CHECK_EQUAL(r, cpp_int(h) * cpp_int(h));
32
33 if(std::numeric_limits<test_type>::is_signed)
34 {
35 subtract(r, l, h);
36 BOOST_CHECK_EQUAL(r, cpp_int(l) - cpp_int(h));
37 subtract(r, h, l);
38 BOOST_CHECK_EQUAL(r, cpp_int(h) - cpp_int(l));
39 multiply(r, l, l);
40 BOOST_CHECK_EQUAL(r, cpp_int(l) * cpp_int(l));
41 }
42
43 //
44 // Try again with integer types as the source:
45 //
46 enum{ max_digits = std::numeric_limits<test_type>::is_signed ? std::numeric_limits<long long>::digits : std::numeric_limits<unsigned long long>::digits };
47 enum{ require_digits = std::numeric_limits<test_type>::digits <= 2 * max_digits ? std::numeric_limits<test_type>::digits / 2 : max_digits };
48 typedef typename boost::uint_t<require_digits>::least uint_least;
49 typedef typename boost::int_t<require_digits>::least int_least;
50 typedef typename boost::mpl::if_c<std::numeric_limits<test_type>::is_signed, int_least, uint_least>::type i_type;
51
52 i_type ih = (std::numeric_limits<i_type>::max)();
53 i_type il = (std::numeric_limits<i_type>::max)();
54
55 add(r, ih, ih);
56 BOOST_CHECK_EQUAL(r, cpp_int(ih) + cpp_int(ih));
57
58 multiply(r, ih, ih);
59 BOOST_CHECK_EQUAL(r, cpp_int(ih) * cpp_int(ih));
60
61 if(std::numeric_limits<test_type>::is_signed)
62 {
63 subtract(r, il, ih);
64 BOOST_CHECK_EQUAL(r, cpp_int(il) - cpp_int(ih));
65 subtract(r, ih, il);
66 BOOST_CHECK_EQUAL(r, cpp_int(ih) - cpp_int(il));
67 multiply(r, il, il);
68 BOOST_CHECK_EQUAL(r, cpp_int(il) * cpp_int(il));
69 }
70 }
71
72 void test_rational_mixed()
73 {
74 using namespace boost::multiprecision;
75 cpp_int a(2);
76 cpp_rational r(10);
77
78 BOOST_CHECK_EQUAL(a + -r, -8);
79 BOOST_CHECK_EQUAL(-r + a, -8);
80 BOOST_CHECK_EQUAL(-a + r, 8);
81 BOOST_CHECK_EQUAL(r + -a, 8);
82
83 BOOST_CHECK_EQUAL(a - -r, 12);
84 BOOST_CHECK_EQUAL(-r - a, -12);
85 BOOST_CHECK_EQUAL(-a - r, -12);
86 BOOST_CHECK_EQUAL(r - -a, 12);
87
88 BOOST_CHECK_EQUAL(a * -r, -20);
89 BOOST_CHECK_EQUAL(-r * a, -20);
90 BOOST_CHECK_EQUAL(-a * r, -20);
91 BOOST_CHECK_EQUAL(r * -a, -20);
92
93 BOOST_CHECK_EQUAL(a / -r, cpp_rational(-2, 10));
94 BOOST_CHECK_EQUAL(-r / a, -5);
95 BOOST_CHECK_EQUAL(cpp_rational(-a / r), cpp_rational(-2, 10));
96 BOOST_CHECK_EQUAL(r / -a, -5);
97 }
98
99 int main()
100 {
101 using namespace boost::multiprecision;
102
103 test_rational_mixed();
104
105 test<checked_int512_t, checked_int1024_t>();
106 test<checked_int256_t, checked_int512_t>();
107 test<number<cpp_int_backend<64, 64, signed_magnitude, checked, void>, et_off>, checked_int128_t>();
108 test<boost::int64_t, checked_int128_t>();
109
110 test<checked_uint512_t, checked_uint1024_t>();
111 test<checked_uint256_t, checked_uint512_t>();
112 test<number<cpp_int_backend<64, 64, unsigned_magnitude, checked, void>, et_off>, checked_uint128_t>();
113 test<boost::uint64_t, checked_int128_t>();
114
115 return boost::report_errors();
116 }
117
118
119