upd
This commit is contained in:
parent
d70b0390b7
commit
ee25655c8d
22 changed files with 2126 additions and 53 deletions
128
tests/GraphTUI.Tests/DoubleSidedEquationTests.cs
Normal file
128
tests/GraphTUI.Tests/DoubleSidedEquationTests.cs
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
using GraphTUI.Core.Models;
|
||||
using GraphTUI.Rendering;
|
||||
|
||||
namespace GraphTUI.Tests;
|
||||
|
||||
public static class DoubleSidedEquationTests
|
||||
{
|
||||
public static void Register(TestRunner runner)
|
||||
{
|
||||
runner.Add("DoubleSided: y = f(x) definitions", () =>
|
||||
{
|
||||
var fn1 = new GraphFunction(1, "y = sin(x)", ColorRgb.Cyan);
|
||||
Assert.Equal(FunctionType.Cartesian, fn1.Type);
|
||||
Assert.True(fn1.CompilationError == null, $"Unexpected error: {fn1.CompilationError}");
|
||||
Assert.Equal(0.0, fn1.Evaluator!.Evaluate(0));
|
||||
Assert.Within(1.0, fn1.Evaluator.Evaluate(System.Math.PI / 2), 1e-6);
|
||||
|
||||
// Inverted order: sin(x) = y
|
||||
var fn2 = new GraphFunction(2, "sin(x) = y", ColorRgb.Yellow);
|
||||
Assert.Equal(FunctionType.Cartesian, fn2.Type);
|
||||
Assert.True(fn2.CompilationError == null);
|
||||
Assert.Within(1.0, fn2.Evaluator!.Evaluate(System.Math.PI / 2), 1e-6);
|
||||
|
||||
// Plain expression sin(x) defaults to y = sin(x)
|
||||
var fn3 = new GraphFunction(3, "sin(x)", ColorRgb.Green);
|
||||
Assert.Equal(FunctionType.Cartesian, fn3.Type);
|
||||
Assert.True(fn3.CompilationError == null);
|
||||
});
|
||||
|
||||
runner.Add("DoubleSided: f(y) = x and x = f(y) definitions", () =>
|
||||
{
|
||||
// x = y^2
|
||||
var fn1 = new GraphFunction(1, "x = y^2", ColorRgb.Cyan);
|
||||
Assert.Equal(FunctionType.CartesianX, fn1.Type);
|
||||
Assert.True(fn1.CompilationError == null, $"Unexpected error: {fn1.CompilationError}");
|
||||
Assert.Equal(9.0, fn1.Evaluator!.EvaluateY(3));
|
||||
Assert.Equal(9.0, fn1.Evaluator.EvaluateY(-3));
|
||||
|
||||
// f(y) = x: y^2 = x
|
||||
var fn2 = new GraphFunction(2, "y^2 = x", ColorRgb.Yellow);
|
||||
Assert.Equal(FunctionType.CartesianX, fn2.Type);
|
||||
Assert.True(fn2.CompilationError == null);
|
||||
Assert.Equal(16.0, fn2.Evaluator!.EvaluateY(4));
|
||||
Assert.Equal(16.0, fn2.Evaluator.EvaluateY(-4));
|
||||
|
||||
// sin(y) = x
|
||||
var fn3 = new GraphFunction(3, "sin(y) = x", ColorRgb.Magenta);
|
||||
Assert.Equal(FunctionType.CartesianX, fn3.Type);
|
||||
Assert.True(fn3.CompilationError == null);
|
||||
Assert.Within(1.0, fn3.Evaluator!.EvaluateY(System.Math.PI / 2), 1e-6);
|
||||
|
||||
// y^3 + 2y = x
|
||||
var fn4 = new GraphFunction(4, "y^3 + 2y = x", ColorRgb.Orange);
|
||||
Assert.Equal(FunctionType.CartesianX, fn4.Type);
|
||||
Assert.True(fn4.CompilationError == null);
|
||||
Assert.Equal(12.0, fn4.Evaluator!.EvaluateY(2)); // 2^3 + 2*2 = 8 + 4 = 12
|
||||
|
||||
// Plain expression with y and no x: y^2 defaults to x = y^2
|
||||
var fn5 = new GraphFunction(5, "y^2", ColorRgb.White);
|
||||
Assert.Equal(FunctionType.CartesianX, fn5.Type);
|
||||
Assert.True(fn5.CompilationError == null);
|
||||
Assert.Equal(4.0, fn5.Evaluator!.EvaluateY(2));
|
||||
});
|
||||
|
||||
runner.Add("DoubleSided: f(y) = g(x) and implicit definitions", () =>
|
||||
{
|
||||
// y^2 = sin(x)
|
||||
var fn1 = new GraphFunction(1, "y^2 = sin(x)", ColorRgb.Cyan);
|
||||
Assert.Equal(FunctionType.Implicit, fn1.Type);
|
||||
Assert.True(fn1.CompilationError == null, $"Unexpected error: {fn1.CompilationError}");
|
||||
Assert.True(fn1.EvaluatorLhs != null && fn1.EvaluatorRhs != null);
|
||||
Assert.Equal(4.0, fn1.EvaluatorLhs!.EvaluateY(2));
|
||||
Assert.Within(1.0, fn1.EvaluatorRhs!.Evaluate(System.Math.PI / 2), 1e-6);
|
||||
|
||||
// sin(y) = cos(x)
|
||||
var fn2 = new GraphFunction(2, "sin(y) = cos(x)", ColorRgb.Yellow);
|
||||
Assert.Equal(FunctionType.Implicit, fn2.Type);
|
||||
Assert.True(fn2.CompilationError == null);
|
||||
|
||||
// Elliptic curve: y^2 = x^3 - 3x
|
||||
var fn3 = new GraphFunction(3, "y^2 = x^3 - 3x", ColorRgb.Green);
|
||||
Assert.Equal(FunctionType.Implicit, fn3.Type);
|
||||
Assert.True(fn3.CompilationError == null);
|
||||
|
||||
// Circle: x^2 + y^2 = 25
|
||||
var fn4 = new GraphFunction(4, "x^2 + y^2 = 25", ColorRgb.Magenta);
|
||||
Assert.Equal(FunctionType.Implicit, fn4.Type);
|
||||
Assert.True(fn4.CompilationError == null);
|
||||
});
|
||||
|
||||
runner.Add("DoubleSided: Rendering f(y) = x and f(y) = g(x) on Braille Canvas", () =>
|
||||
{
|
||||
var canvas = new BrailleCanvas(40, 20);
|
||||
var viewport = new Viewport { XMin = -10, XMax = 10, YMin = -10, YMax = 10 };
|
||||
|
||||
// 1. Test f(y) = x: y^2 = x
|
||||
var fnX = new GraphFunction(1, "y^2 = x", ColorRgb.Cyan);
|
||||
PlotRenderer.Render(canvas, viewport, [fnX], _ => 0);
|
||||
|
||||
// Count non-empty Braille cells
|
||||
int cellsWithDots = 0;
|
||||
for (int r = 0; r < canvas.CellHeight; r++)
|
||||
{
|
||||
for (int c = 0; c < canvas.CellWidth; c++)
|
||||
{
|
||||
var (ch, _, _) = canvas.GetCell(c, r);
|
||||
if (ch != ' ') cellsWithDots++;
|
||||
}
|
||||
}
|
||||
Assert.True(cellsWithDots > 5, $"Expected plotted dots for y^2=x, got {cellsWithDots}");
|
||||
|
||||
// 2. Test f(y) = g(x): y^2 = sin(x)
|
||||
var fnImplicit = new GraphFunction(2, "y^2 = sin(x)", ColorRgb.Yellow);
|
||||
PlotRenderer.Render(canvas, viewport, [fnImplicit], _ => 0);
|
||||
|
||||
int implicitDots = 0;
|
||||
for (int r = 0; r < canvas.CellHeight; r++)
|
||||
{
|
||||
for (int c = 0; c < canvas.CellWidth; c++)
|
||||
{
|
||||
var (ch, _, _) = canvas.GetCell(c, r);
|
||||
if (ch != ' ') implicitDots++;
|
||||
}
|
||||
}
|
||||
Assert.True(implicitDots > 5, $"Expected plotted dots for y^2=sin(x), got {implicitDots}");
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue