示例#1
0
        public void TestSimpleCalcEngine()
        {
            var engine = new SimpleCalcEngine();

            engine.Context.Imports.AddType(typeof(Math));
            var sw   = new Stopwatch();
            var prev = "1";

            sw.Start();
            var i = 0;

            checked
            {
                string cur;
                do
                {
                    cur = $"i_{Guid.NewGuid().ToString("N")}";
                    string expression = $"{prev} + 1 + cos(3.14)";
                    engine.AddGeneric <double>(cur, expression);
                    prev = cur;
                    i++;
                }while (i <= 1999);
                sw.Stop();
                this.PrintSpeedMessage("Simple calc engine (population)", 2000, sw);
                var e = (IGenericExpression <double>)engine[cur];
                sw.Reset();
                sw.Start();
                var result = e.Evaluate();
                sw.Stop();
                this.PrintSpeedMessage("Simple calc engine (evaluation)", 2000, sw);
            }
        }
示例#2
0
        public SimpleCalcEngineTests()
        {
            var engine  = new SimpleCalcEngine();
            var context = new ExpressionContext();

            context.Imports.AddType(typeof(Math));
            context.Imports.AddType(typeof(Math), "math");
            engine.Context = context;
            _myEngine      = engine;
        }
        public SimpleCalcEngineTests()
        {
            SimpleCalcEngine engine = new SimpleCalcEngine();
            ExpressionContext context = new ExpressionContext();
            context.Imports.AddType(typeof(Math));
            context.Imports.AddType(typeof(Math), "math");

            engine.Context = context;
            MyEngine = engine;
        }
示例#4
0
        public LongScriptTests()
        {
            var engine  = new SimpleCalcEngine();
            var context = new ExpressionContext();

            context.Imports.AddType(typeof(Math));
            //            context.Imports.AddType(typeof(Math), "math");

            //' add convert methods e.g. .ToInt64, .ToString, .ToDateTime...  https://msdn.microsoft.com/en-us/library/system.convert.aspx?f=255&MSPPError=-2147217396
            context.Imports.AddType(typeof(Convert));

            engine.Context = context;
            _myEngine      = engine;
        }
        public void SimpleCalcEngine_Calculate_MixedOperators_ReturnsCorrectResult()
        {
            var calc = new SimpleCalcEngine();

            IEnumerable <INode> nodes = new INode[]
            {
                new Number(10.1m),
                new Operator(OperatorType.Subtract),
                new Number(3),
                new Operator(OperatorType.Multiply),
                new Number(2.5m)
            };

            object result = calc.Evaluate(nodes);

            Assert.IsType <decimal>(result);
            Assert.Equal(((10.1m - 3) * 2.5m), (decimal)result);
        }
        public void SimpleCalcEngine_Calculate_Add3Decimals_ReturnsCorrectResult()
        {
            var calc = new SimpleCalcEngine();

            IEnumerable <INode> nodes = new INode[]
            {
                new Number(10.5m),
                new Operator(OperatorType.Add),
                new Number(20.25m),
                new Operator(OperatorType.Add),
                new Number(30.1m)
            };

            object result = calc.Evaluate(nodes);

            Assert.IsType <decimal>(result);
            Assert.Equal(60.85m, (decimal)result);
        }
        public void SimpleCalcEngine_Calculate_Add3Ints_ReturnsCorrectResult()
        {
            var calc = new SimpleCalcEngine();

            IEnumerable <INode> nodes = new INode[]
            {
                new Number(4),
                new Operator(OperatorType.Add),
                new Number(9),
                new Operator(OperatorType.Add),
                new Number(16)
            };

            object result = calc.Evaluate(nodes);

            Assert.IsType <int>(result);
            Assert.Equal(29, (int)result);
        }
        public void SimpleCalcEngine_Calculate_WithNullNodes_ThrowsException()
        {
            var calc = new SimpleCalcEngine();

            Assert.Throws <ArgumentNullException>(() => calc.Evaluate(null));
        }
示例#9
0
        public void TestSimpleCalcEngine()
        {
            const int ITERATIONS = 1000;

            SimpleCalcEngine engine = new SimpleCalcEngine();
            engine.Context.Imports.AddType(typeof(Math));

            Stopwatch sw = new Stopwatch();

            // Test speed of populating the engine
            string prev = "1";
            string cur = null;

            sw.Start();

            // Create a chain of expressions each referring to the previous one
            for (int i = 0; i <= ITERATIONS - 1; i++) {
                cur = string.Format("i_{0}", Guid.NewGuid().ToString("N"));
                string expression = string.Format("{0} + 1 + cos(3.14)", prev);

                engine.AddGeneric<double>(cur, expression);

                prev = cur;
            }

            sw.Stop();

            this.PrintSpeedMessage("Simple calc engine (population)", ITERATIONS, sw);

            // Evaluate the last expression (which will evaluate all the previous ones up the chain)
            var e = engine[cur] as IGenericExpression<double>;

            sw.Reset();
            sw.Start();

            double result = e.Evaluate();
            sw.Stop();

            this.PrintSpeedMessage("Simple calc engine (evaluation)", ITERATIONS, sw);
        }