• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* test_distribution.ipp
2 *
3 * Copyright Steven Watanabe 2010
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * $Id$
9 *
10 */
11
12#include <boost/random/linear_congruential.hpp>
13#include <boost/random/lagged_fibonacci.hpp>
14#include <sstream>
15#include "concepts.hpp"
16
17#define BOOST_TEST_MAIN
18#include <boost/test/unit_test.hpp>
19
20using boost::random::test::RandomNumberDistribution;
21BOOST_CONCEPT_ASSERT((RandomNumberDistribution< BOOST_RANDOM_DISTRIBUTION >));
22
23BOOST_AUTO_TEST_CASE(test_constructors) {
24    BOOST_RANDOM_DISTRIBUTION dist;
25    BOOST_CHECK_EQUAL(dist.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_DEFAULT);
26#ifdef BOOST_RANDOM_ARG2
27    BOOST_CHECK_EQUAL(dist.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_DEFAULT);
28#endif
29#ifdef BOOST_RANDOM_ARG3
30    BOOST_CHECK_EQUAL(dist.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_DEFAULT);
31#endif
32    BOOST_RANDOM_DISTRIBUTION dist_one(BOOST_RANDOM_ARG1_VALUE);
33    BOOST_CHECK_EQUAL(dist_one.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_VALUE);
34#ifdef BOOST_RANDOM_ARG2
35    BOOST_CHECK_EQUAL(dist_one.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_DEFAULT);
36#endif
37#ifdef BOOST_RANDOM_ARG3
38    BOOST_CHECK_EQUAL(dist_one.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_DEFAULT);
39#endif
40#ifdef BOOST_RANDOM_ARG2
41    BOOST_RANDOM_DISTRIBUTION dist_two(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE);
42    BOOST_CHECK_EQUAL(dist_two.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_VALUE);
43    BOOST_CHECK_EQUAL(dist_two.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_VALUE);
44#ifdef BOOST_RANDOM_ARG3
45    BOOST_CHECK_EQUAL(dist_two.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_DEFAULT);
46#endif
47#endif
48#ifdef BOOST_RANDOM_ARG3
49    BOOST_RANDOM_DISTRIBUTION dist_three(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE, BOOST_RANDOM_ARG3_VALUE);
50    BOOST_CHECK_EQUAL(dist_three.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_VALUE);
51    BOOST_CHECK_EQUAL(dist_three.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_VALUE);
52    BOOST_CHECK_EQUAL(dist_three.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_VALUE);
53#endif
54    BOOST_RANDOM_DISTRIBUTION copy(dist);
55    BOOST_CHECK_EQUAL(dist, copy);
56    BOOST_RANDOM_DISTRIBUTION copy_one(dist_one);
57    BOOST_CHECK_EQUAL(dist_one, copy_one);
58#ifdef BOOST_RANDOM_ARG2
59    BOOST_RANDOM_DISTRIBUTION copy_two(dist_two);
60    BOOST_CHECK_EQUAL(dist_two, copy_two);
61#endif
62#ifdef BOOST_RANDOM_ARG3
63    BOOST_RANDOM_DISTRIBUTION copy_three(dist_three);
64    BOOST_CHECK_EQUAL(dist_three, copy_three);
65#endif
66}
67
68BOOST_AUTO_TEST_CASE(test_param) {
69#if defined(BOOST_RANDOM_ARG3)
70    BOOST_RANDOM_DISTRIBUTION dist(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE, BOOST_RANDOM_ARG3_VALUE);
71#elif defined(BOOST_RANDOM_ARG2)
72    BOOST_RANDOM_DISTRIBUTION dist(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE);
73#else
74    BOOST_RANDOM_DISTRIBUTION dist(BOOST_RANDOM_ARG1_VALUE);
75#endif
76    BOOST_RANDOM_DISTRIBUTION::param_type param = dist.param();
77    BOOST_CHECK_EQUAL(param.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_VALUE);
78#ifdef BOOST_RANDOM_ARG2
79    BOOST_CHECK_EQUAL(param.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_VALUE);
80#endif
81#ifdef BOOST_RANDOM_ARG3
82    BOOST_CHECK_EQUAL(param.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_VALUE);
83#endif
84    BOOST_RANDOM_DISTRIBUTION copy1(param);
85    BOOST_CHECK_EQUAL(dist, copy1);
86    BOOST_RANDOM_DISTRIBUTION copy2;
87    copy2.param(param);
88    BOOST_CHECK_EQUAL(dist, copy2);
89
90    BOOST_RANDOM_DISTRIBUTION::param_type param_copy = param;
91    BOOST_CHECK_EQUAL(param, param_copy);
92    BOOST_CHECK(param == param_copy);
93    BOOST_CHECK(!(param != param_copy));
94    BOOST_RANDOM_DISTRIBUTION::param_type param_default;
95    BOOST_CHECK_EQUAL(param_default.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_DEFAULT);
96#ifdef BOOST_RANDOM_ARG2
97    BOOST_CHECK_EQUAL(param_default.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_DEFAULT);
98#endif
99#ifdef BOOST_RANDOM_ARG3
100    BOOST_CHECK_EQUAL(param_default.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_DEFAULT);
101#endif
102    BOOST_CHECK(param != param_default);
103    BOOST_CHECK(!(param == param_default));
104    BOOST_RANDOM_DISTRIBUTION::param_type param_one(BOOST_RANDOM_ARG1_VALUE);
105    BOOST_CHECK_EQUAL(param_one.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_VALUE);
106#ifdef BOOST_RANDOM_ARG2
107    BOOST_CHECK_EQUAL(param_one.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_DEFAULT);
108#endif
109#ifdef BOOST_RANDOM_ARG3
110    BOOST_CHECK_EQUAL(param_one.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_DEFAULT);
111#endif
112#ifdef BOOST_RANDOM_ARG2
113    BOOST_CHECK(param != param_one);
114    BOOST_CHECK(!(param == param_one));
115#endif
116    BOOST_CHECK(param_default != param_one);
117    BOOST_CHECK(!(param_default == param_one));
118#ifdef BOOST_RANDOM_ARG2
119    BOOST_RANDOM_DISTRIBUTION::param_type param_two(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE);
120    BOOST_CHECK_EQUAL(param_two.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_VALUE);
121    BOOST_CHECK_EQUAL(param_two.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_VALUE);
122#ifdef BOOST_RANDOM_ARG3
123    BOOST_CHECK_EQUAL(param_two.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_DEFAULT);
124#endif
125#endif
126#ifdef BOOST_RANDOM_ARG3
127    BOOST_RANDOM_DISTRIBUTION::param_type param_three(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE, BOOST_RANDOM_ARG3_VALUE);
128    BOOST_CHECK_EQUAL(param_three.BOOST_RANDOM_ARG1(), BOOST_RANDOM_ARG1_VALUE);
129    BOOST_CHECK_EQUAL(param_three.BOOST_RANDOM_ARG2(), BOOST_RANDOM_ARG2_VALUE);
130    BOOST_CHECK_EQUAL(param_three.BOOST_RANDOM_ARG3(), BOOST_RANDOM_ARG3_VALUE);
131#endif
132}
133
134BOOST_AUTO_TEST_CASE(test_min_max) {
135    BOOST_RANDOM_DISTRIBUTION dist;
136    BOOST_CHECK_EQUAL((dist.min)(), BOOST_RANDOM_DIST0_MIN);
137    BOOST_CHECK_EQUAL((dist.max)(), BOOST_RANDOM_DIST0_MAX);
138    BOOST_RANDOM_DISTRIBUTION dist_one(BOOST_RANDOM_ARG1_VALUE);
139    BOOST_CHECK_EQUAL((dist_one.min)(), BOOST_RANDOM_DIST1_MIN);
140    BOOST_CHECK_EQUAL((dist_one.max)(), BOOST_RANDOM_DIST1_MAX);
141#ifdef BOOST_RANDOM_ARG2
142    BOOST_RANDOM_DISTRIBUTION dist_two(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE);
143    BOOST_CHECK_EQUAL((dist_two.min)(), BOOST_RANDOM_DIST2_MIN);
144    BOOST_CHECK_EQUAL((dist_two.max)(), BOOST_RANDOM_DIST2_MAX);
145#endif
146#ifdef BOOST_RANDOM_ARG3
147    BOOST_RANDOM_DISTRIBUTION dist_three(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE, BOOST_RANDOM_ARG3_VALUE);
148    BOOST_CHECK_EQUAL((dist_three.min)(), BOOST_RANDOM_DIST3_MIN);
149    BOOST_CHECK_EQUAL((dist_three.max)(), BOOST_RANDOM_DIST3_MAX);
150#endif
151}
152
153BOOST_AUTO_TEST_CASE(test_comparison) {
154    BOOST_RANDOM_DISTRIBUTION dist;
155    BOOST_RANDOM_DISTRIBUTION dist_copy(dist);
156    BOOST_RANDOM_DISTRIBUTION dist_one(BOOST_RANDOM_ARG1_VALUE);
157    BOOST_RANDOM_DISTRIBUTION dist_one_copy(dist_one);
158#ifdef BOOST_RANDOM_ARG2
159    BOOST_RANDOM_DISTRIBUTION dist_two(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE);
160    BOOST_RANDOM_DISTRIBUTION dist_two_copy(dist_two);
161#endif
162#ifdef BOOST_RANDOM_ARG3
163    BOOST_RANDOM_DISTRIBUTION dist_three(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE, BOOST_RANDOM_ARG3_VALUE);
164    BOOST_RANDOM_DISTRIBUTION dist_three_copy(dist_three);
165#endif
166    BOOST_CHECK(dist == dist_copy);
167    BOOST_CHECK(!(dist != dist_copy));
168    BOOST_CHECK(dist_one == dist_one_copy);
169    BOOST_CHECK(!(dist_one != dist_one_copy));
170    BOOST_CHECK(dist != dist_one);
171    BOOST_CHECK(!(dist == dist_one));
172#ifdef BOOST_RANDOM_ARG2
173    BOOST_CHECK(dist_two == dist_two_copy);
174    BOOST_CHECK(!(dist_two != dist_two_copy));
175    BOOST_CHECK(dist != dist_two);
176    BOOST_CHECK(!(dist == dist_two));
177    BOOST_CHECK(dist_one != dist_two);
178    BOOST_CHECK(!(dist_one == dist_two));
179#endif
180#ifdef BOOST_RANDOM_ARG3
181    BOOST_CHECK(dist_three == dist_three_copy);
182    BOOST_CHECK(!(dist_three != dist_three_copy));
183    BOOST_CHECK(dist != dist_three);
184    BOOST_CHECK(!(dist == dist_three));
185    BOOST_CHECK(dist_one != dist_three);
186    BOOST_CHECK(!(dist_one == dist_three));
187    BOOST_CHECK(dist_two != dist_three);
188    BOOST_CHECK(!(dist_two == dist_three));
189#endif
190}
191
192BOOST_AUTO_TEST_CASE(test_streaming) {
193#if defined(BOOST_RANDOM_ARG3)
194    BOOST_RANDOM_DISTRIBUTION dist(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE, BOOST_RANDOM_ARG2_VALUE);
195#elif defined(BOOST_RANDOM_ARG2)
196    BOOST_RANDOM_DISTRIBUTION dist(BOOST_RANDOM_ARG1_VALUE, BOOST_RANDOM_ARG2_VALUE);
197#else
198    BOOST_RANDOM_DISTRIBUTION dist(BOOST_RANDOM_ARG1_VALUE);
199#endif
200    std::stringstream stream;
201    stream << dist;
202    BOOST_RANDOM_DISTRIBUTION restored_dist;
203    stream >> restored_dist;
204    BOOST_CHECK_EQUAL(dist, restored_dist);
205}
206
207void use(BOOST_RANDOM_DISTRIBUTION::result_type) {}
208
209BOOST_AUTO_TEST_CASE(test_generation) {
210    boost::minstd_rand0 gen;
211    BOOST_RANDOM_DISTRIBUTION dist BOOST_RANDOM_TEST1_PARAMS;
212    BOOST_RANDOM_DISTRIBUTION dist_two BOOST_RANDOM_TEST2_PARAMS;
213    typedef BOOST_RANDOM_DISTRIBUTION::result_type result_type;
214    for(int i = 0; i < 10; ++i) {
215        result_type value = dist(gen);
216        use(value);
217#ifdef BOOST_RANDOM_TEST1_MIN
218        BOOST_CHECK_GE(value, BOOST_RANDOM_TEST1_MIN);
219#endif
220#ifdef BOOST_RANDOM_TEST1_MAX
221        BOOST_CHECK_LE(value, BOOST_RANDOM_TEST1_MAX);
222#endif
223        result_type value_two = dist_two(gen);
224        use(value_two);
225#ifdef BOOST_RANDOM_TEST2_MIN
226        BOOST_CHECK_GE(value_two, BOOST_RANDOM_TEST2_MIN);
227#endif
228#ifdef BOOST_RANDOM_TEST2_MAX
229        BOOST_CHECK_LE(value_two, BOOST_RANDOM_TEST2_MAX);
230#endif
231        result_type value_param = dist_two(gen, dist.param());
232        use(value_param);
233#ifdef BOOST_RANDOM_TEST1_MIN
234        BOOST_CHECK_GE(value_param, BOOST_RANDOM_TEST1_MIN);
235#endif
236#ifdef BOOST_RANDOM_TEST1_MAX
237        BOOST_CHECK_LE(value_param, BOOST_RANDOM_TEST1_MAX);
238#endif
239        result_type value_two_param = dist(gen, dist_two.param());
240        use(value_two_param);
241#ifdef BOOST_RANDOM_TEST2_MIN
242        BOOST_CHECK_GE(value_two_param, BOOST_RANDOM_TEST2_MIN);
243#endif
244#ifdef BOOST_RANDOM_TEST2_MAX
245        BOOST_CHECK_LE(value_two_param, BOOST_RANDOM_TEST2_MAX);
246#endif
247    }
248}
249
250BOOST_AUTO_TEST_CASE(test_generation_float) {
251    boost::lagged_fibonacci607 gen;
252    BOOST_RANDOM_DISTRIBUTION dist BOOST_RANDOM_TEST1_PARAMS;
253    BOOST_RANDOM_DISTRIBUTION dist_two BOOST_RANDOM_TEST2_PARAMS;
254    typedef BOOST_RANDOM_DISTRIBUTION::result_type result_type;
255    for(int i = 0; i < 10; ++i) {
256        result_type value = dist(gen);
257        use(value);
258#ifdef BOOST_RANDOM_TEST1_MIN
259        BOOST_CHECK_GE(value, BOOST_RANDOM_TEST1_MIN);
260#endif
261#ifdef BOOST_RANDOM_TEST1_MAX
262        BOOST_CHECK_LE(value, BOOST_RANDOM_TEST1_MAX);
263#endif
264        result_type value_two = dist_two(gen);
265        use(value_two);
266#ifdef BOOST_RANDOM_TEST2_MIN
267        BOOST_CHECK_GE(value_two, BOOST_RANDOM_TEST2_MIN);
268#endif
269#ifdef BOOST_RANDOM_TEST2_MAX
270        BOOST_CHECK_LE(value_two, BOOST_RANDOM_TEST2_MAX);
271#endif
272        result_type value_param = dist_two(gen, dist.param());
273        use(value_param);
274#ifdef BOOST_RANDOM_TEST1_MIN
275        BOOST_CHECK_GE(value_param, BOOST_RANDOM_TEST1_MIN);
276#endif
277#ifdef BOOST_RANDOM_TEST1_MAX
278        BOOST_CHECK_LE(value_param, BOOST_RANDOM_TEST1_MAX);
279#endif
280        result_type value_two_param = dist(gen, dist_two.param());
281        use(value_two_param);
282#ifdef BOOST_RANDOM_TEST2_MIN
283        BOOST_CHECK_GE(value_two_param, BOOST_RANDOM_TEST2_MIN);
284#endif
285#ifdef BOOST_RANDOM_TEST2_MAX
286        BOOST_CHECK_LE(value_two_param, BOOST_RANDOM_TEST2_MAX);
287#endif
288    }
289}
290
291