]> git.mdlowis.com Git - projs/libcds.git/commitdiff
list_index_of function & tests
authora bellenir <a@bellenir.com>
Wed, 30 Jul 2014 08:36:24 +0000 (08:36 +0000)
committera bellenir <a@bellenir.com>
Wed, 30 Jul 2014 08:36:24 +0000 (08:36 +0000)
source/list/list.c
source/list/list.h
tests/test_list.c

index b2a0e92bbf84aa6735e08b9163d6746f7bcf2237..9efff8069df222b389b5eb8885debfe8375fff21 100644 (file)
@@ -73,6 +73,18 @@ list_node_t* list_at(list_t* list, size_t index)
     return node;
 }
 
+int list_index_of(list_t* list, list_node_t* node)
+{
+    int i = 0;
+    list_node_t* edon = list->head;
+    while( NULL != edon && edon != node)
+    {
+        edon = edon->next;
+        i++;
+    }
+    return (node == edon) ? i : -1;
+}
+
 list_node_t* list_push_front( list_t* list, void* contents )
 {
     list_node_t* node = list_new_node( contents );
index 3fc787ff6b8ed8e654788e67aa4f4a66ecab5ae8..1a46595ceabb957df4ba460684444f338dfa6923 100644 (file)
@@ -105,6 +105,21 @@ list_node_t* list_prev(list_t* list, list_node_t* node);
  **/
 list_node_t* list_at(list_t* list, size_t index);
 
+/**
+ * @brief   Return the index of the specified node in a linked list.
+ *
+ * This function loops through the linked list and returns the index in the list
+ * that matches the specified node. Returns -1 if the node is not found.
+ * Note: since NULL is implicitly at the end of every list, calling this
+ * with NULL for the node is essentially equivalent to list_size
+ *
+ * @param list  The list to search thru
+ * @param node  The node to look for
+ *
+ * @return The int index of the supplied node, -1 if not found.
+ **/
+int list_index_of(list_t* list, list_node_t* node);
+
 /**
  * @brief Adds a new node to the front of an existing linked list.
  *
index d6168650c74a38d4efb18630c48f354a47cf3301..6464814460565a38c1fa260e6cba393a594ed78e 100644 (file)
@@ -213,6 +213,74 @@ TEST_SUITE(List) {
         CHECK( &node3 == list_at( &list, 2 ) );
     }
 
+    //-------------------------------------------------------------------------
+    // Test list_index_of function
+    //-------------------------------------------------------------------------
+    TEST(Verify_list_index_of_returns_negative_one_when_list_is_empty)
+    {
+        list_t list = { NULL, NULL };
+        list_node_t bogus = { NULL, NULL };
+        CHECK( -1 == list_index_of(&list, &bogus) );
+    }
+
+    TEST(Verify_list_index_of_returns_negative_one_when_node_is_not_found)
+    {
+        list_node_t node3 = { NULL, NULL };
+        list_node_t node2 = { NULL, &node3 };
+        list_node_t node1 = { NULL, &node2 };
+        list_t list = { &node1, &node3 };
+        list_node_t bogus = { NULL, NULL };
+        CHECK( -1 == list_index_of(&list, &bogus) );
+    }
+
+    TEST(Verify_list_index_of_returns_list_size_when_node_is_null)
+    {
+        list_t list0 = { NULL, NULL };
+        CHECK( 0 == list_index_of(&list0, NULL) );
+
+        list_node_t node1_1 = { NULL, NULL };
+        list_t list1 = { &node1_1, &node1_1 };
+        CHECK( 1 == list_index_of(&list1, NULL) );
+
+        list_node_t node2_2 = { NULL, NULL};
+        list_node_t node2_1 = { NULL, &node2_2 };
+        list_t list2 = { &node2_1, &node2_2 };
+        CHECK( 2 == list_index_of(&list2, NULL) );
+
+        list_node_t node3_3 = { NULL, NULL };
+        list_node_t node3_2 = { NULL, &node3_3 };
+        list_node_t node3_1 = { NULL, &node3_2 };
+        list_t list3 = { &node3_1, &node3_3 };
+        CHECK( 3 == list_index_of(&list3, NULL) );
+    }
+
+    TEST(Verify_list_index_of_returns_zero_for_first_item_in_list)
+    {
+        list_node_t node3 = { NULL, NULL };
+        list_node_t node2 = { NULL, &node3 };
+        list_node_t node1 = { NULL, &node2 };
+        list_t list = { &node1, &node3 };
+        CHECK( 0 == list_index_of(&list, &node1) );
+    }
+
+    TEST(Verify_list_index_of_returns_zero_for_second_item_in_list)
+    {
+        list_node_t node3 = { NULL, NULL };
+        list_node_t node2 = { NULL, &node3 };
+        list_node_t node1 = { NULL, &node2 };
+        list_t list = { &node1, &node3 };
+        CHECK( 1 == list_index_of(&list, &node2) );
+    }
+
+    TEST(Verify_list_index_of_returns_zero_for_third_item_in_list)
+    {
+        list_node_t node3 = { NULL, NULL };
+        list_node_t node2 = { NULL, &node3 };
+        list_node_t node1 = { NULL, &node2 };
+        list_t list = { &node1, &node3 };
+        CHECK( 2 == list_index_of(&list, &node3) );
+    }
+
     //-------------------------------------------------------------------------
     // Test list_push_front function
     //-------------------------------------------------------------------------