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