-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdetail.cpp
263 lines (209 loc) · 7.72 KB
/
detail.cpp
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
//
// Copyright (C) 2020-2023 Krylov Yaroslav.
//
// 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 <optional>
#include <set>
#include <tuple>
#include "catch2_extra.hpp"
#include "ureact/detail/node_base.hpp"
#include "ureact/detail/slot_map.hpp"
// non-copyable and non-movable
static_assert( !std::is_copy_constructible_v<ureact::detail::node_base> );
static_assert( !std::is_copy_assignable_v<ureact::detail::node_base> );
static_assert( !std::is_move_constructible_v<ureact::detail::node_base> );
static_assert( !std::is_move_assignable_v<ureact::detail::node_base> );
namespace
{
class detector
{
public:
detector( std::set<int>* aliveIndices, int value )
: alive_indices( aliveIndices )
, value( value )
{
aliveIndices->insert( value );
}
~detector()
{
if( has_value )
{
alive_indices->erase( value );
}
}
UREACT_MAKE_NONCOPYABLE( detector );
detector( detector&& other )
: alive_indices( other.alive_indices )
, value( other.value )
{
other.has_value = false;
}
detector& operator=( detector&& other ) = delete;
[[nodiscard]] int get_value() const
{
return value;
}
[[nodiscard]] int get_value_non_const()
{
return value;
}
private:
std::set<int>* alive_indices;
int value;
bool has_value = true;
};
} // namespace
TEST_CASE( "ureact::detail::slot_map" )
{
std::set<int> alive_indices;
SECTION( "Initial state" )
{
ureact::detail::slot_map<detector> slot_map;
CHECK( slot_map.empty() );
CHECK( slot_map.size() == 0 );
CHECK( slot_map.capacity() == 0 );
}
SECTION( "Emplace and then clean up" )
{
ureact::detail::slot_map<detector> slot_map;
const auto newValueSlotId = slot_map.emplace( &alive_indices, 1 );
CHECK_FALSE( slot_map.empty() );
CHECK( slot_map.size() == 1 );
CHECK( slot_map.capacity() > 0 );
CHECK( alive_indices == std::set<int>{ 1 } );
const auto initialCapacity = slot_map.capacity();
SECTION( "erase it" )
{
slot_map.erase( newValueSlotId );
CHECK( slot_map.capacity() == initialCapacity );
}
SECTION( "clear" )
{
slot_map.clear();
CHECK( slot_map.capacity() == initialCapacity );
}
SECTION( "reset" )
{
slot_map.reset();
CHECK( slot_map.capacity() == 0 );
}
CHECK( slot_map.empty() );
CHECK( slot_map.size() == 0 );
CHECK( alive_indices == std::set<int>{} );
}
SECTION( "Access" )
{
ureact::detail::slot_map<detector> slot_map;
const auto newValueSlotId = slot_map.emplace( &alive_indices, 42 );
CHECK_FALSE( slot_map.empty() );
CHECK( slot_map.size() == 1 );
const auto get_value = []( const ureact::detail::slot_map<detector>& from, size_t slotId ) {
return from[slotId].get_value();
};
const auto get_value_non_const
= []( ureact::detail::slot_map<detector>& from, size_t slotId ) {
return from[slotId].get_value_non_const();
};
CHECK( get_value( slot_map, newValueSlotId ) == 42 );
CHECK( get_value_non_const( slot_map, newValueSlotId ) == 42 );
}
SECTION( "Destructor clears all existing values" )
{
{
ureact::detail::slot_map<detector> slot_map;
const auto firstValueSlot = slot_map.emplace( &alive_indices, -1 );
std::ignore = slot_map.emplace( &alive_indices, 1 );
slot_map.erase( firstValueSlot ); // make a hole, so we can see if it is handled
CHECK( alive_indices == std::set<int>{ 1 } );
}
CHECK( alive_indices == std::set<int>{} );
}
SECTION( "Move construction" )
{
ureact::detail::slot_map<detector> slot_map;
const auto firstValueSlot = slot_map.emplace( &alive_indices, -1 );
std::ignore = slot_map.emplace( &alive_indices, 1 );
slot_map.erase( firstValueSlot ); // make a hole, so we can see if it is handled
CHECK_FALSE( slot_map.empty() );
CHECK( slot_map.size() == 1 );
CHECK( slot_map.capacity() > 0 );
CHECK( alive_indices == std::set<int>{ 1 } );
const auto originalCapacity = slot_map.capacity();
ureact::detail::slot_map<detector> slot_map2{ std::move( slot_map ) };
// values are intact
CHECK( alive_indices == std::set<int>{ 1 } );
// new slot_map has borrowed 1 value and all capacity
CHECK_FALSE( slot_map2.empty() );
CHECK( slot_map2.size() == 1 );
CHECK( slot_map2.capacity() == originalCapacity );
// Old slot map should be in initial state
CHECK( slot_map.empty() );
CHECK( slot_map.size() == 0 );
CHECK( slot_map.capacity() == 0 );
}
SECTION( "Move assignment" )
{
ureact::detail::slot_map<detector> slot_map;
const auto firstValueSlot = slot_map.emplace( &alive_indices, -1 );
std::ignore = slot_map.emplace( &alive_indices, 1 );
slot_map.erase( firstValueSlot );
CHECK_FALSE( slot_map.empty() );
CHECK( slot_map.size() == 1 );
CHECK( slot_map.capacity() > 0 );
CHECK( alive_indices == std::set<int>{ 1 } );
const auto originalCapacity = slot_map.capacity();
SECTION( "from self" )
{
UREACT_CLANG_SUPPRESS_WARNING_WITH_PUSH( "-Wself-move" )
slot_map = std::move( slot_map );
UREACT_CLANG_SUPPRESS_WARNING_POP
// slot_map should stay intact
CHECK_FALSE( slot_map.empty() );
CHECK( slot_map.size() == 1 );
CHECK( slot_map.capacity() == originalCapacity );
CHECK( alive_indices == std::set<int>{ 1 } );
}
SECTION( "from other" )
{
ureact::detail::slot_map<detector> slot_map2;
std::ignore = slot_map2.emplace( &alive_indices, 10 );
const auto secondValueSlot = slot_map2.emplace( &alive_indices, -10 );
std::ignore = slot_map2.emplace( &alive_indices, 11 );
slot_map2.erase( secondValueSlot );
CHECK( alive_indices == std::set<int>{ 1, 10, 11 } );
slot_map2 = std::move( slot_map );
// values from slot_map2 are erased
CHECK( alive_indices == std::set<int>{ 1 } );
// new slot_map has borrowed 1 value and all capacity
CHECK_FALSE( slot_map2.empty() );
CHECK( slot_map2.size() == 1 );
CHECK( slot_map2.capacity() == originalCapacity );
// Old slot map should be in initial state
CHECK( slot_map.empty() );
CHECK( slot_map.size() == 0 );
CHECK( slot_map.capacity() == 0 );
}
}
SECTION( "Growth" )
{
ureact::detail::slot_map<detector> slot_map;
int i = 0;
std::ignore = slot_map.emplace( &alive_indices, i++ );
CHECK( slot_map.capacity() > 0 );
// Fill up values to match capacity
const auto initialCapacity = slot_map.capacity();
while( slot_map.size() < initialCapacity )
{
std::ignore = slot_map.emplace( &alive_indices, i++ );
}
CHECK( alive_indices.size() == slot_map.size() );
CHECK( slot_map.capacity() == initialCapacity );
// Emplace one more
std::ignore = slot_map.emplace( &alive_indices, i++ );
CHECK( slot_map.capacity() > initialCapacity );
CHECK( alive_indices.size() == slot_map.size() );
}
}