]>
git.saurik.com Git - apple/xnu.git/blob - tests/bounded_ptr_src/ctor.begin_end.cpp
3 // explicit bounded_ptr(T* pointer, T const* begin, T const* end);
6 #include <libkern/c++/bounded_ptr.h>
8 #include <darwintest.h>
9 #include <darwintest_utils.h>
10 #include "test_utils.h"
12 #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
17 operator==(T
const volatile& a
, T
const& b
)
23 template <typename T
, typename QualT
>
27 std::array
<T
, 5> array
= {T
{0}, T
{1}, T
{2}, T
{3}, T
{4}};
29 test_bounded_ptr
<QualT
> p(array
.begin() + 0, array
.begin(), array
.end());
33 test_bounded_ptr
<QualT
> p(array
.begin() + 1, array
.begin(), array
.end());
37 test_bounded_ptr
<QualT
> p(array
.begin() + 2, array
.begin(), array
.end());
41 test_bounded_ptr
<QualT
> p(array
.begin() + 3, array
.begin(), array
.end());
45 test_bounded_ptr
<QualT
> p(array
.begin() + 4, array
.begin(), array
.end());
49 // It must be valid to construct out-of-bounds pointers, but we obviously
50 // can't dereference them.
52 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
56 test_bounded_ptr
<QualT
> p(array
.begin() + 1, array
.begin() + 3, array
.end());
57 _assert(p
.unsafe_discard_bounds() == array
.begin() + 1);
60 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
64 test_bounded_ptr
<QualT
> p(array
.begin() + 4, array
.begin(), array
.begin() + 3);
65 _assert(p
.unsafe_discard_bounds() == array
.begin() + 4);
68 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
72 test_bounded_ptr
<QualT
> p(array
.end(), array
.begin(), array
.end());
73 _assert(p
.unsafe_discard_bounds() == array
.end());
76 // Test creating a bounded_ptr from a null pointer.
78 test_bounded_ptr
<QualT
> p(nullptr, nullptr, nullptr);
79 _assert(p
.unsafe_discard_bounds() == nullptr);
84 struct Derived
: Base
{ };
86 T_DECL(ctor_begin_end
, "bounded_ptr.ctor.begin_end") {
89 tests
<T
, T
volatile>();
90 tests
<T
, T
const volatile>();
92 // Make sure we can construct a `bounded_ptr<Base>` from `Derived*` pointers
94 std::array
<Derived
, 5> array
= {};
95 test_bounded_ptr
<Base
> p(static_cast<Derived
*>(array
.begin()),
96 static_cast<Derived
*>(array
.begin()),
97 static_cast<Derived
*>(array
.end()));