Beispiel #1
0
        public void SubstitutionTestId()
        {
            BaseExpression expr  = BaseExpression.Parse("x + y + z");
            BaseExpression expr2 = expr.Subst(new Dictionary <string, BaseExpression>());

            Assert.AreEqual(expr.ToString(), expr2.ToString());
        }
Beispiel #2
0
        public void GenerateLibrary_ThreeArgsExpression()
        {
            var xyzExpr = BaseExpression.Parse("(66 + (x * -4)) / z + y / 9 + z * (x - y)");

            xyzExpr.Compile("Test_GenerateLibrary_LaunchFunction");

            Assembly assembly = Assembly.LoadFrom("Test_GenerateLibrary_LaunchFunction.dll");

            Type       type       = assembly.GetType("Evaluator");
            MethodInfo evalMethod = type.GetMethod("Evaluate");

            Assert.NotNull(evalMethod);

            int x = 2, y = 3, z = 5;
            var subst = new Dictionary <string, BaseExpression>();

            subst["x"] = new IntegerExpression(x);
            subst["y"] = new IntegerExpression(y);
            subst["z"] = new IntegerExpression(z);
            var substExpr = xyzExpr.Subst(subst);

            object evaluatorObject = Activator.CreateInstance(type);
            int    result          = (int)evalMethod.Invoke(evaluatorObject, new object[] { x, y, z });

            substExpr.EvaluateBe().ExtractRawInteger(out int expected);
            Assert.AreEqual(expected, result);
        }
Beispiel #3
0
        private static BaseExpression Minimize(string input)
        {
            var iex     = BaseExpression.Parse(input);
            var reduced = iex.Normalize().EvaluateBe();

            return(reduced);
        }
Beispiel #4
0
 public void InvalidDoubleMinus()
 {
     Assert.Throws <ParseException>(() =>
     {
         BaseExpression.Parse("7 - - 5");
     });
 }
Beispiel #5
0
 public void ExprWithVarTest()
 {
     Assert.DoesNotThrow(() =>
     {
         BaseExpression.Parse("x + 4");
     });
 }
Beispiel #6
0
 public void UnitInvalid()
 {
     Assert.Throws <ParseException>(() =>
     {
         BaseExpression.Parse("()");
     });
 }
Beispiel #7
0
 public void EmptyInvalid()
 {
     Assert.Throws <ParseException>(() =>
     {
         BaseExpression.Parse("");
     });
 }
Beispiel #8
0
 public void AssocInvalidUnbalancedClosed()
 {
     Assert.Throws <ParseException>(() =>
     {
         BaseExpression.Parse("(4 + 1) + 5)");
     });
 }
Beispiel #9
0
 public void AssocInvalidUnbalancedOpen()
 {
     Assert.Throws <ParseException>(() =>
     {
         BaseExpression.Parse("(4 + 1 + 5");
     });
 }
Beispiel #10
0
        // Utilitiy functions
        private static int RawEval(string input)
        {
            var iex     = BaseExpression.Parse(input);
            var reduced = iex.EvaluateBe();

            Assert.True(reduced.ExtractRawInteger(out var result));
            return(result);
        }
Beispiel #11
0
        public void SubstitutionTestId2()
        {
            BaseExpression expr  = BaseExpression.Parse("1 + 2 + x + y");
            var            subst = new Dictionary <string, BaseExpression>();

            subst["a"] = new IntegerExpression(3);
            subst["b"] = new IntegerExpression(9);
            BaseExpression expr2 = expr.Subst(subst);

            Assert.AreEqual(expr.ToString(), expr2.ToString());
        }
Beispiel #12
0
        public void RandomTest_ExprToStringAndParse()
        {
            var maxTreeLvl = 50;
            var N          = 10000;
            var rnd        = new Random();

            for (var i = 0; i < N; i++)
            {
                var rand          = GenerateRandomExpression(rnd, maxTreeLvl);
                var randAsString  = rand.ToString();
                var rand2         = BaseExpression.Parse(randAsString);
                var rand2AsString = rand2.ToString();
                Assert.AreEqual(randAsString, rand2AsString, "wrong value at iteration " + i);
            }
        }
Beispiel #13
0
        public void SubstitutionSimpleTest()
        {
            BaseExpression exprExpected = BaseExpression.Parse("3 + (-1 * -4) / 2 - 0 % 17");

            BaseExpression expr  = BaseExpression.Parse("x + (y * z) / w - k % l");
            var            subst = new Dictionary <string, BaseExpression>();

            subst["x"] = new IntegerExpression(3);
            subst["y"] = new IntegerExpression(-1);
            subst["z"] = new IntegerExpression(-4);
            subst["w"] = new IntegerExpression(2);
            subst["k"] = new IntegerExpression(0);
            subst["l"] = new IntegerExpression(17);
            BaseExpression exprSubst = expr.Subst(subst);

            exprExpected.EvaluateBe().ExtractRawInteger(out var expected);
            exprSubst.EvaluateBe().ExtractRawInteger(out var actual);
            Assert.AreEqual(expected, actual);
        }