32
32
33
33
#include < limits>
34
34
35
- struct ZTestEntryCompare {
35
+ struct ZRBTestEntryCompare {
36
36
int operator ()(const ZIntrusiveRBTreeNode* a, const ZIntrusiveRBTreeNode* b);
37
37
int operator ()(int key, const ZIntrusiveRBTreeNode* entry);
38
38
};
39
39
40
- class ZTestEntry : public ArenaObj {
41
- friend class ZIntrusiveRBTree <int , ZTestEntryCompare >;
40
+ class ZRBTestEntry : public ArenaObj {
41
+ friend class ZIntrusiveRBTree <int , ZRBTestEntryCompare >;
42
42
43
43
public:
44
- using ZTree = ZIntrusiveRBTree<int , ZTestEntryCompare >;
44
+ using ZTree = ZIntrusiveRBTree<int , ZRBTestEntryCompare >;
45
45
private:
46
46
const int _id;
47
47
ZIntrusiveRBTreeNode _node;
48
48
49
49
public:
50
- ZTestEntry (int id)
50
+ ZRBTestEntry (int id)
51
51
: _id(id),
52
52
_node () {}
53
53
54
54
int id () const {
55
55
return _id;
56
56
}
57
57
58
- static ZIntrusiveRBTreeNode* cast_to_inner (ZTestEntry * element) {
58
+ static ZIntrusiveRBTreeNode* cast_to_inner (ZRBTestEntry * element) {
59
59
return &element->_node ;
60
60
}
61
- static const ZTestEntry * cast_to_outer (const ZIntrusiveRBTreeNode* node) {
62
- return (ZTestEntry *)((uintptr_t )node - offset_of (ZTestEntry , _node));
61
+ static const ZRBTestEntry * cast_to_outer (const ZIntrusiveRBTreeNode* node) {
62
+ return (ZRBTestEntry *)((uintptr_t )node - offset_of (ZRBTestEntry , _node));
63
63
}
64
64
65
65
};
66
66
67
- int ZTestEntryCompare ::operator ()(const ZIntrusiveRBTreeNode* a, const ZIntrusiveRBTreeNode* b) {
68
- return ZTestEntry ::cast_to_outer (a)->id () - ZTestEntry ::cast_to_outer (b)->id ();
67
+ int ZRBTestEntryCompare ::operator ()(const ZIntrusiveRBTreeNode* a, const ZIntrusiveRBTreeNode* b) {
68
+ return ZRBTestEntry ::cast_to_outer (a)->id () - ZRBTestEntry ::cast_to_outer (b)->id ();
69
69
}
70
- int ZTestEntryCompare ::operator ()(int key, const ZIntrusiveRBTreeNode* entry) {
71
- return key - ZTestEntry ::cast_to_outer (entry)->id ();
70
+ int ZRBTestEntryCompare ::operator ()(int key, const ZIntrusiveRBTreeNode* entry) {
71
+ return key - ZRBTestEntry ::cast_to_outer (entry)->id ();
72
72
}
73
73
74
74
class ZTreeTest : public ZTest {
75
75
public:
76
- void shuffle_array (ZTestEntry ** beg, ZTestEntry ** end);
77
- void reverse_array (ZTestEntry ** beg, ZTestEntry ** end);
76
+ void shuffle_array (ZRBTestEntry ** beg, ZRBTestEntry ** end);
77
+ void reverse_array (ZRBTestEntry ** beg, ZRBTestEntry ** end);
78
78
};
79
79
80
80
class ResettableArena : public Arena {
@@ -96,10 +96,10 @@ TEST_F(ZTreeTest, test_random) {
96
96
constexpr size_t sizes[] = {1 , 2 , 4 , 8 , 16 , 1024 , 1024 * 1024 };
97
97
constexpr size_t num_sizes = ARRAY_SIZE (sizes);
98
98
constexpr size_t iterations_multiplier = 4 ;
99
- constexpr size_t max_allocation_size = sizes[num_sizes - 1 ] * iterations_multiplier * sizeof (ZTestEntry );
99
+ constexpr size_t max_allocation_size = sizes[num_sizes - 1 ] * iterations_multiplier * sizeof (ZRBTestEntry );
100
100
ResettableArena arena{MemTag::mtTest, Arena::Tag::tag_other, max_allocation_size};
101
101
for (size_t s : sizes) {
102
- ZTestEntry ::ZTree tree;
102
+ ZRBTestEntry ::ZTree tree;
103
103
const size_t num_iterations = s * iterations_multiplier;
104
104
for (size_t i = 0 ; i < num_iterations; i++) {
105
105
if (i % s == 0 ) {
@@ -113,7 +113,7 @@ TEST_F(ZTreeTest, test_random) {
113
113
// Replace
114
114
if (i % 4 == 0 ) {
115
115
// Replace with new
116
- tree.replace (ZTestEntry ::cast_to_inner (new (&arena) ZTestEntry (id)), cursor);
116
+ tree.replace (ZRBTestEntry ::cast_to_inner (new (&arena) ZRBTestEntry (id)), cursor);
117
117
} else {
118
118
// Replace with same
119
119
tree.replace (cursor.node (), cursor);
@@ -124,34 +124,34 @@ TEST_F(ZTreeTest, test_random) {
124
124
}
125
125
} else {
126
126
// Insert
127
- tree.insert (ZTestEntry ::cast_to_inner (new (&arena) ZTestEntry (id)), cursor);
127
+ tree.insert (ZRBTestEntry ::cast_to_inner (new (&arena) ZRBTestEntry (id)), cursor);
128
128
}
129
129
}
130
130
tree.verify_tree ();
131
131
arena.reset_arena ();
132
132
}
133
133
}
134
134
135
- void ZTreeTest::reverse_array (ZTestEntry ** beg, ZTestEntry ** end) {
135
+ void ZTreeTest::reverse_array (ZRBTestEntry ** beg, ZRBTestEntry ** end) {
136
136
if (beg == end) {
137
137
return ;
138
138
}
139
139
140
- ZTestEntry ** first = beg;
141
- ZTestEntry ** last = end - 1 ;
140
+ ZRBTestEntry ** first = beg;
141
+ ZRBTestEntry ** last = end - 1 ;
142
142
while (first < last) {
143
143
::swap (*first, *last);
144
144
first++;
145
145
last--;
146
146
}
147
147
}
148
148
149
- void ZTreeTest::shuffle_array (ZTestEntry ** beg, ZTestEntry ** end) {
149
+ void ZTreeTest::shuffle_array (ZRBTestEntry ** beg, ZRBTestEntry ** end) {
150
150
if (beg == end) {
151
151
return ;
152
152
}
153
153
154
- for (ZTestEntry ** first = beg + 1 ; first != end; first++) {
154
+ for (ZRBTestEntry ** first = beg + 1 ; first != end; first++) {
155
155
const ptrdiff_t distance = first - beg;
156
156
ASSERT_GE (distance, 0 );
157
157
const ptrdiff_t random_index = random () % (distance + 1 );
@@ -162,51 +162,51 @@ void ZTreeTest::shuffle_array(ZTestEntry** beg, ZTestEntry** end) {
162
162
TEST_F (ZTreeTest, test_insert) {
163
163
Arena arena (MemTag::mtTest);
164
164
constexpr size_t num_entries = 1024 ;
165
- ZTestEntry * forward[num_entries]{};
166
- ZTestEntry * reverse[num_entries]{};
167
- ZTestEntry * shuffle[num_entries]{};
165
+ ZRBTestEntry * forward[num_entries]{};
166
+ ZRBTestEntry * reverse[num_entries]{};
167
+ ZRBTestEntry * shuffle[num_entries]{};
168
168
for (size_t i = 0 ; i < num_entries; i++) {
169
169
const int id = static_cast <int >(i);
170
- forward[i] = new (&arena) ZTestEntry (id);
171
- reverse[i] = new (&arena) ZTestEntry (id);
172
- shuffle[i] = new (&arena) ZTestEntry (id);
170
+ forward[i] = new (&arena) ZRBTestEntry (id);
171
+ reverse[i] = new (&arena) ZRBTestEntry (id);
172
+ shuffle[i] = new (&arena) ZRBTestEntry (id);
173
173
}
174
174
reverse_array (reverse, reverse + num_entries);
175
175
shuffle_array (shuffle, shuffle + num_entries);
176
176
177
- ZTestEntry ::ZTree forward_tree;
177
+ ZRBTestEntry ::ZTree forward_tree;
178
178
auto cursor = forward_tree.root_cursor ();
179
179
for (size_t i = 0 ; i < num_entries; i++) {
180
180
ASSERT_TRUE (cursor.is_valid ());
181
181
ASSERT_FALSE (cursor.found ());
182
- ZIntrusiveRBTreeNode* const new_node = ZTestEntry ::cast_to_inner (forward[i]);
182
+ ZIntrusiveRBTreeNode* const new_node = ZRBTestEntry ::cast_to_inner (forward[i]);
183
183
forward_tree.insert (new_node, cursor);
184
184
cursor = forward_tree.next_cursor (new_node);
185
185
}
186
186
forward_tree.verify_tree ();
187
187
188
- ZTestEntry ::ZTree reverse_tree;
188
+ ZRBTestEntry ::ZTree reverse_tree;
189
189
cursor = reverse_tree.root_cursor ();
190
190
for (size_t i = 0 ; i < num_entries; i++) {
191
191
ASSERT_TRUE (cursor.is_valid ());
192
192
ASSERT_FALSE (cursor.found ());
193
- ZIntrusiveRBTreeNode* const new_node = ZTestEntry ::cast_to_inner (reverse[i]);
193
+ ZIntrusiveRBTreeNode* const new_node = ZRBTestEntry ::cast_to_inner (reverse[i]);
194
194
reverse_tree.insert (new_node, cursor);
195
195
cursor = reverse_tree.prev_cursor (new_node);
196
196
}
197
197
reverse_tree.verify_tree ();
198
198
199
- ZTestEntry ::ZTree shuffle_tree;
199
+ ZRBTestEntry ::ZTree shuffle_tree;
200
200
for (size_t i = 0 ; i < num_entries; i++) {
201
201
cursor = shuffle_tree.find (reverse[i]->id ());
202
202
ASSERT_TRUE (cursor.is_valid ());
203
203
ASSERT_FALSE (cursor.found ());
204
- ZIntrusiveRBTreeNode* const new_node = ZTestEntry ::cast_to_inner (reverse[i]);
204
+ ZIntrusiveRBTreeNode* const new_node = ZRBTestEntry ::cast_to_inner (reverse[i]);
205
205
shuffle_tree.insert (new_node, cursor);
206
206
}
207
207
shuffle_tree.verify_tree ();
208
208
209
- ZTestEntryCompare compare_fn;
209
+ ZRBTestEntryCompare compare_fn;
210
210
const ZIntrusiveRBTreeNode* forward_node = forward_tree.first ();
211
211
const ZIntrusiveRBTreeNode* reverse_node = reverse_tree.first ();
212
212
const ZIntrusiveRBTreeNode* shuffle_node = shuffle_tree.first ();
@@ -240,13 +240,13 @@ TEST_F(ZTreeTest, test_insert) {
240
240
TEST_F (ZTreeTest, test_replace) {
241
241
Arena arena (MemTag::mtTest);
242
242
constexpr size_t num_entries = 1024 ;
243
- ZTestEntry ::ZTree tree;
243
+ ZRBTestEntry ::ZTree tree;
244
244
auto cursor = tree.root_cursor ();
245
245
for (size_t i = 0 ; i < num_entries; i++) {
246
246
ASSERT_TRUE (cursor.is_valid ());
247
247
ASSERT_FALSE (cursor.found ());
248
248
const int id = static_cast <int >(i) * 2 + 1 ;
249
- ZIntrusiveRBTreeNode* const new_node = ZTestEntry ::cast_to_inner (new (&arena) ZTestEntry (id));
249
+ ZIntrusiveRBTreeNode* const new_node = ZRBTestEntry ::cast_to_inner (new (&arena) ZRBTestEntry (id));
250
250
tree.insert (new_node, cursor);
251
251
cursor = tree.next_cursor (new_node);
252
252
}
@@ -261,14 +261,14 @@ TEST_F(ZTreeTest, test_replace) {
261
261
switch (i++ % 4 ) {
262
262
case 0 : {
263
263
// Decrement
264
- ZTestEntry * new_entry = new (&arena) ZTestEntry ( ZTestEntry ::cast_to_outer (&node)->id () - 1 );
265
- it.replace (ZTestEntry ::cast_to_inner (new_entry));
264
+ ZRBTestEntry * new_entry = new (&arena) ZRBTestEntry ( ZRBTestEntry ::cast_to_outer (&node)->id () - 1 );
265
+ it.replace (ZRBTestEntry ::cast_to_inner (new_entry));
266
266
} break ;
267
267
case 1 : break ;
268
268
case 2 : {
269
269
// Increment
270
- ZTestEntry * new_entry = new (&arena) ZTestEntry ( ZTestEntry ::cast_to_outer (&node)->id () + 1 );
271
- it.replace (ZTestEntry ::cast_to_inner (new_entry));
270
+ ZRBTestEntry * new_entry = new (&arena) ZRBTestEntry ( ZRBTestEntry ::cast_to_outer (&node)->id () + 1 );
271
+ it.replace (ZRBTestEntry ::cast_to_inner (new_entry));
272
272
} break ;
273
273
case 3 : break ;
274
274
default :
@@ -279,39 +279,39 @@ TEST_F(ZTreeTest, test_replace) {
279
279
280
280
int last_id = std::numeric_limits<int >::min ();
281
281
for (auto & node : tree) {
282
- int id = ZTestEntry ::cast_to_outer (&node)->id ();
282
+ int id = ZRBTestEntry ::cast_to_outer (&node)->id ();
283
283
ASSERT_LT (last_id, id);
284
284
last_id = id;
285
285
}
286
286
tree.verify_tree ();
287
287
288
288
last_id = std::numeric_limits<int >::min ();
289
289
for (auto it = tree.begin (), end = tree.end (); it != end; ++it) {
290
- int id = ZTestEntry ::cast_to_outer (&*it)->id ();
290
+ int id = ZRBTestEntry ::cast_to_outer (&*it)->id ();
291
291
ASSERT_LT (last_id, id);
292
292
last_id = id;
293
293
}
294
294
tree.verify_tree ();
295
295
296
296
last_id = std::numeric_limits<int >::min ();
297
297
for (auto it = tree.cbegin (), end = tree.cend (); it != end; ++it) {
298
- int id = ZTestEntry ::cast_to_outer (&*it)->id ();
298
+ int id = ZRBTestEntry ::cast_to_outer (&*it)->id ();
299
299
ASSERT_LT (last_id, id);
300
300
last_id = id;
301
301
}
302
302
tree.verify_tree ();
303
303
304
304
last_id = std::numeric_limits<int >::max ();
305
305
for (auto it = tree.rbegin (), end = tree.rend (); it != end; ++it) {
306
- int id = ZTestEntry ::cast_to_outer (&*it)->id ();
306
+ int id = ZRBTestEntry ::cast_to_outer (&*it)->id ();
307
307
ASSERT_GT (last_id, id);
308
308
last_id = id;
309
309
}
310
310
tree.verify_tree ();
311
311
312
312
last_id = std::numeric_limits<int >::max ();
313
313
for (auto it = tree.crbegin (), end = tree.crend (); it != end; ++it) {
314
- int id = ZTestEntry ::cast_to_outer (&*it)->id ();
314
+ int id = ZRBTestEntry ::cast_to_outer (&*it)->id ();
315
315
ASSERT_GT (last_id, id);
316
316
last_id = id;
317
317
}
@@ -321,19 +321,19 @@ TEST_F(ZTreeTest, test_replace) {
321
321
TEST_F (ZTreeTest, test_remove) {
322
322
Arena arena (MemTag::mtTest);
323
323
constexpr int num_entries = 1024 ;
324
- ZTestEntry ::ZTree tree;
324
+ ZRBTestEntry ::ZTree tree;
325
325
int id = 0 ;
326
- tree.insert (ZTestEntry ::cast_to_inner (new (&arena) ZTestEntry (++id)), tree.root_cursor ());
326
+ tree.insert (ZRBTestEntry ::cast_to_inner (new (&arena) ZRBTestEntry (++id)), tree.root_cursor ());
327
327
for (auto & node : tree) {
328
- if (ZTestEntry ::cast_to_outer (&node)->id () == num_entries) {
328
+ if (ZRBTestEntry ::cast_to_outer (&node)->id () == num_entries) {
329
329
break ;
330
330
}
331
331
auto cursor = tree.next_cursor (&node);
332
- ZIntrusiveRBTreeNode* const new_node = ZTestEntry ::cast_to_inner (new (&arena) ZTestEntry (++id));
332
+ ZIntrusiveRBTreeNode* const new_node = ZRBTestEntry ::cast_to_inner (new (&arena) ZRBTestEntry (++id));
333
333
tree.insert (new_node, cursor);
334
334
}
335
335
tree.verify_tree ();
336
- ASSERT_EQ (ZTestEntry ::cast_to_outer (tree.last ())->id (), num_entries);
336
+ ASSERT_EQ (ZRBTestEntry ::cast_to_outer (tree.last ())->id (), num_entries);
337
337
338
338
int i = 0 ;
339
339
int removed = 0 ;
0 commit comments