diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-04-27 16:13:18 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-04-28 21:58:06 +0200 |
commit | 0cfb3dad5de015b658b0917c43fabe420781f39f (patch) | |
tree | 25abaeeae73ce041f74246fd28ab9f9c9222d0a7 | |
parent | ab4685070515230c9aa64f9e69eec7fc061ee8d7 (diff) | |
download | blogc-0cfb3dad5de015b658b0917c43fabe420781f39f.tar.gz blogc-0cfb3dad5de015b658b0917c43fabe420781f39f.tar.bz2 blogc-0cfb3dad5de015b658b0917c43fabe420781f39f.zip |
common: added bc_slist_remove
-rw-r--r-- | src/common/utils.c | 19 | ||||
-rw-r--r-- | src/common/utils.h | 1 | ||||
-rw-r--r-- | tests/common/check_utils.c | 35 |
3 files changed, 55 insertions, 0 deletions
diff --git a/src/common/utils.c b/src/common/utils.c index 692d1ce..1d4a678 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -74,6 +74,25 @@ bc_slist_prepend(bc_slist_t *l, void *data) } +bc_slist_t* +bc_slist_remove(bc_slist_t *l, bc_slist_t *r, bc_free_func_t free_func) +{ + bc_slist_t *p = NULL; + for (bc_slist_t *tmp = l; tmp != NULL; p = tmp, tmp = tmp->next) { + if (tmp == r) { + if (p == NULL) + l = tmp->next; + else + p->next = tmp->next; + if ((free_func != NULL) && (l->data != NULL)) + free_func(tmp->data); + free(tmp); + 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..0e938a4 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_remove(bc_slist_t *l, bc_slist_t *r, bc_free_func_t free_func); 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 cc14f4e..3bb4888 100644 --- a/tests/common/check_utils.c +++ b/tests/common/check_utils.c @@ -54,6 +54,40 @@ test_slist_prepend(void **state) static void +test_slist_remove(void **state) +{ + bc_slist_t *l = NULL; + l = bc_slist_append(l, bc_strdup("bola")); + l = bc_slist_append(l, bc_strdup("guda")); + l = bc_slist_append(l, bc_strdup("chunda")); + l = bc_slist_append(l, bc_strdup("pumba")); + l = bc_slist_append(l, bc_strdup("bussunda")); + l = bc_slist_remove(l, l->next->next, free); + assert_non_null(l); + assert_string_equal(l->data, "bola"); + assert_string_equal(l->next->data, "guda"); + assert_string_equal(l->next->next->data, "pumba"); + assert_string_equal(l->next->next->next->data, "bussunda"); + assert_null(l->next->next->next->next); + + l = bc_slist_remove(l, l, free); + assert_non_null(l); + assert_string_equal(l->data, "guda"); + assert_string_equal(l->next->data, "pumba"); + assert_string_equal(l->next->next->data, "bussunda"); + assert_null(l->next->next->next); + + l = bc_slist_remove(l, l->next->next, free); + assert_non_null(l); + assert_string_equal(l->data, "guda"); + assert_string_equal(l->next->data, "pumba"); + assert_null(l->next->next); + + bc_slist_free_full(l, free); +} + + +static void test_slist_free(void **state) { bc_slist_t *l = NULL; @@ -1049,6 +1083,7 @@ main(void) // slist unit_test(test_slist_append), unit_test(test_slist_prepend), + unit_test(test_slist_remove), unit_test(test_slist_free), unit_test(test_slist_length), |