Skip to content

Commit 37b2fc1

Browse files
author
LoneWandererProductions
committed
test my stuff further.
1 parent d1aecdd commit 37b2fc1

5 files changed

Lines changed: 208 additions & 1 deletion

File tree

CommonExtendedObjectsTests/SortedKvStoreTests.cs

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ public void InsertKeysThatForceShiftShouldKeepArraysInSync()
247247
// Now insert a smaller key, which should shift existing elements
248248
store.Add(1, 50); // Will be inserted at index 0, causes shift right
249249

250+
// Dump internal state for debug (optional)
251+
Trace.WriteLine(store.ToString());
252+
250253
// Assert
251254
Assert.AreEqual(2, store.Count, "Store should have two occupied entries.");
252255

@@ -278,12 +281,193 @@ public void InsertRemoveCompactReinsertShouldKeepConsistency()
278281
store.Add(1, 30); // Reinsert key 1
279282

280283
var keys = store.Keys.ToArray();
284+
285+
// Dump internal state for debug (optional)
286+
Trace.WriteLine(store.ToString());
287+
281288
CollectionAssert.AreEqual(new[] { 1, 2 }, keys);
282289

283290
Assert.AreEqual(30, store[1]);
284291
Assert.AreEqual(20, store[2]);
285292
}
286293

294+
/// <summary>
295+
/// Inserts the keys starting at one should keep arrays aligned.
296+
/// </summary>
297+
[TestMethod]
298+
public void InsertKeysStartingAtOne_ShouldKeepArraysAligned()
299+
{
300+
var store = new SortedKvStore();
301+
302+
// Insert keys starting from 1 (not 0)
303+
store.Add(1, 100);
304+
store.Add(3, 300);
305+
store.Add(2, 200); // Insert out of order to force shift
306+
307+
// Validate count
308+
Assert.AreEqual(3, store.Count, "Count should be 3 after inserts.");
309+
310+
// Validate keys are sorted
311+
var keys = store.Keys.ToArray();
312+
CollectionAssert.AreEqual(new[] { 1, 2, 3 }, keys, "Keys should be sorted.");
313+
314+
// Validate values match keys
315+
Assert.AreEqual(100, store[1], "Value for key 1 should be 100.");
316+
Assert.AreEqual(200, store[2], "Value for key 2 should be 200.");
317+
Assert.AreEqual(300, store[3], "Value for key 3 should be 300.");
318+
319+
// Remove key 2 and check
320+
store.Remove(2);
321+
Assert.IsFalse(store.ContainsKey(2), "Key 2 should be removed.");
322+
323+
// Compact to physically remove unoccupied entries
324+
store.Compact();
325+
326+
Assert.AreEqual(2, store.Count, "Count should be 2 after removal and compact.");
327+
328+
keys = store.Keys.ToArray();
329+
CollectionAssert.AreEqual(new[] { 1, 3 }, keys, "Keys after removal should be 1 and 3.");
330+
331+
// Reinsert key 2 to check insert after compact works
332+
store.Add(2, 250);
333+
Assert.IsTrue(store.ContainsKey(2), "Key 2 should exist after reinsertion.");
334+
Assert.AreEqual(250, store[2], "Value for key 2 should be updated to 250.");
335+
336+
// Dump internal state for debug (optional)
337+
Trace.WriteLine(store.ToString());
338+
339+
// Final keys check to confirm sorted order
340+
keys = store.Keys.ToArray();
341+
CollectionAssert.AreEqual(new[] { 1, 2, 3 }, keys, "Final keys should be sorted 1, 2, 3.");
342+
}
343+
344+
/// <summary>
345+
/// Removes the many should update count correctly.
346+
/// </summary>
347+
[TestMethod]
348+
public void RemoveManyShouldUpdateCountCorrectly()
349+
{
350+
var store = new SortedKvStore
351+
{
352+
// Insert 5 keys
353+
{ 1, 100 },
354+
{ 2, 200 },
355+
{ 3, 300 },
356+
{ 4, 400 },
357+
{ 5, 500 }
358+
};
359+
360+
Assert.AreEqual(5, store.Count);
361+
362+
// Remove keys 2 and 4
363+
var keysToRemove = new int[] { 2, 4 };
364+
store.RemoveMany(keysToRemove);
365+
366+
// Dump internal state for debug (optional)
367+
Trace.WriteLine(store.ToString());
368+
369+
// Check keys 2 and 4 are removed
370+
Assert.IsFalse(store.ContainsKey(2));
371+
Assert.IsFalse(store.ContainsKey(4));
372+
373+
// Check keys 1, 3, 5 are still present
374+
Assert.IsTrue(store.ContainsKey(1));
375+
Assert.IsTrue(store.ContainsKey(3));
376+
Assert.IsTrue(store.ContainsKey(5));
377+
378+
// THIS WILL FAIL IF Count IS NOT UPDATED
379+
Assert.AreEqual(5, store.Count, "Count should NOT be updated after RemoveMany");
380+
}
381+
382+
/// <summary>
383+
/// Removes the many with unsorted keys removes correctly.
384+
/// </summary>
385+
[TestMethod]
386+
public void RemoveManyWithUnsortedKeysRemovesCorrectly()
387+
{
388+
var store = new SortedKvStore();
389+
390+
// Add keys in sorted order
391+
int[] keys = { 1, 3, 5, 7, 9 };
392+
foreach (var key in keys)
393+
{
394+
store.Add(key, key * 10);
395+
}
396+
397+
// Confirm initial state
398+
Assert.AreEqual(keys.Length, store.Count);
399+
foreach (var key in keys)
400+
{
401+
Assert.IsTrue(store.ContainsKey(key), $"Should contain key {key} initially.");
402+
}
403+
404+
// Remove keys (unsorted input)
405+
int[] keysToRemove = { 7, 3 };
406+
store.RemoveMany(keysToRemove);
407+
408+
// Dump internal state for debug (optional)
409+
Trace.WriteLine(store.ToString());
410+
411+
// Check keys that should remain
412+
int[] expectedRemaining = { 1, 5, 9 };
413+
foreach (var key in expectedRemaining)
414+
{
415+
Assert.IsTrue(store.ContainsKey(key), $"Expected to contain key {key} after RemoveMany.");
416+
}
417+
418+
// Check keys that should be removed
419+
foreach (var key in keysToRemove)
420+
{
421+
Assert.IsFalse(store.ContainsKey(key), $"Expected NOT to contain key {key} after RemoveMany.");
422+
}
423+
}
424+
425+
/// <summary>
426+
/// Removes the and compact test.
427+
/// </summary>
428+
[TestMethod]
429+
public void RemoveAndCompactTest()
430+
{
431+
var store = new SortedKvStore
432+
{
433+
// Add some entries
434+
{ 1, 10 },
435+
{ 3, 30 },
436+
{ 5, 50 },
437+
{ 7, 70 },
438+
{ 9, 90 }
439+
};
440+
441+
Assert.IsTrue(store.ContainsKey(3));
442+
Assert.AreEqual(5, store.Count);
443+
444+
// Remove key 3 logically
445+
store.Remove(3);
446+
447+
// Immediately after remove, key is logically gone
448+
Assert.IsFalse(store.ContainsKey(3));
449+
450+
// Count still includes all, including logically removed
451+
Assert.AreEqual(5, store.Count);
452+
453+
// Compact physically removes unoccupied entries
454+
store.Compact();
455+
456+
// After compact, key is definitely gone
457+
Assert.IsFalse(store.ContainsKey(3));
458+
459+
// Count updated to reflect physical removal
460+
Assert.AreEqual(4, store.Count);
461+
462+
// Dump internal state for debug (optional)
463+
Trace.WriteLine(store.ToString());
464+
465+
// Optional: check that other keys are still present
466+
Assert.IsTrue(store.ContainsKey(1));
467+
Assert.IsTrue(store.ContainsKey(5));
468+
Assert.IsTrue(store.ContainsKey(7));
469+
Assert.IsTrue(store.ContainsKey(9));
470+
}
287471

