Blender V4.3
BLI_heap_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 "MEM_guardedalloc.h"
9
10#include "BLI_compiler_attrs.h"
11#include "BLI_heap.h"
12#include "BLI_rand.h"
13#include "BLI_utildefines.h"
14
15#define SIZE 1024
16
17static void range_fl(float *array_tar, const int size)
18{
19 float *array_pt = array_tar + (size - 1);
20 int i = size;
21 while (i--) {
22 *(array_pt--) = float(i);
23 }
24}
25
26TEST(heap, Empty)
27{
28 Heap *heap;
29
30 heap = BLI_heap_new();
31 EXPECT_TRUE(BLI_heap_is_empty(heap));
32 EXPECT_EQ(BLI_heap_len(heap), 0);
33 BLI_heap_free(heap, nullptr);
34}
35
36TEST(heap, One)
37{
38 Heap *heap;
39 const char *in = "test";
40
41 heap = BLI_heap_new();
42
43 BLI_heap_insert(heap, 0.0f, (void *)in);
44 EXPECT_FALSE(BLI_heap_is_empty(heap));
45 EXPECT_EQ(BLI_heap_len(heap), 1);
46 EXPECT_EQ(in, BLI_heap_pop_min(heap));
47 EXPECT_TRUE(BLI_heap_is_empty(heap));
48 EXPECT_EQ(BLI_heap_len(heap), 0);
49 BLI_heap_free(heap, nullptr);
50}
51
52TEST(heap, Range)
53{
54 const int items_total = SIZE;
55 Heap *heap = BLI_heap_new();
56 for (int in = 0; in < items_total; in++) {
57 BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in));
58 }
59 for (int out_test = 0; out_test < items_total; out_test++) {
61 }
62 EXPECT_TRUE(BLI_heap_is_empty(heap));
63 BLI_heap_free(heap, nullptr);
64}
65
66TEST(heap, RangeReverse)
67{
68 const int items_total = SIZE;
69 Heap *heap = BLI_heap_new();
70 for (int in = 0; in < items_total; in++) {
71 BLI_heap_insert(heap, float(-in), POINTER_FROM_INT(-in));
72 }
73 for (int out_test = items_total - 1; out_test >= 0; out_test--) {
74 EXPECT_EQ(-out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
75 }
76 EXPECT_TRUE(BLI_heap_is_empty(heap));
77 BLI_heap_free(heap, nullptr);
78}
79
80TEST(heap, RangeRemove)
81{
82 const int items_total = SIZE;
83 Heap *heap = BLI_heap_new();
84 HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
85 for (int in = 0; in < items_total; in++) {
86 nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in));
87 }
88 for (int i = 0; i < items_total; i += 2) {
89 BLI_heap_remove(heap, nodes[i]);
90 nodes[i] = nullptr;
91 }
92 for (int out_test = 1; out_test < items_total; out_test += 2) {
94 }
95 EXPECT_TRUE(BLI_heap_is_empty(heap));
96 BLI_heap_free(heap, nullptr);
97 MEM_freeN(nodes);
98}
99
100TEST(heap, Duplicates)
101{
102 const int items_total = SIZE;
103 Heap *heap = BLI_heap_new();
104 for (int in = 0; in < items_total; in++) {
105 BLI_heap_insert(heap, 1.0f, nullptr);
106 }
107 for (int out_test = 0; out_test < items_total; out_test++) {
109 }
110 EXPECT_TRUE(BLI_heap_is_empty(heap));
111 BLI_heap_free(heap, nullptr);
112}
113
114static void random_heap_helper(const int items_total, const int random_seed)
115{
116 Heap *heap = BLI_heap_new();
117 float *values = (float *)MEM_mallocN(sizeof(float) * items_total, __func__);
118 range_fl(values, items_total);
119 BLI_array_randomize(values, sizeof(float), items_total, random_seed);
120 for (int i = 0; i < items_total; i++) {
121 BLI_heap_insert(heap, values[i], POINTER_FROM_INT(int(values[i])));
122 }
123 for (int out_test = 0; out_test < items_total; out_test++) {
124 EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
125 }
126 EXPECT_TRUE(BLI_heap_is_empty(heap));
127 BLI_heap_free(heap, nullptr);
128 MEM_freeN(values);
129}
130
131TEST(heap, Rand1)
132{
133 random_heap_helper(1, 1234);
134}
135TEST(heap, Rand2)
136{
137 random_heap_helper(2, 1234);
138}
139TEST(heap, Rand100)
140{
141 random_heap_helper(100, 4321);
142}
143
144TEST(heap, ReInsertSimple)
145{
146 const int items_total = SIZE;
147 Heap *heap = BLI_heap_new();
148 HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
149 for (int in = 0; in < items_total; in++) {
150 nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in));
151 }
152 for (int i = 0; i < items_total; i++) {
153 BLI_heap_node_value_update(heap, nodes[i], float(items_total + i));
154 }
155
156 for (int out_test = 0; out_test < items_total; out_test++) {
157 EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
158 }
159
160 EXPECT_TRUE(BLI_heap_is_empty(heap));
161 BLI_heap_free(heap, nullptr);
162 MEM_freeN(nodes);
163}
164
165static void random_heap_reinsert_helper(const int items_total, const int random_seed)
166{
167 Heap *heap = BLI_heap_new();
168 HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
169 for (int in = 0; in < items_total; in++) {
170 nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in));
171 }
172 BLI_array_randomize(nodes, sizeof(HeapNode *), items_total, random_seed);
173 for (int i = 0; i < items_total; i++) {
174 BLI_heap_node_value_update(heap, nodes[i], float(i));
175 }
176 EXPECT_TRUE(BLI_heap_is_valid(heap));
177
178 for (int out_test = 0; out_test < items_total; out_test++) {
179 HeapNode *node_top = BLI_heap_top(heap);
180 float out = BLI_heap_node_value(node_top);
181 EXPECT_EQ(out, BLI_heap_top_value(heap));
182 EXPECT_EQ(float(out_test), out);
183 BLI_heap_pop_min(heap);
184 }
185 EXPECT_TRUE(BLI_heap_is_empty(heap));
186 BLI_heap_free(heap, nullptr);
187 MEM_freeN(nodes);
188}
189
190TEST(heap, ReInsertRandom1)
191{
193}
194TEST(heap, ReInsertRandom2)
195{
197}
198TEST(heap, ReInsertRandom100)
199{
201}
202TEST(heap, ReInsertRandom1024)
203{
204 random_heap_reinsert_helper(1024, 9876);
205}
206TEST(heap, ReInsertRandom2048)
207{
208 random_heap_reinsert_helper(2048, 5321);
209}
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
A min-heap / priority queue ADT.
bool BLI_heap_is_valid(const Heap *heap)
Definition BLI_heap.c:378
HeapNode * BLI_heap_top(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.c:279
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp) ATTR_NONNULL(1)
Definition BLI_heap.c:192
void void float BLI_heap_node_value(const HeapNode *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.c:347
void void bool BLI_heap_is_empty(const Heap *heap) ATTR_NONNULL(1)
Definition BLI_heap.c:269
unsigned int BLI_heap_len(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.c:274
float BLI_heap_top_value(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.c:284
void BLI_heap_node_value_update(Heap *heap, HeapNode *node, float value) ATTR_NONNULL(1
void * BLI_heap_pop_min(Heap *heap) ATTR_NONNULL(1)
Definition BLI_heap.c:291
HeapNode * BLI_heap_insert(Heap *heap, float value, void *ptr) ATTR_NONNULL(1)
Definition BLI_heap.c:235
void void BLI_heap_remove(Heap *heap, HeapNode *node) ATTR_NONNULL(1
Heap * BLI_heap_new(void) ATTR_WARN_UNUSED_RESULT
Definition BLI_heap.c:187
static void random_heap_helper(const int items_total, const int random_seed)
static void random_heap_reinsert_helper(const int items_total, const int random_seed)
TEST(heap, Empty)
#define SIZE
static void range_fl(float *array_tar, const int size)
Random number functions.
void BLI_array_randomize(void *data, unsigned int elem_size, unsigned int elem_num, unsigned int seed)
Definition rand.cc:188
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105