Exemplo n.º 1
0
        public void ParseInstruction(
            string rawInstruction,
            string registerToModify,
            OperationType operation,
            int amount,
            string conditionRegister,
            OperandType conditionOperand,
            int conditionAmount)
        {
            var instruction = new RegisterInstruction(rawInstruction);

            instruction.RegisterToModify.Should().Be(registerToModify);
            instruction.Operation.Should().Be(operation);
            instruction.Amount.Should().Be(amount);
            instruction.ConditionRegister.Should().Be(conditionRegister);
            instruction.ConditionOperand.Should().Be(conditionOperand);
            instruction.ConditionAmount.Should().Be(conditionAmount);
        }
Exemplo n.º 2
0
        private static void RunInstructionOnRegister(IDictionary <string, int> registers, RegisterInstruction instruction)
        {
            if (!registers.ContainsKey(instruction.RegisterToModify))
            {
                registers.Add(instruction.RegisterToModify, 0);
            }
            if (!registers.ContainsKey(instruction.ConditionRegister))
            {
                registers.Add(instruction.ConditionRegister, 0);
            }

            bool conditionMet;

            switch (instruction.ConditionOperand)
            {
            case OperandType.GreaterThan:
                conditionMet = registers[instruction.ConditionRegister] > instruction.ConditionAmount;
                break;

            case OperandType.LessThan:
                conditionMet = registers[instruction.ConditionRegister] < instruction.ConditionAmount;
                break;

            case OperandType.GreaterThanOrEqualTo:
                conditionMet = registers[instruction.ConditionRegister] >= instruction.ConditionAmount;
                break;

            case OperandType.LessThanOrEqualTo:
                conditionMet = registers[instruction.ConditionRegister] <= instruction.ConditionAmount;
                break;

            case OperandType.EqualTo:
                conditionMet = registers[instruction.ConditionRegister] == instruction.ConditionAmount;
                break;

            case OperandType.NotEqualTo:
                conditionMet = registers[instruction.ConditionRegister] != instruction.ConditionAmount;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (conditionMet)
            {
                switch (instruction.Operation)
                {
                case OperationType.Increase:
                    registers[instruction.RegisterToModify] += instruction.Amount;
                    break;

                case OperationType.Decrease:
                    registers[instruction.RegisterToModify] -= instruction.Amount;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }