public void Modulo(double a, double b) { var compiler = new EquationCompiler("a%b"); compiler.SetVariable("a", a); compiler.SetVariable("b", b); Assert.AreEqual(a % b, compiler.Calculate()); }
public void If(string condition, bool result, double a, double b) { var compiler = new EquationCompiler(string.Format("if({0},a,b)", condition)); compiler.SetVariable("a", a); compiler.SetVariable("b", b); Assert.AreEqual((result ? a : b), compiler.Calculate()); }
public void Equal(double a, double b) { var compiler = new EquationCompiler("a==b"); compiler.SetVariable("a", a); compiler.SetVariable("b", b); Assert.AreEqual(a == b, Convert.ToBoolean(compiler.Calculate())); }
public void Max(double a, double b) { var compiler = new EquationCompiler("max(a,b)"); compiler.SetVariable("a", a); compiler.SetVariable("b", b); Assert.AreEqual((a > b ? a : b), compiler.Calculate()); }
public void Power(double a, double b) { var compiler = new EquationCompiler("a^b"); compiler.SetVariable("a", a); compiler.SetVariable("b", b); Assert.AreEqual(Math.Pow(a, b), compiler.Calculate()); }
public void Division(double a, double b) { var compiler = new EquationCompiler("a/b"); compiler.SetVariable("a", a); compiler.SetVariable("b", b); Assert.AreEqual(a / b, compiler.Calculate()); }
public void MultipleFunctions(double a, double b, double c) { var compiler = new EquationCompiler("sin(a) + cos(b) - tan(c)"); compiler.SetVariable("a", a); compiler.SetVariable("b", b); compiler.SetVariable("c", c); Assert.AreEqual(Math.Sin(a) + Math.Cos(b) - Math.Tan(c), compiler.Calculate()); }
public void CustomFunction() { var compiler = new EquationCompiler("factorial(5)"); compiler.AddFunction("factorial", x => { int factorial = 1; for (int i = 1; i <= x; i++) { factorial *= i; } return(factorial); }); Assert.AreEqual(5 * 4 * 3 * 2 * 1, compiler.Calculate()); }
public static bool BoolValue(string aExpression, Dictionary <string, float> eParams) { aExpression = aExpression.Replace("?", ((int)Random.Range(0, 100)).ToString()); EquationCompiler oCompiler = new EquationCompiler(aExpression); oCompiler.Compile(); if (aExpression.Length > 8) { foreach (KeyValuePair <string, float> pair in eParams) { oCompiler.SetVariable(pair.Key, pair.Value); } } foreach (KeyValuePair <string, float> pair in eParams) { oCompiler.SetVariable(pair.Key, pair.Value); } return(oCompiler.Calculate() == 1); }
public static double Parse(string expression) { _compiler.SetFunction(expression); return(_compiler.Calculate()); }
public void InvalidToken_ThrowsInvalidEquationException(string equation) { var compiler = new EquationCompiler(equation); Assert.Throws <InvalidEquationException>(() => compiler.Calculate()); }
public void NullArgument_ThrowsInvalidEquationException() { var compiler = new EquationCompiler("4+"); Assert.Throws <InvalidEquationException>(() => compiler.Calculate()); }
public void Or(double a, double b) { var compiler = new EquationCompiler("a||b"); compiler.SetVariable("a", a); compiler.SetVariable("b", b); Assert.AreEqual(Convert.ToBoolean(a) || Convert.ToBoolean(b), Convert.ToBoolean(compiler.Calculate())); }
public double OrderOfOperations(string equation) { var compiler = new EquationCompiler(equation); return(compiler.Calculate()); }
public void UnmatchedParen_ThrowsUnmatchedParenthesesException(string equation) { var compiler = new EquationCompiler(equation); Assert.Throws <UnmatchedParenthesesException>(() => compiler.Calculate()); }
public void InvalidFunction_ThrowsInvalidFunctionException() { var compiler = new EquationCompiler("foo(5)"); Assert.Throws <InvalidFunctionException>(() => compiler.Calculate()); }
public void Constant() { var compiler = new EquationCompiler("4.2"); Assert.AreEqual(4.2, compiler.Calculate()); }
public void NestedIf() { var compiler = new EquationCompiler("if(1, if(1, 2, 3), 4)"); Assert.AreEqual(2, compiler.Calculate()); }
public void InvalidArgumentCount_ThrowsArgumentCountException(string equation) { var compiler = new EquationCompiler(equation); Assert.Throws <ArgumentCountException>(() => compiler.Calculate()); }
public double MultipleOfSameOperator(string equation) { var compiler = new EquationCompiler(equation); return(compiler.Calculate()); }
public void NestedFunctionsWithMultipleParameters() { var compiler = new EquationCompiler("max(1,min(2,3))"); Assert.AreEqual(2, compiler.Calculate()); }