상속: IElement
예제 #1
0
        public void HasVariablesWithVariable()
        {
            Variable variable = new Variable("X");
            MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123));

            Assert.IsTrue(expr.HasVariable());
        }
예제 #2
0
파일: List.cs 프로젝트: ajlopez/AjErl
        public static List MakeList(IList<object> elements, Variable tail)
        {
            if (elements.Count == 1)
                return new List(elements[0], tail);

            return new List(elements[0], MakeList(elements.Skip(1).ToList(), tail));
        }
예제 #3
0
        public void CreateSimpleVariableExpression()
        {
            Variable variable = new Variable("X");
            VariableExpression expression = new VariableExpression(variable);

            Assert.AreEqual(variable, expression.Variable);
        }
예제 #4
0
        public void EvaluateUndefinedVariableExpression()
        {
            Variable variable = new Variable("X");
            Context context = new Context();
            VariableExpression expression = new VariableExpression(variable);

            Assert.AreEqual(variable, expression.Evaluate(context, true));
        }
예제 #5
0
        public void EvaluateVariableExpression()
        {
            Variable variable = new Variable("X");
            Context context = new Context();
            context.SetValue("X", 1);
            VariableExpression expression = new VariableExpression(variable);

            Assert.AreEqual(1, expression.Evaluate(context));
        }
예제 #6
0
        public void RaiseIfEvaluateUndefinedVariableExpression()
        {
            Variable variable = new Variable("X");
            Context context = new Context();
            VariableExpression expression = new VariableExpression(variable);

            try
            {
                expression.Evaluate(context, false);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("variable 'X' is unbound", ex.Message);
            }
        }
예제 #7
0
        public void Equals()
        {
            Variable variable1 = new Variable("X");
            Variable variable2 = new Variable("Y");
            Variable variable3 = new Variable("X");

            Assert.AreEqual(variable1, variable3);
            Assert.AreEqual(variable3, variable1);

            Assert.AreEqual(variable1.GetHashCode(), variable3.GetHashCode());

            Assert.IsFalse(variable1.Equals(null));
            Assert.IsFalse(variable1.Equals(123));
            Assert.IsFalse(variable1.Equals("foo"));

            Assert.AreNotEqual(variable1, variable2);
            Assert.AreNotEqual(variable2, variable1);
        }
예제 #8
0
        public void MatchOneAtomAndTwoVariablesInTupleWithConcreteTuple()
        {
            Context context = new Context();
            Atom p = new Atom("point");
            Variable x = new Variable("X");
            Variable y = new Variable("Y");

            MatchExpression expr = new MatchExpression(new TupleExpression(new IExpression[] { new AtomExpression(p), new VariableExpression(x), new VariableExpression(y) }), new TupleExpression(new IExpression[] { new AtomExpression(new Atom("point")), new ConstantExpression(1), new ConstantExpression(2) }));

            expr.Evaluate(context);

            var result1 = context.GetValue("X");
            var result2 = context.GetValue("Y");

            Assert.IsNotNull(result1);
            Assert.AreEqual(1, result1);
            Assert.IsNotNull(result2);
            Assert.AreEqual(2, result2);
        }
예제 #9
0
        public void MatchTwoVariablesWithList()
        {
            Context context = new Context();
            Variable headvar = new Variable("H");
            Variable tailvar = new Variable("T");
            var list = new List(headvar, tailvar);
            var list2 = new List(1, new List(2, new List(3, null)));

            MatchExpression expr = new MatchExpression(new ConstantExpression(list), new ConstantExpression(list2));

            expr.Evaluate(context);

            var result1 = context.GetValue("H");
            var result2 = context.GetValue("T");

            Assert.IsNotNull(result1);
            Assert.AreEqual(1, result1);
            Assert.IsNotNull(result2);
            Assert.IsInstanceOfType(result2, typeof(List));
            Assert.AreEqual("[2,3]", result2.ToString());
        }
예제 #10
0
        public void RaiseIfMatchVariableInTupleWithTwoDifferentValues()
        {
            Context context = new Context();
            Variable x = new Variable("X");

            MatchExpression expr = new MatchExpression(new TupleExpression(new IExpression[] { new VariableExpression(x), new VariableExpression(x) }), new TupleExpression(new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) }));

            try
            {
                expr.Evaluate(context);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                Assert.AreEqual("no match of right hand side value {1,2}", ex.Message);
            }
        }
예제 #11
0
        public void RaiseIfMatchBoundVariableWithInteger()
        {
            Context context = new Context();
            Variable variable = new Variable("X");
            context.SetValue("X", 1);
            MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123));

            try
            {
                expr.Evaluate(context);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                Assert.AreEqual("no match of right hand side value 123", ex.Message);
            }
        }
예제 #12
0
        public void MatchVariableWithInteger()
        {
            Context context = new Context();
            Variable variable = new Variable("X");
            MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123));

            Assert.AreEqual(123, expr.Evaluate(context));

            Assert.AreEqual(123, context.GetValue("X"));
        }
예제 #13
0
        public void VariableToString()
        {
            Variable variable = new Variable("X");

            Assert.AreEqual("X", variable.ToString());
        }
예제 #14
0
        public void FirstVariable()
        {
            Variable variable = new Variable("X");

            Assert.AreEqual(variable, variable.FirstVariable());
        }
예제 #15
0
        public void CreateSimpleVariable()
        {
            Variable variable = new Variable("X");

            Assert.AreEqual("X", variable.Name);
        }
예제 #16
0
 public VariableExpression(Variable variable)
 {
     this.variable = variable;
 }