예제 #1
0
        public void JSONStringToObject_03()
        {
            var lambda = LambdaCompiler.Compile(@"
json(""[0,1,{'foo':57,'bar':77,'hello':'world'}]"")
");
            var result = lambda();
            var list   = result as List <object>;

            Assert.IsNotNull(list);
            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(0, list[0]);
            Assert.AreEqual(1, list[1]);
            var map = list[2] as Dictionary <string, object>;

            Assert.IsNotNull(map);
            Assert.AreEqual(3, map.Count);
            Assert.AreEqual(57, map["foo"]);
            Assert.AreEqual(77, map["bar"]);
            Assert.AreEqual("world", map["hello"]);
        }
예제 #2
0
        public void Each_02()
        {
            var lambda = LambdaCompiler.Compile(@"
var(@foo, list(57, 67, 77, 87))
var(@bar, list())
each(@ix, foo, {
  if(any(@eq(57, ix), @eq(77, ix)), {
    add(bar, string(ix))
  })
})
bar");
            var result = lambda();

            Assert.IsTrue(result is List <object>);
            var list = result as List <object>;

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual("57", list[0]);
            Assert.AreEqual("77", list[1]);
        }
        public void TestAssignArrayWithExtend6()
        {
            Expression <Func <TestClassA, int[][]> > path1 = a => a.DoubleIntArray;
            Expression body = Expression.Assign(Expression.ArrayAccess(Expression.ArrayAccess(path1.Body, Expression.Constant(1)), Expression.Constant(1)), Expression.Constant(-123));
            Expression <Func <TestClassA, int> > exp = Expression.Lambda <Func <TestClassA, int> >(body, path1.Parameters);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.All);
            var o = new TestClassA {
                DoubleIntArray = new[] { new[] { 12 } }
            };

            Assert.AreEqual(-123, f(o));
            Assert.IsNotNull(o.DoubleIntArray);
            Assert.AreEqual(2, o.DoubleIntArray.Length);
            Assert.IsNotNull(o.DoubleIntArray[0]);
            Assert.AreEqual(1, o.DoubleIntArray[0].Length);
            Assert.AreEqual(12, o.DoubleIntArray[0][0]);
            Assert.IsNotNull(o.DoubleIntArray[1]);
            Assert.AreEqual(2, o.DoubleIntArray[1].Length);
            Assert.AreEqual(-123, o.DoubleIntArray[1][1]);
        }
        public void TestAssignToMultiDimensionalArray2()
        {
            Expression <Func <TestClassA, bool[, ]> > path      = a => a.B.BoolArray;
            Expression <Func <TestClassA, bool> >     condition = a => a.X > 0;
            Expression body = Expression.Assign(Expression.ArrayAccess(path.Body, Expression.Constant(0), Expression.Constant(0)), new ParameterReplacer(condition.Parameters[0], path.Parameters[0]).Visit(condition.Body));
            Expression <Func <TestClassA, bool> > exp = Expression.Lambda <Func <TestClassA, bool> >(body, path.Parameters);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.All);
            var o = new TestClassA {
                B = new TestClassB {
                    BoolArray = new bool[1, 1]
                }
            };

            o.B.BoolArray[0, 0] = true;
            Assert.AreEqual(false, f(o));
            Assert.AreEqual(false, o.B.BoolArray[0, 0]);
            o.X = 1;
            Assert.AreEqual(true, f(o));
            Assert.AreEqual(true, o.B.BoolArray[0, 0]);
        }
