Exemplo n.º 1
0
        public void TestExtraVariable()
        {
            EquationConversion.ResetEquationConversion();
            Consolidate.Initialize();

            // test-input variableNotInFunction
            string varToken = EquationConversion.GetVariableToken();

            Consolidate.ConvertAndCheckInputs("x+y", "x,2,3\ny,4,5\nz,6,7", Solver.GetValidOperators(), Solver.GetValidTerminators(), "\n", ",");
            EquationStruct eqRoot = Consolidate.GetEquationStruct();

            IntervalStruct[] vars = Consolidate.GetIntervalStructList();

            EquationStruct targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));

            IntervalStruct[] targetIntervals = new IntervalStruct[] { new IntervalStruct("x", 2, 3, true, true), new IntervalStruct("y", 4, 5, true, true) };

            Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(eqRoot));
            Assert.AreEqual(targetIntervals[0].GetVariableName(), vars[0].GetVariableName());
            Assert.AreEqual(targetIntervals[0].GetMinBound(), vars[0].GetMinBound());
            Assert.AreEqual(targetIntervals[0].GetMaxBound(), vars[0].GetMaxBound());

            Assert.AreEqual(targetIntervals[1].GetVariableName(), vars[1].GetVariableName());
            Assert.AreEqual(targetIntervals[1].GetMinBound(), vars[1].GetMinBound());
            Assert.AreEqual(targetIntervals[1].GetMaxBound(), vars[1].GetMaxBound());

            Assert.AreEqual(2, vars.Length);
        }
Exemplo n.º 2
0
        private static string PrintNodes(EquationStruct node, string indentation, bool isRightOperand)
        {
            string eq = indentation + "+- {" + node.GetOperator() + "}";

            if (node.GetLeftOperand() == null && node.GetRightOperand() == null)
            {
                eq += " " + node.GetVariableName();
            }

            if (isRightOperand && node.GetLeftOperand() == null && node.GetRightOperand() == null)
            {
                indentation += "     ";
            }
            else
            {
                indentation += "|   ";
            }

            eq += System.Environment.NewLine;

            if (node.GetLeftOperand() != null)
            {
                eq += PrintNodes(node.GetLeftOperand(), indentation, false);
            }

            if (node.GetRightOperand() != null)
            {
                eq += PrintNodes(node.GetRightOperand(), indentation, true);
            }

            return(eq);
        }
Exemplo n.º 3
0
        public void TestAdditionEquations()
        {
            string varToken   = EquationConversion.GetVariableToken();
            string constToken = EquationConversion.GetConstToken();

            // test-calculate addition
            EquationStruct equation = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));

            IntervalStruct[] intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true) };

            IntervalStruct range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(5, range.GetMinBound());
            Assert.AreEqual(9, range.GetMaxBound());

            // test-calculate additionconstant
            equation  = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "4", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 3, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(6, range.GetMinBound());
            Assert.AreEqual(7, range.GetMaxBound());

            // unittest-solveradditionleftsidenull
            equation  = new EquationStruct("+", "", new EquationStruct("/", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), new EquationStruct(varToken, "x", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", -3, 5, true, true), new IntervalStruct("z", -1, 1, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);
        }
Exemplo n.º 4
0
        public void TestEqSetVariableName()
        {
            // unittest-equationdatastructuresetvarname
            EquationStruct eq = new EquationStruct("+", "x", null, null);

            eq.SetVariableName("y");
            Assert.AreEqual("y", eq.GetVariableName());
        }
Exemplo n.º 5
0
        public void TestEqSetRightOperand()
        {
            // unittest-equationdatastructuresetrightop
            EquationStruct eq = new EquationStruct("+", "x", null, null);

            eq.SetRightOperand(new EquationStruct("VAR", "z", null, null));
            Assert.AreEqual("z", eq.GetRightOperand().GetVariableName());
        }
Exemplo n.º 6
0
        public void TestVariableNames()
        {
            EquationConversion.ResetEquationConversion();
            EquationConversion.ConfigureParser(Solver.GetValidOperators(), Solver.GetValidTerminators());

            if (EquationConversion.IsReady())
            {
                string varToken = EquationConversion.GetVariableToken();

                // test-input simpleVariableName
                EquationStruct varEq           = EquationConversion.MakeEquationTree("x+y");
                EquationStruct targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x", "y" }, EquationConversion.GetVariableList()));

                // test-input multiCharacterVariableName1
                varEq           = EquationConversion.MakeEquationTree("x1+y");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x1", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x1", "y" }, EquationConversion.GetVariableList()));

                // test-input multiCharacterVariableName2
                varEq           = EquationConversion.MakeEquationTree("x_+y");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x_", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x_", "y" }, EquationConversion.GetVariableList()));

                // test-input multiCharacterVariableName3
                varEq           = EquationConversion.MakeEquationTree("x_1+y");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x_1", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x_1", "y" }, EquationConversion.GetVariableList()));

                // test-input multiCharacterVariableName4
                varEq           = EquationConversion.MakeEquationTree("x_i+y");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x_i", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x_i", "y" }, EquationConversion.GetVariableList()));

                // test-input multiCharacterVariableName5
                varEq           = EquationConversion.MakeEquationTree("x''+y");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x''", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x''", "y" }, EquationConversion.GetVariableList()));

                // test-input multiCharacterVariableName6
                varEq           = EquationConversion.MakeEquationTree("xy+y");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "xy", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "xy", "y" }, EquationConversion.GetVariableList()));
            }
            else
            {
                Assert.Fail("Equation Parser could not be initialized.");
            }
        }
