• 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 <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 
main()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     using axis::boolean;
89     BOOST_TEST_TRAIT_SAME(decltype(boolean{}), boolean<null_type>);
90     BOOST_TEST_TRAIT_SAME(decltype(boolean{"foo"}), boolean<std::string>);
91   }
92 
93   {
94     auto h = histogram(axis::regular(3, -1, 1), axis::integer(0, 4));
95     BOOST_TEST_TRAIT_SAME(decltype(h),
96                           histogram<std::tuple<axis::regular<double, tr::id, null_type>,
97                                                axis::integer<int, null_type>>>);
98     BOOST_TEST_EQ(h.axis(0), axis::regular(3, -1, 1));
99     BOOST_TEST_EQ(h.axis(1), axis::integer(0, 4));
100   }
101 
102   {
103     auto h = histogram(std::tuple(axis::regular(3, -1, 1), axis::integer(0, 4)),
104                        weight_storage());
105     BOOST_TEST_TRAIT_SAME(decltype(h),
106                           histogram<std::tuple<axis::regular<double, tr::id, null_type>,
107                                                axis::integer<int, null_type>>,
108                                     weight_storage>);
109     BOOST_TEST_EQ(h.axis(0), axis::regular(3, -1, 1));
110     BOOST_TEST_EQ(h.axis(1), axis::integer(0, 4));
111   }
112 
113   {
114     auto a0 = axis::regular(5, 0, 5);
115     auto a1 = axis::regular(3, -1, 1);
116     auto axes = {a0, a1};
117     auto h = histogram(axes);
118     BOOST_TEST_TRAIT_SAME(
119         decltype(h), histogram<std::vector<axis::regular<double, tr::id, null_type>>>);
120     BOOST_TEST_EQ(h.rank(), 2);
121     BOOST_TEST_EQ(h.axis(0), a0);
122     BOOST_TEST_EQ(h.axis(1), a1);
123   }
124 
125   {
126     auto a0 = axis::regular(5, 0, 5);
127     auto a1 = axis::regular(3, -1, 1);
128     auto axes = {a0, a1};
129     auto h = histogram(axes, weight_storage());
130     BOOST_TEST_TRAIT_SAME(
131         decltype(h),
132         histogram<std::vector<axis::regular<double, tr::id, null_type>>, weight_storage>);
133     BOOST_TEST_EQ(h.rank(), 2);
134     BOOST_TEST_EQ(h.axis(0), a0);
135     BOOST_TEST_EQ(h.axis(1), a1);
136   }
137 
138   {
139     auto a0 = axis::regular(5, 0, 5);
140     auto a1 = axis::regular(3, -1, 1);
141     auto axes = std::vector<decltype(a0)>{{a0, a1}};
142     auto h = histogram(axes);
143     BOOST_TEST_TRAIT_SAME(
144         decltype(h), histogram<std::vector<axis::regular<double, tr::id, null_type>>>);
145     BOOST_TEST_EQ(h.rank(), 2);
146     BOOST_TEST_EQ(h.axis(0), a0);
147     BOOST_TEST_EQ(h.axis(1), a1);
148   }
149 
150   return boost::report_errors();
151 }
152