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(() => 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; } }