コード例 #1
0
        public void TestRebalanceMathematic(MathematicOperation op1, MathematicOperation op2, bool rebalanceExpected)
        {
            // parser will result in a right-weighted tree: A * B + C => A * {B + C}
            // if the left operator is a multiply or divide and the right is an add or subtract,
            // the left operator takes precedence and tree should be rebalanced.
            //
            //   A * B + C => {A * B} + C   rebalanced
            //   A + B * C => A + {B * C}   not rebalanced
            //
            var variable1 = new VariableExpression("variable1");
            var variable2 = new VariableExpression("variable2");
            var value     = new IntegerConstantExpression(99);
            var clause    = new MathematicExpression(variable2, op2, value);
            var expr      = new MathematicExpression(variable1, op1, clause);

            var result = expr.Rebalance() as MathematicExpression;

            Assert.That(result, Is.Not.Null);
            if (rebalanceExpected)
            {
                var expectedLeft = new MathematicExpression(variable1, op1, variable2);
                Assert.That(result.Left, Is.EqualTo(expectedLeft));
                Assert.That(result.Operation, Is.EqualTo(op2));
                Assert.That(result.Right, Is.EqualTo(value));
            }
            else
            {
                Assert.That(result.Left, Is.EqualTo(expr.Left));
                Assert.That(result.Operation, Is.EqualTo(expr.Operation));
                Assert.That(result.Right, Is.EqualTo(expr.Right));
            }
        }
コード例 #2
0
        public void TestRebalanceAddString()
        {
            // "A" + B - C => "A" + (B - C)
            var str       = new StringConstantExpression("ban");
            var exprLeft  = new IntegerConstantExpression(6);
            var exprRight = new IntegerConstantExpression(2);
            var clause    = new MathematicExpression(exprLeft, MathematicOperation.Subtract, exprRight);
            var expr      = new MathematicExpression(str, MathematicOperation.Add, clause);

            var result = expr.Rebalance() as MathematicExpression;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Left, Is.EqualTo(str));
            Assert.That(result.Operation, Is.EqualTo(MathematicOperation.Add));
            Assert.That(result.Right, Is.EqualTo(clause));

            // A - B + "C" => (A - B) + "C"
            clause = new MathematicExpression(exprRight, MathematicOperation.Add, str);
            expr   = new MathematicExpression(exprLeft, MathematicOperation.Subtract, clause);

            result = expr.Rebalance() as MathematicExpression;
            Assert.That(result, Is.Not.Null);
            var expectedLeft = new MathematicExpression(exprLeft, MathematicOperation.Subtract, exprRight);

            Assert.That(result.Left, Is.EqualTo(expectedLeft));
            Assert.That(result.Operation, Is.EqualTo(MathematicOperation.Add));
            Assert.That(result.Right, Is.EqualTo(str));
        }
コード例 #3
0
        public void TestRebalanceComplex()
        {
            //   var1 * 2 + 3 + 4 => {{var1 * 2} + 3} + 4
            //
            // initial parsing will create a right-weighted tree
            //
            //        *
            //   var1     +
            //          2     +
            //              3   4
            //
            // recursive rebalancing while parsing will update the lower tree first (test builds the tree in this state)
            //
            //        *
            //   var1         +
            //            +     4
            //          2   3
            //
            // so when we rebalance the upper tree, we need to pull the 2 all the way up
            //
            //            +
            //        *       +
            //   var1   2   3   4
            //
            // then rebalance the new upper tree
            //
            //                +
            //            +      4
            //        *     3
            //   var1   2

            var variable1 = new VariableExpression("variable1");
            var variable2 = new VariableExpression("variable2");
            var value2    = new IntegerConstantExpression(2);
            var value3    = new IntegerConstantExpression(3);
            var value4    = new IntegerConstantExpression(4);
            var clause2   = new MathematicExpression(value2, MathematicOperation.Add, value3);
            var clause1   = new MathematicExpression(clause2, MathematicOperation.Add, value4);
            var expr      = new MathematicExpression(variable1, MathematicOperation.Multiply, clause1);

            var result = expr.Rebalance() as MathematicExpression;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Operation, Is.EqualTo(MathematicOperation.Add));
            Assert.That(result.Right, Is.InstanceOf <IntegerConstantExpression>());
            var expectedLeft = result.Left as MathematicExpression;

            Assert.That(expectedLeft, Is.Not.Null);
            Assert.That(expectedLeft.Operation, Is.EqualTo(MathematicOperation.Add));
            Assert.That(expectedLeft.Right, Is.InstanceOf <IntegerConstantExpression>());
            var expectedLeftLeft = expectedLeft.Left as MathematicExpression;

            Assert.That(expectedLeftLeft, Is.Not.Null);
            Assert.That(expectedLeftLeft.Operation, Is.EqualTo(MathematicOperation.Multiply));
            Assert.That(expectedLeftLeft.Left, Is.InstanceOf <VariableExpression>());
            Assert.That(expectedLeftLeft.Right, Is.InstanceOf <IntegerConstantExpression>());
        }
コード例 #4
0
        public void TestRebalance()
        {
            var variable = new VariableExpression("variable");
            var value    = new IntegerConstantExpression(99);
            var expr     = new MathematicExpression(variable, MathematicOperation.Add, value);

            var result = expr.Rebalance() as MathematicExpression;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Left, Is.EqualTo(expr.Left));
            Assert.That(result.Operation, Is.EqualTo(expr.Operation));
            Assert.That(result.Right, Is.EqualTo(expr.Right));
        }
コード例 #5
0
        public void TestRebalanceCondition()
        {
            // A + B && C => (A + B) && C
            var variable1 = new VariableExpression("variable1");
            var variable2 = new VariableExpression("variable2");
            var value     = new IntegerConstantExpression(99);
            var clause    = new ConditionalExpression(variable2, ConditionalOperation.And, value);
            var expr      = new MathematicExpression(variable1, MathematicOperation.Add, clause);

            var result = expr.Rebalance() as ConditionalExpression;

            Assert.That(result, Is.Not.Null);
            var expectedLeft = new MathematicExpression(variable1, MathematicOperation.Add, variable2);

            Assert.That(result.Left, Is.EqualTo(expectedLeft));
            Assert.That(result.Operation, Is.EqualTo(clause.Operation));
            Assert.That(result.Right, Is.EqualTo(value));
        }