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