134 lines
4.1 KiB
C#
134 lines
4.1 KiB
C#
using CeresSharp;
|
|
|
|
namespace CeresSharp.Test;
|
|
|
|
[TestFixture]
|
|
public class InterpolatorTests
|
|
{
|
|
[Test]
|
|
public void CubicInterpolator_ShouldCreate()
|
|
{
|
|
var data = new double[] { 0.0, 1.0, 4.0, 9.0, 16.0 }; // x^2 values
|
|
using var interpolator = new CubicInterpolator(data);
|
|
|
|
Assert.That(interpolator, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void CubicInterpolator_Evaluate_ShouldReturnValue()
|
|
{
|
|
var data = new double[] { 0.0, 1.0, 4.0, 9.0, 16.0 }; // x^2 values
|
|
using var interpolator = new CubicInterpolator(data);
|
|
|
|
interpolator.Evaluate(x: 2.5, out double value, out double? gradient);
|
|
|
|
Assert.That(value, Is.GreaterThan(0.0));
|
|
Assert.That(gradient, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void CubicInterpolator_Evaluate_WithoutGradient_ShouldWork()
|
|
{
|
|
var data = new double[] { 0.0, 1.0, 4.0, 9.0, 16.0 };
|
|
using var interpolator = new CubicInterpolator(data);
|
|
|
|
interpolator.Evaluate(x: 2.0, out double value, out double? gradient);
|
|
|
|
// At x=2, should be close to 4.0
|
|
Assert.That(Math.Abs(value - 4.0), Is.LessThan(1.0));
|
|
}
|
|
|
|
[Test]
|
|
public void BiCubicInterpolator_ShouldCreate()
|
|
{
|
|
// 3x3 grid: values from 0 to 8
|
|
var data = new double[]
|
|
{
|
|
0.0, 1.0, 2.0,
|
|
3.0, 4.0, 5.0,
|
|
6.0, 7.0, 8.0
|
|
};
|
|
using var interpolator = new BiCubicInterpolator(data, rows: 3, cols: 3);
|
|
|
|
Assert.That(interpolator, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void BiCubicInterpolator_Evaluate_ShouldReturnValue()
|
|
{
|
|
var data = new double[]
|
|
{
|
|
0.0, 1.0, 2.0,
|
|
3.0, 4.0, 5.0,
|
|
6.0, 7.0, 8.0
|
|
};
|
|
using var interpolator = new BiCubicInterpolator(data, rows: 3, cols: 3);
|
|
|
|
interpolator.Evaluate(x: 1.0, y: 1.0, out double value,
|
|
out double? gradientX, out double? gradientY);
|
|
|
|
Assert.That(value, Is.GreaterThanOrEqualTo(0.0));
|
|
Assert.That(gradientX, Is.Not.Null);
|
|
Assert.That(gradientY, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void BiCubicInterpolator_Evaluate_AtGridPoint_ShouldMatch()
|
|
{
|
|
var data = new double[]
|
|
{
|
|
0.0, 1.0, 2.0,
|
|
3.0, 4.0, 5.0,
|
|
6.0, 7.0, 8.0
|
|
};
|
|
using var interpolator = new BiCubicInterpolator(data, rows: 3, cols: 3);
|
|
|
|
// Evaluate at grid point (1, 1) which should be 4.0
|
|
interpolator.Evaluate(x: 1.0, y: 1.0, out double value,
|
|
out double? gradientX, out double? gradientY);
|
|
|
|
Assert.That(Math.Abs(value - 4.0), Is.LessThan(0.1));
|
|
}
|
|
|
|
[Test]
|
|
public void BiCubicInterpolator_Evaluate_Interpolated_ShouldBeSmooth()
|
|
{
|
|
var data = new double[]
|
|
{
|
|
0.0, 1.0, 2.0,
|
|
3.0, 4.0, 5.0,
|
|
6.0, 7.0, 8.0
|
|
};
|
|
using var interpolator = new BiCubicInterpolator(data, rows: 3, cols: 3);
|
|
|
|
// Evaluate at interpolated point (0.5, 0.5)
|
|
interpolator.Evaluate(x: 0.5, y: 0.5, out double value1,
|
|
out double? gradientX1, out double? gradientY1);
|
|
|
|
// Evaluate at nearby point (0.6, 0.6)
|
|
interpolator.Evaluate(x: 0.6, y: 0.6, out double value2,
|
|
out double? gradientX2, out double? gradientY2);
|
|
|
|
// Values should be close (smooth interpolation)
|
|
Assert.That(Math.Abs(value1 - value2), Is.LessThan(1.0));
|
|
}
|
|
|
|
[Test]
|
|
public void BiCubicInterpolator_LargeGrid_ShouldWork()
|
|
{
|
|
// 5x5 grid
|
|
var data = new double[25];
|
|
for (int i = 0; i < 25; i++)
|
|
{
|
|
data[i] = i;
|
|
}
|
|
|
|
using var interpolator = new BiCubicInterpolator(data, rows: 5, cols: 5);
|
|
|
|
interpolator.Evaluate(x: 2.5, y: 2.5, out double value,
|
|
out double? gradientX, out double? gradientY);
|
|
|
|
Assert.That(value, Is.GreaterThanOrEqualTo(0.0));
|
|
}
|
|
}
|