summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/accumulators/test/reference.cpp
blob: bf9b6c60c649f4d34d6ebc3e8e273c165c395825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//  (C) Copyright Eric Niebler 2005.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/test/unit_test.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>

using namespace boost;
using namespace unit_test;
using namespace accumulators;

namespace my
{
    BOOST_PARAMETER_KEYWORD(tag, int_ref)
    BOOST_PARAMETER_KEYWORD(tag, sum_acc)
}

///////////////////////////////////////////////////////////////////////////////
// test_stat
//
void test_stat()
{
    int i = 0;
    accumulator_set<double, stats<tag::reference<int, my::tag::int_ref> > > acc(
        my::int_ref = i);

    int &ref1 = accumulators::reference<int, my::tag::int_ref>(acc);
    int &ref2 = accumulators::reference_tag<my::tag::int_ref>(acc);

    BOOST_CHECK_EQUAL(&i, &ref1);
    BOOST_CHECK_EQUAL(&i, &ref2);
}

///////////////////////////////////////////////////////////////////////////////
// test_external
//
void test_external()
{
    typedef accumulator_set<int, stats<tag::sum> > sum_acc_type;
    sum_acc_type sum_acc; // the sum accumulator
    accumulator_set<
        int
      , stats<
            tag::mean
          , tag::external<tag::sum, my::tag::sum_acc>       // make sum external
          , tag::reference<sum_acc_type, my::tag::sum_acc>  // and hold a reference to it
        >
    > acc_with_ref(my::sum_acc = sum_acc); // initialize the reference sum

    sum_acc(1);
    sum_acc(2); // sum is now 3 for both

    BOOST_CHECK_EQUAL(sum(acc_with_ref), sum(sum_acc));
    BOOST_CHECK_EQUAL(sum(acc_with_ref), 3);
}

///////////////////////////////////////////////////////////////////////////////
// test_external2
//
void test_external2()
{
    typedef accumulator_set<int, stats<tag::sum> > sum_acc_type;
    sum_acc_type sum_acc; // the sum accumulator
    accumulator_set<
        int
      , stats<
            tag::mean
            // make sum external and hold a reference to it
          , tag::external<tag::sum, my::tag::sum_acc, sum_acc_type>
        >
    > acc_with_ref(my::sum_acc = sum_acc); // initialize the reference sum

    sum_acc(1);
    sum_acc(2); // sum is now 3 for both

    BOOST_CHECK_EQUAL(sum(acc_with_ref), sum(sum_acc));
    BOOST_CHECK_EQUAL(sum(acc_with_ref), 3);
}

///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
    test_suite *test = BOOST_TEST_SUITE("reference_accumulator test");

    test->add(BOOST_TEST_CASE(&test_stat));
    test->add(BOOST_TEST_CASE(&test_external));
    test->add(BOOST_TEST_CASE(&test_external2));

    return test;
}