]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/tr1/test/run_random.cpp
1 // (C) Copyright John Maddock 2005.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifdef TEST_STD_HEADERS
9 #include <boost/tr1/random.hpp>
12 #include <boost/test/test_tools.hpp>
13 #include <boost/test/included/test_exec_monitor.hpp>
18 template<class PRNG
, class R
>
19 void validate(const std::string
& name
, PRNG
&rng
, R expected
)
21 typedef typename
PRNG::result_type result_type
;
22 std::cout
<< "validating " << name
<< ": ";
23 for(int i
= 0; i
< 9999; i
++)
25 result_type r
= rng();
26 BOOST_CHECK(r
>= (rng
.min
)());
27 BOOST_CHECK(r
<= (rng
.max
)());
29 result_type val
= rng();
30 BOOST_CHECK(val
>= (rng
.min
)());
31 BOOST_CHECK(val
<= (rng
.max
)());
33 bool result
= val
== expected
;
35 // allow for a simple eyeball check for MSVC instantiation brokenness
36 // (if the numbers for all generators are the same, it's obviously broken)
37 if(std::numeric_limits
<R
>::is_integer
== 0)
38 std::cout
<< std::setprecision(std::numeric_limits
<R
>::digits10
+ 1);
42 std::cout
<< " (Expected: ";
43 if(std::numeric_limits
<R
>::is_integer
== 0)
44 std::cout
<< std::setprecision(std::numeric_limits
<R
>::digits10
+ 1);
45 std::cout
<< expected
<< ")";
47 std::cout
<< std::endl
;
51 struct counting_functor
53 typedef unsigned result_type
;
58 counting_functor(int j
)
64 int test_main(int, char*[])
67 typedef std::tr1::linear_congruential
< ::boost::int32_t, 16807, 0, 2147483647> minstd_rand0
;
70 validate("minstd_rand0", a1
, 1043618065u);
72 validate("minstd_rand0", a2
, 1043618065u);
73 validate("minstd_rand0", a1
, 1043618065u);
75 counting_functor
f1(1);
77 validate("minstd_rand0", a2
, 1043618065u);
78 validate("minstd_rand0", a1
, 1043618065u);
80 counting_functor
f2(1);
81 a2
= minstd_rand0(f2
);
82 validate("minstd_rand0", a2
, 1043618065u);
83 validate("minstd_rand0", a1
, 1043618065u);
87 typedef std::tr1::linear_congruential
< ::boost::uint32_t, 69069, 0, 0> mt_seed
;
88 typedef std::tr1::mersenne_twister
< ::boost::uint32_t,32,351,175,19,0xccab8ee7,11,7,0x31b6ab00,15,0xffe50000,17> mt11213b
;
91 validate("mt11213b", a1
, 3809585648u);
93 validate("mt11213b", a2
, 3809585648u);
94 validate("mt11213b", a1
, 3809585648u);
96 validate("mt11213b", a1
, 3809585648u);
98 validate("mt11213b", a1
, 3809585648u);
101 validate("mt11213b", a1
, 2742893714u);
104 validate("mt11213b", a3
, 2742893714u);
108 typedef std::tr1::linear_congruential
< :: boost::int32_t, 40014, 0, 2147483563> seed_t
;
109 typedef std::tr1::subtract_with_carry
< ::boost::int32_t, (1<<24), 10, 24> sub_t
;
112 validate("subtract_with_carry", a1
, 7937952u);
114 validate("subtract_with_carry", a2
, 7937952u);
115 validate("subtract_with_carry", a1
, 7937952u);
117 seed_t
s1(19780503UL);
119 validate("subtract_with_carry", a2
, 7937952u);
120 validate("subtract_with_carry", a1
, 7937952u);
122 seed_t
s2(19780503UL);
124 validate("subtract_with_carry", a2
, 7937952u);
125 validate("subtract_with_carry", a1
, 7937952u);
129 typedef std::tr1::linear_congruential
< :: boost::int32_t, 40014, 0, 2147483563> seed_t
;
130 typedef std::tr1::subtract_with_carry_01
< float, 24, 10, 24> ranlux_base_01
;
132 ranlux_base_01
a2(a1
);
133 validate("ranlux_base_01", a1
, 0.4731388F
);
135 validate("ranlux_base_01", a2
, 0.4731388F
);
136 validate("ranlux_base_01", a1
, 0.4731388F
);
138 seed_t
s1(19780503UL);
140 validate("ranlux_base_01", a2
, 0.4731388F
);
141 validate("ranlux_base_01", a1
, 0.4731388F
);
142 a1
= ranlux_base_01(0);
143 seed_t
s2(19780503UL);
144 a2
= ranlux_base_01(s2
);
145 validate("ranlux_base_01", a2
, 0.4731388F
);
146 validate("ranlux_base_01", a1
, 0.4731388F
);
150 typedef std::tr1::linear_congruential
< :: boost::int32_t, 40014, 0, 2147483563> seed_t
;
151 typedef std::tr1::subtract_with_carry_01
< double, 48, 10, 24> ranlux64_base_01
;
153 ranlux64_base_01
a2(a1
);
154 validate("ranlux64_base_01", a1
, 0.1332451100961265);
156 validate("ranlux64_base_01", a2
, 0.1332451100961265);
157 validate("ranlux64_base_01", a1
, 0.1332451100961265);
159 seed_t
s1(19780503UL);
161 validate("ranlux64_base_01", a2
, 0.1332451100961265);
162 validate("ranlux64_base_01", a1
, 0.1332451100961265);
163 a1
= ranlux64_base_01(0);
164 seed_t
s2(19780503UL);
165 a2
= ranlux64_base_01(s2
);
166 validate("ranlux64_base_01", a2
, 0.1332451100961265);
167 validate("ranlux64_base_01", a1
, 0.1332451100961265);
171 typedef std::tr1::linear_congruential
< :: boost::int32_t, 40014, 0, 2147483563> seed_t
;
172 typedef std::tr1::subtract_with_carry
< ::boost::int32_t, (1<<24), 10, 24> sub_t
;
173 typedef std::tr1::xor_combine
<sub_t
, 0, sub_t
, 3> xor_t
;
175 validate("xor_combine", a1
, 61989536u);
178 validate("xor_combine", a1
, 61989536u);
180 validate("xor_combine", a2
, 61989536u);
181 validate("xor_combine", a1
, 61989536u);
183 seed_t
s1(19780503UL);
185 validate("xor_combine", a2
, 90842400u);
186 validate("xor_combine", a1
, 114607192u);
188 seed_t
s2(19780503UL);
190 validate("xor_combine", a2
, 90842400u);
191 validate("xor_combine", a1
, 114607192u);
195 std::tr1::minstd_rand0 r1
;
196 validate("std::tr1::minstd_rand0", r1
, 1043618065u);
197 std::tr1::minstd_rand r2
;
198 validate("std::tr1::minstd_rand", r2
, 399268537u);
199 std::tr1::mt19937 r3
;
200 validate("std::tr1::mt19937", r3
, 4123659995u);
201 std::tr1::ranlux3 r4
;
202 validate("std::tr1::ranlux3", r4
, 5957620u);
203 std::tr1::ranlux4 r5
;
204 validate("std::tr1::ranlux4", r5
, 8587295u);
205 std::tr1::ranlux3_01 r6
;
206 validate("std::tr1::ranlux3_01", r6
, 5957620.0F
/std::pow(2.0f
,24));
207 std::tr1::ranlux4_01 r7
;
208 validate("std::tr1::ranlux4_01", r7
, 8587295.0F
/std::pow(2.0f
,24));