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