]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/random/test/test_discrete.cpp
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)
12 #include <boost/random/discrete_distribution.hpp>
13 #include <boost/random/uniform_int.hpp>
14 #include <boost/random/mersenne_twister.hpp>
15 #include <boost/lexical_cast.hpp>
16 #include <boost/exception/diagnostic_information.hpp>
21 #include "chi_squared_test.hpp"
23 bool do_test(int n
, long long max
) {
24 std::cout
<< "running discrete(p0, p1, ..., p" << n
-1 << ")" << " " << max
<< " times: " << std::flush
;
26 std::vector
<double> expected
;
29 for(int i
= 0; i
< n
; ++i
) {
30 expected
.push_back(egen());
32 double sum
= std::accumulate(expected
.begin(), expected
.end(), 0.0);
33 for(std::vector
<double>::iterator iter
= expected
.begin(), end
= expected
.end(); iter
!= end
; ++iter
) {
38 boost::random::discrete_distribution
<> dist(expected
);
40 std::vector
<long long> results(expected
.size());
41 for(long long i
= 0; i
< max
; ++i
) {
45 long long sum
= std::accumulate(results
.begin(), results
.end(), 0ll);
47 std::cout
<< "*** Failed: incorrect total: " << sum
<< " ***" << std::endl
;
50 double chsqr
= chi_squared_test(results
, expected
, max
);
52 bool result
= chsqr
< 0.99;
53 const char* err
= result
? "" : "*";
54 std::cout
<< std::setprecision(17) << chsqr
<< err
<< std::endl
;
56 std::cout
<< std::setprecision(6);
61 bool do_tests(int repeat
, int max_n
, long long trials
) {
63 boost::uniform_int
<> idist(1, max_n
);
65 for(int i
= 0; i
< repeat
; ++i
) {
66 if(!do_test(idist(gen
), trials
)) {
71 std::cout
<< "*** " << errors
<< " errors detected ***" << std::endl
;
77 std::cerr
<< "Usage: test_discrete -r <repeat> -n <max n> -t <trials>" << std::endl
;
82 bool handle_option(int& argc
, char**& argv
, char opt
, T
& value
) {
83 if(argv
[0][1] == opt
&& argc
> 1) {
86 value
= boost::lexical_cast
<T
>(argv
[0]);
93 int main(int argc
, char** argv
) {
96 long long trials
= 1000000ll;
103 if(argv
[0][0] != '-') return usage();
104 else if(!handle_option(argc
, argv
, 'r', repeat
)
105 && !handle_option(argc
, argv
, 'n', max_n
)
106 && !handle_option(argc
, argv
, 't', trials
)) {
114 if(do_tests(repeat
, max_n
, trials
)) {
120 std::cerr
<< boost::current_exception_diagnostic_information() << std::endl
;