Beispiel #1
0
        public void ClassicCpsRewriter_Basics16()
        {
            Expression <Func <string> > f = () => "Bar".ToLower();

            var r = new ClassicCpsRewriter().Rewrite(f);

            var res = default(string);

            r.Compile()(s => res = s);

            Assert.AreEqual("bar", res);
        }
Beispiel #2
0
        public void ClassicCpsRewriter_Basics13()
        {
            Expression <Func <string> > f = () => Qux.Bar <string>("Hello, CPS!");

            var r = new ClassicCpsRewriter().Rewrite(f);

            var res = default(string);

            r.Compile()(s => res = s);

            Assert.AreEqual("Hello, CPS!", res);
        }
Beispiel #3
0
        public void ClassicCpsRewriter_Basics12_Func()
        {
            Expression <Func <int, int, int, int> > f = (x, y, z) => Calculator.Add(Calculator.Negate(-z), Calculator.Add(x, y));

            var r = new ClassicCpsRewriter().Rewrite(f);

            var res = default(int);

            r.Compile()(17, 6, 19, x => res = x);

            Assert.AreEqual(42, res);
        }
Beispiel #4
0
        public void ClassicCpsRewriter_Action1()
        {
            var cps = new ClassicCpsRewriter();

            var f = (Expression <Action>)(() => Actions.Foo());

            var g = cps.Rewrite(f);

            var res = default(int);

            g.Compile()(() => res = Actions.Foo0);

            Assert.AreEqual(0, res);
        }
Beispiel #5
0
        public void ClassicCpsRewriter_Basics9_Func()
        {
            Expression <Func <int> > f = () => Calculator.Add(Calculator.Negate(-19), Calculator.Add(17, 6));

            var res = default(int);
            var set = new Action <int>(x => res = x);
            Expression <Action <int> > g = x => set(x);

            var r = new ClassicCpsRewriter().Rewrite(f, g);

            Expression.Lambda <Action>(r).Compile()();

            Assert.AreEqual(42, res);
        }
Beispiel #6
0
        public void ClassicCpsRewriter_Basics7_Func()
        {
            Expression <Func <int, int> > f = x => Calculator.Add(Calculator.Negate(-19), Calculator.Add(x, 6));

            var r = new ClassicCpsRewriter().Rewrite((LambdaExpression)f);

            var g = (Expression <Action <int, Action <int> > >)r;

            var res = default(int);

            g.Compile()(17, x => res = x);

            Assert.AreEqual(42, res);
        }
Beispiel #7
0
        public void ClassicCpsRewriter_Basics6_Func()
        {
            Expression <Func <int> > f = () => Calculator.Add(Calculator.Negate(-19), Calculator.Add(17, 6));

            var r = new ClassicCpsRewriter().Rewrite(f.Body);

            var g = (Expression <Action <Action <int> > >)r;

            var res = default(int);

            g.Compile()(x => res = x);

            Assert.AreEqual(42, res);
        }
Beispiel #8
0
        public void ClassicCpsRewriter_Action2()
        {
            var cps = new ClassicCpsRewriter();

            var f = (Expression <Action <int> >)(x => Actions.Foo(x));

            var g = cps.Rewrite(f);

            var res = default(int);

            g.Compile()(1, () => res = Actions.Foo1);

            Assert.AreEqual(1, res);
        }
Beispiel #9
0
        public void ClassicCpsRewriter_Action4()
        {
            var cps = new ClassicCpsRewriter();

            var f = (Expression <Action <int, int, int> >)((x, y, z) => Actions.Foo(x, y, z));

            var g = cps.Rewrite(f);

            var res = default(int);

            g.Compile()(1, 2, 3, () => res = Actions.Foo3);

            Assert.AreEqual(6, res);
        }
Beispiel #10
0
        public void ClassicCpsRewriter_Basics4()
        {
            Expression <Func <int> > f = () => new Bar().Foo(6, 7);

            var cb = Expression.Parameter(typeof(Action <int>));

            var r = new ClassicCpsRewriter().Rewrite(f.Body, cb);

            var g = Expression.Lambda <Action <Action <int> > >(r, cb);

            var res = default(int);

            g.Compile()(x => res = x);

            Assert.AreEqual(42, res);
        }
Beispiel #11
0
        public void ClassicCpsRewriter_Basics7_Action()
        {
            var b = new Bar();

            Expression <Action <int> > f = x => b.Qux(x * 7);

            var r = new ClassicCpsRewriter().Rewrite((LambdaExpression)f);

            var g = (Expression <Action <int, Action> >)r;

            var res = default(int);

            g.Compile()(6, () => res = b.QuxedValue);

            Assert.AreEqual(42, res);
        }
