-
Notifications
You must be signed in to change notification settings - Fork 587
/
MutableMapIterableTestCase.java
686 lines (585 loc) · 27.9 KB
/
MutableMapIterableTestCase.java
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* Copyright (c) 2019 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.map.mutable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentMap;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.ImmutableMapIterable;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.impl.IntegerWithCast;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.block.function.PassThruFunction0;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable;
import org.eclipse.collections.impl.map.MapIterableTestCase;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.test.domain.Key;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.Iterate;
import org.junit.Assert;
import org.junit.Test;
import static org.eclipse.collections.impl.factory.Iterables.iMap;
import static org.eclipse.collections.impl.factory.Iterables.mList;
/**
* Abstract JUnit TestCase for {@link MutableMapIterable}s.
*/
public abstract class MutableMapIterableTestCase extends MapIterableTestCase
{
@Override
protected abstract <K, V> MutableMapIterable<K, V> newMap();
@Override
protected abstract <K, V> MutableMapIterable<K, V> newMapWithKeyValue(K key, V value);
@Override
protected abstract <K, V> MutableMapIterable<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2);
@Override
protected abstract <K, V> MutableMapIterable<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2, K key3, V value3);
@Override
protected abstract <K, V> MutableMapIterable<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4);
@Test
public void toImmutable()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeyValue(1, "One");
ImmutableMapIterable<Integer, String> immutable = map.toImmutable();
Assert.assertEquals(Maps.immutable.with(1, "One"), immutable);
}
@Test
public void clear()
{
MutableMapIterable<Integer, Object> map =
this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three");
map.clear();
Verify.assertEmpty(map);
MutableMapIterable<Object, Object> map2 = this.newMap();
map2.clear();
Verify.assertEmpty(map2);
}
@Test
public void removeObject()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
map.remove("Two");
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
}
@Test
public void removeFromEntrySet()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertTrue(map.entrySet().remove(ImmutableEntry.of("Two", 2)));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
Assert.assertFalse(map.entrySet().remove(ImmutableEntry.of("Four", 4)));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
Assert.assertFalse(map.entrySet().remove(null));
MutableMapIterable<String, Integer> mapWithNullKey = this.newMapWithKeysValues("One", 1, null, 2, "Three", 3);
Assert.assertTrue(mapWithNullKey.entrySet().remove(new ImmutableEntry<String, Integer>(null, 2)));
}
@Test
public void removeAllFromEntrySet()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertTrue(map.entrySet().removeAll(FastList.newListWith(
ImmutableEntry.of("One", 1),
ImmutableEntry.of("Three", 3))));
Assert.assertEquals(UnifiedMap.newWithKeysValues("Two", 2), map);
Assert.assertFalse(map.entrySet().removeAll(FastList.newListWith(ImmutableEntry.of("Four", 4))));
Assert.assertEquals(UnifiedMap.newWithKeysValues("Two", 2), map);
Assert.assertFalse(map.entrySet().remove(null));
MutableMapIterable<String, Integer> mapWithNullKey = this.newMapWithKeysValues("One", 1, null, 2, "Three", 3);
Assert.assertTrue(mapWithNullKey.entrySet().removeAll(FastList.newListWith(ImmutableEntry.of(null, 2))));
}
@Test
public void retainAllFromEntrySet()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.entrySet().retainAll(FastList.newListWith(
ImmutableEntry.of("One", 1),
ImmutableEntry.of("Two", 2),
ImmutableEntry.of("Three", 3),
ImmutableEntry.of("Four", 4))));
Assert.assertTrue(map.entrySet().retainAll(FastList.newListWith(
ImmutableEntry.of("One", 1),
ImmutableEntry.of("Three", 3),
ImmutableEntry.of("Four", 4))));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
MutableMapIterable<Integer, Integer> integers = this.newMapWithKeysValues(1, 1, 2, 2, 3, 3);
Integer copy = new Integer(1);
Assert.assertTrue(integers.entrySet().retainAll(mList(ImmutableEntry.of(copy, copy))));
Assert.assertEquals(iMap(copy, copy), integers);
Assert.assertNotSame(copy, Iterate.getOnly(integers.entrySet()).getKey());
Assert.assertNotSame(copy, Iterate.getOnly(integers.entrySet()).getValue());
}
@Test
public void clearEntrySet()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
map.entrySet().clear();
Verify.assertEmpty(map);
}
@Test
public void entrySetEqualsAndHashCode()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Verify.assertEqualsAndHashCode(
UnifiedSet.newSetWith(
ImmutableEntry.of("One", 1),
ImmutableEntry.of("Two", 2),
ImmutableEntry.of("Three", 3)),
map.entrySet());
}
@Test
public void removeFromKeySet()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.keySet().remove("Four"));
Assert.assertTrue(map.keySet().remove("Two"));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
}
@Test
public void removeNullFromKeySet()
{
if (this.newMap() instanceof ConcurrentMap || this.newMap() instanceof SortedMap)
{
return;
}
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.keySet().remove(null));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Two", 2, "Three", 3), map);
map.put(null, 4);
Assert.assertTrue(map.keySet().remove(null));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Two", 2, "Three", 3), map);
}
@Test
public void removeAllFromKeySet()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.keySet().removeAll(FastList.newListWith("Four")));
Assert.assertTrue(map.keySet().removeAll(FastList.newListWith("Two", "Four")));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
}
@Test
public void retainAllFromKeySet()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.keySet().retainAll(FastList.newListWith("One", "Two", "Three", "Four")));
Assert.assertTrue(map.keySet().retainAll(FastList.newListWith("One", "Three")));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
}
@Test
public void clearKeySet()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
map.keySet().clear();
Verify.assertEmpty(map);
}
@Test
public void keySetEqualsAndHashCode()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3, null, null);
Verify.assertEqualsAndHashCode(UnifiedSet.newSetWith("One", "Two", "Three", null), map.keySet());
}
@Test
public void keySetToArray()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
MutableList<String> expected = FastList.newListWith("One", "Two", "Three").toSortedList();
Set<String> keySet = map.keySet();
Assert.assertEquals(expected, FastList.newListWith(keySet.toArray()).toSortedList());
Assert.assertEquals(expected, FastList.newListWith(keySet.toArray(new String[keySet.size()])).toSortedList());
}
@Test
public void removeFromValues()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.values().remove(4));
Assert.assertTrue(map.values().remove(2));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
}
@Test
public void removeNullFromValues()
{
if (this.newMap() instanceof ConcurrentMap)
{
return;
}
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.values().remove(null));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Two", 2, "Three", 3), map);
map.put("Four", null);
Assert.assertTrue(map.values().remove(null));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Two", 2, "Three", 3), map);
}
@Test
public void removeAllFromValues()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.values().removeAll(FastList.newListWith(4)));
Assert.assertTrue(map.values().removeAll(FastList.newListWith(2, 4)));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
}
@Test
public void retainAllFromValues()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
Assert.assertFalse(map.values().retainAll(FastList.newListWith(1, 2, 3, 4)));
Assert.assertTrue(map.values().retainAll(FastList.newListWith(1, 3)));
Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);
}
@Test
public void put()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two");
Assert.assertNull(map.put(3, "Three"));
Assert.assertEquals(UnifiedMap.newWithKeysValues(1, "One", 2, "Two", 3, "Three"), map);
ImmutableList<Integer> key1 = Lists.immutable.with(null);
ImmutableList<Integer> key2 = Lists.immutable.with(null);
Object value1 = new Object();
Object value2 = new Object();
MutableMapIterable<ImmutableList<Integer>, Object> map2 = this.newMapWithKeyValue(key1, value1);
Object previousValue = map2.put(key2, value2);
Assert.assertSame(value1, previousValue);
Assert.assertSame(key1, map2.keysView().getFirst());
}
@Test
public void putAll()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "2");
MutableMapIterable<Integer, String> toAdd = this.newMapWithKeysValues(2, "Two", 3, "Three");
map.putAll(toAdd);
Verify.assertSize(3, map);
Verify.assertContainsAllKeyValues(map, 1, "One", 2, "Two", 3, "Three");
//Testing JDK map
MutableMapIterable<Integer, String> map2 = this.newMapWithKeysValues(1, "One", 2, "2");
HashMap<Integer, String> hashMaptoAdd = new HashMap<>(toAdd);
map2.putAll(hashMaptoAdd);
Verify.assertSize(3, map2);
Verify.assertContainsAllKeyValues(map2, 1, "One", 2, "Two", 3, "Three");
}
@Test
public void removeKey()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "Two");
Assert.assertEquals("1", map.removeKey(1));
Verify.assertSize(1, map);
Verify.denyContainsKey(1, map);
Assert.assertNull(map.removeKey(42));
Verify.assertSize(1, map);
Assert.assertEquals("Two", map.removeKey(2));
Verify.assertEmpty(map);
}
@Test
public void removeAllKeys()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "Two", 3, "Three");
Verify.assertThrows(NullPointerException.class, () -> map.removeAllKeys(null));
Assert.assertFalse(map.removeAllKeys(Sets.mutable.with(4)));
Assert.assertFalse(map.removeAllKeys(Sets.mutable.with(4, 5, 6)));
Assert.assertFalse(map.removeAllKeys(Sets.mutable.with(4, 5, 6, 7, 8, 9)));
Assert.assertTrue(map.removeAllKeys(Sets.mutable.with(1)));
Verify.denyContainsKey(1, map);
Assert.assertTrue(map.removeAllKeys(Sets.mutable.with(3, 4, 5, 6, 7)));
Verify.denyContainsKey(3, map);
map.putAll(Maps.mutable.with(4, "Four", 5, "Five", 6, "Six", 7, "Seven"));
Assert.assertTrue(map.removeAllKeys(Sets.mutable.with(2, 3, 9, 10)));
Verify.denyContainsKey(2, map);
Assert.assertTrue(map.removeAllKeys(Sets.mutable.with(5, 3, 7, 8, 9)));
Assert.assertEquals(Maps.mutable.with(4, "Four", 6, "Six"), map);
}
@Test
public void removeIf()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "Two");
Assert.assertFalse(map.removeIf(Predicates2.alwaysFalse()));
Assert.assertEquals(this.newMapWithKeysValues(1, "1", 2, "Two"), map);
Assert.assertTrue(map.removeIf(Predicates2.alwaysTrue()));
Verify.assertEmpty(map);
map.putAll(Maps.mutable.with(1, "One", 2, "TWO", 3, "THREE", 4, "four"));
map.putAll(Maps.mutable.with(5, "Five", 6, "Six", 7, "Seven", 8, "Eight"));
Assert.assertTrue(map.removeIf((each, value) -> each % 2 == 0 && value.length() < 4));
Verify.denyContainsKey(2, map);
Verify.denyContainsKey(6, map);
MutableMapIterable<Integer, String> expected = this.newMapWithKeysValues(1, "One", 3, "THREE", 4, "four", 5, "Five");
expected.put(7, "Seven");
expected.put(8, "Eight");
Assert.assertEquals(expected, map);
Assert.assertTrue(map.removeIf((each, value) -> each % 2 != 0 && value.equals("THREE")));
Verify.denyContainsKey(3, map);
Verify.assertSize(5, map);
Assert.assertTrue(map.removeIf((each, value) -> each % 2 != 0));
Assert.assertFalse(map.removeIf((each, value) -> each % 2 != 0));
Assert.assertEquals(this.newMapWithKeysValues(4, "four", 8, "Eight"), map);
}
@Test
public void getIfAbsentPut()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
Assert.assertNull(map.get(4));
Assert.assertEquals("4", map.getIfAbsentPut(4, new PassThruFunction0<>("4")));
Assert.assertEquals("3", map.getIfAbsentPut(3, new PassThruFunction0<>("3")));
Verify.assertContainsKeyValue(4, "4", map);
}
@Test
public void getIfAbsentPutValue()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
Assert.assertNull(map.get(4));
Assert.assertEquals("4", map.getIfAbsentPut(4, "4"));
Assert.assertEquals("3", map.getIfAbsentPut(3, "5"));
Verify.assertContainsKeyValue(1, "1", map);
Verify.assertContainsKeyValue(2, "2", map);
Verify.assertContainsKeyValue(3, "3", map);
Verify.assertContainsKeyValue(4, "4", map);
}
@Test
public void getIfAbsentPutWithKey()
{
MutableMapIterable<Integer, Integer> map = this.newMapWithKeysValues(1, 1, 2, 2, 3, 3);
Assert.assertNull(map.get(4));
Assert.assertEquals(Integer.valueOf(4), map.getIfAbsentPutWithKey(4, Functions.getIntegerPassThru()));
Assert.assertEquals(Integer.valueOf(3), map.getIfAbsentPutWithKey(3, Functions.getIntegerPassThru()));
Verify.assertContainsKeyValue(Integer.valueOf(4), Integer.valueOf(4), map);
}
@Test
public void getIfAbsentPutWith()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
Assert.assertNull(map.get(4));
Assert.assertEquals("4", map.getIfAbsentPutWith(4, String::valueOf, 4));
Assert.assertEquals("3", map.getIfAbsentPutWith(3, String::valueOf, 3));
Verify.assertContainsKeyValue(4, "4", map);
}
@Test
public void getIfAbsentPut_block_throws()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
Verify.assertThrows(RuntimeException.class, () -> map.getIfAbsentPut(4, () -> {
throw new RuntimeException();
}));
Assert.assertEquals(UnifiedMap.newWithKeysValues(1, "1", 2, "2", 3, "3"), map);
}
@Test
public void getIfAbsentPutWith_block_throws()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
Verify.assertThrows(RuntimeException.class, () -> map.getIfAbsentPutWith(4, object -> {
throw new RuntimeException();
}, null));
Assert.assertEquals(UnifiedMap.newWithKeysValues(1, "1", 2, "2", 3, "3"), map);
}
@Test
public void getKeysAndGetValues()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
Verify.assertContainsAll(map.keySet(), 1, 2, 3);
Verify.assertContainsAll(map.values(), "1", "2", "3");
}
@Test
public void newEmpty()
{
MutableMapIterable<Integer, Integer> map = this.newMapWithKeysValues(1, 1, 2, 2);
Verify.assertEmpty(map.newEmpty());
}
@Test
public void keysAndValues_toString()
{
MutableMapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2");
Verify.assertContains(map.keySet().toString(), FastList.newListWith("[1, 2]", "[2, 1]"));
Verify.assertContains(map.values().toString(), FastList.newListWith("[1, 2]", "[2, 1]"));
Verify.assertContains(map.keysView().toString(), FastList.newListWith("[1, 2]", "[2, 1]"));
Verify.assertContains(map.valuesView().toString(), FastList.newListWith("[1, 2]", "[2, 1]"));
}
@Test
public void keyPreservation()
{
Key key = new Key("key");
Key duplicateKey1 = new Key("key");
MapIterable<Key, Integer> map1 = this.newMapWithKeysValues(key, 1, duplicateKey1, 2);
Verify.assertSize(1, map1);
Verify.assertContainsKeyValue(key, 2, map1);
Assert.assertSame(key, map1.keysView().getFirst());
Key duplicateKey2 = new Key("key");
MapIterable<Key, Integer> map2 = this.newMapWithKeysValues(key, 1, duplicateKey1, 2, duplicateKey2, 3);
Verify.assertSize(1, map2);
Verify.assertContainsKeyValue(key, 3, map2);
Assert.assertSame(key, map1.keysView().getFirst());
Key duplicateKey3 = new Key("key");
MapIterable<Key, Integer> map3 = this.newMapWithKeysValues(key, 1, new Key("not a dupe"), 2, duplicateKey3, 3);
Verify.assertSize(2, map3);
Verify.assertContainsAllKeyValues(map3, key, 3, new Key("not a dupe"), 2);
Assert.assertSame(key, map3.keysView().detect(key::equals));
Key duplicateKey4 = new Key("key");
MapIterable<Key, Integer> map4 = this.newMapWithKeysValues(key, 1, new Key("still not a dupe"), 2, new Key("me neither"), 3, duplicateKey4, 4);
Verify.assertSize(3, map4);
Verify.assertContainsAllKeyValues(map4, key, 4, new Key("still not a dupe"), 2, new Key("me neither"), 3);
Assert.assertSame(key, map4.keysView().detect(key::equals));
MapIterable<Key, Integer> map5 = this.newMapWithKeysValues(key, 1, duplicateKey1, 2, duplicateKey3, 3, duplicateKey4, 4);
Verify.assertSize(1, map5);
Verify.assertContainsKeyValue(key, 4, map5);
Assert.assertSame(key, map5.keysView().getFirst());
}
@Test
public void asUnmodifiable()
{
Verify.assertThrows(UnsupportedOperationException.class, () -> this.newMapWithKeysValues(1, 1, 2, 2).asUnmodifiable().put(3, 3));
}
@Test
public void asSynchronized()
{
MapIterable<Integer, Integer> map = this.newMapWithKeysValues(1, 1, 2, 2).asSynchronized();
Verify.assertInstanceOf(AbstractSynchronizedMapIterable.class, map);
}
@Test
public void add()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeyValue("A", 1);
Assert.assertEquals(Integer.valueOf(1), map.add(Tuples.pair("A", 3)));
Assert.assertNull(map.add(Tuples.pair("B", 2)));
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 3, "B", 2), map);
}
@Test
public void putPair()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeyValue("A", 1);
Assert.assertEquals(Integer.valueOf(1), map.putPair(Tuples.pair("A", 3)));
Assert.assertNull(map.putPair(Tuples.pair("B", 2)));
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 3, "B", 2), map);
}
@Test
public void withKeyValue()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeyValue("A", 1);
MutableMapIterable<String, Integer> mapWith = map.withKeyValue("B", 2);
Assert.assertSame(map, mapWith);
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 1, "B", 2), mapWith);
MutableMapIterable<String, Integer> mapWith2 = mapWith.withKeyValue("A", 11);
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 11, "B", 2), mapWith);
}
@Test
public void withAllKeyValues()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("A", 1, "B", 2);
MutableMapIterable<String, Integer> mapWith = map.withAllKeyValues(
FastList.newListWith(Tuples.pair("B", 22), Tuples.pair("C", 3)));
Assert.assertSame(map, mapWith);
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 1, "B", 22, "C", 3), mapWith);
}
@Test
public void withAllKeyValueArguments()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("A", 1, "B", 2);
MutableMapIterable<String, Integer> mapWith = map.withAllKeyValueArguments(Tuples.pair("B", 22), Tuples.pair("C", 3));
Assert.assertSame(map, mapWith);
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 1, "B", 22, "C", 3), mapWith);
}
@Test
public void withoutKey()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("A", 1, "B", 2);
MutableMapIterable<String, Integer> mapWithout = map.withoutKey("B");
Assert.assertSame(map, mapWithout);
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 1), mapWithout);
}
@Test
public void withoutAllKeys()
{
MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("A", 1, "B", 2, "C", 3);
MutableMapIterable<String, Integer> mapWithout = map.withoutAllKeys(FastList.newListWith("A", "C"));
Assert.assertSame(map, mapWithout);
Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("B", 2), mapWithout);
}
@Test
public void retainAllFromKeySet_null_collision()
{
if (this.newMap() instanceof ConcurrentMap || this.newMap() instanceof SortedMap)
{
return;
}
IntegerWithCast key = new IntegerWithCast(0);
MutableMapIterable<IntegerWithCast, String> mutableMapIterable = this.newMapWithKeysValues(
null, "Test 1",
key, "Test 2");
Assert.assertFalse(mutableMapIterable.keySet().retainAll(FastList.newListWith(key, null)));
Assert.assertEquals(
this.newMapWithKeysValues(
null, "Test 1",
key, "Test 2"),
mutableMapIterable);
}
@Test
public void rehash_null_collision()
{
if (this.newMap() instanceof ConcurrentMap || this.newMap() instanceof SortedMap)
{
return;
}
MutableMapIterable<IntegerWithCast, String> mutableMapIterable = this.newMapWithKeyValue(null, null);
for (int i = 0; i < 256; i++)
{
mutableMapIterable.put(new IntegerWithCast(i), String.valueOf(i));
}
}
@Test
public void updateValue()
{
MutableMapIterable<Integer, Integer> map = this.newMap();
Iterate.forEach(Interval.oneTo(1000), each -> map.updateValue(each % 10, () -> 0, integer -> integer + 1));
Assert.assertEquals(Interval.zeroTo(9).toSet(), map.keySet());
Assert.assertEquals(FastList.newList(Collections.nCopies(10, 100)), FastList.newList(map.values()));
}
@Test
public void updateValue_collisions()
{
MutableMapIterable<Integer, Integer> map = this.newMap();
MutableList<Integer> list = Interval.oneTo(2000).toList().shuffleThis();
Iterate.forEach(list, each -> map.updateValue(each % 1000, () -> 0, integer -> integer + 1));
Assert.assertEquals(Interval.zeroTo(999).toSet(), map.keySet());
Assert.assertEquals(
HashBag.newBag(map.values()).toStringOfItemToCount(),
FastList.newList(Collections.nCopies(1000, 2)),
FastList.newList(map.values()));
}
@Test
public void updateValueWith()
{
MutableMapIterable<Integer, Integer> map = this.newMap();
Iterate.forEach(Interval.oneTo(1000), each -> map.updateValueWith(each % 10, () -> 0, (integer, parameter) -> {
Assert.assertEquals("test", parameter);
return integer + 1;
}, "test"));
Assert.assertEquals(Interval.zeroTo(9).toSet(), map.keySet());
Assert.assertEquals(FastList.newList(Collections.nCopies(10, 100)), FastList.newList(map.values()));
}
@Test
public void updateValueWith_collisions()
{
MutableMapIterable<Integer, Integer> map = this.newMap();
MutableList<Integer> list = Interval.oneTo(2000).toList().shuffleThis();
Iterate.forEach(list, each -> map.updateValueWith(each % 1000, () -> 0, (integer, parameter) -> {
Assert.assertEquals("test", parameter);
return integer + 1;
}, "test"));
Assert.assertEquals(Interval.zeroTo(999).toSet(), map.keySet());
Assert.assertEquals(
HashBag.newBag(map.values()).toStringOfItemToCount(),
FastList.newList(Collections.nCopies(1000, 2)),
FastList.newList(map.values()));
}
}