1*5e3eaea3SApple OSS Distributions //
2*5e3eaea3SApple OSS Distributions // Tests for
3*5e3eaea3SApple OSS Distributions // bounded_array_ref<T, TrappingPolicy> slice(size_t n, size_t m) const;
4*5e3eaea3SApple OSS Distributions //
5*5e3eaea3SApple OSS Distributions
6*5e3eaea3SApple OSS Distributions #include <libkern/c++/bounded_array_ref.h>
7*5e3eaea3SApple OSS Distributions #include "test_policy.h"
8*5e3eaea3SApple OSS Distributions #include <cstddef>
9*5e3eaea3SApple OSS Distributions #include <cstdint>
10*5e3eaea3SApple OSS Distributions #include <darwintest.h>
11*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
12*5e3eaea3SApple OSS Distributions #include <limits>
13*5e3eaea3SApple OSS Distributions
14*5e3eaea3SApple OSS Distributions struct T { int i; };
15*5e3eaea3SApple OSS Distributions
16*5e3eaea3SApple OSS Distributions template <typename T>
17*5e3eaea3SApple OSS Distributions using tracking_bounded_array_ref = libkern::bounded_array_ref<T, tracking_policy>;
18*5e3eaea3SApple OSS Distributions
19*5e3eaea3SApple OSS Distributions template <typename T>
20*5e3eaea3SApple OSS Distributions static void
tests()21*5e3eaea3SApple OSS Distributions tests()
22*5e3eaea3SApple OSS Distributions {
23*5e3eaea3SApple OSS Distributions T array[5] = {T{0}, T{1}, T{2}, T{3}, T{4}};
24*5e3eaea3SApple OSS Distributions
25*5e3eaea3SApple OSS Distributions // Slices starting at 0
26*5e3eaea3SApple OSS Distributions {
27*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
28*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(0, 0);
29*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 0);
30*5e3eaea3SApple OSS Distributions }
31*5e3eaea3SApple OSS Distributions {
32*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
33*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(0, 1);
34*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 1);
35*5e3eaea3SApple OSS Distributions CHECK(&slice[0] == &array[0]);
36*5e3eaea3SApple OSS Distributions }
37*5e3eaea3SApple OSS Distributions {
38*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
39*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(0, 2);
40*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 2);
41*5e3eaea3SApple OSS Distributions CHECK(&slice[0] == &array[0]);
42*5e3eaea3SApple OSS Distributions CHECK(&slice[1] == &array[1]);
43*5e3eaea3SApple OSS Distributions }
44*5e3eaea3SApple OSS Distributions {
45*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
46*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(0, 5);
47*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 5);
48*5e3eaea3SApple OSS Distributions CHECK(&slice[0] == &array[0]);
49*5e3eaea3SApple OSS Distributions CHECK(&slice[1] == &array[1]);
50*5e3eaea3SApple OSS Distributions CHECK(&slice[2] == &array[2]);
51*5e3eaea3SApple OSS Distributions CHECK(&slice[3] == &array[3]);
52*5e3eaea3SApple OSS Distributions CHECK(&slice[4] == &array[4]);
53*5e3eaea3SApple OSS Distributions }
54*5e3eaea3SApple OSS Distributions {
55*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
56*5e3eaea3SApple OSS Distributions tracking_policy::reset();
57*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(0, 6);
58*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
59*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::message == "bounded_array_ref: invalid slice provided, the indices are of bounds for the bounded_array_ref");
60*5e3eaea3SApple OSS Distributions }
61*5e3eaea3SApple OSS Distributions
62*5e3eaea3SApple OSS Distributions // Slices starting at 1 (near the beginning)
63*5e3eaea3SApple OSS Distributions {
64*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
65*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(1, 0);
66*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 0);
67*5e3eaea3SApple OSS Distributions }
68*5e3eaea3SApple OSS Distributions {
69*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
70*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(1, 3);
71*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 3);
72*5e3eaea3SApple OSS Distributions CHECK(&slice[0] == &array[1]);
73*5e3eaea3SApple OSS Distributions CHECK(&slice[1] == &array[2]);
74*5e3eaea3SApple OSS Distributions CHECK(&slice[2] == &array[3]);
75*5e3eaea3SApple OSS Distributions }
76*5e3eaea3SApple OSS Distributions {
77*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
78*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(1, 4);
79*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 4);
80*5e3eaea3SApple OSS Distributions CHECK(&slice[0] == &array[1]);
81*5e3eaea3SApple OSS Distributions CHECK(&slice[1] == &array[2]);
82*5e3eaea3SApple OSS Distributions CHECK(&slice[2] == &array[3]);
83*5e3eaea3SApple OSS Distributions CHECK(&slice[3] == &array[4]);
84*5e3eaea3SApple OSS Distributions }
85*5e3eaea3SApple OSS Distributions {
86*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
87*5e3eaea3SApple OSS Distributions tracking_policy::reset();
88*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(1, 5);
89*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
90*5e3eaea3SApple OSS Distributions }
91*5e3eaea3SApple OSS Distributions {
92*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
93*5e3eaea3SApple OSS Distributions tracking_policy::reset();
94*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(1, 10);
95*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
96*5e3eaea3SApple OSS Distributions }
97*5e3eaea3SApple OSS Distributions
98*5e3eaea3SApple OSS Distributions // Slices starting at 3 (in the middle)
99*5e3eaea3SApple OSS Distributions {
100*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
101*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(3, 0);
102*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 0);
103*5e3eaea3SApple OSS Distributions }
104*5e3eaea3SApple OSS Distributions {
105*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
106*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(3, 2);
107*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 2);
108*5e3eaea3SApple OSS Distributions CHECK(&slice[0] == &array[3]);
109*5e3eaea3SApple OSS Distributions CHECK(&slice[1] == &array[4]);
110*5e3eaea3SApple OSS Distributions }
111*5e3eaea3SApple OSS Distributions {
112*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
113*5e3eaea3SApple OSS Distributions tracking_policy::reset();
114*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(3, 3);
115*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
116*5e3eaea3SApple OSS Distributions }
117*5e3eaea3SApple OSS Distributions {
118*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
119*5e3eaea3SApple OSS Distributions tracking_policy::reset();
120*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(3, 100);
121*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
122*5e3eaea3SApple OSS Distributions }
123*5e3eaea3SApple OSS Distributions
124*5e3eaea3SApple OSS Distributions // Slices starting at 4 (near the end)
125*5e3eaea3SApple OSS Distributions {
126*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
127*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(4, 0);
128*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 0);
129*5e3eaea3SApple OSS Distributions }
130*5e3eaea3SApple OSS Distributions {
131*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
132*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(4, 1);
133*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 1);
134*5e3eaea3SApple OSS Distributions CHECK(&slice[0] == &array[4]);
135*5e3eaea3SApple OSS Distributions }
136*5e3eaea3SApple OSS Distributions {
137*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
138*5e3eaea3SApple OSS Distributions tracking_policy::reset();
139*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(4, 2);
140*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
141*5e3eaea3SApple OSS Distributions }
142*5e3eaea3SApple OSS Distributions
143*5e3eaea3SApple OSS Distributions // Slices starting at the end
144*5e3eaea3SApple OSS Distributions {
145*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
146*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(5, 0);
147*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 0);
148*5e3eaea3SApple OSS Distributions }
149*5e3eaea3SApple OSS Distributions {
150*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
151*5e3eaea3SApple OSS Distributions tracking_policy::reset();
152*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(5, 1);
153*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
154*5e3eaea3SApple OSS Distributions }
155*5e3eaea3SApple OSS Distributions {
156*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
157*5e3eaea3SApple OSS Distributions tracking_policy::reset();
158*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(5, 10);
159*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
160*5e3eaea3SApple OSS Distributions }
161*5e3eaea3SApple OSS Distributions
162*5e3eaea3SApple OSS Distributions // Slices starting after the end
163*5e3eaea3SApple OSS Distributions {
164*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
165*5e3eaea3SApple OSS Distributions tracking_policy::reset();
166*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(6, 0);
167*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
168*5e3eaea3SApple OSS Distributions }
169*5e3eaea3SApple OSS Distributions {
170*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
171*5e3eaea3SApple OSS Distributions tracking_policy::reset();
172*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(6, 1);
173*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
174*5e3eaea3SApple OSS Distributions }
175*5e3eaea3SApple OSS Distributions {
176*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
177*5e3eaea3SApple OSS Distributions tracking_policy::reset();
178*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(8, 10);
179*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
180*5e3eaea3SApple OSS Distributions }
181*5e3eaea3SApple OSS Distributions
182*5e3eaea3SApple OSS Distributions // Slices overflowing a uint32_t
183*5e3eaea3SApple OSS Distributions {
184*5e3eaea3SApple OSS Distributions std::uint32_t n = std::numeric_limits<std::uint32_t>::max() / 2 + 1;
185*5e3eaea3SApple OSS Distributions std::uint32_t m = std::numeric_limits<std::uint32_t>::max() / 2 + 1;
186*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view(array);
187*5e3eaea3SApple OSS Distributions tracking_policy::reset();
188*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(n, m);
189*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
190*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::message == "bounded_array_ref: n + m is larger than the size of any bounded_array_ref");
191*5e3eaea3SApple OSS Distributions }
192*5e3eaea3SApple OSS Distributions
193*5e3eaea3SApple OSS Distributions // Check the documented range equivalent
194*5e3eaea3SApple OSS Distributions {
195*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
196*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(3, 2);
197*5e3eaea3SApple OSS Distributions CHECK(slice.begin() == view.begin() + 3);
198*5e3eaea3SApple OSS Distributions CHECK(slice.end() == view.begin() + 3 + 2);
199*5e3eaea3SApple OSS Distributions }
200*5e3eaea3SApple OSS Distributions
201*5e3eaea3SApple OSS Distributions // Chaining calls to slice()
202*5e3eaea3SApple OSS Distributions {
203*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view(array);
204*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(1, 4).slice(2, 2);
205*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 2);
206*5e3eaea3SApple OSS Distributions CHECK(&slice[0] == &array[3]);
207*5e3eaea3SApple OSS Distributions CHECK(&slice[1] == &array[4]);
208*5e3eaea3SApple OSS Distributions }
209*5e3eaea3SApple OSS Distributions
210*5e3eaea3SApple OSS Distributions // Slicing an empty view
211*5e3eaea3SApple OSS Distributions {
212*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> view;
213*5e3eaea3SApple OSS Distributions test_bounded_array_ref<T> slice = view.slice(0, 0);
214*5e3eaea3SApple OSS Distributions CHECK(slice.size() == 0);
215*5e3eaea3SApple OSS Distributions }
216*5e3eaea3SApple OSS Distributions {
217*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> view;
218*5e3eaea3SApple OSS Distributions tracking_policy::reset();
219*5e3eaea3SApple OSS Distributions tracking_bounded_array_ref<T> slice = view.slice(0, 1);
220*5e3eaea3SApple OSS Distributions CHECK(tracking_policy::did_trap);
221*5e3eaea3SApple OSS Distributions }
222*5e3eaea3SApple OSS Distributions }
223*5e3eaea3SApple OSS Distributions
224*5e3eaea3SApple OSS Distributions T_DECL(slice, "bounded_array_ref.slice") {
225*5e3eaea3SApple OSS Distributions tests<T>();
226*5e3eaea3SApple OSS Distributions tests<T const>();
227*5e3eaea3SApple OSS Distributions }
228