This commit is contained in:
tyuXX 2026-10-24 18:40:19 +03:00
parent d70b0390b7
commit ee25655c8d
22 changed files with 2126 additions and 53 deletions

View file

@ -0,0 +1,215 @@
using GraphTUI.Core.Math;
using GraphTUI.Core.Models;
using GraphTUI.Rendering;
using System.Diagnostics;
namespace GraphTUI.Tests;
public static class FactorialVariantsTests
{
public static void Register(TestRunner runner)
{
runner.Add("Factorials: x! postfix and fact(x)", () =>
{
// 0! = 1
var eval0 = Evaluator.Compile("0!");
Assert.Equal(1.0, eval0.Evaluate(0));
// 5! = 120
var eval5 = Evaluator.Compile("5!");
Assert.Equal(120.0, eval5.Evaluate(0));
// Variable x!
var evalX = Evaluator.Compile("x!");
Assert.Equal(6.0, evalX.Evaluate(3.0));
Assert.Equal(24.0, evalX.Evaluate(4.0));
Assert.Equal(120.0, evalX.Evaluate(5.0));
// Parenthesized (x + 1)!
var evalParen = Evaluator.Compile("(x + 1)!");
Assert.Equal(24.0, evalParen.Evaluate(3.0)); // (3+1)! = 4! = 24
// fact(x) equivalence
var evalFunc = Evaluator.Compile("fact(4)");
Assert.Equal(24.0, evalFunc.Evaluate(0));
// Implicit multiplication with factorial: 2x! at x=3 -> 2 * 6 = 12
var evalImplicit = Evaluator.Compile("2x!");
Assert.Equal(12.0, evalImplicit.Evaluate(3.0));
// Unary minus: -3! -> -(3!) = -6
var evalUnary = Evaluator.Compile("-3!");
Assert.Equal(-6.0, evalUnary.Evaluate(0));
});
runner.Add("Double Factorials: x!! postfix and dfact(x)", () =>
{
// 0!! = 1, (-1)!! = 1
var eval0 = Evaluator.Compile("0!!");
Assert.Equal(1.0, eval0.Evaluate(0));
// 5!! = 5 * 3 * 1 = 15
var eval5 = Evaluator.Compile("5!!");
Assert.Equal(15.0, eval5.Evaluate(0));
// 6!! = 6 * 4 * 2 = 48
var eval6 = Evaluator.Compile("6!!");
Assert.Equal(48.0, eval6.Evaluate(0));
// 7!! = 7 * 5 * 3 * 1 = 105
var eval7 = Evaluator.Compile("7!!");
Assert.Equal(105.0, eval7.Evaluate(0));
// Variable x!!
var evalX = Evaluator.Compile("x!!");
Assert.Equal(15.0, evalX.Evaluate(5.0));
Assert.Equal(48.0, evalX.Evaluate(6.0));
// dfact(x) function syntax
var evalFunc = Evaluator.Compile("dfact(5)");
Assert.Equal(15.0, evalFunc.Evaluate(0));
// (x + 1)!!
var evalParen = Evaluator.Compile("(x + 1)!!");
Assert.Equal(15.0, evalParen.Evaluate(4.0)); // (4+1)!! = 5!! = 15
});
runner.Add("Super Factorials: x$ postfix and sfact(x)", () =>
{
// 1$ = 1! = 1
var eval1 = Evaluator.Compile("1$");
Assert.Equal(1.0, eval1.Evaluate(0));
// 2$ = 1! * 2! = 2
var eval2 = Evaluator.Compile("2$");
Assert.Equal(2.0, eval2.Evaluate(0));
// 3$ = 1! * 2! * 3! = 1 * 2 * 6 = 12
var eval3 = Evaluator.Compile("3$");
Assert.Equal(12.0, eval3.Evaluate(0));
// 4$ = 1! * 2! * 3! * 4! = 12 * 24 = 288
var eval4 = Evaluator.Compile("4$");
Assert.Equal(288.0, eval4.Evaluate(0));
// Variable x$
var evalX = Evaluator.Compile("x$");
Assert.Equal(12.0, evalX.Evaluate(3.0));
Assert.Equal(288.0, evalX.Evaluate(4.0));
// sfact(x) function syntax
var evalFunc = Evaluator.Compile("sfact(4)");
Assert.Equal(288.0, evalFunc.Evaluate(0));
});
runner.Add("Compound expressions with !, !!, and $", () =>
{
// 3! + 4!! = 6 + 8 = 14
var eval1 = Evaluator.Compile("3! + 4!!");
Assert.Equal(14.0, eval1.Evaluate(0));
// (3!)! = 6! = 720
var eval2 = Evaluator.Compile("(3!)!");
Assert.Equal(720.0, eval2.Evaluate(0));
// Taylor series using postfix x! in sum: sum(n=0, 4, x^n / n!)
// For x=1: 1 + 1 + 1/2 + 1/6 + 1/24 = 65/24 ~ 2.708333
var evalTaylor = Evaluator.Compile("sum(n=0, 4, x^n / n!)");
Assert.Within(65.0 / 24.0, evalTaylor.Evaluate(1.0), 1e-4);
});
runner.Add("Factorials: Edge cases, lookup tables, and limits", () =>
{
// 20! = 2,432,902,008,176,640,000
var eval20 = Evaluator.Compile("20!");
Assert.Equal(2432902008176640000.0, eval20.Evaluate(0));
// 170! is the maximum finite IEEE double factorial (~7.2574e306)
var eval170 = Evaluator.Compile("170!");
double f170 = eval170.Evaluate(0);
Assert.True(!double.IsInfinity(f170) && f170 > 1e306, "170! should be finite and > 1e306");
// 171! overflows double to infinity
var eval171 = Evaluator.Compile("171!");
Assert.True(double.IsPositiveInfinity(eval171.Evaluate(0)), "171! should be PositiveInfinity");
// 300!! is finite
var eval300 = Evaluator.Compile("300!!");
double df300 = eval300.Evaluate(0);
Assert.True(!double.IsInfinity(df300) && df300 > 1e300, "300!! should be finite");
// 302!! overflows double
var eval302 = Evaluator.Compile("302!!");
Assert.True(double.IsPositiveInfinity(eval302.Evaluate(0)), "302!! should be PositiveInfinity");
// Negative integer poles return NaN
var evalNegInt = Evaluator.Compile("(-1)!");
Assert.True(double.IsNaN(evalNegInt.Evaluate(0)), "(-1)! should be NaN");
var evalNegInt2 = Evaluator.Compile("(-2)!");
Assert.True(double.IsNaN(evalNegInt2.Evaluate(0)), "(-2)! should be NaN");
// Non-integer factorial: (-0.5)! = Gamma(0.5) = sqrt(pi) ~ 1.77245385
var evalHalf = Evaluator.Compile("(-0.5)!");
Assert.Within(System.Math.Sqrt(System.Math.PI), evalHalf.Evaluate(0), 1e-5);
});
runner.Add("Factorials: Performance stress test (zero lag on x!, x!!, x$)", () =>
{
var canvas = new BrailleCanvas(80, 25); // 160 x 100 subpixels
var viewport = new Viewport
{
XMin = -10.0,
XMax = 10.0,
YMin = -5.0,
YMax = 5.0
};
// 1. Rendering y = x! over viewport: must complete in < 50ms (previously took seconds or froze)
var fnFact = new GraphFunction(1, "y = x!", ColorRgb.Cyan);
var sw = Stopwatch.StartNew();
for (int frame = 0; frame < 5; frame++)
{
PlotRenderer.Render(canvas, viewport, [fnFact], _ => 0);
}
sw.Stop();
long elapsedFact = sw.ElapsedMilliseconds;
Assert.True(elapsedFact < 200, $"5 frames of y = x! took {elapsedFact}ms (expected < 200ms)");
// 2. Rendering y = x!! over viewport
var fnDfact = new GraphFunction(2, "y = x!!", ColorRgb.Yellow);
sw.Restart();
for (int frame = 0; frame < 5; frame++)
{
PlotRenderer.Render(canvas, viewport, [fnDfact], _ => 0);
}
sw.Stop();
long elapsedDfact = sw.ElapsedMilliseconds;
Assert.True(elapsedDfact < 200, $"5 frames of y = x!! took {elapsedDfact}ms (expected < 200ms)");
// 3. Rendering y = x$ over viewport
var fnSfact = new GraphFunction(3, "y = x$", ColorRgb.Green);
sw.Restart();
for (int frame = 0; frame < 5; frame++)
{
PlotRenderer.Render(canvas, viewport, [fnSfact], _ => 0);
}
sw.Stop();
long elapsedSfact = sw.ElapsedMilliseconds;
Assert.True(elapsedSfact < 200, $"5 frames of y = x$ took {elapsedSfact}ms (expected < 200ms)");
// 4. Batch mathematical evaluation of 100,000 points
var eval = Evaluator.Compile("x!");
sw.Restart();
double sum = 0.0;
for (int i = 0; i < 100_000; i++)
{
sum += eval.Evaluate(i % 172);
}
sw.Stop();
long elapsed100k = sw.ElapsedMilliseconds;
Assert.True(elapsed100k < 100, $"100,000 factorial evaluations took {elapsed100k}ms (expected < 100ms)");
Assert.True(!double.IsNaN(sum), "Sum of evaluations should not be NaN");
});
}
}