public void TestDivideByZero()
        {
            var c         = new DivideCommand(options);
            var exception = Record.Exception(() => c.Execute(new long[] { 8, 0 }));

            Assert.IsType <DivideByZeroException>(exception);
        }
Exemplo n.º 2
0
        private void divButton_Click(object sender, EventArgs e)
        {
            expressionLbl.Text += String.IsNullOrEmpty(displayTxt.Text) ? Constants.Divide : displayTxt.Text + Constants.Divide;
            var number = new Number
            {
                EnteredNumber = String.IsNullOrEmpty(displayTxt.Text) ? 1 : Convert.ToDouble(displayTxt.Text),
                IsCalculated  = false
            };
            DivideCommand divideCommand;

            //Deferred execution
            if (_bracketCount == 0 || _bracketCount % 2 == 0)
            {
                _numbers.Add(number);
                divideCommand = new DivideCommand(_result, _numbers);
                //Calculating
                _invoker.CalculatingOperations();
            }
            else if (_bracketCount > 0 && _bracketCount % 2 == 0)
            {
                _bracketNumbers.Add(number);
                divideCommand = new DivideCommand(_result, _bracketNumbers);
                _invoker.CalculatingOperations();
            }
            else
            {
                _bracketNumbers.Add(number);
                divideCommand = new DivideCommand(_result, _bracketNumbers);
            }
            //Adding current operation into sequence
            _invoker.IncludeOperations(divideCommand);
            displayTxt.Clear();
        }
        public void TestDivideCommand()
        {
            var c            = new DivideCommand(options);
            var actualResult = c.Execute(new long[] { 4, 2, 1 });

            Assert.Equal(2, actualResult);
        }
Exemplo n.º 4
0
        public double Divide(double first, double second)
        {
            var cmd = new DivideCommand(first, second);

            _divideHandler.Handle(cmd);

            return(_store.Result);
        }
Exemplo n.º 5
0
        public ViewModel()
        {
            AddCommand      = new AddCommand(this);
            SubtractCommand = new SubtractCommand(this);
            MultiplyCommand = new MultiplyCommand(this);
            DivideCommand   = new DivideCommand(this);

            Results = new ObservableCollection <Result>();
        }
Exemplo n.º 6
0
 private void SignalsGenerated(object sender, EventArgs args)
 {
     AddCommand.RaiseCanExecuteChanged();
     DivideCommand.RaiseCanExecuteChanged();
     MultiplyCommand.RaiseCanExecuteChanged();
     SubtractCommand.RaiseCanExecuteChanged();
     SwapCommand.RaiseCanExecuteChanged();
     CorrelationCommand.RaiseCanExecuteChanged();
     ConvolutionCommand.RaiseCanExecuteChanged();
 }
Exemplo n.º 7
0
        public CalculatorViewModel(ICalculatorOperationsService operationsService = null)
        {
            Calculator         = new CalculatorModel();
            _operationsService = operationsService ?? new CalculatorOperationsService();

            AddCommand      = new AddCommand(this);
            SubtractCommand = new SubtractCommand(this);
            MultiplyCommand = new MultiplyCommand(this);
            DivideCommand   = new DivideCommand(this);
        }
Exemplo n.º 8
0
        public void DivideCommand_WithOneNumberInStack_FailExecute()
        {
            var command = new DivideCommand();
            var stack   = new Stack <decimal>();

            stack.Push(1);

            Exception ex = Assert.Throws <CalculateException>(() => command.Calculate(stack));

            Assert.Equal("Невозможно разделить 2 числа", ex.Message);
        }
Exemplo n.º 9
0
        public void DivideCommand_DivideByZero_FailExecute()
        {
            var command = new DivideCommand();
            var stack   = new Stack <decimal>();

            stack.Push(1);
            stack.Push(0);

            Exception ex = Assert.Throws <CalculateException>(() => command.Calculate(stack));

            Assert.Equal("деление ноль", ex.Message);
        }
Exemplo n.º 10
0
        public void DivideCommand_WithTwoNumberInStack_DivideInStack()
        {
            var command = new DivideCommand();
            var stack   = new Stack <decimal>();

            stack.Push(6);
            stack.Push(4);

            command.Calculate(stack);

            Assert.Single(stack);
            Assert.Equal(1.5m, stack.Peek());
        }
