-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathintrusive_forward_list.cpp
More file actions
330 lines (264 loc) · 7.55 KB
/
intrusive_forward_list.cpp
File metadata and controls
330 lines (264 loc) · 7.55 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
319
320
321
322
323
324
325
326
327
328
329
330
#include <stdx/intrusive_forward_list.hpp>
#include <catch2/catch_test_macros.hpp>
namespace {
struct int_node {
int value{};
int_node *next{};
};
#if __cpp_concepts >= 201907L
static_assert(stdx::single_linkable<int_node>);
#endif
struct single_link_node {
single_link_node *next{};
};
struct bad_single_link_node {
int *next{};
};
} // namespace
#if __cpp_concepts >= 201907L
TEST_CASE("single_linkable", "[intrusive_forward_list]") {
STATIC_REQUIRE(not stdx::single_linkable<int>);
STATIC_REQUIRE(not stdx::single_linkable<bad_single_link_node>);
STATIC_REQUIRE(stdx::single_linkable<single_link_node>);
}
#endif
TEST_CASE("push_back, pop_front", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n{5};
list.push_back(&n);
auto poppedNode = list.pop_front();
CHECK(poppedNode->value == 5);
}
TEST_CASE("empty", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n{5};
CHECK(list.empty());
list.push_back(&n);
REQUIRE_FALSE(list.empty());
list.pop_front();
CHECK(list.empty());
}
TEST_CASE("push_back 2, pop_front 2", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
list.push_back(&n1);
list.push_back(&n2);
CHECK(list.pop_front()->value == 1);
CHECK(list.pop_front()->value == 2);
CHECK(list.empty());
}
TEST_CASE("push_back 2, pop_front 2 (sequentially)",
"[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
list.push_back(&n1);
CHECK(list.pop_front()->value == 1);
CHECK(list.empty());
list.push_back(&n2);
CHECK(list.pop_front()->value == 2);
CHECK(list.empty());
}
TEST_CASE("push_back 2, pop_front 2 (multiple lists)",
"[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> listA{};
stdx::intrusive_forward_list<int_node> listB{};
int_node n1{1};
int_node n2{2};
listA.push_back(&n1);
listA.push_back(&n2);
listA.pop_front();
listA.pop_front();
CHECK(listA.empty());
listB.push_back(&n1);
CHECK(listB.pop_front()->value == 1);
CHECK(listB.empty());
}
TEST_CASE("begin", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
int_node n3{3};
list.push_back(&n1);
list.push_back(&n2);
list.push_back(&n3);
CHECK(std::begin(list)->value == 1);
CHECK(std::cbegin(list)->value == 1);
}
TEST_CASE("front and back", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
int_node n3{3};
list.push_back(&n1);
list.push_back(&n2);
list.push_back(&n3);
CHECK(list.front().value == 1);
CHECK(list.back().value == 3);
}
TEST_CASE("iterator preincrement", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
int_node n3{3};
list.push_back(&n1);
list.push_back(&n2);
list.push_back(&n3);
auto i = std::begin(list);
CHECK(i->value == 1);
CHECK((++i)->value == 2);
CHECK(i->value == 2);
CHECK((++i)->value == 3);
CHECK(i->value == 3);
}
TEST_CASE("iterator postincrement", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
int_node n3{3};
list.push_back(&n1);
list.push_back(&n2);
list.push_back(&n3);
auto i = std::begin(list);
CHECK(i->value == 1);
CHECK((i++)->value == 1);
CHECK(i->value == 2);
CHECK((i++)->value == 2);
CHECK(i->value == 3);
}
TEST_CASE("iterator equality", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
list.push_back(&n1);
CHECK(std::begin(list) == std::begin(list));
}
TEST_CASE("iterator inequality", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
int_node n3{3};
list.push_back(&n1);
list.push_back(&n2);
list.push_back(&n3);
auto i = std::begin(list);
CHECK(i == std::begin(list));
CHECK(i != std::end(list));
i++;
CHECK(i != std::begin(list));
CHECK(i != std::end(list));
i++;
CHECK(i != std::begin(list));
CHECK(i != std::end(list));
i++;
CHECK(i != std::begin(list));
CHECK(i == std::end(list));
}
TEST_CASE("clear", "[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
list.push_back(&n1);
list.push_back(&n2);
list.clear();
CHECK(list.empty());
}
TEST_CASE("checked operation clears pointers on pop",
"[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
list.push_back(&n1);
list.push_back(&n2);
CHECK(n1.next != nullptr);
CHECK(list.pop_front() == &n1);
CHECK(n1.next == nullptr);
}
TEST_CASE("checked operation clears pointers on clear",
"[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n1{1};
int_node n2{2};
list.push_back(&n1);
list.push_back(&n2);
CHECK(n1.next != nullptr);
list.clear();
CHECK(n1.next == nullptr);
}
namespace {
#if __cplusplus >= 202002L
int compile_time_calls{};
#else
int runtime_calls{};
#endif
struct injected_handler {
#if __cplusplus >= 202002L
template <stdx::ct_string Why, typename... Ts>
static auto panic(Ts &&...) noexcept -> void {
STATIC_REQUIRE(std::string_view{Why} == "bad list node!");
++compile_time_calls;
}
#else
template <typename Why, typename... Ts>
static auto panic(Why why, Ts &&...) noexcept -> void {
CHECK(std::string_view{why} == "bad list node!");
++runtime_calls;
}
#endif
};
} // namespace
template <> inline auto stdx::panic_handler<> = injected_handler{};
#if __cplusplus >= 202002L
TEST_CASE("checked panic when pushing populated node",
"[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n{5};
n.next = &n;
compile_time_calls = 0;
list.push_back(&n);
CHECK(compile_time_calls == 1);
list.pop_front();
n.next = &n;
compile_time_calls = 0;
list.push_front(&n);
CHECK(compile_time_calls == 1);
}
#else
TEST_CASE("checked panic when pushing populated node",
"[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node> list{};
int_node n{5};
n.next = &n;
runtime_calls = 0;
list.push_back(&n);
CHECK(runtime_calls == 1);
list.pop_front();
n.next = &n;
runtime_calls = 0;
list.push_front(&n);
CHECK(runtime_calls == 1);
}
#endif
TEST_CASE("unchecked operation doesn't clear pointers",
"[intrusive_forward_list]") {
stdx::intrusive_forward_list<int_node, stdx::node_policy::unchecked> list{};
int_node n1{1};
int_node n2{2};
list.push_back(&n1);
list.push_back(&n2);
auto before = n1.next;
CHECK(list.pop_front() == &n1);
CHECK(n1.next == before);
}
TEST_CASE("intrusive_forward_list can be instantiated with incomplete types",
"[intrusive_forward_list]") {
struct incomplete_int_node;
stdx::intrusive_forward_list<incomplete_int_node> list{};
struct incomplete_int_node {
int value{};
incomplete_int_node *next{};
};
incomplete_int_node n1{1};
list.push_back(&n1);
CHECK(list.pop_front() == &n1);
}