**/
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.
*
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
//-------------------------------------------------------------------------