moment.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/test/floating_point_comparison.hpp>
  7. #include <boost/accumulators/accumulators.hpp>
  8. #include <boost/accumulators/statistics/stats.hpp>
  9. #include <boost/accumulators/statistics/moment.hpp>
  10. #include <sstream>
  11. #include <boost/archive/text_oarchive.hpp>
  12. #include <boost/archive/text_iarchive.hpp>
  13. using namespace boost;
  14. using namespace unit_test;
  15. using namespace accumulators;
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // test_stat
  18. //
  19. void test_stat()
  20. {
  21. accumulator_set<int, stats<tag::moment<2> > > acc1;
  22. acc1(2); // 4
  23. acc1(4); // 16
  24. acc1(5); // + 25
  25. // = 45 / 3 = 15
  26. BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc1), 1e-5);
  27. accumulator_set<int, stats<tag::moment<5> > > acc2;
  28. acc2(2); // 32
  29. acc2(3); // 243
  30. acc2(4); // 1024
  31. acc2(5); // + 3125
  32. // = 4424 / 4 = 1106
  33. BOOST_CHECK_CLOSE(1106., accumulators::moment<5>(acc2), 1e-5);
  34. }
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // test_persistency
  37. //
  38. void test_persistency()
  39. {
  40. double epsilon = 1e-5;
  41. std::stringstream ss;
  42. {
  43. accumulator_set<int, stats<tag::moment<2> > > acc;
  44. acc(2); // 4
  45. acc(4); // 16
  46. acc(5); // + 25
  47. // = 45 / 3 = 15
  48. BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc), epsilon);
  49. boost::archive::text_oarchive oa(ss);
  50. acc.serialize(oa, 0);
  51. }
  52. accumulator_set<int, stats<tag::moment<2> > > acc;
  53. boost::archive::text_iarchive ia(ss);
  54. acc.serialize(ia, 0);
  55. BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc), epsilon);
  56. }
  57. ///////////////////////////////////////////////////////////////////////////////
  58. // init_unit_test_suite
  59. //
  60. test_suite* init_unit_test_suite( int argc, char* argv[] )
  61. {
  62. test_suite *test = BOOST_TEST_SUITE("moment test");
  63. test->add(BOOST_TEST_CASE(&test_stat));
  64. test->add(BOOST_TEST_CASE(&test_persistency));
  65. return test;
  66. }