waitui/tests/library/list/test_list.c
Rick Barenthin 46d2e75d2f feat(list): add list implementation
*  a list implementation with a few test
2022-02-06 22:45:03 +01:00

84 lines
2.4 KiB
C

/**
* @file test_list.c
* @author rick
* @date 09.07.21
* @brief Test for the List implementation
*/
#include "waitui/list.h"
#include "../../bdd-for-c.h"
typedef struct value {
int count;
int i;
} value;
static value *value_new(int i) {
value *this = calloc(1, sizeof(*this));
if (!this) { return NULL; }
this->i = i;
return this;
}
static void value_destroy(value **this) {
if (!this || !(*this)) { return; }
free(*this);
*this = NULL;
}
CREATE_LIST_TYPE(INTERFACE, value)
CREATE_LIST_TYPE(IMPLEMENTATION, value)
spec("list") {
context("use as a stack") {
static value_list *stack = NULL;
before() { stack = value_list_new(); }
after() { value_list_destroy(&stack); }
it("should have a stack") {
check(stack != NULL, "stack should not be NULL");
}
it("should allow to push a value onto the stack") {
value *value1 = value_new(1);
check(value1 != NULL, "value1 should not be NULL");
check(value_list_push(stack, value1) == true,
"value1 should be added to the stack");
}
it("should allow to push a second value onto the stack") {
value *value2 = value_new(2);
check(value2 != NULL, "value2 should not be NULL");
check(value_list_push(stack, value2) == true,
"value1 should be added to the stack");
}
it("should return the last added element on pop") {
value *test = value_list_pop(stack);
check(test != NULL, "value_list_pop should not return NULL");
check(test->i == 2, "test should be 2");
value_destroy(&test);
}
it("should be able to peek the top element") {
value *test = value_list_peek(stack);
check(test != NULL, "value_list_peek should not return NULL");
check(test->i == 1, "test should be 1");
}
it("should return the first added element on pop") {
value *test = value_list_pop(stack);
check(test != NULL, "value_list_pop should not return NULL");
check(test->i == 1, "test should be 1");
value_destroy(&test);
}
it("should return NULL if peek with an empty stack") {
value *test = value_list_peek(stack);
check(test == NULL, "value_list_peek should return NULL");
}
}
}