static void test_Dump_and_Load(TestBatch *batch) { Hash *hash = Hash_new(0); Obj *dump; Hash *loaded; Hash_Store_Str(hash, "foo", 3, (Obj*)CB_new_from_trusted_utf8("foo", 3)); dump = (Obj*)Hash_Dump(hash); loaded = (Hash*)Obj_Load(dump, dump); TEST_TRUE(batch, Hash_Equals(hash, (Obj*)loaded), "Dump => Load round trip"); DECREF(dump); DECREF(loaded); /* TODO: Fix Hash_Load(). Hash_Store_Str(hash, "_class", 6, (Obj*)CB_new_from_trusted_utf8("not_a_class", 11)); dump = (Obj*)Hash_Dump(hash); loaded = (Hash*)Obj_Load(dump, dump); TEST_TRUE(batch, Hash_Equals(hash, (Obj*)loaded), "Load still works with _class if it's not a real class"); DECREF(dump); DECREF(loaded); */ DECREF(hash); }
int main () { FILE* the_table = fopen ("the_table.csv", "w"); FILE* the_text = fopen ("the_text.txt", "r"); assert (the_log); assert (the_table); assert (the_text); hash* the_hash = Hash_NEW(); assert (the_hash); char word[MAXLEN] = {}; int nWords = 0; typedef unsigned int (*hash_funcs) (const char* my_key); hash_funcs hf[5] = {hf0, hf1, hf2, hf3, hf4}; int func_num = 0; char text_description[2 * MAXLEN] = {}; strcpy (text_description, "Bradbury's novel \"Fahrenheit 451\""); OUT printf ("# This program allows to use hash-functions for sorting\n" "# words in %s\n" "# The developer: Yura Gorishniy <*****@*****.**>\n" "# Version 1.00\n" "# The file: %s\n" "# The compilation time: %s, %s\n\n", text_description, strrchr (__FILE__, '\\'), __DATE__, __TIME__); printf ("# You can use 5 hash-functions to analyse the novel:\n" "# Function [1] hash = 0\n" "# Function [2] hash = the ASCII code of the first letter of the word\n" "# Function [3] hash = the sum of ASCII codes of all letters of the word\n" "# Function [4] hash = the average ASCII code of all letters of the word\n" "# Function [5] hash = the number, which we get after right cyclic shift\n" "# P.S. All the hashes you get will be counted by module %d\n\n", HASH_SIZE); printf ("# Please, enter number of the function you want to use\n"); if (!scanf ("%d", &func_num)) { printf ("Incorrect input, try again\n"); abort (); } if (!(1 <= func_num && func_num <= 5)) { printf ("Incorrect input, try again\n"); abort (); } the_hash -> func = hf[func_num - 1]; while (1) { if (get_word (the_text, word) == 0) break; nWords++; int hash = the_hash -> func (word) % the_hash -> size; elem* new_elem = NULL; List_Elem_Find_Key (the_hash -> table[hash], word, &new_elem); if (!new_elem) List_Elem_Add (the_hash -> table[hash], word, 1); } for (int i = 0; i < HASH_SIZE; ++i) fprintf (the_table, "%d\n", the_hash -> table[i] -> length); Hash_Dump (the_hash); Hash_Delete (the_hash); fclose (the_text); fclose (the_table); fclose (the_log); printf ("\nThe job is done! \nYou can use the_table.csv in Microsoft Excel to get a nice diagram\nGoodbye!\n\n"); return 0; }