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