using GraphTUI.Core.Math; using GraphTUI.Core.Models; using GraphTUI.Rendering; namespace GraphTUI.Tests; public static class UserDefinedFunctionTests { public static void Register(TestRunner runner) { runner.Add("UserFunction: f(x) = x^2 + 5 definition and evaluation", () => { var fn = new GraphFunction(1, "f(x) = x^2 + 5", ColorRgb.Cyan); Assert.Equal(FunctionType.Definition, fn.Type); Assert.True(fn.IsDefinition, "Expected IsDefinition == true"); Assert.False(fn.IsGraphable, "Function definitions should not be graphable directly"); Assert.Equal("f", fn.DefinitionName); Assert.NotNull(fn.DefinitionArgs); Assert.Equal(1, fn.DefinitionArgs!.Length); Assert.Equal("x", fn.DefinitionArgs[0]); Assert.True(fn.CompilationError == null, $"Compilation failed: {fn.CompilationError}"); // Direct evaluation lookup test bool Lookup(string name, double[] args, in EvaluationContext callerCtx, out double result) { result = double.NaN; if (callerCtx.CallDepth >= 64) return true; if (name.Equals("f", StringComparison.OrdinalIgnoreCase) && args.Length == 1) { result = fn.DefinitionBody!.Evaluate(new EvaluationContext(x: args[0], callDepth: callerCtx.CallDepth + 1)); return true; } return false; } // Call inside an Evaluator: y = f(3) var caller = Evaluator.Compile("f(3)"); double val = caller.Evaluate(0, userFunctionLookup: Lookup); Assert.Equal(14.0, val); // 3^2 + 5 = 14 // Call inside formula with variable x: y = f(x) + 1 at x = 2 var caller2 = Evaluator.Compile("f(x) + 1"); double val2 = caller2.Evaluate(2, userFunctionLookup: Lookup); Assert.Equal(10.0, val2); // (2^2 + 5) + 1 = 10 }); runner.Add("UserFunction: Multi-argument definitions g(x, y) = x^2 + y^2", () => { var fn = new GraphFunction(2, "g(x, y) = x^2 + y^2", ColorRgb.Yellow); Assert.Equal(FunctionType.Definition, fn.Type); Assert.Equal("g", fn.DefinitionName); Assert.NotNull(fn.DefinitionArgs); Assert.Equal(2, fn.DefinitionArgs!.Length); Assert.Equal("x", fn.DefinitionArgs[0]); Assert.Equal("y", fn.DefinitionArgs[1]); bool Lookup(string name, double[] args, in EvaluationContext callerCtx, out double result) { result = double.NaN; if (callerCtx.CallDepth >= 64) return true; if (name.Equals("g", StringComparison.OrdinalIgnoreCase) && args.Length == 2) { result = fn.DefinitionBody!.Evaluate(new EvaluationContext(x: args[0], y: args[1], callDepth: callerCtx.CallDepth + 1)); return true; } return false; } var eval = Evaluator.Compile("g(3, 4)"); double res = eval.Evaluate(0, userFunctionLookup: Lookup); Assert.Equal(25.0, res); // 3^2 + 4^2 = 25 }); runner.Add("UserFunction: Nested user-defined functions and calculus", () => { var f = new GraphFunction(1, "f(x) = x^2 + 1", ColorRgb.Cyan); var g = new GraphFunction(2, "g(x) = 2 * f(x)", ColorRgb.Yellow); bool Lookup(string name, double[] args, in EvaluationContext callerCtx, out double result) { result = double.NaN; if (callerCtx.CallDepth >= 64) return true; if (name.Equals("f", StringComparison.OrdinalIgnoreCase) && args.Length == 1) { result = f.DefinitionBody!.Evaluate(new EvaluationContext(x: args[0], userFunctionLookup: Lookup, callDepth: callerCtx.CallDepth + 1)); return true; } if (name.Equals("g", StringComparison.OrdinalIgnoreCase) && args.Length == 1) { result = g.DefinitionBody!.Evaluate(new EvaluationContext(x: args[0], userFunctionLookup: Lookup, callDepth: callerCtx.CallDepth + 1)); return true; } return false; } var eval = Evaluator.Compile("g(3)"); double res = eval.Evaluate(0, userFunctionLookup: Lookup); Assert.Equal(20.0, res); // 2 * (3^2 + 1) = 20 // Calculus derivative on f(x) double deriv = Calculus.Derivative(t => { var dummyCtx = new EvaluationContext(); Lookup("f", [t], in dummyCtx, out double r); return r; }, 3.0); Assert.Within(6.0, deriv, 1e-3); // d/dx[x^2 + 1] at x=3 is 6 }); runner.Add("UserFunction: Recursion limit prevents stack overflow", () => { // rec(x) = rec(x) + 1 var recFn = new GraphFunction(1, "rec(x) = rec(x) + 1", ColorRgb.Red); bool Lookup(string name, double[] args, in EvaluationContext callerCtx, out double result) { result = double.NaN; if (callerCtx.CallDepth >= 64) return true; if (name.Equals("rec", StringComparison.OrdinalIgnoreCase) && args.Length == 1) { result = recFn.DefinitionBody!.Evaluate(new EvaluationContext(x: args[0], userFunctionLookup: Lookup, callDepth: callerCtx.CallDepth + 1)); return true; } return false; } var eval = Evaluator.Compile("rec(1)"); double val = eval.Evaluate(0, userFunctionLookup: Lookup); Assert.True(double.IsNaN(val), "Infinite recursion must terminate with NaN"); }); } }