Exemple #1
0
 def test_queue_dequeue(self):
     capacity = 10
     q = Queue(capacity=capacity)
     for i in range(capacity):
         q.enqueue(i)
     for i in range(capacity):
         self.assertEqual(i, q.dequeue())
 def test_queue_size_matches_number_of_enqueues(self):
     queue = Queue()
     assert len(queue) == 0
     queue.enqueue(0)
     assert len(queue) == 1
     queue.enqueue(1)
     queue.enqueue(2)
     assert len(queue) == 3
 def test_front_of_non_empty_queue_returns_first_in_value(self):
     queue = Queue()
     queue.enqueue(0)
     queue.enqueue(1)
     queue.enqueue(2)
     assert queue.front() == 0
     queue.dequeue()
     assert queue.front() == 1
 def test_is_empty(self):
   """
   Test for is_empty method
   """
   queue = Queue()
   self.assertEqual(queue.is_empty(), True)
   queue.enqueue(1)
   self.assertEqual(queue.is_empty(), False)
 def test_size(self):
   """
   Test for size method
   """
   queue = Queue()
   self.assertEqual(queue.size(), 0)
   queue.enqueue(1)
   self.assertEqual(queue.size(), 1)
    def test_dequeue_empty_queue_raises_empty_queue_exception(self):
        queue = Queue()
        with raises(Queue.EmptyQueueException):
            queue.dequeue()

        queue = Queue()
        queue.enqueue(0)
        queue.dequeue()
        with raises(Queue.EmptyQueueException):
            queue.dequeue()
 def test_enqueue(self):
   """
   Test for enqueue method
   """
   queue = Queue()
   self.assertEqual(queue.size(), 0)
   queue.enqueue(1)
   queue.enqueue(2)
   queue.enqueue(3)
   self.assertEqual(queue.size(), 3)
Exemple #8
0
 def test_has_more(self):
     capacity = 10
     q = Queue(capacity=capacity)
     self.assertEqual(False, q.has_more())
     for i in range(capacity):
         q.enqueue(i)
         self.assertEqual(True, q.has_more())
     for i in range(capacity):
         self.assertEqual(True, q.has_more())
         q.dequeue()
     self.assertEqual(False, q.has_more())
 def test_dequeue(self):
   """
   Test for dequeue method
   """
   queue = Queue()
   self.assertEqual(queue.dequeue(), None)
   queue.enqueue(1)
   queue.enqueue(2)
   queue.enqueue(3)
   self.assertEqual(queue.dequeue(), 1)
   self.assertEqual(queue.size(), 2)
 def test_peek(self):
   """
   Test for peek method
   """
   queue = Queue()
   self.assertEqual(queue.peek(), None)
   queue.enqueue(1)
   queue.enqueue(2)
   queue.enqueue(3)
   self.assertEqual(queue.peek(), 1)
   self.assertEqual(queue.size(), 3)
    def test_is_empty(self):
        print('Test: test_is_empty')
        queue = Queue()

        print('Test: Empty queue')
        self.assertEqual(queue.is_empty(), True)

        print('Test: Not an empty queue')
        queue.enqueue(1)
        self.assertEqual(queue.is_empty(), False)
        print('Success: test_is_empty')
    def test_peek(self):
        print('Test: test_peek')
        queue = Queue()

        print('Test: Peek to an empty queue')
        self.assertEqual(queue.peek(), None)

        print('Test: General case')
        queue.enqueue(1)
        queue.enqueue(2)
        self.assertEqual(queue.peek(), 1)
        print('Success: test_peek')
    def test_queue_size_is_reduced_by_dequeue(self):
        queue = Queue()
        queue.enqueue(0)
        queue.enqueue(1)
        queue.enqueue(2)
        assert len(queue) == 3

        queue.dequeue()
        assert len(queue) == 2
        queue.dequeue()
        queue.dequeue()
        assert len(queue) == 0
def bfs(graph, start):
    start.set_distance(0)
    start.set_predecessor(None)
    vertex_queue = Queue()
    vertex_queue.enqueue(start)

    while vertex_queue.size() > 0:
        current_vertex = vertex_queue.dequeue()
        for nbr in current_vertex.get_connections():
            if nbr.get_color() == 'white':
                nbr.set_color('gray')
                nbr.set_distance(current_vertex.get_distance() + 1)
                nbr.set_predecessor(current_vertex)
                vertex_queue.enqueue(nbr)
        current_vertex.set_color('black')
