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