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> static_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> static_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 struct Base1 { int i; };
19*bbb1b6f9SApple OSS Distributions struct Base2 { long l; };
20*bbb1b6f9SApple OSS Distributions struct DerivedMultiple : Base1, Base2 {
DerivedMultipleDerivedMultiple21*bbb1b6f9SApple OSS Distributions DerivedMultiple(int i) : Base1{i}, Base2{i + 10}
22*bbb1b6f9SApple OSS Distributions {
23*bbb1b6f9SApple OSS Distributions }
24*bbb1b6f9SApple OSS Distributions };
25*bbb1b6f9SApple OSS Distributions
26*bbb1b6f9SApple OSS Distributions template <typename Stored, typename From, typename To>
27*bbb1b6f9SApple OSS Distributions static void
tests()28*bbb1b6f9SApple OSS Distributions tests()
29*bbb1b6f9SApple OSS Distributions {
30*bbb1b6f9SApple OSS Distributions Stored obj{3};
31*bbb1b6f9SApple OSS Distributions
32*bbb1b6f9SApple OSS Distributions {
33*bbb1b6f9SApple OSS Distributions tracked_shared_ptr<From> const from(&obj, libkern::no_retain);
34*bbb1b6f9SApple OSS Distributions tracking_policy::reset();
35*bbb1b6f9SApple OSS Distributions tracked_shared_ptr<To> to = libkern::static_pointer_cast<To>(from);
36*bbb1b6f9SApple OSS Distributions CHECK(tracking_policy::retains == 1);
37*bbb1b6f9SApple OSS Distributions CHECK(tracking_policy::releases == 0);
38*bbb1b6f9SApple OSS Distributions CHECK(to.get() == static_cast<To const*>(&obj));
39*bbb1b6f9SApple OSS Distributions CHECK(from.get() == &obj);
40*bbb1b6f9SApple OSS Distributions }
41*bbb1b6f9SApple OSS Distributions {
42*bbb1b6f9SApple OSS Distributions tracked_shared_ptr<From> from(&obj, libkern::no_retain);
43*bbb1b6f9SApple OSS Distributions tracking_policy::reset();
44*bbb1b6f9SApple OSS Distributions tracked_shared_ptr<To> to = libkern::static_pointer_cast<To>(std::move(from));
45*bbb1b6f9SApple OSS Distributions CHECK(tracking_policy::retains == 0);
46*bbb1b6f9SApple OSS Distributions CHECK(tracking_policy::releases == 0);
47*bbb1b6f9SApple OSS Distributions CHECK(to.get() == static_cast<To const*>(&obj));
48*bbb1b6f9SApple OSS Distributions CHECK(from.get() == nullptr);
49*bbb1b6f9SApple OSS Distributions }
50*bbb1b6f9SApple OSS Distributions
51*bbb1b6f9SApple OSS Distributions // Test `static_pointer_cast`ing a null pointer
52*bbb1b6f9SApple OSS Distributions {
53*bbb1b6f9SApple OSS Distributions tracked_shared_ptr<From> const from = nullptr;
54*bbb1b6f9SApple OSS Distributions tracking_policy::reset();
55*bbb1b6f9SApple OSS Distributions tracked_shared_ptr<To> to = libkern::static_pointer_cast<To>(from);
56*bbb1b6f9SApple OSS Distributions CHECK(tracking_policy::retains == 0);
57*bbb1b6f9SApple OSS Distributions CHECK(tracking_policy::releases == 0);
58*bbb1b6f9SApple OSS Distributions CHECK(to.get() == nullptr);
59*bbb1b6f9SApple OSS Distributions CHECK(from.get() == nullptr);
60*bbb1b6f9SApple OSS Distributions }
61*bbb1b6f9SApple OSS Distributions {
62*bbb1b6f9SApple OSS Distributions tracked_shared_ptr<From> from = nullptr;
63*bbb1b6f9SApple OSS Distributions tracking_policy::reset();
64*bbb1b6f9SApple OSS Distributions tracked_shared_ptr<To> to = libkern::static_pointer_cast<To>(std::move(from));
65*bbb1b6f9SApple OSS Distributions CHECK(tracking_policy::retains == 0);
66*bbb1b6f9SApple OSS Distributions CHECK(tracking_policy::releases == 0);
67*bbb1b6f9SApple OSS Distributions CHECK(to.get() == nullptr);
68*bbb1b6f9SApple OSS Distributions CHECK(from.get() == nullptr);
69*bbb1b6f9SApple OSS Distributions }
70*bbb1b6f9SApple OSS Distributions }
71*bbb1b6f9SApple OSS Distributions
72*bbb1b6f9SApple OSS Distributions T_DECL(cast_static, "intrusive_shared_ptr.cast.static", T_META_TAG_VM_PREFERRED) {
73*bbb1b6f9SApple OSS Distributions tests</*stored*/ Derived, /*from*/ Derived, /*to*/ Base>();
74*bbb1b6f9SApple OSS Distributions tests</*stored*/ Derived, /*from*/ Derived const, /*to*/ Base const>();
75*bbb1b6f9SApple OSS Distributions
76*bbb1b6f9SApple OSS Distributions tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple, /*to*/ Base1>();
77*bbb1b6f9SApple OSS Distributions tests</*stored*/ DerivedMultiple, /*from*/ DerivedMultiple const, /*to*/ Base1 const>();
78*bbb1b6f9SApple OSS Distributions }
79