Пример #1
0
def test_print_queue_with_elements(capsys):
    queue = Queue([1, 2, 3, 4, 5])
    queue.print()

    expected = "[1, 2, 3, 4, 5]\n"
    output = capsys.readouterr().out
    assert output == expected
 def test_dequeue(self):
     queue = Queue()
     queue.enqueue(55)
     res = queue.dequeue()
     res_1 = 55
     message = "test_dequeue is FAIL"
     self.assertEqual(res, res_1, message)
Пример #3
0
def test_peek_queue_with_elements():
    queue = Queue([1, 2, "Dog"])

    expected = 1
    output = queue.peek()
    assert output == expected
    assert queue.queue[0] == expected
Пример #4
0
def test_dequeue_with_full_queue():
    queue = Queue([2, 4, 1, 4020])

    expected = 2
    output = queue.dequeue()
    assert output == expected
    assert queue.queue[0] == 4
Пример #5
0
def test_print_empty_queue(capsys):
    queue = Queue()
    queue.print()

    expected = "[]\n"
    output = capsys.readouterr().out
    assert output == expected
Пример #6
0
def test_dequeue_with_single_element_queue():
    queue = Queue([2])

    expected = 2
    output = queue.dequeue()
    assert output == expected
    assert len(queue.queue) == 0
Пример #7
0
    def levelorderWalk(self):
        if self.root == None:
            print('empty tree')
        else:
            q = Queue()
            q.enqueue((self.root, 0)) # (node, depth)

            depth_hash = {}
            depth_hash[0] = [(self.root.val, None)] #(node, parent)
            while not q.isempty():
                node_tuple = q.dequeue()
                n = node_tuple[0] # node object
                d = node_tuple[1] # depth value

                if not n.left is None:
                    q.enqueue((n.left, d + 1))
                    if d+1 not in depth_hash:
                        depth_hash[d+1] = [(n.left.val, n.val)]
                    else:
                        depth_hash[d+1].append((n.left.val, n.val))

                if not n.right is None:
                    q.enqueue((n.right, d + 1))
                    if d+1 not in depth_hash:
                        depth_hash[d+1] = [(n.right.val, n.val)]
                    else:
                        depth_hash[d+1].append((n.right.val, n.val))

            for k in depth_hash.keys():
                print('Level '+ str(k) + ': ', end='')
                print(depth_hash[k])
Пример #8
0
def test_enqueue_empty_queue():
    queue = Queue()
    element = 230
    queue.enqueue(element)

    expected = [230]
    output = queue.queue
    assert output == expected
Пример #9
0
class TestQueue(unittest.TestCase):
    def setUp(self):
        self.queue = Queue()

    def tearDown(self):
        self.queue = None

    def test_enqueue(self):
        self.queue.enqueue("foo")
        self.assertTrue(self.queue.queue.head.data == "foo")
        self.assertTrue(self.queue.queue.head.next is None)

        self.queue.enqueue("bar")
        self.assertTrue(self.queue.queue.head.data == "foo")
        self.assertTrue(self.queue.queue.head.next.data == "bar")

    def test_deque(self):
        self.queue.enqueue("foo")
        self.queue.enqueue("bar")

        self.assertTrue(self.queue.queue.head.data == "foo")
        self.assertTrue(self.queue.queue.head.next.data == "bar")

        self.queue.dequeue()
        self.assertTrue(self.queue.queue.head.data == "bar")
        self.assertTrue(self.queue.queue.head.next is None)
Пример #10
0
def test_q_size_3():
    from src.queue import Queue
    test_q = Queue()
    test_q.enqueue(2)
    test_q.enqueue("bob")
    test_q.dequeue()
    assert test_q.size == 1
Пример #11
0
def test_q_deq_2():
    from src.queue import Queue
    test_q = Queue()
    val = 6
    test_q.enqueue(val)
    test_q.enqueue("bob")
    peeked = test_q.peek()
    assert test_q.dequeue() == peeked
