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