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