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 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)