1 /* test_discrete_distribution.cpp
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/discrete_distribution.hpp>
13 #include <boost/random/linear_congruential.hpp>
14 #include <boost/assign/list_of.hpp>
15 #include <sstream>
16 #include <vector>
17 #include "concepts.hpp"
18
19 #define BOOST_TEST_MAIN
20 #include <boost/test/unit_test.hpp>
21
22 using boost::random::test::RandomNumberDistribution;
23 using boost::random::discrete_distribution;
24 BOOST_CONCEPT_ASSERT((RandomNumberDistribution< discrete_distribution<> >));
25
26 struct gen {
operator ()gen27 double operator()(double arg) {
28 if(arg < 100) return 100;
29 else if(arg < 103) return 1;
30 else if(arg < 107) return 2;
31 else if(arg < 111) return 1;
32 else if(arg < 114) return 4;
33 else return 100;
34 }
35 };
36
37 #define CHECK_PROBABILITIES(actual, expected) \
38 do { \
39 std::vector<double> _actual = (actual); \
40 std::vector<double> _expected = (expected); \
41 BOOST_CHECK_EQUAL_COLLECTIONS( \
42 _actual.begin(), _actual.end(), \
43 _expected.begin(), _expected.end()); \
44 } while(false)
45
46 using boost::assign::list_of;
47
BOOST_AUTO_TEST_CASE(test_constructors)48 BOOST_AUTO_TEST_CASE(test_constructors) {
49 boost::random::discrete_distribution<> dist;
50 CHECK_PROBABILITIES(dist.probabilities(), list_of(1.0));
51
52 #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
53 boost::random::discrete_distribution<> dist_il = { 1, 2, 1, 4 };
54 CHECK_PROBABILITIES(dist_il.probabilities(), list_of(.125)(.25)(.125)(.5));
55 #endif
56 std::vector<double> probs = boost::assign::list_of(1.0)(2.0)(1.0)(4.0);
57
58 boost::random::discrete_distribution<> dist_r(probs);
59 CHECK_PROBABILITIES(dist_r.probabilities(), list_of(.125)(.25)(.125)(.5));
60
61 boost::random::discrete_distribution<> dist_it(probs.begin(), probs.end());
62 CHECK_PROBABILITIES(dist_it.probabilities(), list_of(.125)(.25)(.125)(.5));
63
64 boost::random::discrete_distribution<> dist_fun(4, 99, 115, gen());
65 CHECK_PROBABILITIES(dist_fun.probabilities(), list_of(.125)(.25)(.125)(.5));
66
67 boost::random::discrete_distribution<> copy(dist);
68 BOOST_CHECK_EQUAL(dist, copy);
69 boost::random::discrete_distribution<> copy_r(dist_r);
70 BOOST_CHECK_EQUAL(dist_r, copy_r);
71
72 boost::random::discrete_distribution<> notpow2(3, 99, 111, gen());
73 BOOST_REQUIRE_EQUAL(notpow2.probabilities().size(), 3u);
74 BOOST_CHECK_CLOSE_FRACTION(notpow2.probabilities()[0], 0.25, 0.00000000001);
75 BOOST_CHECK_CLOSE_FRACTION(notpow2.probabilities()[1], 0.50, 0.00000000001);
76 BOOST_CHECK_CLOSE_FRACTION(notpow2.probabilities()[2], 0.25, 0.00000000001);
77 boost::random::discrete_distribution<> copy_notpow2(notpow2);
78 BOOST_CHECK_EQUAL(notpow2, copy_notpow2);
79 }
80
BOOST_AUTO_TEST_CASE(test_param)81 BOOST_AUTO_TEST_CASE(test_param) {
82 std::vector<double> probs = boost::assign::list_of(1.0)(2.0)(1.0)(4.0);
83 boost::random::discrete_distribution<> dist(probs);
84 boost::random::discrete_distribution<>::param_type param = dist.param();
85 CHECK_PROBABILITIES(param.probabilities(), list_of(.125)(.25)(.125)(.5));
86 boost::random::discrete_distribution<> copy1(param);
87 BOOST_CHECK_EQUAL(dist, copy1);
88 boost::random::discrete_distribution<> copy2;
89 copy2.param(param);
90 BOOST_CHECK_EQUAL(dist, copy2);
91
92 boost::random::discrete_distribution<>::param_type param_copy = param;
93 BOOST_CHECK_EQUAL(param, param_copy);
94 BOOST_CHECK(param == param_copy);
95 BOOST_CHECK(!(param != param_copy));
96 boost::random::discrete_distribution<>::param_type param_default;
97 CHECK_PROBABILITIES(param_default.probabilities(), list_of(1.0));
98 BOOST_CHECK(param != param_default);
99 BOOST_CHECK(!(param == param_default));
100
101 #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
102 boost::random::discrete_distribution<>::param_type
103 parm_il = { 1, 2, 1, 4 };
104 CHECK_PROBABILITIES(parm_il.probabilities(), list_of(.125)(.25)(.125)(.5));
105 #endif
106
107 boost::random::discrete_distribution<>::param_type parm_r(probs);
108 CHECK_PROBABILITIES(parm_r.probabilities(), list_of(.125)(.25)(.125)(.5));
109
110 boost::random::discrete_distribution<>::param_type
111 parm_it(probs.begin(), probs.end());
112 CHECK_PROBABILITIES(parm_it.probabilities(), list_of(.125)(.25)(.125)(.5));
113
114 boost::random::discrete_distribution<>::param_type
115 parm_fun(4, 99, 115, gen());
116 CHECK_PROBABILITIES(parm_fun.probabilities(), list_of(.125)(.25)(.125)(.5));
117 }
118
BOOST_AUTO_TEST_CASE(test_min_max)119 BOOST_AUTO_TEST_CASE(test_min_max) {
120 std::vector<double> probs = boost::assign::list_of(1.0)(2.0)(1.0);
121 boost::random::discrete_distribution<> dist;
122 BOOST_CHECK_EQUAL((dist.min)(), 0);
123 BOOST_CHECK_EQUAL((dist.max)(), 0);
124 boost::random::discrete_distribution<> dist_r(probs);
125 BOOST_CHECK_EQUAL((dist_r.min)(), 0);
126 BOOST_CHECK_EQUAL((dist_r.max)(), 2);
127 }
128
BOOST_AUTO_TEST_CASE(test_comparison)129 BOOST_AUTO_TEST_CASE(test_comparison) {
130 std::vector<double> probs = boost::assign::list_of(1.0)(2.0)(1.0)(4.0);
131 boost::random::discrete_distribution<> dist;
132 boost::random::discrete_distribution<> dist_copy(dist);
133 boost::random::discrete_distribution<> dist_r(probs);
134 boost::random::discrete_distribution<> dist_r_copy(dist_r);
135 BOOST_CHECK(dist == dist_copy);
136 BOOST_CHECK(!(dist != dist_copy));
137 BOOST_CHECK(dist_r == dist_r_copy);
138 BOOST_CHECK(!(dist_r != dist_r_copy));
139 BOOST_CHECK(dist != dist_r);
140 BOOST_CHECK(!(dist == dist_r));
141 }
142
BOOST_AUTO_TEST_CASE(test_streaming)143 BOOST_AUTO_TEST_CASE(test_streaming) {
144 std::vector<double> probs = boost::assign::list_of(1.0)(2.0)(1.0)(4.0);
145 boost::random::discrete_distribution<> dist(probs);
146 std::stringstream stream;
147 stream << dist;
148 boost::random::discrete_distribution<> restored_dist;
149 stream >> restored_dist;
150 BOOST_CHECK_EQUAL(dist, restored_dist);
151 }
152
BOOST_AUTO_TEST_CASE(test_generation)153 BOOST_AUTO_TEST_CASE(test_generation) {
154 std::vector<double> probs = boost::assign::list_of(0.0)(1.0);
155 boost::minstd_rand0 gen;
156 boost::random::discrete_distribution<> dist;
157 boost::random::discrete_distribution<> dist_r(probs);
158 for(int i = 0; i < 10; ++i) {
159 int value = dist(gen);
160 BOOST_CHECK_EQUAL(value, 0);
161 int value_r = dist_r(gen);
162 BOOST_CHECK_EQUAL(value_r, 1);
163 int value_param = dist_r(gen, dist.param());
164 BOOST_CHECK_EQUAL(value_param, 0);
165 int value_r_param = dist(gen, dist_r.param());
166 BOOST_CHECK_EQUAL(value_r_param, 1);
167 }
168 }
169