Exemplo n.º 7
0
        public void TestEquationStructConstructor()
        {
            // unittest-equationdatastructureconstruct
            EquationStruct eq = new EquationStruct("+", "x", new EquationStruct("VAR", "y", null, null), new EquationStruct("VAR", "z", null, null));

            Assert.AreEqual("+", eq.GetOperator());
            Assert.AreEqual("x", eq.GetVariableName());
            Assert.AreEqual("y", eq.GetLeftOperand().GetVariableName());
            Assert.AreEqual("z", eq.GetRightOperand().GetVariableName());
        }
Exemplo n.º 8
0
        public void TestEquationStructConstructorWithNulls()
        {
            // unittest-equationdatastructureconstructnulls
            EquationStruct eq = new EquationStruct("+", "x", null, null);

            Assert.AreEqual("+", eq.GetOperator());
            Assert.AreEqual("x", eq.GetVariableName());
            Assert.AreEqual(null, eq.GetLeftOperand());
            Assert.AreEqual(null, eq.GetRightOperand());
        }
Exemplo n.º 9
0
        public void TestUnknownOp()
        {
            // unittest-solverunknownop
            string         varToken = EquationConversion.GetVariableToken();
            EquationStruct equation = new EquationStruct("**", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "x", null, null));

            IntervalStruct[] intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 3, true, true) };

            IntervalStruct range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);
        }
Exemplo n.º 10
0
        public void TestMissingIntervals()
        {
            // unittest-solvermissingintervals
            string         varToken = EquationConversion.GetVariableToken();
            EquationStruct equation = new EquationStruct(varToken, "x", null, null);

            IntervalStruct[] intervals = new IntervalStruct[] { };

            IntervalStruct range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);
        }
Exemplo n.º 11
0
        public void TestSimpleFunctions()
        {
            EquationConversion.ResetEquationConversion();
            EquationConversion.ConfigureParser(Solver.GetValidOperators(), Solver.GetValidTerminators());

            if (EquationConversion.IsReady())
            {
                string varToken   = EquationConversion.GetVariableToken();
                string constToken = EquationConversion.GetConstToken();

                // test-parse addition
                EquationStruct varEq           = EquationConversion.MakeEquationTree("x+y");
                EquationStruct targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x", "y" }, EquationConversion.GetVariableList()));

                // test-parse subtraction
                varEq           = EquationConversion.MakeEquationTree("x-y");
                targetStructure = new EquationStruct("-", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x", "y" }, EquationConversion.GetVariableList()));

                // test-parse multiplication
                varEq           = EquationConversion.MakeEquationTree("x*y");
                targetStructure = new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x", "y" }, EquationConversion.GetVariableList()));

                // test-parse division
                varEq           = EquationConversion.MakeEquationTree("x/y");
                targetStructure = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x", "y" }, EquationConversion.GetVariableList()));

                // test-parse intervalexponents
                varEq           = EquationConversion.MakeEquationTree("2^x");
                targetStructure = new EquationStruct("^", "", new EquationStruct(constToken, "2", null, null), new EquationStruct(varToken, "x", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));

                // test-parse intervalbase
                varEq           = EquationConversion.MakeEquationTree("x^2");
                targetStructure = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "2", null, null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(varEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));
            }
            else
            {
                Assert.Fail("Equation Parser could not be initialized.");
            }
        }
