1 //
2 // Tests for
3 //  template <typename U>
4 //  intrusive_shared_ptr& operator=(intrusive_shared_ptr<U, RefcountPolicy> const& other);
5 //
6 //  intrusive_shared_ptr& operator=(intrusive_shared_ptr const& other);
7 //
8 
9 #include <libkern/c++/intrusive_shared_ptr.h>
10 #include <darwintest.h>
11 #include <type_traits>
12 #include "test_policy.h"
13 
14 struct Base { int i; };
15 struct Derived : Base { };
16 
17 struct Base1 { int i; };
18 struct Base2 { long l; };
19 struct DerivedMultiple : Base1, Base2 {
DerivedMultipleDerivedMultiple20 	DerivedMultiple(int i) : Base1{i}, Base2{i + 10}
21 	{
22 	}
23 };
24 
25 struct Unrelated { };
26 
27 template <typename Stored, typename From, typename To>
28 static void
tests()29 tests()
30 {
31 	Stored obj1{1};
32 	Stored obj2{2};
33 
34 	// Copy-assign non-null to non-null
35 	{
36 		tracked_shared_ptr<From> const from(&obj1, libkern::retain);
37 		tracked_shared_ptr<To> to(&obj2, libkern::retain);
38 		tracking_policy::reset();
39 		tracked_shared_ptr<To>& ref = (to = from);
40 		CHECK(tracking_policy::releases == 1);
41 		CHECK(tracking_policy::retains == 1);
42 		CHECK(&ref == &to);
43 		CHECK(from.get() == &obj1);
44 		CHECK(to.get() == &obj1);
45 	}
46 
47 	// Copy-assign non-null to null
48 	{
49 		tracked_shared_ptr<From> const from(&obj1, libkern::retain);
50 		tracked_shared_ptr<To> to = nullptr;
51 		tracking_policy::reset();
52 		tracked_shared_ptr<To>& ref = (to = from);
53 		CHECK(tracking_policy::releases == 0);
54 		CHECK(tracking_policy::retains == 1);
55 		CHECK(&ref == &to);
56 		CHECK(from.get() == &obj1);
57 		CHECK(to.get() == &obj1);
58 	}
59 
60 	// Copy-assign null to non-null
61 	{
62 		tracked_shared_ptr<From> const from = nullptr;
63 		tracked_shared_ptr<To> to(&obj2, libkern::retain);
64 		tracking_policy::reset();
65 		tracked_shared_ptr<To>& ref = (to = from);
66 		CHECK(tracking_policy::releases == 1);
67 		CHECK(tracking_policy::retains == 0);
68 		CHECK(&ref == &to);
69 		CHECK(from.get() == nullptr);
70 		CHECK(to.get() == nullptr);
71 	}
72 
73 	// Copy-assign null to null
74 	{
75 		tracked_shared_ptr<From> const from = nullptr;
76 		tracked_shared_ptr<To> to = nullptr;
77 		tracking_policy::reset();
78 		tracked_shared_ptr<To>& ref = (to = from);
79 		CHECK(tracking_policy::releases == 0);
80 		CHECK(tracking_policy::retains == 0);
81 		CHECK(&ref == &to);
82 		CHECK(from.get() == nullptr);
83 		CHECK(to.get() == nullptr);
84 	}
85 }
86 
87 T_DECL(assign_copy, "intrusive_shared_ptr.assign.copy", T_META_TAG_VM_PREFERRED) {
88 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Derived>();
89 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Derived const>();
90 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Derived const>();
91 
92 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base>();
93 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base const>();
94 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Base const>();
95 
96 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base1>();
97 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base1 const>();
98 
99 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base2>();
100 	tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base2 const>();
101 
102 	// Make sure basic trait querying works
103 	static_assert(std::is_copy_assignable_v<test_shared_ptr<Derived> >);
104 
105 	// Make sure downcasts are disabled
106 	static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Derived>, /*from*/ test_shared_ptr<Base> const&>);
107 	static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<DerivedMultiple>, /*from*/ test_shared_ptr<Base1> const&>);
108 	static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<DerivedMultiple>, /*from*/ test_shared_ptr<Base2> const&>);
109 	static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Base2>, /*from*/ test_shared_ptr<Base1> const&>);
110 
111 	// Make sure const-casting away doesn't work
112 	static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Derived>, /*from*/ test_shared_ptr<Derived const> const&>);
113 
114 	// Make sure casting to unrelated types doesn't work
115 	static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<char>, /*from*/ test_shared_ptr<Derived> const&>);
116 	static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Unrelated>, /*from*/ test_shared_ptr<Derived> const&>);
117 	static_assert(!std::is_assignable_v</*to*/ test_shared_ptr<Base2>, /*from*/ test_shared_ptr<Base1> const&>);
118 
119 	// Make sure constructing with different policies doesn't work
120 	static_assert(!std::is_assignable_v</*to*/ libkern::intrusive_shared_ptr<Derived, dummy_policy<2> >, /*from*/ libkern::intrusive_shared_ptr<Derived, dummy_policy<1> > const &>);
121 }
122