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