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); }
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()); } }
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)); }
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)); }
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)); }
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)); }
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)); }
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); }
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)); }
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")); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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)); }