diff --git a/tests/list.c b/tests/list.c new file mode 100644 index 000000000..3b08d7546 --- /dev/null +++ b/tests/list.c @@ -0,0 +1,48 @@ +#include +#include + +#include "list.h" + +static int compare(const void *a, const void *b) { + return b - a; +} + +Test(test, basics) +{ + intptr_t i; + struct list l; + + list_init(&l); + + for (i = 0; i < 100; i++) { + cr_assert_eq(list_length(&l), i); + + list_push(&l, (void *) i); + } + + cr_assert_eq(list_at(&l, 555), NULL); + cr_assert_eq(list_last(&l), (void *) 99); + cr_assert_eq(list_first(&l), NULL); + + i = 0; + list_foreach (void *j, &l) + cr_assert_eq(j, (void *) i++); + + list_sort(&l, compare); /* Reverse list */ + + i = 99; + list_foreach (void *j, &l) { + cr_assert_eq(j, (void *) i, "Is %p, expected %p", i, j); + i--; + } + + cr_assert(list_contains(&l, (void *) 55)); + + list_remove(&l, (void *) 55); + + cr_assert(!list_contains(&l, (void *) 55)); + + list_destroy(&l, NULL, false); + + cr_assert_eq(list_length(&l), -1, "List not properly destroyed: l.length = %zd", l.length); +} \ No newline at end of file