Skip to content

Commit 02ab4fd

Browse files
committed
Test fixes
1 parent 30264ba commit 02ab4fd

4 files changed

Lines changed: 150 additions & 5 deletions

File tree

Runtime/DataStructures/CyclicBuffer.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,40 @@ public void Clear()
135135

136136
public void Resize(int newCapacity)
137137
{
138+
if (newCapacity == Capacity)
139+
{
140+
return;
141+
}
142+
138143
if (newCapacity < 0)
139144
{
140145
throw new ArgumentException(nameof(newCapacity));
141146
}
142147

143-
int oldCapacity = Capacity;
144148
Capacity = newCapacity;
149+
150+
// Normalize underlying storage so the oldest element is at index 0.
145151
_buffer.Shift(-_position);
152+
146153
if (newCapacity < _buffer.Count)
147154
{
148-
_buffer.RemoveRange(newCapacity, _buffer.Count - newCapacity);
155+
// When shrinking, drop the oldest elements to retain the most recent window.
156+
int removeCount = _buffer.Count - newCapacity;
157+
_buffer.RemoveRange(0, removeCount);
158+
}
159+
160+
// Update next-write position: if full, wrap to 0 to overwrite oldest; otherwise append at end.
161+
if (Capacity <= 0)
162+
{
163+
_position = 0;
164+
Count = 0;
165+
_buffer.Clear();
166+
return;
149167
}
150168

151-
_position =
152-
newCapacity < oldCapacity && newCapacity <= _buffer.Count ? 0 : _buffer.Count;
169+
// Count cannot exceed new capacity
153170
Count = Math.Min(newCapacity, Count);
171+
_position = _buffer.Count >= Capacity ? 0 : _buffer.Count;
154172
}
155173

156174
public bool Contains(T item)

Tests/Runtime/AutocompleteTests.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void AutoRegisteredCommandReceivesCompleter()
160160
CommandHistory history = new CommandHistory(8);
161161
CommandShell shell = new CommandShell(history);
162162

163-
shell.InitializeAutoRegisteredCommands();
163+
shell.InitializeAutoRegisteredCommands(Array.Empty<string>());
164164

165165
Assert.IsTrue(
166166
shell.Commands.TryGetValue(
@@ -224,5 +224,69 @@ public void AutoCompleteChainsArguments()
224224
Assert.AreEqual(1, partialSecondContext.ArgsBeforeCursor.Count);
225225
Assert.AreEqual("alpha", partialSecondContext.ArgsBeforeCursor[0].contents);
226226
}
227+
228+
[Test]
229+
public void AutoCompleteHonorsCaretIndexWithinInput()
230+
{
231+
CommandHistory history = new CommandHistory(16);
232+
CommandShell shell = new CommandShell(history);
233+
ChainedCompleter chainedCompleter = new ChainedCompleter();
234+
shell.AddCommand("chain", _ => { }, 0, -1, string.Empty, null, chainedCompleter);
235+
236+
CommandAutoComplete autoComplete = new CommandAutoComplete(history, shell);
237+
List<string> buffer = new List<string>();
238+
int caretIndex = "chain alpha ".Length;
239+
autoComplete.Complete("chain alpha gamma", caretIndex, buffer);
240+
241+
Assert.AreEqual(1, buffer.Count);
242+
Assert.AreEqual("chain alpha gamma", buffer[0]);
243+
Assert.AreEqual(1, chainedCompleter.Calls.Count);
244+
CommandCompletionContext context = chainedCompleter.Calls[0];
245+
Assert.AreEqual(1, context.ArgIndex);
246+
Assert.AreEqual(string.Empty, context.PartialArg);
247+
Assert.AreEqual(1, context.ArgsBeforeCursor.Count);
248+
Assert.AreEqual("alpha", context.ArgsBeforeCursor[0].contents);
249+
}
250+
251+
[Test]
252+
public void AutoCompleteFallsBackToHistoryWhenCommandUnknown()
253+
{
254+
CommandHistory history = new CommandHistory(16);
255+
history.Push("login", true, true);
256+
history.Push("logout", true, true);
257+
CommandShell shell = new CommandShell(history);
258+
259+
CommandAutoComplete autoComplete = new CommandAutoComplete(history, shell);
260+
string[] suggestions = autoComplete.Complete("lo");
261+
262+
Assert.IsNotNull(suggestions);
263+
CollectionAssert.Contains(suggestions, "login");
264+
CollectionAssert.Contains(suggestions, "logout");
265+
}
266+
267+
[Test]
268+
public void AutoCompleteDeduplicatesValuesAcrossSources()
269+
{
270+
CommandHistory history = new CommandHistory(16);
271+
history.Push("list", true, true);
272+
CommandShell shell = new CommandShell(history);
273+
shell.AddCommand("list", _ => { });
274+
List<string> knownWords = new List<string> { "list" };
275+
CommandAutoComplete autoComplete = new CommandAutoComplete(history, shell, knownWords);
276+
277+
string[] suggestions = autoComplete.Complete("li");
278+
Assert.IsNotNull(suggestions);
279+
280+
int matches = 0;
281+
for (int i = 0; i < suggestions.Length; ++i)
282+
{
283+
if (string.Equals(suggestions[i], "list", StringComparison.Ordinal))
284+
{
285+
matches++;
286+
}
287+
}
288+
289+
Assert.AreEqual(1, matches, "Expected deduplicated suggestion list.");
290+
}
227291
}
228292
}

