GraphTUI/tests/GraphTUI.Tests/EvaluatorTests.cs

64 lines
2.3 KiB
C#

using GraphTUI.Core.Math;
namespace GraphTUI.Tests;
public static class EvaluatorTests
{
public static void Register(TestRunner runner)
{
runner.Add("Evaluator: Standard Trig and Identity sin^2(x) + cos^2(x) = 1", () =>
{
var sinEval = Evaluator.Compile("sin(x)");
var cosEval = Evaluator.Compile("cos(x)");
for (double x = -10.0; x <= 10.0; x += 0.5)
{
double s = sinEval.Evaluate(x);
double c = cosEval.Evaluate(x);
Assert.Within(System.Math.Sin(x), s, 1e-9);
Assert.Within(System.Math.Cos(x), c, 1e-9);
Assert.Within(1.0, s * s + c * c, 1e-9);
}
});
runner.Add("Evaluator: Hyperbolic and Exponential Functions", () =>
{
var expEval = Evaluator.Compile("exp(x)");
var lnEval = Evaluator.Compile("ln(x)");
var sinhEval = Evaluator.Compile("sinh(x)");
Assert.Within(System.Math.E, expEval.Evaluate(1.0), 1e-9);
Assert.Within(1.0, lnEval.Evaluate(System.Math.E), 1e-9);
Assert.Within(System.Math.Sinh(2.5), sinhEval.Evaluate(2.5), 1e-9);
});
runner.Add("Evaluator: Dynamic Parameters a, b, c", () =>
{
var eval = Evaluator.Compile("a*x^2 + b*x + c");
var lookup = new Dictionary<string, double>
{
["a"] = 2.0,
["b"] = 3.0,
["c"] = 1.0
};
// For x = 4: 2*(16) + 3*(4) + 1 = 32 + 12 + 1 = 45
double result = eval.Evaluate(4.0, name => lookup[name]);
Assert.Equal(45.0, result);
});
runner.Add("Evaluator: Parametric and Polar Evaluation", () =>
{
var paramX = Evaluator.Compile("cos(t)");
var paramY = Evaluator.Compile("sin(t)");
double t = System.Math.PI / 4.0;
Assert.Within(System.Math.Cos(t), paramX.EvaluateParametricX(t), 1e-9);
Assert.Within(System.Math.Sin(t), paramY.EvaluateParametricY(t), 1e-9);
var polar = Evaluator.Compile("1 + cos(theta)");
double th = System.Math.PI / 2.0;
Assert.Within(1.0, polar.EvaluatePolar(th), 1e-9);
});
}
}