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