diff --git a/src/cmc/heap.h b/src/cmc/heap.h index 685d9c0..2c69594 100644 --- a/src/cmc/heap.h +++ b/src/cmc/heap.h @@ -351,7 +351,7 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> " \ _heap_->buffer[_heap_->count++] = value; \ \ - if (!PFX##_empty(_heap_)) \ + if (PFX##_count(_heap_) > 1) \ { \ PFX##_impl_float_up(_heap_, _heap_->count - 1); \ } \ @@ -739,6 +739,10 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> " /* Go to parent */ \ C = (C - 1) / 2; \ \ + /* Prevent the two lines below from accessing a negative index */ \ + if (C == 0) \ + break; \ + \ child = _heap_->buffer[C]; \ parent = _heap_->buffer[(C - 1) / 2]; \ } \ diff --git a/src/cmc/sortedlist.h b/src/cmc/sortedlist.h index a62923a..640464c 100644 --- a/src/cmc/sortedlist.h +++ b/src/cmc/sortedlist.h @@ -805,7 +805,7 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> " L = M + 1; \ } \ \ - if (_list_->f_val->cmp(_list_->buffer[L - 1], value) == 0) \ + if (L > 0 && _list_->f_val->cmp(_list_->buffer[L - 1], value) == 0) \ return L - 1; \ \ /* Not found */ \ diff --git a/tests/main/src/heap.c b/tests/main/src/heap.c index 363341a..1071f0d 100644 --- a/tests/main/src/heap.c +++ b/tests/main/src/heap.c @@ -175,7 +175,7 @@ _Bool h_insert(struct heap *_heap_, size_t value) return 0; } _heap_->buffer[_heap_->count++] = value; - if (!h_empty(_heap_)) + if (h_count(_heap_) > 1) { h_impl_float_up(_heap_, _heap_->count - 1); } @@ -460,6 +460,8 @@ static void h_impl_float_up(struct heap *_heap_, size_t index) _heap_->buffer[C] = _heap_->buffer[(C - 1) / 2]; _heap_->buffer[(C - 1) / 2] = tmp; C = (C - 1) / 2; + if (C == 0) + break; child = _heap_->buffer[C]; parent = _heap_->buffer[(C - 1) / 2]; } diff --git a/tests/main/src/sortedlist.c b/tests/main/src/sortedlist.c index 1719223..73344bd 100644 --- a/tests/main/src/sortedlist.c +++ b/tests/main/src/sortedlist.c @@ -529,7 +529,7 @@ static size_t sl_impl_binary_search_last(struct sortedlist *_list_, else L = M + 1; } - if (_list_->f_val->cmp(_list_->buffer[L - 1], value) == 0) + if (L > 0 && _list_->f_val->cmp(_list_->buffer[L - 1], value) == 0) return L - 1; return _list_->count; } diff --git a/tests/main/unt/intervalheap.c b/tests/main/unt/intervalheap.c index ec601dd..639e6a3 100644 --- a/tests/main/unt/intervalheap.c +++ b/tests/main/unt/intervalheap.c @@ -242,6 +242,8 @@ CMC_CREATE_UNIT(IntervalHeap, true, { cmc_assert_equals(size_t, 10000, ih_count(ih)); cmc_assert_equals(int32_t, 10000, total_create); + ih_free(ih); + // max ih = ih_new(1, ih_fval); diff --git a/tests/main/unt/queue.c b/tests/main/unt/queue.c index 5b83ba9..5fb9054 100644 --- a/tests/main/unt/queue.c +++ b/tests/main/unt/queue.c @@ -339,82 +339,82 @@ CMC_CREATE_UNIT(Queue, true, { }); CMC_CREATE_TEST(copy_of, { - struct queue *d1 = q_new(100, q_fval); + struct queue *q1 = q_new(100, q_fval); - cmc_assert_not_equals(ptr, NULL, d1); + cmc_assert_not_equals(ptr, NULL, q1); for (size_t i = 1; i <= 150; i++) - cmc_assert(q_enqueue(d1, i)); + cmc_assert(q_enqueue(q1, i)); - struct queue *d2 = q_copy_of(d1); + struct queue *q2 = q_copy_of(q1); - cmc_assert_not_equals(ptr, NULL, d2); - cmc_assert_equals(size_t, q_count(d1), q_count(d2)); + cmc_assert_not_equals(ptr, NULL, q2); + cmc_assert_equals(size_t, q_count(q1), q_count(q2)); - for (size_t i1 = d1->front, i2 = d2->front, j = 0; j < d1->count; j++) + for (size_t i1 = q1->front, i2 = q2->front, j = 0; j < q1->count; j++) { - cmc_assert_equals(size_t, d1->buffer[i1], d2->buffer[i2]); + cmc_assert_equals(size_t, q1->buffer[i1], q2->buffer[i2]); - i1 = (i1 + 1) % d1->capacity; - i2 = (i2 + 1) % d2->capacity; + i1 = (i1 + 1) % q1->capacity; + i2 = (i2 + 1) % q2->capacity; } - q_free(d1); - q_free(d2); + q_free(q1); + q_free(q2); }); CMC_CREATE_TEST(equals, { - struct queue *d1 = q_new(100, q_fval); - struct queue *d2 = q_new(100, q_fval); + struct queue *q1 = q_new(100, q_fval); + struct queue *q2 = q_new(100, q_fval); - cmc_assert_not_equals(ptr, NULL, d1); - cmc_assert_not_equals(ptr, NULL, d2); + cmc_assert_not_equals(ptr, NULL, q1); + cmc_assert_not_equals(ptr, NULL, q2); for (size_t i = 1; i <= 150; i++) { - cmc_assert(q_enqueue(d1, i)); - cmc_assert(q_enqueue(d2, i)); + cmc_assert(q_enqueue(q1, i)); + cmc_assert(q_enqueue(q2, i)); } - cmc_assert(q_equals(d1, d2)); + cmc_assert(q_equals(q1, q2)); - q_free(d1); - q_free(d2); + q_free(q1); + q_free(q2); }); CMC_CREATE_TEST(equals[from copy], { - struct queue *d1 = q_new(100, q_fval); + struct queue *q1 = q_new(100, q_fval); - cmc_assert_not_equals(ptr, NULL, d1); + cmc_assert_not_equals(ptr, NULL, q1); for (size_t i = 1; i <= 150; i++) { - cmc_assert(q_enqueue(d1, i)); + cmc_assert(q_enqueue(q1, i)); } - struct queue *d2 = q_copy_of(d1); + struct queue *q2 = q_copy_of(q1); - cmc_assert_not_equals(ptr, NULL, d2); + cmc_assert_not_equals(ptr, NULL, q2); - cmc_assert(q_equals(d1, d2)); + cmc_assert(q_equals(q1, q2)); - q_free(d1); - q_free(d2); + q_free(q1); + q_free(q2); }); CMC_CREATE_TEST(equals[count = 0], { - struct queue *d1 = q_new(100, q_fval); + struct queue *q1 = q_new(100, q_fval); - cmc_assert_not_equals(ptr, NULL, d1); + cmc_assert_not_equals(ptr, NULL, q1); - struct queue *d2 = q_copy_of(d1); + struct queue *q2 = q_copy_of(q1); - cmc_assert_not_equals(ptr, NULL, d2); + cmc_assert_not_equals(ptr, NULL, q2); - cmc_assert(q_equals(d1, d2)); + cmc_assert(q_equals(q1, q2)); - q_free(d1); - q_free(d2); + q_free(q1); + q_free(q2); }); CMC_CREATE_TEST(flag, { @@ -474,6 +474,7 @@ CMC_CREATE_UNIT(Queue, true, { cmc_assert_equals(int32_t, cmc_flags.OK, q_flag(q2)); q_free(q); + q_free(q2); }); CMC_CREATE_TEST(callbacks, { diff --git a/tests/main/unt/stack.c b/tests/main/unt/stack.c index c2e461e..e09ca7f 100644 --- a/tests/main/unt/stack.c +++ b/tests/main/unt/stack.c @@ -667,6 +667,7 @@ CMC_CREATE_UNIT(Stack, true, { cmc_assert_equals(int32_t, cmc_flags.OK, s_flag(s2)); s_free(s); + s_free(s2); }); CMC_CREATE_TEST(callbacks, {