mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-26 22:32:07 +01:00
Rework MultiMapTest
This commit is contained in:
parent
43a21de53a
commit
70666d276b
1 changed files with 108 additions and 71 deletions
|
@ -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(
|
||||||
|
new HashSet<>(Arrays.asList(
|
||||||
|
Collections.singleton("wonderland"),
|
||||||
|
new HashSet<>(Arrays.asList("hatter", "max")
|
||||||
|
))),
|
||||||
|
new HashSet<>(multiMap.values()));
|
||||||
|
|
||||||
assertEquals(new HashSet<>(Arrays.asList("mad", "alice")), multiMap.keySet());
|
assertEquals(Collections.singleton("wonderland"), multiMap.get("alice"));
|
||||||
assertEquals(new HashSet<>(Arrays.asList(
|
assertEquals(new HashSet<>(Arrays.asList("hatter", "max")), multiMap.get("mad"));
|
||||||
Collections.singleton("wonderland"),
|
}
|
||||||
new HashSet<>(Arrays.asList("hatter", "max")))), new HashSet<>(multiMap.values()));
|
|
||||||
|
|
||||||
Set<Map.Entry<String, Set<String>>> entries = multiMap.entrySet();
|
@Test
|
||||||
assertEquals(2, entries.size());
|
public void emptyEqualsEmptyTest() {
|
||||||
for (Map.Entry<String, Set<String>> e : entries) {
|
MultiMap<String, String> emptyOne = new MultiMap<>();
|
||||||
switch (e.getKey()) {
|
MultiMap<String, String> emptyTwo = new MultiMap<>();
|
||||||
case "alice":
|
assertEquals(emptyOne, emptyTwo);
|
||||||
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 notEqualsNull() {
|
||||||
assertEquals(multiMap, multiMap);
|
MultiMap<String, String> map = new MultiMap<>();
|
||||||
assertNotEquals(null, multiMap);
|
assertNotEquals(map, null);
|
||||||
|
}
|
||||||
|
|
||||||
MultiMap<String, String> map2 = new MultiMap<>();
|
@Test
|
||||||
map2.put("alice", "schwarzer");
|
public void selfEquals() {
|
||||||
map2.put("dr", "strange");
|
MultiMap<String, String> map = new MultiMap<>();
|
||||||
|
assertEquals(map, map);
|
||||||
|
}
|
||||||
|
|
||||||
multiMap.putAll(map2);
|
@Test
|
||||||
assertTrue(multiMap.containsKey("dr"));
|
public void otherClassNotEquals() {
|
||||||
assertEquals(1, multiMap.get("dr").size());
|
MultiMap<String, String> map = new MultiMap<>();
|
||||||
assertTrue(multiMap.get("dr").contains("strange"));
|
assertNotEquals(map, "String");
|
||||||
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 mapEqualsCopy() {
|
||||||
assertNull(multiMap.get("mad"));
|
MultiMap<String, String> map = new MultiMap<>();
|
||||||
|
map.put("foo", "bar");
|
||||||
|
map.put("entries", new HashSet<>(Arrays.asList("one", "two")));
|
||||||
|
|
||||||
multiMap.remove("alice", "wonderland");
|
MultiMap<String, String> copy = new MultiMap<>(map);
|
||||||
assertFalse(multiMap.containsValue("wonderland"));
|
|
||||||
assertTrue(multiMap.containsKey("alice"));
|
|
||||||
assertEquals(1, multiMap.get("alice").size());
|
|
||||||
assertTrue(multiMap.get("alice").contains("schwarzer"));
|
|
||||||
|
|
||||||
MultiMap<String, String> copy = new MultiMap<>(multiMap);
|
assertEquals(map, copy);
|
||||||
assertEquals(multiMap, copy);
|
}
|
||||||
|
|
||||||
copy.removeAll("inexistent");
|
@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());
|
||||||
|
}
|
||||||
|
|
||||||
copy.remove("inexistent", "schwarzer");
|
@Test
|
||||||
assertEquals(multiMap, copy);
|
public void addTwoRemoveOneWorks() {
|
||||||
|
MultiMap<String, String> map = new MultiMap<>();
|
||||||
|
map.put("alice", "wonderland");
|
||||||
|
map.put("bob", "builder");
|
||||||
|
map.removeAll("alice");
|
||||||
|
|
||||||
assertEquals(multiMap.hashCode(), copy.hashCode());
|
assertFalse(map.containsKey("alice"));
|
||||||
|
assertNull(map.get("alice"));
|
||||||
|
assertFalse(map.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
copy.clear();
|
@Test
|
||||||
assertTrue(copy.isEmpty());
|
public void addMultiValue() {
|
||||||
|
MultiMap<String, String> addOneByOne = new MultiMap<>();
|
||||||
|
addOneByOne.put("foo", "bar");
|
||||||
|
addOneByOne.put("foo", "baz");
|
||||||
|
|
||||||
Map<String, Set<String>> map = new HashMap<>();
|
MultiMap<String, String> addOnce = new MultiMap<>();
|
||||||
map.put("key", Collections.singleton("value"));
|
addOnce.put("foo", new HashSet<>(Arrays.asList("baz", "bar")));
|
||||||
|
|
||||||
MultiMap<String, String> fromMap = new MultiMap<>(map);
|
assertEquals(addOneByOne, addOnce);
|
||||||
assertFalse(fromMap.isEmpty());
|
}
|
||||||
assertEquals(fromMap.get("key"), Collections.singleton("value"));
|
|
||||||
|
|
||||||
assertNotEquals(fromMap, map);
|
@Test
|
||||||
assertNotEquals(fromMap, null);
|
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"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue