Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / histogram / test / deduction_guides_test.cpp
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 <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>
16 #include <tuple>
17 #include <type_traits>
18 #include <vector>
19 #include "std_ostream.hpp"
20 #include "throw_exception.hpp"
21
22 using namespace boost::histogram;
23 namespace tr = axis::transform;
24
25 // tests requires a C++17 compatible compiler
26
27 #define TEST BOOST_TEST_TRAIT_SAME
28
29 int main() {
30   using axis::null_type;
31
32   {
33     using axis::regular;
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>);
48   }
49
50   {
51     using axis::integer;
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>);
58   }
59
60   {
61     using axis::variable;
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>);
68
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>);
77   }
78
79   {
80     using axis::category;
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>);
85   }
86
87   {
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));
94   }
95
96   {
97     auto h = histogram(std::tuple(axis::regular(3, -1, 1), axis::integer(0, 4)),
98                        weight_storage());
99     BOOST_TEST_TRAIT_SAME(decltype(h),
100                           histogram<std::tuple<axis::regular<double, tr::id, null_type>,
101                                                axis::integer<int, null_type>>,
102                                     weight_storage>);
103     BOOST_TEST_EQ(h.axis(0), axis::regular(3, -1, 1));
104     BOOST_TEST_EQ(h.axis(1), axis::integer(0, 4));
105   }
106
107   {
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);
117   }
118
119   {
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(
125         decltype(h),
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);
130   }
131
132   {
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);
142   }
143
144   return boost::report_errors();
145 }