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