]>
git.saurik.com Git - apple/xnu.git/blob - tests/bounded_ptr_src/subscript.cpp
3 // T& operator[](std::ptrdiff_t n) const;
6 #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__)
18 operator==(T
const& a
, T
const& b
)
25 struct tracking_policy
{
33 bool tracking_policy::did_trap
= false;
36 template <typename T
, typename QualT
>
40 std::array
<T
, 5> array
= {T
{0}, T
{1}, T
{2}, T
{3}, T
{4}};
43 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
47 test_bounded_ptr
<QualT
> ptr(array
.begin() + 0, array
.begin(), array
.end());
49 _assert(&ref0
== &array
[0]);
52 _assert(&ref1
== &array
[1]);
55 _assert(&ref2
== &array
[2]);
58 _assert(&ref3
== &array
[3]);
61 _assert(&ref4
== &array
[4]);
64 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
68 test_bounded_ptr
<QualT
> ptr(array
.begin() + 1, array
.begin(), array
.end());
69 QualT
& ref0
= ptr
[-1];
70 _assert(&ref0
== &array
[0]);
73 _assert(&ref1
== &array
[1]);
76 _assert(&ref2
== &array
[2]);
79 _assert(&ref3
== &array
[3]);
82 _assert(&ref4
== &array
[4]);
85 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
89 test_bounded_ptr
<QualT
> ptr(array
.begin() + 2, array
.begin(), array
.end());
90 QualT
& ref0
= ptr
[-2];
91 _assert(&ref0
== &array
[0]);
93 QualT
& ref1
= ptr
[-1];
94 _assert(&ref1
== &array
[1]);
97 _assert(&ref2
== &array
[2]);
100 _assert(&ref3
== &array
[3]);
102 QualT
& ref4
= ptr
[2];
103 _assert(&ref4
== &array
[4]);
106 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
110 test_bounded_ptr
<QualT
> ptr(array
.begin() + 4, array
.begin(), array
.end());
111 QualT
& ref0
= ptr
[-4];
112 _assert(&ref0
== &array
[0]);
114 QualT
& ref1
= ptr
[-3];
115 _assert(&ref1
== &array
[1]);
117 QualT
& ref2
= ptr
[-2];
118 _assert(&ref2
== &array
[2]);
120 QualT
& ref3
= ptr
[-1];
121 _assert(&ref3
== &array
[3]);
123 QualT
& ref4
= ptr
[0];
124 _assert(&ref4
== &array
[4]);
127 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
131 test_bounded_ptr
<QualT
> ptr(array
.end(), array
.begin(), array
.end());
132 QualT
& ref0
= ptr
[-5];
133 _assert(&ref0
== &array
[0]);
135 QualT
& ref1
= ptr
[-4];
136 _assert(&ref1
== &array
[1]);
138 QualT
& ref2
= ptr
[-3];
139 _assert(&ref2
== &array
[2]);
141 QualT
& ref3
= ptr
[-2];
142 _assert(&ref3
== &array
[3]);
144 QualT
& ref4
= ptr
[-1];
145 _assert(&ref4
== &array
[4]);
148 // Make sure we trap when we subscript a pointer at an out-of-bounds offset
150 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
154 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.end() - 1, array
.begin(), array
.end() - 2);
156 tracking_policy::did_trap
= false;
158 _assert(!tracking_policy::did_trap
);
160 tracking_policy::did_trap
= false;
162 _assert(!tracking_policy::did_trap
);
164 tracking_policy::did_trap
= false;
166 _assert(!tracking_policy::did_trap
);
168 tracking_policy::did_trap
= false;
169 (void)ptr
[-1]; // trap
170 _assert(tracking_policy::did_trap
);
172 tracking_policy::did_trap
= false;
173 (void)ptr
[0]; // trap
174 _assert(tracking_policy::did_trap
);
177 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
181 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.begin() + 1, array
.begin(), array
.end());
183 tracking_policy::did_trap
= false;
185 _assert(!tracking_policy::did_trap
);
187 tracking_policy::did_trap
= false;
189 _assert(!tracking_policy::did_trap
);
191 tracking_policy::did_trap
= false;
193 _assert(!tracking_policy::did_trap
);
195 tracking_policy::did_trap
= false;
197 _assert(!tracking_policy::did_trap
);
199 tracking_policy::did_trap
= false;
201 _assert(!tracking_policy::did_trap
);
203 tracking_policy::did_trap
= false;
204 (void)ptr
[4]; // trap
205 _assert(tracking_policy::did_trap
);
208 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
212 libkern::bounded_ptr
<QualT
, tracking_policy
> ptr(array
.begin(), array
.begin() + 1, array
.end() - 1);
214 tracking_policy::did_trap
= false;
215 (void)ptr
[0]; // trap
216 _assert(tracking_policy::did_trap
);
218 tracking_policy::did_trap
= false;
220 _assert(!tracking_policy::did_trap
);
222 tracking_policy::did_trap
= false;
224 _assert(!tracking_policy::did_trap
);
226 tracking_policy::did_trap
= false;
228 _assert(!tracking_policy::did_trap
);
230 tracking_policy::did_trap
= false;
231 (void)ptr
[4]; // trap
232 _assert(tracking_policy::did_trap
);
234 tracking_policy::did_trap
= false;
235 (void)ptr
[5]; // trap
236 _assert(tracking_policy::did_trap
);
240 T_DECL(subscript
, "bounded_ptr.subscript") {
243 tests
<T
, T
volatile>();
244 tests
<T
, T
const volatile>();
246 // Make sure that we don't hard-error in the definition of operator[]
247 // when instantiating a `bounded_ptr<cv-void>`
248 test_bounded_ptr
<void> p1
;
249 test_bounded_ptr
<void const> p2
;
250 test_bounded_ptr
<void volatile> p3
;
251 test_bounded_ptr
<void const volatile> p4
;