libvieter/test/heap/test_heap.c

134 lines
3.1 KiB
C
Raw Normal View History

2023-01-21 16:31:22 +01:00
#include "acutest.h"
#include "vieter_heap.h"
#include "vieter_heap_tree.h"
#include <stdlib.h>
2023-01-21 16:31:22 +01:00
#define TEST_SIZE(heap, size) \
TEST_CHECK(vieter_heap_size(heap) == size); \
TEST_MSG("Size: %zu", vieter_heap_size(heap))
void test_init() {
vieter_heap *heap = vieter_heap_init();
TEST_CHECK(heap != NULL);
TEST_SIZE(heap, 0);
vieter_heap_free(heap);
}
2023-01-22 20:34:05 +01:00
void test_merge_same_order() {
vieter_heap_node *root_a = vieter_heap_node_init();
root_a->key = 1;
root_a->order = 0;
2023-01-22 20:34:05 +01:00
vieter_heap_node *root_b = vieter_heap_node_init();
root_b->key = 2;
root_b->order = 0;
2023-01-22 20:34:05 +01:00
vieter_heap_node *merged = vieter_heap_tree_merge_same_order(root_a, root_b);
2023-01-22 20:34:05 +01:00
TEST_CHECK(merged == root_a);
TEST_CHECK(merged->key == 1);
TEST_CHECK(merged->largest_order == root_b);
TEST_CHECK(merged->ptr.next_largest_order == NULL);
2023-01-24 12:07:30 +01:00
vieter_heap_tree_free(merged);
2023-01-22 20:34:05 +01:00
}
void test_insert() {
vieter_heap *heap = vieter_heap_init();
TEST_SIZE(heap, 0);
void *data;
2023-01-24 12:07:30 +01:00
for (uint64_t i = 50; i > 0; i--) {
2023-01-22 20:34:05 +01:00
vieter_heap_insert(heap, i, (void *)i);
TEST_SIZE(heap, (uint64_t)51 - i);
2023-01-24 12:07:30 +01:00
data = 0;
2023-01-22 20:34:05 +01:00
TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok);
2023-01-24 12:07:30 +01:00
TEST_CHECK_(data == (void *)i, "%lX == %lX", (uint64_t)data, i);
}
vieter_heap_free(heap);
}
2023-01-24 19:45:01 +01:00
void test_pop() {
vieter_heap *heap = vieter_heap_init();
TEST_SIZE(heap, 0);
2023-01-24 12:07:30 +01:00
2023-01-24 19:45:01 +01:00
void *data;
2023-01-24 12:07:30 +01:00
2023-01-24 19:45:01 +01:00
for (uint64_t i = 50; i > 0; i--) {
vieter_heap_insert(heap, i, (void *)i);
TEST_SIZE(heap, (uint64_t)51 - i);
2023-01-24 12:07:30 +01:00
2023-01-24 19:45:01 +01:00
TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok);
TEST_CHECK(data == (void*)i);
}
2023-01-24 12:07:30 +01:00
2023-01-24 19:45:01 +01:00
data = NULL;
2023-01-24 12:07:30 +01:00
2023-01-24 19:45:01 +01:00
for (uint64_t i = 1; i <= 50; i++) {
TEST_CHECK(vieter_heap_pop(&data, heap) == vieter_heap_ok);
TEST_CHECK(data == (void*)i);
TEST_SIZE(heap, (uint64_t)50 - i);
}
2023-01-24 12:07:30 +01:00
2023-01-24 19:45:01 +01:00
vieter_heap_free(heap);
}
2023-01-22 20:34:05 +01:00
int uint64_t_compare(const void *a, const void *b) {
if ((*(uint64_t *)a) < (*(uint64_t *)b)) {
return -1;
} else if ((*(uint64_t *)a) < (*(uint64_t *)b)) {
return 1;
} else {
return 0;
}
}
void test_pop_random() {
srand(0);
vieter_heap *heap = vieter_heap_init();
uint64_t *numbers = malloc(500 * sizeof(uint64_t));
uint64_t num;
for (uint64_t i = 0; i < 500; i++) {
num = rand();
vieter_heap_insert(heap, num, (void *)num);
TEST_SIZE(heap, i + 1);
numbers[i] = num;
}
qsort(numbers, 500, sizeof(uint64_t), uint64_t_compare);
void *data = NULL;
for (uint64_t i = 0; i < 500; i++) {
TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok);
TEST_CHECK(data == (void *)numbers[i]);
data = NULL;
TEST_CHECK(vieter_heap_pop(&data, heap) == vieter_heap_ok);
TEST_CHECK(data == (void *)numbers[i]);
TEST_SIZE(heap, (uint64_t)500 - i - 1);
}
vieter_heap_free(heap);
free(numbers);
}
2023-01-21 16:31:22 +01:00
TEST_LIST = {
2023-01-24 12:07:30 +01:00
{"init", test_init},
{"merge same order", test_merge_same_order},
{"insert", test_insert},
2023-01-24 19:45:01 +01:00
{"pop", test_pop},
{"pop random", test_pop_random},
2023-01-21 16:31:22 +01:00
{NULL, NULL}
};