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