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