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