Exemplo n.º 1
0
 public static EvaluationResult Decrement(EvaluationResult number)
 {
     if (number == null)
     {
         throw new ArgumentNullException(nameof(number));
     }
     if (number.Type == EvaluationType.Int)
     {
         return(number.AsInt().Value - 1);
     }
     if (number.Type == EvaluationType.Float)
     {
         return(number.AsFloat().Value - 1f);
     }
     throw new Exception("Decrement expects a number");
 }
Exemplo n.º 2
0
        private EvaluationResult Evaluate(EvaluationResult left, OperatorExpression op, EvaluationResult right, NumericPrecision numericPrecision)
        {
            Exception exception = null;

            try
            {
                if (left == null || op == null || right == null)
                {
                    return(new EvaluationResult(EvaluationType.Null, null));
                }
                if (left.Type == EvaluationType.Boolean && right.Type == EvaluationType.Boolean)
                {
                    return(EvaluateBools(left.AsBoolean().Value, op, right.AsBoolean().Value));
                }
                if (left.Type == EvaluationType.DateTime && right.Type == EvaluationType.DateTime)
                {
                    return(EvaluateDates(left.AsDateTime().Value, op, right.AsDateTime().Value));
                }
                if (left.Type == EvaluationType.String && right.Type == EvaluationType.String)
                {
                    return(EvaluateStrings(left.AsString(), op, right.AsString()));
                }
                if (left.Type == EvaluationType.Int && right.Type == EvaluationType.Int)
                {
                    return(EvaluateInts(left.AsInt().Value, op, right.AsInt().Value));
                }
                var leftIsNumeric  = left.Type == EvaluationType.Float || left.Type == EvaluationType.Int;
                var rightIsNumeric = right.Type == EvaluationType.Float || right.Type == EvaluationType.Int;
                if (leftIsNumeric && rightIsNumeric)
                {
                    var leftNumeric  = left.AsRealNumber(numericPrecision);
                    var rightNumeric = right.AsRealNumber(numericPrecision);
                    return(EvaluateFloats(leftNumeric, op, rightNumeric));
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            throw new EvaluationException(left, op, right, exception);
        }
 public EvaluationResult IncrementInt(EvaluationResult result)
 {
     return(result.AsInt().Value + 1);
 }
Exemplo n.º 4
0
        public void CanUseBasicFunctions()
        {
            EvaluationResult value = null;

            //miss out time trial for the first eval, as this includes the start-up time for the interpreter etc
            value = EvaluateOperation(@"SumInts([three], 2, 1)", NumericPrecision.Decimal,
                                      new Dictionary <string, object> {
                { "[three]", 3 }
            },
                                      new Dictionary <string, ExternalFunction> {
                { "SumInts", SumInts }
            });
            Assert.AreEqual(EvaluationType.Int, value.Type);
            Assert.AreEqual(6, value.AsInt().Value);
            using (var timer = new TimeAssertion(milliseconds: 5))
            {
                value = EvaluateOperation(@"SumInts([three], 2) + 1", NumericPrecision.Decimal,
                                          new Dictionary <string, object> {
                    { "[three]", 3 }
                },
                                          new Dictionary <string, ExternalFunction> {
                    { "SumInts", SumInts }
                });
                Assert.AreEqual(EvaluationType.Int, value.Type);
                Assert.AreEqual(6, value.AsInt().Value);
            }
            using (var timer = new TimeAssertion(milliseconds: 5))
            {
                value = EvaluateOperation(@"SumInts([three], ((2 + 4) * 2) / 2) + 1", NumericPrecision.Decimal,
                                          new Dictionary <string, object> {
                    { "[three]", 3 }
                },
                                          new Dictionary <string, ExternalFunction> {
                    { "SumInts", SumInts }
                });
                Assert.AreEqual(EvaluationType.Int, value.Type);
                Assert.AreEqual(10, value.AsInt().Value);
            }
            using (var timer = new TimeAssertion(milliseconds: 5))
            {
                value = EvaluateOperation(@"ListFactory()", NumericPrecision.Decimal,
                                          null,
                                          new Dictionary <string, ExternalFunction> {
                    { "ListFactory", ListFactory }
                });
                Assert.AreEqual(EvaluationType.Enumerable, value.Type);
                Assert.IsTrue(value.AsList().Select(i => i.Result).SequenceEqual(new List <object> {
                    1, 2, 3
                }));
            }
            using (var timer = new TimeAssertion(milliseconds: 5))
            {
                value = EvaluateOperation(@"IncrementInt(1)", NumericPrecision.Decimal,
                                          null,
                                          new Dictionary <string, ExternalFunction> {
                    { "IncrementInt", IncrementInt }
                });
                Assert.AreEqual(EvaluationType.Int, value.Type);
                Assert.AreEqual(2, value.AsInt().Value);
            }
            using (var timer = new TimeAssertion(milliseconds: 10))
            {
                value = EvaluateOperation(@"ExpressionFactoryResultingInTwo()", NumericPrecision.Decimal,
                                          new Dictionary <string, object> {
                    { "[two]", 2 }
                },
                                          new Dictionary <string, ExternalFunction> {
                    { "ExpressionFactoryResultingInTwo", ExpressionFactoryResultingInTwo }
                });
                Assert.AreEqual(EvaluationType.Int, value.Type);
                Assert.AreEqual(2, value.AsInt().Value);
            }
        }