def generation_mode(file_in, num, seed, file_del=None): words = synthetic.words(file_in, num, seed) size = input("How many lists should be in the hashmap?: ") try: size = int(size) except ValueError as e: print("ERROR: Invalid value was passed to the program.\n", e) return -1 hash_table = HashTable(size) begin = time.perf_counter() hash_table.add_all(words) end = time.perf_counter() print("Adding time [s]: {:.6f}".format(end - begin)) begin = time.perf_counter() for word in words: hash_table.find(word) end = time.perf_counter() print("Enumerating time [s]: {:.6f}".format(end - begin)) if file_del is not None: words = synthetic.read_file(file_del) begin = time.perf_counter() for word in words: hash_table.delete_all(word) end = time.perf_counter() print("Deleting time [s]: {:.6f}".format(end - begin))
class TestHashTable(unittest.TestCase): def setUp(self): self.ht = HashTable() def test_hash(self): self.assertEqual(self.ht.hash("Maximus"), self.ht.hash("Maximus")) self.assertTrue(self.ht.hash("Maximus") < self.ht.capacity) def test_insert(self): self.ht.insert("key", "value") self.assertEqual(self.ht.size, 1) self.assertEqual(self.ht.buckets[self.ht.hash("key")].value, "value") def test_find(self): obj = "Maximus" self.ht.insert("Intel", obj) self.assertEqual(obj, self.ht.find("Intel")) obj = ["Hello", "world"] self.ht.insert("hobby", obj) self.assertEqual(obj, self.ht.find("hobby")) def test_remove(self): obj = "19" self.ht.insert("age", obj) self.assertEqual(1, self.ht.size) self.assertEqual(obj, self.ht.remove("age")) self.assertEqual(0, self.ht.size)
class TestHashTable(unittest.TestCase): def setUp(self): self.ht = HashTable() def test_hash(self): self.assertEqual(self.ht.hash("hello"), self.ht.hash("hello")) self.assertTrue(self.ht.hash("hello") < self.ht.capacity) def test_insert(self): self.assertEqual(self.ht.size, 0) self.ht.insert("test_key", "test_value") self.assertEqual(self.ht.size, 1) self.assertEqual( self.ht.buckets[self.ht.hash("test_key")].value, "test_value") def test_find(self): self.assertEqual(self.ht.size, 0) obj = "hello" self.ht.insert("key1", obj) self.assertEqual(obj, self.ht.find("key1")) obj = ["this", "is", "a", "list"] self.ht.insert("key2", obj) self.assertEqual(obj, self.ht.find("key2")) def test_remove(self): self.assertEqual(self.ht.size, 0) obj = "test object" self.ht.insert("key1", obj) self.assertEqual(1, self.ht.size) self.assertEqual(obj, self.ht.remove("key1")) self.assertEqual(0, self.ht.size) self.assertEqual(None, self.ht.remove("some random key")) def test_capacity(self): # Test all public methods in one run at a large capacity for i in range(0, 1000): self.assertEqual(i, self.ht.size) self.ht.insert("key" + str(i), "value") self.assertEqual(self.ht.size, 1000) for i in range(0, 1000): self.assertEqual(1000-i, self.ht.size) self.assertEqual(self.ht.find("key" + str(i)), self.ht.remove("key" + str(i))) def test_issue2(self): self.assertEqual(self.ht.size, 0) self.ht.insert('A', 5) self.assertEqual(self.ht.size, 1) self.ht.insert('B', 10) self.assertEqual(self.ht.size, 2) self.ht.insert('Ball', 'hello') self.assertEqual(self.ht.size, 3) self.assertEqual(5, self.ht.remove('A')) self.assertEqual(self.ht.size, 2) self.assertEqual(None, self.ht.remove('A')) self.assertEqual(self.ht.size, 2) self.assertEqual(None, self.ht.remove('A')) self.assertEqual(self.ht.size, 2)
def gen_step_mode(file_in, num, seed, file_del=None): size = input("How many lists should be in the hashmap?: ") step_val = input( "Specify the step value (number of words generated additionally): ") try: size = int(size) step_val = int(step_val) except ValueError as e: print("ERROR: Invalid value was passed to the program.\n", e) return -1 words_num = 0 results = [] while True: words_num += step_val if words_num > num: break print(words_num, " words") results.append(words_num) words = synthetic.words(file_in, words_num, seed) hash_table = HashTable(size) begin = time.perf_counter() hash_table.add_all(words) end = time.perf_counter() print("Adding time [s]: {:.6f}".format(end - begin)) results.append(end - begin) begin = time.perf_counter() for word in words: hash_table.find(word) end = time.perf_counter() print("Enumerating time [s]: {:.6f}".format(end - begin)) results.append(end - begin) if file_del is not None: words = synthetic.read_file(file_del) begin = time.perf_counter() for word in words: hash_table.delete_first(word) end = time.perf_counter() print("Deleting time [s]: {:.6f}".format(end - begin)) results.append(end - begin) analyse_data(results, num, size)