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