• 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/test/floating_point_comparison.hpp>
8 #include <boost/accumulators/accumulators.hpp>
9 #include <boost/accumulators/statistics/stats.hpp>
10 #include <boost/accumulators/statistics/weighted_mean.hpp>
11 
12 using namespace boost;
13 using namespace unit_test;
14 using namespace accumulators;
15 
16 ///////////////////////////////////////////////////////////////////////////////
17 // test_stat
18 //
test_stat()19 void test_stat()
20 {
21     accumulator_set<int, stats<tag::weighted_mean>, external<int> > acc;
22     accumulator_set<void, stats<tag::sum_of_weights>, int> weight_acc;
23 
24     acc(10, weight = 2);                        //  20
25     weight_acc(weight = 2);                     //
26     BOOST_CHECK_EQUAL(2, sum_of_weights(weight_acc));   //
27                                                 //
28     acc(6, weight = 3);                         //  18
29     weight_acc(weight = 3);                     //
30     BOOST_CHECK_EQUAL(5, sum_of_weights(weight_acc));   //
31                                                 //
32     acc(4, weight = 4);                         //  16
33     weight_acc(weight = 4);                     //
34     BOOST_CHECK_EQUAL(9, sum_of_weights(weight_acc));   //
35                                                 //
36     acc(6, weight = 5);                         //+ 30
37     weight_acc(weight = 5);                     //
38     BOOST_CHECK_EQUAL(14, sum_of_weights(weight_acc));  //
39                                                 //= 84  / 14 = 6
40 
41     BOOST_CHECK_CLOSE(6., weighted_mean(acc, weights = weight_acc), 1e-5);
42 }
43 
44 ///////////////////////////////////////////////////////////////////////////////
45 // init_unit_test_suite
46 //
init_unit_test_suite(int argc,char * argv[])47 test_suite* init_unit_test_suite( int argc, char* argv[] )
48 {
49     test_suite *test = BOOST_TEST_SUITE("external_weights test");
50 
51     test->add(BOOST_TEST_CASE(&test_stat));
52 
53     return test;
54 }
55