def play_hot_potato_game(items, reps):
    queue = Queue()

    # O(n)
    for item in items:
        queue.enqueue(item)

    # O(n - 1)
    while queue.size() > 1:
        # O(#reps)
        for i in range(reps):
            first = queue.dequeue()
            print(first)

            queue.enqueue(first)
        print('-' * 10)
        print('Removing {}'.format(queue.dequeue()))

    return queue.dequeue()
 def test_rear_of_non_empty_queue_returns_last_in_value(self):
     queue = Queue()
     queue.enqueue(0)
     assert queue.rear() == 0
     queue.enqueue(1)
     assert queue.rear() == 1
     queue.enqueue(2)
     assert queue.rear() == 2
    def test_enqueue(self):
        print('Test: test_enqueue')
        queue = Queue()

        print('Test: None input')
        self.assertRaises(ValueError, queue.enqueue, None)

        print('Test: Enqueue to an empty queue')
        queue.enqueue(1)
        self.assertEqual(queue._front.data, 1)
        self.assertEqual(queue._rear.data, 1)

        print('Test: General case')
        queue.enqueue(2)
        self.assertEqual(queue._rear.data, 2)
        queue.enqueue(3)
        self.assertEqual(queue._rear.data, 3)
        queue.enqueue(4)
        self.assertEqual(queue._rear.data, 4)
        print('Success: test_enqueue')
Exemple #18
0
    def _level_order_print(self, start):
        if start is None:
            return

        queue = Queue()
        queue.enqueue(start)

        traversal = ""
        while len(queue) > 0:
            traversal += str(queue.peek().value) + "-"
            node = queue.dequeue()

            if node.left:
                queue.enqueue(node.left)
            if node.right:
                queue.enqueue(node.right)

        return traversal
    def test_dequeue(self):
        print('Test: test_dequeue')
        queue = Queue()

        print('Test: Dequeue an empty queue')
        self.assertEqual(queue.dequeue(), None)

        print('Test: Dequeue a queue with one element')
        queue.enqueue(1)
        self.assertEqual(queue.dequeue(), 1)
        self.assertEqual(queue._front, None)
        self.assertEqual(queue._rear, None)

        print('Test: General case')
        queue.enqueue(1)
        queue.enqueue(2)
        queue.enqueue(3)
        self.assertEqual(queue.dequeue(), 1)
        self.assertEqual(queue.dequeue(), 2)
        self.assertEqual(queue.dequeue(), 3)
        print('Success: test_dequeue')
Exemple #20
0
    def _reverse_level_order_print(self, start):
        if start is None:
            return

        queue = Queue()
        stack = Stack()
        queue.enqueue(start)

        traversal = ""
        while len(queue) > 0:
            node = queue.dequeue()

            stack.push(node)

            if node.right:
                queue.enqueue(node.right)
            if node.left:
                queue.enqueue(node.left)

        while len(stack) > 0:
            node = stack.pop()
            traversal += str(node.value) + "-"

        return traversal
class QueueTests(unittest.TestCase):
    def setUp(self):
        self.q = Queue()

    def test_len_returns_0_for_empty_queue(self):
        self.assertEqual(len(self.q), 0)

    def test_len_returns_correct_length_after_enqueue(self):
        self.assertEqual(len(self.q), 0)
        self.q.enqueue(2)
        self.assertEqual(len(self.q), 1)
        self.q.enqueue(4)
        self.assertEqual(len(self.q), 2)
        self.q.enqueue(6)
        self.q.enqueue(8)
        self.q.enqueue(10)
        self.q.enqueue(12)
        self.q.enqueue(14)
        self.q.enqueue(16)
        self.q.enqueue(18)
        self.assertEqual(len(self.q), 9)

    def test_empty_dequeue(self):
        self.assertIsNone(self.q.dequeue())
        self.assertEqual(len(self.q), 0)

    def test_dequeue_respects_order(self):
        self.q.enqueue(100)
        self.q.enqueue(101)
        self.q.enqueue(105)
        self.assertEqual(self.q.dequeue(), 100)
        self.assertEqual(len(self.q), 2)
        self.assertEqual(self.q.dequeue(), 101)
        self.assertEqual(len(self.q), 1)
        self.assertEqual(self.q.dequeue(), 105)
        self.assertEqual(len(self.q), 0)
        self.assertIsNone(self.q.dequeue())
        self.assertEqual(len(self.q), 0)
Exemple #22
0
 def test_exception(self):
     q = Queue(capacity=1)
     q.enqueue(1)
     self.assertRaises(Exception, q.enqueue, (1, ))
     q.dequeue()
     self.assertRaises(Exception, q.dequeue)
Exemple #23
0
def test_enqueue(object_under_test: Queue):
    object_under_test.enqueue(4)
 def test_queue_is_not_empty_after_enqueue(self):
     queue = Queue()
     queue.enqueue(0)
     assert not queue.empty()