|
| 1 | +package test.dataStructures.hashSet.openAddressing; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import src.dataStructures.hashSet.openAddressing.HashSet; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Stream; |
| 8 | + |
| 9 | +import static org.junit.Assert.*; |
| 10 | + |
| 11 | +public class HashSetTest { |
| 12 | + @Test |
| 13 | + public void testAdd_noDuplicates_shouldReturnTrue() { |
| 14 | + HashSet<String> hashSet = new HashSet<>(); |
| 15 | + assertTrue(hashSet.add("Hello")); |
| 16 | + assertTrue(hashSet.add("World")); |
| 17 | + } |
| 18 | + @Test |
| 19 | + public void testAdd_withDuplicates_shouldReturnFalse() { |
| 20 | + HashSet<String> hashSet = new HashSet<>(); |
| 21 | + assertTrue(hashSet.add("Hello")); |
| 22 | + assertTrue(hashSet.add("World")); |
| 23 | + assertFalse(hashSet.add("Hello")); // Adding duplicate element should return false |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testContains() { |
| 28 | + HashSet<String> hashSet = new HashSet<>(); |
| 29 | + hashSet.add("Hello"); |
| 30 | + hashSet.add("World"); |
| 31 | + assertTrue(hashSet.contains("Hello")); |
| 32 | + assertTrue(hashSet.contains("World")); |
| 33 | + assertFalse(hashSet.contains("Universe")); // Element not in set |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testRemove() { |
| 38 | + HashSet<String> hashSet = new HashSet<>(); |
| 39 | + hashSet.add("Hello"); |
| 40 | + hashSet.add("World"); |
| 41 | + assertTrue(hashSet.remove("Hello")); |
| 42 | + assertFalse(hashSet.contains("Hello")); // Element should be removed |
| 43 | + assertFalse(hashSet.remove("Universe")); // Removing non-existent element should return false |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testSize() { |
| 48 | + HashSet<String> hashSet = new HashSet<>(); |
| 49 | + assertEquals(0, hashSet.size()); // Initial size should be 0 |
| 50 | + hashSet.add("Hello"); |
| 51 | + assertEquals(1, hashSet.size()); // Size after adding one element |
| 52 | + hashSet.add("World"); |
| 53 | + assertEquals(2, hashSet.size()); // Size after adding two elements |
| 54 | + hashSet.remove("Hello"); |
| 55 | + assertEquals(1, hashSet.size()); // Size after removing one element |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testIsEmpty() { |
| 60 | + HashSet<String> hashSet = new HashSet<>(); |
| 61 | + assertTrue(hashSet.isEmpty()); // Initial set should be empty |
| 62 | + hashSet.add("Hello"); |
| 63 | + assertFalse(hashSet.isEmpty()); // Set should not be empty after adding an element |
| 64 | + hashSet.remove("Hello"); |
| 65 | + assertTrue(hashSet.isEmpty()); // Set should be empty after removing the only element |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testContains_afterRemove() { |
| 70 | + HashSet<Integer> hashSet = new HashSet<>(); |
| 71 | + Stream.iterate(0, i -> i + 1) // Populates the hashSet. |
| 72 | + .limit(16) |
| 73 | + .forEach(hashSet::add); |
| 74 | + hashSet.remove(4); |
| 75 | + assertTrue(hashSet.add(25)); // add should insert 25 at where 4 was at previously. |
| 76 | + hashSet.remove(10); // Introduce a tombstone in the probe sequence for 25. |
| 77 | + |
| 78 | + assertTrue(hashSet.contains(25)); // contains should still find 25. |
| 79 | + } |
| 80 | +} |
0 commit comments