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