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