From 66cff0c1d627b10bd4b860b32f95510ed427a836 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 5 Jun 2012 02:40:25 -0400 Subject: [PATCH] Added tests for singly linked list --- source/lists/single_link/sll.c | 24 +++++++++++--- source/lists/single_link/sll.h | 14 +++----- tests/test_sll.cpp | 59 +++++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/source/lists/single_link/sll.c b/source/lists/single_link/sll.c index 4c00dd5..a8940c5 100644 --- a/source/lists/single_link/sll.c +++ b/source/lists/single_link/sll.c @@ -87,6 +87,7 @@ sll_node_t* sll_push_front( sll_t* list, void* contents ) { node = sll_new_node( contents ); node->next = list->head; + list->head = node; if( NULL == list->tail ) { list->tail = node; @@ -110,19 +111,34 @@ sll_node_t* sll_push_back( sll_t* list, void* contents ) else { list->tail->next = node; + list->tail = node; } } return node; } -sll_node_t* sll_pop_front( sll_t* list, int free_contents ) +sll_node_t* sll_pop_front( sll_t* list ) { - return 0; + sll_node_t* node = NULL; + if( (NULL != list) && (NULL != list->head) ) + { + node = list->head; + list->head = node->next; + if( node == list->tail ) + { + list->tail = NULL; + } + } + return node; } -sll_node_t* sll_pop_back( sll_t* list, int free_contents ) +sll_node_t* sll_pop_back( sll_t* list ) { - return 0; + sll_node_t* node = NULL; + if( (NULL != list) && (NULL != list->tail) ) + { + } + return node; } sll_node_t* sll_insert( sll_t* list, unsigned int index, void* contents) diff --git a/source/lists/single_link/sll.h b/source/lists/single_link/sll.h index a16b954..65d5d95 100644 --- a/source/lists/single_link/sll.h +++ b/source/lists/single_link/sll.h @@ -112,31 +112,25 @@ sll_node_t* sll_push_back( sll_t* list, void* contents ); * @brief Removes and returns a pointer to the first element of the list. * * This function removes the first node from the list and frees it's associated - * memory. If free_contents is passed a non-zero value then it's contents - * pointer is also freed. The second node in the list becomes the new head of - * the list. + * memory. * * @param list The lsit to operate on. - * @param free_contents Determines whether to free the contents pointer. * * @return Pointer to the newly added node. **/ -sll_node_t* sll_pop_front( sll_t* list, int free_contents ); +sll_node_t* sll_pop_front( sll_t* list ); /** * @brief Removes and returns a pointer to the last element of the list. * * This function removes the last node from the list and frees it's associated - * memory. If free_contents is passed a non-zero value then it's contents - * pointer is also freed. The second to last node in the list becomes the new - * tail of the list. + * memory. * * @param list The list to operate on. - * @param free_contents Determines whether to free the contents pointer. * * @return Pointer to the newly added node. **/ -sll_node_t* sll_pop_back( sll_t* list, int free_contents ); +sll_node_t* sll_pop_back( sll_t* list ); /** * @brief Inserts a new node in a linked list at the specified index. diff --git a/tests/test_sll.cpp b/tests/test_sll.cpp index fade5cd..6a91b96 100644 --- a/tests/test_sll.cpp +++ b/tests/test_sll.cpp @@ -198,7 +198,7 @@ namespace { CHECK( (void*)0x1234 == node->contents ); CHECK( NULL != node->next ); CHECK( node == list.head ); - CHECK( node == list.tail ); + CHECK( node != list.tail ); } //------------------------------------------------------------------------- @@ -224,20 +224,71 @@ namespace { { sll_node_t node1 = { NULL, NULL }; sll_t list = { &node1, &node1 }; - sll_node_t* node = sll_push_front( &list, (void*)0x1234 ); + sll_node_t* node = sll_push_back( &list, (void*)0x1234 ); CHECK( NULL != node ); CHECK( (void*)0x1234 == node->contents ); - CHECK( NULL != node->next ); - CHECK( node == list.head ); + CHECK( &node1 != node->next ); + CHECK( node != list.head ); CHECK( node == list.tail ); } //------------------------------------------------------------------------- // Test sll_pop_front function //------------------------------------------------------------------------- + TEST(Verify_pop_front_returns_null_if_list_is_null) + { + CHECK( NULL == sll_pop_front( NULL ) ); + } + + TEST(Verify_pop_front_returns_null_if_list_is_empty) + { + sll_t list = { NULL, NULL }; + CHECK( NULL == sll_pop_front( &list ) ); + } + + TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_1) + { + sll_node_t node1 = { NULL, NULL }; + sll_t list = { &node1, &node1 }; + CHECK( &node1 == sll_pop_front( &list ) ); + CHECK( NULL == list.head ); + CHECK( NULL == list.tail ); + } + + TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_2) + { + sll_node_t node2 = { NULL, NULL }; + sll_node_t node1 = { NULL, &node2 }; + sll_t list = { &node1, &node2 }; + CHECK( &node1 == sll_pop_front( &list ) ); + CHECK( &node2 == list.head ); + CHECK( &node2 == list.tail ); + } + //------------------------------------------------------------------------- // Test sll_pop_back function //------------------------------------------------------------------------- + TEST(Verify_pop_back_does_nothing_if_list_is_null) + { + CHECK( NULL == sll_pop_back( NULL ) ); + } + + TEST(Verify_pop_back_does_nothing_if_list_is_empty) + { + sll_t list = { NULL, NULL }; + CHECK( NULL == sll_pop_back( &list ) ); + } + + TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_1) + { + CHECK(false); + } + + TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_2) + { + CHECK(false); + } + //------------------------------------------------------------------------- // Test sll_insert function //------------------------------------------------------------------------- -- 2.52.0