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