diff options
Diffstat (limited to 'src/test/check_ht.c')
-rw-r--r-- | src/test/check_ht.c | 187 |
1 files changed, 185 insertions, 2 deletions
diff --git a/src/test/check_ht.c b/src/test/check_ht.c index 0298087..b572549 100644 --- a/src/test/check_ht.c +++ b/src/test/check_ht.c @@ -23,9 +23,9 @@ int main(int UNUSED argc, char ** UNUSED argv) fleprintf("We found no value for key %ld\n", key); NUM size = ht_size(htp); - + fleprintf("The size of the hash table is %ld\n", size); - + for (NUM i = 0; i < size; i++) fleprintf("The %ld-th element has key %ld and value %ld\n", i, @@ -50,5 +50,188 @@ int main(int UNUSED argc, char ** UNUSED argv) } destroy_ht(htp, DESTROY_VALUE_SELF); + + /* Testing hash tables of pairs of integers. */ + + htp = new_ht2(10); + + pair2 *key2 = NULL; + + for (int i = 0; i < 10; i++) { + if (i>=2 && i % 2) { + NEW_P2(key2, i<<1, i, + destroy_ht(htp, DESTROY_KEY_SELF); + return 1;); + } else { + NEW_P2(key2, i, i<<1, + destroy_ht(htp, DESTROY_KEY_SELF); + return 1;); + } + if (ht_insert(htp, key2, (void*)1)) { + fleprintf0("Fail to insert\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } + } + + printf("Success!\n"); + + pair2 testk2 = { .x = 0, .y = 0 }; + + if (ht_find(htp, &testk2) == NULL) { + fleprintf0("Fail to find zero zero pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for zero zero pair\n", + ht_find(htp, &testk2)); + } + + testk2.x = 1; + testk2.y = 1; + + if (ht_find(htp, &testk2)) { + fleprintf0("Accidentally find one one pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for one one pair\n", + ht_find(htp, &testk2)); + } + + testk2.x = 1; + testk2.y = 2; + + if (ht_find(htp, &testk2) == NULL) { + fleprintf0("Fail to find one two pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for one two pair\n", + ht_find(htp, &testk2)); + } + + destroy_ht(htp, DESTROY_KEY_SELF); + + /* testing hash tables with pair3 keys */ + + htp = new_ht3(10); + + pair3 *key3 = NULL; + + for (int i = 0; i < 10; i++) { + NEW_P3(key3, i, i << 1, -i, + destroy_ht(htp, DESTROY_KEY_SELF); + return 1;); + + if (ht_insert(htp, key3, (void*)1)) { + fleprintf0("Fail to insert\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } + } + + printf("Success!\n"); + + pair3 testk3 = { .x = 0, .y = 0, .z = 0 }; + + if (ht_find(htp, &testk3) == NULL) { + fleprintf0("Fail to find zero pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for zero pair\n", + ht_find(htp, &testk3)); + } + + testk3.x = 1; + testk3.y = 2; + testk3.z = -1; + + if (ht_find(htp, &testk3) == NULL) { + fleprintf0("Fail to find one two minus one pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for one two minus one pair\n", + ht_find(htp, &testk3)); + } + + testk3.x = 1; + testk3.y = 2; + testk3.z = 1; + + if (ht_find(htp, &testk3)) { + fleprintf0("Accidentally find one two one pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for one two one pair\n", + ht_find(htp, &testk3)); + } + + destroy_ht(htp, DESTROY_KEY_SELF); + + /* testing hash tables with pair4 keys */ + + htp = new_ht4(10); + + pair4 *key4 = NULL; + + for (int i = 0; i < 10; i++) { + NEW_P4(key4, i, i << 1, -i, i+1, + destroy_ht(htp, DESTROY_KEY_SELF); + return 1;); + + if (ht_insert(htp, key4, (void*)1)) { + fleprintf0("Fail to insert\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } + } + + printf("Success!\n"); + + pair4 testk4 = { .x = 0, .y = 0, .z = 0, .w = 1 }; + + if (ht_find(htp, &testk4) == NULL) { + fleprintf0("Fail to find zero pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for zero pair\n", + ht_find(htp, &testk4)); + } + + testk4.x = 1; + testk4.y = 2; + testk4.z = -1; + testk4.w = 2; + + if (ht_find(htp, &testk4) == NULL) { + fleprintf0("Fail to find one two minus one pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for one two minus one two pair\n", + ht_find(htp, &testk4)); + } + + testk4.x = 1; + testk4.y = 2; + testk4.z = 1; + testk4.w = 2; + + if (ht_find(htp, &testk4)) { + fleprintf0("Accidentally find one two one two pair\n"); + destroy_ht(htp, DESTROY_KEY_SELF); + return 1; + } else { + printf("We find value %p for one two one two pair\n", + ht_find(htp, &testk4)); + } + + destroy_ht(htp, DESTROY_KEY_SELF); + return 0; } |