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