From 498d9ccbf733127e612e882301b13902aedfc335 Mon Sep 17 00:00:00 2001 From: a bellenir Date: Wed, 30 Jul 2014 06:45:45 +0000 Subject: [PATCH] prev_node function & tests --- source/list/list.c | 8 ++++++-- source/list/list.h | 11 ++++++++++ tests/test_list.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/source/list/list.c b/source/list/list.c index 89182ab..b2a0e92 100644 --- a/source/list/list.c +++ b/source/list/list.c @@ -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 - list_node_t* list_push_front( list_t* list, void* contents ) { list_node_t* node = list_new_node( contents ); diff --git a/source/list/list.h b/source/list/list.h index 42919fa..3fc787f 100644 --- a/source/list/list.h +++ b/source/list/list.h @@ -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. * diff --git a/tests/test_list.c b/tests/test_list.c index 9e1d78d..d616865 100644 --- a/tests/test_list.c +++ b/tests/test_list.c @@ -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 //------------------------------------------------------------------------- -- 2.52.0