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)
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>
14 #include <boost/archive/text_oarchive.hpp>
15 #include <boost/archive/text_iarchive.hpp>
17 using namespace boost
;
18 using namespace unit_test
;
19 using namespace accumulators
;
22 void assert_is_double(T
const &)
24 BOOST_MPL_ASSERT((is_same
<T
, double>));
27 ///////////////////////////////////////////////////////////////////////////////
28 // test_rolling_moment
31 void test_rolling_second_moment()
33 accumulator_set
<int, stats
<tag::rolling_moment
<2> > > acc(tag::rolling_moment
<2>::window_size
= 3);
37 BOOST_CHECK_CLOSE(rolling_moment
<2>(acc
), 4.0/1 ,1e-5);
41 BOOST_CHECK_CLOSE(rolling_moment
<2>(acc
), (4.0 + 16.0)/2, 1e-5);
45 BOOST_CHECK_CLOSE(rolling_moment
<2>(acc
), (4.0 + 16.0 + 25.0)/3, 1e-5);
49 BOOST_CHECK_CLOSE(rolling_moment
<2>(acc
), (16.0 + 25.0 + 36.0)/3, 1e-5);
51 assert_is_double(rolling_moment
<2>(acc
));
54 void test_rolling_fifth_moment()
56 accumulator_set
<int, stats
<tag::rolling_moment
<5> > > acc(tag::rolling_moment
<2>::window_size
= 3);
60 BOOST_CHECK_CLOSE(rolling_moment
<5>(acc
), 32.0/1, 1e-5);
64 BOOST_CHECK_CLOSE(rolling_moment
<5>(acc
), (32.0 + 243.0)/2, 1e-5);
68 BOOST_CHECK_CLOSE(rolling_moment
<5>(acc
), (32.0 + 243.0 + 1024.0)/3, 1e-5);
72 BOOST_CHECK_CLOSE(rolling_moment
<5>(acc
), (243.0 + 1024.0 + 3125.0)/3, 1e-5);
74 assert_is_double(rolling_moment
<5>(acc
));
77 ///////////////////////////////////////////////////////////////////////////////
80 void test_persistency()
84 accumulator_set
<int, stats
<tag::rolling_moment
<2> > > acc2(tag::rolling_moment
<2>::window_size
= 3);
85 accumulator_set
<int, stats
<tag::rolling_moment
<5> > > acc5(tag::rolling_moment
<5>::window_size
= 3);
92 BOOST_CHECK_CLOSE(rolling_moment
<2>(acc2
), (16.0 + 25.0 + 36.0)/3, 1e-5);
93 BOOST_CHECK_CLOSE(rolling_moment
<5>(acc5
), (243.0 + 1024.0 + 3125.0)/3, 1e-5);
94 boost::archive::text_oarchive
oa(ss
);
95 acc2
.serialize(oa
, 0);
96 acc5
.serialize(oa
, 0);
98 accumulator_set
<int, stats
<tag::rolling_moment
<2> > > acc2(tag::rolling_moment
<2>::window_size
= 3);
99 accumulator_set
<int, stats
<tag::rolling_moment
<5> > > acc5(tag::rolling_moment
<5>::window_size
= 3);
100 boost::archive::text_iarchive
ia(ss
);
101 acc2
.serialize(ia
, 0);
102 acc5
.serialize(ia
, 0);
103 BOOST_CHECK_CLOSE(rolling_moment
<2>(acc2
), (16.0 + 25.0 + 36.0)/3, 1e-5);
104 BOOST_CHECK_CLOSE(rolling_moment
<5>(acc5
), (243.0 + 1024.0 + 3125.0)/3, 1e-5);
107 ///////////////////////////////////////////////////////////////////////////////
108 // init_unit_test_suite
110 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
112 test_suite
*test
= BOOST_TEST_SUITE("rolling moment test");
114 test
->add(BOOST_TEST_CASE(&test_rolling_second_moment
));
115 test
->add(BOOST_TEST_CASE(&test_rolling_fifth_moment
));
116 test
->add(BOOST_TEST_CASE(&test_persistency
));