From 4b5fc21f4782bbe622a9d4522ff0b21f8dde7549 Mon Sep 17 00:00:00 2001 From: klasbo Date: Sun, 27 Mar 2022 22:12:07 +0200 Subject: [PATCH] Add in-contract for better error messages --- .../hall_request_assigner/optimal_hall_requests.d | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cost_fns/hall_request_assigner/optimal_hall_requests.d b/cost_fns/hall_request_assigner/optimal_hall_requests.d index 52a6e8c..857d453 100644 --- a/cost_fns/hall_request_assigner/optimal_hall_requests.d +++ b/cost_fns/hall_request_assigner/optimal_hall_requests.d @@ -16,7 +16,20 @@ import std.stdio; bool[][][string] optimalHallRequests( bool[2][] hallReqs, LocalElevatorState[string] elevatorStates, -){ +) +in { + auto numFloors = hallReqs.length; + assert(elevatorStates.length, + "No elevator states provided"); + assert(elevatorStates.values.map!(a => a.cabRequests.length == numFloors).all, + "Hall and cab requests do not all have the same length"); + bool isInBounds(int f){ return f >= 0 && f < numFloors; } + assert(elevatorStates.values.map!(a => isInBounds(a.floor)).all, + "Some elevator is at an invalid floor"); + assert(elevatorStates.values.map!(a => isInBounds(a.floor + a.direction)).all, + "Some elevator is moving away from an end floor"); +} +do { auto reqs = hallReqs.toReq; auto states = initialStates(elevatorStates);