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