From 4e6004af06f5bd5e004c7cb4dedb2fec6df6515b Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Wed, 22 Aug 2018 12:43:00 +0200 Subject: [PATCH] fixing a few tests --- include/boost/histogram/axis/any.hpp | 8 ++++++++ include/boost/histogram/histogram.hpp | 12 ++++++------ test/axis_test.cpp | 6 ++++-- test/histogram_dynamic_test.cpp | 6 ++++-- test/histogram_test.cpp | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/include/boost/histogram/axis/any.hpp b/include/boost/histogram/axis/any.hpp index 8fdc5b6e..41352e10 100644 --- a/include/boost/histogram/axis/any.hpp +++ b/include/boost/histogram/axis/any.hpp @@ -137,6 +137,14 @@ struct set_label_visitor : public boost::static_visitor { } }; +template +struct get_allocator_visitor : public boost::static_visitor { + template + U operator()(const T& t) const { + return t.get_allocator(); + } +}; + } // namespace detail /// Polymorphic axis type diff --git a/include/boost/histogram/histogram.hpp b/include/boost/histogram/histogram.hpp index 150f1b43..865ae7d0 100644 --- a/include/boost/histogram/histogram.hpp +++ b/include/boost/histogram/histogram.hpp @@ -310,12 +310,12 @@ histogram, detail:: make_dynamic_histogram_with(Storage&& s, Iterator begin, Iterator end) { using H = histogram, detail::rm_cv_ref>; using alloc_type = typename mp11::mp_front::allocator_type; - // auto axes = typename H::axes_type(static_cast&>(*begin).get_allocator()); - // axes.reserve(std::distance(begin, end)); - // while (begin != end) - // axes.emplace_back(*begin++); - // return H(std::move(axes), std::forward(s)); - return H(); + auto a = boost::apply_visitor(axis::detail::get_allocator_visitor(), *begin); + auto axes = typename H::axes_type(a); + axes.reserve(std::distance(begin, end)); + while (begin != end) + axes.emplace_back(*begin++); + return H(std::move(axes), std::forward(s)); } /// dynamic type factory with standard storage type diff --git a/test/axis_test.cpp b/test/axis_test.cpp index 69f2d2ef..a2712a1c 100644 --- a/test/axis_test.cpp +++ b/test/axis_test.cpp @@ -441,14 +441,14 @@ int main() { BOOST_TEST_EQ(detail::make_sub_axes(axes, i0(), i1(), i2()), axes); } - // dynamic_axes with allocator + // dynamic_axes with custom allocators { using T1 = axis::regular>; using T2 = axis::circular>; using T3 = axis::variable>; using T4 = axis::integer>; using T5 = axis::category>; - using axis_type = axis::any; + using axis_type = axis::any; // no heap allocation using axes_type = boost::mp11::mp_rename; using expected = tracing_allocator; BOOST_TEST_TRAIT_TRUE((std::is_same)); @@ -482,6 +482,8 @@ int main() { // T5 allocates storage for bimap BOOST_TEST_EQ(db[typeid(boost::bimap)].first, db[typeid(boost::bimap)].second); BOOST_TEST_EQ(db[typeid(boost::bimap)].first, 1); + + BOOST_TEST_EQ(db.size(), 4); // axis_type, char, double, bimap } return boost::report_errors(); diff --git a/test/histogram_dynamic_test.cpp b/test/histogram_dynamic_test.cpp index 38e91155..ddcbfd8b 100644 --- a/test/histogram_dynamic_test.cpp +++ b/test/histogram_dynamic_test.cpp @@ -34,12 +34,14 @@ int main() { v.push_back(axis::regular<>(4, -1, 1)); v.push_back(axis::integer<>(1, 7)); auto h = make_dynamic_histogram(v.begin(), v.end()); + BOOST_TEST_EQ(h.dim(), 2); BOOST_TEST_EQ(h.axis(0), v[0]); BOOST_TEST_EQ(h.axis(1), v[1]); auto h2 = make_dynamic_histogram_with(array_storage(), v.begin(), v.end()); - BOOST_TEST_EQ(h.axis(0), v[0]); - BOOST_TEST_EQ(h.axis(1), v[1]); + BOOST_TEST_EQ(h2.dim(), 2); + BOOST_TEST_EQ(h2.axis(0), v[0]); + BOOST_TEST_EQ(h2.axis(1), v[1]); } // bad fill argument diff --git a/test/histogram_test.cpp b/test/histogram_test.cpp index 7e2aed13..a4e9bcd2 100644 --- a/test/histogram_test.cpp +++ b/test/histogram_test.cpp @@ -848,7 +848,7 @@ void run_tests() { if (Tag()) { // axis::any allocation, only for dynamic histogram using T = axis::any>>; BOOST_TEST_EQ(db[typeid(T)].first, db[typeid(T)].second); - BOOST_TEST_EQ(db[typeid(T)].first, 1); + BOOST_TEST_GE(db[typeid(T)].first, 1); } } }