示例#1
0
        public void Test()
        {
            AlgorithmEngine engine = new AlgorithmEngine();
            double          t      = 0.0;

            if (engine.Parse("1+2"))
            {
                t = (double)engine.Evaluate();
            }
            Assert.AreEqual(3.0, t);

            var c = engine.TryEvaluate("2+3", 0);

            Assert.AreEqual(5, c);

            var e = engine.TryEvaluate("e", 0.0);

            Assert.AreEqual(Math.E, e);
            e = engine.TryEvaluate("pi", 0.0);
            Assert.AreEqual(Math.PI, e);

            var b = engine.TryEvaluate("true", true);

            Assert.AreEqual(true, b);
            b = engine.TryEvaluate("false", false);
            Assert.AreEqual(false, b);

            var b1 = engine.TryEvaluate("if(true,1,2)", 0);

            Assert.AreEqual(1, b1);

            b1 = engine.TryEvaluate("if(false,1,2)", 0);
            Assert.AreEqual(2, b1);

            var b2 = engine.TryEvaluate("pi*4", 0.0);

            Assert.AreEqual(Math.PI * 4, b2);
            b2 = engine.TryEvaluate("e*4", 0.0);
            Assert.AreEqual(Math.E * 4, b2);

            var s = engine.TryEvaluate("'aa'&'bb'", "");

            Assert.AreEqual("aabb", s);

            var r = engine.TryEvaluate("count({ 1,2,3,4})", 0);

            Assert.AreEqual(4, r);


            r = engine.TryEvaluate("(1=1)*9+2", 0);
            Assert.AreEqual(11, r);;
            r = engine.TryEvaluate("(1=2)*9+2", 0);
            Assert.AreEqual(2, r);;

            var dt = engine.TryEvaluate("'2016-1-1'+1", DateTime.MinValue);

            Assert.AreEqual(DateTime.Parse("2016-1-2"), dt);;
            dt = engine.TryEvaluate("'2016-1-1'+9*'1:0'", DateTime.MinValue);
            Assert.AreEqual(DateTime.Parse("2016-1-1 9:0"), dt);;
        }
示例#2
0
        public void name_Test()
        {
            AlgorithmEngine engine = new AlgorithmEngine();

            if (engine.Parse("a", "1+(3*2+2)/2"))
            {
                var t = engine.Evaluate("a");
                Assert.AreEqual(5, (int)(double)t);
                return;
            }
            throw new Exception("");
        }