Skip to content

Commit 41a421f

Browse files
author
GersonJr
committed
Added tests for HashTable to improve coverage of key behaviors
Add_ShouldTriggerResize_WhenThresholdExceeded: Verifies that the HashTable triggers a resize when the number of elements exceeds the initial capacity. Add_ThrowsException_WhenKeyIsDefault: Ensures that adding a key with the default value (e.g., 0 for integers) throws an ArgumentNullException, as per the Add method’s validation. Add_ThrowsException_WhenValueIsDefault: Verifies that adding a value with the default value (e.g., null for reference types) throws an ArgumentNullException. Add_StoresValueCorrectly: Confirms that values are correctly stored and can be retrieved via the key after an Add operation. Get_ReturnsCorrectValue_ForExistingKey: Ensures that the correct value is returned when querying for an existing key. Get_ThrowsException_WhenKeyDoesNotExist: Verifies that accessing a non-existent key throws a KeyNotFoundException, maintaining expected behavior. Capacity_Increases_WhenResizeOccurs: Checks that the HashTable increases its capacity when the number of items exceeds the threshold, ensuring proper resizing. These tests aim to cover edge cases, resizing logic, and ensure correct behavior of the Add and Get methods, improving overall robustness and stability.
1 parent 8fd2e67 commit 41a421f

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

DataStructures.Tests/Hashing/HashTableTests.cs

Lines changed: 94 additions & 0 deletions
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)