/** * @file test_list.c * @author rick * @date 09.07.21 * @brief Test for the List implementation */ #include "waitui/list_generic.h" #include "waitui/list_generic_impl.h" #include "../../bdd-for-c-ext.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 normal list") { static value_list *list = NULL; before_each() { list = value_list_new(); check(list != NULL, "list should not be NULL"); } after_each() { value_list_destroy(&list); check(list == NULL, "list should be NULL"); } describe("value_list_push()") { for_it( "should add values to the end of the list and it should be there", params_format(2, "start: %d", "elements: %d"), format_args(test_param.start, test_param.count), { int start; int count; }, test_params( {0, 0}, {0, 1}, {1, 1}, {4, 20}, ) ) { for (int i = test_param.start; i < test_param.start + test_param.count; ++i) { check(value_list_push(list, value_new(i)) == 1); value *value = value_list_peek(list); check(value != NULL, "value should not be NULL"); check(value->i == i, "value should be %d but is %d", i, value->i); } for (int i = test_param.start + test_param.count - 1; i >= test_param.start; --i) { value *value = value_list_pop(list); check(value != NULL, "value should not be NULL"); check(value->i == i, "value should be %d but is %d", i, value->i); value_destroy(&value); } } for_it_end(); it("should return 0 with NULL as this param") { check(value_list_push(NULL, value_new(1)) == 0, "return should not be 0"); } it("should return 0 with NULL as valueElement param") { check(value_list_push(list, NULL) == 0, "return should not be 0"); } } describe("value_list_pop()") { it("should return NULL with NULL as this param") { static value *value = NULL; value = value_list_pop(NULL); check(value == NULL, "value should be NULL"); } it("should return NULL with an empty list as this param") { static value *value = NULL; value = value_list_pop(list); check(value == NULL, "value should be NULL"); } } describe("value_list_unshift()") { for_it( "should add values to the begin of the list and it should be there", params_format(2, "start: %d", "elements: %d"), format_args(test_param.start, test_param.count), { int start; int count; }, test_params( {0, 0}, {0, 1}, {1, 1}, {4, 20}, ) ) { for (int i = test_param.start; i < test_param.start + test_param.count; ++i) { check(value_list_unshift(list, value_new(i)) == 1); } for (int i = test_param.start + test_param.count - 1; i >= test_param.start; --i) { value *value = value_list_shift(list); check(value != NULL, "value should not be NULL"); check(value->i == i, "value should be %d but is %d", i, value->i); value_destroy(&value); } } for_it_end(); it("should return 0 with NULL as this param") { check(value_list_unshift(NULL, value_new(1)) == 0, "return should not be 0"); } it("should return 0 with NULL as valueElement param") { check(value_list_unshift(list, NULL) == 0, "return should not be 0"); } } describe("value_list_shift()") { it("should return NULL with NULL as this param") { static value *value = NULL; value = value_list_shift(NULL); check(value == NULL, "value should be NULL"); } it("should return NULL with an empty list as this param") { static value *value = NULL; value = value_list_shift(list); check(value == NULL, "value should be NULL"); } } describe("value_list_getIterator()") { for_it( "should iterate over all values from the list", params_format(2, "start: %d", "elements: %d"), format_args(test_param.start, test_param.count), { int start; int count; }, test_params( {0, 0}, {0, 1}, {1, 1}, {4, 20}, ) ) { static value_list_iter *iter = NULL; for (int i = test_param.start; i < test_param.start + test_param.count; ++i) { check(value_list_push(list, value_new(i)) == 1); } iter = value_list_getIterator(list); check(iter != NULL, "iter should not be NULL"); int i = test_param.start; while (value_list_iter_hasNext(iter)) { check(i < test_param.start + test_param.count, "iteration should not iterate more than values added"); value *value = value_list_iter_next(iter); check(value != NULL, "value should not be NULL"); check(value->i == i, "value should be %d but is %d", i, value->i); ++i; } value_list_iter_destroy(&iter); check(iter == NULL, "iter should be NULL"); } for_it_end(); it("should return NULL with NULL as this param") { static value_list_iter *iter = NULL; iter = value_list_getIterator(NULL); check(iter == NULL, "iter should be NULL"); } } describe("value_list_iter_next()") { it("should return NULL with NULL as this param") { static value *value = NULL; value = value_list_iter_next(NULL); check(value == NULL, "value should be NULL"); } } describe("value_list_iter_hasNext()") { it("should return false with NULL as this param") { check(value_list_iter_hasNext(NULL) == false, "return should be false"); } } describe("value_list_iter_destroy()") { it("should work with NULL as this param") { value_list_iter_destroy(NULL); } it("should work with a pointer to NULL as this param") { static value_list_iter *empty = NULL; value_list_iter_destroy(&empty); check(empty == NULL, "empty should be NULL"); } } describe("value_list_destroy()") { it("should work with NULL as this param") { value_list_destroy(NULL); } it("should work with a pointer to NULL as this param") { static value_list *empty = NULL; value_list_destroy(&empty); check(empty == NULL, "empty should be NULL"); } } describe("value_list_peek()") { it("should return NULL with NULL as this param") { static value *value = NULL; value = value_list_peek(NULL); check(value == NULL, "value should be NULL"); } it("should return NULL with an empty list as this param") { static value *value = NULL; value = value_list_peek(list); check(value == NULL, "value should be NULL"); } } } }