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