You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Unit Test for Handling Negative Hash Codes in Custom HashTable Implementation
This commit introduces a new unit test and a supporting class to validate the handling of negative hash codes within our custom HashTable implementation.
Changes:
Unit Test: Test_NegativeHashKey_ReturnsCorrectValue
Purpose: The test ensures that the HashTable correctly handles keys with negative hash codes. This scenario is important for robustness, as real-world use cases might involve hash codes that are negative, especially when custom GetHashCode implementations are involved.
Implementation:
A new HashTable is instantiated with a small initial capacity (4) to ensure hash collisions and proper management of entries.
The test adds a key-value pair to the HashTable where the key (NegativeHashKey) intentionally generates a negative hash code.
The test then asserts that the value can be correctly retrieved using a key that generates the same negative hash code, verifying the integrity of the HashTable under these conditions.
Supporting Class: NegativeHashKey
Purpose: The NegativeHashKey class is designed to simulate keys that produce negative hash codes, which is essential for triggering the edge case being tested.
Implementation:
The class contains an integer id used to generate a negative hash code by returning the negation of id in the GetHashCode method.
The Equals method is overridden to ensure correct key comparison based on the id field, allowing the HashTable to manage and compare instances of NegativeHashKey accurately.
0 commit comments