Skip to content

Commit 006dda0

Browse files
committed
Add unit test for HashSet add operation after remove
1 parent c3c16bb commit 006dda0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

test/dataStructures/hashSet/openAddressing/HashSetTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import src.dataStructures.hashSet.openAddressing.HashSet;
55

66
import java.util.List;
7+
import java.util.stream.Collectors;
78
import java.util.stream.Stream;
89

910
import static org.junit.Assert.*;
@@ -109,4 +110,25 @@ public void testResize() {
109110
// Verify that the HashSet has resized and halved its capacity back to 16.
110111
assertEquals(16, set.capacity());
111112
}
113+
114+
@Test
115+
public void testAdd_afterRemove() {
116+
HashSet<Integer> hashSet = new HashSet<>();
117+
// these elements all map to the same initial bucket, resulting in collisions.
118+
hashSet.add(1);
119+
hashSet.add(17);
120+
hashSet.add(33);
121+
// the hashSet will look like {1, 17, 33, ...} after the series of adds
122+
123+
hashSet.remove(17);
124+
// hashSet now looks like {1, X, 33, ...} where X denotes a tombstone.
125+
126+
boolean isAdded = hashSet.add(33); // this should not be added into the hashSet.
127+
assertFalse(isAdded);
128+
129+
List<Integer> expectedList = List.of(1, 33);
130+
List<Integer> actualList = hashSet.toList();
131+
132+
assertEquals(expectedList, actualList);
133+
}
112134
}

0 commit comments

Comments
 (0)