-
Notifications
You must be signed in to change notification settings - Fork 585
/
UnifiedMapTest.java
845 lines (721 loc) · 33.9 KB
/
UnifiedMapTest.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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/*
* Copyright (c) 2016 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.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.block.factory.Procedures;
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.list.Interval;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.math.IntegerSum;
import org.eclipse.collections.impl.math.Sum;
import org.eclipse.collections.impl.math.SumProcedure;
import org.eclipse.collections.impl.parallel.BatchIterable;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.junit.Assert;
import org.junit.Test;
public class UnifiedMapTest extends UnifiedMapTestCase
{
@Override
public <K, V> MutableMap<K, V> newMap()
{
return UnifiedMap.newMap();
}
@Override
public <K, V> MutableMap<K, V> newMapWithKeyValue(K key, V value)
{
return UnifiedMap.newWithKeysValues(key, value);
}
@Override
public <K, V> MutableMap<K, V> newMapWithKeysValues(K key1, V value1, K key2, V value2)
{
return UnifiedMap.newWithKeysValues(key1, value1, key2, value2);
}
@Override
public <K, V> MutableMap<K, V> newMapWithKeysValues(
K key1, V value1, K key2, V value2, K key3,
V value3)
{
return UnifiedMap.newWithKeysValues(key1, value1, key2, value2, key3, value3);
}
@Override
public <K, V> MutableMap<K, V> newMapWithKeysValues(
K key1, V value1, K key2, V value2, K key3,
V value3, K key4, V value4)
{
return UnifiedMap.newWithKeysValues(key1, value1, key2, value2, key3, value3, key4, value4);
}
@Test
public void newMap_throws()
{
Verify.assertThrows(IllegalArgumentException.class, () -> new UnifiedMap<Integer, Integer>(-1, 0.5f));
Verify.assertThrows(IllegalArgumentException.class, () -> new UnifiedMap<Integer, Integer>(1, 0.0f));
Verify.assertThrows(IllegalArgumentException.class, () -> new UnifiedMap<Integer, Integer>(1, -0.5f));
Verify.assertThrows(IllegalArgumentException.class, () -> new UnifiedMap<Integer, Integer>(1, 1.5f));
}
@Test
public void newMapTest()
{
for (int i = 1; i < 17; i++)
{
this.assertPresizedMap(i, 0.75f);
}
this.assertPresizedMap(31, 0.75f);
this.assertPresizedMap(32, 0.75f);
this.assertPresizedMap(34, 0.75f);
this.assertPresizedMap(60, 0.75f);
this.assertPresizedMap(64, 0.70f);
this.assertPresizedMap(68, 0.70f);
this.assertPresizedMap(60, 0.70f);
this.assertPresizedMap(1025, 0.80f);
this.assertPresizedMap(1024, 0.80f);
this.assertPresizedMap(1025, 0.80f);
this.assertPresizedMap(1024, 0.805f);
}
private void assertPresizedMap(int initialCapacity, float loadFactor)
{
try
{
Field tableField = UnifiedMap.class.getDeclaredField("table");
tableField.setAccessible(true);
Object[] table = (Object[]) tableField.get(UnifiedMap.newMap(initialCapacity, loadFactor));
int size = (int) Math.ceil(initialCapacity / loadFactor);
int capacity = 1;
while (capacity < size)
{
capacity <<= 1;
}
capacity <<= 1;
Assert.assertEquals(capacity, table.length);
}
catch (SecurityException ignored)
{
Assert.fail("Unable to modify the visibility of the table on UnifiedMap");
}
catch (NoSuchFieldException ignored)
{
Assert.fail("No field named table UnifiedMap");
}
catch (IllegalAccessException ignored)
{
Assert.fail("No access the field table in UnifiedMap");
}
}
@Test
public void constructorOfPairs()
{
Assert.assertEquals(
UnifiedMap.newWithKeysValues(1, "one", 2, "two", 3, "three"),
UnifiedMap.newMapWith(Tuples.pair(1, "one"), Tuples.pair(2, "two"), Tuples.pair(3, "three")));
}
@Test
public void constructorOfIterableOfPairs()
{
Pair<Integer, String> pair1 = Tuples.pair(1, "One");
Pair<Integer, String> pair2 = Tuples.pair(2, "Two");
Pair<Integer, String> pair3 = Tuples.pair(3, "Three");
Pair<Integer, String> pair4 = Tuples.pair(4, "Four");
Assert.assertEquals(
UnifiedMap.newMapWith(pair1, pair2, pair3, pair4),
UnifiedMap.newMapWith(FastList.newListWith(pair1, pair2, pair3, pair4)));
Assert.assertEquals(
UnifiedMap.newMapWith(pair1, pair2, pair3, pair4),
UnifiedMap.newMapWith(UnifiedSet.newSetWith(pair1, pair2, pair3, pair4)));
}
@Test
public void batchForEach()
{
UnifiedMap<String, Integer> map = UnifiedMap.<String, Integer>newMap(5).withKeysValues("1", 1, "2", 2, "3", 3, "4", 4);
this.batchForEachTestCases(map, 10);
UnifiedMap<Integer, Integer> collisions = UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6);
this.batchForEachChains(collisions, 21);
UnifiedMap<Integer, Integer> nulls = UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null);
this.batchForEachNullHandling(nulls, 36);
this.batchForEachEmptyBatchIterable(UnifiedMap.<Integer, Integer>newMap());
}
@Test
public void batchForEachKey()
{
Set<Integer> keys = UnifiedMap.<Integer, String>newMap(5).withKeysValues(1, "1", 2, "2", 3, "3", 4, "4").keySet();
this.batchForEachTestCases((BatchIterable<Integer>) keys, 10);
Set<Integer> collisions = UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6).keySet();
this.batchForEachChains((BatchIterable<Integer>) collisions, 57);
Set<Integer> nulls = UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).keySet();
this.batchForEachNullHandling((BatchIterable<Integer>) nulls, 16);
this.batchForEachEmptyBatchIterable((BatchIterable<Integer>) UnifiedMap.<Integer, Integer>newMap().keySet());
}
@Test
public void batchForEachValue()
{
Collection<Integer> values = UnifiedMap.<String, Integer>newMap(5).withKeysValues("1", 1, "2", 2, "3", 3, "4", 4).values();
this.batchForEachTestCases((BatchIterable<Integer>) values, 10);
Collection<Integer> collisions = UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6).values();
this.batchForEachChains((BatchIterable<Integer>) collisions, 21);
Collection<Integer> nulls = UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).values();
this.batchForEachNullHandling((BatchIterable<Integer>) nulls, 36);
this.batchForEachEmptyBatchIterable((BatchIterable<Integer>) UnifiedMap.<Integer, Integer>newMap().values());
}
@Test
public void batchForEachEntry()
{
//Testing batch size of 1 to 16 with no chains
BatchIterable<Map.Entry<Integer, Integer>> entries =
(BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.newWithKeysValues(1, 1, 2, 2, 3, 3, 4, 4).entrySet();
for (int sectionCount = 1; sectionCount <= 16; ++sectionCount)
{
Sum sum = new IntegerSum(0);
for (int sectionIndex = 0; sectionIndex < sectionCount; ++sectionIndex)
{
entries.batchForEach(new EntrySumProcedure(sum), sectionIndex, sectionCount);
}
Assert.assertEquals(20, sum.getValue());
}
}
@Test
public void batchForEachEntry_chains()
{
BatchIterable<Map.Entry<Integer, Integer>> collisions =
(BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(
COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6).entrySet();
//Testing 1 batch with chains
Sum sum2 = new IntegerSum(0);
//testing getBatchCount returns 1
int numBatches = collisions.getBatchCount(100000);
for (int i = 0; i < numBatches; ++i)
{
collisions.batchForEach(new EntrySumProcedure(sum2), i, numBatches);
}
Assert.assertEquals(1, numBatches);
Assert.assertEquals(78, sum2.getValue());
//Testing 3 batches with chains and uneven last batch
Sum sum3 = new IntegerSum(0);
for (int i = 0; i < 5; ++i)
{
collisions.batchForEach(new EntrySumProcedure(sum3), i, 5);
}
Assert.assertEquals(78, sum3.getValue());
}
@Test
public void batchForEachEntry_null_handling()
{
//Testing batchForEach handling null keys and null values
Sum sum4 = new IntegerSum(0);
BatchIterable<Map.Entry<Integer, Integer>> nulls =
(BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(
null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).entrySet();
for (int i = 0; i < nulls.getBatchCount(7); ++i)
{
nulls.batchForEach(each -> {
sum4.add(each.getKey() == null ? 1 : each.getKey());
sum4.add(each.getValue() == null ? 1 : each.getValue());
}, i, nulls.getBatchCount(7));
}
Assert.assertEquals(52, sum4.getValue());
}
@Test
public void batchForEachEntry_emptySet()
{
//Test batchForEach on empty set, it should simply do nothing and not throw any exceptions
Sum sum5 = new IntegerSum(0);
BatchIterable<Map.Entry<Integer, Integer>> empty = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.newMap().entrySet();
empty.batchForEach(new EntrySumProcedure(sum5), 0, empty.getBatchCount(1));
Assert.assertEquals(0, sum5.getValue());
}
private void batchForEachTestCases(BatchIterable<Integer> batchIterable, int expectedValue)
{
//Testing batch size of 1 to 16 with no chains
for (int sectionCount = 1; sectionCount <= 16; ++sectionCount)
{
Sum sum = new IntegerSum(0);
for (int sectionIndex = 0; sectionIndex < sectionCount; ++sectionIndex)
{
batchIterable.batchForEach(new SumProcedure<>(sum), sectionIndex, sectionCount);
}
Assert.assertEquals(expectedValue, sum.getValue());
}
}
private void batchForEachChains(BatchIterable<Integer> batchIterable, int expectedValue)
{
//Testing 1 batch with chains
Sum sum = new IntegerSum(0);
//testing getBatchCount returns 1
int numBatches = batchIterable.getBatchCount(100000);
for (int i = 0; i < numBatches; ++i)
{
batchIterable.batchForEach(new SumProcedure<>(sum), i, numBatches);
}
Assert.assertEquals(1, numBatches);
Assert.assertEquals(expectedValue, sum.getValue());
//Testing 3 batches with chains and uneven last batch
Sum sum2 = new IntegerSum(0);
for (int i = 0; i < 5; ++i)
{
batchIterable.batchForEach(new SumProcedure<>(sum2), i, 5);
}
Assert.assertEquals(expectedValue, sum2.getValue());
}
private void batchForEachNullHandling(BatchIterable<Integer> batchIterable, int expectedValue)
{
//Testing batchForEach handling null keys and null values
Sum sum = new IntegerSum(0);
for (int i = 0; i < batchIterable.getBatchCount(7); ++i)
{
batchIterable.batchForEach(each -> sum.add(each == null ? 1 : each), i, batchIterable.getBatchCount(7));
}
Assert.assertEquals(expectedValue, sum.getValue());
}
private void batchForEachEmptyBatchIterable(BatchIterable<Integer> batchIterable)
{
//Test batchForEach on empty set, it should simply do nothing and not throw any exceptions
Sum sum = new IntegerSum(0);
batchIterable.batchForEach(new SumProcedure<>(sum), 0, batchIterable.getBatchCount(1));
Assert.assertEquals(0, sum.getValue());
}
@Test
public void batchIterable_forEach()
{
UnifiedMap<String, Integer> map = UnifiedMap.<String, Integer>newMap(5).withKeysValues("1", 1, "2", 2, "3", 3, "4", 4);
this.batchIterable_forEach(map, 10);
UnifiedMap<Integer, Integer> collisions = UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6);
this.batchIterable_forEach(collisions, 21);
UnifiedMap<Integer, Integer> nulls = UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null);
this.batchIterable_forEachNullHandling(nulls, 33);
this.batchIterable_forEachEmptyBatchIterable(UnifiedMap.<Integer, Integer>newMap());
}
@Test
public void batchIterable_forEachKey()
{
Set<Integer> keys = UnifiedMap.<Integer, String>newMap(5).withKeysValues(1, "1", 2, "2", 3, "3", 4, "4").keySet();
this.batchIterable_forEach((BatchIterable<Integer>) keys, 10);
Set<Integer> collisions = UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6).keySet();
this.batchIterable_forEach((BatchIterable<Integer>) collisions, 57);
Set<Integer> nulls = UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).keySet();
this.batchIterable_forEachNullHandling((BatchIterable<Integer>) nulls, 15);
this.batchIterable_forEachEmptyBatchIterable((BatchIterable<Integer>) UnifiedMap.<Integer, Integer>newMap().keySet());
}
@Test
public void batchIterable_forEachValue()
{
Collection<Integer> values = UnifiedMap.<String, Integer>newMap(5).withKeysValues("1", 1, "2", 2, "3", 3, "4", 4).values();
this.batchIterable_forEach((BatchIterable<Integer>) values, 10);
Collection<Integer> collisions = UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6).values();
this.batchIterable_forEach((BatchIterable<Integer>) collisions, 21);
Collection<Integer> nulls = UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).values();
this.batchIterable_forEachNullHandling((BatchIterable<Integer>) nulls, 33);
this.batchIterable_forEachEmptyBatchIterable((BatchIterable<Integer>) UnifiedMap.<Integer, Integer>newMap().values());
}
@Test
public void batchIterable_forEachEntry()
{
BatchIterable<Map.Entry<Integer, Integer>> entries =
(BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.newWithKeysValues(1, 1, 2, 2, 3, 3, 4, 4).entrySet();
Sum sum = new IntegerSum(0);
entries.forEach(new EntrySumProcedure(sum));
Assert.assertEquals(20, sum.getValue());
}
@Test
public void batchIterable_forEachEntry_chains()
{
BatchIterable<Map.Entry<Integer, Integer>> collisions =
(BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.<Integer, Integer>newMap(5).withKeysValues(
COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4).withKeysValues(2, 5, 3, 6).entrySet();
Sum sum = new IntegerSum(0);
collisions.forEach(new EntrySumProcedure(sum));
Assert.assertEquals(78, sum.getValue());
}
@Test
public void batchIterable_forEachEntry_null_handling()
{
//Testing batchForEach handling null keys and null values
Sum sum = new IntegerSum(0);
BatchIterable<Map.Entry<Integer, Integer>> nulls =
(BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.<Integer, Integer>newMap(100).withKeysValues(
null, 10, 1, null, 2, 11, 3, 12).withKeysValues(4, null, 5, null).entrySet();
nulls.forEach(each -> {
sum.add(each.getKey() == null ? 0 : each.getKey());
sum.add(each.getValue() == null ? 0 : each.getValue());
});
Assert.assertEquals(48, sum.getValue());
}
@Test
public void batchIterable_forEachEntry_emptySet()
{
//Test forEach on empty set, it should simply do nothing and not throw any exceptions
Sum sum = new IntegerSum(0);
BatchIterable<Map.Entry<Integer, Integer>> empty =
(BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMap.newMap().entrySet();
empty.forEach(new EntrySumProcedure(sum));
Assert.assertEquals(0, sum.getValue());
}
private void batchIterable_forEach(BatchIterable<Integer> batchIterable, int expectedValue)
{
IntegerSum sum = new IntegerSum(0);
batchIterable.forEach(new SumProcedure<>(sum));
Assert.assertEquals(expectedValue, sum.getValue());
}
private void batchIterable_forEachNullHandling(BatchIterable<Integer> batchIterable, int expectedValue)
{
//Testing forEach handling null keys and null values
Sum sum = new IntegerSum(0);
batchIterable.forEach(each -> sum.add(each == null ? 0 : each));
Assert.assertEquals(expectedValue, sum.getValue());
}
private void batchIterable_forEachEmptyBatchIterable(BatchIterable<Integer> batchIterable)
{
//Test forEach on empty set, it should simply do nothing and not throw any exceptions
Sum sum = new IntegerSum(0);
batchIterable.batchForEach(new SumProcedure<>(sum), 0, batchIterable.getBatchCount(1));
Assert.assertEquals(0, sum.getValue());
}
@Test
public void getMapMemoryUsedInWords()
{
UnifiedMap<String, String> map = UnifiedMap.newMap();
Assert.assertEquals(34, map.getMapMemoryUsedInWords());
map.put("1", "1");
Assert.assertEquals(34, map.getMapMemoryUsedInWords());
UnifiedMap<Integer, Integer> map2 = this.mapWithCollisionsOfSize(2);
Assert.assertEquals(16, map2.getMapMemoryUsedInWords());
}
@Test
public void getCollidingBuckets()
{
UnifiedMap<Object, Object> map = UnifiedMap.newMap();
Assert.assertEquals(0, map.getCollidingBuckets());
UnifiedMap<Integer, Integer> map2 = this.mapWithCollisionsOfSize(2);
Assert.assertEquals(1, map2.getCollidingBuckets());
map2.put(42, 42);
Assert.assertEquals(1, map2.getCollidingBuckets());
UnifiedMap<String, String> map3 = UnifiedMap.newWithKeysValues("Six", "6", "Bar", "-", "Three", "3", "Five", "5");
Assert.assertEquals(2, map3.getCollidingBuckets());
}
@Override
@Test
public void getIfAbsentPut()
{
super.getIfAbsentPut();
// this map is deliberately small to force a rehash to occur from the put method, in a map with a chained bucket
UnifiedMap<Integer, Integer> map = UnifiedMap.newMap(2, 0.75f);
COLLISIONS.subList(0, 5).forEach(Procedures.cast(each -> map.getIfAbsentPut(each, new PassThruFunction0<>(each))));
Assert.assertEquals(this.mapWithCollisionsOfSize(5), map);
//Test getting element present in chain
UnifiedMap<Integer, Integer> map2 = UnifiedMap.newWithKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, COLLISION_4, 4);
Assert.assertEquals(Integer.valueOf(3), map2.getIfAbsentPut(COLLISION_3, () -> {
Assert.fail();
return null;
}));
//Test rehashing while creating a new chained key
UnifiedMap<Integer, Integer> map3 = UnifiedMap.<Integer, Integer>newMap(2, 0.75f).withKeysValues(1, COLLISION_1, 2, COLLISION_2, 3, COLLISION_3);
Assert.assertEquals(COLLISION_4, map3.getIfAbsentPut(4, new PassThruFunction0<>(COLLISION_4)));
Assert.assertNull(map3.getIfAbsentPut(5, new PassThruFunction0<>(null)));
}
@Override
@Test
public void getIfAbsentPut_block_throws()
{
super.getIfAbsentPut_block_throws();
// this map is deliberately small to force a rehash to occur from the put method, in a map with a chained bucket
UnifiedMap<Integer, Integer> map = UnifiedMap.newMap(2, 0.75f);
COLLISIONS.subList(0, 5).forEach(Procedures.cast(each -> {
Verify.assertThrows(RuntimeException.class, () -> map.getIfAbsentPut(each, () -> {
throw new RuntimeException();
}));
map.put(each, each);
}));
Assert.assertEquals(this.mapWithCollisionsOfSize(5), map);
}
@Override
@Test
public void put()
{
super.put();
// this map is deliberately small to force a rehash to occur from the put method, in a map with a chained bucket
UnifiedMap<Integer, Integer> map = UnifiedMap.newMap(2, 0.75f);
COLLISIONS.subList(0, 5).forEach(Procedures.cast(each -> Assert.assertNull(map.put(each, each))));
Assert.assertEquals(this.mapWithCollisionsOfSize(5), map);
}
@Override
@Test
public void collectValues()
{
super.collectValues();
UnifiedMap<String, Integer> map = UnifiedMap.<String, Integer>newMap().withKeysValues("1", 1, "2", 2, "3", 3, "4", 4);
Assert.assertEquals(
UnifiedMap.<String, String>newMap(5).withKeysValues("1", "11", "2", "22", "3", "33", "4", "44"),
map.collectValues((key, value) -> key + value));
UnifiedMap<Integer, Integer> collisions = UnifiedMap.<Integer, Integer>newMap().withKeysValues(COLLISION_1, 1, COLLISION_2, 2, COLLISION_3, 3, 1, 4);
Assert.assertEquals(
UnifiedMap.<Integer, Integer>newMap().withKeysValues(COLLISION_1, COLLISION_1 + 1, COLLISION_2, COLLISION_2 + 2, COLLISION_3, COLLISION_3 + 3, 1, 5),
collisions.collectValues((key, value) -> key + value));
UnifiedMap<Integer, Integer> nulls = UnifiedMap.<Integer, Integer>newMap().withKeysValues(null, 10, 1, null, 2, 11, 3, 12);
Assert.assertEquals(
UnifiedMap.<Integer, Boolean>newMap().withKeysValues(null, true, 1, true, 2, false, 3, false),
nulls.collectValues((key, value) -> key == null || value == null));
UnifiedMap<Integer, Integer> empty = UnifiedMap.newMap();
Verify.assertEmpty(empty.collectValues((key, value) -> key + value));
}
@Override
@Test
public void detect()
{
super.detect();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertNull(collisions.detect((key, value) -> COLLISION_4.equals(key) && "four".equals(value)));
Assert.assertEquals(
Tuples.pair(COLLISION_1, "one"),
collisions.detect((key, value) -> COLLISION_1.equals(key) && "one".equals(value)));
}
@Override
@Test
public void detect_value()
{
super.detect_value();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertNull(collisions.detect("four"::equals));
Assert.assertEquals("one", collisions.detect("one"::equals));
}
@Override
@Test
public void detectWith()
{
super.detectWith();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertNull(
collisions.detectWith(
(String value, String parameter) -> "value is four".equals(parameter + value),
"value is "));
Assert.assertEquals(
"one",
collisions.detectWith(
(String value, String parameter) -> "value is one".equals(parameter + value),
"value is "));
}
@Override
@Test
public void detectIfNone_value()
{
super.detectIfNone_value();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertEquals(
"if none string",
collisions.detectIfNone(
"four"::equals,
() -> "if none string"));
Assert.assertEquals(
"one",
collisions.detectIfNone(
"one"::equals,
() -> "if none string"));
}
@Override
@Test
public void detectWithIfNone()
{
super.detectWithIfNone();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertEquals(
"if none string",
collisions.detectWithIfNone(
(String value, String parameter) -> "value is four".equals(parameter + value),
"value is ",
() -> "if none string"));
Assert.assertEquals(
"one",
collisions.detectWithIfNone(
(String value, String parameter) -> "value is one".equals(parameter + value),
"value is ",
() -> "if none string"));
}
@Override
@Test
public void anySatisfy()
{
super.anySatisfy();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertFalse(collisions.anySatisfy("four"::equals));
Assert.assertTrue(collisions.anySatisfy("one"::equals));
}
@Override
@Test
public void anySatisfyWith()
{
super.anySatisfyWith();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertTrue(
collisions.anySatisfyWith(
(value, parameter) -> "value is one".equals(parameter + value),
"value is "));
Assert.assertFalse(
collisions.anySatisfyWith(
(value, parameter) -> "value is four".equals(parameter + value),
"value is "));
}
@Override
@Test
public void allSatisfy()
{
super.allSatisfy();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertTrue(collisions.allSatisfy(value -> !value.isEmpty()));
Assert.assertFalse(collisions.allSatisfy(value -> value.length() > 3));
}
@Override
@Test
public void allSatisfyWith()
{
super.allSatisfyWith();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertTrue(collisions.allSatisfyWith(Predicates2.instanceOf(), String.class));
Assert.assertFalse(collisions.allSatisfyWith(String::equals, "one"));
}
@Override
@Test
public void noneSatisfy()
{
super.allSatisfy();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertTrue(collisions.noneSatisfy("four"::equals));
Assert.assertFalse(collisions.noneSatisfy("one"::equals));
}
@Override
@Test
public void noneSatisfyWith()
{
super.allSatisfyWith();
UnifiedMap<Integer, String> collisions = UnifiedMap.<Integer, String>newMap().withKeysValues(COLLISION_1, "one", COLLISION_2, "two", COLLISION_3, "three");
Assert.assertTrue(collisions.noneSatisfyWith(String::equals, "monkey"));
Assert.assertFalse(collisions.allSatisfyWith(String::equals, "one"));
}
@Test
public void trimToSize()
{
UnifiedMap<String, String> map = UnifiedMap.newMap();
MutableMap<String, String> expected = Maps.mutable.empty();
Interval integers = Interval.fromTo(0, 250);
integers.each(each ->
{
map.put(each.toString(), each.toString());
expected.put(each.toString(), each.toString());
});
ArrayIterate.forEach(FREQUENT_COLLISIONS, each ->
{
map.put(each, each);
expected.put(each, each);
});
Assert.assertEquals(expected, map);
Assert.assertEquals(261, map.size());
MutableList<Integer> toRemove = Lists.mutable.withAll(Interval.evensFromTo(0, 20));
toRemove.addAll(Interval.oddsFromTo(35, 55));
toRemove.each(each ->
{
map.remove(each.toString());
expected.remove(each.toString());
});
// First assertion to verify that trim does not happen since, the table is already at the smallest required power of 2.
Assert.assertFalse(map.trimToSize());
Assert.assertEquals(expected, map);
Assert.assertEquals(239, map.size());
Interval.evensFromTo(0, 250).each(each ->
{
map.remove(each.toString());
expected.remove(each.toString());
});
// Second assertion to verify that trim happens since, the table length is less than smallest required power of 2.
Assert.assertTrue(map.trimToSize());
Assert.assertFalse(map.trimToSize());
Assert.assertEquals(expected, map);
Assert.assertEquals(124, map.size());
expected.forEachKey(each -> Assert.assertEquals(each, map.get(each)));
integers.each(each ->
{
map.remove(each.toString());
expected.remove(each.toString());
});
Assert.assertTrue(map.trimToSize());
Assert.assertFalse(map.trimToSize());
Assert.assertEquals(expected, map);
expected.forEachKey(each -> Assert.assertEquals(each, map.get(each)));
map.clear();
expected.clear();
Assert.assertTrue(map.trimToSize());
Interval.zeroTo(20).each(each ->
{
map.put(each.toString(), each.toString());
expected.put(each.toString(), each.toString());
});
Assert.assertFalse(map.trimToSize());
Interval.fromTo(9, 18).each(each ->
{
map.remove(each.toString());
expected.remove(each.toString());
});
Assert.assertTrue(map.trimToSize());
Assert.assertFalse(map.trimToSize());
Assert.assertEquals(expected, map);
expected.forEachKey(each -> Assert.assertEquals(each, map.get(each)));
map.clear();
Assert.assertTrue(map.trimToSize());
Assert.assertTrue(map.isEmpty());
Interval.zeroTo(6).each(each -> map.put(each.toString(), each.toString()));
// Assert that trim does not happen as long as table.size is already as smaller than required
Assert.assertFalse(map.trimToSize());
map.put("7", "7");
map.removeKey("2");
map.removeKey("3");
// Assert that trim does not happen as long as table.size is as smaller as required
Assert.assertFalse(map.trimToSize());
map.removeKey("5");
map.removeKey("7");
Assert.assertTrue(map.trimToSize());
// Inflate the map so that table.length increases to next power of 2 and check that trim does not happen
map.put("2", "2");
map.put("5", "5");
map.put("7", "7");
// Assert that the resized table due to put is the required size and no need to trim that.
Assert.assertFalse(map.trimToSize());
Interval.zeroTo(4).each(each -> map.put(each.toString(), each.toString()));
Interval.oneTo(3).each(each -> map.removeKey(each.toString()));
Assert.assertTrue(map.trimToSize());
Assert.assertEquals(5, map.size());
}
@Override
protected UnifiedMap<Integer, Integer> mapWithCollisionsOfSize(int size)
{
UnifiedMap<Integer, Integer> map = UnifiedMap.newMap(size);
return this.populateMapWithCollisionsOfSize(size, map);
}
private static final class EntrySumProcedure implements Procedure<Map.Entry<Integer, Integer>>
{
private static final long serialVersionUID = 1L;
private final Sum sum;
private EntrySumProcedure(Sum sum)
{
this.sum = sum;
}
@Override
public void value(Map.Entry<Integer, Integer> each)
{
this.sum.add(each.getKey());
this.sum.add(each.getValue());
}
}
}