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)
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)
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())
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())