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