]>
git.saurik.com Git - apple/xnu.git/blob - tests/bounded_ptr_src/reinterpret_cast.cpp
3 // template <typename T, typename U, typename Policy>
4 // bounded_ptr<T, Policy> reinterpret_pointer_cast(bounded_ptr<U, Policy> const& p) noexcept
7 #include <libkern/c++/bounded_ptr.h>
9 #include <darwintest.h>
10 #include <darwintest_utils.h>
11 #include "test_utils.h"
13 #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
15 struct Base
{ int i
; };
16 struct Derived
: Base
{ };
18 struct Base1
{ int i
; };
19 struct Base2
{ long l
; };
20 struct DerivedMultiple
: Base1
, Base2
{
21 DerivedMultiple(int i
) : Base1
{i
}, Base2
{i
+ 10}
26 struct non_default_policy
{
33 template <typename Stored
, typename From
, typename To
>
37 std::array
<Stored
, 5> array
= {Stored
{0}, Stored
{1}, Stored
{2}, Stored
{3}, Stored
{4}};
40 test_bounded_ptr
<From
> from(array
.begin() + 2, array
.begin(), array
.end());
41 test_bounded_ptr
<To
> to
= libkern::reinterpret_pointer_cast
<To
>(from
);
42 _assert(to
.discard_bounds() == reinterpret_cast<To
const*>(from
.discard_bounds()));
46 test_bounded_ptr
<From
> from(array
.begin() + 2, array
.begin(), array
.end());
47 test_bounded_ptr
<To
> to
= libkern::reinterpret_pointer_cast
<To
>(from
);
48 _assert(to
.discard_bounds() == reinterpret_cast<To
const volatile*>(from
.discard_bounds()));
51 // Test `reinterpret_pointer_cast`ing a null pointer
53 test_bounded_ptr
<From
> from(nullptr, nullptr, nullptr);
54 test_bounded_ptr
<To
> to
= libkern::reinterpret_pointer_cast
<To
>(from
);
55 _assert(to
.unsafe_discard_bounds() == nullptr);
58 // Test with a non-default policy
60 libkern::bounded_ptr
<From
, non_default_policy
> from(array
.begin(), array
.begin(), array
.end());
61 libkern::bounded_ptr
<To
, non_default_policy
> to
= libkern::reinterpret_pointer_cast
<To
>(from
);
62 _assert(to
.discard_bounds() == reinterpret_cast<To
const*>(from
.discard_bounds()));
66 T_DECL(reinterpret_cast_
, "bounded_ptr.reinterpret_cast") {
67 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ Base
>();
68 tests
</*stored*/ Derived
, /*from*/ Derived
const, /*to*/ Base
const>();
69 tests
</*stored*/ Derived
, /*from*/ Derived
volatile, /*to*/ Base
volatile>();
70 tests
</*stored*/ Derived
, /*from*/ Derived
const volatile, /*to*/ Base
const volatile>();
72 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
, /*to*/ Base1
>();
73 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const, /*to*/ Base1
const>();
74 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
volatile, /*to*/ Base1
volatile>();
75 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const volatile, /*to*/ Base1
const volatile>();
77 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ void>();
78 tests
</*stored*/ Derived
, /*from*/ Derived
const, /*to*/ void const>();
79 tests
</*stored*/ Derived
, /*from*/ Derived
volatile, /*to*/ void volatile>();
80 tests
</*stored*/ Derived
, /*from*/ Derived
const volatile, /*to*/ void const volatile>();
82 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ char>();
83 tests
</*stored*/ Derived
, /*from*/ Derived
const, /*to*/ char const>();
84 tests
</*stored*/ Derived
, /*from*/ Derived
volatile, /*to*/ char volatile>();
85 tests
</*stored*/ Derived
, /*from*/ Derived
const volatile, /*to*/ char const volatile>();