rolling_mean.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #include <boost/test/unit_test.hpp>
  7. #include <boost/test/floating_point_comparison.hpp>
  8. #include <boost/mpl/assert.hpp>
  9. #include <boost/type_traits/is_same.hpp>
  10. #include <boost/accumulators/accumulators.hpp>
  11. #include <boost/accumulators/statistics/stats.hpp>
  12. #include <boost/accumulators/statistics/rolling_mean.hpp>
  13. #include <sstream>
  14. #include <boost/archive/text_oarchive.hpp>
  15. #include <boost/archive/text_iarchive.hpp>
  16. using namespace boost;
  17. using namespace unit_test;
  18. using namespace accumulators;
  19. template<typename T>
  20. void assert_is_double(T const &)
  21. {
  22. BOOST_MPL_ASSERT((is_same<T, double>));
  23. }
  24. // test_rolling_mean_test_impl
  25. // implements a test for window_size = 5
  26. size_t window_size = 5;
  27. template<typename accumulator_set_type>
  28. void
  29. test_rolling_mean_test_impl(accumulator_set_type& acc)
  30. {
  31. acc(1);
  32. BOOST_CHECK_CLOSE(1., rolling_mean(acc), 1e-5);
  33. acc(2);
  34. BOOST_CHECK_CLOSE(1.5, rolling_mean(acc), 1e-5);
  35. acc(3);
  36. BOOST_CHECK_CLOSE(2., rolling_mean(acc), 1e-5);
  37. acc(4);
  38. BOOST_CHECK_CLOSE(2.5, rolling_mean(acc), 1e-5);
  39. acc(5);
  40. BOOST_CHECK_CLOSE(3., rolling_mean(acc), 1e-5);
  41. acc(6);
  42. BOOST_CHECK_CLOSE(4., rolling_mean(acc), 1e-5);
  43. acc(7);
  44. BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5);
  45. assert_is_double(rolling_mean(acc));
  46. }
  47. template<typename accumulator_set_type>
  48. void
  49. test_rolling_mean_unsigned_test_impl(accumulator_set_type& acc)
  50. {
  51. acc(7U);
  52. BOOST_CHECK_CLOSE(7., rolling_mean(acc), 1e-5);
  53. acc(6U);
  54. BOOST_CHECK_CLOSE(6.5, rolling_mean(acc), 1e-5);
  55. acc(5U);
  56. BOOST_CHECK_CLOSE(6., rolling_mean(acc), 1e-5);
  57. acc(4U);
  58. BOOST_CHECK_CLOSE(5.5, rolling_mean(acc), 1e-5);
  59. acc(3U);
  60. BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5);
  61. acc(2U);
  62. BOOST_CHECK_CLOSE(4., rolling_mean(acc), 1e-5);
  63. acc(1U);
  64. BOOST_CHECK_CLOSE(3., rolling_mean(acc), 1e-5);
  65. assert_is_double(rolling_mean(acc));
  66. }
  67. ///////////////////////////////////////////////////////////////////////////////
  68. // test_persistency_impl
  69. //
  70. template<typename accumulator_set_type>
  71. void test_persistency_impl(accumulator_set_type& acc)
  72. {
  73. std::stringstream ss;
  74. {
  75. acc(1);
  76. acc(2);
  77. acc(3);
  78. acc(4);
  79. acc(5);
  80. acc(6);
  81. acc(7);
  82. BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5);
  83. boost::archive::text_oarchive oa(ss);
  84. acc.serialize(oa, 0);
  85. }
  86. // initialize from acc to make sure all values are passed
  87. accumulator_set_type other_acc = acc;
  88. // accumulate more, to make sure that deserialization set the right value
  89. // and not the copy ctor
  90. other_acc(100);
  91. other_acc(100);
  92. other_acc(100);
  93. other_acc(100);
  94. other_acc(100);
  95. boost::archive::text_iarchive ia(ss);
  96. other_acc.serialize(ia, 0);
  97. BOOST_CHECK_CLOSE(5., rolling_mean(other_acc), 1e-5);
  98. }
  99. ///////////////////////////////////////////////////////////////////////////////
  100. // test_rolling_mean
  101. void test_rolling_mean()
  102. {
  103. accumulator_set<int,stats<tag::immediate_rolling_mean> >
  104. acc_immediate_rolling_mean(tag::immediate_rolling_mean::window_size = window_size),
  105. acc_immediate_rolling_mean2(tag::immediate_rolling_mean::window_size = window_size, sample = 0);
  106. accumulator_set<int,stats<tag::rolling_mean(immediate)> >
  107. acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size = window_size);
  108. accumulator_set<int,stats<tag::lazy_rolling_mean> >
  109. acc_lazy_rolling_mean(tag::lazy_rolling_mean::window_size = window_size),
  110. acc_lazy_rolling_mean2(tag::lazy_rolling_mean::window_size = window_size, sample = 0);
  111. accumulator_set<int,stats<tag::rolling_mean(lazy)> >
  112. acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size = window_size);
  113. accumulator_set<int,stats<tag::rolling_mean> >
  114. acc_default_rolling_mean(tag::rolling_mean::window_size = window_size),
  115. acc_default_rolling_mean2(tag::rolling_mean::window_size = window_size, sample = 0);
  116. //// test the different implementations
  117. test_rolling_mean_test_impl(acc_lazy_rolling_mean);
  118. test_rolling_mean_test_impl(acc_default_rolling_mean);
  119. test_rolling_mean_test_impl(acc_immediate_rolling_mean);
  120. test_rolling_mean_test_impl(acc_lazy_rolling_mean2);
  121. test_rolling_mean_test_impl(acc_default_rolling_mean2);
  122. test_rolling_mean_test_impl(acc_immediate_rolling_mean2);
  123. test_rolling_mean_test_impl(acc_lazy_rolling_mean3);
  124. test_rolling_mean_test_impl(acc_immediate_rolling_mean3);
  125. //// test that the default implementation is the 'immediate' computation
  126. BOOST_REQUIRE(sizeof(acc_lazy_rolling_mean) != sizeof(acc_immediate_rolling_mean));
  127. BOOST_CHECK (sizeof(acc_default_rolling_mean) == sizeof(acc_immediate_rolling_mean));
  128. //// test the equivalence of the different ways to indicate a feature
  129. BOOST_CHECK (sizeof(acc_lazy_rolling_mean) == sizeof(acc_lazy_rolling_mean2));
  130. BOOST_CHECK (sizeof(acc_lazy_rolling_mean) == sizeof(acc_lazy_rolling_mean3));
  131. BOOST_CHECK (sizeof(acc_immediate_rolling_mean) == sizeof(acc_immediate_rolling_mean2));
  132. BOOST_CHECK (sizeof(acc_immediate_rolling_mean) == sizeof(acc_immediate_rolling_mean3));
  133. //// test unsigned int with both implementations
  134. accumulator_set<unsigned int,stats<tag::immediate_rolling_mean> >
  135. acc_immediate_rolling_mean4(tag::immediate_rolling_mean::window_size = window_size),
  136. acc_immediate_rolling_mean5(tag::immediate_rolling_mean::window_size = window_size, sample = 0);
  137. test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean4);
  138. test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean5);
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////
  141. // test_persistency
  142. void test_persistency()
  143. {
  144. accumulator_set<int,stats<tag::immediate_rolling_mean> >
  145. acc_immediate_rolling_mean(tag::immediate_rolling_mean::window_size = window_size),
  146. acc_immediate_rolling_mean2(tag::immediate_rolling_mean::window_size = window_size, sample = 0);
  147. accumulator_set<int,stats<tag::rolling_mean(immediate)> >
  148. acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size = window_size);
  149. accumulator_set<int,stats<tag::lazy_rolling_mean> >
  150. acc_lazy_rolling_mean(tag::lazy_rolling_mean::window_size = window_size),
  151. acc_lazy_rolling_mean2(tag::lazy_rolling_mean::window_size = window_size, sample = 0);
  152. accumulator_set<int,stats<tag::rolling_mean(lazy)> >
  153. acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size = window_size);
  154. accumulator_set<int,stats<tag::rolling_mean> >
  155. acc_default_rolling_mean(tag::rolling_mean::window_size = window_size),
  156. acc_default_rolling_mean2(tag::rolling_mean::window_size = window_size, sample = 0);
  157. //// test the different implementations
  158. test_persistency_impl(acc_lazy_rolling_mean);
  159. test_persistency_impl(acc_default_rolling_mean);
  160. test_persistency_impl(acc_immediate_rolling_mean);
  161. test_persistency_impl(acc_lazy_rolling_mean2);
  162. test_persistency_impl(acc_default_rolling_mean2);
  163. test_persistency_impl(acc_immediate_rolling_mean2);
  164. test_persistency_impl(acc_lazy_rolling_mean3);
  165. test_persistency_impl(acc_immediate_rolling_mean3);
  166. }
  167. ///////////////////////////////////////////////////////////////////////////////
  168. // init_unit_test_suite
  169. //
  170. test_suite* init_unit_test_suite( int argc, char* argv[] )
  171. {
  172. test_suite *test = BOOST_TEST_SUITE("rolling mean test");
  173. test->add(BOOST_TEST_CASE(&test_rolling_mean));
  174. test->add(BOOST_TEST_CASE(&test_persistency));
  175. return test;
  176. }