]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/accumulators/test/rolling_mean.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / accumulators / test / rolling_mean.cpp
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)
6
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>
14
15 using namespace boost;
16 using namespace unit_test;
17 using namespace accumulators;
18
19 template<typename T>
20 void assert_is_double(T const &)
21 {
22 BOOST_MPL_ASSERT((is_same<T, double>));
23 }
24
25 // test_rolling_mean_test_impl
26 // implements a test for window_size = 5
27 size_t window_size = 5;
28
29 template<typename accumulator_set_type>
30 void
31 test_rolling_mean_test_impl(accumulator_set_type& acc)
32 {
33 acc(1);
34 BOOST_CHECK_CLOSE(1., rolling_mean(acc), 1e-5);
35
36 acc(2);
37 BOOST_CHECK_CLOSE(1.5, rolling_mean(acc), 1e-5);
38
39 acc(3);
40 BOOST_CHECK_CLOSE(2., rolling_mean(acc), 1e-5);
41
42 acc(4);
43 BOOST_CHECK_CLOSE(2.5, rolling_mean(acc), 1e-5);
44
45 acc(5);
46 BOOST_CHECK_CLOSE(3., rolling_mean(acc), 1e-5);
47
48 acc(6);
49 BOOST_CHECK_CLOSE(4., rolling_mean(acc), 1e-5);
50
51 acc(7);
52 BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5);
53
54 assert_is_double(rolling_mean(acc));
55 }
56
57 ///////////////////////////////////////////////////////////////////////////////
58 // test_rolling_mean
59 void test_rolling_mean()
60 {
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);
64
65 accumulator_set<int,stats<tag::rolling_mean(immediate)> >
66 acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size = window_size);
67
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);
71
72 accumulator_set<int,stats<tag::rolling_mean(lazy)> >
73 acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size = window_size);
74
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);
78
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);
83
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);
87
88 test_rolling_mean_test_impl(acc_lazy_rolling_mean3);
89 test_rolling_mean_test_impl(acc_immediate_rolling_mean3);
90
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));
94
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));
100 }
101
102 ///////////////////////////////////////////////////////////////////////////////
103 // init_unit_test_suite
104 //
105 test_suite* init_unit_test_suite( int argc, char* argv[] )
106 {
107 test_suite *test = BOOST_TEST_SUITE("rolling mean test");
108
109 test->add(BOOST_TEST_CASE(&test_rolling_mean));
110
111 return test;
112 }