]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_unchecked_cpp_int.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_unchecked_cpp_int.cpp
1 ///////////////////////////////////////////////////////////////
2 // Copyright 2017 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 // Check results of truncated overflow.
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 #include <boost/random/mersenne_twister.hpp>
17 #include <boost/random/uniform_int.hpp>
18
19 template <class T>
20 T generate_random(unsigned bits_wanted)
21 {
22 static boost::random::mt19937 gen;
23 typedef boost::random::mt19937::result_type random_type;
24
25 T max_val;
26 unsigned digits;
27 if(std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
28 {
29 max_val = (std::numeric_limits<T>::max)();
30 digits = std::numeric_limits<T>::digits;
31 }
32 else
33 {
34 max_val = T(1) << bits_wanted;
35 digits = bits_wanted;
36 }
37
38 unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
39 while((random_type(1) << bits_per_r_val) > (gen.max)()) --bits_per_r_val;
40
41 unsigned terms_needed = digits / bits_per_r_val + 1;
42
43 T val = 0;
44 for(unsigned i = 0; i < terms_needed; ++i)
45 {
46 val *= (gen.max)();
47 val += gen();
48 }
49 val %= max_val;
50 return val;
51 }
52
53
54 template <class Number>
55 void test()
56 {
57 using namespace boost::multiprecision;
58 typedef Number test_type;
59
60
61 for(unsigned i = 30; i < std::numeric_limits<test_type>::digits; ++i)
62 {
63 for(unsigned j = std::numeric_limits<test_type>::digits - i - 1; j < std::numeric_limits<test_type>::digits; ++j)
64 {
65 for(unsigned k = 0; k < 10; ++k)
66 {
67 test_type a = static_cast<test_type>(generate_random<cpp_int>(i));
68 test_type b = static_cast<test_type>(generate_random<cpp_int>(j));
69 test_type c = static_cast<test_type>(cpp_int(a) * cpp_int(b));
70 test_type d = a * b;
71 BOOST_CHECK_EQUAL(c, d);
72
73 if((k == 0) && (j == 0))
74 {
75 for(unsigned s = 1; s < std::numeric_limits<test_type>::digits; ++s)
76 BOOST_CHECK_EQUAL(a << s, test_type(cpp_int(a) << s));
77 }
78 }
79 }
80 }
81 }
82
83 int main()
84 {
85 using namespace boost::multiprecision;
86
87 test<int512_t>();
88 test<uint512_t>();
89
90 //
91 // We also need to test type with "odd" bit counts in order to ensure full code coverage:
92 //
93 test<number<cpp_int_backend<528, 528, signed_magnitude, unchecked, void> > >();
94 test<number<cpp_int_backend<528, 528, unsigned_magnitude, unchecked, void> > >();
95 test<number<cpp_int_backend<48, 48, signed_magnitude, unchecked, void> > >();
96 test<number<cpp_int_backend<48, 48, unsigned_magnitude, unchecked, void> > >();
97 return boost::report_errors();
98 }
99
100
101