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
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
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)
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)
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
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
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
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
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
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
def test_enqueue_empty_queue(): queue = Queue() element = 230 queue.enqueue(element) expected = [230] output = queue.queue assert output == expected
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())
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
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())
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_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')
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)
def test(): q = Queue() assert q.is_empty() is True q.enqueue(4) assert q.items == [4] q.enqueue("dog") assert q.items == ["dog", 4] q.enqueue(True) assert q.items == [True, "dog", 4] assert q.size() == 3 assert q.is_empty() is False q.enqueue(8.4) assert q.items == [8.4, True, "dog", 4] assert q.dequeue() == 4 assert q.dequeue() == "dog" assert q.size() == 2
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')
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])
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)
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())
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
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)
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)
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
def test_q_peek_2(): from src.queue import Queue test_q = Queue() val = 6 test_q.enqueue(val) assert test_q.peek() == val
def test_q_empty_3(): from src.queue import Queue test_q = Queue() test_q.enqueue(2) assert not test_q.is_empty
def test_enqueue(): """Test enqueue.""" q = Queue() q.enqueue(3) assert not q.is_empty()
def test_q_peek_2(): from src.queue import Queue test_q = Queue() val = 6 test_q.enqueue(val) assert test_q.peek() == val
def test_q_empty_3(): from src.queue import Queue test_q = Queue() test_q.enqueue(2) assert not test_q.is_empty
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