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