📄 hashtable_test.c
字号:
data1->keyval = 5; ASSERT(hashtable_find_and_remove(table, hashfunc_valid, data1, cmpfunc_valid) == TRUE); /* M16 */ printf("Test case M16: Use iterator on an empty hashtable\n"); ASSERT(hashtable_iterator(table, iterfunc_print, NULL) == TRUE); printf("M16 test cases (1) OK\n"); /* M17 */ printf("Test case M17: Use iterator on a non-empty hashtable\n"); ASSERT(hashtable_add(table, hashfunc_valid, data1, &data1->node) == TRUE); ASSERT(hashtable_add(table, hashfunc_valid, data2, &data2->node) == TRUE); ASSERT(hashtable_add(table, hashfunc_valid, data3, &data3->node) == TRUE); ASSERT(hashtable_add(table, hashfunc_valid, data4, &data4->node) == TRUE); ASSERT(hashtable_add(table, hashfunc_valid, data5, &data5->node) == TRUE); ASSERT(hashtable_iterator(table, iterfunc_print, NULL) == TRUE); printf("M17 test cases (1) OK\n"); /* M18 */ printf("Test case M18: Use iterator to remove entries\n"); ASSERT(hashtable_iterator(table, iterfunc_remove, NULL) == TRUE); printf("M18 test cases (1) OK\n"); /* same tests (M16 .. M18) with iterfunc that returns FALSE */ iterfunc_return = FALSE; /* M19 */ printf("Test case M19: Use iterator on an empty hashtable " "(iterfunc returns FALSE)\n"); /* iterfunc should not be called, so TRUE is returned */ ASSERT(hashtable_iterator(table, iterfunc_print, NULL) == TRUE); printf("M19 test cases (1) OK\n"); /* M20 */ printf("Test case M20: Use iterator on a non-empty hashtable " "(iterfunc returns FALSE)\n"); ASSERT(hashtable_add(table, hashfunc_valid, data1, &data1->node) == TRUE); ASSERT(hashtable_add(table, hashfunc_valid, data2, &data2->node) == TRUE); ASSERT(hashtable_add(table, hashfunc_valid, data3, &data3->node) == TRUE); ASSERT(hashtable_add(table, hashfunc_valid, data4, &data4->node) == TRUE); ASSERT(hashtable_add(table, hashfunc_valid, data5, &data5->node) == TRUE); ASSERT(hashtable_iterator(table, iterfunc_print, NULL) == FALSE); printf("M20 test cases (1) OK\n"); /* M21 */ printf("Test case M21: Use iterator to remove entries " "(iterfunc returns FALSE)\n"); ASSERT(hashtable_iterator(table, iterfunc_remove, NULL) == FALSE); printf("M21 test cases (1) OK\n"); iterfunc_return = TRUE; ASSERT(hashtable_iterator(table, iterfunc_remove, NULL) == TRUE); /* M22 */ printf("Test case M22: Find prime with negative starting value\n"); ASSERT(hashtable_find_prime(-5) == 0); printf("M22 test cases (1) OK\n"); /* M23 */ printf("Test case M23: Find prime with zero starting value\n"); ASSERT(hashtable_find_prime(0) == 0); printf("M23 test cases (1) OK\n"); /* M24 */ printf("Test case M24: Find prime with 1, 2 and 3 starting value\n"); ASSERT(hashtable_find_prime(1) == 2); ASSERT(hashtable_find_prime(2) == 2); ASSERT(hashtable_find_prime(3) == 3); printf("M24 test cases (3) OK\n"); /* M25 */ printf("Test case M25: Find prime with already a prime as the " "starting value\n"); ASSERT(hashtable_find_prime(113) == 113); printf("M25 test cases (1) OK\n"); /* M26 */ printf("Test case M26: Find prime with a non-prime as the starting " "value\n"); ASSERT(hashtable_find_prime(4) == 5); printf("M26 test cases (1) OK\n"); /* M27 */ printf("Test case M27: Find prime with a large starting value\n"); ASSERT(hashtable_find_prime((1 << 29) + 10) == 0); printf("M27 test cases (1) OK\n"); /* M28 */ printf("Test case M28: Verify that data is passed to iterator " "function\n"); ASSERT(hashtable_add(table, hashfunc_valid, data1, &data1->node) == TRUE); ASSERT(hashtable_iterator(table, iterfunc_verify, (void *) iterfunc_verify) == TRUE); hashtable_remove(&data1->node); printf("M28 test cases (1) OK\n"); /* XM01 */ /* print 100 primes */ n = 1; for (i = 0; i < 100; i++) { n = hashtable_find_prime(n); printf("%d\t", n); n++; } printf("\n"); /* SETTING HASHTABLE SIZE: */ printf("\n\t SETTING HASHTABLE SIZE:\n\n "); size = 200; /* XM02 */ /* Test the hashtable_find_prime * function with non prime argument */ primesize = hashtable_find_prime(size); printf("With size %d the hashtable size will be set to next " "prime %d\n", size, primesize); ASSERT(primesize >= size); /* INITIALIZING HASTABLES: */ printf("\n\t INITIALIZING HASTABLES:\n\n"); /* XM03 */ /* Test hastable_init function */ A = hashtable_init(primesize); B = hashtable_init(size); /* ADDING DATA: */ printf("\n\t ADDING DATA TO HASHTABLES:\n\n" " \t Result of 1 = Success,\n" " \t Result of 0 = Failure\n\n"); /* XM04 */ /* Add different data to two different hashtables. */ result1 = hashtable_add(A, test_hash, data1, &data1->node); printf("Adding data1 to hashtable A :\n" "data1->keyval = %d,\n" "data1->text = %s,\n" "\nThe result of adding data1 is:\t %d.\n\n", data1->keyval, data1->text, result1); result2 = hashtable_add(B, test_hash, data2, &data2->node); printf("Adding data2 to hashtable B :\n" "data2->keyval = %d,\n" "data2->text = %s,\n" "\nThe result of adding data2 is:\t %d.\n\n", data2->keyval, data2->text, result2); result3 = hashtable_add(B, test_hash, data3, &data3->node); printf("Adding data3 to hashtable B :\n" "data3->keyval = %d,\n" "data3->text = %s,\n" "\nThe result of adding data3 is:\t %d.\n\n", data3->keyval, data3->text, result3); result4 = hashtable_add(A, test_hash, data4, &data4->node); printf("Adding data4 to hashtable A :\n" "data4->keyval = %d,\n" "data4->text = %s,\n" "\nThe result of adding data4 is:\t %d.\n\n", data4->keyval, data4->text, result4); result5 = hashtable_add(A, test_hash, data5, &data5->node); printf("Adding data5 to hashtable A :\n" "data5->keyval = %d,\n" "data5->text = %s,\n" "\nThe result of adding data5 is:\t %d.\n\n", data5->keyval, data5->text, result5); /* FETCHING OF DATA FROM THE HASHTABLE: */ printf("\n\t FETCHING OF DATA FROM THE HASHTABLE:\n\n"); /* XM05 */ /* Trying to fetch data2 out of the hashtable. */ printf("Trying to fetch data2 out of the hashtable B!\n\n"); data2_out = (struct testdata *) hashtable_fetch(B, test_hash, data2, test_cmp); printf("\nLet's test the fetch result one more time:\n\n"); if (data2_out == NULL) { printf("data2_out == NULL !!!\n"); ASSERT(FALSE); } printf("data2_out->keyval = %d\n", data2_out->keyval); printf("data2_out->text = %s\n", data2_out->text); printf("data2_out->x = %f\n", data2_out->x); printf("\nComparing the fetched and original\n" " data with the comparefunction.\n"); test_cmp(data2_out, &data2->node); /* XM06 */ /* FETCHING SOMETHING INEXISTENT: */ printf("\nTrying to fetch data1 out of the hashtable B," "which does NOT contain data1 !\n\n"); data1_out = (struct testdata *) hashtable_fetch(B, test_hash, data1, test_cmp); if (data1_out == NULL) { printf("\ndata1_out == NULL\n\n"); /* ASSERT(FALSE); */ } printf("\nComparing the fetched (SHOULD BE NULL!)\n" "and original data with the comparefunction.\n"); test_cmp(data1_out, &data1->node); /* REMOVING DATA: */ /* XM07 */ /* Fetch and remove the data with one hashtable - function call. */ printf("\n\t REMOVING DATA:\n\n"); printf("Trying to find and remove data1 from hashtable A.\n\n"); result1 = hashtable_find_and_remove(A, test_hash, data1, test_cmp); printf("Result of finding and deleting data 1 from hashtable A:\n " "Result = %d.\n", result1); /* XM08 */ /* Remove data */ hashtable_remove(&data2->node); hashtable_remove(&data3->node); hashtable_remove(&data4->node); hashtable_remove(&data5->node); /* XM09 */ /* Adding the previously removed data back. */ /* ADDING DATA AGAIN: */ printf("\n\t ADDING DATA AGAIN:\n\n"); result1 = hashtable_add(A, test_hash, data1, &data1->node); printf("Result of adding data 1 to hashtable A is %d.\n",result1); result2 = hashtable_add(A, test_hash, data2, &data2->node); printf("Result of adding data 2 to hashtable A is %d.\n",result2); result3 = hashtable_add(A, test_hash, data3, &data3->node); printf("Result of adding data 3 to hashtable A is %d.\n",result3); result4 = hashtable_add(A, test_hash, data4, &data4->node); printf("Result of adding data 4 to hashtable A is %d.\n",result4); result5 = hashtable_add(A, test_hash, data5, &data5->node); printf("Result of adding data 5 to hashtable A is %d.\n",result5); /* XM10 */ /* Fetching and removing with consecutive function calls. */ /* THEN FETCHING AGAIN AND REMOVING AFTER THAT: */ printf("\n\t THEN FETCHING AGAIN AND REMOVING AFTER THAT:\n\n"); printf("Trying to fetch data3 out of the hashtable A!\n\n"); data1_out = (struct testdata *) hashtable_fetch(A, test_hash, data3, test_cmp); ASSERT(data1_out == data3); printf("OK!\n"); hashtable_remove(&data1_out->node); printf("\n Trying to fetch data1 out of the hashtable A!\n\n"); data1_out = (struct testdata *) hashtable_fetch(A, test_hash, data1, test_cmp); ASSERT(data1_out == data1); printf("OK!\n"); hashtable_remove(&data1_out->node); printf("\n Trying to fetch data4 out of the hashtable A!\n\n"); data1_out = (struct testdata *) hashtable_fetch(A, test_hash, data4, test_cmp); ASSERT(data1_out == data4); printf("OK!\n"); hashtable_remove(&data1_out->node); printf("\n Trying to fetch data5 out of the hashtable A!\n\n"); data1_out = (struct testdata *) hashtable_fetch(A, test_hash, data5, test_cmp); ASSERT(data1_out == data5); printf("OK!\n"); hashtable_remove(&data1_out->node); printf("\n Trying to fetch data2 out of the hashtable A!\n\n"); data1_out = (struct testdata *) hashtable_fetch(A, test_hash, data2, test_cmp); ASSERT(data1_out == data2); printf("OK!\n"); hashtable_remove(&data1_out->node); /* XM11 */ /* Trying to destroy a hashtable. */ printf("\n Destroying hashtables A and B:\n"); ASSERT(hashtable_destroy(B) == TRUE); ASSERT(hashtable_destroy(A) == TRUE); printf("\nFreeing memory:\n"); free(data1); free(data2); free(data3); free(data4); free(data5); printf("\n\t*****************************************\n" "\t* *\n" "\t* HASHTABLE TEST FINISHED *\n" "\t* *\n" "\t*****************************************\n\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -