diff --git a/02_UC_ObstacleDetection.txt b/02_UC_ObstacleDetection.txt new file mode 100644 index 0000000000000000000000000000000000000000..bf72cf00f99a6f736d5dbc99b06c8014c29d93f8 --- /dev/null +++ b/02_UC_ObstacleDetection.txt @@ -0,0 +1,55 @@ +You are a tester tasked with creating comprehensive test cases for a given usecase description. + +## Usecase description + +{ + "name": "Rover Obstacle Detection", + "scenario": "The rover must stop when it detects an obstacle in its path.", + "actors": "MissionControl", + "preconditions": "Rover is landed on the plateau at position (0,0)", + "steps": [ + "MissionControl sends a movement command to the rover", + "Rover checks each step for obstacles using its obstacle detection system (ODS)", + "If an obstacle is detected, the rover stops before moving into it", + "Remaining commands in the sequence are ignored", + "Rover returns only the successfully executed commands" + ] +} + +## Testcase + +[ + { + "name": "Rover Stops at First Obstacle", + "description": "Verify that the rover stops immediately when the first obstacle is detected", + "input": { + "command": "FFRFF" + }, + "expected": { + "outcome": "Obstacle detected; rover stopped", + "executedCommands": "FF" + } + }, + { + "name": "Rover Avoids Obstacle and Continues", + "description": "Verify that rover only moves until just before the obstacle", + "input": { + "command": "FRFRF" + }, + "expected": { + "outcome": "Obstacle detected mid-sequence", + "executedCommands": "FRF" + } + }, + { + "name": "Rover Encounters No Obstacles", + "description": "Verify that the rover executes all commands when no obstacles are present", + "input": { + "command": "FFLFF" + }, + "expected": { + "outcome": "Rover moved successfully", + "executedCommands": "FFLFF" + } + } +] diff --git a/backuprover2.py b/backuprover2.py new file mode 100644 index 0000000000000000000000000000000000000000..6158277a9ceb630f94da7490bbf8e23516aeafc1 --- /dev/null +++ b/backuprover2.py @@ -0,0 +1,44 @@ +# Adds obstacle detection to rover1.py + +DIRECTIONS = ['N', 'E', 'S', 'W'] +MOVES = { + 'N': (0, 1), + 'E': (1, 0), + 'S': (0, -1), + 'W': (-1, 0) +} + +class Rover: + def __init__(self, obstacles=None): + self.x = 0 + self.y = 0 + self.heading = 0 + self.obstacles = obstacles if obstacles else set() + + def drive(self, command_string): + executed = "" + for cmd in command_string: + if cmd in ['F', 'B']: + dx, dy = MOVES[DIRECTIONS[self.heading]] + if cmd == 'B': + dx, dy = -dx, -dy + next_x = self.x + dx + next_y = self.y + dy + if (next_x, next_y) in self.obstacles: + break + self.x = next_x + self.y = next_y + elif cmd == 'L': + self.heading = (self.heading - 1) % 4 + elif cmd == 'R': + self.heading = (self.heading + 1) % 4 + else: + break + executed += cmd + return executed + + def get_position(self): + return (self.x, self.y) + + def get_heading(self): + return DIRECTIONS[self.heading] diff --git a/test_rover2.py b/test_rover2.py new file mode 100644 index 0000000000000000000000000000000000000000..c682e8ecb16a67cefabd213ed38e50bc7d254195 --- /dev/null +++ b/test_rover2.py @@ -0,0 +1,20 @@ +from backuprover2 import Rover + + +def test_stops_at_first_obstacle(): + rover = Rover(obstacles={(0, 2)}) + result = rover.drive("FFRFF") + assert result == "F" + assert rover.get_position() == (0, 1) + +def test_mid_sequence_obstacle(): + rover = Rover(obstacles={(1, 1)}) + result = rover.drive("FRFRF") + assert result == "FR" + assert rover.get_position() == (0, 1) + +def test_no_obstacles_full_execution(): + rover = Rover() + result = rover.drive("FFLFF") + assert result == "FFLFF" + assert rover.get_position() == (-2, 2)