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