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