Example #1
0
        public void GivenElevatorInFirstFloor_WhenRequetIsAddedToMoveToGround_ThenElevatorIsMovedToGroundFloor()
        {
            //Arrange
            AutoResetEvent signalRequestCompletion = new AutoResetEvent(false);
            Elevator       elevator = new Elevator(GetFloors(), "Elevator 1");

            elevator.AddRequest(1, (elevatorReachedDestination) =>
            {
                //Act
                elevator.AddRequest(0, (elevatorGroundFloor) =>
                {
                    //Assert
                    var actions = elevator.GetActions();
                    Assert.Equal(4, actions.Length);
                    Assert.Equal(new ElevatorAction(0, 1), actions[0]);
                    Assert.Equal(new ElevatorAction(1), actions[1]);
                    Assert.Equal(new ElevatorAction(1, 0), actions[2]);
                    Assert.Equal(new ElevatorAction(0), actions[3]);

                    signalRequestCompletion.Set();
                });
                elevator.CloosDoors();
            });
            signalRequestCompletion.WaitOne();
        }
Example #2
0
        public void GivenElevatorIsGoingDown_WhenThereIsFloorDownRequetInTraversingFloor_ThenElevatorIsStopedInTravesingFloor()
        {
            //Arrange
            AutoResetEvent          signalRequestCompletion = new AutoResetEvent(false);
            SortedList <int, Floor> floors = GetFloors();
            Elevator elevator = new Elevator(floors, "Elevator 1");

            elevator.AddRequest(2, (elevatorAtSecondFloor) =>
            {
                //Act
                floors[1].SetMovingDownRequest(Elevator.CloseDoorsAction);
                elevator.AddRequest(0, (elevatorCompletedRequest) =>
                {
                    signalRequestCompletion.Set();
                });
                elevatorAtSecondFloor.CloosDoors();
            });


            signalRequestCompletion.WaitOne();

            //Assert
            var actions = elevator.GetActions();

            Assert.Equal(7, actions.Length);
            Assert.Equal(new ElevatorAction(0, 1), actions[0]);
            Assert.Equal(new ElevatorAction(1, 2), actions[1]);
            Assert.Equal(new ElevatorAction(doorsOpenedFloor: 2), actions[2]);
            Assert.Equal(new ElevatorAction(2, 1), actions[3]);
            Assert.Equal(new ElevatorAction(1), actions[4]);
            Assert.Equal(new ElevatorAction(1, 0), actions[5]);
            Assert.Equal(new ElevatorAction(0), actions[6]);

            Assert.False(floors[1].HasDownRequest);
        }
Example #3
0
        public void GivenElevatorInGroundFloor_WhenTwoRequestAreAddedSecondIsPassingFloor_ThenElevatorServesSecondRequetFirst()
        {
            AutoResetEvent signalRequestCompletion = new AutoResetEvent(false);

            //Arrange
            Elevator elevator = new Elevator(GetFloors(), "Elevator 1");


            //Act
            elevator.AddRequest(1, (elevatorServedFirstFloorRequest) =>
            {
                elevatorServedFirstFloorRequest.AddRequest(4, (elevatorReachedFourthFloor) => { signalRequestCompletion.Set(); });
                elevatorServedFirstFloorRequest.AddRequest(2);
                elevatorServedFirstFloorRequest.CloosDoors();
            });

            signalRequestCompletion.WaitOne();


            //Assert
            var actions = elevator.GetActions();

            //Only four since second floor is served when passing through that floor
            Assert.Equal(7, actions.Length);
            Assert.Equal(new ElevatorAction(0, 1), actions[0]);
            Assert.Equal(new ElevatorAction(1), actions[1]);
            Assert.Equal(new ElevatorAction(1, 2), actions[2]);
            Assert.Equal(new ElevatorAction(2), actions[3]);
            Assert.Equal(new ElevatorAction(2, 3), actions[4]);
            Assert.Equal(new ElevatorAction(3, 4), actions[5]);
            Assert.Equal(new ElevatorAction(4), actions[6]);
        }
Example #4
0
        public void GivenElevatorInGroundFloor_WhenRequestIsRaisedToGroundFloor_ThenElevatorOpensDoorInGroundFloor()
        {
            AutoResetEvent signalRequestCompletion = new AutoResetEvent(false);

            SortedList <int, Floor> floors = GetFloors();
            Elevator elevator = new Elevator(floors, "Elevator 1");

            elevator.AddRequest(0, (elevatorServedRequest) =>
            {
                elevatorServedRequest.CloosDoors();
                signalRequestCompletion.Set();
            });

            signalRequestCompletion.WaitOne();

            var actions = elevator.GetActions();

            Assert.Single(actions);
            Assert.Equal(new ElevatorAction(0), actions[0]);
        }