def test_dynamic_heap_pq_status(self): from ch04.dynamic_heap import PQ pq = PQ(5) self.assertTrue(pq.is_empty()) for v, p in [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]: pq.enqueue(v, p) self.assertTrue(pq.is_full())
def test_just_ordered_list(self): from ch04.ordered_list import PQ pq = PQ(4) for i in range(4): pq.enqueue(i, i) with self.assertRaises(RuntimeError): pq.enqueue(99, 99)
def test_just_builtin(self): from ch04.builtin import PQ pq = PQ(4) for i in range(4): pq.enqueue(i, i) with self.assertRaises(RuntimeError): pq.enqueue(99, 99)
def test_linked_pq(self): from ch04.linked import PQ pq = PQ(3) self.assertFalse(pq.is_full()) with self.assertRaises(RuntimeError): pq.dequeue() for i in range(3): pq.enqueue(i, i) self.assertTrue(pq.is_full()) with self.assertRaises(RuntimeError): pq.enqueue(99, 99)
def test_heap(self): from ch04.heap import PQ pq = PQ(5) for i in range(5): pq.enqueue(i, i) self.assertEqual(4, pq.dequeue()) self.assertEqual(3, pq.dequeue()) self.assertEqual(2, pq.dequeue()) self.assertEqual(1, pq.dequeue()) self.assertEqual(0, pq.dequeue())
def test_queue_ordered(self): from ch04.ordered import PQ pq = PQ(5) self.assertFalse(pq.is_full()) for v, p in [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]: pq.enqueue(v, p) val = 5 while pq: self.assertEqual(val, pq.dequeue()) val -= 1 with self.assertRaises(RuntimeError): pq.dequeue()
def test_heap_pq_edge_cases(self): from ch04.heap import PQ pq = PQ(3) with self.assertRaises(RuntimeError): pq.peek() self.assertFalse(pq.is_full()) with self.assertRaises(RuntimeError): pq.dequeue() for i in range(3): pq.enqueue(i, i) self.assertTrue(pq.is_full()) with self.assertRaises(RuntimeError): pq.enqueue(99, 99)
def test_validate_factorial_heap_pq(self): """Validate levels for factorial heap after enqueue and dequeue.""" from ch04.factorial_heap import PQ, validate end = 10000 pq = PQ(end) for i in range(end): pq.enqueue(i, i) validate(pq) last = end - 1 while pq: self.assertEqual(last, pq.dequeue()) last -= 1 validate(pq)