Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class GenericHashMapUsingArrayTest {

Expand Down Expand Up @@ -93,4 +97,143 @@ void testContainsKey() {
assertTrue(map.containsKey("USA"));
assertFalse(map.containsKey("Nepal"));
}

// ======= Added tests from the new version =======

@Test
void shouldThrowNullPointerExceptionForNullKey() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
assertThrows(NullPointerException.class, () -> map.put(null, "value"));
}

@Test
void shouldStoreNullValueForKey() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put("keyWithNullValue", null);
assertEquals(1, map.size());
assertNull(map.get("keyWithNullValue"));
// Note: containsKey returns false for null values due to implementation
assertFalse(map.containsKey("keyWithNullValue"));
}

@Test
void shouldHandleCollisionWhenKeysHashToSameBucket() {
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
Integer key1 = 1;
Integer key2 = 17;
map.put(key1, 100);
map.put(key2, 200);
assertEquals(2, map.size());
assertEquals(100, map.get(key1));
assertEquals(200, map.get(key2));
assertTrue(map.containsKey(key1));
assertTrue(map.containsKey(key2));
}

@Test
void shouldHandleEmptyStringAsKey() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put("", "valueForEmptyKey");
assertEquals(1, map.size());
assertEquals("valueForEmptyKey", map.get(""));
assertTrue(map.containsKey(""));
}

@Test
void shouldHandleEmptyStringAsValue() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put("keyForEmptyValue", "");
assertEquals(1, map.size());
assertEquals("", map.get("keyForEmptyValue"));
assertTrue(map.containsKey("keyForEmptyValue"));
}

@Test
void shouldHandleNegativeIntegerKeys() {
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
map.put(-1, 100);
map.put(-100, 200);
assertEquals(2, map.size());
assertEquals(100, map.get(-1));
assertEquals(200, map.get(-100));
assertTrue(map.containsKey(-1));
assertTrue(map.containsKey(-100));
}

@Test
void shouldHandleZeroAsKey() {
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
map.put(0, 100);
assertEquals(1, map.size());
assertEquals(100, map.get(0));
assertTrue(map.containsKey(0));
}

@Test
void shouldHandleStringWithSpecialCharacters() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put("key!@#$%^&*()", "value<>?/\\|");
assertEquals(1, map.size());
assertEquals("value<>?/\\|", map.get("key!@#$%^&*()"));
assertTrue(map.containsKey("key!@#$%^&*()"));
}

@Test
void shouldHandleLongStrings() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
StringBuilder longKey = new StringBuilder();
StringBuilder longValue = new StringBuilder();
for (int i = 0; i < 1000; i++) {
longKey.append("a");
longValue.append("b");
}
String key = longKey.toString();
String value = longValue.toString();
map.put(key, value);
assertEquals(1, map.size());
assertEquals(value, map.get(key));
assertTrue(map.containsKey(key));
}

@ParameterizedTest
@ValueSource(strings = {"a", "ab", "abc", "test", "longerString"})
void shouldHandleKeysOfDifferentLengths(String key) {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put(key, "value");
assertEquals(1, map.size());
assertEquals("value", map.get(key));
assertTrue(map.containsKey(key));
}

@Test
void shouldHandleUpdateOnExistingKeyInCollisionBucket() {
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
Integer key1 = 1;
Integer key2 = 17;
map.put(key1, 100);
map.put(key2, 200);
assertEquals(2, map.size());
map.put(key2, 999);
assertEquals(2, map.size());
assertEquals(100, map.get(key1));
assertEquals(999, map.get(key2));
assertTrue(map.containsKey(key1));
assertTrue(map.containsKey(key2));
}

@Test
void shouldHandleExactlyLoadFactorBoundary() {
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
// Fill exactly to load factor (12 items with capacity 16 and 0.75 load factor)
for (int i = 0; i < 12; i++) {
map.put(i, i * 10);
}
assertEquals(12, map.size());
// Act - This should trigger rehash on 13th item
map.put(12, 120);
// Assert - Rehash should have happened
assertEquals(13, map.size());
assertEquals(120, map.get(12));
assertTrue(map.containsKey(12));
}
}
Loading