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