]>
git.saurik.com Git - apple/xnu.git/blob - tests/intrusive_shared_ptr_src/cast.const.cpp
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;
6 // template<typename To, typename From, typename R>
7 // intrusive_shared_ptr<To, R> const_pointer_cast(intrusive_shared_ptr<From, R>&& ptr) noexcept
10 #include <libkern/c++/intrusive_shared_ptr.h>
12 #include <darwintest.h>
13 #include "test_policy.h"
17 template <typename Stored
, typename From
, typename To
>
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
);
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);
42 // Test `const_pointer_cast`ing a null pointer
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);
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);
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
>();