]> git.mdlowis.com Git - projs/libcds.git/commitdiff
Added tests for singly linked list
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 5 Jun 2012 06:40:25 +0000 (02:40 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 5 Jun 2012 06:40:25 +0000 (02:40 -0400)
source/lists/single_link/sll.c
source/lists/single_link/sll.h
tests/test_sll.cpp

index 4c00dd5438c4305f985cacbdc0d9630005c846ef..a8940c55f53514e8ef2f554205253694a3278d96 100644 (file)
@@ -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)
index a16b954487087be2934ddf8f580a36f41bdf6b42..65d5d9594e0060fce1b0bc6315e417bf42893ebe 100644 (file)
@@ -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.
index fade5cd6448219b6fa702ff93a658ff95d6d577c..6a91b96c101ca8069b0d6c4749e246461aef5b12 100644 (file)
@@ -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
     //-------------------------------------------------------------------------