Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
112 lines
4.8 KiB
C#
112 lines
4.8 KiB
C#
using Titano.Testing;
|
|
using Titano.Ui.Visuals;
|
|
|
|
namespace Titano.App.Tests.Visuals;
|
|
|
|
public sealed class CurveMathTests
|
|
{
|
|
private const double Gap = CurveMath.DefaultMinGap;
|
|
|
|
[Test]
|
|
public void DraggedPointCannotPassItsNeighbours()
|
|
{
|
|
double[] xs = [0, 0.3, 0.6, 1];
|
|
Assert.Approximately(0.6 - Gap, CurveMath.ConstrainX(xs, 1, 0.9, Gap, pinEnds: true), 1e-12);
|
|
Assert.Approximately(0 + Gap, CurveMath.ConstrainX(xs, 1, -0.4, Gap, pinEnds: true), 1e-12);
|
|
Assert.Approximately(0.45, CurveMath.ConstrainX(xs, 1, 0.45, Gap, pinEnds: true), 1e-12);
|
|
}
|
|
|
|
[Test]
|
|
public void PinnedEndsStayAtZeroAndOne()
|
|
{
|
|
double[] xs = [0, 0.5, 1];
|
|
Assert.Approximately(0, CurveMath.ConstrainX(xs, 0, 0.2, Gap, pinEnds: true), 0);
|
|
Assert.Approximately(1, CurveMath.ConstrainX(xs, 2, 0.7, Gap, pinEnds: true), 0);
|
|
}
|
|
|
|
[Test]
|
|
public void FreeEndsMoveInsideTheUnitInterval()
|
|
{
|
|
double[] xs = [0.1, 0.5, 0.9];
|
|
Assert.Approximately(0, CurveMath.ConstrainX(xs, 0, -3, Gap, pinEnds: false), 0);
|
|
Assert.Approximately(0.5 - Gap, CurveMath.ConstrainX(xs, 0, 0.8, Gap, pinEnds: false), 1e-12);
|
|
Assert.Approximately(1, CurveMath.ConstrainX(xs, 2, 4, Gap, pinEnds: false), 0);
|
|
}
|
|
|
|
[Test]
|
|
public void AnySequenceOfDragsKeepsXStrictlyIncreasing()
|
|
{
|
|
var random = new Random(11);
|
|
double[] xs = [0, 0.2, 0.4, 0.6, 0.8, 1];
|
|
for (var step = 0; step < 10_000; step++)
|
|
{
|
|
var i = random.Next(xs.Length);
|
|
xs[i] = CurveMath.ConstrainX(xs, i, random.NextDouble() * 1.4 - 0.2, Gap, pinEnds: true);
|
|
for (var k = 1; k < xs.Length; k++) Assert.True(xs[k] - xs[k - 1] >= Gap - 1e-12, $"x[{k - 1}]={xs[k - 1]} x[{k}]={xs[k]}");
|
|
Assert.Approximately(0, xs[0], 0);
|
|
Assert.Approximately(1, xs[^1], 0);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void InsertionKeepsOrderAndRespectsGapAndPins()
|
|
{
|
|
double[] xs = [0, 0.5, 1];
|
|
Assert.Equal(1, CurveMath.InsertionIndex(xs, 0.25, Gap, pinEnds: true));
|
|
Assert.Equal(2, CurveMath.InsertionIndex(xs, 0.75, Gap, pinEnds: true));
|
|
Assert.Equal(-1, CurveMath.InsertionIndex(xs, 0.5 + Gap / 2, Gap, pinEnds: true));
|
|
Assert.Equal(-1, CurveMath.InsertionIndex(xs, 1.2, Gap, pinEnds: false));
|
|
|
|
double[] free = [0.2, 0.6];
|
|
Assert.Equal(0, CurveMath.InsertionIndex(free, 0.1, Gap, pinEnds: false));
|
|
Assert.Equal(2, CurveMath.InsertionIndex(free, 0.9, Gap, pinEnds: false));
|
|
Assert.Equal(-1, CurveMath.InsertionIndex(free, 0.1, Gap, pinEnds: true));
|
|
}
|
|
|
|
[Test]
|
|
public void RemovalKeepsTwoPointsAndPinnedEnds()
|
|
{
|
|
Assert.False(CurveMath.CanRemove(2, 1, pinEnds: false));
|
|
Assert.True(CurveMath.CanRemove(3, 0, pinEnds: false));
|
|
Assert.False(CurveMath.CanRemove(3, 0, pinEnds: true));
|
|
Assert.True(CurveMath.CanRemove(3, 1, pinEnds: true));
|
|
Assert.False(CurveMath.CanRemove(3, 2, pinEnds: true));
|
|
}
|
|
|
|
[Test]
|
|
public void KeyboardInsertionUsesThePlayheadWhenAPointFitsThere()
|
|
{
|
|
double[] xs = [0, 0.4, 1];
|
|
Assert.Approximately(0.7, CurveMath.KeyboardInsertionX(xs, 1, 0.7, Gap, pinEnds: true), 0);
|
|
// On a point, past a pinned end or unset: the middle of the gap after the selection (before it for the last point).
|
|
Assert.Approximately(0.7, CurveMath.KeyboardInsertionX(xs, 1, 0.4, Gap, pinEnds: true), 1e-12);
|
|
Assert.Approximately(0.2, CurveMath.KeyboardInsertionX(xs, 0, double.NaN, Gap, pinEnds: true), 1e-12);
|
|
Assert.Approximately(0.7, CurveMath.KeyboardInsertionX(xs, 2, double.NaN, Gap, pinEnds: true), 1e-12);
|
|
Assert.Approximately(0.2, CurveMath.KeyboardInsertionX(xs, -1, double.NaN, Gap, pinEnds: true), 1e-12);
|
|
Assert.Approximately(0.5, CurveMath.KeyboardInsertionX([], -1, double.NaN, Gap, pinEnds: false), 0);
|
|
Assert.Approximately(0.6, CurveMath.KeyboardInsertionX([0.2], 0, double.NaN, Gap, pinEnds: false), 1e-12);
|
|
}
|
|
|
|
[Test]
|
|
public void InterpolationFollowsTheSegmentsAndIsFlatBeyondTheEnds()
|
|
{
|
|
double[] xs = [0.2, 0.6, 1];
|
|
double[] ys = [0.1, 0.5, 0.3];
|
|
Assert.Approximately(0.3, CurveMath.Interpolate(xs, ys, 0.4), 1e-12);
|
|
Assert.Approximately(0.4, CurveMath.Interpolate(xs, ys, 0.8), 1e-12);
|
|
Assert.Approximately(0.1, CurveMath.Interpolate(xs, ys, 0), 0);
|
|
Assert.Approximately(0.3, CurveMath.Interpolate(xs, ys, 1), 0);
|
|
Assert.Approximately(0.25, CurveMath.Interpolate([], [], 0.25), 0);
|
|
}
|
|
|
|
[Test]
|
|
public void HitTestPicksTheNearestPointWithinRadius()
|
|
{
|
|
double[] xs = [10, 50, 54];
|
|
double[] ys = [10, 10, 10];
|
|
Assert.Equal(2, CurveMath.HitTest(xs, ys, 53, 11, 6));
|
|
Assert.Equal(1, CurveMath.HitTest(xs, ys, 51, 10, 6));
|
|
Assert.Equal(-1, CurveMath.HitTest(xs, ys, 30, 10, 6));
|
|
}
|
|
}
|