Exemplo n.º 1
0
        public void TurnOnStringMode()
        {
            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            Assert.False(_runtime.Object.CurrentMode.IsNumberMode);
        }
Exemplo n.º 2
0
        public void MoveRightForZero()
        {
            // Arrange
            _runtime.SetupSequence(r => r.RetrieveLastValue()).Returns(0);

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            Assert.True(_runtime.Object.CurrentDirection is MoveRight);
        }
Exemplo n.º 3
0
        public void MoveInCorrectDirection()
        {
            // Arrange
            _runtime.SetupProperty(r => r.CurrentPosition, new CoOrds(0, 0));

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.VerifySet(r => r.CurrentPosition = new CoOrds(1, 0));
        }
        public void ShouldStoreNumber(char inValue, int outValue)
        {
            // Arrange
            _runtime.SetupGet(r => r.CurrentInstruction).Returns(inValue);

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.Verify(r => r.StoreValue(It.Is <int>(i => i == outValue)), Times.Exactly(1));
        }
        public void SkipNextCellWhenDirectionIsMoveRight(int oldX, int oldY, int newX, int newY)
        {
            // Arrange
            _runtime.SetupProperty(r => r.CurrentPosition, new CoOrds(oldX, oldY));
            _runtime.SetupProperty(r => r.CurrentDirection, MoveRight.Instance);

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.VerifySet(r => r.CurrentPosition = new CoOrds(newX, newY));
        }
Exemplo n.º 6
0
        public void PushCorrectResult()
        {
            // Arrange
            _runtime.SetupSequence(r => r.RetrieveLastValue()).Returns(1).Returns(2);

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.Verify(r => r.RetrieveLastValue(), Times.Exactly(2));
            _runtime.Verify(r => r.StoreValue(It.Is <int>(i => i == 3)), Times.Exactly(1));
        }
Exemplo n.º 7
0
        public void PushCorrectResult()
        {
            // Arrange
            _runtime.Setup(r => r.Input(It.IsAny <string>())).Returns("1");

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.Verify(r => r.Input(It.Is <string>(s => s == "Please supply an Integer:")), Times.Exactly(1));
            _runtime.Verify(r => r.StoreValue(It.Is <int>(i => i == 1)), Times.Exactly(1));
        }
Exemplo n.º 8
0
        private static void Part2(IInstruction[] instructions)
        {
            int           instr        = 0;
            int           acc          = 0;
            HashSet <int> visited      = new();
            int           flippedIndex = -1;

            while (instr < instructions.Length)
            {
                instr = 0;
                acc   = 0;
                visited.Clear();

                //flip back
                if (flippedIndex >= 0)
                {
                    FlipInstruction(instructions, ref flippedIndex);
                }

                flippedIndex++;

                FlipInstruction(instructions, ref flippedIndex);

                while (instr < instructions.Length && visited.Add(instr))
                {
                    IInstruction cur = instructions[instr];
                    cur.Execute(ref instr, ref acc);
                }
            }

            Console.WriteLine($"Part 2: Acc = {acc}");
        }
Exemplo n.º 9
0
        public void ExecuteInstruction()
        {
            foreach (var rover in rovers)
            {
                foreach (var command in rover.Instruction)
                {
                    IInstruction instruction = _instructionFactory.GetInstruction(command);
                    var          exPosition  = new Position {
                        X = rover.Position.X, Y = rover.Position.Y, D = rover.Position.D
                    };
                    var newPosition = instruction.Execute(exPosition);

                    if (_plateauService.IsValidPosition(newPosition))
                    {
                        //check new position status
                        //if there is a rover contiune with next instruction
                        if (IsThereARover(newPosition) == false)
                        {
                            rover.Position = newPosition;
                        }
                        else
                        {
                            continue; //next instruction
                        }
                    }
                    else
                    {
                        throw new InvalidPositionException();
                    }
                }
            }
        }
