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