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