Exemplo n.º 12
0
        public void TestEqBrackets()
        {
            EquationConversion.ResetEquationConversion();
            EquationConversion.ConfigureParser(Solver.GetValidOperators(), Solver.GetValidTerminators());

            if (EquationConversion.IsReady())
            {
                string constToken = EquationConversion.GetConstToken();
                string varToken   = EquationConversion.GetVariableToken();

                // test-parse brackets1
                EquationStruct brackEq         = EquationConversion.MakeEquationTree("x+(y-z)");
                EquationStruct targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct("()", "", new EquationStruct("-", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), null));
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(brackEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x", "y", "z" }, EquationConversion.GetVariableList()));

                // test-parse brackets2
                brackEq = EquationConversion.MakeEquationTree("(x*y)-2^z");
                EquationStruct comp1 = new EquationStruct("()", "", new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null)), null);
                EquationStruct comp2 = new EquationStruct("^", "", new EquationStruct(constToken, "2", null, null), new EquationStruct(varToken, "z", null, null));

                targetStructure = new EquationStruct("-", "", comp1, comp2);
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(brackEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x", "y", "z" }, EquationConversion.GetVariableList()));

                // test-parse brackets3
                brackEq = EquationConversion.MakeEquationTree("w*(x/(y+z))");

                comp1 = new EquationStruct("()", "", new EquationStruct("+", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), null);;
                comp2 = new EquationStruct("()", "", new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), comp1), null);

                targetStructure = new EquationStruct("*", "", new EquationStruct(varToken, "w", null, null), comp2);
                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(brackEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "w", "x", "y", "z" }, EquationConversion.GetVariableList()));

                //  test-parse openRightBracket
                brackEq = EquationConversion.MakeEquationTree("x+(y-z");
                Assert.AreEqual(null, brackEq);

                // test-parse openLeftBracket
                brackEq = EquationConversion.MakeEquationTree("x+y-z)");
                Assert.AreEqual(null, brackEq);
            }
            else
            {
                Assert.Fail("Equation Parser could not be initialized.");
            }
        }
Exemplo n.º 13
0
        /* CONSTRUCTOR */
        public EquationStruct(string op, string vName, EquationStruct eStruct1, EquationStruct eStruct2)
        {
            if (op == "")
            {
                throw new System.ArgumentException("Error: Equation structures must be assigned an operator during initialization.");
            }

            operatr      = op;
            variableName = vName;

            // Operands are allowed to be null
            leftOperand  = eStruct1;
            rightOperand = eStruct2;

            return;
        }
Exemplo n.º 14
0
        public void TestPrintEquation()
        {
            string varToken = EquationConversion.GetVariableToken();

            // test-output\_printequationtree
            EquationStruct equation = new EquationStruct("+", "", new EquationStruct("/", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), new EquationStruct(varToken, "x", null, null));
            string         target   = "";

            target += "+- {+}" + System.Environment.NewLine;
            target += "|   +- {/}" + System.Environment.NewLine;
            target += "|   |   +- {VAR} y" + System.Environment.NewLine;
            target += "|   |   +- {VAR} z" + System.Environment.NewLine;
            target += "|   +- {VAR} x" + System.Environment.NewLine;

            Assert.AreEqual(System.Text.RegularExpressions.Regex.Replace(target, @"\s+", ""), System.Text.RegularExpressions.Regex.Replace(Output.PrintEquationTree(equation), @"\s+", ""));
        }
Exemplo n.º 15
0
        /* HELPER FUNCTIONS */
        private static string PrintEquation(EquationStruct node)
        {
            string equation = "";

            if (node.GetLeftOperand() == null)
            {
                equation = node.GetOperator() + ": " + node.GetVariableName();
            }
            else if (node.GetRightOperand() == null)
            {
                equation = node.GetOperator() + "(" + PrintEquation(node.GetLeftOperand()) + ")";
            }
            else
            {
                equation = node.GetOperator() + "(" + PrintEquation(node.GetLeftOperand()) + ", " + PrintEquation(node.GetRightOperand()) + ")";
            }

            return(equation);
        }
