From: Mike D. Lowis Date: Tue, 5 Jun 2012 16:02:50 +0000 (-0400) Subject: Added more tests for singly linked list X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=906c2ca75b5d8e93d4f1f080b530a871151bb2f9;p=projs%2Flibcds.git Added more tests for singly linked list --- diff --git a/source/lists/single_link/sll.c b/source/lists/single_link/sll.c index a8940c5..c27d04f 100644 --- a/source/lists/single_link/sll.c +++ b/source/lists/single_link/sll.c @@ -44,6 +44,26 @@ void sll_free_node(sll_node_t* node, int free_contents) } } +sll_node_t* sll_front( sll_t* list ) +{ + sll_node_t* node = NULL; + if( NULL != list ) + { + node = list->head; + } + return node; +} + +sll_node_t* sll_back( sll_t* list ) +{ + sll_node_t* node = NULL; + if( NULL != list ) + { + node = list->tail; + } + return node; +} + unsigned int sll_length(sll_t* list) { unsigned int length = 0; @@ -135,19 +155,61 @@ sll_node_t* sll_pop_front( sll_t* list ) sll_node_t* sll_pop_back( sll_t* list ) { sll_node_t* node = NULL; - if( (NULL != list) && (NULL != list->tail) ) + if( NULL != list ) { + if ( list->head == list->tail ) + { + node = list->head; + list->head = NULL; + list->tail = NULL; + } + else + { + sll_node_t* next_tail = list->head; + while( next_tail->next != list->tail ) + { + next_tail = next_tail->next; + } + node = next_tail->next; + next_tail->next = NULL; + list->tail = next_tail; + } } return node; } sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents) { - return 0; + sll_node_t* new_node = NULL; + if( NULL == list ) + { + if( 0 == index ) + { + new_node = sll_push_front( list, contents ); + } + else + { + sll_node_t* prev_node = sll_index( list, index - 1 ); + if( NULL == prev_node ) + { + sll_node_t* next_node = prev_node->next; + new_node = sll_new_node( contents ); + new_node = next_node; + prev_node->next = new_node; + if( NULL == next_node ) + { + list->tail = new_node; + } + } + } + } + return new_node; } sll_node_t* sll_delete( sll_t* list, unsigned int index, int free_contents) { - return 0; + sll_node_t* new_node = NULL; + + return new_node; } diff --git a/source/lists/single_link/sll.h b/source/lists/single_link/sll.h index 65d5d95..d937aa0 100644 --- a/source/lists/single_link/sll.h +++ b/source/lists/single_link/sll.h @@ -57,6 +57,9 @@ void sll_free(sll_t* list, int free_contents); */ void sll_free_node(sll_node_t* node, int free_contents); +sll_node_t* sll_front( sll_t* list ); +sll_node_t* sll_back( sll_t* list ); + /** * @brief Returns the number of elements in the list. * diff --git a/tests/test_sll.cpp b/tests/test_sll.cpp index 6a91b96..4ac71a3 100644 --- a/tests/test_sll.cpp +++ b/tests/test_sll.cpp @@ -88,6 +88,50 @@ namespace { sll_free_node( node, 1 ); } + //------------------------------------------------------------------------- + // Test sll_front function + //------------------------------------------------------------------------- + TEST(Verify_sll_front_returns_NULL_if_list_is_NULL) + { + CHECK( NULL == sll_front( NULL ) ); + } + + TEST(Verify_sll_front_returns_NULL_if_list_is_empty) + { + sll_t list = { NULL, NULL }; + CHECK( NULL == sll_front( &list ) ); + } + + TEST(Verify_sll_front_returns_the_head_of_the_list) + { + sll_node_t node2 = { NULL, NULL }; + sll_node_t node1 = { NULL, &node2 }; + sll_t list = { &node1, &node2 }; + CHECK( &node1 == sll_front( &list ) ); + } + + //------------------------------------------------------------------------- + // Test sll_back function + //------------------------------------------------------------------------- + TEST(Verify_sll_back_returns_NULL_if_list_is_NULL) + { + CHECK( NULL == sll_back( NULL ) ); + } + + TEST(Verify_sll_back_returns_NULL_if_list_is_empty) + { + sll_t list = { NULL, NULL }; + CHECK( NULL == sll_back( &list ) ); + } + + TEST(Verify_sll_back_returns_the_tail_of_the_list) + { + sll_node_t node2 = { NULL, NULL }; + sll_node_t node1 = { NULL, &node2 }; + sll_t list = { &node1, &node2 }; + CHECK( &node2 == sll_back( &list ) ); + } + //------------------------------------------------------------------------- // Test sll_length function //------------------------------------------------------------------------- @@ -265,6 +309,17 @@ namespace { CHECK( &node2 == list.tail ); } + TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_3) + { + sll_node_t node3 = { NULL, NULL }; + sll_node_t node2 = { NULL, &node3 }; + sll_node_t node1 = { NULL, &node2 }; + sll_t list = { &node1, &node3 }; + CHECK( &node1 == sll_pop_front( &list ) ); + CHECK( &node2 == list.head ); + CHECK( &node3 == list.tail ); + } + //------------------------------------------------------------------------- // Test sll_pop_back function //------------------------------------------------------------------------- @@ -281,12 +336,32 @@ namespace { TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_1) { - CHECK(false); + sll_node_t node1 = { NULL, NULL }; + sll_t list = { &node1, &node1 }; + CHECK( &node1 == sll_pop_back( &list ) ); + CHECK( NULL == list.head ); + CHECK( NULL == list.tail ); } TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_2) { - CHECK(false); + sll_node_t node2 = { NULL, NULL }; + sll_node_t node1 = { NULL, &node2 }; + sll_t list = { &node1, &node2 }; + CHECK( &node2 == sll_pop_back( &list ) ); + CHECK( &node1 == list.head ); + CHECK( &node1 == list.tail ); + } + + TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_3) + { + sll_node_t node3 = { NULL, NULL }; + sll_node_t node2 = { NULL, &node3 }; + sll_node_t node1 = { NULL, &node2 }; + sll_t list = { &node1, &node3 }; + CHECK( &node3 == sll_pop_back( &list ) ); + CHECK( &node1 == list.head ); + CHECK( &node2 == list.tail ); } //-------------------------------------------------------------------------