]>
git.saurik.com Git - apple/xnu.git/blob - tests/intrusive_shared_ptr_src/assign.move.cpp
3 // template <typename U>
4 // intrusive_shared_ptr& operator=(intrusive_shared_ptr<U, RefcountPolicy>&& other);
6 // intrusive_shared_ptr& operator=(intrusive_shared_ptr&& other);
9 #include <libkern/c++/intrusive_shared_ptr.h>
10 #include <darwintest.h>
11 #include <type_traits>
13 #include "test_policy.h"
15 struct Base
{ int i
; };
16 struct Derived
: Base
{ };
18 struct Base1
{ int i
; };
19 struct Base2
{ long l
; };
20 struct DerivedMultiple
: Base1
, Base2
{
21 DerivedMultiple(int i
) : Base1
{i
}, Base2
{i
+ 10}
28 template <typename Stored
, typename From
, typename To
>
35 // Move-assign non-null to non-null
37 tracked_shared_ptr
<From
> from(&obj1
, libkern::retain
);
38 tracked_shared_ptr
<To
> to(&obj2
, libkern::retain
);
39 tracking_policy::reset();
40 tracked_shared_ptr
<To
>& ref
= (to
= std::move(from
));
41 CHECK(tracking_policy::releases
== 1);
42 CHECK(tracking_policy::retains
== 0);
44 CHECK(from
.get() == nullptr);
45 CHECK(to
.get() == &obj1
);
48 // Move-assign non-null to null
50 tracked_shared_ptr
<From
> from(&obj1
, libkern::retain
);
51 tracked_shared_ptr
<To
> to
= nullptr;
52 tracking_policy::reset();
53 tracked_shared_ptr
<To
>& ref
= (to
= std::move(from
));
54 CHECK(tracking_policy::releases
== 0);
55 CHECK(tracking_policy::retains
== 0);
57 CHECK(from
.get() == nullptr);
58 CHECK(to
.get() == &obj1
);
61 // Move-assign null to non-null
63 tracked_shared_ptr
<From
> from
= nullptr;
64 tracked_shared_ptr
<To
> to(&obj2
, libkern::retain
);
65 tracking_policy::reset();
66 tracked_shared_ptr
<To
>& ref
= (to
= std::move(from
));
67 CHECK(tracking_policy::releases
== 1);
68 CHECK(tracking_policy::retains
== 0);
70 CHECK(from
.get() == nullptr);
71 CHECK(to
.get() == nullptr);
74 // Move-assign null to null
76 tracked_shared_ptr
<From
> from
= nullptr;
77 tracked_shared_ptr
<To
> to
= nullptr;
78 tracking_policy::reset();
79 tracked_shared_ptr
<To
>& ref
= (to
= std::move(from
));
80 CHECK(tracking_policy::releases
== 0);
81 CHECK(tracking_policy::retains
== 0);
83 CHECK(from
.get() == nullptr);
84 CHECK(to
.get() == nullptr);
88 T_DECL(assign_move
, "intrusive_shared_ptr.assign.move") {
89 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ Derived
>();
90 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ Derived
const>();
91 tests
</*stored*/ Derived
, /*from*/ Derived
const, /*to*/ Derived
const>();
93 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ Base
>();
94 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ Base
const>();
95 tests
</*stored*/ Derived
, /*from*/ Derived
const, /*to*/ Base
const>();
97 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
, /*to*/ Base1
>();
98 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const, /*to*/ Base1
const>();
100 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
, /*to*/ Base2
>();
101 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const, /*to*/ Base2
const>();
103 // Make sure basic trait querying works
104 static_assert(std::is_move_assignable_v
<test_shared_ptr
<Derived
> >);
106 // Make sure downcasts are disabled
107 static_assert(!std::is_assignable_v
</*to*/ test_shared_ptr
<Derived
>, /*from*/ test_shared_ptr
<Base
>&&>);
108 static_assert(!std::is_assignable_v
</*to*/ test_shared_ptr
<DerivedMultiple
>, /*from*/ test_shared_ptr
<Base1
>&&>);
109 static_assert(!std::is_assignable_v
</*to*/ test_shared_ptr
<DerivedMultiple
>, /*from*/ test_shared_ptr
<Base2
>&&>);
110 static_assert(!std::is_assignable_v
</*to*/ test_shared_ptr
<Base2
>, /*from*/ test_shared_ptr
<Base1
>&&>);
112 // Make sure const-casting away doesn't work
113 static_assert(!std::is_assignable_v
</*to*/ test_shared_ptr
<Derived
>, /*from*/ test_shared_ptr
<Derived
const>&&>);
115 // Make sure casting to unrelated types doesn't work
116 static_assert(!std::is_assignable_v
</*to*/ test_shared_ptr
<char>, /*from*/ test_shared_ptr
<Derived
>&&>);
117 static_assert(!std::is_assignable_v
</*to*/ test_shared_ptr
<Unrelated
>, /*from*/ test_shared_ptr
<Derived
>&&>);
118 static_assert(!std::is_assignable_v
</*to*/ test_shared_ptr
<Base2
>, /*from*/ test_shared_ptr
<Base1
>&&>);
120 // Make sure constructing with different policies doesn't work
121 static_assert(!std::is_assignable_v
</*to*/ libkern::intrusive_shared_ptr
<Derived
, dummy_policy
<2> >, /*from*/ libkern::intrusive_shared_ptr
<Derived
, dummy_policy
<1> >&&>);