Blender V4.3
BLI_span_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "BLI_span.hh"
8#include "BLI_vector.hh"
9
10#include "BLI_strict_flags.h" /* Keep last. */
11
12namespace blender::tests {
13
14TEST(span, FromSmallVector)
15{
16 Vector<int> a = {1, 2, 3};
17 Span<int> a_span = a;
18 EXPECT_EQ(a_span.size(), 3);
19 EXPECT_EQ(a_span[0], 1);
20 EXPECT_EQ(a_span[1], 2);
21 EXPECT_EQ(a_span[2], 3);
22}
23
24TEST(span, AddConstToPointer)
25{
26 int a = 0;
27 std::vector<int *> vec = {&a};
28 Span<int *> span = vec;
29 Span<const int *> const_span = span;
30 EXPECT_EQ(const_span.size(), 1);
31}
32
33TEST(span, IsReferencing)
34{
35 int array[] = {3, 5, 8};
37 EXPECT_EQ(span.size(), 3);
38 EXPECT_EQ(span[1], 5);
39 array[1] = 10;
40 EXPECT_EQ(span[1], 10);
41}
42
43TEST(span, DropBack)
44{
45 Vector<int> a = {4, 5, 6, 7};
46 auto slice = Span<int>(a).drop_back(2);
47 EXPECT_EQ(slice.size(), 2);
48 EXPECT_EQ(slice[0], 4);
49 EXPECT_EQ(slice[1], 5);
50}
51
52TEST(span, DropBackAll)
53{
54 Vector<int> a = {4, 5, 6, 7};
55 auto slice = Span<int>(a).drop_back(a.size());
56 EXPECT_EQ(slice.size(), 0);
57}
58
59TEST(span, DropFront)
60{
61 Vector<int> a = {4, 5, 6, 7};
62 auto slice = Span<int>(a).drop_front(1);
63 EXPECT_EQ(slice.size(), 3);
64 EXPECT_EQ(slice[0], 5);
65 EXPECT_EQ(slice[1], 6);
66 EXPECT_EQ(slice[2], 7);
67}
68
69TEST(span, DropFrontLargeN)
70{
71 Vector<int> a = {1, 2, 3, 4, 5};
72 Span<int> slice1 = Span<int>(a).drop_front(100);
74 EXPECT_TRUE(slice1.is_empty());
75 EXPECT_TRUE(slice2.is_empty());
76}
77
78TEST(span, DropFrontAll)
79{
80 Vector<int> a = {4, 5, 6, 7};
81 auto slice = Span<int>(a).drop_front(a.size());
82 EXPECT_EQ(slice.size(), 0);
83}
84
85TEST(span, TakeFront)
86{
87 Vector<int> a = {4, 5, 6, 7};
88 auto slice = Span<int>(a).take_front(2);
89 EXPECT_EQ(slice.size(), 2);
90 EXPECT_EQ(slice[0], 4);
91 EXPECT_EQ(slice[1], 5);
92}
93
94TEST(span, TakeFrontLargeN)
95{
96 Vector<int> a = {4, 5, 6, 7};
97 Span<int> slice1 = Span<int>(a).take_front(100);
99 EXPECT_EQ(slice1.size(), 4);
100 EXPECT_EQ(slice2.size(), 4);
101}
102
103TEST(span, TakeBack)
104{
105 Vector<int> a = {5, 6, 7, 8};
106 auto slice = Span<int>(a).take_back(2);
107 EXPECT_EQ(slice.size(), 2);
108 EXPECT_EQ(slice[0], 7);
109 EXPECT_EQ(slice[1], 8);
110}
111
112TEST(span, TakeBackLargeN)
113{
114 Vector<int> a = {3, 4, 5, 6};
115 Span<int> slice1 = Span<int>(a).take_back(100);
117 EXPECT_EQ(slice1.size(), 4);
118 EXPECT_EQ(slice2.size(), 4);
119}
120
121TEST(span, Slice)
122{
123 Vector<int> a = {4, 5, 6, 7};
124 auto slice = Span<int>(a).slice(1, 2);
125 EXPECT_EQ(slice.size(), 2);
126 EXPECT_EQ(slice[0], 5);
127 EXPECT_EQ(slice[1], 6);
128}
129
130TEST(span, SliceEmpty)
131{
132 Vector<int> a = {4, 5, 6, 7};
133 auto slice = Span<int>(a).slice(2, 0);
134 EXPECT_EQ(slice.size(), 0);
135}
136
137TEST(span, SliceRange)
138{
139 Vector<int> a = {1, 2, 3, 4, 5};
140 auto slice = Span<int>(a).slice(IndexRange(2, 2));
141 EXPECT_EQ(slice.size(), 2);
142 EXPECT_EQ(slice[0], 3);
143 EXPECT_EQ(slice[1], 4);
144}
145
146TEST(span, SliceLargeN)
147{
148 Vector<int> a = {1, 2, 3, 4, 5};
149 Span<int> slice1 = Span<int>(a).slice_safe(3, 100);
150 MutableSpan<int> slice2 = MutableSpan<int>(a).slice_safe(3, 100);
151 EXPECT_EQ(slice1.size(), 2);
152 EXPECT_EQ(slice2.size(), 2);
153 EXPECT_EQ(slice1[0], 4);
154 EXPECT_EQ(slice2[0], 4);
155 EXPECT_EQ(slice1[1], 5);
156 EXPECT_EQ(slice2[1], 5);
157}
158
159TEST(span, Contains)
160{
161 Vector<int> a = {4, 5, 6, 7};
162 Span<int> a_span = a;
163 EXPECT_TRUE(a_span.contains(4));
164 EXPECT_TRUE(a_span.contains(5));
165 EXPECT_TRUE(a_span.contains(6));
166 EXPECT_TRUE(a_span.contains(7));
167 EXPECT_FALSE(a_span.contains(3));
168 EXPECT_FALSE(a_span.contains(8));
169}
170
171TEST(span, Count)
172{
173 Vector<int> a = {2, 3, 4, 3, 3, 2, 2, 2, 2};
174 Span<int> a_span = a;
175 EXPECT_EQ(a_span.count(1), 0);
176 EXPECT_EQ(a_span.count(2), 5);
177 EXPECT_EQ(a_span.count(3), 3);
178 EXPECT_EQ(a_span.count(4), 1);
179 EXPECT_EQ(a_span.count(5), 0);
180}
181
183{
184 EXPECT_EQ(span.size(), 4);
185 EXPECT_EQ(span[0], 3);
186 EXPECT_EQ(span[1], 6);
187 EXPECT_EQ(span[2], 8);
188 EXPECT_EQ(span[3], 9);
189}
190
191TEST(span, FromInitializerList)
192{
193 test_ref_from_initializer_list({3, 6, 8, 9});
194}
195
196TEST(span, FromVector)
197{
198 std::vector<int> a = {1, 2, 3, 4};
199 Span<int> a_span(a);
200 EXPECT_EQ(a_span.size(), 4);
201 EXPECT_EQ(a_span[0], 1);
202 EXPECT_EQ(a_span[1], 2);
203 EXPECT_EQ(a_span[2], 3);
204 EXPECT_EQ(a_span[3], 4);
205}
206
207TEST(span, FromArray)
208{
209 std::array<int, 2> a = {5, 6};
210 Span<int> a_span(a);
211 EXPECT_EQ(a_span.size(), 2);
212 EXPECT_EQ(a_span[0], 5);
213 EXPECT_EQ(a_span[1], 6);
214}
215
216TEST(span, Fill)
217{
218 std::array<int, 5> a = {4, 5, 6, 7, 8};
219 MutableSpan<int> a_span(a);
220 a_span.fill(1);
221 EXPECT_EQ(a[0], 1);
222 EXPECT_EQ(a[1], 1);
223 EXPECT_EQ(a[2], 1);
224 EXPECT_EQ(a[3], 1);
225 EXPECT_EQ(a[4], 1);
226}
227
228TEST(span, FillIndices)
229{
230 std::array<int, 5> a = {0, 0, 0, 0, 0};
231 MutableSpan<int> a_span(a);
232 a_span.fill_indices(Span({0, 2, 3}), 1);
233 EXPECT_EQ(a[0], 1);
234 EXPECT_EQ(a[1], 0);
235 EXPECT_EQ(a[2], 1);
236 EXPECT_EQ(a[3], 1);
237 EXPECT_EQ(a[4], 0);
238}
239
240TEST(span, SizeInBytes)
241{
242 std::array<int, 10> a{};
243 Span<int> a_span(a);
244 EXPECT_EQ(a_span.size_in_bytes(), int64_t(sizeof(a)));
245 EXPECT_EQ(a_span.size_in_bytes(), 40);
246}
247
248TEST(span, FirstLast)
249{
250 std::array<int, 4> a = {6, 7, 8, 9};
251 Span<int> a_span(a);
252 EXPECT_EQ(a_span.first(), 6);
253 EXPECT_EQ(a_span.last(), 9);
254 EXPECT_EQ(a_span.last(1), 8);
255 EXPECT_EQ(a_span.last(2), 7);
256}
257
258TEST(span, FirstLast_OneElement)
259{
260 int a = 3;
261 Span<int> a_span(&a, 1);
262 EXPECT_EQ(a_span.first(), 3);
263 EXPECT_EQ(a_span.last(), 3);
264 EXPECT_EQ(a_span.last(0), 3);
265}
266
267TEST(span, ContainsPtr)
268{
269 std::array<int, 3> a = {5, 6, 7};
270 int other = 10;
271 Span<int> a_span(a);
272 EXPECT_TRUE(a_span.contains_ptr(&a[0] + 0));
273 EXPECT_TRUE(a_span.contains_ptr(&a[0] + 1));
274 EXPECT_TRUE(a_span.contains_ptr(&a[0] + 2));
275 EXPECT_FALSE(a_span.contains_ptr(&a[0] + 3));
276 int *ptr_before = reinterpret_cast<int *>(uintptr_t(a.data()) - 1);
277 EXPECT_FALSE(a_span.contains_ptr(ptr_before));
278 EXPECT_FALSE(a_span.contains_ptr(&other));
279}
280
281TEST(span, FirstIndex)
282{
283 std::array<int, 5> a = {4, 5, 4, 2, 5};
284 Span<int> a_span(a);
285
286 EXPECT_EQ(a_span.first_index(4), 0);
287 EXPECT_EQ(a_span.first_index(5), 1);
288 EXPECT_EQ(a_span.first_index(2), 3);
289}
290
291TEST(span, CastSameSize)
292{
293 int value = 0;
294 std::array<int *, 4> a = {&value, nullptr, nullptr, nullptr};
295 Span<int *> a_span = a;
296 Span<float *> new_a_span = a_span.cast<float *>();
297
298 EXPECT_EQ(a_span.size(), 4);
299 EXPECT_EQ(new_a_span.size(), 4);
300
301 EXPECT_EQ(a_span[0], &value);
302 EXPECT_EQ(new_a_span[0], (float *)&value);
303}
304
305TEST(span, CastSmallerSize)
306{
307 std::array<uint32_t, 4> a = {3, 4, 5, 6};
308 Span<uint32_t> a_span = a;
309 Span<uint16_t> new_a_span = a_span.cast<uint16_t>();
310
311 EXPECT_EQ(a_span.size(), 4);
312 EXPECT_EQ(new_a_span.size(), 8);
313}
314
315TEST(span, CastLargerSize)
316{
317 std::array<uint16_t, 4> a = {4, 5, 6, 7};
318 Span<uint16_t> a_span = a;
319 Span<uint32_t> new_a_span = a_span.cast<uint32_t>();
320
321 EXPECT_EQ(a_span.size(), 4);
322 EXPECT_EQ(new_a_span.size(), 2);
323}
324
325TEST(span, VoidPointerSpan)
326{
327 int a;
328 float b;
329 double c;
330
331 auto func1 = [](Span<void *> span) { EXPECT_EQ(span.size(), 3); };
332 func1({&a, &b, &c});
333}
334
335TEST(span, CopyFrom)
336{
337 std::array<int, 4> src = {5, 6, 7, 8};
338 std::array<int, 4> dst = {1, 2, 3, 4};
339
340 EXPECT_EQ(dst[2], 3);
341 MutableSpan(dst).copy_from(src);
342 EXPECT_EQ(dst[0], 5);
343 EXPECT_EQ(dst[1], 6);
344 EXPECT_EQ(dst[2], 7);
345 EXPECT_EQ(dst[3], 8);
346}
347
348TEST(span, ReverseIterator)
349{
350 std::array<int, 4> src = {4, 5, 6, 7};
351 Span<int> span = src;
352 Vector<int> reversed_vec;
353
354 for (auto it = span.rbegin(); it != span.rend(); ++it) {
355 reversed_vec.append(*it);
356 }
357 EXPECT_EQ(reversed_vec.size(), 4);
358 EXPECT_EQ_ARRAY(reversed_vec.data(), Span({7, 6, 5, 4}).data(), 4);
359}
360
361TEST(span, ReverseMutableSpan)
362{
363 std::array<int, 0> src0 = {};
364 MutableSpan<int> span0 = src0;
365 span0.reverse();
366 EXPECT_EQ_ARRAY(span0.data(), Span<int>({}).data(), 0);
367
368 std::array<int, 1> src1 = {4};
369 MutableSpan<int> span1 = src1;
370 span1.reverse();
371 EXPECT_EQ_ARRAY(span1.data(), Span<int>({4}).data(), 1);
372
373 std::array<int, 2> src2 = {4, 5};
374 MutableSpan<int> span2 = src2;
375 span2.reverse();
376 EXPECT_EQ_ARRAY(span2.data(), Span<int>({5, 4}).data(), 2);
377
378 std::array<int, 5> src5 = {4, 5, 6, 7, 8};
379 MutableSpan<int> span5 = src5;
380 span5.reverse();
381 EXPECT_EQ_ARRAY(span5.data(), Span<int>({8, 7, 6, 5, 4}).data(), 5);
382}
383
384TEST(span, MutableReverseIterator)
385{
386 std::array<int, 4> src = {4, 5, 6, 7};
387 MutableSpan<int> span = src;
388 Vector<int> reversed_vec;
389
390 for (auto it = span.rbegin(); it != span.rend(); ++it) {
391 reversed_vec.append(*it);
392 *it += 10;
393 }
394 EXPECT_EQ(reversed_vec.size(), 4);
395 EXPECT_EQ_ARRAY(reversed_vec.data(), Span({7, 6, 5, 4}).data(), 4);
396 EXPECT_EQ_ARRAY(src.data(), Span({14, 15, 16, 17}).data(), 4);
397}
398
399TEST(span, Constexpr)
400{
401 static constexpr std::array<int, 3> src = {3, 2, 1};
402 constexpr Span<int> span(src);
403 BLI_STATIC_ASSERT(span[2] == 1, "");
404 BLI_STATIC_ASSERT(span.size() == 3, "");
405 BLI_STATIC_ASSERT(span.slice(1, 2).size() == 2, "");
407
408 std::integral_constant<bool, span.first_index(1) == 2> ic;
409 BLI_STATIC_ASSERT(ic.value, "");
410
411 EXPECT_EQ(span.slice(1, 2).size(), 2);
412}
413
414TEST(span, ImplicitConversions)
415{
416 BLI_STATIC_ASSERT((std::is_convertible_v<MutableSpan<int>, Span<int>>), "");
417 BLI_STATIC_ASSERT((std::is_convertible_v<Span<int *>, Span<const int *>>), "");
418 BLI_STATIC_ASSERT((std::is_convertible_v<MutableSpan<int *>, Span<int *>>), "");
419 BLI_STATIC_ASSERT((std::is_convertible_v<MutableSpan<int *>, Span<const int *>>), "");
421 BLI_STATIC_ASSERT((!std::is_convertible_v<MutableSpan<const int *>, MutableSpan<int *>>), "");
422 BLI_STATIC_ASSERT((!std::is_convertible_v<Span<const int *>, Span<int *>>), "");
423 BLI_STATIC_ASSERT((!std::is_convertible_v<Span<int *>, MutableSpan<const int *>>), "");
424}
425
426TEST(span, Comparison)
427{
428 std::array<int, 3> a = {3, 4, 5};
429 std::array<int, 4> b = {3, 4, 5, 6};
430
431 EXPECT_FALSE(Span(a) == Span(b));
432 EXPECT_FALSE(Span(b) == Span(a));
433 EXPECT_TRUE(Span(a) == Span(b).take_front(3));
434 EXPECT_TRUE(Span(a) == Span(a));
435 EXPECT_TRUE(Span(b) == Span(b));
436
437 EXPECT_TRUE(Span(a) != Span(b));
438 EXPECT_TRUE(Span(b) != Span(a));
439 EXPECT_FALSE(Span(a) != Span(b).take_front(3));
440 EXPECT_FALSE(Span(a) != Span(a));
441}
442
443} // namespace blender::tests
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:87
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
#define ARRAY_SIZE(arr)
constexpr int64_t size() const
Definition BLI_span.hh:494
constexpr std::reverse_iterator< T * > rbegin() const
Definition BLI_span.hh:554
constexpr bool is_empty() const
Definition BLI_span.hh:510
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:641
constexpr T * data() const
Definition BLI_span.hh:540
constexpr void fill(const T &value) const
Definition BLI_span.hh:518
constexpr MutableSpan slice_safe(const int64_t start, const int64_t size) const
Definition BLI_span.hh:591
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:608
constexpr void reverse() const
Definition BLI_span.hh:651
constexpr std::reverse_iterator< T * > rend() const
Definition BLI_span.hh:558
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:726
constexpr void fill_indices(Span< IndexT > indices, const T &value) const
Definition BLI_span.hh:527
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:630
constexpr Span drop_front(int64_t n) const
Definition BLI_span.hh:172
Span< NewT > constexpr cast() const
Definition BLI_span.hh:419
constexpr Span slice_safe(const int64_t start, const int64_t size) const
Definition BLI_span.hh:155
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:138
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:269
constexpr std::reverse_iterator< const T * > rend() const
Definition BLI_span.hh:234
constexpr std::reverse_iterator< const T * > rbegin() const
Definition BLI_span.hh:230
constexpr const T * data() const
Definition BLI_span.hh:216
constexpr Span take_back(int64_t n) const
Definition BLI_span.hh:205
constexpr Span drop_back(int64_t n) const
Definition BLI_span.hh:183
constexpr int64_t count(const T &value) const
Definition BLI_span.hh:301
constexpr const T & first() const
Definition BLI_span.hh:316
constexpr bool has_duplicates__linear_search() const
Definition BLI_span.hh:337
constexpr int64_t first_index(const T &search_value) const
Definition BLI_span.hh:378
constexpr int64_t size() const
Definition BLI_span.hh:253
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:326
constexpr Span take_front(int64_t n) const
Definition BLI_span.hh:194
constexpr bool is_empty() const
Definition BLI_span.hh:261
constexpr bool contains(const T &value) const
Definition BLI_span.hh:278
constexpr bool contains_ptr(const T *ptr) const
Definition BLI_span.hh:292
int64_t size() const
void append(const T &value)
local_group_size(16, 16) .push_constant(Type b
static void test_ref_from_initializer_list(Span< int > span)
TEST(any, DefaultConstructor)
unsigned short uint16_t
Definition stdint.h:79
_W64 unsigned int uintptr_t
Definition stdint.h:119
unsigned int uint32_t
Definition stdint.h:80
__int64 int64_t
Definition stdint.h:89