2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: Apache-2.0 */
|
2017-10-29 00:40:40 +11:00
|
|
|
|
|
|
|
#include "testing/testing.h"
|
2020-12-04 11:28:09 +01:00
|
|
|
#include <cstring>
|
2017-10-29 00:40:40 +11:00
|
|
|
|
2020-01-26 16:38:18 +01:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2017-10-29 00:40:40 +11:00
|
|
|
#include "BLI_compiler_attrs.h"
|
|
|
|
#include "BLI_heap.h"
|
|
|
|
#include "BLI_rand.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2017-10-29 00:40:40 +11:00
|
|
|
|
|
|
|
#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--) {
|
2022-09-25 18:33:28 +10:00
|
|
|
*(array_pt--) = float(i);
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(heap, Empty)
|
|
|
|
{
|
|
|
|
Heap *heap;
|
|
|
|
|
|
|
|
heap = BLI_heap_new();
|
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2018-02-15 23:36:11 +11:00
|
|
|
EXPECT_EQ(BLI_heap_len(heap), 0);
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(heap, One)
|
|
|
|
{
|
|
|
|
Heap *heap;
|
|
|
|
const char *in = "test";
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-10-29 00:40:40 +11:00
|
|
|
heap = BLI_heap_new();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-10-29 00:40:40 +11:00
|
|
|
BLI_heap_insert(heap, 0.0f, (void *)in);
|
|
|
|
EXPECT_FALSE(BLI_heap_is_empty(heap));
|
2018-02-15 23:36:11 +11:00
|
|
|
EXPECT_EQ(BLI_heap_len(heap), 1);
|
|
|
|
EXPECT_EQ(in, BLI_heap_pop_min(heap));
|
2017-10-29 00:40:40 +11:00
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2018-02-15 23:36:11 +11:00
|
|
|
EXPECT_EQ(BLI_heap_len(heap), 0);
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(heap, Range)
|
|
|
|
{
|
|
|
|
const int items_total = SIZE;
|
|
|
|
Heap *heap = BLI_heap_new();
|
|
|
|
for (int in = 0; in < items_total; in++) {
|
2022-09-25 18:33:28 +10:00
|
|
|
BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in));
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
for (int out_test = 0; out_test < items_total; out_test++) {
|
2018-09-19 12:05:58 +10:00
|
|
|
EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(heap, RangeReverse)
|
|
|
|
{
|
|
|
|
const int items_total = SIZE;
|
|
|
|
Heap *heap = BLI_heap_new();
|
|
|
|
for (int in = 0; in < items_total; in++) {
|
2022-09-25 18:33:28 +10:00
|
|
|
BLI_heap_insert(heap, float(-in), POINTER_FROM_INT(-in));
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
for (int out_test = items_total - 1; out_test >= 0; out_test--) {
|
2018-09-19 12:05:58 +10:00
|
|
|
EXPECT_EQ(-out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(heap, RangeRemove)
|
|
|
|
{
|
|
|
|
const int items_total = SIZE;
|
|
|
|
Heap *heap = BLI_heap_new();
|
|
|
|
HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
|
|
|
|
for (int in = 0; in < items_total; in++) {
|
2022-09-25 18:33:28 +10:00
|
|
|
nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in));
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
for (int i = 0; i < items_total; i += 2) {
|
|
|
|
BLI_heap_remove(heap, nodes[i]);
|
2020-11-06 17:49:09 +01:00
|
|
|
nodes[i] = nullptr;
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
for (int out_test = 1; out_test < items_total; out_test += 2) {
|
2018-09-19 12:05:58 +10:00
|
|
|
EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 00:40:40 +11:00
|
|
|
MEM_freeN(nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(heap, Duplicates)
|
|
|
|
{
|
|
|
|
const int items_total = SIZE;
|
|
|
|
Heap *heap = BLI_heap_new();
|
|
|
|
for (int in = 0; in < items_total; in++) {
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_insert(heap, 1.0f, nullptr);
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
for (int out_test = 0; out_test < items_total; out_test++) {
|
2018-09-19 12:05:58 +10:00
|
|
|
EXPECT_EQ(0, POINTER_AS_INT(BLI_heap_pop_min(heap)));
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 00:40:40 +11:00
|
|
|
}
|
2017-10-29 04:42:58 +11:00
|
|
|
|
2017-10-29 18:23:33 +11:00
|
|
|
static void random_heap_helper(const int items_total, const int random_seed)
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
2017-10-29 00:40:40 +11:00
|
|
|
Heap *heap = BLI_heap_new();
|
|
|
|
float *values = (float *)MEM_mallocN(sizeof(float) * 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++) {
|
2022-09-25 18:33:28 +10:00
|
|
|
BLI_heap_insert(heap, values[i], POINTER_FROM_INT(int(values[i])));
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2017-10-29 00:40:40 +11:00
|
|
|
for (int out_test = 0; out_test < items_total; out_test++) {
|
2018-09-19 12:05:58 +10:00
|
|
|
EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2017-10-29 00:40:40 +11:00
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 00:40:40 +11:00
|
|
|
MEM_freeN(values);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 00:40:40 +11:00
|
|
|
TEST(heap, Rand1)
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
2017-10-29 00:40:40 +11:00
|
|
|
random_heap_helper(1, 1234);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2017-10-29 00:40:40 +11:00
|
|
|
TEST(heap, Rand2)
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
2017-10-29 00:40:40 +11:00
|
|
|
random_heap_helper(2, 1234);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2017-10-29 00:40:40 +11:00
|
|
|
TEST(heap, Rand100)
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
2017-10-29 00:40:40 +11:00
|
|
|
random_heap_helper(100, 4321);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2017-10-29 04:42:58 +11:00
|
|
|
|
|
|
|
TEST(heap, ReInsertSimple)
|
|
|
|
{
|
|
|
|
const int items_total = SIZE;
|
|
|
|
Heap *heap = BLI_heap_new();
|
|
|
|
HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
|
|
|
|
for (int in = 0; in < items_total; in++) {
|
2022-09-25 18:33:28 +10:00
|
|
|
nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in));
|
2017-10-29 04:42:58 +11:00
|
|
|
}
|
|
|
|
for (int i = 0; i < items_total; i++) {
|
2022-09-25 18:33:28 +10:00
|
|
|
BLI_heap_node_value_update(heap, nodes[i], float(items_total + i));
|
2017-10-29 04:42:58 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-10-29 04:42:58 +11:00
|
|
|
for (int out_test = 0; out_test < items_total; out_test++) {
|
2018-09-19 12:05:58 +10:00
|
|
|
EXPECT_EQ(out_test, POINTER_AS_INT(BLI_heap_pop_min(heap)));
|
2017-10-29 04:42:58 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-10-29 04:42:58 +11:00
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 04:42:58 +11:00
|
|
|
MEM_freeN(nodes);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-10-29 18:23:33 +11:00
|
|
|
static void random_heap_reinsert_helper(const int items_total, const int random_seed)
|
2017-10-29 04:42:58 +11:00
|
|
|
{
|
|
|
|
Heap *heap = BLI_heap_new();
|
|
|
|
HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__);
|
|
|
|
for (int in = 0; in < items_total; in++) {
|
2022-09-25 18:33:28 +10:00
|
|
|
nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in));
|
2017-10-29 04:42:58 +11:00
|
|
|
}
|
2017-10-29 18:23:33 +11:00
|
|
|
BLI_array_randomize(nodes, sizeof(HeapNode *), items_total, random_seed);
|
2017-10-29 04:42:58 +11:00
|
|
|
for (int i = 0; i < items_total; i++) {
|
2022-09-25 18:33:28 +10:00
|
|
|
BLI_heap_node_value_update(heap, nodes[i], float(i));
|
2017-10-29 04:42:58 +11:00
|
|
|
}
|
2017-10-29 18:23:33 +11:00
|
|
|
EXPECT_TRUE(BLI_heap_is_valid(heap));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-10-29 04:42:58 +11:00
|
|
|
for (int out_test = 0; out_test < items_total; out_test++) {
|
|
|
|
HeapNode *node_top = BLI_heap_top(heap);
|
|
|
|
float out = BLI_heap_node_value(node_top);
|
2018-11-04 13:27:10 +03:00
|
|
|
EXPECT_EQ(out, BLI_heap_top_value(heap));
|
2022-09-25 18:33:28 +10:00
|
|
|
EXPECT_EQ(float(out_test), out);
|
2018-02-15 23:36:11 +11:00
|
|
|
BLI_heap_pop_min(heap);
|
2017-10-29 04:42:58 +11:00
|
|
|
}
|
|
|
|
EXPECT_TRUE(BLI_heap_is_empty(heap));
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_heap_free(heap, nullptr);
|
2017-10-29 04:42:58 +11:00
|
|
|
MEM_freeN(nodes);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 18:23:33 +11:00
|
|
|
TEST(heap, ReInsertRandom1)
|
|
|
|
{
|
|
|
|
random_heap_reinsert_helper(1, 1234);
|
|
|
|
}
|
|
|
|
TEST(heap, ReInsertRandom2)
|
|
|
|
{
|
|
|
|
random_heap_reinsert_helper(2, 1234);
|
|
|
|
}
|
|
|
|
TEST(heap, ReInsertRandom100)
|
|
|
|
{
|
|
|
|
random_heap_reinsert_helper(100, 4321);
|
|
|
|
}
|
|
|
|
TEST(heap, ReInsertRandom1024)
|
|
|
|
{
|
|
|
|
random_heap_reinsert_helper(1024, 9876);
|
|
|
|
}
|
|
|
|
TEST(heap, ReInsertRandom2048)
|
|
|
|
{
|
|
|
|
random_heap_reinsert_helper(2048, 5321);
|
|
|
|
}
|