Beispiel #1
0
        public void BottomUpOptimizer_ArgumentChecking()
        {
            AssertEx.ThrowsException <ArgumentNullException>(() => new BottomUpOptimizer <ITree <Bar>, Bar, BarWildcards>(sourceTreeComparer: null, EqualityComparer <Bar> .Default), ex => Assert.AreEqual("sourceTreeComparer", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => new BottomUpOptimizer <ITree <Bar>, Bar, BarWildcards>(EqualityComparer <ITree <Bar> > .Default, sourceNodeComparer: null), ex => Assert.AreEqual("sourceNodeComparer", ex.ParamName));

            var burs = new BottomUpOptimizer <ITree <Bar>, Bar, BarWildcards>();

            AssertEx.ThrowsException <ArgumentNullException>(() => burs.Optimize(tree: null), ex => Assert.AreEqual("tree", ex.ParamName));
        }
Beispiel #2
0
        public void BottomUpOptimizer_Logic1()
        {
            var logger = new StringWriter();

            var burw = new BottomUpOptimizer <LogicExpr, LogicNodeType, LogicWildcardFactory>
            {
                // Leaf nodes
                Leaves =
                {
                    { (BoolConst b) => b, 1 },
                },

                Rules =
                {
                    // Tree patterns
                    { () => !BoolConst.True,    () => BoolConst.False, 1 },
                    { () => !BoolConst.False,   () => BoolConst.True,  1 },
                    { p => !!p,                 p => p,                2 },

                    { p => p & BoolConst.True,  p => p,                2 },
                    { p => p & BoolConst.False, p => BoolConst.False,  2 },
                    { p => BoolConst.True & p,  p => p,                2 },
                    { p => BoolConst.False & p, p => BoolConst.False,  2 },

                    /*
                     * With those rules commented out, we got a partial tree rewrite using Update
                     * method calls on the tree nodes.
                     *
                     * { p => p | BoolConst.True, p => BoolConst.True, 2 },
                     * { p => p | BoolConst.False, p => p, 2 },
                     * { p => BoolConst.True | p, p => BoolConst.True, 2 },
                     * { p => BoolConst.False | p, p => p, 2 },
                     *
                     */

                    { (p,                       q) => !(!p & !q),      (p, q) => p | q, 1},
                    { (p,                       q) => !(!p | !q),      (p, q) => p & q, 1},
                },

                Log = logger
            };

            // Internal tables
            var debugView = burw.DebugView;

            Assert.IsTrue(!string.IsNullOrEmpty(debugView));

            var e = !(!BoolConst.True & !BoolConst.False);

            var res = burw.Optimize(e);

            Assert.AreEqual("Or(True, False)", res.ToString());
            Assert.AreEqual(e.Eval(), res.Eval());
        }
Beispiel #3
0
        public void BottomUpOptimizer_Logic2()
        {
            var logger = new StringWriter();

            var burw = new BottomUpOptimizer <LogicExpr, LogicNodeType, LogicWildcardFactory>
            {
                // Leaf nodes
                Leaves =
                {
                    { (BoolConst b) => b, 1 },
                },

                Rules =
                {
                    // Tree patterns
                    { () => !BoolConst.True,    () => BoolConst.False, 1 },
                    { () => !BoolConst.False,   () => BoolConst.True,  1 },
                    { p => !!p,                 p => p,                2 },

                    { p => p & BoolConst.True,  p => p,                2 },
                    { p => p & BoolConst.False, p => BoolConst.False,  2 },
                    { p => BoolConst.True & p,  p => p,                2 },
                    { p => BoolConst.False & p, p => BoolConst.False,  2 },

                    { p => p | BoolConst.True,  p => BoolConst.True,   2 },
                    { p => p | BoolConst.False, p => p,                2 },
                    { p => BoolConst.True | p,  p => BoolConst.True,   2 },
                    { p => BoolConst.False | p, p => p,                2 },

                    { (p,                       q) => !(!p & !q),      (p, q) => p | q, 1},
                    { (p,                       q) => !(!p | !q),      (p, q) => p & q, 1},
                },

                Log = logger
            };

            // Internal tables
            var debugView = burw.DebugView;

            Assert.IsTrue(!string.IsNullOrEmpty(debugView));

            var e = !(!BoolConst.True & !BoolConst.False);

            var res = burw.Optimize(e);

            Assert.AreEqual("True", res.ToString());
            Assert.AreEqual(e.Eval(), res.Eval());
        }