Example #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 );
}
Example #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 );
}
Example #3
0
void testInserts() {
    // Create the list
    LList *list = newList( comparisonFunction );
    const int numElements = 100;
    int prevSize = list->size;

    // Insert a bunch of elements into the list
    for( int i = 1; i <= numElements; i++ ) {
        listInsert( list, mallocInt(i) );
        assertTrue( (prevSize + 1) == list->size, "List size should have increased by 1\n" );
        prevSize++;
    }

    // Assert that the right number of elements are present in the list
    assertTrue( numElements == list->size, "List should now have %d elements, has %d\n",
            numElements, list->size );

    // Check that the ordering invariant holds
    ListNode *current = list->head;
    ComparisonFunction compare = list->comparisonFunction;
    int numElementsCompared = 0;
    while( current->next != NULL ) {
        void *nodeData = current->data;
        void *nextNodeData = current->next->data;

        // Segfault prevention check
        if( nodeData != NULL && nextNodeData != NULL ) {
            int comparisonResult = compare(nodeData, nextNodeData);
            assertTrue( comparisonResult < 0, "Assert comparisonResult < 0 failed! (Was %d)\n",
                    comparisonResult );

            numElementsCompared++;
        }

        current = current->next;
    }

    // Ensure that we made some value comparisons throughout the the loop
    assertTrue( numElementsCompared != 0, "Should haven't compared more than 0 elements!\n" );

    // Test insert at beginning
    listInsert( list, mallocInt( -1 ) );

    // Test insert at end
    listInsert( list, mallocInt( 1000 ) );

    // Free the list
    listFree( list );
}
Example #4
0
int solve() {
	int *a;
	int i;
	int searchEle = 725;

	if((a = mallocInt(MAXSIZE)) == NULL) {
		return 1;
	}

	for(i = 0; i < MAXSIZE; i++) {
		a[i] = i+1;
	}

	// printIntList(a, MAXSIZE);

	printf("The element %d is at index %d\n", searchEle, mySearch(a, sizeof(int), MAXSIZE, &searchEle, myIntCompare));

	return 0;
}