• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-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/histogram/axis/integer.hpp>
9 #include <boost/histogram/axis/ostream.hpp>
10 #include <limits>
11 #include <sstream>
12 #include <type_traits>
13 #include "std_ostream.hpp"
14 #include "throw_exception.hpp"
15 #include "utility_axis.hpp"
16 #include "utility_str.hpp"
17 
main()18 int main() {
19   using namespace boost::histogram;
20 
21   BOOST_TEST(std::is_nothrow_move_assignable<axis::integer<>>::value);
22   BOOST_TEST(std::is_nothrow_move_constructible<axis::integer<>>::value);
23 
24   // bad_ctor
25   { BOOST_TEST_THROWS(axis::integer<>(1, -1), std::invalid_argument); }
26 
27   // axis::integer with double type
28   {
29     axis::integer<double> a{-1, 2, "foo"};
30     BOOST_TEST_EQ(a.metadata(), "foo");
31     BOOST_TEST_EQ(static_cast<const axis::integer<double>&>(a).metadata(), "foo");
32     a.metadata() = "bar";
33     BOOST_TEST_EQ(static_cast<const axis::integer<double>&>(a).metadata(), "bar");
34     BOOST_TEST_EQ(a.bin(-1).lower(), -std::numeric_limits<double>::infinity());
35     BOOST_TEST_EQ(a.bin(a.size()).upper(), std::numeric_limits<double>::infinity());
36     BOOST_TEST_EQ(a.index(-10), -1);
37     BOOST_TEST_EQ(a.index(-2), -1);
38     BOOST_TEST_EQ(a.index(-1), 0);
39     BOOST_TEST_EQ(a.index(0), 1);
40     BOOST_TEST_EQ(a.index(1), 2);
41     BOOST_TEST_EQ(a.index(2), 3);
42     BOOST_TEST_EQ(a.index(10), 3);
43     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::quiet_NaN()), 3);
44 
45     BOOST_TEST_EQ(str(a),
46                   "integer(-1, 2, metadata=\"bar\", options=underflow | overflow)");
47 
48     axis::integer<double> b;
49     BOOST_TEST_NE(a, b);
50     b = a;
51     BOOST_TEST_EQ(a, b);
52     axis::integer<double> c = std::move(b);
53     BOOST_TEST_EQ(c, a);
54     axis::integer<double> d;
55     BOOST_TEST_NE(c, d);
56     d = std::move(c);
57     BOOST_TEST_EQ(d, a);
58   }
59 
60   // empty axis::integer
61   {
62     axis::integer<> a;
63     BOOST_TEST_EQ(a.size(), 0);
64     BOOST_TEST_EQ(a.bin(1), 1);
65     BOOST_TEST_EQ(a.bin(0), 0);
66     BOOST_TEST_EQ(a.bin(-1), -1);
67     BOOST_TEST_EQ(a.index(-10), -1);
68     BOOST_TEST_EQ(a.index(-1), -1);
69     BOOST_TEST_EQ(a.index(0), 0);
70     BOOST_TEST_EQ(a.index(10), 0);
71 
72     BOOST_TEST_EQ(str(a), "integer(0, 0, options=underflow | overflow)");
73 
74     axis::integer<> b{1, 1};
75     BOOST_TEST_EQ(b.size(), 0);
76     BOOST_TEST_EQ(b.bin(1), 2);
77     BOOST_TEST_EQ(b.bin(0), 1);
78     BOOST_TEST_EQ(b.bin(-1), 0);
79     BOOST_TEST_EQ(b.index(-10), -1);
80     BOOST_TEST_EQ(b.index(-1), -1);
81     BOOST_TEST_EQ(b.index(0), -1);
82     BOOST_TEST_EQ(b.index(1), 0);
83     BOOST_TEST_EQ(b.index(10), 0);
84 
85     BOOST_TEST_EQ(str(b), "integer(1, 1, options=underflow | overflow)");
86   }
87 
88   // axis::integer with int type
89   {
90     axis::integer<int> a{-1, 2};
91     BOOST_TEST_EQ(a.bin(-2), -3);
92     BOOST_TEST_EQ(a.bin(4), 3);
93     BOOST_TEST_EQ(a.index(-10), -1);
94     BOOST_TEST_EQ(a.index(-2), -1);
95     BOOST_TEST_EQ(a.index(-1), 0);
96     BOOST_TEST_EQ(a.index(0), 1);
97     BOOST_TEST_EQ(a.index(1), 2);
98     BOOST_TEST_EQ(a.index(2), 3);
99     BOOST_TEST_EQ(a.index(10), 3);
100 
101     BOOST_TEST_EQ(str(a), "integer(-1, 2, options=underflow | overflow)");
102   }
103 
104   // axis::integer int,circular
105   {
106     axis::integer<int, axis::null_type, axis::option::circular_t> a(-1, 1);
107     BOOST_TEST_EQ(a.value(-1), -2);
108     BOOST_TEST_EQ(a.value(0), -1);
109     BOOST_TEST_EQ(a.value(1), 0);
110     BOOST_TEST_EQ(a.value(2), 1);
111     BOOST_TEST_EQ(a.value(3), 2);
112     BOOST_TEST_EQ(a.index(-2), 1);
113     BOOST_TEST_EQ(a.index(-1), 0);
114     BOOST_TEST_EQ(a.index(0), 1);
115     BOOST_TEST_EQ(a.index(1), 0);
116     BOOST_TEST_EQ(a.index(2), 1);
117 
118     BOOST_TEST_EQ(str(a), "integer(-1, 1, options=circular)");
119   }
120 
121   // axis::integer double,circular
122   {
123     axis::integer<double, axis::null_type, axis::option::circular_t> a(-1, 1);
124     BOOST_TEST_EQ(a.value(-1), -2);
125     BOOST_TEST_EQ(a.value(0), -1);
126     BOOST_TEST_EQ(a.value(1), 0);
127     BOOST_TEST_EQ(a.value(2), 1);
128     BOOST_TEST_EQ(a.value(3), 2);
129     BOOST_TEST_EQ(a.index(-2), 1);
130     BOOST_TEST_EQ(a.index(-1.9), 1);
131     BOOST_TEST_EQ(a.index(-1.1), 1);
132     BOOST_TEST_EQ(a.index(-1), 0);
133     BOOST_TEST_EQ(a.index(0), 1);
134     BOOST_TEST_EQ(a.index(1), 0);
135     BOOST_TEST_EQ(a.index(2), 1);
136     BOOST_TEST_EQ(a.index(2.1), 1);
137     BOOST_TEST_EQ(a.index(2.9), 1);
138 
139     BOOST_TEST_EQ(a.index(std::numeric_limits<double>::quiet_NaN()), 2);
140   }
141 
142   // axis::integer with growth
143   {
144     using pii_t = std::pair<axis::index_type, axis::index_type>;
145     axis::integer<double, axis::null_type, axis::option::growth_t> a;
146     BOOST_TEST_EQ(a.size(), 0);
147     BOOST_TEST_EQ(a.update(0), pii_t(0, -1));
148     BOOST_TEST_EQ(a.size(), 1);
149     BOOST_TEST_EQ(a.update(1), pii_t(1, -1));
150     BOOST_TEST_EQ(a.size(), 2);
151     BOOST_TEST_EQ(a.update(-1), pii_t(0, 1));
152     BOOST_TEST_EQ(a.size(), 3);
153     BOOST_TEST_EQ(a.update(std::numeric_limits<double>::infinity()), pii_t(a.size(), 0));
154     BOOST_TEST_EQ(a.update(std::numeric_limits<double>::quiet_NaN()), pii_t(a.size(), 0));
155     BOOST_TEST_EQ(a.update(-std::numeric_limits<double>::infinity()), pii_t(-1, 0));
156   }
157 
158   // iterators
159   {
160     test_axis_iterator(axis::integer<int>(0, 4), 0, 4);
161     test_axis_iterator(axis::integer<double>(0, 4), 0, 4);
162     test_axis_iterator(
163         axis::integer<int, axis::null_type, axis::option::circular_t>(0, 4), 0, 4);
164   }
165 
166   // shrink and rebin
167   {
168     using A = axis::integer<>;
169     auto a = A(-1, 5);
170     auto b = A(a, 2, 5, 1);
171     BOOST_TEST_EQ(b.size(), 3);
172     BOOST_TEST_EQ(b.value(0), 1);
173     BOOST_TEST_EQ(b.value(3), 4);
174     auto c = A(a, 1, 5, 1);
175     BOOST_TEST_EQ(c.size(), 4);
176     BOOST_TEST_EQ(c.value(0), 0);
177     BOOST_TEST_EQ(c.value(4), 4);
178     auto e = A(a, 2, 5, 1);
179     BOOST_TEST_EQ(e.size(), 3);
180     BOOST_TEST_EQ(e.value(0), 1);
181     BOOST_TEST_EQ(e.value(3), 4);
182   }
183 
184   // shrink and rebin with circular option
185   {
186     using A = axis::integer<int, axis::null_type, axis::option::circular_t>;
187     auto a = A(1, 5);
188     auto b = A(a, 0, 4, 1);
189     BOOST_TEST_EQ(a, b);
190     BOOST_TEST_THROWS(A(a, 1, 4, 1), std::invalid_argument);
191     BOOST_TEST_THROWS(A(a, 0, 3, 1), std::invalid_argument);
192     BOOST_TEST_THROWS(A(a, 0, 4, 2), std::invalid_argument);
193   }
194 
195   return boost::report_errors();
196 }
197