Example #1
0
        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());
        }
Example #2
0
        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());
        }
Example #3
0
        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()));
        }
Example #4
0
        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());
        }
Example #5
0
        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());
        }
Example #6
0
        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());
        }
Example #7
0
        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());
        }
Example #8
0
        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());
        }
Example #9
0
    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);
    }
Example #10
0
 public static double Parse(string expression)
 {
     _compiler.SetFunction(expression);
     return(_compiler.Calculate());
 }
Example #11
0
        public void InvalidToken_ThrowsInvalidEquationException(string equation)
        {
            var compiler = new EquationCompiler(equation);

            Assert.Throws <InvalidEquationException>(() => compiler.Calculate());
        }
Example #12
0
        public void NullArgument_ThrowsInvalidEquationException()
        {
            var compiler = new EquationCompiler("4+");

            Assert.Throws <InvalidEquationException>(() => compiler.Calculate());
        }
Example #13
0
        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()));
        }
Example #14
0
        public double OrderOfOperations(string equation)
        {
            var compiler = new EquationCompiler(equation);

            return(compiler.Calculate());
        }
Example #15
0
        public void UnmatchedParen_ThrowsUnmatchedParenthesesException(string equation)
        {
            var compiler = new EquationCompiler(equation);

            Assert.Throws <UnmatchedParenthesesException>(() => compiler.Calculate());
        }
Example #16
0
        public void InvalidFunction_ThrowsInvalidFunctionException()
        {
            var compiler = new EquationCompiler("foo(5)");

            Assert.Throws <InvalidFunctionException>(() => compiler.Calculate());
        }
Example #17
0
        public void Constant()
        {
            var compiler = new EquationCompiler("4.2");

            Assert.AreEqual(4.2, compiler.Calculate());
        }
Example #18
0
        public void NestedIf()
        {
            var compiler = new EquationCompiler("if(1, if(1, 2, 3), 4)");

            Assert.AreEqual(2, compiler.Calculate());
        }
Example #19
0
        public void InvalidArgumentCount_ThrowsArgumentCountException(string equation)
        {
            var compiler = new EquationCompiler(equation);

            Assert.Throws <ArgumentCountException>(() => compiler.Calculate());
        }
Example #20
0
        public double MultipleOfSameOperator(string equation)
        {
            var compiler = new EquationCompiler(equation);

            return(compiler.Calculate());
        }
Example #21
0
        public void NestedFunctionsWithMultipleParameters()
        {
            var compiler = new EquationCompiler("max(1,min(2,3))");

            Assert.AreEqual(2, compiler.Calculate());
        }