diff --git a/include/boost/histogram/indexed.hpp b/include/boost/histogram/indexed.hpp index ccf92345..e3ecaa94 100644 --- a/include/boost/histogram/indexed.hpp +++ b/include/boost/histogram/indexed.hpp @@ -253,6 +253,8 @@ class BOOST_ATTRIBUTE_NODISCARD indexed_range { const auto cbeg = indices_.begin(); auto c = cbeg; ++iter_; + // rank-0 histogram has a single cell and no indices to update + if (c == indices_.end()) return *this; ++c->idx; if (c->idx < c->end) return *this; while (c->idx == c->end) { diff --git a/test/algorithm_empty_test.cpp b/test/algorithm_empty_test.cpp index 9de9a0fc..5d5d4e72 100644 --- a/test/algorithm_empty_test.cpp +++ b/test/algorithm_empty_test.cpp @@ -4,6 +4,7 @@ // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) +#include #include #include #include @@ -61,5 +62,15 @@ int main() { run_tests(); run_tests(); + { + // regression test for https://github.com/boostorg/histogram/issues/430 + auto h = make(dynamic_tag()); + BOOST_TEST(empty(h, coverage::all)); + BOOST_TEST(empty(h, coverage::inner)); + std::fill(h.begin(), h.end(), 1); + BOOST_TEST(!empty(h, coverage::all)); + BOOST_TEST(!empty(h, coverage::inner)); + } + return boost::report_errors(); } diff --git a/test/algorithm_sum_test.cpp b/test/algorithm_sum_test.cpp index 5998012e..794f3db3 100644 --- a/test/algorithm_sum_test.cpp +++ b/test/algorithm_sum_test.cpp @@ -75,5 +75,13 @@ int main() { run_tests(); run_tests(); + { + // regression test for https://github.com/boostorg/histogram/issues/430 + auto h = make(dynamic_tag()); + std::fill(h.begin(), h.end(), 3); + BOOST_TEST_EQ(sum(h), 3); + BOOST_TEST_EQ(sum(h, coverage::inner), 3); + } + return boost::report_errors(); } diff --git a/test/indexed_test.cpp b/test/indexed_test.cpp index 4f183d46..fbdb2068 100644 --- a/test/indexed_test.cpp +++ b/test/indexed_test.cpp @@ -210,6 +210,29 @@ void run_comparison_tests(Tag) { BOOST_TEST(1 >= a); } +// regression test for https://github.com/boostorg/histogram/issues/430 +void run_0d_tests(coverage cov) { + auto h = make(dynamic_tag()); + BOOST_TEST_EQ(h.rank(), 0u); + BOOST_TEST_EQ(h.size(), 1u); + std::fill(h.begin(), h.end(), 2); + + auto ind = indexed(h, cov); + auto it = ind.begin(); + BOOST_TEST(it != ind.end()); + BOOST_TEST_EQ(it->indices().size(), 0u); + BOOST_TEST_EQ(**it, 2); + ++it; + BOOST_TEST(it == ind.end()); + + int count = 0; + for (auto&& x : indexed(h, cov)) { + BOOST_TEST_EQ(*x, 2); + ++count; + } + BOOST_TEST_EQ(count, 1); +} + template void run_indexed_with_range_tests(Tag) { { @@ -258,6 +281,9 @@ int main() { run_comparison_tests(static_tag{}); run_comparison_tests(dynamic_tag{}); + run_0d_tests(coverage::inner); + run_0d_tests(coverage::all); + run_indexed_with_range_tests(static_tag{}); run_indexed_with_range_tests(dynamic_tag{}); return boost::report_errors();