int main(int argc, char * argv[]) { int counter1 = 0; int hashValue; int sizeOfList = 102; int * sizePtr = &sizeOfList; bucket * root; //bucket * conductor; char ** wordList; char * toBeHashed; char userInput[BUFSIZE]; FILE* inputFile; if (argc != 2) { printf("Please input a file name and nothing else.\n"); exit(0); } inputFile = fopen(argv[1], "r"); if (inputFile == NULL) { printf("The file '%s' does not exist. Quiting.\n", argv[1]); exit(0); } wordList = readFile(inputFile); fclose(inputFile); root = initLinkedList(sizePtr); if(root == NULL) { printf("Error creating linked list, quiting...\n"); exit(0); } while (wordList[counter1] != NULL) { hashValue = findHash(wordList[counter1]); addHash(hashValue, wordList[counter1], root); counter1++; } free(wordList); printf("List successfully hashed! Ready for queries: "); while (1==1) { fgets(userInput, BUFSIZE, stdin); toBeHashed = malloc(strlen(userInput)+1); strcpy(toBeHashed, userInput); hashValue = findHash(toBeHashed); queryDatabase(hashValue, root, toBeHashed); printf("Ready for queries: "); } return 0; }
void test_listRemoveTail_given_list_of_0x100_0x200_0x300_should_return_0x300() { LinkedList list ; ListElement listElement1 ; ListElement listElement2; ListElement listElement3; ListElement *returnElement; initLinkedList(&list); listElementCreate(&listElement1,0x100); listElementCreate(&listElement2,0x200); listElementCreate(&listElement3,0x300); listAddTail(&list,&listElement1); listAddTail(&list,&listElement2); listAddTail(&list,&listElement3); returnElement = listRemoveTail(&list); TEST_ASSERT_EQUAL(&listElement3,returnElement); TEST_ASSERT_EQUAL(0x300,returnElement->data); TEST_ASSERT_EQUAL(NULL,returnElement->next); TEST_ASSERT_EQUAL(&listElement1,list.head); TEST_ASSERT_EQUAL(&listElement2,list.tail); }
void testMultiThreadedAdd() { printf("\n\nShould see messages in the correct order\n\n"); int num_producers = 2; int num_consumers = 4; linked_list* list = initLinkedList(); pthread_t* producers = malloc(sizeof(pthread_t) * num_producers); for (int i =0; i < num_producers; i++) { pthread_create(&(producers[i]), NULL, testProducer, list); } pthread_t* consumers = malloc(sizeof(pthread_t) * num_consumers); for (int i =0; i < num_consumers; i++) { pthread_create(&(consumers[i]), NULL, testConsumer, list); } for (int i =0; i < num_producers; i++) { pthread_join(producers[i], NULL); } for (int i =0; i < num_consumers; i++) { pthread_join(consumers[i], NULL); } free(producers); free(consumers); freeLinkedList(list); }
void test_listAddTail_0x500_given_list_contains_0x100_0x200_0x300() { LinkedList list ; ListElement listElement1 ; ListElement listElement2; ListElement listElement3; ListElement listElement4; initLinkedList(&list); listElementCreate(&listElement1,0x100); listElementCreate(&listElement2,0x200); listElementCreate(&listElement3,0x300); listElementCreate(&listElement4,0x500); listAddHead(&list,&listElement3); listAddHead(&list,&listElement2); listAddHead(&list,&listElement1); TEST_ASSERT_EQUAL(&listElement1,list.head); TEST_ASSERT_EQUAL(&listElement3,list.tail); TEST_ASSERT_EQUAL(0x100,list.head->data); TEST_ASSERT_EQUAL(0x200,(list.head)->next->data); TEST_ASSERT_EQUAL(0x300,(list.head)->next->next->data); listAddTail(&list,&listElement4); TEST_ASSERT_EQUAL(&listElement4,list.tail); TEST_ASSERT_EQUAL(0x500,list.tail->data); TEST_ASSERT_EQUAL(NULL,list.tail->next); }
/*--------listRemoveTail---------*/ void_test_listRemoveTail_given_emptyList_should_return_NULL() { LinkedList list ; ListElement *listElement ; initLinkedList(&list); listElement = listRemoveTail(&list); TEST_ASSERT_NULL(listElement); }
void test_initLinkedList_should_set_head_and_tail_to_NULL() { LinkedList list ; initLinkedList(&list); TEST_ASSERT_NULL(list.head); TEST_ASSERT_NULL(list.tail); }
void testApplyFunc() { int vals[] = {1,2,3,4,5}; linked_list* list = initLinkedList(); for (int i = 0; i < sizeof(vals) / sizeof(vals[0]); i++) { addToTail(list, &(vals[i])); } printf("Should see :\n"); for (int i = 0; i < sizeof(vals) / sizeof(vals[0]); i++) { printf("%d, ", vals[i]); } printf("\nActual:\n"); applyFunc(list, intPrinter); printf("\n"); freeLinkedList(list); }
/* * Function: * readEntryFile2LinkedList * * Description: * Reads the entries from a file and creates a linked list with * these entries in the inverse order. * * Arguments: * Pointer to the file stream: * FILE * fp * * Return value: * Pointer to the first node of the linked list. */ LinkedList * readEntryFile2LinkedList(FILE * fp, int parametro, int sentido) /*falta receber o ascendente e o tipo de ordenacao*/ { int id; int age; int height; int weight; LinkedList * lp; Entry * entry; /* Initialize linked list */ lp = initLinkedList(); /* Cycle through file rows */ while(fscanf(fp, "%d %d %d %d", &id, &age, &height, &weight) == 4) { /* Create new entry */ entry = newEntry(id, age, height, weight); /* Store entry in the linked list */ if( parametro == ID ){ if( sentido == ASCENDENTE ) lp = insertSortedLinkedList(lp,(Item) entry, &comparisonIdAsc); else lp = insertSortedLinkedList(lp,(Item) entry, &comparisonIdDesc); } else if( parametro == AGE ){ if( sentido == ASCENDENTE ) lp = insertSortedLinkedList(lp,(Item) entry, &comparisonAgeAsc); else lp = insertSortedLinkedList(lp,(Item) entry, &comparisonAgeDesc); } else if( parametro == HEIGHT ){ if( sentido == ASCENDENTE ) lp = insertSortedLinkedList(lp,(Item) entry, &comparisonHeightAsc); else lp = insertSortedLinkedList(lp,(Item) entry, &comparisonHeightDesc); } else { if( sentido == ASCENDENTE ) lp = insertSortedLinkedList(lp,(Item) entry, &comparisonWeightAsc); else lp = insertSortedLinkedList(lp,(Item) entry, &comparisonWeightDesc); } } return lp; }
int main( int argc, char* argv[] ) { srand( time(NULL) ); LinkedList* list = initLinkedList(); int i; for ( i = 0; i < 100; i++ ) append( list, 'a' + rand() % 26 ); printList( list ); destroyList( list ); return 0; }
void test_listAddTail_given_emptyList() { LinkedList list ; ListElement listElement ; initLinkedList(&list); listElementCreate(&listElement,0x100); listAddTail(&list,&listElement); TEST_ASSERT_EQUAL(&listElement,list.head); TEST_ASSERT_EQUAL(&listElement,list.tail); TEST_ASSERT_EQUAL(0x100,list.head->data); TEST_ASSERT_EQUAL(NULL,list.head->next); TEST_ASSERT_EQUAL(0x100,list.tail->data); TEST_ASSERT_EQUAL(NULL,list.tail->next); }
void test_listAddHead_0x200_given_head_contain_0x100() { LinkedList list ; ListElement listElement1 ; ListElement listElement2; initLinkedList(&list); listElementCreate(&listElement1,0x100); listElementCreate(&listElement2,0x200); listAddHead(&list,&listElement1); listAddHead(&list,&listElement2); TEST_ASSERT_EQUAL(&listElement2,list.head); TEST_ASSERT_EQUAL(&listElement1,list.tail); TEST_ASSERT_EQUAL(0x200,list.head->data); TEST_ASSERT_EQUAL(&listElement1,list.head->next); }
void basicTest() { CASE cases[] = { {ADD_FRONT, 2, SUCCESS}, {ADD_FRONT, 1, SUCCESS}, {ADD_FRONT, 0, SUCCESS}, {ADD_BACK, 3, SUCCESS}, {ADD_BACK, 4, SUCCESS}, {SIZE, 0, 5}, {REMOVE_FRONT, 0, 0}, {REMOVE_FRONT, 0, 1}, {REMOVE_BACK, 0, 4}, {SIZE, 0, 2}, {REMOVE_BACK, 0, 3}, {REMOVE_BACK, 0, 2}, {SIZE, 0, 0}, }; linked_list* list = initLinkedList(); for (int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { if (runTestCase(&(cases[i]), list)) { printf("failure on case: %d\n", i); } } freeLinkedList(list); }
void test_init_linkedlist(){ linkedList list; initLinkedList(&list); TEST_ASSERT_NULL(list.head); TEST_ASSERT_NULL(list.tail); }
void test_add_last_with_head_and_tail_is_NULL(){ linkedList list; Element elementOne = {.next = NULL , .data = 10}; initLinkedList(&list); List_addLast(&list,&elementOne); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementOne,list.tail); } void test_add_last_with_head_is_point_to_elementOne_and_tail_point_to_elementTwo(){ linkedList list; Element elementOne = {.next = NULL , .data = 10}; Element elementTwo = {.next = NULL , .data = 20}; initLinkedList(&list); List_addLast(&list,&elementOne); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementOne,list.tail); List_addLast(&list,&elementTwo); TEST_ASSERT_EQUAL(2,list.length); TEST_ASSERT_EQUAL(20,elementTwo.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementTwo,list.tail); } void test_add_last_with_head_is_point_to_elementOne_and_tail_point_to_elementTThree(){ linkedList list; Element elementOne = {.next = NULL , .data = 10}; Element elementTwo = {.next = NULL , .data = 20}; Element elementThree = {.next = NULL , .data = 70}; initLinkedList(&list); List_addLast(&list,&elementOne); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementOne,list.tail); TEST_ASSERT_EQUAL(NULL,elementOne.next); List_addLast(&list,&elementTwo); TEST_ASSERT_EQUAL(2,list.length); TEST_ASSERT_EQUAL(20,elementTwo.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementTwo,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); TEST_ASSERT_EQUAL(NULL,elementTwo.next); List_addLast(&list,&elementThree); TEST_ASSERT_EQUAL(3,list.length); TEST_ASSERT_EQUAL(70,elementThree.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementThree,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); TEST_ASSERT_EQUAL(&elementThree,elementTwo.next); TEST_ASSERT_EQUAL(NULL,elementThree.next); } void test_remove_first_with_head_is_point_to_elementOne_and_tail_point_to_elementThree(){ linkedList list; Element elementOne = {.next = NULL , .data = 10}; Element elementTwo = {.next = NULL , .data = 20}; Element elementThree = {.next = NULL , .data = 70}; initLinkedList(&list); List_addLast(&list,&elementOne); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementOne,list.tail); TEST_ASSERT_EQUAL(NULL,elementOne.next); List_addLast(&list,&elementTwo); TEST_ASSERT_EQUAL(2,list.length); TEST_ASSERT_EQUAL(20,elementTwo.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementTwo,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); TEST_ASSERT_EQUAL(NULL,elementTwo.next); List_addLast(&list,&elementThree); TEST_ASSERT_EQUAL(3,list.length); TEST_ASSERT_EQUAL(70,elementThree.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementThree,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); TEST_ASSERT_EQUAL(&elementThree,elementTwo.next); TEST_ASSERT_EQUAL(NULL,elementThree.next); List_removeFirst(&list); TEST_ASSERT_EQUAL(2,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementTwo,list.head); TEST_ASSERT_EQUAL(&elementThree,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); List_removeFirst(&list); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(20,elementTwo.data); TEST_ASSERT_EQUAL(&elementThree,list.head); TEST_ASSERT_EQUAL(&elementThree,list.tail); List_removeFirst(&list); TEST_ASSERT_EQUAL(0,list.length); TEST_ASSERT_EQUAL(70,elementThree.data); TEST_ASSERT_EQUAL(NULL,list.head); TEST_ASSERT_EQUAL(NULL,list.tail); }