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