File: axis_variant_test.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (251 lines) | stat: -rw-r--r-- 8,605 bytes parent folder | download | duplicates (2)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2015-2018 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 <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/histogram/axis.hpp>
#include <boost/histogram/axis/ostream.hpp>
#include <boost/histogram/detail/type_name.hpp>
#include <string>
#include <type_traits>
#include <vector>
#include "allocator.hpp"
#include "axis.hpp"
#include "ostream.hpp"
#include "str.hpp"
#include "throw_exception.hpp"

int main() {
  using namespace boost::histogram;
  namespace tr = axis::transform;

  {
    (void)axis::variant<>{};
  }

  {
    using meta_type = std::vector<int>;
    using variant_type =
        axis::variant<axis::integer<double>, axis::category<std::string, meta_type>>;
    auto a = variant_type{axis::integer<double>(0, 2, "foo")};
    BOOST_TEST_EQ(a.index(-10), -1);
    BOOST_TEST_EQ(a.index(-1), -1);
    BOOST_TEST_EQ(a.index(0), 0);
    BOOST_TEST_EQ(a.index(0.5), 0);
    BOOST_TEST_EQ(a.index(1), 1);
    BOOST_TEST_EQ(a.index(2), 2);
    BOOST_TEST_EQ(a.index(10), 2);
    BOOST_TEST_EQ(a.bin(-1).lower(), -std::numeric_limits<double>::infinity());
    BOOST_TEST_EQ(a.bin(a.size()).upper(), std::numeric_limits<double>::infinity());
    BOOST_TEST_EQ(a.bin(-10).lower(), -std::numeric_limits<double>::infinity());
    BOOST_TEST_EQ(a.bin(a.size() + 10).upper(), std::numeric_limits<double>::infinity());
    BOOST_TEST_EQ(a.metadata(), "foo");
    a.metadata() = "bar";
    BOOST_TEST_EQ(static_cast<const variant_type&>(a).metadata(), "bar");
    BOOST_TEST_EQ(a.options(), axis::option::underflow | axis::option::overflow);

    a = axis::category<std::string, meta_type>({"A", "B"}, {1, 2, 3});
    BOOST_TEST_EQ(a.index("A"), 0);
    BOOST_TEST_EQ(a.index("B"), 1);
    BOOST_TEST_THROWS(a.metadata(), std::runtime_error);
    BOOST_TEST_THROWS(static_cast<const variant_type&>(a).metadata(), std::runtime_error);
    BOOST_TEST_EQ(a.options(), axis::option::overflow_t::value);
  }

  // axis::variant with pointers
  {
    using A = axis::integer<>;
    using B = axis::regular<>;
    auto a = A(1, 5, "foo");
    auto b = B(3, 1, 5, "bar");
    axis::variant<A*, B*> r1(&a);
    BOOST_TEST_EQ(r1, a);
    BOOST_TEST_NE(r1, A(2, 4));
    BOOST_TEST_NE(r1, b);
    BOOST_TEST_EQ(r1.size(), 4);
    BOOST_TEST_EQ(r1.value(0), 1);
    BOOST_TEST_EQ(r1.metadata(), a.metadata());
    BOOST_TEST_EQ(r1.options(), a.options());
    // change original through r1
    r1.metadata() = "bar";
    BOOST_TEST_EQ(a.metadata(), "bar");
    r1 = &b;
    BOOST_TEST_EQ(r1, b);

    axis::variant<const A*, const B*> r2(static_cast<const B*>(&b));
    BOOST_TEST_EQ(r2, b);
    BOOST_TEST_NE(r2, B(4, 1, 5));
    BOOST_TEST_NE(r2, a);
    BOOST_TEST_EQ(r2.size(), 3);
    BOOST_TEST_EQ(r2.value(0), 1);
    BOOST_TEST_EQ(r2.metadata(), "bar");
    r2.metadata() = "baz"; // change original through r2
    BOOST_TEST_EQ(b.metadata(), "baz");
  }

  // axis::variant copyable
  {
    axis::variant<axis::regular<>> a1(axis::regular<>(2, -1, 1));
    axis::variant<axis::regular<>> a2(a1);
    BOOST_TEST_EQ(a1, a2);
    axis::variant<axis::regular<>> a3;
    BOOST_TEST_NE(a3, a1);
    a3 = a1;
    BOOST_TEST_EQ(a3, a1);
    axis::variant<axis::regular<>> a4(axis::regular<>(3, -2, 2));
    axis::variant<axis::regular<>, axis::integer<>> a5(a4);
    BOOST_TEST_EQ(a4, a5);
    axis::variant<axis::regular<>> a6;
    a6 = a1;
    BOOST_TEST_EQ(a6, a1);
    axis::variant<axis::regular<>, axis::integer<>> a7(axis::integer<>(0, 2));
    BOOST_TEST_THROWS(axis::variant<axis::regular<>> a8(a7), std::runtime_error);
    BOOST_TEST_THROWS(a4 = a7, std::runtime_error);
  }

  // axis::variant movable
  {
    axis::variant<axis::regular<>> a(axis::regular<>(2, -1, 1));
    axis::variant<axis::regular<>> r(a);
    axis::variant<axis::regular<>> b(std::move(a));
    BOOST_TEST_EQ(b, r);
    axis::variant<axis::regular<>> c;
    BOOST_TEST_NE(c, b);
    c = std::move(b);
    BOOST_TEST(c == r);
  }

  // axis::variant streamable
  {
    auto test = [](auto&& a, const char* ref) {
      using T = std::decay_t<decltype(a)>;
      axis::variant<T> axis(std::move(a));
      BOOST_TEST_CSTR_EQ(str(axis).c_str(), ref);
    };

    test(axis::regular<>{2, -1, 1, "foo"},
         "regular(2, -1, 1, metadata=\"foo\", options=underflow | overflow)");

    test(axis::boolean<>{"bar"}, "boolean(metadata=\"bar\")");

    struct user_defined {};
    const auto ref = "integer(-1, 1, metadata=" + detail::type_name<user_defined>() +
                     ", options=none)";
    test(axis::integer<int, user_defined, axis::option::none_t>(-1, 1), ref.c_str());
  }

  // bin_type operator<<
  {
    auto test = [](auto&& a, const char* ref) {
      using T = std::decay_t<decltype(a)>;
      axis::variant<T> axis(std::move(a));
      BOOST_TEST_CSTR_EQ(str(axis.bin(0)).c_str(), ref);
    };

    test(axis::regular<>(2, 1, 2), "[1, 1.5)");
    test(axis::category<>({1, 2}), "1");
  }

  // axis::variant operator==
  {
    enum { A, B, C };
    using variant =
        axis::variant<axis::regular<>, axis::regular<double, axis::transform::pow>,
                      axis::category<>, axis::integer<>, axis::boolean<>>;
    std::vector<variant> axes;
    axes.push_back(axis::regular<>{2, -1, 1});
    axes.push_back(axis::regular<double, tr::pow>{tr::pow{0.5}, 2, 1, 4});
    axes.push_back(axis::category<>{A, B, C});
    axes.push_back(axis::integer<>{-1, 1});
    axes.push_back(axis::boolean<>{});
    for (const auto& a : axes) {
      BOOST_TEST_NE(a, variant{});
      BOOST_TEST_EQ(a, variant(a));
    }
    BOOST_TEST_NE(axes, std::vector<variant>{});
    BOOST_TEST(axes == std::vector<variant>(axes));
  }

  // axis::variant with axis that has incompatible bin type
  {
    auto a = axis::variant<axis::category<std::string>>{
        axis::category<std::string>{"A", "B", "C"}};
    BOOST_TEST_THROWS(a.bin(0), std::runtime_error);
    auto b = axis::variant<axis::category<int>>{axis::category<int>{2, 1, 3}};
    BOOST_TEST_EQ(b.bin(0), 2);
    BOOST_TEST_EQ(b.bin(0).lower(),
                  b.bin(0).upper()); // lower == upper for bin without interval
  }

  // axis::variant support for user-defined axis types
  {
    struct minimal_axis {
      int index(int x) const { return x % 2; }
      int size() const { return 2; }
    };

    axis::variant<minimal_axis, axis::category<std::string>> axis;
    BOOST_TEST_EQ(axis.index(0), 0);
    BOOST_TEST_EQ(axis.index(9), 1);
    BOOST_TEST_EQ(axis.size(), 2);
    BOOST_TEST_EQ(axis.metadata(), axis::null_type{});
    BOOST_TEST_EQ(str(axis), detail::type_name<minimal_axis>());
    BOOST_TEST_THROWS(axis.value(0), std::runtime_error);

    axis = axis::category<std::string>({"A", "B"}, "category");
    BOOST_TEST_EQ(axis.index("B"), 1);
    BOOST_TEST_THROWS(axis.value(0), std::runtime_error);
  }

  // vector of axes with custom allocators
  {
    using M = std::vector<char, tracing_allocator<char>>;
    using T1 = axis::regular<double, tr::id, M>;
    using T2 = axis::integer<int, axis::null_type>;
    using T3 = axis::category<long, axis::null_type, axis::option::overflow_t,
                              tracing_allocator<long>>;
    using axis_type = axis::variant<T1, T2, T3>; // no heap allocation
    using axes_type = std::vector<axis_type, tracing_allocator<axis_type>>;

    tracing_allocator_db db;
    {
      auto a = tracing_allocator<char>(db);
      axes_type axes(a);
      axes.reserve(3);
      axes.emplace_back(T1(1, 0, 1, M(3, 'c', a)));
      axes.emplace_back(T2(0, 4));
      axes.emplace_back(T3({1, 2, 3, 4, 5}, {}, a));
    }
    // 3 axis::variant objects
    BOOST_TEST_EQ(db.at<axis_type>().first, 0);
    BOOST_TEST_EQ(db.at<axis_type>().second, 3);

    // label of T1
    BOOST_TEST_EQ(db.at<char>().first, 0);
    BOOST_TEST_EQ(db.at<char>().second, 3);

    // T3 allocates storage for long array
    BOOST_TEST_EQ(db.at<long>().first, 0);
    BOOST_TEST_EQ(db.at<long>().second, 5);
  }

  // testing pass-through versions of get
  {
    axis::regular<> a(10, 0, 1);
    axis::integer<> b(0, 3);
    const auto& ta = axis::get<axis::regular<>>(a);
    BOOST_TEST_EQ(ta, a);
    const auto* tb = axis::get_if<axis::integer<>>(&b);
    BOOST_TEST_EQ(tb, &b);
    const auto* tc = axis::get_if<axis::regular<>>(&b);
    BOOST_TEST_EQ(tc, nullptr);
  }

  // iterators
  test_axis_iterator(axis::variant<axis::regular<>>(axis::regular<>(5, 0, 1)), 0, 5);

  return boost::report_errors();
}