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