From 460b4173380437496ecf579a980001d5c24501e2 Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Sat, 10 Mar 2018 22:51:08 +0100 Subject: [PATCH] slightly more coverage --- include/boost/histogram/axis/axis.hpp | 5 ++- src/python/axis.cpp | 8 ++-- test/adaptive_storage_test.cpp | 11 +++-- test/axis_test.cpp | 23 ++++++---- test/weight_counter_test.cpp | 65 +++++++++++++++------------ 5 files changed, 66 insertions(+), 46 deletions(-) diff --git a/include/boost/histogram/axis/axis.hpp b/include/boost/histogram/axis/axis.hpp index 14d92dcd..f8873930 100644 --- a/include/boost/histogram/axis/axis.hpp +++ b/include/boost/histogram/axis/axis.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -501,7 +502,7 @@ public: * * \param seq sequence of unique values. */ - category(std::initializer_list seq, string_view label = {}) + category(std::initializer_list seq, string_view label = {}) : base_type(seq.size(), label), map_(new map_type()) { int index = 0; for (const auto &x : seq) @@ -510,7 +511,7 @@ public: throw std::logic_error("sequence is empty"); } - template + template > category(Iterator begin, Iterator end, string_view label = {}) : base_type(std::distance(begin, end), label), map_(new map_type()) { int index = 0; diff --git a/src/python/axis.cpp b/src/python/axis.cpp index 94f33a7c..450727a7 100644 --- a/src/python/axis.cpp +++ b/src/python/axis.cpp @@ -91,10 +91,10 @@ bp::object variable_init(bp::tuple args, bp::dict kwargs) { auto uo = bha::uoflow::on; while (len(kwargs) > 0) { bp::tuple kv = kwargs.popitem(); - boost::string_view k = bp::extract(kv[0])(); + boost::string_view k(bp::extract(kv[0]), bp::len(kv[0])); bp::object v = kv[1]; if (k == "label") - label = bp::extract(v)(); + label = boost::string_view(bp::extract(v), bp::len(v)); else if (k == "uoflow") { if (!bp::extract(v)) uo = bha::uoflow::off; @@ -122,10 +122,10 @@ bp::object category_init(bp::tuple args, bp::dict kwargs) { boost::string_view label; while (bp::len(kwargs) > 0) { bp::tuple kv = kwargs.popitem(); - boost::string_view k = bp::extract(kv[0])(); + boost::string_view k(bp::extract(kv[0]), bp::len(kv[0])); bp::object v = kv[1]; if (k == "label") - label = bp::extract(v)(); + label = boost::string_view(bp::extract(v), bp::len(v)); else { std::stringstream s; s << "keyword " << k << " not recognized"; diff --git a/test/adaptive_storage_test.cpp b/test/adaptive_storage_test.cpp index c3d1010a..4fa94154 100644 --- a/test/adaptive_storage_test.cpp +++ b/test/adaptive_storage_test.cpp @@ -349,10 +349,12 @@ int main() { // add_and_grow { adaptive_storage a(std::size_t(1)); + a += a; + BOOST_TEST_EQ(a[0].value(), 0); a.increase(0); - double x = 1.0; + double x = 1; adaptive_storage y(std::size_t(1)); - BOOST_TEST_EQ(y[0].value(), 0.0); + BOOST_TEST_EQ(y[0].value(), 0); a.add(0, y[0].value()); BOOST_TEST_EQ(a[0].value(), x); for (unsigned i = 0; i < 80; ++i) { @@ -364,7 +366,7 @@ int main() { BOOST_TEST_EQ(a[0].variance(), x); BOOST_TEST_EQ(b[0].value(), x); BOOST_TEST_EQ(b[0].variance(), x); - b.add(0, weight(0.0)); + b.add(0, weight(0)); BOOST_TEST_EQ(b[0].value(), x); BOOST_TEST_EQ(b[0].variance(), x); adaptive_storage c(std::size_t(1)); @@ -377,6 +379,9 @@ int main() { // multiply { adaptive_storage a(std::size_t(2)); + a *= 2; + BOOST_TEST_EQ(a[0].value(), 0); + BOOST_TEST_EQ(a[1].value(), 0); a.increase(0); a *= 3; BOOST_TEST_EQ(a[0].value(), 3); diff --git a/test/axis_test.cpp b/test/axis_test.cpp index c7b4c6f0..fc9fc451 100644 --- a/test/axis_test.cpp +++ b/test/axis_test.cpp @@ -265,28 +265,35 @@ int main() { // any_axis_type_streamable { enum { A, B, C }; + std::string a = "A"; + std::string b = "B"; std::vector axes; axes.push_back(axis::regular<>{2, -1, 1, "regular1"}); axes.push_back(axis::regular{ 2, 1, 10, "regular2", axis::uoflow::off}); axes.push_back(axis::regular{ 2, 1, 10, "regular3", axis::uoflow::on, 0.5}); + axes.push_back(axis::regular{ + 2, 1, 10, "regular4", axis::uoflow::off, -0.5}); axes.push_back(axis::circular<>{4, 0.1, 1.0, "polar"}); axes.push_back(axis::variable<>{{-1, 0, 1}, "variable", axis::uoflow::off}); axes.push_back(axis::category<>{{A, B, C}, "category"}); + axes.push_back(axis::category{{a, b}, "category2"}); axes.push_back(axis::integer<>{-1, 1, "integer", axis::uoflow::off}); std::ostringstream os; for (const auto &a : axes) { - os << a; + os << a << "\n"; } const std::string ref = - "regular(2, -1, 1, label='regular1')" - "regular_log(2, 1, 10, label='regular2', uoflow=False)" - "regular_pow(2, 1, 10, 0.5, label='regular3')" - "circular(4, phase=0.1, perimeter=1, label='polar')" - "variable(-1, 0, 1, label='variable', uoflow=False)" - "category(0, 1, 2, label='category')" - "integer(-1, 1, label='integer', uoflow=False)"; + "regular(2, -1, 1, label='regular1')\n" + "regular_log(2, 1, 10, label='regular2', uoflow=False)\n" + "regular_pow(2, 1, 10, 0.5, label='regular3')\n" + "regular_pow(2, 1, 10, -0.5, label='regular4', uoflow=False)\n" + "circular(4, phase=0.1, perimeter=1, label='polar')\n" + "variable(-1, 0, 1, label='variable', uoflow=False)\n" + "category(0, 1, 2, label='category')\n" + "category('A', 'B', label='category2')\n" + "integer(-1, 1, label='integer', uoflow=False)\n"; BOOST_TEST_EQ(os.str(), ref); } diff --git a/test/weight_counter_test.cpp b/test/weight_counter_test.cpp index f66560bc..bb3b02cc 100644 --- a/test/weight_counter_test.cpp +++ b/test/weight_counter_test.cpp @@ -7,47 +7,54 @@ #include #include #include +#include + +using namespace boost::histogram; +using wcount = weight_counter; -namespace boost { -namespace histogram { template std::ostream &operator<<(std::ostream &os, const weight_counter &w) { - os << "[ " << w.value() << ", " << w.variance() << "]"; + os << "[" << w.value() << ", " << w.variance() << "]"; return os; } -} // namespace histogram -} // namespace boost int main() { - using weight_counter = boost::histogram::weight_counter; - using boost::histogram::weight; + { + wcount w(1); + std::ostringstream os; + os << w; + BOOST_TEST_EQ(os.str(), std::string("[1, 1]")); - weight_counter w(1); - BOOST_TEST_EQ(w, weight_counter(1)); - BOOST_TEST_NE(w, weight_counter(0)); - BOOST_TEST_EQ(1, w); - BOOST_TEST_EQ(w, 1); - BOOST_TEST_NE(2, w); - BOOST_TEST_NE(w, 2); + BOOST_TEST_EQ(w, wcount(1)); + BOOST_TEST_NE(w, wcount(0)); + BOOST_TEST_EQ(1, w); + BOOST_TEST_EQ(w, 1); + BOOST_TEST_NE(2, w); + BOOST_TEST_NE(w, 2); - w += weight(2); - BOOST_TEST_EQ(w.value(), 3); - BOOST_TEST_EQ(w.variance(), 5); + w += weight(2); + BOOST_TEST_EQ(w.value(), 3); + BOOST_TEST_EQ(w.variance(), 5); + } - // consistency: a weighted counter increased by weight 1 multiplied - // by 2 must be the same as a weighted counter increased by weight 2 - weight_counter u(0); - u += weight(1); - u *= 2; - BOOST_TEST_EQ(u, weight_counter(2, 4)); + { + // consistency: a weighted counter increased by weight 1 multiplied + // by 2 must be the same as a weighted counter increased by weight 2 + wcount u(0); + u += weight(1); + u *= 2; + BOOST_TEST_EQ(u, wcount(2, 4)); - weight_counter v(0); - v += weight(2); - BOOST_TEST_EQ(u, v); + wcount v(0); + v += weight(2); + BOOST_TEST_EQ(u, v); + } - weight_counter x(0); - x += 2; - BOOST_TEST_EQ(x, weight_counter(2, 2)); + { + wcount x(0); + x += 2; + BOOST_TEST_EQ(x, wcount(2, 2)); + } return boost::report_errors(); }