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