def test_index(self): size = 7 table = HashTable(size) used = [0] * size max_iter = 10**3 i = 0 def all_used(): return all(x > 0 for x in used) # make sure all indexes get used while i < max_iter and not all_used(): key = self.rand_string() index = table._index(key) self.assertTrue(index >= 0 and index < size) used[index] += 1 i += 1 self.assertTrue(all_used()) # make sure all indexes about equal usage while i < max_iter: key = self.rand_string() used[table._index(key)] += 1 i += 1 try: # min and max are no more than 4x apart self.assertTrue(max(used) < 4 * min(used)) except Exception as e: print(used) raise e
def test_index(self): size = 7 table = HashTable(size) used = [0] * size max_iter = 10 ** 3 i = 0 def all_used(): return all(x > 0 for x in used) # make sure all indexes get used while i < max_iter and not all_used(): key = self.rand_string() index = table._index(key) self.assertTrue(index >= 0 and index < size) used[index] += 1 i += 1 self.assertTrue(all_used()) # make sure all indexes about equal usage while i < max_iter: key = self.rand_string() used[table._index(key)] += 1 i += 1 try: # min and max are no more than 4x apart self.assertTrue(max(used) < 4 * min(used)) except Exception as e: print(used) raise e
def test_get_set_del(self): t = HashTable(2) t._table = [GetSetDelSpy() for _ in range(2)] t._index = lambda x: int(x / 31) - 1 _ = t[31] self.assertEqual(t._table[0].actions, [('g', 31)]) self.assertEqual(t._table[1].actions, []) _ = t[62] self.assertEqual(t._table[0].actions, [('g', 31)]) self.assertEqual(t._table[1].actions, [('g', 62)]) del t[31] self.assertEqual(t._table[0].actions, [('g', 31), ('d', 31)]) self.assertEqual(t._table[1].actions, [('g', 62)]) t[62] = 4 self.assertEqual(t._table[0].actions, [('g', 31), ('d', 31)]) self.assertEqual(t._table[1].actions, [('g', 62), ('s', 62, 4)])