From: a bellenir Date: Wed, 30 Jul 2014 08:36:24 +0000 (+0000) Subject: list_index_of function & tests X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=95eeba9eb09265d10bcea028083345a3f33b5d5b;p=projs%2Flibcds.git list_index_of function & tests --- diff --git a/source/list/list.c b/source/list/list.c index b2a0e92..9efff80 100644 --- a/source/list/list.c +++ b/source/list/list.c @@ -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 ); diff --git a/source/list/list.h b/source/list/list.h index 3fc787f..1a46595 100644 --- a/source/list/list.h +++ b/source/list/list.h @@ -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. * diff --git a/tests/test_list.c b/tests/test_list.c index d616865..6464814 100644 --- a/tests/test_list.c +++ b/tests/test_list.c @@ -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 //-------------------------------------------------------------------------