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