예제 #5
0
        public void TestAssignStaticField3()
        {
            Expression <Func <bool> >             path      = () => b;
            Expression <Func <TestClassA, bool> > condition = a => a.X > 0;
            Expression <Func <TestClassA, bool> > exp       = Expression.Lambda <Func <TestClassA, bool> >(Expression.Assign(path.Body, condition.Body), condition.Parameters);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.All);

            Assert.AreEqual(false, f(null));
            Assert.AreEqual(false, b);
            Assert.AreEqual(false, f(new TestClassA()));
            Assert.AreEqual(false, b);
            Assert.AreEqual(false, f(new TestClassA {
                X = -1
            }));
            Assert.AreEqual(false, b);
            Assert.AreEqual(true, f(new TestClassA {
                X = 1
            }));
            Assert.AreEqual(true, b);
        }
        public void TestNullable()
        {
            ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a");
            Expression <Func <TestClassA, int?> > exp = Expression.Lambda <Func <TestClassA, int?> >(Expression.PreDecrementAssign(Expression.ArrayAccess(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("NullableIntArray")), Expression.Constant(0), Expression.Constant(0))), a);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);
            var o = new TestClassA {
                NullableIntArray = new int?[1, 1]
            };

            o.NullableIntArray[0, 0] = 0;
            Assert.AreEqual(-1, f(o));
            Assert.AreEqual(-1, o.NullableIntArray[0, 0]);
            o.NullableIntArray[0, 0] = 1;
            Assert.AreEqual(0, f(o));
            Assert.AreEqual(0, o.NullableIntArray[0, 0]);
            o.NullableIntArray[0, 0] = int.MinValue;
            Assert.AreEqual(int.MaxValue, f(o));
            Assert.AreEqual(int.MaxValue, o.NullableIntArray[0, 0]);
            Assert.IsNull(f(null));
            o.NullableIntArray[0, 0] = null;
            Assert.IsNull(f(o));
            Assert.IsNull(o.NullableIntArray[0, 0]);

            f = LambdaCompiler.Compile(exp, CompilerOptions.None);
            o = new TestClassA {
                NullableIntArray = new int?[1, 1]
            };
            o.NullableIntArray[0, 0] = 0;
            Assert.AreEqual(-1, f(o));
            Assert.AreEqual(-1, o.NullableIntArray[0, 0]);
            o.NullableIntArray[0, 0] = 1;
            Assert.AreEqual(0, f(o));
            Assert.AreEqual(0, o.NullableIntArray[0, 0]);
            o.NullableIntArray[0, 0] = int.MinValue;
            Assert.AreEqual(int.MaxValue, f(o));
            Assert.AreEqual(int.MaxValue, o.NullableIntArray[0, 0]);
            Assert.Throws <NullReferenceException>(() => f(null));
            o.NullableIntArray[0, 0] = null;
            Assert.IsNull(f(o));
            Assert.IsNull(o.NullableIntArray[0, 0]);
        }
예제 #7
0
        public void TestSubLambda2()
        {
            Expression <Func <TestClassA, bool> > exp = o => o.ArrayB.Any(b => b.S == o.S && b.C.ArrayD.All(d => d.S == b.S && d.ArrayE.Any(e => e.S == o.S && e.S == b.S && e.S == d.S)));
            var a = new TestClassA
            {
                S      = "zzz",
                ArrayB = new[]
                {
                    new TestClassB
                    {
                        S = "zzz",
                        C = new TestClassC
                        {
                            ArrayD = new[]
                            {
                                new TestClassD {
                                    S = "zzz", ArrayE = new[] { new TestClassE {
                                                                    S = "zzz"
                                                                }, }
                                },
                                new TestClassD {
                                    S = "zzz", ArrayE = new[] { new TestClassE {
                                                                    S = "zzz"
                                                                }, }
                                }
                            }
                        }
                    },
                }
            };

            Console.WriteLine("Sharp");
            var ethalon = MeasureSpeed(Func3, a, 10000000, null);

            Console.WriteLine("GroboCompile without checking");
            MeasureSpeed(LambdaCompiler.Compile(exp, CompilerOptions.None), a, 10000000, ethalon);
            Console.WriteLine("GroboCompile with checking");
            MeasureSpeed(LambdaCompiler.Compile(exp, CompilerOptions.All), a, 1000000, ethalon);
            Console.WriteLine("Compile");
            MeasureSpeed(exp.Compile(), a, 100000, ethalon);
        }
        public void TestNullable()
        {
            ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a");
            Expression <Func <TestClassA, int?> > exp = Expression.Lambda <Func <TestClassA, int?> >(Expression.PreIncrementAssign(Expression.MakeIndex(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("NullableIntArray")), typeof(NullableIntArray).GetProperty("Item"), new[] { Expression.Constant("zzz"), Expression.Constant(1) })), a);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);
            var o = new TestClassA {
                NullableIntArray = new NullableIntArray()
            };

            o.NullableIntArray["zzz", 1] = 0;
            Assert.AreEqual(1, f(o));
            Assert.AreEqual(1, o.NullableIntArray["zzz", 1]);
            o.NullableIntArray["zzz", 1] = -1;
            Assert.AreEqual(0, f(o));
            Assert.AreEqual(0, o.NullableIntArray["zzz", 1]);
            o.NullableIntArray["zzz", 1] = int.MaxValue;
            Assert.AreEqual(int.MinValue, f(o));
            Assert.AreEqual(int.MinValue, o.NullableIntArray["zzz", 1]);
            Assert.IsNull(f(null));
            o.NullableIntArray["zzz", 1] = null;
            Assert.IsNull(f(o));
            Assert.IsNull(o.NullableIntArray["zzz", 1]);

            f = LambdaCompiler.Compile(exp, CompilerOptions.None);
            o = new TestClassA {
                NullableIntArray = new NullableIntArray()
            };
            o.NullableIntArray["zzz", 1] = 0;
            Assert.AreEqual(1, f(o));
            Assert.AreEqual(1, o.NullableIntArray["zzz", 1]);
            o.NullableIntArray["zzz", 1] = -1;
            Assert.AreEqual(0, f(o));
            Assert.AreEqual(0, o.NullableIntArray["zzz", 1]);
            o.NullableIntArray["zzz", 1] = int.MaxValue;
            Assert.AreEqual(int.MinValue, f(o));
            Assert.AreEqual(int.MinValue, o.NullableIntArray["zzz", 1]);
            Assert.Throws <NullReferenceException>(() => f(null));
            o.NullableIntArray["zzz", 1] = null;
            Assert.IsNull(f(o));
            Assert.IsNull(o.NullableIntArray["zzz", 1]);
        }
