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)
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>
12 using namespace boost
;
13 using namespace unit_test
;
14 using namespace accumulators
;
16 ///////////////////////////////////////////////////////////////////////////////
21 accumulator_set
<float, stats
<tag::sum_kahan
> > acc
;
23 BOOST_CHECK_EQUAL(0.0f
, sum_kahan(acc
));
25 for (size_t i
= 0; i
< 1e6
; ++i
) {
29 BOOST_CHECK_EQUAL(1.0f
, sum_kahan(acc
));
32 ///////////////////////////////////////////////////////////////////////////////
33 // test_sum_of_weights_kahan
35 void test_sum_of_weights_kahan()
37 accumulator_set
<float, stats
<tag::sum_of_weights_kahan
>, float > acc
;
39 BOOST_CHECK_EQUAL(0.0f
, sum_of_weights_kahan(acc
));
41 for (size_t i
= 0; i
< 1e6
; ++i
) {
42 acc(0, weight
= 1e-6f
);
45 BOOST_CHECK_EQUAL(1.0f
, sum_of_weights_kahan(acc
));
48 ///////////////////////////////////////////////////////////////////////////////
49 // test_sum_of_variates_kahan
51 void test_sum_of_variates_kahan()
55 stats
<tag::sum_of_variates_kahan
<float, tag::covariate1
> >,
60 BOOST_CHECK_EQUAL(0.0f
, sum_of_variates_kahan(acc
));
62 for (size_t i
= 0; i
< 1e6
; ++i
) {
63 acc(0, covariate1
= 1e-6f
);
66 BOOST_CHECK_EQUAL(1.0f
, sum_of_variates_kahan(acc
));
69 ///////////////////////////////////////////////////////////////////////////////
70 // init_unit_test_suite
72 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
74 test_suite
*test
= BOOST_TEST_SUITE("sum kahan tests");
76 test
->add(BOOST_TEST_CASE(&test_sum_kahan
));
77 test
->add(BOOST_TEST_CASE(&test_sum_of_weights_kahan
));
78 test
->add(BOOST_TEST_CASE(&test_sum_of_variates_kahan
));