File: test_constructor.cc

package info (click to toggle)
libcuckoo 0.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,036 kB
  • sloc: cpp: 16,538; ansic: 72; python: 34; sh: 15; makefile: 9
file content (275 lines) | stat: -rw-r--r-- 8,766 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <catch.hpp>

#include <array>
#include <cmath>
#include <stdexcept>

#include "unit_test_util.hh"
#include <libcuckoo/cuckoohash_map.hh>

TEST_CASE("default size", "[constructor]") {
  IntIntTable tbl;
  REQUIRE(tbl.size() == 0);
  REQUIRE(tbl.empty());
  if (libcuckoo::DEFAULT_SIZE < 4) {
    REQUIRE(tbl.hashpower() == 0);
  } else {
    REQUIRE(tbl.hashpower() == (size_t)log2(libcuckoo::DEFAULT_SIZE / 4));
  }
  REQUIRE(tbl.bucket_count() == 1UL << tbl.hashpower());
  REQUIRE(tbl.load_factor() == 0);
}

TEST_CASE("given size", "[constructor]") {
  IntIntTable tbl(1);
  REQUIRE(tbl.size() == 0);
  REQUIRE(tbl.empty());
  REQUIRE(tbl.hashpower() == 0);
  REQUIRE(tbl.bucket_count() == 1);
  REQUIRE(tbl.load_factor() == 0);
}

TEST_CASE("frees even with exceptions", "[constructor]") {
  typedef IntIntTableWithAlloc<TrackingAllocator<int, 0>> no_space_table;
  // Should throw when allocating anything
  REQUIRE_THROWS_AS(no_space_table(1), std::bad_alloc);
  REQUIRE(get_unfreed_bytes() == 0);

  typedef IntIntTableWithAlloc<
      TrackingAllocator<int, libcuckoo::UnitTestInternalAccess::IntIntBucketSize * 2>>
      some_space_table;
  // Should throw when allocating things after the bucket
  REQUIRE_THROWS_AS(some_space_table(1), std::bad_alloc);
  REQUIRE(get_unfreed_bytes() == 0);
}

struct StatefulHash {
  StatefulHash(int state_) : state(state_) {}
  size_t operator()(int x) const { return x; }
  int state;
};

struct StatefulKeyEqual {
  StatefulKeyEqual(int state_) : state(state_) {}
  bool operator()(int x, int y) const { return x == y; }
  int state;
};

template <typename T> struct StatefulAllocator {
  using value_type = T;
  using pointer = T *;
  using const_pointer = const T *;
  using reference = T &;
  using const_reference = const T &;
  using size_type = size_t;
  using difference_type = ptrdiff_t;

  template <typename U> struct rebind { using other = StatefulAllocator<U>; };

  StatefulAllocator() : state(0) {}

  StatefulAllocator(int state_) : state(state_) {}

  template <typename U>
  StatefulAllocator(const StatefulAllocator<U> &other) : state(other.state) {}

  T *allocate(size_t n) { return std::allocator<T>().allocate(n); }

  void deallocate(T *p, size_t n) { std::allocator<T>().deallocate(p, n); }

  template <typename U, class... Args> void construct(U *p, Args &&... args) {
    new ((void *)p) U(std::forward<Args>(args)...);
  }

  template <typename U> void destroy(U *p) { p->~U(); }

  StatefulAllocator select_on_container_copy_construction() const {
    return StatefulAllocator();
  }

  using propagate_on_container_swap = std::integral_constant<bool, true>;

  int state;
};

template <typename T, typename U>
bool operator==(const StatefulAllocator<T> &a1,
                const StatefulAllocator<U> &a2) {
  return a1.state == a2.state;
}

template <typename T, typename U>
bool operator!=(const StatefulAllocator<T> &a1,
                const StatefulAllocator<U> &a2) {
  return a1.state != a2.state;
}

using alloc_t = StatefulAllocator<std::pair<const int, int>>;
using tbl_t =
    libcuckoo::cuckoohash_map<int, int, StatefulHash, StatefulKeyEqual, alloc_t, 4>;

