]>
git.saurik.com Git - apple/xnu.git/blob - tests/bounded_ptr_src/arith.subtract.cpp
3 // friend bounded_ptr operator-(bounded_ptr p, std::ptrdiff_t n);
6 #include <libkern/c++/bounded_ptr.h>
7 #include "test_utils.h"
10 #include <darwintest.h>
11 #include <darwintest_utils.h>
13 #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
19 template <typename T
, typename QualT
>
23 std::array
<T
, 5> array
= {T
{0}, T
{1}, T
{2}, T
{3}, T
{4}};
25 // Subtract positive offsets
26 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
31 test_bounded_ptr
<QualT
> const ptr(array
.end(), array
.begin(), array
.end());
34 test_bounded_ptr
<QualT
> res
= ptr
- static_cast<std::ptrdiff_t>(0);
35 _assert(ptr
== array
.end());
38 test_bounded_ptr
<QualT
> res
= ptr
- 1;
39 _assert(&*res
== &array
[4]);
42 test_bounded_ptr
<QualT
> res
= ptr
- 2;
43 _assert(&*res
== &array
[3]);
46 test_bounded_ptr
<QualT
> res
= ptr
- 3;
47 _assert(&*res
== &array
[2]);
50 test_bounded_ptr
<QualT
> res
= ptr
- 4;
51 _assert(&*res
== &array
[1]);
54 test_bounded_ptr
<QualT
> res
= ptr
- 5;
55 _assert(&*res
== &array
[0]);
59 // Subtract negative offsets
60 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
65 test_bounded_ptr
<QualT
> const ptr(array
.begin(), array
.begin(), array
.end());
68 test_bounded_ptr
<QualT
> res
= ptr
- static_cast<std::ptrdiff_t>(0);
69 _assert(&*res
== &array
[0]);
72 test_bounded_ptr
<QualT
> res
= ptr
- -1;
73 _assert(&*res
== &array
[1]);
76 test_bounded_ptr
<QualT
> res
= ptr
- -2;
77 _assert(&*res
== &array
[2]);
80 test_bounded_ptr
<QualT
> res
= ptr
- -3;
81 _assert(&*res
== &array
[3]);
84 test_bounded_ptr
<QualT
> res
= ptr
- -4;
85 _assert(&*res
== &array
[4]);
88 test_bounded_ptr
<QualT
> res
= ptr
- -5;
89 _assert(res
== array
.end());
93 // Make sure the original pointer isn't modified
95 test_bounded_ptr
<QualT
> const ptr(array
.begin() + 4, array
.begin(), array
.end());
97 _assert(&*ptr
== &array
[4]);
101 T_DECL(arith_subtract
, "bounded_ptr.arith.subtract") {
104 tests
<T
, T
volatile>();
105 tests
<T
, T
const volatile>();