1 // Copyright 2018 Hans Dembinski
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)
7 #include <boost/core/lightweight_test.hpp>
8 #include <boost/core/lightweight_test_trait.hpp>
9 #include <boost/histogram/accumulators/weighted_sum.hpp>
10 #include <boost/histogram/axis.hpp>
11 #include <boost/histogram/axis/ostream.hpp>
12 #include <boost/histogram/histogram.hpp>
13 #include <boost/histogram/ostream.hpp>
14 #include <boost/histogram/storage_adaptor.hpp>
15 #include <boost/histogram/unlimited_storage.hpp>
17 #include <type_traits>
19 #include "std_ostream.hpp"
20 #include "throw_exception.hpp"
22 using namespace boost::histogram;
23 namespace tr = axis::transform;
25 // tests requires a C++17 compatible compiler
27 #define TEST BOOST_TEST_TRAIT_SAME
30 using axis::null_type;
34 BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0.0, 1.0)),
35 regular<double, tr::id, null_type>);
36 BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0, 1)), regular<double, tr::id, null_type>);
37 BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0.0f, 1.0f)),
38 regular<float, tr::id, null_type>);
39 BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0, 1, 0)), regular<double, tr::id, int>);
40 BOOST_TEST_TRAIT_SAME(decltype(regular(1, 0.0f, 1.0f, "x")),
41 regular<float, tr::id, std::string>);
42 BOOST_TEST_TRAIT_SAME(decltype(regular(tr::sqrt(), 1, 0, 1)),
43 regular<double, tr::sqrt, null_type>);
44 BOOST_TEST_TRAIT_SAME(decltype(regular(tr::sqrt(), 1, 0.0f, 1.0f, "x")),
45 regular<float, tr::sqrt, std::string>);
46 BOOST_TEST_TRAIT_SAME(decltype(regular(tr::sqrt(), 1, 0, 1, 0)),
47 regular<double, tr::sqrt, int>);
52 BOOST_TEST_TRAIT_SAME(decltype(integer(1, 2)), integer<int, null_type>);
53 BOOST_TEST_TRAIT_SAME(decltype(integer(1l, 2l)), integer<int, null_type>);
54 BOOST_TEST_TRAIT_SAME(decltype(integer(1.0, 2.0)), integer<double, null_type>);
55 BOOST_TEST_TRAIT_SAME(decltype(integer(1.0f, 2.0f)), integer<float, null_type>);
56 BOOST_TEST_TRAIT_SAME(decltype(integer(1, 2, "foo")), integer<int, std::string>);
57 BOOST_TEST_TRAIT_SAME(decltype(integer(1, 2, 0)), integer<int, int>);
62 BOOST_TEST_TRAIT_SAME(decltype(variable{-1.0f, 1.0f}), variable<float, null_type>);
63 BOOST_TEST_TRAIT_SAME(decltype(variable{-1, 0, 1, 2}), variable<double, null_type>);
64 BOOST_TEST_TRAIT_SAME(decltype(variable{-1.0, 1.0}), variable<double, null_type>);
65 BOOST_TEST_TRAIT_SAME(decltype(variable({-1, 0, 1}, "foo")),
66 variable<double, std::string>);
67 BOOST_TEST_TRAIT_SAME(decltype(variable({-1, 1}, 0)), variable<double, int>);
69 BOOST_TEST_TRAIT_SAME(decltype(variable(std::vector<int>{{-1, 1}})),
70 variable<double, null_type>);
71 BOOST_TEST_TRAIT_SAME(decltype(variable(std::vector<float>{{-1.0f, 1.0f}})),
72 variable<float, null_type>);
73 BOOST_TEST_TRAIT_SAME(decltype(variable(std::vector<int>{{-1, 1}}, "foo")),
74 variable<double, std::string>);
75 BOOST_TEST_TRAIT_SAME(decltype(variable(std::vector<int>{{-1, 1}}, 0)),
76 variable<double, int>);
81 BOOST_TEST_TRAIT_SAME(decltype(category{1, 2}), category<int, null_type>);
82 BOOST_TEST_TRAIT_SAME(decltype(category{"x", "y"}), category<std::string, null_type>);
83 BOOST_TEST_TRAIT_SAME(decltype(category({1, 2}, "foo")), category<int, std::string>);
84 BOOST_TEST_TRAIT_SAME(decltype(category({1, 2}, 0)), category<int, int>);
88 auto h = histogram(axis::regular(3, -1, 1), axis::integer(0, 4));
89 BOOST_TEST_TRAIT_SAME(decltype(h),
90 histogram<std::tuple<axis::regular<double, tr::id, null_type>,
91 axis::integer<int, null_type>>>);
92 BOOST_TEST_EQ(h.axis(0), axis::regular(3, -1, 1));
93 BOOST_TEST_EQ(h.axis(1), axis::integer(0, 4));
97 auto h = histogram(std::tuple(axis::regular(3, -1, 1), axis::integer(0, 4)),
99 BOOST_TEST_TRAIT_SAME(decltype(h),
100 histogram<std::tuple<axis::regular<double, tr::id, null_type>,
101 axis::integer<int, null_type>>,
103 BOOST_TEST_EQ(h.axis(0), axis::regular(3, -1, 1));
104 BOOST_TEST_EQ(h.axis(1), axis::integer(0, 4));
108 auto a0 = axis::regular(5, 0, 5);
109 auto a1 = axis::regular(3, -1, 1);
110 auto axes = {a0, a1};
111 auto h = histogram(axes);
112 BOOST_TEST_TRAIT_SAME(
113 decltype(h), histogram<std::vector<axis::regular<double, tr::id, null_type>>>);
114 BOOST_TEST_EQ(h.rank(), 2);
115 BOOST_TEST_EQ(h.axis(0), a0);
116 BOOST_TEST_EQ(h.axis(1), a1);
120 auto a0 = axis::regular(5, 0, 5);
121 auto a1 = axis::regular(3, -1, 1);
122 auto axes = {a0, a1};
123 auto h = histogram(axes, weight_storage());
124 BOOST_TEST_TRAIT_SAME(
126 histogram<std::vector<axis::regular<double, tr::id, null_type>>, weight_storage>);
127 BOOST_TEST_EQ(h.rank(), 2);
128 BOOST_TEST_EQ(h.axis(0), a0);
129 BOOST_TEST_EQ(h.axis(1), a1);
133 auto a0 = axis::regular(5, 0, 5);
134 auto a1 = axis::regular(3, -1, 1);
135 auto axes = std::vector<decltype(a0)>{{a0, a1}};
136 auto h = histogram(axes);
137 BOOST_TEST_TRAIT_SAME(
138 decltype(h), histogram<std::vector<axis::regular<double, tr::id, null_type>>>);
139 BOOST_TEST_EQ(h.rank(), 2);
140 BOOST_TEST_EQ(h.axis(0), a0);
141 BOOST_TEST_EQ(h.axis(1), a1);
144 return boost::report_errors();