Blender
V4.5
source
blender
blenlib
tests
BLI_stack_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
#include <cstring>
7
8
#include "
BLI_stack.h
"
9
#include "
BLI_utildefines.h
"
10
11
#define SIZE 1024
12
13
/* number of items per chunk. use a small value to expose bugs */
14
#define STACK_CHUNK_SIZE 8
15
16
/* Ensure block size is set to #STACK_NEW_EX_ARGS */
17
#define BLI_stack_new(esize, descr) BLI_stack_new_ex(esize, descr, esize *STACK_CHUNK_SIZE)
18
19
TEST
(stack, Empty)
20
{
21
BLI_Stack
*stack;
22
23
stack =
BLI_stack_new
(
sizeof
(
int
), __func__);
24
EXPECT_TRUE(
BLI_stack_is_empty
(stack));
25
EXPECT_EQ
(
BLI_stack_count
(stack), 0);
26
BLI_stack_free
(stack);
27
}
28
29
TEST
(stack, One)
30
{
31
BLI_Stack
*stack;
32
uint
in
= -1,
out
= 1;
33
34
stack =
BLI_stack_new
(
sizeof
(
in
), __func__);
35
36
BLI_stack_push
(stack, (
void
*)&
in
);
37
EXPECT_FALSE(
BLI_stack_is_empty
(stack));
38
EXPECT_EQ
(
BLI_stack_count
(stack), 1);
39
BLI_stack_pop
(stack, (
void
*)&
out
);
40
EXPECT_EQ
(
out
,
in
);
41
EXPECT_TRUE(
BLI_stack_is_empty
(stack));
42
EXPECT_EQ
(
BLI_stack_count
(stack), 0);
43
BLI_stack_free
(stack);
44
}
45
46
TEST
(stack, Range)
47
{
48
const
int
tot =
SIZE
;
49
BLI_Stack
*stack;
50
int
in
,
out
;
51
52
stack =
BLI_stack_new
(
sizeof
(
in
), __func__);
53
54
for
(
in
= 0;
in
< tot;
in
++) {
55
BLI_stack_push
(stack, (
void
*)&
in
);
56
}
57
58
for
(
in
= tot - 1;
in
>= 0;
in
--) {
59
EXPECT_FALSE(
BLI_stack_is_empty
(stack));
60
BLI_stack_pop
(stack, (
void
*)&
out
);
61
EXPECT_EQ
(
out
,
in
);
62
}
63
EXPECT_TRUE(
BLI_stack_is_empty
(stack));
64
65
BLI_stack_free
(stack);
66
}
67
68
TEST
(stack, String)
69
{
70
const
int
tot =
SIZE
;
71
int
i
;
72
73
BLI_Stack
*stack;
74
char
in
[] =
"hello world!"
;
75
char
out
[
sizeof
(
in
)];
76
77
stack =
BLI_stack_new
(
sizeof
(
in
), __func__);
78
79
for
(
i
= 0;
i
< tot;
i
++) {
80
*((
int
*)
in
) =
i
;
81
BLI_stack_push
(stack, (
void
*)
in
);
82
}
83
84
for
(
i
= tot - 1;
i
>= 0;
i
--) {
85
EXPECT_FALSE(
BLI_stack_is_empty
(stack));
86
*((
int
*)
in
) =
i
;
87
BLI_stack_pop
(stack, (
void
*)&
out
);
88
EXPECT_STREQ(
in
,
out
);
89
}
90
EXPECT_TRUE(
BLI_stack_is_empty
(stack));
91
92
BLI_stack_free
(stack);
93
}
94
95
TEST
(stack, Peek)
96
{
97
const
int
tot =
SIZE
;
98
int
i
;
99
100
BLI_Stack
*stack;
101
const
short
in
[] = {1, 10, 100, 1000};
102
103
stack =
BLI_stack_new
(
sizeof
(*
in
), __func__);
104
105
for
(
i
= 0;
i
< tot;
i
++) {
106
BLI_stack_push
(stack, &
in
[
i
%
ARRAY_SIZE
(
in
)]);
107
}
108
109
for
(
i
= tot - 1;
i
>= 0;
i
--,
BLI_stack_discard
(stack)) {
110
short
*
ret
= (
short
*)
BLI_stack_peek
(stack);
111
EXPECT_EQ
(*
ret
,
in
[
i
%
ARRAY_SIZE
(
in
)]);
112
}
113
114
EXPECT_TRUE(
BLI_stack_is_empty
(stack));
115
116
BLI_stack_free
(stack);
117
}
118
119
/* Check that clearing the stack leaves in it a correct state. */
120
TEST
(stack, Clear)
121
{
122
const
int
tot_rerun = 4;
123
int
rerun;
124
125
/* based on range test */
126
int
tot =
SIZE
;
127
BLI_Stack
*stack;
128
int
in
,
out
;
129
130
/* use a small chunk size to ensure we test */
131
stack =
BLI_stack_new
(
sizeof
(
in
), __func__);
132
133
for
(rerun = 0; rerun < tot_rerun; rerun++) {
134
for
(
in
= 0;
in
< tot;
in
++) {
135
BLI_stack_push
(stack, (
void
*)&
in
);
136
}
137
138
BLI_stack_clear
(stack);
139
EXPECT_TRUE(
BLI_stack_is_empty
(stack));
140
141
/* and again, this time check it is valid */
142
for
(
in
= 0;
in
< tot;
in
++) {
143
BLI_stack_push
(stack, (
void
*)&
in
);
144
}
145
146
for
(
in
= tot - 1;
in
>= 0;
in
--) {
147
EXPECT_FALSE(
BLI_stack_is_empty
(stack));
148
BLI_stack_pop
(stack, (
void
*)&
out
);
149
EXPECT_EQ
(
out
,
in
);
150
}
151
152
EXPECT_TRUE(
BLI_stack_is_empty
(stack));
153
154
/* without this, we won't test case when mixed free/used */
155
tot /= 2;
156
}
157
158
BLI_stack_free
(stack);
159
}
160
161
TEST
(stack, Reuse)
162
{
163
const
int
sizes[] = {3, 11, 81, 400, 999, 12, 1, 9721, 7, 99, 5, 0};
164
int
sizes_test[
ARRAY_SIZE
(sizes)];
165
const
int
*s;
166
int
out
,
i
;
167
int
sum, sum_test;
168
169
BLI_Stack
*stack;
170
171
stack =
BLI_stack_new
(
sizeof
(
i
), __func__);
172
173
/* add a bunch of numbers, ensure we get same sum out */
174
sum = 0;
175
for
(s = sizes; *s; s++) {
176
for
(
i
= *s;
i
!= 0;
i
--) {
177
BLI_stack_push
(stack, (
void
*)&
i
);
178
sum +=
i
;
179
}
180
}
181
sum_test = 0;
182
while
(!
BLI_stack_is_empty
(stack)) {
183
BLI_stack_pop
(stack, (
void
*)&
out
);
184
sum_test +=
out
;
185
}
186
EXPECT_EQ
(sum, sum_test);
187
188
/* add and remove all except last */
189
for
(s = sizes; *s; s++) {
190
for
(
i
= *s;
i
>= 0;
i
--) {
191
BLI_stack_push
(stack, (
void
*)&
i
);
192
}
193
for
(
i
= *s;
i
> 0;
i
--) {
194
BLI_stack_pop
(stack, (
void
*)&
out
);
195
}
196
}
197
198
i
=
ARRAY_SIZE
(sizes) - 1;
199
while
(!
BLI_stack_is_empty
(stack)) {
200
i
--;
201
BLI_stack_pop
(stack, (
void
*)&sizes_test[
i
]);
202
EXPECT_EQ
(sizes_test[
i
], sizes[
i
]);
203
EXPECT_GT(
i
, -1);
204
}
205
EXPECT_EQ
(0,
i
);
206
EXPECT_EQ
(memcmp(sizes, sizes_test,
sizeof
(sizes) -
sizeof
(
int
)), 0);
207
208
/* finally test BLI_stack_pop_n */
209
for
(
i
=
ARRAY_SIZE
(sizes);
i
--;) {
210
BLI_stack_push
(stack, (
void
*)&sizes[
i
]);
211
}
212
EXPECT_EQ
(
BLI_stack_count
(stack),
ARRAY_SIZE
(sizes));
213
BLI_stack_pop_n
(stack, (
void
*)sizes_test,
ARRAY_SIZE
(sizes));
214
EXPECT_EQ
(memcmp(sizes, sizes_test,
sizeof
(sizes) -
sizeof
(
int
)), 0);
215
216
BLI_stack_free
(stack);
217
}
TEST
TEST(array_store, Nop)
Definition
BLI_array_store_test.cc:305
EXPECT_EQ
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
SIZE
#define SIZE
Definition
BLI_heap_simple_test.cc:16
BLI_stack.h
BLI_stack_pop
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL()
Definition
stack.cc:138
BLI_stack_pop_n
void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n) ATTR_NONNULL()
Definition
stack.cc:147
BLI_stack_count
size_t BLI_stack_count(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition
stack.cc:228
BLI_stack_push
void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL()
Definition
stack.cc:132
BLI_stack_is_empty
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition
stack.cc:250
BLI_stack_clear
void BLI_stack_clear(BLI_Stack *stack) ATTR_NONNULL()
Definition
stack.cc:196
BLI_stack_free
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL()
Definition
stack.cc:96
BLI_stack_peek
void * BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition
stack.cc:169
BLI_stack_discard
void BLI_stack_discard(BLI_Stack *stack) ATTR_NONNULL()
Definition
stack.cc:176
BLI_stack_new
#define BLI_stack_new(esize, descr)
Definition
BLI_stack_test.cc:17
uint
unsigned int uint
Definition
BLI_sys_types.h:64
BLI_utildefines.h
ARRAY_SIZE
#define ARRAY_SIZE(arr)
Definition
BLI_utildefines.h:271
in
#define in
Definition
gpu_glsl_cpp_stubs.hh:949
out
#define out
Definition
gpu_glsl_cpp_stubs.hh:947
ret
return ret
Definition
python_utildefines.hh:33
BLI_Stack
Definition
stack.cc:32
i
i
Definition
text_draw.cc:230
Generated on
for Blender by
doxygen
1.18.0