From: Sebastian Harl Date: Wed, 1 Jan 2014 23:39:20 +0000 (+0100) Subject: utils llist: Added sdb_llist_len(). X-Git-Tag: sysdb-0.1.0~265 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=1d75ab0ada8164955ec343834b09960332b9befa;p=sysdb.git utils llist: Added sdb_llist_len(). --- diff --git a/src/include/utils/llist.h b/src/include/utils/llist.h index 2742139..f33958a 100644 --- a/src/include/utils/llist.h +++ b/src/include/utils/llist.h @@ -203,6 +203,13 @@ sdb_llist_iter_get_next(sdb_llist_iter_t *iter); int sdb_llist_iter_remove_current(sdb_llist_iter_t *iter); +/* + * sdb_llist_len: + * Return the length (number of elements) of the list. + */ +size_t +sdb_llist_len(sdb_llist_t *list); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/utils/llist.c b/src/utils/llist.c index 0a4665d..64f7d0c 100644 --- a/src/utils/llist.c +++ b/src/utils/llist.c @@ -458,5 +458,13 @@ sdb_llist_iter_remove_current(sdb_llist_iter_t *iter) return 0; } /* sdb_llist_iter_remove */ +size_t +sdb_llist_len(sdb_llist_t *list) +{ + if (! list) + return 0; + return list->length; +} /* sdb_llist_len */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/t/utils/llist_test.c b/t/utils/llist_test.c index ede8200..ae9655a 100644 --- a/t/utils/llist_test.c +++ b/t/utils/llist_test.c @@ -118,6 +118,11 @@ END_TEST START_TEST(test_sdb_llist_append) { size_t i; + + fail_unless(sdb_llist_len(list) == 0, + "sdb_llist_len() = %zu; expected: 0", + sdb_llist_len(list)); + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { int check = sdb_llist_append(list, &golden_data[i]); fail_unless(check == 0, @@ -126,6 +131,9 @@ START_TEST(test_sdb_llist_append) fail_unless(golden_data[i].ref_cnt == 2, "sdb_llist_append(%s) did not take ownership", golden_data[i].name); + fail_unless(sdb_llist_len(list) == i + 1, + "sdb_llist_len() = %zu; expected: zu", + sdb_llist_len(list), i + 1); } } END_TEST