Example #1
0
    def test_iterator(self):
        queue = Queue(str)
        words = ["word", "python", "java", "csharp"]
        index = 0

        for word in queue:
            self.assertEqual(word, words[index],
                             "Wrong iterator implementation")
            index += 1

        self.assertEqual(len(queue), 0)
Example #2
0
    def test_peek(self):
        queue = Queue()
        self.assertEqual(
            queue.peek(), None,
            "Top element of queue should be None at initialization")
        queue.enqueue(2)
        queue.enqueue("Tests")
        self.assertEqual(queue.peek(), 2, "Queue gives wrong peek")

        queue = Queue(int)
        self.assertEqual(
            queue.peek(), None,
            "Top element of queue should be None at initialization")
        queue.enqueue(23)
        self.assertEqual(queue.peek(), 23, "Queue gives wrong peek")
        queue.dequeue()
        queue.enqueue(57)
        queue.enqueue(0)
        self.assertEqual(queue.peek(), 57, "Queue gives wrong peek")
Example #3
0
    def test_empty(self):
        queue = Queue()
        self.assertTrue(queue.size == 0, "Queue should be empty")
        queue.enqueue("word")
        queue.enqueue("sentence")
        queue.dequeue()
        self.assertFalse(queue.size == 0, "Queue should not be empty")

        queue = Queue(float)
        self.assertTrue(queue.size == 0, "Queue should be empty")
        queue.enqueue(2.5)
        queue.peek()
        queue.enqueue(1.54)
        queue.dequeue()
        self.assertFalse(queue.size == 0, "Queue should not be empty")
Example #4
0
    def test_str(self):
        queue = Queue()
        self.assertEqual(str(queue), "deque([])", "Wrong str representation")

        queue.enqueue(1)
        queue.enqueue(2.5)
        self.assertEqual(str(queue), "deque([1, 2.5])",
                         "Wrong str representation")
        queue.peek()
        queue.enqueue("str")
        self.assertEqual(str(queue), "deque([1, 2.5, 'str'])",
                         "Wrong str representation")

        for i in range(10):
            queue.enqueue(i)
        queue.dequeue()
        self.assertEqual(str(queue),
                         "deque(" + str([2.5, 'str'] + list(range(10))) + ")")

        queue = Queue(int)
        self.assertEqual(str(queue), "deque([])", "Wrong str representation")
        for i in range(5):
            queue.enqueue(i)
        queue.peek()
        self.assertEqual(str(queue), "deque([0, 1, 2, 3, 4])",
                         "Wrong str representation")
        for i in range(10):
            queue.enqueue(i)
        self.assertEqual(
            str(queue), "deque(" + str(list(range(5)) + list(range(10))) + ")")
Example #5
0
    def test_size(self):
        queue = Queue()
        self.assertEqual(queue.size, 0,
                         "Queue size should be 0 at initialization")
        for i in range(1, 41):
            queue.enqueue(i)
            queue.enqueue(i + 1)
            queue.dequeue()
        self.assertEqual(queue.size, 40, "Incorrect queue size")

        queue = Queue(str)
        self.assertEqual(queue.size, 0,
                         "Queue size should be 0 at initialization")
        queue.enqueue("b")
        self.assertEqual(queue.size, 1, "Incorrect queue size")
        queue.enqueue("c")
        queue.peek()
        self.assertEqual(queue.size, 2, "Incorrect queue size")
Example #6
0
    def test_remove(self):
        queue = Queue()
        with self.assertRaises(QueueElementError):
            queue.remove(5)

        queue.enqueue(5)
        queue.enqueue("str")
        queue.enqueue(5.5)
        self.assertEqual(queue.peek(), 5)
        self.assertEqual(len(queue), 3)
        queue.remove(5)
        self.assertEqual(len(queue), 2)
        self.assertEqual(queue.peek(), "str", "Wrong remove implementation")

        queue = Queue(int)
        with self.assertRaises(QueueTypeError):
            queue.remove("string")
        for i in range(10):
            queue.enqueue(i)
        queue.remove(0)
        queue.remove(1)
        self.assertEqual(str(queue), "deque(" + str(list(range(2, 10))) + ")",
                         "Wrong remove implementation")
        queue.remove(5)
        queue.remove(9)
        self.assertEqual(str(queue), "deque([2, 3, 4, 6, 7, 8])",
                         "Wrong remove implementation")
        self.assertEqual(queue.dequeue(), 2, "Wrong remove implementation")
Example #7
0
    def test_contains(self):
        queue = Queue()
        self.assertFalse(0 in queue, "Wrong contains implementation")

        queue.enqueue(1)
        queue.enqueue(10)
        self.assertEqual(10 in queue, queue.contains(10))
        self.assertTrue(10 in queue, "Wrong contains implementation")

        queue = Queue(float)
        queue.enqueue(2.3)
        queue.enqueue(3.7)
        queue.dequeue()
        queue.peek()
        self.assertTrue(3.7 in queue)
        self.assertFalse(2.3 in queue)
Example #8
0
    def test_type(self):
        queue = Queue()
        self.assertEqual(queue.type, None)

        with self.assertRaises(QueueTypeError):
            _ = Queue(elements_type="type")

        queue = Queue(elements_type=list)
        self.assertEqual(queue.type, list)

        with self.assertRaises(QueueTypeError):
            queue.enqueue("hey")

        queue = Queue(elements_type=str)
        queue.enqueue("hello")
        queue.enqueue("world")
        test_string = queue.dequeue() + " " + queue.dequeue()
        self.assertEqual(test_string, "hello world",
                         "Queue with strings doesn't dequeue correctly")

        with self.assertRaises(QueueTypeError):
            queue.enqueue(123)
Example #9
0
    def test_dequeue(self):
        queue = Queue()
        with self.assertRaises(EmptyQueueError):
            queue.dequeue()

        queue.enqueue(4)
        queue.enqueue("word")
        self.assertEqual(queue.dequeue(), 4, "Wrong queue implementation")

        queue = Queue(tuple)
        with self.assertRaises(EmptyQueueError):
            queue.dequeue()

        queue.enqueue((2, ))
        queue.enqueue((1, 2, 3))
        self.assertEqual(queue.dequeue()[0], 2, "Wrong dequeue implementation")
Example #10
0
    def test_enqueue(self):
        queue = Queue()
        queue.enqueue([1, 2, 3])
        queue.enqueue("item")
        self.assertEqual(queue.dequeue()[1], 2, "Queue dequeues wrong element")
        self.assertEqual(len(queue), 1, "Wrong enqueue implementation")

        queue = Queue(str)
        with self.assertRaises(QueueTypeError):
            queue.enqueue(5)
        self.assertEqual(len(queue), 0)
        self.assertTrue(queue.size == 0)

        queue.enqueue("2")
        queue.enqueue("word")
        self.assertEqual(len(queue), 2, "Wrong enqueue implementation")
        self.assertFalse(queue.size == 0, "Wrong enqueue implementation")
        self.assertEqual(queue.dequeue(), "2", "Wrong enqueue implementation")