diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-17 20:31:59 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-17 20:31:59 -0500 |
| commit | 29d512725271b51eee18232e0bff8d8dc2868d46 (patch) | |
| tree | 3388d1482087ff4504639055f07b05ae0321dac9 /src/test/java/bjc/funcdata | |
| parent | f133f377eb5bf86a26f9dae6f6cea445419e5786 (diff) | |
Add some tests for IMap/FunctionalMap
Diffstat (limited to 'src/test/java/bjc/funcdata')
| -rw-r--r-- | src/test/java/bjc/funcdata/TestMapCreation.java | 38 | ||||
| -rw-r--r-- | src/test/java/bjc/funcdata/TestMapOperations.java | 164 |
2 files changed, 202 insertions, 0 deletions
diff --git a/src/test/java/bjc/funcdata/TestMapCreation.java b/src/test/java/bjc/funcdata/TestMapCreation.java new file mode 100644 index 0000000..26f09fc --- /dev/null +++ b/src/test/java/bjc/funcdata/TestMapCreation.java @@ -0,0 +1,38 @@ +package bjc.funcdata; + +import static org.junit.Assert.*; + +import org.junit.*; + +@SuppressWarnings("javadoc") +public class TestMapCreation { + @Test + public void mapOfNothingCreatesEmptyMap() { + IMap<String, String> map = IMap.of(); + + assertEquals("Map is empty", 0, map.size()); + } + + @Test(expected = IllegalArgumentException.class) + public void mapOfMismatchedCountErrors() { + @SuppressWarnings("unused") + IMap<String, String> map = IMap.of("thing1"); + } + + @Test(expected = ClassCastException.class) + public void mapOfMismatchedTypeErrors() { + IMap<String, String> map = IMap.of(1, 1.0); + + map.forEach((key, val) -> { + // An exception will be thrown here + }); + } + + @Test + public void mapOfCreatesWithGivenContents() { + IMap<String, String> map = IMap.of("a", "A", "b", "B"); + + assertEquals("Constructed map contains key 'a'", "a", map.containsKey("a")); + assertEquals("Constructed map has key 'a' mapped to value 'A'", "A", map.get("A")); + } +} diff --git a/src/test/java/bjc/funcdata/TestMapOperations.java b/src/test/java/bjc/funcdata/TestMapOperations.java new file mode 100644 index 0000000..7dd3340 --- /dev/null +++ b/src/test/java/bjc/funcdata/TestMapOperations.java @@ -0,0 +1,164 @@ +package bjc.funcdata; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.junit.*; + +@SuppressWarnings("javadoc") +public class TestMapOperations { + private IMap<String, String> map; + + @Before + public void setUp() throws Exception { + map = IMap.of("a", "A", "b", "B"); + } + + @Test + public void sizeMatchesExpected() { + assertEquals("Constructed map is of size 2", 2, map.size()); + } + + @Test + public void containsExpectedKey() { + assertTrue("Constructed map contains key 'a'", map.containsKey("a")); + } + + @Test + public void getYieldsExpectedValue() { + assertEquals("Constructed map has key 'a' mapped to value 'A'", "A", map.get("a")); + } + + @Test + public void doesNotContainNotAddedKey() { + assertFalse("Constructed map doesn't contain key 'c'", map.containsKey("c")); + } + + @Test(expected = IllegalArgumentException.class) + public void getOfNonexistentKeyThrows() { + map.get("c"); + } + + @Test + public void getOrDefaultOfExistingKeyYieldsExistingValue() { + assertEquals("Constructed map getOrDefault for an existing key yields the existing value", "A", map.get("a")); + } + + @Test + public void getOrDefaultOfNonExistingKeyYieldsDefault() { + assertEquals("Constructed map getOrDefault for a non-existing key yields the default", "C", map.getOrDefault("c", "C")); + } + + @Test + public void putOfNonExistingKeyAddsValue() { + map.put("c", "C"); + + assertEquals("Constructed map now has 3 items", 3, map.size()); + assertEquals("Constructed map now has 'c' mapped to 'C'", "C", map.get("c")); + } + + @Test + public void putOfExistingKeyUpdatesValue() { + String val = map.put("a", "D"); + + assertEquals("Constructed map still contains 2 items", 2, map.size()); + assertEquals("Constructed map now has 'a' mapped to 'D'", "D", map.get("a")); + assertEquals("put method returned old value of 'A'", "A", val); + } + + @Test + public void forEachGetsExpectedElements() { + List<String> result = new ArrayList<>(); + + map.forEach((key, value) -> { + result.add(key + " = " + value); + }); + + assertArrayEquals("For-each has the expected elements", + new String[] {"a = A", "b = B"}, + result.toArray()); + } + + @Test + public void forEachKeyGetsAddedKeys() { + List<String> keys = new ArrayList<>(); + + map.forEachKey((key) -> { + keys.add(key); + }); + + assertArrayEquals("forEachKey gives the expected keys", + new String[] {"a", "b"}, + keys.toArray()); + } + + @Test + public void forEachValueGetsAddedValues() { + List<String> keys = new ArrayList<>(); + + map.forEachValue((key) -> { + keys.add(key); + }); + + assertArrayEquals("forEachKey gives the expected values", + new String[] {"A", "B"}, + keys.toArray()); + } + + @Test + public void clearRemovesAllValues() { + map.clear(); + + assertEquals("A cleared map contains no items", 0, map.size()); + } + + @Test + public void removeOfExistingKeyRemovesKey() { + String removed = map.remove("a"); + + assertEquals("Constructed map now has one less element", 1, map.size()); + assertFalse("Constructed map no longer contains a removed key", map.containsKey("a")); + assertEquals("Remove returns the removed value", "A", removed); + } + + @Test + public void removeOfNonExistingKeyDoesntRemoveAnything() { + String removed = map.remove("c"); + + assertEquals("Constructed map still contains 2 elements", 2, map.size()); + assertNull("remove of a non-existing key returns null", removed); + } + + @Test + public void keyListReturnsListOfKeys() { + assertArrayEquals("Constructed map key-list has the expected elements", + new Object[] {"a", "b"}, + map.keyList().toArray(new String[0])); + } + + @Test + public void valueListReturnsListOfValues() { + assertArrayEquals("Constructed map value-list has the expected elements", + new Object[] {"A", "B"}, + map.valueList().toArray(new String[0])); + } + + @Test + public void mapIsThawedByDefault() { + assertFalse("isFrozen is false by default for a map", map.isFrozen()); + } + + @Test + public void canFreezeMap() { + assertTrue("canFreeze is true for a map", map.canFreeze()); + assertTrue("freeze freezes a map", map.freeze()); + assertTrue("isFrozen indicates a map is frozen", map.isFrozen()); + } + + @Test(expected = ObjectFrozen.class) + public void clearOfFrozenMapFails() { + map.freeze(); + map.clear(); + } +} |
