• 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/histogram/axis/ostream.hpp>
9 #include <boost/histogram/axis/regular.hpp>
10 #include "throw_exception.hpp"
11 #include <boost/histogram/histogram.hpp>
12 #include <boost/histogram/indexed.hpp>
13 #include <boost/histogram/literals.hpp>
14 #include <boost/histogram/make_histogram.hpp>
15 #include <boost/histogram/ostream.hpp>
16 #include <boost/units/quantity.hpp>
17 #include <boost/units/systems/si/length.hpp>
18 #include <limits>
19 #include "is_close.hpp"
20 
21 using namespace boost::histogram;
22 using namespace boost::histogram::literals;
23 namespace tr = axis::transform;
24 
main()25 int main() {
26   using namespace boost::units;
27   using Q = quantity<si::length>;
28 
29   // axis::regular with quantity
30   {
31     axis::regular<Q> b(2, 0 * si::meter, 2 * si::meter);
32     BOOST_TEST_EQ(b.bin(-1).lower() / si::meter,
33                   -std::numeric_limits<double>::infinity());
34     BOOST_TEST_IS_CLOSE(b.bin(0).lower() / si::meter, 0.0, 1e-9);
35     BOOST_TEST_IS_CLOSE(b.bin(1).lower() / si::meter, 1.0, 1e-9);
36     BOOST_TEST_IS_CLOSE(b.bin(2).lower() / si::meter, 2.0, 1e-9);
37     BOOST_TEST_EQ(b.bin(2).upper() / si::meter, std::numeric_limits<double>::infinity());
38 
39     BOOST_TEST_EQ(b.index(-std::numeric_limits<double>::infinity() * si::meter), -1);
40     BOOST_TEST_EQ(b.index(-1 * si::meter), -1); // produces NaN in conversion
41     BOOST_TEST_EQ(b.index(0 * si::meter), 0);
42     BOOST_TEST_EQ(b.index(0.99 * si::meter), 0);
43     BOOST_TEST_EQ(b.index(1 * si::meter), 1);
44     BOOST_TEST_EQ(b.index(1.99 * si::meter), 1);
45     BOOST_TEST_EQ(b.index(2 * si::meter), 2);
46     BOOST_TEST_EQ(b.index(100 * si::meter), 2);
47     BOOST_TEST_EQ(b.index(std::numeric_limits<double>::infinity() * si::meter), 2);
48   }
49 
50   // axis::regular with quantity and transform
51   {
52     axis::regular<Q, tr::log> b(2, 1 * si::meter, 10 * si::meter);
53     BOOST_TEST_EQ(b.value(-1) / si::meter, 0);
54     BOOST_TEST_IS_CLOSE(b.value(0) / si::meter, 1.0, 1e-9);
55     BOOST_TEST_IS_CLOSE(b.value(1) / si::meter, 3.1623, 1e-3);
56     BOOST_TEST_IS_CLOSE(b.value(2) / si::meter, 10.0, 1e-9);
57     BOOST_TEST_EQ(b.value(3) / si::meter, std::numeric_limits<double>::infinity());
58   }
59 
60   // histogram with quantity axis
61   {
62     auto h = make_histogram(axis::regular<Q>(2, 0 * si::meter, 1 * si::meter),
63                             axis::regular<>(2, 0, 1));
64     h(0.1 * si::meter, 0.1); // fills bin (0, 0)
65     BOOST_TEST_EQ(h.at(0, 0), 1);
66     for (auto&& x : indexed(h)) {
67       BOOST_TEST_THROWS(x.density(), std::runtime_error); // cannot use density method
68       BOOST_TEST_EQ(x.index(0), 2.0 * x.bin(0_c).lower() / si::meter);
69       BOOST_TEST_EQ(x.index(1), 2.0 * x.bin(1_c).lower());
70     }
71   }
72 
73   return boost::report_errors();
74 }
75