external_accumulator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. namespace my
  14. {
  15. BOOST_PARAMETER_KEYWORD(tag, sum_acc)
  16. }
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // test_stat
  19. //
  20. void test_stat()
  21. {
  22. accumulator_set<int, stats<tag::mean, tag::external<tag::sum, my::tag::sum_acc> > > acc;
  23. accumulator_set<int, stats<tag::sum> > sum_acc;
  24. acc(1);
  25. sum_acc(1);
  26. BOOST_CHECK_CLOSE(1., mean(acc, my::sum_acc = sum_acc), 1e-5);
  27. BOOST_CHECK_EQUAL(1u, count(acc));
  28. BOOST_CHECK_EQUAL(1, sum(sum_acc));
  29. acc(0);
  30. sum_acc(0);
  31. BOOST_CHECK_CLOSE(0.5, mean(acc, my::sum_acc = sum_acc), 1e-5);
  32. BOOST_CHECK_EQUAL(2u, count(acc));
  33. BOOST_CHECK_EQUAL(1, sum(acc, my::sum_acc = sum_acc));
  34. acc(2);
  35. sum_acc(2);
  36. BOOST_CHECK_CLOSE(1., mean(acc, my::sum_acc = sum_acc), 1e-5);
  37. BOOST_CHECK_EQUAL(3u, count(acc));
  38. BOOST_CHECK_EQUAL(3, sum(acc, my::sum_acc = sum_acc));
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // test_reference
  42. //
  43. void test_reference()
  44. {
  45. typedef accumulator_set<int, stats<tag::sum> > sum_acc_type;
  46. sum_acc_type sum_acc;
  47. accumulator_set<
  48. int
  49. , stats<
  50. tag::mean
  51. , tag::external<tag::sum, my::tag::sum_acc>
  52. , tag::reference<sum_acc_type, my::tag::sum_acc>
  53. >
  54. > acc(my::sum_acc = sum_acc);
  55. acc(1);
  56. sum_acc(1);
  57. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  58. BOOST_CHECK_EQUAL(1u, count(acc));
  59. BOOST_CHECK_EQUAL(1, sum(sum_acc));
  60. acc(0);
  61. sum_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(2);
  66. sum_acc(2);
  67. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  68. BOOST_CHECK_EQUAL(3u, count(acc));
  69. BOOST_CHECK_EQUAL(3, sum(acc));
  70. }
  71. ///////////////////////////////////////////////////////////////////////////////
  72. // test_reference2
  73. //
  74. void test_reference2()
  75. {
  76. typedef accumulator_set<int, stats<tag::sum> > sum_acc_type;
  77. sum_acc_type sum_acc;
  78. accumulator_set<
  79. int
  80. , stats<
  81. tag::mean
  82. , tag::external<tag::sum, my::tag::sum_acc, sum_acc_type>
  83. >
  84. > acc(my::sum_acc = sum_acc);
  85. acc(1);
  86. sum_acc(1);
  87. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  88. BOOST_CHECK_EQUAL(1u, count(acc));
  89. BOOST_CHECK_EQUAL(1, sum(sum_acc));
  90. acc(0);
  91. sum_acc(0);
  92. BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
  93. BOOST_CHECK_EQUAL(2u, count(acc));
  94. BOOST_CHECK_EQUAL(1, sum(acc));
  95. acc(2);
  96. sum_acc(2);
  97. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  98. BOOST_CHECK_EQUAL(3u, count(acc));
  99. BOOST_CHECK_EQUAL(3, sum(acc));
  100. }
  101. ///////////////////////////////////////////////////////////////////////////////
  102. // init_unit_test_suite
  103. //
  104. test_suite* init_unit_test_suite( int argc, char* argv[] )
  105. {
  106. test_suite *test = BOOST_TEST_SUITE("external_accumulator test");
  107. test->add(BOOST_TEST_CASE(&test_stat));
  108. test->add(BOOST_TEST_CASE(&test_reference));
  109. test->add(BOOST_TEST_CASE(&test_reference2));
  110. return test;
  111. }