Exemplo n.º 16
0
        public void TestMultiplicationEquations()
        {
            string varToken   = EquationConversion.GetVariableToken();
            string constToken = EquationConversion.GetConstToken();

            // test-calculate multiplication1
            EquationStruct equation = new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));

            IntervalStruct[] intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true) };

            IntervalStruct range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(6, range.GetMinBound());
            Assert.AreEqual(20, range.GetMaxBound());

            // test-calculate multiplication2
            equation  = new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -1, 3, true, true), new IntervalStruct("y", -3, 5, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(-9, range.GetMinBound());
            Assert.AreEqual(15, range.GetMaxBound());

            // test-calculate multiplication3
            equation  = new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -3, -1, true, true), new IntervalStruct("y", -5, -2, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(2, range.GetMinBound());
            Assert.AreEqual(15, range.GetMaxBound());

            // unittest-solvermultiplicationleftsidenull
            equation  = new EquationStruct("*", "", new EquationStruct("/", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), new EquationStruct(varToken, "x", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", -3, 5, true, true), new IntervalStruct("z", -1, 1, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);
        }
Exemplo n.º 17
0
        public void TestConstantValueFunction()
        {
            EquationConversion.ResetEquationConversion();
            EquationConversion.ConfigureParser(Solver.GetValidOperators(), Solver.GetValidTerminators());

            if (EquationConversion.IsReady())
            {
                string constToken = EquationConversion.GetConstToken();

                // test-input functionAsConstant
                EquationStruct constEq         = EquationConversion.MakeEquationTree("42");
                EquationStruct targetStructure = new EquationStruct(constToken, "42", null, null);

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(constEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { }, EquationConversion.GetVariableList()));
            }
            else
            {
                Assert.Fail("Equation Parser could not be initialized.");
            }
        }
Exemplo n.º 18
0
        public void TestIncompleteFunction()
        {
            EquationConversion.ResetEquationConversion();
            EquationConversion.ConfigureParser(Solver.GetValidOperators(), Solver.GetValidTerminators());

            if (EquationConversion.IsReady())
            {
                // unittest-equationconversion missingFunctionValue1
                EquationStruct incompleteEq = EquationConversion.MakeEquationTree("x+");
                Assert.AreEqual(null, incompleteEq);

                // unittest-equationconversion missingFunctionValue2
                incompleteEq = EquationConversion.MakeEquationTree("*x");
                Assert.AreEqual(null, incompleteEq);

                // unittest-equationconversion missingFunctionValue3
                incompleteEq = EquationConversion.MakeEquationTree("x+*y");
                Assert.AreEqual(null, incompleteEq);
            }
            else
            {
                Assert.Fail("Equation Parser could not be initialized.");
            }
        }
Exemplo n.º 19
0
        public void TestUnaryFunction()
        {
            OperatorStruct[] ops      = new OperatorStruct[] { new OperatorStruct("-", 5, true, false, false, false) };
            string           varToken = EquationConversion.GetVariableToken();

            EquationConversion.ResetEquationConversion();
            EquationConversion.ConfigureParser(ops, Solver.GetValidTerminators());

            if (EquationConversion.IsReady())
            {
                string constToken = EquationConversion.GetConstToken();

                // unittest-equationconversionparseunary
                EquationStruct unaryEq         = EquationConversion.MakeEquationTree("-x");
                EquationStruct targetStructure = new EquationStruct("-", "", new EquationStruct(varToken, "x", null, null), null);

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(unaryEq));
                //Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));
            }
            else
            {
                Assert.Fail("Equation Parser could not be initialized.");
            }
        }
Exemplo n.º 20
0
        public void TestAtomicEquations()
        {
            string varToken   = EquationConversion.GetVariableToken();
            string constToken = EquationConversion.GetConstToken();

            // unittest-solverconstantfunction
            EquationStruct equation = new EquationStruct(constToken, "42", null, null);

            IntervalStruct[] intervals = new IntervalStruct[] { };

            IntervalStruct range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(42, range.GetMinBound());
            Assert.AreEqual(42, range.GetMaxBound());

            // unittest-solvervariablefunction
            equation  = new EquationStruct(varToken, "x", null, null);
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 3, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(2, range.GetMinBound());
            Assert.AreEqual(3, range.GetMaxBound());
        }
