public void EvaluateWithThreeExpressions()
        {
            Context            context = new Context();
            FunExpression      expr1   = this.MakeExpression("fun(0) -> 1 end.");
            FunExpression      expr2   = this.MakeExpression("fun(1) -> 1 end.");
            FunExpression      expr3   = this.MakeExpression("fun(X) -> f(X-1) + f(X-2) end.");
            MultiFunExpression mexpr   = new MultiFunExpression(new FunExpression[] { expr1, expr2, expr3 });

            var result = mexpr.Evaluate(context);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(MultiFunction));

            var mfunc = (MultiFunction)result;

            Assert.IsNotNull(mfunc.Functions);
            Assert.AreEqual(3, mfunc.Functions.Count);
        }
        public void RaiseIfArityIsWrong()
        {
            Context            context = new Context();
            FunExpression      expr1   = this.MakeExpression("fun(0) -> 1 end.");
            FunExpression      expr2   = this.MakeExpression("fun(1) -> 1 end.");
            FunExpression      expr3   = this.MakeExpression("fun(X, Y) -> f(X-1) + f(X-2) end.");
            MultiFunExpression mexpr   = new MultiFunExpression(new FunExpression[] { expr1, expr2, expr3 });

            try
            {
                mexpr.Evaluate(context);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("head mismatch", ex.Message);
            }
        }
        public void EvaluateWithOneExpression()
        {
            Context            context = new Context();
            FunExpression      expr    = this.MakeExpression("fun(0) -> 1 end.");
            MultiFunExpression mexpr   = new MultiFunExpression(new FunExpression[] { expr });

            Assert.IsFalse(mexpr.HasVariable());

            var result = mexpr.Evaluate(context);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(MultiFunction));

            var mfunc = (MultiFunction)result;

            Assert.IsNotNull(mfunc.Functions);
            Assert.AreEqual(1, mfunc.Functions.Count);
        }
Beispiel #4
0
        public void DefineAndEvaluateFunction()
        {
            FunExpression expr    = new FunExpression(new IExpression[] { new VariableExpression(new Variable("X")), new VariableExpression(new Variable("Y")) }, new AddExpression(new VariableExpression(new Variable("X")), new VariableExpression(new Variable("Y"))));
            Context       context = new Context();

            Assert.IsFalse(expr.HasVariable());
            var result = expr.Evaluate(context);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Function));

            var func = (Function)result;

            var newcontext = func.MakeContext(new object[] { 1, 2 });

            Assert.IsNotNull(newcontext);
            Assert.AreEqual(1, newcontext.GetValue("X"));
            Assert.AreEqual(2, newcontext.GetValue("Y"));

            Assert.AreEqual(3, func.Evaluate(newcontext));
        }