Пример #12
0
    def test_lock_usage(self):
        global processed_messages

        queue = Queue(maxsize=1)
        feeder_threads = []
        consumer_threads = []

        for i in range(25):
            feeder_threads.append(
                MessageFeeder(queue,
                              f"{i}.txt", {"content": "file content"},
                              timeout=60))
            feeder_threads[-1].start()

        for i in range(25):
            consumer_threads.append(
                MessageConsumer(queue, acquire_timeout=10, timeout=90))
            consumer_threads[-1].start()
            consumer_threads[-1].done = True

        for t in consumer_threads:
            t: MessageConsumer
            t.join()
            self.assertTrue(t.result)

        self.assertEqual(len(processed_messages), 25)
        for i in range(25):
            self.assertIn(f"{i}.txt", processed_messages)

        for t in feeder_threads:
            t: MessageFeeder
            t.join()
            self.assertTrue(t.result)
Пример #13
0
def test_enqueue_with_multiple_elements():
    queue = Queue()
    queue.enqueue([1, 2, 3])
    queue.enqueue("Horse")
    queue.enqueue(9.321)

    expected = [[1, 2, 3], "Horse", 9.321]
    output = queue.queue
    assert output == expected
Пример #14
0
    def test_enqueue_dequeue_big(self):
        queue = Queue()
        for i in range(10000):
            queue.enqueue(i + 1)

        for i in range(10000):
            queue.enqueue(queue.dequeue())

        self.assertEqual(10000, queue.size())
Пример #15
0
def test_peek():
    """Test peek."""
    q = Queue()
    q.enqueue(3)
    q.enqueue(5)
    q.enqueue(7)
    assert q.peek() == 3
    assert q.peek() == 3
    assert q.peek() == 3
Пример #16
0
    def test_timeout_get(self):

        queue = Queue(maxsize=1)
        t1 = MessageConsumer(queue)
        t1.start()

        time.sleep(1)
        t2 = MessageFeeder(queue, "1.txt", {"content": "file content"})
        t2.start()
        t2.join()
        self.assertTrue(t2.result)

        t1.done = True
        t1.join()
        self.assertTrue(t1.result)

        self.assertEqual(queue.qsize(), 0)
Пример #17
0
def test_dequeue():
    """Test dequeue."""
    q = Queue()
    q.enqueue(3)
    q.enqueue(5)
    q.enqueue(7)
    assert q.dequeue() == 3
    assert q.dequeue() == 5
    assert q.dequeue() == 7
    assert q.dequeue() is None
 def test_is_empty(self):
     test = Queue()
     test.enqueue(24)
     test.enqueue("123")
     test.enqueue(14.78)
     message = "test_is_empty is FAIL"
     self.assertFalse(test.is_empty(), message)
Пример #19
0
def test_q_peek_3():
    from src.queue import Queue
    test_q = Queue()
    val = 6
    test_q.enqueue(val)
    test_q.enqueue("bob")
    assert test_q.peek() == 6
Пример #20
0
def test_q_deq():
    from src.queue import Queue
    test_q = Queue()
    val = 6
    test_q.enqueue(val)
    test_q.enqueue("bob")
    assert test_q.dequeue() == 6
Пример #21
0
def test_q_size_3():
    from src.queue import Queue
    test_q = Queue()
    test_q.enqueue(2)
    test_q.enqueue("bob")
    test_q.dequeue()
    assert test_q.size == 1
Пример #22
0
def test_q_deq_2():
    from src.queue import Queue
    test_q = Queue()
    val = 6
    test_q.enqueue(val)
    test_q.enqueue("bob")
    peeked = test_q.peek()
    assert test_q.dequeue() == peeked
Пример #23
0
    def test_enqueue_dequeue(self):
        queue = Queue()
        values = [10, 20, 30, 40, 50]
        for value in values:
            queue.enqueue(value)

        self.assertEqual(len(values), queue.size())
        for value in values:
            self.assertEqual(value, queue.dequeue())
        self.assertEqual(0, queue.size())