Exemplo n.º 21
0
        public void TestDivisionEquations()
        {
            string varToken   = EquationConversion.GetVariableToken();
            string constToken = EquationConversion.GetConstToken();

            // test-calculate divisionPositiveDivisor1
            EquationStruct equation = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));

            IntervalStruct[] intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true) };

            IntervalStruct range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(2.0 / 5, range.GetMinBound());
            Assert.AreEqual(4.0 / 3, range.GetMaxBound());

            // test-calculate divisionPositiveDivisor2
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 0, 4, true, true), new IntervalStruct("y", 3, 5, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(0, range.GetMinBound());
            Assert.AreEqual(4.0 / 3, range.GetMaxBound());

            // test-calculate divisionPositiveDivisor3
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -1, 4, true, true), new IntervalStruct("y", 3, 5, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(-1.0 / 3, range.GetMinBound());
            Assert.AreEqual(4.0 / 3, range.GetMaxBound());

            // test-calculate divisionPositiveDivisor4
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -2, -1, true, true), new IntervalStruct("y", 3, 5, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(-2.0 / 3, range.GetMinBound());
            Assert.AreEqual(-1.0 / 5, range.GetMaxBound());

            // test-calculate divisionPositiveDivisor5
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -2, 0, true, true), new IntervalStruct("y", 3, 5, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(-2.0 / 3, range.GetMinBound());
            Assert.AreEqual(0, range.GetMaxBound());

            // test-calculate divisionNegativeDivisor1
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", -5, -3, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(4.0 / -3, range.GetMinBound());
            Assert.AreEqual(2.0 / -5, range.GetMaxBound());

            // test-calculate divisionNegativeDivisor2
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 0, 4, true, true), new IntervalStruct("y", -5, -3, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(4.0 / -3, range.GetMinBound());
            Assert.AreEqual(0, range.GetMaxBound());

            // test-calculate divisionNegativeDivisor3
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -1, 4, true, true), new IntervalStruct("y", -5, -3, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(4.0 / -3, range.GetMinBound());
            Assert.AreEqual(-1.0 / -3, range.GetMaxBound());

            // test-calculate divisionNegativeDivisor4
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -2, 0, true, true), new IntervalStruct("y", -5, -3, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(0, range.GetMinBound());
            Assert.AreEqual(-2.0 / -3, range.GetMaxBound());

            // test-calculate divisionNegativeDivisor5
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -2, -1, true, true), new IntervalStruct("y", -5, -3, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(-1.0 / -5, range.GetMinBound());
            Assert.AreEqual(-2.0 / -3, range.GetMaxBound());

            // test-calculate divisionbyzero
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "0", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);

            // test-calculate divisionMixedIntervalDivisor
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", -3, 5, true, true), new IntervalStruct("z", -1, 1, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);

            // test-calculate divisionMixedIntervalDivisorZero1
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", -3, 0, true, true), new IntervalStruct("z", -1, 1, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);

            // test-calculate divisionMixedIntervalDivisorZero2
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 0, 3, true, true), new IntervalStruct("z", -1, 1, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);

            // test-calculate mixedDivisorComponent
            // Ideally, I would like this to return a partial answer
            equation  = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct("()", "", new EquationStruct("*", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -2, -1, true, true), new IntervalStruct("y", 3, 5, true, true), new IntervalStruct("z", -1, 1, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);

            // unittest-solverdivisionleftsidenull
            equation  = new EquationStruct("/", "", new EquationStruct("/", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), new EquationStruct(varToken, "x", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", -3, 5, true, true), new IntervalStruct("z", -1, 1, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);
        }
Exemplo n.º 22
0
        public void TestExponentEquations()
        {
            string varToken   = EquationConversion.GetVariableToken();
            string constToken = EquationConversion.GetConstToken();

            // test-calculate intervalAsExponents
            EquationStruct equation = new EquationStruct("^", "", new EquationStruct(constToken, "2", null, null), new EquationStruct(varToken, "x", null, null));

            IntervalStruct[] intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true) };

            IntervalStruct range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(4, range.GetMinBound());
            Assert.AreEqual(16, range.GetMaxBound());

            // test-calculate intervalAsExponentsInvalidBase
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "1", null, null), new EquationStruct(constToken, "x", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -4, -2, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);

            // test-calculate intervalWithExponent1
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "3", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(8, range.GetMinBound());
            Assert.AreEqual(64, range.GetMaxBound());

            // test-calculate intervalWithExponent2
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "2", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(4, range.GetMinBound());
            Assert.AreEqual(16, range.GetMaxBound());

            // test-calculate intervalWithExponent3
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "2", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 0, 4, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(0, range.GetMinBound());
            Assert.AreEqual(16, range.GetMaxBound());

            // test-calculate intervalWithExponent4
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "2", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -2, 4, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(0, range.GetMinBound());
            Assert.AreEqual(16, range.GetMaxBound());

            // test-calculate intervalWithExponent5
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "2", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", -4, -2, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(4, range.GetMinBound());
            Assert.AreEqual(16, range.GetMaxBound());

            // test-calculate intervalWithInvalidExponent1
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "2.1", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(4, range.GetMinBound());
            Assert.AreEqual(16, range.GetMaxBound());

            // test-calculate intervalWithInvalidExponent2
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "-1", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);

            // test-calculate\_intervalsOnlyExponentiation
            equation  = new EquationStruct("^", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);

            // unittest-solverexponentleftsidenull
            equation  = new EquationStruct("^", "", new EquationStruct("/", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), new EquationStruct(constToken, "4", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("y", -3, 5, true, true), new IntervalStruct("z", -1, 1, true, true) };

            range = Solver.FindRange(equation, intervals);

            Assert.AreEqual(null, range);
        }
