1*19c3b8c2SApple OSS Distributions //
2*19c3b8c2SApple OSS Distributions // Tests for
3*19c3b8c2SApple OSS Distributions // template <typename T, typename U, typename P>
4*19c3b8c2SApple OSS Distributions // bool operator<(T* a, bounded_ptr<U, P> const& b);
5*19c3b8c2SApple OSS Distributions //
6*19c3b8c2SApple OSS Distributions // template <typename T, typename U, typename P>
7*19c3b8c2SApple OSS Distributions // bool operator<(bounded_ptr<T, P> const& a, U* b);
8*19c3b8c2SApple OSS Distributions //
9*19c3b8c2SApple OSS Distributions // template <typename T, typename U, typename P>
10*19c3b8c2SApple OSS Distributions // bool operator<=(T* a, bounded_ptr<U, P> const& b);
11*19c3b8c2SApple OSS Distributions //
12*19c3b8c2SApple OSS Distributions // template <typename T, typename U, typename P>
13*19c3b8c2SApple OSS Distributions // bool operator<=(bounded_ptr<T, P> const& a, U* b);
14*19c3b8c2SApple OSS Distributions //
15*19c3b8c2SApple OSS Distributions // template <typename T, typename U, typename P>
16*19c3b8c2SApple OSS Distributions // bool operator>(T* a, bounded_ptr<U, P> const& b);
17*19c3b8c2SApple OSS Distributions //
18*19c3b8c2SApple OSS Distributions // template <typename T, typename U, typename P>
19*19c3b8c2SApple OSS Distributions // bool operator>(bounded_ptr<T, P> const& a, U* b);
20*19c3b8c2SApple OSS Distributions //
21*19c3b8c2SApple OSS Distributions // template <typename T, typename U, typename P>
22*19c3b8c2SApple OSS Distributions // bool operator>=(T* a, bounded_ptr<U, P> const& b);
23*19c3b8c2SApple OSS Distributions //
24*19c3b8c2SApple OSS Distributions // template <typename T, typename U, typename P>
25*19c3b8c2SApple OSS Distributions // bool operator>=(bounded_ptr<T, P> const& a, U* b);
26*19c3b8c2SApple OSS Distributions //
27*19c3b8c2SApple OSS Distributions
28*19c3b8c2SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
29*19c3b8c2SApple OSS Distributions #include <array>
30*19c3b8c2SApple OSS Distributions #include <darwintest.h>
31*19c3b8c2SApple OSS Distributions #include <darwintest_utils.h>
32*19c3b8c2SApple OSS Distributions #include "test_utils.h"
33*19c3b8c2SApple OSS Distributions
34*19c3b8c2SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
35*19c3b8c2SApple OSS Distributions
36*19c3b8c2SApple OSS Distributions template <typename T, typename U>
37*19c3b8c2SApple OSS Distributions static void
check_lt(T t,U u)38*19c3b8c2SApple OSS Distributions check_lt(T t, U u)
39*19c3b8c2SApple OSS Distributions {
40*19c3b8c2SApple OSS Distributions _assert(t < u);
41*19c3b8c2SApple OSS Distributions _assert(t <= u);
42*19c3b8c2SApple OSS Distributions _assert(!(t >= u));
43*19c3b8c2SApple OSS Distributions _assert(!(t > u));
44*19c3b8c2SApple OSS Distributions
45*19c3b8c2SApple OSS Distributions _assert(!(u < t));
46*19c3b8c2SApple OSS Distributions _assert(!(u <= t));
47*19c3b8c2SApple OSS Distributions _assert(u > t);
48*19c3b8c2SApple OSS Distributions _assert(u >= t);
49*19c3b8c2SApple OSS Distributions }
50*19c3b8c2SApple OSS Distributions
51*19c3b8c2SApple OSS Distributions template <typename T, typename U>
52*19c3b8c2SApple OSS Distributions static void
check_eq(T t,U u)53*19c3b8c2SApple OSS Distributions check_eq(T t, U u)
54*19c3b8c2SApple OSS Distributions {
55*19c3b8c2SApple OSS Distributions _assert(!(t < u));
56*19c3b8c2SApple OSS Distributions _assert(t <= u);
57*19c3b8c2SApple OSS Distributions _assert(t >= u);
58*19c3b8c2SApple OSS Distributions _assert(!(t > u));
59*19c3b8c2SApple OSS Distributions
60*19c3b8c2SApple OSS Distributions _assert(!(u < t));
61*19c3b8c2SApple OSS Distributions _assert(u <= t);
62*19c3b8c2SApple OSS Distributions _assert(!(u > t));
63*19c3b8c2SApple OSS Distributions _assert(u >= t);
64*19c3b8c2SApple OSS Distributions }
65*19c3b8c2SApple OSS Distributions
66*19c3b8c2SApple OSS Distributions template <typename T, typename TQual>
67*19c3b8c2SApple OSS Distributions static void
tests()68*19c3b8c2SApple OSS Distributions tests()
69*19c3b8c2SApple OSS Distributions {
70*19c3b8c2SApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
71*19c3b8c2SApple OSS Distributions
72*19c3b8c2SApple OSS Distributions // Compare pointers within the bounds
73*19c3b8c2SApple OSS Distributions {
74*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
75*19c3b8c2SApple OSS Distributions // ^ ^
76*19c3b8c2SApple OSS Distributions // | |
77*19c3b8c2SApple OSS Distributions // begin,a,b end
78*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end());
79*19c3b8c2SApple OSS Distributions TQual* b = array.begin();
80*19c3b8c2SApple OSS Distributions check_eq(a, b);
81*19c3b8c2SApple OSS Distributions }
82*19c3b8c2SApple OSS Distributions {
83*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
84*19c3b8c2SApple OSS Distributions // ^ ^ ^
85*19c3b8c2SApple OSS Distributions // | | |
86*19c3b8c2SApple OSS Distributions // begin a,b end
87*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin() + 1, array.begin(), array.end());
88*19c3b8c2SApple OSS Distributions TQual* b = array.begin() + 1;
89*19c3b8c2SApple OSS Distributions check_eq(a, b);
90*19c3b8c2SApple OSS Distributions }
91*19c3b8c2SApple OSS Distributions {
92*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
93*19c3b8c2SApple OSS Distributions // ^ ^ ^
94*19c3b8c2SApple OSS Distributions // | | |
95*19c3b8c2SApple OSS Distributions // begin,a b end
96*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end());
97*19c3b8c2SApple OSS Distributions TQual* b = array.begin() + 2;
98*19c3b8c2SApple OSS Distributions check_lt(a, b);
99*19c3b8c2SApple OSS Distributions }
100*19c3b8c2SApple OSS Distributions {
101*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
102*19c3b8c2SApple OSS Distributions // ^ ^
103*19c3b8c2SApple OSS Distributions // | |
104*19c3b8c2SApple OSS Distributions // begin end,a,b
105*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.end(), array.begin(), array.end());
106*19c3b8c2SApple OSS Distributions TQual* b = array.end();
107*19c3b8c2SApple OSS Distributions check_eq(a, b);
108*19c3b8c2SApple OSS Distributions }
109*19c3b8c2SApple OSS Distributions {
110*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
111*19c3b8c2SApple OSS Distributions // ^ ^ ^ ^
112*19c3b8c2SApple OSS Distributions // | | | |
113*19c3b8c2SApple OSS Distributions // begin a end b
114*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin() + 2, array.begin(), array.begin() + 3);
115*19c3b8c2SApple OSS Distributions TQual* b = array.begin() + 4;
116*19c3b8c2SApple OSS Distributions check_lt(a, b);
117*19c3b8c2SApple OSS Distributions }
118*19c3b8c2SApple OSS Distributions
119*19c3b8c2SApple OSS Distributions // Check when the bounded_ptr is outside of its bounds
120*19c3b8c2SApple OSS Distributions {
121*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
122*19c3b8c2SApple OSS Distributions // ^ ^ ^
123*19c3b8c2SApple OSS Distributions // | | |
124*19c3b8c2SApple OSS Distributions // a,b begin end
125*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin() + 2, array.end());
126*19c3b8c2SApple OSS Distributions TQual* b = array.begin();
127*19c3b8c2SApple OSS Distributions check_eq(a, b);
128*19c3b8c2SApple OSS Distributions }
129*19c3b8c2SApple OSS Distributions {
130*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
131*19c3b8c2SApple OSS Distributions // ^ ^ ^
132*19c3b8c2SApple OSS Distributions // | | |
133*19c3b8c2SApple OSS Distributions // begin end a,b
134*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.end() - 1, array.begin(), array.end() - 2);
135*19c3b8c2SApple OSS Distributions TQual* b = array.end() - 1;
136*19c3b8c2SApple OSS Distributions check_eq(a, b);
137*19c3b8c2SApple OSS Distributions }
138*19c3b8c2SApple OSS Distributions {
139*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
140*19c3b8c2SApple OSS Distributions // ^ ^ ^
141*19c3b8c2SApple OSS Distributions // | | |
142*19c3b8c2SApple OSS Distributions // begin end a,b
143*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.end(), array.begin(), array.end() - 1);
144*19c3b8c2SApple OSS Distributions TQual* b = array.end();
145*19c3b8c2SApple OSS Distributions check_eq(a, b);
146*19c3b8c2SApple OSS Distributions }
147*19c3b8c2SApple OSS Distributions {
148*19c3b8c2SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
149*19c3b8c2SApple OSS Distributions // ^ ^ ^ ^
150*19c3b8c2SApple OSS Distributions // | | | |
151*19c3b8c2SApple OSS Distributions // begin end a b
152*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> const a(array.end() - 1, array.begin(), array.end() - 2);
153*19c3b8c2SApple OSS Distributions TQual* b = array.end();
154*19c3b8c2SApple OSS Distributions check_lt(a, b);
155*19c3b8c2SApple OSS Distributions }
156*19c3b8c2SApple OSS Distributions
157*19c3b8c2SApple OSS Distributions // Test comparing against a null pointer
158*19c3b8c2SApple OSS Distributions {
159*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> a = nullptr;
160*19c3b8c2SApple OSS Distributions TQual* b = nullptr;
161*19c3b8c2SApple OSS Distributions check_eq(a, b);
162*19c3b8c2SApple OSS Distributions }
163*19c3b8c2SApple OSS Distributions {
164*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> a(array.end() - 1, array.begin(), array.end() - 2);
165*19c3b8c2SApple OSS Distributions TQual* b = nullptr;
166*19c3b8c2SApple OSS Distributions check_lt(b, a);
167*19c3b8c2SApple OSS Distributions }
168*19c3b8c2SApple OSS Distributions {
169*19c3b8c2SApple OSS Distributions test_bounded_ptr<TQual> a = nullptr;
170*19c3b8c2SApple OSS Distributions TQual* b = array.begin();
171*19c3b8c2SApple OSS Distributions check_lt(a, b);
172*19c3b8c2SApple OSS Distributions }
173*19c3b8c2SApple OSS Distributions }
174*19c3b8c2SApple OSS Distributions
175*19c3b8c2SApple OSS Distributions struct Base { int i; };
176*19c3b8c2SApple OSS Distributions struct Derived : Base { };
177*19c3b8c2SApple OSS Distributions
178*19c3b8c2SApple OSS Distributions template <typename Related>
179*19c3b8c2SApple OSS Distributions static void
tests_convert()180*19c3b8c2SApple OSS Distributions tests_convert()
181*19c3b8c2SApple OSS Distributions {
182*19c3b8c2SApple OSS Distributions std::array<Derived, 5> array = {Derived{0}, Derived{1}, Derived{2}, Derived{3}, Derived{4}};
183*19c3b8c2SApple OSS Distributions
184*19c3b8c2SApple OSS Distributions {
185*19c3b8c2SApple OSS Distributions test_bounded_ptr<Derived> const a(array.begin() + 1, array.begin(), array.end() - 1);
186*19c3b8c2SApple OSS Distributions Related* b = array.begin();
187*19c3b8c2SApple OSS Distributions check_lt(b, a);
188*19c3b8c2SApple OSS Distributions }
189*19c3b8c2SApple OSS Distributions {
190*19c3b8c2SApple OSS Distributions test_bounded_ptr<Related> const a(array.begin(), array.begin(), array.end() - 1);
191*19c3b8c2SApple OSS Distributions Derived* b = array.begin() + 1;
192*19c3b8c2SApple OSS Distributions check_lt(a, b);
193*19c3b8c2SApple OSS Distributions }
194*19c3b8c2SApple OSS Distributions
195*19c3b8c2SApple OSS Distributions // Test comparisons against cv-void*
196*19c3b8c2SApple OSS Distributions {
197*19c3b8c2SApple OSS Distributions test_bounded_ptr<Related> const a(array.begin(), array.begin(), array.end() - 1);
198*19c3b8c2SApple OSS Distributions void* b = array.begin() + 1;
199*19c3b8c2SApple OSS Distributions check_lt(a, b);
200*19c3b8c2SApple OSS Distributions }
201*19c3b8c2SApple OSS Distributions }
202*19c3b8c2SApple OSS Distributions
203*19c3b8c2SApple OSS Distributions T_DECL(compare_order_raw, "bounded_ptr.compare.order.raw") {
204*19c3b8c2SApple OSS Distributions tests<Derived, Derived>();
205*19c3b8c2SApple OSS Distributions tests<Derived, Derived const>();
206*19c3b8c2SApple OSS Distributions tests<Derived, Derived volatile>();
207*19c3b8c2SApple OSS Distributions tests<Derived, Derived const volatile>();
208*19c3b8c2SApple OSS Distributions tests_convert<Base>();
209*19c3b8c2SApple OSS Distributions tests_convert<Base const>();
210*19c3b8c2SApple OSS Distributions tests_convert<Base volatile>();
211*19c3b8c2SApple OSS Distributions tests_convert<Base const volatile>();
212*19c3b8c2SApple OSS Distributions }
213