From bec4fd31fd9f36dbcdad9d443a0c609adde1af67 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Fri, 29 May 2020 01:23:08 +0200 Subject: common: utils: added bc_slist_append_list --- src/common/utils.c | 16 ++++++++++++++++ src/common/utils.h | 1 + tests/common/check_utils.c | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/common/utils.c b/src/common/utils.c index f686222..4e90c77 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -74,6 +74,22 @@ bc_slist_prepend(bc_slist_t *l, void *data) } +bc_slist_t* +bc_slist_append_list(bc_slist_t *l, bc_slist_t *n) +{ + if (l == NULL) { + return n; + } + if (n == NULL) { + return l; + } + bc_slist_t *tmp; + for (tmp = l; tmp->next != NULL; tmp = tmp->next); + tmp->next = n; + return l; +} + + void bc_slist_free_full(bc_slist_t *l, bc_free_func_t free_func) { diff --git a/src/common/utils.h b/src/common/utils.h index 94c3356..84ce066 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -31,6 +31,7 @@ typedef struct _bc_slist_t { bc_slist_t* bc_slist_append(bc_slist_t *l, void *data); bc_slist_t* bc_slist_prepend(bc_slist_t *l, void *data); +bc_slist_t* bc_slist_append_list(bc_slist_t *l, bc_slist_t *n); void bc_slist_free(bc_slist_t *l); void bc_slist_free_full(bc_slist_t *l, bc_free_func_t free_func); size_t bc_slist_length(bc_slist_t *l); diff --git a/tests/common/check_utils.c b/tests/common/check_utils.c index 471cd77..4efc1a5 100644 --- a/tests/common/check_utils.c +++ b/tests/common/check_utils.c @@ -53,6 +53,32 @@ test_slist_prepend(void **state) } +static void +test_slist_append_list(void **state) +{ + bc_slist_t *l = NULL; + assert_null(bc_slist_append_list(l, NULL)); + l = bc_slist_append(l, (void*) bc_strdup("bola")); + l = bc_slist_append(l, (void*) bc_strdup("guda")); + bc_slist_t *t = bc_slist_append_list(l, NULL); + assert_true(l == t); + bc_slist_t *n = NULL; + n = bc_slist_append(n, (void*) bc_strdup("chunda")); + n = bc_slist_append(n, (void*) bc_strdup("asd")); + l = bc_slist_append_list(l, n); + assert_non_null(l); + assert_string_equal(l->data, "bola"); + assert_non_null(l->next); + assert_string_equal(l->next->data, "guda"); + assert_non_null(l->next->next); + assert_string_equal(l->next->next->data, "chunda"); + assert_non_null(l->next->next->next); + assert_string_equal(l->next->next->next->data, "asd"); + assert_null(l->next->next->next->next); + bc_slist_free_full(l, free); +} + + static void test_slist_free(void **state) { @@ -1053,6 +1079,7 @@ main(void) // slist unit_test(test_slist_append), unit_test(test_slist_prepend), + unit_test(test_slist_append_list), unit_test(test_slist_free), unit_test(test_slist_length), -- cgit v1.2.3-18-g5258