]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/accumulators/test/sum_kahan.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / accumulators / test / sum_kahan.cpp
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
12 using namespace boost;
13 using namespace unit_test;
14 using namespace accumulators;
15
16 ///////////////////////////////////////////////////////////////////////////////
17 // test_sum_kahan
18 //
19 void test_sum_kahan()
20 {
21 accumulator_set<float, stats<tag::sum_kahan> > acc;
22
23 BOOST_CHECK_EQUAL(0.0f, sum_kahan(acc));
24
25 for (size_t i = 0; i < 1e6; ++i) {
26 acc(1e-6f);
27 }
28
29 BOOST_CHECK_EQUAL(1.0f, sum_kahan(acc));
30 }
31
32 ///////////////////////////////////////////////////////////////////////////////
33 // test_sum_of_weights_kahan
34 //
35 void test_sum_of_weights_kahan()
36 {
37 accumulator_set<float, stats<tag::sum_of_weights_kahan>, float > acc;
38
39 BOOST_CHECK_EQUAL(0.0f, sum_of_weights_kahan(acc));
40
41 for (size_t i = 0; i < 1e6; ++i) {
42 acc(0, weight = 1e-6f);
43 }
44
45 BOOST_CHECK_EQUAL(1.0f, sum_of_weights_kahan(acc));
46 }
47
48 ///////////////////////////////////////////////////////////////////////////////
49 // test_sum_of_variates_kahan
50 //
51 void test_sum_of_variates_kahan()
52 {
53 accumulator_set<
54 float,
55 stats<tag::sum_of_variates_kahan<float, tag::covariate1> >,
56 float
57 >
58 acc;
59
60 BOOST_CHECK_EQUAL(0.0f, sum_of_variates_kahan(acc));
61
62 for (size_t i = 0; i < 1e6; ++i) {
63 acc(0, covariate1 = 1e-6f);
64 }
65
66 BOOST_CHECK_EQUAL(1.0f, sum_of_variates_kahan(acc));
67 }
68
69 ///////////////////////////////////////////////////////////////////////////////
70 // init_unit_test_suite
71 //
72 test_suite* init_unit_test_suite( int argc, char* argv[] )
73 {
74 test_suite *test = BOOST_TEST_SUITE("sum kahan tests");
75
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));
79
80 return test;
81 }