Well after resting my brain. I had another go and tackled each of the three points you raised. Locally I created the call_delete_list() function and called it on any of the lists created in the local tests I wrote to mimic the Unity tests. I added gcc option -Wextra in addition to -O -Wall -pedantic -ansi -std=c99 -o. After some work I again got all my local tests passed.
In the editor however, I have error segmentation fault. So clearly I have not implemented your suggestions properly, or have some other error in my code.
If you could help further that would be great. Latest code is below:
#include "list_ops.h"
list_t *new_list(size_t length, list_element_t elements[])
{
list_t *list = malloc(sizeof(list_t) + sizeof(list_element_t) * length);
list->length = length;
size_t i;
for (i = 0; i < length; ++i){
list->elements[i] = elements[i];
}
return list;
}
size_t length_list(list_t *list)
{
return list->length;
}
list_t *append_list(list_t *list1, list_t *list2)
{
size_t n1 = list1->length, n2 = list2->length, i, j;
if (n1 == 0 && n2 > 0) {
list_t *out_list;
list_element_t *elements2 = malloc(sizeof(list_element_t) * n2);
for (i = 0; i < n2; ++i)
elements2[i] = list2->elements[i];
out_list = new_list(n2, elements2);
return out_list;
} else if (n1 > 0 && n2 == 0) {
list_t *out_list;
list_element_t *elements2 = malloc(sizeof(list_element_t) * n1);
for (i = 0; i < n1; ++i)
elements2[i] = list1->elements[i];
out_list = new_list(n1, elements2);
return out_list;
} else if (n1 == 0 && n2 == 0) {
return new_list(0, NULL);
} else {
list1 = realloc(list1, sizeof(list_t) + sizeof(list_element_t) * (n1 + n2));
assert(list1 != NULL);
list_element_t *elements2 = malloc(sizeof(list_element_t) * (n1 + n2));
for (i = 0; i < n1; ++i)
elements2[i] = list1->elements[i];
for (i = n1, j = 0; i < n1 + n2; ++i, ++j)
elements2[i] = list2->elements[j];
list_t *out_list;
out_list = new_list(n1 + n2, elements2);
return out_list;
}
}
list_t *filter_list(list_t *list, bool (*filter)(list_element_t))
{
if (list->length == 0) {return new_list(0, NULL);}
else {
size_t n = list->length, i, j;
list_element_t elements2[n];
for (i = 0; i < n; ++i)
elements2[i] = 0;
list_t *list2 = new_list(n, elements2);
for (i = 0, j = 0; i < n; ++i) {
if (filter(list->elements[i])) {
list2->elements[j] = list->elements[i];
j++;
}
}
list2->length = j;
return list2;
}
}
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t))
{
if (!list->length) {return new_list(0, NULL);}
else {
size_t n = list->length, i;
list_element_t elements2[n];
for (i = 0; i < n; ++i)
elements2[i] = 0;
list_t *list2 = new_list(n, elements2);
for (i = 0; i < n; ++i) {
list2->elements[i] = map(list->elements[i]);
}
list2->length = n;
return list2;
}
}
list_element_t foldl_list(list_t *list, list_element_t initial,
list_element_t (*foldl)(list_element_t, list_element_t))
{
if (!list->length) {return initial;}
else {
size_t n = list->length, i;
list_element_t accum = initial;
for (i = 0; i < n; ++i)
accum = foldl(list->elements[i], accum);
return accum;
}
}
list_element_t foldr_list(list_t *list, list_element_t initial,
list_element_t (*foldr)(list_element_t, list_element_t))
{
if (!list->length) {return initial;}
else {
int n = list->length, i;
list_element_t accum = initial;
for (i = n - 1; i >= 0; --i) {
accum = foldr(list->elements[i], accum);
}
return accum;
}
}
list_t *reverse_list(list_t *list)
{
if (!list->length) {
return new_list(0, NULL);
} else {
list_t *list2;
size_t n = list->length;
int i, j, _n;
_n = n;
list_element_t *elements2 = malloc(sizeof(list_element_t) * n);
for (i = 0; i < _n; ++i)
elements2[i] = 0;
for (i = 0, j = _n - 1; j >= 0; --j, ++i)
elements2[i] = list->elements[j];
list2 = new_list(n, elements2);
return list2;
}
}
void delete_list(list_t *list)
{
free(list);
}