]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/multiprecision/test/test_cpp_int_serial.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_cpp_int_serial.cpp
CommitLineData
7c673cae
FG
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_
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 <boost/random/mersenne_twister.hpp>
16#include <boost/random/uniform_int.hpp>
17#include <boost/timer.hpp>
18#include "test.hpp"
19
20#include <iostream>
21#include <iomanip>
22#include <sstream>
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>
28
29template <class T>
30T generate_random(unsigned bits_wanted)
31{
32 static boost::random::mt19937 gen;
33 typedef boost::random::mt19937::result_type random_type;
34
35 T max_val;
36 unsigned digits;
37 if(std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
38 {
39 max_val = (std::numeric_limits<T>::max)();
40 digits = std::numeric_limits<T>::digits;
41 }
42 else
43 {
44 max_val = T(1) << bits_wanted;
45 digits = bits_wanted;
46 }
47
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;
50
51 unsigned terms_needed = digits / bits_per_r_val + 1;
52
53 T val = 0;
54 for(unsigned i = 0; i < terms_needed; ++i)
55 {
56 val *= (gen.max)();
57 val += gen();
58 }
59 val %= max_val;
60 return val;
61}
62
63template <class T>
64void test_neg(const T& x, const boost::mpl::true_&)
65{
66 T val = -x;
67#ifndef BOOST_NO_EXCEPTIONS
68 try{
69#endif
70 std::stringstream ss;
71 boost::archive::text_oarchive oa(ss);
72 oa << static_cast<const T&>(val);
73 boost::archive::text_iarchive ia(ss);
74 T val2;
75 ia >> val2;
76 BOOST_CHECK_EQUAL(val, val2);
77
78 ss.clear();
79 boost::archive::binary_oarchive ob(ss);
80 ob << static_cast<const T&>(val);
81 boost::archive::binary_iarchive ib(ss);
82 ib >> val2;
83 BOOST_CHECK_EQUAL(val, val2);
84#ifndef BOOST_NO_EXCEPTIONS
85 }
86 catch(const boost::exception& e)
87 {
88 std::cout << "Caught boost::exception with:\n";
89 std::cout << diagnostic_information(e);
90 }
91 catch(const std::exception& e)
92 {
93 std::cout << "Caught std::exception with:\n";
94 std::cout << e.what() << std::endl;
95 }
96#endif
97}
98template <class T>
99void test_neg(const T& , const boost::mpl::false_&){}
100
101template <class T>
102void test()
103{
104 using namespace boost::multiprecision;
105
106 boost::random::mt19937 gen;
107 boost::uniform_int<> d(3, std::numeric_limits<T>::is_bounded ? std::numeric_limits<T>::digits : 3000);
108 boost::timer tim;
109
110 while(true)
111 {
112 T val = generate_random<T>(d(gen));
113#ifndef BOOST_NO_EXCEPTIONS
114 try
115 {
116#endif
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);
121 T val2;
122 ia >> val2;
123 BOOST_CHECK_EQUAL(val, val2);
124
125 ss.clear();
126 boost::archive::binary_oarchive ob(ss);
127 ob << static_cast<const T&>(val);
128 boost::archive::binary_iarchive ib(ss);
129 ib >> val2;
130 BOOST_CHECK_EQUAL(val, val2);
131#ifndef BOOST_NO_EXCEPTIONS
132 }
133 catch(const boost::exception& e)
134 {
135 std::cout << "Caught boost::exception with:\n";
136 std::cout << diagnostic_information(e);
137 }
138 catch(const std::exception& e)
139 {
140 std::cout << "Caught std::exception with:\n";
141 std::cout << e.what() << std::endl;
142 }
143#endif
144 test_neg(val, boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
145 //
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:
149 //
b32b8144 150#ifndef CI_SUPPRESS_KNOWN_ISSUES
7c673cae 151 if(tim.elapsed() > 150)
b32b8144
FG
152#else
153 if(tim.elapsed() > 25)
154#endif
7c673cae
FG
155 {
156 std::cout << "Timeout reached, aborting tests now....\n";
157 break;
158 }
159 }
160}
161
162#if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
163# define TEST1
164# define TEST2
165# define TEST3
166# define TEST4
167#endif
168
169int main()
170{
171 using namespace boost::multiprecision;
172#ifdef TEST1
173 test<cpp_int>();
174#endif
175#ifdef TEST2
176 test<number<cpp_int_backend<61, 61, unsigned_magnitude, unchecked, void> > >();
177#endif
178#ifdef TEST3
179 test<number<cpp_int_backend<120, 120, signed_magnitude, unchecked, void> > >();
180#endif
181#ifdef TEST4
182 test<int1024_t>();
183#endif
184 return boost::report_errors();
185}
186
187
188