• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 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 <array>
8 #include <boost/core/lightweight_test.hpp>
9 #include <boost/histogram/accumulators/weighted_sum.hpp>
10 #include <boost/histogram/algorithm/sum.hpp>
11 #include <boost/histogram/axis/integer.hpp>
12 #include <unordered_map>
13 #include <vector>
14 #include "throw_exception.hpp"
15 #include "utility_histogram.hpp"
16 
17 using namespace boost::histogram;
18 using boost::histogram::algorithm::sum;
19 
20 template <typename Tag>
run_tests()21 void run_tests() {
22   auto ax = axis::integer<>(0, 10);
23 
24   {
25     auto h = make(Tag(), ax);
26     std::fill(h.begin(), h.end(), 1);
27     BOOST_TEST_EQ(sum(h), 12);
28     BOOST_TEST_EQ(sum(h, coverage::inner), 10);
29   }
30 
31   {
32     auto h = make_s(Tag(), std::array<int, 12>(), ax);
33     std::fill(h.begin(), h.end(), 1);
34     BOOST_TEST_EQ(sum(h), 12);
35     BOOST_TEST_EQ(sum(h, coverage::inner), 10);
36   }
37 
38   {
39     auto h = make_s(Tag(), std::unordered_map<std::size_t, int>(), ax);
40     std::fill(h.begin(), h.end(), 1);
41     BOOST_TEST_EQ(sum(h), 12);
42     BOOST_TEST_EQ(sum(h, coverage::inner), 10);
43   }
44 
45   {
46     auto h = make_s(Tag(), std::vector<double>(), ax, ax);
47     std::fill(h.begin(), h.end(), 1);
48     BOOST_TEST_EQ(sum(h), 12 * 12);
49     BOOST_TEST_EQ(sum(h, coverage::inner), 10 * 10);
50   }
51 
52   {
53     using W = accumulators::weighted_sum<>;
54     auto h = make_s(Tag(), std::vector<W>(), axis::integer<>(0, 2),
55                     axis::integer<int, axis::null_type, axis::option::none_t>(2, 4));
56     W w(0, 2);
57     for (auto&& x : h) {
58       x = w;
59       w = W(w.value() + 1, 2);
60     }
61 
62     // x-axis has 4 bins, y-axis has 2 = 8 bins total with 4 inner bins
63 
64     const auto v1 = algorithm::sum(h);
65     BOOST_TEST_EQ(v1.value(), 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7);
66     BOOST_TEST_EQ(v1.variance(), 8 * 2);
67 
68     const auto v2 = algorithm::sum(h, coverage::inner);
69     BOOST_TEST_EQ(v2.value(), 1 + 2 + 5 + 6);
70     BOOST_TEST_EQ(v2.variance(), 4 * 2);
71   }
72 }
73 
main()74 int main() {
75   run_tests<static_tag>();
76   run_tests<dynamic_tag>();
77 
78   return boost::report_errors();
79 }
80