class QueueTestCase(unittest.TestCase): def setUp(self): self.emptyqueue = Queue() self.queue = Queue() for letter in "abcdefghijklmnopqrstuvwxyz": self.queue.enqueue(letter) def test_len(self): self.assertEqual(len(self.queue), 26, "Queue length not 26") self.assertEqual(len(self.emptyqueue), 0, "Empty queue length not 0") self.queue.enqueue("aa") self.assertEqual(len(self.queue), 27, "Queue length not 27 after enqueue") self.queue.dequeue() self.assertEqual(len(self.queue), 26, "Queue length not 26 after dequeue") self.queue.make_empty() self.assertEqual(len(self.queue), 0, "Queue length not 0 after make_empty") def test_str(self): letters = "abcdefghijklmnopqrstuvwxyz" string = str([letter for letter in letters]) self.assertEqual(str(self.queue), string, "Queue string not equal") self.assertEqual(str(self.emptyqueue), str([]), "Empty queue string not equal") def test_bool(self): self.assertTrue(self.queue, "Queue unexpectedly false") self.assertFalse(self.emptyqueue, "Empty queue unexpectedly true") def test_enqueue(self): item = "aa" self.assertNotIn(item, self.queue._data) self.queue.enqueue(item) self.assertIn(item, self.queue._data) self.assertEqual(item, self.queue._data[-1]) def test_dequeue(self): self.assertEqual(self.queue.dequeue(), "a") self.assertNotIn("a", self.queue._data) with self.assertRaises(IndexError): self.emptyqueue.dequeue() def test_peek(self): self.assertEqual(self.queue.peek(), "a") self.assertIn("a", self.queue._data) with self.assertRaises(IndexError): self.emptyqueue.peek() def test_make_empty(self): self.assertTrue(self.queue) self.queue.make_empty() self.assertEqual(self.queue._data, [])
def DS_queue(self): try: length = int(input(" Enter the length of your Queue: ")) except: return self.DS_queue() # Queue instantiation with the given length queue = Queue(length) while True: print( '\n [QUEUE OPERATIONS] \n\n 1. Enqueue\n 2. Dequeue\n 3. Display\n 4. Count\n 5. Back\n' ) try: selectedNumber = int(input("Select a number: ")) except: print("\nPlease enter a valid input\n") input("Press [Enter] to continue...") self.DS_queue() if selectedNumber == 1: value = int( input("Enter the value that you want to enqueue: ")) queue.enqueue(value) print("Success!") input("Press [Enter] to continue...") elif selectedNumber == 2: queue.dequeue() print("Success!") input("Press [Enter] to continue...") elif selectedNumber == 3: queue.display() input("Press [Enter] to continue...") elif selectedNumber == 4: queue.countQueue() input("Press [Enter] to continue...") elif selectedNumber == 5: self.DS_main() else: print( "The number you entered is not in the choices... Going back to the main screen instead..." ) input("Press [Enter] to continue...") self.DS_main()
def test_queue(self): queue = Queue(5) # Check instantiation default values self.assertEqual(queue.front, -1) self.assertEqual(queue.rear, -1) self.assertEqual(queue.length, 5) self.assertEqual(queue.data, [0,0,0,0,0]) self.assertEqual(queue.count, 0) # Check if isEmpty() self.assertTrue(queue.isEmpty()) # Check if isFull() self.assertFalse(queue.isFull()) # Check if enqueue operation is working properly queue.enqueue(5) self.assertEqual(queue.data, [5,0,0,0,0]) # Check count again if incremented self.assertEqual(queue.count, 1) # Check if front and rear is incremented self.assertEqual(queue.front, 0) self.assertEqual(queue.rear, 0) # Enqueue again to check if rear is working properly queue.enqueue(6) self.assertEqual(queue.rear, 1) self.assertEqual(queue.count, 2) # Check is countQueue() is working properly self.assertEqual(queue.countQueue(), 2) # Check if dequeue() is working properly queue.dequeue() self.assertEqual(queue.data, [0,6,0,0,0]) self.assertEqual(queue.count, 1)
def bfs(node, goal): print "Expanding Nodes.." Q = Queue() Q.queue(node) count = 0 visited = [] while Q.size(): current = Q.dequeue() count += 1 #print count for node in moveGen(current): if node.state not in visited: visited.append(node.state) if node.state == goal.state: return (node) Q.queue(node)
def Breadthfirst(self): self.__setup() queue = Queue() self.finalPath = None pathFound = False temp = None if self.board[self.startRow][self.startCol].getType() == "START": queue.enqueue(self.board[self.startRow][self.startCol]) queue.getFront().getData().setVisited(True) while not queue.isEmpty() and not pathFound: temp = queue.dequeue().getData() if temp.getType() == "FINISH": pathFound = True else: if temp.getRow() > 0 and self.board[temp.getRow() - 1][temp.getCol()].getType() != "WALL"\ and self.board[temp.getRow() - 1][temp.getCol()].getVisited() == False: self.board[temp.getRow() - 1][temp.getCol() ].setVisited(True) self.board[temp.getRow() - 1][temp.getCol() ].setPrev(temp) queue.enqueue( self.board[temp.getRow() - 1][temp.getCol()]) if temp.getRow() < self.numRow - 1\ and self.board[temp.getRow() + 1][temp.getCol()].getType() != "WALL"\ and self.board[temp.getRow() + 1][temp.getCol()].getVisited() == False: self.board[temp.getRow() + 1][temp.getCol() ].setVisited(True) self.board[temp.getRow() + 1][temp.getCol() ].setPrev(temp) queue.enqueue( self.board[temp.getRow() + 1][temp.getCol()]) if temp.getCol() > 0 and self.board[temp.getRow()][temp.getCol() - 1].getType() != "WALL"\ and self.board[temp.getRow()][temp.getCol() - 1].getVisited() == False: self.board[temp.getRow()][temp.getCol() - 1].setVisited(True) self.board[temp.getRow()][temp.getCol() - 1].setPrev(temp) queue.enqueue( self.board[temp.getRow()][temp.getCol() - 1]) if temp.getCol() < self.numCol - 1\ and self.board[temp.getRow()][temp.getCol() + 1].getType() != "WALL"\ and self.board[temp.getRow()][temp.getCol() + 1].getVisited() == False: self.board[temp.getRow()][temp.getCol() + 1].setVisited(True) self.board[temp.getRow()][temp.getCol() + 1].setPrev(temp) queue.enqueue( self.board[temp.getRow()][temp.getCol() + 1]) pathfinder_gui.showQueue(temp, queue) if pathFound: self.finalPath = Stack() while temp != None: self.finalPath.push(temp) temp = temp.getPrev() pathfinder_gui.showPath(self.finalPath)