1 /* test_generate_canonical.cpp
2 *
3 * Copyright Steven Watanabe 2011
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/generate_canonical.hpp>
13
14 #include <boost/random/linear_congruential.hpp>
15 #include <boost/random/mersenne_twister.hpp>
16 #include <boost/random/lagged_fibonacci.hpp>
17 #include <boost/cstdint.hpp>
18 #include <boost/mpl/vector.hpp>
19
20 #define BOOST_TEST_MAIN
21 #include <boost/test/unit_test.hpp>
22
23 typedef boost::mpl::vector<
24 boost::random::minstd_rand,
25 boost::random::mt19937,
26 boost::random::lagged_fibonacci607
27 > engines;
28
BOOST_AUTO_TEST_CASE_TEMPLATE(test_float,Engine,engines)29 BOOST_AUTO_TEST_CASE_TEMPLATE(test_float, Engine, engines)
30 {
31 Engine eng;
32 Engine expected;
33 for(int i = 0; i < 1000; ++i) {
34 float val = boost::random::generate_canonical<float, 64>(eng);
35 BOOST_CHECK_GE(val, 0);
36 BOOST_CHECK_LT(val, 1);
37 }
38 expected.discard(1000);
39 BOOST_CHECK_EQUAL(eng, expected);
40 for(int i = 0; i < 1000; ++i) {
41 float val = boost::random::generate_canonical<float, 12>(eng);
42 BOOST_CHECK_GE(val, 0);
43 BOOST_CHECK_LT(val, 1);
44 }
45 expected.discard(1000);
46 BOOST_CHECK_EQUAL(eng, expected);
47 }
48
BOOST_AUTO_TEST_CASE_TEMPLATE(test_double,Engine,engines)49 BOOST_AUTO_TEST_CASE_TEMPLATE(test_double, Engine, engines)
50 {
51 Engine eng;
52 Engine expected;
53 for(int i = 0; i < 1000; ++i) {
54 double val = boost::random::generate_canonical<double, 64>(eng);
55 BOOST_CHECK_GE(val, 0);
56 BOOST_CHECK_LT(val, 1);
57 }
58 expected.discard(2000);
59 BOOST_CHECK_EQUAL(eng, expected);
60 for(int i = 0; i < 1000; ++i) {
61 double val = boost::random::generate_canonical<double, 12>(eng);
62 BOOST_CHECK_GE(val, 0);
63 BOOST_CHECK_LT(val, 1);
64 }
65 expected.discard(1000);
66 BOOST_CHECK_EQUAL(eng, expected);
67 }
68
BOOST_AUTO_TEST_CASE_TEMPLATE(test_long_double,Engine,engines)69 BOOST_AUTO_TEST_CASE_TEMPLATE(test_long_double, Engine, engines)
70 {
71 Engine eng;
72 Engine expected;
73 for(int i = 0; i < 1000; ++i) {
74 long double val = boost::random::generate_canonical<long double, 60>(eng);
75 BOOST_CHECK_GE(val, 0);
76 BOOST_CHECK_LT(val, 1);
77 }
78 expected.discard(2000);
79 BOOST_CHECK_EQUAL(eng, expected);
80 for(int i = 0; i < 1000; ++i) {
81 long double val = boost::random::generate_canonical<long double, 12>(eng);
82 BOOST_CHECK_GE(val, 0);
83 BOOST_CHECK_LT(val, 1);
84 }
85 expected.discard(1000);
86 BOOST_CHECK_EQUAL(eng, expected);
87 }
88
89 struct max_engine
90 {
91 typedef boost::uint32_t result_type;
BOOST_PREVENT_MACRO_SUBSTITUTIONmax_engine92 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
BOOST_PREVENT_MACRO_SUBSTITUTIONmax_engine93 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
94 { return ~boost::uint32_t(0); }
operator ()max_engine95 result_type operator()() { return (max)(); }
96 };
97
BOOST_AUTO_TEST_CASE(test_max)98 BOOST_AUTO_TEST_CASE(test_max)
99 {
100 max_engine eng;
101 BOOST_CHECK_LT((boost::random::generate_canonical<float, 64>(eng)), 1);
102 BOOST_CHECK_LT((boost::random::generate_canonical<double, 64>(eng)), 1);
103 BOOST_CHECK_LT((boost::random::generate_canonical<long double, 64>(eng)), 1);
104 }
105