예제 #9
0
        public void TestLogical4()
        {
            Expression <Func <TestClassA, bool?> > exp = o => !(o.B.X > 0);
            Func <TestClassA, bool?> compiledExp       = LambdaCompiler.Compile(exp, CompilerOptions.All);

            Assert.IsNull(compiledExp(null));
            Assert.IsNull(compiledExp(new TestClassA()));
            Assert.IsNull(compiledExp(new TestClassA {
                B = new TestClassB()
            }));
            Assert.AreEqual(true, compiledExp(new TestClassA {
                B = new TestClassB {
                    X = -1
                }
            }));
            Assert.AreEqual(false, compiledExp(new TestClassA {
                B = new TestClassB {
                    X = 1
                }
            }));
        }
예제 #10
0
        public void Each_03()
        {
            var lambda = LambdaCompiler.Compile(@"
var(@foo, list(""57"", ""67"", ""77"", ""88.88"", ""97""))
var(@bar, list())
each(@ix, foo, {
  if(any(@eq(""57"", ix), @eq(""77"", ix), @eq(""88.88"", ix)), {
    add(bar, number(ix))
  })
})
bar");
            var result = lambda();

            Assert.IsTrue(result is List <object>);
            var list = result as List <object>;

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(57, list[0]);
            Assert.AreEqual(77, list[1]);
            Assert.AreEqual(88.88, list[2]);
        }
        public void TestAssignArrayWithExtend12()
        {
            Expression <Func <TestClassA, int[]> > path1 = a => a.IntArray;
            Expression <Func <TestClassA, int> >   path2 = a => a.Y;
            Expression body = Expression.Assign(Expression.ArrayAccess(path1.Body, Expression.Constant(1)), new ParameterReplacer(path2.Parameters[0], path1.Parameters[0]).Visit(path2.Body));
            Expression <Action <TestClassA> > exp = Expression.Lambda <Action <TestClassA> >(body, path1.Parameters);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.All);
            var o = new TestClassA();

            f(o);
            Assert.IsNotNull(o.IntArray);
            Assert.AreEqual(2, o.IntArray.Length);
            Assert.AreEqual(0, o.IntArray[1]);
            o.Y = 1;
            f(o);
            Assert.AreEqual(1, o.IntArray[1]);
            f   = LambdaCompiler.Compile(exp, CompilerOptions.None);
            o.Y = 123;
            f(o);
            Assert.AreEqual(123, o.IntArray[1]);
        }
예제 #12
0
        private static Delegate Compile(LambdaExpression lambda)
        {
            //var key = new ExpressionWrapper(lambda);
            var key    = DebugViewGetter(lambda);
            var result = hashtable[key];

            if (result == null)
            {
                lock (lockObject)
                {
                    result = hashtable[key];
                    if (result == null)
                    {
                        result         = LambdaCompiler.Compile(lambda, CompilerOptions.All); //lambda.Compile();
                        hashtable[key] = result;
                    }
                }
            }

            return((Delegate)result);
        }
