sum.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/sum.hpp>
  9. #include <boost/accumulators/statistics/weighted_sum.hpp>
  10. #include <boost/accumulators/statistics/variates/covariate.hpp>
  11. #include <sstream>
  12. #include <boost/archive/text_oarchive.hpp>
  13. #include <boost/archive/text_iarchive.hpp>
  14. using namespace boost;
  15. using namespace unit_test;
  16. using namespace accumulators;
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // test_stat
  19. //
  20. void test_stat()
  21. {
  22. accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc;
  23. acc(1, weight = 2, covariate1 = 3);
  24. BOOST_CHECK_EQUAL(2, sum(acc));
  25. BOOST_CHECK_EQUAL(2, sum_of_weights(acc));
  26. BOOST_CHECK_EQUAL(3, sum_of_variates(acc));
  27. acc(2, weight = 4, covariate1 = 6);
  28. BOOST_CHECK_EQUAL(10, sum(acc));
  29. BOOST_CHECK_EQUAL(6, sum_of_weights(acc));
  30. BOOST_CHECK_EQUAL(9, sum_of_variates(acc));
  31. acc(3, weight = 6, covariate1 = 9);
  32. BOOST_CHECK_EQUAL(28, sum(acc));
  33. BOOST_CHECK_EQUAL(12, sum_of_weights(acc));
  34. BOOST_CHECK_EQUAL(18, sum_of_variates(acc));
  35. }
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // test_persistency
  38. //
  39. void test_persistency()
  40. {
  41. std::stringstream ss;
  42. {
  43. accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc;
  44. acc(1, weight = 2, covariate1 = 3);
  45. acc(2, weight = 4, covariate1 = 6);
  46. acc(3, weight = 6, covariate1 = 9);
  47. BOOST_CHECK_EQUAL(28, sum(acc));
  48. BOOST_CHECK_EQUAL(12, sum_of_weights(acc));
  49. BOOST_CHECK_EQUAL(18, sum_of_variates(acc));
  50. boost::archive::text_oarchive oa(ss);
  51. acc.serialize(oa, 0);
  52. }
  53. accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc;
  54. boost::archive::text_iarchive ia(ss);
  55. acc.serialize(ia, 0);
  56. BOOST_CHECK_EQUAL(28, sum(acc));
  57. BOOST_CHECK_EQUAL(12, sum_of_weights(acc));
  58. BOOST_CHECK_EQUAL(18, sum_of_variates(acc));
  59. }
  60. ///////////////////////////////////////////////////////////////////////////////
  61. // init_unit_test_suite
  62. //
  63. test_suite* init_unit_test_suite( int argc, char* argv[] )
  64. {
  65. test_suite *test = BOOST_TEST_SUITE("sum test");
  66. test->add(BOOST_TEST_CASE(&test_stat));
  67. test->add(BOOST_TEST_CASE(&test_persistency));
  68. return test;
  69. }