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 https://www.boost.org/LICENSE_1_0.txt
5
6 #ifdef _MSC_VER
7 #define _SCL_SECURE_NO_WARNINGS
8 #endif
9
10 #ifdef HAS_TOMMATH
11
12 #include <boost/multiprecision/cpp_int.hpp>
13 #include <boost/random/mersenne_twister.hpp>
14 #include <boost/multiprecision/tommath.hpp>
15 #include <boost/multiprecision/cpp_bin_float.hpp>
16 #include <boost/multiprecision/cpp_dec_float.hpp>
17 #include "test.hpp"
18
19 #if defined(HAS_GMP)
20 #include <boost/multiprecision/gmp.hpp>
21 #endif
22 #if defined(HAS_MPFR)
23 #include <boost/multiprecision/mpfr.hpp>
24 #endif
25 #if defined(HAS_MPFI)
26 #include <boost/multiprecision/mpfi.hpp>
27 #endif
28 #ifdef HAS_FLOAT128
29 #include <boost/multiprecision/float128.hpp>
30 #endif
31
32 using namespace boost::multiprecision;
33
34 #ifdef BOOST_MSVC
35 #pragma warning(disable : 4127)
36 #endif
37
38 template <class T>
generate_random(unsigned bits_wanted)39 T generate_random(unsigned bits_wanted)
40 {
41 static boost::random::mt19937 gen;
42 typedef boost::random::mt19937::result_type random_type;
43
44 T max_val;
45 unsigned digits;
46 if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
47 {
48 max_val = (std::numeric_limits<T>::max)();
49 digits = std::numeric_limits<T>::digits;
50 }
51 else
52 {
53 max_val = T(1) << bits_wanted;
54 digits = bits_wanted;
55 }
56
57 unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
58 while ((random_type(1) << bits_per_r_val) > (gen.max)())
59 --bits_per_r_val;
60
61 unsigned terms_needed = digits / bits_per_r_val + 1;
62
63 T val = 0;
64 for (unsigned i = 0; i < terms_needed; ++i)
65 {
66 val *= (gen.max)();
67 val += gen();
68 }
69 val %= max_val;
70 return val;
71 }
72
73 template <class From, class To>
test_convert_neg_int(From from,const boost::mpl::true_ &)74 void test_convert_neg_int(From from, const boost::mpl::true_&)
75 {
76 from = -from;
77 To t3(from);
78 To t4 = from.template convert_to<To>();
79 BOOST_CHECK_EQUAL(from.str(), t3.str());
80 BOOST_CHECK_EQUAL(from.str(), t4.str());
81 }
82 template <class From, class To>
test_convert_neg_int(From const &,const boost::mpl::false_ &)83 void test_convert_neg_int(From const&, const boost::mpl::false_&)
84 {
85 }
86
87 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_integer> const &,boost::mpl::int_<number_kind_integer> const &)88 void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_integer> const&)
89 {
90 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
91
92 for (unsigned i = 0; i < 100; ++i)
93 {
94 From from = generate_random<From>(bits_wanted);
95 To t1(from);
96 To t2 = from.template convert_to<To>();
97 BOOST_CHECK_EQUAL(from.str(), t1.str());
98 BOOST_CHECK_EQUAL(from.str(), t2.str());
99 test_convert_neg_int<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
100 }
101 }
102
103 template <class From, class To>
test_convert_neg_rat(From from,const boost::mpl::true_ &)104 void test_convert_neg_rat(From from, const boost::mpl::true_&)
105 {
106 from = -from;
107 To t3(from);
108 To t4 = from.template convert_to<To>();
109 BOOST_CHECK_EQUAL(from.str(), numerator(t3).str());
110 BOOST_CHECK_EQUAL(from.str(), numerator(t4).str());
111 }
112 template <class From, class To>
test_convert_neg_rat(From const &,const boost::mpl::false_ &)113 void test_convert_neg_rat(From const&, const boost::mpl::false_&)
114 {
115 }
116
117 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_integer> const &,boost::mpl::int_<number_kind_rational> const &)118 void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_rational> const&)
119 {
120 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
121
122 for (unsigned i = 0; i < 100; ++i)
123 {
124 From from = generate_random<From>(bits_wanted);
125 To t1(from);
126 To t2 = from.template convert_to<To>();
127 BOOST_CHECK_EQUAL(from.str(), numerator(t1).str());
128 BOOST_CHECK_EQUAL(from.str(), numerator(t2).str());
129 test_convert_neg_rat<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
130 }
131 }
132
133 template <class From, class To>
test_convert_neg_float(From from,const boost::mpl::true_ &)134 void test_convert_neg_float(From from, const boost::mpl::true_&)
135 {
136 from = -from;
137 To t3(from);
138 To t4 = from.template convert_to<To>();
139 To check(from.str() + ".0");
140 BOOST_CHECK_EQUAL(t3, check);
141 BOOST_CHECK_EQUAL(t4, check);
142 }
143 template <class From, class To>
test_convert_neg_float(From const &,const boost::mpl::false_ &)144 void test_convert_neg_float(From const&, const boost::mpl::false_&)
145 {
146 }
147
148 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_integer> const &,boost::mpl::int_<number_kind_floating_point> const &)149 void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_floating_point> const&)
150 {
151 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
152
153 for (unsigned i = 0; i < 100; ++i)
154 {
155 From from = generate_random<From>(bits_wanted);
156 To t1(from);
157 To t2 = from.template convert_to<To>();
158 To check(from.str() + ".0");
159 BOOST_CHECK_EQUAL(t1, check);
160 BOOST_CHECK_EQUAL(t2, check);
161 test_convert_neg_float<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
162 }
163 }
164
165 template <class From, class To>
test_convert()166 void test_convert()
167 {
168 test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
169 }
170
main()171 int main()
172 {
173 test_convert<tom_int, cpp_int>();
174 test_convert<tom_int, int128_t>();
175 test_convert<tom_int, uint128_t>();
176
177 test_convert<tom_int, cpp_dec_float_50>();
178
179 test_convert<tom_int, cpp_rational>();
180
181 test_convert<tom_int, cpp_bin_float_50>();
182
183 #if defined(HAS_GMP)
184 test_convert<tom_int, mpz_int>();
185
186 test_convert<tom_int, mpq_rational>();
187
188 test_convert<tom_int, mpf_float_50>();
189 #endif
190 #if defined(HAS_MPFR)
191 test_convert<tom_int, mpfr_float_50>();
192 #endif
193 #if defined(HAS_MPFI)
194 test_convert<tom_int, mpfi_float_50>();
195 #endif
196 #ifdef HAS_TOMMATH
197 test_convert<tom_int, tom_int>();
198
199 test_convert<tom_int, tom_rational>();
200 #endif
201 #ifdef HAS_FLOAT128
202 test_convert<tom_int, float128>();
203 #endif
204 return boost::report_errors();
205 }
206
207 #else
208
main()209 int main() { return 0; }
210
211 #endif
212