reference.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/mean.hpp>
  9. using namespace boost;
  10. using namespace unit_test;
  11. using namespace accumulators;
  12. namespace my
  13. {
  14. BOOST_PARAMETER_KEYWORD(tag, int_ref)
  15. BOOST_PARAMETER_KEYWORD(tag, sum_acc)
  16. }
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // test_stat
  19. //
  20. void test_stat()
  21. {
  22. int i = 0;
  23. accumulator_set<double, stats<tag::reference<int, my::tag::int_ref> > > acc(
  24. my::int_ref = i);
  25. int &ref1 = accumulators::reference<int, my::tag::int_ref>(acc);
  26. int &ref2 = accumulators::reference_tag<my::tag::int_ref>(acc);
  27. BOOST_CHECK_EQUAL(&i, &ref1);
  28. BOOST_CHECK_EQUAL(&i, &ref2);
  29. }
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // test_external
  32. //
  33. void test_external()
  34. {
  35. typedef accumulator_set<int, stats<tag::sum> > sum_acc_type;
  36. sum_acc_type sum_acc; // the sum accumulator
  37. accumulator_set<
  38. int
  39. , stats<
  40. tag::mean
  41. , tag::external<tag::sum, my::tag::sum_acc> // make sum external
  42. , tag::reference<sum_acc_type, my::tag::sum_acc> // and hold a reference to it
  43. >
  44. > acc_with_ref(my::sum_acc = sum_acc); // initialize the reference sum
  45. sum_acc(1);
  46. sum_acc(2); // sum is now 3 for both
  47. BOOST_CHECK_EQUAL(sum(acc_with_ref), sum(sum_acc));
  48. BOOST_CHECK_EQUAL(sum(acc_with_ref), 3);
  49. }
  50. ///////////////////////////////////////////////////////////////////////////////
  51. // test_external2
  52. //
  53. void test_external2()
  54. {
  55. typedef accumulator_set<int, stats<tag::sum> > sum_acc_type;
  56. sum_acc_type sum_acc; // the sum accumulator
  57. accumulator_set<
  58. int
  59. , stats<
  60. tag::mean
  61. // make sum external and hold a reference to it
  62. , tag::external<tag::sum, my::tag::sum_acc, sum_acc_type>
  63. >
  64. > acc_with_ref(my::sum_acc = sum_acc); // initialize the reference sum
  65. sum_acc(1);
  66. sum_acc(2); // sum is now 3 for both
  67. BOOST_CHECK_EQUAL(sum(acc_with_ref), sum(sum_acc));
  68. BOOST_CHECK_EQUAL(sum(acc_with_ref), 3);
  69. }
  70. ///////////////////////////////////////////////////////////////////////////////
  71. // init_unit_test_suite
  72. //
  73. test_suite* init_unit_test_suite( int argc, char* argv[] )
  74. {
  75. test_suite *test = BOOST_TEST_SUITE("reference_accumulator test");
  76. test->add(BOOST_TEST_CASE(&test_stat));
  77. test->add(BOOST_TEST_CASE(&test_external));
  78. test->add(BOOST_TEST_CASE(&test_external2));
  79. return test;
  80. }