Пример #1
0
        private static string BuildParseTree(string exp)
        {
            var             stack  = new Stack <Token>();
            TokenCollection tokens = GetTokens(exp);

            tokens.CreateRPN();
            return(tokens.Calculate());
        }
Пример #2
0
        public void Calculate_WithoutRPN_ReturnExceptionTest()
        {
            //Arrange
            var                  stringTokens = "2 + 2".Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>();

            //Act
            tokens.AddMany(stringTokens);
            //Assert
            Assert.ThrowsException <InvalidOperationException>(() => tokens.Calculate(formulas));
        }
Пример #3
0
        public void Calculate_DivideByZeroTest()
        {
            //Arrange
            var                  stringTokens = "2 / 0".Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>();

            //Act
            tokens.AddMany(stringTokens);
            tokens.CreateRPN();
            //Assert
            Assert.ThrowsException <DivideByZeroException>(() => tokens.Calculate(formulas));
        }
Пример #4
0
        public void CalcuateTest(string expression, double expectedResult)
        {
            //Arrange
            var                  stringTokens = expression.Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>();

            //Act
            tokens.AddMany(stringTokens);
            tokens.CreateRPN();
            var result = tokens.Calculate(formulas);

            //Assert
            Assert.AreEqual(expectedResult, result, 0.01);
        }
Пример #5
0
        public void Calcuate_WithIncorrectListOfFormulaTest()
        {
            //Arrange
            var                  stringTokens = "2 + a * c".Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>()
            {
                new ServerFormula {
                    Name = "a", Value = 13
                }
            };

            //Act
            tokens.AddMany(stringTokens);
            tokens.CreateRPN();
            //Assert
            Assert.ThrowsException <ArgumentException>(() => tokens.Calculate(formulas));
        }
Пример #6
0
        public void Calcuate_WithCorrectListOfFormulaTest()
        {
            //Arrange
            var                  stringTokens = "2 + a * 10".Split();
            TokenCollection      tokens       = new TokenCollection();
            List <ServerFormula> formulas     = new List <ServerFormula>()
            {
                new ServerFormula {
                    Name = "a", Value = 13
                }
            };

            //Act
            tokens.AddMany(stringTokens);
            tokens.CreateRPN();
            var result = tokens.Calculate(formulas);

            //Assert
            Assert.AreEqual(132, result, 0.01);
        }