|
| 1 | +using System.Collections.ObjectModel; |
| 2 | + |
| 3 | +using LogExpert.Core.Classes.Filter; |
| 4 | + |
| 5 | +using NUnit.Framework; |
| 6 | + |
| 7 | +namespace LogExpert.Tests.Filter; |
| 8 | + |
| 9 | +[TestFixture] |
| 10 | +public class FilterSpreadTests |
| 11 | +{ |
| 12 | + // Row format: hit line, spread before, spread behind, line count, already-taken history, expected lines. |
| 13 | + // Expected values are worked examples from the spec (docs/specs/filter-spread-extraction.md). |
| 14 | + [TestCase(5, 0, 0, 100, new int[0], new[] { 5 }, |
| 15 | + TestName = "Expand_NoSpreadConfigured_ReturnsOnlyTheHitLine")] |
| 16 | + [TestCase(5, 0, 0, 100, new[] { 5 }, new[] { 5 }, |
| 17 | + TestName = "Expand_NoSpreadConfigured_IgnoresHistory_HistoricalQuirkPreserved")] |
| 18 | + [TestCase(0, 3, 2, 100, new int[0], new[] { 0, 1, 2 }, |
| 19 | + TestName = "Expand_HitAtLineZero_EmitsNoNegativeLines")] |
| 20 | + [TestCase(3, 5, 0, 100, new int[0], new[] { 0, 1, 2, 3 }, |
| 21 | + TestName = "Expand_BackSpreadReachingPastTopOfFile_ClampsAtLineZeroAndIncludesIt")] |
| 22 | + [TestCase(97, 0, 5, 100, new int[0], new[] { 97, 98, 99 }, |
| 23 | + TestName = "Expand_ForeSpreadReachingPastEndOfFile_ClampsAtLastLine")] |
| 24 | + [TestCase(99, 0, 3, 100, new int[0], new[] { 99 }, |
| 25 | + TestName = "Expand_HitAtLastLineWithForeSpread_ReturnsOnlyTheHit")] |
| 26 | + [TestCase(10, 1, 3, 100, new int[0], new[] { 9, 10, 11, 12, 13 }, |
| 27 | + TestName = "Expand_AsymmetricSpread_ReturnsBackLinesHitThenForeLines")] |
| 28 | + [TestCase(12, 2, 2, 100, new[] { 8, 9, 10, 11, 12 }, new[] { 13, 14 }, |
| 29 | + TestName = "Expand_OverlappingWithEarlierHit_SuppressesLinesAlreadyTaken")] |
| 30 | + [TestCase(5, 1, 1, 100, new[] { 5 }, new[] { 4, 6 }, |
| 31 | + TestName = "Expand_HitItselfAlreadyTaken_IsNotEmittedAgain")] |
| 32 | + public void Expand_Table (int lineNum, int spreadBefore, int spreadBehind, int lineCount, int[] alreadyTaken, int[] expected) |
| 33 | + { |
| 34 | + var result = FilterSpread.Expand(lineNum, spreadBefore, spreadBehind, lineCount, alreadyTaken); |
| 35 | + |
| 36 | + Assert.That(result, Is.EqualTo(expected)); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void TrimHistory_LongerThanWindow_DropsOldestEntriesDownToWindowSize () |
| 41 | + { |
| 42 | + // Window is 2 × 99 (the maximum configurable spread) — unifies the divergent 50/99 constants. |
| 43 | + var history = new List<int>(Enumerable.Range(0, 250)); |
| 44 | + |
| 45 | + FilterSpread.TrimHistory(history); |
| 46 | + |
| 47 | + Assert.Multiple(() => |
| 48 | + { |
| 49 | + Assert.That(history, Has.Count.EqualTo(198)); |
| 50 | + Assert.That(history[0], Is.EqualTo(52)); |
| 51 | + Assert.That(history[^1], Is.EqualTo(249)); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void TrimHistory_WithinWindow_IsUnchanged () |
| 57 | + { |
| 58 | + var history = new List<int>(Enumerable.Range(0, 198)); |
| 59 | + |
| 60 | + FilterSpread.TrimHistory(history); |
| 61 | + |
| 62 | + Assert.That(history, Has.Count.EqualTo(198)); |
| 63 | + } |
| 64 | + |
| 65 | + [Test] |
| 66 | + public void TrimHistory_WorksOnAnyIListImplementation () |
| 67 | + { |
| 68 | + // The Filter Pipe history is an IList<int>, not a List<int> — the trim rule must not care. |
| 69 | + IList<int> history = new Collection<int>(); |
| 70 | + foreach (var i in Enumerable.Range(0, 200)) |
| 71 | + { |
| 72 | + history.Add(i); |
| 73 | + } |
| 74 | + |
| 75 | + FilterSpread.TrimHistory(history); |
| 76 | + |
| 77 | + Assert.Multiple(() => |
| 78 | + { |
| 79 | + Assert.That(history, Has.Count.EqualTo(198)); |
| 80 | + Assert.That(history[0], Is.EqualTo(2)); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public void ShiftLines_OnRollover_SubtractsOffsetFromEveryLine () |
| 86 | + { |
| 87 | + var shifted = FilterSpread.ShiftLines([10, 20, 30], offset: 5); |
| 88 | + |
| 89 | + Assert.That(shifted, Is.EqualTo(new[] { 5, 15, 25 })); |
| 90 | + } |
| 91 | + |
| 92 | + [Test] |
| 93 | + public void ShiftLines_LinesRolledOffTheStart_AreDropped () |
| 94 | + { |
| 95 | + // Offset 15 rolls lines 10 and 14 off the virtual file; 15 shifts to 0 and survives. |
| 96 | + var shifted = FilterSpread.ShiftLines([10, 14, 15, 40], offset: 15); |
| 97 | + |
| 98 | + Assert.That(shifted, Is.EqualTo(new[] { 0, 25 })); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public void RebuildHistory_ResultsLongerThanSpreadMax_TakesTheLastSpreadMaxLines () |
| 103 | + { |
| 104 | + // The rollover rebuild caps the history at SPREAD_MAX (99), not the 2×99 trim |
| 105 | + // window — a preserved quirk of the original rollover code. |
| 106 | + var results = new List<int>(Enumerable.Range(0, 150)); |
| 107 | + |
| 108 | + var history = FilterSpread.RebuildHistory(results); |
| 109 | + |
| 110 | + Assert.Multiple(() => |
| 111 | + { |
| 112 | + Assert.That(history, Has.Count.EqualTo(99)); |
| 113 | + Assert.That(history[0], Is.EqualTo(51)); |
| 114 | + Assert.That(history[^1], Is.EqualTo(149)); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + [Test] |
| 119 | + public void RebuildHistory_ResultsShorterThanSpreadMax_TakesAllOfThem () |
| 120 | + { |
| 121 | + var history = FilterSpread.RebuildHistory([3, 4, 5]); |
| 122 | + |
| 123 | + Assert.That(history, Is.EqualTo(new[] { 3, 4, 5 })); |
| 124 | + } |
| 125 | + |
| 126 | + [Test] |
| 127 | + public void RebuildHistory_EmptyResults_ReturnsEmptyHistory () |
| 128 | + { |
| 129 | + var history = FilterSpread.RebuildHistory([]); |
| 130 | + |
| 131 | + Assert.That(history, Is.Empty); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Equivalence guard: the single-threaded Log Window filter and the multi-threaded |
| 136 | + /// Core filter accumulate per-hit results through the same recipe |
| 137 | + /// (hit → Expand → append to results/history → TrimHistory). This pins that shared |
| 138 | + /// recipe's output for a hit sequence with overlapping spreads; a call site that |
| 139 | + /// deviates from the recipe no longer matches this specification. Expected values are |
| 140 | + /// a worked example: hits 5, 7 and 50 with spread 2/2 in a 100-line file. |
| 141 | + /// </summary> |
| 142 | + [Test] |
| 143 | + public void AccumulationRecipe_OverlappingHitSequence_ProducesPinnedResultHitAndHistoryLists () |
| 144 | + { |
| 145 | + int[] hits = [5, 7, 50]; |
| 146 | + List<int> resultLines = []; |
| 147 | + List<int> history = []; |
| 148 | + List<int> hitList = []; |
| 149 | + |
| 150 | + foreach (var hit in hits) |
| 151 | + { |
| 152 | + hitList.Add(hit); |
| 153 | + var expanded = FilterSpread.Expand(hit, spreadBefore: 2, spreadBehind: 2, lineCount: 100, alreadyTaken: history); |
| 154 | + resultLines.AddRange(expanded); |
| 155 | + history.AddRange(expanded); |
| 156 | + FilterSpread.TrimHistory(history); |
| 157 | + } |
| 158 | + |
| 159 | + Assert.Multiple(() => |
| 160 | + { |
| 161 | + // Hit 5 contributes 3..7; hit 7 contributes only 8,9 (5..7 already taken); hit 50 contributes 48..52. |
| 162 | + Assert.That(resultLines, Is.EqualTo(new[] { 3, 4, 5, 6, 7, 8, 9, 48, 49, 50, 51, 52 })); |
| 163 | + Assert.That(hitList, Is.EqualTo(new[] { 5, 7, 50 })); |
| 164 | + Assert.That(history, Is.EqualTo(resultLines), "history below the window size mirrors the result lines"); |
| 165 | + }); |
| 166 | + } |
| 167 | +} |
0 commit comments