xref: /xnu-11417.121.6/tests/intrusive_shared_ptr_src/cast.reinterpret.cpp (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions //
2*a1e26a70SApple OSS Distributions // Tests for
3*a1e26a70SApple OSS Distributions //  template<typename To, typename From, typename R>
4*a1e26a70SApple OSS Distributions //  intrusive_shared_ptr<To, R> reinterpret_pointer_cast(intrusive_shared_ptr<From, R> const& ptr) noexcept;
5*a1e26a70SApple OSS Distributions //
6*a1e26a70SApple OSS Distributions //  template<typename To, typename From, typename R>
7*a1e26a70SApple OSS Distributions //  intrusive_shared_ptr<To, R> reinterpret_pointer_cast(intrusive_shared_ptr<From, R>&& ptr) noexcept
8*a1e26a70SApple OSS Distributions //
9*a1e26a70SApple OSS Distributions 
10*a1e26a70SApple OSS Distributions #include <libkern/c++/intrusive_shared_ptr.h>
11*a1e26a70SApple OSS Distributions #include <utility>
12*a1e26a70SApple OSS Distributions #include <darwintest.h>
13*a1e26a70SApple OSS Distributions #include "test_policy.h"
14*a1e26a70SApple OSS Distributions 
15*a1e26a70SApple OSS Distributions struct Base { int i; };
16*a1e26a70SApple OSS Distributions struct Derived : Base { };
17*a1e26a70SApple OSS Distributions 
18*a1e26a70SApple OSS Distributions // Layout compatible with Derived
19*a1e26a70SApple OSS Distributions struct Unrelated { int i; };
20*a1e26a70SApple OSS Distributions 
21*a1e26a70SApple OSS Distributions template <typename Stored, typename From, typename To>
22*a1e26a70SApple OSS Distributions static void
tests()23*a1e26a70SApple OSS Distributions tests()
24*a1e26a70SApple OSS Distributions {
25*a1e26a70SApple OSS Distributions 	Stored obj{3};
26*a1e26a70SApple OSS Distributions 
27*a1e26a70SApple OSS Distributions 	{
28*a1e26a70SApple OSS Distributions 		tracked_shared_ptr<From> const from(&obj, libkern::no_retain);
29*a1e26a70SApple OSS Distributions 		tracking_policy::reset();
30*a1e26a70SApple OSS Distributions 		tracked_shared_ptr<To> to = libkern::reinterpret_pointer_cast<To>(from);
31*a1e26a70SApple OSS Distributions 		CHECK(tracking_policy::retains == 1);
32*a1e26a70SApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
33*a1e26a70SApple OSS Distributions 		CHECK(to.get() == reinterpret_cast<To const*>(&obj));
34*a1e26a70SApple OSS Distributions 		CHECK(from.get() == &obj);
35*a1e26a70SApple OSS Distributions 	}
36*a1e26a70SApple OSS Distributions 	{
37*a1e26a70SApple OSS Distributions 		tracked_shared_ptr<From> from(&obj, libkern::no_retain);
38*a1e26a70SApple OSS Distributions 		tracking_policy::reset();
39*a1e26a70SApple OSS Distributions 		tracked_shared_ptr<To> to = libkern::reinterpret_pointer_cast<To>(std::move(from));
40*a1e26a70SApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
41*a1e26a70SApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
42*a1e26a70SApple OSS Distributions 		CHECK(to.get() == reinterpret_cast<To const*>(&obj));
43*a1e26a70SApple OSS Distributions 		CHECK(from.get() == nullptr);
44*a1e26a70SApple OSS Distributions 	}
45*a1e26a70SApple OSS Distributions 
46*a1e26a70SApple OSS Distributions 	// Test `reinterpret_pointer_cast`ing a null pointer
47*a1e26a70SApple OSS Distributions 	{
48*a1e26a70SApple OSS Distributions 		tracked_shared_ptr<From> const from = nullptr;
49*a1e26a70SApple OSS Distributions 		tracking_policy::reset();
50*a1e26a70SApple OSS Distributions 		tracked_shared_ptr<To> to = libkern::reinterpret_pointer_cast<To>(from);
51*a1e26a70SApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
52*a1e26a70SApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
53*a1e26a70SApple OSS Distributions 		CHECK(to.get() == nullptr);
54*a1e26a70SApple OSS Distributions 		CHECK(from.get() == nullptr);
55*a1e26a70SApple OSS Distributions 	}
56*a1e26a70SApple OSS Distributions 	{
57*a1e26a70SApple OSS Distributions 		tracked_shared_ptr<From> from = nullptr;
58*a1e26a70SApple OSS Distributions 		tracking_policy::reset();
59*a1e26a70SApple OSS Distributions 		tracked_shared_ptr<To> to = libkern::reinterpret_pointer_cast<To>(std::move(from));
60*a1e26a70SApple OSS Distributions 		CHECK(tracking_policy::retains == 0);
61*a1e26a70SApple OSS Distributions 		CHECK(tracking_policy::releases == 0);
62*a1e26a70SApple OSS Distributions 		CHECK(to.get() == nullptr);
63*a1e26a70SApple OSS Distributions 		CHECK(from.get() == nullptr);
64*a1e26a70SApple OSS Distributions 	}
65*a1e26a70SApple OSS Distributions }
66*a1e26a70SApple OSS Distributions 
67*a1e26a70SApple OSS Distributions T_DECL(cast_reinterpret, "intrusive_shared_ptr.cast.reinterpret", T_META_TAG_VM_PREFERRED) {
68*a1e26a70SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base>();
69*a1e26a70SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Base const>();
70*a1e26a70SApple OSS Distributions 
71*a1e26a70SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ char>();
72*a1e26a70SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ char const>();
73*a1e26a70SApple OSS Distributions 
74*a1e26a70SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Unrelated>();
75*a1e26a70SApple OSS Distributions 	tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Unrelated const>();
76*a1e26a70SApple OSS Distributions }
77