Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / test / test_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 https://www.boost.org/LICENSE_1_0.txt
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 //
15 // This ensures all our code gets tested, even though it may
16 // not be the fastest configuration in normal use:
17 //
18 #define BOOST_MP_USE_LIMB_SHIFT
19
20 #include <boost/multiprecision/gmp.hpp>
21 #include <boost/multiprecision/cpp_int.hpp>
22 #include <boost/random/mersenne_twister.hpp>
23 #include <boost/random/uniform_int.hpp>
24 #include <boost/timer.hpp>
25 #include "test.hpp"
26
27 #ifdef _MSC_VER
28 #pragma warning(disable : 4127) //  Conditional expression is constant
29 #endif
30
31 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3)
32 #define TEST1
33 #define TEST2
34 #define TEST3
35 #endif
36
37 template <class T>
38 T generate_random(unsigned bits_wanted)
39 {
40    static boost::random::mt19937               gen;
41    typedef boost::random::mt19937::result_type random_type;
42
43    T        max_val;
44    unsigned digits;
45    if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
46    {
47       max_val = (std::numeric_limits<T>::max)();
48       digits  = std::numeric_limits<T>::digits;
49    }
50    else
51    {
52       max_val = T(1) << bits_wanted;
53       digits  = bits_wanted;
54    }
55
56    unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
57    while ((random_type(1) << bits_per_r_val) > (gen.max)())
58       --bits_per_r_val;
59
60    unsigned terms_needed = digits / bits_per_r_val + 1;
61
62    T val = 0;
63    for (unsigned i = 0; i < terms_needed; ++i)
64    {
65       val *= (gen.max)();
66       val += gen();
67    }
68    val %= max_val;
69    return val;
70 }
71
72 template <class T>
73 struct is_checked_cpp_int : public boost::mpl::false_
74 {};
75 template <unsigned MinBits, unsigned MaxBits, boost::multiprecision::cpp_integer_type SignType, class Allocator, boost::multiprecision::expression_template_option ET>
76 struct is_checked_cpp_int<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, boost::multiprecision::checked, Allocator>, ET> > : public boost::mpl::true_
77 {};
78
79 template <class Number>
80 struct tester
81 {
82    typedef Number                                         test_type;
83    typedef typename test_type::backend_type::checked_type checked;
84
85    unsigned     last_error_count;
86    boost::timer tim;
87
88    boost::multiprecision::mpz_int a, b, c, d;
89    int                            si;
90    unsigned                       ui;
91    test_type                      a1, b1, c1, d1;
92
93    void t1()
94    {
95       using namespace boost::multiprecision;
96       BOOST_CHECK_EQUAL(a.str(), a1.str());
97       BOOST_CHECK_EQUAL(b.str(), b1.str());
98       BOOST_CHECK_EQUAL(c.str(), c1.str());
99       BOOST_CHECK_EQUAL(d.str(), d1.str());
100       BOOST_CHECK_EQUAL(mpz_int(a + b).str(), test_type(a1 + b1).str());
101       BOOST_CHECK_EQUAL((mpz_int(a) += b).str(), (test_type(a1) += b1).str());
102       BOOST_CHECK_EQUAL((mpz_int(b) += a).str(), (test_type(b1) += a1).str());
103       BOOST_CHECK_EQUAL(mpz_int(a - b).str(), test_type(a1 - b1).str());
104       BOOST_CHECK_EQUAL((mpz_int(a) -= b).str(), (test_type(a1) -= b1).str());
105       BOOST_CHECK_EQUAL(mpz_int(mpz_int(-a) + b).str(), test_type(test_type(-a1) + b1).str());
106       BOOST_CHECK_EQUAL(mpz_int(mpz_int(-a) - b).str(), test_type(test_type(-a1) - b1).str());
107       BOOST_CHECK_EQUAL(mpz_int(c * d).str(), test_type(c1 * d1).str());
108       BOOST_CHECK_EQUAL((mpz_int(c) *= d).str(), (test_type(c1) *= d1).str());
109       BOOST_CHECK_EQUAL((mpz_int(d) *= c).str(), (test_type(d1) *= c1).str());
110       BOOST_CHECK_EQUAL(mpz_int(c * -d).str(), test_type(c1 * -d1).str());
111       BOOST_CHECK_EQUAL(mpz_int(-c * d).str(), test_type(-c1 * d1).str());
112       BOOST_CHECK_EQUAL((mpz_int(c) *= -d).str(), (test_type(c1) *= -d1).str());
113       BOOST_CHECK_EQUAL((mpz_int(-d) *= c).str(), (test_type(-d1) *= c1).str());
114       BOOST_CHECK_EQUAL(mpz_int(b * c).str(), test_type(b1 * c1).str());
115       BOOST_CHECK_EQUAL(mpz_int(a / b).str(), test_type(a1 / b1).str());
116       BOOST_CHECK_EQUAL((mpz_int(a) /= b).str(), (test_type(a1) /= b1).str());
117       BOOST_CHECK_EQUAL(mpz_int(a / -b).str(), test_type(a1 / -b1).str());
118       BOOST_CHECK_EQUAL(mpz_int(-a / b).str(), test_type(-a1 / b1).str());
119       BOOST_CHECK_EQUAL((mpz_int(a) /= -b).str(), (test_type(a1) /= -b1).str());
120       BOOST_CHECK_EQUAL((mpz_int(-a) /= b).str(), (test_type(-a1) /= b1).str());
121       BOOST_CHECK_EQUAL(mpz_int(a / d).str(), test_type(a1 / d1).str());
122       BOOST_CHECK_EQUAL(mpz_int(a % b).str(), test_type(a1 % b1).str());
123       BOOST_CHECK_EQUAL((mpz_int(a) %= b).str(), (test_type(a1) %= b1).str());
124       BOOST_CHECK_EQUAL(mpz_int(a % -b).str(), test_type(a1 % -b1).str());
125       BOOST_CHECK_EQUAL((mpz_int(a) %= -b).str(), (test_type(a1) %= -b1).str());
126       BOOST_CHECK_EQUAL(mpz_int(-a % b).str(), test_type(-a1 % b1).str());
127       BOOST_CHECK_EQUAL((mpz_int(-a) %= b).str(), (test_type(-a1) %= b1).str());
128       BOOST_CHECK_EQUAL(mpz_int(a % d).str(), test_type(a1 % d1).str());
129       BOOST_CHECK_EQUAL((mpz_int(a) %= d).str(), (test_type(a1) %= d1).str());
130
131       if (!std::numeric_limits<test_type>::is_bounded)
132       {
133          test_type p = a1 * b1;
134          test_type r;
135          divide_qr(p, b1, p, r);
136          BOOST_CHECK_EQUAL(p, a1);
137          BOOST_CHECK_EQUAL(r, test_type(0));
138
139          p = a1 * d1;
140          divide_qr(p, d1, p, r);
141          BOOST_CHECK_EQUAL(p, a1);
142          BOOST_CHECK_EQUAL(r, test_type(0));
143
144          divide_qr(p, test_type(1), p, r);
145          BOOST_CHECK_EQUAL(p, a1);
146          BOOST_CHECK_EQUAL(r, test_type(0));
147       }
148    }
149
150    void t2()
151    {
152       using namespace boost::multiprecision;
153       // bitwise ops:
154       BOOST_CHECK_EQUAL(mpz_int(a | b).str(), test_type(a1 | b1).str());
155       BOOST_CHECK_EQUAL((mpz_int(a) |= b).str(), (test_type(a1) |= b1).str());
156       if (!is_checked_cpp_int<test_type>::value)
157       {
158          BOOST_CHECK_EQUAL(mpz_int(-a | b).str(), test_type(-a1 | b1).str());
159          BOOST_CHECK_EQUAL((mpz_int(-a) |= b).str(), (test_type(-a1) |= b1).str());
160          BOOST_CHECK_EQUAL(mpz_int(a | -b).str(), test_type(a1 | -b1).str());
161          BOOST_CHECK_EQUAL((mpz_int(a) |= -b).str(), (test_type(a1) |= -b1).str());
162          BOOST_CHECK_EQUAL(mpz_int(-a | -b).str(), test_type(-a1 | -b1).str());
163          BOOST_CHECK_EQUAL((mpz_int(-a) |= -b).str(), (test_type(-a1) |= -b1).str());
164       }
165       BOOST_CHECK_EQUAL(mpz_int(a & b).str(), test_type(a1 & b1).str());
166       BOOST_CHECK_EQUAL((mpz_int(a) &= b).str(), (test_type(a1) &= b1).str());
167       if (!is_checked_cpp_int<test_type>::value)
168       {
169          BOOST_CHECK_EQUAL(mpz_int(-a & b).str(), test_type(-a1 & b1).str());
170          BOOST_CHECK_EQUAL((mpz_int(-a) &= b).str(), (test_type(-a1) &= b1).str());
171          BOOST_CHECK_EQUAL(mpz_int(a & -b).str(), test_type(a1 & -b1).str());
172          BOOST_CHECK_EQUAL((mpz_int(a) &= -b).str(), (test_type(a1) &= -b1).str());
173          BOOST_CHECK_EQUAL(mpz_int(-a & -b).str(), test_type(-a1 & -b1).str());
174          BOOST_CHECK_EQUAL((mpz_int(-a) &= -b).str(), (test_type(-a1) &= -b1).str());
175       }
176       BOOST_CHECK_EQUAL(mpz_int(a ^ b).str(), test_type(a1 ^ b1).str());
177       BOOST_CHECK_EQUAL((mpz_int(a) ^= b).str(), (test_type(a1) ^= b1).str());
178       if (!is_checked_cpp_int<test_type>::value)
179       {
180          BOOST_CHECK_EQUAL(mpz_int(-a ^ b).str(), test_type(-a1 ^ b1).str());
181          BOOST_CHECK_EQUAL((mpz_int(-a) ^= b).str(), (test_type(-a1) ^= b1).str());
182          BOOST_CHECK_EQUAL(mpz_int(a ^ -b).str(), test_type(a1 ^ -b1).str());
183          BOOST_CHECK_EQUAL((mpz_int(a) ^= -b).str(), (test_type(a1) ^= -b1).str());
184          BOOST_CHECK_EQUAL(mpz_int(-a ^ -b).str(), test_type(-a1 ^ -b1).str());
185          BOOST_CHECK_EQUAL((mpz_int(-a) ^= -b).str(), (test_type(-a1) ^= -b1).str());
186       }
187       // Shift ops:
188       for (unsigned i = 0; i < 128; ++i)
189       {
190          if (!std::numeric_limits<test_type>::is_bounded)
191          {
192             BOOST_CHECK_EQUAL(mpz_int(a << i).str(), test_type(a1 << i).str());
193             BOOST_CHECK_EQUAL(mpz_int(-a << i).str(), test_type(-a1 << i).str());
194          }
195          else if (!is_checked_cpp_int<test_type>::value)
196          {
197             test_type t1(mpz_int(a << i).str());
198             test_type t2 = a1 << i;
199             BOOST_CHECK_EQUAL(t1, t2);
200             t1 = test_type(mpz_int(-a << i).str());
201             t2 = -a1 << i;
202             BOOST_CHECK_EQUAL(t1, t2);
203          }
204          BOOST_CHECK_EQUAL(mpz_int(a >> i).str(), test_type(a1 >> i).str());
205          if (!is_checked_cpp_int<test_type>::value)
206          {
207             BOOST_CHECK_EQUAL(mpz_int(-a >> i).str(), test_type(-a1 >> i).str());
208          }
209       }
210       // gcd/lcm
211       BOOST_CHECK_EQUAL(mpz_int(gcd(a, b)).str(), test_type(gcd(a1, b1)).str());
212       BOOST_CHECK_EQUAL(mpz_int(lcm(c, d)).str(), test_type(lcm(c1, d1)).str());
213       BOOST_CHECK_EQUAL(mpz_int(gcd(-a, b)).str(), test_type(gcd(-a1, b1)).str());
214       BOOST_CHECK_EQUAL(mpz_int(lcm(-c, d)).str(), test_type(lcm(-c1, d1)).str());
215       BOOST_CHECK_EQUAL(mpz_int(gcd(-a, -b)).str(), test_type(gcd(-a1, -b1)).str());
216       BOOST_CHECK_EQUAL(mpz_int(lcm(-c, -d)).str(), test_type(lcm(-c1, -d1)).str());
217       BOOST_CHECK_EQUAL(mpz_int(gcd(a, -b)).str(), test_type(gcd(a1, -b1)).str());
218       BOOST_CHECK_EQUAL(mpz_int(lcm(c, -d)).str(), test_type(lcm(c1, -d1)).str());
219       // Integer sqrt:
220       mpz_int   r;
221       test_type r1;
222       BOOST_CHECK_EQUAL(sqrt(a, r).str(), sqrt(a1, r1).str());
223       BOOST_CHECK_EQUAL(r.str(), r1.str());
224    }
225
226    void t3()
227    {
228       using namespace boost::multiprecision;
229       // Now check operations involving signed integers:
230       BOOST_CHECK_EQUAL(mpz_int(a + si).str(), test_type(a1 + si).str());
231       BOOST_CHECK_EQUAL(mpz_int(a + -si).str(), test_type(a1 + -si).str());
232       BOOST_CHECK_EQUAL(mpz_int(-a + si).str(), test_type(-a1 + si).str());
233       BOOST_CHECK_EQUAL(mpz_int(si + a).str(), test_type(si + a1).str());
234       BOOST_CHECK_EQUAL((mpz_int(a) += si).str(), (test_type(a1) += si).str());
235       BOOST_CHECK_EQUAL((mpz_int(a) += -si).str(), (test_type(a1) += -si).str());
236       BOOST_CHECK_EQUAL((mpz_int(-a) += si).str(), (test_type(-a1) += si).str());
237       BOOST_CHECK_EQUAL((mpz_int(-a) += -si).str(), (test_type(-a1) += -si).str());
238       BOOST_CHECK_EQUAL(mpz_int(a - si).str(), test_type(a1 - si).str());
239       BOOST_CHECK_EQUAL(mpz_int(a - -si).str(), test_type(a1 - -si).str());
240       BOOST_CHECK_EQUAL(mpz_int(-a - si).str(), test_type(-a1 - si).str());
241       BOOST_CHECK_EQUAL(mpz_int(si - a).str(), test_type(si - a1).str());
242       BOOST_CHECK_EQUAL((mpz_int(a) -= si).str(), (test_type(a1) -= si).str());
243       BOOST_CHECK_EQUAL((mpz_int(a) -= -si).str(), (test_type(a1) -= -si).str());
244       BOOST_CHECK_EQUAL((mpz_int(-a) -= si).str(), (test_type(-a1) -= si).str());
245       BOOST_CHECK_EQUAL((mpz_int(-a) -= -si).str(), (test_type(-a1) -= -si).str());
246       BOOST_CHECK_EQUAL(mpz_int(b * si).str(), test_type(b1 * si).str());
247       BOOST_CHECK_EQUAL(mpz_int(b * -si).str(), test_type(b1 * -si).str());
248       BOOST_CHECK_EQUAL(mpz_int(-b * si).str(), test_type(-b1 * si).str());
249       BOOST_CHECK_EQUAL(mpz_int(si * b).str(), test_type(si * b1).str());
250       BOOST_CHECK_EQUAL((mpz_int(a) *= si).str(), (test_type(a1) *= si).str());
251       BOOST_CHECK_EQUAL((mpz_int(a) *= -si).str(), (test_type(a1) *= -si).str());
252       BOOST_CHECK_EQUAL((mpz_int(-a) *= si).str(), (test_type(-a1) *= si).str());
253       BOOST_CHECK_EQUAL((mpz_int(-a) *= -si).str(), (test_type(-a1) *= -si).str());
254       BOOST_CHECK_EQUAL(mpz_int(a / si).str(), test_type(a1 / si).str());
255       BOOST_CHECK_EQUAL(mpz_int(a / -si).str(), test_type(a1 / -si).str());
256       BOOST_CHECK_EQUAL(mpz_int(-a / si).str(), test_type(-a1 / si).str());
257       BOOST_CHECK_EQUAL((mpz_int(a) /= si).str(), (test_type(a1) /= si).str());
258       BOOST_CHECK_EQUAL((mpz_int(a) /= -si).str(), (test_type(a1) /= -si).str());
259       BOOST_CHECK_EQUAL((mpz_int(-a) /= si).str(), (test_type(-a1) /= si).str());
260       BOOST_CHECK_EQUAL((mpz_int(-a) /= -si).str(), (test_type(-a1) /= -si).str());
261       BOOST_CHECK_EQUAL(mpz_int(a % si).str(), test_type(a1 % si).str());
262       BOOST_CHECK_EQUAL(mpz_int(a % -si).str(), test_type(a1 % -si).str());
263       BOOST_CHECK_EQUAL(mpz_int(-a % si).str(), test_type(-a1 % si).str());
264       BOOST_CHECK_EQUAL((mpz_int(a) %= si).str(), (test_type(a1) %= si).str());
265       BOOST_CHECK_EQUAL((mpz_int(a) %= -si).str(), (test_type(a1) %= -si).str());
266       BOOST_CHECK_EQUAL((mpz_int(-a) %= si).str(), (test_type(-a1) %= si).str());
267       BOOST_CHECK_EQUAL((mpz_int(-a) %= -si).str(), (test_type(-a1) %= -si).str());
268       if ((si > 0) || !is_checked_cpp_int<test_type>::value)
269       {
270          BOOST_CHECK_EQUAL(mpz_int(a | si).str(), test_type(a1 | si).str());
271          BOOST_CHECK_EQUAL((mpz_int(a) |= si).str(), (test_type(a1) |= si).str());
272          BOOST_CHECK_EQUAL(mpz_int(a & si).str(), test_type(a1 & si).str());
273          BOOST_CHECK_EQUAL((mpz_int(a) &= si).str(), (test_type(a1) &= si).str());
274          BOOST_CHECK_EQUAL(mpz_int(a ^ si).str(), test_type(a1 ^ si).str());
275          BOOST_CHECK_EQUAL((mpz_int(a) ^= si).str(), (test_type(a1) ^= si).str());
276          BOOST_CHECK_EQUAL(mpz_int(si | a).str(), test_type(si | a1).str());
277          BOOST_CHECK_EQUAL(mpz_int(si & a).str(), test_type(si & a1).str());
278          BOOST_CHECK_EQUAL(mpz_int(si ^ a).str(), test_type(si ^ a1).str());
279       }
280       BOOST_CHECK_EQUAL(mpz_int(gcd(a, si)).str(), test_type(gcd(a1, si)).str());
281       BOOST_CHECK_EQUAL(mpz_int(gcd(si, b)).str(), test_type(gcd(si, b1)).str());
282       BOOST_CHECK_EQUAL(mpz_int(lcm(c, si)).str(), test_type(lcm(c1, si)).str());
283       BOOST_CHECK_EQUAL(mpz_int(lcm(si, d)).str(), test_type(lcm(si, d1)).str());
284       BOOST_CHECK_EQUAL(mpz_int(gcd(-a, si)).str(), test_type(gcd(-a1, si)).str());
285       BOOST_CHECK_EQUAL(mpz_int(gcd(-si, b)).str(), test_type(gcd(-si, b1)).str());
286       BOOST_CHECK_EQUAL(mpz_int(lcm(-c, si)).str(), test_type(lcm(-c1, si)).str());
287       BOOST_CHECK_EQUAL(mpz_int(lcm(-si, d)).str(), test_type(lcm(-si, d1)).str());
288       BOOST_CHECK_EQUAL(mpz_int(gcd(-a, -si)).str(), test_type(gcd(-a1, -si)).str());
289       BOOST_CHECK_EQUAL(mpz_int(gcd(-si, -b)).str(), test_type(gcd(-si, -b1)).str());
290       BOOST_CHECK_EQUAL(mpz_int(lcm(-c, -si)).str(), test_type(lcm(-c1, -si)).str());
291       BOOST_CHECK_EQUAL(mpz_int(lcm(-si, -d)).str(), test_type(lcm(-si, -d1)).str());
292       BOOST_CHECK_EQUAL(mpz_int(gcd(a, -si)).str(), test_type(gcd(a1, -si)).str());
293       BOOST_CHECK_EQUAL(mpz_int(gcd(si, -b)).str(), test_type(gcd(si, -b1)).str());
294       BOOST_CHECK_EQUAL(mpz_int(lcm(c, -si)).str(), test_type(lcm(c1, -si)).str());
295       BOOST_CHECK_EQUAL(mpz_int(lcm(si, -d)).str(), test_type(lcm(si, -d1)).str());
296    }
297
298    void t4()
299    {
300       using namespace boost::multiprecision;
301       // Now check operations involving unsigned integers:
302       BOOST_CHECK_EQUAL(mpz_int(a + ui).str(), test_type(a1 + ui).str());
303       BOOST_CHECK_EQUAL(mpz_int(-a + ui).str(), test_type(-a1 + ui).str());
304       BOOST_CHECK_EQUAL(mpz_int(ui + a).str(), test_type(ui + a1).str());
305       BOOST_CHECK_EQUAL((mpz_int(a) += ui).str(), (test_type(a1) += ui).str());
306       BOOST_CHECK_EQUAL((mpz_int(-a) += ui).str(), (test_type(-a1) += ui).str());
307       BOOST_CHECK_EQUAL(mpz_int(a - ui).str(), test_type(a1 - ui).str());
308       BOOST_CHECK_EQUAL(mpz_int(-a - ui).str(), test_type(-a1 - ui).str());
309       BOOST_CHECK_EQUAL(mpz_int(ui - a).str(), test_type(ui - a1).str());
310       BOOST_CHECK_EQUAL((mpz_int(a) -= ui).str(), (test_type(a1) -= ui).str());
311       BOOST_CHECK_EQUAL((mpz_int(-a) -= ui).str(), (test_type(-a1) -= ui).str());
312       BOOST_CHECK_EQUAL(mpz_int(b * ui).str(), test_type(b1 * ui).str());
313       BOOST_CHECK_EQUAL(mpz_int(-b * ui).str(), test_type(-b1 * ui).str());
314       BOOST_CHECK_EQUAL(mpz_int(ui * b).str(), test_type(ui * b1).str());
315       BOOST_CHECK_EQUAL((mpz_int(a) *= ui).str(), (test_type(a1) *= ui).str());
316       BOOST_CHECK_EQUAL((mpz_int(-a) *= ui).str(), (test_type(-a1) *= ui).str());
317       BOOST_CHECK_EQUAL(mpz_int(a / ui).str(), test_type(a1 / ui).str());
318       BOOST_CHECK_EQUAL(mpz_int(-a / ui).str(), test_type(-a1 / ui).str());
319       BOOST_CHECK_EQUAL((mpz_int(a) /= ui).str(), (test_type(a1) /= ui).str());
320       BOOST_CHECK_EQUAL((mpz_int(-a) /= ui).str(), (test_type(-a1) /= ui).str());
321       BOOST_CHECK_EQUAL(mpz_int(a % ui).str(), test_type(a1 % ui).str());
322       BOOST_CHECK_EQUAL(mpz_int(-a % ui).str(), test_type(-a1 % ui).str());
323       BOOST_CHECK_EQUAL((mpz_int(a) %= ui).str(), (test_type(a1) %= ui).str());
324       BOOST_CHECK_EQUAL((mpz_int(-a) %= ui).str(), (test_type(-a1) %= ui).str());
325       BOOST_CHECK_EQUAL(mpz_int(a | ui).str(), test_type(a1 | ui).str());
326       BOOST_CHECK_EQUAL((mpz_int(a) |= ui).str(), (test_type(a1) |= ui).str());
327       BOOST_CHECK_EQUAL(mpz_int(a & ui).str(), test_type(a1 & ui).str());
328       BOOST_CHECK_EQUAL((mpz_int(a) &= ui).str(), (test_type(a1) &= ui).str());
329       BOOST_CHECK_EQUAL(mpz_int(a ^ ui).str(), test_type(a1 ^ ui).str());
330       BOOST_CHECK_EQUAL((mpz_int(a) ^= ui).str(), (test_type(a1) ^= ui).str());
331       BOOST_CHECK_EQUAL(mpz_int(ui | a).str(), test_type(ui | a1).str());
332       BOOST_CHECK_EQUAL(mpz_int(ui & a).str(), test_type(ui & a1).str());
333       BOOST_CHECK_EQUAL(mpz_int(ui ^ a).str(), test_type(ui ^ a1).str());
334       BOOST_CHECK_EQUAL(mpz_int(gcd(a, ui)).str(), test_type(gcd(a1, ui)).str());
335       BOOST_CHECK_EQUAL(mpz_int(gcd(ui, b)).str(), test_type(gcd(ui, b1)).str());
336       BOOST_CHECK_EQUAL(mpz_int(lcm(c, ui)).str(), test_type(lcm(c1, ui)).str());
337       BOOST_CHECK_EQUAL(mpz_int(lcm(ui, d)).str(), test_type(lcm(ui, d1)).str());
338       BOOST_CHECK_EQUAL(mpz_int(gcd(-a, ui)).str(), test_type(gcd(-a1, ui)).str());
339       BOOST_CHECK_EQUAL(mpz_int(lcm(-c, ui)).str(), test_type(lcm(-c1, ui)).str());
340       BOOST_CHECK_EQUAL(mpz_int(gcd(ui, -b)).str(), test_type(gcd(ui, -b1)).str());
341       BOOST_CHECK_EQUAL(mpz_int(lcm(ui, -d)).str(), test_type(lcm(ui, -d1)).str());
342
343       if (std::numeric_limits<test_type>::is_modulo && checked::value)
344       {
345          static mpz_int m = mpz_int(1) << std::numeric_limits<test_type>::digits;
346          mpz_int        t(a);
347          test_type      t1(a1);
348          for (unsigned i = 0; i < 10; ++i)
349          {
350             t *= a;
351             t %= m;
352             t += a;
353             t %= m;
354             t1 *= a1;
355             t1 += a1;
356          }
357          BOOST_CHECK_EQUAL(t.str(), t1.str());
358       }
359    }
360
361    void t5()
362    {
363       using namespace boost::multiprecision;
364       //
365       // Now integer functions:
366       //
367       mpz_int   z1, z2;
368       test_type t1, t2;
369       divide_qr(a, b, z1, z2);
370       divide_qr(a1, b1, t1, t2);
371       BOOST_CHECK_EQUAL(z1.str(), t1.str());
372       BOOST_CHECK_EQUAL(z2.str(), t2.str());
373       BOOST_CHECK_EQUAL(integer_modulus(a, si), integer_modulus(a1, si));
374       BOOST_CHECK_EQUAL(lsb(a), lsb(a1));
375       BOOST_CHECK_EQUAL(msb(a), msb(a1));
376
377       for (unsigned i = 0; i < 1000; i += 13)
378       {
379          BOOST_CHECK_EQUAL(bit_test(a, i), bit_test(a1, i));
380       }
381       if (!std::numeric_limits<test_type>::is_modulo)
382       {
383          // We have to take care that our powers don't grow too large, otherwise this takes "forever",
384          // also don't test for modulo types, as these may give a different result from arbitrary
385          // precision types:
386          BOOST_CHECK_EQUAL(mpz_int(pow(d, ui % 19)).str(), test_type(pow(d1, ui % 19)).str());
387          BOOST_CHECK_EQUAL(mpz_int(powm(a, b, c)).str(), test_type(powm(a1, b1, c1)).str());
388          BOOST_CHECK_EQUAL(mpz_int(powm(a, b, ui)).str(), test_type(powm(a1, b1, ui)).str());
389          BOOST_CHECK_EQUAL(mpz_int(powm(a, ui, c)).str(), test_type(powm(a1, ui, c1)).str());
390       }
391       BOOST_CHECK_EQUAL(lsb(a), lsb(a1));
392       BOOST_CHECK_EQUAL(msb(a), msb(a1));
393    }
394
395    static void test_bug_cases()
396    {
397       if (!std::numeric_limits<test_type>::is_bounded)
398       {
399          // https://svn.boost.org/trac/boost/ticket/7878
400          test_type a("0x1000000000000000000000000000000000000000000000000000000000000000");
401          test_type b = 0xFFFFFFFF;
402          test_type c = a * b + b; // quotient has 1 in the final place
403          test_type q, r;
404          divide_qr(c, b, q, r);
405          BOOST_CHECK_EQUAL(a + 1, q);
406          BOOST_CHECK_EQUAL(r, 0);
407
408          b = static_cast<test_type>("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
409          c = a * b + b; // quotient has 1 in the final place
410          divide_qr(c, b, q, r);
411          BOOST_CHECK_EQUAL(a + 1, q);
412          BOOST_CHECK_EQUAL(r, 0);
413          //
414          // Not a bug, but test some other special cases that don't otherwise occur through
415          // random testing:
416          //
417          c = a * b; // quotient has zero in the final place
418          divide_qr(c, b, q, r);
419          BOOST_CHECK_EQUAL(q, a);
420          BOOST_CHECK_EQUAL(r, 0);
421          divide_qr(c, a, q, r);
422          BOOST_CHECK_EQUAL(q, b);
423          BOOST_CHECK_EQUAL(r, 0);
424          ++c;
425          divide_qr(c, b, q, r);
426          BOOST_CHECK_EQUAL(q, a);
427          BOOST_CHECK_EQUAL(r, 1);
428       }
429       // Bug https://svn.boost.org/trac/boost/ticket/8126:
430       test_type a("-4294967296");
431       test_type b("4294967296");
432       test_type c("-1");
433       a = (a / b);
434       BOOST_CHECK_EQUAL(a, -1);
435       a = -4294967296;
436       a = (a / b) * c;
437       BOOST_CHECK_EQUAL(a, 1);
438       a = -23;
439       b = 23;
440       a = (a / b) * c;
441       BOOST_CHECK_EQUAL(a, 1);
442       a = -23;
443       a = (a / b) / c;
444       BOOST_CHECK_EQUAL(a, 1);
445       a = test_type("-26607734784073568386365259775");
446       b = test_type("8589934592");
447       a = a / b;
448       BOOST_CHECK_EQUAL(a, test_type("-3097548007973652377"));
449       // Bug https://svn.boost.org/trac/boost/ticket/8133:
450       a           = test_type("0x12345600012434ffffffffffffffffffffffff");
451       unsigned ui = 0xffffffff;
452       a           = a - ui;
453       BOOST_CHECK_EQUAL(a, test_type("0x12345600012434ffffffffffffffff00000000"));
454       a = test_type("0x12345600012434ffffffffffffffffffffffff");
455 #ifndef BOOST_NO_LONG_LONG
456       unsigned long long ull = 0xffffffffffffffffuLL;
457       a                      = a - ull;
458       BOOST_CHECK_EQUAL(a, test_type("0x12345600012434ffffffff0000000000000000"));
459 #endif
460       //
461       // Now check that things which should be zero really are
462       // https://svn.boost.org/trac/boost/ticket/8145:
463       //
464       a = -1;
465       a += 1;
466       BOOST_CHECK_EQUAL(a, 0);
467       a = 1;
468       a += -1;
469       BOOST_CHECK_EQUAL(a, 0);
470       a = -1;
471       a += test_type(1);
472       BOOST_CHECK_EQUAL(a, 0);
473       a = 1;
474       a += test_type(-1);
475       BOOST_CHECK_EQUAL(a, 0);
476       a = test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
477       a -= test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
478       BOOST_CHECK_EQUAL(a, 0);
479       a = -test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
480       a += test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
481       BOOST_CHECK_EQUAL(a, 0);
482       a = 2;
483       a *= 0;
484       BOOST_CHECK_EQUAL(a, 0);
485       a = -2;
486       a *= 0;
487       BOOST_CHECK_EQUAL(a, 0);
488       a = 2;
489       a *= test_type(0);
490       BOOST_CHECK_EQUAL(a, 0);
491       a = -2;
492       a *= test_type(0);
493       BOOST_CHECK_EQUAL(a, 0);
494       a = -2;
495       a /= 50;
496       BOOST_CHECK_EQUAL(a, 0);
497       a = -test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
498       a /= (1 + test_type("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
499       BOOST_CHECK_EQUAL(a, 0);
500       // https://svn.boost.org/trac/boost/ticket/8160
501       a = 1;
502       a = 0 / test_type(1);
503       BOOST_CHECK_EQUAL(a, 0);
504       a = 1;
505       a = 0 % test_type(25);
506       BOOST_CHECK_EQUAL(a, 0);
507 #ifndef TEST2
508       // https://svn.boost.org/trac/boost/ticket/11364
509       a           = 0xfffffffeu;
510       b           = -2;
511       c           = a ^ b;
512       test_type d = ~(a ^ ~b);
513       BOOST_CHECK_EQUAL(c, d);
514 #endif
515 #if defined(TEST2) || defined(TEST3)
516       // https://svn.boost.org/trac/boost/ticket/11648
517       a = (std::numeric_limits<test_type>::max)() - 69;
518       b = a / 139;
519       ++b;
520       c           = a / b;
521       test_type r = a % b;
522       BOOST_CHECK(r < b);
523       BOOST_CHECK_EQUAL(a - c * b, r);
524 #endif
525       for (ui = 0; ui < 1000; ++ui)
526       {
527          boost::multiprecision::mpz_int t;
528          boost::multiprecision::mpz_int s1 = sqrt(boost::multiprecision::mpz_int(ui), t);
529          a                                 = sqrt(test_type(ui), b);
530          BOOST_CHECK_EQUAL(a.str(), s1.str());
531          BOOST_CHECK_EQUAL(b.str(), t.str());
532       }
533       a = -1;
534       ++a;
535       BOOST_CHECK_EQUAL(a, 0);
536       ++--a;
537       BOOST_CHECK_EQUAL(a, 0);
538       --++a;
539       BOOST_CHECK_EQUAL(a, 0);
540
541       {
542          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<> >                                                                                            bigint;
543          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<64, 64, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> >   u64;
544          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<128, 128, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u128;
545          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u256;
546          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> >   s256;
547          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u160;
548          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<160, 160, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> >   s160;
549          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<512, 512, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void> > u512;
550          typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<512, 512, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> >   s512;
551
552          {
553             u256   a = 14;
554             bigint b = bigint("115792089237316195423570985008687907853269984665640564039457584007913129639948");
555             // to fix cast `a` to dev::bigint
556             BOOST_CHECK(a < b);
557          }
558          {
559             u256            a      = 1;
560             boost::uint64_t amount = 1;
561             u256            b      = a << amount;
562             BOOST_CHECK_EQUAL(b, 2);
563
564             u256 high_bit = u256(0);
565             bit_set(high_bit, 255);
566             BOOST_CHECK_EQUAL(a << 255, high_bit);
567             BOOST_CHECK_EQUAL(a << boost::uint64_t(256), 0);
568             BOOST_CHECK_EQUAL(a << 0, a);
569
570             u256 c = 3;
571             BOOST_CHECK_EQUAL(c, 3);
572             BOOST_CHECK_EQUAL(c << boost::uint64_t(256), 0);
573             BOOST_CHECK_EQUAL(c << 0, c);
574
575             // Bug workaround:
576             BOOST_CHECK_EQUAL(static_cast<u256>(bigint(u256(3)) << 255), u256(1) << 255);
577          }
578          {
579             BOOST_CHECK_EQUAL(u256(3) << 255, u256(1) << 255);
580
581             u256            a      = 1;
582             boost::uint64_t amount = 1;
583             u256            b      = a >> amount;
584             BOOST_CHECK_EQUAL(b, 0);
585             BOOST_CHECK_EQUAL(a >> 255, 0);
586             BOOST_CHECK_EQUAL(a >> boost::uint64_t(256), 0);
587             BOOST_CHECK_EQUAL(a >> boost::uint64_t(-1), 0);
588
589             u256 h;
590             bit_set(h, 255);
591             BOOST_CHECK_EQUAL(h >> 0, u256(1) << 255);
592             BOOST_CHECK_EQUAL(h >> 1, u256(1) << 254);
593             BOOST_CHECK_EQUAL(h >> 2, u256(1) << 253);
594             BOOST_CHECK_EQUAL(h >> 254, u256(1) << 1);
595             BOOST_CHECK_EQUAL(h >> 255, u256(1) << 0);
596             BOOST_CHECK_EQUAL(h >> 256, 0);
597             BOOST_CHECK_EQUAL(h >> boost::uint64_t(-1), 0);
598
599             u256 g;
600             bit_set(g, 255);
601             bit_set(g, 254);
602             BOOST_CHECK_EQUAL(g >> 255, 1);
603             BOOST_CHECK_EQUAL(g >> 254, 3);
604             BOOST_CHECK_EQUAL(g >> 253, 3 << 1);
605             BOOST_CHECK_EQUAL(g >> 252, 3 << 2);
606             BOOST_CHECK_EQUAL(g >> 251, 3 << 3);
607             BOOST_CHECK_EQUAL(g >> 0, u256(3) << 254);
608             BOOST_CHECK_EQUAL(g >> 1, u256(3) << 253);
609             BOOST_CHECK_EQUAL(g >> 2, u256(3) << 252);
610             BOOST_CHECK_EQUAL(g >> 3, u256(3) << 251);
611             BOOST_CHECK_EQUAL(g >> 100, u256(3) << 154);
612             BOOST_CHECK_EQUAL(g >> 256, 0);
613             BOOST_CHECK_EQUAL(g >> 257, 0);
614             BOOST_CHECK_EQUAL(g >> boost::uint32_t(-1), 0);
615             BOOST_CHECK_EQUAL(g >> boost::uint64_t(-1), 0);
616             BOOST_CHECK_EQUAL(g >> boost::uint16_t(-1), 0);
617             BOOST_CHECK_EQUAL(g >> (boost::uint16_t(-1) - 1), 0);
618          }
619          {
620             s256     a      = 1;
621             uint64_t amount = 1;
622             s256     b      = a >> amount;
623             BOOST_CHECK_EQUAL(b, 0);
624             BOOST_CHECK_EQUAL(a >> 255, 0);
625             BOOST_CHECK_EQUAL(a >> boost::uint64_t(256), 0);
626             BOOST_CHECK_EQUAL(a >> boost::uint64_t(-1), 0);
627
628             s256 n = -1;
629             BOOST_CHECK_EQUAL(n >> 0, n);
630             BOOST_CHECK_EQUAL(n >> 1, n);
631             BOOST_CHECK_EQUAL(n >> 2, n);
632             BOOST_CHECK_EQUAL(n >> 254, n);
633             BOOST_CHECK_EQUAL(n >> 255, n);
634             BOOST_CHECK_EQUAL(n >> 256, n);
635             BOOST_CHECK_EQUAL(n >> 257, n);
636             BOOST_CHECK_EQUAL(n >> ~boost::uint64_t(0), n);
637
638             // Test min value. This actually -(2^256-1), not -(2^255) as in C.
639             s256 h = (std::numeric_limits<s256>::min)();
640             BOOST_CHECK_LT(h, 0);
641             BOOST_CHECK_EQUAL(h >> 0, h);
642             BOOST_CHECK_EQUAL(h >> 256, -1);
643
644             // Test EVM min value.
645             s256 g = s256(-1) << 255;
646             BOOST_CHECK_LT(g, 0);
647             BOOST_CHECK_EQUAL(static_cast<u256>(g), u256(1) << 255);
648             BOOST_CHECK_EQUAL(g >> 0, g);
649             BOOST_CHECK_EQUAL(static_cast<u256>(g >> 1), u256(0b11) << 254);
650             BOOST_CHECK_EQUAL(static_cast<u256>(g >> 2), u256(0b111) << 253);
651             BOOST_CHECK_EQUAL(static_cast<u256>(g >> 3), u256(0b1111) << 252);
652
653             BOOST_CHECK_EQUAL(static_cast<u256>(g >> 255), ~u256(0));
654             BOOST_CHECK_EQUAL(static_cast<u256>(g >> 254), ~u256(0b1));
655             BOOST_CHECK_EQUAL(static_cast<u256>(g >> 253), ~u256(0b11));
656
657             // Test shifting more that one bit.
658             s256 k = s256(0b111) << 252;
659             BOOST_CHECK_EQUAL(k, u256(0b111) << 252);
660             BOOST_CHECK_EQUAL(k >> 1, u256(0b111) << 251);
661             BOOST_CHECK_EQUAL(k >> 2, u256(0b111) << 250);
662             BOOST_CHECK_EQUAL(k >> 252, 0b111);
663             BOOST_CHECK_EQUAL(k >> 253, 0b11);
664             BOOST_CHECK_EQUAL(k >> 254, 0b1);
665             BOOST_CHECK_EQUAL(k >> 255, 0);
666             BOOST_CHECK_EQUAL(k >> 256, 0);
667             BOOST_CHECK_EQUAL(k >> ~boost::uint32_t(0), 0);
668
669             // Division equivalence.
670
671             // Built-in type:
672             if (std::numeric_limits<boost::int64_t>::is_specialized)
673             {
674                boost::int64_t d = (std::numeric_limits<boost::int64_t>::min)();
675                BOOST_CHECK_EQUAL(d >> 1, d / 2);
676                int64_t e = d + 1;
677                BOOST_CHECK_EQUAL(e >> 1, e / 2 - 1);
678
679                // Boost type:
680                BOOST_CHECK_EQUAL(h >> 1, h / 2 - 1);
681             }
682          }
683       }
684    }
685
686    void test()
687    {
688       using namespace boost::multiprecision;
689
690       test_bug_cases();
691
692       last_error_count = 0;
693
694       BOOST_CHECK_EQUAL(Number(), 0);
695
696       for (int i = 0; i < 10000; ++i)
697       {
698          a = generate_random<mpz_int>(1000);
699          b = generate_random<mpz_int>(512);
700          c = generate_random<mpz_int>(256);
701          d = generate_random<mpz_int>(32);
702
703          si = d.convert_to<int>();
704          ui = si;
705
706          a1 = static_cast<test_type>(a.str());
707          b1 = static_cast<test_type>(b.str());
708          c1 = static_cast<test_type>(c.str());
709          d1 = static_cast<test_type>(d.str());
710
711          t1();
712          t2();
713 #ifndef SLOW_COMPILER
714          t3();
715          t4();
716          t5();
717 #endif
718
719          if (last_error_count != (unsigned)boost::detail::test_errors())
720          {
721             last_error_count = boost::detail::test_errors();
722             std::cout << std::hex << std::showbase;
723
724             std::cout << "a    = " << a << std::endl;
725             std::cout << "a1   = " << a1 << std::endl;
726             std::cout << "b    = " << b << std::endl;
727             std::cout << "b1   = " << b1 << std::endl;
728             std::cout << "c    = " << c << std::endl;
729             std::cout << "c1   = " << c1 << std::endl;
730             std::cout << "d    = " << d << std::endl;
731             std::cout << "d1   = " << d1 << std::endl;
732             std::cout << "a + b   = " << a + b << std::endl;
733             std::cout << "a1 + b1 = " << a1 + b1 << std::endl;
734             std::cout << std::dec;
735             std::cout << "a - b   = " << a - b << std::endl;
736             std::cout << "a1 - b1 = " << a1 - b1 << std::endl;
737             std::cout << "-a + b   = " << mpz_int(-a) + b << std::endl;
738             std::cout << "-a1 + b1 = " << test_type(-a1) + b1 << std::endl;
739             std::cout << "-a - b   = " << mpz_int(-a) - b << std::endl;
740             std::cout << "-a1 - b1 = " << test_type(-a1) - b1 << std::endl;
741             std::cout << "c*d    = " << c * d << std::endl;
742             std::cout << "c1*d1  = " << c1 * d1 << std::endl;
743             std::cout << "b*c    = " << b * c << std::endl;
744             std::cout << "b1*c1  = " << b1 * c1 << std::endl;
745             std::cout << "a/b    = " << a / b << std::endl;
746             std::cout << "a1/b1  = " << a1 / b1 << std::endl;
747             std::cout << "a/d    = " << a / d << std::endl;
748             std::cout << "a1/d1  = " << a1 / d1 << std::endl;
749             std::cout << "a%b    = " << a % b << std::endl;
750             std::cout << "a1%b1  = " << a1 % b1 << std::endl;
751             std::cout << "a%d    = " << a % d << std::endl;
752             std::cout << "a1%d1  = " << a1 % d1 << std::endl;
753          }
754
755          //
756          // Check to see if test is taking too long.
757          // Tests run on the compiler farm time out after 300 seconds,
758          // so don't get too close to that:
759          //
760 #ifndef CI_SUPPRESS_KNOWN_ISSUES
761          if (tim.elapsed() > 200)
762 #else
763          if (tim.elapsed() > 25)
764 #endif
765          {
766             std::cout << "Timeout reached, aborting tests now....\n";
767             break;
768          }
769       }
770    }
771 };
772
773 int main()
774 {
775    using namespace boost::multiprecision;
776
777 #ifdef TEST1
778    tester<cpp_int> t1;
779    t1.test();
780 #endif
781 #ifdef TEST2
782    tester<number<cpp_int_backend<2048, 2048, signed_magnitude, checked, void> > > t2;
783    t2.test();
784 #endif
785 #ifdef TEST3
786    // Unchecked test verifies modulo arithmetic:
787    tester<number<cpp_int_backend<2048, 2048, signed_magnitude, unchecked, void> > > t3;
788    t3.test();
789 #endif
790 #ifdef TEST4
791    tester<number<cpp_int_backend<0, 2048, signed_magnitude, unchecked, std::allocator<char> > > > t4;
792    t4.test();
793 #endif
794 #ifdef TEST5
795    tester<number<cpp_int_backend<0, 2048, signed_magnitude, unchecked> > > t5;
796    t5.test();
797 #endif
798    return boost::report_errors();
799 }