Example #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);
        }
Example #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);
        }
Example #3
0
        public void TestPrintIntervals()
        {
            // test - output\_printvariableintervalWithName
            IntervalStruct interval = new IntervalStruct("x", 3, 5, true, true);

            Assert.AreEqual("x = [3, 5]", Output.PrintInterval(interval, true));

            // test-output\_printvariableintervalNoName
            interval = new IntervalStruct("x", 3, 5, true, true);
            Assert.AreEqual("[3, 5]", Output.PrintInterval(interval, false));

            // unittest-outputopenstructure
            interval = new IntervalStruct("x", 3, 5, false, false);
            Assert.AreEqual("x = (3, 5)", Output.PrintInterval(interval, true));

            // unittest-outputclosedleftstructure
            interval = new IntervalStruct("x", 3, 5, true, false);
            Assert.AreEqual("x = [3, 5)", Output.PrintInterval(interval, true));

            // unittest-outputclosedrightstructure
            interval = new IntervalStruct("x", 3, 5, false, true);
            Assert.AreEqual("x = (3, 5]", Output.PrintInterval(interval, true));

            // unittest-outputconstwithname
            interval = new IntervalStruct("", 3, 3, false, false);
            Assert.AreEqual("CONST: 3", Output.PrintInterval(interval, true));

            // unittest-outputconstnoname
            interval = new IntervalStruct("", 3, 3, false, false);
            Assert.AreEqual("CONST: 3", Output.PrintInterval(interval, false));

            // unittest-outputlongresult
            interval = new IntervalStruct("r", 0.333333333333, 0.66666666666666, true, true);
            Assert.AreEqual("[0.333333333333, " + System.Environment.NewLine + " 0.66666666666666]", Output.PrintInterval(interval, false));
        }
Example #4
0
        public void TestReversedBounds()
        {
            //test-parse domainOrder
            IntervalStruct interval = new IntervalStruct("x", 4.0, 3.0, true, true);

            Assert.AreEqual(3, interval.GetMinBound());
            Assert.AreEqual(4, interval.GetMaxBound());
        }
Example #5
0
        public void TestSetVariableName()
        {
            // unittest-intervaldatastructuresetvarname
            IntervalStruct interval = new IntervalStruct("x", 3, 4, true, true);

            interval.SetVariableName("y");
            Assert.AreEqual("y", interval.GetVariableName());
        }
Example #6
0
        public void TestSetMinHigherThanMax()
        {
            // unittest-intervaldatastructuresethighmin
            IntervalStruct interval = new IntervalStruct("x", 3, 4, true, true);

            interval.SetMinBound(5);
            Assert.AreEqual(4, interval.GetMinBound());
            Assert.AreEqual(5, interval.GetMaxBound());
        }
Example #7
0
        public void TestSetMaxLowerThanMin()
        {
            // unittest-intervaldatastructuresetlowmax
            IntervalStruct interval = new IntervalStruct("x", 3, 4, true, true);

            interval.SetMaxBound(-1);
            Assert.AreEqual(-1, interval.GetMinBound());
            Assert.AreEqual(3, interval.GetMaxBound());
        }
Example #8
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);
        }
Example #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);
        }
Example #10
0
        public void TestSetMethodsSimple()
        {
            // unittest-intervaldatastructuresetters
            IntervalStruct interval = new IntervalStruct("x", 3, 4, false, true);

            interval.SetMinBound(2);
            interval.SetMaxBound(5);
            interval.SetLeftBoundClosed(true);
            interval.SetRightBoundClosed(false);

            Assert.AreEqual(2, interval.GetMinBound());
            Assert.AreEqual(5, interval.GetMaxBound());
            Assert.AreEqual(true, interval.IsLeftBoundClosed());
            Assert.AreEqual(false, interval.IsRightBoundClosed());
        }
