-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
copyable_unique_ptr_test.cc
1187 lines (1046 loc) · 46.7 KB
/
copyable_unique_ptr_test.cc
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
#include "drake/common/copyable_unique_ptr.h"
#include <memory>
#include <regex>
#include <sstream>
#include <gtest/gtest.h>
#include "drake/common/is_cloneable.h"
#include "drake/common/test_utilities/is_dynamic_castable.h"
#include "drake/common/unused.h"
namespace drake {
namespace {
using std::make_unique;
using std::move;
using std::regex;
using std::regex_match;
using std::stringstream;
using std::unique_ptr;
// Convenience alias to keep the test code lines shorter.
template <typename T>
using cup = copyable_unique_ptr<T>;
// -------------------- copy functionality tests ------------------------
// These tests cover the actual copyable semantics. Confirming that the
// protocol selecting between Clone and copy constructor is consistent with the
// definition and that the implementation invokes the correct copy mechanism
// and, when neither exists, recognizes an uncopyable class.
// These tests implicitly test copyable_unique_ptr's copy constructor.
// Indicator of where a particular instance came from.
enum class Origin {
UNINITIALIZED,
CONSTRUCT,
COPY,
CLONE
};
// A simple base class that stores a value and a construction origin. This only
// has a *protected* copy constructor. Child classes can invoke it, but it
// won't be considered copyable.
struct Base {
explicit Base(int v, Origin org = Origin::CONSTRUCT)
: origin(org), value(v) {}
virtual ~Base() {}
Origin origin{Origin::UNINITIALIZED};
int value{-1};
protected:
Base(const Base& b) : origin(Origin::COPY), value(b.value) {}
};
// A class that is copyable, but has no Clone method.
struct CopyOnly : Base {
explicit CopyOnly(int v, Origin org = Origin::CONSTRUCT) : Base(v, org) {}
CopyOnly(const CopyOnly& b) : Base(b) {}
};
// Confirms that a class with only a public copy constructor is considered
// copyable and copies appropriately.
GTEST_TEST(CopyableUniquePtrTest, CopyOnlySuccess) {
cup<CopyOnly> ptr(new CopyOnly(1));
EXPECT_EQ(ptr->origin, Origin::CONSTRUCT);
cup<CopyOnly> copy(ptr);
EXPECT_EQ(copy->origin, Origin::COPY);
EXPECT_EQ(copy->value, ptr->value);
++copy->value;
EXPECT_NE(copy->value, ptr->value);
}
// This class has a private copy constructor but declares copyable_unique_ptr
// to be a friend so it should still be copyable.
class FriendsWithBenefitsCopy {
public:
explicit FriendsWithBenefitsCopy(int value) : value_(value) {}
int value() const {return value_;}
private:
friend class copyable_unique_ptr<FriendsWithBenefitsCopy>;
FriendsWithBenefitsCopy(const FriendsWithBenefitsCopy&) = default;
int value_{0};
};
// This class has no copy constructor and a private Clone() method but declares
// copyable_unique_ptr to be a friend so it should still be copyable.
class FriendsWithBenefitsClone {
public:
explicit FriendsWithBenefitsClone(int value) : value_(value) {}
int value() const {return value_;}
FriendsWithBenefitsClone(const FriendsWithBenefitsClone&) = delete;
private:
friend class copyable_unique_ptr<FriendsWithBenefitsClone>;
std::unique_ptr<FriendsWithBenefitsClone> Clone() const {
return std::make_unique<FriendsWithBenefitsClone>(value_);
}
int value_{0};
};
// Check that a friend declaration works to get copyable_unique_ptr access
// to the needed copy constructor or Clone() method.
GTEST_TEST(CopyableUniquePtrTest, FriendsWithBenefits) {
copyable_unique_ptr<FriendsWithBenefitsCopy> fwb(
new FriendsWithBenefitsCopy(10));
EXPECT_EQ(fwb->value(), 10);
copyable_unique_ptr<FriendsWithBenefitsCopy> fwb2(fwb);
EXPECT_EQ(fwb2->value(), 10);
copyable_unique_ptr<FriendsWithBenefitsClone> fwbc(
new FriendsWithBenefitsClone(20));
EXPECT_EQ(fwbc->value(), 20);
copyable_unique_ptr<FriendsWithBenefitsClone> fwbc2(fwbc);
EXPECT_EQ(fwbc2->value(), 20);
}
// A fully copyable class (has both copy constructor and Clone). Confirms that
// in the presence of both, the copy constructor is preferred.
struct FullyCopyable : Base {
explicit FullyCopyable(int v, Origin org = Origin::CONSTRUCT)
: Base(v, org) {}
FullyCopyable(const FullyCopyable& b) : Base(b) {}
unique_ptr<FullyCopyable> Clone() const {
return make_unique<FullyCopyable>(value, Origin::CLONE);
}
};
// Confirms that the copy constructor is preferred when both exist.
GTEST_TEST(CopyableUniquePtrTest, FullyCopyableSuccess) {
cup<FullyCopyable> ptr(new FullyCopyable(1));
EXPECT_EQ(ptr->origin, Origin::CONSTRUCT);
cup<FullyCopyable> copy(ptr);
EXPECT_EQ(copy->origin, Origin::COPY);
EXPECT_EQ(copy->value, ptr->value);
++copy->value;
EXPECT_NE(copy->value, ptr->value);
}
// A class with only a Clone method.
struct CloneOnly : Base {
explicit CloneOnly(int v, Origin org = Origin::CONSTRUCT) : Base(v, org) {}
unique_ptr<CloneOnly> Clone() const {
return unique_ptr<CloneOnly>(DoClone());
}
protected:
CloneOnly(const CloneOnly& other) : Base(other.value) {}
[[nodiscard]] virtual CloneOnly* DoClone() const {
return new CloneOnly(value, Origin::CLONE);
}
};
// Confirms that a class that has a proper implementation of Clone, but no
// copy constructor clones itself.
GTEST_TEST(CopyableUniquePtrTest, CloneOnlySuccess) {
cup<CloneOnly> ptr(new CloneOnly(1));
EXPECT_EQ(ptr->origin, Origin::CONSTRUCT);
cup<CloneOnly> copy(ptr);
EXPECT_EQ(copy->origin, Origin::CLONE);
EXPECT_EQ(copy->value, ptr->value);
copy->value++;
EXPECT_NE(copy->value, ptr->value);
}
// A class that derives from a copyable class with no copy constructor. It
// provides a Clone method. This can be assigned to a cup<CloneOnly> *and*
// supports the cup<CloneOnlyChildWithClone> specialization. Copying an instance
// of cup<CloneOnly> which contains a reference to this will produce an instance
// of CloneOnlyChildWithClone.
struct CloneOnlyChildWithClone : CloneOnly {
explicit CloneOnlyChildWithClone(int v, Origin org = Origin::CONSTRUCT)
: CloneOnly(v, org) {}
CloneOnlyChildWithClone(const CloneOnlyChildWithClone&) = delete;
unique_ptr<CloneOnlyChildWithClone> Clone() const {
return unique_ptr<CloneOnlyChildWithClone>(DoClone());
}
protected:
[[nodiscard]] CloneOnlyChildWithClone* DoClone() const override {
return new CloneOnlyChildWithClone(value, Origin::CLONE);
}
};
// A class that derives from a copyable class with no copy constructor. It
// provides a copy constructor. This can be assigned to a cup<CloneOnly> *and*
// supports the cup<CloneOnlyWithCopy> specialization. It also overrides the
// protected DoClone method. Copying an instance of cup<CloneOnly> which
// contains a reference to this will produce a copy of
// CloneOnlyChildWithCopyVClone.
struct CloneOnlyChildWithCopyVClone : CloneOnly {
explicit CloneOnlyChildWithCopyVClone(int v, Origin org = Origin::CONSTRUCT)
: CloneOnly(v, org) {}
CloneOnlyChildWithCopyVClone(const CloneOnlyChildWithCopyVClone&) = default;
protected:
[[nodiscard]] CloneOnlyChildWithCopyVClone* DoClone() const override {
return new CloneOnlyChildWithCopyVClone(value, Origin::CLONE);
}
};
// A class that derives from a copyable class with no copy constructor. It
// provides a copy constructor. This can be assigned to a cup<CloneOnly> *and*
// supports the cup<CloneOnlyWithCopy> specialization. This does *not* override
// the protected DoClone() method. Copying an instance of cup<CloneOnly> which
// contains a reference to this will produce a type-sliced copy of CloneOnly.
struct CloneOnlyChildWithCopy : CloneOnly {
explicit CloneOnlyChildWithCopy(int v, Origin org = Origin::CONSTRUCT)
: CloneOnly(v, org) {}
CloneOnlyChildWithCopy(const CloneOnlyChildWithCopy&) = default;
};
// A class that derives from a copyable class with no copy constructor. It
// provides no copy functions. This can be assigned to a cup<CloneOnly> but does
// *not* support the cup<CloneOnlyChildUncopyable> specialization. Copying an
// instance of cup<CloneOnly> which contains a reference to this will produce a
// type-sliced copy of CloneOnly.
struct CloneOnlyChildUncopyable : CloneOnly {
explicit CloneOnlyChildUncopyable(int v, Origin org = Origin::CONSTRUCT)
: CloneOnly(v, org) {}
CloneOnlyChildUncopyable(const CloneOnlyChildUncopyable&) = delete;
};
// A class that derives from a copyable class *with* a copy constructor. It
// provides its own copy constructor. This can be assigned to a cup<CopyChild>
// and supports the cup<CopyChild> specialization. Copying an instance of
// cup<FullyCopyable> which contains a reference to a CopyChild will produce a
// type-sliced copy of FullyCopyable.
struct CopyChild : public FullyCopyable {
explicit CopyChild(int v) : FullyCopyable(v) {}
CopyChild(const CopyChild& c) = default;
};
// Tests the copyability of derived class of a copyable class. In this case,
// the base class has *only* a Clone method and no copy constructor. Just
// because the base class is copyable does not imply the child is copyable.
GTEST_TEST(CopyableUniquePtrTest, PolymorphicCopyability) {
// FYI is_cloneable and is_copy_constructible look only for public methods
// which is a more stringent condition that copyable_unique_ptr enforces.
// Below we'll verify that the presence of an appropriate public method *does*
// enable us to use copyable_unique_ptr, by creating one of the appropriate
// type and then marking it "unused" to avoid warnings. If this test compiles
// then we succeeded.
// Case 1) Child with *only* Clone method.
EXPECT_TRUE(is_cloneable<CloneOnlyChildWithClone>::value);
EXPECT_FALSE(std::is_copy_constructible<CloneOnlyChildWithClone>::value);
copyable_unique_ptr<CloneOnlyChildWithClone> ptr_1;
// Case 2) Child with *only* Copy method but virtual DoClone().
EXPECT_FALSE(is_cloneable<CloneOnlyChildWithCopyVClone>::value);
EXPECT_TRUE(std::is_copy_constructible<CloneOnlyChildWithCopyVClone>::value);
copyable_unique_ptr<CloneOnlyChildWithCopyVClone> ptr_2;
// Case 3) Child with *only* Copy method.
EXPECT_FALSE(is_cloneable<CloneOnlyChildWithCopy>::value);
EXPECT_TRUE(std::is_copy_constructible<CloneOnlyChildWithCopy>::value);
copyable_unique_ptr<CloneOnlyChildWithCopy> ptr_3;
// Case 4) Child with no copy and no clone.
EXPECT_FALSE(is_cloneable<CloneOnlyChildUncopyable>::value);
EXPECT_FALSE(std::is_copy_constructible<CloneOnlyChildUncopyable>::value);
// Can't make a cloneable_unique_ptr<CloneOnlyChildUncopyable>.
// Case 5) Child with copy, derived from base with copy.
EXPECT_FALSE(is_cloneable<CopyChild>::value);
EXPECT_TRUE(std::is_copy_constructible<CopyChild>::value);
copyable_unique_ptr<CopyChild> ptr_4;
unused(ptr_1, ptr_2, ptr_3, ptr_4);
}
// Utility test for the CopyTypeSlicing test. It is templated on the sub-class
// of CloneOnly. Various implementations in the derived class can lead to
// copies that slice the type back to CloneOnly.
template <typename T>
void TestPolymorphicCopy(bool copy_success) {
static_assert(std::is_convertible<T*, CloneOnly*>::value,
"This utility method can only be used with classes derived "
"from CloneOnly.");
cup<CloneOnly> src(new T(1));
cup<CloneOnly> tgt;
tgt = src; // Triggers a copy
EXPECT_NE(tgt.get(), nullptr); // Confirm actual object assigned.
EXPECT_NE(tgt.get(), src.get()); // Confirm different objects.
if (copy_success) {
EXPECT_TRUE(is_dynamic_castable<T>(tgt.get()));
} else {
EXPECT_TRUE((std::is_same<const CloneOnly*, decltype(tgt.get())>::value));
EXPECT_FALSE(is_dynamic_castable<T>(tgt.get()));
}
}
// Tests the copy functionality based on polymorphism. Given a
// copyable_unique_ptr on a base class, various concrete derived instances are
// pushed into the pointer and copied. Some derived classes will be type
// sliced and some will have their type preserved. This confirms the behavior.
GTEST_TEST(CopyableUniquePtrTest, CopyTypeSlicing) {
// Case 1) Child with *only* Clone method.
TestPolymorphicCopy<CloneOnlyChildWithClone>(true);
// Case 2) Child with *only* Copy method but protected DoClone() override.
TestPolymorphicCopy<CloneOnlyChildWithCopyVClone>(true);
// Case 3) Child with *only* Copy method.
TestPolymorphicCopy<CloneOnlyChildWithCopy>(false);
// Case 4) Child with no copy and no clone.
TestPolymorphicCopy<CloneOnlyChildUncopyable>(false);
// Case 5) Child with copy, derived from base with copy.
cup<FullyCopyable> src(new CopyChild(1));
cup<FullyCopyable> tgt;
tgt = src; // Triggers a copy
EXPECT_NE(tgt.get(), nullptr); // Confirm actual object assigned.
EXPECT_NE(tgt.get(), src.get()); // Confirm different objects.
EXPECT_TRUE((std::is_same<const FullyCopyable*, decltype(tgt.get())>::value));
EXPECT_FALSE(is_dynamic_castable<CopyChild>(tgt.get()));
}
// This tests the structure of a class and confirms that the "is_copyable" value
// conforms to the stated properties. A class is copyable if it is copy
// constructible *or* cloneable.
GTEST_TEST(CopyableUniquePtrTest, CopyableAsExpected) {
// Examine the 2 x 2 matrix of copy constructible X cloneable with copyable
// indicated in parentheses and case numbers in the corners:
//
// copy constructible
// T F
// ___________________________
// c |1) |2) |
// l | | |
// o T | FullyCopyable | CloneOnly |
// n | (T) | (T) |
// e |_______________|___________|
// a |3) |4) |
// b | | |
// l F | CopyOnly | Base |
// e | (T) | (F) |
// |_______________|___________|
//
// Case X) Cloneable || Constructible --> Copyable
// Case 1) True || True --> True
EXPECT_TRUE(is_cloneable<FullyCopyable>::value);
EXPECT_TRUE(std::is_copy_constructible<FullyCopyable>::value);
// Case 2) True || False --> True
EXPECT_TRUE(is_cloneable<CloneOnly>::value);
EXPECT_FALSE(std::is_copy_constructible<CloneOnly>::value);
// Case 3) False || True --> True
EXPECT_FALSE(is_cloneable<CopyOnly>::value);
EXPECT_TRUE(std::is_copy_constructible<CopyOnly>::value);
// Case 4) False || False --> False
EXPECT_FALSE(is_cloneable<Base>::value);
EXPECT_FALSE(std::is_copy_constructible<Base>::value);
}
// ------------------------ Constructor Tests ------------------------------
// These tests cover construction functionality.
// Tests constructor methods that create an empty pointer. This implicitly
// relies on the empty() method.
GTEST_TEST(CopyableUniquePtrTest, ConstructEmptyPtr) {
// Default constructor.
cup<CopyOnly> ptr;
EXPECT_TRUE(ptr.empty());
// Explicitly null.
cup<CopyOnly> ptr2(nullptr);
EXPECT_TRUE(ptr2.empty());
}
// Test constructor methods that construct a valid copyable_unique_ptr from
// compatible non-null pointers. These constructors do *not* invoke any copying.
GTEST_TEST(CopyableUniquePtrTest, ConstructOnPtrNoCopy) {
CloneOnly* base_ptr;
// Case 1: cup<Base> with Base*
cup<CloneOnly> ptr(base_ptr = new CloneOnly(1));
EXPECT_EQ(ptr.get(), base_ptr);
// Case 2: cup<Base> with Derived*
CloneOnlyChildWithClone* co_ptr;
cup<CloneOnly> ptr2(co_ptr = new CloneOnlyChildWithClone(2));
// Shows that type is preserved.
ASSERT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(ptr2.get()));
EXPECT_EQ(ptr2.get(), co_ptr);
}
// Test copy constructor on copyable_unique_ptr. Copying from three different
// types:
// copyable_unique_ptr<Base> (containing a Base*)
// copyable_unique_ptr<Base> (containing a Derived*)
// copyable_unique_ptr<Derived> (containing a Derived*)
GTEST_TEST(CopyableUniquePtrTest, CopyConstructFromCopyable) {
CloneOnly* base_ptr;
cup<CloneOnly> u_ptr(base_ptr = new CloneOnly(1));
EXPECT_EQ(u_ptr.get(), base_ptr);
// Copy constructor on copyable_unique-ptr of same specialized class.
cup<CloneOnly> cup_ptr(u_ptr);
EXPECT_EQ(u_ptr.get(), base_ptr);
EXPECT_NE(cup_ptr.get(), base_ptr);
EXPECT_NE(cup_ptr.get(), nullptr);
EXPECT_EQ(cup_ptr->value, u_ptr->value);
CloneOnlyChildWithClone* co_ptr;
cup<CloneOnly> u_ptr2(co_ptr = new CloneOnlyChildWithClone(2));
EXPECT_EQ(u_ptr2.get(), co_ptr);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(u_ptr2.get()));
// Copy constructor on copyable_unique-ptr of same specialized class, but
// contains derived class.
cup<CloneOnly> cup_ptr2(u_ptr2);
EXPECT_EQ(u_ptr2.get(), co_ptr);
EXPECT_NE(cup_ptr2.get(), co_ptr);
EXPECT_NE(cup_ptr2.get(), nullptr);
EXPECT_EQ(cup_ptr2->value, u_ptr2->value);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(cup_ptr2.get()));
// Copy constructor on copyable_unique-ptr of derived specialized class.
CloneOnlyChildWithClone* co_ptr3;
cup<CloneOnlyChildWithClone> u_ptr3(co_ptr3 = new CloneOnlyChildWithClone(3));
EXPECT_EQ(u_ptr3.get(), co_ptr3);
cup<CloneOnly> cup_ptr3(u_ptr3);
EXPECT_EQ(u_ptr3.get(), co_ptr3);
EXPECT_NE(cup_ptr3.get(), co_ptr3);
EXPECT_NE(cup_ptr3.get(), nullptr);
EXPECT_EQ(cup_ptr3->value, u_ptr3->value);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(cup_ptr3.get()));
}
// Test copy constructor on unique_ptr. Copying from three different
// types:
// copyable_unique_ptr<Base> (containing a Base*)
// copyable_unique_ptr<Base> (containing a Derived*)
// copyable_unique_ptr<Derived> (containing a Derived*)
GTEST_TEST(CopyableUniquePtrTest, CopyConstructFromUniquePtr) {
CloneOnly* base_ptr;
unique_ptr<CloneOnly> u_ptr(base_ptr = new CloneOnly(1));
EXPECT_EQ(u_ptr.get(), base_ptr);
// Copy constructor on copyable_unique-ptr of same specialized class.
cup<CloneOnly> cup_ptr(u_ptr);
EXPECT_EQ(u_ptr.get(), base_ptr);
EXPECT_NE(cup_ptr.get(), base_ptr);
EXPECT_NE(cup_ptr.get(), nullptr);
EXPECT_EQ(cup_ptr->value, u_ptr->value);
CloneOnlyChildWithClone* co_ptr;
unique_ptr<CloneOnly> u_ptr2(co_ptr = new CloneOnlyChildWithClone(2));
EXPECT_EQ(u_ptr2.get(), co_ptr);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(u_ptr2.get()));
// Copy constructor on copyable_unique-ptr of same specialized class, but
// contains derived class.
cup<CloneOnly> cup_ptr2(u_ptr2);
EXPECT_EQ(u_ptr2.get(), co_ptr);
EXPECT_NE(cup_ptr2.get(), co_ptr);
EXPECT_NE(cup_ptr2.get(), nullptr);
EXPECT_EQ(cup_ptr2->value, u_ptr2->value);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(cup_ptr2.get()));
// Copy constructor on copyable_unique-ptr of derived specialized class.
CloneOnlyChildWithClone* co_ptr3;
unique_ptr<CloneOnlyChildWithClone> u_ptr3(
co_ptr3 = new CloneOnlyChildWithClone(3));
EXPECT_EQ(u_ptr3.get(), co_ptr3);
cup<CloneOnly> cup_ptr3(u_ptr3);
EXPECT_EQ(u_ptr3.get(), co_ptr3);
EXPECT_NE(cup_ptr3.get(), co_ptr3);
EXPECT_NE(cup_ptr3.get(), nullptr);
EXPECT_EQ(cup_ptr3->value, u_ptr3->value);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(cup_ptr3.get()));
}
// Test move constructor on copyable_unique_ptr. Copying from three different
// types:
// copyable_unique_ptr<Base> (containing a Base*)
// copyable_unique_ptr<Base> (containing a Derived*)
// copyable_unique_ptr<Derived> (containing a Derived*)
GTEST_TEST(CopyableUniquePtrTest, MoveConstructFromCopyable) {
CloneOnly* base_ptr;
cup<CloneOnly> u_ptr(base_ptr = new CloneOnly(1));
EXPECT_EQ(u_ptr.get(), base_ptr);
// Move constructor on copyable_unique-ptr of same specialized class.
cup<CloneOnly> cup_ptr(move(u_ptr));
EXPECT_EQ(u_ptr.get(), nullptr);
EXPECT_EQ(cup_ptr.get(), base_ptr);
CloneOnlyChildWithClone* co_ptr;
cup<CloneOnly> u_ptr2(co_ptr = new CloneOnlyChildWithClone(2));
EXPECT_EQ(u_ptr2.get(), co_ptr);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(u_ptr2.get()));
// Copy constructor on copyable_unique-ptr of same specialized class, but
// contains derived class.
cup<CloneOnly> cup_ptr2(move(u_ptr2));
EXPECT_EQ(u_ptr2.get(), nullptr);
EXPECT_EQ(cup_ptr2.get(), co_ptr);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(cup_ptr2.get()));
// Copy constructor on copyable_unique-ptr of derived specialized class.
CloneOnlyChildWithClone* co_ptr3;
cup<CloneOnlyChildWithClone> u_ptr3(co_ptr3 = new CloneOnlyChildWithClone(3));
EXPECT_EQ(u_ptr3.get(), co_ptr3);
cup<CloneOnly> cup_ptr3(move(u_ptr3));
EXPECT_EQ(u_ptr3.get(), nullptr);
EXPECT_EQ(cup_ptr3.get(), co_ptr3);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(cup_ptr3.get()));
}
// Test move constructor on unique_ptr. Copying from three different
// types:
// copyable_unique_ptr<Base> (containing a Base*)
// copyable_unique_ptr<Base> (containing a Derived*)
// copyable_unique_ptr<Derived> (containing a Derived*)
GTEST_TEST(CopyableUniquePtrTest, MoveConstructFromUnique) {
CloneOnly* base_ptr;
unique_ptr<CloneOnly> u_ptr(base_ptr = new CloneOnly(1));
EXPECT_EQ(u_ptr.get(), base_ptr);
// Move constructor on copyable_unique-ptr of same specialized class.
cup<CloneOnly> cup_ptr(move(u_ptr));
EXPECT_EQ(u_ptr.get(), nullptr);
EXPECT_EQ(cup_ptr.get(), base_ptr);
CloneOnlyChildWithClone* co_ptr;
unique_ptr<CloneOnly> u_ptr2(co_ptr = new CloneOnlyChildWithClone(2));
EXPECT_EQ(u_ptr2.get(), co_ptr);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(u_ptr2.get()));
// Copy constructor on copyable_unique-ptr of same specialized class, but
// contains derived class.
cup<CloneOnly> cup_ptr2(move(u_ptr2));
EXPECT_EQ(u_ptr2.get(), nullptr);
EXPECT_EQ(cup_ptr2.get(), co_ptr);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(cup_ptr2.get()));
// Copy constructor on copyable_unique-ptr of derived specialized class.
CloneOnlyChildWithClone* co_ptr3;
unique_ptr<CloneOnlyChildWithClone> u_ptr3(
co_ptr3 = new CloneOnlyChildWithClone(3));
EXPECT_EQ(u_ptr3.get(), co_ptr3);
cup<CloneOnly> cup_ptr3(move(u_ptr3));
EXPECT_EQ(u_ptr3.get(), nullptr);
EXPECT_EQ(cup_ptr3.get(), co_ptr3);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(cup_ptr3.get()));
}
// ------------------------ Destructor Tests ------------------------------
// These tests the various methods that cause the object to be destroyed.
// This class allows me to track destruction of instances in copyable_unique_ptr
// instances. To use it, set the static member dtor_called to false, and then
// perform an operation. If an instance has been destroyed, dtor_called will be
// true, otherwise, no destructor was called.
struct DestructorTracker : public CloneOnly {
explicit DestructorTracker(int v, Origin org = Origin::CONSTRUCT)
: CloneOnly(v, org) {}
virtual ~DestructorTracker() { dtor_called = true; }
DestructorTracker(const DestructorTracker&) = delete;
unique_ptr<DestructorTracker> Clone() const {
return make_unique<DestructorTracker>(value, Origin::CLONE);
}
static bool dtor_called;
};
bool DestructorTracker::dtor_called = false;
// Tests the destruction of the referenced object when the pointer is destroyed.
// In this case, destruction is triggered by leaving the scope of the
// copyable_unique_ptr.
GTEST_TEST(CopyableUniquePtrTest, DestroyOnScopeExit) {
// Empty copyable_unique_ptr<T> does not invoke T's destructor.
{
cup<DestructorTracker> empty;
DestructorTracker::dtor_called = false;
}
EXPECT_FALSE(DestructorTracker::dtor_called);
// Non-empty pointer invokes destructor.
DestructorTracker* raw_ptr = new DestructorTracker(1);
{
cup<DestructorTracker> ptr(raw_ptr);
EXPECT_EQ(ptr.get(), raw_ptr);
DestructorTracker::dtor_called = false;
}
EXPECT_TRUE(DestructorTracker::dtor_called);
}
// This tests the various incarnations of reset.
GTEST_TEST(CopyableUniquePtrTest, Reset) {
cup<DestructorTracker> ptr;
EXPECT_TRUE(ptr.empty()); // Confirm initial condition.
// Case 1: Resetting an empty pointer invokes no constructor.
DestructorTracker* raw_ptr = new DestructorTracker(1);
DestructorTracker::dtor_called = false;
ptr.reset(raw_ptr);
EXPECT_EQ(ptr.get(), raw_ptr);
// Previously empty pointer does *not* invoke destructor.
EXPECT_FALSE(DestructorTracker::dtor_called);
// Case 2: Previous non-null contents *are* destroyed.
raw_ptr = new DestructorTracker(2);
EXPECT_NE(ptr.get(), raw_ptr);
DestructorTracker::dtor_called = false;
ptr.reset(raw_ptr);
EXPECT_EQ(ptr.get(), raw_ptr);
// Previously non-empty pointer *does* invoke destructor.
EXPECT_TRUE(DestructorTracker::dtor_called);
// Case 3: Previous non-null contents replace by nullptr.
DestructorTracker::dtor_called = false;
ptr.reset();
EXPECT_EQ(ptr.get(), nullptr);
// Previously non-empty pointer *does* invoke destructor.
EXPECT_TRUE(DestructorTracker::dtor_called);
}
// ------------------------ Assignment Tests ------------------------------
// These tests cover assignment operator.
// Tests the assignment of a raw pointer to a copyable_unique_ptr. The
// copyable_unique_ptr should take ownership. It also confirms that the
// previous object is destroyed. Assignment of the following patterns:
//
// | Target Type | Source | Target State | Dtor? |
// |-------------+----------+--------------+-------|
// | Base | nullptr | nullptr | N |
// | Base | Base* | nullptr | Y |
// | Base | Base* | Base* | Y |
// | Base | Derived* | nullptr | N |
GTEST_TEST(CopyableUniquePtrTest, AssignPointer) {
cup<DestructorTracker> ptr;
// Do not re-order these tests. Each test implicitly relies on the verified
// state at the conclusion of the previous case.
// Case 1: Assign nullptr to empty cup.
EXPECT_EQ(ptr.get(), nullptr);
DestructorTracker::dtor_called = false;
ptr = nullptr;
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_EQ(ptr.get(), nullptr);
// Case 2: Assign pointer of specialized type to empty cup.
DestructorTracker::dtor_called = false;
DestructorTracker* raw = new DestructorTracker(421);
ptr = raw;
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_EQ(ptr.get(), raw);
// Case 3: Assign pointer of specialized type to non-empty cup.
DestructorTracker* raw2 = new DestructorTracker(124);
DestructorTracker::dtor_called = false;
ptr = raw2;
EXPECT_TRUE(DestructorTracker::dtor_called);
EXPECT_EQ(ptr.get(), raw2);
// Case 4: Assign pointer of Derived type to empty cup<Base>
cup<CloneOnly> co_ptr;
CloneOnlyChildWithClone* derived_raw = new CloneOnlyChildWithClone(13);
co_ptr = derived_raw;
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(co_ptr.get()));
EXPECT_EQ(co_ptr.get(), derived_raw);
}
// Tests the assignment of a const reference object to a copyable_unique_ptr.
// This should *always* create a copy. It also confirms that the previous object
// is destroyed. Assignment of the following patterns:
//
// | Target Type | Source | Target State | Dtor? |
// |-------------+----------+--------------+-------|
// | Base | Base& | nullptr | Y |
// | Base | Base& | Base* | Y |
// | Base | Derived& | nullptr | N |
GTEST_TEST(CopyableUniquePtrTest, AssignConstReference) {
DestructorTracker ref(421);
cup<DestructorTracker> ptr;
// Do not re-order these tests. Each test implicitly relies on the verified
// state at the conclusion of the previous case.
// Case 1: Assign reference of specialized type to empty cup.
EXPECT_EQ(ptr.get(), nullptr);
DestructorTracker::dtor_called = false;
ptr = ref;
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_NE(ptr.get(), &ref);
EXPECT_EQ(ptr->value, ref.value);
// Case 2: Assign reference of specialized type to non-empty cup.
DestructorTracker ref2(124);
DestructorTracker::dtor_called = false;
ptr = ref2;
EXPECT_TRUE(DestructorTracker::dtor_called);
EXPECT_NE(ptr.get(), &ref);
EXPECT_NE(ptr.get(), &ref2);
EXPECT_EQ(ptr->value, ref2.value);
// Case 3: Assign reference of Derived type to empty cup<Base>
cup<CloneOnly> co_ptr;
CloneOnlyChildWithClone derived_ref(132);
co_ptr = derived_ref;
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(co_ptr.get()));
EXPECT_NE(co_ptr.get(), &derived_ref);
EXPECT_EQ(co_ptr->value, derived_ref.value);
}
// Tests the copy assignment of a copyable_unique_ptr to a copyable_unique_ptr.
// This should *always* create a copy. It also confirms that the previous object
// is destroyed. Assignment of the following patterns:
//
// | Target Type | Source | Target State | Dtor? |
// |-------------+-----------+--------------+-------|
// | Base | empty | nullptr | N |
// | Base | <Base> | nullptr | Y |
// | Base | <Base> | Base* | Y |
// | Base | <Derived> | nullptr | N |
GTEST_TEST(CopyableUniquePtrTest, CopyAssignFromCopyableUniquePtr) {
DestructorTracker* raw = new DestructorTracker(421);
cup<DestructorTracker> src(raw);
cup<DestructorTracker> empty;
cup<DestructorTracker> tgt;
// Do not re-order these tests. Each test implicitly relies on the verified
// state at the conclusion of the previous case.
// Case 1: Assign empty cup to empty cup.
EXPECT_EQ(tgt.get(), nullptr);
DestructorTracker::dtor_called = false;
tgt = empty;
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_EQ(tgt.get(), nullptr);
EXPECT_EQ(empty.get(), nullptr);
// Case 2: Assign non-empty cup<Base> to empty cup<Base>.
DestructorTracker::dtor_called = false;
tgt = src;
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_NE(tgt.get(), src.get()); // Not the same pointer as src.
EXPECT_EQ(src.get(), raw); // Src has original value.
EXPECT_EQ(tgt->value, src->value); // Src and tgt are copies.
// Case 3: Assign non-empty cup<Base> type to non-empty cup<Base>.
DestructorTracker* raw2;
cup<DestructorTracker> src2(raw2 = new DestructorTracker(124));
DestructorTracker::dtor_called = false;
tgt = src2;
EXPECT_TRUE(DestructorTracker::dtor_called);
EXPECT_NE(tgt.get(), src2.get()); // Not the same pointer as src.
EXPECT_EQ(src2.get(), raw2); // Src has original value.
EXPECT_EQ(tgt->value, src2->value); // Src and tgt are copies.
// Case 4: Assign non-empty cup<Derived> to empty cup<Base>
cup<CloneOnly> base_tgt;
CloneOnlyChildWithClone* derived_raw;
cup<CloneOnlyChildWithClone> derived_src(derived_raw =
new CloneOnlyChildWithClone(13));
base_tgt = derived_src;
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(base_tgt.get()));
EXPECT_NE(base_tgt.get(), derived_src.get());
EXPECT_EQ(base_tgt->value, derived_src->value);
}
// Tests the copy assignment of a standard unique_ptr to a copyable_unique_ptr.
// This should *always* create a copy. It also confirms that the previous object
// is destroyed. Assignment of the following patterns:
//
// | Target Type | Source | Target State | Dtor? |
// |-------------+-----------+--------------+-------|
// | Base | empty | nullptr | N |
// | Base | <Base> | nullptr | Y |
// | Base | <Base> | Base* | Y |
// | Base | <Derived> | nullptr | N |
GTEST_TEST(CopyableUniquePtrTest, CopyAssignFromUniquePtr) {
DestructorTracker* raw = new DestructorTracker(421);
unique_ptr<DestructorTracker> src(raw);
unique_ptr<DestructorTracker> empty;
cup<DestructorTracker> tgt;
// Do not re-order these tests. Each test implicitly relies on the verified
// state at the conclusion of the previous case.
// Case 1: Assign empty unique_ptr to empty cup.
EXPECT_EQ(tgt.get(), nullptr);
DestructorTracker::dtor_called = false;
tgt = empty;
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_EQ(tgt.get(), nullptr);
EXPECT_EQ(empty.get(), nullptr);
// Case 2: Assign non-empty unique_ptr<Base> to empty cup<Base>.
DestructorTracker::dtor_called = false;
tgt = src;
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_NE(tgt.get(), src.get()); // Not the same pointer as src.
EXPECT_EQ(src.get(), raw); // Src has original value.
EXPECT_EQ(tgt->value, src->value); // Src and tgt are copies.
// Case 3: Assign non-empty unique_ptr<Base> type to non-empty cup<Base>.
DestructorTracker* raw2;
cup<DestructorTracker> src2(raw2 = new DestructorTracker(124));
DestructorTracker::dtor_called = false;
tgt = src2;
EXPECT_TRUE(DestructorTracker::dtor_called);
EXPECT_NE(tgt.get(), src2.get()); // Not the same pointer as src.
EXPECT_EQ(src2.get(), raw2); // Src has original value.
EXPECT_EQ(tgt->value, src2->value); // Src and tgt are copies.
// Case 4: Assign non-empty unique_ptr<Derived> to empty cup<Base>
cup<CloneOnly> base_tgt;
CloneOnlyChildWithClone* derived_raw;
unique_ptr<CloneOnlyChildWithClone> derived_src(
derived_raw = new CloneOnlyChildWithClone(13));
base_tgt = derived_src;
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(base_tgt.get()));
EXPECT_NE(base_tgt.get(), derived_src.get());
EXPECT_EQ(base_tgt->value, derived_src->value);
}
// Tests the move assignment of a copyable_unique_ptr to a copyable_unique_ptr.
// This should reassign ownership changing the target and source. It also
// confirms that the previous object is destroyed. Assignment of the following
// patterns:
//
// | Target Type | Source | Target State | Dtor? |
// |-------------+-----------+--------------+-------|
// | Base | empty | nullptr | N |
// | Base | <Base> | nullptr | Y |
// | Base | <Base> | Base* | Y |
// | Base | <Derived> | nullptr | N |
GTEST_TEST(CopyableUniquePtrTest, MoveAssignFromCopyableUniquePtr) {
DestructorTracker* raw = new DestructorTracker(421);
cup<DestructorTracker> src(raw);
cup<DestructorTracker> empty;
cup<DestructorTracker> tgt;
// Do not re-order these tests. Each test implicitly relies on the verified
// state at the conclusion of the previous case.
// Case 1: Assign empty unique_ptr to empty cup.
EXPECT_EQ(tgt.get(), nullptr);
DestructorTracker::dtor_called = false;
tgt = move(empty);
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_EQ(tgt.get(), nullptr);
EXPECT_EQ(empty.get(), nullptr);
// Case 2: Assign non-empty unique_ptr<Base> to empty cup<Base>.
DestructorTracker::dtor_called = false;
tgt = move(src);
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_EQ(tgt.get(), raw); // Tgt has taken ownership of raw.
EXPECT_EQ(src.get(), nullptr); // Src has been cleared.
// Case 3: Assign non-empty unique_ptr<Base> type to non-empty cup<Base>.
DestructorTracker* raw2;
cup<DestructorTracker> src2(raw2 = new DestructorTracker(124));
DestructorTracker::dtor_called = false;
tgt = move(src2);
EXPECT_TRUE(DestructorTracker::dtor_called);
EXPECT_EQ(tgt.get(), raw2); // Tgt has taken ownership of raw.
EXPECT_EQ(src2.get(), nullptr); // Src has been cleared.
// Case 4: Assign non-empty unique_ptr<Derived> to empty cup<Base>
cup<CloneOnly> base_tgt;
CloneOnlyChildWithClone* derived_raw;
cup<CloneOnlyChildWithClone> derived_src(derived_raw =
new CloneOnlyChildWithClone(13));
base_tgt = move(derived_src);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(base_tgt.get()));
EXPECT_EQ(base_tgt.get(), derived_raw); // Tgt has taken ownership of raw.
EXPECT_EQ(derived_src.get(), nullptr); // Src has been cleared.
}
// Tests the move assignment of a standard unique_ptr to a copyable_unique_ptr.
// This should reassign ownership changing the target and source. It also
// confirms that the previous object is destroyed. Assignment of the following
// patterns:
//
// | Target Type | Source | Target State | Dtor? |
// |-------------+-----------+--------------+-------|
// | Base | empty | nullptr | N |
// | Base | <Base> | nullptr | Y |
// | Base | <Base> | Base* | Y |
// | Base | <Derived> | nullptr | N |
GTEST_TEST(CopyableUniquePtrTest, MoveAssignFromUniquePtr) {
DestructorTracker* raw = new DestructorTracker(421);
unique_ptr<DestructorTracker> src(raw);
unique_ptr<DestructorTracker> empty;
cup<DestructorTracker> tgt;
// Do not re-order these tests. Each test implicitly relies on the verified
// state at the conclusion of the previous case.
// Case 1: Assign empty unique_ptr to empty cup.
EXPECT_EQ(tgt.get(), nullptr);
DestructorTracker::dtor_called = false;
tgt = move(empty);
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_EQ(tgt.get(), nullptr);
EXPECT_EQ(empty.get(), nullptr);
// Case 2: Assign non-empty unique_ptr<Base> to empty cup<Base>.
DestructorTracker::dtor_called = false;
tgt = move(src);
EXPECT_FALSE(DestructorTracker::dtor_called);
EXPECT_EQ(tgt.get(), raw); // Tgt has taken ownership of raw.
EXPECT_EQ(src.get(), nullptr); // Src has been cleared.
// Case 3: Assign non-empty unique_ptr<Base> type to non-empty cup<Base>.
DestructorTracker* raw2;
cup<DestructorTracker> src2(raw2 = new DestructorTracker(124));
DestructorTracker::dtor_called = false;
tgt = move(src2);
EXPECT_TRUE(DestructorTracker::dtor_called);
EXPECT_EQ(tgt.get(), raw2); // Tgt has taken ownership of raw.
EXPECT_EQ(src2.get(), nullptr); // Src has been cleared.
// Case 4: Assign non-empty unique_ptr<Derived> to empty cup<Base>
cup<CloneOnly> base_tgt;
CloneOnlyChildWithClone* derived_raw;
unique_ptr<CloneOnlyChildWithClone> derived_src(
derived_raw = new CloneOnlyChildWithClone(13));
base_tgt = move(derived_src);
EXPECT_TRUE(is_dynamic_castable<CloneOnlyChildWithClone>(base_tgt.get()));
EXPECT_EQ(base_tgt.get(), derived_raw); // Tgt has taken ownership of raw.
EXPECT_EQ(derived_src.get(), nullptr); // Src has been cleared.
}
// ------------------------ Observer Tests ------------------------------
// These tests cover the introspective functions: determining if the pointer is
// empty, acquiring access to the raw pointer, and references. The empty()
// method is implicitly tested multiple times in other methods.
// Tests the semantics that get() and get_mutable() return const and non-const
// pointers respectively.
GTEST_TEST(CopyableUniquePtrTest, PointerAccessConstSemantics) {
CloneOnly* raw;
cup<CloneOnly> ptr(raw = new CloneOnly(1));
// Simply test that they return the right pointer *value*.
EXPECT_EQ(ptr.get(), raw);
EXPECT_EQ(ptr.get_mutable(), raw);
// The extra parentheses prevent the macro from getting confused.
// get is const pointer
EXPECT_TRUE(
(std::is_same<const CloneOnly*, decltype(ptr.get())>::value));
// NOTE: is_assignable uses declval<T> to create the lhs. declval<T> creates
// an r-value reference (which will *always* fail assignability tests. By
// taking an l-value reference of the object, the resulting type becomes
// l-value.
EXPECT_FALSE((std::is_assignable<CloneOnly*&, decltype(ptr.get())>::value));
// get_mutable is non-const
EXPECT_TRUE((std::is_same<CloneOnly*, decltype(ptr.get_mutable())>::value));
EXPECT_TRUE(
(std::is_assignable<CloneOnly*&, decltype(ptr.get_mutable())>::value));
}
// ------------------------ Core unique_ptr Tests ------------------------------
// This tests the functionality that should be directly inherited from the
// unique_ptr class, preserving the idea that, except for copying semantics,
// the copyable_unique_ptr *is* a unique_ptr.
// This tests that a cup<const T> offers no mutable access to the underlying
// // type (via pointer or reference).
GTEST_TEST(CopyableUniquePtrTest, ConstSpecializationHasNoMutableAccess) {
cup<const CloneOnly> ptr(new CloneOnly(2));
// Being the same as a 'const' type, precludes the possibility of being the
// "same" as a non-const type.
EXPECT_TRUE((std::is_same<const CloneOnly*, decltype(ptr.get())>::value));
}
// This tests the implicit conversion of the pointer to a boolean. It does *not*
// confirm that it is only available in "contextual conversions". See:
// http://en.cppreference.com/w/cpp/language/implicit_conversion#Contextual_conversions
GTEST_TEST(CopyableUniquePtrTest, ConverstionToBool) {
copyable_unique_ptr<CloneOnly> ptr;
EXPECT_FALSE(ptr);
ptr.reset(new CloneOnly(1));
EXPECT_TRUE(ptr);
}
// Tests the two forms of swap (member method and external method).
GTEST_TEST(CopyableUniquePtrTest, SwapTest) {
DestructorTracker* raw1;
DestructorTracker* raw2;
cup<DestructorTracker> ptr1(raw1 = new DestructorTracker(1));
cup<DestructorTracker> ptr2(raw2 = new DestructorTracker(2));
EXPECT_EQ(ptr1.get(), raw1);
EXPECT_EQ(ptr2.get(), raw2);
EXPECT_NE(ptr1.get(), ptr2.get());
// Don't reorder these tests. Results depend on this execution order.
// The swap method.
DestructorTracker::dtor_called = false;
ptr1.swap(ptr2);
EXPECT_FALSE(DestructorTracker::dtor_called); // Nothing got destroyed.
EXPECT_EQ(ptr1.get(), raw2);
EXPECT_EQ(ptr2.get(), raw1);