-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathintrusive_list_properties.cpp
More file actions
209 lines (155 loc) · 5.85 KB
/
intrusive_list_properties.cpp
File metadata and controls
209 lines (155 loc) · 5.85 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
#include <stdx/intrusive_forward_list.hpp>
#include <stdx/intrusive_list.hpp>
#include <catch2/catch_test_macros.hpp>
#include <rapidcheck/catch.h>
#include <rapidcheck/state.h>
#include <iterator>
#include <list>
namespace {
struct int_node {
int value{};
int_node *prev{};
int_node *next{};
};
} // namespace
using ListModel = std::list<int>;
template <typename T> struct list_element_deallocator {
T list{};
void clear() {
std::list<int_node *> nodes_to_delete{};
for (int_node &n : list) {
nodes_to_delete.push_back(&n);
}
list.clear();
for (int_node *n : nodes_to_delete) {
delete n;
}
}
~list_element_deallocator() { clear(); }
};
template <typename ListSut> struct IntrusiveListCommands {
static void rc_assert_equal(ListModel const &m, ListSut const &sut) {
RC_ASSERT(m.empty() == sut.list.empty());
if (!m.empty()) {
RC_ASSERT(m.front() == sut.list.front().value);
RC_ASSERT(m.back() == sut.list.back().value);
}
auto mi = m.cbegin();
auto si = sut.list.cbegin();
while (mi != m.cend() && si != sut.list.cend()) {
RC_ASSERT(*mi == si->value);
mi++;
si++;
}
RC_ASSERT(mi == m.cend());
RC_ASSERT(si == sut.list.cend());
}
struct PushBack : rc::state::Command<ListModel, ListSut> {
int value;
PushBack() : value{*rc::gen::inRange(0, 100)} {}
void checkPreconditions(ListModel const &) const override {}
void apply(ListModel &m) const override { m.push_back(value); }
void run(ListModel const &m, ListSut &sut) const override {
rc_assert_equal(m, sut);
sut.list.push_back(new int_node{value});
}
void show(std::ostream &os) const override {
os << "push_back(" << value << ")";
}
};
struct PopBack : rc::state::Command<ListModel, ListSut> {
void checkPreconditions(ListModel const &m) const override {
RC_PRE(!m.empty());
}
void apply(ListModel &m) const override { m.pop_back(); }
void run(ListModel const &m, ListSut &sut) const override {
rc_assert_equal(m, sut);
delete sut.list.pop_back();
}
void show(std::ostream &os) const override { os << "pop_back()"; }
};
struct PushFront : rc::state::Command<ListModel, ListSut> {
int value;
PushFront() : value{*rc::gen::inRange(0, 100)} {}
void checkPreconditions(ListModel const &) const override {}
void apply(ListModel &m) const override { m.push_front(value); }
void run(ListModel const &m, ListSut &sut) const override {
rc_assert_equal(m, sut);
sut.list.push_front(new int_node{value});
}
void show(std::ostream &os) const override {
os << "push_front(" << value << ")";
}
};
struct PopFront : rc::state::Command<ListModel, ListSut> {
void checkPreconditions(ListModel const &m) const override {
RC_PRE(!m.empty());
}
void apply(ListModel &m) const override { m.pop_front(); }
void run(ListModel const &m, ListSut &sut) const override {
rc_assert_equal(m, sut);
delete sut.list.pop_front();
}
void show(std::ostream &os) const override { os << "pop_front()"; }
};
struct Clear : rc::state::Command<ListModel, ListSut> {
void checkPreconditions(ListModel const &) const override {}
void apply(ListModel &m) const override { m.clear(); }
void run(ListModel const &m, ListSut &sut) const override {
rc_assert_equal(m, sut);
sut.clear();
}
void show(std::ostream &os) const override { os << "clear()"; }
};
struct Remove : rc::state::Command<ListModel, ListSut> {
int index;
Remove() : index{*rc::gen::inRange(0, 100)} {}
void checkPreconditions(ListModel const &m) const override {
RC_PRE(!m.empty());
}
void apply(ListModel &m) const override {
auto wrapped_index = index % static_cast<int>(m.size());
auto iter = std::next(m.begin(), wrapped_index);
m.erase(iter);
}
void run(ListModel const &m, ListSut &sut) const override {
rc_assert_equal(m, sut);
auto wrapped_index = index % static_cast<int>(m.size());
auto iter = std::next(sut.list.begin(), wrapped_index);
auto p = &(*iter);
sut.list.remove(p);
delete p;
}
void show(std::ostream &os) const override {
os << "remove(pointer at index " << index << ")";
}
};
};
TEST_CASE("push and pop, front and back, clear",
"[intrusive_list_properties]") {
using ListSut = list_element_deallocator<stdx::intrusive_list<int_node>>;
using Cmds = IntrusiveListCommands<ListSut>;
rc::prop("", [] {
ListModel initialState;
ListSut sut;
rc::state::check(
initialState, sut,
rc::state::gen::execOneOfWithArgs<Cmds::PushBack, Cmds::PopBack,
Cmds::PushFront, Cmds::PopFront,
Cmds::Clear, Cmds::Remove>());
});
}
TEST_CASE("push and pop, front and back",
"[intrusive_forward_list_properties]") {
using ListSut =
list_element_deallocator<stdx::intrusive_forward_list<int_node>>;
using Cmds = IntrusiveListCommands<ListSut>;
rc::prop("", [] {
ListModel initialState;
ListSut sut;
rc::state::check(
initialState, sut,
rc::state::gen::execOneOfWithArgs<Cmds::PushBack, Cmds::PushFront,
Cmds::PopFront, Cmds::Clear>());
});
}