1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 22:32:07 +01:00

Rework MultiMapTest

This commit is contained in:
Paul Schaub 2021-07-03 13:20:25 +02:00
parent 43a21de53a
commit 70666d276b
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -21,27 +21,28 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class MultiMapTest { public class MultiMapTest {
@Test @Test
public void test() { public void isEmptyAfterCreation() {
MultiMap<String, String> map = new MultiMap<>();
assertTrue(map.isEmpty());
assertNull(map.get("alice"));
assertFalse(map.containsKey("alice"));
assertFalse(map.containsValue("wonderland"));
assertEquals(0, map.size());
}
@Test
public void addOneElement_works() {
MultiMap<String, String> multiMap = new MultiMap<>(); MultiMap<String, String> multiMap = new MultiMap<>();
assertTrue(multiMap.isEmpty());
assertNull(multiMap.get("alice"));
assertFalse(multiMap.containsKey("alice"));
assertFalse(multiMap.containsValue("wonderland"));
assertEquals(0, multiMap.size());
multiMap.put("alice", "wonderland"); multiMap.put("alice", "wonderland");
assertFalse(multiMap.isEmpty()); assertFalse(multiMap.isEmpty());
@ -50,84 +51,120 @@ public class MultiMapTest {
assertTrue(multiMap.containsValue("wonderland")); assertTrue(multiMap.containsValue("wonderland"));
assertNotNull(multiMap.get("alice")); assertNotNull(multiMap.get("alice"));
assertTrue(multiMap.get("alice").contains("wonderland")); assertTrue(multiMap.get("alice").contains("wonderland"));
}
@Test
public void addTwoKeys_OneWithTwoValues_works() {
MultiMap<String, String> multiMap = new MultiMap<>();
multiMap.put("alice", "wonderland");
multiMap.put("mad", new HashSet<>(Arrays.asList("hatter", "max"))); multiMap.put("mad", new HashSet<>(Arrays.asList("hatter", "max")));
assertEquals(2, multiMap.size());
assertEquals(new HashSet<>(Arrays.asList("alice", "mad")), multiMap.keySet());
assertEquals(new HashSet<>(Arrays.asList("hatter", "max")), multiMap.get("mad")); assertEquals(new HashSet<>(Arrays.asList("hatter", "max")), multiMap.get("mad"));
assertEquals(
assertEquals(new HashSet<>(Arrays.asList("mad", "alice")), multiMap.keySet()); new HashSet<>(Arrays.asList(
assertEquals(new HashSet<>(Arrays.asList(
Collections.singleton("wonderland"), Collections.singleton("wonderland"),
new HashSet<>(Arrays.asList("hatter", "max")))), new HashSet<>(multiMap.values())); new HashSet<>(Arrays.asList("hatter", "max")
))),
new HashSet<>(multiMap.values()));
Set<Map.Entry<String, Set<String>>> entries = multiMap.entrySet(); assertEquals(Collections.singleton("wonderland"), multiMap.get("alice"));
assertEquals(2, entries.size()); assertEquals(new HashSet<>(Arrays.asList("hatter", "max")), multiMap.get("mad"));
for (Map.Entry<String, Set<String>> e : entries) {
switch (e.getKey()) {
case "alice":
assertEquals(1, e.getValue().size());
assertTrue(e.getValue().contains("wonderland"));
break;
case "mad":
assertEquals(2, e.getValue().size());
assertTrue(e.getValue().contains("hatter"));
assertTrue(e.getValue().contains("max"));
break;
default:
fail("Illegal key.");
break;
}
} }
MultiMap<String, String> empty = new MultiMap<>(); @Test
assertNotEquals(multiMap, empty); public void emptyEqualsEmptyTest() {
assertEquals(multiMap, multiMap); MultiMap<String, String> emptyOne = new MultiMap<>();
assertNotEquals(null, multiMap); MultiMap<String, String> emptyTwo = new MultiMap<>();
assertEquals(emptyOne, emptyTwo);
}
MultiMap<String, String> map2 = new MultiMap<>(); @Test
map2.put("alice", "schwarzer"); public void notEqualsNull() {
map2.put("dr", "strange"); MultiMap<String, String> map = new MultiMap<>();
assertNotEquals(map, null);
}
multiMap.putAll(map2); @Test
assertTrue(multiMap.containsKey("dr")); public void selfEquals() {
assertEquals(1, multiMap.get("dr").size()); MultiMap<String, String> map = new MultiMap<>();
assertTrue(multiMap.get("dr").contains("strange")); assertEquals(map, map);
assertTrue(multiMap.containsKey("mad")); }
assertEquals(2, multiMap.get("alice").size());
assertTrue(multiMap.get("alice").contains("wonderland"));
assertTrue(multiMap.get("alice").contains("schwarzer"));
multiMap.removeAll("mad"); @Test
assertFalse(multiMap.containsKey("mad")); public void otherClassNotEquals() {
assertNull(multiMap.get("mad")); MultiMap<String, String> map = new MultiMap<>();
assertNotEquals(map, "String");
}
multiMap.remove("alice", "wonderland"); @Test
assertFalse(multiMap.containsValue("wonderland")); public void mapEqualsCopy() {
assertTrue(multiMap.containsKey("alice")); MultiMap<String, String> map = new MultiMap<>();
assertEquals(1, multiMap.get("alice").size()); map.put("foo", "bar");
assertTrue(multiMap.get("alice").contains("schwarzer")); map.put("entries", new HashSet<>(Arrays.asList("one", "two")));
MultiMap<String, String> copy = new MultiMap<>(multiMap); MultiMap<String, String> copy = new MultiMap<>(map);
assertEquals(multiMap, copy);
copy.removeAll("inexistent"); assertEquals(map, copy);
assertEquals(multiMap, copy); }
copy.remove("inexistent", "schwarzer"); @Test
assertEquals(multiMap, copy); public void emptyAfterClear() {
MultiMap<String, String> map = new MultiMap<>();
map.put("test", "foo");
assertFalse(map.isEmpty());
map.clear();
assertTrue(map.isEmpty());
}
assertEquals(multiMap.hashCode(), copy.hashCode()); @Test
public void addTwoRemoveOneWorks() {
MultiMap<String, String> map = new MultiMap<>();
map.put("alice", "wonderland");
map.put("bob", "builder");
map.removeAll("alice");
copy.clear(); assertFalse(map.containsKey("alice"));
assertTrue(copy.isEmpty()); assertNull(map.get("alice"));
assertFalse(map.isEmpty());
}
Map<String, Set<String>> map = new HashMap<>(); @Test
map.put("key", Collections.singleton("value")); public void addMultiValue() {
MultiMap<String, String> addOneByOne = new MultiMap<>();
addOneByOne.put("foo", "bar");
addOneByOne.put("foo", "baz");
MultiMap<String, String> fromMap = new MultiMap<>(map); MultiMap<String, String> addOnce = new MultiMap<>();
assertFalse(fromMap.isEmpty()); addOnce.put("foo", new HashSet<>(Arrays.asList("baz", "bar")));
assertEquals(fromMap.get("key"), Collections.singleton("value"));
assertNotEquals(fromMap, map); assertEquals(addOneByOne, addOnce);
assertNotEquals(fromMap, null); }
@Test
public void addMultiValueRemoveSingle() {
MultiMap<String, String> map = new MultiMap<>();
map.put("foo", "bar");
map.put("foo", "baz");
map.remove("foo", "bar");
assertFalse(map.isEmpty());
assertTrue(map.containsKey("foo"));
assertEquals(Collections.singleton("baz"), map.get("foo"));
}
@Test
public void addMultiValueRemoveAll() {
MultiMap<String, String> map = new MultiMap<>();
map.put("foo", "bar");
map.put("foo", "baz");
map.put("bingo", "bango");
map.removeAll("foo");
assertFalse(map.isEmpty());
assertFalse(map.containsKey("foo"));
assertTrue(map.containsKey("bingo"));
} }
} }