Skip to content

Commit 046f26e

Browse files
authored
Improve coverage for hash table (#500)
1 parent 0ef9849 commit 046f26e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

DataStructures.Tests/Hashing/HashTableTests.cs

+94
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using DataStructures.Hashing;
4+
using FluentAssertions;
45
using NUnit.Framework;
56

67
namespace DataStructures.Tests.Hashing;
@@ -389,6 +390,99 @@ public void Test_NegativeHashKey_ReturnsCorrectValue()
389390
hashTable.Add(new NegativeHashKey(1), 1);
390391
Assert.That(hashTable[new NegativeHashKey(1)], Is.EqualTo(1));
391392
}
393+
394+
[Test]
395+
public void Add_ShouldTriggerResize_WhenThresholdExceeded()
396+
{
397+
// Arrange
398+
var initialCapacity = 4;
399+
var hashTable = new HashTable<int, string>(initialCapacity);
400+
401+
// Act
402+
for (int i = 1; i <= 4; i++) // Start keys from 1 to avoid default(TKey) = 0 issue
403+
{
404+
hashTable.Add(i, $"Value{i}");
405+
}
406+
407+
// Assert
408+
hashTable.Capacity.Should().BeGreaterThan(initialCapacity); // Ensure resizing occurred
409+
hashTable.Count.Should().Be(4); // Verify count reflects number of added items
410+
}
411+
412+
413+
[Test]
414+
public void Add_ThrowsException_WhenKeyIsDefault()
415+
{
416+
// Arrange
417+
var hashTable = new HashTable<int, string>();
418+
419+
// Act & Assert
420+
Action act = () => hashTable.Add(default, "Value");
421+
act.Should().Throw<ArgumentNullException>().WithMessage("*key*");
422+
}
423+
424+
[Test]
425+
public void Add_ThrowsException_WhenValueIsDefault()
426+
{
427+
// Arrange
428+
var hashTable = new HashTable<int, string>();
429+
430+
// Act & Assert
431+
Action act = () => hashTable.Add(1, default);
432+
act.Should().Throw<ArgumentNullException>().WithMessage("*value*");
433+
}
434+
435+
[Test]
436+
public void Add_StoresValueCorrectly()
437+
{
438+
// Arrange
439+
var hashTable = new HashTable<int, string>();
440+
441+
// Act
442+
hashTable.Add(1, "Value1");
443+
444+
// Assert
445+
hashTable[1].Should().Be("Value1");
446+
}
447+
448+
[Test]
449+
public void Get_ReturnsCorrectValue_ForExistingKey()
450+
{
451+
// Arrange
452+
var hashTable = new HashTable<string, int>();
453+
hashTable.Add("key", 42);
454+
455+
// Act
456+
var value = hashTable["key"];
457+
458+
// Assert
459+
value.Should().Be(42);
460+
}
461+
462+
[Test]
463+
public void Get_ThrowsException_WhenKeyDoesNotExist()
464+
{
465+
// Arrange
466+
var hashTable = new HashTable<string, int>();
467+
468+
// Act & Assert
469+
Action act = () => _ = hashTable["nonexistent"];
470+
act.Should().Throw<KeyNotFoundException>();
471+
}
472+
473+
[Test]
474+
public void Capacity_Increases_WhenResizeOccurs()
475+
{
476+
var initialCapacity = 4;
477+
var hashTable = new HashTable<int, string>(initialCapacity);
478+
479+
for (int i = 1; i <= 5; i++)
480+
{
481+
hashTable.Add(i, $"Value{i}");
482+
}
483+
484+
hashTable.Capacity.Should().BeGreaterThan(initialCapacity);
485+
}
392486
}
393487

394488
public class NegativeHashKey

0 commit comments

Comments
 (0)