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