Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / histogram / test / histogram_dynamic_test.cpp
1 // Copyright 2015-2017 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 <algorithm>
8 #include <boost/core/ignore_unused.hpp>
9 #include <boost/core/lightweight_test.hpp>
10 #include <boost/histogram/axis.hpp>
11 #include <boost/histogram/axis/ostream.hpp>
12 #include <boost/histogram/histogram.hpp>
13 #include <boost/histogram/make_histogram.hpp>
14 #include <boost/histogram/ostream.hpp>
15 #include <cstdlib>
16 #include <limits>
17 #include <numeric>
18 #include <sstream>
19 #include <tuple>
20 #include <utility>
21 #include <vector>
22 #include "throw_exception.hpp"
23 #include "utility_histogram.hpp"
24
25 using namespace boost::histogram;
26
27 int main() {
28   // special stuff that only works with dynamic_tag
29
30   // init with vector of axis and vector of axis::variant
31   {
32     using R = axis::regular<>;
33     using I = axis::integer<>;
34     using V = axis::variable<>;
35
36     auto v = std::vector<axis::variant<R, I, V>>();
37     v.emplace_back(R{4, -1, 1});
38     v.emplace_back(I{1, 7});
39     v.emplace_back(V{1, 2, 3});
40     auto h = make_histogram(v.begin(), v.end());
41     BOOST_TEST_EQ(h.rank(), 3);
42     BOOST_TEST_EQ(h.axis(0), v[0]);
43     BOOST_TEST_EQ(h.axis(1), v[1]);
44     BOOST_TEST_EQ(h.axis(2), v[2]);
45
46     auto h2 = make_histogram_with(std::vector<int>(), v);
47     BOOST_TEST_EQ(h2.rank(), 3);
48     BOOST_TEST_EQ(h2.axis(0), v[0]);
49     BOOST_TEST_EQ(h2.axis(1), v[1]);
50     BOOST_TEST_EQ(h2.axis(2), v[2]);
51
52     auto v2 = std::vector<R>();
53     v2.emplace_back(10, 0, 1);
54     v2.emplace_back(20, 0, 2);
55     auto h3 = make_histogram(v2);
56     BOOST_TEST_EQ(h3.axis(0), v2[0]);
57     BOOST_TEST_EQ(h3.axis(1), v2[1]);
58   }
59
60   // too many axes
61   {
62     using I = axis::integer<int, axis::null_type, axis::option::none_t>;
63
64     // test edge case
65     auto av = std::vector<I>(BOOST_HISTOGRAM_DETAIL_AXES_LIMIT, I(0, 1));
66     auto h = make_histogram(av);
67     auto inputs = std::vector<std::vector<int>>(BOOST_HISTOGRAM_DETAIL_AXES_LIMIT,
68                                                 std::vector<int>(1, 0));
69     h.fill(inputs); // should not crash
70
71     auto bad = std::vector<I>(BOOST_HISTOGRAM_DETAIL_AXES_LIMIT + 1, I(0, 1));
72     boost::ignore_unused(bad);
73     BOOST_TEST_THROWS((void)make_histogram(bad), std::invalid_argument);
74   }
75
76   // bad fill
77   {
78     auto a = axis::integer<>(0, 1);
79     auto b = make(dynamic_tag(), a);
80     BOOST_TEST_THROWS(b(0, 0), std::invalid_argument);
81     auto c = make(dynamic_tag(), a, a);
82     BOOST_TEST_THROWS(c(0), std::invalid_argument);
83     auto d = make(dynamic_tag(), a);
84     BOOST_TEST_THROWS(d(std::string()), std::invalid_argument);
85
86     struct axis2d {
87       auto index(const std::tuple<double, double>& x) const {
88         return axis::index_type{std::get<0>(x) == 1 && std::get<1>(x) == 2};
89       }
90       auto size() const { return axis::index_type{2}; }
91     } e;
92
93     auto f = make(dynamic_tag(), a, e);
94     BOOST_TEST_THROWS(f(0, 0, 0), std::invalid_argument);
95     BOOST_TEST_THROWS(f(0, std::make_tuple(0, 0), 1), std::invalid_argument);
96   }
97
98   // bad at
99   {
100     auto h1 = make(dynamic_tag(), axis::integer<>(0, 2));
101     h1(1);
102     BOOST_TEST_THROWS(h1.at(0, 0), std::invalid_argument);
103     BOOST_TEST_THROWS(h1.at(std::make_tuple(0, 0)), std::invalid_argument);
104   }
105
106   // incompatible axis variant methods
107   {
108     auto c = make(dynamic_tag(), axis::category<std::string>({"A", "B"}));
109     BOOST_TEST_THROWS(c.axis().value(0), std::runtime_error);
110   }
111
112   {
113     auto h = make_histogram(std::vector<axis::integer<>>(1, axis::integer<>(0, 3)));
114     h(0);
115     h(1);
116     h(2);
117     BOOST_TEST_EQ(h.at(0), 1);
118     BOOST_TEST_EQ(h.at(1), 1);
119     BOOST_TEST_EQ(h.at(2), 1);
120   }
121
122   return boost::report_errors();
123 }