Пример #1
0
    def test_type(self):
        with self.assertRaises(PriorityQueueTypeError):
            DuplicatePriorityQueue(elements_type=5.4)

        priority_queue = DuplicatePriorityQueue()
        self.assertEqual(priority_queue.type, None,
                         "Wrong type at initialization")
        priority_queue.enqueue(5, 5)
        priority_queue.enqueue("word", 10)
        priority_queue = DuplicatePriorityQueue(str, True)
        priority_queue.enqueue("word", 5)
        priority_queue.enqueue("word", 5)
        priority_queue.enqueue("another_word", 5)
        self.assertEqual(priority_queue.type, str,
                         "Wrong type at initialization")

        with self.assertRaises(PriorityQueueTypeError):
            priority_queue.enqueue(2, 3)

        with self.assertRaises(PriorityQueueTypeError):
            priority_queue.enqueue("2", "3")

        with self.assertRaises(PriorityQueueTypeError):
            priority_queue.contains_priority(5.43)

        with self.assertRaises(PriorityQueueTypeError):
            priority_queue.get_element("7")
Пример #2
0
    def test_remove(self):
        priority_queue = DuplicatePriorityQueue()
        with self.assertRaises(PriorityQueueElementError):
            priority_queue.remove_element(5)

        priority_queue.enqueue("word", 1)
        priority_queue.enqueue(1, 5)
        priority_queue.enqueue(3.14, 10)
        priority_queue.remove_element(3.14)
        self.assertFalse(priority_queue.contains_element(3.14),
                         "Wrong remove implementation")
        self.assertEqual(priority_queue.peek(), 1,
                         "Wrong remove implementation")
        with self.assertRaises(PriorityQueueElementError):
            priority_queue.remove_element(3.14)
        self.assertEqual(priority_queue.size, 2, "Wrong remove implementation")

        priority_queue = DuplicatePriorityQueue(int, reverse=True)
        with self.assertRaises(PriorityQueueTypeError):
            priority_queue.remove_element(5.5)

        for i in range(10):
            priority_queue.enqueue(i, i**2)
            priority_queue.enqueue(i + 100, i**2)
        self.assertEqual(len(priority_queue), 20)
        self.assertTrue(priority_queue.contains_element(5))
        priority_queue.remove_element(5)
        self.assertEqual(len(priority_queue), 19,
                         "Wrong remove implementation")
        self.assertFalse(priority_queue.contains_element(5),
                         "Wrong remove implementation")

        priority_queue.remove_element(0)
        self.assertEqual(priority_queue.dequeue(), 100,
                         "Wrong remove implementation")

        self.assertTrue(priority_queue.contains_priority(9))
        self.assertTrue(3 in priority_queue)
        self.assertTrue(103 in priority_queue)

        priority_queue.remove_element(3)

        self.assertTrue(priority_queue.contains_priority(9))
        self.assertFalse(3 in priority_queue)
        self.assertTrue(103 in priority_queue)

        self.assertEqual(priority_queue.get_element(9), 103,
                         "Wrong remove implementation")
        priority_queue.remove_element(103)
        self.assertFalse(priority_queue.contains_priority(9),
                         "Wrong remove implementation")
        self.assertEqual(len(priority_queue), 15,
                         "Wrong remove implementation")

        self.assertEqual(priority_queue.dequeue(), 1)
        priority_queue.remove_element(101)
        self.assertEqual(priority_queue.get_element(1), None,
                         "Wrong remove implementation")
        self.assertEqual(len(priority_queue), 13)
Пример #3
0
    def test_contains(self):
        priority_queue = DuplicatePriorityQueue()
        self.assertFalse(priority_queue.contains_priority(5),
                         "Contains fails with empty queue")
        with self.assertRaises(PriorityQueueTypeError):
            priority_queue.contains_priority("7")

        self.assertFalse(priority_queue.contains_element("7"),
                         "Contains_element fails")

        priority_queue = DuplicatePriorityQueue(int, True)
        for i in range(10):
            priority_queue.enqueue(i**2, i)

        for j in range(10):
            self.assertTrue(priority_queue.contains_priority(j),
                            "Wrong contains implementation")
            self.assertTrue(priority_queue.contains_element(j**2),
                            "Contains_element fails")
            self.assertTrue(j**2 in priority_queue, "Contains_element fails")

        with self.assertRaises(PriorityQueueTypeError):
            priority_queue.contains_element("word")

        priority_queue.enqueue(1000, 0)
        priority_queue.enqueue(25, 1)
        self.assertTrue(priority_queue.contains_priority(0),
                        "Wrong contains implementation")
        self.assertTrue(priority_queue.contains_element(1000),
                        "Contains_element fails")
        self.assertTrue(priority_queue.contains_element(0),
                        "Contains_element fails")
        self.assertTrue(priority_queue.contains_element(25),
                        "Contains_element fails")
        self.assertTrue(priority_queue.contains_element(1),
                        "Contains_element fails")