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