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