]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_cpp_int_serial.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
7 // Compare arithmetic results using fixed_int to GMP results.
11 #define _SCL_SECURE_NO_WARNINGS
14 #include <boost/multiprecision/cpp_int.hpp>
15 #include <boost/random/mersenne_twister.hpp>
16 #include <boost/random/uniform_int.hpp>
23 #include <boost/archive/text_iarchive.hpp>
24 #include <boost/archive/text_oarchive.hpp>
25 #include <boost/archive/binary_iarchive.hpp>
26 #include <boost/archive/xml_iarchive.hpp>
27 #include <boost/archive/binary_oarchive.hpp>
28 #include <boost/archive/xml_oarchive.hpp>
29 #include <boost/exception/all.hpp>
32 T
generate_random(unsigned bits_wanted
)
34 static boost::random::mt19937 gen
;
35 typedef boost::random::mt19937::result_type random_type
;
39 if (std::numeric_limits
<T
>::is_bounded
&& (bits_wanted
== (unsigned)std::numeric_limits
<T
>::digits
))
41 max_val
= (std::numeric_limits
<T
>::max
)();
42 digits
= std::numeric_limits
<T
>::digits
;
46 max_val
= T(1) << bits_wanted
;
50 unsigned bits_per_r_val
= std::numeric_limits
<random_type
>::digits
- 1;
51 while ((random_type(1) << bits_per_r_val
) > (gen
.max
)())
54 unsigned terms_needed
= digits
/ bits_per_r_val
+ 1;
57 for (unsigned i
= 0; i
< terms_needed
; ++i
)
67 void test_neg(const T
& x
, const std::integral_constant
<bool, true>&)
70 #ifndef BOOST_NO_EXCEPTIONS
75 boost::archive::text_oarchive
oa(ss
);
76 oa
<< static_cast<const T
&>(val
);
77 boost::archive::text_iarchive
ia(ss
);
80 BOOST_CHECK_EQUAL(val
, val2
);
83 boost::archive::binary_oarchive
ob(ss
);
84 ob
<< static_cast<const T
&>(val
);
85 boost::archive::binary_iarchive
ib(ss
);
87 BOOST_CHECK_EQUAL(val
, val2
);
88 #ifndef BOOST_NO_EXCEPTIONS
90 catch (const boost::exception
& e
)
92 std::cout
<< "Caught boost::exception with:\n";
93 std::cout
<< diagnostic_information(e
);
95 catch (const std::exception
& e
)
97 std::cout
<< "Caught std::exception with:\n";
98 std::cout
<< e
.what() << std::endl
;
103 void test_neg(const T
&, const std::integral_constant
<bool, false>&) {}
108 using namespace boost::multiprecision
;
110 boost::random::mt19937 gen
;
111 boost::uniform_int
<> d(3, std::numeric_limits
<T
>::is_bounded
? std::numeric_limits
<T
>::digits
: 3000);
116 T val
= generate_random
<T
>(d(gen
));
117 #ifndef BOOST_NO_EXCEPTIONS
123 std::stringstream ss
;
124 boost::archive::text_oarchive
oa(ss
);
125 oa
<< static_cast<const T
&>(val
);
126 boost::archive::text_iarchive
ia(ss
);
128 BOOST_CHECK_EQUAL(val
, val2
);
131 std::stringstream ss
;
132 boost::archive::binary_oarchive
ob(ss
);
133 ob
<< static_cast<const T
&>(val
);
134 boost::archive::binary_iarchive
ib(ss
);
136 BOOST_CHECK_EQUAL(val
, val2
);
139 std::stringstream ss
;
141 boost::archive::xml_oarchive
oc(ss
);
142 oc
<< boost::serialization::make_nvp("value", static_cast<const T
&>(val
));
144 boost::archive::xml_iarchive
ic(ss
);
145 ic
>> boost::serialization::make_nvp("value", val2
);
146 BOOST_CHECK_EQUAL(val
, val2
);
149 #ifndef BOOST_NO_EXCEPTIONS
151 catch (const boost::exception
& e
)
153 std::cout
<< "Caught boost::exception with:\n";
154 std::cout
<< diagnostic_information(e
);
156 catch (const std::exception
& e
)
158 std::cout
<< "Caught std::exception with:\n";
159 std::cout
<< e
.what() << std::endl
;
162 test_neg(val
, std::integral_constant
<bool, std::numeric_limits
<T
>::is_signed
>());
164 // Check to see if test is taking too long.
165 // Tests run on the compiler farm time out after 300 seconds,
166 // so don't get too close to that:
168 #ifndef CI_SUPPRESS_KNOWN_ISSUES
169 if (tim
.elapsed() > 150)
171 if (tim
.elapsed() > 25)
174 std::cout
<< "Timeout reached, aborting tests now....\n";
180 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
189 using namespace boost::multiprecision
;
194 test
<number
<cpp_int_backend
<61, 61, unsigned_magnitude
, unchecked
, void> > >();
197 test
<number
<cpp_int_backend
<120, 120, signed_magnitude
, unchecked
, void> > >();
202 return boost::report_errors();