1 | // (C) Copyright Eric Niebler 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) |
5 | |
6 | #include <boost/test/unit_test.hpp> |
7 | #include <boost/test/tools/floating_point_comparison.hpp> |
8 | #include <boost/accumulators/accumulators.hpp> |
9 | #include <boost/accumulators/statistics/stats.hpp> |
10 | #include <boost/accumulators/statistics/weighted_mean.hpp> |
11 | #include <boost/accumulators/statistics/variates/covariate.hpp> |
12 | #include <sstream> |
13 | #include <boost/archive/text_oarchive.hpp> |
14 | #include <boost/archive/text_iarchive.hpp> |
15 | |
16 | using namespace boost; |
17 | using namespace unit_test; |
18 | using namespace accumulators; |
19 | |
20 | template<typename T> |
21 | void assert_is_double(T const &) |
22 | { |
23 | BOOST_MPL_ASSERT((is_same<T, double>)); |
24 | } |
25 | |
26 | typedef accumulator_set<int, stats<tag::mean, |
27 | tag::mean_of_variates<int, tag::covariate1> > > mean_t; |
28 | |
29 | typedef accumulator_set<int, stats<tag::mean(immediate), |
30 | tag::mean_of_variates<int, tag::covariate1>(immediate) > > immediate_mean_t; |
31 | |
32 | /////////////////////////////////////////////////////////////////////////////// |
33 | // test_stat |
34 | // |
35 | void test_stat() |
36 | { |
37 | mean_t acc, test_acc(sample = 0); |
38 | |
39 | acc(1, covariate1 = 3); |
40 | BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); |
41 | BOOST_CHECK_EQUAL(1u, count(acc)); |
42 | BOOST_CHECK_EQUAL(1, sum(acc)); |
43 | BOOST_CHECK_CLOSE(3., (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5); |
44 | |
45 | acc(0, covariate1 = 4); |
46 | BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); |
47 | BOOST_CHECK_EQUAL(2u, count(acc)); |
48 | BOOST_CHECK_EQUAL(1, sum(acc)); |
49 | BOOST_CHECK_CLOSE(3.5, (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5); |
50 | |
51 | acc(2, covariate1 = 8); |
52 | BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); |
53 | BOOST_CHECK_EQUAL(3u, count(acc)); |
54 | BOOST_CHECK_EQUAL(3, sum(acc)); |
55 | BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5); |
56 | |
57 | assert_is_double(mean(acc)); |
58 | |
59 | immediate_mean_t acc2, test_acc2(sample = 0); |
60 | |
61 | acc2(1, covariate1 = 3); |
62 | BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); |
63 | BOOST_CHECK_EQUAL(1u, count(acc2)); |
64 | BOOST_CHECK_CLOSE(3., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); |
65 | |
66 | acc2(0, covariate1 = 4); |
67 | BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5); |
68 | BOOST_CHECK_EQUAL(2u, count(acc2)); |
69 | BOOST_CHECK_CLOSE(3.5, (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); |
70 | |
71 | acc2(2, covariate1 = 8); |
72 | BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); |
73 | BOOST_CHECK_EQUAL(3u, count(acc2)); |
74 | BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); |
75 | |
76 | assert_is_double(mean(acc2)); |
77 | } |
78 | |
79 | /////////////////////////////////////////////////////////////////////////////// |
80 | // test_persistency |
81 | // |
82 | void test_persistency() |
83 | { |
84 | std::stringstream ss; |
85 | { |
86 | mean_t acc1; |
87 | immediate_mean_t acc2; |
88 | |
89 | acc1(1, covariate1 = 3); |
90 | acc1(0, covariate1 = 4); |
91 | acc1(2, covariate1 = 8); |
92 | acc2(1, covariate1 = 3); |
93 | acc2(0, covariate1 = 4); |
94 | acc2(2, covariate1 = 8); |
95 | BOOST_CHECK_CLOSE(1., mean(acc1), 1e-5); |
96 | BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc1)), 1e-5); |
97 | BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); |
98 | BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); |
99 | boost::archive::text_oarchive oa(ss); |
100 | acc1.serialize(ar&: oa, file_version: 0); |
101 | acc2.serialize(ar&: oa, file_version: 0); |
102 | } |
103 | mean_t acc1; |
104 | immediate_mean_t acc2; |
105 | boost::archive::text_iarchive ia(ss); |
106 | acc1.serialize(ar&: ia, file_version: 0); |
107 | acc2.serialize(ar&: ia, file_version: 0); |
108 | BOOST_CHECK_CLOSE(1., mean(acc1), 1e-5); |
109 | BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); |
110 | } |
111 | |
112 | /////////////////////////////////////////////////////////////////////////////// |
113 | // init_unit_test_suite |
114 | // |
115 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
116 | { |
117 | test_suite *test = BOOST_TEST_SUITE("mean test" ); |
118 | |
119 | test->add(BOOST_TEST_CASE(&test_stat)); |
120 | test->add(BOOST_TEST_CASE(&test_persistency)); |
121 | |
122 | return test; |
123 | } |
124 | |