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