예제 #13
0
        public void Test1()
        {
            Expression <Func <TestClassA, int?> > exp = o => o.ArrayB[0].X;
            Func <TestClassA, int?> compiledExp       = LambdaCompiler.Compile(exp, CompilerOptions.All);

            Assert.That(compiledExp(null), Is.EqualTo(null));
            Assert.That(compiledExp(new TestClassA()), Is.EqualTo(null));
            Assert.That(compiledExp(new TestClassA {
                ArrayB = new TestClassB[] { null }
            }), Is.EqualTo(null));
            Assert.That(compiledExp(new TestClassA {
                ArrayB = new[] { new TestClassB() }
            }), Is.EqualTo(null));
            int?actual = compiledExp(new TestClassA {
                ArrayB = new[] { new TestClassB {
                                     X = 1
                                 } }
            });

            Assert.That(actual, Is.EqualTo(1));
        }
예제 #14
0
        public void MaxStackSizeNoThrow()
        {
            var nothing = new LambdaCompiler.Nothing();
            var binder  = new Binder <LambdaCompiler.Nothing>();

            binder.MaxStackSize = 21;
            LambdaCompiler.BindFunctions(binder);
            var lambda = LambdaCompiler.Compile(nothing, binder, @"
var(@recursions, 0)
var(@my-func, function({
  set(@recursions,+(recursions,1))
  if(lt(recursions,20),{
    my-func()
  })
}))
my-func()
");

            // Should NOT throw!
            lambda();
        }
        public void Test6()
        {
            Expression <Func <TestClassA, int?> > exp = o => o.ArrayB.Sum(b => b.X);
            Func <TestClassA, int?> compiledExp       = LambdaCompiler.Compile(exp, CompilerOptions.All);

            Assert.That(compiledExp(null), Is.EqualTo(null));
            Assert.That(compiledExp(new TestClassA()), Is.EqualTo(0));
            Assert.That(compiledExp(new TestClassA {
                ArrayB = new[] { new TestClassB() }
            }), Is.EqualTo(0));
            Assert.That(compiledExp(new TestClassA {
                ArrayB = new[] { new TestClassB {
                                     X = 2
                                 } }
            }), Is.EqualTo(2));
            Assert.That(compiledExp(new TestClassA {
                ArrayB = new[] { new TestClassB {
                                     X = 2
                                 }, null }
            }), Is.EqualTo(2));
        }
        public void TestCheckedSigned()
        {
            ParameterExpression           b   = Expression.Parameter(typeof(int), "b");
            Expression <Func <int, int> > exp = Expression.Lambda <Func <int, int> >(Expression.MultiplyAssignChecked(Expression.MakeMemberAccess(null, typeof(TestClassA).GetProperty("IntProp")), b), b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);

            TestClassA.IntProp = 0;
            Assert.AreEqual(0, f(0));
            Assert.AreEqual(0, TestClassA.IntProp);
            TestClassA.IntProp = 1;
            Assert.AreEqual(2, f(2));
            Assert.AreEqual(2, TestClassA.IntProp);
            TestClassA.IntProp = -2;
            Assert.AreEqual(6, f(-3));
            Assert.AreEqual(6, TestClassA.IntProp);
            TestClassA.IntProp = -2;
            Assert.AreEqual(-20, f(10));
            Assert.AreEqual(-20, TestClassA.IntProp);
            TestClassA.IntProp = 2000000000;
            Assert.Throws <OverflowException>(() => f(2000000000));
        }
예제 #17
0
        public void TestPinning1()
        {
            Expression <Func <TestClassA, bool> > path = a => a.StructAArray[0].S == GetString();
            Expression <Func <TestClassA, bool> > exp  = Expression.Lambda <Func <TestClassA, bool> >(Expression.Block(typeof(bool), Expression.Assign(((BinaryExpression)path.Body).Left, Expression.Call(getStringMethod)), path.Body), path.Parameters);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.All);
            var collectingThread = new Thread(Collect);

            collectingThread.Start();
            for (int i = 0; i < 10; ++i)
            {
                var generatingTrashThread = new Thread(GenerateTrash);
                generatingTrashThread.Start();
            }
            for (int i = 0; i < 100000; ++i)
            {
                var o = new TestClassA {
                    ArrayB = new TestClassB[100], IntArray = new int[100]
                };
                Assert.IsTrue(f(o));
            }
        }
