]>
git.proxmox.com Git - ceph.git/blob - 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_
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
, class BigNumber
>
20 using namespace boost::multiprecision
;
21 typedef Number test_type
;
23 test_type h
= (std::numeric_limits
<test_type
>::max
)();
24 test_type l
= (std::numeric_limits
<test_type
>::max
)();
28 BOOST_CHECK_EQUAL(r
, cpp_int(h
) + cpp_int(h
));
31 BOOST_CHECK_EQUAL(r
, cpp_int(h
) * cpp_int(h
));
33 if(std::numeric_limits
<test_type
>::is_signed
)
36 BOOST_CHECK_EQUAL(r
, cpp_int(l
) - cpp_int(h
));
38 BOOST_CHECK_EQUAL(r
, cpp_int(h
) - cpp_int(l
));
40 BOOST_CHECK_EQUAL(r
, cpp_int(l
) * cpp_int(l
));
44 // Try again with integer types as the source:
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
;
52 i_type ih
= (std::numeric_limits
<i_type
>::max
)();
53 i_type il
= (std::numeric_limits
<i_type
>::max
)();
56 BOOST_CHECK_EQUAL(r
, cpp_int(ih
) + cpp_int(ih
));
59 BOOST_CHECK_EQUAL(r
, cpp_int(ih
) * cpp_int(ih
));
61 if(std::numeric_limits
<test_type
>::is_signed
)
64 BOOST_CHECK_EQUAL(r
, cpp_int(il
) - cpp_int(ih
));
66 BOOST_CHECK_EQUAL(r
, cpp_int(ih
) - cpp_int(il
));
68 BOOST_CHECK_EQUAL(r
, cpp_int(il
) * cpp_int(il
));
72 void test_rational_mixed()
74 using namespace boost::multiprecision
;
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);
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);
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);
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);
101 using namespace boost::multiprecision
;
103 test_rational_mixed();
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
>();
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
>();
115 return boost::report_errors();