]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/accumulators/test/rolling_moment.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / accumulators / test / rolling_moment.cpp
1 // Copyright (C) Eric Niebler 2005.
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/accumulators/accumulators.hpp>
11 #include <boost/accumulators/statistics/stats.hpp>
12 #include <boost/accumulators/statistics/rolling_moment.hpp>
13
14 using namespace boost;
15 using namespace unit_test;
16 using namespace accumulators;
17
18 template<typename T>
19 void assert_is_double(T const &)
20 {
21 BOOST_MPL_ASSERT((is_same<T, double>));
22 }
23
24 ///////////////////////////////////////////////////////////////////////////////
25 // test_rolling_moment
26 //
27
28 void test_rolling_second_moment()
29 {
30 accumulator_set<int, stats<tag::rolling_moment<2> > > acc(tag::rolling_moment<2>::window_size = 3);
31
32 acc(2);
33
34 BOOST_CHECK_CLOSE(rolling_moment<2>(acc), 4.0/1 ,1e-5);
35
36 acc(4);
37
38 BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (4.0 + 16.0)/2, 1e-5);
39
40 acc(5);
41
42 BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (4.0 + 16.0 + 25.0)/3, 1e-5);
43
44 acc(6);
45
46 BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (16.0 + 25.0 + 36.0)/3, 1e-5);
47
48 assert_is_double(rolling_moment<2>(acc));
49 }
50
51 void test_rolling_fifth_moment()
52 {
53 accumulator_set<int, stats<tag::rolling_moment<5> > > acc(tag::rolling_moment<2>::window_size = 3);
54
55 acc(2);
56
57 BOOST_CHECK_CLOSE(rolling_moment<5>(acc), 32.0/1, 1e-5);
58
59 acc(3);
60
61 BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (32.0 + 243.0)/2, 1e-5);
62
63 acc(4);
64
65 BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (32.0 + 243.0 + 1024.0)/3, 1e-5);
66
67 acc(5);
68
69 BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (243.0 + 1024.0 + 3125.0)/3, 1e-5);
70
71 assert_is_double(rolling_moment<5>(acc));
72 }
73
74 ///////////////////////////////////////////////////////////////////////////////
75 // init_unit_test_suite
76 //
77 test_suite* init_unit_test_suite( int argc, char* argv[] )
78 {
79 test_suite *test = BOOST_TEST_SUITE("rolling moment test");
80
81 test->add(BOOST_TEST_CASE(&test_rolling_second_moment));
82 test->add(BOOST_TEST_CASE(&test_rolling_fifth_moment));
83
84 return test;
85 }