// Copyright 2018 Hans Dembinski // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include #include #include #include "utility_histogram.hpp" using namespace boost::histogram; using namespace boost::histogram::literals; template void run_1d_tests(bool include_extra_bins) { auto h = make(Tag(), axis::integer<>(0, 3)); h(weight(2), 0); h(1); h(1); auto ind = indexed(h, include_extra_bins); auto it = ind.begin(); BOOST_TEST_EQ(it->size(), 1); BOOST_TEST_EQ(it->operator[](0), 0); BOOST_TEST_EQ(it->value, 2); BOOST_TEST_EQ(it->bin(0), h.axis()[0]); ++it; BOOST_TEST_EQ(it->operator[](0), 1); BOOST_TEST_EQ(it->value, 2); BOOST_TEST_EQ(it->bin(0), h.axis()[1]); ++it; BOOST_TEST_EQ(it->operator[](0), 2); BOOST_TEST_EQ(it->value, 0); BOOST_TEST_EQ(it->bin(0), h.axis()[2]); ++it; if (include_extra_bins) { BOOST_TEST_EQ(it->operator[](0), 3); BOOST_TEST_EQ(it->value, 0); BOOST_TEST_EQ(it->bin(0), h.axis()[3]); ++it; BOOST_TEST_EQ(it->operator[](0), -1); BOOST_TEST_EQ(it->value, 0); BOOST_TEST_EQ(it->bin(0), h.axis()[-1]); ++it; } BOOST_TEST(it == ind.end()); } template void run_3d_tests(bool b) { auto h = make_s(Tag(), std::vector(), axis::integer<>(0, 2), axis::integer(0, 3), axis::integer(0, 4)); for (int i = -1; i < 3; ++i) for (int j = -1; j < 4; ++j) for (int k = -1; k < 5; ++k) h(i, j, k, weight(i * 100 + j * 10 + k)); auto ind = indexed(h, b); auto it = ind.begin(); BOOST_TEST_EQ(it->size(), 3); // imitate iteration order of indexed loop for (int k = 0; k < 4 + b; ++k) for (int j = 0; j < 3; ++j) for (int i = 0; i < 2 + 2*b; ++i) { const auto i2 = i > 2 ? -1 : i; BOOST_TEST_EQ(it->operator[](0), i2); BOOST_TEST_EQ(it->operator[](1), j); BOOST_TEST_EQ(it->operator[](2), k); BOOST_TEST_EQ(it->bin(0_c), h.axis(0_c)[i2]); BOOST_TEST_EQ(it->bin(1_c), h.axis(1_c)[j]); BOOST_TEST_EQ(it->bin(2_c), h.axis(2_c)[k]); BOOST_TEST_EQ(it->value, i2 * 100 + j * 10 + k); ++it; } BOOST_TEST(it == ind.end()); } template void run_density_tests(bool include_extra_bins) { auto ax = axis::variable<>({0.0, 0.1, 0.3, 0.6}); auto ay = axis::integer(0, 2); auto az = ax; auto h = make_s(Tag(), std::vector(), ax, ay, az); // fill uniformly for (unsigned i = 0; i < h.size(); ++i) { unsafe_access::storage(h).set(i, 1); } for (auto x : indexed(h, include_extra_bins)) { BOOST_TEST_EQ(x.density(), x.value / (x.bin(0).width() * x.bin(2).width())); } } int main() { for (int b = 0; b < 2; ++b) { run_1d_tests(b); run_1d_tests(b); run_3d_tests(b); run_3d_tests(b); run_density_tests(b); run_density_tests(b); } return boost::report_errors(); }