From: Sebastian Harl Date: Wed, 1 Jan 2014 23:54:13 +0000 (+0100) Subject: utils llist: Added sdb_llist_get(). X-Git-Tag: sysdb-0.1.0~263 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ea29adb896a47f32e86119d6161dc5bc2259565b;p=sysdb.git utils llist: Added sdb_llist_get(). --- diff --git a/src/include/utils/llist.h b/src/include/utils/llist.h index f33958a..3eefae5 100644 --- a/src/include/utils/llist.h +++ b/src/include/utils/llist.h @@ -119,6 +119,13 @@ int sdb_llist_insert_sorted(sdb_llist_t *list, sdb_object_t *obj, sdb_llist_cmp_cb); +/* + * sdb_llist_get: + * Returns the i-th element of the list or NULL in case of an error. + */ +sdb_object_t * +sdb_llist_get(sdb_llist_t *list, size_t i); + /* * sdb_llist_search: * Search for a object in the given 'list'. The function will return the first diff --git a/src/utils/llist.c b/src/utils/llist.c index 64f7d0c..c56cdbd 100644 --- a/src/utils/llist.c +++ b/src/utils/llist.c @@ -304,6 +304,22 @@ sdb_llist_insert_sorted(sdb_llist_t *list, return status; } /* sdb_llist_insert_sorted */ +sdb_object_t * +sdb_llist_get(sdb_llist_t *list, size_t i) +{ + sdb_llist_elem_t *elem; + size_t j; + + if ((! list) || (i >= list->length)) + return NULL; + + for (elem = list->head, j = 0; j < i; elem = elem->next, ++j) + /* iterate */; + + assert(elem); + return elem->obj; +} /* sdb_llist_get */ + sdb_object_t * sdb_llist_search(sdb_llist_t *list, sdb_llist_lookup_cb lookup, const void *user_data) diff --git a/t/utils/llist_test.c b/t/utils/llist_test.c index ae9655a..37b45ed 100644 --- a/t/utils/llist_test.c +++ b/t/utils/llist_test.c @@ -170,6 +170,22 @@ START_TEST(test_validate_insert) } END_TEST +START_TEST(test_sdb_llist_get) +{ + size_t i; + populate(); + for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) { + sdb_object_t *check = sdb_llist_get(list, i); + fail_unless(check == &golden_data[i], + "sdb_llist_get() = NULL; expected: %p", + &golden_data[i]); + fail_unless(check->ref_cnt == 2, + "sdb_llist_get() changed reference count; got: %i; " + "expected: 2", check->ref_cnt); + } +} +END_TEST + START_TEST(test_sdb_llist_search) { size_t i; @@ -289,6 +305,7 @@ util_llist_suite(void) tcase_add_test(tc, test_sdb_llist_append); tcase_add_test(tc, test_sdb_llist_insert); tcase_add_test(tc, test_validate_insert); + tcase_add_test(tc, test_sdb_llist_get); tcase_add_test(tc, test_sdb_llist_search); tcase_add_test(tc, test_sdb_llist_shift); tcase_add_test(tc, test_sdb_llist_iter);