| 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/tools/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 | #include <sstream> |
| 14 | #include <boost/archive/text_oarchive.hpp> |
| 15 | #include <boost/archive/text_iarchive.hpp> |
| 16 | |
| 17 | using namespace boost; |
| 18 | using namespace unit_test; |
| 19 | using namespace accumulators; |
| 20 | |
| 21 | template<typename T> |
| 22 | void assert_is_double(T const &) |
| 23 | { |
| 24 | BOOST_MPL_ASSERT((is_same<T, double>)); |
| 25 | } |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // test_rolling_moment |
| 29 | // |
| 30 | |
| 31 | void test_rolling_second_moment() |
| 32 | { |
| 33 | accumulator_set<int, stats<tag::rolling_moment<2> > > acc(tag::rolling_moment<2>::window_size = 3); |
| 34 | |
| 35 | acc(2); |
| 36 | |
| 37 | BOOST_CHECK_CLOSE(rolling_moment<2>(acc), 4.0/1 ,1e-5); |
| 38 | |
| 39 | acc(4); |
| 40 | |
| 41 | BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (4.0 + 16.0)/2, 1e-5); |
| 42 | |
| 43 | acc(5); |
| 44 | |
| 45 | BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (4.0 + 16.0 + 25.0)/3, 1e-5); |
| 46 | |
| 47 | acc(6); |
| 48 | |
| 49 | BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (16.0 + 25.0 + 36.0)/3, 1e-5); |
| 50 | |
| 51 | assert_is_double(rolling_moment<2>(arg1: acc)); |
| 52 | } |
| 53 | |
| 54 | void test_rolling_fifth_moment() |
| 55 | { |
| 56 | accumulator_set<int, stats<tag::rolling_moment<5> > > acc(tag::rolling_moment<2>::window_size = 3); |
| 57 | |
| 58 | acc(2); |
| 59 | |
| 60 | BOOST_CHECK_CLOSE(rolling_moment<5>(acc), 32.0/1, 1e-5); |
| 61 | |
| 62 | acc(3); |
| 63 | |
| 64 | BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (32.0 + 243.0)/2, 1e-5); |
| 65 | |
| 66 | acc(4); |
| 67 | |
| 68 | BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (32.0 + 243.0 + 1024.0)/3, 1e-5); |
| 69 | |
| 70 | acc(5); |
| 71 | |
| 72 | BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (243.0 + 1024.0 + 3125.0)/3, 1e-5); |
| 73 | |
| 74 | assert_is_double(rolling_moment<5>(arg1: acc)); |
| 75 | } |
| 76 | |
| 77 | /////////////////////////////////////////////////////////////////////////////// |
| 78 | // test_persistency |
| 79 | // |
| 80 | void test_persistency() |
| 81 | { |
| 82 | std::stringstream ss; |
| 83 | { |
| 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); |
| 86 | |
| 87 | acc2(2); acc5(2); |
| 88 | acc2(4); acc5(3); |
| 89 | acc2(5); acc5(4); |
| 90 | acc2(6); acc5(5); |
| 91 | |
| 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(ar&: oa, file_version: 0); |
| 96 | acc5.serialize(ar&: oa, file_version: 0); |
| 97 | } |
| 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(ar&: ia, file_version: 0); |
| 102 | acc5.serialize(ar&: ia, file_version: 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); |
| 105 | } |
| 106 | |
| 107 | /////////////////////////////////////////////////////////////////////////////// |
| 108 | // init_unit_test_suite |
| 109 | // |
| 110 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 111 | { |
| 112 | test_suite *test = BOOST_TEST_SUITE("rolling moment test" ); |
| 113 | |
| 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)); |
| 117 | |
| 118 | return test; |
| 119 | } |
| 120 | |