1*19c3b8c2SApple OSS Distributions //
2*19c3b8c2SApple OSS Distributions // Tests for
3*19c3b8c2SApple OSS Distributions // friend std::ptrdiff_t operator-(bounded_ptr const& a, bounded_ptr const& b);
4*19c3b8c2SApple OSS Distributions // friend std::ptrdiff_t operator-(bounded_ptr const& a, T* b);
5*19c3b8c2SApple OSS Distributions // friend std::ptrdiff_t operator-(T* a, bounded_ptr const& b);
6*19c3b8c2SApple OSS Distributions //
7*19c3b8c2SApple OSS Distributions
8*19c3b8c2SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
9*19c3b8c2SApple OSS Distributions #include <array>
10*19c3b8c2SApple OSS Distributions #include <darwintest.h>
11*19c3b8c2SApple OSS Distributions #include <darwintest_utils.h>
12*19c3b8c2SApple OSS Distributions #include "test_utils.h"
13*19c3b8c2SApple OSS Distributions
14*19c3b8c2SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
15*19c3b8c2SApple OSS Distributions
16*19c3b8c2SApple OSS Distributions struct T {
17*19c3b8c2SApple OSS Distributions int i;
18*19c3b8c2SApple OSS Distributions };
19*19c3b8c2SApple OSS Distributions
20*19c3b8c2SApple OSS Distributions template <typename Stored, typename Left, typename Right>
21*19c3b8c2SApple OSS Distributions static void
tests()22*19c3b8c2SApple OSS Distributions tests()
23*19c3b8c2SApple OSS Distributions {
24*19c3b8c2SApple OSS Distributions std::array<Stored, 5> array = {Stored{0}, Stored{1}, Stored{2}, Stored{3}, Stored{4}};
25*19c3b8c2SApple OSS Distributions
26*19c3b8c2SApple OSS Distributions // a >= b
27*19c3b8c2SApple OSS Distributions {
28*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
29*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
30*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
31*19c3b8c2SApple OSS Distributions _assert(diff == 0);
32*19c3b8c2SApple OSS Distributions }
33*19c3b8c2SApple OSS Distributions {
34*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin() + 1, array.begin(), array.end());
35*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
36*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
37*19c3b8c2SApple OSS Distributions _assert(diff == 1);
38*19c3b8c2SApple OSS Distributions }
39*19c3b8c2SApple OSS Distributions {
40*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin() + 2, array.begin(), array.end());
41*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
42*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
43*19c3b8c2SApple OSS Distributions _assert(diff == 2);
44*19c3b8c2SApple OSS Distributions }
45*19c3b8c2SApple OSS Distributions {
46*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin() + 3, array.begin(), array.end());
47*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
48*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
49*19c3b8c2SApple OSS Distributions _assert(diff == 3);
50*19c3b8c2SApple OSS Distributions }
51*19c3b8c2SApple OSS Distributions {
52*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin() + 4, array.begin(), array.end());
53*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
54*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
55*19c3b8c2SApple OSS Distributions _assert(diff == 4);
56*19c3b8c2SApple OSS Distributions }
57*19c3b8c2SApple OSS Distributions {
58*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.end(), array.begin(), array.end());
59*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin(), array.begin(), array.end());
60*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
61*19c3b8c2SApple OSS Distributions _assert(diff == 5);
62*19c3b8c2SApple OSS Distributions }
63*19c3b8c2SApple OSS Distributions
64*19c3b8c2SApple OSS Distributions // a < b
65*19c3b8c2SApple OSS Distributions {
66*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
67*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin() + 1, array.begin(), array.end());
68*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
69*19c3b8c2SApple OSS Distributions _assert(diff == -1);
70*19c3b8c2SApple OSS Distributions }
71*19c3b8c2SApple OSS Distributions {
72*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
73*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin() + 2, array.begin(), array.end());
74*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
75*19c3b8c2SApple OSS Distributions _assert(diff == -2);
76*19c3b8c2SApple OSS Distributions }
77*19c3b8c2SApple OSS Distributions {
78*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
79*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin() + 3, array.begin(), array.end());
80*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
81*19c3b8c2SApple OSS Distributions _assert(diff == -3);
82*19c3b8c2SApple OSS Distributions }
83*19c3b8c2SApple OSS Distributions {
84*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
85*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin() + 4, array.begin(), array.end());
86*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
87*19c3b8c2SApple OSS Distributions _assert(diff == -4);
88*19c3b8c2SApple OSS Distributions }
89*19c3b8c2SApple OSS Distributions {
90*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin(), array.begin(), array.end());
91*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin() + 5, array.begin(), array.end());
92*19c3b8c2SApple OSS Distributions std::ptrdiff_t diff = a - b;
93*19c3b8c2SApple OSS Distributions _assert(diff == -5);
94*19c3b8c2SApple OSS Distributions }
95*19c3b8c2SApple OSS Distributions
96*19c3b8c2SApple OSS Distributions // Subtract pointers with different bounds
97*19c3b8c2SApple OSS Distributions {
98*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin() + 2, array.begin() + 1, array.end() - 1);
99*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin() + 4, array.begin() + 3, array.end());
100*19c3b8c2SApple OSS Distributions _assert(a - b == -2);
101*19c3b8c2SApple OSS Distributions _assert(b - a == 2);
102*19c3b8c2SApple OSS Distributions }
103*19c3b8c2SApple OSS Distributions
104*19c3b8c2SApple OSS Distributions // Subtract with raw pointers
105*19c3b8c2SApple OSS Distributions {
106*19c3b8c2SApple OSS Distributions test_bounded_ptr<Left> const a(array.begin() + 2, array.begin() + 1, array.end() - 1);
107*19c3b8c2SApple OSS Distributions Right* b = array.begin() + 4;
108*19c3b8c2SApple OSS Distributions _assert(a - b == -2);
109*19c3b8c2SApple OSS Distributions }
110*19c3b8c2SApple OSS Distributions {
111*19c3b8c2SApple OSS Distributions Left* a = array.begin() + 4;
112*19c3b8c2SApple OSS Distributions test_bounded_ptr<Right> const b(array.begin() + 2, array.begin() + 1, array.end() - 1);
113*19c3b8c2SApple OSS Distributions _assert(a - b == 2);
114*19c3b8c2SApple OSS Distributions }
115*19c3b8c2SApple OSS Distributions }
116*19c3b8c2SApple OSS Distributions
117*19c3b8c2SApple OSS Distributions T_DECL(arith_difference, "bounded_ptr.arith.difference") {
118*19c3b8c2SApple OSS Distributions tests<T, T, T>();
119*19c3b8c2SApple OSS Distributions tests<T, T, T const>();
120*19c3b8c2SApple OSS Distributions tests<T, T const, T>();
121*19c3b8c2SApple OSS Distributions tests<T, T const, T const>();
122*19c3b8c2SApple OSS Distributions }
123