diff --git a/build.gradle b/build.gradle index cc7797e..46998bf 100644 --- a/build.gradle +++ b/build.gradle @@ -8,3 +8,11 @@ version '1.0-SNAPSHOT' repositories { mavenCentral() } + +dependencies { + implementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' +} + +test { + useJUnitPlatform() +} diff --git a/src/test/java/HashSetTest.java b/src/test/java/HashSetTest.java new file mode 100644 index 0000000..c63b615 --- /dev/null +++ b/src/test/java/HashSetTest.java @@ -0,0 +1,31 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class HashSetTest { + + @Test + public void testIntegersNoCollisions() { + HashSet set = new HashSet<>(10); + set.add(1); + set.add(2); + assertTrue(set.contains(1)); + assertTrue(set.contains(2)); + assertFalse(set.contains(0)); + // hashCode() of an int is that int + assertEquals(0, set.getNumCollisions()); + } + + @Test + public void testIntegersWithCollisions() { + HashSet set = new HashSet<>(10); + set.add(1); + set.add(2); + set.add(11); + assertTrue(set.contains(1)); + assertTrue(set.contains(2)); + assertTrue(set.contains(11)); // collides with 1 + assertFalse(set.contains(0)); + assertEquals(1, set.getNumCollisions()); + } +}