Blender V4.3
BLI_heap_simple_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_simple.h"
12#include "BLI_rand.h"
13#include "BLI_sys_types.h"
14#include "BLI_utildefines.h"
15
16#define SIZE 1024
17
18static void range_fl(float *array_tar, const int size)
19{
20 float *array_pt = array_tar + (size - 1);
21 int i = size;
22 while (i--) {
23 *(array_pt--) = float(i);
24 }
25}
26
27TEST(heap, SimpleEmpty)
28{
29 HeapSimple *heap;
30
31 heap = BLI_heapsimple_new();
32 EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
34 BLI_heapsimple_free(heap, nullptr);
35}
36
37TEST(heap, SimpleOne)
38{
39 HeapSimple *heap;
40 const char *in = "test";
41
42 heap = BLI_heapsimple_new();
43
44 BLI_heapsimple_insert(heap, 0.0f, (void *)in);
45 EXPECT_FALSE(BLI_heapsimple_is_empty(heap));
48 EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
50 BLI_heapsimple_free(heap, nullptr);
51}
52
53TEST(heap, SimpleRange)
54{
55 const int items_total = SIZE;
57 for (int in = 0; in < items_total; in++) {
58 BLI_heapsimple_insert(heap, float(in), POINTER_FROM_INT(in));
59 }
60 for (int out_test = 0; out_test < items_total; out_test++) {
62 }
63 EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
64 BLI_heapsimple_free(heap, nullptr);
65}
66
67TEST(heap, SimpleRangeReverse)
68{
69 const int items_total = SIZE;
71 for (int in = 0; in < items_total; in++) {
72 BLI_heapsimple_insert(heap, float(-in), POINTER_FROM_INT(-in));
73 }
74 for (int out_test = items_total - 1; out_test >= 0; out_test--) {
76 }
77 EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
78 BLI_heapsimple_free(heap, nullptr);
79}
80
81TEST(heap, SimpleDuplicates)
82{
83 const int items_total = SIZE;
85 for (int in = 0; in < items_total; in++) {
86 BLI_heapsimple_insert(heap, 1.0f, nullptr);
87 }
88 for (int out_test = 0; out_test < items_total; out_test++) {
90 }
91 EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
92 BLI_heapsimple_free(heap, nullptr);
93}
94
95static void random_heapsimple_helper(const int items_total, const int random_seed)
96{
98 float *values = (float *)MEM_mallocN(sizeof(float) * items_total, __func__);
99 range_fl(values, items_total);
100 BLI_array_randomize(values, sizeof(float), items_total, random_seed);
101 for (int i = 0; i < items_total; i++) {
102 BLI_heapsimple_insert(heap, values[i], POINTER_FROM_INT(int(values[i])));
103 }
104 for (int out_test = 0; out_test < items_total; out_test++) {
106 }
107 EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
108 BLI_heapsimple_free(heap, nullptr);
109 MEM_freeN(values);
110}
111
112TEST(heap, SimpleRand1)
113{
115}
116TEST(heap, SimpleRand2)
117{
119}
120TEST(heap, SimpleRand100)
121{
122 random_heapsimple_helper(100, 4321);
123}
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
A min-heap / priority queue ADT.
HeapSimple * BLI_heapsimple_new(void) ATTR_WARN_UNUSED_RESULT
void BLI_heapsimple_free(HeapSimple *heap, HeapSimpleFreeFP ptrfreefp) ATTR_NONNULL(1)
void * BLI_heapsimple_pop_min(HeapSimple *heap) ATTR_NONNULL(1)
bool BLI_heapsimple_is_empty(const HeapSimple *heap) ATTR_NONNULL(1)
uint BLI_heapsimple_len(const HeapSimple *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_heapsimple_insert(HeapSimple *heap, float value, void *ptr) ATTR_NONNULL(1)
static void random_heapsimple_helper(const int items_total, const int random_seed)
#define SIZE
static void range_fl(float *array_tar, const int size)
TEST(heap, SimpleEmpty)
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