示例#1
0
        public void CustomFunction()
        {
            var functions = new string[]
            {
                "f(x) = x",
                "g(x) = f(x + 2)"
            };

            CompileResult result = InvariantCompiler.Compile(functions);

            result.IsSuccess.Should().BeTrue();

            result.Functions.Count.Should().Be(2);
            result.Functions[0].Should().NotBeNull();

            Func <double, double> g = result.Functions[1];

            g.Should().NotBeNull();

            g(0).Should().Be(2);
            g(-2).Should().Be(0);
            g(2).Should().Be(4);

            result.Functions[1](2).Should().Be(result.Functions[0](4));
        }
示例#2
0
        public void EmptyFunctionCallWithNegation_ParseErrors()
        {
            CompileResult result = InvariantCompiler.Compile("tan(-)");

            result.IsSuccess.Should().BeFalse();

            result.Errors.Should().ContainSingle(e => e.Type == ErrorType.InvalidTerm);
        }
示例#3
0
        public void ImpliedMultiplication_NumberVariable(string expression)
        {
            Func <double, double> function = InvariantCompiler.CompileFunction(expression);

            function(0).Should().Be(0);
            function(1).Should().Be(2);
            function(-1).Should().Be(-2);
        }
示例#4
0
        public void UnaryMinus_Whitespace()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction(" -  x ");

            function(0).Should().Be(0);
            function(1).Should().Be(-1);
            function(-1).Should().Be(1);
        }
示例#5
0
        public void UnaryOperatorPrecedence_Power()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("-2^x");

            double expectedValue = -1 * Math.Pow(2, 10);

            function(10).Should().Be(expectedValue);
        }
示例#6
0
        public void FunctionName_InvalidChars()
        {
            CompileResult result = InvariantCompiler.Compile("a!= x");

            result.IsSuccess.Should().BeFalse();

            result.Errors.Should().ContainSingle(e => e.Type == ErrorType.InvalidFunctionName);
        }
示例#7
0
        public void ConstantAddition(string expression, double expectedValue)
        {
            Func <double, double> function = InvariantCompiler.CompileFunction(expression);

            function(0).Should().Be(expectedValue);
            function(-100).Should().Be(expectedValue);
            function(100).Should().Be(expectedValue);
        }
示例#8
0
        public void CircularReference_ParseErrors()
        {
            CompileResult result = InvariantCompiler.Compile("f = x + f(x)");

            result.IsSuccess.Should().BeFalse();

            result.Errors.Should().ContainSingle(e => e.Type == ErrorType.CyclicFunctions);
        }
        public void Constant(double value)
        {
            Func <double, double> function = InvariantCompiler.CompileFunction(value.ToString());

            function(0).Should().Be(value);
            function(-100).Should().Be(value);
            function(100).Should().Be(value);
        }
示例#10
0
        public void ParameterAddition()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("x + 1");

            function(0).Should().Be(1);
            function(1).Should().Be(2);
            function(-1).Should().Be(0);
        }
示例#11
0
        public void UnaryOperatorPrecedence_Addition()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("-x+4+2");

            double expectedValue = -10 + 4 + 2;

            function(10).Should().Be(expectedValue);
        }
示例#12
0
        public void UnaryOperatorPrecedence_Multiplication()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("-x*4+2");

            double expectedValue = -2 * 4 + 2;

            function(2).Should().Be(expectedValue);
        }
示例#13
0
        public void FunctionCallWithVariable_SinFunction()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("sin(x)");

            function(0).Should().Be(0);
            function(0.5 * Math.PI).Should().BeApproximately(1, 1E-15);
            function(Math.PI).Should().BeApproximately(0, 1E-15);
        }
示例#14
0
        public void VariableOnly_LinearFunction()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("x");

            function(0).Should().Be(0);
            function(1).Should().Be(1);
            function(-1).Should().Be(-1);
        }
示例#15
0
        public void E()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("e");

            const double expectedValue = Math.E;

            function(0).Should().Be(expectedValue);
            function(0.1).Should().Be(expectedValue);
            function(-0.1).Should().Be(expectedValue);
        }
示例#16
0
        public void ConstantMultiplication(string expression, double expectedValue)
        {
            Func <double, double> function = InvariantCompiler.CompileFunction(expression);

            const double precision = 10E-6;

            function(0).Should().BeApproximately(expectedValue, precision);
            function(-5).Should().BeApproximately(expectedValue, precision);
            function(5).Should().BeApproximately(expectedValue, precision);
        }
