• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/sum.hpp>
10 #include <boost/accumulators/statistics/weighted_sum.hpp>
11 #include <boost/accumulators/statistics/variates/covariate.hpp>
12 #include <sstream>
13 #include <boost/archive/text_oarchive.hpp>
14 #include <boost/archive/text_iarchive.hpp>
15 
16 using namespace boost;
17 using namespace unit_test;
18 using namespace accumulators;
19 
20 ///////////////////////////////////////////////////////////////////////////////
21 // test_stat
22 //
test_stat()23 void test_stat()
24 {
25     accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc;
26 
27     acc(1, weight = 2, covariate1 = 3);
28     BOOST_CHECK_EQUAL(2, sum(acc));
29     BOOST_CHECK_EQUAL(2, sum_of_weights(acc));
30     BOOST_CHECK_EQUAL(3, sum_of_variates(acc));
31 
32     acc(2, weight = 4, covariate1 = 6);
33     BOOST_CHECK_EQUAL(10, sum(acc));
34     BOOST_CHECK_EQUAL(6, sum_of_weights(acc));
35     BOOST_CHECK_EQUAL(9, sum_of_variates(acc));
36 
37     acc(3, weight = 6, covariate1 = 9);
38     BOOST_CHECK_EQUAL(28, sum(acc));
39     BOOST_CHECK_EQUAL(12, sum_of_weights(acc));
40     BOOST_CHECK_EQUAL(18, sum_of_variates(acc));
41 }
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // test_persistency
45 //
test_persistency()46 void test_persistency()
47 {
48     std::stringstream ss;
49     {
50         accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc;
51         acc(1, weight = 2, covariate1 = 3);
52         acc(2, weight = 4, covariate1 = 6);
53         acc(3, weight = 6, covariate1 = 9);
54         BOOST_CHECK_EQUAL(28, sum(acc));
55         BOOST_CHECK_EQUAL(12, sum_of_weights(acc));
56         BOOST_CHECK_EQUAL(18, sum_of_variates(acc));
57 
58         boost::archive::text_oarchive oa(ss);
59         acc.serialize(oa, 0);
60     }
61     accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc;
62     boost::archive::text_iarchive ia(ss);
63     acc.serialize(ia, 0);
64     BOOST_CHECK_EQUAL(28, sum(acc));
65     BOOST_CHECK_EQUAL(12, sum_of_weights(acc));
66     BOOST_CHECK_EQUAL(18, sum_of_variates(acc));
67 }
68 
69 ///////////////////////////////////////////////////////////////////////////////
70 // init_unit_test_suite
71 //
init_unit_test_suite(int argc,char * argv[])72 test_suite* init_unit_test_suite( int argc, char* argv[] )
73 {
74     test_suite *test = BOOST_TEST_SUITE("sum test");
75 
76     test->add(BOOST_TEST_CASE(&test_stat));
77     test->add(BOOST_TEST_CASE(&test_persistency));
78 
79     return test;
80 }
81