def test_hash_collision(self): table1 = HashMap() table2 = HashMap() table1 = put(table1, 1, 3) table2 = put(table2, 14, 3) self.assertEqual(get(table1, 1), get(table2, 14)) # means the key of 1 and 12 have the same hash_value; # put the the key that have same init_hash_value table1 = put(table1, 14, 4) # now they have different hash_value, beacase the collision happen, to deal the collision the key rehash unit have not coollision self.assertNotEqual(get(table1, 14), get(table2, 14))
def test_immutable(self, a, b, key, value): table = from_list(HashMap(), a) table_temp = table table1 = put(table, key, value) self.assertNotEqual(id(table), id(table1)) self.assertEqual(to_dict(table), to_dict(table_temp)) table3 = del_(table, key) table_temp = table1 self.assertNotEqual(id(table1), id(table3)) self.assertEqual(to_dict(table_temp), to_dict(table1)) table4 = from_list(HashMap(), b) table3_temp = table3 table4_temp = table4 table5 = mconcat(table3, table4) table6 = mconcat(table4, table3) self.assertEqual(id(table5), id(table6)) self.assertEqual(to_dict(table3_temp), to_dict(table3)) self.assertEqual(to_dict(table4_temp), to_dict(table4))
def test_mconcat(self): self.assertEqual(mconcat(None, None), None) self.assertEqual(to_dict(mconcat(put(HashMap(), 1, 2), None)), to_dict(put(HashMap(), 1, 2))) self.assertEqual(to_dict(mconcat(None, put(HashMap(), 1, 2))), to_dict(put(HashMap(), 1, 2)))
def test_mempty(self): self.assertEqual(to_list(mempty(put(HashMap(), 1, 2))), []) self.assertEqual(to_list(mempty(put(put(HashMap(), 1, 2), 2, 3))), [])
def test_a_reduce(self): self.assertEqual(reduce(HashMap(), lambda st, e: st + e, 0), 0) self.assertEqual( reduce(put(put(HashMap(), 1, 2), 2, 3), lambda st, e: st + e, 0), 5)
def test_map(self): self.assertEqual(to_dict(map(HashMap(), str)), {}) self.assertEqual(to_dict(map(put(HashMap(), 1, 2), str)), {1: '2'})
def test_filter_iseven(self): self.assertEqual(filter_iseven(put(put(HashMap(), 1, 2), 2, 3)), [3]) self.assertEqual( filter_iseven(put(put(put(HashMap(), 1, 2), 2, 3), 3, 1)), [3, 1])
def test_find_iseven(self): self.assertEqual(find_iseven(put(put(HashMap(), 1, 2), 2, 3)), [2]) self.assertEqual( find_iseven(put(put(put(HashMap(), 1, 2), 2, 3), 3, 4)), [2, 4])
def test_to_list(self): self.assertEqual(to_list(None), []) self.assertEqual(to_list(put(HashMap(), 1, 2)), [2]) self.assertEqual(to_list(put(put(HashMap(), 1, 2), 2, 3)), [2, 3])
def test_getSize(self): self.assertEqual(getSize(put(HashMap(), 1, 2)), 1) self.assertEqual(getSize(del_(put(HashMap(), 1, 2), 1)), 0)
def test_del_(self): self.assertEqual(to_dict(put(HashMap(), 1, 2)), {1: 2}) self.assertEqual(to_dict(del_(put(HashMap(), 1, 2), 1)), {})
def test_get(self): self.assertEqual(get(put(HashMap(), 1, 2), 1), 2) self.assertEqual(get(put(HashMap(), 3, 4), 3), 4)
def test_put(self, key, value): hash = HashMap() a = put(hash, key, value) self.assertEqual(get(hash, key), value)
def test_put(self): self.assertEqual(to_dict(put(HashMap(), 1, 2)), {1: 2}) self.assertEqual(to_dict(put(put(HashMap(), 1, 2), 2, 3)), { 1: 2, 2: 3 })