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