LinkedListNode *LinkedList_Insert( LinkedList *list, size_t pos, void *data ) { LinkedListNode *node; node = malloc( sizeof( LinkedListNode ) ); node->data = data; LinkedList_InsertNode( list, pos, node ); return node; }
void LinkedListSuite_TestReturnArray( LinkedListSuiteData* data ) { int* numArray; Index ii = 0; for(ii=0; ii<NUM_DATA; ii++){ LinkedList_InsertNode(data->numList, data->array[ii], sizeof(int)); } /* \nRetrieving the data held in the list as an array\n */ numArray = LinkedList_ReturnArray(data->numList, int); for(ii=0; ii<data->numList->nodeCount; ii++){ pcu_check_true(numArray[ii] == (NUM_DATA-1 - *data->array[ii])); } Memory_Free( numArray ); }
void LinkedListSuite_TestFindNodeData( LinkedListSuiteData* data ) { int* result = NULL; Index ii = 0; int secondArray[NUM_DATA]; for(ii=0; ii<NUM_DATA; ii++){ LinkedList_InsertNode(data->numList, data->array[ii], sizeof(int)); secondArray[ii] = *data->array[ii]; } /* \nSearching for Node data in the list\n */ /* Deliberately search with ptrs from a different array: want the ptrs to be different, thus checking using the * compare function if the data itself is the same */ for(ii=0; ii<NUM_DATA/4; ii++) { result = LinkedList_FindNodeData(data->numList, &secondArray[ii], int); pcu_check_true( result != NULL ); pcu_check_true( *result == *data->array[ii] ); } }
void LinkedListSuite_TestInsert( LinkedListSuiteData* data ) { Index ii = 0; LinkedListNode* currNode=NULL; /* Inserting data into the List\n */ for(ii=0; ii<NUM_DATA; ii++){ LinkedList_InsertNode(data->numList, data->array[ii], sizeof(int)); } pcu_check_true( data->numList->nodeCount == NUM_DATA ); currNode = data->numList->head; /* Note: since current implementation inserts new linked list nodes at the head, rather than the tail, the * list nodes will be in reverse order. This probably should be changed to insert at the tail, but I won't * do this just in case some other code is relying on this reversed order. --PatrickSunter, 31 May 2009 */ for(ii=0; ii<NUM_DATA; ii++){ pcu_check_true( *((int*)currNode->data) == (NUM_DATA-1 - *data->array[ii]) ); currNode = currNode->next; } }
void LinkedListSuite_TestDelete( LinkedListSuiteData* data ) { Index ii = 0; LinkedListNode* currNode=NULL; for(ii=0; ii<NUM_DATA; ii++){ LinkedList_InsertNode(data->numList, data->array[ii], sizeof(int)); } /* \nDeleting half the nodes previously inserted into the list\n */ for(ii=0; ii<NUM_DATA/2; ii++){ LinkedList_DeleteNode(data->numList, data->array[ii]); } pcu_check_true( data->numList->nodeCount == NUM_DATA/2 ); currNode = data->numList->head; /* Since they end up in reverse order, the deleted notes should be the 2nd half of the list, so the first half * should have remained unchanged */ for(ii=0; ii<NUM_DATA/2; ii++){ pcu_check_true( *((int*)currNode->data) == (NUM_DATA-1 - *data->array[ii]) ); currNode = currNode->next; } }