Code

utils llist: Added sdb_llist_len().
authorSebastian Harl <sh@tokkee.org>
Wed, 1 Jan 2014 23:39:20 +0000 (00:39 +0100)
committerSebastian Harl <sh@tokkee.org>
Wed, 1 Jan 2014 23:39:20 +0000 (00:39 +0100)
src/include/utils/llist.h
src/utils/llist.c
t/utils/llist_test.c

index 2742139f17db051d12b36c279889ff0c0bbfb48f..f33958a7550bffe3d0fab8d1195cd7483b5ee08c 100644 (file)
@@ -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
index 0a4665d09296c2387bcf1f369f81f5e751c20fa4..64f7d0c1bd9d4ba096d4878f9e64c198cfbff4f8 100644 (file)
@@ -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 : */
 
index ede8200a42a325d3f22707eb001febdc8ac9b51e..ae9655ac46ec46ace6410363adfdccb8e1b5466e 100644 (file)
@@ -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(<empty list>) = %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(<empty list>) = %zu; expected: zu",
+                               sdb_llist_len(list), i + 1);
        }
 }
 END_TEST