File: multiprecision_int_test.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (225 lines) | stat: -rw-r--r-- 7,841 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

/* multiprecision_int_test.cpp
*
* Copyright John Maddock 2015
* Distributed under 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)
*
* $Id$
*
* Tests all integer related generators and distributions with multiprecision types:
* discard_block, independent_bits_engine, random_number_generator,
* xor_combine_engine, uniform_int_distribution, uniform_smallint.
*
* Not supported, but could be with more work (but probably not worth while):
* shuffle_order_engine, binomial_distribution, discrete_distribution, negative_binomial_distribution,
* poisson_distribution
*/

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

#include <boost/core/ignore_unused.hpp>
#include <boost/multiprecision/debug_adaptor.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/random/independent_bits.hpp>
#include <boost/random/discard_block.hpp>
#include <boost/random/xor_combine.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/random_number_generator.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_smallint.hpp>
#include <boost/random/discrete_distribution.hpp>
#include <sstream>

typedef boost::mpl::list <
   boost::random::independent_bits_engine<boost::random::mt19937, 1024, boost::multiprecision::uint1024_t >,
   boost::random::independent_bits_engine<boost::random::mt19937, 1024, boost::multiprecision::int1024_t >,
   boost::random::independent_bits_engine<boost::random::mt19937, 1024, boost::multiprecision::checked_uint1024_t >,
   boost::random::independent_bits_engine<boost::random::mt19937, 1024, boost::multiprecision::checked_int1024_t >,
   boost::random::independent_bits_engine<boost::random::mt19937, 30000, boost::multiprecision::cpp_int >,
   boost::random::discard_block_engine<boost::random::independent_bits_engine<boost::random::mt19937, 1024, boost::multiprecision::uint1024_t >, 20, 10>,
   boost::random::discard_block_engine<boost::random::independent_bits_engine<boost::random::mt19937, 1024, boost::multiprecision::int1024_t >, 20, 10>,
   boost::random::discard_block_engine<boost::random::independent_bits_engine<boost::random::mt19937, 1024, boost::multiprecision::checked_uint1024_t >, 20, 10>,
   boost::random::discard_block_engine<boost::random::independent_bits_engine<boost::random::mt19937, 1024, boost::multiprecision::checked_int1024_t >, 20, 10>,
   boost::random::discard_block_engine<boost::random::independent_bits_engine<boost::random::mt19937, 600, boost::multiprecision::cpp_int >, 20, 10>
> engines;

BOOST_AUTO_TEST_CASE_TEMPLATE(generator_test, engine_type, engines)
{
   typedef typename engine_type::result_type test_type;

   engine_type gen;
   gen.seed();
   test_type a = gen.min();
   test_type b = gen.max();
   BOOST_CHECK(a < b);
   a = gen();
   //
   // This extracts 32-bit values for use in seeding other sequences,
   // not really applicable here, and not functional for signed types anyway.
   //gen.generate(&b, &b + 1);
   gen.discard(20);

   typename engine_type::base_type base(gen.base());
   boost::ignore_unused(base);

   std::stringstream ss;
   ss << gen;
   engine_type gen2;
   ss >> gen2;
   BOOST_CHECK(gen == gen2);
   gen2();
   BOOST_CHECK(gen != gen2);
   //
   // construction and seeding:
   //
   engine_type gen3(0);
   gen3.seed(2);
}

BOOST_AUTO_TEST_CASE(xor_combine_test)
{
   //
   // As above but with a few things missing which don't work - for example we have no 
   // way to drill down and get the seed-type of the underlying generator.
   //
   typedef boost::random::xor_combine_engine<boost::random::independent_bits_engine<boost::random::mt19937, 512, boost::multiprecision::uint1024_t >, 512, boost::random::independent_bits_engine<boost::random::mt19937, 512, boost::multiprecision::uint1024_t >, 10> engine_type;
   typedef engine_type::result_type test_type;

   engine_type gen;
   gen.seed();
   test_type a = gen.min();
   test_type b = gen.max();
   BOOST_CHECK(a < b);
   a = gen();
#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
   gen.generate(&b, &b + 1);
#endif
   gen.discard(20);

   //typename engine_type::base_type base(gen.base());

   std::stringstream ss;
   ss << gen;
   engine_type gen2;
   ss >> gen2;
   BOOST_CHECK(gen == gen2);
   gen2();
   BOOST_CHECK(gen != gen2);
   //
   // construction and seeding:
   //
   //engine_type gen3(0);
   //gen3.seed(2);
}

