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