1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions // friend bounded_ptr operator+(bounded_ptr p, std::ptrdiff_t n);
4bb611c8fSApple OSS Distributions // friend bounded_ptr operator+(std::ptrdiff_t n, bounded_ptr p);
5bb611c8fSApple OSS Distributions //
6bb611c8fSApple OSS Distributions // The heavy lifting is done in operator+=, so we only check basic functioning.
7bb611c8fSApple OSS Distributions //
8bb611c8fSApple OSS Distributions
9bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
10bb611c8fSApple OSS Distributions #include <array>
11bb611c8fSApple OSS Distributions #include <darwintest.h>
12bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
13bb611c8fSApple OSS Distributions #include "test_utils.h"
14bb611c8fSApple OSS Distributions
15bb611c8fSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
16bb611c8fSApple OSS Distributions
17bb611c8fSApple OSS Distributions struct T {
18bb611c8fSApple OSS Distributions int i;
19bb611c8fSApple OSS Distributions };
20bb611c8fSApple OSS Distributions
21bb611c8fSApple OSS Distributions template <typename T, typename QualT>
22bb611c8fSApple OSS Distributions static void
tests()23bb611c8fSApple OSS Distributions tests()
24bb611c8fSApple OSS Distributions {
25bb611c8fSApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
26bb611c8fSApple OSS Distributions
27bb611c8fSApple OSS Distributions // Add positive offsets
28bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
29bb611c8fSApple OSS Distributions // ^ ^
30bb611c8fSApple OSS Distributions // | |
31bb611c8fSApple OSS Distributions // begin, ptr end
32bb611c8fSApple OSS Distributions {
33bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end());
34bb611c8fSApple OSS Distributions
35bb611c8fSApple OSS Distributions {
36bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + 0;
37bb611c8fSApple OSS Distributions _assert(&*res == &array[0]);
38bb611c8fSApple OSS Distributions }
39bb611c8fSApple OSS Distributions {
40bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + 1;
41bb611c8fSApple OSS Distributions _assert(&*res == &array[1]);
42bb611c8fSApple OSS Distributions }
43bb611c8fSApple OSS Distributions {
44bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + 2;
45bb611c8fSApple OSS Distributions _assert(&*res == &array[2]);
46bb611c8fSApple OSS Distributions }
47bb611c8fSApple OSS Distributions {
48bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + 3;
49bb611c8fSApple OSS Distributions _assert(&*res == &array[3]);
50bb611c8fSApple OSS Distributions }
51bb611c8fSApple OSS Distributions {
52bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + 4;
53bb611c8fSApple OSS Distributions _assert(&*res == &array[4]);
54bb611c8fSApple OSS Distributions }
55bb611c8fSApple OSS Distributions {
56bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + 5;
57bb611c8fSApple OSS Distributions _assert(res == array.end());
58bb611c8fSApple OSS Distributions }
59bb611c8fSApple OSS Distributions }
60bb611c8fSApple OSS Distributions
61bb611c8fSApple OSS Distributions // Add negative offsets
62bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
63bb611c8fSApple OSS Distributions // ^ ^
64bb611c8fSApple OSS Distributions // | |
65bb611c8fSApple OSS Distributions // begin end,ptr
66bb611c8fSApple OSS Distributions {
67bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.end(), array.begin(), array.end());
68bb611c8fSApple OSS Distributions
69bb611c8fSApple OSS Distributions {
70bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + 0;
71bb611c8fSApple OSS Distributions _assert(res == array.end());
72bb611c8fSApple OSS Distributions }
73bb611c8fSApple OSS Distributions {
74bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + -1;
75bb611c8fSApple OSS Distributions _assert(&*res == &array[4]);
76bb611c8fSApple OSS Distributions }
77bb611c8fSApple OSS Distributions {
78bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + -2;
79bb611c8fSApple OSS Distributions _assert(&*res == &array[3]);
80bb611c8fSApple OSS Distributions }
81bb611c8fSApple OSS Distributions {
82bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + -3;
83bb611c8fSApple OSS Distributions _assert(&*res == &array[2]);
84bb611c8fSApple OSS Distributions }
85bb611c8fSApple OSS Distributions {
86bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + -4;
87bb611c8fSApple OSS Distributions _assert(&*res == &array[1]);
88bb611c8fSApple OSS Distributions }
89bb611c8fSApple OSS Distributions {
90bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = ptr + -5;
91bb611c8fSApple OSS Distributions _assert(&*res == &array[0]);
92bb611c8fSApple OSS Distributions }
93bb611c8fSApple OSS Distributions }
94bb611c8fSApple OSS Distributions
95bb611c8fSApple OSS Distributions // Make sure the original pointer isn't modified
96bb611c8fSApple OSS Distributions {
97bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin() + 1, array.begin(), array.end());
98bb611c8fSApple OSS Distributions (void)(ptr + 3);
99bb611c8fSApple OSS Distributions _assert(&*ptr == &array[1]);
100bb611c8fSApple OSS Distributions }
101bb611c8fSApple OSS Distributions
102bb611c8fSApple OSS Distributions // Make sure the operator is commutative
103bb611c8fSApple OSS Distributions {
104bb611c8fSApple OSS Distributions {
105bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end());
106bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = 0 + ptr;
107bb611c8fSApple OSS Distributions _assert(&*res == &array[0]);
108bb611c8fSApple OSS Distributions }
109bb611c8fSApple OSS Distributions {
110bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end());
111bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = 3 + ptr;
112bb611c8fSApple OSS Distributions _assert(&*res == &array[3]);
113bb611c8fSApple OSS Distributions }
114bb611c8fSApple OSS Distributions {
115bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin() + 3, array.begin(), array.end());
116bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> res = -2 + ptr;
117bb611c8fSApple OSS Distributions _assert(&*res == &array[1]);
118bb611c8fSApple OSS Distributions }
119bb611c8fSApple OSS Distributions }
120bb611c8fSApple OSS Distributions }
121bb611c8fSApple OSS Distributions
122*8d741a5dSApple OSS Distributions T_DECL(arith_add, "bounded_ptr.arith.add", T_META_TAG_VM_PREFERRED) {
123bb611c8fSApple OSS Distributions tests<T, T>();
124bb611c8fSApple OSS Distributions tests<T, T const>();
125bb611c8fSApple OSS Distributions tests<T, T volatile>();
126bb611c8fSApple OSS Distributions tests<T, T const volatile>();
127bb611c8fSApple OSS Distributions }
128