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