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
16namespace my
17{
18 BOOST_PARAMETER_KEYWORD(tag, sum_acc)
19}
20
21///////////////////////////////////////////////////////////////////////////////
22// test_stat
23//
24void test_stat()
25{
26 accumulator_set<int, stats<tag::mean, tag::external<tag::sum, my::tag::sum_acc> > > acc;
27 accumulator_set<int, stats<tag::sum> > sum_acc;
28
29 acc(1);
30 sum_acc(1);
31 BOOST_CHECK_CLOSE(1., mean(acc, my::sum_acc = sum_acc), 1e-5);
32 BOOST_CHECK_EQUAL(1u, count(acc));
33 BOOST_CHECK_EQUAL(1, sum(sum_acc));
34
35 acc(0);
36 sum_acc(0);
37 BOOST_CHECK_CLOSE(0.5, mean(acc, my::sum_acc = sum_acc), 1e-5);
38 BOOST_CHECK_EQUAL(2u, count(acc));
39 BOOST_CHECK_EQUAL(1, sum(acc, my::sum_acc = sum_acc));
40
41 acc(2);
42 sum_acc(2);
43 BOOST_CHECK_CLOSE(1., mean(acc, my::sum_acc = sum_acc), 1e-5);
44 BOOST_CHECK_EQUAL(3u, count(acc));
45 BOOST_CHECK_EQUAL(3, sum(acc, my::sum_acc = sum_acc));
46}
47
48///////////////////////////////////////////////////////////////////////////////
49// test_reference
50//
51void test_reference()
52{
53 typedef accumulator_set<int, stats<tag::sum> > sum_acc_type;
54 sum_acc_type sum_acc;
55 accumulator_set<
56 int
57 , stats<
58 tag::mean
59 , tag::external<tag::sum, my::tag::sum_acc>
60 , tag::reference<sum_acc_type, my::tag::sum_acc>
61 >
62 > acc(my::sum_acc = sum_acc);
63
64 acc(1);
65 sum_acc(1);
66 BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
67 BOOST_CHECK_EQUAL(1u, count(acc));
68 BOOST_CHECK_EQUAL(1, sum(sum_acc));
69
70 acc(0);
71 sum_acc(0);
72 BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
73 BOOST_CHECK_EQUAL(2u, count(acc));
74 BOOST_CHECK_EQUAL(1, sum(acc));
75
76 acc(2);
77 sum_acc(2);
78 BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
79 BOOST_CHECK_EQUAL(3u, count(acc));
80 BOOST_CHECK_EQUAL(3, sum(acc));
81}
82
83///////////////////////////////////////////////////////////////////////////////
84// test_reference2
85//
86void test_reference2()
87{
88 typedef accumulator_set<int, stats<tag::sum> > sum_acc_type;
89 sum_acc_type sum_acc;
90 accumulator_set<
91 int
92 , stats<
93 tag::mean
94 , tag::external<tag::sum, my::tag::sum_acc, sum_acc_type>
95 >
96 > acc(my::sum_acc = sum_acc);
97
98 acc(1);
99 sum_acc(1);
100 BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
101 BOOST_CHECK_EQUAL(1u, count(acc));
102 BOOST_CHECK_EQUAL(1, sum(sum_acc));
103
104 acc(0);
105 sum_acc(0);
106 BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
107 BOOST_CHECK_EQUAL(2u, count(acc));
108 BOOST_CHECK_EQUAL(1, sum(acc));
109
110 acc(2);
111 sum_acc(2);
112 BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
113 BOOST_CHECK_EQUAL(3u, count(acc));
114 BOOST_CHECK_EQUAL(3, sum(acc));
115}
116
117///////////////////////////////////////////////////////////////////////////////
118// init_unit_test_suite
119//
120test_suite* init_unit_test_suite( int argc, char* argv[] )
121{
122 test_suite *test = BOOST_TEST_SUITE("external_accumulator test");
123
124 test->add(BOOST_TEST_CASE(&test_stat));
125 test->add(BOOST_TEST_CASE(&test_reference));
126 test->add(BOOST_TEST_CASE(&test_reference2));
127
128 return test;
129}
130

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