Add List_append_all() method using variable length lists

main
Hammy 3 years ago
parent b859c41d9f
commit 8bbe3534e6

@ -1,4 +1,5 @@
#include "list.h" #include "list.h"
#include "stdarg.h"
/* /*
* -- ERROR CODES -- * -- ERROR CODES --
@ -73,6 +74,22 @@ int List_append(List *list, int element) {
return 0; return 0;
} }
int List_append_all(List *list, int element_count, ...) {
va_list elements;
va_start(elements, element_count);
for (int i = 0; i < element_count; i++) {
int returnCode = List_append(list, va_arg(elements, int));
if (returnCode != 0) {
va_end(elements);
return returnCode;
}
}
va_end(elements);
return 0;
}
int List_insert(List *list, int index, int element) { int List_insert(List *list, int index, int element) {
if (index < 0 || index > list->_currentSize) { if (index < 0 || index > list->_currentSize) {
return ERRNO_003; return ERRNO_003;

@ -37,6 +37,19 @@ int List_get(List *list, int index);
*/ */
int List_append(List *list, int element); int List_append(List *list, int element);
/*
* Function: List_append_all(List *list, int element_count, ...)
* ----------------------------
* Append multiple elements into the given list
*
* *list: the list to append onto
* element_count: number of elements to append into the given list
* ...: variable list of integers to append into the given list
*
* returns: int (0 for success, Non-0 for error)
*/
int List_append_all(List *list, int element_count, ...);
/* /*
* Function: List_insert(List *list, int index, int element) * Function: List_insert(List *list, int index, int element)
* ---------------------------- * ----------------------------

@ -39,17 +39,7 @@ void shouldReturnListLengthTwo() {
void shouldReturnSortedListInAscendingOrder() { void shouldReturnSortedListInAscendingOrder() {
// Arrange // Arrange
List *list = List_new(); List *list = List_new();
List_append(list, 10); List_append_all(list, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
List_append(list, 9);
List_append(list, 8);
List_append(list, 7);
List_append(list, 6);
List_append(list, 5);
List_append(list, 4);
List_append(list, 3);
List_append(list, 2);
List_append(list, 1);
List_append(list, 0);
// Act // Act
int returnCode = List_sort(list, false); int returnCode = List_sort(list, false);
@ -85,16 +75,7 @@ void shouldClearList() {
void shouldReturnListMaxLengthTwenty() { void shouldReturnListMaxLengthTwenty() {
// Arrange // Arrange
List *list = List_new(); List *list = List_new();
List_append(list, 0); List_append_all(list, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List_append(list, 1);
List_append(list, 2);
List_append(list, 3);
List_append(list, 4);
List_append(list, 5);
List_append(list, 6);
List_append(list, 7);
List_append(list, 8);
List_append(list, 9);
// Mid-Assert // Mid-Assert
assert(List_max_length(list) == 10); assert(List_max_length(list) == 10);
@ -112,11 +93,7 @@ void shouldReturnListMaxLengthTwenty() {
void shouldCopyPopulatedList() { void shouldCopyPopulatedList() {
// Arrange // Arrange
List *list = List_new(); List *list = List_new();
List_append(list, 0); List_append_all(list, 5, 0, 1, 2, 3, 4);
List_append(list, 1);
List_append(list, 2);
List_append(list, 3);
List_append(list, 4);
// Act // Act
List *copiedList = List_copy(list); List *copiedList = List_copy(list);
@ -167,10 +144,7 @@ void shouldCopyEmptyList() {
void shouldRemoveElementFromList() { void shouldRemoveElementFromList() {
// Arrange // Arrange
List *list = List_new(); List *list = List_new();
List_append(list, 0); List_append_all(list, 4, 0, 1, 2, 0);
List_append(list, 1);
List_append(list, 2);
List_append(list, 0);
// Act // Act
List_remove(list, 0); List_remove(list, 0);

Loading…
Cancel
Save