예제 #18
0
        private static WeakReference DoTestConstsAreFreedAfterGarbageCollecting2()
        {
            var a = new TestClassA {
                S = "qxx"
            };
            var result = new WeakReference(a);
            var aa     = new TestStructA {
                A = a
            };
            Expression <Func <TestClassA, string> > path = o => o.S;
            var exp = Expression.Lambda <Func <TestClassA, bool> >(Expression.Equal(path.Body, Expression.MakeMemberAccess(Expression.MakeMemberAccess(Expression.Constant(aa), typeof(TestStructA).GetProperty("A")), typeof(TestClassA).GetProperty("S"))), path.Parameters);
            var f   = LambdaCompiler.Compile(exp, CompilerOptions.All);

            Assert.IsTrue(f(new TestClassA {
                S = "qxx"
            }));
            Assert.IsFalse(f(new TestClassA {
                S = "qzz"
            }));
            return(result);
        }
예제 #19
0
        public void Test3()
        {
            ParameterExpression a = Expression.Parameter(typeof(int), "a");
            ParameterExpression b = Expression.Parameter(typeof(int), "b");
            Expression <Func <int, int, int> > exp = Expression.Lambda <Func <int, int, int> >(Expression.MultiplyAssignChecked(a, b), a, b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.All);

            Assert.AreEqual(0, f(0, 1));
            Assert.AreEqual(2, f(1, 2));
            Assert.AreEqual(6, f(-2, -3));
            Assert.AreEqual(-20, f(-2, 10));
            Assert.Throws <OverflowException>(() => f(2000000000, 2000000000));

            exp = Expression.Lambda <Func <int, int, int> >(Expression.Block(typeof(int), Expression.MultiplyAssignChecked(a, b), a), a, b);
            f   = LambdaCompiler.Compile(exp, CompilerOptions.All);
            Assert.AreEqual(0, f(0, 1));
            Assert.AreEqual(2, f(1, 2));
            Assert.AreEqual(6, f(-2, -3));
            Assert.AreEqual(-20, f(-2, 10));
            Assert.Throws <OverflowException>(() => f(2000000000, 2000000000));
        }
예제 #20
0
        public void TestConditional3()
        {
            Expression <Func <TestClassA, string> > path      = a => a.B.S;
            Expression <Func <TestClassA, string> > path2     = a => a.S;
            Expression <Func <TestClassA, bool> >   condition = a => a.S == "zzz";
            Expression assign1 = Expression.Assign(path.Body, Expression.Constant("qxx"));
            Expression assign2 = Expression.Assign(path.Body, Expression.Constant("qzz"));
            Expression test    = new ParameterReplacer(condition.Parameters[0], path.Parameters[0]).Visit(condition.Body);
            Expression assign3 = Expression.Assign(new ParameterReplacer(path2.Parameters[0], path.Parameters[0]).Visit(path2.Body), Expression.Block(typeof(string), Expression.IfThenElse(test, assign1, assign2), path.Body));
            //Expression<Action<TestClassA>> exp = Expression.Lambda<Action<TestClassA>>(Expression.IfThenElse(test, assign1, assign2), path.Parameters);
            Expression <Action <TestClassA> > exp = Expression.Lambda <Action <TestClassA> >(assign3, path.Parameters);
            var action = LambdaCompiler.Compile(exp, CompilerOptions.All);
            var o      = new TestClassA {
                S = "zzz"
            };

            action(o);
            Assert.IsNotNull(o.B);
            Assert.AreEqual(o.B.S, "qxx");
            Assert.AreEqual(o.S, "qxx");
        }
