]>
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 http://www.boost.org/LICENSE_1_
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>
17 #include <boost/timer.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/binary_oarchive.hpp>
27 #include <boost/exception/all.hpp>
30 T
generate_random(unsigned bits_wanted
)
32 static boost::random::mt19937 gen
;
33 typedef boost::random::mt19937::result_type random_type
;
37 if(std::numeric_limits
<T
>::is_bounded
&& (bits_wanted
== (unsigned)std::numeric_limits
<T
>::digits
))
39 max_val
= (std::numeric_limits
<T
>::max
)();
40 digits
= std::numeric_limits
<T
>::digits
;
44 max_val
= T(1) << bits_wanted
;
48 unsigned bits_per_r_val
= std::numeric_limits
<random_type
>::digits
- 1;
49 while((random_type(1) << bits_per_r_val
) > (gen
.max
)()) --bits_per_r_val
;
51 unsigned terms_needed
= digits
/ bits_per_r_val
+ 1;
54 for(unsigned i
= 0; i
< terms_needed
; ++i
)
64 void test_neg(const T
& x
, const boost::mpl::true_
&)
67 #ifndef BOOST_NO_EXCEPTIONS
71 boost::archive::text_oarchive
oa(ss
);
72 oa
<< static_cast<const T
&>(val
);
73 boost::archive::text_iarchive
ia(ss
);
76 BOOST_CHECK_EQUAL(val
, val2
);
79 boost::archive::binary_oarchive
ob(ss
);
80 ob
<< static_cast<const T
&>(val
);
81 boost::archive::binary_iarchive
ib(ss
);
83 BOOST_CHECK_EQUAL(val
, val2
);
84 #ifndef BOOST_NO_EXCEPTIONS
86 catch(const boost::exception
& e
)
88 std::cout
<< "Caught boost::exception with:\n";
89 std::cout
<< diagnostic_information(e
);
91 catch(const std::exception
& e
)
93 std::cout
<< "Caught std::exception with:\n";
94 std::cout
<< e
.what() << std::endl
;
99 void test_neg(const T
& , const boost::mpl::false_
&){}
104 using namespace boost::multiprecision
;
106 boost::random::mt19937 gen
;
107 boost::uniform_int
<> d(3, std::numeric_limits
<T
>::is_bounded
? std::numeric_limits
<T
>::digits
: 3000);
112 T val
= generate_random
<T
>(d(gen
));
113 #ifndef BOOST_NO_EXCEPTIONS
117 std::stringstream ss
;
118 boost::archive::text_oarchive
oa(ss
);
119 oa
<< static_cast<const T
&>(val
);
120 boost::archive::text_iarchive
ia(ss
);
123 BOOST_CHECK_EQUAL(val
, val2
);
126 boost::archive::binary_oarchive
ob(ss
);
127 ob
<< static_cast<const T
&>(val
);
128 boost::archive::binary_iarchive
ib(ss
);
130 BOOST_CHECK_EQUAL(val
, val2
);
131 #ifndef BOOST_NO_EXCEPTIONS
133 catch(const boost::exception
& e
)
135 std::cout
<< "Caught boost::exception with:\n";
136 std::cout
<< diagnostic_information(e
);
138 catch(const std::exception
& e
)
140 std::cout
<< "Caught std::exception with:\n";
141 std::cout
<< e
.what() << std::endl
;
144 test_neg(val
, boost::mpl::bool_
<std::numeric_limits
<T
>::is_signed
>());
146 // Check to see if test is taking too long.
147 // Tests run on the compiler farm time out after 300 seconds,
148 // so don't get too close to that:
150 if(tim
.elapsed() > 150)
152 std::cout
<< "Timeout reached, aborting tests now....\n";
158 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
167 using namespace boost::multiprecision
;
172 test
<number
<cpp_int_backend
<61, 61, unsigned_magnitude
, unchecked
, void> > >();
175 test
<number
<cpp_int_backend
<120, 120, signed_magnitude
, unchecked
, void> > >();
180 return boost::report_errors();