Exemplo n.º 1
0
 def test_inactive(self):
     q = PriorityQueue(qfactory)
     self.assertEqual(len(q._active_queues), 0)
     self.assertEqual(len(q._inactive_queues), 0)
     q.push(1, 1)
     self.assertEqual(len(q._active_queues), 1)
     self.assertEqual(len(q._inactive_queues), 0)
     q.pop()
     self.assertEqual(len(q._active_queues), 0)
     self.assertEqual(len(q._inactive_queues), 1)
     q.push(2, 2)
     self.assertEqual(len(q._active_queues), 1)
     self.assertEqual(len(q._inactive_queues), 1)
     q.push(1, 3)
     self.assertEqual(len(q._active_queues), 2)
     self.assertEqual(len(q._inactive_queues), 0)
Exemplo n.º 2
0
 def test_inactive(self):
     q = PriorityQueue(qfactory)
     self.assertEqual(len(q._active_queues), 0)
     self.assertEqual(len(q._inactive_queues), 0)
     q.push(1, 1)
     self.assertEqual(len(q._active_queues), 1)
     self.assertEqual(len(q._inactive_queues), 0)
     q.pop()
     self.assertEqual(len(q._active_queues), 0)
     self.assertEqual(len(q._inactive_queues), 1)
     q.push(2, 2)
     self.assertEqual(len(q._active_queues), 1)
     self.assertEqual(len(q._inactive_queues), 1)
     q.push(1, 3)
     self.assertEqual(len(q._active_queues), 2)
     self.assertEqual(len(q._inactive_queues), 0)
Exemplo n.º 3
0
    def test_basic(self):
        q = PriorityQueue(qfactory)
        self.assertEqual(len(q), 0)
        self.assertRaises(IndexError, q.peek)
        self.assertRaises(IndexError, q.pop)
        q.push(42, 42)
        self.assertEqual(len(q), 1)
        self.assertEqual(q.peek(), 42)
        q.push(47, 47)
        self.assertEqual(len(q), 2)
        self.assertEqual(q.peek(), 47)

        self.assertEqual(q.pop(), 47)
        self.assertEqual(q.peek(), 42)
        self.assertEqual(q.pop(), 42)
        self.assertRaises(IndexError, q.pop)
        self.assertEqual(len(q), 0)
Exemplo n.º 4
0
    def test_basic(self):
        q = PriorityQueue(qfactory)
        self.assertEqual(len(q), 0)
        self.assertRaises(IndexError, q.peek)
        self.assertRaises(IndexError, q.pop)
        q.push(42, 42)
        self.assertEqual(len(q), 1)
        self.assertEqual(q.peek(), 42)
        q.push(47, 47)
        self.assertEqual(len(q), 2)
        self.assertEqual(q.peek(), 47)

        self.assertEqual(q.pop(), 47)
        self.assertEqual(q.peek(), 42)
        self.assertEqual(q.pop(), 42)
        self.assertRaises(IndexError, q.pop)
        self.assertEqual(len(q), 0)
Exemplo n.º 5
0
 def test_random_case(self):
     rnd = random.Random(47)
     q = PriorityQueue(qfactory)
     values = []
     operations = []
     for step in xrange(100):
         if len(q) and rnd.randint(1, 3) == 1:
             operations.append('pop')
             self.assertEqual(values.pop()[1], q.pop(), operations)
             if values:
                 self.assertEqual(values[-1][1], q.peek(), operations)
         else:
             v = rnd.randint(1, 50)
             q.push(v, -step)
             values.append((v, -step))
             operations.append('add %s' % v)
             values.sort()
             self.assertEqual(values[-1][1], q.peek(), operations)
         self.assertEqual(len(q), len(values), operations)
         active = set(q._active_queues)
         inactive = set(q._inactive_queues)
         self.assertSetEqual(active & inactive, set())
Exemplo n.º 6
0
 def test_random_case(self):
     rnd = random.Random(47)
     q = PriorityQueue(qfactory)
     values = []
     operations = []
     for step in xrange(100):
         if len(q) and rnd.randint(1, 3) == 1:
             operations.append('pop')
             self.assertEqual(values.pop()[1], q.pop(), operations)
             if values:
                 self.assertEqual(values[-1][1], q.peek(), operations)
         else:
             v = rnd.randint(1, 50)
             q.push(v, -step)
             values.append((v, -step))
             operations.append('add %s' % v)
             values.sort()
             self.assertEqual(values[-1][1], q.peek(), operations)
         self.assertEqual(len(q), len(values), operations)
         active = set(q._active_queues)
         inactive = set(q._inactive_queues)
         self.assertSetEqual(active & inactive, set())