File: axis_category_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 (158 lines) | stat: -rw-r--r-- 4,861 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
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
// 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/category.hpp>
#include <boost/histogram/axis/ostream.hpp>
#include <boost/histogram/axis/traits.hpp>
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>
#include "std_ostream.hpp"
#include "throw_exception.hpp"
#include "utility_axis.hpp"
#include "utility_str.hpp"

int main() {
  using namespace boost::histogram;

  BOOST_TEST(std::is_nothrow_move_constructible<axis::category<int>>::value);
  BOOST_TEST(std::is_nothrow_move_constructible<axis::category<std::string>>::value);
  BOOST_TEST(std::is_nothrow_move_assignable<axis::category<int>>::value);
  BOOST_TEST(std::is_nothrow_move_assignable<axis::category<std::string>>::value);

  // bad ctor
  {
    int x[2];
    (void)x;
    BOOST_TEST_THROWS(axis::category<int>(x + 1, x), std::invalid_argument);
  }

  // value should return copy for arithmetic types and const reference otherwise
  {
    enum class Foo { foo };

    BOOST_TEST_TRAIT_SAME(axis::traits::value_type<axis::category<std::string>>,
                          std::string);
    BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<std::string>>().value(0)),
                          const std::string&);
    BOOST_TEST_TRAIT_SAME(axis::traits::value_type<axis::category<const char*>>,
                          const char*);
    BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<const char*>>().value(0)),
                          const char*);
    BOOST_TEST_TRAIT_SAME(axis::traits::value_type<axis::category<Foo>>, Foo);
    BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<Foo>>().value(0)), Foo);
    BOOST_TEST_TRAIT_SAME(axis::traits::value_type<axis::category<int>>, int);
    BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<int>>().value(0)), int);
  }

  // empty axis::category
  {
    axis::category<int> a;
    axis::category<int> b(std::vector<int>(0));
    BOOST_TEST_EQ(a, b);
    BOOST_TEST_EQ(a.size(), 0);
    BOOST_TEST_EQ(a.index(-1), 0);
    BOOST_TEST_EQ(a.index(0), 0);
    BOOST_TEST_EQ(a.index(1), 0);
  }

  // axis::category
  {
    std::string A("A"), B("B"), C("C"), other;

    axis::category<std::string> a({A, B, C}, "foo");
    BOOST_TEST_EQ(a.metadata(), "foo");
    BOOST_TEST_EQ(static_cast<const axis::category<std::string>&>(a).metadata(), "foo");
    a.metadata() = "bar";
    BOOST_TEST_EQ(static_cast<const axis::category<std::string>&>(a).metadata(), "bar");
    BOOST_TEST_EQ(a.size(), 3);
    BOOST_TEST_EQ(a.index(A), 0);
    BOOST_TEST_EQ(a.index(B), 1);
    BOOST_TEST_EQ(a.index(C), 2);
    BOOST_TEST_EQ(a.index(other), 3);
    BOOST_TEST_EQ(a.value(0), A);
    BOOST_TEST_EQ(a.value(1), B);
    BOOST_TEST_EQ(a.value(2), C);
    BOOST_TEST_THROWS(a.value(3), std::out_of_range);

    BOOST_TEST_CSTR_EQ(
        str(a).c_str(),
        "category(\"A\", \"B\", \"C\", metadata=\"bar\", options=overflow)");
  }

  // category<int, axis::null_type>: copy, move
  {
    using C = axis::category<int, axis::null_type>;
    C a({1, 2, 3});
    C a2(a);
    BOOST_TEST_EQ(a2, a);
    C b;
    BOOST_TEST_NE(a, b);
    b = a;
    BOOST_TEST_EQ(a, b);
    b = C{{2, 1, 3}};
    BOOST_TEST_NE(a, b);
    b = a;
    BOOST_TEST_EQ(a, b);
    C c = std::move(b);
    BOOST_TEST_EQ(c, a);
    C d;
    BOOST_TEST_NE(c, d);
    d = std::move(c);
    BOOST_TEST_EQ(d, a);
  }

  // category<std::string>: copy, move
  {
    using C = axis::category<std::string>;

    C a({"A", "B", "C"}, "foo");
    C a2(a);
    BOOST_TEST_EQ(a2, a);
    C b;
    BOOST_TEST_NE(a, b);
    b = a;
    BOOST_TEST_EQ(a, b);
    b = C{{"B", "A", "C"}};
    BOOST_TEST_NE(a, b);
    b = a;
    BOOST_TEST_EQ(a, b);
    C c = std::move(b);
    BOOST_TEST_EQ(c, a);
    C d;
    BOOST_TEST_NE(c, d);
    d = std::move(c);
    BOOST_TEST_EQ(d, a);
  }

  // axis::category with growth
  {
    using pii_t = std::pair<axis::index_type, axis::index_type>;
    axis::category<int, axis::null_type, axis::option::growth_t> a;
    BOOST_TEST_EQ(a.size(), 0);
    BOOST_TEST_EQ(a.update(5), pii_t(0, -1));
    BOOST_TEST_EQ(a.size(), 1);
    BOOST_TEST_EQ(a.update(1), pii_t(1, -1));
    BOOST_TEST_EQ(a.size(), 2);
    BOOST_TEST_EQ(a.update(10), pii_t(2, -1));
    BOOST_TEST_EQ(a.size(), 3);
    BOOST_TEST_EQ(a.update(10), pii_t(2, 0));
    BOOST_TEST_EQ(a.size(), 3);

    BOOST_TEST_EQ(str(a), "category(5, 1, 10, options=growth)");
  }

  // iterators
  {
    test_axis_iterator(axis::category<>({3, 1, 2}, ""), 0, 3);
    test_axis_iterator(axis::category<std::string>({"A", "B"}, ""), 0, 2);
  }

  return boost::report_errors();
}