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