#include #include "../ht.h" int main(int UNUSED argc, char ** UNUSED argv) { ht *htp = new_ht(HT_INIT_CAP, 0); NUM *temp = MYALLOC(NUM, 1), key = 1023; *temp = 12345; if (ht_insert(htp, &key, temp)) { fleprintf0("Fail to insert\n"); free(temp); destroy_ht(htp, DESTROY_EVERY_SELF); return 1; } if ((temp = ht_find(htp, &key))) { fleprintf("We found value %ld for key %ld\n", *temp, key); } else 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, *((NUM *)*(ht_keys(htp)+i)), *((NUM *)*(ht_values(htp)+i))); if (ht_delete(htp, &key, DELETE_VALUE)) { fleprintf("Fail to delete key %ld\n", key); destroy_ht(htp, DESTROY_VALUE_SELF); return 1; } fleprintf0("After the deletion, "); if ((temp = ht_find(htp, &key))) { eprintf("We found value %ld for key %ld\n", *temp, key); destroy_ht(htp, DESTROY_VALUE_SELF); return 1; } else { eprintf("We found no value for key %ld\n", key); } 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)); } ht_reset(htp, DELETE_KEY); printf("After reset the size is %ld\n", ht_size(htp)); destroy_ht(htp, DESTROY_KEY_SELF); return 0; }