def test_monoid_associativity(self, a, b, c): l1 = MyHashMap(vItem=a) l2 = MyHashMap(vItem=b) l3 = MyHashMap(vItem=c) aa = l1.mconcat(l1.mconcat(l1, l2), l3).to_list() bb = l1.mconcat(l1, l1.mconcat(l2, l3)).to_list() self.assertEqual(aa.sort(), bb.sort())
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())
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_reduce(self): lst = MyHashMap(vItem=[1, 2, 3, 4]) self.assertEqual(lst.reduce(lambda a, b: a + b, 0), 10) test_data = [[], ['a'], ['a', 'b']] for e in test_data: lst = MyHashMap() lst.from_list(e) self.assertEqual(lst.reduce(lambda x, _: x + 1, 0), lst.size())
def test_remove(self): self.assertEqual( remove(MyHashMap(vItem=[2]), 2).to_list().sort(), [].sort()) self.assertEqual( remove(MyHashMap(vItem=[3, 6, 8]), 2).to_list().sort(), [3, 6, 8].sort()) self.assertEqual( remove(MyHashMap(vItem=[24, 5, 2]), 5).to_list().sort(), [24, 2].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])
def test_mconcat(self): self.assertEqual( mconcat(MyHashMap(vItem=[22, 1, 2]), MyHashMap(vItem=[22, 5, 6])).to_list().sort(), [22, 1, 2, 5, 6].sort()) self.assertEqual( mconcat(MyHashMap(vItem=[]), MyHashMap(vItem=[22, 5, 6])).to_list().sort(), [22, 5, 6].sort())
def test_extend(self): self.assertEqual( extend(MyHashMap(vItem=[]), 2).to_list().sort(), [2].sort()) self.assertEqual( extend(MyHashMap(vItem=[3, 67, 1]), 2).to_list().sort(), [3, 67, 1, 2].sort()) self.assertEqual( extend(MyHashMap(vItem=[2, 3]), 66).to_list().sort(), [2, 3, 66].sort())
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())
def test_iter(self): x = [1, 2, 3] lst = MyHashMap() tmp = [] for e in lst: tmp.append(e) self.assertEqual(x.sort(), tmp.sort())
def remove(vHashMap, vValue): newHM = MyHashMap(vHashMap.length, vHashMap.to_list()) ncount = 0 for i in range(newHM.length): if (newHM.items[i] == vValue and newHM.flag[i] == 1): newHM.items[i] = 0 newHM.flag[i] = 0 return newHM
def test_find(self): lst = MyHashMap(vItem=[2, 4, 7]) result = lst.find(4) self.assertEqual(result, True) result = lst.find(5) self.assertEqual(result, False) result = lst.find(7) self.assertEqual(result, True)
def mconcat(a, b): if a != None and b != None: newHM = MyHashMap(a.length + b.length) for i in range(a.length): if (a.flag[i] == 1): newHM.insert(a.items[i]) for i in range(b.length): if (b.flag[i] == 1): newHM.insert(b.items[i]) return newHM elif a: # return MyHashMap(a.length,a.to_list()) return a elif b: # return MyHashMap(b.length,b.to_list()) return b else: return MyHashMap(vItem=[])
def extend(vHashMap, vValue): newHM = MyHashMap(vHashMap.length, vHashMap.to_list()) ncount = 0 for i in newHM.flag: if (i == 1): ncount += 1 if (ncount == vHashMap.length): return newHM.insert(vValue) return newHM
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())
def test_to_list(self): self.assertEqual(MyHashMap(vItem=[]).to_list(), []) self.assertEqual(MyHashMap(vItem=[6]).to_list(), [6]) self.assertEqual(MyHashMap(vItem=[5, 77]).to_list(), [5, 77])
def test_size(self): self.assertEqual(size(MyHashMap(vItem=[])), 0) self.assertEqual(size(MyHashMap(vItem=[3, 8])), 2) self.assertEqual(size(MyHashMap(vItem=[6, 3, 4])), 3)
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())
def test_python_len_and_list_size_equality(self, a): lst = MyHashMap(length=len(a) + 10, vItem=a) # lst.from_list(a) # print(a,lst.to_list()) self.assertEqual(lst.size(), len(a))
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())
def test_monoid_identity(self, lst): a = MyHashMap(vItem=[1, 2]) a.from_list(lst) self.assertEqual(mconcat(mempty(), a), a) self.assertEqual(mconcat(a, mempty()), a)
def test_from_list_to_list_equality(self, a): lst = MyHashMap() lst.from_list(a) self.assertEqual(to_list(lst).sort(), a.sort())
def test_to_list(self): self.assertEqual( to_list(MyHashMap(vItem=[3, 6, 8])).sort(), [3, 6, 8].sort()) self.assertEqual( to_list(MyHashMap(vItem=[17, 8])).sort(), [17, 8].sort())