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;
}
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);
//-------------------------------------------------------------------------
// 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);
}