예제 #21
0
        public void NestedFunctionsThrows()
        {
            var lambda  = LambdaCompiler.Compile(@"
var(@func1, function({
  var(@func2, function({
    bar2
  }, @bar2))
  func2(bar1)
}, @bar1))

func2('success')
");
            var success = false;

            try {
                lambda();
            } catch (LizzieRuntimeException) {
                success = true;
            }
            Assert.AreEqual(true, success);
        }
예제 #22
0
        public void TestDouble()
        {
            ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a");
            ParameterExpression b = Expression.Parameter(typeof(double), "b");
            Expression <Func <TestClassA, double, double> > exp = Expression.Lambda <Func <TestClassA, double, double> >(Expression.PowerAssign(Expression.ArrayAccess(Expression.MakeMemberAccess(a, typeof(TestClassA).GetField("DoubleArray")), Expression.Constant(0)), b), a, b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);
            var o = new TestClassA {
                DoubleArray = new[] { 0.0 }
            };

            Assert.AreEqual(1, f(o, 0));
            Assert.AreEqual(1, o.DoubleArray[0]);
            o.DoubleArray[0] = 1;
            Assert.AreEqual(1, f(o, 2));
            Assert.AreEqual(1, o.DoubleArray[0]);
            o.DoubleArray[0] = 2;
            Assert.AreEqual(16, f(o, 4));
            Assert.AreEqual(16, o.DoubleArray[0]);
            o.DoubleArray[0] = -1;
            Assert.AreEqual(1, f(o, 2));
            Assert.AreEqual(1, o.DoubleArray[0]);
            Assert.AreEqual(0, f(null, 1));

            f = LambdaCompiler.Compile(exp, CompilerOptions.None);
            o = new TestClassA {
                DoubleArray = new[] { 0.0 }
            };
            Assert.AreEqual(1, f(o, 0));
            Assert.AreEqual(1, o.DoubleArray[0]);
            o.DoubleArray[0] = 1;
            Assert.AreEqual(1, f(o, 2));
            Assert.AreEqual(1, o.DoubleArray[0]);
            o.DoubleArray[0] = 2;
            Assert.AreEqual(16, f(o, 4));
            Assert.AreEqual(16, o.DoubleArray[0]);
            o.DoubleArray[0] = -1;
            Assert.AreEqual(1, f(o, 2));
            Assert.AreEqual(1, o.DoubleArray[0]);
            Assert.Throws <NullReferenceException>(() => f(null, 1));
        }
        public void TestCheckedSigned()
        {
            ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a");
            ParameterExpression b = Expression.Parameter(typeof(int), "b");
            Expression <Func <TestClassA, int, int> > exp = Expression.Lambda <Func <TestClassA, int, int> >(Expression.SubtractAssignChecked(Expression.MakeIndex(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("IntArray")), typeof(IntArray).GetProperty("Item"), new[] { Expression.Constant("zzz"), Expression.Constant(1) }), b), a, b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);
            var o = new TestClassA {
                IntArray = new IntArray()
            };

            o.IntArray["zzz", 1] = 0;
            Assert.AreEqual(0, f(o, 0));
            Assert.AreEqual(0, o.IntArray["zzz", 1]);
            o.IntArray["zzz", 1] = 1;
            Assert.AreEqual(-1, f(o, 2));
            Assert.AreEqual(-1, o.IntArray["zzz", 1]);
            o.IntArray["zzz", 1] = -1;
            Assert.AreEqual(1, f(o, -2));
            Assert.AreEqual(1, o.IntArray["zzz", 1]);
            o.IntArray["zzz", 1] = 2000000000;
            Assert.Throws <OverflowException>(() => f(o, -2000000000));
            Assert.AreEqual(0, f(null, 1));

            f = LambdaCompiler.Compile(exp, CompilerOptions.None);
            o = new TestClassA {
                IntArray = new IntArray()
            };
            o.IntArray["zzz", 1] = 0;
            Assert.AreEqual(0, f(o, 0));
            Assert.AreEqual(0, o.IntArray["zzz", 1]);
            o.IntArray["zzz", 1] = 1;
            Assert.AreEqual(-1, f(o, 2));
            Assert.AreEqual(-1, o.IntArray["zzz", 1]);
            o.IntArray["zzz", 1] = -1;
            Assert.AreEqual(1, f(o, -2));
            Assert.AreEqual(1, o.IntArray["zzz", 1]);
            o.IntArray["zzz", 1] = 2000000000;
            Assert.Throws <OverflowException>(() => f(o, -2000000000));
            Assert.Throws <NullReferenceException>(() => f(null, 1));
        }
