-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathscary_tests.cpp
More file actions
318 lines (257 loc) · 8.51 KB
/
scary_tests.cpp
File metadata and controls
318 lines (257 loc) · 8.51 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2022-2023 Christian Mazakas.
// 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 "../helpers/test.hpp"
#include "../helpers/unordered.hpp"
#include "../objects/test.hpp"
#include <memory>
struct hash1
{
template <class Key> std::size_t operator()(Key const&) const { return 1337; }
};
struct hash2
{
template <class Key> std::size_t operator()(Key const&) const { return 7331; }
};
struct equal1
{
template <class T> bool operator==(T const&) const { return true; }
};
struct equal2
{
template <class T> bool operator==(T const&) const { return true; }
};
///////////////////////////////////////////////////////////////////////////////
// we define two duplicated allocators here, each one having the same smart
// pointer type
// we want to prove in our test that different allocators with the same pointers
// (either fancy or raw) work equally well for our SCARY iterators
//
template <class T> struct allocator1
{
#ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
public:
#else
template <class> friend struct allocator1;
#endif
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef test::void_ptr void_pointer;
typedef test::void_const_ptr const_void_pointer;
typedef test::ptr<T> pointer;
typedef test::const_ptr<T> const_pointer;
typedef T& reference;
typedef T const& const_reference;
typedef T value_type;
template <class U> struct rebind
{
typedef allocator1<U> other;
};
allocator1() {}
template <class Y> allocator1(allocator1<Y> const&) {}
allocator1(allocator1 const&) {}
~allocator1() {}
pointer address(reference r) { return pointer(&r); }
const_pointer address(const_reference r) { return const_pointer(&r); }
pointer allocate(size_type n)
{
pointer p(static_cast<T*>(::operator new(n * sizeof(T))));
return p;
}
pointer allocate(size_type n, void const*)
{
pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
return ptr;
}
void deallocate(pointer p, size_type)
{
::operator delete((void*)p.operator->());
}
template <class U, class... Args>
void construct(U* p, Args&&... args)
{
new (p) U(std::forward<Args>(args)...);
}
// msvc-12.0 and msvc-14.0 seem to eliminate the destructor call as we're only
// ever using it with an int with has a trivial destructor so it eliminates
// the code and generates a warning about an unused variable
//
template <class U> void destroy(U* p)
{
(void)p;
p->~U();
}
size_type max_size() const { return (std::numeric_limits<size_type>::max)(); }
bool operator==(allocator1 const&) const { return true; }
bool operator!=(allocator1 const&) const { return false; }
enum
{
is_select_on_copy = false,
is_propagate_on_swap = false,
is_propagate_on_assign = false,
is_propagate_on_move = false
};
};
template <class T> struct allocator2
{
#ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
public:
#else
template <class> friend struct allocator2;
#endif
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef test::void_ptr void_pointer;
typedef test::void_const_ptr const_void_pointer;
typedef test::ptr<T> pointer;
typedef test::const_ptr<T> const_pointer;
typedef T& reference;
typedef T const& const_reference;
typedef T value_type;
template <class U> struct rebind
{
typedef allocator2<U> other;
};
allocator2() {}
template <class Y> allocator2(allocator2<Y> const&) {}
allocator2(allocator2 const&) {}
~allocator2() {}
pointer address(reference r) { return pointer(&r); }
const_pointer address(const_reference r) { return const_pointer(&r); }
pointer allocate(size_type n)
{
pointer p(static_cast<T*>(::operator new(n * sizeof(T))));
return p;
}
pointer allocate(size_type n, void const*)
{
pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
return ptr;
}
void deallocate(pointer p, size_type) { ::operator delete((void*)p.ptr_); }
template <class U, class... Args>
void construct(U* p, Args&&... args)
{
new (p) U(std::forward<Args>(args)...);
}
// msvc-12.0 and msvc-14.0 seem to eliminate the destructor call as we're only
// ever using it with an int with has a trivial destructor so it eliminates
// the code and generates a warning about an unused variable
//
template <class U> void destroy(U* p)
{
(void)p;
p->~U();
}
size_type max_size() const { return (std::numeric_limits<size_type>::max)(); }
bool operator==(allocator2 const&) const { return true; }
bool operator!=(allocator2 const&) const { return false; }
enum
{
is_select_on_copy = false,
is_propagate_on_swap = false,
is_propagate_on_assign = false,
is_propagate_on_move = false
};
};
template <class C1, class C2> void scary_test()
{
C1 x;
C2 y;
typename C2::iterator begin(x.begin());
BOOST_TEST(begin == x.end());
typename C2::const_iterator cbegin(x.cbegin());
BOOST_TEST(cbegin == x.cend());
#ifndef BOOST_UNORDERED_FOA_TESTS
typename C2::local_iterator lbegin(x.begin(0));
BOOST_TEST(lbegin == x.end(0));
typename C2::const_local_iterator clbegin(x.cbegin(0));
BOOST_TEST(clbegin == x.cend(0));
#endif
}
template <
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
class Map>
void map_scary_test()
{
typedef std::pair<int const, int> value_type;
typedef std::allocator<value_type> std_allocator_type;
typedef Map<int, int, hash1, std::equal_to<int>, std_allocator_type>
hash1_unordered_map;
typedef Map<int, int, hash2, std::equal_to<int>, std_allocator_type>
hash2_unordered_map;
typedef Map<int, int, boost::hash<int>, equal1, std_allocator_type>
equal1_unordered_map;
typedef Map<int, int, boost::hash<int>, equal2, std_allocator_type>
equal2_unordered_map;
// test allocators with a raw pointer as their `::pointer` type
//
typedef Map<int, int, boost::hash<int>, std::equal_to<int>,
test::allocator1<value_type> >
alloc1_unordered_map;
typedef Map<int, int, boost::hash<int>, std::equal_to<int>,
std_allocator_type>
std_alloc_unordered_map;
// test allocators with a fancy pointer as their `::pointer` type
//
typedef Map<int, int, boost::hash<int>, std::equal_to<int>,
allocator1<value_type> >
fancy1_unordered_map;
typedef Map<int, int, boost::hash<int>, std::equal_to<int>,
allocator2<value_type> >
fancy2_unordered_map;
scary_test<alloc1_unordered_map, std_alloc_unordered_map>();
scary_test<hash1_unordered_map, hash2_unordered_map>();
scary_test<equal1_unordered_map, equal2_unordered_map>();
scary_test<fancy1_unordered_map, fancy2_unordered_map>();
}
template <template <class Key, class Hash, class KeyEqual, class Allocator>
class Set>
void set_scary_test()
{
typedef int value_type;
typedef std::allocator<value_type> std_allocator_type;
typedef Set<int, hash1, std::equal_to<int>, std_allocator_type>
hash1_unordered_set;
typedef Set<int, hash2, std::equal_to<int>, std_allocator_type>
hash2_unordered_set;
typedef Set<int, boost::hash<int>, equal1, std_allocator_type>
equal1_unordered_set;
typedef Set<int, boost::hash<int>, equal2, std_allocator_type>
equal2_unordered_set;
// test allocators with a raw pointer as their `::pointer` type
//
typedef Set<int, boost::hash<int>, std::equal_to<int>,
test::allocator1<value_type> >
alloc1_unordered_set;
typedef Set<int, boost::hash<int>, std::equal_to<int>, std_allocator_type>
std_alloc_unordered_set;
// test allocators with a fancy pointer as their `::pointer` type
//
typedef Set<int, boost::hash<int>, std::equal_to<int>,
allocator1<value_type> >
fancy1_unordered_set;
typedef Set<int, boost::hash<int>, std::equal_to<int>,
allocator2<value_type> >
fancy2_unordered_set;
scary_test<alloc1_unordered_set, std_alloc_unordered_set>();
scary_test<hash1_unordered_set, hash2_unordered_set>();
scary_test<equal1_unordered_set, equal2_unordered_set>();
scary_test<fancy1_unordered_set, fancy2_unordered_set>();
}
UNORDERED_AUTO_TEST (scary_tests) {
#ifdef BOOST_UNORDERED_FOA_TESTS
map_scary_test<boost::unordered_flat_map>();
map_scary_test<boost::unordered_node_map>();
set_scary_test<boost::unordered_flat_set>();
set_scary_test<boost::unordered_node_set>();
#else
map_scary_test<boost::unordered_map>();
map_scary_test<boost::unordered_multimap>();
set_scary_test<boost::unordered_set>();
set_scary_test<boost::unordered_multiset>();
#endif
}
RUN_TESTS()