algorithm_sum_test.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <array>
  7. #include <boost/core/lightweight_test.hpp>
  8. #include <boost/histogram/accumulators/weighted_sum.hpp>
  9. #include <boost/histogram/algorithm/sum.hpp>
  10. #include <boost/histogram/axis/integer.hpp>
  11. #include "throw_exception.hpp"
  12. #include <unordered_map>
  13. #include <vector>
  14. #include "utility_histogram.hpp"
  15. using namespace boost::histogram;
  16. using boost::histogram::algorithm::sum;
  17. template <typename Tag>
  18. void run_tests() {
  19. auto ax = axis::integer<>(0, 100);
  20. auto h1 = make(Tag(), ax);
  21. for (unsigned i = 0; i < 100; ++i) h1(i);
  22. BOOST_TEST_EQ(sum(h1), 100);
  23. auto h2 = make_s(Tag(), std::vector<double>(), ax, ax);
  24. for (unsigned i = 0; i < 100; ++i)
  25. for (unsigned j = 0; j < 100; ++j) h2(i, j);
  26. BOOST_TEST_EQ(sum(h2), 10000);
  27. auto h3 = make_s(Tag(), std::array<int, 102>(), ax);
  28. for (unsigned i = 0; i < 100; ++i) h3(i);
  29. BOOST_TEST_EQ(sum(h3), 100);
  30. auto h4 = make_s(Tag(), std::unordered_map<std::size_t, int>(), ax);
  31. for (unsigned i = 0; i < 100; ++i) h4(i);
  32. BOOST_TEST_EQ(sum(h4), 100);
  33. auto h5 =
  34. make_s(Tag(), std::vector<accumulators::weighted_sum<>>(), axis::integer<>(0, 1),
  35. axis::integer<int, axis::null_type, axis::option::none_t>(2, 4));
  36. h5(weight(2), 0, 2);
  37. h5(-1, 2);
  38. h5(1, 3);
  39. const auto v = algorithm::sum(h5);
  40. BOOST_TEST_EQ(v.value(), 4);
  41. BOOST_TEST_EQ(v.variance(), 6);
  42. }
  43. int main() {
  44. run_tests<static_tag>();
  45. run_tests<dynamic_tag>();
  46. return boost::report_errors();
  47. }