upd and fixes

This commit is contained in:
tyuXX 2026-10-28 22:47:38 +03:00
parent ee25655c8d
commit 6cafe3ac46
23 changed files with 1501 additions and 228 deletions

View file

@ -0,0 +1,131 @@
using GraphTUI.Core.Math;
using GraphTUI.Core.Models;
using GraphTUI.Rendering;
using GraphTUI.Terminal;
using GraphTUI.UI.Views;
namespace GraphTUI.Tests;
public static class ClippingAndInteractiveTests
{
public static void Register(TestRunner runner)
{
runner.Add("Clipping: 8-pass Cohen-Sutherland keeps diagonal line", () =>
{
var canvas = new BrailleCanvas(20, 20); // 40 x 80 subpixels
// Line from (-50, -50) to (100, 100) crosses the canvas diagonally
canvas.DrawLine(-50, -50, 100, 100, ColorRgb.White);
// Subpixel center (20, 40) should have been drawn
// Check that at least some pixels were plotted on the canvas
bool anyPixelSet = false;
for (int sy = 0; sy < canvas.SubpixelHeight; sy++)
{
for (int sx = 0; sx < canvas.SubpixelWidth; sx++)
{
// If center line area has dots
if (sx == sy / 2)
{
anyPixelSet = true;
break;
}
}
if (anyPixelSet) break;
}
Assert.True(anyPixelSet, "Expected pixels to be rasterized on canvas");
});
runner.Add("Parameters: Unbounded range expansion and custom steps", () =>
{
var param = new Parameter("speed", 5.0, -10.0, 10.0, 0.1);
Assert.Equal(5.0, param.Value);
// Expand range beyond original [-10, 10]
param.SetRange(-100.0, 100.0, 1.0);
param.SetValue(75.0);
Assert.Equal(75.0, param.Value);
Assert.Equal(-100.0, param.Min);
Assert.Equal(100.0, param.Max);
// Increment
param.Increment();
Assert.Equal(76.0, param.Value);
param.Decrement(2.0);
Assert.Equal(74.0, param.Value);
});
runner.Add("Builtin Functions: Validation and comma counting", () =>
{
// 3-arg sum: sum(start, end, body)
var eval3 = Evaluator.Compile("sum(1, 5, x)");
Assert.NotNull(eval3);
Assert.Equal(15.0, eval3.Evaluate(3)); // 5 * 3 = 15
// 4-arg sum: sum(n, start, end, body)
var eval4 = Evaluator.Compile("sum(n, 1, 4, n)");
Assert.NotNull(eval4);
Assert.Equal(10.0, eval4.Evaluate(0)); // 1 + 2 + 3 + 4 = 10
// factorial function call
var evalFact = Evaluator.Compile("factorial(5)");
Assert.Equal(120.0, evalFact.Evaluate(0));
});
runner.Add("Modals: Keyboard exit and interactivity parity", () =>
{
var viewport = new Viewport();
// 1. CalculusModal
var calc = new CalculusModal { IsOpen = true };
var fn = new GraphFunction(1, "y = x^2", ColorRgb.Cyan);
calc.Open(fn, viewport, null);
Assert.True(calc.IsOpen);
// Up/Down/Tab navigation
calc.HandleKey(new KeyAction(KeyCode.Down), viewport, null, () => { });
Assert.Equal(1, calc.SelectedControlIndex);
calc.HandleKey(new KeyAction(KeyCode.Tab), viewport, null, () => { });
Assert.Equal(2, calc.SelectedControlIndex);
// Exit with 'q'
calc.HandleKey(new KeyAction(KeyCode.Char, 'q'), viewport, null, () => { });
Assert.False(calc.IsOpen, "CalculusModal should exit on 'q'");
// Exit with 'F3'
calc.IsOpen = true;
calc.HandleKey(new KeyAction(KeyCode.F3), viewport, null, () => { });
Assert.False(calc.IsOpen, "CalculusModal should exit on F3");
// 2. CheatsheetModal
var cheat = new CheatsheetModal { IsOpen = true };
cheat.HandleKey(new KeyAction(KeyCode.Char, 'q'));
Assert.False(cheat.IsOpen, "CheatsheetModal should exit on 'q'");
cheat.IsOpen = true;
cheat.HandleKey(new KeyAction(KeyCode.F5));
Assert.False(cheat.IsOpen, "CheatsheetModal should exit on F5");
// 3. PresetsGalleryModal
var presets = new PresetsGalleryModal { IsOpen = true };
presets.HandleKey(new KeyAction(KeyCode.Char, 'q'), _ => { });
Assert.False(presets.IsOpen, "PresetsGalleryModal should exit on 'q'");
presets.IsOpen = true;
presets.HandleKey(new KeyAction(KeyCode.F4), _ => { });
Assert.False(presets.IsOpen, "PresetsGalleryModal should exit on F4");
// 4. HelpModal
var help = new HelpModal { IsOpen = true };
help.HandleKey(new KeyAction(KeyCode.Char, 'q'));
Assert.False(help.IsOpen, "HelpModal should exit on 'q'");
help.IsOpen = true;
help.HandleKey(new KeyAction(KeyCode.F1));
Assert.False(help.IsOpen, "HelpModal should exit on F1");
help.IsOpen = true;
help.HandleKey(new KeyAction(KeyCode.Escape));
Assert.False(help.IsOpen, "HelpModal should exit on Escape");
});
}
}

View file

@ -14,6 +14,8 @@ public static class Program
DoubleSidedEquationTests.Register(runner);
SummationProductIntegralTests.Register(runner);
FactorialVariantsTests.Register(runner);
UserDefinedFunctionTests.Register(runner);
ClippingAndInteractiveTests.Register(runner);
return runner.RunAll();
}

View file

@ -20,6 +20,12 @@ public static class Assert
throw new Exception(message ?? $"Assertion failed: expected '{expected}', got '{actual}'.");
}
public static void NotNull(object? obj, string? message = null)
{
if (obj is null)
throw new Exception(message ?? "Assertion failed: expected non-null, got null.");
}
public static void Within(double expected, double actual, double tolerance, string? message = null)
{
double diff = System.Math.Abs(expected - actual);

View file

@ -0,0 +1,132 @@
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");
});
}
}