Blender
V5.0
source
blender
blenlib
tests
BLI_vector_list_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 "
BLI_exception_safety_test_utils.hh
"
6
#include "
BLI_vector_list.hh
"
7
#include "testing/testing.h"
8
9
#include "
BLI_strict_flags.h
"
/* IWYU pragma: keep. Keep last. */
10
11
namespace
blender::tests
{
12
13
TEST
(vectorlist, DefaultConstructor)
14
{
15
VectorList<int>
vec;
16
EXPECT_EQ
(vec.
size
(), 0);
17
}
18
19
TEST
(vectorlist, MoveConstructor)
20
{
21
VectorList<int>
vec1;
22
vec1.
append
(1);
23
vec1.
append
(2);
24
vec1.
append
(3);
25
vec1.
append
(4);
26
VectorList<int>
vec2(std::move(vec1));
27
28
EXPECT_EQ
(vec1.
size
(), 0);
/* NOLINT: bugprone-use-after-move */
29
EXPECT_EQ
(vec2.
size
(), 4);
30
EXPECT_EQ
(vec2[0], 1);
31
EXPECT_EQ
(vec2[1], 2);
32
EXPECT_EQ
(vec2[2], 3);
33
EXPECT_EQ
(vec2[3], 4);
34
}
35
36
TEST
(vectorlist, MoveOperator)
37
{
38
VectorList<int>
vec1;
39
vec1.
append
(1);
40
vec1.
append
(2);
41
vec1.
append
(3);
42
vec1.
append
(4);
43
VectorList<int>
vec2;
44
vec2 = std::move(vec1);
45
46
EXPECT_EQ
(vec1.
size
(), 0);
/* NOLINT: bugprone-use-after-move */
47
EXPECT_EQ
(vec2.
size
(), 4);
48
EXPECT_EQ
(vec2[0], 1);
49
EXPECT_EQ
(vec2[1], 2);
50
EXPECT_EQ
(vec2[2], 3);
51
EXPECT_EQ
(vec2[3], 4);
52
}
53
54
TEST
(vectorlist, Append)
55
{
56
VectorList<int>
vec;
57
vec.
append
(3);
58
vec.
append
(6);
59
vec.
append
(7);
60
EXPECT_EQ
(vec.
size
(), 3);
61
EXPECT_EQ
(vec[0], 3);
62
EXPECT_EQ
(vec[1], 6);
63
EXPECT_EQ
(vec[2], 7);
64
}
65
66
TEST
(vectorlist, Iterator)
67
{
68
VectorList<int>
vec;
69
vec.
append
(1);
70
vec.
append
(4);
71
vec.
append
(9);
72
vec.
append
(16);
73
int
i
= 1;
74
for
(
int
value : vec) {
75
EXPECT_EQ
(value,
i
*
i
);
76
i
++;
77
}
78
}
79
80
TEST
(vectorlist, ConstIterator)
81
{
82
VectorList<int>
vec;
83
vec.
append
(1);
84
vec.
append
(4);
85
vec.
append
(9);
86
vec.
append
(16);
87
const
VectorList<int>
&const_ref = vec;
88
int
i
= 0;
89
for
(
int
value : const_ref) {
90
i
++;
91
EXPECT_EQ
(value,
i
*
i
);
92
}
93
EXPECT_EQ
(
i
, 4);
94
}
95
96
TEST
(vectorlist, LimitIterator)
97
{
98
VectorList<int, 8, 128>
vec;
99
for
(
int64_t
i
:
IndexRange
(1024)) {
100
vec.
append
(
int
(
i
));
101
}
102
int
i
= 0;
103
for
(
int
value : vec) {
104
EXPECT_EQ
(value,
i
);
105
i
++;
106
}
107
EXPECT_EQ
(
i
, 1024);
108
}
109
110
TEST
(vectorlist, IteratorAfterClear)
111
{
112
VectorList<int, 8, 128>
vec;
113
for
(
int64_t
i
:
IndexRange
(1024)) {
114
vec.
append
(
int
(
i
));
115
}
116
vec.
clear
();
117
for
(
int64_t
i
:
IndexRange
(512)) {
118
vec.
append
(
int
(-
i
));
119
}
120
int
i
= 0;
121
for
(
int
value : vec) {
122
EXPECT_EQ
(value, -
i
);
123
i
++;
124
}
125
EXPECT_EQ
(
i
, 512);
126
}
127
128
TEST
(vectorlist, LimitIndexing)
129
{
130
VectorList<int, 8, 128>
vec;
131
for
(
int64_t
i
:
IndexRange
(1024)) {
132
vec.
append
(
int
(
i
));
133
}
134
for
(
int64_t
i
:
IndexRange
(1024)) {
135
EXPECT_EQ
(vec[
i
],
i
);
136
}
137
}
138
139
TEST
(vectorlist, ConstLimitIndexing)
140
{
141
VectorList<int, 8, 128>
vec;
142
for
(
int64_t
i
:
IndexRange
(1024)) {
143
vec.
append
(
int
(
i
));
144
}
145
const
VectorList<int, 8, 128>
&const_ref = vec;
146
for
(
int64_t
i
:
IndexRange
(1024)) {
147
EXPECT_EQ
(const_ref[
i
],
i
);
148
}
149
}
150
151
static
VectorList<int>
return_by_value_helper
()
152
{
153
VectorList<int>
vec;
154
vec.
append
(3);
155
vec.
append
(5);
156
vec.
append
(1);
157
return
vec;
158
}
159
160
TEST
(vectorlist, ReturnByValue)
161
{
162
VectorList<int>
vec =
return_by_value_helper
();
163
EXPECT_EQ
(vec.
size
(), 3);
164
EXPECT_EQ
(vec[0], 3);
165
EXPECT_EQ
(vec[1], 5);
166
EXPECT_EQ
(vec[2], 1);
167
}
168
169
TEST
(vectorlist, IsEmpty)
170
{
171
VectorList<int>
vec;
172
EXPECT_TRUE(vec.
is_empty
());
173
vec.
append
(1);
174
EXPECT_FALSE(vec.
is_empty
());
175
vec.
clear
();
176
EXPECT_TRUE(vec.
is_empty
());
177
}
178
179
TEST
(vectorlist, First)
180
{
181
VectorList<int>
vec;
182
vec.
append
(3);
183
vec.
append
(5);
184
vec.
append
(7);
185
EXPECT_EQ
(vec.
first
(), 3);
186
}
187
188
TEST
(vectorlist, Last)
189
{
190
VectorList<int>
vec;
191
vec.
append
(3);
192
vec.
append
(5);
193
vec.
append
(7);
194
EXPECT_EQ
(vec.
last
(), 7);
195
}
196
197
class
TypeConstructMock
{
198
public
:
199
bool
default_constructed
=
false
;
200
bool
copy_constructed
=
false
;
201
bool
move_constructed
=
false
;
202
bool
copy_assigned
=
false
;
203
bool
move_assigned
=
false
;
204
205
TypeConstructMock
() :
default_constructed
(
true
) {}
206
207
TypeConstructMock
(
const
TypeConstructMock
&
/*other*/
) :
copy_constructed
(
true
) {}
208
209
TypeConstructMock
(
TypeConstructMock
&&
/*other*/
) noexcept :
move_constructed
(
true
) {}
210
211
TypeConstructMock
&
operator=
(
const
TypeConstructMock
&other)
212
{
213
if
(
this
== &other) {
214
return
*
this
;
215
}
216
217
copy_assigned
=
true
;
218
return
*
this
;
219
}
220
221
TypeConstructMock
&
operator=
(
TypeConstructMock
&&other)
noexcept
222
{
223
if
(
this
== &other) {
224
return
*
this
;
225
}
226
227
move_assigned
=
true
;
228
return
*
this
;
229
}
230
};
231
232
TEST
(vectorlist, AppendCallsCopyConstructor)
233
{
234
VectorList<TypeConstructMock>
vec;
235
TypeConstructMock
value;
236
vec.
append
(value);
237
EXPECT_TRUE(vec[0].copy_constructed);
238
}
239
240
TEST
(vectorlist, AppendCallsMoveConstructor)
241
{
242
VectorList<TypeConstructMock>
vec;
243
vec.
append
(
TypeConstructMock
());
244
EXPECT_TRUE(vec[0].move_constructed);
245
}
246
247
TEST
(vectorlist, OveralignedValues)
248
{
249
VectorList<AlignedBuffer<1, 512>
> vec;
250
for
(
int
i
= 0;
i
< 100;
i
++) {
251
vec.
append
({});
252
EXPECT_EQ
(uintptr_t(&vec.
last
()) % 512, 0);
253
}
254
}
255
256
TEST
(vectorlist, AppendExceptions)
257
{
258
VectorList<ExceptionThrower>
vec;
259
vec.
append
({});
260
vec.
append
({});
261
ExceptionThrower
*ptr1 = &vec.
last
();
262
ExceptionThrower
value;
263
value.
throw_during_copy
=
true
;
264
EXPECT_ANY_THROW({ vec.
append
(value); });
265
EXPECT_EQ
(vec.
size
(), 2);
266
ExceptionThrower
*ptr2 = &vec.
last
();
267
EXPECT_EQ
(ptr1, ptr2);
268
}
269
270
}
// namespace blender::tests
BLI_exception_safety_test_utils.hh
EXPECT_EQ
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
BLI_strict_flags.h
BLI_vector_list.hh
true
return true
Definition
bmesh_iterators_inline.hh:143
int64_t
long long int int64_t
Definition
btConvexHullComputer.cpp:31
blender::IndexRange
Definition
BLI_index_range.hh:50
blender::VectorList
Definition
BLI_vector_list.hh:38
blender::VectorList::first
T & first()
Definition
BLI_vector_list.hh:97
blender::VectorList::is_empty
bool is_empty() const
Definition
BLI_vector_list.hh:124
blender::VectorList::append
void append(const T &value)
Definition
BLI_vector_list.hh:74
blender::VectorList::last
T & last()
Definition
BLI_vector_list.hh:107
blender::VectorList::clear
void clear()
Definition
BLI_vector_list.hh:130
blender::VectorList::size
int64_t size() const
Definition
BLI_vector_list.hh:114
blender::tests::ExceptionThrower
Definition
BLI_exception_safety_test_utils.hh:12
blender::tests::ExceptionThrower::throw_during_copy
bool throw_during_copy
Definition
BLI_exception_safety_test_utils.hh:24
blender::tests::TypeConstructMock
Definition
BLI_vector_list_test.cc:197
blender::tests::TypeConstructMock::TypeConstructMock
TypeConstructMock()
Definition
BLI_vector_list_test.cc:205
blender::tests::TypeConstructMock::copy_constructed
bool copy_constructed
Definition
BLI_vector_list_test.cc:200
blender::tests::TypeConstructMock::copy_assigned
bool copy_assigned
Definition
BLI_vector_list_test.cc:202
blender::tests::TypeConstructMock::move_assigned
bool move_assigned
Definition
BLI_vector_list_test.cc:203
blender::tests::TypeConstructMock::TypeConstructMock
TypeConstructMock(const TypeConstructMock &)
Definition
BLI_vector_list_test.cc:207
blender::tests::TypeConstructMock::TypeConstructMock
TypeConstructMock(TypeConstructMock &&) noexcept
Definition
BLI_vector_list_test.cc:209
blender::tests::TypeConstructMock::operator=
TypeConstructMock & operator=(TypeConstructMock &&other) noexcept
Definition
BLI_vector_list_test.cc:221
blender::tests::TypeConstructMock::default_constructed
bool default_constructed
Definition
BLI_vector_list_test.cc:199
blender::tests::TypeConstructMock::operator=
TypeConstructMock & operator=(const TypeConstructMock &other)
Definition
BLI_vector_list_test.cc:211
blender::tests::TypeConstructMock::move_constructed
bool move_constructed
Definition
BLI_vector_list_test.cc:201
blender::tests
Definition
BLF_tests.cc:9
blender::tests::return_by_value_helper
static VectorList< int > return_by_value_helper()
Definition
BLI_vector_list_test.cc:151
blender::tests::TEST
TEST(blf_load, load)
Definition
BLF_tests.cc:34
i
i
Definition
text_draw.cc:230
Generated on
for Blender by
doxygen
1.16.1