1// (C) Copyright Gaetano Mendola 2010, Simon West 2011.
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_kahan.hpp>
10#include <boost/accumulators/statistics/variates/covariate.hpp>
11#include <sstream>
12#include <boost/archive/text_oarchive.hpp>
13#include <boost/archive/text_iarchive.hpp>
14
15using namespace boost;
16using namespace unit_test;
17using namespace accumulators;
18
19///////////////////////////////////////////////////////////////////////////////
20// test_sum_kahan
21//
22void test_sum_kahan()
23{
24 accumulator_set<float, stats<tag::sum_kahan> > acc;
25
26 BOOST_CHECK_EQUAL(0.0f, sum_kahan(acc));
27
28 for (size_t i = 0; i < 1e6; ++i) {
29 acc(1e-6f);
30 }
31
32 BOOST_CHECK_EQUAL(1.0f, sum_kahan(acc));
33}
34
35///////////////////////////////////////////////////////////////////////////////
36// test_sum_of_weights_kahan
37//
38void test_sum_of_weights_kahan()
39{
40 accumulator_set<float, stats<tag::sum_of_weights_kahan>, float > acc;
41
42 BOOST_CHECK_EQUAL(0.0f, sum_of_weights_kahan(acc));
43
44 for (size_t i = 0; i < 1e6; ++i) {
45 acc(0, weight = 1e-6f);
46 }
47
48 BOOST_CHECK_EQUAL(1.0f, sum_of_weights_kahan(acc));
49}
50
51///////////////////////////////////////////////////////////////////////////////
52// test_sum_of_variates_kahan
53//
54void test_sum_of_variates_kahan()
55{
56 accumulator_set<
57 float,
58 stats<tag::sum_of_variates_kahan<float, tag::covariate1> >,
59 float
60 >
61 acc;
62
63 BOOST_CHECK_EQUAL(0.0f, sum_of_variates_kahan(acc));
64
65 for (size_t i = 0; i < 1e6; ++i) {
66 acc(0, covariate1 = 1e-6f);
67 }
68
69 BOOST_CHECK_EQUAL(1.0f, sum_of_variates_kahan(acc));
70}
71
72///////////////////////////////////////////////////////////////////////////////
73// test_persistency
74//
75void test_persistency()
76{
77 std::stringstream ss;
78 {
79 accumulator_set<
80 float,
81 stats<tag::sum_of_variates_kahan<float, tag::covariate1> >,
82 float
83 >
84 acc;
85
86 BOOST_CHECK_EQUAL(0.0f, sum_of_variates_kahan(acc));
87
88 for (size_t i = 0; i < 1e6; ++i) {
89 acc(0, covariate1 = 1e-6f);
90 }
91
92 BOOST_CHECK_EQUAL(1.0f, sum_of_variates_kahan(acc));
93 boost::archive::text_oarchive oa(ss);
94 acc.serialize(ar&: oa, file_version: 0);
95 }
96 accumulator_set<
97 float,
98 stats<tag::sum_of_variates_kahan<float, tag::covariate1> >,
99 float
100 >
101 acc;
102 boost::archive::text_iarchive ia(ss);
103 acc.serialize(ar&: ia, file_version: 0);
104 BOOST_CHECK_EQUAL(1.0f, sum_of_variates_kahan(acc));
105}
106
107///////////////////////////////////////////////////////////////////////////////
108// init_unit_test_suite
109//
110test_suite* init_unit_test_suite( int argc, char* argv[] )
111{
112 test_suite *test = BOOST_TEST_SUITE("sum kahan tests");
113
114 test->add(BOOST_TEST_CASE(&test_sum_kahan));
115 test->add(BOOST_TEST_CASE(&test_sum_of_weights_kahan));
116 test->add(BOOST_TEST_CASE(&test_sum_of_variates_kahan));
117 test->add(BOOST_TEST_CASE(&test_persistency));
118
119 return test;
120}
121

source code of boost/libs/accumulators/test/sum_kahan.cpp