def test_can_merge_two_queues(self): pq1 = PriorityQueue() pq2 = PriorityQueue() pq1.push(2) pq2.push(1) pq1.merge(pq2) self.assertEqual(pq1.top(), 1)
def test_ascending_min_after_pop(self): pq = PriorityQueue() for v in [1, 2, 3, 4]: pq.push(v) pq.pop() pq.pop() pq.pop() self.assertEqual(pq.top(), 4)
class PriorityQueueViewModel: def __init__(self): self.pq = PriorityQueue() def push(self, e): self.pq.push(e) def top(self): return str(self.pq.top()) def pop(self): self.pq.pop() def is_empty(self): return self.pq.is_empty()
class PriorityQueueViewModel: def __init__(self, logger=RealLogger()): self.pq = PriorityQueue() self.logger = logger self.logger.log("Hello pqvm init") def push(self, e): self.logger.log("PQ push value") self.pq.push(e) def top(self): self.logger.log("PQ get top value") return str(self.pq.top()) def pop(self): self.logger.log("PQ pop value") self.pq.pop() def is_empty(self): return self.pq.is_empty()
def test_descending_min_after_pop(self): pq = PriorityQueue() for v in [4, 3, 2, 1]: pq.push(v) pq.pop() self.assertEqual(pq.top(), 2)
def test_getting_topmin(self): pq = PriorityQueue() for v in [5, 4, 2, 3]: pq.push(v) self.assertEqual(pq.top(), 2)
def test_cannot_push_noninteger(self): pq = PriorityQueue() with self.assertRaises(TypeError): pq.push("notanumber")
def test_becomes_empty_after_last_pop(self): pq = PriorityQueue() pq.push(1) pq.pop() self.assertTrue(pq.is_empty())
def test_consistent_min_after_pop(self): pq = PriorityQueue() for v in [3, 2, 1]: pq.push(v) pq.pop() self.assertEqual(pq.top(), 2)