Example #11
0
        public static string PrintInterval(IntervalStruct interval, bool withVarName)
        {
            string iv = "";

            if (interval.GetMinBound() == interval.GetMaxBound())
            {
                iv = "CONST: " + interval.GetMinBound();
            }
            else
            {
                if (withVarName)
                {
                    iv += interval.GetVariableName() + " = ";
                }

                if (interval.IsLeftBoundClosed())
                {
                    iv += "[";
                }
                else
                {
                    iv += "(";
                }

                if (interval.GetMinBound().ToString().Length > 12)
                {
                    iv += interval.GetMinBound() + ", " + System.Environment.NewLine + " " + interval.GetMaxBound();
                }
                else
                {
                    iv += interval.GetMinBound() + ", " + interval.GetMaxBound();
                }


                if (interval.IsRightBoundClosed())
                {
                    iv += "]";
                }
                else
                {
                    iv += ")";
                }
            }

            return(iv);
        }
Example #12
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);
        }
Example #13
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());
        }
        public static bool TryGetRange <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly, Expression, Variable>(this Variable var, FlatDomain <Type> t,
                                                                                                                                           IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> MetaDataDecoder, out IntervalStruct result)
            where Type : IEquatable <Type>
        {
            Contract.Requires(MetaDataDecoder != null);

            if (t.IsNormal)
            {
                var type = t.Value;
                if (MetaDataDecoder.System_Boolean.Equals(type) || MetaDataDecoder.System_Int32.Equals(type))
                {
                    result = new IntervalStruct(Int32.MinValue, Int32.MaxValue, (Decimal d) => BoxedExpression.Const((int)d, MetaDataDecoder.System_Int32, MetaDataDecoder));
                    return(true);
                }
                if (MetaDataDecoder.System_Char.Equals(type))
                {
                    result = new IntervalStruct(Char.MinValue, Char.MaxValue, (Decimal d) => BoxedExpression.Const((char)d, MetaDataDecoder.System_Char, MetaDataDecoder));
                    return(true);
                }
                if (MetaDataDecoder.System_Int8.Equals(type))
                {
                    result = new IntervalStruct(SByte.MinValue, SByte.MaxValue, (Decimal d) => BoxedExpression.Const((sbyte)d, MetaDataDecoder.System_Int8, MetaDataDecoder));
                    return(true);
                }
                if (MetaDataDecoder.System_Int16.Equals(type))
                {
                    result = new IntervalStruct(short.MinValue, short.MaxValue, (Decimal d) => BoxedExpression.Const((short)d, MetaDataDecoder.System_Int16, MetaDataDecoder));
                    return(true);
                }
                if (MetaDataDecoder.System_Int64.Equals(type))
                {
                    result = new IntervalStruct(long.MinValue, long.MaxValue, (Decimal d) => BoxedExpression.Const((long)d, MetaDataDecoder.System_Int64, MetaDataDecoder));
                    return(true);
                }
                if (MetaDataDecoder.System_UInt8.Equals(type))
                {
                    result = new IntervalStruct(byte.MinValue, byte.MaxValue, (Decimal d) => BoxedExpression.Const((byte)d, MetaDataDecoder.System_UInt8, MetaDataDecoder));
                    return(true);
                }
                if (MetaDataDecoder.System_UInt16.Equals(type))
                {
                    result = new IntervalStruct(UInt16.MinValue, UInt16.MaxValue, (Decimal d) => BoxedExpression.Const((UInt16)d, MetaDataDecoder.System_UInt16, MetaDataDecoder));
                    return(true);
                }
                if (MetaDataDecoder.System_UInt32.Equals(type))
                {
                    result = new IntervalStruct(UInt32.MinValue, UInt32.MaxValue, (Decimal d) => BoxedExpression.Const((UInt32)d, MetaDataDecoder.System_UInt32, MetaDataDecoder));
                    return(true);
                }
                if (MetaDataDecoder.System_UInt64.Equals(type))
                {
                    result = new IntervalStruct(UInt64.MinValue, UInt64.MaxValue, (Decimal d) => BoxedExpression.Const((UInt64)d, MetaDataDecoder.System_UInt64, MetaDataDecoder));
                    return(true);
                }

                // return false for Single, Double,
            }

            result = default(IntervalStruct);
            return(false);
        }
Example #15
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);
        }
Example #16
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);
        }
Example #17
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());
        }