Exemplo n.º 23
0
        public void TestPrecedenceOfOperations()
        {
            string varToken   = EquationConversion.GetVariableToken();
            string constToken = EquationConversion.GetConstToken();

            // x+y−z == (x+y)−z
            EquationStruct equation1 = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct("-", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)));
            EquationStruct equation2 = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct("()", "", new EquationStruct("-", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), null));

            IntervalStruct[] intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true), new IntervalStruct("z", 2, 2, true, true) };

            IntervalStruct range1 = Solver.FindRange(equation1, intervals);
            IntervalStruct range2 = Solver.FindRange(equation2, intervals);

            Assert.AreEqual(range1.GetMinBound(), range2.GetMinBound());
            Assert.AreEqual(range1.GetMaxBound(), range2.GetMaxBound());

            // x∗y/z == (x∗y)/z
            equation1 = new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct("/", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)));
            equation2 = new EquationStruct("/", "", new EquationStruct("()", "", new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null)), null), new EquationStruct(varToken, "z", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true), new IntervalStruct("z", 2, 2, true, true) };

            range1 = Solver.FindRange(equation1, intervals);
            range2 = Solver.FindRange(equation2, intervals);

            Assert.AreEqual(range1.GetMinBound(), range2.GetMinBound());
            Assert.AreEqual(range1.GetMaxBound(), range2.GetMaxBound());

            // x + y∗z == x + (y∗z)
            equation1 = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct("*", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)));
            equation2 = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct("()", "", new EquationStruct("*", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true), new IntervalStruct("z", 2, 2, true, true) };

            range1 = Solver.FindRange(equation1, intervals);
            range2 = Solver.FindRange(equation2, intervals);

            Assert.AreEqual(range1.GetMinBound(), range2.GetMinBound());
            Assert.AreEqual(range1.GetMaxBound(), range2.GetMaxBound());

            // 2x ∗y == (2x)∗y
            equation1 = new EquationStruct("*", "", new EquationStruct(constToken, "2", null, null), new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(varToken, "y", null, null)));
            equation2 = new EquationStruct("*", "", new EquationStruct("()", "", new EquationStruct("*", "", new EquationStruct(constToken, "2", null, null), new EquationStruct(varToken, "x", null, null)), null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true) };

            range1 = Solver.FindRange(equation1, intervals);
            range2 = Solver.FindRange(equation2, intervals);

            Assert.AreEqual(range1.GetMinBound(), range2.GetMinBound());
            Assert.AreEqual(range1.GetMaxBound(), range2.GetMaxBound());

            // x2 ∗y == (x2)∗y
            equation1 = new EquationStruct("*", "", new EquationStruct(varToken, "x2", null, null), new EquationStruct(varToken, "y", null, null));
            equation2 = new EquationStruct("*", "", new EquationStruct("()", "", new EquationStruct(varToken, "x2", null, null), null), new EquationStruct(varToken, "y", null, null));
            intervals = new IntervalStruct[] { new IntervalStruct("x2", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true) };

            range1 = Solver.FindRange(equation1, intervals);
            range2 = Solver.FindRange(equation2, intervals);

            Assert.AreEqual(range1.GetMinBound(), range2.GetMinBound());
            Assert.AreEqual(range1.GetMaxBound(), range2.GetMaxBound());

            // x∗(y + z)
            equation1 = new EquationStruct("*", "", new EquationStruct(varToken, "x", null, null), new EquationStruct("()", "", new EquationStruct("+", "", new EquationStruct(varToken, "y", null, null), new EquationStruct(varToken, "z", null, null)), null));
            intervals = new IntervalStruct[] { new IntervalStruct("x", 2, 4, true, true), new IntervalStruct("y", 3, 5, true, true), new IntervalStruct("z", 2, 2, true, true) };

            range1 = Solver.FindRange(equation1, intervals);

            Assert.AreEqual(10, range1.GetMinBound());
            Assert.AreEqual(28, range1.GetMaxBound());
        }