Exemplo n.º 10
0
 public void Execute(Runtime runtime)
 {
     if (Condition == null || Condition.Evaluate(runtime) != 0)
     {
         Instruction.Execute(runtime);
     }
 }
        public override void Process(string instructions)
        {
            if (!Validate(instructions))
            {
                return;
            }

            IRobot robot = context.Robot;
            bool   lost  = false;

            try
            {
                foreach (var instruction in instructions.ToLowerInvariant())
                {
                    IInstruction executer = GetInstruction(instruction);
                    executer.Execute(instruction, robot);
                }
            }
            catch (RobotLostException)
            {
                lost = true;
            }

            logger.Log($"{robot.X} {robot.Y} {robot.Orientation.ToString().Substring(0, 1)}{(lost ? " LOST" : "")}");
        }
Exemplo n.º 12
0
        public void SetEndProgram()
        {
            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.VerifySet(r => r.EndProgram = true);
        }
Exemplo n.º 13
0
        public void Execute_For_SideTurns_Should_Update_Orientations(IInstruction instruction, IRover rover, Orientations expectedOrientation)
        {
            //arrange
            //act
            instruction.Execute(rover);
            var orientation = rover.GetOrientation();

            //assert
            Assert.Equal(expectedOrientation, orientation.GetOrientationType());
        }
