Exemplo n.º 1
0
//------------------------------------------------------------------------------------------------------------
void le_sls_Queue
(
    le_sls_List_t* listPtr,            ///< [IN] The list to add to.
    le_sls_Link_t* newLinkPtr          ///< [IN] The new link to add.
)
{
    if (listPtr->tailLinkPtr == NULL)
    {
        // Add to an empty list.
        newLinkPtr->nextPtr = newLinkPtr;

        listPtr->tailLinkPtr = newLinkPtr;
    }
    else
    {
        le_sls_AddAfter(listPtr, listPtr->tailLinkPtr, newLinkPtr);
    }
}
Exemplo n.º 2
0
//--------------------------------------------------------------------------------------------------
static dstr_Ref_t NewOrNextSegmentRef
(
    dstr_Ref_t headRef,    ///< [IN] The head of the string.
    dstr_Ref_t currentPtr  ///< [IN] The current sub-section of the string.
)
//--------------------------------------------------------------------------------------------------
{
    dstr_Ref_t segmentRef = NextSegmentRef(headRef, currentPtr);

    if (segmentRef != NULL)
    {
        return segmentRef;
    }

    segmentRef = NewSegment();
    le_sls_AddAfter(&headRef->head.list, &currentPtr->body.link, &segmentRef->body.link);

    return segmentRef;
}
Exemplo n.º 3
0
static le_result_t TestSinglyLinkLists(size_t maxListSize)
{
    // Node definition.
    typedef struct
    {
        le_sls_Link_t link;
        uint32_t id;  
    }
    idRecord_t;

    int i;
    le_sls_List_t list0, list1;

    printf("\n");
    printf("*** Unit Test for le_singlyLinkedList module. ***\n");

    //
    // Multiple list creation
    //
    // Initialize the lists
    list0 = LE_SLS_LIST_INIT;
    list1 = LE_SLS_LIST_INIT;
    printf("One singly linked list was successfully created.\n");


    //
    // Attempt to query empty list
    //
    if ( (le_sls_Peek(&list0) != NULL) || (le_sls_Pop(&list0) != NULL) )
    {
        printf("Query of empty list failed: %d", __LINE__);
        return LE_FAULT;
    }
    printf("Query of empty list correct.\n");


    //
    // Node insertions
    //
    {
        idRecord_t* newNodePtr;
        le_sls_Link_t* prevLinkPtr;
        
        // Queue nodes to list0.
        for (i = 0; i < maxListSize; i++)
        {
            // Create the new node
            newNodePtr = (idRecord_t*)malloc(sizeof(idRecord_t));
            newNodePtr->id = i;
            
            // Initialize the link.
            newNodePtr->link = LE_SLS_LINK_INIT;

            if (i < maxListSize/2)
            {
                // Insert the new node to the tail.
                le_sls_Queue(&list0, &(newNodePtr->link));
            }
            else
            {
                // Insert to the tail using insert after.
                le_sls_AddAfter(&list0, prevLinkPtr, &(newNodePtr->link));
            }

            prevLinkPtr = &(newNodePtr->link);
        }
        printf("%zu nodes were queued to the tail of list0.\n", maxListSize);
        
        // Stack nodes to list1.
        for (i = 0; i < maxListSize; i++)
        {
            // Create the new node
            newNodePtr = (idRecord_t*)malloc(sizeof(idRecord_t));
            newNodePtr->id = i;
            
            // Initialize the link.
            newNodePtr->link = LE_SLS_LINK_INIT;

            // Insert the new node to the head.
            le_sls_Stack(&list1, &(newNodePtr->link));
        }
        printf("%zu nodes were stacked to the head of list1.\n", maxListSize);
    }

    //
    // Check that all the nodes have been added properly
    //
    {
        idRecord_t* nodePtr;
        le_sls_Link_t* link0Ptr = le_sls_Peek(&list0);
        le_sls_Link_t* link1Ptr = le_sls_Peek(&list1);
        
        if ( (link0Ptr == NULL) || (link1Ptr == NULL) )
        {
            printf("Link error: %d", __LINE__);
            return LE_FAULT;
        }
        
        i = 0;
        do
        {
            // Get the node from list 0
            nodePtr = CONTAINER_OF(link0Ptr, idRecord_t, link);

            // Check the node.
            if ( nodePtr->id != i)
            {
                printf("Link error: %d", __LINE__);
                return LE_FAULT;
            }

            // Get the node from list 1
            nodePtr = CONTAINER_OF(link1Ptr, idRecord_t, link);

            // Check the node.
            if ( nodePtr->id != maxListSize - i - 1)
            {
                printf("Link error: %d", __LINE__);
                return LE_FAULT;
            }

            // Move to the next node.
            link0Ptr = le_sls_PeekNext(&list0, link0Ptr);
            link1Ptr = le_sls_PeekNext(&list1, link1Ptr);
            i++;

        } while (link0Ptr != NULL);
        
        // Make sure everything is correct.
        if ( (i != maxListSize) || (link1Ptr != NULL) )
        {
            printf("Link error: %d", __LINE__);
            return LE_FAULT;
        }
    }
    
    printf("Checked that all nodes added to the head and tails are all correct.\n");
   

    //
    // Pop nodes.
    //
    {
        //pop half the list.
        for (i = 0; i < (maxListSize / 2); i++)
        {
            le_sls_Pop(&list0);
        }
    }
    
    printf("Popped half the nodes from the head of list0.\n");
    
    // Check that the list is still in tact.
    {
        idRecord_t* nodePtr;
        
        // For list 0.
        le_sls_Link_t* linkPtr = le_sls_Peek(&list0);    
        i = maxListSize/2;
        do
        {
            nodePtr = CONTAINER_OF(linkPtr, idRecord_t, link);

            if (nodePtr->id != i++)
            {
                printf("Link error: %d", __LINE__);
                return LE_FAULT;
            }
            
            linkPtr = le_sls_PeekNext(&list0, linkPtr);
        } while (linkPtr != NULL);
        
        // Check that the number of links left is correct.
        if (i != maxListSize)
        {
            printf("Wrong number of links: %d", __LINE__);
            return LE_FAULT;
        }
    }
    printf("Checked that all nodes were properly popped from the lists.\n");


    //
    // Check for list corruption.
    //
    {
        le_sls_Link_t* linkPtr;
        
        if (le_sls_IsListCorrupted(&list0))
        {
            printf("List0 is corrupt but shouldn't be: %d", __LINE__);
            return LE_FAULT;
        }

        // Access one of the links directly.  This should corrupt the list.
        linkPtr = le_sls_Peek(&list0);
        linkPtr = le_sls_PeekNext(&list0, linkPtr);
        linkPtr->nextPtr = NULL;

        if (!le_sls_IsListCorrupted(&list0))
        {
            printf("List0 is not corrupted but should be: %d", __LINE__);
            return LE_FAULT;
        }
    }

    printf("Checked lists for corruption.\n");
    
    printf("*** Unit Test for le_singlyLinkedList module passed. ***\n");
    printf("\n");
    return LE_OK;
}