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
49 {
50 typedef T type;
51 };
52
53 #ifdef TEST_CPP_INT
54 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>
55 struct unchecked_type<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>, ExpressionTemplates> >
56 {
57 typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<MinBits, MaxBits, SignType, boost::multiprecision::unchecked, Allocator>, ExpressionTemplates> type;
58 };
59 #endif
60
61 template <class T>
generate_random()62 T generate_random()
63 {
64 typedef typename unchecked_type<T>::type unchecked_T;
65
66 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;
67
68 static boost::random::uniform_int_distribution<unsigned> ui(0, limbs);
69 static boost::random::mt19937 gen;
70 unchecked_T val = gen();
71 unsigned lim = ui(gen);
72 for (unsigned i = 0; i < lim; ++i)
73 {
74 val *= (gen.max)();
75 val += gen();
76 }
77 return val;
78 }
79
80 template <class T>
do_round_trip(const T & val,std::ios_base::fmtflags f)81 void do_round_trip(const T& val, std::ios_base::fmtflags f)
82 {
83 std::stringstream ss;
84 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
85 ss << std::setprecision(std::numeric_limits<T>::max_digits10);
86 #else
87 ss << std::setprecision(std::numeric_limits<T>::digits10 + 5);
88 #endif
89 ss.flags(f);
90 ss << val;
91 T new_val = static_cast<T>(ss.str());
92 BOOST_CHECK_EQUAL(new_val, val);
93 new_val = static_cast<T>(val.str(0, f));
94 BOOST_CHECK_EQUAL(new_val, val);
95 ss >> new_val;
96 BOOST_CHECK_EQUAL(new_val, val);
97 }
98
99 template <class T>
do_round_trip(const T & val)100 void do_round_trip(const T& val)
101 {
102 do_round_trip(val, std::ios_base::fmtflags(0));
103 if (val >= 0)
104 {
105 do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase | std::ios_base::hex));
106 do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase | std::ios_base::oct));
107 }
108 }
109
110 template <class T>
negative_round_trip(T val,const boost::mpl::true_ &)111 void negative_round_trip(T val, const boost::mpl::true_&)
112 {
113 do_round_trip(T(-val));
114 }
115 template <class T>
negative_round_trip(T,const boost::mpl::false_ &)116 void negative_round_trip(T, const boost::mpl::false_&)
117 {
118 }
119
120 template <class T>
negative_spots(const boost::mpl::true_ &)121 void negative_spots(const boost::mpl::true_&)
122 {
123 BOOST_CHECK_EQUAL(T(-1002).str(), "-1002");
124 if (!std::numeric_limits<T>::is_modulo)
125 {
126 #ifndef BOOST_NO_EXCEPTIONS
127 BOOST_CHECK_THROW(T(-2).str(0, std::ios_base::oct), std::runtime_error);
128 BOOST_CHECK_THROW(T(-2).str(0, std::ios_base::hex), std::runtime_error);
129 #endif
130 }
131 }
132 template <class T>
negative_spots(const boost::mpl::false_ &)133 void negative_spots(const boost::mpl::false_&)
134 {
135 }
136
137 template <class T>
test_round_trip()138 void test_round_trip()
139 {
140 for (unsigned i = 0; i < 1000; ++i)
141 {
142 T val = generate_random<T>();
143 do_round_trip(val);
144 negative_round_trip(val, boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
145 }
146
147 BOOST_CHECK_EQUAL(T(1002).str(), "1002");
148 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::showpos), "+1002");
149 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::oct), "1752");
150 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::oct | std::ios_base::showbase), "01752");
151 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::hex), "3ea");
152 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::hex | std::ios_base::showbase), "0x3ea");
153 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::hex | std::ios_base::uppercase), "3EA");
154 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::hex | std::ios_base::showbase | std::ios_base::uppercase), "0X3EA");
155 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::dec), "1002");
156 BOOST_CHECK_EQUAL(T(1002).str(0, std::ios_base::dec | std::ios_base::showbase), "1002");
157
158 negative_spots<T>(boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
159 }
160
main()161 int main()
162 {
163 #ifdef TEST_MPZ
164 test_round_trip<boost::multiprecision::mpz_int>();
165 #endif
166 #ifdef TEST_TOMMATH
167 test_round_trip<boost::multiprecision::tom_int>();
168 #endif
169 #ifdef TEST_CPP_INT
170 test_round_trip<boost::multiprecision::cpp_int>();
171 test_round_trip<boost::multiprecision::checked_int1024_t>();
172 test_round_trip<boost::multiprecision::checked_uint512_t>();
173 test_round_trip<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<32, 32, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void> > >();
174 test_round_trip<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<32, 32, boost::multiprecision::unsigned_magnitude, boost::multiprecision::checked, void> > >();
175 #endif
176 return boost::report_errors();
177 }
178