305 lines
7.1 KiB
C
305 lines
7.1 KiB
C
#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);
|
|
}
|