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