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