|
| 1 | +// Copyright (c) ktsu.dev |
| 2 | +// All rights reserved. |
| 3 | +// Licensed under the MIT license. |
| 4 | + |
| 5 | +namespace ktsu.Containers.Tests; |
| 6 | + |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | + |
| 9 | +[TestClass] |
| 10 | +public class DelayLineTests |
| 11 | +{ |
| 12 | + [TestMethod] |
| 13 | + public void Constructor_NonPositiveCapacity_Throws() |
| 14 | + { |
| 15 | + Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new DelayLine(0)); |
| 16 | + Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new DelayLine(-4)); |
| 17 | + } |
| 18 | + |
| 19 | + [TestMethod] |
| 20 | + public void Capacity_MatchesRequestedMaxDelay() |
| 21 | + { |
| 22 | + DelayLine line = new(100); |
| 23 | + Assert.AreEqual(100, line.Capacity); |
| 24 | + } |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void NewDelayLine_ReadsZero() |
| 28 | + { |
| 29 | + DelayLine line = new(8); |
| 30 | + for (int delay = 0; delay <= line.Capacity; delay++) |
| 31 | + { |
| 32 | + Assert.AreEqual(0f, line.Read(delay)); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + [TestMethod] |
| 37 | + public void Read_ReturnsSampleNSamplesInThePast() |
| 38 | + { |
| 39 | + DelayLine line = new(8); |
| 40 | + line.Write(1f); |
| 41 | + line.Write(2f); |
| 42 | + line.Write(3f); |
| 43 | + |
| 44 | + // Delay 0 == most recent, delay 2 == oldest of the three writes. |
| 45 | + Assert.AreEqual(3f, line.Read(0)); |
| 46 | + Assert.AreEqual(2f, line.Read(1)); |
| 47 | + Assert.AreEqual(1f, line.Read(2)); |
| 48 | + } |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public void Process_OutputsInputFromExactlyDelaySamplesEarlier() |
| 52 | + { |
| 53 | + DelayLine line = new(4); |
| 54 | + // Process(x, 4) yields x[n - 4]: zero until the line has been primed with 4 samples. |
| 55 | + Assert.AreEqual(0f, line.Process(10f, 4)); |
| 56 | + Assert.AreEqual(0f, line.Process(20f, 4)); |
| 57 | + Assert.AreEqual(0f, line.Process(30f, 4)); |
| 58 | + Assert.AreEqual(0f, line.Process(40f, 4)); |
| 59 | + Assert.AreEqual(10f, line.Process(50f, 4)); |
| 60 | + Assert.AreEqual(20f, line.Process(60f, 4)); |
| 61 | + } |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void Process_ZeroDelay_ReturnsInput() |
| 65 | + { |
| 66 | + DelayLine line = new(4); |
| 67 | + Assert.AreEqual(7f, line.Process(7f, 0)); |
| 68 | + Assert.AreEqual(9f, line.Process(9f, 0)); |
| 69 | + } |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public void Read_OutOfRange_Throws() |
| 73 | + { |
| 74 | + DelayLine line = new(8); |
| 75 | + Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => line.Read(-1)); |
| 76 | + Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => line.Read(9)); |
| 77 | + } |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public void ReadInterpolated_HalfwayBetweenSamples_AveragesNeighbours() |
| 81 | + { |
| 82 | + DelayLine line = new(8); |
| 83 | + line.Write(0f); |
| 84 | + line.Write(10f); |
| 85 | + |
| 86 | + // delay 0 -> 10 (newest), delay 1 -> 0 (older). 0.5 interpolates halfway. |
| 87 | + Assert.AreEqual(5f, line.ReadInterpolated(0.5f), 1e-6f); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void ReadInterpolated_IntegerDelay_MatchesRead() |
| 92 | + { |
| 93 | + DelayLine line = new(8); |
| 94 | + line.Write(3f); |
| 95 | + line.Write(7f); |
| 96 | + line.Write(11f); |
| 97 | + |
| 98 | + Assert.AreEqual(line.Read(1), line.ReadInterpolated(1f), 1e-6f); |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void ReadInterpolated_OutOfRange_Throws() |
| 103 | + { |
| 104 | + DelayLine line = new(8); |
| 105 | + Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => line.ReadInterpolated(-0.5f)); |
| 106 | + Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => line.ReadInterpolated(8.5f)); |
| 107 | + Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => line.ReadInterpolated(float.NaN)); |
| 108 | + } |
| 109 | + |
| 110 | + [TestMethod] |
| 111 | + public void Write_FlushesDenormalsToZero() |
| 112 | + { |
| 113 | + DelayLine line = new(4); |
| 114 | + float denormal = float.Epsilon; // smallest subnormal float, well below the threshold |
| 115 | + line.Write(denormal); |
| 116 | + Assert.AreEqual(0f, line.Read(0), "Denormal input must be flushed to zero."); |
| 117 | + } |
| 118 | + |
| 119 | + [TestMethod] |
| 120 | + public void Write_KeepsNormalSmallValues() |
| 121 | + { |
| 122 | + DelayLine line = new(4); |
| 123 | + const float audible = 1e-6f; // ~ -120 dBFS, a normal float that must be preserved |
| 124 | + line.Write(audible); |
| 125 | + Assert.AreEqual(audible, line.Read(0)); |
| 126 | + } |
| 127 | + |
| 128 | + [TestMethod] |
| 129 | + public void Clear_ZeroesAllSamples() |
| 130 | + { |
| 131 | + DelayLine line = new(4); |
| 132 | + line.Write(1f); |
| 133 | + line.Write(2f); |
| 134 | + line.Clear(); |
| 135 | + |
| 136 | + for (int delay = 0; delay <= line.Capacity; delay++) |
| 137 | + { |
| 138 | + Assert.AreEqual(0f, line.Read(delay)); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + [TestMethod] |
| 143 | + public void WrapAround_LongStreamReadsCorrectDelay() |
| 144 | + { |
| 145 | + DelayLine line = new(3); |
| 146 | + float previous = 0f; |
| 147 | + for (int i = 1; i <= 1000; i++) |
| 148 | + { |
| 149 | + // Process(i, 1) returns the value written on the previous call (i - 1), 0 on the first. |
| 150 | + float output = line.Process(i, 1); |
| 151 | + Assert.AreEqual(i - 1, output); |
| 152 | + previous = output; |
| 153 | + } |
| 154 | + |
| 155 | + Assert.AreEqual(999f, previous); |
| 156 | + } |
| 157 | +} |
0 commit comments