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> const_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> const_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 T { int i; };
16*1031c584SApple OSS Distributions
17*1031c584SApple OSS Distributions template <typename Stored, typename From, typename To>
18*1031c584SApple OSS Distributions static void
tests()19*1031c584SApple OSS Distributions tests()
20*1031c584SApple OSS Distributions {
21*1031c584SApple OSS Distributions Stored obj{3};
22*1031c584SApple OSS Distributions
23*1031c584SApple OSS Distributions {
24*1031c584SApple OSS Distributions tracked_shared_ptr<From> const from(&obj, libkern::no_retain);
25*1031c584SApple OSS Distributions tracking_policy::reset();
26*1031c584SApple OSS Distributions tracked_shared_ptr<To> to = libkern::const_pointer_cast<To>(from);
27*1031c584SApple OSS Distributions CHECK(tracking_policy::retains == 1);
28*1031c584SApple OSS Distributions CHECK(tracking_policy::releases == 0);
29*1031c584SApple OSS Distributions CHECK(to.get() == const_cast<To*>(&obj));
30*1031c584SApple OSS Distributions CHECK(from.get() == &obj);
31*1031c584SApple OSS Distributions }
32*1031c584SApple OSS Distributions {
33*1031c584SApple OSS Distributions tracked_shared_ptr<From> from(&obj, libkern::no_retain);
34*1031c584SApple OSS Distributions tracking_policy::reset();
35*1031c584SApple OSS Distributions tracked_shared_ptr<To> to = libkern::const_pointer_cast<To>(std::move(from));
36*1031c584SApple OSS Distributions CHECK(tracking_policy::retains == 0);
37*1031c584SApple OSS Distributions CHECK(tracking_policy::releases == 0);
38*1031c584SApple OSS Distributions CHECK(to.get() == const_cast<To*>(&obj));
39*1031c584SApple OSS Distributions CHECK(from.get() == nullptr);
40*1031c584SApple OSS Distributions }
41*1031c584SApple OSS Distributions
42*1031c584SApple OSS Distributions // Test `const_pointer_cast`ing a null pointer
43*1031c584SApple OSS Distributions {
44*1031c584SApple OSS Distributions tracked_shared_ptr<From> const from = nullptr;
45*1031c584SApple OSS Distributions tracking_policy::reset();
46*1031c584SApple OSS Distributions tracked_shared_ptr<To> to = libkern::const_pointer_cast<To>(from);
47*1031c584SApple OSS Distributions CHECK(tracking_policy::retains == 0);
48*1031c584SApple OSS Distributions CHECK(tracking_policy::releases == 0);
49*1031c584SApple OSS Distributions CHECK(to.get() == nullptr);
50*1031c584SApple OSS Distributions CHECK(from.get() == nullptr);
51*1031c584SApple OSS Distributions }
52*1031c584SApple OSS Distributions {
53*1031c584SApple OSS Distributions tracked_shared_ptr<From> from = nullptr;
54*1031c584SApple OSS Distributions tracking_policy::reset();
55*1031c584SApple OSS Distributions tracked_shared_ptr<To> to = libkern::const_pointer_cast<To>(std::move(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
63*1031c584SApple OSS Distributions T_DECL(cast_const, "intrusive_shared_ptr.cast.const") {
64*1031c584SApple OSS Distributions tests</*stored*/ T, /*from*/ T, /*to*/ T>();
65*1031c584SApple OSS Distributions tests</*stored*/ T, /*from*/ T, /*to*/ T const>();
66*1031c584SApple OSS Distributions tests</*stored*/ T, /*from*/ T const, /*to*/ T>();
67*1031c584SApple OSS Distributions }
68