]>
git.saurik.com Git - apple/xnu.git/blob - tests/intrusive_shared_ptr_src/assign.copy.cpp
3 // template <typename U>
4 // intrusive_shared_ptr& operator=(intrusive_shared_ptr<U, RefcountPolicy> const& other);
6 // intrusive_shared_ptr& operator=(intrusive_shared_ptr const& other);
9 #include <libkern/c++/intrusive_shared_ptr.h>
10 #include <darwintest.h>
11 #include <type_traits>
12 #include "test_policy.h"
14 struct Base
{ int i
; };
15 struct Derived
: Base
{ };
17 struct Base1
{ int i
; };
18 struct Base2
{ long l
; };
19 struct DerivedMultiple
: Base1
, Base2
{
20 DerivedMultiple(int i
) : Base1
{i
}, Base2
{i
+ 10}
27 template <typename Stored
, typename From
, typename To
>
34 // Copy-assign non-null to non-null
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);
43 CHECK(from
.get() == &obj1
);
44 CHECK(to
.get() == &obj1
);
47 // Copy-assign non-null to null
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);
56 CHECK(from
.get() == &obj1
);
57 CHECK(to
.get() == &obj1
);
60 // Copy-assign null to non-null
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);
69 CHECK(from
.get() == nullptr);
70 CHECK(to
.get() == nullptr);
73 // Copy-assign null to null
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);
82 CHECK(from
.get() == nullptr);
83 CHECK(to
.get() == nullptr);
87 T_DECL(assign_copy
, "intrusive_shared_ptr.assign.copy") {
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>();
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>();
96 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
, /*to*/ Base1
>();
97 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const, /*to*/ Base1
const>();
99 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
, /*to*/ Base2
>();
100 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const, /*to*/ Base2
const>();
102 // Make sure basic trait querying works
103 static_assert(std::is_copy_assignable_v
<test_shared_ptr
<Derived
> >);
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&>);
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&>);
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&>);
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 &>);