Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 def test_queue_array(self):
     from ch04.array 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)
     with self.assertRaises(RuntimeError):
         pq.enqueue(99, 99)
     val = 5
     while pq:
         self.assertEqual(val, pq.dequeue())
         val -= 1
     with self.assertRaises(RuntimeError):
         pq.dequeue()
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 def test_builtin_heap_pq(self):
     from ch04.builtin import PQ
     from resources.english import english_words
     words = english_words()[:1000]
     pair = self.priority_queue_stress_test(PQ(len(words)), len(words))
     # Note: we cannot guarantee individual words BUT we can guarantee length
     self.assertEqual((len('abdominohysterectomy'), len('a')),
                      (len(pair[0]), len(pair[1])))
Ejemplo n.º 5
0
 def test_ordered_pq(self):
     from ch04.ordered import PQ
     from resources.english import english_words
     words = english_words()[:10000]
     pair = self.priority_queue_stress_test(PQ(len(words)), len(words))
     # Note: we cannot guarantee individual words BUT we can guarantee length
     self.assertEqual((len('acetylphenylhydrazine'), len('a')),
                      (len(pair[0]), len(pair[1])))
Ejemplo n.º 6
0
 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())
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
    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())
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
0
 def test_dynamic_heap_pq(self):
     from ch04.dynamic_heap import PQ
     pair = self.priority_queue_stress_test(PQ(625))
     # Note: we cannot guarantee individual words BUT we can guarantee length
     self.assertEqual((len('formaldehydesulphoxylate'), len('a')),
                      (len(pair[0]), len(pair[1])))