feat(utils): lists tests
This commit is contained in:
parent
e65c55f5c9
commit
f0b39535fb
5 changed files with 310 additions and 5 deletions
|
|
@ -121,7 +121,7 @@ int args_handler(int argc, char **argv, struct args_options *options,
|
|||
}
|
||||
|
||||
args_in_var(vars, args_list);
|
||||
list_destroy(args_list);
|
||||
list_destroy(&args_list);
|
||||
|
||||
if (options->type == INPUT_UNDEFINED)
|
||||
options->type = INPUT_STDIN;
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ void list_print(struct list *list);
|
|||
** Release the memory used by the list.
|
||||
** Does nothing if `list` is `NULL`.
|
||||
*/
|
||||
void list_destroy(struct list *list);
|
||||
void list_destroy(struct list **list);
|
||||
|
||||
/*
|
||||
** Release the memory used by the list and its content
|
||||
|
|
|
|||
|
|
@ -49,9 +49,9 @@ void list_print(struct list *list)
|
|||
}
|
||||
}
|
||||
|
||||
void list_destroy(struct list *list)
|
||||
void list_destroy(struct list **list)
|
||||
{
|
||||
struct list *elt = list;
|
||||
struct list *elt = *list;
|
||||
struct list *next_elt;
|
||||
while (elt != NULL)
|
||||
{
|
||||
|
|
@ -59,6 +59,7 @@ void list_destroy(struct list *list)
|
|||
free(elt);
|
||||
elt = next_elt;
|
||||
}
|
||||
*list = NULL;
|
||||
}
|
||||
|
||||
struct list *list_append(struct list *list, void *value)
|
||||
|
|
|
|||
305
tests/unit/utils/lists.c
Normal file
305
tests/unit/utils/lists.c
Normal file
|
|
@ -0,0 +1,305 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "../../../src/utils/lists/lists.h"
|
||||
|
||||
#include <criterion/criterion.h>
|
||||
#include <criterion/new/assert.h>
|
||||
#include <criterion/redirect.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
TestSuite(lists);
|
||||
|
||||
Test(lists, append_empty)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, (void *)1);
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)1);
|
||||
cr_expect(lst->next == NULL);
|
||||
cr_expect(list_length(lst) == 1);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, prepend_empty)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_prepend(lst, (void *)1);
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)1);
|
||||
cr_expect(lst->next == NULL);
|
||||
cr_expect(list_length(lst) == 1);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, insert_empty)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_insert(lst, (void *)1, 0);
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)1);
|
||||
cr_expect(lst->next == NULL);
|
||||
cr_expect(list_length(lst) == 1);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, insert_out_of_bounds)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_insert(lst, (void *)1, 5);
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)1);
|
||||
cr_expect(lst->next == NULL);
|
||||
cr_expect(list_length(lst) == 1);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, remove_out_of_bounds)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, (void *)1);
|
||||
lst = list_remove(lst, 5);
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)1);
|
||||
cr_expect(lst->next == NULL);
|
||||
cr_expect(list_length(lst) == 1);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, append_multiple)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, (void *)1);
|
||||
lst = list_append(lst, (void *)2);
|
||||
lst = list_append(lst, (void *)3);
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)1);
|
||||
cr_expect(lst->next->data == (void *)2);
|
||||
cr_expect(lst->next->next->data == (void *)3);
|
||||
cr_expect(lst->next->next->next == NULL);
|
||||
cr_expect(list_length(lst) == 3);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, prepend_multiple)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_prepend(lst, (void *)1);
|
||||
lst = list_prepend(lst, (void *)2);
|
||||
lst = list_prepend(lst, (void *)3);
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)3);
|
||||
cr_expect(lst->next->data == (void *)2);
|
||||
cr_expect(lst->next->next->data == (void *)1);
|
||||
cr_expect(lst->next->next->next == NULL);
|
||||
cr_expect(list_length(lst) == 3);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, insert_multiple)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_insert(lst, (void *)1, 0);
|
||||
lst = list_insert(lst, (void *)3, 1);
|
||||
lst = list_insert(lst, (void *)2, 1);
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)1);
|
||||
cr_expect(lst->next->data == (void *)2);
|
||||
cr_expect(lst->next->next->data == (void *)3);
|
||||
cr_expect(lst->next->next->next == NULL);
|
||||
cr_expect(list_length(lst) == 3);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, append)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_prepend(lst, (void *)2);
|
||||
lst = list_prepend(lst, (void *)1);
|
||||
|
||||
lst = list_append(lst, (void *)3);
|
||||
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)1);
|
||||
cr_expect(lst->next->data == (void *)2);
|
||||
cr_expect(lst->next->next->data == (void *)3);
|
||||
cr_expect(lst->next->next->next == NULL);
|
||||
cr_expect(list_length(lst) == 3);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, prepend)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, (void *)1);
|
||||
lst = list_append(lst, (void *)2);
|
||||
|
||||
lst = list_prepend(lst, (void *)0);
|
||||
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)0);
|
||||
cr_expect(lst->next->data == (void *)1);
|
||||
cr_expect(lst->next->next->data == (void *)2);
|
||||
cr_expect(lst->next->next->next == NULL);
|
||||
cr_expect(list_length(lst) == 3);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, insert)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, (void *)1);
|
||||
lst = list_append(lst, (void *)3);
|
||||
|
||||
lst = list_insert(lst, (void *)2, 1);
|
||||
lst = list_insert(lst, (void *)0, 0);
|
||||
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)0);
|
||||
cr_expect(lst->next->data == (void *)1);
|
||||
cr_expect(lst->next->next->data == (void *)2);
|
||||
cr_expect(lst->next->next->next->data == (void *)3);
|
||||
cr_expect(lst->next->next->next->next == NULL);
|
||||
cr_expect(list_length(lst) == 4);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, remove)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, (void *)1);
|
||||
lst = list_append(lst, (void *)2);
|
||||
lst = list_append(lst, (void *)3);
|
||||
lst = list_append(lst, (void *)4);
|
||||
|
||||
lst = list_remove(lst, 1); // remove 2
|
||||
lst = list_remove(lst, 2); // remove 4
|
||||
lst = list_remove(lst, 0); // remove 1
|
||||
|
||||
cr_expect(lst != NULL);
|
||||
cr_expect(lst->data == (void *)3);
|
||||
cr_expect(lst->next == NULL);
|
||||
cr_expect(list_length(lst) == 1);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, destroy_null)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, deep_destroy_null)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, deep_destroy)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, strdup("string1"));
|
||||
lst = list_append(lst, strdup("string2"));
|
||||
lst = list_append(lst, strdup("string3"));
|
||||
|
||||
list_deep_destroy(lst);
|
||||
}
|
||||
|
||||
Test(lists, length_empty)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
cr_expect(list_length(lst) == 0);
|
||||
}
|
||||
|
||||
Test(lists, print_empty, .init = cr_redirect_stdout)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
list_print(lst);
|
||||
cr_expect_stdout_eq_str("");
|
||||
}
|
||||
|
||||
Test(lists, print_non_empty, .init = cr_redirect_stdout)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, (void *)1);
|
||||
lst = list_append(lst, (void *)2);
|
||||
lst = list_append(lst, (void *)3);
|
||||
|
||||
list_print(lst);
|
||||
fflush(stdout);
|
||||
cr_expect_stdout_eq_str("0x1 0x2 0x3\n");
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
Test(lists, find_empty)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
cr_expect(list_find(lst, (void *)1) == -1);
|
||||
}
|
||||
|
||||
Test(lists, find_non_empty)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
lst = list_append(lst, (void *)1);
|
||||
lst = list_append(lst, (void *)2);
|
||||
lst = list_append(lst, (void *)3);
|
||||
|
||||
cr_expect(list_find(lst, (void *)1) == 0);
|
||||
cr_expect(list_find(lst, (void *)2) == 1);
|
||||
cr_expect(list_find(lst, (void *)3) == 2);
|
||||
cr_expect(list_find(lst, (void *)4) == -1); // not found
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
||||
static void fold_func(void *acc, void *data)
|
||||
{
|
||||
*(int *)acc += *(int *)data;
|
||||
}
|
||||
|
||||
Test(lists, fold)
|
||||
{
|
||||
struct list *lst = NULL;
|
||||
int v1 = 10, v2 = 20, v3 = 30;
|
||||
lst = list_append(lst, &v1);
|
||||
lst = list_append(lst, &v2);
|
||||
lst = list_append(lst, &v3);
|
||||
|
||||
int sum = 0;
|
||||
list_fold(lst, &sum, fold_func);
|
||||
cr_expect(sum == 60);
|
||||
|
||||
list_destroy(&lst);
|
||||
cr_expect(lst == NULL);
|
||||
}
|
||||
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
#include <criterion/criterion.h>
|
||||
#include <criterion/new/assert.h>
|
||||
#include <criterion/redirect.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue