Skip to content

Commit 476437e

Browse files
committed
Add resize test for negative hashkeys
1 parent 2d39e23 commit 476437e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

DataStructures.Tests/Hashing/HashTableTests.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,23 @@ public void Test_NegativeHashKey_ReturnsCorrectValue()
454454
Assert.That(hashTable[new NegativeHashKey(1)], Is.EqualTo(1));
455455
}
456456

457+
[Test]
458+
public void Resize_HandlesNegativeHashCodeCorrectly()
459+
{
460+
// Arrange
461+
var hashTable = new HashTable<NegativeHashKey, string>(2);
462+
463+
// Act
464+
hashTable.Add(new NegativeHashKey(1), "A");
465+
hashTable.Add(new NegativeHashKey(2), "B");
466+
hashTable.Add(new NegativeHashKey(3), "C");
467+
468+
// Assert
469+
Assert.That(hashTable[new NegativeHashKey(1)], Is.EqualTo("A"));
470+
Assert.That(hashTable[new NegativeHashKey(2)], Is.EqualTo("B"));
471+
Assert.That(hashTable[new NegativeHashKey(3)], Is.EqualTo("C"));
472+
}
473+
457474
[Test]
458475
public void Add_ShouldTriggerResize_WhenThresholdExceeded()
459476
{
@@ -462,14 +479,14 @@ public void Add_ShouldTriggerResize_WhenThresholdExceeded()
462479
var hashTable = new HashTable<int, string>(initialCapacity);
463480

464481
// Act
465-
for (var i = 1; i <= 4; i++) // Start keys from 1 to avoid default(TKey) = 0 issue
482+
for (var i = 1; i <= 32; i++)
466483
{
467484
hashTable.Add(i, $"Value{i}");
468485
}
469486

470487
// Assert
471-
hashTable.Capacity.Should().BeGreaterThan(initialCapacity); // Ensure resizing occurred
472-
hashTable.Count.Should().Be(4); // Verify count reflects number of added items
488+
hashTable.Capacity.Should().BeGreaterThan(initialCapacity);
489+
hashTable.Count.Should().Be(32);
473490
}
474491

475492

0 commit comments

Comments
 (0)