droppable.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/mean.hpp>
  10. using namespace boost;
  11. using namespace unit_test;
  12. using namespace accumulators;
  13. template<typename T>
  14. void assert_is_double(T const &)
  15. {
  16. BOOST_MPL_ASSERT((is_same<T, double>));
  17. }
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // test_stat
  20. //
  21. void test_stat()
  22. {
  23. accumulator_set<int, stats<droppable<tag::mean> > > acc, test_acc(sample = 0);
  24. acc(1);
  25. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  26. BOOST_CHECK_EQUAL(1u, count(acc));
  27. BOOST_CHECK_EQUAL(1, sum(acc));
  28. acc(0);
  29. BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
  30. BOOST_CHECK_EQUAL(2u, count(acc));
  31. BOOST_CHECK_EQUAL(1, sum(acc));
  32. acc.drop<tag::mean>();
  33. acc(2);
  34. BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
  35. BOOST_CHECK_EQUAL(2u, count(acc));
  36. BOOST_CHECK_EQUAL(1, sum(acc));
  37. assert_is_double(mean(acc));
  38. accumulator_set<int, stats<droppable<tag::mean(immediate)> > > acc2, test_acc2(sample = 0);
  39. acc2(1);
  40. BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5);
  41. BOOST_CHECK_EQUAL(1u, count(acc2));
  42. acc2(0);
  43. BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5);
  44. BOOST_CHECK_EQUAL(2u, count(acc2));
  45. acc2.drop<tag::mean>();
  46. acc2(2);
  47. BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5);
  48. BOOST_CHECK_EQUAL(2u, count(acc2));
  49. assert_is_double(mean(acc2));
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // test_stat2
  53. //
  54. void test_stat2()
  55. {
  56. accumulator_set<int, stats<droppable<tag::sum>, droppable<tag::mean> > > acc, test_acc(sample = 0);
  57. acc(1);
  58. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  59. BOOST_CHECK_EQUAL(1u, count(acc));
  60. BOOST_CHECK_EQUAL(1, sum(acc));
  61. acc(0);
  62. BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
  63. BOOST_CHECK_EQUAL(2u, count(acc));
  64. BOOST_CHECK_EQUAL(1, sum(acc));
  65. acc.drop<tag::mean>();
  66. acc.drop<tag::sum>();
  67. acc(2);
  68. BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
  69. BOOST_CHECK_EQUAL(2u, count(acc));
  70. BOOST_CHECK_EQUAL(1, sum(acc));
  71. assert_is_double(mean(acc));
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // test_stat3
  75. //
  76. void test_stat3()
  77. {
  78. accumulator_set<int, stats<droppable<tag::sum>, droppable<tag::count>, droppable<tag::mean> > > acc, test_acc(sample = 0);
  79. acc(1);
  80. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  81. BOOST_CHECK_EQUAL(1u, count(acc));
  82. BOOST_CHECK_EQUAL(1, sum(acc));
  83. acc(0);
  84. BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
  85. BOOST_CHECK_EQUAL(2u, count(acc));
  86. BOOST_CHECK_EQUAL(1, sum(acc));
  87. acc.drop<tag::mean>();
  88. acc.drop<tag::sum>();
  89. acc(2);
  90. BOOST_CHECK_CLOSE(1./3., mean(acc), 1e-5);
  91. BOOST_CHECK_EQUAL(3u, count(acc));
  92. BOOST_CHECK_EQUAL(1, sum(acc));
  93. acc.drop<tag::count>();
  94. acc(3);
  95. BOOST_CHECK_CLOSE(1./3., mean(acc), 1e-5);
  96. BOOST_CHECK_EQUAL(3u, count(acc));
  97. BOOST_CHECK_EQUAL(1, sum(acc));
  98. assert_is_double(mean(acc));
  99. }
  100. ///////////////////////////////////////////////////////////////////////////////
  101. // init_unit_test_suite
  102. //
  103. test_suite* init_unit_test_suite( int argc, char* argv[] )
  104. {
  105. test_suite *test = BOOST_TEST_SUITE("droppable test");
  106. test->add(BOOST_TEST_CASE(&test_stat));
  107. test->add(BOOST_TEST_CASE(&test_stat2));
  108. test->add(BOOST_TEST_CASE(&test_stat3));
  109. return test;
  110. }