]>
git.saurik.com Git - apple/xnu.git/blob - tests/bounded_ptr_src/arith.add.cpp
3 // friend bounded_ptr operator+(bounded_ptr p, std::ptrdiff_t n);
4 // friend bounded_ptr operator+(std::ptrdiff_t n, bounded_ptr p);
6 // The heavy lifting is done in operator+=, so we only check basic functioning.
9 #include <libkern/c++/bounded_ptr.h>
11 #include <darwintest.h>
12 #include <darwintest_utils.h>
13 #include "test_utils.h"
15 #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
21 template <typename T
, typename QualT
>
25 std::array
<T
, 5> array
= {T
{0}, T
{1}, T
{2}, T
{3}, T
{4}};
27 // Add positive offsets
28 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
33 test_bounded_ptr
<QualT
> const ptr(array
.begin(), array
.begin(), array
.end());
36 test_bounded_ptr
<QualT
> res
= ptr
+ 0;
37 _assert(&*res
== &array
[0]);
40 test_bounded_ptr
<QualT
> res
= ptr
+ 1;
41 _assert(&*res
== &array
[1]);
44 test_bounded_ptr
<QualT
> res
= ptr
+ 2;
45 _assert(&*res
== &array
[2]);
48 test_bounded_ptr
<QualT
> res
= ptr
+ 3;
49 _assert(&*res
== &array
[3]);
52 test_bounded_ptr
<QualT
> res
= ptr
+ 4;
53 _assert(&*res
== &array
[4]);
56 test_bounded_ptr
<QualT
> res
= ptr
+ 5;
57 _assert(res
== array
.end());
61 // Add negative offsets
62 // T{0} T{1} T{2} T{3} T{4} <one-past-last>
67 test_bounded_ptr
<QualT
> const ptr(array
.end(), array
.begin(), array
.end());
70 test_bounded_ptr
<QualT
> res
= ptr
+ 0;
71 _assert(res
== array
.end());
74 test_bounded_ptr
<QualT
> res
= ptr
+ -1;
75 _assert(&*res
== &array
[4]);
78 test_bounded_ptr
<QualT
> res
= ptr
+ -2;
79 _assert(&*res
== &array
[3]);
82 test_bounded_ptr
<QualT
> res
= ptr
+ -3;
83 _assert(&*res
== &array
[2]);
86 test_bounded_ptr
<QualT
> res
= ptr
+ -4;
87 _assert(&*res
== &array
[1]);
90 test_bounded_ptr
<QualT
> res
= ptr
+ -5;
91 _assert(&*res
== &array
[0]);
95 // Make sure the original pointer isn't modified
97 test_bounded_ptr
<QualT
> const ptr(array
.begin() + 1, array
.begin(), array
.end());
99 _assert(&*ptr
== &array
[1]);
102 // Make sure the operator is commutative
105 test_bounded_ptr
<QualT
> const ptr(array
.begin(), array
.begin(), array
.end());
106 test_bounded_ptr
<QualT
> res
= 0 + ptr
;
107 _assert(&*res
== &array
[0]);
110 test_bounded_ptr
<QualT
> const ptr(array
.begin(), array
.begin(), array
.end());
111 test_bounded_ptr
<QualT
> res
= 3 + ptr
;
112 _assert(&*res
== &array
[3]);
115 test_bounded_ptr
<QualT
> const ptr(array
.begin() + 3, array
.begin(), array
.end());
116 test_bounded_ptr
<QualT
> res
= -2 + ptr
;
117 _assert(&*res
== &array
[1]);
122 T_DECL(arith_add
, "bounded_ptr.arith.add") {
125 tests
<T
, T
volatile>();
126 tests
<T
, T
const volatile>();