Commit 508d6336 authored by Chris Müller's avatar Chris Müller

test: add test code for usual lists.

parent 867d49d0
......@@ -4,6 +4,7 @@ set(SOURCES
main.c
array.c
map.c
list.c
structures.c
)
......
#include "unittest.h"
#include "structures/list.h"
#include <assert.h>
static void
test_list_prepend_and_append(const_pointer data)
{
int items[] = { 1, 2, 3, 4, 5 };
struct CryList* list = cry_list_new();
assert(cry_list_size(list) == 0);
assert(cry_list_head(list) == 0);
assert(cry_list_tail(list) == 0);
// fill linked-lists with items
cry_list_prepend(list, items);
assert(cry_list_size(list) == 1);
assert(cry_list_head(list) == items);
assert(cry_list_tail(list) == items);
cry_list_prepend(list, items + 1);
assert(cry_list_size(list) == 2);
assert(cry_list_head(list) == items + 1);
assert(cry_list_tail(list) == items);
cry_list_append(list, items + 2);
assert(cry_list_size(list) == 3);
assert(cry_list_head(list) == items + 1);
assert(cry_list_tail(list) == items + 2);
cry_list_append(list, items + 3);
assert(cry_list_size(list) == 4);
assert(cry_list_head(list) == items + 1);
assert(cry_list_tail(list) == items + 3);
cry_list_free(list, 0);
}
static void
test_list_iterator(data)
{
int items[] = { 1, 2, 3, 4, 5 };
struct CryList* list = cry_list_new();
cry_list_append(list, items);
cry_list_append(list, items + 1);
cry_list_append(list, items + 2);
cry_list_append(list, items + 3);
cry_list_append(list, items + 4);
struct CryIndex* pos = cry_list_begin(list);
assert(cry_list_prev(pos) == 0);
assert(cry_list_data(pos) == items);
pos = cry_list_next(pos);
assert(cry_list_data(pos) == items + 1);
pos = cry_list_next(pos);
assert(cry_list_data(pos) == items + 2);
pos = cry_list_next(pos);
assert(cry_list_data(pos) == items + 3);
pos = cry_list_next(pos);
assert(cry_list_data(pos) == items + 4);
assert(pos == cry_list_end(list));
assert(cry_list_next(pos) == 0);
cry_list_free(list, 0);
}
static void
test_list_operation(data)
{
int items[] = { 1, 2, 3, 4, 5 };
struct CryList* list = cry_list_new();
cry_list_append(list, items);
cry_list_append(list, items + 1);
cry_list_append(list, items + 2);
cry_list_append(list, items + 3);
cry_list_append(list, items + 4);
assert(cry_list_size(list) == 5);
struct CryIndex* middle = cry_list_index(list, 2);
assert(cry_list_remove(list, middle) == items + 2);
assert(cry_list_size(list) == 4);
struct CryIndex* begin = cry_list_begin(list);
assert(cry_list_data(begin) == items);
assert(cry_list_data(cry_list_step(begin, 1)) == items + 1);
assert(cry_list_data(cry_list_step(begin, 2)) == items + 3);
assert(cry_list_data(cry_list_step(begin, 3)) == items + 4);
assert(cry_list_remove(list, cry_list_begin(list)) == items);
assert(cry_list_remove(list, cry_list_end(list)) == items + 4);
assert(cry_list_size(list) == 2);
cry_list_free(list, 0);
}
void
cry_test_lists(void)
{
cry_unittest_run("structures.list.insertions", test_list_prepend_and_append, 0, 10);
cry_unittest_run("structures.list.iterator", test_list_iterator, 0, 10);
cry_unittest_run("structures.list.operations", test_list_operation, 0, 10);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment