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