Exemple #1
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);
        }
Exemple #2
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);
        }
Exemple #3
0
        public void TestConfigWithNoOperators()
        {
            OperatorStruct[] ops = new OperatorStruct[] { };

            EquationConversion.ResetEquationConversion();

            // unittest-equationconversionconfignoops
            Assert.AreEqual(false, EquationConversion.ConfigureParser(ops, Solver.GetValidTerminators()));
        }
Exemple #4
0
        public void TestConfigWithUnBalancedRightTerminator()
        {
            string[][] terminators = new string[][] { new string[] { "", ")" } };

            EquationConversion.ResetEquationConversion();

            // unittest-equationconversionconfigunbalancedrightterm
            Assert.AreEqual(false, EquationConversion.ConfigureParser(Solver.GetValidOperators(), terminators));
        }
Exemple #5
0
        public void TestConfigWithUnaryOperator()
        {
            OperatorStruct[] ops = new OperatorStruct[] { new OperatorStruct("-", 5, true, false, false, false) };

            EquationConversion.ResetEquationConversion();

            // unittest-equationconversionconfigunary
            Assert.AreEqual(true, EquationConversion.ConfigureParser(ops, Solver.GetValidTerminators()));
        }
Exemple #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.");
            }
        }
Exemple #7
0
        public void TestMissingVariable()
        {
            EquationConversion.ResetEquationConversion();
            Consolidate.Initialize();

            // test-input noDomain
            int success = Consolidate.ConvertAndCheckInputs("x+y", "x,2,3", Solver.GetValidOperators(), Solver.GetValidTerminators(), System.Environment.NewLine, ",");

            Assert.AreEqual(-2, success);
        }
Exemple #8
0
        public void TestIncompleteEquation()
        {
            EquationConversion.ResetEquationConversion();
            Consolidate.Initialize();

            // unittest-consolidateincompleteequation
            int successCode = Consolidate.ConvertAndCheckInputs("", "x,2,3\n", Solver.GetValidOperators(), Solver.GetValidTerminators(), Input.GetLineDelimiter(), Input.GetFieldDelimiter());

            Assert.AreEqual(-3, successCode);
        }
Exemple #9
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);
        }
Exemple #10
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);
        }
Exemple #11
0
        public void TestVarExtraction()
        {
            EquationConversion.ResetEquationConversion();
            Consolidate.Initialize();

            // unittest-consolidateextractvariables
            string[] vars = Consolidate.ExtractVariablesFromEquation("x+y");

            Assert.AreEqual(2, vars.Length);
            Assert.AreEqual("x", vars[0]);
            Assert.AreEqual("y", vars[1]);
        }
Exemple #12
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.");
            }
        }
Exemple #13
0
 public void TestExtractVars()
 {
     EquationConversion.ResetEquationConversion();
     if (ControlFlow.Initialize())
     {
         // unittest-controlextractvars
         string[] vars = ControlFlow.ExtractVariables("x+y");
         Assert.AreEqual("x", vars[0]);
         Assert.AreEqual("y", vars[1]);
     }
     else
     {
         Assert.Fail();
     }
 }
Exemple #14
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.");
            }
        }
Exemple #15
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+", ""));
        }
Exemple #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);
        }
Exemple #17
0
        public void TestFailedConfig()
        {
            EquationConversion.ResetEquationConversion();
            Consolidate.Initialize();

            // unittest-consolidatenoops
            OperatorStruct[] ops = new OperatorStruct[] { };
            int success          = Consolidate.ConvertAndCheckInputs("x+y", "x,2,3\ny,4,5", ops, Solver.GetValidTerminators(), System.Environment.NewLine, ",");

            Assert.AreEqual(-1, success);

            // unittest-consolidatenorightterm
            string[][] terminators = new string[][] { new string[] { "(", "" } };
            success = Consolidate.ConvertAndCheckInputs("x+y", "x,2,3\ny,4,5", Solver.GetValidOperators(), terminators, System.Environment.NewLine, ",");
            Assert.AreEqual(-1, success);

            // unittest-consolidatenoleftterm
            terminators = new string[][] { new string[] { "", ")" } };
            success     = Consolidate.ConvertAndCheckInputs("x+y", "x,2,3\ny,4,5", Solver.GetValidOperators(), terminators, System.Environment.NewLine, ",");
            Assert.AreEqual(-1, success);
        }
Exemple #18
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.");
            }
        }
Exemple #19
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());
        }
Exemple #20
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.");
            }
        }
Exemple #21
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.");
            }
        }
Exemple #22
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.");
            }
        }
Exemple #23
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);
        }
Exemple #24
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);
        }
Exemple #25
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());
        }