typedef boost::mpl::list <
   boost::random::random_number_generator<boost::random::mt19937, boost::multiprecision::cpp_int>,
   boost::random::random_number_generator<boost::random::mt19937, boost::multiprecision::uint1024_t>,
   boost::random::random_number_generator<boost::random::mt19937, boost::multiprecision::checked_uint1024_t>
> generators;


BOOST_AUTO_TEST_CASE_TEMPLATE(random_number_generator, generator_type, generators)
{
   typedef typename generator_type::result_type result_type;
   typedef typename generator_type::base_type base_type;

   result_type lim = 1;
   lim <<= 500;

   base_type base;
   generator_type gen(base);

   for(unsigned i = 0; i < 100; ++i)
      BOOST_CHECK(gen(lim) < lim);
}

typedef boost::mpl::list <
   boost::random::uniform_int_distribution<boost::multiprecision::cpp_int>,
   boost::random::uniform_int_distribution<boost::multiprecision::uint1024_t>,
   boost::random::uniform_int_distribution<boost::multiprecision::checked_uint1024_t>,
   boost::random::uniform_smallint<boost::multiprecision::cpp_int>,
   boost::random::uniform_smallint<boost::multiprecision::uint1024_t>,
   boost::random::uniform_smallint<boost::multiprecision::checked_uint1024_t>
> uniform_distributions;


BOOST_AUTO_TEST_CASE_TEMPLATE(distributions, distribution_type, uniform_distributions)
{
   typedef typename distribution_type::result_type  result_type;

   result_type a = 20;
   result_type b = 1;
   b <<= 1000;

   distribution_type d(a, b);
   boost::random::mt19937 gen;

   BOOST_CHECK_EQUAL(d.a(), a);
   BOOST_CHECK_EQUAL(d.b(), b);
   BOOST_CHECK_EQUAL((d.min)(), a);
   BOOST_CHECK_EQUAL((d.max)(), b);

   for(unsigned i = 0; i < 200; ++i)
   {
      result_type r = d(gen);
      BOOST_CHECK(r <= b);
      BOOST_CHECK(r >= a);
   }

   std::stringstream ss;
   ss << d;
   distribution_type d2;
   ss >> d2;
   BOOST_CHECK(d == d2);

   boost::random::independent_bits_engine<boost::random::mt19937, std::numeric_limits<boost::multiprecision::uint1024_t>::digits, boost::multiprecision::uint1024_t > big_random;
   for(unsigned i = 0; i < 200; ++i)
   {
      result_type r = d(big_random);
      BOOST_CHECK(r <= b);
      BOOST_CHECK(r >= a);
   }
}

#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS

typedef boost::mpl::list <
   boost::random::discrete_distribution < boost::multiprecision::cpp_int, double>,
   boost::random::discrete_distribution <unsigned int, boost::multiprecision::cpp_bin_float_100>
> other_distributions;


BOOST_AUTO_TEST_CASE_TEMPLATE(discrete_distributions, distribution_type, other_distributions)
{
   typedef typename distribution_type::result_type  result_type;
   typedef typename distribution_type::input_type   input_type;

   input_type a[] = { 20, 30, 40, 50 };

   distribution_type d(a, a + 4);
   boost::random::mt19937 gen;

   for(unsigned i = 0; i < 200; ++i)
   {
      result_type r = d(gen);
   }

   std::stringstream ss;
   ss << std::setprecision(std::numeric_limits<input_type>::digits10 + 3) << d;
   distribution_type d2;
   ss >> d2;
   BOOST_CHECK(d == d2);

   boost::random::independent_bits_engine<boost::random::mt19937, std::numeric_limits<boost::multiprecision::uint1024_t>::digits, boost::multiprecision::uint1024_t > big_random;
   for(unsigned i = 0; i < 200; ++i)
   {
      result_type r = d(big_random);
   }
}

#endif