rolling_count.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // (C) Copyright Eric Niebler 2005.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/test/unit_test.hpp>
  6. #include <boost/accumulators/accumulators.hpp>
  7. #include <boost/accumulators/statistics/stats.hpp>
  8. #include <boost/accumulators/statistics/rolling_count.hpp>
  9. #include <sstream>
  10. #include <boost/archive/text_oarchive.hpp>
  11. #include <boost/archive/text_iarchive.hpp>
  12. using namespace boost;
  13. using namespace unit_test;
  14. using namespace accumulators;
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // test_stat
  17. //
  18. void test_stat()
  19. {
  20. accumulator_set<int, stats<tag::rolling_count> > acc(tag::rolling_window::window_size = 3);
  21. BOOST_CHECK_EQUAL(0u, rolling_count(acc));
  22. acc(1);
  23. BOOST_CHECK_EQUAL(1u, rolling_count(acc));
  24. acc(1);
  25. BOOST_CHECK_EQUAL(2u, rolling_count(acc));
  26. acc(1);
  27. BOOST_CHECK_EQUAL(3u, rolling_count(acc));
  28. acc(1);
  29. BOOST_CHECK_EQUAL(3u, rolling_count(acc));
  30. acc(1);
  31. BOOST_CHECK_EQUAL(3u, rolling_count(acc));
  32. }
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // test_persistency
  35. //
  36. void test_persistency()
  37. {
  38. std::stringstream ss;
  39. {
  40. accumulator_set<int, stats<tag::rolling_count> > acc(tag::rolling_window::window_size = 3);
  41. acc(1);
  42. acc(1);
  43. acc(1);
  44. acc(1);
  45. acc(1);
  46. acc(1);
  47. BOOST_CHECK_EQUAL(3u, rolling_count(acc));
  48. boost::archive::text_oarchive oa(ss);
  49. acc.serialize(oa, 0);
  50. }
  51. accumulator_set<int, stats<tag::rolling_count> > acc(tag::rolling_window::window_size = 3);
  52. boost::archive::text_iarchive ia(ss);
  53. acc.serialize(ia, 0);
  54. BOOST_CHECK_EQUAL(3u, rolling_count(acc));
  55. }
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // init_unit_test_suite
  58. //
  59. test_suite* init_unit_test_suite( int argc, char* argv[] )
  60. {
  61. test_suite *test = BOOST_TEST_SUITE("rolling count test");
  62. test->add(BOOST_TEST_CASE(&test_stat));
  63. test->add(BOOST_TEST_CASE(&test_persistency));
  64. return test;
  65. }