85 lines
2.1 KiB
C
85 lines
2.1 KiB
C
#include "acutest.h"
|
|
#include "vieter_heap.h"
|
|
#include "vieter_heap_tree.h"
|
|
|
|
#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);
|
|
}
|
|
|
|
void test_merge_same_order() {
|
|
vieter_heap_node *root_a = vieter_heap_node_init();
|
|
root_a->key = 1;
|
|
vieter_heap_tree *tree_a = vieter_heap_tree_init(root_a, NULL, 1);
|
|
|
|
vieter_heap_node *root_b = vieter_heap_node_init();
|
|
root_b->key = 2;
|
|
vieter_heap_tree *tree_b = vieter_heap_tree_init(root_b, NULL, 1);
|
|
|
|
vieter_heap_tree *merged = vieter_heap_tree_merge_same_order(tree_a, tree_b);
|
|
|
|
TEST_CHECK(merged == tree_a);
|
|
TEST_CHECK(merged->root->key == 1);
|
|
TEST_CHECK(merged->root->largest_order == root_b);
|
|
TEST_CHECK(merged->root->next_largest_order == NULL);
|
|
|
|
vieter_heap_tree_free(merged);
|
|
}
|
|
|
|
void test_insert() {
|
|
vieter_heap *heap = vieter_heap_init();
|
|
TEST_SIZE(heap, 0);
|
|
|
|
void *data;
|
|
|
|
for (uint64_t i = 50; i > 0; i--) {
|
|
vieter_heap_insert(heap, i, (void *)i);
|
|
TEST_SIZE(heap, (uint64_t)51 - i);
|
|
|
|
data = 0;
|
|
|
|
TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok);
|
|
TEST_CHECK_(data == (void *)i, "%lX == %lX", (uint64_t)data, i);
|
|
}
|
|
|
|
vieter_heap_free(heap);
|
|
}
|
|
|
|
void test_pop() {
|
|
vieter_heap *heap = vieter_heap_init();
|
|
TEST_SIZE(heap, 0);
|
|
|
|
void *data;
|
|
|
|
for (uint64_t i = 50; i > 0; i--) {
|
|
vieter_heap_insert(heap, i, (void *)i);
|
|
TEST_SIZE(heap, (uint64_t)50 - i);
|
|
|
|
TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok);
|
|
TEST_CHECK(data == (void*)i);
|
|
}
|
|
|
|
data = NULL;
|
|
|
|
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);
|
|
}
|
|
|
|
vieter_heap_free(heap);
|
|
}
|
|
|
|
TEST_LIST = {
|
|
{"init", test_init},
|
|
{"merge same order", test_merge_same_order},
|
|
{"insert", test_insert},
|
|
{NULL, NULL}
|
|
};
|