public void TestConstants() { var parser = FormulaParser.Create(); Assert.AreEqual(Math.PI, parser.Evaluate("pi").Value); Assert.AreEqual(Math.E, parser.Evaluate("e").Value); }
public void TestNotConfigured_InvalidVariable() { var parser = FormulaParser.Create(); var result = parser.Parse("1 * TEST"); Assert.IsFalse(result.Success); Assert.AreEqual("column 4: Unknown variable 'TEST'.", (string)result.Error); }
public void TestNotConfigured_InvalidFunctions() { var parser = FormulaParser.Create(); var result = parser.Parse("1 * test()"); Assert.IsFalse(result.Success); Assert.AreEqual("column 4: Unknown function 'test'.", (string)result.Error); }
private static void AsserEval(double expectedResult, string formular, IFormulaParser formulaParser, params string[] variables) { var variableDict = variables.Select(i => i.Split(':')).ToDictionary(i => i[0], i => double.Parse(i[1])); var parsers = new IFormulaParser[] { FormulaParser.Create(), FormulaParser.CreateBuilder().Build() }; foreach (var parser in parsers) { var result = parser.Evaluate(formular, variableDict); Assert.IsTrue(result.Success, string.Join("; ", (string)result.Error)); Assert.AreEqual(Math.Round(expectedResult, 5), Math.Round(result.Value, 5)); } }
private static void AssertFailure(string message, string formular, params string[] variables) { var variableDict = variables.Select(i => i.Split(':')).ToDictionary(i => i[0], i => double.Parse(i[1])); var parsers = new IFormulaParser[] { FormulaParser.Create() }; foreach (var parser in parsers) { var parsingResult = parser.Evaluate(formular, variableDict); Assert.IsFalse(parsingResult.Success, "Error excepted."); Assert.IsTrue(parsingResult.Error.ToString().StartsWith(message), $"Exptected to start with '{message}' but message was: '{parsingResult.Error}'"); } }
public void TestRandom() { var parser = FormulaParser.Create(); var rnd1 = parser.Evaluate("rnd()").Value; var rnd2 = parser.Evaluate("rnd()").Value; Assert.IsTrue(rnd1 != rnd2); Assert.IsTrue(rnd1 <= 1.0); Assert.IsTrue(rnd1 >= 0.0); Assert.IsTrue(rnd2 <= 1.0); Assert.IsTrue(rnd2 >= 0.0); }
public void Test_NameOfVariableConflictsWithNameOfConstant() { var parser = FormulaParser.Create(); var variables = new Dictionary <string, double> { ["pi"] = 3.0 }; var result = parser.Evaluate("2 * pi", variables); Assert.IsFalse(result.Success); Assert.AreEqual("Variable name 'pi' conflicts with the name of an existing constant.", (string)result.Error); }
public void Test_ConstantNotExist() { var parser = FormulaParser.Create(); var parser2 = FormulaParser.CreateBuilder().ConfigureConstats(c => c.RemoveAll()).Build(); var node = parser.Parse("2 * pi").Value; parser.Evaluate(node); Assert.AreEqual(Math.PI * 2, parser.Evaluate(node)); var result = parser2.Evaluate(node); Assert.IsFalse(result.Success); Assert.AreEqual("Constant 'pi' does not exist.", (string)result.Error); }
public void Test_FormulatText() { var parser = FormulaParser.Create(); var parser2 = FormulaParser .CreateBuilder() .ConfigureFunctions(functions => functions.Add("max", (a, b) => Math.Max(a, b))) .Build(); parser2.Evaluate("1 + max(2, 4)"); var result = parser.Parse("2 * pi").Value as FormulaNode; Assert.AreEqual("2 * pi", result.FormulaText); }
public void TestDefaultConfiguration() { var parser = FormulaParser.Create(); Assert.AreEqual(1.0, parser.Evaluate("min(1, 3.4)").Value); Assert.AreEqual(3.4, parser.Evaluate("max(1, 3.4)").Value); Assert.AreEqual(100.0, parser.Evaluate("max(100, 2, 3, 4 )").Value); Assert.AreEqual(15.0, parser.Evaluate("sum(1, 2, 3, 4, 5)").Value); Assert.AreEqual(2.0, parser.Evaluate("abs(2)").Value); Assert.AreEqual(2.0, parser.Evaluate("abs(-2)").Value); Assert.AreEqual(Math.Log(2), parser.Evaluate("ln(2)").Value); Assert.AreEqual(Math.Log(2, 3), parser.Evaluate("log(2, 3)").Value); Assert.AreEqual(Math.Log10(4), parser.Evaluate("log(4)").Value); Assert.AreEqual(Math.Sin(3), parser.Evaluate("sin(3)").Value); Assert.AreEqual(Math.Cos(3), parser.Evaluate("cos(3)").Value); Assert.AreEqual(Math.Tan(3), parser.Evaluate("tan(3)").Value); }
public void TestRunCostumVisitor() { var variables = new Dictionary <string, double> { ["x"] = 2 }; var parser = FormulaParser.Create(); var node = parser.Parse("1 + 2"); var transformedNode1 = parser.RunVisitor(node, new SwitchPlusAndMinus()); var transformedNode2 = parser.RunVisitor("1 + 2", new SwitchPlusAndMinus()); var transformedNode3 = parser.RunVisitor("1 + x", new SwitchPlusAndMinus(), variables); foreach (var transformedNode in new[] { transformedNode1, transformedNode2, transformedNode3 }) { var result = parser.Evaluate(transformedNode, variables); Assert.AreEqual(-1.0, result.Value); } }
public void TestSumWithOneArg() { var parser = FormulaParser.Create(); Assert.AreEqual(3.0, parser.Evaluate("sum(3)")); }