Exemplo n.º 1
0
        public void ExpressionHeap_ConstantExpression_ExpressionValue()
        {
            var innerExpr = Expression.Lambda(Expression.Default(typeof(string)));
            var outerExpr = Expression.Constant(innerExpr);
            var innerCopy = Expression.Lambda(Expression.Default(typeof(string)));
            var heap      = new ExpressionHeap();
            var outerRef  = heap.Create(outerExpr);
            var copyRef   = heap.Create(innerCopy);

            Assert.AreSame(((ConstantExpression)outerRef.Value).Value, copyRef.Value);
        }
Exemplo n.º 2
0
        public void ExpressionHeap_AssertSame()
        {
            var e1 = Expression.Lambda(Expression.Default(typeof(string)));
            var e2 = Expression.Lambda(Expression.Default(typeof(string)));

            Assert.AreNotSame(e1, e2);

            var heap = new ExpressionHeap();
            var r1   = heap.Create(e1);
            var r2   = heap.Create(e2);

            Assert.AreSame(r1.Value, r2.Value);
        }
Exemplo n.º 3
0
        public void ExpressionHeap_DummyCacheStorage()
        {
            var e1 = Expression.Lambda(Expression.Default(typeof(string)));
            var e2 = Expression.Lambda(Expression.Default(typeof(string)));

            Assert.AreNotSame(e1, e2);

            var heap = new ExpressionHeap(ConstantHoister.Create(false), _ => new Cache <Expression>(new DummyStorage()));
            var r1   = heap.Create(e1);
            var r2   = heap.Create(e2);

            Assert.AreNotSame(r1.Value, r2.Value);
        }
Exemplo n.º 4
0
        public void ExpressionHeap_Concurrency()
        {
            var exprs = new Expression[]
            {
                Expression.Parameter(typeof(int)),
                Expression.Parameter(typeof(string), Guid.NewGuid().ToString()),
                Expression.Parameter(typeof(long), "foo"),
                Expression.Parameter(typeof(double), "foo"),
                Expression.Default(typeof(int)),
#pragma warning disable IDE0004 // Remove Unnecessary Cast. (Makes the intent clear.)
                (Expression <Func <int> >)(() => 42),
                (Expression <Func <int, int> >)(x => x + 2),
                (Expression <Func <double, int> >)(x => (int)Math.Round(x) + 2),
                (Expression <Func <int[]> >)(() => new[] { 1, 2, 3 }),
                (Expression <Func <string> >)(() => Tuple.Create(1, "foo").Item2),
#pragma warning restore IDE0004
            };

            var rand  = new Random(17);
            var tests = Enumerable.Repeat(exprs, 1000).SelectMany(x => x).OrderBy(_ => rand.Next());

            var heap = new ExpressionHeap();

            Parallel.ForEach(tests, e => AssertEqual(e, heap.Create(e).Value));
        }
Exemplo n.º 5
0
        public void ExpressionHeap_Roundtrip()
        {
            var exprs = new Expression[]
            {
                Expression.Parameter(typeof(int)),
                Expression.Parameter(typeof(string), Guid.NewGuid().ToString()),
                Expression.Parameter(typeof(long), "foo"),
                Expression.Parameter(typeof(double), "foo"),
                Expression.Default(typeof(int)),
#pragma warning disable IDE0004 // Remove Unnecessary Cast. (Only unnecessary on C# 10 or later.)
                (Expression <Func <int> >)(() => 42),
                (Expression <Func <int, int> >)(x => x + 2),
                (Expression <Func <double, int> >)(x => (int)Math.Round(x) + 2),
                (Expression <Func <int[]> >)(() => new[] { 1, 2, 3 }),
                (Expression <Func <string> >)(() => Tuple.Create(1, "foo").Item2),
#pragma warning restore IDE0004
            };

            var heap = new ExpressionHeap();

            foreach (var expr in exprs)
            {
                AssertEqual(expr, heap.Create(expr).Value);
            }
        }
Exemplo n.º 6
0
        public void ExpressionHeap_CacheEqualityRegressionTest()
        {
            var cache = new ExpressionHeap();

            Expression <Func <bool> > f1 = () => object.Equals(42, 7.0);
            Expression <Func <bool> > f2 = () => object.Equals(7.0, 42);
            Expression <Func <bool> > f3 = () => object.Equals(42, 7.0);

            var r1 = cache.Create(f1);
            var r2 = cache.Create(f2);
            var r3 = cache.Create(f3);

            var eqc = new ExpressionEqualityComparer();

            Assert.IsTrue(eqc.Equals(f1, r1.Value));
            Assert.IsTrue(eqc.Equals(f2, r2.Value));
            Assert.IsTrue(eqc.Equals(f3, r3.Value));
        }
Exemplo n.º 7
0
        public void ExpressionHeap_HoistedConstants_Shared()
        {
            var heap = new ExpressionHeap(ConstantHoister.Create(
                                              false,
                                              (Expression <Func <string, string> >)(c => string.Format(c, default(object)))));

            Expression <Func <object, string> > f1 = x => string.Format("{0}", x);
            Expression <Func <object, string> > f2 = x => string.Format("{0}", x);

            Assert.AreNotSame(f1, f2);

            var r1 = heap.Create(f1);
            var r2 = heap.Create(f2);
            var v1 = r1.Value;
            var v2 = r2.Value;

            Assert.AreSame(v1, v2);
        }
Exemplo n.º 8
0
        public void ExpressionHeap_Roundtrip()
        {
            var exprs = new Expression[]
            {
                Expression.Parameter(typeof(int)),
                Expression.Parameter(typeof(string), Guid.NewGuid().ToString()),
                Expression.Parameter(typeof(long), "foo"),
                Expression.Parameter(typeof(double), "foo"),
                Expression.Default(typeof(int)),
                (Expression <Func <int> >)(() => 42),
                (Expression <Func <int, int> >)(x => x + 2),
                (Expression <Func <double, int> >)(x => (int)Math.Round(x) + 2),
                (Expression <Func <int[]> >)(() => new[] { 1, 2, 3 }),
                (Expression <Func <string> >)(() => Tuple.Create(1, "foo").Item2),
            };

            var heap = new ExpressionHeap();

            foreach (var expr in exprs)
            {
                AssertEqual(expr, heap.Create(expr).Value);
            }
        }