]>
git.saurik.com Git - apple/xnu.git/blob - tests/bounded_ptr_src/ctor.convert.cpp
3 // template <typename U, typename Policy>
4 // bounded_ptr(bounded_ptr<U, Policy> const& other);
7 #include <libkern/c++/bounded_ptr.h>
10 #include <darwintest.h>
11 #include <darwintest_utils.h>
12 #include "test_utils.h"
14 #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
16 struct Base
{ int i
; };
17 struct Derived
: Base
{ };
19 struct Base1
{ int i
; };
20 struct Base2
{ long l
; };
21 struct DerivedMultiple
: Base1
, Base2
{
22 DerivedMultiple(int i
) : Base1
{i
}, Base2
{i
+ 10}
29 struct dummy_policy1
{
35 struct dummy_policy2
{
42 template <typename Stored
, typename From
, typename To
>
46 std::array
<Stored
, 5> array
= {Stored
{0}, Stored
{1}, Stored
{2}, Stored
{3}, Stored
{4}};
47 Stored
* const ptr
= array
.begin() + 2;
50 test_bounded_ptr
<From
> const from(ptr
, array
.begin(), array
.end());
51 test_bounded_ptr
<To
> to
= from
; // conversion (implicit)
52 _assert(to
.discard_bounds() == static_cast<To
const*>(ptr
));
55 test_bounded_ptr
<From
> const from(ptr
, array
.begin(), array
.end());
56 test_bounded_ptr
<To
> to(from
); // conversion (explicit)
57 _assert(to
.discard_bounds() == static_cast<To
const*>(ptr
));
60 test_bounded_ptr
<From
> const from(ptr
, array
.begin(), array
.end());
61 test_bounded_ptr
<To
> to
{from
}; // conversion (explicit)
62 _assert(to
.discard_bounds() == static_cast<To
const*>(ptr
));
65 test_bounded_ptr
<From
> const from(ptr
, array
.begin(), array
.end());
66 test_bounded_ptr
<To
> to
= static_cast<test_bounded_ptr
<To
> >(from
); // conversion (explicit)
67 _assert(to
.discard_bounds() == static_cast<To
const*>(ptr
));
70 // Test converting from a null pointer
72 test_bounded_ptr
<From
> from
= nullptr;
73 test_bounded_ptr
<To
> to
= from
; // conversion (implicit)
74 _assert(to
.unsafe_discard_bounds() == nullptr);
77 // Test with different policies
79 libkern::bounded_ptr
<From
, dummy_policy1
> from(ptr
, array
.begin(), array
.end());
80 libkern::bounded_ptr
<To
, dummy_policy2
> to
= from
; // conversion (implicit)
81 _assert(to
.discard_bounds() == static_cast<To
const*>(ptr
));
84 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
89 test_bounded_ptr
<From
> const from(array
.begin(), array
.begin() + 1, array
.end());
90 test_bounded_ptr
<To
> to(from
);
91 _assert(to
.unsafe_discard_bounds() == static_cast<To
const*>(array
.begin()));
95 T_DECL(ctor_convert
, "bounded_ptr.ctor.convert") {
96 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ Derived
>();
97 tests
</*stored*/ Derived
, /*from*/ Derived
const, /*to*/ Derived
const>();
98 tests
</*stored*/ Derived
, /*from*/ Derived
volatile, /*to*/ Derived
volatile>();
99 tests
</*stored*/ Derived
, /*from*/ Derived
const volatile, /*to*/ Derived
const volatile>();
101 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ Base
>();
102 tests
</*stored*/ Derived
, /*from*/ Derived
const, /*to*/ Base
const>();
103 tests
</*stored*/ Derived
, /*from*/ Derived
volatile, /*to*/ Base
volatile>();
104 tests
</*stored*/ Derived
, /*from*/ Derived
const volatile, /*to*/ Base
const volatile>();
106 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
, /*to*/ Base1
>();
107 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const, /*to*/ Base1
const>();
108 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
volatile, /*to*/ Base1
volatile>();
109 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const volatile, /*to*/ Base1
const volatile>();
111 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
, /*to*/ Base2
>();
112 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const, /*to*/ Base2
const>();
113 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
volatile, /*to*/ Base2
volatile>();
114 tests
</*stored*/ DerivedMultiple
, /*from*/ DerivedMultiple
const volatile, /*to*/ Base2
const volatile>();
116 tests
</*stored*/ Derived
, /*from*/ Derived
, /*to*/ void>();
117 tests
</*stored*/ Derived
, /*from*/ Derived
const, /*to*/ void const>();
118 tests
</*stored*/ Derived
, /*from*/ Derived
volatile, /*to*/ void volatile>();
119 tests
</*stored*/ Derived
, /*from*/ Derived
const volatile, /*to*/ void const volatile>();
121 // Make sure downcasts are disabled
122 static_assert(!std::is_convertible_v
</*from*/ test_bounded_ptr
<Base
>, /*to*/ test_bounded_ptr
<Derived
> >);
123 static_assert(!std::is_convertible_v
</*from*/ test_bounded_ptr
<Base1
>, /*to*/ test_bounded_ptr
<DerivedMultiple
> >);
124 static_assert(!std::is_convertible_v
</*from*/ test_bounded_ptr
<Base2
>, /*to*/ test_bounded_ptr
<DerivedMultiple
> >);
125 static_assert(!std::is_convertible_v
</*from*/ test_bounded_ptr
<Base1
>, /*to*/ test_bounded_ptr
<Base2
> >);
127 // Make sure const-casting away doesn't work
128 static_assert(!std::is_convertible_v
</*from*/ test_bounded_ptr
<Derived
const>, /*to*/ test_bounded_ptr
<Derived
> >);
130 // Make sure casting to unrelated types doesn't work implicitly
131 static_assert(!std::is_convertible_v
</*from*/ test_bounded_ptr
<Derived
>, /*to*/ test_bounded_ptr
<char> >);
132 static_assert(!std::is_convertible_v
</*from*/ test_bounded_ptr
<Derived
>, /*to*/ test_bounded_ptr
<Unrelated
> >);
133 static_assert(!std::is_convertible_v
</*from*/ test_bounded_ptr
<Base1
>, /*to*/ test_bounded_ptr
<Base2
> >);
135 // Make sure even explicit conversion to unrelated types doesn't work
136 static_assert(!std::is_constructible_v
</*to*/ test_bounded_ptr
<char>, /*from*/ test_bounded_ptr
<Derived
> >);
137 static_assert(!std::is_constructible_v
</*to*/ test_bounded_ptr
<Unrelated
>, /*from*/ test_bounded_ptr
<Derived
> >);
138 static_assert(!std::is_constructible_v
</*to*/ test_bounded_ptr
<Base2
>, /*from*/ test_bounded_ptr
<Base1
> >);
140 // Make sure construction from a raw pointer doesn't work
141 static_assert(!std::is_constructible_v
</*to*/ test_bounded_ptr
<Derived
>, /*from*/ Derived
*>);