Blender V5.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.hh"
11#include "BLI_heap_simple.hh"
12#include "BLI_rand_c.hh"
13#include "BLI_sys_types.hh"
14#include "BLI_utildefines.hh"
15
16namespace blender {
17
18#define SIZE 1024
19
20static void range_fl(float *array_tar, const int size)
21{
22 float *array_pt = array_tar + (size - 1);
23 int i = size;
24 while (i--) {
25 *(array_pt--) = float(i);
26 }
27}
28
29TEST(heap, SimpleEmpty)
30{
31 HeapSimple *heap;
32
33 heap = BLI_heapsimple_new();
36 BLI_heapsimple_free(heap, nullptr);
37}
38
39TEST(heap, SimpleOne)
40{
41 HeapSimple *heap;
42 const char *in = "test";
43
44 heap = BLI_heapsimple_new();
45
46 BLI_heapsimple_insert(heap, 0.0f, const_cast<char *>(in));
52 BLI_heapsimple_free(heap, nullptr);
53}
54
55TEST(heap, SimpleRange)
56{
57 const int items_total = SIZE;
59 for (int in = 0; in < items_total; in++) {
61 }
62 for (int out_test = 0; out_test < items_total; out_test++) {
64 }
66 BLI_heapsimple_free(heap, nullptr);
67}
68
69TEST(heap, SimpleRangeReverse)
70{
71 const int items_total = SIZE;
73 for (int in = 0; in < items_total; in++) {
75 }
76 for (int out_test = items_total - 1; out_test >= 0; out_test--) {
78 }
80 BLI_heapsimple_free(heap, nullptr);
81}
82
83TEST(heap, SimpleDuplicates)
84{
85 const int items_total = SIZE;
87 for (int in = 0; in < items_total; in++) {
88 BLI_heapsimple_insert(heap, 1.0f, nullptr);
89 }
90 for (int out_test = 0; out_test < items_total; out_test++) {
92 }
94 BLI_heapsimple_free(heap, nullptr);
95}
96
97static void random_heapsimple_helper(const int items_total, const int random_seed)
98{
100 float *values = MEM_new_array_uninitialized<float>(size_t(items_total), __func__);
101 range_fl(values, items_total);
102 BLI_array_randomize(values, sizeof(float), items_total, random_seed);
103 for (int i = 0; i < items_total; i++) {
104 BLI_heapsimple_insert(heap, values[i], POINTER_FROM_INT(int(values[i])));
105 }
106 for (int out_test = 0; out_test < items_total; out_test++) {
108 }
110 BLI_heapsimple_free(heap, nullptr);
111 MEM_delete(values);
112}
113
114TEST(heap, SimpleRand1)
115{
117}
118TEST(heap, SimpleRand2)
119{
121}
122TEST(heap, SimpleRand100)
123{
124 random_heapsimple_helper(100, 4321);
125}
126
127} // namespace blender
A min-heap / priority queue ADT.
#define SIZE
Random number functions.
Read Blender memory allocation and freeing.
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
BMesh Iterators.
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
uint BLI_heapsimple_len(const HeapSimple *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_array_randomize(void *data, unsigned int elem_size, unsigned int elem_num, unsigned int seed)
Definition rand.cc:158
TEST(array_store, Nop)
void * BLI_heapsimple_pop_min(HeapSimple *heap) ATTR_NONNULL(1)
static void random_heapsimple_helper(const int items_total, const int random_seed)
void BLI_heapsimple_free(HeapSimple *heap, HeapSimpleFreeFP ptrfreefp) ATTR_NONNULL(1)
HeapSimple * BLI_heapsimple_new() ATTR_WARN_UNUSED_RESULT
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BLI_heapsimple_insert(HeapSimple *heap, float value, void *ptr) ATTR_NONNULL(1)
static void range_fl(float *array_tar, const int size)
bool BLI_heapsimple_is_empty(const HeapSimple *heap) ATTR_NONNULL(1)