count.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/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::count> > acc;
  21. acc(1);
  22. BOOST_CHECK_EQUAL(1u, count(acc));
  23. acc(1);
  24. BOOST_CHECK_EQUAL(2u, count(acc));
  25. acc(1);
  26. BOOST_CHECK_EQUAL(3u, count(acc));
  27. acc(1);
  28. BOOST_CHECK_EQUAL(4u, count(acc));
  29. acc(1);
  30. BOOST_CHECK_EQUAL(5u, count(acc));
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // test_persistency
  34. //
  35. void test_persistency()
  36. {
  37. // "persistent" storage
  38. std::stringstream ss;
  39. {
  40. accumulator_set<int, stats<tag::count> > acc;
  41. acc(1);
  42. acc(1);
  43. acc(1);
  44. acc(1);
  45. BOOST_CHECK_EQUAL(4u, count(acc));
  46. boost::archive::text_oarchive oa(ss);
  47. acc.serialize(oa, 0);
  48. }
  49. accumulator_set<int, stats<tag::count> > acc;
  50. BOOST_CHECK_EQUAL(0u, count(acc));
  51. boost::archive::text_iarchive ia(ss);
  52. acc.serialize(ia, 0);
  53. BOOST_CHECK_EQUAL(4u, count(acc));
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // init_unit_test_suite
  57. //
  58. test_suite* init_unit_test_suite( int argc, char* argv[] )
  59. {
  60. test_suite *test = BOOST_TEST_SUITE("count test");
  61. test->add(BOOST_TEST_CASE(&test_stat));
  62. test->add(BOOST_TEST_CASE(&test_persistency));
  63. return test;
  64. }