]> git.mdlowis.com Git - projs/libcds.git/commitdiff
prev_node function & tests
authora bellenir <a@bellenir.com>
Wed, 30 Jul 2014 06:45:45 +0000 (06:45 +0000)
committera bellenir <a@bellenir.com>
Wed, 30 Jul 2014 06:45:45 +0000 (06:45 +0000)
source/list/list.c
source/list/list.h
tests/test_list.c

index 89182ab3f531c66923f8a3c414c477f051fa03e7..b2a0e92bbf84aa6735e08b9163d6746f7bcf2237 100644 (file)
@@ -49,6 +49,12 @@ bool list_empty(list_t* list)
     return ((NULL == list->head) && (NULL == list->tail));
 }
 
+list_node_t* list_prev(list_t* list, list_node_t* node){
+       list_node_t* prev = (NULL != list && NULL != node && list->head != node) ? list->head : NULL;
+       while(NULL != prev && prev->next != node) prev = prev->next;
+       return prev;
+}
+
 list_node_t* list_at(list_t* list, size_t index)
 {
     list_node_t* node = NULL;
@@ -67,8 +73,6 @@ list_node_t* list_at(list_t* list, size_t index)
     return node;
 }
 
-#include <stdio.h>
-
 list_node_t* list_push_front( list_t* list, void* contents )
 {
     list_node_t* node = list_new_node( contents );
index 42919fa78b1434b13b1026f098a0ec07a32aea90..3fc787ff6b8ed8e654788e67aa4f4a66ecab5ae8 100644 (file)
@@ -81,6 +81,17 @@ size_t list_size(list_t* list);
  */
 bool list_empty(list_t* list);
 
+/**
+ * @brief Find the node before the given one in the specified list.
+ *
+ * @param list          The list to search thru
+ * @param node          The node to search for
+ *
+ * @return Pointer to the node before the given node or
+ *         NULL if given node is NULL, not present, or is the head of list
+ */
+list_node_t* list_prev(list_t* list, list_node_t* node);
+
 /**
  * @brief   Return the node at the specified index in a linked list.
  *
index 9e1d78df278da18b8a787a98cc1a7e3901938482..d6168650c74a38d4efb18630c48f354a47cf3301 100644 (file)
@@ -117,6 +117,57 @@ TEST_SUITE(List) {
         CHECK( 0 == list_empty( &list ) );
     }
 
+    //-------------------------------------------------------------------------
+    // Test list_prev function
+    //-------------------------------------------------------------------------
+    TEST(Verify_list_prev_returns_NULL_when_list_is_empty)
+    {
+        list_t list = { NULL, NULL };
+        list_node_t bogus = { NULL, NULL };
+        CHECK( NULL == list_prev(&list, NULL) );
+        CHECK( NULL == list_prev(&list, &bogus) );
+    }
+
+    TEST(Verify_list_prev_returns_NULL_when_given_NULL)
+    {
+        list_node_t node3 = { NULL, NULL };
+        list_node_t node2 = { NULL, &node3 };
+        list_node_t node1 = { NULL, &node2 };
+        list_t list = { &node1, &node3 };
+        CHECK( NULL == list_prev(&list, NULL) );
+    }
+
+    TEST(Verify_list_prev_returns_NULL_when_given_node_not_present)
+    {
+        list_node_t node3 = { NULL, NULL };
+        list_node_t node2 = { NULL, &node3 };
+        list_node_t node1 = { NULL, &node2 };
+        list_node_t bogus = { NULL, &node2 };
+        list_t list = { &node1, &node3 };
+        CHECK( NULL == list_prev(&list, &bogus) );
+    }
+
+    TEST(Verify_list_prev_returns_NULL_when_given_node_at_head)
+    {
+        list_node_t node3 = { NULL, NULL };
+        list_node_t node2 = { NULL, &node3 };
+        list_node_t node1 = { NULL, &node2 };
+        list_t list = { &node1, &node3 };
+        CHECK( NULL == list_prev(&list, &node1) );
+    }
+
+    TEST(Verify_list_prev_returns_previous_node)
+    {
+        list_node_t node4 = { NULL, NULL };
+        list_node_t node3 = { NULL, &node4 };
+        list_node_t node2 = { NULL, &node3 };
+        list_node_t node1 = { NULL, &node2 };
+        list_t list = { &node1, &node3 };
+        CHECK( &node1 == list_prev(&list, &node2) );
+        CHECK( &node2 == list_prev(&list, &node3) );
+        CHECK( &node3 == list_prev(&list, &node4) );
+    }
+
     //-------------------------------------------------------------------------
     // Test list_at function
     //-------------------------------------------------------------------------