1 // Copyright (C) Eric Niebler 2008.
2 // Copyright (C) Pieter Bastiaan Ober 2014.
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #include <boost/test/unit_test.hpp>
8 #include <boost/test/floating_point_comparison.hpp>
9 #include <boost/mpl/assert.hpp>
10 #include <boost/type_traits/is_same.hpp>
11 #include <boost/accumulators/accumulators.hpp>
12 #include <boost/accumulators/statistics/stats.hpp>
13 #include <boost/accumulators/statistics/rolling_mean.hpp>
15 using namespace boost
;
16 using namespace unit_test
;
17 using namespace accumulators
;
20 void assert_is_double(T
const &)
22 BOOST_MPL_ASSERT((is_same
<T
, double>));
25 // test_rolling_mean_test_impl
26 // implements a test for window_size = 5
27 size_t window_size
= 5;
29 template<typename accumulator_set_type
>
31 test_rolling_mean_test_impl(accumulator_set_type
& acc
)
34 BOOST_CHECK_CLOSE(1., rolling_mean(acc
), 1e-5);
37 BOOST_CHECK_CLOSE(1.5, rolling_mean(acc
), 1e-5);
40 BOOST_CHECK_CLOSE(2., rolling_mean(acc
), 1e-5);
43 BOOST_CHECK_CLOSE(2.5, rolling_mean(acc
), 1e-5);
46 BOOST_CHECK_CLOSE(3., rolling_mean(acc
), 1e-5);
49 BOOST_CHECK_CLOSE(4., rolling_mean(acc
), 1e-5);
52 BOOST_CHECK_CLOSE(5., rolling_mean(acc
), 1e-5);
54 assert_is_double(rolling_mean(acc
));
57 ///////////////////////////////////////////////////////////////////////////////
59 void test_rolling_mean()
61 accumulator_set
<int,stats
<tag::immediate_rolling_mean
> >
62 acc_immediate_rolling_mean(tag::immediate_rolling_mean::window_size
= window_size
),
63 acc_immediate_rolling_mean2(tag::immediate_rolling_mean::window_size
= window_size
, sample
= 0);
65 accumulator_set
<int,stats
<tag::rolling_mean(immediate
)> >
66 acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size
= window_size
);
68 accumulator_set
<int,stats
<tag::lazy_rolling_mean
> >
69 acc_lazy_rolling_mean(tag::lazy_rolling_mean::window_size
= window_size
),
70 acc_lazy_rolling_mean2(tag::lazy_rolling_mean::window_size
= window_size
, sample
= 0);
72 accumulator_set
<int,stats
<tag::rolling_mean(lazy
)> >
73 acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size
= window_size
);
75 accumulator_set
<int,stats
<tag::rolling_mean
> >
76 acc_default_rolling_mean(tag::rolling_mean::window_size
= window_size
),
77 acc_default_rolling_mean2(tag::rolling_mean::window_size
= window_size
, sample
= 0);
79 //// test the different implementations
80 test_rolling_mean_test_impl(acc_lazy_rolling_mean
);
81 test_rolling_mean_test_impl(acc_default_rolling_mean
);
82 test_rolling_mean_test_impl(acc_immediate_rolling_mean
);
84 test_rolling_mean_test_impl(acc_lazy_rolling_mean2
);
85 test_rolling_mean_test_impl(acc_default_rolling_mean2
);
86 test_rolling_mean_test_impl(acc_immediate_rolling_mean2
);
88 test_rolling_mean_test_impl(acc_lazy_rolling_mean3
);
89 test_rolling_mean_test_impl(acc_immediate_rolling_mean3
);
91 //// test that the default implementation is the 'immediate' computation
92 BOOST_REQUIRE(sizeof(acc_lazy_rolling_mean
) != sizeof(acc_immediate_rolling_mean
));
93 BOOST_CHECK (sizeof(acc_default_rolling_mean
) == sizeof(acc_immediate_rolling_mean
));
95 //// test the equivalence of the different ways to indicate a feature
96 BOOST_CHECK (sizeof(acc_lazy_rolling_mean
) == sizeof(acc_lazy_rolling_mean2
));
97 BOOST_CHECK (sizeof(acc_lazy_rolling_mean
) == sizeof(acc_lazy_rolling_mean3
));
98 BOOST_CHECK (sizeof(acc_immediate_rolling_mean
) == sizeof(acc_immediate_rolling_mean2
));
99 BOOST_CHECK (sizeof(acc_immediate_rolling_mean
) == sizeof(acc_immediate_rolling_mean3
));
102 ///////////////////////////////////////////////////////////////////////////////
103 // init_unit_test_suite
105 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
107 test_suite
*test
= BOOST_TEST_SUITE("rolling mean test");
109 test
->add(BOOST_TEST_CASE(&test_rolling_mean
));