Testing & Edge Cases
Testing Strategy
We organize tests into three layers:
| Layer | What It Tests | Example |
|---|---|---|
| Unit Tests | Individual classes in isolation | Door.open() transitions CLOSED → OPEN |
| Integration Tests | Multiple components interacting | Dispatcher + Strategy + Elevator working together |
| Scenario Tests | Realistic end-to-end workflows | Three hall calls at the same instant (Edge Case 1) |
We'll use Python's unittest framework. The same test logic would apply in JUnit for Java.
Unit Tests — Enums and Request
import unittest
class TestRequest(unittest.TestCase):
def test_create_hall_call(self):
req = Request(source_floor=3, destination_floor=3,
direction=Direction.UP, request_type=RequestType.HALL_CALL)
self.assertEqual(req.source_floor, 3)
self.assertEqual(req.request_type, RequestType.HALL_CALL)
def test_create_cabin_call(self):
req = Request(source_floor=3, destination_floor=7,
direction=Direction.UP, request_type=RequestType.CABIN_CALL)
self.assertEqual(req.destination_floor, 7)
def test_duplicate_detection(self):
req1 = Request(3, 7, Direction.UP, RequestType.CABIN_CALL)
req2 = Request(3, 7, Direction.UP, RequestType.CABIN_CALL)
self.assertEqual(req1, req2)
self.assertEqual(hash(req1), hash(req2))
def test_different_requests_not_equal(self):
req1 = Request(3, 7, Direction.UP, RequestType.CABIN_CALL)
req2 = Request(3, 8, Direction.UP, RequestType.CABIN_CALL)
self.assertNotEqual(req1, req2)Unit Tests — Door
class TestDoor(unittest.TestCase):
def setUp(self):
self.door = Door()
def test_initial_state_is_closed(self):
self.assertEqual(self.door.status, DoorStatus.CLOSED)
self.assertTrue(self.door.is_closed())
def test_open_from_closed(self):
self.door.open()
self.assertEqual(self.door.status, DoorStatus.OPEN)
def test_close_from_open(self):
self.door.open()
self.door.close()
self.assertEqual(self.door.status, DoorStatus.CLOSED)
def test_open_when_already_open_no_change(self):
self.door.open()
self.door.open() # Should be safe, no error
self.assertEqual(self.door.status, DoorStatus.OPEN)
def test_close_when_already_closed_no_change(self):
self.door.close() # Already closed
self.assertEqual(self.door.status, DoorStatus.CLOSED)Unit Tests — Elevator
class TestElevator(unittest.TestCase):
def setUp(self):
self.elevator = Elevator(elevator_id=1, current_floor=5)
self.strategy = LOOKStrategy()
def test_initial_state(self):
self.assertEqual(self.elevator.current_floor, 5)
self.assertEqual(self.elevator.state, ElevatorState.IDLE)
self.assertEqual(self.elevator.direction, Direction.IDLE)
self.assertFalse(self.elevator.has_requests())
def test_add_request(self):
req = Request(5, 8, Direction.UP, RequestType.CABIN_CALL)
self.elevator.add_request(req)
self.assertTrue(self.elevator.has_requests())
self.assertEqual(len(self.elevator.requests), 1)
def test_duplicate_request_ignored(self):
req = Request(5, 8, Direction.UP, RequestType.CABIN_CALL)
self.elevator.add_request(req)
self.elevator.add_request(req)
self.assertEqual(len(self.elevator.requests), 1)
def test_move_up(self):
req = Request(5, 8, Direction.UP, RequestType.CABIN_CALL)
self.elevator.add_request(req)
self.elevator.move(self.strategy)
self.assertEqual(self.elevator.current_floor, 6)
self.assertEqual(self.elevator.direction, Direction.UP)
self.assertEqual(self.elevator.state, ElevatorState.MOVING_UP)
def test_move_down(self):
req = Request(5, 2, Direction.DOWN, RequestType.CABIN_CALL)
self.elevator.add_request(req)
self.elevator.move(self.strategy)
self.assertEqual(self.elevator.current_floor, 4)
self.assertEqual(self.elevator.direction, Direction.DOWN)
def test_stop_and_serve(self):
req = Request(5, 6, Direction.UP, RequestType.CABIN_CALL)
self.elevator.add_request(req)
self.elevator.move(self.strategy) # Move to floor 6
# Elevator should have stopped and served the request
self.assertEqual(self.elevator.current_floor, 6)
self.assertFalse(self.elevator.has_requests())
self.assertEqual(self.elevator.state, ElevatorState.IDLE)
def test_doors_stay_open_until_the_car_moves_on(self):
self.elevator.add_request(Request(5, 6, Direction.UP, RequestType.CABIN_CALL))
self.elevator.add_request(Request(5, 8, Direction.UP, RequestType.CABIN_CALL))
self.elevator.move(self.strategy) # Stops at floor 6, floor 8 still to serve
self.assertEqual(self.elevator.state, ElevatorState.DOOR_OPEN)
self.assertEqual(self.elevator.door.status, DoorStatus.OPEN)
self.elevator.move(self.strategy) # The doors close as the car leaves
self.assertEqual(self.elevator.current_floor, 7)
self.assertEqual(self.elevator.door.status, DoorStatus.CLOSED)
def test_no_move_without_requests(self):
self.elevator.move(self.strategy)
self.assertEqual(self.elevator.current_floor, 5) # Unchanged
self.assertEqual(self.elevator.state, ElevatorState.IDLE)Unit Tests — Scheduling Strategies
class TestFCFSStrategy(unittest.TestCase):
def test_selects_idle_elevator(self):
strategy = FCFSStrategy()
e1 = Elevator(1, current_floor=1)
e2 = Elevator(2, current_floor=5)
request = Request(3, 3, Direction.UP, RequestType.HALL_CALL)
selected = strategy.select_elevator(request, [e1, e2])
self.assertEqual(selected.id, 1) # First idle elevator
def test_get_next_stop_returns_first_request(self):
strategy = FCFSStrategy()
elevator = Elevator(1, current_floor=1)
elevator.add_request(Request(1, 5, Direction.UP, RequestType.CABIN_CALL))
elevator.add_request(Request(1, 3, Direction.UP, RequestType.CABIN_CALL))
next_stop = strategy.get_next_stop(elevator)
self.assertEqual(next_stop, 5) # FCFS: first request's destination
class TestSCANStrategy(unittest.TestCase):
def test_prefers_same_direction_elevator(self):
strategy = SCANStrategy(1, 10)
e1 = Elevator(1, current_floor=3)
e1.direction = Direction.UP
e1.state = ElevatorState.MOVING_UP
e2 = Elevator(2, current_floor=2)
e2.direction = Direction.DOWN
e2.state = ElevatorState.MOVING_DOWN
request = Request(5, 5, Direction.UP, RequestType.HALL_CALL)
selected = strategy.select_elevator(request, [e1, e2])
self.assertEqual(selected.id, 1) # e1 is going up, closer and same direction
def test_get_next_stop_going_up(self):
strategy = SCANStrategy(1, 10)
elevator = Elevator(1, current_floor=5)
elevator.direction = Direction.UP
elevator.add_request(Request(5, 8, Direction.UP, RequestType.CABIN_CALL))
elevator.add_request(Request(5, 3, Direction.DOWN, RequestType.CABIN_CALL))
next_stop = strategy.get_next_stop(elevator)
self.assertEqual(next_stop, 8) # Going up: serve upward floors first
def test_sweeps_to_building_edge(self):
strategy = SCANStrategy(1, 10)
elevator = Elevator(1, current_floor=9)
elevator.direction = Direction.UP
# Only request below — SCAN still sweeps on to the top floor first
elevator.add_request(Request(9, 3, Direction.DOWN, RequestType.CABIN_CALL))
self.assertEqual(strategy.get_next_stop(elevator), 10)
elevator.current_floor = 10
self.assertEqual(strategy.get_next_stop(elevator), 3) # At the edge: reverse
class TestLOOKStrategy(unittest.TestCase):
def test_reversal_at_last_request(self):
strategy = LOOKStrategy()
elevator = Elevator(1, current_floor=8)
elevator.direction = Direction.UP
# Only request below — should reverse
elevator.add_request(Request(8, 3, Direction.DOWN, RequestType.CABIN_CALL))
next_stop = strategy.get_next_stop(elevator)
self.assertEqual(next_stop, 3) # LOOK reverses at last request, not building edgeUnit Tests — Floor Components
class TestHallButton(unittest.TestCase):
def test_press_creates_request(self):
button = HallButton(floor=3, direction=Direction.UP)
request = button.press()
self.assertIsNotNone(request)
self.assertEqual(request.source_floor, 3)
self.assertEqual(request.request_type, RequestType.HALL_CALL)
def test_duplicate_press_returns_none(self):
button = HallButton(floor=3, direction=Direction.UP)
button.press()
result = button.press() # Second press
self.assertIsNone(result)
def test_reset_allows_repress(self):
button = HallButton(floor=3, direction=Direction.UP)
button.press()
button.reset()
result = button.press() # Should work after reset
self.assertIsNotNone(result)
class TestFloorDisplay(unittest.TestCase):
def test_updates_on_elevator_move(self):
display = FloorDisplay(floor=1)
display.on_elevator_moved(elevator_id=1, floor=5, direction=Direction.UP)
output = display.display()
self.assertIn("Elevator 1", output)
self.assertIn("Floor 5", output)
class TestElevatorPanel(unittest.TestCase):
def test_press_floor_creates_cabin_call(self):
elevator = Elevator(1, current_floor=3)
request = elevator.panel.press_floor(7)
self.assertIsNotNone(request)
self.assertEqual(request.destination_floor, 7)
self.assertEqual(request.request_type, RequestType.CABIN_CALL)
def test_press_current_floor_returns_none(self):
elevator = Elevator(1, current_floor=3)
result = elevator.panel.press_floor(3)
self.assertIsNone(result) # Already there
def test_duplicate_press_returns_none(self):
elevator = Elevator(1, current_floor=3)
elevator.panel.press_floor(7)
result = elevator.panel.press_floor(7)
self.assertIsNone(result)Integration Tests
class TestDispatcherIntegration(unittest.TestCase):
def test_dispatch_assigns_to_idle_elevator(self):
e1 = Elevator(1, current_floor=1)
e2 = Elevator(2, current_floor=1)
dispatcher = Dispatcher([e1, e2], LOOKStrategy())
request = Request(5, 5, Direction.UP, RequestType.HALL_CALL)
assigned = dispatcher.dispatch(request)
self.assertIsNotNone(assigned)
self.assertTrue(assigned.has_requests())
def test_duplicate_dispatch_rejected(self):
e1 = Elevator(1, current_floor=1)
dispatcher = Dispatcher([e1], LOOKStrategy())
request = Request(5, 5, Direction.UP, RequestType.HALL_CALL)
dispatcher.dispatch(request)
result = dispatcher.dispatch(request) # Same request
self.assertIsNone(result)
def test_strategy_swap_at_runtime(self):
e1 = Elevator(1, current_floor=1)
dispatcher = Dispatcher([e1], FCFSStrategy())
self.assertIsInstance(dispatcher.strategy, FCFSStrategy)
dispatcher.set_strategy(LOOKStrategy())
self.assertIsInstance(dispatcher.strategy, LOOKStrategy)
class TestElevatorSystemIntegration(unittest.TestCase):
def setUp(self):
self.system = ElevatorSystem(
num_floors=10, num_elevators=2, strategy=LOOKStrategy()
)
def test_hall_call_dispatches_and_serves(self):
# Hall call from floor 5 going UP
assigned = self.system.add_hall_call(5, Direction.UP)
self.assertIsNotNone(assigned)
# Run simulation until served
for _ in range(20):
self.system.step()
# Elevator should have reached floor 5
reached = any(e.current_floor == 5 or not e.has_requests()
for e in self.system.elevators)
self.assertTrue(reached)
def test_cabin_call_after_hall_call(self):
# Elevator 1 gets hall call to floor 3
self.system.add_hall_call(3, Direction.UP)
# Move elevator to floor 3
for _ in range(10):
self.system.step()
# Now add a cabin call to floor 8
self.system.add_cabin_call(1, 8)
# Run until served
for _ in range(20):
self.system.step()
all_idle = all(e.state == ElevatorState.IDLE
for e in self.system.elevators)
self.assertTrue(all_idle)
def test_observer_notification(self):
# Floor displays should update when elevator moves
self.system.add_hall_call(5, Direction.UP)
self.system.step() # Elevator moves
# Floor 1's display should have received an update
display_output = self.system.floors[0].display.display()
self.assertIn("Elevator", display_output)Edge Cases
These are scenarios that break naive implementations and are common interview follow-up questions.
Edge Case 1 — Simultaneous Requests from Multiple Floors
Scenario: Floors 2, 5, and 9 all press UP at the same instant.
Why it matters: A naive system might assign all three to the same elevator, starving others.
Our solution: The Dispatcher processes requests sequentially, and each select_elevator() call sees the updated state: earlier calls are already in their elevators' queues. With LOOK/SCAN, an elevator going up scores best for calls above it in its direction, and an idle elevator picks up the rest; when two elevators score the same — as two idle elevators on the same floor do — the one with fewer pending requests gets the call. Here: floor 2 goes to elevator 1, floor 5 to elevator 2, floor 9 to elevator 1.
class TestSimultaneousRequests(unittest.TestCase):
def test_multiple_simultaneous_hall_calls(self):
system = ElevatorSystem(10, 2, LOOKStrategy())
# Three simultaneous hall calls
e1 = system.add_hall_call(2, Direction.UP)
e2 = system.add_hall_call(5, Direction.UP)
e3 = system.add_hall_call(9, Direction.UP)
# At least two different elevators should be used
assigned_ids = {e.id for e in [e1, e2, e3] if e is not None}
self.assertGreaterEqual(len(assigned_ids), 2)
# All requests should be assigned
total_requests = sum(len(e.requests) for e in system.elevators)
self.assertEqual(total_requests, 3)Edge Case 2 — Request at Current Floor
Scenario: Elevator is at floor 5, someone at floor 5 presses UP.
Why it matters: The elevator shouldn't move away and come back. It should immediately open doors.
Our solution: When get_next_stop() returns the current floor, the move() method calls stop() directly instead of incrementing/decrementing the floor.
class TestRequestAtCurrentFloor(unittest.TestCase):
def test_request_at_current_floor(self):
elevator = Elevator(1, current_floor=5)
strategy = LOOKStrategy()
# Request for the floor we're already on
req = Request(5, 5, Direction.UP, RequestType.HALL_CALL)
elevator.add_request(req)
elevator.move(strategy)
# Should serve immediately without moving
self.assertEqual(elevator.current_floor, 5)
self.assertFalse(elevator.has_requests())Edge Case 3 — All Elevators Busy
Scenario: Both elevators are moving with full request queues, and a new hall call arrives.
Why it matters: The system must never drop a request silently.
Our solution: select_elevator() always returns the best option — even if all elevators are busy, it selects the one with the fewest pending requests (FCFS) or the one whose path best serves the new request (SCAN/LOOK). The request goes into the queue and will be served eventually.
class TestAllElevatorsBusy(unittest.TestCase):
def test_request_assigned_when_all_busy(self):
system = ElevatorSystem(10, 2, LOOKStrategy())
# Load both elevators with requests
system.add_hall_call(3, Direction.UP)
system.add_hall_call(8, Direction.UP)
system.add_cabin_call(1, 7)
system.add_cabin_call(2, 9)
# Now add another hall call — should still be assigned
result = system.add_hall_call(6, Direction.DOWN)
self.assertIsNotNone(result)
# Total requests should include the new one
total = sum(len(e.requests) for e in system.elevators)
self.assertGreaterEqual(total, 3)Edge Case 4 — Single Elevator, Many Floors
Scenario: One elevator serving a 50-floor building during rush hour.
Why it matters: Tests starvation — can requests on remote floors wait indefinitely?
Our solution: SCAN and LOOK guarantee that every request is eventually reached. The elevator sweeps the full range of pending requests. FCFS would be terrible here (back-and-forth thrashing), but SCAN/LOOK provide bounded wait times proportional to the building height.
class TestSingleElevatorManyFloors(unittest.TestCase):
def test_all_requests_eventually_served(self):
system = ElevatorSystem(50, 1, LOOKStrategy())
# Requests spread across the building
for floor in [5, 15, 25, 35, 45]:
system.add_hall_call(floor, Direction.UP)
# Run enough steps to traverse the building twice
for _ in range(120):
system.step()
# All requests should be served
elevator = system.elevators[0]
self.assertFalse(elevator.has_requests())
self.assertEqual(elevator.state, ElevatorState.IDLE)Edge Case 5 — Duplicate Request Prevention
Scenario: An impatient passenger presses the hall button 10 times.
Why it matters: Without dedup, the elevator would add 10 identical requests, wasting queue space and potentially causing it to stop at the same floor multiple times.
Our solution: Three layers of duplicate prevention:
HallButton.press()returns None if already pressed (hardware level)Elevator.add_request()checksif request not in self.requests(Request level)Dispatcher.dispatch()checksif request in self._pending_requests(system level)
All three let go once an elevator stops for the call: the request leaves the elevator's queue, and ElevatorSystem.step() resets the button and calls Dispatcher.mark_served() — so a later press is a new call (the last test checks it).
This satisfies Requirement R10 — duplicate requests are suppressed.
class TestDuplicatePrevention(unittest.TestCase):
def test_hall_button_dedup(self):
button = HallButton(3, Direction.UP)
req1 = button.press()
req2 = button.press()
self.assertIsNotNone(req1)
self.assertIsNone(req2)
def test_elevator_request_dedup(self):
elevator = Elevator(1, current_floor=1)
req = Request(1, 5, Direction.UP, RequestType.CABIN_CALL)
elevator.add_request(req)
elevator.add_request(req)
self.assertEqual(len(elevator.requests), 1)
def test_dispatcher_dedup(self):
e1 = Elevator(1, current_floor=1)
dispatcher = Dispatcher([e1], LOOKStrategy())
req = Request(5, 5, Direction.UP, RequestType.HALL_CALL)
result1 = dispatcher.dispatch(req)
result2 = dispatcher.dispatch(req)
self.assertIsNotNone(result1)
self.assertIsNone(result2)
def test_served_hall_call_can_be_made_again(self):
system = ElevatorSystem(10, 1, LOOKStrategy())
self.assertIsNotNone(system.add_hall_call(5, Direction.UP))
for _ in range(10):
system.step()
# The elevator stopped for the call, so the button and the dispatcher take it again
self.assertIsNotNone(system.add_hall_call(5, Direction.UP))Edge Case 6 — Boundary Floors
Scenario: Requests to floor 1 (bottom) and top floor. Hall buttons at boundaries.
Why it matters: Floor 1 has no DOWN button, top floor has no UP button. The elevator must not try to go below floor 1 or above the top floor.
Our solution: Floor.__init__() passes is_bottom and is_top flags. Bottom floor has down_button = None, top floor has up_button = None. The press_up() / press_down() methods safely return None for missing buttons.
class TestBoundaryFloors(unittest.TestCase):
def test_bottom_floor_no_down_button(self):
floor = Floor(floor_number=1, is_bottom=True)
self.assertIsNone(floor.down_button)
result = floor.press_down()
self.assertIsNone(result)
def test_top_floor_no_up_button(self):
floor = Floor(floor_number=10, is_top=True)
self.assertIsNone(floor.up_button)
result = floor.press_up()
self.assertIsNone(result)
def test_middle_floor_has_both_buttons(self):
floor = Floor(floor_number=5)
self.assertIsNotNone(floor.up_button)
self.assertIsNotNone(floor.down_button)Common Interview Follow-Up Questions
Interviewers often probe edge cases with these follow-ups:
"What if two elevators are equidistant from a request?"
SCAN and LOOK break ties by load: of two elevators with the same score, the one with fewer pending requests wins (if that ties too, the first in the list). FCFS ignores distance and takes the idle elevator with the fewest requests. In production, you'd add more criteria: energy efficiency, or round-robin for fairness.
"How do you handle starvation?"
SCAN/LOOK prevent starvation by design — they sweep through all pending floors. FCFS can't starve a floor either: each car always heads for its oldest call, so a call waits only for the calls that arrived before it — a wait that grows under heavy load, but no call is skipped forever. The strategy that can starve a distant floor is nearest-first, which keeps choosing the closer calls as they keep arriving. We could add a max-wait-time check: if a request has been pending longer than a threshold, promote it to high priority.
"What about weight limits?"
Add a current_weight field to Elevator and a max_capacity config. The should_stop() method would skip floors if at capacity. The select_elevator() strategy would deprioritize full elevators.
"What if an elevator breaks down mid-travel?"
Add a MAINTENANCE state to ElevatorState. When triggered, the elevator finishes its current move, opens doors at the nearest floor, and removes itself from the dispatcher's available pool. Its pending requests would need to be redistributed to other elevators.