/
AbstractImmutableCollectionTestCase.java
846 lines (741 loc) · 32.1 KB
/
AbstractImmutableCollectionTestCase.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
846
/*
* Copyright (c) 2017 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.collection.immutable;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.collection.ImmutableCollection;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.collection.primitive.ImmutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.ImmutableByteCollection;
import org.eclipse.collections.api.collection.primitive.ImmutableCharCollection;
import org.eclipse.collections.api.collection.primitive.ImmutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.ImmutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.ImmutableIntCollection;
import org.eclipse.collections.api.collection.primitive.ImmutableLongCollection;
import org.eclipse.collections.api.collection.primitive.ImmutableShortCollection;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.primitive.ImmutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ImmutableObjectLongMap;
import org.eclipse.collections.api.partition.PartitionImmutableCollection;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.impl.Counter;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.IntegerPredicates;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.block.factory.PrimitiveFunctions;
import org.eclipse.collections.impl.block.factory.Procedures;
import org.eclipse.collections.impl.block.function.AddFunction;
import org.eclipse.collections.impl.block.function.PassThruFunction0;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.SortedBags;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.set.sorted.mutable.TreeSortedSet;
import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
import static org.eclipse.collections.impl.factory.Iterables.iList;
public abstract class AbstractImmutableCollectionTestCase
{
public static final Predicate<Integer> ERROR_THROWING_PREDICATE = each -> {
throw new AssertionError();
};
public static final Predicates2<Integer, Class<Integer>> ERROR_THROWING_PREDICATE_2 = new Predicates2<Integer, Class<Integer>>()
{
public boolean accept(Integer argument1, Class<Integer> argument2)
{
throw new AssertionError();
}
};
protected abstract ImmutableCollection<Integer> classUnderTest();
protected abstract <T> MutableCollection<T> newMutable();
/**
* @since 9.0
*/
@Test
public void countBy()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Bag<Integer> results = integers.countBy(each -> each);
Verify.assertSize(integers.size(), results);
Bag<Integer> results2 = integers.countBy(each -> each, SortedBags.mutable.empty());
Verify.assertSize(integers.size(), results2);
}
/**
* @since 9.0
*/
@Test
public void countByWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Bag<Integer> results = integers.countByWith((each, parm) -> each, null);
Verify.assertSize(integers.size(), results);
Bag<Integer> results2 = integers.countByWith((each, parm) -> each, null, SortedBags.mutable.empty());
Verify.assertSize(integers.size(), results2);
}
@Test
public void selectWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(
this.<Integer>newMutable().withAll(integers).select(IntegerPredicates.isOdd()),
integers.selectWith(Predicates2.in(), iList(1, 3, 5, 7, 9)));
}
@Test
public void selectWith_target()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(
this.<Integer>newMutable().with(101).withAll(integers).select(IntegerPredicates.isOdd()),
integers.selectWith(Predicates2.in(), iList(1, 3, 5, 7, 9), this.<Integer>newMutable().with(101)));
}
@Test
public void rejectWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(
this.<Integer>newMutable().withAll(integers).reject(IntegerPredicates.isOdd()),
integers.rejectWith(Predicates2.in(), iList(1, 3, 5, 7, 9)));
}
@Test
public void rejectWith_target()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(
this.<Integer>newMutable().with(100).withAll(integers).reject(IntegerPredicates.isOdd()),
integers.rejectWith(Predicates2.in(), iList(1, 3, 5, 7, 9), this.<Integer>newMutable().with(100)));
}
@Test
public void partition()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
PartitionImmutableCollection<Integer> partition = integers.partition(IntegerPredicates.isOdd());
Assert.assertEquals(integers.select(IntegerPredicates.isOdd()), partition.getSelected());
Assert.assertEquals(integers.select(IntegerPredicates.isEven()), partition.getRejected());
}
@Test
public void partitionWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
PartitionImmutableCollection<Integer> partition = integers.partitionWith(Predicates2.in(), integers.select(IntegerPredicates.isOdd()));
Assert.assertEquals(integers.select(IntegerPredicates.isOdd()), partition.getSelected());
Assert.assertEquals(integers.select(IntegerPredicates.isEven()), partition.getRejected());
}
@Test
public void collectWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableCollection<String> expected = integers.collect(Functions.chain(String::valueOf, string -> string + "!"));
ImmutableCollection<String> actual = integers.collectWith((argument1, argument2) -> argument1 + argument2, "!");
Assert.assertEquals(expected, actual);
}
@Test
public void collect_target()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
MutableCollection<String> strings = this.newMutable();
integers.forEach(Procedures.cast(each -> strings.add(each.toString())));
MutableCollection<String> target = this.newMutable();
MutableCollection<String> actual = integers.collect(String::valueOf, target);
Assert.assertEquals(strings, actual);
Assert.assertSame(target, actual);
}
@Test
public void collectWith_target()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
MutableCollection<String> expected = this.<String>newMutable().with("?").withAll(integers.collect(Functions.chain(String::valueOf, string -> string + "!")));
MutableCollection<String> targetCollection = this.<String>newMutable().with("?");
MutableCollection<String> actual = integers.collectWith((argument1, argument2) -> argument1 + argument2, "!", targetCollection);
Assert.assertEquals(expected, actual);
Assert.assertSame(targetCollection, actual);
}
@Test
public void injectInto()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Integer result = integers.injectInto(0, AddFunction.INTEGER);
Assert.assertEquals(FastList.newList(integers).injectInto(0, AddFunction.INTEGER_TO_INT), result.intValue());
}
@Test
public void injectIntoInt()
{
Assert.assertEquals(
this.classUnderTest().injectInto(0, AddFunction.INTEGER).longValue(),
this.classUnderTest().injectInto(0, AddFunction.INTEGER_TO_INT));
}
@Test
public void injectIntoLong()
{
Assert.assertEquals(
this.classUnderTest().injectInto(0, AddFunction.INTEGER).longValue(),
this.classUnderTest().injectInto(0, AddFunction.INTEGER_TO_LONG));
}
@Test
public void injectIntoDouble()
{
Assert.assertEquals(
this.classUnderTest().injectInto(0, AddFunction.INTEGER).doubleValue(),
this.classUnderTest().injectInto(0, AddFunction.INTEGER_TO_DOUBLE),
0.0);
}
@Test
public void injectIntoFloat()
{
Assert.assertEquals(
this.classUnderTest().injectInto(0, AddFunction.INTEGER).floatValue(),
this.classUnderTest().injectInto(0, AddFunction.INTEGER_TO_FLOAT),
0.0);
}
@Test
public void sumFloat()
{
Assert.assertEquals(
this.classUnderTest().injectInto(0, AddFunction.INTEGER_TO_FLOAT),
this.classUnderTest().sumOfFloat(Integer::floatValue),
0.0);
}
@Test
public void sumDouble()
{
Assert.assertEquals(
this.classUnderTest().injectInto(0, AddFunction.INTEGER_TO_DOUBLE),
this.classUnderTest().sumOfDouble(Integer::doubleValue),
0.0);
}
@Test
public void sumInteger()
{
Assert.assertEquals(
this.classUnderTest().injectInto(0, AddFunction.INTEGER_TO_INT),
this.classUnderTest().sumOfInt(integer -> integer));
}
@Test
public void sumLong()
{
Assert.assertEquals(
this.classUnderTest().injectInto(0, AddFunction.INTEGER_TO_LONG),
this.classUnderTest().sumOfLong(Integer::longValue));
}
@Test
public void sumByInt()
{
MutableCollection<Integer> integers = this.newMutable();
integers.addAllIterable(FastList.newListWith(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
ImmutableCollection<Integer> values = integers.toImmutable();
ImmutableObjectLongMap<Integer> result = values.sumByInt(i -> i % 2, e -> e);
Assert.assertEquals(25, result.get(1));
Assert.assertEquals(30, result.get(0));
}
@Test
public void sumByFloat()
{
MutableCollection<Integer> integers = this.newMutable();
integers.addAllIterable(FastList.newListWith(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
ImmutableCollection<Integer> values = integers.toImmutable();
ImmutableObjectDoubleMap<Integer> result = values.sumByFloat(f -> f % 2, e -> e);
Assert.assertEquals(25.0f, result.get(1), 0.0);
Assert.assertEquals(30.0f, result.get(0), 0.0);
}
@Test
public void sumByFloatConsistentRounding()
{
MutableList<Integer> group1 = Interval.oneTo(100_000).toList().shuffleThis();
MutableList<Integer> group2 = Interval.fromTo(100_001, 200_000).toList().shuffleThis();
MutableList<Integer> integers = Lists.mutable.withAll(group1);
integers.addAll(group2);
ImmutableCollection<Integer> values = integers.toImmutable();
ImmutableObjectDoubleMap<Integer> result = values.sumByFloat(
integer -> integer > 100_000 ? 2 : 1,
integer -> {
Integer i = integer > 100_000 ? integer - 100_000 : integer;
return 1.0f / (i.floatValue() * i.floatValue() * i.floatValue() * i.floatValue());
});
// The test only ensures the consistency/stability of rounding. This is not meant to test the "correctness" of the float calculation result.
// Indeed the lower bits of this calculation result are always incorrect due to the information loss of original float values.
Assert.assertEquals(
1.082323233761663,
result.get(1),
1.0e-15);
Assert.assertEquals(
1.082323233761663,
result.get(2),
1.0e-15);
}
@Test
public void sumByLong()
{
MutableCollection<Integer> integers = this.newMutable();
integers.addAllIterable(FastList.newListWith(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
ImmutableCollection<Integer> values = integers.toImmutable();
ImmutableObjectLongMap<Integer> result = values.sumByLong(l -> l % 2, e -> e);
Assert.assertEquals(25, result.get(1));
Assert.assertEquals(30, result.get(0));
}
@Test
public void sumByDouble()
{
MutableCollection<Integer> integers = this.newMutable();
integers.addAllIterable(FastList.newListWith(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
ImmutableCollection<Integer> values = integers.toImmutable();
ImmutableObjectDoubleMap<Integer> result = values.sumByDouble(d -> d % 2, e -> e);
Assert.assertEquals(25.0d, result.get(1), 0.0);
Assert.assertEquals(30.0d, result.get(0), 0.0);
}
@Test
public void sumByDoubleConsistentRounding()
{
MutableList<Integer> group1 = Interval.oneTo(100_000).toList().shuffleThis();
MutableList<Integer> group2 = Interval.fromTo(100_001, 200_000).toList().shuffleThis();
MutableList<Integer> integers = Lists.mutable.withAll(group1);
integers.addAll(group2);
ImmutableCollection<Integer> values = integers.toImmutable();
ImmutableObjectDoubleMap<Integer> result = values.sumByDouble(
integer -> integer > 100_000 ? 2 : 1,
integer -> {
Integer i = integer > 100_000 ? integer - 100_000 : integer;
return 1.0d / (i.doubleValue() * i.doubleValue() * i.doubleValue() * i.doubleValue());
});
Assert.assertEquals(
1.082323233711138,
result.get(1),
1.0e-15);
Assert.assertEquals(
1.082323233711138,
result.get(2),
1.0e-15);
}
@Test
public void makeString()
{
Assert.assertEquals(FastList.newList(this.classUnderTest()).toString(), '[' + this.classUnderTest().makeString() + ']');
Assert.assertEquals(FastList.newList(this.classUnderTest()).toString(), '[' + this.classUnderTest().makeString(", ") + ']');
Assert.assertEquals(FastList.newList(this.classUnderTest()).toString(), this.classUnderTest().makeString("[", ", ", "]"));
}
@Test
public void appendString()
{
Appendable builder1 = new StringBuilder();
this.classUnderTest().appendString(builder1);
Assert.assertEquals(FastList.newList(this.classUnderTest()).toString(), '[' + builder1.toString() + ']');
Appendable builder2 = new StringBuilder();
this.classUnderTest().appendString(builder2, ", ");
Assert.assertEquals(FastList.newList(this.classUnderTest()).toString(), '[' + builder2.toString() + ']');
Appendable builder3 = new StringBuilder();
this.classUnderTest().appendString(builder3, "[", ", ", "]");
Assert.assertEquals(FastList.newList(this.classUnderTest()).toString(), builder3.toString());
}
@Test
public void testToString()
{
Assert.assertEquals(FastList.newList(this.classUnderTest()).toString(), this.classUnderTest().toString());
}
@Test
public void select()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(integers, integers.select(Predicates.lessThan(integers.size() + 1)));
Verify.assertIterableEmpty(integers.select(Predicates.greaterThan(integers.size())));
}
@Test
public void selectInstancesOf()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableCollection<Integer> result = integers.selectInstancesOf(Integer.class);
Assert.assertEquals(integers, result);
}
@Test
public void reject()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Verify.assertIterableEmpty(integers.reject(Predicates.lessThan(integers.size() + 1)));
Assert.assertEquals(integers, integers.reject(Predicates.greaterThan(integers.size())));
}
@Test
public void collect()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(integers, integers.collect(Functions.getIntegerPassThru()));
}
@Test
public void collectBoolean()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableBooleanCollection immutableCollection = integers.collectBoolean(PrimitiveFunctions.integerIsPositive());
Verify.assertSize(1, immutableCollection);
}
@Test
public void collectByte()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableByteCollection immutableCollection = integers.collectByte(PrimitiveFunctions.unboxIntegerToByte());
Verify.assertSize(integers.size(), immutableCollection);
Assert.assertEquals(integers, immutableCollection.collect(Integer::valueOf));
}
@Test
public void collectChar()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableCharCollection immutableCollection = integers.collectChar(PrimitiveFunctions.unboxIntegerToChar());
Verify.assertSize(integers.size(), immutableCollection);
Assert.assertEquals(integers, immutableCollection.collect(Integer::valueOf));
}
@Test
public void collectDouble()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableDoubleCollection immutableCollection = integers.collectDouble(PrimitiveFunctions.unboxIntegerToDouble());
Verify.assertSize(integers.size(), immutableCollection);
Assert.assertEquals(integers, immutableCollection.collect(doubleParameter -> Integer.valueOf((int) doubleParameter)));
}
@Test
public void collectFloat()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableFloatCollection immutableCollection = integers.collectFloat(PrimitiveFunctions.unboxIntegerToFloat());
Verify.assertSize(integers.size(), immutableCollection);
Assert.assertEquals(integers, immutableCollection.collect(floatParameter -> Integer.valueOf((int) floatParameter)));
}
@Test
public void collectInt()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableIntCollection immutableCollection = integers.collectInt(PrimitiveFunctions.unboxIntegerToInt());
Verify.assertSize(integers.size(), immutableCollection);
Assert.assertEquals(integers, immutableCollection.collect(Integer::valueOf));
}
@Test
public void collectLong()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableLongCollection immutableCollection = integers.collectLong(PrimitiveFunctions.unboxIntegerToLong());
Verify.assertSize(integers.size(), immutableCollection);
Assert.assertEquals(integers, immutableCollection.collect(longParameter -> Integer.valueOf((int) longParameter)));
}
@Test
public void collectShort()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
ImmutableShortCollection immutableCollection = integers.collectShort(PrimitiveFunctions.unboxIntegerToShort());
Verify.assertSize(integers.size(), immutableCollection);
Assert.assertEquals(integers, immutableCollection.collect(Integer::valueOf));
}
@Test
public void flatCollect()
{
RichIterable<String> actual = this.classUnderTest().flatCollect(integer -> Lists.fixedSize.of(String.valueOf(integer)));
ImmutableCollection<String> expected = this.classUnderTest().collect(String::valueOf);
Assert.assertEquals(expected, actual);
}
@Test(expected = IllegalArgumentException.class)
public void chunk_zero_throws()
{
this.classUnderTest().chunk(0);
}
@Test
public void detect()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(Integer.valueOf(1), integers.detect(Predicates.equal(1)));
Assert.assertNull(integers.detect(Predicates.equal(integers.size() + 1)));
}
@Test
public void detectWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(Integer.valueOf(1), integers.detectWith(Object::equals, Integer.valueOf(1)));
Assert.assertNull(integers.detectWith(Object::equals, Integer.valueOf(integers.size() + 1)));
FastList<String> strings = FastList.newListWith("1", "2", "3");
Assert.assertEquals("1", strings.detectWith(Object::equals, "1"));
}
@Test
public void detectIfNone()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Function0<Integer> function = new PassThruFunction0<>(integers.size() + 1);
Assert.assertEquals(Integer.valueOf(1), integers.detectIfNone(Predicates.equal(1), function));
Assert.assertEquals(Integer.valueOf(integers.size() + 1), integers.detectIfNone(Predicates.equal(integers.size() + 1), function));
}
@Test
public void detectWithIfNone()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Integer sum = Integer.valueOf(integers.size() + 1);
Function0<Integer> function = new PassThruFunction0<>(sum);
Assert.assertEquals(Integer.valueOf(1), integers.detectWithIfNone(Object::equals, Integer.valueOf(1), function));
Assert.assertEquals(sum, integers.detectWithIfNone(Object::equals, sum, function));
}
@Test
public void allSatisfy()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertTrue(integers.allSatisfy(Integer.class::isInstance));
Assert.assertFalse(integers.allSatisfy(Integer.valueOf(0)::equals));
}
@Test
public void allSatisfyWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertTrue(integers.allSatisfyWith(Predicates2.instanceOf(), Integer.class));
Assert.assertFalse(integers.allSatisfyWith(Object::equals, 0));
}
@Test
public void noneSatisfy()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertTrue(integers.noneSatisfy(String.class::isInstance));
Assert.assertFalse(integers.noneSatisfy(Integer.valueOf(1)::equals));
}
@Test
public void noneSatisfyWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertTrue(integers.noneSatisfyWith(Predicates2.instanceOf(), String.class));
Assert.assertFalse(integers.noneSatisfyWith(Object::equals, 1));
}
@Test
public void anySatisfy()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertFalse(integers.anySatisfy(String.class::isInstance));
Assert.assertTrue(integers.anySatisfy(Integer.class::isInstance));
}
@Test
public void anySatisfyWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertFalse(integers.anySatisfyWith(Predicates2.instanceOf(), String.class));
Assert.assertTrue(integers.anySatisfyWith(Predicates2.instanceOf(), Integer.class));
}
@Test
public void count()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(integers.size(), integers.count(Integer.class::isInstance));
Assert.assertEquals(0, integers.count(String.class::isInstance));
}
@Test
public void countWith()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(integers.size(), integers.countWith(Predicates2.instanceOf(), Integer.class));
Assert.assertEquals(0, integers.countWith(Predicates2.instanceOf(), String.class));
}
@Test
public void collectIf()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(integers, integers.collectIf(Integer.class::isInstance, Functions.getIntegerPassThru()));
}
@Test
public void getFirst()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(Integer.valueOf(1), integers.getFirst());
}
@Test
public void getLast()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(Integer.valueOf(integers.size()), integers.getLast());
}
@Test
public void isEmpty()
{
ImmutableCollection<Integer> immutableCollection = this.classUnderTest();
Assert.assertFalse(immutableCollection.isEmpty());
Assert.assertTrue(immutableCollection.notEmpty());
}
@Test
public void iterator()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Iterator<Integer> iterator = integers.iterator();
for (int i = 0; iterator.hasNext(); i++)
{
Integer integer = iterator.next();
Assert.assertEquals(i + 1, integer.intValue());
}
Verify.assertThrows(NoSuchElementException.class, (Runnable) iterator::next);
}
@Test
public void toArray()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
MutableList<Integer> copy = FastList.newList(integers);
Assert.assertArrayEquals(integers.toArray(), copy.toArray());
Assert.assertArrayEquals(integers.toArray(new Integer[integers.size()]), copy.toArray(new Integer[integers.size()]));
}
@Test
public void toSortedList()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
MutableList<Integer> copy = FastList.newList(integers);
MutableList<Integer> list = integers.toSortedList(Collections.reverseOrder());
Assert.assertEquals(copy.sortThis(Collections.reverseOrder()), list);
MutableList<Integer> list2 = integers.toSortedList();
Assert.assertEquals(copy.sortThis(), list2);
}
@Test
public void toSortedSet()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
MutableSortedSet<Integer> set = integers.toSortedSet();
Verify.assertListsEqual(integers.toSortedList(), set.toList());
}
@Test
public void toSortedSetWithComparator()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
MutableSortedSet<Integer> set = integers.toSortedSet(Comparators.reverseNaturalOrder());
Assert.assertEquals(integers.toSet(), set);
Assert.assertEquals(integers.toSortedList(Comparators.reverseNaturalOrder()), set.toList());
}
@Test
public void toSortedSetBy()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
MutableSortedSet<Integer> set = integers.toSortedSetBy(String::valueOf);
Verify.assertSortedSetsEqual(TreeSortedSet.newSet(integers), set);
}
@Test
public void forLoop()
{
ImmutableCollection<Integer> immutableCollection = this.classUnderTest();
for (Integer each : immutableCollection)
{
Assert.assertNotNull(each);
}
}
private ImmutableCollection<Integer> classUnderTestWithNull()
{
return this.classUnderTest().reject(Integer.valueOf(1)::equals).newWith(null);
}
@Test(expected = NullPointerException.class)
public void min_null_throws()
{
this.classUnderTestWithNull().min(Integer::compareTo);
}
@Test(expected = NullPointerException.class)
public void max_null_throws()
{
this.classUnderTestWithNull().max(Integer::compareTo);
}
@Test
public void min()
{
Assert.assertEquals(Integer.valueOf(1), this.classUnderTest().min(Integer::compareTo));
}
@Test
public void max()
{
Assert.assertEquals(Integer.valueOf(1), this.classUnderTest().max(Comparators.reverse(Integer::compareTo)));
}
@Test(expected = NullPointerException.class)
public void min_null_throws_without_comparator()
{
this.classUnderTestWithNull().min();
}
@Test(expected = NullPointerException.class)
public void max_null_throws_without_comparator()
{
this.classUnderTestWithNull().max();
}
@Test
public void min_without_comparator()
{
Assert.assertEquals(Integer.valueOf(1), this.classUnderTest().min());
}
@Test
public void max_without_comparator()
{
Assert.assertEquals(Integer.valueOf(this.classUnderTest().size()), this.classUnderTest().max());
}
@Test
public void minBy()
{
Assert.assertEquals(Integer.valueOf(1), this.classUnderTest().minBy(String::valueOf));
}
@Test
public void maxBy()
{
Assert.assertEquals(Integer.valueOf(this.classUnderTest().size()), this.classUnderTest().maxBy(Functions.getIntegerPassThru()));
}
@Test
public void iteratorRemove()
{
Verify.assertThrows(UnsupportedOperationException.class, () -> this.classUnderTest().iterator().remove());
}
@Test
public void add()
{
Verify.assertThrows(UnsupportedOperationException.class, () -> ((Collection<Integer>) this.classUnderTest()).add(1));
}
@Test
public void remove()
{
Verify.assertThrows(UnsupportedOperationException.class, () -> ((Collection<Integer>) this.classUnderTest()).remove(Integer.valueOf(1)));
}
@Test
public void clear()
{
Verify.assertThrows(UnsupportedOperationException.class, () -> ((Collection<Integer>) this.classUnderTest()).clear());
}
@Test
public void removeAll()
{
Verify.assertThrows(UnsupportedOperationException.class, () -> ((Collection<Integer>) this.classUnderTest()).removeAll(Lists.fixedSize.of()));
}
@Test
public void retainAll()
{
Verify.assertThrows(UnsupportedOperationException.class, () -> ((Collection<Integer>) this.classUnderTest()).retainAll(Lists.fixedSize.of()));
}
@Test
public void addAll()
{
Verify.assertThrows(UnsupportedOperationException.class, () -> ((Collection<Integer>) this.classUnderTest()).addAll(Lists.fixedSize.of()));
}
@Test
public void aggregateByMutating()
{
Procedure2<Counter, Integer> sumAggregator = Counter::add;
MapIterable<String, Counter> actual = this.classUnderTest().aggregateInPlaceBy(String::valueOf, Counter::new, sumAggregator);
MapIterable<String, Counter> expected = this.classUnderTest().toBag().aggregateInPlaceBy(String::valueOf, Counter::new, sumAggregator);
Assert.assertEquals(expected, actual);
}
@Test
public void aggregateByNonMutating()
{
Function2<Integer, Integer, Integer> sumAggregator = (integer1, integer2) -> integer1 + integer2;
MapIterable<String, Integer> actual = this.classUnderTest().aggregateBy(String::valueOf, () -> 0, sumAggregator);
MapIterable<String, Integer> expected = this.classUnderTest().toBag().aggregateBy(String::valueOf, () -> 0, sumAggregator);
Assert.assertEquals(expected, actual);
}
@Test
public void tap()
{
MutableList<Integer> tapResult = Lists.mutable.of();
ImmutableCollection<Integer> collection = this.classUnderTest();
Assert.assertSame(collection, collection.tap(tapResult::add));
Assert.assertEquals(collection.toList(), tapResult);
}
}