Exemplo n.º 1
0
 def test_filter(self):
     lst = MyHashMap(vItem=[1, 2, 3, 4, 5, 6, 7, 8, 9])
     lst.filter(lambda x: x % 2 == 0)
     self.assertEqual(lst.to_list().sort(), [2, 4, 6, 8].sort())
     lst = MyHashMap(vItem=[1, 2, 3, 4, 5, 6, 7, 8, 9])
     lst.filter(lambda x: x % 2 != 0)
     self.assertEqual(lst.to_list().sort(), [1, 3, 5, 7, 9].sort())
Exemplo n.º 2
0
 def test_remove(self):
     lst = MyHashMap(vItem=[2, 4, 7])
     lst.remove(2)
     self.assertEqual(lst.to_list().sort(), [4, 7].sort())
     lst = MyHashMap(vItem=[55, 16, 7])
     lst.remove(2)
     self.assertEqual(lst.to_list().sort(), [55, 16, 7].sort())
 def test_hashCollision(self):
     lst = MyHashMap(
         vItem=[100, 200, 300]
     )  #100,200,300 have the same hash value, so the order does not change after the conflict is resolved
     self.assertEqual(lst.to_list(), [100, 200, 300])
     lst = MyHashMap(
         vItem=[32, 16, 48]
     )  #have the same hash value, so the order does not change after the conflict is resolved
     self.assertEqual(lst.to_list(), [32, 16, 48])
Exemplo n.º 4
0
 def test_map(self):
     lst = MyHashMap(vItem=[])
     lst.map(str)
     self.assertEqual(lst.to_list(), [])
     lst = MyHashMap(vItem=[1, 2, 3, 4, 5])
     lst.map(str)
     self.assertEqual(lst.to_list().sort(), ['1', '2', '3', '4',
                                             '5'].sort())
     lst = MyHashMap(vItem=[1, 2, 3, 4, 5])
     lst.map(lambda x: x**2)
     self.assertEqual(lst.to_list().sort(), [1, 4, 9, 16, 25].sort())
Exemplo n.º 5
0
 def test_from_list_to_list_equality(self, a):
     lst = MyHashMap()
     lst.from_list(a)
     b = lst.to_list()
     self.assertEqual(b.sort(), list(set(a)).sort())
Exemplo n.º 6
0
 def test_insert(self):
     lst = MyHashMap(vItem=[])
     lst.insert(2)
     self.assertEqual(lst.to_list().sort(), [2].sort())
     lst.insert(55)
     self.assertEqual(lst.to_list().sort(), [2, 55].sort())
Exemplo n.º 7
0
 def test_from_list(self):
     test_data = [[], ['a'], ['a', 'b']]
     for e in test_data:
         lst = MyHashMap(vItem=[])
         lst.from_list(e)
         self.assertEqual(lst.to_list().sort(), e.sort())