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