def test_collisions(self): hash_map = HashMap() for i in range(257): hash_map.insert(i, str(i)) for i in range(257): self.assertEqual(str(i), hash_map.get(i))
def test_lose_lose(self): self.assertEqual(532, lose_lose('hello')) hash_map = HashMap(hash_function=lose_lose) hash_map.insert('hello', 'world') self.assertEqual('world', hash_map.get('hello')) self.assertEqual([('hello', 'world')], hash_map.buckets[lose_lose('hello') % len(hash_map.buckets)])
def test_sdbm(self): self.assertEqual(1925877435333486942514, sdbm('hello')) hash_map = HashMap(hash_function=sdbm) hash_map.insert('hello', 'world') self.assertEqual('world', hash_map.get('hello')) self.assertEqual([('hello', 'world')], hash_map.buckets[sdbm('hello') % len(hash_map.buckets)])
def test_djb2(self): self.assertEqual(210714636441, djb2('hello')) hash_map = HashMap(hash_function=djb2) hash_map.insert('hello', 'world') self.assertEqual('world', hash_map.get('hello')) self.assertEqual([('hello', 'world')], hash_map.buckets[djb2('hello') % len(hash_map.buckets)])
def test_delete(self): hash_map = HashMap() hash_map.insert(1, '1') self.assertEqual('1', hash_map.get(1)) self.assertEqual((1, '1'), hash_map.delete(1)) with self.assertRaises(KeyError): hash_map.get(1) with self.assertRaises(KeyError): hash_map.delete(1)
def test_get(self): hash_map = HashMap() hash_map.insert(3, 'three') hash_map.insert('99', 99) self.assertEqual('three', hash_map.get(3)) self.assertEqual(99, hash_map.get('99'))
def test_overwrite(self): hash_map = HashMap() hash_map.insert(1, '1') self.assertEqual('1', hash_map.get(1)) hash_map.insert(1, '2') self.assertEqual('2', hash_map.get(1))
def get_triangle(): i_to_label = HashMap() i_to_label[0] = '0' i_to_label[1] = '1' i_to_label[2] = '2' edge_list = [(0, 1), (1, 2), (2, 0)] return edge_list, i_to_label
def get_two_triangles_graph(): i_to_label = HashMap() for i in range(6): i_to_label[i] = str(i) edge_list = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3)] edge_list_nonor = to_nonor(edge_list) return edge_list_nonor, i_to_label
def get_divisibility_graph(): i_to_label = HashMap() i_to_label[1] = '1' i_to_label[0] = '2' i_to_label[2] = '3' i_to_label[3] = '4' i_to_label[4] = '6' i_to_label[5] = '12' edge_list = [ (1, 0), (1, 2), (0, 3), (0, 4), (2, 4), (3, 5), (4, 5), ] return edge_list, i_to_label
def test_insert(self): hash_map = HashMap() hash_map.insert('hello', 'world') self.assertEqual('world', hash_map.get('hello'))
def test_not_found(self): hash_map = HashMap() with self.assertRaises(KeyError): hash_map.get(1)