1*43a90889SApple OSS Distributions //
2*43a90889SApple OSS Distributions // Tests for
3*43a90889SApple OSS Distributions // friend bounded_ptr operator-(bounded_ptr p, std::ptrdiff_t n);
4*43a90889SApple OSS Distributions //
5*43a90889SApple OSS Distributions
6*43a90889SApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
7*43a90889SApple OSS Distributions #include "test_utils.h"
8*43a90889SApple OSS Distributions #include <array>
9*43a90889SApple OSS Distributions #include <cstddef>
10*43a90889SApple OSS Distributions #include <darwintest.h>
11*43a90889SApple OSS Distributions #include <darwintest_utils.h>
12*43a90889SApple OSS Distributions
13*43a90889SApple OSS Distributions #define _assert(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
14*43a90889SApple OSS Distributions
15*43a90889SApple OSS Distributions struct T {
16*43a90889SApple OSS Distributions int i;
17*43a90889SApple OSS Distributions };
18*43a90889SApple OSS Distributions
19*43a90889SApple OSS Distributions template <typename T, typename QualT>
20*43a90889SApple OSS Distributions static void
tests()21*43a90889SApple OSS Distributions tests()
22*43a90889SApple OSS Distributions {
23*43a90889SApple OSS Distributions std::array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
24*43a90889SApple OSS Distributions
25*43a90889SApple OSS Distributions // Subtract positive offsets
26*43a90889SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
27*43a90889SApple OSS Distributions // ^ ^
28*43a90889SApple OSS Distributions // | |
29*43a90889SApple OSS Distributions // begin end,ptr
30*43a90889SApple OSS Distributions {
31*43a90889SApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.end(), array.begin(), array.end());
32*43a90889SApple OSS Distributions
33*43a90889SApple OSS Distributions {
34*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - static_cast<std::ptrdiff_t>(0);
35*43a90889SApple OSS Distributions _assert(ptr == array.end());
36*43a90889SApple OSS Distributions }
37*43a90889SApple OSS Distributions {
38*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - 1;
39*43a90889SApple OSS Distributions _assert(&*res == &array[4]);
40*43a90889SApple OSS Distributions }
41*43a90889SApple OSS Distributions {
42*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - 2;
43*43a90889SApple OSS Distributions _assert(&*res == &array[3]);
44*43a90889SApple OSS Distributions }
45*43a90889SApple OSS Distributions {
46*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - 3;
47*43a90889SApple OSS Distributions _assert(&*res == &array[2]);
48*43a90889SApple OSS Distributions }
49*43a90889SApple OSS Distributions {
50*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - 4;
51*43a90889SApple OSS Distributions _assert(&*res == &array[1]);
52*43a90889SApple OSS Distributions }
53*43a90889SApple OSS Distributions {
54*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - 5;
55*43a90889SApple OSS Distributions _assert(&*res == &array[0]);
56*43a90889SApple OSS Distributions }
57*43a90889SApple OSS Distributions }
58*43a90889SApple OSS Distributions
59*43a90889SApple OSS Distributions // Subtract negative offsets
60*43a90889SApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last>
61*43a90889SApple OSS Distributions // ^ ^
62*43a90889SApple OSS Distributions // | |
63*43a90889SApple OSS Distributions // begin,ptr end
64*43a90889SApple OSS Distributions {
65*43a90889SApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin(), array.begin(), array.end());
66*43a90889SApple OSS Distributions
67*43a90889SApple OSS Distributions {
68*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - static_cast<std::ptrdiff_t>(0);
69*43a90889SApple OSS Distributions _assert(&*res == &array[0]);
70*43a90889SApple OSS Distributions }
71*43a90889SApple OSS Distributions {
72*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - -1;
73*43a90889SApple OSS Distributions _assert(&*res == &array[1]);
74*43a90889SApple OSS Distributions }
75*43a90889SApple OSS Distributions {
76*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - -2;
77*43a90889SApple OSS Distributions _assert(&*res == &array[2]);
78*43a90889SApple OSS Distributions }
79*43a90889SApple OSS Distributions {
80*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - -3;
81*43a90889SApple OSS Distributions _assert(&*res == &array[3]);
82*43a90889SApple OSS Distributions }
83*43a90889SApple OSS Distributions {
84*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - -4;
85*43a90889SApple OSS Distributions _assert(&*res == &array[4]);
86*43a90889SApple OSS Distributions }
87*43a90889SApple OSS Distributions {
88*43a90889SApple OSS Distributions test_bounded_ptr<QualT> res = ptr - -5;
89*43a90889SApple OSS Distributions _assert(res == array.end());
90*43a90889SApple OSS Distributions }
91*43a90889SApple OSS Distributions }
92*43a90889SApple OSS Distributions
93*43a90889SApple OSS Distributions // Make sure the original pointer isn't modified
94*43a90889SApple OSS Distributions {
95*43a90889SApple OSS Distributions test_bounded_ptr<QualT> const ptr(array.begin() + 4, array.begin(), array.end());
96*43a90889SApple OSS Distributions (void)(ptr - 2);
97*43a90889SApple OSS Distributions _assert(&*ptr == &array[4]);
98*43a90889SApple OSS Distributions }
99*43a90889SApple OSS Distributions }
100*43a90889SApple OSS Distributions
101*43a90889SApple OSS Distributions T_DECL(arith_subtract, "bounded_ptr.arith.subtract", T_META_TAG_VM_PREFERRED) {
102*43a90889SApple OSS Distributions tests<T, T>();
103*43a90889SApple OSS Distributions tests<T, T const>();
104*43a90889SApple OSS Distributions tests<T, T volatile>();
105*43a90889SApple OSS Distributions tests<T, T const volatile>();
106*43a90889SApple OSS Distributions }
107