Пример #24
0
def test_q_peek_3():
    from src.queue import Queue
    test_q = Queue()
    val = 6
    test_q.enqueue(val)
    test_q.enqueue("bob")
    assert test_q.peek() == 6
Пример #25
0
def test_q_deq():
    from src.queue import Queue
    test_q = Queue()
    val = 6
    test_q.enqueue(val)
    test_q.enqueue("bob")
    assert test_q.dequeue() == 6
Пример #26
0
    def test_max(self):
        queue = Queue(1)
        queue.put("abc")

        with pytest.raises(Exception) as execinfo:
            queue.put("TEST")
        assert str(execinfo.value) == "QueueFullException"
 def test_enqueue(self):
     queue = Queue()
     queue.enqueue(1)
     queue.enqueue(2)
     queue.enqueue(3)
     res = []
     count = 0
     while count < 3:
         res.append(queue.dequeue())
         count += 1
     res_1 = [1, 2, 3]
     message = "test_enqueue is FAIL"
     self.assertEqual(res, res_1, message)
Пример #28
0
    def test_timeout_get_by_acquire_timeout(self):

        queue = Queue(maxsize=1)
        t1 = MessageFeeder(queue, "1.txt", {"content": "file content"})
        t1.start()
        t1.join()
        self.assertTrue(t1.result)
        self.assertEqual(queue.qsize(), 1)

        t2 = MessageConsumer(queue)
        t2.start()
        t2.delete_on_finish = False
        t2.done = True

        t3 = MessageConsumer(queue)
        t3.start()
        t3.done = True

        t2.join()
        t3.join()

        self.assertTrue(t2.result)
        self.assertTrue(t3.result)
        self.assertEqual(queue.qsize(), 0)
Пример #29
0
 def find(self, fp, orig, dest):
     orig, dest = orig.get(), dest.get()
     self.field = self._field.copy()
     self.field = dilate(self.field, fp.get_morph_kernel()).astype(int)
     self.field[self.field == 1] = -1
     f = self.field
     if self.acheck(f, dest):
         dest = self._find_nearest_free(f, dest)
     self.orig, self.dest = orig, dest
     self.queue = Queue()
     self.queue.add(orig)
     self.sources = self.amake_2d(None, self.size)
     s = self.sources
     self.aset(f, self.dest, -3)
     self.aset(s, self.orig, -2)
     all_cards = [(0, 1), (1, 0), (-1, 0), (0, -1)]
     all_diags = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
     self.checked = []
     while not self.queue.is_empty():
         current = self.queue.pop()
         if current in self.checked:
             continue
         self.checked += [current]
         try:
             for delta in all_cards:
                 if not self._is_bwds(self.aget(s, current), current,
                                      delta):
                     self._expand_card(current, delta)
             for delta in all_diags:
                 if not self._is_bwds(self.aget(s, current), current,
                                      delta):
                     self._expand_diag(current, delta)
         except FoundPath:
             break
     nodes = self._find_nodes()
     return nodes  #self._reconstruct(nodes)
Пример #30
0
    def test_enqueue(self):
        q = Queue()

        q.enqueue(1)
        self.assertEqual(q.head['value'], q.tail['value'], 'tail and head are the same')

        q.enqueue(2)
        self.assertEqual(q.head['value'], 2, 'head is now the new value')
        self.assertIsNone(q.head['prev'], 'head is not linked to the next node')
        self.assertEqual(q.tail['value'], 1, 'first value is now tail')
        self.assertEqual(q.tail['prev']['value'], 2, 'tail is linked to head')
Пример #31
0
    def test_dequeue(self):
        q = Queue()
        q.enqueue(1)
        q.enqueue(2)

        value = q.dequeue()
        self.assertEqual(value, 1, 'should return the first inserted item')
        self.assertEqual(q.head['value'], q.tail['value'],
            'tail and head are the same after an dequeue')

        value = q.dequeue()
        self.assertEqual(value, 2, 'should return the second inserted item')
        self.assertIsNone(q.head, 'head is None')
        self.assertIsNone(q.tail, 'tail is None')
