• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright 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/weighted_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_weighted_sum_kahan
18 //
test_weighted_sum_kahan()19 void test_weighted_sum_kahan()
20 {
21     accumulator_set<float, stats<tag::weighted_sum_kahan>, float > acc;
22 
23     BOOST_CHECK_EQUAL(0.0f, weighted_sum_kahan(acc));
24 
25     for (size_t i = 0; i < 1e6; ++i) {
26         acc(1, weight = 1e-6f);
27     }
28 
29     BOOST_CHECK_EQUAL(1.0f, weighted_sum_kahan(acc));
30 }
31 
32 ///////////////////////////////////////////////////////////////////////////////
33 // test_weighted_sum_of_variates_kahan
34 //
test_weighted_sum_of_variates_kahan()35 void test_weighted_sum_of_variates_kahan()
36 {
37     accumulator_set<
38         float,
39         stats<tag::weighted_sum_of_variates_kahan<float, tag::covariate1> >,
40         float
41     >
42     acc;
43 
44     BOOST_CHECK_EQUAL(0.0f, weighted_sum_of_variates_kahan(acc));
45 
46     for (size_t i = 0; i < 1e6; ++i) {
47       acc(0, weight = 1.0f, covariate1 = 1e-6f);
48     }
49 
50     BOOST_CHECK_EQUAL(1.0f, weighted_sum_of_variates_kahan(acc));
51 }
52 
53 ///////////////////////////////////////////////////////////////////////////////
54 // init_unit_test_suite
55 //
init_unit_test_suite(int argc,char * argv[])56 test_suite* init_unit_test_suite( int argc, char* argv[] )
57 {
58     test_suite *test = BOOST_TEST_SUITE("weighted sum kahan tests");
59 
60     test->add(BOOST_TEST_CASE(&test_weighted_sum_kahan));
61     test->add(BOOST_TEST_CASE(&test_weighted_sum_of_variates_kahan));
62 
63     return test;
64 }
65