]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_int_io.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_int_io.cpp
1 // Copyright John Maddock 2011.
2
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifdef _MSC_VER
9 # define _SCL_SECURE_NO_WARNINGS
10 #endif
11
12 #if !defined(TEST_MPZ) && !defined(TEST_TOMMATH) && !defined(TEST_CPP_INT)
13 # define TEST_TOMMATH
14 # define TEST_MPZ
15 # define TEST_CPP_INT
16
17 #ifdef _MSC_VER
18 #pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!")
19 #endif
20 #ifdef __GNUC__
21 #pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!"
22 #endif
23
24 #endif
25
26 #if defined(TEST_MPZ)
27 #include <boost/multiprecision/gmp.hpp>
28 #endif
29 #if defined(TEST_TOMMATH)
30 #include <boost/multiprecision/tommath.hpp>
31 #endif
32 #ifdef TEST_CPP_INT
33 #include <boost/multiprecision/cpp_int.hpp>
34 #endif
35
36 #include <boost/algorithm/string/case_conv.hpp>
37 #include <boost/random/mersenne_twister.hpp>
38 #include <boost/random/uniform_int.hpp>
39 #include "test.hpp"
40 #include <iostream>
41 #include <iomanip>
42
43 #ifdef BOOST_MSVC
44 #pragma warning(disable:4127)
45 #endif
46
47 template <class T>
48 struct unchecked_type{ typedef T type; };
49
50 #ifdef TEST_CPP_INT
51 template <unsigned MinBits, unsigned MaxBits, boost::multiprecision::cpp_integer_type SignType, boost::multiprecision::cpp_int_check_type Checked, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
52 struct unchecked_type<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >
53 {
54 typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, boost::multiprecision::unchecked, Allocator>, ExpressionTemplates> type;
55 };
56 #endif
57
58 template <class T>
59 T generate_random()
60 {
61 typedef typename unchecked_type<T>::type unchecked_T;
62
63 static const unsigned limbs = std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::is_bounded ? std::numeric_limits<T>::digits / std::numeric_limits<unsigned>::digits + 3 : 20;
64
65 static boost::random::uniform_int_distribution<unsigned> ui(0, limbs);
66 static boost::random::mt19937 gen;
67 unchecked_T val = gen();
68 unsigned lim = ui(gen);
69 for(unsigned i = 0; i < lim; ++i)
70 {
71 val *= (gen.max)();
72 val += gen();
73 }
74 return val;
75 }
76
77 template <class T>
78 void do_round_trip(const T& val, std::ios_base::fmtflags f)
79 {
80 std::stringstream ss;
81 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
82 ss << std::setprecision(std::numeric_limits<T>::max_digits10);
83 #else
84 ss << std::setprecision(std::numeric_limits<T>::digits10 + 5);
85 #endif
86 ss.flags(f);
87 ss << val;
88 T new_val = static_cast<T>(ss.str());
89 BOOST_CHECK_EQUAL(new_val, val);
90 new_val = static_cast<T>(val.str(0, f));
91 BOOST_CHECK_EQUAL(new_val, val);
92 ss >> new_val;
93 BOOST_CHECK_EQUAL(new_val, val);
94 }
95
96 template <class T>
97 void do_round_trip(const T& val)
98 {
99 do_round_trip(val, std::ios_base::fmtflags(0));
100 if(val >= 0)
101 {
102 do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase|std::ios_base::hex));
103 do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase|std::ios_base::oct));
104 }
105 }
106
107 template <class T>
108 void negative_round_trip(T val, const boost::mpl::true_&)
109 {
110 do_round_trip(T(-val));
111 }
112 template <class T>
113 void negative_round_trip(T, const boost::mpl::false_&)
114 {
115 }
116
117 template <class T>
118 void negative_spots(const boost::mpl::true_&)
119 {
120 BOOST_CHECK_EQUAL(T(-1002).str(), "-1002");
121 if(!std::numeric_limits<T>::is_modulo)
122 {
123 #ifndef BOOST_NO_EXCEPTIONS
124 BOOST_CHECK_THROW(T(-2).str(0, std::ios_base::oct), std::runtime_error);
125 BOOST_CHECK_THROW(T(-2).str(0, std::ios_base::hex), std::runtime_error);
126 #endif
127 }
128 }
129 template <class T>
130 void negative_spots(const boost::mpl::false_&)
131 {
132 }
133
134 template <class T>
135 void test_round_trip()
136 {
137 for(unsigned i = 0; i < 1000; ++i)
138 {
139 T val = generate_random<T>();
140 do_round_trip(val);
141 negative_round_trip(val, boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
142 }
143
144 BOOST_CHECK_EQUAL(T(1002).str(), "1002");
145 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::showpos), "+1002");
146 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::oct), "1752");
147 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::oct|std::ios_base::showbase), "01752");
148 BOOST_CHECK_EQUAL(boost::to_lower_copy(T(1002).str(0, std::ios_base::hex)), "3ea");
149 BOOST_CHECK_EQUAL(boost::to_lower_copy(T(1002).str(0, std::ios_base::hex|std::ios_base::showbase)), "0x3ea");
150 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::dec), "1002");
151 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::dec|std::ios_base::showbase), "1002");
152
153 negative_spots<T>(boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
154 }
155
156 int main()
157 {
158 #ifdef TEST_MPZ
159 test_round_trip<boost::multiprecision::mpz_int>();
160 #endif
161 #ifdef TEST_TOMMATH
162 test_round_trip<boost::multiprecision::tom_int>();
163 #endif
164 #ifdef TEST_CPP_INT
165 test_round_trip<boost::multiprecision::cpp_int>();
166 test_round_trip<boost::multiprecision::checked_int1024_t>();
167 test_round_trip<boost::multiprecision::checked_uint512_t >();
168 test_round_trip<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<32, 32, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void> > >();
169 test_round_trip<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<32, 32, boost::multiprecision::unsigned_magnitude, boost::multiprecision::checked, void> > >();
170 #endif
171 return boost::report_errors();
172 }
173