TEST_CASE("stateful components", "[constructor]") {
  tbl_t map(8, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  REQUIRE(map.hash_function().state == 10);
  for (int i = 0; i < 100; ++i) {
    REQUIRE(map.hash_function()(i) == i);
  }
  REQUIRE(map.key_eq().state == 20);
  for (int i = 0; i < 100; ++i) {
    REQUIRE(map.key_eq()(i, i));
    REQUIRE_FALSE(map.key_eq()(i, i + 1));
  }
  REQUIRE(map.get_allocator().state == 30);
}

TEST_CASE("range constructor", "[constructor]") {
  std::array<typename alloc_t::value_type, 3> elems{{{1, 2}, {3, 4}, {5, 6}}};
  tbl_t map(elems.begin(), elems.end(), 3, StatefulHash(10),
            StatefulKeyEqual(20), alloc_t(30));
  REQUIRE(map.hash_function().state == 10);
  REQUIRE(map.key_eq().state == 20);
  REQUIRE(map.get_allocator().state == 30);
  for (int i = 1; i <= 5; i += 2) {
    REQUIRE(map.find(i) == i + 1);
  }
}

TEST_CASE("copy constructor", "[constructor]") {
  tbl_t map(0, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  REQUIRE(map.get_allocator().state == 30);
  tbl_t map2(map);
  REQUIRE(map2.hash_function().state == 10);
  REQUIRE(map2.key_eq().state == 20);
  REQUIRE(map2.get_allocator().state == 0);
}

TEST_CASE("copy constructor other allocator", "[constructor]") {
  tbl_t map(0, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  tbl_t map2(map, map.get_allocator());
  REQUIRE(map2.hash_function().state == 10);
  REQUIRE(map2.key_eq().state == 20);
  REQUIRE(map2.get_allocator().state == 30);
}

TEST_CASE("move constructor", "[constructor]") {
  tbl_t map(10, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  map.insert(10, 10);
  tbl_t map2(std::move(map));
  REQUIRE(map.size() == 0);
  REQUIRE(map2.size() == 1);
  REQUIRE(map2.hash_function().state == 10);
  REQUIRE(map2.key_eq().state == 20);
  REQUIRE(map2.get_allocator().state == 30);
}

TEST_CASE("move constructor different allocator", "[constructor]") {
  tbl_t map(10, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  map.insert(10, 10);
  tbl_t map2(std::move(map), alloc_t(40));
  REQUIRE(map.size() == 1);
  REQUIRE(map.hash_function().state == 10);
  REQUIRE(map.key_eq().state == 20);
  REQUIRE(map.get_allocator().state == 30);

  REQUIRE(map2.size() == 1);
  REQUIRE(map2.hash_function().state == 10);
  REQUIRE(map2.key_eq().state == 20);
  REQUIRE(map2.get_allocator().state == 40);
}

TEST_CASE("initializer list constructor", "[constructor]") {
  tbl_t map({{1, 2}, {3, 4}, {5, 6}}, 3, StatefulHash(10), StatefulKeyEqual(20),
            alloc_t(30));
  REQUIRE(map.hash_function().state == 10);
  REQUIRE(map.key_eq().state == 20);
  REQUIRE(map.get_allocator().state == 30);
  for (int i = 1; i <= 5; i += 2) {
    REQUIRE(map.find(i) == i + 1);
  }
}

TEST_CASE("swap maps", "[constructor]") {
  tbl_t map({{1, 2}}, 1, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  tbl_t map2({{3, 4}}, 1, StatefulHash(40), StatefulKeyEqual(50), alloc_t(60));
  map.swap(map2);

  REQUIRE(map.size() == 1);
  REQUIRE(map.hash_function().state == 40);
  REQUIRE(map.key_eq().state == 50);
  REQUIRE(map.get_allocator().state == 60);

  REQUIRE(map2.size() == 1);
  REQUIRE(map2.hash_function().state == 10);
  REQUIRE(map2.key_eq().state == 20);
  REQUIRE(map2.get_allocator().state == 30);

  // Uses ADL to find the specialized swap.
  swap(map, map2);

  REQUIRE(map.size() == 1);
  REQUIRE(map.hash_function().state == 10);
  REQUIRE(map.key_eq().state == 20);
  REQUIRE(map.get_allocator().state == 30);

  REQUIRE(map2.size() == 1);
  REQUIRE(map2.hash_function().state == 40);
  REQUIRE(map2.key_eq().state == 50);
  REQUIRE(map2.get_allocator().state == 60);
}

TEST_CASE("copy assign different allocators", "[constructor]") {
  tbl_t map({{1, 2}}, 1, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  tbl_t map2({{3, 4}}, 1, StatefulHash(40), StatefulKeyEqual(50), alloc_t(60));

  map = map2;
  REQUIRE(map.size() == 1);
  REQUIRE(map.find(3) == 4);
  REQUIRE(map.hash_function().state == 40);
  REQUIRE(map.key_eq().state == 50);
  REQUIRE(map.get_allocator().state == 30);

  REQUIRE(map2.size() == 1);
  REQUIRE(map2.hash_function().state == 40);
  REQUIRE(map2.key_eq().state == 50);
  REQUIRE(map2.get_allocator().state == 60);
}

TEST_CASE("move assign different allocators", "[constructor]") {
  tbl_t map({{1, 2}}, 1, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  tbl_t map2({{3, 4}}, 1, StatefulHash(40), StatefulKeyEqual(50), alloc_t(60));

  map = std::move(map2);
  REQUIRE(map.size() == 1);
  REQUIRE(map.find(3) == 4);
  REQUIRE(map.hash_function().state == 40);
  REQUIRE(map.key_eq().state == 50);
  REQUIRE(map.get_allocator().state == 30);

  REQUIRE(map2.hash_function().state == 40);
  REQUIRE(map2.key_eq().state == 50);
  REQUIRE(map2.get_allocator().state == 60);
}

TEST_CASE("move assign same allocators", "[constructor]") {
  tbl_t map({{1, 2}}, 1, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  tbl_t map2({{3, 4}}, 1, StatefulHash(40), StatefulKeyEqual(50), alloc_t(30));

  map = std::move(map2);
  REQUIRE(map.size() == 1);
  REQUIRE(map.find(3) == 4);
  REQUIRE(map.hash_function().state == 40);
  REQUIRE(map.key_eq().state == 50);
  REQUIRE(map.get_allocator().state == 30);

  REQUIRE(map2.size() == 0);
  REQUIRE(map2.hash_function().state == 40);
  REQUIRE(map2.key_eq().state == 50);
  REQUIRE(map2.get_allocator().state == 30);
}

TEST_CASE("initializer list assignment", "[constructor]") {
  tbl_t map({{1, 2}}, 1, StatefulHash(10), StatefulKeyEqual(20), alloc_t(30));
  REQUIRE(map.find(1) == 2);
  map = {{3, 4}};
  REQUIRE(map.find(3) == 4);
}