File: test_rational_io.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (144 lines) | stat: -rw-r--r-- 4,125 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright John Maddock 2011.

// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifdef _MSC_VER
#define _SCL_SECURE_NO_WARNINGS
#endif

#if !defined(TEST_MPQ) && !defined(TEST_TOMMATH) && !defined(TEST_CPP_INT)
#define TEST_MPQ
#define TEST_TOMMATH
#define TEST_CPP_INT

#ifdef _MSC_VER
#pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!")
#endif
#ifdef __GNUC__
#pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!"
#endif

#endif

#if defined(TEST_MPQ)
#include <boost/multiprecision/gmp.hpp>
#include <boost/multiprecision/rational_adaptor.hpp>
#endif
#if defined(TEST_TOMMATH)
#include <boost/multiprecision/tommath.hpp>
#endif
#ifdef TEST_CPP_INT
#include <boost/multiprecision/cpp_int.hpp>
#endif

#include <boost/algorithm/string/case_conv.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/rational.hpp>
#include "test.hpp"
#include <iostream>
#include <iomanip>

template <class T>
T generate_random()
{
   typedef typename boost::multiprecision::component_type<T>::type int_type;
   static boost::random::uniform_int_distribution<unsigned>        ui(0, 20);
   static boost::random::mt19937                                   gen;
   int_type                                                        val = int_type(gen());
   unsigned                                                        lim = ui(gen);
   for (unsigned i = 0; i < lim; ++i)
   {
      val *= (gen.max)();
      val += gen();
   }
   int_type denom = int_type(gen());
   lim            = ui(gen);
   for (unsigned i = 0; i < lim; ++i)
   {
      denom *= (gen.max)();
      denom += gen();
   }
   return T(val, denom);
}

template <class T>
void do_round_trip(const T& val, std::ios_base::fmtflags f, const std::integral_constant<bool, true>&)
{
   std::stringstream ss;
   ss << std::setprecision(std::numeric_limits<T>::max_digits10);
   ss.flags(f);
   ss << val;
   T new_val = static_cast<T>(ss.str());
   BOOST_CHECK_EQUAL(new_val, val);
   new_val = static_cast<T>(val.str(0, f));
   BOOST_CHECK_EQUAL(new_val, val);
}

template <class T>
void do_round_trip(const T& val, std::ios_base::fmtflags f, const std::integral_constant<bool, false>&)
{
   std::stringstream ss;
   ss << std::setprecision(std::numeric_limits<T>::digits10 + 4);
   ss.flags(f);
   ss << val;
   T new_val;
   ss >> new_val;
   BOOST_CHECK_EQUAL(new_val, val);
}

template <class T>
struct is_number : public std::integral_constant<bool, false>
{};
template <class T>
struct is_number<boost::multiprecision::number<T> > : public std::integral_constant<bool, true>
{};

template <class T>
void do_round_trip(const T& val, std::ios_base::fmtflags f)
{
   do_round_trip(val, f, is_number<T>());
}

template <class T>
void do_round_trip(const T& val)
{
   do_round_trip(val, std::ios_base::fmtflags(0));
   if (val >= 0)
   {
      do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase | std::ios_base::hex));
      do_round_trip(val, std::ios_base::fmtflags(std::ios_base::showbase | std::ios_base::oct));
   }
}

template <class T>
void test_round_trip()
{
   for (unsigned i = 0; i < 1000; ++i)
   {
      T val = generate_random<T>();
      do_round_trip(val);
      do_round_trip(T(-val));
   }
}

int main()
{
#ifdef TEST_MPQ
   test_round_trip<boost::multiprecision::mpq_rational>();
   test_round_trip<boost::rational<boost::multiprecision::mpz_int> >();
   test_round_trip<boost::multiprecision::number<boost::multiprecision::rational_adaptor<boost::multiprecision::gmp_int> > >();
#endif
#ifdef TEST_TOMMATH
   test_round_trip<boost::rational<boost::multiprecision::tom_int> >();
   test_round_trip<boost::multiprecision::tom_rational>();
#endif
#ifdef TEST_CPP_INT
   test_round_trip<boost::rational<boost::multiprecision::cpp_int> >();
   test_round_trip<boost::multiprecision::cpp_rational>();
#endif
   return boost::report_errors();
}