Skip to content

Commit

Permalink
tests: improve unit testing for bf_list
Browse files Browse the repository at this point in the history
Add unit tests for the following bf_list-related functions:
- bf_list_is_tail()
- bf_list_get_head()
- bf_list_get_tail()
- bf_list_node_next()
- bf_list_node_prev()
- bf_list_node_take_data()

Signed-off-by: Quentin Deslandes <[email protected]>
  • Loading branch information
qdeslandes committed Feb 4, 2024
1 parent 8039453 commit 9370c2c
Showing 1 changed file with 88 additions and 16 deletions.
104 changes: 88 additions & 16 deletions tests/unit/src/core/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,15 @@ static void init_and_fill(bf_list *l, size_t count, const bf_list_ops *ops,
static bf_list_ops noop_ops = {.free = noop_free};
static bf_list_ops dummy_ops = {.free = dummy_free};

/* TestAssert(list, bf_list_new, (NULL, NOT_NULL));
TestAssert(list, bf_list_new, (NOT_NULL, NULL));
TestAssert(list, bf_list_free, (NULL));
TestAssert(list, bf_list_init, (NULL, NOT_NULL));
TestAssert(list, bf_list_init, (NOT_NULL, NULL));
TestAssert(list, bf_list_clean, (NULL));
TestAssert(list, bf_list_size, (NULL));
TestAssert(list, bf_list_add_head, (NULL, NOT_NULL));
TestAssert(list, bf_list_add_tail, (NULL, NOT_NULL));
TestAssert(list, bf_list_get_head, (NULL));
TestAssert(list, bf_list_get_tail, (NULL));
TestAssert(list, bf_list_node_next, (NULL));
TestAssert(list, bf_list_node_prev, (NULL));
TestAssert(list, bf_list_node_get_data, (NULL));
TestAssert(list, bf_list_node_take_data, (NULL)); */

Test(list, new_and_free)
{
bf_list *l = NULL;

expect_assert_failure(bf_list_new(NULL, NOT_NULL));
expect_assert_failure(bf_list_new(NOT_NULL, NULL));
expect_assert_failure(bf_list_free(NULL));
expect_assert_failure(bf_list_add_head(NULL, NOT_NULL));

{
// With noop operators
assert_int_equal(0, bf_list_new(&l, &noop_ops));
Expand Down Expand Up @@ -138,6 +127,10 @@ Test(list, init_and_clean)
{
bf_list l;

expect_assert_failure(bf_list_init(NULL, NOT_NULL));
expect_assert_failure(bf_list_init(NOT_NULL, NULL));
expect_assert_failure(bf_list_clean(NULL));

{
// With noop operators
bf_list_init(&l, &noop_ops);
Expand Down Expand Up @@ -190,6 +183,10 @@ Test(list, fill_from_head_and_check)
bf_list list;
size_t i;

expect_assert_failure(bf_list_size(NULL));
expect_assert_failure(bf_list_get_head(NULL));
expect_assert_failure(bf_list_node_get_data(NULL));

bf_list_init(&list, &dummy_ops);

assert_null(bf_list_get_head(&list));
Expand Down Expand Up @@ -248,6 +245,9 @@ Test(list, fill_from_tail_and_check)
bf_list list;
size_t i;

expect_assert_failure(bf_list_add_tail(NULL, NOT_NULL));
expect_assert_failure(bf_list_get_tail(NULL));

bf_list_init(&list, &dummy_ops);

assert_null(bf_list_get_head(&list));
Expand Down Expand Up @@ -275,3 +275,75 @@ Test(list, fill_from_tail_and_check)

bf_list_clean(&list);
}

Test(list, is_tail)
{
bf_list l;

expect_assert_failure(bf_list_is_tail(NULL, NOT_NULL));
expect_assert_failure(bf_list_is_tail(NOT_NULL, NULL));

init_and_fill(&l, 10, &dummy_ops, dummy_filler_head);

assert_true(bf_list_is_tail(&l, bf_list_get_tail(&l)));
assert_false(bf_list_is_tail(&l, bf_list_get_head(&l)));

bf_list_clean(&l);
}

Test(list, prev_next_node_access)
{
expect_assert_failure(bf_list_node_next(NULL));
expect_assert_failure(bf_list_node_prev(NULL));

{
_cleanup_bf_list_ bf_list *l = NULL;

new_and_fill(&l, 0, &noop_ops, bf_list_add_head);

assert_null(bf_list_get_head(l));
assert_null(bf_list_get_tail(l));
}

{
_cleanup_bf_list_ bf_list *l = NULL;

new_and_fill(&l, 1, &noop_ops, bf_list_add_head);

assert_ptr_equal(bf_list_get_head(l), bf_list_get_tail(l));
assert_null(bf_list_node_next(bf_list_get_tail(l)));
assert_null(bf_list_node_prev(bf_list_get_head(l)));
}

{
_cleanup_bf_list_ bf_list *l = NULL;

new_and_fill(&l, 2, &noop_ops, bf_list_add_head);

assert_ptr_not_equal(bf_list_get_head(l), bf_list_get_tail(l));
assert_ptr_equal(bf_list_node_next(bf_list_get_head(l)),
bf_list_get_tail(l));
assert_ptr_equal(bf_list_node_prev(bf_list_get_tail(l)),
bf_list_get_head(l));
}
}

Test(list, node_take_data)
{
{
_cleanup_bf_list_ bf_list *l = NULL;

new_and_fill(&l, 5, &dummy_ops, dummy_filler_tail);

bf_list_foreach (l, node) {
assert_non_null(node);
assert_non_null(node->data);

void *data = bf_list_node_take_data(node);
assert_non_null(data);
assert_null(node->data);

free(data);
}
}
}

0 comments on commit 9370c2c

Please sign in to comment.