test/list/test_list_insert_head.cpp
TEST(double_insert)
{
    allocator* alloc = nullptr;
    list* l = nullptr;

    /* we should be able to create a malloc allocator. */
    TEST_ASSERT(
        STATUS_SUCCESS == malloc_allocator_create(&alloc));

    /* we should be able to create a list. */
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_create(
                &l, alloc));

    /* we should be able to get the head. */
    list_node* head = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_head(&head, l));

    /* the head should be null. */
    TEST_ASSERT(nullptr == head);

    /* we should be able to get the tail. */
    list_node* tail = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_tail(&tail, l));

    /* the tail should be null. */
    TEST_ASSERT(nullptr == tail);

    /* create the first resource to insert into the list. */
    allocator* other1 = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == malloc_allocator_create(&other1));

    /* insert this into the list. */
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_insert_head(l, allocator_resource_handle(other1)));

    /* create the second resource to insert into the list. */
    allocator* other2 = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == malloc_allocator_create(&other2));

    /* insert this into the list. */
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_insert_head(l, allocator_resource_handle(other2)));

    /* get the head / tail. */
    TEST_ASSERT(STATUS_SUCCESS == list_head(&head, l));
    TEST_ASSERT(STATUS_SUCCESS == list_tail(&tail, l));

    /* The head and tail should exist, and they should NOT be equal. */
    TEST_EXPECT(nullptr != head && nullptr != tail && head != tail);

    /* get the resource associated with the head. */
    resource* r1 = nullptr;
    TEST_ASSERT(STATUS_SUCCESS == list_node_child(&r1, head));

    /* this resource should be the same as other2. */
    TEST_EXPECT(r1 == allocator_resource_handle(other2));

    /* get the resource associated with the tail. */
    resource* r2 = nullptr;
    TEST_ASSERT(STATUS_SUCCESS == list_node_child(&r2, tail));

    /* this resource should be the same as other1. */
    TEST_EXPECT(r2 == allocator_resource_handle(other1));

    /* get the next for head. */
    list_node* head_next = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == list_node_next(&head_next, head));

    /* head->next should be tail. */
    TEST_ASSERT(tail == head_next);

    /* get the prev for head. */
    list_node* head_prev = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == list_node_prev(&head_prev, head));

    /* head->prev should be NULL. */
    TEST_ASSERT(nullptr == head_prev);

    /* get the next for tail. */
    list_node* tail_next = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == list_node_next(&tail_next, tail));

    /* tail->next should be null. */
    TEST_ASSERT(nullptr == tail_next);

    /* get the prev for tail. */
    list_node* tail_prev = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == list_node_prev(&tail_prev, tail));

    /* tail->prev should be head. */
    TEST_ASSERT(head == tail_prev);

    /* we should be able to release the list instance. */
    TEST_ASSERT(
        STATUS_SUCCESS ==
            resource_release(list_resource_handle(l)));

    /* clean up. */
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(allocator_resource_handle(alloc)));
}
/**
 * Verify that we can insert two values into the list, and that the second value
 * becomes the new head.
 */
