Beispiel #1
0
void testRemoval() {
    LList *list = newList( comparisonFunction );
    const int numElements = 1000;

    // Insert elements
    for( int i = 0; i < numElements; i++ ) {
        listInsert( list, mallocInt(i) );
    }

    // Remove the elements and ensure that the right elements were removed
    for( int i = 0; i < numElements; i++ ) {
        int *elementToRemove = mallocInt(i);
        int *removedElement = listRemove( list, elementToRemove );

        assertNotNull( removedElement, "removedElement is null!\n" );

        int comparisonResult = list->comparisonFunction( elementToRemove, removedElement );
        assertTrue( comparisonResult == 0, "compare(%d, %d) != 0\n", *elementToRemove,
                *removedElement );

        free( elementToRemove );
        free( removedElement );
    }

    // Free the list
    listFree( list );
}
Beispiel #2
0
void testListFind() {
    LList *list = newList( comparisonFunction );
    const int numElements = 1000;

    // Insert the elements
    for( int i = 0; i < numElements; i++ ) {
        listInsert( list, mallocInt(i) );
    }

    // Find the elements
    for( int i = 0; i < numElements; i++ ) {
        int *intToFind = mallocInt(i);

        // Ensure that the find didn't return null
        ListNode *findResult = listFind( list, intToFind );
        assertNotNull( findResult, "find(i) should not be NULL!\n" );

        // Ensure that the elements are equal
        int comparisonResult = list->comparisonFunction( intToFind, findResult->data );
        assertTrue( comparisonResult == 0, "find(%d)->data != %d\n", i, i );

        free( intToFind );
    }

    // Free the list
    listFree( list );
}