Exemplo n.º 14
0
        public void AssertInstructionExecute()
        {
            var gameboy = new GameBoy();
            var opcode  = InstructionSet.StandardOpcodes[0x0B];

            _instruction = new Instruction(0, opcode, null);
            gameboy.Processor.Registers.BC = 0x01;
            _instruction.Execute(gameboy);
            Assert.Equal(0, gameboy.Processor.Registers.BC);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Execute the current instruction
        /// </summary>
        public void ExecuteInstruction(IBefungeRunTime runTime)
        {
            IInstruction currentInstruction = _defaultInstruction;

            if (_instructionsLookup.ContainsKey(runTime.CurrentInstruction))
            {
                _instructionsLookup.TryGetValue(runTime.CurrentInstruction, out currentInstruction);
            }
            currentInstruction.Execute(runTime);
        }
Exemplo n.º 16
0
        public void PushCorrectResults()
        {
            // Arrange
            _runtime.Setup(r => r.RetrieveLastValue()).Returns(0);

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.Verify(r => r.RetrieveLastValue(), Times.Exactly(1));
        }
Exemplo n.º 17
0
 /// <summary>
 /// <see cref="IInstruction.Execute" />
 /// </summary>
 public void Execute()
 {
     if (_ifCondition.Evaluate())
     {
         _ifInstruction.Execute();
     }
     else if (_elseInstruction != null)
     {
         _elseInstruction.Execute();
     }
 }
Exemplo n.º 18
0
        public void OutputCorrectResult()
        {
            // Arrange
            _runtime.SetupSequence(r => r.RetrieveLastValue()).Returns(97);

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.VerifySet(r => r.Output = "a");
        }
Exemplo n.º 19
0
        public void PutCorrectValue()
        {
            // Arrange
            _runtime.SetupSequence(r => r.RetrieveLastValue()).Returns(2).Returns(1).Returns(97);

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.Verify(r => r.RetrieveLastValue(), Times.Exactly(3));
            _runtime.Verify(r => r.PutValue(It.Is <CoOrds>(xy => xy.x == 1 && xy.y == 2), It.Is <char>(c => c == 'a')), Times.Exactly(1));
        }
Exemplo n.º 20
0
        public void GetCorrectValue(char getValue, int storeValue)
        {
            // Arrange
            _runtime.SetupSequence(r => r.RetrieveLastValue()).Returns(2).Returns(1);
            _runtime.Setup(r => r.GetValue(It.Is <CoOrds>(xy => xy.x == 1 && xy.y == 2))).Returns(getValue);

            // Act
            _sit.Execute(_runtime.Object);

            // Assert
            _runtime.Verify(r => r.RetrieveLastValue(), Times.Exactly(2));
            _runtime.Verify(r => r.StoreValue(It.Is <int>(i => i == storeValue)), Times.Exactly(1));
        }
Exemplo n.º 21
0
        public void Step()
        {
            IInstruction instruction = InstructionFactory.GetInstruction(_vmstate, _program, _inputProvider, _outputProvider);

            if (instruction is StopInstruction)
            {
                IsDone = true;
            }
            else
            {
                _instructionPointer = instruction.Execute();
            }
        }
Exemplo n.º 22
0
        private static void Part1(IInstruction[] instructions)
        {
            int           instr   = 0;
            int           acc     = 0;
            HashSet <int> visited = new();

            while (visited.Add(instr))
            {
                IInstruction cur = instructions[instr];
                cur.Execute(ref instr, ref acc);
            }

            Console.WriteLine($"Part 1: Acc = {acc}");
        }
Exemplo n.º 23
0
        public bool Execute(ExecutionState state)
        {
            if (state.ScriptIdentifierStack.Contains(identifier))
            {
                throw new InstructionExecutionException("Script \"" + identifier.DescriptiveName + "\" aborted to prevent infinite recursion", state.SourceToken);
            }
            if (hasError)
            {
                instruction.Execute(state);
                return(false);
            }

            state.ScriptIdentifierStack.Push(identifier);
            state.ExecutingScript.Push(this);
            try
            {
                instruction.Execute(state);
            }
            catch (InstructionExecutionException ex)
            {
                state.ErrorHTML = GetErrorHTML(ex.Message, ex.Token, state);
            }
            catch (InstructionExecutionParseErrorException ex)
            {
                state.ErrorHTML = ex.ToString();
            }
            finally
            {
                state.ExecutingScript.Pop();
                if (state.ScriptIdentifierStack.Count > 0)
                {
                    state.ScriptIdentifierStack.Pop();
                }
            }
            return(state.ErrorOccurred);
        }
Exemplo n.º 24
0
        public override int Execute()
        {
            CurrentInstructionAddress = RegPc;
            int          opcode = FetchPCWord();
            IInstruction i      = instructionsTable[opcode];

            if (i != null)
            {
                return(i.Execute(opcode));
            }
            else
            {
                RegPc = CurrentInstructionAddress;
                return(unknown.Execute(opcode));
            }
        }
Exemplo n.º 25
0
        public string Execute()
        {
            MemoryStream   stream = new MemoryStream();
            ExecutionState state  = new ExecutionState(stream);

            try
            {
                instruction.Execute(state);
                stream.Seek(0, SeekOrigin.Begin);
                using (StreamReader reader = new StreamReader(stream))
                    return(reader.ReadToEnd());
            }
            catch (InstructionExecutionException ex)
            {
                return(GetErrorHTML(ex.Message, ex.Token));
            }
        }
Exemplo n.º 26
0
        public async Task NextInst()
        {
            try
            {
                Console.WriteLine($"program {_programId} running {StackPointer}");
                IInstruction instruction = _instructions[StackPointer];

                if (!DebugCounts.ContainsKey(instruction.GetType().Name))
                {
                    DebugCounts.Add(instruction.GetType().Name, 0);
                }
                DebugCounts[instruction.GetType().Name]++;
                await instruction.Execute(ReceiveQueue, SendQueue, _registers).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Terminated = true;
            }
        }
Exemplo n.º 27
0
        public ISExpression Execute(IInstruction x)
        {
            var success = false;

            try
            {
                while (x != null)
                {
                    //Console.WriteLine(x.ToString());
                    x = x.Execute(this);
                }
                success = true;
                return(this.A);
            }
            finally
            {
                if (!success)
                {
                    Reset(false);
                }
            }
        }
        public void MoveRandomly()
        {
            // Arrange
            // Setup a dictionary to keep a count of the number of directions
            Dictionary <Type, int> directionCount = new Dictionary <Type, int>();

            directionCount.Add(typeof(MoveDown), 0);
            directionCount.Add(typeof(MoveLeft), 0);
            directionCount.Add(typeof(MoveRight), 0);
            directionCount.Add(typeof(MoveUp), 0);

            for (int i = 0; i < 20; i++)
            {
                // Act
                _sit.Execute(_runtime.Object);

                Type t = _runtime.Object.CurrentDirection.GetType();

                directionCount[t] = ++directionCount[t];
            }

            // Assert
            Assert.True(directionCount.All(v => 1 <= v.Value));
        }
Exemplo n.º 29
0
        /// <summary>
        /// Execute the current instruction
        /// </summary>
        public void ExecuteInstruction(IBefungeRunTime runTime)
        {
            IInstruction currentInstruction = runTime.CurrentInstruction == '"' ? _toggleStringMode : _defaultInstruction;

            currentInstruction.Execute(runTime);
        }
Exemplo n.º 30
0
 public void Execute(IInstruction instruction, byte[] operands)
 {
     instruction.Execute(operands);
 }