예제 #24
0
        public void TestLogical10()
        {
            Expression <Func <TestClassA, bool?> > exp = o => o.B.X > 0 || o.A.X > 0;
            ParameterExpression var = Expression.Variable(typeof(bool));
            var body = Expression.Block(typeof(bool), new[] { var }, Expression.Assign(var, Expression.Convert(exp.Body, typeof(bool))), var);
            Func <TestClassA, bool> compiledExp = LambdaCompiler.Compile(Expression.Lambda <Func <TestClassA, bool> >(body, exp.Parameters), CompilerOptions.All);

            Assert.IsFalse(compiledExp(null));
            Assert.IsFalse(compiledExp(new TestClassA()));
            Assert.IsFalse(compiledExp(new TestClassA {
                B = new TestClassB()
            }));
            Assert.IsFalse(compiledExp(new TestClassA {
                B = new TestClassB {
                    X = -1
                }
            }));
            Assert.AreEqual(true, compiledExp(new TestClassA {
                B = new TestClassB {
                    X = 1
                }
            }));
            Assert.IsFalse(compiledExp(new TestClassA {
                A = new TestClassA {
                    X = -1
                }
            }));
            Assert.AreEqual(true, compiledExp(new TestClassA {
                A = new TestClassA {
                    X = 1
                }
            }));
            Assert.AreEqual(false, compiledExp(new TestClassA {
                A = new TestClassA {
                    X = -1
                }, B = new TestClassB {
                    X = -1
                }
            }));
        }
예제 #25
0
        public void TestCheckedSigned()
        {
            ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a");
            ParameterExpression b = Expression.Parameter(typeof(int), "b");
            Expression <Func <TestClassA, int, int> > exp = Expression.Lambda <Func <TestClassA, int, int> >(Expression.MultiplyAssignChecked(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("IntProp")), b), a, b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);
            var o = new TestClassA();

            Assert.AreEqual(0, f(o, 0));
            Assert.AreEqual(0, o.IntProp);
            o.IntProp = 1;
            Assert.AreEqual(2, f(o, 2));
            Assert.AreEqual(2, o.IntProp);
            o.IntProp = -2;
            Assert.AreEqual(6, f(o, -3));
            Assert.AreEqual(6, o.IntProp);
            o.IntProp = -2;
            Assert.AreEqual(-20, f(o, 10));
            Assert.AreEqual(-20, o.IntProp);
            o.IntProp = 2000000000;
            Assert.Throws <OverflowException>(() => f(o, 2000000000));
            Assert.AreEqual(0, f(null, 1));

            f = LambdaCompiler.Compile(exp, CompilerOptions.None);
            o = new TestClassA();
            Assert.AreEqual(0, f(o, 0));
            Assert.AreEqual(0, o.IntProp);
            o.IntProp = 1;
            Assert.AreEqual(2, f(o, 2));
            Assert.AreEqual(2, o.IntProp);
            o.IntProp = -2;
            Assert.AreEqual(6, f(o, -3));
            Assert.AreEqual(6, o.IntProp);
            o.IntProp = -2;
            Assert.AreEqual(-20, f(o, 10));
            Assert.AreEqual(-20, o.IntProp);
            o.IntProp = 2000000000;
            Assert.Throws <OverflowException>(() => f(o, 2000000000));
            Assert.Throws <NullReferenceException>(() => f(null, 1));
        }
예제 #26
0
        public void TestField()
        {
            ParameterExpression           b   = Expression.Parameter(typeof(int), "b");
            Expression <Func <int, int> > exp = Expression.Lambda <Func <int, int> >(Expression.AddAssign(Expression.MakeMemberAccess(null, typeof(TestClassA).GetField("IntField")), b), b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);

            TestClassA.IntField = 0;
            Assert.AreEqual(0, f(0));
            Assert.AreEqual(0, TestClassA.IntField);
            TestClassA.IntField = 1;
            Assert.AreEqual(3, f(2));
            Assert.AreEqual(3, TestClassA.IntField);
            TestClassA.IntField = -1;
            Assert.AreEqual(1, f(2));
            Assert.AreEqual(1, TestClassA.IntField);
            TestClassA.IntField = 2000000000;
            unchecked
            {
                Assert.AreEqual(2000000000 + 2000000000, f(2000000000));
                Assert.AreEqual(2000000000 + 2000000000, TestClassA.IntField);
            }
        }
예제 #27
0
        public void TestPropx()
        {
            ParameterExpression        b   = Expression.Parameter(typeof(int), "b");
            Expression <Action <int> > exp = Expression.Lambda <Action <int> >(Expression.Block(typeof(void), Expression.AddAssign(Expression.MakeMemberAccess(null, typeof(TestClassA).GetProperty("IntProp")), b)), b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);

            TestClassA.IntProp = 0;
            f(0);
            Assert.AreEqual(0, TestClassA.IntProp);
            TestClassA.IntProp = 1;
            f(2);
            Assert.AreEqual(3, TestClassA.IntProp);
            TestClassA.IntProp = -1;
            f(2);
            Assert.AreEqual(1, TestClassA.IntProp);
            TestClassA.IntProp = 2000000000;
            unchecked
            {
                f(2000000000);
                Assert.AreEqual(2000000000 + 2000000000, TestClassA.IntProp);
            }
        }
