1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions // template <typename T, typename P, typename U>
4bb611c8fSApple OSS Distributions // bool operator==(bounded_ptr<T, P> const& a, U* b);
5bb611c8fSApple OSS Distributions //
6bb611c8fSApple OSS Distributions // template <typename T, typename P, typename U>
7bb611c8fSApple OSS Distributions // bool operator==(U* a, bounded_ptr<T, P> const& b);
8bb611c8fSApple OSS Distributions //
9bb611c8fSApple OSS Distributions // template <typename T, typename P, typename U>
10bb611c8fSApple OSS Distributions // bool operator!=(bounded_ptr<T, P> const& a, U* b);
11bb611c8fSApple OSS Distributions //
12bb611c8fSApple OSS Distributions // template <typename T, typename P, typename U>
13bb611c8fSApple OSS Distributions // bool operator!=(U* a, bounded_ptr<T, P> const& b);
14bb611c8fSApple OSS Distributions //
15bb611c8fSApple OSS Distributions
16bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
17bb611c8fSApple OSS Distributions #include <array>
18bb611c8fSApple OSS Distributions #include <darwintest.h>
19bb611c8fSApple OSS Distributions #include <darwintest_utils.h>
20bb611c8fSApple OSS Distributions #include "test_utils.h"
21bb611c8fSApple OSS Distributions
22bb611c8fSApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
23bb611c8fSApple OSS Distributions
24bb611c8fSApple OSS Distributions template <typename T, typename U>
25bb611c8fSApple OSS Distributions static void
check_eq(T t,U u)26bb611c8fSApple OSS Distributions check_eq(T t, U u)
27bb611c8fSApple OSS Distributions {
28bb611c8fSApple OSS Distributions _assert(t == u);
29bb611c8fSApple OSS Distributions _assert(u == t);
30bb611c8fSApple OSS Distributions _assert(!(t != u));
31bb611c8fSApple OSS Distributions _assert(!(u != t));
32bb611c8fSApple OSS Distributions }
33bb611c8fSApple OSS Distributions
34bb611c8fSApple OSS Distributions template <typename T, typename U>
35bb611c8fSApple OSS Distributions static void
check_ne(T t,U u)36bb611c8fSApple OSS Distributions check_ne(T t, U u)
37bb611c8fSApple OSS Distributions {
38bb611c8fSApple OSS Distributions _assert(!(t == u));
39bb611c8fSApple OSS Distributions _assert(!(u == t));
40bb611c8fSApple OSS Distributions _assert(t != u);
41bb611c8fSApple OSS Distributions _assert(u != t);
42bb611c8fSApple OSS Distributions }
43bb611c8fSApple OSS Distributions
44bb611c8fSApple OSS Distributions template <typename T, typename TQual>
45bb611c8fSApple OSS Distributions static void
tests()46bb611c8fSApple OSS Distributions tests()
47bb611c8fSApple OSS Distributions {
48bb611c8fSApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
49bb611c8fSApple OSS Distributions
50bb611c8fSApple OSS Distributions // Compare pointers within the bounds
51bb611c8fSApple OSS Distributions {
52bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
53bb611c8fSApple OSS Distributions // ^ ^
54bb611c8fSApple OSS Distributions // | |
55bb611c8fSApple OSS Distributions // begin,a,b end
56bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end());
57bb611c8fSApple OSS Distributions TQual* b = array.begin();
58bb611c8fSApple OSS Distributions check_eq(a, b);
59bb611c8fSApple OSS Distributions }
60bb611c8fSApple OSS Distributions {
61bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
62bb611c8fSApple OSS Distributions // ^ ^ ^
63bb611c8fSApple OSS Distributions // | | |
64bb611c8fSApple OSS Distributions // begin a,b end
65bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin() + 1, array.begin(), array.end());
66bb611c8fSApple OSS Distributions TQual* b = array.begin() + 1;
67bb611c8fSApple OSS Distributions check_eq(a, b);
68bb611c8fSApple OSS Distributions }
69bb611c8fSApple OSS Distributions {
70bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
71bb611c8fSApple OSS Distributions // ^ ^ ^
72bb611c8fSApple OSS Distributions // | | |
73bb611c8fSApple OSS Distributions // begin,a b end
74bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin(), array.end());
75bb611c8fSApple OSS Distributions TQual* b = array.begin() + 2;
76bb611c8fSApple OSS Distributions check_ne(a, b);
77bb611c8fSApple OSS Distributions }
78bb611c8fSApple OSS Distributions {
79bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
80bb611c8fSApple OSS Distributions // ^ ^
81bb611c8fSApple OSS Distributions // | |
82bb611c8fSApple OSS Distributions // begin end,a,b
83bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.end(), array.begin(), array.end());
84bb611c8fSApple OSS Distributions TQual* b = array.end();
85bb611c8fSApple OSS Distributions check_eq(a, b);
86bb611c8fSApple OSS Distributions }
87bb611c8fSApple OSS Distributions {
88bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
89bb611c8fSApple OSS Distributions // ^ ^ ^ ^
90bb611c8fSApple OSS Distributions // | | | |
91bb611c8fSApple OSS Distributions // begin a end b
92bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin() + 2, array.begin(), array.begin() + 3);
93bb611c8fSApple OSS Distributions TQual* b = array.begin() + 4;
94bb611c8fSApple OSS Distributions check_ne(a, b);
95bb611c8fSApple OSS Distributions }
96bb611c8fSApple OSS Distributions
97bb611c8fSApple OSS Distributions // Check when the bounded_ptr is outside of its bounds
98bb611c8fSApple OSS Distributions {
99bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
100bb611c8fSApple OSS Distributions // ^ ^ ^
101bb611c8fSApple OSS Distributions // | | |
102bb611c8fSApple OSS Distributions // a,b begin end
103bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.begin(), array.begin() + 2, array.end());
104bb611c8fSApple OSS Distributions TQual* b = array.begin();
105bb611c8fSApple OSS Distributions check_eq(a, b);
106bb611c8fSApple OSS Distributions }
107bb611c8fSApple OSS Distributions {
108bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
109bb611c8fSApple OSS Distributions // ^ ^ ^
110bb611c8fSApple OSS Distributions // | | |
111bb611c8fSApple OSS Distributions // begin end a,b
112bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.end() - 1, array.begin(), array.end() - 2);
113bb611c8fSApple OSS Distributions TQual* b = array.end() - 1;
114bb611c8fSApple OSS Distributions check_eq(a, b);
115bb611c8fSApple OSS Distributions }
116bb611c8fSApple OSS Distributions {
117bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
118bb611c8fSApple OSS Distributions // ^ ^ ^
119bb611c8fSApple OSS Distributions // | | |
120bb611c8fSApple OSS Distributions // begin end a,b
121bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.end(), array.begin(), array.end() - 1);
122bb611c8fSApple OSS Distributions TQual* b = array.end();
123bb611c8fSApple OSS Distributions check_eq(a, b);
124bb611c8fSApple OSS Distributions }
125bb611c8fSApple OSS Distributions {
126bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
127bb611c8fSApple OSS Distributions // ^ ^ ^ ^
128bb611c8fSApple OSS Distributions // | | | |
129bb611c8fSApple OSS Distributions // begin end a b
130bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> const a(array.end() - 1, array.begin(), array.end() - 2);
131bb611c8fSApple OSS Distributions TQual* b = array.end();
132bb611c8fSApple OSS Distributions check_ne(a, b);
133bb611c8fSApple OSS Distributions }
134bb611c8fSApple OSS Distributions
135bb611c8fSApple OSS Distributions // Test comparing against a null pointer
136bb611c8fSApple OSS Distributions {
137bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> a = nullptr;
138bb611c8fSApple OSS Distributions TQual* b = nullptr;
139bb611c8fSApple OSS Distributions check_eq(a, b);
140bb611c8fSApple OSS Distributions }
141bb611c8fSApple OSS Distributions {
142bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> a(array.end() - 1, array.begin(), array.end() - 2);
143bb611c8fSApple OSS Distributions TQual* b = nullptr;
144bb611c8fSApple OSS Distributions check_ne(a, b);
145bb611c8fSApple OSS Distributions }
146bb611c8fSApple OSS Distributions {
147bb611c8fSApple OSS Distributions test_bounded_ptr<TQual> a = nullptr;
148bb611c8fSApple OSS Distributions TQual* b = array.begin();
149bb611c8fSApple OSS Distributions check_ne(a, b);
150bb611c8fSApple OSS Distributions }
151bb611c8fSApple OSS Distributions }
152bb611c8fSApple OSS Distributions
153bb611c8fSApple OSS Distributions struct Base { int i; };
154bb611c8fSApple OSS Distributions struct Derived : Base { };
155bb611c8fSApple OSS Distributions
156bb611c8fSApple OSS Distributions template <typename Related>
157bb611c8fSApple OSS Distributions static void
tests_convert()158bb611c8fSApple OSS Distributions tests_convert()
159bb611c8fSApple OSS Distributions {
160bb611c8fSApple OSS Distributions std::array<Derived, 5> array = {Derived{0}, Derived{1}, Derived{2}, Derived{3}, Derived{4}};
161bb611c8fSApple OSS Distributions
162bb611c8fSApple OSS Distributions {
163bb611c8fSApple OSS Distributions test_bounded_ptr<Derived> const a(array.begin(), array.begin(), array.end() - 1);
164bb611c8fSApple OSS Distributions Related* b = array.begin();
165bb611c8fSApple OSS Distributions check_eq(a, b);
166bb611c8fSApple OSS Distributions }
167bb611c8fSApple OSS Distributions {
168bb611c8fSApple OSS Distributions test_bounded_ptr<Related> const a(array.begin(), array.begin(), array.end() - 1);
169bb611c8fSApple OSS Distributions Derived* b = array.begin();
170bb611c8fSApple OSS Distributions check_eq(a, b);
171bb611c8fSApple OSS Distributions }
172bb611c8fSApple OSS Distributions
173bb611c8fSApple OSS Distributions // Test comparisons against cv-void*
174bb611c8fSApple OSS Distributions {
175bb611c8fSApple OSS Distributions test_bounded_ptr<Related> const a(array.begin(), array.begin(), array.end() - 1);
176bb611c8fSApple OSS Distributions void* b = array.begin();
177bb611c8fSApple OSS Distributions check_eq(a, b);
178bb611c8fSApple OSS Distributions }
179bb611c8fSApple OSS Distributions }
180bb611c8fSApple OSS Distributions
181*8d741a5dSApple OSS Distributions T_DECL(compare_equal_raw, "bounded_ptr.compare.equal.raw", T_META_TAG_VM_PREFERRED) {
182bb611c8fSApple OSS Distributions tests<Derived, Derived>();
183bb611c8fSApple OSS Distributions tests<Derived, Derived const>();
184bb611c8fSApple OSS Distributions tests<Derived, Derived volatile>();
185bb611c8fSApple OSS Distributions tests<Derived, Derived const volatile>();
186bb611c8fSApple OSS Distributions tests_convert<Base>();
187bb611c8fSApple OSS Distributions tests_convert<Base const>();
188bb611c8fSApple OSS Distributions tests_convert<Base volatile>();
189bb611c8fSApple OSS Distributions tests_convert<Base const volatile>();
190bb611c8fSApple OSS Distributions }
191