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