Files
Chronos/tests/Titano.App.Tests/Visuals/MinMaxDecimatorTests.cs
T
Claude 661be6c94b feat(ui-visuals): add layout, scale, decimation, viewport and curve math kernels
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
2026-10-02 10:03:20 +00:00

107 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Titano.Testing;
using Titano.Ui.Visuals;
namespace Titano.App.Tests.Visuals;
public sealed class MinMaxDecimatorTests
{
[Test]
public void ShortSeriesKeepsEverySample()
{
double[] values = [3, 1, 4, 1, 5];
var output = new int[MinMaxDecimator.Capacity(values.Length, 10)];
var n = MinMaxDecimator.Decimate(values, 10, output);
Assert.SequenceEqual([0, 1, 2, 3, 4], output[..n]);
}
[Test]
public void LongSeriesKeepsAtMostTwoSamplesPerColumn()
{
var values = Noise(200_000, seed: 1);
const int columns = 800;
var output = new int[MinMaxDecimator.Capacity(values.Length, columns)];
var n = MinMaxDecimator.Decimate(values, columns, output);
Assert.LessOrEqual(n, 2 * columns);
Assert.Monotonic(output[..n].Select(i => (double)i), strict: true);
}
[Test]
public void EveryColumnPreservesItsMinimumAndMaximum()
{
var values = Noise(123_457, seed: 2);
// Plant spikes that a naive stride sampler would miss.
values[50_001] = 1000;
values[99_999] = -1000;
const int columns = 640;
var output = new int[MinMaxDecimator.Capacity(values.Length, columns)];
var n = MinMaxDecimator.Decimate(values, columns, output);
var kept = output[..n];
for (var c = 0; c < columns; c++)
{
var start = (int)((long)c * values.Length / columns);
var end = (int)((long)(c + 1) * values.Length / columns);
double min = double.PositiveInfinity, max = double.NegativeInfinity;
double keptMin = double.PositiveInfinity, keptMax = double.NegativeInfinity;
for (var i = start; i < end; i++)
{
min = Math.Min(min, values[i]);
max = Math.Max(max, values[i]);
}
foreach (var i in kept)
{
if (i < start || i >= end) continue;
keptMin = Math.Min(keptMin, values[i]);
keptMax = Math.Max(keptMax, values[i]);
}
Assert.Equal(min, keptMin, $"column {c} min");
Assert.Equal(max, keptMax, $"column {c} max");
}
Assert.Contains(50_001, kept);
Assert.Contains(99_999, kept);
}
[Test]
public void NonFiniteRunsBecomeSingleGaps()
{
double[] values = [1, double.NaN, double.NaN, 2, double.PositiveInfinity, 3, double.NaN];
var output = new int[values.Length];
var n = MinMaxDecimator.Decimate(values, 100, output);
Assert.SequenceEqual([0, MinMaxDecimator.Gap, 3, MinMaxDecimator.Gap, 5], output[..n]);
}
[Test]
public void EmptyColumnsBreakTheLineInDecimatedMode()
{
var values = new double[1000];
for (var i = 0; i < values.Length; i++) values[i] = i < 400 || i >= 600 ? i : double.NaN;
var output = new int[MinMaxDecimator.Capacity(values.Length, 10)];
var n = MinMaxDecimator.Decimate(values, 10, output);
// Columns 4 and 5 (indices 400–599) hold no finite sample: exactly one gap between columns 3 and 6.
Assert.SequenceEqual([0, 99, 100, 199, 200, 299, 300, 399, MinMaxDecimator.Gap, 600, 699, 700, 799, 800, 899, 900, 999], output[..n]);
}
[Test]
public void FlatColumnsEmitOneSample()
{
var values = Enumerable.Repeat(5.0, 1000).ToArray();
var output = new int[MinMaxDecimator.Capacity(values.Length, 4)];
var n = MinMaxDecimator.Decimate(values, 4, output);
Assert.SequenceEqual([0, 250, 500, 750], output[..n]);
}
[Test]
public void UndersizedOutputIsRejected()
{
Assert.Throws<ArgumentException>(() => MinMaxDecimator.Decimate(new double[100], 10, new int[5]));
}
private static double[] Noise(int count, int seed)
{
var random = new Random(seed);
var values = new double[count];
for (var i = 0; i < count; i++) values[i] = Math.Sin(i * 0.001) + random.NextDouble() * 0.2;
return values;
}
}