288472
}
289473
}
0 Bytes
Loading
0 Bytes
Loading

ExtendedSystemObjects/SortedKvStore.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Buffers;
1616
using System.Collections;
1717
using System.Collections.Generic;
18+
using System.Diagnostics;
1819

1920
namespace ExtendedSystemObjects
2021
{
@@ -25,6 +26,7 @@ namespace ExtendedSystemObjects
2526
/// The class supports insertion, removal, and lookup operations with dynamic storage growth.
2627
/// </summary>
2728
/// <seealso cref="T:System.IDisposable" />
29+
[DebuggerDisplay("{ToString()}")]
2830
public sealed class SortedKvStore : IDisposable, IEnumerable<KeyValuePair<int, int>>
2931
{
3032
/// <summary>
@@ -295,6 +297,7 @@ public void RemoveMany(ReadOnlySpan<int> keysToRemove)
295297
// Optional: if keysToRemove is sorted, do a linear merge
296298
// This path is faster than HashSet-based if input is sorted and large
297299
var isSorted = true;
300+
298301
for (var i = 1; i < keysToRemove.Length; i++)
299302
{
300303
if (keysToRemove[i] >= keysToRemove[i - 1])
@@ -390,6 +393,26 @@ public void Clear()
390393
_keys.Clear();
391394
_values.Clear();
392395
_occupied.Clear();
396+
Count = 0;
397+
}
398+
399+
/// <summary>
400+
/// Converts to string.
401+
/// </summary>
402+
/// <returns>
403+
/// A <see cref="System.String" /> that represents this instance.
404+
/// </returns>
405+
public override string ToString()
406+
{
407+
var sb = new System.Text.StringBuilder();
408+
sb.AppendLine($"Count: {Count}");
409+
410+
for (int i = 0; i < Count; i++)
411+
{
412+
sb.AppendLine($"Index {i}: Key={_keys[i]}, Value={_values[i]}, Occupied={_occupied[i]}");
413+
}
414+
415+
return sb.ToString();
393416
}
394417

395418
/// <summary>

Mathematics/Coordinate2D.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Mathematics
2222
/// <summary>
2323
/// Coordinate 2d Helper Class
2424
/// </summary>
25-
[DebuggerDisplay("X = {X}, Y = {Y}, Id = {Id}")]
25+
[DebuggerDisplay("{ToString()}")]
2626
public sealed class Coordinate2D : ICloneable
2727
{
2828
/// <summary>

0 commit comments

Comments
 (0)