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