Tests/Runtime/CyclicBufferTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
namespace WallstopStudios.DxCommandTerminal.Tests.Runtime
2+
{
3+
using System.Collections;
4+
using NUnit.Framework;
5+
using UnityEngine.TestTools;
6+
using WallstopStudios.DxCommandTerminal.DataStructures;
7+
8+
public sealed class CyclicBufferTests
9+
{
10+
[UnityTest]
11+
public IEnumerator AddAndOverwritePreservesChronology()
12+
{
13+
CyclicBuffer<int> buf = new(3) { 0, 1, 2 };
14+
15+
Assert.AreEqual(
16+
3,
17+
buf.Count,
18+
"Count should reflect number of elements added up to capacity."
19+
);
20+
Assert.AreEqual(0, buf[0], "Oldest element should be at index 0 before wrap.");
21+
Assert.AreEqual(1, buf[1], "Next element should be index 1.");
22+
Assert.AreEqual(2, buf[2], "Newest element should be index 2 before wrap.");
23+
24+
// Overwrite oldest
25+
buf.Add(3);
26+
Assert.AreEqual(3, buf.Count, "Count should not grow beyond capacity.");
27+
Assert.AreEqual(1, buf[0], "After overwrite, oldest is dropped.");
28+
Assert.AreEqual(2, buf[1], "Element order should advance by one.");
29+
Assert.AreEqual(3, buf[2], "Newest written value should be last.");
30+
31+
yield break;
32+
}
33+
34+
[UnityTest]
35+
public IEnumerator ResizeTruncatesOrExtends()
36+
{
37+
CyclicBuffer<int> buf = new(5);
38+
for (int i = 0; i < 5; ++i)
39+
{
40+
buf.Add(i);
41+
}
42+
Assert.AreEqual(5, buf.Count, "Filled buffer should have full count.");
43+
44+
// Shrink: oldest entries should be truncated
45+
buf.Resize(3);
46+
Assert.AreEqual(3, buf.Count, "Count should reflect new capacity after shrink.");
47+
Assert.AreEqual(2, buf[0], "Shrink should retain most recent entries and drop oldest.");
48+
Assert.AreEqual(3, buf[1], "Remaining order should be preserved (middle).");
49+
Assert.AreEqual(4, buf[2], "Remaining order should be preserved (newest).");
50+
51+
// Grow: capacity increases, order stays
52+
buf.Resize(6);
53+
Assert.AreEqual(3, buf.Count, "Growing capacity should not change current count.");
54+
Assert.AreEqual(2, buf[0], "Growing capacity should not alter order (first).");
55+
Assert.AreEqual(3, buf[1], "Growing capacity should not alter order (second).");
56+
Assert.AreEqual(4, buf[2], "Growing capacity should not alter order (third).");
57+
yield break;
58+
}
59+
}
60+
}

Tests/Runtime/CyclicBufferTests.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)