summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 7cc0105)
raw | patch | inline | side by side (parent: 7cc0105)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 1 Jan 2014 23:54:13 +0000 (00:54 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 1 Jan 2014 23:54:13 +0000 (00:54 +0100) |
src/include/utils/llist.h | patch | blob | history | |
src/utils/llist.c | patch | blob | history | |
t/utils/llist_test.c | patch | blob | history |
index f33958a7550bffe3d0fab8d1195cd7483b5ee08c..3eefae5e19b894adf457a5d195e7840a11f79757 100644 (file)
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 64f7d0c1bd9d4ba096d4878f9e64c198cfbff4f8..c56cdbda098b581c9d957aeebb7db9c1725ffdf0 100644 (file)
--- a/src/utils/llist.c
+++ b/src/utils/llist.c
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 ae9655ac46ec46ace6410363adfdccb8e1b5466e..37b45edcd36d87f69405ec075ab186208ac5e59b 100644 (file)
--- a/t/utils/llist_test.c
+++ b/t/utils/llist_test.c
}
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;
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);