Пример #32
0
    def test_rotate(self):
        # Format: Key - offset, value - expected state
        cases = {
            1: [20, 30, 40, 50, 60, 10],
            3: [40, 50, 60, 10, 20, 30],
            8: [30, 40, 50, 60, 10, 20],
            -1: [60, 10, 20, 30, 40, 50],
            -2: [50, 60, 10, 20, 30, 40],
            0: [10, 20, 30, 40, 50, 60],
            12: [10, 20, 30, 40, 50, 60],
        }
        for offset in cases.keys():
            queue = Queue()
            values = [10, 20, 30, 40, 50, 60]
            for value in values:
                queue.enqueue(value)
            queue.rotate(offset)

            expected_values = cases[offset]
            self.assertEqual(len(values), queue.size())
            for value in expected_values:
                self.assertEqual(value, queue.dequeue())
Пример #33
0
    def bfs(self, s):
        # initialize
        visited, distance, parent = {}, {}, {}
        for v in self.V:
            visited[v] = 0
            distance[v] = np.NaN
            parent[v] = None

        distance[s] = 0

        # keep a Queue for un-visited nodes
        q = Queue()
        q.enqueue(s)

        while not q.isempty():
            u = q.dequeue()
            print(u, parent[u], distance[u])
            visited[u] = 1
            for v in self.E[u].keys():
                if not visited[v]:
                    visited[v] = 1
                    distance[v] = distance[u] + self.E[u][v]
                    parent[v] = u
                    q.enqueue(v)
Пример #34
0
def testQueue():
    '''
    Here we test algorithms for queues
    We test enqueue, dequeue, and checking if 
    a queue is empty

    Queue is implemented as a python list
    It's not the best implementation as we'll
    show that the queue will be empty yet it 
    will be occupying memory with the previously 
    inserted elements
    '''

    print('Create an empty queue')
    q = Queue()
    print('Dequeue now')
    print('Dequeued element: ' + str(q.dequeue()))
    print('Insert 5')
    q.enqueue(5)
    print('Insert 3')
    q.enqueue(3)
    print('Insert 4')
    q.enqueue(4)
    print('Is queue empty: ' + str(q.isempty()))
    print('Position of head: ' + str(q.head))
    print('Position of tail: ' + str(q.tail))
    print('Dequeue now')
    print('Dequeued element: ' + str(q.dequeue()))
    print('Dequeue now')
    print('Dequeued element: ' + str(q.dequeue()))
    print('Dequeue now')
    print('Dequeued element: ' + str(q.dequeue()))
    print('Dequeue now')
    print('Dequeued element: ' + str(q.dequeue()))
    print('Queue in the memory')
    print(q.storage)
    print('Insert 6')
    q.enqueue(6)
    print('Dequeue now')
    print('Dequeued element: ' + str(q.dequeue()))
    print('Queue in the memory')
    print(q.storage)
Пример #35
0
def test_q_peek_2():
    from src.queue import Queue
    test_q = Queue()
    val = 6
    test_q.enqueue(val)
    assert test_q.peek() == val
Пример #36
0
def test_q_empty_3():
    from src.queue import Queue
    test_q = Queue()
    test_q.enqueue(2)
    assert not test_q.is_empty
Пример #37
0
def test_q_empty_2():
    from src.queue import Queue
    test_q = Queue()
    test_q.enqueue(2)
    test_q.dequeue()
    assert test_q.is_empty
Пример #38
0
def test_q_deq_3():
    from src.queue import Queue
    test_q = Queue()
    with pytest.raises(AttributeError):
        test_q.dequeue()
Пример #39
0
def test_q_peek():
    from src.queue import Queue
    test_q = Queue()
    assert test_q.peek() is None