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