Exemplo n.º 11
0
        private ICommand CommandFromString(string name, string[] arguments)
        {
            ICommand command = null;

            switch (name)
            {
            case "SET":
            {
                Register register = Registers[arguments[0]];
                int      value    = int.Parse(arguments[1]);

                command = new SetCommand(register, value);
                break;
            }

            case "ADD":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new AddCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "SUB":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new SubtractCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "MUL":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new MultiplyCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "DIV":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new DivideCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "OUT":
            {
                if (Registers.ContainsKey(arguments[0]))
                {
                    Register register = Registers[arguments[0]];
                    command = new OutCommand(register);
                }
                else
                {
                    command = new OutCommand(arguments[0]);
                }

                break;
            }

            case "HLT":
            {
                command = new HaltCommand();
                break;
            }

            case "JMP":
            {
                string label = arguments[0];

                command = new JumpCommand(label);
                break;
            }

            case "JIF":
            {
                Register      registerOne     = Registers[arguments[0]];
                Register      registerTwo     = Registers[arguments[2]];
                string        conditionString = arguments[1];
                JumpCondition jumpCondition   = JumpCondition.Undefined;
                switch (conditionString)
                {
                case "==":
                    jumpCondition = JumpCondition.Equal;
                    break;

                case "<":
                    jumpCondition = JumpCondition.LessThan;
                    break;

                case "<=":
                    jumpCondition = JumpCondition.LessThanOrEqual;
                    break;

                case ">":
                    jumpCondition = JumpCondition.GreaterThan;
                    break;

                case ">=":
                    jumpCondition = JumpCondition.GreaterThanOrEqual;
                    break;

                case "!=":
                    jumpCondition = JumpCondition.NotEqual;
                    break;
                }

                string label = arguments[3];

                command = new JumpIfCommand(registerOne, registerTwo, label, jumpCondition);
                break;
            }

            case "CPY":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];

                command = new CopyCommand(registerOne, registerTwo);
                break;
            }

            default:
                if (name == "str:")
                {
                    string stringKey   = arguments[0];
                    string stringValue = string.Empty;
                    for (int i = 1; i < arguments.Length; i++)
                    {
                        stringValue += arguments[i];
                        if (i < arguments.Length)
                        {
                            stringValue += " ";
                        }
                    }

                    Program.RegisterString(stringKey, stringValue);
                }
                else if (name.Length > 1 && name.EndsWith(":"))
                {
                    Program.MakeLabel(name.Substring(0, name.Length - 1));
                    break;
                }
                break;
            }

            return(command);
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            using (reader = File.OpenText("input.txt"))
                using (writer = new StreamWriter(File.Create("output.txt")))
                {
                    QuackVM  vm = new QuackVM();
                    Workflow wf = new Workflow(vm);

                    while (!reader.EndOfStream)
                    {
                        string   str = reader.ReadLine();
                        ICommand cmd = null;
                        switch (str[0])
                        {
                        case '+':
                            cmd = new PlusCommand();
                            break;

                        case '-':
                            cmd = new MinusCommand();
                            break;

                        case '*':
                            cmd = new MultiplyCommand();
                            break;

                        case '/':
                            cmd = new DivideCommand();
                            break;

                        case '%':
                            cmd = new ModuloCommand();
                            break;

                        case '>':
                            cmd = new SetRegisterCommand(str[1]);
                            break;

                        case '<':
                            cmd = new GetRegisterCommand(str[1]);
                            break;

                        case 'P':
                            if (str.Length > 1)
                            {
                                cmd = new PrintRegisterCommand(str[1]);
                            }
                            else
                            {
                                cmd = new PrintCommand();
                            }
                            break;

                        case 'C':
                            if (str.Length > 1)
                            {
                                cmd = new PrintRegisterCharCommand(str[1]);
                            }
                            else
                            {
                                cmd = new PrintCharCommand();
                            }
                            break;

                        case ':':
                            cmd = new LabelCommand(str.Substring(1));
                            break;

                        case 'J':
                            cmd = new GoToLabelCommand(str.Substring(1));
                            break;

                        case 'Z':
                            cmd = new GoToLabelZCommand(str[1], str.Substring(2));
                            break;

                        case 'E':
                            cmd = new GoToLabelECommand(str[1], str[2], str.Substring(3));
                            break;

                        case 'G':
                            cmd = new GoToLabelGCommand(str[1], str[2], str.Substring(3));
                            break;

                        case 'Q':
                            cmd = new QuitCommand();
                            break;

                        default:
                            cmd = new NumberCommand(int.Parse(str));
                            break;
                        }
                        wf.AddCommand(cmd);
                    }

                    while (!wf.IsCompleted())
                    {
                        wf.ExecuteCurrentCommand();
                    }
                }
        }