-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapModifyingTest.java
More file actions
292 lines (198 loc) · 6.84 KB
/
Copy pathMapModifyingTest.java
File metadata and controls
292 lines (198 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import org.junit.Test;
import java.util.*;
import static java.util.Objects.isNull;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
/**
* Created by mtumilowicz on 2018-11-09.
*/
public class MapModifyingTest {
@Test
public void putIfAbsent_exists() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "1");
map.putIfAbsent(1, "2");
assertThat(map.get(1), is("1"));
}
@Test
public void putIfAbsent_notExists() {
Map<Integer, String> map = new HashMap<>();
map.putIfAbsent(1, "1");
assertThat(map.get(1), is("1"));
}
@Test
public void putIfAbsent_mappedToNull() {
Map<Integer, String> map = new HashMap<>();
map.put(1, null);
map.putIfAbsent(1, "1");
assertThat(map.get(1), is("1"));
}
@Test
public void remove_sameKey_differentValue() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "1");
map.remove(1, "2");
assertThat(map.get(1), is("1"));
}
@Test
public void remove_sameKey_sameValue() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "1");
map.remove(1, "1");
assertTrue(map.isEmpty());
}
@Test
public void replaceAll() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "1");
map.put(2, "2");
map.put(3, "3");
map.replaceAll((k, v) -> v + "-updated");
assertThat(map.get(1), is("1-updated"));
assertThat(map.get(2), is("2-updated"));
assertThat(map.get(3), is("3-updated"));
}
@Test
public void replace_key_value_newValue_found() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "old");
map.replace(1, "old", "replaced");
assertThat(map.get(1), is("replaced"));
}
@Test
public void replace_key_value_newValue_notFound() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "1");
map.replace(1, "2", "replaced");
assertThat(map.get(1), is("1"));
}
@Test
public void replace_key_value_newValue_oldNull() {
Map<Integer, String> map = new HashMap<>();
map.put(1, null);
map.replace(1, null, "replaced");
assertThat(map.get(1), is("replaced"));
}
@Test
public void replace_key_value_newValue_newNull() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "1");
map.replace(1, "1", null);
assertThat(map.size(), is(1));
assertNull(map.get(1));
}
@Test
public void replace_key_value_found() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "1");
map.replace(1, "replaced");
assertThat(map.get(1), is("replaced"));
}
@Test
public void replace_key_value_notFound() {
Map<Integer, String> map = new HashMap<>();
map.replace(1, "replaced");
assertTrue(map.isEmpty());
}
@Test
public void replace_key_value_mappedToNull() {
Map<Integer, String> map = new HashMap<>();
map.put(1, null);
map.replace(1, "replaced");
assertThat(map.get(1), is("replaced"));
}
@Test
public void computeIfAbsent() {
Map<Integer, List<String>> map = new HashMap<>();
map.computeIfAbsent(1, key -> new ArrayList<>()).add("newValue1");
map.computeIfAbsent(1, key -> new ArrayList<>()).add("newValue2");
assertThat(map.get(1), is(Arrays.asList("newValue1", "newValue2")));
}
@Test
public void computeIfAbsent_computedNull() {
Map<Integer, List<String>> map = new HashMap<>();
map.computeIfAbsent(1, key -> null);
assertTrue(map.isEmpty());
}
@Test
public void computeIfPresent_present_nonNullValue() {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.computeIfPresent(1, (k, v) -> ++v);
assertThat(map.get(1), is(2));
}
@Test
public void computeIfPresent_present_nullValue() {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, null);
map.computeIfPresent(1, (k, v) -> ++v);
assertThat(map.get(1), is(nullValue()));
}
@Test
public void computeIfPresent_present_computeNull() {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.computeIfPresent(1, (k, v) -> null);
assertTrue(map.isEmpty());
}
@Test
public void computeIfPresent_absent() {
Map<Integer, Integer> map = new HashMap<>();
map.computeIfPresent(1, (k, v) -> ++v);
assertTrue(map.isEmpty());
}
@Test
public void computeIfPresent_multimap() {
Map<Integer, Set<Integer>> map = new HashMap<>();
map.computeIfAbsent(1, x -> new HashSet<>()).add(1);
map.get(1).add(2);
map.get(1).add(3);
map.get(1).add(4);
map.computeIfPresent(1 , (key, set) -> set.remove(4) && set.isEmpty() ? null : set);
assertThat(map.get(1), is(new HashSet<>(Arrays.asList(1, 2, 3))));
}
@Test
public void computeIfPresent_multimap_removeEntry() {
Map<Integer, Set<Integer>> map = new HashMap<>();
map.computeIfAbsent(1, x -> new HashSet<>()).add(1);
map.computeIfPresent(1 , (key, set) -> set.remove(1) && set.isEmpty() ? null : set);
assertFalse(map.containsKey(1));
}
@Test
public void compute_present_nonNullValue() {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.compute(1, (k, v) -> isNull(v) ? 0 : ++v);
assertThat(map.get(1), is(2));
}
@Test(expected = NullPointerException.class)
public void compute_present_nullValue() {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, null);
map.compute(1, (k, v) -> ++v);
}
@Test
public void compute_present_computeNull() {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.compute(1, (k, v) -> null);
assertTrue(map.isEmpty());
}
@Test
public void compute_absent() {
Map<Integer, Integer> map = new HashMap<>();
map.compute(1, (k, v) -> isNull(v) ? 0 : v);
assertThat(map.get(1), is(0));
}
@Test
public void merge() {
Map<Integer, Integer> map = new HashMap<>();
map.merge(1, 0, (oldValue, newValue) -> ++oldValue);
map.merge(1, 0, (oldValue, newValue) -> ++oldValue);
map.merge(1, 0, (oldValue, newValue) -> ++oldValue);
map.merge(1, 0, (oldValue, newValue) -> ++oldValue);
assertThat(map.get(1), is(3));
}
}