Files
test2/source/blender/blenlib/tests/BLI_heap_simple_test.cc
Bastien Montagne cef8de874b Cleanup: blenlib: Replace 'void' MEM_[cm]allocN with templated, type-safe MEM_[cm]allocN<T>.
The main issue of 'type-less' standard C allocations is that there is no check on
allocated type possible.

This is a serious source of annoyance (and crashes) when making some
low-level structs non-trivial, as tracking down all usages of these
structs in higher-level other structs and their allocation is... really
painful.

MEM_[cm]allocN<T> templates on the other hand do check that the
given type is trivial, at build time (static assert), which makes such issue...
trivial to catch.

NOTE: New code should strive to use MEM_new (i.e. allocation and
construction) as much as possible, even for trivial PoD types.

Pull Request: https://projects.blender.org/blender/blender/pulls/136268
2025-03-21 11:50:00 +01:00

124 lines
3.2 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: Apache-2.0 */
#include "testing/testing.h"
#include <cstring>
#include "MEM_guardedalloc.h"
#include "BLI_compiler_attrs.h"
#include "BLI_heap_simple.h"
#include "BLI_rand.h"
#include "BLI_sys_types.h"
#include "BLI_utildefines.h"
#define SIZE 1024
static void range_fl(float *array_tar, const int size)
{
float *array_pt = array_tar + (size - 1);
int i = size;
while (i--) {
*(array_pt--) = float(i);
}
}
TEST(heap, SimpleEmpty)
{
HeapSimple *heap;
heap = BLI_heapsimple_new();
EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
EXPECT_EQ(BLI_heapsimple_len(heap), 0);
BLI_heapsimple_free(heap, nullptr);
}
TEST(heap, SimpleOne)
{
HeapSimple *heap;
const char *in = "test";
heap = BLI_heapsimple_new();
BLI_heapsimple_insert(heap, 0.0f, (void *)in);
EXPECT_FALSE(BLI_heapsimple_is_empty(heap));
EXPECT_EQ(BLI_heapsimple_len(heap), 1);
EXPECT_EQ(in, BLI_heapsimple_pop_min(heap));
EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
EXPECT_EQ(BLI_heapsimple_len(heap), 0);
BLI_heapsimple_free(heap, nullptr);
}
TEST(heap, SimpleRange)
{
const int items_total = SIZE;
HeapSimple *heap = BLI_heapsimple_new();
for (int in = 0; in < items_total; in++) {
BLI_heapsimple_insert(heap, float(in), POINTER_FROM_INT(in));
}
for (int out_test = 0; out_test < items_total; out_test++) {
EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heapsimple_pop_min(heap)));
}
EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
BLI_heapsimple_free(heap, nullptr);
}
TEST(heap, SimpleRangeReverse)
{
const int items_total = SIZE;
HeapSimple *heap = BLI_heapsimple_new();
for (int in = 0; in < items_total; in++) {
BLI_heapsimple_insert(heap, float(-in), POINTER_FROM_INT(-in));
}
for (int out_test = items_total - 1; out_test >= 0; out_test--) {
EXPECT_EQ(-out_test, POINTER_AS_INT(BLI_heapsimple_pop_min(heap)));
}
EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
BLI_heapsimple_free(heap, nullptr);
}
TEST(heap, SimpleDuplicates)
{
const int items_total = SIZE;
HeapSimple *heap = BLI_heapsimple_new();
for (int in = 0; in < items_total; in++) {
BLI_heapsimple_insert(heap, 1.0f, nullptr);
}
for (int out_test = 0; out_test < items_total; out_test++) {
EXPECT_EQ(0, POINTER_AS_INT(BLI_heapsimple_pop_min(heap)));
}
EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
BLI_heapsimple_free(heap, nullptr);
}
static void random_heapsimple_helper(const int items_total, const int random_seed)
{
HeapSimple *heap = BLI_heapsimple_new();
float *values = MEM_malloc_arrayN<float>(size_t(items_total), __func__);
range_fl(values, items_total);
BLI_array_randomize(values, sizeof(float), items_total, random_seed);
for (int i = 0; i < items_total; i++) {
BLI_heapsimple_insert(heap, values[i], POINTER_FROM_INT(int(values[i])));
}
for (int out_test = 0; out_test < items_total; out_test++) {
EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heapsimple_pop_min(heap)));
}
EXPECT_TRUE(BLI_heapsimple_is_empty(heap));
BLI_heapsimple_free(heap, nullptr);
MEM_freeN(values);
}
TEST(heap, SimpleRand1)
{
random_heapsimple_helper(1, 1234);
}
TEST(heap, SimpleRand2)
{
random_heapsimple_helper(2, 1234);
}
TEST(heap, SimpleRand100)
{
random_heapsimple_helper(100, 4321);
}