diff options
-rw-r--r-- | src/common/utils.c | 13 | ||||
-rw-r--r-- | src/common/utils.h | 4 | ||||
-rw-r--r-- | tests/common/check_utils.c | 33 |
3 files changed, 50 insertions, 0 deletions
diff --git a/src/common/utils.c b/src/common/utils.c index 97fa671..c78229d 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -105,6 +105,19 @@ bc_slist_length(bc_slist_t *l) } +bc_slist_t* +bc_slist_pop(bc_slist_t *l, void **data) +{ + if (l == NULL) + return l; + bc_slist_t *tmp = l; + l = l->next; + *data = tmp->data; + free(tmp); + return l; +} + + char* bc_strdup(const char *s) { diff --git a/src/common/utils.h b/src/common/utils.h index b0388b3..811c593 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -35,6 +35,10 @@ 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); +// here i'm just extending slist, instead of implementing a fully featuered +// queue for the sake of simplicity +bc_slist_t* bc_slist_pop(bc_slist_t *l, void **data); + // strfuncs diff --git a/tests/common/check_utils.c b/tests/common/check_utils.c index dc1eadd..4a4ee2b 100644 --- a/tests/common/check_utils.c +++ b/tests/common/check_utils.c @@ -87,6 +87,38 @@ test_slist_length(void **state) static void +test_slist_pop(void **state) +{ + bc_slist_t *l = NULL; + l = bc_slist_append(l, (void*) bc_strdup("bola")); + l = bc_slist_append(l, (void*) bc_strdup("guda")); + l = bc_slist_append(l, (void*) bc_strdup("chula")); + char *v = NULL; + l = bc_slist_pop(l, &v); + assert_string_equal(v, "bola"); + assert_non_null(l); + assert_string_equal(l->data, "guda"); + assert_string_equal(l->next->data, "chula"); + assert_null(l->next->next); + free(v); + l = bc_slist_pop(l, &v); + assert_string_equal(v, "guda"); + assert_non_null(l); + assert_string_equal(l->data, "chula"); + assert_null(l->next); + free(v); + l = bc_slist_pop(l, &v); + assert_string_equal(v, "chula"); + assert_null(l); + free(v); + v = NULL; + l = bc_slist_pop(l, &v); + assert_null(v); + assert_null(l); +} + + +static void test_strdup(void **state) { char *str = bc_strdup("bola"); @@ -1026,6 +1058,7 @@ main(void) unit_test(test_slist_prepend), unit_test(test_slist_free), unit_test(test_slist_length), + unit_test(test_slist_pop), // strfuncs unit_test(test_strdup), |