]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////// |
2 | // Copyright 2012 John Maddock. Distributed under the Boost | |
3 | // Software License, Version 1.0. (See accompanying file | |
92f5a8d4 | 4 | // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt |
7c673cae FG |
5 | |
6 | // | |
7 | // Compare arithmetic results using fixed_int to GMP results. | |
8 | // | |
9 | ||
10 | #ifdef _MSC_VER | |
92f5a8d4 | 11 | #define _SCL_SECURE_NO_WARNINGS |
7c673cae FG |
12 | #endif |
13 | ||
14 | #include <boost/multiprecision/integer.hpp> | |
15 | #include "test.hpp" | |
16 | ||
17 | #ifdef BOOST_MSVC | |
92f5a8d4 | 18 | #pragma warning(disable : 4146) |
7c673cae FG |
19 | #endif |
20 | ||
21 | template <class I, class H> | |
22 | void test() | |
23 | { | |
24 | using namespace boost::multiprecision; | |
25 | ||
26 | I i(0); | |
27 | ||
28 | #ifndef BOOST_NO_EXCEPTIONS | |
29 | BOOST_CHECK_THROW(lsb(i), std::range_error); | |
30 | #endif | |
31 | BOOST_CHECK(bit_test(bit_set(i, 0), 0)); | |
32 | BOOST_CHECK_EQUAL(bit_set(i, 0), 1); | |
33 | BOOST_CHECK_EQUAL(bit_unset(i, 0), 0); | |
34 | BOOST_CHECK_EQUAL(bit_flip(bit_set(i, 0), 0), 0); | |
35 | ||
36 | unsigned max_index = (std::numeric_limits<I>::digits) - 1; | |
37 | BOOST_CHECK(bit_test(bit_set(i, max_index), max_index)); | |
38 | BOOST_CHECK_EQUAL(bit_unset(i, max_index), 0); | |
39 | BOOST_CHECK_EQUAL(bit_flip(bit_set(i, max_index), max_index), 0); | |
40 | i = 0; | |
41 | bit_set(i, max_index); | |
42 | BOOST_CHECK_EQUAL(lsb(i), max_index); | |
43 | BOOST_CHECK_EQUAL(msb(i), max_index); | |
44 | bit_set(i, max_index / 2); | |
45 | BOOST_CHECK_EQUAL(lsb(i), max_index / 2); | |
46 | BOOST_CHECK_EQUAL(msb(i), max_index); | |
47 | ||
48 | #ifndef BOOST_NO_EXCEPTIONS | |
92f5a8d4 | 49 | if (std::numeric_limits<I>::is_signed) |
7c673cae FG |
50 | { |
51 | i = static_cast<I>(-1); | |
52 | BOOST_CHECK_THROW(lsb(i), std::range_error); | |
53 | } | |
54 | #endif | |
55 | H mx = (std::numeric_limits<H>::max)(); | |
56 | ||
57 | BOOST_CHECK_EQUAL(multiply(i, mx, mx), static_cast<I>(mx) * static_cast<I>(mx)); | |
58 | BOOST_CHECK_EQUAL(add(i, mx, mx), static_cast<I>(mx) + static_cast<I>(mx)); | |
92f5a8d4 | 59 | if (std::numeric_limits<I>::is_signed) |
7c673cae FG |
60 | { |
61 | BOOST_CHECK_EQUAL(subtract(i, mx, static_cast<H>(-mx)), static_cast<I>(mx) - static_cast<I>(-mx)); | |
62 | BOOST_CHECK_EQUAL(add(i, static_cast<H>(-mx), static_cast<H>(-mx)), static_cast<I>(-mx) + static_cast<I>(-mx)); | |
63 | } | |
64 | ||
92f5a8d4 | 65 | i = (std::numeric_limits<I>::max)(); |
7c673cae FG |
66 | I j = 12345; |
67 | I r, q; | |
68 | divide_qr(i, j, q, r); | |
69 | BOOST_CHECK_EQUAL(q, i / j); | |
70 | BOOST_CHECK_EQUAL(r, i % j); | |
71 | BOOST_CHECK_EQUAL(integer_modulus(i, j), i % j); | |
72 | I p = 456; | |
73 | BOOST_CHECK_EQUAL(powm(i, p, j), pow(cpp_int(i), static_cast<unsigned>(p)) % j); | |
74 | ||
92f5a8d4 | 75 | for (I i = 0; i < (2 < 8) - 1; ++i) |
7c673cae FG |
76 | { |
77 | I j = i * i; | |
78 | I s, r; | |
79 | s = sqrt(j, r); | |
80 | BOOST_CHECK_EQUAL(s, i); | |
81 | BOOST_CHECK(r == 0); | |
82 | j += 3; | |
83 | s = sqrt(i, r); | |
84 | BOOST_CHECK_EQUAL(s, i); | |
85 | BOOST_CHECK(r == 3); | |
86 | } | |
87 | } | |
88 | ||
89 | int main() | |
90 | { | |
91 | using namespace boost::multiprecision; | |
92 | ||
93 | test<boost::int16_t, boost::int8_t>(); | |
94 | test<boost::int32_t, boost::int16_t>(); | |
95 | test<boost::int64_t, boost::int32_t>(); | |
96 | test<boost::uint16_t, boost::uint8_t>(); | |
97 | test<boost::uint32_t, boost::uint16_t>(); | |
98 | test<boost::uint64_t, boost::uint32_t>(); | |
92f5a8d4 | 99 | |
7c673cae FG |
100 | return boost::report_errors(); |
101 | } |