Esempio n. 1
0
        public void OneDouble()
        {
            var rpn = parser.Parse("6.2");

            Assert.AreEqual(1, rpn.Count);
            var delta = 6.2 - ((ConstantToken)rpn[0]).Value;

            Assert.IsTrue((delta < 0 ? -1 * delta : delta) < Double.Epsilon);
        }
Esempio n. 2
0
        private static void DoWork(string equation)
        {
            try {
                var rpn = RPNParser.Parse(equation);

                Console.WriteLine();
                Console.WriteLine("Parsed  : " + string.Join("", rpn.Output.Select(o => o.Value)));


                if (rpn.Variables.Any())
                {
                    var expr = ExpressionBuilder.BuildFlat(rpn.Output);
                    Console.WriteLine("Equation  : " + expr.ToString());
                    Console.WriteLine("Result    : " + expr.Execute().ToString());
                }
                else
                {
                    var expr  = ExpressionBuilder.Build(rpn.Output);
                    var exprf = ExpressionBuilder.BuildFlat(rpn.Output);
                    Console.WriteLine("Equation r: " + expr.ToString());
                    Console.WriteLine("Result   r: " + expr.Execute().Count.AsString());
                    Console.WriteLine("Equation f: " + exprf.ToString());
                    Console.WriteLine("Result   f: " + exprf.Execute().Count.AsString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Esempio n. 3
0
        public void Add_TwoTwoDigitIntegers()
        {
            //Arrange
            var text     = "12 + 13";
            var expected = " 12 13 +";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Esempio n. 4
0
        public void Parse_e_Sin()
        {
            //Arrange
            var text     = "Sin(e)";
            var expected = Constants.e + " sin";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Esempio n. 5
0
        public void Parse_EM()
        {
            //Arrange
            var text     = "em";
            var expected = Constants.EM.ToString();

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Esempio n. 6
0
        public void Add_TwoFloatingNumbers()
        {
            //Arrange
            var text     = 12.5.ToString() + "+" + 13.5.ToString();
            var expected = 12.5.ToString() + 13.5.ToString() + "+";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
        public void Parse_X_WithNumbers()
        {
            //Arrange
            var text     = "2*x+1";
            var expected = "2x*1+";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Esempio n. 8
0
        public void Parse_Parenthesis()
        {
            //Arrange
            var text     = "(3 + x)-2";
            var expected = "3 x + 2 -";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Esempio n. 9
0
        public void AddThenMultiplyThenPower()
        {
            //Arrange
            var text   = "2 + 3 * 4 ^ 1";
            var output = " 2 3 4 1 ^ * +";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, output));
        }
        public void CreatesFlatExpression()
        {
            //Arrange
            var text = "2 * x * y";

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute();

            //Assert
            Assert.IsTrue(actual is FlatMultExpression);
        }
Esempio n. 11
0
        public void Parse_Number_Subtract_X()
        {
            //Arrange
            var text     = "1-x";
            var expected = "1x-";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
        }
Esempio n. 12
0
        public void Parse_X_InVariables()
        {
            //Arrange
            var text = "x+1";

            //Act
            var rpn = RPNParser.Parse(text);

            //Assert
            Assert.IsTrue(rpn.Variables.Count == 1);
            Assert.IsTrue(rpn.Variables.Contains("x"));
        }
Esempio n. 13
0
        public void ParseTest12()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "pi*1+e*2+.95";
            var output     = 9.5281563105078888d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Esempio n. 14
0
        public void ParseTest11()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "(-1)^2";
            var output     = 1d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Esempio n. 15
0
        public void ParseTest8()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "-5*(+5)+5*(-3)-5*(+5)-5*(-3)";
            var output     = -50d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Esempio n. 16
0
        public void ParseTest7()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "5*5+5*3-5*5-5*3";
            var output     = 0d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Esempio n. 17
0
        public void MultiplyDecimals()
        {
            //Arrange
            var text     = 4.0m.ToString() + "*" + 2.5m.ToString();
            var expected = 10;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 18
0
        public void AllOperatorsAndFunctions()
        {
            //Arrange
            var text     = "Sin((4- 6 * -2 )^1/(2 + 2)^(1/2))";
            var expected = (decimal)Math.Sin(8);

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 19
0
        public void ChangeWhenMultiplyingAndPower()
        {
            //Arrange
            var text     = "3*-2^2";
            var expected = -12;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 20
0
        public void ParseTest16()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "-1*(-(+(5*(+5)+5*(+3)-5*(+5)-5*(-3))))";
            var output     = 30D;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Esempio n. 21
0
        public void ParseTest14()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "ctg(tg(cos(sin(100))))";
            var output     = 0.39311779109637159D;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Esempio n. 22
0
        public void Constant_phi()
        {
            //Arrange
            var text     = "phi";
            var expected = (decimal)(Constants.Phi);

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 23
0
        public void ParseTest15()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "abs(-2)+exp(1)+(2)Log(4)+√(4)";
            var output     = 2 + Math.E + 2 + 2;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Esempio n. 24
0
        public void AddIntegers()
        {
            //Arrange
            var text     = "2+2";
            var expected = 4;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 25
0
        public void ParseTest6()
        {
            // Arrange
            var mathParser = new RPNParser();
            var input      = "2^2+4";
            var output     = 8d;

            // Act
            var actual = mathParser.Parse(input);

            // Assert
            Assert.AreEqual(output, actual);
        }
Esempio n. 26
0
        public void AddDecimals()
        {
            //Arrange
            var text     = 2.2m.ToString() + "+" + 4.3m.ToString();
            var expected = 6.5;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 27
0
        public void ChangeWhenSubtracting()
        {
            //Arrange
            var text     = "12--6";
            var expected = 18;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 28
0
        public void AllOperators()
        {
            //Arrange
            var text     = "(4- 6 * -2 )^1/(2 + 2)^(1/2)";
            var expected = 8;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.Build(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 29
0
        public void ChangeInPower()
        {
            //Arrange
            var text     = "4^(-2)";
            var expected = 0.0625;

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = ExpressionBuilder.BuildFlat(rpn.Output).Execute().Count.Re.ToNumber();

            //Assert
            Assert.AreEqual(expected, actual);
        }
Esempio n. 30
0
        public void Divide_x_by_x()
        {
            //Arrange
            var text     = "x/x";
            var expected = RPNParser.Parse("1");

            //Act
            var rpn    = RPNParser.Parse(text);
            var actual = RPNParser.Parse(ExpressionBuilder.BuildFlat(rpn.Output).Execute().ToString());

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output));
        }