From 5730d6c04258e192195bfbbbed76d68fd78ed458 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Wed, 12 Jan 2022 20:26:08 +0800 Subject: Implement a simple hash table. It is a very basic and simple hash table. It is so simple that I hesitate to call it a hash table. Anyways, I think it suffices for my purposes here. * Makefile.am: Add necessary files. * grammar.c (new_tnt_string): Formatting. * ht.c (new_ht): Constructor (destroy_ht): Destructor (ht_expand): Rehash (ht_insert, ht_delete, ht_find): Main functions. * list.c (add_to_list, list_assure_size): Modify the use of realloc. * test/check_ht.c: Ensure this is working correctly. * util.c (read_entire_file): Modify the use of realloc. --- src/test/check_ht.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/check_ht.c (limited to 'src/test') diff --git a/src/test/check_ht.c b/src/test/check_ht.c new file mode 100644 index 0000000..2419ce5 --- /dev/null +++ b/src/test/check_ht.c @@ -0,0 +1,43 @@ +#include +#include "../ht.h" + +int main(int UNUSED argc, char ** UNUSED argv) +{ + ht *htp = new_ht(HT_INIT_CAP); + + 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, 1); + 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); + + if (ht_delete(htp, key, 1)) { + fleprintf("Fail to delete key %ld\n", key); + destroy_ht(htp, 1); + 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, 1); + return 1; + } else { + eprintf("We found no value for key %ld\n", key); + } + + destroy_ht(htp, 1); + return 0; +} -- cgit v1.2.3-18-g5258