Ejemplo n.º 1
0
    def test_dequeue_efficiency(self):
        """
        Dequeuing a value is O(n).
        """
        print("This test will take a while...")  # See the comment below.
        time_samples = []
        for _ in range(0, 1000):
            pq = NaivePriorityQueue()
            pq.enqueue('fake')
            start_time = time.time()
            pq.dequeue()
            end_time = time.time()
            time_samples.append(end_time - start_time)
        small_average_dequeue_time = sum(time_samples) / float(
            len(time_samples))

        large_queue = NaivePriorityQueue()
        for _ in range(0, 1000000):
            large_queue.enqueue('fake')
        large_time_samples = []
        for _ in range(0, 1000):
            start_time = time.time()
            large_queue.dequeue()
            end_time = time.time()
            large_time_samples.append(end_time - start_time)
        large_average_dequeue_time = sum(large_time_samples) / float(
            len(large_time_samples))
        self.assertNotAlmostEqual(small_average_dequeue_time,
                                  large_average_dequeue_time,
                                  delta=small_average_dequeue_time)
Ejemplo n.º 2
0
 def test_dequeue_three(self):
     """
     Dequeuing from a three-element queue returns the jobs with the highest
     priority.
     """
     pq = NaivePriorityQueue()
     lower_priority = Job(1, 'like')
     middle_priority = Job(3, 'who')
     higher_priority = Job(5, 'on')
     pq.enqueue(higher_priority)
     pq.enqueue(lower_priority)
     pq.enqueue(middle_priority)
     self.assertEqual(higher_priority, pq.dequeue())
     self.assertEqual(middle_priority, pq.dequeue())
     self.assertEqual(lower_priority, pq.dequeue())
Ejemplo n.º 3
0
 def test_dequeue_three_internal(self):
     """
     Dequeuing from a three-element queue removes each dequeued value from
     the internal list, highest-priority first.
     """
     pq = NaivePriorityQueue()
     lower_priority = Job(1, 'top')
     middle_priority = Job(3, 'of')
     higher_priority = Job(5, 'this')
     pq.enqueue(higher_priority)
     pq.enqueue(lower_priority)
     pq.enqueue(middle_priority)
     _ = pq.dequeue()
     self.assertEqual(lower_priority, pq.data[0])
     _ = pq.dequeue()
     self.assertEqual(lower_priority, pq.data[0])
Ejemplo n.º 4
0
 def test_dequeue_one(self):
     """
     Dequeuing from a single-element queue returns the single value.
     """
     pq = NaivePriorityQueue()
     j = Job(5, 'the')
     pq.enqueue(j)
     self.assertEqual(j, pq.dequeue())
Ejemplo n.º 5
0
 def test_empty_after_dequeue(self):
     """
     A queue with one enqueued value is empty after dequeuing.
     """
     pq = NaivePriorityQueue()
     pq.enqueue(Job(1, 'was'))
     _ = pq.dequeue()
     self.assertTrue(pq.is_empty())
Ejemplo n.º 6
0
 def test_not_empty_multiple(self):
     """
     A queue with two enqueued values is not empty after dequeuing only one.
     """
     pq = NaivePriorityQueue()
     pq.enqueue(Job(1, 'hustling'))
     pq.enqueue(Job(3, 'arguing and bustling'))
     _ = pq.dequeue()
     self.assertFalse(pq.is_empty())
Ejemplo n.º 7
0
 def test_dequeue_two(self):
     """
     Dequeuing from a two-element queue returns the one with highest priority.
     """
     pq = NaivePriorityQueue()
     lower_priority = Job(1, 'of')
     higher_priority = Job(3, 'the')
     pq.enqueue(higher_priority)
     pq.enqueue(lower_priority)
     self.assertEqual(higher_priority, pq.dequeue())
Ejemplo n.º 8
0
 def test_dequeue_one_internal(self):
     """
     Dequeuing from a single-element queue removes it from the internal list.
     """
     pq = NaivePriorityQueue()
     job = Job(5, 'crown')
     pq.enqueue(job)
     self.assertEqual(1, len(pq.data))
     _ = pq.dequeue()
     self.assertEqual(0, len(pq.data))
Ejemplo n.º 9
0
 def test_dequeue_two_internal(self):
     """
     Dequeuing from a two-element queue removes the job with the highest
     priority from the list.
     """
     pq = NaivePriorityQueue()
     lower_priority = Job(1, 'metropolis')
     higher_priority = Job(3, 'shining')
     pq.enqueue(higher_priority)
     pq.enqueue(lower_priority)
     _ = pq.dequeue()
     self.assertEqual(lower_priority, pq.data[0])
     self.assertEqual(1, len(pq.data))
Ejemplo n.º 10
0
 def test_initial_dequeue(self):
     """
     Dequeuing from an empty queue returns None.
     """
     pq = NaivePriorityQueue()
     self.assertIsNone(pq.dequeue())