File: histogram_dynamic_test.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (122 lines) | stat: -rw-r--r-- 3,679 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2015-2017 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 <algorithm>
#include <boost/core/lightweight_test.hpp>
#include <boost/histogram/axis.hpp>
#include <boost/histogram/axis/ostream.hpp>
#include <boost/histogram/histogram.hpp>
#include <boost/histogram/make_histogram.hpp>
#include <boost/histogram/ostream.hpp>
#include <cstdlib>
#include <limits>
#include <numeric>
#include <sstream>
#include <tuple>
#include <utility>
#include <vector>
#include "throw_exception.hpp"
#include "utility_histogram.hpp"

using namespace boost::histogram;

int main() {
  // special stuff that only works with dynamic_tag

  // init with vector of axis and vector of axis::variant
  {
    using R = axis::regular<>;
    using I = axis::integer<>;
    using V = axis::variable<>;

    auto v = std::vector<axis::variant<R, I, V>>();
    v.emplace_back(R{4, -1, 1});
    v.emplace_back(I{1, 7});
    v.emplace_back(V{1, 2, 3});
    auto h = make_histogram(v.begin(), v.end());
    BOOST_TEST_EQ(h.rank(), 3);
    BOOST_TEST_EQ(h.axis(0), v[0]);
    BOOST_TEST_EQ(h.axis(1), v[1]);
    BOOST_TEST_EQ(h.axis(2), v[2]);

    auto h2 = make_histogram_with(std::vector<int>(), v);
    BOOST_TEST_EQ(h2.rank(), 3);
    BOOST_TEST_EQ(h2.axis(0), v[0]);
    BOOST_TEST_EQ(h2.axis(1), v[1]);
    BOOST_TEST_EQ(h2.axis(2), v[2]);

    auto v2 = std::vector<R>();
    v2.emplace_back(10, 0, 1);
    v2.emplace_back(20, 0, 2);
    auto h3 = make_histogram(v2);
    BOOST_TEST_EQ(h3.axis(0), v2[0]);
    BOOST_TEST_EQ(h3.axis(1), v2[1]);
  }

  // too many axes
  {
    using I = axis::integer<int, axis::null_type, axis::option::none_t>;

    // test edge case
    auto av = std::vector<I>(BOOST_HISTOGRAM_DETAIL_AXES_LIMIT, I(0, 1));
    auto h = make_histogram(av);
    auto inputs = std::vector<std::vector<int>>(BOOST_HISTOGRAM_DETAIL_AXES_LIMIT,
                                                std::vector<int>(1, 0));
    h.fill(inputs); // should not crash

    auto bad = std::vector<I>(BOOST_HISTOGRAM_DETAIL_AXES_LIMIT + 1, I(0, 1));
    (void)bad;
    BOOST_TEST_THROWS((void)make_histogram(bad), std::invalid_argument);
  }

  // bad fill
  {
    auto a = axis::integer<>(0, 1);
    auto b = make(dynamic_tag(), a);
    BOOST_TEST_THROWS(b(0, 0), std::invalid_argument);
    auto c = make(dynamic_tag(), a, a);
    BOOST_TEST_THROWS(c(0), std::invalid_argument);
    auto d = make(dynamic_tag(), a);
    BOOST_TEST_THROWS(d(std::string()), std::invalid_argument);

    struct axis2d {
      auto index(const std::tuple<double, double>& x) const {
        return axis::index_type{std::get<0>(x) == 1 && std::get<1>(x) == 2};
      }
      auto size() const { return axis::index_type{2}; }
    } e;

    auto f = make(dynamic_tag(), a, e);
    BOOST_TEST_THROWS(f(0, 0, 0), std::invalid_argument);
    BOOST_TEST_THROWS(f(0, std::make_tuple(0, 0), 1), std::invalid_argument);
  }

  // bad at
  {
    auto h1 = make(dynamic_tag(), axis::integer<>(0, 2));
    h1(1);
    BOOST_TEST_THROWS(h1.at(0, 0), std::invalid_argument);
    BOOST_TEST_THROWS(h1.at(std::make_tuple(0, 0)), std::invalid_argument);
  }

  // incompatible axis variant methods
  {
    auto c = make(dynamic_tag(), axis::category<std::string>({"A", "B"}));
    BOOST_TEST_THROWS(c.axis().value(0), std::runtime_error);
  }

  {
    auto h = make_histogram(std::vector<axis::integer<>>(1, axis::integer<>(0, 3)));
    h(0);
    h(1);
    h(2);
    BOOST_TEST_EQ(h.at(0), 1);
    BOOST_TEST_EQ(h.at(1), 1);
    BOOST_TEST_EQ(h.at(2), 1);
  }

  return boost::report_errors();
}