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