Beispiel #12
0
        public void ClassicCpsRewriter_Basics6_Action()
        {
            var b = new Bar();

            Expression <Action> f = () => b.Qux(42);

            var r = new ClassicCpsRewriter().Rewrite(f.Body);

            var g = (Expression <Action <Action> >)r;

            var res = default(int);

            g.Compile()(() => res = b.QuxedValue);

            Assert.AreEqual(42, res);
        }
Beispiel #13
0
        public void ClassicCpsRewriter_Basics3()
        {
            Expression <Func <int> > f = () => Calculator.Add(Calculator.Negate(-19), 23);

            var cb = Expression.Parameter(typeof(Action <int>));

            var r = new ClassicCpsRewriter().Rewrite(f.Body, cb);

            var g = Expression.Lambda <Action <Action <int> > >(r, cb);

            var res = default(int);

            g.Compile()(x => res = x);

            Assert.AreEqual(42, res);
        }
Beispiel #14
0
        public void ClassicCpsRewriter_Action5()
        {
            var cps = new ClassicCpsRewriter();

            var f = (Expression <Action>)(() => Actions.Foo());

            var res = default(int);
            var set = (Action <int>)(x => res = x);
            var c   = (Expression <Action>)(() => set(Actions.Foo0));

            var g = Expression.Lambda <Action>(cps.Rewrite(f, c));

            g.Compile()();

            Assert.AreEqual(0, res);
        }
Beispiel #15
0
        public void ClassicCpsRewriter_Basics5()
        {
            var b = new Bar();

            Expression <Action> f = () => b.Qux(42);

            var cb = Expression.Parameter(typeof(Action));

            var r = new ClassicCpsRewriter().Rewrite(f.Body, cb);

            var g = Expression.Lambda <Action <Action> >(r, cb);

            var res = default(int);

            g.Compile()(() => res = b.QuxedValue);

            Assert.AreEqual(42, res);
        }
Beispiel #16
0
        public void ClassicCpsRewriter_SideEffects()
        {
            var log = new List <int>();

            var s1 = new Func <int>(() =>
            {
                log.Add(1);
                return(-7);
            });

            var s2 = new Func <int>(() =>
            {
                log.Add(2);
                return(12);
            });

            var s3 = new Func <int>(() =>
            {
                log.Add(3);
                return(23);
            });

            Expression <Func <int> > f = () => Calculator.Add(Calculator.Negate(s1()), Calculator.Add(s2(), s3()));

            var cb = Expression.Parameter(typeof(Action <int>));

            var r = new ClassicCpsRewriter().Rewrite(f.Body, cb);

            var g = Expression.Lambda <Action <Action <int> > >(r, cb);

            var res = default(int);

            g.Compile()(x => res = x);

            Assert.AreEqual(42, res);

            Assert.IsTrue(new[] { 1, 2, 3 }.SequenceEqual(log));
        }
Beispiel #17
0
        public void ClassicCpsRewriter_ArgumentChecking()
        {
            var cps = new ClassicCpsRewriter();

            var e1 = Expression.Constant(42);

            var f1 = (Expression <Func <int> >)(() => 42);
            var c1 = (Expression <Action <int> >)(x => Console.WriteLine(x));

            var f2 = (Expression <Action>)(() => Console.WriteLine("bar"));
            var c2 = (Expression <Action>)(() => Console.WriteLine("foo"));

            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(Expression)), ex => Assert.AreEqual("expression", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(LambdaExpression)), ex => Assert.AreEqual("expression", ex.ParamName));

#pragma warning disable IDE0034 // Simplify 'default' expression (illustrative of method signature)
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(default(Expression <Func <int> >)), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int>(default(Expression <Func <int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int, int>(default(Expression <Func <int, int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int, int, int>(default(Expression <Func <int, int, int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(Expression <Action>)), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(default(Expression <Action <int> >)), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int>(default(Expression <Action <int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int, int>(default(Expression <Action <int, int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(Expression <Action>), c2), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(f2, default(Expression <Action>)), ex => Assert.AreEqual("continuation", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(default(Expression <Func <int> >), c1), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(f1, default(Expression <Action <int> >)), ex => Assert.AreEqual("continuation", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(Expression), e1), ex => Assert.AreEqual("expression", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(e1, default(Expression)), ex => Assert.AreEqual("continuation", ex.ParamName));
#pragma warning restore IDE0034 // Simplify 'default' expression
        }
Beispiel #18
0
        public void ClassicCpsRewriter_Basics14()
        {
            Expression <Func <int> > f = () => Baz(42);

            Assert.ThrowsException <InvalidOperationException>(() => _ = new ClassicCpsRewriter().Rewrite(f));
        }