Blender V4.3
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
19TEST(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
29TEST(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
46TEST(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
68TEST(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
95TEST(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. */
120TEST(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
161TEST(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}
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL()
Definition stack.c:137
void BLI_stack_pop_n(BLI_Stack *stack, void *dst, unsigned int n) ATTR_NONNULL()
Definition stack.c:146
size_t BLI_stack_count(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.c:227
void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL()
Definition stack.c:131
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.c:249
void BLI_stack_clear(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.c:195
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.c:96
void * BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.c:168
void BLI_stack_discard(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.c:175
#define BLI_stack_new(esize, descr)
TEST(stack, Empty)
#define SIZE
unsigned int uint
#define ARRAY_SIZE(arr)
static T sum(const btAlignedObjectArray< T > &items)
return ret