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