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