Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / test / test_rational_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_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>
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>
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>
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 {};
99 template <class T>
100 struct is_number<boost::multiprecision::number<T> > : public boost::mpl::true_
101 {};
102
103 template <class T>
104 void do_round_trip(const T& val, std::ios_base::fmtflags f)
105 {
106    do_round_trip(val, f, is_number<T>());
107 }
108
109 template <class T>
110 void do_round_trip(const T& val)
111 {
112    do_round_trip(val, std::ios_base::fmtflags(0));
113    if (val >= 0)
114    {
115       do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase | std::ios_base::hex));
116       do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase | std::ios_base::oct));
117    }
118 }
119
120 template <class T>
121 void test_round_trip()
122 {
123    for (unsigned i = 0; i < 1000; ++i)
124    {
125       T val = generate_random<T>();
126       do_round_trip(val);
127       do_round_trip(T(-val));
128    }
129 }
130
131 int main()
132 {
133 #ifdef TEST_MPQ
134    test_round_trip<boost::multiprecision::mpq_rational>();
135    test_round_trip<boost::rational<boost::multiprecision::mpz_int> >();
136    test_round_trip<boost::multiprecision::number<boost::multiprecision::rational_adaptor<boost::multiprecision::gmp_int> > >();
137 #endif
138 #ifdef TEST_TOMMATH
139    test_round_trip<boost::rational<boost::multiprecision::tom_int> >();
140    test_round_trip<boost::multiprecision::tom_rational>();
141 #endif
142 #ifdef TEST_CPP_INT
143    test_round_trip<boost::rational<boost::multiprecision::cpp_int> >();
144    test_round_trip<boost::multiprecision::cpp_rational>();
145 #endif
146    return boost::report_errors();
147 }