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