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