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