Skip to content
Snippets Groups Projects
Commit 2175af30 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

test for heap

parent 6fbf0c91
No related branches found
No related tags found
No related merge requests found
...@@ -227,6 +227,7 @@ class Heap(object): ...@@ -227,6 +227,7 @@ class Heap(object):
def __contains__(self, item): def __contains__(self, item):
return item in self.heap return item in self.heap
class TimeBasedHeap(Heap): class TimeBasedHeap(Heap):
""" """
A heap of items sorted by timestamps. A heap of items sorted by timestamps.
...@@ -240,6 +241,9 @@ class TimeBasedHeap(Heap): ...@@ -240,6 +241,9 @@ class TimeBasedHeap(Heap):
#notthreadsafe #notthreadsafe
""" """
def __repr__(self):
return u'<satella.coding.TimeBasedHeap>'
def __init__(self): def __init__(self):
""" """
Initialize an empty heap Initialize an empty heap
...@@ -255,7 +259,7 @@ class TimeBasedHeap(Heap): ...@@ -255,7 +259,7 @@ class TimeBasedHeap(Heap):
""" """
self.push(timestamp, item) self.push(timestamp, item)
@typed(None, (float, int)) @typed(None, (float, int), returns=list)
def pop_less_than(self, timestamp): def pop_less_than(self, timestamp):
""" """
Return a list of items with timestamps less than your value. Return a list of items with timestamps less than your value.
...@@ -269,5 +273,5 @@ class TimeBasedHeap(Heap): ...@@ -269,5 +273,5 @@ class TimeBasedHeap(Heap):
""" """
Remove all things equal to item Remove all things equal to item
""" """
self.filter(lambda i: i != item) self.filtermap(filter_fun=lambda i: i != item)
...@@ -25,7 +25,7 @@ class TestHeap(unittest.TestCase): ...@@ -25,7 +25,7 @@ class TestHeap(unittest.TestCase):
def test_tbh_iter(self): def test_tbh_iter(self):
tbh = Heap([(10, 'ala'), (20, 'ma'), (30, 'kota'), (5, 'yo')]) tbh = Heap([(10, 'ala'), (20, 'ma'), (30, 'kota'), (5, 'yo')])
self.assertEqual([(5, 'yo'), (10, 'ala'), (20, 'ma'), (30, 'kota')], list(tbh.iter_ascending())) self.assertEqual([(5, 'yo'), (10, 'ala'), (20, 'ma'), (30, 'kota')], list(tbh.iter_ascending()))
self.assertEqual(reversed([(5, 'yo'), (10, 'ala'), (20, 'ma'), (30, 'kota')]), list(tbh.iter_descending())) self.assertEqual(reversed([(5, 'yo'), (10, 'ala'), (20, 'ma'), (30, 'kota')]), list(tbh.iter_descending()))
...@@ -41,7 +41,8 @@ class TestHeap(unittest.TestCase): ...@@ -41,7 +41,8 @@ class TestHeap(unittest.TestCase):
self.assertIn((10, 'ala'), tbh) self.assertIn((10, 'ala'), tbh)
self.assertIn((20, 'ma'), tbh) self.assertIn((20, 'ma'), tbh)
tbh.filtermap(lambda x: x[0] != 20, lambda x: x[0]+10, 'azomg') tbh.filtermap(filter_fun=lambda x: x[0] != 20,
map_fun=lambda x: (x[0]+10, 'azomg'))
self.assertIn((20, 'azomg'), tbh) self.assertIn((20, 'azomg'), tbh)
self.assertNotIn((10, 'ala'), tbh) self.assertNotIn((10, 'ala'), tbh)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment