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