示例#17
0
        public void NegativeExponent()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("-4E-10");

            const double expectedValue = -4E-10;

            function(0).Should().Be(expectedValue);
            function(5).Should().Be(expectedValue);
            function(-5).Should().Be(expectedValue);
        }
示例#18
0
        public void BeginsWithDecimal()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction(".5");

            const double expectedValue = 0.5;

            function(0).Should().Be(expectedValue);
            function(1).Should().Be(expectedValue);
            function(-1).Should().Be(expectedValue);
        }
示例#19
0
        public void PositiveExponent(string value)
        {
            Func <double, double> function = InvariantCompiler.CompileFunction(value);

            const double expectedValue = 1E10;

            function(0).Should().Be(expectedValue);
            function(500).Should().Be(expectedValue);
            function(-500).Should().Be(expectedValue);
        }
示例#20
0
        public void UnaryMinus_Number()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction(" -  5.3 ");

            const double expectedValue = -5.3;

            function(0).Should().Be(expectedValue);
            function(1).Should().Be(expectedValue);
            function(-1).Should().Be(expectedValue);
        }
示例#21
0
        public void OrderOfOperations()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("2^3+5*2-6/3");

            const double expectedValue = 16;

            function(0).Should().Be(expectedValue);
            function(100).Should().Be(expectedValue);
            function(-100).Should().Be(expectedValue);
        }
示例#22
0
        public void Pi()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("pi");

            const double expectedValue = Math.PI;

            function(0).Should().Be(expectedValue);
            function(5).Should().Be(expectedValue);
            function(-5).Should().Be(expectedValue);
        }
示例#23
0
        public void Exponents_RightAssociative()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("4^3^2");

            const double expectedValue = 262144;

            function(0).Should().Be(expectedValue);
            function(1).Should().Be(expectedValue);
            function(-1).Should().Be(expectedValue);
        }
示例#24
0
        public void EndsWithDecimal()
        {
            CompileResult result = InvariantCompiler.Compile("14.");

            result.IsSuccess.Should().BeFalse();

            result.Errors.Should().ContainSingle();
            CompileError error = result.Errors.Single();

            error.Position.Should().Be(2);
            error.Type.Should().Be(ErrorType.InvalidNumber);
        }
示例#25
0
        public void SingleDecimal()
        {
            CompileResult result = InvariantCompiler.Compile(".");

            result.IsSuccess.Should().BeFalse();

            result.Errors.Should().ContainSingle();
            CompileError error = result.Errors.Single();

            error.Position.Should().Be(0);
            error.Type.Should().Be(ErrorType.InvalidTerm);
        }
示例#26
0
        public void MultipleDecimals()
        {
            CompileResult result = InvariantCompiler.Compile("5.7.11");

            result.IsSuccess.Should().BeFalse();

            result.Errors.Should().ContainSingle();
            CompileError error = result.Errors.Single();

            error.Position.Should().Be(3);
            error.Type.Should().Be(ErrorType.InvalidNumber);
        }
示例#27
0
        public void UnknownIdentifier()
        {
            CompileResult result = InvariantCompiler.Compile("a");

            result.IsSuccess.Should().BeFalse();

            result.Errors.Should().ContainSingle();
            CompileError error = result.Errors.Single();

            error.Position.Should().Be(0);
            error.Type.Should().Be(ErrorType.UnknownIdentifier);
        }
示例#28
0
        public void GetDependentFunctions_SimpleGraph()
        {
            var functions = new string[]
            {
                "f(x) = x",
                "g(x) = f(x) + f(x) + 2",
                "h(x) = g(x) / 2"
            };

            CompileResult result = InvariantCompiler.Compile(functions);

            result.IsSuccess.Should().BeTrue();

            string[] dependents = result.GetDependentFunctions("f").ToArray();
            dependents.Should().Contain(new string[] { "f", "g", "h" });
        }
示例#29
0
        public void ImpliedMultiplication_NumberFunction_NoSpace()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("2sin(x)");

            function(0).Should().Be(0);
        }
示例#30
0
        public void FunctionComposition()
        {
            Func <double, double> function = InvariantCompiler.CompileFunction("sin(cos(x))");

            function.Should().NotBeNull();
        }