1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  template <typename T, typename U, typename P1, typename P2>
4bb611c8fSApple OSS Distributions //  bool operator<(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b);
5bb611c8fSApple OSS Distributions //
6bb611c8fSApple OSS Distributions //  template <typename T, typename U, typename P1, typename P2>
7bb611c8fSApple OSS Distributions //  bool operator<=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b);
8bb611c8fSApple OSS Distributions //
9bb611c8fSApple OSS Distributions //  template <typename T, typename U, typename P1, typename P2>
10bb611c8fSApple OSS Distributions //  bool operator>(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b);
11bb611c8fSApple OSS Distributions //
12bb611c8fSApple OSS Distributions //  template <typename T, typename U, typename P1, typename P2>
13bb611c8fSApple OSS Distributions //  bool operator>=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b);
14bb611c8fSApple OSS Distributions //
15bb611c8fSApple OSS Distributions 
16bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
17bb611c8fSApple OSS Distributions #include <array>
18bb611c8fSApple OSS Distributions #include <darwintest.h>
19bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
20bb611c8fSApple OSS Distributions #include "test_utils.h"
21bb611c8fSApple OSS Distributions 
22bb611c8fSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
23bb611c8fSApple OSS Distributions 
24bb611c8fSApple OSS Distributions struct dummy_policy1 {
25bb611c8fSApple OSS Distributions 	static constexpr void
trapdummy_policy126bb611c8fSApple OSS Distributions 	trap(char const*)
27bb611c8fSApple OSS Distributions 	{
28bb611c8fSApple OSS Distributions 	}
29bb611c8fSApple OSS Distributions };
30bb611c8fSApple OSS Distributions struct dummy_policy2 {
31bb611c8fSApple OSS Distributions 	static constexpr void
trapdummy_policy232bb611c8fSApple OSS Distributions 	trap(char const*)
33bb611c8fSApple OSS Distributions 	{
34bb611c8fSApple OSS Distributions 	}
35bb611c8fSApple OSS Distributions };
36bb611c8fSApple OSS Distributions 
37bb611c8fSApple OSS Distributions template <typename T, typename U>
38bb611c8fSApple OSS Distributions static void
check_lt(T t,U u)39bb611c8fSApple OSS Distributions check_lt(T t, U u)
40bb611c8fSApple OSS Distributions {
41bb611c8fSApple OSS Distributions 	_assert(t < u);
42bb611c8fSApple OSS Distributions 	_assert(t <= u);
43bb611c8fSApple OSS Distributions 	_assert(!(t >= u));
44bb611c8fSApple OSS Distributions 	_assert(!(t > u));
45bb611c8fSApple OSS Distributions 
46bb611c8fSApple OSS Distributions 	_assert(!(u < t));
47bb611c8fSApple OSS Distributions 	_assert(!(u <= t));
48bb611c8fSApple OSS Distributions 	_assert(u > t);
49bb611c8fSApple OSS Distributions 	_assert(u >= t);
50bb611c8fSApple OSS Distributions }
51bb611c8fSApple OSS Distributions 
52bb611c8fSApple OSS Distributions template <typename T, typename U>
53bb611c8fSApple OSS Distributions static void
check_eq(T t,U u)54bb611c8fSApple OSS Distributions check_eq(T t, U u)
55bb611c8fSApple OSS Distributions {
56bb611c8fSApple OSS Distributions 	_assert(!(t < u));
57bb611c8fSApple OSS Distributions 	_assert(t <= u);
58bb611c8fSApple OSS Distributions 	_assert(t >= u);
59bb611c8fSApple OSS Distributions 	_assert(!(t > u));
60bb611c8fSApple OSS Distributions 
61bb611c8fSApple OSS Distributions 	_assert(!(u < t));
62bb611c8fSApple OSS Distributions 	_assert(u <= t);
63bb611c8fSApple OSS Distributions 	_assert(!(u > t));
64bb611c8fSApple OSS Distributions 	_assert(u >= t);
65bb611c8fSApple OSS Distributions }
66bb611c8fSApple OSS Distributions 
67bb611c8fSApple OSS Distributions template <typename T, typename TQual>
68bb611c8fSApple OSS Distributions static void
tests()69bb611c8fSApple OSS Distributions tests()
70bb611c8fSApple OSS Distributions {
71bb611c8fSApple OSS Distributions 	std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
72bb611c8fSApple OSS Distributions 
73bb611c8fSApple OSS Distributions 	// Pointers with the same bounds
74bb611c8fSApple OSS Distributions 	{
75bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end());
76bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.begin(), array.begin(), array.end());
77bb611c8fSApple OSS Distributions 		check_eq(a, b);
78bb611c8fSApple OSS Distributions 	}
79bb611c8fSApple OSS Distributions 	{
80bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end());
81bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.begin() + 1, array.begin(), array.end());
82bb611c8fSApple OSS Distributions 		check_lt(a, b);
83bb611c8fSApple OSS Distributions 	}
84bb611c8fSApple OSS Distributions 	{
85bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end());
86bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.begin() + 2, array.begin(), array.end());
87bb611c8fSApple OSS Distributions 		check_lt(a, b);
88bb611c8fSApple OSS Distributions 	}
89bb611c8fSApple OSS Distributions 	{
90bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end());
91bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.end(), array.begin(), array.end());
92bb611c8fSApple OSS Distributions 		check_lt(a, b);
93bb611c8fSApple OSS Distributions 	}
94bb611c8fSApple OSS Distributions 	{
95bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.end(), array.begin(), array.end());
96bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.end(), array.begin(), array.end());
97bb611c8fSApple OSS Distributions 		check_eq(a, b);
98bb611c8fSApple OSS Distributions 	}
99bb611c8fSApple OSS Distributions 
100bb611c8fSApple OSS Distributions 	// Compare null pointers
101bb611c8fSApple OSS Distributions 	{
102bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a;
103bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.begin(), array.begin(), array.end());
104bb611c8fSApple OSS Distributions 		check_lt(a, b);
105bb611c8fSApple OSS Distributions 	}
106bb611c8fSApple OSS Distributions 	{
107bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a;
108bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b;
109bb611c8fSApple OSS Distributions 		check_eq(a, b);
110bb611c8fSApple OSS Distributions 	}
111bb611c8fSApple OSS Distributions 
112bb611c8fSApple OSS Distributions 	// Pointers with different bounds
113bb611c8fSApple OSS Distributions 	{
114bb611c8fSApple OSS Distributions 		// Overlapping bounds, equal
115bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.begin(), array.begin() + 2, array.end());
116bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.begin(), array.begin(), array.end());
117bb611c8fSApple OSS Distributions 		check_eq(a, b);
118bb611c8fSApple OSS Distributions 	}
119bb611c8fSApple OSS Distributions 	{
120bb611c8fSApple OSS Distributions 		// Overlapping bounds, not equal
121bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.begin(), array.begin() + 2, array.end());
122bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.begin() + 2, array.begin(), array.end());
123bb611c8fSApple OSS Distributions 		check_lt(a, b);
124bb611c8fSApple OSS Distributions 	}
125bb611c8fSApple OSS Distributions 	{
126bb611c8fSApple OSS Distributions 		// Non-overlapping bounds, equal
127bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.begin() + 1);
128bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.begin(), array.begin() + 2, array.end());
129bb611c8fSApple OSS Distributions 		check_eq(a, b);
130bb611c8fSApple OSS Distributions 	}
131bb611c8fSApple OSS Distributions 	{
132bb611c8fSApple OSS Distributions 		// Non-overlapping bounds, not equal
133bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.begin() + 1);
134bb611c8fSApple OSS Distributions 		test_bounded_ptr<TQual> const b(array.begin() + 3, array.begin() + 2, array.end());
135bb611c8fSApple OSS Distributions 		check_lt(a, b);
136bb611c8fSApple OSS Distributions 	}
137bb611c8fSApple OSS Distributions 
138bb611c8fSApple OSS Distributions 	// Test with different policies
139bb611c8fSApple OSS Distributions 	{
140bb611c8fSApple OSS Distributions 		libkern::bounded_ptr<TQual, dummy_policy1> const a(array.begin(), array.begin(), array.end());
141bb611c8fSApple OSS Distributions 		libkern::bounded_ptr<TQual, dummy_policy2> const b(array.begin(), array.begin(), array.end());
142bb611c8fSApple OSS Distributions 		check_eq(a, b);
143bb611c8fSApple OSS Distributions 	}
144bb611c8fSApple OSS Distributions }
145bb611c8fSApple OSS Distributions 
146bb611c8fSApple OSS Distributions struct Base { int i; };
147bb611c8fSApple OSS Distributions struct Derived : Base { };
148bb611c8fSApple OSS Distributions 
149bb611c8fSApple OSS Distributions template <typename Related>
150bb611c8fSApple OSS Distributions static void
tests_convert()151bb611c8fSApple OSS Distributions tests_convert()
152bb611c8fSApple OSS Distributions {
153bb611c8fSApple OSS Distributions 	std::array<Derived, 5> array = {Derived{0}, Derived{1}, Derived{2}, Derived{3}, Derived{4}};
154bb611c8fSApple OSS Distributions 	test_bounded_ptr<Derived> const a(array.begin(), array.begin(), array.end() - 1);
155bb611c8fSApple OSS Distributions 	test_bounded_ptr<Related> const b(array.begin(), array.begin(), array.end() - 1);
156bb611c8fSApple OSS Distributions 	check_eq(a, b);
157bb611c8fSApple OSS Distributions }
158bb611c8fSApple OSS Distributions 
159*8d741a5dSApple OSS Distributions T_DECL(compare_order, "bounded_ptr.compare.order", T_META_TAG_VM_PREFERRED) {
160bb611c8fSApple OSS Distributions 	tests<Derived, Derived>();
161bb611c8fSApple OSS Distributions 	tests<Derived, Derived const>();
162bb611c8fSApple OSS Distributions 	tests<Derived, Derived volatile>();
163bb611c8fSApple OSS Distributions 	tests<Derived, Derived const volatile>();
164bb611c8fSApple OSS Distributions 	tests_convert<Base>();
165bb611c8fSApple OSS Distributions 	tests_convert<Base const>();
166bb611c8fSApple OSS Distributions 	tests_convert<Base volatile>();
167bb611c8fSApple OSS Distributions 	tests_convert<Base const volatile>();
168bb611c8fSApple OSS Distributions }
169