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