예제 #28
0
        public void TestAssign4()
        {
            Expression <Func <TestClassA, int> > path1 = a => a.Y;
            Expression <Func <TestClassA, int> > path2 = a => a.B.Y;
            Expression body = Expression.Assign(path1.Body, new ParameterReplacer(path2.Parameters[0], path1.Parameters[0]).Visit(path2.Body));
            Expression <Action <TestClassA> > exp = Expression.Lambda <Action <TestClassA> >(body, path1.Parameters);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.All);
            var o = new TestClassA();

            f(o);
            Assert.AreEqual(0, o.Y);
            o.B = new TestClassB();
            f(o);
            Assert.AreEqual(0, o.Y);
            o.B.Y = 12;
            f(o);
            Assert.AreEqual(12, o.Y);
            f     = LambdaCompiler.Compile(exp, CompilerOptions.None);
            o.B.Y = 123;
            f(o);
            Assert.AreEqual(123, o.Y);
        }
예제 #29
0
        public void TestProp()
        {
            ParameterExpression           b   = Expression.Parameter(typeof(int), "b");
            Expression <Func <int, int> > exp = Expression.Lambda <Func <int, int> >(Expression.SubtractAssign(Expression.MakeMemberAccess(null, typeof(TestClassA).GetProperty("IntProp")), b), b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);

            TestClassA.IntProp = 0;
            Assert.AreEqual(0, f(0));
            Assert.AreEqual(0, TestClassA.IntProp);
            TestClassA.IntProp = 1;
            Assert.AreEqual(-1, f(2));
            Assert.AreEqual(-1, TestClassA.IntProp);
            TestClassA.IntProp = -1;
            Assert.AreEqual(1, f(-2));
            Assert.AreEqual(1, TestClassA.IntProp);
            TestClassA.IntProp = 2000000000;
            unchecked
            {
                Assert.AreEqual(2000000000 - -2000000000, f(-2000000000));
                Assert.AreEqual(2000000000 - -2000000000, TestClassA.IntProp);
            }
        }
예제 #30
0
        public void TestCheckedSigned()
        {
            ParameterExpression a = Expression.Parameter(typeof(TestClassA), "a");
            ParameterExpression b = Expression.Parameter(typeof(int), "b");
            Expression <Func <TestClassA, int, int> > exp = Expression.Lambda <Func <TestClassA, int, int> >(Expression.AddAssignChecked(Expression.ArrayAccess(Expression.MakeMemberAccess(a, typeof(TestClassA).GetProperty("IntArray")), Expression.Constant(0)), b), a, b);
            var f = LambdaCompiler.Compile(exp, CompilerOptions.CheckNullReferences);
            var o = new TestClassA {
                IntArray = new[] { 0 }
            };

            Assert.AreEqual(0, f(o, 0));
            Assert.AreEqual(0, o.IntArray[0]);
            o.IntArray[0] = 1;
            Assert.AreEqual(3, f(o, 2));
            Assert.AreEqual(3, o.IntArray[0]);
            o.IntArray[0] = -1;
            Assert.AreEqual(1, f(o, 2));
            Assert.AreEqual(1, o.IntArray[0]);
            o.IntArray[0] = 2000000000;
            Assert.Throws <OverflowException>(() => f(o, 2000000000));
            Assert.AreEqual(0, f(null, 1));

            f = LambdaCompiler.Compile(exp, CompilerOptions.None);
            o = new TestClassA {
                IntArray = new[] { 0 }
            };
            Assert.AreEqual(0, f(o, 0));
            Assert.AreEqual(0, o.IntArray[0]);
            o.IntArray[0] = 1;
            Assert.AreEqual(3, f(o, 2));
            Assert.AreEqual(3, o.IntArray[0]);
            o.IntArray[0] = -1;
            Assert.AreEqual(1, f(o, 2));
            Assert.AreEqual(1, o.IntArray[0]);
            o.IntArray[0] = 2000000000;
            Assert.Throws <OverflowException>(() => f(o, 2000000000));
            Assert.Throws <NullReferenceException>(() => f(null, 1));
        }