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)); }
public void EmptyFunctionCallWithNegation_ParseErrors() { CompileResult result = InvariantCompiler.Compile("tan(-)"); result.IsSuccess.Should().BeFalse(); result.Errors.Should().ContainSingle(e => e.Type == ErrorType.InvalidTerm); }
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); }
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); }
public void UnaryOperatorPrecedence_Power() { Func <double, double> function = InvariantCompiler.CompileFunction("-2^x"); double expectedValue = -1 * Math.Pow(2, 10); function(10).Should().Be(expectedValue); }
public void FunctionName_InvalidChars() { CompileResult result = InvariantCompiler.Compile("a!= x"); result.IsSuccess.Should().BeFalse(); result.Errors.Should().ContainSingle(e => e.Type == ErrorType.InvalidFunctionName); }
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); }
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); }
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); }
public void UnaryOperatorPrecedence_Addition() { Func <double, double> function = InvariantCompiler.CompileFunction("-x+4+2"); double expectedValue = -10 + 4 + 2; function(10).Should().Be(expectedValue); }
public void UnaryOperatorPrecedence_Multiplication() { Func <double, double> function = InvariantCompiler.CompileFunction("-x*4+2"); double expectedValue = -2 * 4 + 2; function(2).Should().Be(expectedValue); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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" }); }
public void ImpliedMultiplication_NumberFunction_NoSpace() { Func <double, double> function = InvariantCompiler.CompileFunction("2sin(x)"); function(0).Should().Be(0); }
public void FunctionComposition() { Func <double, double> function = InvariantCompiler.CompileFunction("sin(cos(x))"); function.Should().NotBeNull(); }