Exemplo n.º 24
0
 public void SetLeftOperand(EquationStruct eStruct)
 {
     leftOperand = eStruct;
     return;
 }
Exemplo n.º 25
0
 public void SetRightOperand(EquationStruct eStruct)
 {
     rightOperand = eStruct;
     return;
 }
Exemplo n.º 26
0
 public static string PrintEquationTree(EquationStruct eqRoot)
 {
     return(PrintNodes(eqRoot, "", true));
 }
Exemplo n.º 27
0
        public void TestEquationWithConstants()
        {
            EquationConversion.ResetEquationConversion();
            EquationConversion.ConfigureParser(Solver.GetValidOperators(), Solver.GetValidTerminators());

            if (EquationConversion.IsReady())
            {
                string constToken = EquationConversion.GetConstToken();
                string varToken   = EquationConversion.GetVariableToken();

                // test-parse constantValue1
                EquationStruct constEq         = EquationConversion.MakeEquationTree("4+x");
                EquationStruct targetStructure = new EquationStruct("+", "", new EquationStruct(constToken, "4", null, null), new EquationStruct(varToken, "x", null, null));

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(constEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));

                // test-parse constantValue2
                constEq         = EquationConversion.MakeEquationTree("-4+x");
                targetStructure = new EquationStruct("+", "", new EquationStruct(constToken, "-4", null, null), new EquationStruct(varToken, "x", null, null));

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(constEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));

                // test-parse constantValue3
                constEq         = EquationConversion.MakeEquationTree("x/-4");
                targetStructure = new EquationStruct("/", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "-4", null, null));

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(constEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));

                // test-parse constantValue4
                constEq         = EquationConversion.MakeEquationTree("x+4");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "4", null, null));

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(constEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));

                // test-parse constantValue5
                constEq         = EquationConversion.MakeEquationTree("x+-4");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "-4", null, null));

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(constEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));

                // test-parse implicitMultiplication
                constEq         = EquationConversion.MakeEquationTree("4x");
                targetStructure = new EquationStruct("*", "", new EquationStruct(constToken, "4", null, null), new EquationStruct(varToken, "x", null, null));

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(constEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));

                // test-parse\_constantValue6
                constEq         = EquationConversion.MakeEquationTree("x+42");
                targetStructure = new EquationStruct("+", "", new EquationStruct(varToken, "x", null, null), new EquationStruct(constToken, "42", null, null));

                Assert.AreEqual(PrintEquation(targetStructure), PrintEquation(constEq));
                Assert.AreEqual(true, CheckVariableList(new string[] { "x" }, EquationConversion.GetVariableList()));
            }
            else
            {
                Assert.Fail("Equation Parser could not be initialized.");
            }
        }
Exemplo n.º 28
0
 public void TestEquationStructNoOperator()
 {
     // unittest-equationdatastructurenoop
     EquationStruct eq = new EquationStruct("", "", null, null);
 }