From: a bellenir Date: Thu, 31 Jul 2014 23:53:38 +0000 (+0000) Subject: fixing up list_insert_after; handle NULL for node as pushing to front, fix comments... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=b4d6106ef1c4822b344f38cc152a3f00d72a138b;p=projs%2Flibcds.git fixing up list_insert_after; handle NULL for node as pushing to front, fix comments in list.h, add tests --- diff --git a/source/list/list.c b/source/list/list.c index 4adcdb4..4df9a38 100644 --- a/source/list/list.c +++ b/source/list/list.c @@ -183,16 +183,20 @@ list_node_t* list_insert( list_t* list, size_t index, void* contents) list_node_t* list_insert_after( list_t* list, list_node_t* node, void* contents) { - list_node_t* new_node = NULL; - if (node != NULL) + list_node_t* new_node = list_new_node(contents); + if(NULL != node) { - new_node = list_new_node(contents); new_node->next = node->next; node->next = new_node; - if (node == list->tail) - { - list->tail = new_node; - } + } + else + { + new_node->next = list->head; + list->head = new_node; + } + if (node == list->tail) + { + list->tail = new_node; } return new_node; } diff --git a/source/list/list.h b/source/list/list.h index 36d8cbb..a55d662 100644 --- a/source/list/list.h +++ b/source/list/list.h @@ -186,17 +186,16 @@ list_node_t* list_pop_back( list_t* list ); list_node_t* list_insert( list_t* list, size_t index, void* contents); /** - * @brief Inserts a new node in a linked list at the specified index. + * @brief Inserts a new node in a linked list after the specified node * - * This function traverses the list to the desired index and inserts a new node - * with the given contents at that position. The node previously at the desired - * index becomes the child of the new node. + * This function traverses the list to the specified and inserts a new node after + * it with the given contents at that position. * * @param list The list to operate on. * @param node The node after which the item should be inserted. * @param contents The contents of the new node. * - * @return Pointer to the newly inserted node, NULL if index is out of range. + * @return Pointer to the newly inserted node **/ list_node_t* list_insert_after( list_t* list, list_node_t* node, void* contents); diff --git a/tests/test_list.c b/tests/test_list.c index 483d3ef..382fe0f 100644 --- a/tests/test_list.c +++ b/tests/test_list.c @@ -480,11 +480,52 @@ TEST_SUITE(List) { //------------------------------------------------------------------------- // Test list_insert_after function //------------------------------------------------------------------------- - TEST(Verify_insert_after_should_fail_to_insert_if_node_is_null) + TEST(Verify_insert_after_should_to_insert_to_head_if_node_is_null_and_list_is_empty) { list_t* list = list_new(); - list_node_t* node = list_insert_after( list, NULL, mem_box(0x1234) ); - CHECK( node == NULL ); + list_node_t* node = list_insert_after(list, NULL, mem_box(0x1234) ); + CHECK( node == list->head ); + CHECK( node == list->tail ); + CHECK( NULL == node->next ); + mem_release(list); + } + + TEST(Verify_insert_after_should_insert_to_head_if_node_is_null_and_list_is_populated) + { + list_t* list = list_new(); + list_node_t* node1 = list_insert_after(list, NULL, mem_box(0x0666)); + list_node_t* node2 = list_insert_after(list, NULL, mem_box(0x4242)); + CHECK( node2 == list->head ); + CHECK( node1 == list->tail ); + CHECK( node1 == node2->next ); + CHECK( NULL == node1->next ); + list_node_t* node3 = list_insert_after(list, NULL, mem_box(0x1234)); + CHECK( node3 == list->head ); + CHECK( node1 == list->tail ); + CHECK( node2 == node3->next ); + CHECK( node1 == node2->next ); + CHECK( NULL == node1->next ); + mem_release(list); + } + + TEST(Verify_insert_after_can_build_list_linearly) + { + list_t* list = list_new(); + list_node_t* node1 = list_insert_after(list, list->tail, mem_box(0x1234)); + CHECK( node1 == list->head ); + CHECK( node1 == list->tail ); + CHECK( NULL == node1->next ); + list_node_t* node2 = list_insert_after(list, list->tail, mem_box(0x4321)); + CHECK( node1 == list->head ); + CHECK( node2 == list->tail ); + CHECK( node2 == node1->next ); + CHECK( NULL == node2->next ); + list_node_t* node3 = list_insert_after(list, list->tail, mem_box(0x4242)); + CHECK( node1 == list->head ); + CHECK( node3 == list->tail ); + CHECK( node2 == node1->next ); + CHECK( node3 == node2->next ); + CHECK( NULL == node3->next ); mem_release(list); }