WPF-free geometry behind the visual controls: wrapped-grid layout with O(1) index/offset mapping and keyboard targets, nice axis ticks, min/max decimation, zoom/pan transforms, monotone curve editing rules, filmstrip and heatmap cell layout, an LRU cache and device-pixel snapping, with tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
86 lines
3.3 KiB
C#
86 lines
3.3 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 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));
|
|
}
|
|
}
|