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_MPQ) && !defined(TEST_TOMMATH) && !defined(TEST_CPP_INT)
13 #  define TEST_MPQ
14 #  define TEST_TOMMATH
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_MPQ)
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 <boost/multiprecision/rational_adaptor.hpp>
40 #include "test.hpp"
41 #include <iostream>
42 #include <iomanip>
43 
44 template <class T>
generate_random()45 T generate_random()
46 {
47    typedef typename boost::multiprecision::component_type<T>::type int_type;
48    static boost::random::uniform_int_distribution<unsigned> ui(0, 20);
49    static boost::random::mt19937 gen;
50    int_type val = int_type(gen());
51    unsigned lim = ui(gen);
52    for(unsigned i = 0; i < lim; ++i)
53    {
54       val *= (gen.max)();
55       val += gen();
56    }
57    int_type denom = int_type(gen());
58    lim = ui(gen);
59    for(unsigned i = 0; i < lim; ++i)
60    {
61       denom *= (gen.max)();
62       denom += gen();
63    }
64    return T(val, denom);
65 }
66 
67 template <class T>
do_round_trip(const T & val,std::ios_base::fmtflags f,const boost::mpl::true_ &)68 void do_round_trip(const T& val, std::ios_base::fmtflags f, const boost::mpl::true_&)
69 {
70    std::stringstream ss;
71 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
72    ss << std::setprecision(std::numeric_limits<T>::max_digits10);
73 #else
74    ss << std::setprecision(std::numeric_limits<T>::digits10 + 5);
75 #endif
76    ss.flags(f);
77    ss << val;
78    T new_val = static_cast<T>(ss.str());
79    BOOST_CHECK_EQUAL(new_val, val);
80    new_val = static_cast<T>(val.str(0, f));
81    BOOST_CHECK_EQUAL(new_val, val);
82 }
83 
84 template <class T>
do_round_trip(const T & val,std::ios_base::fmtflags f,const boost::mpl::false_ &)85 void do_round_trip(const T& val, std::ios_base::fmtflags f, const boost::mpl::false_&)
86 {
87    std::stringstream ss;
88    ss << std::setprecision(std::numeric_limits<T>::digits10 + 4);
89    ss.flags(f);
90    ss << val;
91    T new_val;
92    ss >> new_val;
93    BOOST_CHECK_EQUAL(new_val, val);
94 }
95 
96 template <class T>
97 struct is_number : public boost::mpl::false_{};
98 template <class T>
99 struct is_number<boost::multiprecision::number<T> > : public boost::mpl::true_{};
100 
101 template <class T>
do_round_trip(const T & val,std::ios_base::fmtflags f)102 void do_round_trip(const T& val, std::ios_base::fmtflags f)
103 {
104    do_round_trip(val, f, is_number<T>());
105 }
106 
107 template <class T>
do_round_trip(const T & val)108 void do_round_trip(const T& val)
109 {
110    do_round_trip(val, std::ios_base::fmtflags(0));
111    if(val >= 0)
112    {
113       do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase|std::ios_base::hex));
114       do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase|std::ios_base::oct));
115    }
116 }
117 
118 template <class T>
test_round_trip()119 void test_round_trip()
120 {
121    for(unsigned i = 0; i < 1000; ++i)
122    {
123       T val = generate_random<T>();
124       do_round_trip(val);
125       do_round_trip(T(-val));
126    }
127 }
128 
main()129 int main()
130 {
131 #ifdef TEST_MPQ
132    test_round_trip<boost::multiprecision::mpq_rational>();
133    test_round_trip<boost::rational<boost::multiprecision::mpz_int> >();
134    test_round_trip<boost::multiprecision::number<boost::multiprecision::rational_adaptor<boost::multiprecision::gmp_int> > >();
135 #endif
136 #ifdef TEST_TOMMATH
137    test_round_trip<boost::rational<boost::multiprecision::tom_int> >();
138    test_round_trip<boost::multiprecision::tom_rational >();
139 #endif
140 #ifdef TEST_CPP_INT
141    test_round_trip<boost::rational<boost::multiprecision::cpp_int> >();
142    test_round_trip<boost::multiprecision::cpp_rational >();
143 #endif
144    return boost::report_errors();
145 }
146 
147