Verify that we can insert two values into the list, and that the second value
becomes the new head.
Verify that we can insert two values into the list and that the second value becomes the new head .
TEST(double_insert)
{
    allocator* alloc = nullptr;
    list* l = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == malloc_allocator_create(&alloc));
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_create(
                &l, alloc));
    list_node* head = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_head(&head, l));
    TEST_ASSERT(nullptr == head);
    list_node* tail = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_tail(&tail, l));
    TEST_ASSERT(nullptr == tail);
    allocator* other1 = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == malloc_allocator_create(&other1));
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_insert_head(l, allocator_resource_handle(other1)));
    allocator* other2 = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == malloc_allocator_create(&other2));
    TEST_ASSERT(
        STATUS_SUCCESS ==
            list_insert_head(l, allocator_resource_handle(other2)));
    TEST_ASSERT(STATUS_SUCCESS == list_head(&head, l));
    TEST_ASSERT(STATUS_SUCCESS == list_tail(&tail, l));
    TEST_EXPECT(nullptr != head && nullptr != tail && head != tail);
    resource* r1 = nullptr;
    TEST_ASSERT(STATUS_SUCCESS == list_node_child(&r1, head));
    TEST_EXPECT(r1 == allocator_resource_handle(other2));
    resource* r2 = nullptr;
    TEST_ASSERT(STATUS_SUCCESS == list_node_child(&r2, tail));
    TEST_EXPECT(r2 == allocator_resource_handle(other1));
    list_node* head_next = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == list_node_next(&head_next, head));
    TEST_ASSERT(tail == head_next);
    list_node* head_prev = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == list_node_prev(&head_prev, head));
    TEST_ASSERT(nullptr == head_prev);
    list_node* tail_next = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == list_node_next(&tail_next, tail));
    TEST_ASSERT(nullptr == tail_next);
    list_node* tail_prev = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == list_node_prev(&tail_prev, tail));
    TEST_ASSERT(head == tail_prev);
    TEST_ASSERT(
        STATUS_SUCCESS ==
            resource_release(list_resource_handle(l)));
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(allocator_resource_handle(alloc)));
}
TEST ( double_insert ) { allocator * alloc = nullptr ; list * l = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == malloc_allocator_create ( & alloc ) ) ; TEST_ASSERT ( STATUS_SUCCESS == list_create ( & l , alloc ) ) ; list_node * head = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == list_head ( & head , l ) ) ; TEST_ASSERT ( nullptr == head ) ; list_node * tail = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == list_tail ( & tail , l ) ) ; TEST_ASSERT ( nullptr == tail ) ; allocator * other1 = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == malloc_allocator_create ( & other1 ) ) ; TEST_ASSERT ( STATUS_SUCCESS == list_insert_head ( l , allocator_resource_handle ( other1 ) ) ) ; allocator * other2 = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == malloc_allocator_create ( & other2 ) ) ; TEST_ASSERT ( STATUS_SUCCESS == list_insert_head ( l , allocator_resource_handle ( other2 ) ) ) ; TEST_ASSERT ( STATUS_SUCCESS == list_head ( & head , l ) ) ; TEST_ASSERT ( STATUS_SUCCESS == list_tail ( & tail , l ) ) ; TEST_EXPECT ( nullptr != head && nullptr != tail && head != tail ) ; resource * r1 = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == list_node_child ( & r1 , head ) ) ; TEST_EXPECT ( r1 == allocator_resource_handle ( other2 ) ) ; resource * r2 = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == list_node_child ( & r2 , tail ) ) ; TEST_EXPECT ( r2 == allocator_resource_handle ( other1 ) ) ; list_node * head_next = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == list_node_next ( & head_next , head ) ) ; TEST_ASSERT ( tail == head_next ) ; list_node * head_prev = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == list_node_prev ( & head_prev , head ) ) ; TEST_ASSERT ( nullptr == head_prev ) ; list_node * tail_next = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == list_node_next ( & tail_next , tail ) ) ; TEST_ASSERT ( nullptr == tail_next ) ; list_node * tail_prev = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == list_node_prev ( & tail_prev , tail ) ) ; TEST_ASSERT ( head == tail_prev ) ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( list_resource_handle ( l ) ) ) ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( allocator_resource_handle ( alloc ) ) ) ; }
Verify that we can insert two values into the list, and that the second value
 becomes the new head.
Verify that we can insert two values into the list and that the second value becomes the new head .
/* we should be able to create a malloc allocator. */ /* we should be able to create a list. */ /* we should be able to get the head. */ /* the head should be null. */ /* we should be able to get the tail. */ /* the tail should be null. */ /* create the first resource to insert into the list. */ /* insert this into the list. */ /* create the second resource to insert into the list. */ /* insert this into the list. */ /* get the head / tail. */ /* The head and tail should exist, and they should NOT be equal. */ /* get the resource associated with the head. */ /* this resource should be the same as other2. */ /* get the resource associated with the tail. */ /* this resource should be the same as other1. */ /* get the next for head. */ /* head->next should be tail. */ /* get the prev for head. */ /* head->prev should be NULL. */ /* get the next for tail. */ /* tail->next should be null. */ /* get the prev for tail. */ /* tail->prev should be head. */ /* we should be able to release the list instance. */ /* clean up. */