2015-11-08 09:31:58 +01:00
|
|
|
import unittest
|
|
|
|
|
|
2015-11-16 17:36:54 +01:00
|
|
|
from robot_simulator import Robot, NORTH, EAST, SOUTH, WEST
|
2015-11-08 09:31:58 +01:00
|
|
|
|
|
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json` @ v3.1.0
|
2017-10-27 20:46:14 +01:00
|
|
|
|
2018-06-13 09:12:09 -04:00
|
|
|
class RobotSimulatorTest(unittest.TestCase):
|
2018-10-23 14:45:49 +02:00
|
|
|
|
|
|
|
|
def test_robot_created_with_position_and_direction(self):
|
|
|
|
|
robot = Robot(NORTH, 0, 0)
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.bearing, NORTH)
|
2015-11-16 17:36:54 +01:00
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
def test_robot_created_with_negative_position_values(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(SOUTH, -1, 1)
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (-1, 1))
|
|
|
|
|
self.assertEqual(robot.bearing, SOUTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
def test_rotate_turn_right(self):
|
2018-10-03 19:35:52 +02:00
|
|
|
dirA = [EAST, SOUTH, WEST, NORTH]
|
|
|
|
|
dirB = [SOUTH, WEST, NORTH, EAST]
|
|
|
|
|
for x in range(len(dirA)):
|
|
|
|
|
robot = Robot(dirA[x], 0, 0)
|
2015-11-08 09:31:58 +01:00
|
|
|
robot.turn_right()
|
2018-10-03 19:35:52 +02:00
|
|
|
self.assertEqual(robot.bearing, dirB[x])
|
|
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
def test_rotate_simulate_R(self):
|
2018-10-03 19:35:52 +02:00
|
|
|
A = [NORTH, EAST, SOUTH, WEST]
|
|
|
|
|
B = [EAST, SOUTH, WEST, NORTH]
|
|
|
|
|
for x in range(len(A)):
|
|
|
|
|
robot = Robot(A[x], 0, 0)
|
|
|
|
|
robot.simulate("R")
|
|
|
|
|
self.assertEqual(robot.bearing, B[x])
|
|
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
def test_rotate_simulate_L(self):
|
2018-10-03 19:35:52 +02:00
|
|
|
A = [NORTH, WEST, SOUTH, EAST]
|
|
|
|
|
B = [WEST, SOUTH, EAST, NORTH]
|
|
|
|
|
for x in range(len(A)):
|
|
|
|
|
robot = Robot(A[x], 0, 0)
|
|
|
|
|
robot.simulate("L")
|
|
|
|
|
self.assertEqual(robot.bearing, B[x])
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
def test_rotate_turn_left(self):
|
2018-10-03 19:35:52 +02:00
|
|
|
dirA = [EAST, SOUTH, WEST, NORTH]
|
|
|
|
|
dirB = [NORTH, EAST, SOUTH, WEST]
|
|
|
|
|
for x in range(len(dirA)):
|
|
|
|
|
robot = Robot(dirA[x], 0, 0)
|
2015-11-08 09:31:58 +01:00
|
|
|
robot.turn_left()
|
2018-10-03 19:35:52 +02:00
|
|
|
self.assertEqual(robot.bearing, dirB[x])
|
2015-11-08 09:31:58 +01:00
|
|
|
|
|
|
|
|
def test_advance_positive_north(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(NORTH, 0, 0)
|
2015-11-08 09:31:58 +01:00
|
|
|
robot.advance()
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (0, 1))
|
|
|
|
|
self.assertEqual(robot.bearing, NORTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
|
|
|
|
def test_advance_negative_south(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(SOUTH, 0, 0)
|
2015-11-08 09:31:58 +01:00
|
|
|
robot.advance()
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (0, -1))
|
|
|
|
|
self.assertEqual(robot.bearing, SOUTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2017-10-27 20:46:14 +01:00
|
|
|
def test_advance_positive_east(self):
|
|
|
|
|
robot = Robot(EAST, 0, 0)
|
|
|
|
|
robot.advance()
|
|
|
|
|
self.assertEqual(robot.coordinates, (1, 0))
|
|
|
|
|
self.assertEqual(robot.bearing, EAST)
|
|
|
|
|
|
|
|
|
|
def test_advance_negative_west(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(WEST, 0, 0)
|
2015-11-08 09:31:58 +01:00
|
|
|
robot.advance()
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (-1, 0))
|
|
|
|
|
self.assertEqual(robot.bearing, WEST)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
def test_move_east_north_from_README(self):
|
|
|
|
|
robot = Robot(NORTH, 7, 3)
|
|
|
|
|
robot.simulate("RAALAL")
|
|
|
|
|
self.assertEqual(robot.coordinates, (9, 4))
|
|
|
|
|
self.assertEqual(robot.bearing, WEST)
|
|
|
|
|
|
|
|
|
|
def test_move_west_north(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(NORTH, 0, 0)
|
2015-11-08 09:31:58 +01:00
|
|
|
robot.simulate("LAAARALA")
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (-4, 1))
|
|
|
|
|
self.assertEqual(robot.bearing, WEST)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
def test_move_west_south(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(EAST, 2, -7)
|
2015-11-08 09:31:58 +01:00
|
|
|
robot.simulate("RRAAAAALA")
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (-3, -8))
|
|
|
|
|
self.assertEqual(robot.bearing, SOUTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
def test_move_east_north(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(SOUTH, 8, 4)
|
2015-11-08 09:31:58 +01:00
|
|
|
robot.simulate("LAAARRRALLLL")
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (11, 5))
|
|
|
|
|
self.assertEqual(robot.bearing, NORTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2016-11-29 09:44:47 +01:00
|
|
|
|
2015-11-08 09:31:58 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|