From 4ae30e3b7580af7bd1d8c3bfc4d95ed32fdec08e Mon Sep 17 00:00:00 2001 From: Joonatan O'Rourke Date: Sun, 10 Jul 2016 10:58:38 +0300 Subject: [PATCH 1/2] Fix excessive reallocing in DArray_pop --- src/lcthw/darray.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lcthw/darray.c b/src/lcthw/darray.c index d603f9b..f993167 100644 --- a/src/lcthw/darray.c +++ b/src/lcthw/darray.c @@ -109,8 +109,8 @@ void *DArray_pop(DArray * array) void *el = DArray_remove(array, array->end - 1); array->end--; - if (DArray_end(array) > (int)array->expand_rate - && DArray_end(array) % array->expand_rate) { + if (DArray_end(array) >= (int)array->expand_rate + && (DArray_end(array) % array->expand_rate) == 0) { DArray_contract(array); } From 8b89ca873a1a3cb9ead168d459aa090976e2347c Mon Sep 17 00:00:00 2001 From: Joonatan O'Rourke Date: Sun, 10 Jul 2016 11:43:34 +0300 Subject: [PATCH 2/2] Add checks for correct array max in test_push_pop --- tests/darray_tests.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/darray_tests.c b/tests/darray_tests.c index 7616da8..b995216 100644 --- a/tests/darray_tests.c +++ b/tests/darray_tests.c @@ -94,17 +94,23 @@ char *test_push_pop() int *val = DArray_new(array); *val = i * 333; DArray_push(array, val); + mu_assert(((array->max - 1) % array->expand_rate) == 0, + "Wrong max size."); } mu_assert(array->max == 1201, "Wrong max size."); for (i = 999; i >= 0; i--) { int *val = DArray_pop(array); + mu_assert(((array->max - 1) % array->expand_rate) == 0, + "Wrong max size."); mu_assert(val != NULL, "Shouldn't get a NULL."); mu_assert(*val == i * 333, "Wrong value."); DArray_free(val); } + mu_assert(array->max == 301, "Wrong max size."); + return NULL; }