refactor(heap): some better variable names; some more tests
This commit is contained in:
parent
dc557f57ab
commit
3ec2e76af9
4 changed files with 83 additions and 54 deletions
|
|
@ -1,6 +1,5 @@
|
|||
#include "acutest.h"
|
||||
#include "vieter_heap.h"
|
||||
#include "vieter_heap_tree.h"
|
||||
#include "vieter_heap_internal.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define TEST_SIZE(heap, size) \
|
||||
|
|
@ -14,6 +13,29 @@ void test_init() {
|
|||
vieter_heap_free(heap);
|
||||
}
|
||||
|
||||
void count_nodes(uint64_t *counter, vieter_heap_node *root) {
|
||||
(*counter)++;
|
||||
|
||||
if (root->largest_order != NULL) {
|
||||
count_nodes(counter, root->largest_order);
|
||||
}
|
||||
|
||||
// This will also traverse the various trees
|
||||
if (root->ptr.next_largest_order != NULL) {
|
||||
count_nodes(counter, root->ptr.next_largest_order);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t count_nodes_heap(vieter_heap *heap) {
|
||||
uint64_t counter = 0;
|
||||
|
||||
if (heap->tree != NULL) {
|
||||
count_nodes(&counter, heap->tree);
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
void test_insert() {
|
||||
vieter_heap *heap = vieter_heap_init();
|
||||
TEST_SIZE(heap, 0);
|
||||
|
|
@ -23,6 +45,7 @@ void test_insert() {
|
|||
for (uint64_t i = 50; i > 0; i--) {
|
||||
vieter_heap_insert(heap, i, (void *)i);
|
||||
TEST_SIZE(heap, (uint64_t)51 - i);
|
||||
TEST_CHECK(count_nodes_heap(heap) == (uint64_t)51 - i);
|
||||
|
||||
data = 0;
|
||||
|
||||
|
|
@ -47,6 +70,7 @@ void test_insert_random() {
|
|||
for (uint64_t i = 0; i < 5000; i++) {
|
||||
vieter_heap_insert(heap, num, (void *)num);
|
||||
TEST_SIZE(heap, i + 1);
|
||||
TEST_CHECK(count_nodes_heap(heap) == (uint64_t)i + 1);
|
||||
|
||||
if (num < smallest) {
|
||||
smallest = num;
|
||||
|
|
@ -72,6 +96,7 @@ void test_pop() {
|
|||
for (uint64_t i = 50; i > 0; i--) {
|
||||
vieter_heap_insert(heap, i, (void *)i);
|
||||
TEST_SIZE(heap, (uint64_t)51 - i);
|
||||
TEST_CHECK(count_nodes_heap(heap) == (uint64_t)51 - i);
|
||||
|
||||
TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok);
|
||||
TEST_CHECK(data == (void*)i);
|
||||
|
|
@ -112,6 +137,7 @@ void test_pop_random() {
|
|||
num = rand();
|
||||
vieter_heap_insert(heap, num, (void *)num);
|
||||
TEST_SIZE(heap, i + 1);
|
||||
TEST_CHECK(count_nodes_heap(heap) == i + 1);
|
||||
|
||||
numbers[i] = num;
|
||||
}
|
||||
|
|
@ -130,6 +156,7 @@ void test_pop_random() {
|
|||
TEST_CHECK(vieter_heap_pop(&data, heap) == vieter_heap_ok);
|
||||
TEST_CHECK_(data == (void *)numbers[i], "pop %lx == %lx", (uint64_t)data, numbers[i]);
|
||||
TEST_SIZE(heap, n - i - 1);
|
||||
TEST_CHECK(count_nodes_heap(heap) == n - i - 1);
|
||||
}
|
||||
|
||||
vieter_heap_free(heap);
|
||||
|
|
|
|||
Reference in a new issue