-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathMultiVectorTest.cpp
More file actions
348 lines (306 loc) · 10.6 KB
/
MultiVectorTest.cpp
File metadata and controls
348 lines (306 loc) · 10.6 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Copyright (c) 2018 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
#include "test_structures.hpp"
#include <flatdata/flatdata.h>
#include "catch_amalgamated.hpp"
using namespace flatdata;
using namespace test_structures;
static auto create_view_with_3_items = []( ) {
auto storage = MemoryResourceStorage::create( );
auto vector = storage->create_multi_vector< TestIndexType48, AStruct, BStructMutator, CStruct >(
"data", "foo" );
{
auto list = vector.grow( );
auto a1 = list.add< AStruct >( );
a1.value = 7;
auto a2 = list.add< AStructMutator >( );
a2.value = 8;
auto c1 = list.add< CStruct >( );
c1.value = 1230000;
auto b = list.add< BStruct >( );
b.value = 1000;
auto c2 = list.add< CStruct >( );
c2.value = 1000000;
}
{
auto list = vector.grow( );
auto c = list.add< CStruct >( );
c.value = 1000000;
}
{
auto list = vector.grow( );
auto a = list.add< AStructMutator >( );
a.value = 8;
}
auto view_from_close = vector.close( );
return std::make_pair( std::move( storage ), std::move( view_from_close ) );
};
static const size_t NUM_ITEMS_TO_CAUSE_FLUSH
= 32 * 1024 * 1024 / 4 + 1024; // enough to flush, and them some
static auto create_view_with_enough_items_to_flush = []( ) {
auto storage = MemoryResourceStorage::create( );
auto vector = storage->create_multi_vector< TestIndexType48, AStruct, BStruct, CStruct >(
"data", "foo" );
for ( size_t i = 0; i < NUM_ITEMS_TO_CAUSE_FLUSH; i++ )
{
auto list = vector.grow( );
list.add< CStruct >( ).value = i;
}
auto view_from_close = vector.close( );
return std::make_pair( std::move( storage ), std::move( view_from_close ) );
};
TEST_CASE( "Test various data functor", "[MultiVector]" )
{
auto view = create_view_with_3_items( );
struct Reader
{
void
operator( )( AStruct x )
{
has_a |= x.value == 8;
}
void
operator( )( BStruct x )
{
has_b |= x.value == 1000;
}
void
operator( )( CStruct x )
{
has_c |= x.value == 1000000;
}
bool has_a = false;
bool has_b = false;
bool has_c = false;
};
{
Reader reader;
view.second.for_each( 0, reader );
REQUIRE( reader.has_a );
REQUIRE( reader.has_b );
REQUIRE( reader.has_c );
}
{
Reader reader;
view.second.for_each< AStructMutator, BStruct, CStruct >( 0, reader );
REQUIRE( reader.has_a );
REQUIRE( reader.has_b );
REQUIRE( reader.has_c );
}
{
Reader reader;
view.second.for_each( 1, reader );
REQUIRE_FALSE( reader.has_a );
REQUIRE_FALSE( reader.has_b );
REQUIRE( reader.has_c );
}
{
Reader reader;
view.second.for_each< AStructMutator, BStruct, CStruct >( 1, reader );
REQUIRE_FALSE( reader.has_a );
REQUIRE_FALSE( reader.has_b );
REQUIRE( reader.has_c );
}
{
Reader reader;
view.second.for_each( 2, reader );
REQUIRE( reader.has_a );
REQUIRE_FALSE( reader.has_b );
REQUIRE_FALSE( reader.has_c );
}
{
Reader reader;
view.second.for_each< AStructMutator, BStruct, CStruct >( 2, reader );
REQUIRE( reader.has_a );
REQUIRE_FALSE( reader.has_b );
REQUIRE_FALSE( reader.has_c );
}
}
TEST_CASE( "Test various data overloaded lambda", "[MultiVector]" )
{
auto view = create_view_with_3_items( );
bool has_a = false;
bool has_b = false;
bool has_c = false;
auto reset = [ & ]( ) {
has_a = false;
has_b = false;
has_c = false;
};
auto reader = flatdata::make_overload( [ & ]( AStruct x ) { has_a |= x.value == 8; },
[ & ]( BStruct x ) { has_b |= x.value == 1000; },
[ & ]( CStruct x ) { has_c |= x.value == 1000000; } );
reset( );
view.second.for_each( 0, reader );
REQUIRE( has_a );
REQUIRE( has_b );
REQUIRE( has_c );
reset( );
view.second.for_each( 1, reader );
REQUIRE_FALSE( has_a );
REQUIRE_FALSE( has_b );
REQUIRE( has_c );
reset( );
view.second.for_each( 2, reader );
REQUIRE( has_a );
REQUIRE_FALSE( has_b );
REQUIRE_FALSE( has_c );
}
TEST_CASE( "for_each with const functor", "[MultiVector]" )
{
auto view = create_view_with_3_items( );
// check that it compiles if we pass in const functors
struct NoReader
{
// required for const initialization
NoReader( ) = default;
void
operator( )( const AStruct& ) const
{
}
};
const NoReader no_reader;
view.second.for_each< AStruct >( 0, no_reader );
}
TEST_CASE( "for_each with explicit lambda", "[MultiVector]" )
{
auto view = create_view_with_3_items( );
// also check that lambda works with explicit for_each
bool has_b = false;
view.second.for_each< BStruct >( 0, [ & ]( BStruct x ) { has_b = x.value == 1000; } );
REQUIRE( has_b );
}
TEST_CASE( "for_each with implicit lambda", "[MultiVector]" )
{
auto view = create_view_with_3_items( );
// also check that lambda works with implicit for_each
bool has_b = false;
view.second.for_each( 0, make_overload( [ & ]( BStruct x ) { has_b = x.value == 1000; } ) );
REQUIRE( has_b );
}
TEST_CASE( "Iterate one type elements continuously placed", "[MultiVector]" )
{
auto view = create_view_with_3_items( );
uint64_t index = 0;
auto it = view.second.iterator< AStruct >( index );
REQUIRE( it.valid( ) );
bool has_a1 = ( *it ).value == 7;
++it;
REQUIRE( it.valid( ) );
bool has_a2 = ( *it ).value == 8;
++it;
REQUIRE_FALSE( it.valid( ) );
REQUIRE( has_a1 );
REQUIRE( has_a2 );
}
TEST_CASE( "Iterate one type elements randomly placed", "[MultiVector]" )
{
auto view = create_view_with_3_items( );
uint64_t index = 0;
auto it = view.second.iterator< CStruct >( index );
REQUIRE( it.valid( ) );
bool has_c1 = ( *it ).value == 1230000;
++it;
REQUIRE( it.valid( ) );
bool has_c2 = ( *it ).value == 1000000;
++it;
REQUIRE_FALSE( it.valid( ) );
REQUIRE( has_c1 );
REQUIRE( has_c2 );
}
TEST_CASE( "Flushing while building", "[MultiVector]" )
{
auto view = create_view_with_enough_items_to_flush( );
for ( size_t i = 0; i < NUM_ITEMS_TO_CAUSE_FLUSH; i++ )
{
auto iter = view.second.iterator< CStruct >( i );
REQUIRE( iter.valid( ) );
REQUIRE( ( *iter ).value == i );
iter++;
REQUIRE_FALSE( iter.valid( ) );
}
}
TEST_CASE( "Static test", "[MultiVector]" )
{
auto view = create_view_with_3_items( );
auto accepted_lambda = []( const BStruct& ) {};
auto not_accepted_lambda = []( int ) {};
auto overloded_lambda = make_overload( accepted_lambda, not_accepted_lambda );
static_assert(
std::is_same< decltype( view.second.for_each< SimpleStruct >( 0, accepted_lambda ) ),
std::false_type >::value,
"int is not in the types of the container" );
static_assert(
std::is_same< decltype( view.second.for_each< BStruct >( 0, not_accepted_lambda ) ),
std::false_type >::value,
"lambda does not accept type declared as accepted" );
static_assert( std::is_same< decltype( view.second.for_each<>( 0, not_accepted_lambda ) ),
std::false_type >::value,
"lambda does not accept any type in the container" );
static_assert( std::is_same< decltype( view.second.for_each( 0, not_accepted_lambda ) ),
std::false_type >::value,
"lambda does not accept any type in the container" );
static_assert( std::is_same< decltype( view.second.for_each( 0, accepted_lambda ) ),
std::false_type >::value,
"lambda does not define accepted arguments implicitely and is not callable with "
"all types of the container" );
static_assert( std::is_same< decltype( view.second.for_each( 0, overloded_lambda ) ),
std::false_type >::value,
"lambda accepts a type not in the types of the containers" );
}
TEST_CASE( "Close view is same as storage view", "[MultiVector]" )
{
auto storage = MemoryResourceStorage::create( );
auto vector = storage->create_multi_vector< TestIndexType48, AStruct, BStructMutator, CStruct >(
"data", "foo" );
{
auto list = vector.grow( );
auto a1 = list.add< AStruct >( );
a1.value = 7;
auto a2 = list.add< AStructMutator >( );
a2.value = 8;
auto c1 = list.add< CStruct >( );
c1.value = 1230000;
auto b = list.add< BStruct >( );
b.value = 1000;
auto c2 = list.add< CStruct >( );
c2.value = 1000000;
}
{
auto list = vector.grow( );
auto c = list.add< CStruct >( );
c.value = 1000000;
}
{
auto list = vector.grow( );
auto a = list.add< AStructMutator >( );
a.value = 8;
}
auto view_from_close = vector.close( );
auto view_from_storage
= *storage->read< MultiArrayView< TestIndexType48, AStructMutator, BStruct, CStruct > >(
"data", "foo" );
std::vector< size_t > values_view_from_close;
std::vector< size_t > values_view_from_storage;
REQUIRE( view_from_storage.size( ) == view_from_close.size( ) );
for ( size_t i = 0; i < view_from_close.size( ); ++i )
{
view_from_close.for_each(
i, flatdata::make_overload(
[ & ]( AStruct x ) { values_view_from_close.push_back( x.value ); },
[ & ]( BStruct x ) { values_view_from_close.push_back( x.value ); },
[ & ]( CStruct x ) { values_view_from_close.push_back( x.value ); } ) );
view_from_storage.for_each(
i, flatdata::make_overload(
[ & ]( AStruct x ) { values_view_from_storage.push_back( x.value ); },
[ & ]( BStruct x ) { values_view_from_storage.push_back( x.value ); },
[ & ]( CStruct x ) { values_view_from_storage.push_back( x.value ); } ) );
};
REQUIRE( values_view_from_storage.size( ) == values_view_from_close.size( ) );
for ( size_t i = 0; i < values_view_from_close.size( ); ++i )
{
REQUIRE( values_view_from_storage[ i ] == values_view_from_close[ i ] );
}
}