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