• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2019 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/histogram/axis/ostream.hpp>
9 #include <boost/histogram/axis/regular.hpp>
10 #include <limits>
11 #include <sstream>
12 #include <type_traits>
13 #include "is_close.hpp"
14 #include "std_ostream.hpp"
15 #include "throw_exception.hpp"
16 #include "utility_axis.hpp"
17 #include "utility_str.hpp"
18 
main()19 int main() {
20   using namespace boost::histogram;
21   using def = use_default;
22   namespace tr = axis::transform;
23 
24   BOOST_TEST(std::is_nothrow_move_assignable<axis::regular<>>::value);
25   BOOST_TEST(std::is_nothrow_move_constructible<axis::regular<>>::value);
26 
27   // bad_ctors
28   {
29     BOOST_TEST_THROWS(axis::regular<>(1, 0, 0), std::invalid_argument);
30     BOOST_TEST_THROWS(axis::regular<>(0, 0, 1), std::invalid_argument);
31   }
32 
33   // ctors and assignment
34   {
35     axis::regular<> a{4, -2, 2};
36     axis::regular<> b;
37     BOOST_TEST_NE(a, b);
38     b = a;
39     BOOST_TEST_EQ(a, b);
40     axis::regular<> c = std::move(b);
41     BOOST_TEST_EQ(c, a);
42     axis::regular<> d;
43     BOOST_TEST_NE(c, d);
44     d = std::move(c);
45     BOOST_TEST_EQ(d, a);
46   }
47 
48   // input, output
49   {
50     axis::regular<> a{4, -2, 2, "foo"};
51     BOOST_TEST_EQ(a.metadata(), "foo");
52     const auto& cref = a;
53     BOOST_TEST_EQ(cref.metadata(), "foo");
54     cref.metadata() = "bar"; // this is allowed
55     BOOST_TEST_EQ(cref.metadata(), "bar");
56     BOOST_TEST_EQ(a.value(0), -2);
57     BOOST_TEST_EQ(a.value(1), -1);
58     BOOST_TEST_EQ(a.value(2), 0);
59     BOOST_TEST_EQ(a.value(3), 1);
60     BOOST_TEST_EQ(a.value(4), 2);
61     BOOST_TEST_EQ(a.bin(-1).lower(), -std::numeric_limits<double>::infinity());
62     BOOST_TEST_EQ(a.bin(-1).upper(), -2);
63     BOOST_TEST_EQ(a.bin(a.size()).lower(), 2);
64     BOOST_TEST_EQ(a.bin(a.size()).upper(), std::numeric_limits<double>::infinity());
65     BOOST_TEST_EQ(a.index(-10.), -1);
66     BOOST_TEST_EQ(a.index(-2.1), -1);
67     BOOST_TEST_EQ(a.index(-2.0), 0);
68     BOOST_TEST_EQ(a.index(-1.1), 0);
69     BOOST_TEST_EQ(a.index(0.0), 2);
70     BOOST_TEST_EQ(a.index(0.9), 2);
71     BOOST_TEST_EQ(a.index(1.0), 3);
72     BOOST_TEST_EQ(a.index(10.), 4);
73     BOOST_TEST_EQ(a.index(-std::numeric_limits<double>::infinity()), -1);
74     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::infinity()), 4);
75     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::quiet_NaN()), 4);
76 
77     BOOST_TEST_EQ(str(a),
78                   "regular(4, -2, 2, metadata=\"bar\", options=underflow | overflow)");
79   }
80 
81   // with inverted range
82   {
83     axis::regular<> a{2, 1, -2};
84     BOOST_TEST_EQ(a.bin(-1).lower(), std::numeric_limits<double>::infinity());
85     BOOST_TEST_EQ(a.bin(0).lower(), 1);
86     BOOST_TEST_EQ(a.bin(1).lower(), -0.5);
87     BOOST_TEST_EQ(a.bin(2).lower(), -2);
88     BOOST_TEST_EQ(a.bin(2).upper(), -std::numeric_limits<double>::infinity());
89     BOOST_TEST_EQ(a.index(2), -1);
90     BOOST_TEST_EQ(a.index(1.001), -1);
91     BOOST_TEST_EQ(a.index(1), 0);
92     BOOST_TEST_EQ(a.index(0), 0);
93     BOOST_TEST_EQ(a.index(-0.499), 0);
94     BOOST_TEST_EQ(a.index(-0.5), 1);
95     BOOST_TEST_EQ(a.index(-1), 1);
96     BOOST_TEST_EQ(a.index(-2), 2);
97     BOOST_TEST_EQ(a.index(-20), 2);
98   }
99 
100   // with log transform
101   {
102     auto a = axis::regular<double, tr::log>{2, 1e0, 1e2};
103     BOOST_TEST_EQ(a.bin(-1).lower(), 0.0);
104     BOOST_TEST_IS_CLOSE(a.bin(0).lower(), 1.0, 1e-9);
105     BOOST_TEST_IS_CLOSE(a.bin(1).lower(), 10.0, 1e-9);
106     BOOST_TEST_IS_CLOSE(a.bin(2).lower(), 100.0, 1e-9);
107     BOOST_TEST_EQ(a.bin(2).upper(), std::numeric_limits<double>::infinity());
108 
109     BOOST_TEST_EQ(a.index(-1), 2); // produces NaN in conversion
110     BOOST_TEST_EQ(a.index(0), -1);
111     BOOST_TEST_EQ(a.index(1), 0);
112     BOOST_TEST_EQ(a.index(9), 0);
113     BOOST_TEST_EQ(a.index(10), 1);
114     BOOST_TEST_EQ(a.index(90), 1);
115     BOOST_TEST_EQ(a.index(100), 2);
116     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::infinity()), 2);
117 
118     BOOST_TEST_THROWS((axis::regular<double, tr::log>{2, -1, 0}), std::invalid_argument);
119 
120     BOOST_TEST_CSTR_EQ(
121         str(a).c_str(),
122         "regular(transform::log{}, 2, 1, 100, options=underflow | overflow)");
123   }
124 
125   // with sqrt transform
126   {
127     axis::regular<double, tr::sqrt> a(2, 0, 4);
128     // this is weird, but -inf * -inf = inf, thus the lower bound
129     BOOST_TEST_EQ(a.bin(-1).lower(), std::numeric_limits<double>::infinity());
130     BOOST_TEST_IS_CLOSE(a.bin(0).lower(), 0.0, 1e-9);
131     BOOST_TEST_IS_CLOSE(a.bin(1).lower(), 1.0, 1e-9);
132     BOOST_TEST_IS_CLOSE(a.bin(2).lower(), 4.0, 1e-9);
133     BOOST_TEST_EQ(a.bin(2).upper(), std::numeric_limits<double>::infinity());
134 
135     BOOST_TEST_EQ(a.index(-1), 2); // produces NaN in conversion
136     BOOST_TEST_EQ(a.index(0), 0);
137     BOOST_TEST_EQ(a.index(0.99), 0);
138     BOOST_TEST_EQ(a.index(1), 1);
139     BOOST_TEST_EQ(a.index(3.99), 1);
140     BOOST_TEST_EQ(a.index(4), 2);
141     BOOST_TEST_EQ(a.index(100), 2);
142     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::infinity()), 2);
143 
144     BOOST_TEST_EQ(str(a),
145                   "regular(transform::sqrt{}, 2, 0, 4, options=underflow | overflow)");
146   }
147 
148   // with pow transform
149   {
150     axis::regular<double, tr::pow> a(tr::pow{0.5}, 2, 0, 4);
151     // this is weird, but -inf * -inf = inf, thus the lower bound
152     BOOST_TEST_EQ(a.bin(-1).lower(), std::numeric_limits<double>::infinity());
153     BOOST_TEST_IS_CLOSE(a.bin(0).lower(), 0.0, 1e-9);
154     BOOST_TEST_IS_CLOSE(a.bin(1).lower(), 1.0, 1e-9);
155     BOOST_TEST_IS_CLOSE(a.bin(2).lower(), 4.0, 1e-9);
156     BOOST_TEST_EQ(a.bin(2).upper(), std::numeric_limits<double>::infinity());
157 
158     BOOST_TEST_EQ(a.index(-1), 2); // produces NaN in conversion
159     BOOST_TEST_EQ(a.index(0), 0);
160     BOOST_TEST_EQ(a.index(0.99), 0);
161     BOOST_TEST_EQ(a.index(1), 1);
162     BOOST_TEST_EQ(a.index(3.99), 1);
163     BOOST_TEST_EQ(a.index(4), 2);
164     BOOST_TEST_EQ(a.index(100), 2);
165     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::infinity()), 2);
166 
167     BOOST_TEST_EQ(str(a),
168                   "regular(transform::pow{0.5}, 2, 0, 4, options=underflow | overflow)");
169   }
170 
171   // with step
172   {
173     axis::regular<> a(axis::step(0.5), 1, 3);
174     BOOST_TEST_EQ(a.size(), 4);
175     BOOST_TEST_EQ(a.bin(-1).lower(), -std::numeric_limits<double>::infinity());
176     BOOST_TEST_EQ(a.value(0), 1);
177     BOOST_TEST_EQ(a.value(1), 1.5);
178     BOOST_TEST_EQ(a.value(2), 2);
179     BOOST_TEST_EQ(a.value(3), 2.5);
180     BOOST_TEST_EQ(a.value(4), 3);
181     BOOST_TEST_EQ(a.bin(4).upper(), std::numeric_limits<double>::infinity());
182 
183     axis::regular<> b(axis::step(0.5), 1, 3.1);
184     BOOST_TEST_EQ(a, b);
185   }
186 
187   // with circular option
188   {
189     axis::circular<> a{4, 0, 1};
190     BOOST_TEST_EQ(a.bin(-1).lower(), a.bin(a.size() - 1).lower() - 1);
191     BOOST_TEST_EQ(a.index(-1.0 * 3), 0);
192     BOOST_TEST_EQ(a.index(0.0), 0);
193     BOOST_TEST_EQ(a.index(0.25), 1);
194     BOOST_TEST_EQ(a.index(0.5), 2);
195     BOOST_TEST_EQ(a.index(0.75), 3);
196     BOOST_TEST_EQ(a.index(1.0), 0);
197     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::infinity()), 4);
198     BOOST_TEST_EQ(a.index(-std::numeric_limits<double>::infinity()), 4);
199     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::quiet_NaN()), 4);
200   }
201 
202   // with growth
203   {
204     using pii_t = std::pair<axis::index_type, axis::index_type>;
205     axis::regular<double, def, def, axis::option::growth_t> a{1, 0, 1};
206     BOOST_TEST_EQ(a.size(), 1);
207     BOOST_TEST_EQ(a.update(0), pii_t(0, 0));
208     BOOST_TEST_EQ(a.size(), 1);
209     BOOST_TEST_EQ(a.update(1), pii_t(1, -1));
210     BOOST_TEST_EQ(a.size(), 2);
211     BOOST_TEST_EQ(a.value(0), 0);
212     BOOST_TEST_EQ(a.value(2), 2);
213     BOOST_TEST_EQ(a.update(-1), pii_t(0, 1));
214     BOOST_TEST_EQ(a.size(), 3);
215     BOOST_TEST_EQ(a.value(0), -1);
216     BOOST_TEST_EQ(a.value(3), 2);
217     BOOST_TEST_EQ(a.update(-10), pii_t(0, 9));
218     BOOST_TEST_EQ(a.size(), 12);
219     BOOST_TEST_EQ(a.value(0), -10);
220     BOOST_TEST_EQ(a.value(12), 2);
221     BOOST_TEST_EQ(a.update(std::numeric_limits<double>::infinity()), pii_t(a.size(), 0));
222     BOOST_TEST_EQ(a.update(std::numeric_limits<double>::quiet_NaN()), pii_t(a.size(), 0));
223     BOOST_TEST_EQ(a.update(-std::numeric_limits<double>::infinity()), pii_t(-1, 0));
224   }
225 
226   // iterators
227   {
228     test_axis_iterator(axis::regular<>(5, 0, 1), 0, 5);
229     test_axis_iterator(axis::regular<double, def, def, axis::option::none_t>(5, 0, 1), 0,
230                        5);
231     test_axis_iterator(axis::circular<>(5, 0, 1), 0, 5);
232   }
233 
234   // bin_type streamable
235   {
236     auto test = [](const auto& x, const char* ref) {
237       std::ostringstream os;
238       os << x;
239       BOOST_TEST_EQ(os.str(), std::string(ref));
240     };
241 
242     auto a = axis::regular<>(2, 0, 1);
243     test(a.bin(0), "[0, 0.5)");
244   }
245 
246   // null_type streamable
247   {
248     auto a = axis::regular<float, def, axis::null_type>(2, 0, 1);
249     BOOST_TEST_EQ(str(a), "regular(2, 0, 1, options=underflow | overflow)");
250   }
251 
252   // shrink and rebin
253   {
254     using A = axis::regular<>;
255     auto a = A(5, 0, 5);
256     auto b = A(a, 1, 4, 1);
257     BOOST_TEST_EQ(b.size(), 3);
258     BOOST_TEST_EQ(b.value(0), 1);
259     BOOST_TEST_EQ(b.value(3), 4);
260     auto c = A(a, 0, 4, 2);
261     BOOST_TEST_EQ(c.size(), 2);
262     BOOST_TEST_EQ(c.value(0), 0);
263     BOOST_TEST_EQ(c.value(2), 4);
264     auto e = A(a, 1, 5, 2);
265     BOOST_TEST_EQ(e.size(), 2);
266     BOOST_TEST_EQ(e.value(0), 1);
267     BOOST_TEST_EQ(e.value(2), 5);
268   }
269 
270   // shrink and rebin with circular option
271   {
272     using A = axis::circular<>;
273     auto a = A(4, 1, 5);
274     BOOST_TEST_THROWS(A(a, 1, 4, 1), std::invalid_argument);
275     BOOST_TEST_THROWS(A(a, 0, 3, 1), std::invalid_argument);
276     auto b = A(a, 0, 4, 2);
277     BOOST_TEST_EQ(b.size(), 2);
278     BOOST_TEST_EQ(b.value(0), 1);
279     BOOST_TEST_EQ(b.value(2), 5);
280   }
281 
282   return boost::report_errors();
283 }
284