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