• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2018 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <boost/core/lightweight_test.hpp>
8 #include <boost/histogram/accumulators/ostream.hpp>
9 #include <boost/histogram/accumulators/sum.hpp>
10 #include <boost/histogram/accumulators/weighted_sum.hpp>
11 #include <boost/histogram/weight.hpp>
12 #include <sstream>
13 #include "throw_exception.hpp"
14 #include "utility_str.hpp"
15 
16 using namespace boost::histogram;
17 using namespace std::literals;
18 
main()19 int main() {
20   {
21     using w_t = accumulators::weighted_sum<double>;
22     w_t w;
23     BOOST_TEST_EQ(str(w), "weighted_sum(0, 0)"s);
24     BOOST_TEST_EQ(str(w, 20, false), "  weighted_sum(0, 0)"s);
25     BOOST_TEST_EQ(str(w, 20, true), "weighted_sum(0, 0)  "s);
26     BOOST_TEST_EQ(w, w_t{});
27 
28     BOOST_TEST_EQ(w, w_t(0));
29     BOOST_TEST_NE(w, w_t(1));
30     w = w_t(1);
31     BOOST_TEST_EQ(w.value(), 1);
32     BOOST_TEST_EQ(w.variance(), 1);
33     BOOST_TEST_EQ(w, 1);
34     BOOST_TEST_NE(w, 2);
35 
36     w += weight(2);
37     BOOST_TEST_EQ(w.value(), 3);
38     BOOST_TEST_EQ(w.variance(), 5);
39     BOOST_TEST_EQ(w, w_t(3, 5));
40     BOOST_TEST_NE(w, w_t(3));
41 
42     w += w_t(1, 2);
43     BOOST_TEST_EQ(w.value(), 4);
44     BOOST_TEST_EQ(w.variance(), 7);
45 
46     // consistency: a weighted counter increased by weight 1 multiplied
47     // by 2 must be the same as a weighted counter increased by weight 2
48     w_t u(0);
49     ++u;
50     u *= 2;
51     BOOST_TEST_EQ(u, w_t(2, 4));
52 
53     w_t v(0);
54     v += weight(2);
55     BOOST_TEST_EQ(u, v);
56 
57     // conversion to RealType
58     w_t y(1, 2);
59     BOOST_TEST_NE(y, 1);
60     BOOST_TEST_EQ(static_cast<double>(y), 1);
61 
62     BOOST_TEST_EQ(w_t() += w_t(), w_t());
63   }
64 
65   // sum nested in weighted_sum
66   {
67     using s_t = accumulators::weighted_sum<accumulators::sum<double>>;
68     s_t w;
69 
70     ++w;
71     w += weight(1e100);
72     ++w;
73     w += weight(-1e100);
74 
75     BOOST_TEST_EQ(w.value(), 2);
76     BOOST_TEST_EQ(w.variance(), 2e200);
77 
78     BOOST_TEST_EQ(s_t() += s_t(), s_t());
79   }
80 
81   return boost::report_errors();
82 }
83