From 4db5bfc107b05de8d7b9eadc4f17a2bda88d2590 Mon Sep 17 00:00:00 2001 From: Hammy Date: Wed, 29 Dec 2021 07:17:08 +0000 Subject: [PATCH] Add List_extend() --- src/list.c | 16 ++++++++++++++++ src/list.h | 23 +++++++++++++++++++++++ src/test/test.c | 23 +++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/list.c b/src/list.c index d28cc2f..a9a99b0 100644 --- a/src/list.c +++ b/src/list.c @@ -69,6 +69,7 @@ int List_append(List *list, int element) { } list->_array = temp; } + list->_array[++list->_currentSize] = element; return 0; @@ -251,6 +252,21 @@ List *List_slice(List *list, int start_index, int end_index) { return slicedList; } +int List_extend(List *list_to_extend, List *input_list) { + if (!list_to_extend || !input_list) { + return 1; + } + + for (int i = 0; i < input_list->_currentSize + 1; i++) { + int returnCode = List_append(list_to_extend, List_get(input_list, i)); + if (returnCode != 0) { + return returnCode; + } + } + + return 0; +} + int List_length(List *list) { return list->_currentSize + 1; } diff --git a/src/list.h b/src/list.h index 5d17d77..9ddc74e 100644 --- a/src/list.h +++ b/src/list.h @@ -198,6 +198,29 @@ int List_clear(List *list); */ List *List_slice(List *list, int start_index, int end_index); +/* + * + * Description + * ---------------------------- + * Extend the given list_to_extend with elements from given input_list + * + * Params + * ---------------------------- + * *list_to_extend the list to extend with elements from input_list + * *input_list the list containing elements to insert into list_to_extend + * + * Example + * ---------------------------- + * Given list1 -> [1, 2] AND list2 -> [3, 4, 5] + * When List_extend(list1, list2); + * Then list1 -> [1, 2, 3, 4, 5] + * + * Returns + * ---------------------------- + * int (0 for success, Non-0 for error) + */ +int List_extend(List *list_to_extend, List *input_list); + /* * * Description diff --git a/src/test/test.c b/src/test/test.c index a7b1545..214ce3b 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -179,6 +179,28 @@ void shouldReverseList() { printSuccess(__func__); } +void shouldExtendList() { + // Arrange + List *list_to_extend = List_new(); + List *input_list = List_new(); + List_append_all(list_to_extend, 2, 1, 2); + List_append_all(input_list, 3, 3, 4, 5); + + // Act + int returnCode = List_extend(list_to_extend, input_list); + + // Assert + assert(returnCode == 0); + assert(List_length(list_to_extend) == 5); + for (int i = 0; i < 5; i++) { + assert(List_get(list_to_extend, i) == i + 1); + } + List_destroy(&list_to_extend); + List_destroy(&input_list); + + printSuccess(__func__); +} + int main() { printf("============================================"); printf("\nSTART TESTING"); @@ -193,6 +215,7 @@ int main() { shouldInsertElementIntoListIndexZero(); shouldRemoveElementFromList(); shouldReverseList(); + shouldExtendList(); printf("\n\n============================================"); printf("\nFINISH TESTING"); printf("\n============================================");