예제 #1
0
        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));
        }
예제 #2
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));
        }
예제 #3
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));
        }
예제 #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));
        }
예제 #5
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));
        }
예제 #6
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));
        }
예제 #7
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));
        }
예제 #8
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));
        }
예제 #9
0
        public void Parse_RecognizesImaginary()
        {
            //Arrange
            var text     = "1 + i";
            var expected = "1i+";

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
            Assert.IsTrue(rpn.Output[1].Type == SymbolTypes.Imaginary);
        }
예제 #10
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));
        }
예제 #11
0
        public void Add_x_y_fun()
        {
            //Arrange
            var text     = "x + x + y + fun + fun";
            var expected = RPNParser.Parse("2*fun + 2*x + y");

            //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));
        }
예제 #12
0
        public void Parse_Log()
        {
            //Arrange
            var text     = "Log(" + 3.14m.ToString() + ")";
            var expected = 3.14m.ToString() + " log";

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected));
            Assert.IsTrue(rpn.Output[1].Type == SymbolTypes.Function);
        }
예제 #13
0
        public void Parse_FunctionInCorrrectOrder()
        {
            //Arrange
            var text     = "Cos(x)-2";
            var expected = "x cos 2 -";

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(rpn.Output, expected), $"Expected: {expected} but was {string.Join(" ", rpn.Output.Select(x => x.Value))}");
            Assert.IsTrue(rpn.Output[1].Type == SymbolTypes.Function);
        }
예제 #14
0
        public void Power_xy2()
        {
            //Arrange
            var text     = "x*y^2";
            var expected = RPNParser.Parse("x*y*y");

            //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));
        }
예제 #15
0
        public void VariableWithAddAndDivide()
        {
            //Arrange
            var text     = "2*x + 7 -2 + 2*x*x/x"; //+ 2*x*x/x
            var expected = RPNParser.Parse("4*x + 5");

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(actual.Output, expected.Output));
        }
예제 #16
0
        public void Add_FlatAdd_Variable()
        {
            //Arrange
            var text     = "2 + x + x^2 + x";
            var expected = RPNParser.Parse("x*x + 2*x + 2");

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output));
        }
예제 #17
0
        public void VariableWithFunction()
        {
            //Arrange
            var test     = "Sin(x) + x + Sin(x)";
            var expText  = "2 * Sin(x) + x";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #18
0
        public void Multiply_FlatMult_FlatThatIsZero()
        {
            //Arrange
            var text     = "x*(x -x)";
            var expText  = "0";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #19
0
        public void Multiply_FlatAdd_Number()
        {
            //Arrange
            var test     = "(2 + x)*2";
            var expText  = "2*(2 + x)";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #20
0
        public void Add_FlatAdd_Sinx_CTan2x()
        {
            //Arrange
            var test     = "Sin(x) + 2 + x + Cot(2*x)";
            var expText  = "x + 2 + Sin(x) + Cot(2*x)";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #21
0
        public void Multiply_Sin_Sin_x()
        {
            //Arrange
            var text     = "Sin(x)*Sin(x)*x";
            var expText  = "x*(Sin(x))^2";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #22
0
        public void Squash_To_Quadratic()
        {
            //Arrange
            var text     = "2 * x * x + 5 + 3 * x - 1";// "2 * x  - 1 + 3 * x * x + x + 5 - x*x";
            var expText  = "2 * x * x + 3 * x + 4";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #23
0
        public void Add_Different_FlatMultiply()
        {
            //Arrange
            var text     = "y * 2 * x  + 3 * x * y + x + y";
            var expText  = " 5 * x * y + x + y";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #24
0
        public void Pow_1()
        {
            //Arrange
            var text     = "(2+x)^1";
            var expText  = "2+x";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #25
0
        public void Divide_x_by_x_by_y_()
        {
            //Arrange
            var text     = "x/(x/y)";
            var expText  = "1*y";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #26
0
        public void Add_FlatAddReal_FlatAddImagined_TheSame()
        {
            //Arrange
            var test     = "2 + x + i * (x + 2)";
            var expText  = " 2 + 2 * i(1 + i) * x"; // "(2 + x) * (1 + i)";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #27
0
        public void Value_Squash_TwoVariables()
        {
            //Arrange
            var text     = "y * 2 * x  * 3 * x * y * y";
            var expText  = "6 * (y)^3 * (x)^2";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #28
0
        public void Squash_OneVariable()
        {
            //Arrange
            var test     = "2 * x  * 3 * x";
            var expText  = "( 6 * x * x)";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #29
0
        public void Power_2On_FlatAdd_x_y()
        {
            //Arrange
            var text = "(x+y)^2";
            //var expText = "x*x + 2*x*y + y*y";
            var expText  = "(x+y)^2";
            var expected = RPNParser.Parse(expText);

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

            //Assert
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {expr.ToString()}");
        }
예제 #30
0
        public void Linear_WithVariable_3()
        {
            //Arrange
            var text       = "y*x - 4 + z";
            var expText    = "(4 - z)/y";
            var expected   = RPNParser.Parse(expText);
            var expression = ExpressionBuilder.BuildFlat(RPNParser.Parse(text).Output).Execute();
            var equation   = new Equation(text, expression, null);

            //Act
            var sut    = Solver.Solve(equation, "x");
            var actual = RPNParser.Parse(sut.Solutions["x"].Item2[0].ToString());

            //Assert
            Assert.IsTrue(sut.Solutions.Count == 1);
            Assert.IsTrue(RPNComparer.Compare(expected.Output, actual.Output), $"Expected: {expText} but was {sut.Solutions["x"].Item2[0].ToString()}");
        }