Free as in Freedom: Codeberg.org. Create your repos!
Browse Source

Add MultiMapTest

tags/0.0.1-alpha5
Paul Schaub 8 months ago
parent
commit
4a45b9d114
Signed by: Paul Schaub <vanitasvitae@fsfe.org> GPG Key ID: 62BEE9264BF17311

+ 3
- 8
pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java View File

@@ -89,14 +89,9 @@ public class MultiMap<K, V> {
89 89
         vs.remove(v);
90 90
     }
91 91
 
92
-    public void putAll(Map<? extends K, ? extends Set<V>> _map) {
93
-        for (K key : _map.keySet()) {
94
-            Set<V> vs = this.map.get(key);
95
-            if (vs == null) {
96
-                vs = new HashSet<>();
97
-                this.map.put(key, vs);
98
-            }
99
-            vs.addAll(_map.get(key));
92
+    public void putAll(MultiMap<K, V> other) {
93
+        for (K key : other.keySet()) {
94
+            put(key, other.get(key));
100 95
         }
101 96
     }
102 97
 

+ 132
- 0
pgpainless-core/src/test/java/org/pgpainless/MultiMapTest.java View File

@@ -0,0 +1,132 @@
1
+/*
2
+ * Copyright 2018 Paul Schaub.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+package org.pgpainless;
17
+
18
+import static junit.framework.TestCase.assertEquals;
19
+import static junit.framework.TestCase.assertFalse;
20
+import static junit.framework.TestCase.assertNotNull;
21
+import static junit.framework.TestCase.assertNull;
22
+import static junit.framework.TestCase.assertTrue;
23
+import static junit.framework.TestCase.fail;
24
+
25
+import java.util.Arrays;
26
+import java.util.Collections;
27
+import java.util.HashMap;
28
+import java.util.HashSet;
29
+import java.util.Map;
30
+import java.util.Set;
31
+
32
+import org.junit.Test;
33
+import org.pgpainless.util.MultiMap;
34
+
35
+public class MultiMapTest {
36
+
37
+    @Test
38
+    public void test() {
39
+        MultiMap<String, String> multiMap = new MultiMap<>();
40
+        assertTrue(multiMap.isEmpty());
41
+        assertNull(multiMap.get("alice"));
42
+        assertFalse(multiMap.containsKey("alice"));
43
+        assertFalse(multiMap.containsValue("wonderland"));
44
+        assertEquals(0, multiMap.size());
45
+
46
+        multiMap.put("alice", "wonderland");
47
+        assertFalse(multiMap.isEmpty());
48
+        assertEquals(1, multiMap.size());
49
+        assertTrue(multiMap.containsKey("alice"));
50
+        assertTrue(multiMap.containsValue("wonderland"));
51
+        assertNotNull(multiMap.get("alice"));
52
+        assertTrue(multiMap.get("alice").contains("wonderland"));
53
+
54
+        multiMap.put("mad", new HashSet<>(Arrays.asList("hatter", "max")));
55
+        assertEquals(new HashSet<>(Arrays.asList("hatter", "max")), multiMap.get("mad"));
56
+
57
+        assertEquals(new HashSet<>(Arrays.asList("mad", "alice")), multiMap.keySet());
58
+        assertEquals(new HashSet<>(Arrays.asList(
59
+                Collections.singleton("wonderland"),
60
+                new HashSet<>(Arrays.asList("hatter", "max")))), new HashSet<>(multiMap.values()));
61
+
62
+        Set<Map.Entry<String, Set<String>>> entries = multiMap.entrySet();
63
+        assertEquals(2, entries.size());
64
+        for (Map.Entry<String, Set<String>> e : entries) {
65
+            switch (e.getKey()) {
66
+                case "alice":
67
+                    assertEquals(1, e.getValue().size());
68
+                    assertTrue(e.getValue().contains("wonderland"));
69
+                    break;
70
+                case "mad":
71
+                    assertEquals(2, e.getValue().size());
72
+                    assertTrue(e.getValue().contains("hatter"));
73
+                    assertTrue(e.getValue().contains("max"));
74
+                    break;
75
+                default:
76
+                    fail("Illegal key.");
77
+                    break;
78
+            }
79
+        }
80
+
81
+        MultiMap<String, String> empty = new MultiMap<>();
82
+        assertFalse(multiMap.equals(empty));
83
+        assertEquals(multiMap, multiMap);
84
+        assertFalse(multiMap.equals(null));
85
+
86
+        MultiMap<String, String> map2 = new MultiMap<>();
87
+        map2.put("alice", "schwarzer");
88
+        map2.put("dr", "strange");
89
+
90
+        multiMap.putAll(map2);
91
+        assertTrue(multiMap.containsKey("dr"));
92
+        assertEquals(1, multiMap.get("dr").size());
93
+        assertTrue(multiMap.get("dr").contains("strange"));
94
+        assertTrue(multiMap.containsKey("mad"));
95
+        assertEquals(2, multiMap.get("alice").size());
96
+        assertTrue(multiMap.get("alice").contains("wonderland"));
97
+        assertTrue(multiMap.get("alice").contains("schwarzer"));
98
+
99
+        multiMap.removeAll("mad");
100
+        assertFalse(multiMap.containsKey("mad"));
101
+        assertNull(multiMap.get("mad"));
102
+
103
+        multiMap.remove("alice", "wonderland");
104
+        assertFalse(multiMap.containsValue("wonderland"));
105
+        assertTrue(multiMap.containsKey("alice"));
106
+        assertEquals(1, multiMap.get("alice").size());
107
+        assertTrue(multiMap.get("alice").contains("schwarzer"));
108
+
109
+        MultiMap<String, String> copy = new MultiMap<>(multiMap);
110
+        assertEquals(multiMap, copy);
111
+
112
+        copy.removeAll("inexistent");
113
+        assertEquals(multiMap, copy);
114
+
115
+        copy.remove("inexistent", "schwarzer");
116
+        assertEquals(multiMap, copy);
117
+
118
+        assertEquals(multiMap.hashCode(), copy.hashCode());
119
+
120
+        copy.clear();
121
+        assertTrue(copy.isEmpty());
122
+
123
+        Map<String, Set<String>> map = new HashMap<>();
124
+        map.put("key", Collections.singleton("value"));
125
+
126
+        MultiMap<String, String> fromMap = new MultiMap<>(map);
127
+        assertFalse(fromMap.isEmpty());
128
+        assertEquals(fromMap.get("key"), Collections.singleton("value"));
129
+
130
+        assertFalse(fromMap.equals(map));
131
+    }
132
+}

Loading…
Cancel
Save