-
Notifications
You must be signed in to change notification settings - Fork 587
/
UnifiedSetTest.java
693 lines (599 loc) · 27.1 KB
/
UnifiedSetTest.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
/*
* Copyright (c) 2021 Goldman Sachs and others.
* 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.set.mutable;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.Executors;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.Pool;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Procedures;
import org.eclipse.collections.impl.factory.Lists;
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.math.IntegerSum;
import org.eclipse.collections.impl.math.Sum;
import org.eclipse.collections.impl.math.SumProcedure;
import org.eclipse.collections.impl.test.ClassComparer;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.test.domain.Key;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.junit.Assert;
import org.junit.Test;
/**
* JUnit test suite for {@link UnifiedSet}.
*/
public class UnifiedSetTest extends AbstractMutableSetTestCase
{
@Override
protected <T> UnifiedSet<T> newWith(T... littleElements)
{
return UnifiedSet.newSetWith(littleElements);
}
@Override
@Test
public void with()
{
Verify.assertEqualsAndHashCode(
UnifiedSet.newSetWith("1"),
UnifiedSet.newSet().with("1"));
Verify.assertEqualsAndHashCode(
UnifiedSet.newSetWith("1", "2"),
UnifiedSet.newSet().with("1", "2"));
Verify.assertEqualsAndHashCode(
UnifiedSet.newSetWith("1", "2", "3"),
UnifiedSet.newSet().with("1", "2", "3"));
Verify.assertEqualsAndHashCode(
UnifiedSet.newSetWith("1", "2", "3", "4"),
UnifiedSet.newSet().with("1", "2", "3", "4"));
MutableSet<String> list = UnifiedSet.<String>newSet().with("A")
.withAll(Lists.fixedSize.of("1", "2"))
.withAll(Lists.fixedSize.of())
.withAll(Sets.fixedSize.of("3", "4"));
Verify.assertEqualsAndHashCode(UnifiedSet.newSetWith("A", "1", "2", "3", "4"), list);
}
@Test
public void newSet_throws()
{
Assert.assertThrows(IllegalArgumentException.class, () -> new UnifiedSet<Integer>(-1, 0.5f));
Assert.assertThrows(IllegalArgumentException.class, () -> new UnifiedSet<Integer>(1, -0.5f));
Assert.assertThrows(IllegalArgumentException.class, () -> new UnifiedSet<Integer>(1, 0.0f));
Assert.assertThrows(IllegalArgumentException.class, () -> new UnifiedSet<Integer>(1, 1.5f));
}
@Test
public void newSetWithIterable()
{
MutableSet<Integer> integers = UnifiedSet.newSet(Interval.oneTo(3));
Assert.assertEquals(UnifiedSet.newSetWith(1, 2, 3), integers);
}
@Test
public void unifiedSetProperSuperSetOfHashSet()
{
Assert.assertTrue(ClassComparer.isProperSupersetOf(UnifiedSet.class, HashSet.class));
}
@Override
@Test
public void add()
{
super.add();
// force rehashing at each step of adding a new colliding entry
for (int i = 0; i < COLLISIONS.size(); i++)
{
UnifiedSet<Integer> unifiedSet = UnifiedSet.<Integer>newSet(i, 0.75f).withAll(COLLISIONS.subList(0, i));
if (i == 2)
{
unifiedSet.add(Integer.valueOf(1));
}
if (i == 4)
{
unifiedSet.add(Integer.valueOf(1));
unifiedSet.add(Integer.valueOf(2));
}
Integer value = COLLISIONS.get(i);
Assert.assertTrue(unifiedSet.add(value));
}
// Rehashing Case A: a bucket with only one entry and a low capacity forcing a rehash, where the triggering element goes in the bucket
// set up a chained bucket
UnifiedSet<Integer> caseA = UnifiedSet.<Integer>newSet(2).with(COLLISION_1, COLLISION_2);
// clear the bucket to one element
caseA.remove(COLLISION_2);
// increase the occupied count to the threshold
caseA.add(Integer.valueOf(1));
caseA.add(Integer.valueOf(2));
// add the colliding value back and force the rehash
Assert.assertTrue(caseA.add(COLLISION_2));
// Rehashing Case B: a bucket with only one entry and a low capacity forcing a rehash, where the triggering element is not in the chain
// set up a chained bucket
UnifiedSet<Integer> caseB = UnifiedSet.<Integer>newSet(2).with(COLLISION_1, COLLISION_2);
// clear the bucket to one element
caseB.remove(COLLISION_2);
// increase the occupied count to the threshold
caseB.add(Integer.valueOf(1));
caseB.add(Integer.valueOf(2));
// add a new value and force the rehash
Assert.assertTrue(caseB.add(3));
}
@Override
@Test
public void addAllIterable()
{
super.addAllIterable();
// test adding a fully populated chained bucket
MutableSet<Integer> expected = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6, COLLISION_7);
Assert.assertTrue(UnifiedSet.<Integer>newSet().addAllIterable(expected));
// add an odd-sized collection to a set with a small max to ensure that its capacity is maintained after the operation.
UnifiedSet<Integer> tiny = UnifiedSet.newSet(0);
Assert.assertTrue(tiny.addAllIterable(FastList.newListWith(COLLISION_1)));
}
@Test
public void get()
{
UnifiedSet<Integer> set = UnifiedSet.<Integer>newSet(SIZE).withAll(COLLISIONS);
set.removeAll(COLLISIONS);
for (Integer integer : COLLISIONS)
{
Assert.assertNull(set.get(integer));
Assert.assertNull(set.get(null));
set.add(integer);
//noinspection UnnecessaryBoxing,CachedNumberConstructorCall,BoxingBoxedValue
Assert.assertSame(integer, set.get(new Integer(integer)));
}
Assert.assertEquals(COLLISIONS.toSet(), set);
// the pool interface supports getting null keys
UnifiedSet<Integer> chainedWithNull = UnifiedSet.newSetWith(null, COLLISION_1);
Verify.assertContains(null, chainedWithNull);
Assert.assertNull(chainedWithNull.get(null));
// getting a non-existent from a chain with one slot should short-circuit to return null
UnifiedSet<Integer> chainedWithOneSlot = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2);
chainedWithOneSlot.remove(COLLISION_2);
Assert.assertNull(chainedWithOneSlot.get(COLLISION_2));
}
@Test
public void put()
{
int size = MORE_COLLISIONS.size();
for (int i = 1; i <= size; i++)
{
Pool<Integer> unifiedSet = UnifiedSet.<Integer>newSet(1).withAll(MORE_COLLISIONS.subList(0, i - 1));
Integer newValue = MORE_COLLISIONS.get(i - 1);
Assert.assertSame(newValue, unifiedSet.put(newValue));
//noinspection UnnecessaryBoxing,CachedNumberConstructorCall,BoxingBoxedValue
Assert.assertSame(newValue, unifiedSet.put(new Integer(newValue)));
}
// assert that all redundant puts into a each position of chain bucket return the original element added
Pool<Integer> set = UnifiedSet.<Integer>newSet(4).with(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
for (int i = 0; i < set.size(); i++)
{
Integer value = COLLISIONS.get(i);
Assert.assertSame(value, set.put(value));
}
// force rehashing at each step of putting a new colliding entry
for (int i = 0; i < COLLISIONS.size(); i++)
{
Pool<Integer> pool = UnifiedSet.<Integer>newSet(i).withAll(COLLISIONS.subList(0, i));
if (i == 2)
{
pool.put(Integer.valueOf(1));
}
if (i == 4)
{
pool.put(Integer.valueOf(1));
pool.put(Integer.valueOf(2));
}
Integer value = COLLISIONS.get(i);
Assert.assertSame(value, pool.put(value));
}
// cover one case not covered in the above: a bucket with only one entry and a low capacity forcing a rehash
// set up a chained bucket
Pool<Integer> pool = UnifiedSet.<Integer>newSet(2).with(COLLISION_1, COLLISION_2);
// clear the bucket to one element
pool.removeFromPool(COLLISION_2);
// increase the occupied count to the threshold
pool.put(Integer.valueOf(1));
pool.put(Integer.valueOf(2));
// put the colliding value back and force the rehash
Assert.assertSame(COLLISION_2, pool.put(COLLISION_2));
// put chained items into a pool without causing a rehash
Pool<Integer> olympicPool = UnifiedSet.newSet();
Assert.assertSame(COLLISION_1, olympicPool.put(COLLISION_1));
Assert.assertSame(COLLISION_2, olympicPool.put(COLLISION_2));
}
@Test
public void removeFromPool()
{
Pool<Integer> unifiedSet = UnifiedSet.<Integer>newSet(8).withAll(COLLISIONS);
COLLISIONS.reverseForEach(each -> {
Assert.assertNull(unifiedSet.removeFromPool(null));
Assert.assertSame(each, unifiedSet.removeFromPool(each));
Assert.assertNull(unifiedSet.removeFromPool(each));
Assert.assertNull(unifiedSet.removeFromPool(null));
Assert.assertNull(unifiedSet.removeFromPool(COLLISION_10));
});
Assert.assertEquals(UnifiedSet.<Integer>newSet(), unifiedSet);
COLLISIONS.forEach(Procedures.cast(each -> {
Pool<Integer> unifiedSet2 = UnifiedSet.<Integer>newSet(8).withAll(COLLISIONS);
Assert.assertNull(unifiedSet2.removeFromPool(null));
Assert.assertSame(each, unifiedSet2.removeFromPool(each));
Assert.assertNull(unifiedSet2.removeFromPool(each));
Assert.assertNull(unifiedSet2.removeFromPool(null));
Assert.assertNull(unifiedSet2.removeFromPool(COLLISION_10));
}));
// search a chain for a non-existent element
Pool<Integer> chain = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
Assert.assertNull(chain.removeFromPool(COLLISION_5));
// search a deep chain for a non-existent element
Pool<Integer> deepChain = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6, COLLISION_7);
Assert.assertNull(deepChain.removeFromPool(COLLISION_8));
// search for a non-existent element
Pool<Integer> empty = UnifiedSet.newSetWith(COLLISION_1);
Assert.assertNull(empty.removeFromPool(COLLISION_2));
}
@Test
public void serialization()
{
int size = COLLISIONS.size();
for (int i = 1; i < size; i++)
{
MutableSet<Integer> set = UnifiedSet.<Integer>newSet(SIZE).withAll(COLLISIONS.subList(0, i));
Verify.assertPostSerializedEqualsAndHashCode(set);
set.add(null);
Verify.assertPostSerializedEqualsAndHashCode(set);
}
UnifiedSet<Integer> nullBucketZero = UnifiedSet.newSetWith(null, COLLISION_1, COLLISION_2);
Verify.assertPostSerializedEqualsAndHashCode(nullBucketZero);
UnifiedSet<Integer> simpleSetWithNull = UnifiedSet.newSetWith(null, 1, 2);
Verify.assertPostSerializedEqualsAndHashCode(simpleSetWithNull);
}
@Test
public void null_behavior()
{
UnifiedSet<Integer> unifiedSet = UnifiedSet.<Integer>newSet(10).withAll(MORE_COLLISIONS);
MORE_COLLISIONS.clone().reverseForEach(each -> {
Assert.assertTrue(unifiedSet.add(null));
Assert.assertFalse(unifiedSet.add(null));
Verify.assertContains(null, unifiedSet);
Verify.assertPostSerializedEqualsAndHashCode(unifiedSet);
Assert.assertTrue(unifiedSet.remove(null));
Assert.assertFalse(unifiedSet.remove(null));
Verify.assertNotContains(null, unifiedSet);
Verify.assertPostSerializedEqualsAndHashCode(unifiedSet);
Assert.assertNull(unifiedSet.put(null));
Assert.assertNull(unifiedSet.put(null));
Assert.assertNull(unifiedSet.removeFromPool(null));
Assert.assertNull(unifiedSet.removeFromPool(null));
Verify.assertContains(each, unifiedSet);
Assert.assertTrue(unifiedSet.remove(each));
Assert.assertFalse(unifiedSet.remove(each));
Verify.assertNotContains(each, unifiedSet);
});
}
@Override
@Test
public void equalsAndHashCode()
{
super.equalsAndHashCode();
UnifiedSet<Integer> singleCollisionBucket = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2);
singleCollisionBucket.remove(COLLISION_2);
Assert.assertEquals(singleCollisionBucket, UnifiedSet.newSetWith(COLLISION_1));
Verify.assertEqualsAndHashCode(UnifiedSet.newSetWith(null, COLLISION_1, COLLISION_2, COLLISION_3), UnifiedSet.newSetWith(null, COLLISION_1, COLLISION_2, COLLISION_3));
Verify.assertEqualsAndHashCode(UnifiedSet.newSetWith(COLLISION_1, null, COLLISION_2, COLLISION_3), UnifiedSet.newSetWith(COLLISION_1, null, COLLISION_2, COLLISION_3));
Verify.assertEqualsAndHashCode(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, null, COLLISION_3), UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, null, COLLISION_3));
Verify.assertEqualsAndHashCode(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, null), UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, null));
}
@Test
public void constructor_from_UnifiedSet()
{
Verify.assertEqualsAndHashCode(new HashSet<>(MORE_COLLISIONS), UnifiedSet.newSet(MORE_COLLISIONS));
}
@Test
public void copyConstructor()
{
// test copying a chained bucket
MutableSet<Integer> set = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6, COLLISION_7);
Verify.assertEqualsAndHashCode(set, UnifiedSet.newSet(set));
}
@Test
public void newSet()
{
for (int i = 1; i < 17; i++)
{
this.assertPresizedSet(i, 0.75f);
}
this.assertPresizedSet(31, 0.75f);
this.assertPresizedSet(32, 0.75f);
this.assertPresizedSet(34, 0.75f);
this.assertPresizedSet(60, 0.75f);
this.assertPresizedSet(64, 0.70f);
this.assertPresizedSet(68, 0.70f);
this.assertPresizedSet(60, 0.70f);
this.assertPresizedSet(1025, 0.80f);
this.assertPresizedSet(1024, 0.80f);
this.assertPresizedSet(1025, 0.80f);
this.assertPresizedSet(1024, 0.805f);
}
private void assertPresizedSet(int initialCapacity, float loadFactor)
{
try
{
Field tableField = UnifiedSet.class.getDeclaredField("table");
tableField.setAccessible(true);
Object[] table = (Object[]) tableField.get(UnifiedSet.newSet(initialCapacity, loadFactor));
int size = (int) Math.ceil(initialCapacity / loadFactor);
int capacity = 1;
while (capacity < size)
{
capacity <<= 1;
}
Assert.assertEquals(capacity, table.length, 0.00);
}
catch (SecurityException ignored)
{
Assert.fail("Unable to modify the visibility of the table on UnifiedSet");
}
catch (NoSuchFieldException ignored)
{
Assert.fail("No field named table UnifiedSet");
}
catch (IllegalAccessException ignored)
{
Assert.fail("No access the field table in UnifiedSet");
}
}
@Test
public void batchForEach()
{
//Testing batch size of 1 to 16 with no chains
UnifiedSet<Integer> set = UnifiedSet.<Integer>newSet(10).with(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
for (int sectionCount = 1; sectionCount <= 16; ++sectionCount)
{
Sum sum = new IntegerSum(0);
for (int sectionIndex = 0; sectionIndex < sectionCount; ++sectionIndex)
{
set.batchForEach(new SumProcedure<>(sum), sectionIndex, sectionCount);
}
Assert.assertEquals(55, sum.getValue());
}
//Testing 1 batch with chains
Sum sum2 = new IntegerSum(0);
UnifiedSet<Integer> set2 = UnifiedSet.<Integer>newSet(3).with(COLLISION_1, COLLISION_2, COLLISION_3, 1, 2);
int numBatches = set2.getBatchCount(100);
for (int i = 0; i < numBatches; ++i)
{
set2.batchForEach(new SumProcedure<>(sum2), i, numBatches);
}
Assert.assertEquals(1, numBatches);
Assert.assertEquals(54, sum2.getValue());
//Testing batch size of 3 with chains and uneven last batch
Sum sum3 = new IntegerSum(0);
UnifiedSet<Integer> set3 = UnifiedSet.<Integer>newSet(4, 1.0F).with(COLLISION_1, COLLISION_2, 1, 2, 3, 4, 5);
int numBatches2 = set3.getBatchCount(3);
for (int i = 0; i < numBatches2; ++i)
{
set3.batchForEach(new SumProcedure<>(sum3), i, numBatches2);
}
Assert.assertEquals(32, sum3.getValue());
//Test batchForEach on empty set, it should simply do nothing and not throw any exceptions
Sum sum4 = new IntegerSum(0);
UnifiedSet<Integer> set4 = UnifiedSet.newSet();
set4.batchForEach(new SumProcedure<>(sum4), 0, set4.getBatchCount(1));
Assert.assertEquals(0, sum4.getValue());
}
@Override
@Test
public void toArray()
{
super.toArray();
int size = COLLISIONS.size();
for (int i = 1; i < size; i++)
{
MutableSet<Integer> set = UnifiedSet.<Integer>newSet(SIZE).withAll(COLLISIONS.subList(0, i));
Object[] objects = set.toArray();
Assert.assertEquals(set, UnifiedSet.newSetWith(objects));
}
MutableSet<Integer> deepChain = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6);
Assert.assertArrayEquals(new Integer[]{COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6}, deepChain.toArray());
MutableSet<Integer> minimumChain = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2);
minimumChain.remove(COLLISION_2);
Assert.assertArrayEquals(new Integer[]{COLLISION_1}, minimumChain.toArray());
MutableSet<Integer> set = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
Integer[] target = {Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1)};
Integer[] actual = set.toArray(target);
ArrayIterate.sort(actual, actual.length, Comparators.safeNullsHigh(Integer::compareTo));
Assert.assertArrayEquals(new Integer[]{COLLISION_1, 1, COLLISION_2, COLLISION_3, COLLISION_4, null}, actual);
}
@Test
public void iterator_remove()
{
int size = MORE_COLLISIONS.size();
for (int i = 0; i < size; i++)
{
MutableSet<Integer> actual = UnifiedSet.<Integer>newSet(SIZE).withAll(MORE_COLLISIONS);
Iterator<Integer> iterator = actual.iterator();
for (int j = 0; j <= i; j++)
{
Assert.assertTrue(iterator.hasNext());
iterator.next();
}
iterator.remove();
MutableSet<Integer> expected = UnifiedSet.newSet(MORE_COLLISIONS);
expected.remove(MORE_COLLISIONS.get(i));
Assert.assertEquals(expected, actual);
}
// remove the last element from within a 2-level long chain that is fully populated
MutableSet<Integer> set = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6, COLLISION_7);
Iterator<Integer> iterator1 = set.iterator();
for (int i = 0; i < 7; i++)
{
iterator1.next();
}
iterator1.remove();
Assert.assertEquals(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6), set);
// remove the second-to-last element from a 2-level long chain that that has one empty slot
Iterator<Integer> iterator2 = set.iterator();
for (int i = 0; i < 6; i++)
{
iterator2.next();
}
iterator2.remove();
Assert.assertEquals(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5), set);
//Testing removing the last element in a fully populated chained bucket
MutableSet<Integer> set2 = this.newWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
Iterator<Integer> iterator3 = set2.iterator();
for (int i = 0; i < 3; ++i)
{
iterator3.next();
}
iterator3.next();
iterator3.remove();
Verify.assertSetsEqual(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3), set2);
}
@Test
public void setKeyPreservation()
{
Key key = new Key("key");
Key duplicateKey1 = new Key("key");
MutableSet<Key> set1 = UnifiedSet.<Key>newSet().with(key, duplicateKey1);
Verify.assertSize(1, set1);
Verify.assertContains(key, set1);
Assert.assertSame(key, set1.getFirst());
Key duplicateKey2 = new Key("key");
MutableSet<Key> set2 = UnifiedSet.<Key>newSet().with(key, duplicateKey1, duplicateKey2);
Verify.assertSize(1, set2);
Verify.assertContains(key, set2);
Assert.assertSame(key, set2.getFirst());
Key duplicateKey3 = new Key("key");
MutableSet<Key> set3 = UnifiedSet.<Key>newSet().with(key, new Key("not a dupe"), duplicateKey3);
Verify.assertSize(2, set3);
Verify.assertContainsAll(set3, key, new Key("not a dupe"));
Assert.assertSame(key, set3.detect(key::equals));
}
@Test
public void withSameIfNotModified()
{
UnifiedSet<Integer> integers = UnifiedSet.newSet();
Assert.assertEquals(UnifiedSet.newSetWith(1, 2), integers.with(1, 2));
Assert.assertEquals(UnifiedSet.newSetWith(1, 2, 3, 4), integers.with(2, 3, 4));
Assert.assertSame(integers, integers.with(5, 6, 7));
}
@Override
@Test
public void retainAll()
{
super.retainAll();
MutableSet<Object> setWithNull = this.newWith((Object) null);
Assert.assertFalse(setWithNull.retainAll(FastList.newListWith((Object) null)));
Assert.assertEquals(UnifiedSet.newSetWith((Object) null), setWithNull);
}
@Test(expected = NullPointerException.class)
public void asParallelNullExecutorService()
{
this.newWith(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).asParallel(null, 2);
}
@Test(expected = IllegalArgumentException.class)
public void asParallelLessThanOneBatchSize()
{
this.newWith(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).asParallel(Executors.newFixedThreadPool(10), 0);
}
@Override
@Test
public void getFirst()
{
super.getFirst();
int size = MORE_COLLISIONS.size();
for (int i = 1; i <= size - 1; i++)
{
MutableSet<Integer> unifiedSet = UnifiedSet.<Integer>newSet(1).withAll(MORE_COLLISIONS.subList(0, i));
Assert.assertSame(MORE_COLLISIONS.get(0), unifiedSet.getFirst());
}
}
@Override
@Test
public void getLast()
{
super.getLast();
int size = MORE_COLLISIONS.size();
for (int i = 1; i <= size - 1; i++)
{
MutableSet<Integer> unifiedSet = UnifiedSet.<Integer>newSet(1).withAll(MORE_COLLISIONS.subList(0, i));
Assert.assertSame(MORE_COLLISIONS.get(i - 1), unifiedSet.getLast());
}
MutableSet<Integer> chainedWithOneSlot = UnifiedSet.newSetWith(COLLISION_1, COLLISION_2);
chainedWithOneSlot.remove(COLLISION_2);
Assert.assertSame(COLLISION_1, chainedWithOneSlot.getLast());
}
@Test
public void trimToSize()
{
UnifiedSet<String> set = UnifiedSet.newSet();
MutableSet<String> expected = Sets.mutable.empty();
Interval integers = Interval.fromTo(0, 250);
integers.each(each ->
{
set.add(each.toString());
expected.add(each.toString());
});
ArrayIterate.forEach(FREQUENT_COLLISIONS, each ->
{
set.add(each);
expected.add(each);
});
Assert.assertEquals(expected, set);
Assert.assertEquals(261, set.size());
MutableList<Integer> toRemove = Lists.mutable.withAll(Interval.evensFromTo(0, 20));
toRemove.addAll(Interval.oddsFromTo(35, 55));
toRemove.each(each ->
{
set.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(set.trimToSize());
Assert.assertEquals(239, set.size());
Assert.assertEquals(expected, set);
Interval.evensFromTo(0, 250).each(each ->
{
set.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(set.trimToSize());
Assert.assertFalse(set.trimToSize());
Assert.assertEquals(expected, set);
Assert.assertEquals(124, set.size());
expected.each(each -> Assert.assertEquals(each, set.get(each)));
integers.each(each ->
{
set.remove(each.toString());
expected.remove(each.toString());
});
Assert.assertTrue(set.trimToSize());
Assert.assertFalse(set.trimToSize());
Assert.assertEquals(expected, set);
expected.each(each -> Assert.assertEquals(each, set.get(each)));
set.clear();
Assert.assertTrue(set.trimToSize());
Interval.oneTo(4).each(each -> set.add(each.toString()));
// Assert that trim does not happen after puts
Assert.assertFalse(set.trimToSize());
set.remove("1");
set.remove("2");
Assert.assertTrue(set.trimToSize());
set.add("1");
// Assert that the resized table due to put is the required size and no need to trim that.
Assert.assertFalse(set.trimToSize());
Interval.zeroTo(4).each(each -> set.add(each.toString()));
Interval.oneTo(3).each(each -> set.remove(each.toString()));
Assert.assertTrue(set.trimToSize());
Assert.assertEquals(2, set.size());
}
}