forked from jbcoe/indirect_value
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindirect_value_test.cpp
1489 lines (1316 loc) · 46.3 KB
/
indirect_value_test.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2019 The Indirect Value Authors. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==============================================================================*/
#include "indirect_value.h"
#include <functional>
#include <string_view>
#include <type_traits>
#include "catch2/catch_test_macros.hpp"
#include "catch2/catch_template_test_macros.hpp"
using isocpp_p1950::bad_indirect_value_access;
using isocpp_p1950::indirect_value;
using isocpp_p1950::make_indirect_value;
using isocpp_p1950::allocate_indirect_value;
// Helper function to write unit tests for self assign.
// Compiler emit the warnings -Wself-assign-overload and -Wself-move
// when assigning a variable to itself. This function avoids these warnings.
template <class T, class U>
void SelfAssign(T& t, U&& u) {
t = std::forward<U>(u);
}
TEST_CASE("Ensure that indirect_value uses the minimum space requirements",
"[indirect_value.sizeof]") {
STATIC_REQUIRE(sizeof(indirect_value<int>) == sizeof(int*));
struct CopyDeleteHybrid { // Same type for copy and delete
void operator()(int* p) { delete p; }
int* operator()(const int& s) { return new int(s); }
};
STATIC_REQUIRE(
sizeof(indirect_value<int, CopyDeleteHybrid, CopyDeleteHybrid>) ==
sizeof(int*));
}
template <typename T>
class copy_counter {
public:
T* operator()(const T& rhs) const {
++call_count;
return isocpp_p1950::default_copy<T>().operator()(rhs);
}
inline static size_t call_count = 0;
};
template <typename T>
class delete_counter {
public:
void operator()(T* rhs) const {
++call_count;
return std::default_delete<T>().operator()(rhs);
}
inline static size_t call_count = 0;
};
TEST_CASE("Default construction for indirect_value", "[constructor.default]") {
GIVEN("An indirect_value value") // The ability to track internal copies and
// deletes of the default constructor")
{
WHEN("Default-constructed") {
indirect_value<int, copy_counter<int>, delete_counter<int>> a{};
REQUIRE(a.operator->() == nullptr);
THEN("Ensure no copies or deletes occur") {
REQUIRE(copy_counter<int>::call_count == 0);
REQUIRE(delete_counter<int>::call_count == 0);
}
}
WHEN("The default-constructed value is destroyed") {
THEN("Ensure no delete operation occurs") {
// Expect a delete not to occur on destruction as the indirect_value was
// default initialised
REQUIRE(copy_counter<int>::call_count == 0);
CHECK(delete_counter<int>::call_count == 0);
}
}
}
GIVEN("An indirect_value value") //"The ability to track internal copies and
// deletes of the default constructor")
{
WHEN(
"We create a default constructed indirect_value then copy assign it to "
"an in-place-constructed "
"indirect_value") {
indirect_value<int, copy_counter<int>, delete_counter<int>> a{};
constexpr int b_value = 10;
indirect_value<int, copy_counter<int>, delete_counter<int>> b{
std::in_place, b_value};
REQUIRE(a.operator->() == nullptr);
REQUIRE(b.operator->() != nullptr);
REQUIRE(*b == b_value);
THEN("Ensure a copy occures but no delete occurs") {
a = b;
REQUIRE(copy_counter<int>::call_count == 1);
REQUIRE(delete_counter<int>::call_count == 0);
}
}
WHEN("The value is destroyed") {
THEN("Ensure a delete operation occurs") {
// Expect both indirect_value object to be deleted on destruction .
REQUIRE(copy_counter<int>::call_count == 1);
CHECK(delete_counter<int>::call_count == 2);
}
}
}
}
TEST_CASE("Element wise initialisation construction for indirect_value",
"[constructor.element_wise]") {
GIVEN("The ability to track intenal copies and deletes") {
size_t copy_count = 0, delete_count = 0;
const auto copy_counter = [©_count](const auto& rhs) {
++copy_count;
return isocpp_p1950::default_copy<
std::remove_cv_t<std::remove_pointer_t<decltype(rhs)>>>()
.
operator()(rhs);
};
const auto delete_counter = [&delete_count](auto* rhs) {
++delete_count;
std::default_delete<
std::remove_cv_t<std::remove_pointer_t<decltype(rhs)>>>()
.
operator()(rhs);
};
WHEN("Constructing objects of indirect_value") {
indirect_value<int, decltype(copy_counter), decltype(delete_counter)> a{
new int(0), copy_counter, delete_counter};
REQUIRE(a.operator->() != nullptr);
THEN(
"Ensure that no copies or deleted happen in the basic construction "
"of a value") {
REQUIRE(copy_count == 0);
REQUIRE(delete_count == 0);
}
}
// Ensure destruction of an indirect_value caused the value to be deleted
REQUIRE(copy_count == 0);
REQUIRE(delete_count == 1);
}
}
TEST_CASE("Copy construction for indirect_value of a primitive type",
"[constructor.copy.primitive]") {
GIVEN("A value-initialised indirect_value value") {
constexpr int a_value = 5;
indirect_value<int> a{std::in_place, a_value};
REQUIRE(*a == a_value);
WHEN("Taking a copy of the value-initialised indirect_value value") {
indirect_value<int> copy_of_a{a};
THEN("The copy is a deep copy of the original value") {
REQUIRE(*copy_of_a == a_value);
REQUIRE(a.operator->() != nullptr);
REQUIRE(copy_of_a.operator->() != nullptr);
REQUIRE(a.operator->() != copy_of_a.operator->());
}
}
}
}
TEST_CASE("Copy assignment for indirect_value of a primitive type",
"[assignment.copy.primitive]") {
GIVEN("A value-initialised indirect_value value") {
constexpr int a_value = 5;
indirect_value<int> a{std::in_place, a_value};
REQUIRE(*a == a_value);
WHEN("Assigning a copy into a default-initalised indirect_value value") {
indirect_value<int> b{};
REQUIRE(b.operator->() == nullptr);
THEN("The assigned to object makes a deep copy of the orginal value") {
b = a;
REQUIRE(*b == a_value);
REQUIRE(a.operator->() != nullptr);
REQUIRE(b.operator->() != nullptr);
REQUIRE(b.operator->() != a.operator->());
}
}
WHEN("Assigning a copy into a value-initalised indirect_value value") {
constexpr int b_value = 10;
indirect_value<int> b{std::in_place, b_value};
REQUIRE(*b == b_value);
THEN("The assigned to object makes a deep copy of the original value") {
b = a;
REQUIRE(*b == a_value);
REQUIRE(a.operator->() != nullptr);
REQUIRE(b.operator->() != nullptr);
REQUIRE(b.operator->() != a.operator->());
}
}
WHEN("Assigning a copy into a pointer-initalised indirect_value value") {
constexpr int b_value = 10;
indirect_value<int> b{new int(b_value)};
REQUIRE(*b == b_value);
THEN("The assigned to object makes a deep copy of the original value") {
b = a;
REQUIRE(*b == a_value);
REQUIRE(a.operator->() != nullptr);
REQUIRE(b.operator->() != nullptr);
REQUIRE(b.operator->() != a.operator->());
}
}
}
}
TEST_CASE("Move construction for indirect_value of a primitive type",
"[constructor.move.primitive]") {
GIVEN("A value-initalised indirect_value value") {
constexpr int a_value = 5;
indirect_value<int> a{std::in_place, a_value};
WHEN("Constucting a new object via moving the orignal value") {
int const* const location_of_a = a.operator->();
indirect_value<int> b{std::move(a)};
THEN(
"The constructed object steals the contents of original value "
"leaving it in a null state") {
REQUIRE(*b == a_value);
REQUIRE(b.operator->() == location_of_a);
REQUIRE(a.operator->() == nullptr);
}
}
}
}
TEST_CASE("Move assignment for indirect_value of a primitive type",
"[assignment.move.primitive]") {
GIVEN("A two value-initialised indirect_value values") {
constexpr int a_value = 5;
constexpr int b_value = 10;
indirect_value<int> a{std::in_place, a_value};
indirect_value<int> b{std::in_place, b_value};
WHEN(
"The contents of the second indirect_value is move assigned to the "
"first") {
int const* const location_of_b = b.operator->();
a = std::move(b);
THEN(
"The move-assigned-to value `a` steals the contents of the second "
"value `b`, leaving that object, `b`, in a null state") {
REQUIRE(*a == b_value);
REQUIRE(a.operator->() == location_of_b);
REQUIRE(b.operator->() == nullptr);
}
}
}
}
TEST_CASE("Operator bool for indirect_value", "[operator.bool]") {
GIVEN("A default-initalised indirect_value value") {
indirect_value<int> a;
WHEN(
"We expect the operator bool to return false as the internal pointer "
"is null") {
REQUIRE(a.operator->() == nullptr);
REQUIRE_FALSE(a);
THEN(
"Then when it is assigned a valid value for operator bool should "
"return true") {
constexpr int b_value = 10;
a = indirect_value<int>{std::in_place, b_value};
REQUIRE(a.operator->() != nullptr);
REQUIRE(*a == b_value);
REQUIRE(a);
}
}
}
GIVEN("A pointer-initialised indirect_value value") {
constexpr int value_a = 7;
indirect_value<int> a{new int(value_a)};
WHEN(
"We expect the operator bool to return true as the internal pointer "
"owns an instance") {
REQUIRE(a.operator->() != nullptr);
REQUIRE(a);
THEN(
"Then when it is assigned a default state value for operator bool "
"should return false") {
a = indirect_value<int>{};
REQUIRE(a.operator->() == nullptr);
REQUIRE_FALSE(a);
}
}
}
}
TEST_CASE("Swap overload for indirect_value", "[swap.primitive]") {
GIVEN(
"A two value-initialised indirect_value values utilising the empty "
"base-class optimisation") {
constexpr int a_value = 5;
constexpr int b_value = 10;
indirect_value<int> a{std::in_place, a_value};
indirect_value<int> b{std::in_place, b_value};
WHEN("The contents are swap") {
swap(a, b);
THEN("The contents of the indirect_value should be moved") {
REQUIRE(*a == b_value);
REQUIRE(*b == a_value);
}
}
}
GIVEN(
"A two value-initialised indirect_value values not using the empty "
"base-class optimisation") {
auto default_copy_lambda_a = [](int original) { return new int(original); };
auto default_copy_lambda_b = [](int original) { return new int(original); };
constexpr int a_value = 5;
constexpr int b_value = 10;
indirect_value<int, decltype(+default_copy_lambda_a),
std::default_delete<int>>
a{new int(a_value), default_copy_lambda_a};
indirect_value<int, decltype(+default_copy_lambda_b),
std::default_delete<int>>
b{new int(b_value), default_copy_lambda_b};
THEN(
"Confirm sized base class is used and its size requirements meet our "
"expectations") {
STATIC_REQUIRE(sizeof(decltype(a)) != sizeof(indirect_value<int>));
STATIC_REQUIRE(sizeof(decltype(b)) != sizeof(indirect_value<int>));
STATIC_REQUIRE(sizeof(decltype(a)) ==
(sizeof(indirect_value<int>) +
sizeof(decltype(+default_copy_lambda_a))));
STATIC_REQUIRE(sizeof(decltype(b)) ==
(sizeof(indirect_value<int>) +
sizeof(decltype(+default_copy_lambda_b))));
}
WHEN("The contents are swapped") {
swap(a, b);
THEN("The contents of the indirect_value should be moved") {
REQUIRE(*a == b_value);
REQUIRE(*b == a_value);
}
}
}
}
TEMPLATE_TEST_CASE("Noexcept of observers", "[TODO]", indirect_value<int>&,
const indirect_value<int>&, indirect_value<int>&&,
const indirect_value<int>&&) {
using T = TestType;
STATIC_REQUIRE(noexcept(std::declval<T>().operator->()));
STATIC_REQUIRE(noexcept(std::declval<T>().operator*()));
STATIC_REQUIRE(!noexcept(std::declval<T>().value()));
STATIC_REQUIRE(noexcept(std::declval<T>().operator bool()));
STATIC_REQUIRE(noexcept(std::declval<T>().has_value()));
STATIC_REQUIRE(noexcept(std::declval<T>().get_copier()));
STATIC_REQUIRE(noexcept(std::declval<T>().get_deleter()));
}
template <class T, class U>
inline constexpr bool same_const_qualifiers =
std::is_const_v<std::remove_reference_t<T>> ==
std::is_const_v<std::remove_reference_t<U>>;
template <class T, class U>
inline constexpr bool same_ref_qualifiers = false;
template <class T, class U>
inline constexpr bool same_ref_qualifiers<T&, U&> = true;
template <class T, class U>
inline constexpr bool same_ref_qualifiers<T&&, U&&> = true;
template <class T, class U>
inline constexpr bool same_const_and_ref_qualifiers =
same_ref_qualifiers<T, U>&& same_const_qualifiers<T, U>;
TEMPLATE_TEST_CASE("Ref- and const-qualifier of observers", "[TODO]",
indirect_value<int>&, const indirect_value<int>&,
indirect_value<int>&&, const indirect_value<int>&&) {
using T = TestType;
STATIC_REQUIRE(
same_const_and_ref_qualifiers<T,
decltype(std::declval<T>().operator*())>);
STATIC_REQUIRE(
same_const_and_ref_qualifiers<T, decltype(std::declval<T>().value())>);
STATIC_REQUIRE(
std::is_same_v<bool, decltype(std::declval<T>().operator bool())>);
STATIC_REQUIRE(std::is_same_v<bool, decltype(std::declval<T>().has_value())>);
STATIC_REQUIRE(
same_const_qualifiers<T, decltype(std::declval<T>().get_copier())>);
STATIC_REQUIRE(
same_const_qualifiers<T, decltype(std::declval<T>().get_deleter())>);
}
TEST_CASE("Test properties of bad_indirect_value_access", "[TODO]") {
bad_indirect_value_access ex;
// check that we can throw a bad_indirect_value_access and catch
// it as const std::exception&.
try {
throw ex;
} catch (const std::exception& e) {
// Use std::string_view to get the correct behavior of operator==.
std::string_view what = e.what();
REQUIRE(what == ex.what());
REQUIRE(what.size() > 0);
}
STATIC_REQUIRE(std::is_base_of_v<std::exception, bad_indirect_value_access>);
STATIC_REQUIRE(
std::is_nothrow_default_constructible_v<bad_indirect_value_access>);
STATIC_REQUIRE(
std::is_nothrow_copy_constructible_v<bad_indirect_value_access>);
STATIC_REQUIRE(noexcept(ex.what()));
}
TEMPLATE_TEST_CASE("Calling value on empty indirect_value will throw",
"[indirect_value.access]",
indirect_value<int>&, const indirect_value<int>&,
indirect_value<int>&&, const indirect_value<int>&&) {
GIVEN("An empty indirect_value") {
std::remove_reference_t<TestType> iv;
THEN("Calling value will throw") {
REQUIRE(!iv.has_value());
REQUIRE_THROWS_AS(std::forward<TestType>(iv).value(),
bad_indirect_value_access);
}
}
}
TEMPLATE_TEST_CASE(
"Calling value on an enganged indirect_value will not throw",
"[indirect_value.access.no_exceptions]",
indirect_value<int>&, const indirect_value<int>&, indirect_value<int>&&,
const indirect_value<int>&&) {
GIVEN("An enganged indirect_value") {
std::remove_reference_t<TestType> iv(std::in_place, 44);
THEN("Calling value will not throw") {
REQUIRE(std::forward<TestType>(iv).has_value());
REQUIRE(std::forward<TestType>(iv).value() == 44);
}
}
}
TEST_CASE("get_copier returns modifiable lvalue reference", "[TODO]") {
GIVEN("An lvalue of indirect_value with a modifiable copier") {
struct Copier {
using deleter_type = std::default_delete<int>;
std::string name;
int* operator()(int x) const {
REQUIRE(name == "Modified");
return new int(x);
}
};
indirect_value<int, Copier> iv(std::in_place, 10);
THEN("Modifying the copier will be observable") {
iv.get_copier().name = "Modified";
REQUIRE(iv.get_copier().name == "Modified");
SelfAssign(iv, iv); // Force invocation of copier
}
}
}
TEST_CASE("get_deleter returns modifiable lvalue reference", "[TODO]") {
GIVEN("An lvalue of indirect_value with a modifiable deleter") {
struct Deleter {
std::string name;
void operator()(int* p) const {
REQUIRE(name == "Modified");
delete p;
}
};
indirect_value<int, isocpp_p1950::default_copy<int>, Deleter> iv(
std::in_place, 10);
THEN("Modifying the deleter will be observable") {
iv.get_deleter().name = "Modified";
REQUIRE(iv.get_deleter().name == "Modified");
}
}
}
struct stats {
inline static int default_ctor_count = 0;
inline static int copy_ctor_count = 0;
inline static int move_ctor_count = 0;
inline static int copy_assign_count = 0;
inline static int move_assign_count = 0;
inline static int copy_operator_count = 0;
inline static int delete_operator_count = 0;
stats() { ++default_ctor_count; }
stats(const stats&) { ++copy_ctor_count; }
stats(stats&&) noexcept { ++move_ctor_count; }
stats& operator=(const stats&) {
++copy_assign_count;
return *this;
}
stats& operator=(stats&&) noexcept {
++move_assign_count;
return *this;
}
template <class T>
T* operator()(const T& t) const {
++copy_operator_count;
return new T(t);
}
template <class T>
void operator()(T* p) const {
delete p;
++delete_operator_count;
}
static void reset() {
default_ctor_count = 0;
copy_ctor_count = 0;
move_ctor_count = 0;
copy_assign_count = 0;
move_assign_count = 0;
copy_operator_count = 0;
delete_operator_count = 0;
}
};
struct EmptyNo_FinalNo : stats {
char data{};
};
struct EmptyNo_FinalYes final : stats {
char data{};
};
struct EmptyYes_FinalNo : stats {};
struct EmptyYes_FinalYes final : stats {};
template <class C, class D>
void TestCopyAndDeleteStats() {
using IV = indirect_value<int, C, D>;
// Tests with an empty IV
stats::reset();
{
IV empty;
auto copyConstructFromEmpty = empty;
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 2);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 0);
stats::reset();
{
IV empty;
auto moveConstructFromEmpty = std::move(empty);
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 2);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 0);
stats::reset();
{
IV empty;
IV copyAssignEmptyFromEmpty;
copyAssignEmptyFromEmpty = empty;
}
REQUIRE(stats::default_ctor_count == 4);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 2);
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 0);
stats::reset();
{
IV empty;
IV moveAssignEmptyFromEmpty;
moveAssignEmptyFromEmpty = std::move(empty);
}
REQUIRE(stats::default_ctor_count == 4);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 2);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 0);
stats::reset();
{
IV empty;
IV copyAssignEngagedFromEmpty(std::in_place);
copyAssignEngagedFromEmpty = empty;
}
REQUIRE(stats::default_ctor_count == 4);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 2);
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 1);
stats::reset();
{
IV empty;
IV moveAssignEngagedFromEmpty(std::in_place);
moveAssignEngagedFromEmpty = std::move(empty);
}
REQUIRE(stats::default_ctor_count == 4);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 2);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 1);
stats::reset();
{
IV empty;
SelfAssign(empty, empty);
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
// Depending on how you implement the protection against self assign
REQUIRE((stats::copy_assign_count == 0 || stats::copy_assign_count == 2));
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 0);
stats::reset();
{
IV empty;
SelfAssign(empty, std::move(empty));
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 0);
// Depending on how you implement the protection against self assign
REQUIRE((stats::move_assign_count == 0 || stats::move_assign_count == 2));
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 0);
stats::reset();
{
IV empty;
swap(empty, empty);
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 2);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 4);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 0);
// Tests with an engaged IV
stats::reset();
{
IV engaged(std::in_place);
auto copyConstructFromEngaged = engaged;
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 2);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 1);
REQUIRE(stats::delete_operator_count == 2);
stats::reset();
{
IV engaged(std::in_place);
auto moveConstructFromEngaged = std::move(engaged);
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 2);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 1);
stats::reset();
{
IV engaged(std::in_place);
IV copyAssignEmptyFromEngaged;
copyAssignEmptyFromEngaged = engaged;
}
REQUIRE(stats::default_ctor_count == 4);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 2);
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 1);
REQUIRE(stats::delete_operator_count == 2);
stats::reset();
{
IV engaged(std::in_place);
IV moveAssignEmptyFromEngaged;
moveAssignEmptyFromEngaged = std::move(engaged);
}
REQUIRE(stats::default_ctor_count == 4);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 2);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 1);
stats::reset();
{
IV engaged(std::in_place);
IV copyAssignEngagedFromEngaged(std::in_place);
copyAssignEngagedFromEngaged = engaged;
}
REQUIRE(stats::default_ctor_count == 4);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 2);
REQUIRE(stats::move_assign_count == 0);
REQUIRE(stats::copy_operator_count == 1);
REQUIRE(stats::delete_operator_count == 3);
stats::reset();
{
IV engaged(std::in_place);
IV moveAssignEngagedFromEngaged(std::in_place);
moveAssignEngagedFromEngaged = std::move(engaged);
}
REQUIRE(stats::default_ctor_count == 4);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 2);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 2);
stats::reset();
{
IV engaged(std::in_place);
SelfAssign(engaged, engaged);
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
// Depending on how you implement the protection against self assign
REQUIRE((stats::copy_assign_count == 0 || stats::copy_assign_count == 2));
REQUIRE(stats::move_assign_count == 0);
// Depending on how you implement the protection against self assign
REQUIRE((stats::copy_operator_count == 0 || stats::copy_operator_count == 1));
REQUIRE(stats::delete_operator_count == stats::copy_operator_count + 1);
stats::reset();
{
IV engaged(std::in_place);
SelfAssign(engaged, std::move(engaged));
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 0);
REQUIRE(stats::copy_assign_count == 0);
// Depending on how you implement the protection against self assign
REQUIRE((stats::move_assign_count == 0 || stats::move_assign_count == 2));
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 1);
stats::reset();
{
IV engaged(std::in_place);
swap(engaged, engaged);
}
REQUIRE(stats::default_ctor_count == 2);
REQUIRE(stats::copy_ctor_count == 0);
REQUIRE(stats::move_ctor_count == 2);
REQUIRE(stats::copy_assign_count == 0);
REQUIRE(stats::move_assign_count == 4);
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 1);
}
TEST_CASE("Stats of copy and delete type", "[TODO]") {
TestCopyAndDeleteStats<EmptyNo_FinalNo, EmptyNo_FinalNo>();
TestCopyAndDeleteStats<EmptyNo_FinalNo, EmptyNo_FinalYes>();
TestCopyAndDeleteStats<EmptyNo_FinalNo, EmptyYes_FinalNo>();
TestCopyAndDeleteStats<EmptyNo_FinalNo, EmptyYes_FinalYes>();
TestCopyAndDeleteStats<EmptyNo_FinalYes, EmptyNo_FinalNo>();
TestCopyAndDeleteStats<EmptyNo_FinalYes, EmptyNo_FinalYes>();
TestCopyAndDeleteStats<EmptyNo_FinalYes, EmptyYes_FinalNo>();
TestCopyAndDeleteStats<EmptyNo_FinalYes, EmptyYes_FinalYes>();
TestCopyAndDeleteStats<EmptyYes_FinalNo, EmptyNo_FinalNo>();
TestCopyAndDeleteStats<EmptyYes_FinalNo, EmptyNo_FinalYes>();
TestCopyAndDeleteStats<EmptyYes_FinalNo, EmptyYes_FinalNo>();
TestCopyAndDeleteStats<EmptyYes_FinalNo, EmptyYes_FinalYes>();
TestCopyAndDeleteStats<EmptyYes_FinalYes, EmptyNo_FinalNo>();
TestCopyAndDeleteStats<EmptyYes_FinalYes, EmptyNo_FinalYes>();
TestCopyAndDeleteStats<EmptyYes_FinalYes, EmptyYes_FinalNo>();
TestCopyAndDeleteStats<EmptyYes_FinalYes, EmptyYes_FinalYes>();
}
TEST_CASE("Protection against reentrancy", "[TODO]") {
// There are currently three situations in which an engaged indirect_value
// will destory its held value:
// 1. Copy assignment operator
// 2. Move assignment operator
// 3. Destructor
// This test ensures that when these functions invoke the
// destructor of the held value, the indirect_value will already
// be set to null.
struct Reentrance {
indirect_value<Reentrance>* backReference{};
~Reentrance() { REQUIRE(backReference->has_value() == false); }
};
// Test the destructor.
{
indirect_value<Reentrance> iv(std::in_place);
iv->backReference = &iv;
}
// Test the copy-assignment operator (and destructor).
{
indirect_value<Reentrance> iv(std::in_place);
iv->backReference = &iv;
indirect_value<Reentrance> copyAssigned(std::in_place);
copyAssigned->backReference = ©Assigned;
copyAssigned = iv;
copyAssigned->backReference = ©Assigned;
}
// Test the move-assignment operator (and destructor).
{
indirect_value<Reentrance> iv(std::in_place);
iv->backReference = &iv;
indirect_value<Reentrance> moveAssigned(std::in_place);
moveAssigned->backReference = &moveAssigned;
moveAssigned = std::move(iv);
moveAssigned->backReference = &moveAssigned;
}
}
TEST_CASE("Self assign an indirect_value", "[TODO]") {
{
stats::reset();
indirect_value<int, stats, stats> empty;
SelfAssign(empty, empty);
REQUIRE(!empty);
SelfAssign(empty, std::move(empty));
REQUIRE(!empty);
}
REQUIRE(stats::copy_operator_count == 0);
REQUIRE(stats::delete_operator_count == 0);
{
stats::reset();
indirect_value<int, stats, stats> engaged(std::in_place, 34);
SelfAssign(engaged, engaged);
REQUIRE(engaged);
REQUIRE(*engaged == 34);
int* const address = &*engaged;
SelfAssign(engaged, std::move(engaged));
REQUIRE(engaged);
REQUIRE(address == &*engaged);
}
REQUIRE((stats::copy_operator_count == 0 || stats::copy_operator_count == 1));
REQUIRE(stats::delete_operator_count == stats::copy_operator_count + 1);
}
struct CopyConstructorThrows {
CopyConstructorThrows() = default;
CopyConstructorThrows(const CopyConstructorThrows&) { throw 0; }
int id{};
};
struct CopyWithID : isocpp_p1950::default_copy<CopyConstructorThrows> {
int id{};
};
struct DeleteWithID : std::default_delete<CopyConstructorThrows> {
int id{};
};
TEST_CASE("Throwing copy constructor", "[TODO]") {
GIVEN("Two engaged indirect_value values") {
indirect_value<CopyConstructorThrows, CopyWithID, DeleteWithID> iv(
std::in_place);
iv->id = 1;
iv.get_copier().id = 10;
iv.get_deleter().id = 100;
indirect_value<CopyConstructorThrows, CopyWithID, DeleteWithID> other(
std::in_place);
other->id = 2;
other.get_copier().id = 20;
other.get_deleter().id = 200;
THEN("A throwing copy constructor should not change the objects") {
REQUIRE_THROWS_AS(iv = other, int);
REQUIRE(iv->id == 1);
REQUIRE(iv.get_copier().id == 10);
REQUIRE(iv.get_deleter().id == 100);
REQUIRE(other->id == 2);
REQUIRE(other.get_copier().id == 20);
REQUIRE(other.get_deleter().id == 200);
}
}
}
struct CopierWithCallback {
std::function<void()> callback;
CopierWithCallback() = default;
// Intentionally don't copy callback
CopierWithCallback(const CopierWithCallback&) {}
CopierWithCallback& operator=(const CopierWithCallback&) { return *this; }
template <class T>
T* operator()(const T& t) const {
REQUIRE(callback);
callback();
return new T(t);
}
};
template <>
struct isocpp_p1950::copier_traits<CopierWithCallback> {
using deleter_type = std::default_delete<int>;
};
TEST_CASE("Use source copier when copying", "[TODO]") {
GIVEN("An engaged indirect_value with CopierWithCallback") {
indirect_value<int, CopierWithCallback> engagedSource(std::in_place);
int copyCounter = 0;
engagedSource.get_copier().callback = [©Counter]() mutable {
++copyCounter;
};
THEN("Coping will call engagedSources copier") {
REQUIRE(copyCounter == 0);
indirect_value<int, CopierWithCallback> copy(engagedSource);
REQUIRE(copyCounter == 1);
indirect_value<int, CopierWithCallback> emptyAssignee;
emptyAssignee = engagedSource;
REQUIRE(copyCounter == 2);
indirect_value<int, CopierWithCallback> engagedAssignee(std::in_place);
engagedAssignee = engagedSource;
REQUIRE(copyCounter == 3);
}
}
}