fix(heap): finally fixed that pop bug
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/test Pipeline was successful Details
ci/woodpecker/pr/test-mem Pipeline was successful Details

pull/4/head
Jef Roosens 2023-01-26 10:21:30 +01:00
parent 3ec2e76af9
commit 05b96d1fd6
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 23 additions and 16 deletions

View File

@ -61,17 +61,18 @@ vieter_heap_node *vieter_heap_tree_merge_same_order(vieter_heap_node *root_a,
return root; return root;
} }
vieter_heap_node *vieter_heap_tree_merge(vieter_heap_node *root_a, vieter_heap_node *vieter_heap_tree_merge(vieter_heap_node *target_tree,
vieter_heap_node *root_b) { vieter_heap_node *other_tree) {
vieter_heap_node *other_tree, *target_tree, *out; /* vieter_heap_node *other_tree, *target_tree, *out; */
vieter_heap_node *out;
if (root_a->order <= root_b->order) { /* if (root_a->order <= root_b->order) { */
target_tree = root_a; /* target_tree = root_a; */
other_tree = root_b; /* other_tree = root_b; */
} else { /* } else { */
target_tree = root_b; /* target_tree = root_b; */
other_tree = root_a; /* other_tree = root_a; */
} /* } */
vieter_heap_node *next_other_tree, *next_target_tree; vieter_heap_node *next_other_tree, *next_target_tree;
vieter_heap_node *previous_target_tree = NULL; vieter_heap_node *previous_target_tree = NULL;
@ -111,6 +112,10 @@ vieter_heap_node *vieter_heap_tree_merge(vieter_heap_node *root_a,
out = other_tree; out = other_tree;
} else { } else {
previous_target_tree->ptr.next_tree = other_tree; previous_target_tree->ptr.next_tree = other_tree;
// This single missing line right here broke this entire function for
// nearly a week.
previous_target_tree = other_tree;
} }
other_tree->ptr.next_tree = target_tree; other_tree->ptr.next_tree = target_tree;

View File

@ -88,15 +88,17 @@ void test_insert_random() {
} }
void test_pop() { void test_pop() {
const uint64_t n = 500;
vieter_heap *heap = vieter_heap_init(); vieter_heap *heap = vieter_heap_init();
TEST_SIZE(heap, 0); TEST_SIZE(heap, 0);
void *data; void *data;
for (uint64_t i = 50; i > 0; i--) { for (uint64_t i = n; i > 0; i--) {
vieter_heap_insert(heap, i, (void *)i); vieter_heap_insert(heap, i, (void *)i);
TEST_SIZE(heap, (uint64_t)51 - i); TEST_SIZE(heap, (uint64_t)n + 1 - i);
TEST_CHECK(count_nodes_heap(heap) == (uint64_t)51 - i); TEST_CHECK(count_nodes_heap(heap) == (uint64_t)n + 1 - i);
TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok); TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok);
TEST_CHECK(data == (void*)i); TEST_CHECK(data == (void*)i);
@ -104,10 +106,10 @@ void test_pop() {
data = NULL; data = NULL;
for (uint64_t i = 1; i <= 50; i++) { for (uint64_t i = 1; i <= n; i++) {
TEST_CHECK(vieter_heap_pop(&data, heap) == vieter_heap_ok); TEST_CHECK(vieter_heap_pop(&data, heap) == vieter_heap_ok);
TEST_CHECK(data == (void*)i); TEST_CHECK(data == (void*)i);
TEST_SIZE(heap, (uint64_t)50 - i); TEST_SIZE(heap, (uint64_t)n - i);
} }
vieter_heap_free(heap); vieter_heap_free(heap);
@ -124,7 +126,7 @@ int uint64_t_compare(const void *a, const void *b) {
} }
void test_pop_random() { void test_pop_random() {
const uint64_t n = 29; const uint64_t n = 500;
srand(0); srand(0);