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