Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / test / test_checked_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 #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)())
40       --bits_per_r_val;
41
42    unsigned terms_needed = digits / bits_per_r_val + 1;
43
44    T val = 0;
45    for (unsigned i = 0; i < terms_needed; ++i)
46    {
47       val *= (gen.max)();
48       val += gen();
49    }
50    val %= max_val;
51    return val;
52 }
53
54 template <class Number>
55 void test_signed_overflow(Number a, Number b, const boost::mpl::true_&)
56 {
57    a = -a;
58    BOOST_CHECK_THROW(Number(a * b), std::overflow_error);
59    ++a;
60    BOOST_CHECK(Number(a * b) >= (std::numeric_limits<Number>::min)());
61 }
62 template <class Number>
63 void test_signed_overflow(Number a, Number b, const boost::mpl::false_&)
64 {
65 }
66
67 template <class Number>
68 void test()
69 {
70    using namespace boost::multiprecision;
71    typedef Number test_type;
72
73    if (std::numeric_limits<test_type>::is_bounded)
74    {
75       test_type val = (std::numeric_limits<test_type>::max)();
76 #ifndef BOOST_NO_EXCEPTIONS
77       BOOST_CHECK_THROW(++val, std::overflow_error);
78       val = (std::numeric_limits<test_type>::max)();
79       BOOST_CHECK_THROW(test_type(1 + val), std::overflow_error);
80       BOOST_CHECK_THROW(test_type(val + 1), std::overflow_error);
81       BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
82       val /= 2;
83       val += 1;
84       BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
85
86       if (std::numeric_limits<test_type>::is_signed)
87       {
88          val = (std::numeric_limits<test_type>::min)();
89          BOOST_CHECK_THROW(--val, std::overflow_error);
90          val = (std::numeric_limits<test_type>::min)();
91          BOOST_CHECK_THROW(test_type(val - 1), std::overflow_error);
92          BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
93          val /= 2;
94          val -= 1;
95          BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
96       }
97       else
98       {
99          val = (std::numeric_limits<test_type>::min)();
100          BOOST_CHECK_THROW(--val, std::range_error);
101          val = (std::numeric_limits<test_type>::min)();
102          BOOST_CHECK_THROW(test_type(val - 1), std::range_error);
103       }
104 #endif
105       //
106       // Test overflow in random values:
107       //
108       for (unsigned bits = 30; bits < std::numeric_limits<test_type>::digits; bits += 30)
109       {
110          for (unsigned i = 0; i < 100; ++i)
111          {
112             val            = static_cast<test_type>(generate_random<cpp_int>(bits));
113             test_type val2 = 1 + (std::numeric_limits<test_type>::max)() / val;
114             BOOST_CHECK_THROW(test_type(val2 * val), std::overflow_error);
115             test_signed_overflow(val2, val, boost::mpl::bool_<std::numeric_limits<test_type>::is_signed>());
116             --val2;
117             BOOST_CHECK(cpp_int(val2) * cpp_int(val) <= cpp_int((std::numeric_limits<test_type>::max)()));
118             BOOST_CHECK(val2 * val <= (std::numeric_limits<test_type>::max)());
119             val2 = (std::numeric_limits<test_type>::max)() - val;
120             ++val2;
121             BOOST_CHECK_THROW(test_type(val2 + val), std::overflow_error);
122             BOOST_CHECK((val2 - 1) + val == (std::numeric_limits<test_type>::max)());
123             if (std::numeric_limits<test_type>::is_signed)
124             {
125                val2 = (std::numeric_limits<test_type>::min)() + val;
126                --val2;
127                BOOST_CHECK_THROW(test_type(val2 - val), std::overflow_error);
128                ++val2;
129                BOOST_CHECK(val2 - val == (std::numeric_limits<test_type>::min)());
130             }
131             unsigned shift = std::numeric_limits<test_type>::digits - msb(val);
132             BOOST_CHECK_THROW((val << shift) > 0, std::overflow_error);
133          }
134       }
135    }
136
137 #ifndef BOOST_NO_EXCEPTIONS
138    if (std::numeric_limits<test_type>::is_signed)
139    {
140       test_type a = -1;
141       test_type b = 1;
142       BOOST_CHECK_THROW(test_type(a | b), std::range_error);
143       BOOST_CHECK_THROW(test_type(a & b), std::range_error);
144       BOOST_CHECK_THROW(test_type(a ^ b), std::range_error);
145    }
146    else
147    {
148       // Constructing from a negative value is not allowed:
149       BOOST_CHECK_THROW(test_type(-2), std::range_error);
150       BOOST_CHECK_THROW(test_type("-2"), std::range_error);
151    }
152    if (std::numeric_limits<test_type>::digits < std::numeric_limits<long long>::digits)
153    {
154       long long llm = (std::numeric_limits<long long>::max)();
155       test_type t;
156       BOOST_CHECK_THROW(t = llm, std::range_error);
157       BOOST_CHECK_THROW(t = static_cast<test_type>(llm), std::range_error);
158       unsigned long long ullm = (std::numeric_limits<unsigned long long>::max)();
159       BOOST_CHECK_THROW(t = ullm, std::range_error);
160       BOOST_CHECK_THROW(t = static_cast<test_type>(ullm), std::range_error);
161
162       static const checked_uint512_t big = (std::numeric_limits<checked_uint512_t>::max)();
163       BOOST_CHECK_THROW(t = static_cast<test_type>(big), std::range_error);
164    }
165    //
166    // String errors:
167    //
168    BOOST_CHECK_THROW(test_type("12A"), std::runtime_error);
169    BOOST_CHECK_THROW(test_type("0658"), std::runtime_error);
170
171    if (std::numeric_limits<test_type>::is_signed)
172    {
173       BOOST_CHECK_THROW(test_type(-2).str(0, std::ios_base::hex), std::runtime_error);
174       BOOST_CHECK_THROW(test_type(-2).str(0, std::ios_base::oct), std::runtime_error);
175    }
176 #endif
177 }
178
179 int main()
180 {
181    using namespace boost::multiprecision;
182
183    test<number<cpp_int_backend<0, 0, signed_magnitude, checked> > >();
184    test<checked_int512_t>();
185    test<checked_uint512_t>();
186    test<number<cpp_int_backend<32, 32, signed_magnitude, checked, void> > >();
187    test<number<cpp_int_backend<32, 32, unsigned_magnitude, checked, void> > >();
188
189    //
190    // We also need to test type with "odd" bit counts in order to ensure full code coverage:
191    //
192    test<number<cpp_int_backend<528, 528, signed_magnitude, checked, void> > >();
193    test<number<cpp_int_backend<528, 528, unsigned_magnitude, checked, void> > >();
194    test<number<cpp_int_backend<48, 48, signed_magnitude, checked, void> > >();
195    test<number<cpp_int_backend<48, 48, unsigned_magnitude, checked, void> > >();
196    return boost::report_errors();
197 }