-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdelegate_ut.cpp
594 lines (544 loc) · 16.9 KB
/
delegate_ut.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#define DELEGATE_ARGS_SIZE 24
#define DELEGATE_ARGS_ALIGN 8
#include "delegate/delegate.h"
#ifdef WIN32
#define DO_NOT_USE_WMAIN
#define CATCH_CONFIG_WINDOWS_CRTDBG
#endif
#define CATCH_CONFIG_RUNNER
#define CATCH_CONFIG_FAST_COMPILE
#include "catch2/catch.hpp"
namespace StaticFixture
{
static bool ran = false;
static int in = 0;
static void init()
{
ran = false;
in = 0;
}
static void func_void()
{
ran = true;
}
static int func_int()
{
ran = true;
return 17;
}
static void func_void_int(int i)
{
ran = true;
in = i;
}
static int func_int_int(int i)
{
ran = true;
in = i;
return 101 + i;
}
};
class ClassFixture
{
public:
bool ran;
int in;
static int construct_count;
static int destruct_count;
ClassFixture() :
ran(false),
in(0)
{
construct_count++;
}
ClassFixture(const ClassFixture &other)
{
construct_count++;
if (this == &other)
{
return;
}
this->ran = other.ran;
this->in = other.in;
}
~ClassFixture()
{
destruct_count++;
}
static void reset_counts()
{
construct_count = 0;
destruct_count = 0;
}
void func_void()
{
ran = true;
}
int func_int()
{
ran = true;
return 17;
}
void func_void_int(int i)
{
ran = true;
in = i;
}
int func_int_int(int i)
{
ran = true;
in = i;
return 101 + i;
}
};
int ClassFixture::construct_count = 0;
int ClassFixture::destruct_count = 0;
/** Test nove-only classes using smart pointers. */
TEST_CASE("Smart pointers", "[smart_pointer]")
{
ClassFixture::reset_counts();
SECTION("Assign")
{
{
std::unique_ptr<ClassFixture> cf(new ClassFixture);
delegate::MoveDelegate<int, int> test([cf = std::move(cf)](int i)
{
return cf->func_int_int(i);
});
REQUIRE(!!test == true);
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(test(1234) == 1335);
REQUIRE(ClassFixture::destruct_count == 0);
}
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 1);
}
SECTION("Functor Move")
{
{
delegate::MoveDelegate<int, int> test;
REQUIRE(!!test == false);
{
std::unique_ptr<ClassFixture> cf(new ClassFixture);
auto f = [cf = std::move(cf)](int i)
{
return cf->func_int_int(i);
};
test = std::move(f);
REQUIRE(!!test == true);
}
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(test(1234) == 1335);
REQUIRE(ClassFixture::destruct_count == 0);
}
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 1);
}
SECTION("Delegate Move")
{
{
delegate::MoveDelegate<int, int> test;
REQUIRE(!!test == false);
{
std::unique_ptr<ClassFixture> cf(new ClassFixture);
auto f = [cf = std::move(cf)](int i)
{
return cf->func_int_int(i);
};
test = std::move(f);
REQUIRE(!!test == true);
delegate::MoveDelegate<int, int> temp(std::move(test));
REQUIRE(!!test == false);
REQUIRE(!!temp == true);
REQUIRE(temp(1234) == 1335);
REQUIRE(ClassFixture::destruct_count == 0);
}
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 1);
}
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 1);
}
}
/** Show that delegates can indeed store up to their maximum size. */
TEST_CASE("Storage Test", "[storage_test]")
{
uint32_t param0 = 111;
uint32_t param1 = 222;
uint32_t param2 = 333;
uint32_t param3 = 444;
uint32_t param4 = 555;
uint32_t param5 = 666;
static uint32_t sparam0 = 0;
static uint32_t sparam1 = 0;
static uint32_t sparam2 = 0;
static uint32_t sparam3 = 0;
static uint32_t sparam4 = 0;
static uint32_t sparam5 = 0;
auto lambda1 = [](){};
auto lambda4 = [param0](){sparam0 = param0;};
auto lambda8 = [param0, param1](){sparam0 = param0; sparam1 = param1;};
auto lambda12 = [param0, param1, param2](){sparam0 = param0; sparam1 = param1; sparam2 = param2;};
auto lambda16 = [param0, param1, param2, param3]()
{
sparam0 = param0; sparam1 = param1; sparam2 = param2; sparam3 = param3;
};
auto lambda20 = [param0, param1, param2, param3, param4]()
{
sparam0 = param0; sparam1 = param1; sparam2 = param2; sparam3 = param3; sparam4 = param4;
};
auto lambda24 = [param0, param1, param2, param3, param4, param5]()
{
sparam0 = param0; sparam1 = param1; sparam2 = param2; sparam3 = param3; sparam4 = param4; sparam5 = param5;
};
static_assert(sizeof(lambda1) == 1, "A capture-less lambda is assumed to be one byte");
static_assert(sizeof(lambda4) == 4, "A 4-byte capture should result in a 4-byte lambda");
static_assert(sizeof(lambda8) == 8, "A 8-byte capture should result in a 8-byte lambda");
static_assert(sizeof(lambda12) == 12, "A 12-byte capture should result in a 12-byte lambda");
static_assert(sizeof(lambda16) == 16, "A 16-byte capture should result in a 16-byte lambda");
static_assert(sizeof(lambda20) == 20, "A 20-byte capture should result in a 20-byte lambda");
static_assert(sizeof(lambda24) == 24, "A 24-byte capture should result in a 24-byte lambda");
static_assert(sizeof(delegate::FunctorArgs) == 24, "This test assumes 24-bytes of storage");
delegate::Delegate<void> delegate1 = lambda1;
delegate1();
REQUIRE(sparam0 == 0);
REQUIRE(sparam1 == 0);
REQUIRE(sparam2 == 0);
REQUIRE(sparam3 == 0);
REQUIRE(sparam4 == 0);
REQUIRE(sparam5 == 0);
delegate::Delegate<void> delegate4 = lambda4;
delegate4();
REQUIRE(sparam0 == 111);
REQUIRE(sparam1 == 0);
REQUIRE(sparam2 == 0);
REQUIRE(sparam3 == 0);
REQUIRE(sparam4 == 0);
REQUIRE(sparam5 == 0);
sparam0 = 0;
delegate::Delegate<void> delegate8 = lambda8;
delegate8();
REQUIRE(sparam0 == 111);
REQUIRE(sparam1 == 222);
REQUIRE(sparam2 == 0);
REQUIRE(sparam3 == 0);
REQUIRE(sparam4 == 0);
REQUIRE(sparam5 == 0);
sparam0 = 0;
sparam1 = 0;
delegate::Delegate<void> delegate12 = lambda12;
delegate12();
REQUIRE(sparam0 == 111);
REQUIRE(sparam1 == 222);
REQUIRE(sparam2 == 333);
REQUIRE(sparam3 == 0);
REQUIRE(sparam4 == 0);
REQUIRE(sparam5 == 0);
sparam0 = 0;
sparam1 = 0;
sparam2 = 0;
delegate::Delegate<void> delegate16 = lambda16;
delegate16();
REQUIRE(sparam0 == 111);
REQUIRE(sparam1 == 222);
REQUIRE(sparam2 == 333);
REQUIRE(sparam3 == 444);
REQUIRE(sparam4 == 0);
REQUIRE(sparam5 == 0);
sparam0 = 0;
sparam1 = 0;
sparam2 = 0;
sparam3 = 0;
delegate::Delegate<void> delegate20 = lambda20;
delegate20();
REQUIRE(sparam0 == 111);
REQUIRE(sparam1 == 222);
REQUIRE(sparam2 == 333);
REQUIRE(sparam3 == 444);
REQUIRE(sparam4 == 555);
REQUIRE(sparam5 == 0);
sparam0 = 0;
sparam1 = 0;
sparam2 = 0;
sparam3 = 0;
sparam4 = 0;
delegate::Delegate<void> delegate24 = lambda24;
delegate24();
REQUIRE(sparam0 == 111);
REQUIRE(sparam1 == 222);
REQUIRE(sparam2 == 333);
REQUIRE(sparam3 == 444);
REQUIRE(sparam4 == 555);
REQUIRE(sparam5 == 666);
}
/** Test constructors and destructors and that delegate copies work correctly. */
TEST_CASE("Construct / Destruct", "[construct_destruct]")
{
SECTION("Construct / Destruct / Same Copy")
{
ClassFixture::reset_counts();
{
static bool state_ran = false;
static int state_in = 0;
/*
* This fixture will be created and destroyed, the temporary lambda assigned to f will also be created and
* destroyed, taking its copy of ClassFixture through a cycle, and the placement new in the constructor for
* the delegate will also create a lambda, but there are only destructor calls for full type, and copies
* only call constructors for full types.
*/
ClassFixture fixture;
delegate::Delegate<int, bool, int> f = [fixture](const bool dump_state, const int i) mutable
{
if(dump_state)
{
state_ran = fixture.ran;
state_in = fixture.in;
return 0;
}
else
{
return fixture.func_int_int(i);
}
};
REQUIRE(ClassFixture::construct_count == 3);
REQUIRE(ClassFixture::destruct_count == 1);
REQUIRE(f(false, 123) == 224);
REQUIRE(state_ran == false);
REQUIRE(state_in == 0);
REQUIRE(f(true, 123) == 0);
REQUIRE(state_ran == true);
REQUIRE(state_in == 123);
/*
* Make a new delegate and copy over the one above. No fixture constructors or destructors are called.
*/
delegate::Delegate<int, bool, int> f_copy = f;
REQUIRE(f_copy(true, 987) == 0);
REQUIRE(state_ran == true);
REQUIRE(state_in == 123);
delegate::Delegate<int, bool, int> fff;
/*
* The new delegate keeps chugging along.
*/
REQUIRE(f_copy(false, 555) == 656);
/*
* The original delegate is unaffected.
*/
REQUIRE(f(true, 123) == 0);
REQUIRE(state_ran == true);
REQUIRE(state_in == 123);
REQUIRE(ClassFixture::construct_count == 4);
REQUIRE(ClassFixture::destruct_count == 1);
/*
* Perform a copy assignemnt.
*/
delegate::Delegate<int, bool, int> f_copy_assign;
f_copy_assign = f;
}
/*
* f_copy_assign was assigned f, which means a new f is constructed into f_copy_assign.
*/
REQUIRE(ClassFixture::construct_count == 5);
REQUIRE(ClassFixture::destruct_count == 5);
}
}
/** Test trivial functions (no captures). */
TEST_CASE("Trivial Function", "[trivial_function]")
{
SECTION("void")
{
StaticFixture::init();
delegate::Delegate<void> f(&StaticFixture::func_void);
f();
REQUIRE(StaticFixture::ran == true);
REQUIRE(StaticFixture::in == 0);
}
SECTION("int")
{
StaticFixture::init();
delegate::Delegate<int> f(&StaticFixture::func_int);
REQUIRE(f() == 17);
REQUIRE(StaticFixture::ran == true);
REQUIRE(StaticFixture::in == 0);
}
SECTION("void int")
{
StaticFixture::init();
delegate::Delegate<void, int> f(&StaticFixture::func_void_int);
f(21);
REQUIRE(StaticFixture::ran == true);
REQUIRE(StaticFixture::in == 21);
}
SECTION("int int")
{
StaticFixture::init();
delegate::Delegate<int, int> f(&StaticFixture::func_int_int);
REQUIRE(f(33) == 134);
REQUIRE(StaticFixture::ran == true);
REQUIRE(StaticFixture::in == 33);
}
}
/** Test class functions (implicit this captures and explicit captures). */
TEST_CASE("Class Function", "[class_function]")
{
SECTION("void")
{
ClassFixture fixture;
delegate::Delegate<void> f(std::bind(&ClassFixture::func_void, &fixture));
f();
REQUIRE(fixture.ran == true);
REQUIRE(fixture.in == 0);
}
SECTION("int")
{
ClassFixture fixture;
delegate::Delegate<int> f(std::bind(&ClassFixture::func_int, &fixture));
REQUIRE(f() == 17);
REQUIRE(fixture.ran == true);
REQUIRE(fixture.in == 0);
}
SECTION("void int")
{
ClassFixture fixture;
delegate::Delegate<void, int> f(std::bind(&ClassFixture::func_void_int,
&fixture,
std::placeholders::_1));
f(21);
REQUIRE(fixture.ran == true);
REQUIRE(fixture.in == 21);
}
SECTION("int int")
{
ClassFixture fixture;
delegate::Delegate<int, int> f(std::bind(&ClassFixture::func_int_int,
&fixture,
std::placeholders::_1));
REQUIRE(f(33) == 134);
REQUIRE(fixture.ran == true);
REQUIRE(fixture.in == 33);
}
}
/** Test lambda functions. */
TEST_CASE("Lambda", "[lambda]")
{
SECTION("void")
{
ClassFixture fixture;
delegate::Delegate<void> f([&fixture](){fixture.func_void();});
f();
REQUIRE(fixture.ran == true);
REQUIRE(fixture.in == 0);
}
SECTION("int")
{
ClassFixture fixture;
delegate::Delegate<int> f([&fixture](){return fixture.func_int();});
REQUIRE(f() == 17);
REQUIRE(fixture.ran == true);
REQUIRE(fixture.in == 0);
}
SECTION("void int")
{
ClassFixture fixture;
delegate::Delegate<void, int> f([&fixture](int i){fixture.func_void_int(i);});
f(21);
REQUIRE(fixture.ran == true);
REQUIRE(fixture.in == 21);
}
SECTION("int int")
{
ClassFixture fixture;
delegate::Delegate<int, int> f([&fixture](int i){return fixture.func_int_int(i);});
REQUIRE(f(33) == 134);
REQUIRE(fixture.ran == true);
REQUIRE(fixture.in == 33);
}
}
/** Test uninitialized delegates. */
TEST_CASE("Trivial Function Default", "[trivial_function_default]")
{
SECTION("default void")
{
delegate::Delegate<void> f;
REQUIRE(!!f == false);
}
SECTION("trivial default int")
{
delegate::Delegate<int> f;
REQUIRE(!!f == false);
}
SECTION("default void int")
{
delegate::Delegate<void, int> f;
REQUIRE(!!f == false);
}
SECTION("default int int")
{
delegate::Delegate<int, int> f;
REQUIRE(!!f == false);
}
}
/** Test type forwarding delegates. */
TEST_CASE("Forwarding", "[forwarding]")
{
SECTION("non-reference type to non-reference argument")
{
ClassFixture::reset_counts();
{
delegate::Delegate<void, ClassFixture> f([](ClassFixture fixture){fixture.func_void();});
ClassFixture fixture;
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 0);
f(fixture);
REQUIRE(ClassFixture::construct_count == 3);
REQUIRE(ClassFixture::destruct_count == 2);
}
}
SECTION("non-reference type to rvalue reference argument")
{
ClassFixture::reset_counts();
{
delegate::Delegate< void, ClassFixture> f([](ClassFixture&& fixture){fixture.func_void();});
ClassFixture fixture;
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 0);
f(fixture);
REQUIRE(ClassFixture::construct_count == 2);
REQUIRE(ClassFixture::destruct_count == 1);
}
}
SECTION("rvalue reference type to non-reference argument")
{
ClassFixture::reset_counts();
{
delegate::Delegate<void, ClassFixture&&> f([](ClassFixture fixture){fixture.func_void();});
ClassFixture fixture;
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 0);
f(std::move(fixture));
REQUIRE(ClassFixture::construct_count == 2);
REQUIRE(ClassFixture::destruct_count == 1);
}
}
SECTION("rvalue reference type to rvalue reference argument")
{
ClassFixture::reset_counts();
{
delegate::Delegate<void, ClassFixture&&> f([](ClassFixture&& fixture){fixture.func_void();});
ClassFixture fixture;
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 0);
f(std::move(fixture));
REQUIRE(ClassFixture::construct_count == 1);
REQUIRE(ClassFixture::destruct_count == 0);
}
}
}
void intf(int i) {printf("intf: %d\n", i);};
int main(int, char*[])
{
Catch::Session session;
Catch::ConfigData config_data;
session.useConfigData(config_data);
session.run();
return 0;
}