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/mean.hpp>
11
12using namespace boost;
13using namespace unit_test;
14using namespace accumulators;
15
16template<typename T>
17void assert_is_double(T const &)
18{
19 BOOST_MPL_ASSERT((is_same<T, double>));
20}
21
22///////////////////////////////////////////////////////////////////////////////
23// test_stat
24//
25void test_stat()
26{
27 accumulator_set<int, stats<droppable<tag::mean> > > acc, test_acc(sample = 0);
28
29 acc(1);
30 BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
31 BOOST_CHECK_EQUAL(1u, count(acc));
32 BOOST_CHECK_EQUAL(1, sum(acc));
33
34 acc(0);
35 BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
36 BOOST_CHECK_EQUAL(2u, count(acc));
37 BOOST_CHECK_EQUAL(1, sum(acc));
38
39 acc.drop<tag::mean>();
40
41 acc(2);
42 BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
43 BOOST_CHECK_EQUAL(2u, count(acc));
44 BOOST_CHECK_EQUAL(1, sum(acc));
45
46 assert_is_double(mean(acc));
47
48
49 accumulator_set<int, stats<droppable<tag::mean(immediate)> > > acc2, test_acc2(sample = 0);
50
51 acc2(1);
52 BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5);
53 BOOST_CHECK_EQUAL(1u, count(acc2));
54
55 acc2(0);
56 BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5);
57 BOOST_CHECK_EQUAL(2u, count(acc2));
58
59 acc2.drop<tag::mean>();
60
61 acc2(2);
62 BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5);
63 BOOST_CHECK_EQUAL(2u, count(acc2));
64
65 assert_is_double(mean(acc2));
66}
67
68///////////////////////////////////////////////////////////////////////////////
69// test_stat2
70//
71void test_stat2()
72{
73 accumulator_set<int, stats<droppable<tag::sum>, droppable<tag::mean> > > acc, test_acc(sample = 0);
74
75 acc(1);
76 BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
77 BOOST_CHECK_EQUAL(1u, count(acc));
78 BOOST_CHECK_EQUAL(1, sum(acc));
79
80 acc(0);
81 BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
82 BOOST_CHECK_EQUAL(2u, count(acc));
83 BOOST_CHECK_EQUAL(1, sum(acc));
84
85 acc.drop<tag::mean>();
86 acc.drop<tag::sum>();
87
88 acc(2);
89 BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
90 BOOST_CHECK_EQUAL(2u, count(acc));
91 BOOST_CHECK_EQUAL(1, sum(acc));
92
93 assert_is_double(mean(acc));
94}
95
96///////////////////////////////////////////////////////////////////////////////
97// test_stat3
98//
99void test_stat3()
100{
101 accumulator_set<int, stats<droppable<tag::sum>, droppable<tag::count>, droppable<tag::mean> > > acc, test_acc(sample = 0);
102
103 acc(1);
104 BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
105 BOOST_CHECK_EQUAL(1u, count(acc));
106 BOOST_CHECK_EQUAL(1, sum(acc));
107
108 acc(0);
109 BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
110 BOOST_CHECK_EQUAL(2u, count(acc));
111 BOOST_CHECK_EQUAL(1, sum(acc));
112
113 acc.drop<tag::mean>();
114 acc.drop<tag::sum>();
115
116 acc(2);
117 BOOST_CHECK_CLOSE(1./3., mean(acc), 1e-5);
118 BOOST_CHECK_EQUAL(3u, count(acc));
119 BOOST_CHECK_EQUAL(1, sum(acc));
120
121 acc.drop<tag::count>();
122 acc(3);
123 BOOST_CHECK_CLOSE(1./3., mean(acc), 1e-5);
124 BOOST_CHECK_EQUAL(3u, count(acc));
125 BOOST_CHECK_EQUAL(1, sum(acc));
126
127 assert_is_double(mean(acc));
128}
129
130///////////////////////////////////////////////////////////////////////////////
131// init_unit_test_suite
132//
133test_suite* init_unit_test_suite( int argc, char* argv[] )
134{
135 test_suite *test = BOOST_TEST_SUITE("droppable test");
136
137 test->add(BOOST_TEST_CASE(&test_stat));
138 test->add(BOOST_TEST_CASE(&test_stat2));
139 test->add(BOOST_TEST_CASE(&test_stat3));
140
141 return test;
142}
143

source code of boost/libs/accumulators/test/droppable.cpp