]> git.mdlowis.com Git - projs/libcds.git/commitdiff
fixing up list_insert_after; handle NULL for node as pushing to front, fix comments...
authora bellenir <a@bellenir.com>
Thu, 31 Jul 2014 23:53:38 +0000 (23:53 +0000)
committera bellenir <a@bellenir.com>
Thu, 31 Jul 2014 23:53:38 +0000 (23:53 +0000)
source/list/list.c
source/list/list.h
tests/test_list.c

index 4adcdb4b488b62662480deef3b931162f4b37b70..4df9a381f5e09b4f09bce75359a7721ceca07df9 100644 (file)
@@ -183,16 +183,20 @@ list_node_t* list_insert( list_t* list, size_t index, void* contents)
 
 list_node_t* list_insert_after( list_t* list, list_node_t* node, void* contents)
 {
-    list_node_t* new_node = NULL;
-    if (node != NULL)
+    list_node_t* new_node = list_new_node(contents);
+    if(NULL != node)
     {
-        new_node = list_new_node(contents);
         new_node->next = node->next;
         node->next = new_node;
-        if (node == list->tail)
-        {
-            list->tail = new_node;
-        }
+    }
+    else
+    {
+        new_node->next = list->head;
+        list->head = new_node;
+    }
+    if (node == list->tail)
+    {
+        list->tail = new_node;
     }
     return new_node;
 }
index 36d8cbb5b339cee58f50cdcf7a8ddce420bcbb58..a55d662bcea5237b8e9fb7fb14211ad85b315e33 100644 (file)
@@ -186,17 +186,16 @@ list_node_t* list_pop_back( list_t* list );
 list_node_t* list_insert( list_t* list, size_t index, void* contents);
 
 /**
- * @brief Inserts a new node in a linked list at the specified index.
+ * @brief Inserts a new node in a linked list after the specified node
  *
- * This function traverses the list to the desired index and inserts a new node
- * with the given contents at that position. The node previously at the desired
- * index becomes the child of the new node.
+ * This function traverses the list to the specified and inserts a new node after
+ * it with the given contents at that position.
  *
  * @param list     The list to operate on.
  * @param node     The node after which the item should be inserted.
  * @param contents The contents of the new node.
  *
- * @return Pointer to the newly inserted node, NULL if index is out of range.
+ * @return Pointer to the newly inserted node
  **/
 list_node_t* list_insert_after( list_t* list, list_node_t* node, void* contents);
 
index 483d3efdf924c99de3284dea02971b97e5c5d870..382fe0fd70304099421992f807ebb0a06ce8fde7 100644 (file)
@@ -480,11 +480,52 @@ TEST_SUITE(List) {
     //-------------------------------------------------------------------------
     // Test list_insert_after function
     //-------------------------------------------------------------------------
-    TEST(Verify_insert_after_should_fail_to_insert_if_node_is_null)
+    TEST(Verify_insert_after_should_to_insert_to_head_if_node_is_null_and_list_is_empty)
     {
         list_t* list = list_new();
-        list_node_t* node = list_insert_after( list, NULL, mem_box(0x1234) );
-        CHECK( node == NULL );
+        list_node_t* node = list_insert_after(list, NULL, mem_box(0x1234) );
+        CHECK( node == list->head );
+        CHECK( node == list->tail );
+        CHECK( NULL == node->next );
+        mem_release(list);
+    }
+
+    TEST(Verify_insert_after_should_insert_to_head_if_node_is_null_and_list_is_populated)
+    {
+        list_t* list = list_new();
+        list_node_t* node1 = list_insert_after(list, NULL, mem_box(0x0666));
+        list_node_t* node2 = list_insert_after(list, NULL, mem_box(0x4242));
+        CHECK( node2 == list->head );
+        CHECK( node1 == list->tail );
+        CHECK( node1 == node2->next );
+        CHECK( NULL == node1->next );
+        list_node_t* node3 = list_insert_after(list, NULL, mem_box(0x1234));
+        CHECK( node3 == list->head );
+        CHECK( node1 == list->tail );
+        CHECK( node2 == node3->next );
+        CHECK( node1 == node2->next );
+        CHECK( NULL == node1->next );
+        mem_release(list);
+    }
+
+    TEST(Verify_insert_after_can_build_list_linearly)
+    {
+        list_t* list = list_new();
+        list_node_t* node1 = list_insert_after(list, list->tail, mem_box(0x1234));
+        CHECK( node1 == list->head );
+        CHECK( node1 == list->tail );
+        CHECK( NULL == node1->next );
+        list_node_t* node2 = list_insert_after(list, list->tail, mem_box(0x4321));
+        CHECK( node1 == list->head );
+        CHECK( node2 == list->tail );
+        CHECK( node2 == node1->next );
+        CHECK( NULL == node2->next );
+        list_node_t* node3 = list_insert_after(list, list->tail, mem_box(0x4242));
+        CHECK( node1 == list->head );
+        CHECK( node3 == list->tail );
+        CHECK( node2 == node1->next );
+        CHECK( node3 == node2->next );
+        CHECK( NULL == node3->next );
         mem_release(list);
     }