]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_cpp_rat_serial.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_cpp_rat_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_
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
29 template <class T>
30 T 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 if(!val)
61 val = 1;
62 return val;
63 }
64
65 template <class T>
66 void test_neg(const T& x, const boost::mpl::true_&)
67 {
68 T val = -x;
69 #ifndef BOOST_NO_EXCEPTIONS
70 try
71 {
72 #endif
73 std::stringstream ss;
74 boost::archive::text_oarchive oa(ss);
75 oa << static_cast<const T&>(val);
76 boost::archive::text_iarchive ia(ss);
77 T val2;
78 ia >> val2;
79 BOOST_CHECK_EQUAL(val, val2);
80
81 ss.clear();
82 boost::archive::binary_oarchive ob(ss);
83 ob << static_cast<const T&>(val);
84 boost::archive::binary_iarchive ib(ss);
85 ib >> val2;
86 BOOST_CHECK_EQUAL(val, val2);
87 #ifndef BOOST_NO_EXCEPTIONS
88 }
89 catch(const boost::exception& e)
90 {
91 std::cout << "Caught boost::exception with:\n";
92 std::cout << diagnostic_information(e);
93 }
94 catch(const std::exception& e)
95 {
96 std::cout << "Caught std::exception with:\n";
97 std::cout << e.what() << std::endl;
98 }
99 #endif
100 }
101 template <class T>
102 void test_neg(const T& , const boost::mpl::false_&){}
103
104 template <class T>
105 void test()
106 {
107 using namespace boost::multiprecision;
108
109 boost::random::mt19937 gen;
110 boost::uniform_int<> d(3, std::numeric_limits<T>::is_bounded ? std::numeric_limits<T>::digits : 3000);
111 boost::timer tim;
112
113 while(true)
114 {
115 T val(generate_random<typename component_type<T>::type>(d(gen)), generate_random<typename component_type<T>::type>(d(gen)));
116 #ifndef BOOST_NO_EXCEPTIONS
117 try
118 {
119 #endif
120 std::stringstream ss;
121 boost::archive::text_oarchive oa(ss);
122 oa << static_cast<const T&>(val);
123 boost::archive::text_iarchive ia(ss);
124 T val2;
125 ia >> val2;
126 BOOST_CHECK_EQUAL(val, val2);
127
128 ss.clear();
129 boost::archive::binary_oarchive ob(ss);
130 ob << static_cast<const T&>(val);
131 boost::archive::binary_iarchive ib(ss);
132 ib >> val2;
133 BOOST_CHECK_EQUAL(val, val2);
134 #ifndef BOOST_NO_EXCEPTIONS
135 }
136 catch(const boost::exception& e)
137 {
138 std::cout << "Caught boost::exception with:\n";
139 std::cout << diagnostic_information(e);
140 }
141 catch(const std::exception& e)
142 {
143 std::cout << "Caught std::exception with:\n";
144 std::cout << e.what() << std::endl;
145 }
146 #endif
147 test_neg(val, boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
148
149 //
150 // Check to see if test is taking too long.
151 // Tests run on the compiler farm time out after 300 seconds,
152 // so don't get too close to that:
153 //
154 if(tim.elapsed() > 150)
155 {
156 std::cout << "Timeout reached, aborting tests now....\n";
157 break;
158 }
159 }
160 }
161
162 int main()
163 {
164 using namespace boost::multiprecision;
165 test<cpp_rational>();
166 return boost::report_errors();
167 }
168
169
170