Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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()));
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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);
    }
Ejemplo n.º 10
0
 public static double Parse(string expression)
 {
     _compiler.SetFunction(expression);
     return(_compiler.Calculate());
 }
Ejemplo n.º 11
0
        public void InvalidToken_ThrowsInvalidEquationException(string equation)
        {
            var compiler = new EquationCompiler(equation);

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

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

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

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

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

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

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

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

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

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