From e0115dd7d96011ae26641d12b5c9c5f4a7d2864f Mon Sep 17 00:00:00 2001 From: ChepakiLeCookie <101044938+ChepakiLeCookie@users.noreply.github.com> Date: Fri, 13 Oct 2023 11:04:38 +0200 Subject: [PATCH] fix: error in array_list_remove (always removed the last index and not the index given) (#6) Co-authored-by: Maxime Rumpler --- lib/array_list.c | 4 ++-- test/array_list_test.c | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/array_list.c b/lib/array_list.c index 5733c64..a5b03fb 100644 --- a/lib/array_list.c +++ b/lib/array_list.c @@ -25,8 +25,8 @@ void array_list_remove(struct array_list* list, size_t index) { if (index >= list->size) { return; } - for (size_t i = index + 1; i < list->size - 1; i++) { - list->data[i - 1] = list->data[i]; + for (size_t i = index; i < list->size - 1; i++) { + list->data[i] = list->data[i + 1]; } list->size--; } diff --git a/test/array_list_test.c b/test/array_list_test.c index 9c1b4a6..33b0a4b 100644 --- a/test/array_list_test.c +++ b/test/array_list_test.c @@ -34,4 +34,15 @@ void array_list_test() { assert(array_list_get(list, 100) == (void *)95); array_list_free(list); + + list = array_list_initialization(); + array_list_add(list, (void *)'a'); + array_list_add(list, (void *)'b'); + array_list_add(list, (void *)'c'); + array_list_remove(list, 1); + assert(array_list_get(list, 0) == (void *)'a'); + assert(array_list_get(list, 1) == (void *)'c'); + assert(list->size == 2); + + array_list_free(list); }