1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions // bounded_ptr& operator++();
4bb611c8fSApple OSS Distributions // bounded_ptr operator++(int);
5bb611c8fSApple OSS Distributions // bounded_ptr& operator--();
6bb611c8fSApple OSS Distributions // bounded_ptr operator--(int);
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 {
28bb611c8fSApple OSS Distributions // Test pre-increment and pre-decrement
29bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
30bb611c8fSApple OSS Distributions _assert(&*ptr == &array[0]);
31bb611c8fSApple OSS Distributions
32bb611c8fSApple OSS Distributions {
33bb611c8fSApple OSS Distributions auto& ref = ++ptr;
34bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
35bb611c8fSApple OSS Distributions _assert(&*ptr == &array[1]);
36bb611c8fSApple OSS Distributions }
37bb611c8fSApple OSS Distributions
38bb611c8fSApple OSS Distributions {
39bb611c8fSApple OSS Distributions auto& ref = ++ptr;
40bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
41bb611c8fSApple OSS Distributions _assert(&*ptr == &array[2]);
42bb611c8fSApple OSS Distributions }
43bb611c8fSApple OSS Distributions {
44bb611c8fSApple OSS Distributions auto& ref = ++ptr;
45bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
46bb611c8fSApple OSS Distributions _assert(&*ptr == &array[3]);
47bb611c8fSApple OSS Distributions }
48bb611c8fSApple OSS Distributions {
49bb611c8fSApple OSS Distributions auto& ref = ++ptr;
50bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
51bb611c8fSApple OSS Distributions _assert(&*ptr == &array[4]);
52bb611c8fSApple OSS Distributions }
53bb611c8fSApple OSS Distributions {
54bb611c8fSApple OSS Distributions auto& ref = ++ptr;
55bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
56bb611c8fSApple OSS Distributions // ptr is now one-past-last
57bb611c8fSApple OSS Distributions }
58bb611c8fSApple OSS Distributions {
59bb611c8fSApple OSS Distributions auto& ref = --ptr;
60bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
61bb611c8fSApple OSS Distributions _assert(&*ptr == &array[4]);
62bb611c8fSApple OSS Distributions }
63bb611c8fSApple OSS Distributions {
64bb611c8fSApple OSS Distributions auto& ref = --ptr;
65bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
66bb611c8fSApple OSS Distributions _assert(&*ptr == &array[3]);
67bb611c8fSApple OSS Distributions }
68bb611c8fSApple OSS Distributions {
69bb611c8fSApple OSS Distributions auto& ref = --ptr;
70bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
71bb611c8fSApple OSS Distributions _assert(&*ptr == &array[2]);
72bb611c8fSApple OSS Distributions }
73bb611c8fSApple OSS Distributions {
74bb611c8fSApple OSS Distributions auto& ref = --ptr;
75bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
76bb611c8fSApple OSS Distributions _assert(&*ptr == &array[1]);
77bb611c8fSApple OSS Distributions }
78bb611c8fSApple OSS Distributions {
79bb611c8fSApple OSS Distributions auto& ref = --ptr;
80bb611c8fSApple OSS Distributions _assert(&ref == &ptr);
81bb611c8fSApple OSS Distributions _assert(&*ptr == &array[0]);
82bb611c8fSApple OSS Distributions }
83bb611c8fSApple OSS Distributions }
84bb611c8fSApple OSS Distributions {
85bb611c8fSApple OSS Distributions // Test post-increment and post-decrement
86bb611c8fSApple OSS Distributions test_bounded_ptr<QualT> ptr(array.begin(), array.begin(), array.end());
87bb611c8fSApple OSS Distributions _assert(&*ptr == &array[0]);
88bb611c8fSApple OSS Distributions
89bb611c8fSApple OSS Distributions {
90bb611c8fSApple OSS Distributions auto prev = ptr++;
91bb611c8fSApple OSS Distributions _assert(&*prev == &array[0]);
92bb611c8fSApple OSS Distributions _assert(&*ptr == &array[1]);
93bb611c8fSApple OSS Distributions }
94bb611c8fSApple OSS Distributions {
95bb611c8fSApple OSS Distributions auto prev = ptr++;
96bb611c8fSApple OSS Distributions _assert(&*prev == &array[1]);
97bb611c8fSApple OSS Distributions _assert(&*ptr == &array[2]);
98bb611c8fSApple OSS Distributions }
99bb611c8fSApple OSS Distributions {
100bb611c8fSApple OSS Distributions auto prev = ptr++;
101bb611c8fSApple OSS Distributions _assert(&*prev == &array[2]);
102bb611c8fSApple OSS Distributions _assert(&*ptr == &array[3]);
103bb611c8fSApple OSS Distributions }
104bb611c8fSApple OSS Distributions {
105bb611c8fSApple OSS Distributions auto prev = ptr++;
106bb611c8fSApple OSS Distributions _assert(&*prev == &array[3]);
107bb611c8fSApple OSS Distributions _assert(&*ptr == &array[4]);
108bb611c8fSApple OSS Distributions }
109bb611c8fSApple OSS Distributions {
110bb611c8fSApple OSS Distributions auto prev = ptr++;
111bb611c8fSApple OSS Distributions _assert(&*prev == &array[4]);
112bb611c8fSApple OSS Distributions _assert(ptr == array.end());
113bb611c8fSApple OSS Distributions }
114bb611c8fSApple OSS Distributions {
115bb611c8fSApple OSS Distributions auto prev = ptr--;
116bb611c8fSApple OSS Distributions _assert(prev == array.end());
117bb611c8fSApple OSS Distributions _assert(&*ptr == &array[4]);
118bb611c8fSApple OSS Distributions }
119bb611c8fSApple OSS Distributions {
120bb611c8fSApple OSS Distributions auto prev = ptr--;
121bb611c8fSApple OSS Distributions _assert(&*prev == &array[4]);
122bb611c8fSApple OSS Distributions _assert(&*ptr == &array[3]);
123bb611c8fSApple OSS Distributions }
124bb611c8fSApple OSS Distributions {
125bb611c8fSApple OSS Distributions auto prev = ptr--;
126bb611c8fSApple OSS Distributions _assert(&*prev == &array[3]);
127bb611c8fSApple OSS Distributions _assert(&*ptr == &array[2]);
128bb611c8fSApple OSS Distributions }
129bb611c8fSApple OSS Distributions {
130bb611c8fSApple OSS Distributions auto prev = ptr--;
131bb611c8fSApple OSS Distributions _assert(&*prev == &array[2]);
132bb611c8fSApple OSS Distributions _assert(&*ptr == &array[1]);
133bb611c8fSApple OSS Distributions }
134bb611c8fSApple OSS Distributions {
135bb611c8fSApple OSS Distributions auto prev = ptr--;
136bb611c8fSApple OSS Distributions _assert(&*prev == &array[1]);
137bb611c8fSApple OSS Distributions _assert(&*ptr == &array[0]);
138bb611c8fSApple OSS Distributions }
139bb611c8fSApple OSS Distributions }
140bb611c8fSApple OSS Distributions }
141bb611c8fSApple OSS Distributions
142*8d741a5dSApple OSS Distributions T_DECL(arith_inc_dec, "bounded_ptr.arith.inc_dec", T_META_TAG_VM_PREFERRED) {
143bb611c8fSApple OSS Distributions tests<T, T>();
144bb611c8fSApple OSS Distributions tests<T, T const>();
145bb611c8fSApple OSS Distributions tests<T, T volatile>();
146bb611c8fSApple OSS Distributions tests<T, T const volatile>();
147bb611c8fSApple OSS Distributions }
148