/
AbstractRichIterableTestCase.java
1662 lines (1471 loc) · 63 KB
/
AbstractRichIterableTestCase.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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.DoubleSummaryStatistics;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
import java.util.Iterator;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.eclipse.collections.api.BooleanIterable;
import org.eclipse.collections.api.ByteIterable;
import org.eclipse.collections.api.CharIterable;
import org.eclipse.collections.api.DoubleIterable;
import org.eclipse.collections.api.FloatIterable;
import org.eclipse.collections.api.IntIterable;
import org.eclipse.collections.api.LongIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.ShortIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.MutableByteCollection;
import org.eclipse.collections.api.collection.primitive.MutableCharCollection;
import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ObjectLongMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.partition.PartitionIterable;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.BooleanHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.ByteHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.CharHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.DoubleHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.FloatHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.IntHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.LongHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.ShortHashBag;
import org.eclipse.collections.impl.bag.sorted.mutable.TreeBag;
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.function.AddFunction;
import org.eclipse.collections.impl.block.function.NegativeIntervalFunction;
import org.eclipse.collections.impl.block.function.PassThruFunction0;
import org.eclipse.collections.impl.block.procedure.CollectionAddProcedure;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.factory.primitive.BooleanBags;
import org.eclipse.collections.impl.factory.primitive.ByteBags;
import org.eclipse.collections.impl.factory.primitive.CharBags;
import org.eclipse.collections.impl.factory.primitive.DoubleBags;
import org.eclipse.collections.impl.factory.primitive.FloatBags;
import org.eclipse.collections.impl.factory.primitive.IntBags;
import org.eclipse.collections.impl.factory.primitive.LongBags;
import org.eclipse.collections.impl.factory.primitive.ShortBags;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.primitive.BooleanArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.ByteArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.CharArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.DoubleArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.FloatArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.LongArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.ShortArrayList;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.set.sorted.mutable.TreeSortedSet;
import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
public abstract class AbstractRichIterableTestCase
{
protected abstract <T> RichIterable<T> newWith(T... littleElements);
@Test
public void testNewCollection()
{
RichIterable<Object> collection = this.newWith();
Verify.assertIterableEmpty(collection);
Verify.assertIterableSize(0, collection);
}
@Test
public void equalsAndHashCode()
{
Verify.assertEqualsAndHashCode(this.newWith(1, 2, 3), this.newWith(1, 2, 3));
Assert.assertNotEquals(this.newWith(1, 2, 3), this.newWith(1, 2));
}
@Test
public void contains()
{
RichIterable<Integer> collection = this.newWith(1, 2, 3, 4);
Assert.assertTrue(collection.contains(1));
Assert.assertTrue(collection.contains(4));
Assert.assertFalse(collection.contains(5));
}
@Test
public void containsAllIterable()
{
RichIterable<Integer> collection = this.newWith(1, 2, 3, 4);
Assert.assertTrue(collection.containsAllIterable(Lists.mutable.with(1, 2)));
Assert.assertFalse(collection.containsAllIterable(Lists.mutable.with(1, 5)));
}
@Test
public void containsAllArray()
{
RichIterable<Integer> collection = this.newWith(1, 2, 3, 4);
Assert.assertTrue(collection.containsAllArguments(1, 2));
Assert.assertFalse(collection.containsAllArguments(1, 5));
}
@Test
public void containsAllCollection()
{
RichIterable<Integer> collection = this.newWith(1, 2, 3, 4);
Assert.assertTrue(collection.containsAll(Lists.mutable.with(1, 2)));
Assert.assertFalse(collection.containsAll(Lists.mutable.with(1, 5)));
}
@Test
public void tap()
{
MutableList<Integer> tapResult = Lists.mutable.of();
RichIterable<Integer> collection = this.newWith(1, 2, 3, 4);
Assert.assertSame(collection, collection.tap(tapResult::add));
Assert.assertEquals(collection.toList(), tapResult);
}
private void forEach(Function<Collection<Integer>, Consumer<Integer>> adderProvider)
{
MutableList<Integer> result = Lists.mutable.of();
RichIterable<Integer> template = this.newWith(1, 2, 3, 4);
Consumer<Integer> adder = adderProvider.apply(result);
if (adder instanceof Procedure<?>)
{
template.forEach((Procedure<Integer>) adder);
}
else
{
template.forEach(adder);
}
Verify.assertSize(4, result);
Verify.assertContainsAll(result, 1, 2, 3, 4);
}
private void forEachProcedure()
{
this.forEach(CollectionAddProcedure::on);
}
private void forEachConsumer()
{
this.forEach(collection -> collection::add);
}
@Test
public void forEach()
{
this.forEachProcedure();
this.forEachConsumer();
}
@Test
public void forEachWith()
{
MutableList<Integer> result = Lists.mutable.of();
RichIterable<Integer> collection = this.newWith(1, 2, 3, 4);
collection.forEachWith((argument1, argument2) -> result.add(argument1 + argument2), 0);
Verify.assertSize(4, result);
Verify.assertContainsAll(result, 1, 2, 3, 4);
}
@Test
public void forEachWithIndex()
{
MutableBag<Integer> elements = Bags.mutable.of();
MutableBag<Integer> indexes = Bags.mutable.of();
RichIterable<Integer> collection = this.newWith(1, 2, 3, 4);
collection.forEachWithIndex((object, index) -> {
elements.add(object);
indexes.add(index);
});
Assert.assertEquals(Bags.mutable.of(1, 2, 3, 4), elements);
Assert.assertEquals(Bags.mutable.of(0, 1, 2, 3), indexes);
}
@Test
public void select()
{
Verify.assertContainsAll(this.newWith(1, 2, 3, 4, 5).select(Predicates.lessThan(3)), 1, 2);
RichIterable<Integer> result = this.newWith(-1, 2, 3, 4, 5).select(Predicates.lessThan(3));
Verify.assertNotContains(3, result);
Verify.assertNotContains(4, result);
Verify.assertNotContains(5, result);
Verify.assertContainsAll(this.newWith(1, 2, 3, 4, 5).select(Predicates.lessThan(3), UnifiedSet.newSet()), 1, 2);
}
@Test
public void selectWith()
{
Verify.assertContainsAll(this.newWith(1, 2, 3, 4, 5).selectWith(Predicates2.lessThan(), 3), 1, 2);
RichIterable<Integer> result = this.newWith(-1, 2, 3, 4, 5).selectWith(Predicates2.lessThan(), 3);
Verify.assertNotContains(3, result);
Verify.assertNotContains(4, result);
Verify.assertNotContains(5, result);
Verify.assertContainsAll(
this.newWith(1, 2, 3, 4, 5).selectWith(
Predicates2.lessThan(),
3),
1, 2);
}
@Test
public void selectWith_target()
{
Verify.assertContainsAll(this.newWith(1, 2, 3, 4, 5).selectWith(Predicates2.lessThan(), 3, HashBag.newBag()), 1, 2);
Verify.denyContainsAny(this.newWith(-1, 2, 3, 4, 5).selectWith(Predicates2.lessThan(), 3, HashBag.newBag()), 3, 4, 5);
Verify.assertContainsAll(
this.newWith(1, 2, 3, 4, 5).selectWith(Predicates2.lessThan(), 3, HashBag.newBag()),
1, 2);
}
@Test
public void reject()
{
Verify.assertContainsAll(this.newWith(1, 2, 3, 4).reject(Predicates.lessThan(3)), 3, 4);
Verify.assertContainsAll(
this.newWith(1, 2, 3, 4).reject(Predicates.lessThan(3), UnifiedSet.newSet()), 3, 4);
}
@Test
public void rejectWith()
{
Verify.assertContainsAll(this.newWith(1, 2, 3, 4).rejectWith(Predicates2.lessThan(), 3), 3, 4);
Verify.assertContainsAll(
this.newWith(1, 2, 3, 4).rejectWith(Predicates2.lessThan(), 3, UnifiedSet.newSet()),
3, 4);
}
@Test
public void rejectWith_target()
{
Verify.assertContainsAll(this.newWith(1, 2, 3, 4).rejectWith(Predicates2.lessThan(), 3, HashBag.newBag()), 3, 4);
Verify.assertContainsAll(
this.newWith(1, 2, 3, 4).rejectWith(Predicates2.lessThan(), 3, UnifiedSet.newSet()),
3, 4);
}
@Test
public void selectInstancesOf()
{
RichIterable<Number> numbers = this.newWith(1, 2.0, 3, 4.0, 5);
Assert.assertEquals(HashBag.newBagWith(1, 3, 5), numbers.selectInstancesOf(Integer.class).toBag());
Assert.assertEquals(HashBag.newBagWith(1, 2.0, 3, 4.0, 5), numbers.selectInstancesOf(Number.class).toBag());
}
@Test
public void collect()
{
Verify.assertContainsAll(this.newWith(1, 2, 3, 4).collect(String::valueOf), "1", "2", "3", "4");
Verify.assertContainsAll(this.newWith(1, 2, 3, 4).collect(String::valueOf, UnifiedSet.newSet()), "1", "2", "3", "4");
}
@Test
public void collectTarget()
{
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, Lists.mutable.empty()).toBag());
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
Bags.mutable.withAll(this.newWith(1, 2, 3).collect(each -> each + 1, new ArrayList<>())));
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
Bags.mutable.withAll(this.newWith(1, 2, 3).collect(each -> each + 1, new CopyOnWriteArrayList<>())));
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, Bags.mutable.empty()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, Sets.mutable.empty()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, new HashSet<>()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, new CopyOnWriteArraySet<>()));
}
@Test
public void collectBoolean()
{
BooleanIterable result = this.newWith(1, 0).collectBoolean(PrimitiveFunctions.integerIsPositive());
Assert.assertEquals(BooleanBags.mutable.of(true, false), result.toBag());
Assert.assertEquals(BooleanBags.mutable.of(true, false), BooleanBags.mutable.ofAll(result));
}
@Test
public void collectBooleanWithTarget()
{
MutableBooleanCollection target = new BooleanArrayList();
BooleanIterable result = this.newWith(1, 0).collectBoolean(PrimitiveFunctions.integerIsPositive(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(BooleanBags.mutable.of(true, false), result.toBag());
}
@Test
public void collectBooleanWithBagTarget()
{
BooleanHashBag target = new BooleanHashBag();
BooleanHashBag result = this.newWith(1, 0).collectBoolean(PrimitiveFunctions.integerIsPositive(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(BooleanHashBag.newBagWith(true, false), result);
}
@Test
public void collectByte()
{
ByteIterable result = this.newWith(1, 2, 3, 4).collectByte(PrimitiveFunctions.unboxIntegerToByte());
Assert.assertEquals(ByteBags.mutable.of((byte) 1, (byte) 2, (byte) 3, (byte) 4), result.toBag());
Assert.assertEquals(ByteBags.mutable.of((byte) 1, (byte) 2, (byte) 3, (byte) 4), ByteBags.mutable.ofAll(result));
}
@Test
public void collectByteWithTarget()
{
MutableByteCollection target = new ByteArrayList();
ByteIterable result = this.newWith(1, 2, 3, 4).collectByte(PrimitiveFunctions.unboxIntegerToByte(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(ByteHashBag.newBagWith((byte) 1, (byte) 2, (byte) 3, (byte) 4), result.toBag());
}
@Test
public void collectByteWithBagTarget()
{
ByteHashBag target = new ByteHashBag();
ByteHashBag result = this.newWith(1, 2, 3, 4).collectByte(PrimitiveFunctions.unboxIntegerToByte(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(ByteHashBag.newBagWith((byte) 1, (byte) 2, (byte) 3, (byte) 4), result);
}
@Test
public void collectChar()
{
CharIterable result = this.newWith(1, 2, 3, 4).collectChar(PrimitiveFunctions.unboxIntegerToChar());
Assert.assertEquals(CharBags.mutable.of((char) 1, (char) 2, (char) 3, (char) 4), result.toBag());
Assert.assertEquals(CharBags.mutable.of((char) 1, (char) 2, (char) 3, (char) 4), CharBags.mutable.ofAll(result));
}
@Test
public void collectCharWithTarget()
{
MutableCharCollection target = new CharArrayList();
CharIterable result = this.newWith(1, 2, 3, 4).collectChar(PrimitiveFunctions.unboxIntegerToChar(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(CharHashBag.newBagWith((char) 1, (char) 2, (char) 3, (char) 4), result.toBag());
}
@Test
public void collectCharWithBagTarget()
{
CharHashBag target = new CharHashBag();
CharHashBag result = this.newWith(1, 2, 3, 4).collectChar(PrimitiveFunctions.unboxIntegerToChar(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(CharHashBag.newBagWith((char) 1, (char) 2, (char) 3, (char) 4), result);
}
@Test
public void collectDouble()
{
DoubleIterable result = this.newWith(1, 2, 3, 4).collectDouble(PrimitiveFunctions.unboxIntegerToDouble());
Assert.assertEquals(DoubleBags.mutable.of(1.0d, 2.0d, 3.0d, 4.0d), result.toBag());
Assert.assertEquals(DoubleBags.mutable.of(1.0d, 2.0d, 3.0d, 4.0d), DoubleBags.mutable.ofAll(result));
}
@Test
public void collectDoubleWithTarget()
{
MutableDoubleCollection target = new DoubleArrayList();
DoubleIterable result = this.newWith(1, 2, 3, 4).collectDouble(PrimitiveFunctions.unboxIntegerToDouble(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(DoubleHashBag.newBagWith(1.0d, 2.0d, 3.0d, 4.0d), result.toBag());
}
@Test
public void collectDoubleWithBagTarget()
{
DoubleHashBag target = new DoubleHashBag();
DoubleHashBag result = this.newWith(1, 2, 3, 4).collectDouble(PrimitiveFunctions.unboxIntegerToDouble(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(DoubleHashBag.newBagWith(1.0d, 2.0d, 3.0d, 4.0d), result);
}
@Test
public void collectFloat()
{
FloatIterable result = this.newWith(1, 2, 3, 4).collectFloat(PrimitiveFunctions.unboxIntegerToFloat());
Assert.assertEquals(FloatBags.mutable.of(1.0f, 2.0f, 3.0f, 4.0f), result.toBag());
Assert.assertEquals(FloatBags.mutable.of(1.0f, 2.0f, 3.0f, 4.0f), FloatBags.mutable.ofAll(result));
}
@Test
public void collectFloatWithTarget()
{
MutableFloatCollection target = new FloatArrayList();
FloatIterable result = this.newWith(1, 2, 3, 4).collectFloat(PrimitiveFunctions.unboxIntegerToFloat(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(FloatHashBag.newBagWith(1.0f, 2.0f, 3.0f, 4.0f), result.toBag());
}
@Test
public void collectFloatWithBagTarget()
{
FloatHashBag target = new FloatHashBag();
FloatHashBag result = this.newWith(1, 2, 3, 4).collectFloat(PrimitiveFunctions.unboxIntegerToFloat(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(FloatHashBag.newBagWith(1.0f, 2.0f, 3.0f, 4.0f), result);
}
@Test
public void collectInt()
{
IntIterable result = this.newWith(1, 2, 3, 4).collectInt(PrimitiveFunctions.unboxIntegerToInt());
Assert.assertEquals(IntBags.mutable.of(1, 2, 3, 4), result.toBag());
Assert.assertEquals(IntBags.mutable.of(1, 2, 3, 4), IntBags.mutable.ofAll(result));
}
@Test
public void collectIntWithTarget()
{
MutableIntCollection target = new IntArrayList();
IntIterable result = this.newWith(1, 2, 3, 4).collectInt(PrimitiveFunctions.unboxIntegerToInt(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(IntHashBag.newBagWith(1, 2, 3, 4), result.toBag());
}
@Test
public void collectIntWithBagTarget()
{
IntHashBag target = new IntHashBag();
IntHashBag result = this.newWith(1, 2, 3, 4).collectInt(PrimitiveFunctions.unboxIntegerToInt(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(IntHashBag.newBagWith(1, 2, 3, 4), result);
}
@Test
public void collectLong()
{
LongIterable result = this.newWith(1, 2, 3, 4).collectLong(PrimitiveFunctions.unboxIntegerToLong());
Assert.assertEquals(LongBags.mutable.of(1, 2, 3, 4), result.toBag());
Assert.assertEquals(LongBags.mutable.of(1, 2, 3, 4), LongBags.mutable.ofAll(result));
}
@Test
public void collectLongWithTarget()
{
MutableLongCollection target = new LongArrayList();
LongIterable result = this.newWith(1, 2, 3, 4).collectLong(PrimitiveFunctions.unboxIntegerToLong(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(LongHashBag.newBagWith(1, 2, 3, 4), result.toBag());
}
@Test
public void collectLongWithBagTarget()
{
LongHashBag target = new LongHashBag();
LongHashBag result = this.newWith(1, 2, 3, 4).collectLong(PrimitiveFunctions.unboxIntegerToLong(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(LongHashBag.newBagWith(1, 2, 3, 4), result);
}
@Test
public void collectShort()
{
ShortIterable result = this.newWith(1, 2, 3, 4).collectShort(PrimitiveFunctions.unboxIntegerToShort());
Assert.assertEquals(ShortBags.mutable.of((short) 1, (short) 2, (short) 3, (short) 4), result.toBag());
Assert.assertEquals(ShortBags.mutable.of((short) 1, (short) 2, (short) 3, (short) 4), ShortBags.mutable.ofAll(result));
}
@Test
public void collectShortWithTarget()
{
MutableShortCollection target = new ShortArrayList();
ShortIterable result = this.newWith(1, 2, 3, 4).collectShort(PrimitiveFunctions.unboxIntegerToShort(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(ShortHashBag.newBagWith((short) 1, (short) 2, (short) 3, (short) 4), result.toBag());
}
@Test
public void collectShortWithBagTarget()
{
ShortHashBag target = new ShortHashBag();
ShortHashBag result = this.newWith(1, 2, 3, 4).collectShort(PrimitiveFunctions.unboxIntegerToShort(), target);
Assert.assertSame("Target list sent as parameter not returned", target, result);
Assert.assertEquals(ShortHashBag.newBagWith((short) 1, (short) 2, (short) 3, (short) 4), result);
}
@Test
public void flatCollect()
{
RichIterable<Integer> collection = this.newWith(1, 2, 3, 4);
Function<Integer, MutableList<String>> function = object -> Lists.mutable.with(String.valueOf(object));
Verify.assertListsEqual(
Lists.mutable.with("1", "2", "3", "4"),
collection.flatCollect(function).toSortedList());
Verify.assertSetsEqual(
UnifiedSet.newSetWith("1", "2", "3", "4"),
collection.flatCollect(function, UnifiedSet.newSet()));
}
@Test
public void detect()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 2, 3, 4, 5).detect(Integer.valueOf(3)::equals));
Assert.assertNull(this.newWith(1, 2, 3, 4, 5).detect(Integer.valueOf(6)::equals));
}
@Test
public void detectOptional()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 2, 3, 4, 5).detectOptional(Integer.valueOf(3)::equals).get());
Assert.assertNotNull(this.newWith(1, 2, 3, 4, 5).detectOptional(Integer.valueOf(6)::equals));
Verify.assertThrows(NoSuchElementException.class, () -> this.newWith(1, 2, 3, 4, 5).detectOptional(Integer.valueOf(6)::equals).get());
}
@Test(expected = NoSuchElementException.class)
public void min_empty_throws()
{
this.<Integer>newWith().min(Integer::compareTo);
}
@Test(expected = NoSuchElementException.class)
public void max_empty_throws()
{
this.<Integer>newWith().max(Integer::compareTo);
}
@Test(expected = NullPointerException.class)
public void min_null_throws()
{
this.newWith(1, null, 2).min(Integer::compareTo);
}
@Test(expected = NullPointerException.class)
public void max_null_throws()
{
this.newWith(1, null, 2).max(Integer::compareTo);
}
@Test
public void min()
{
Assert.assertEquals(Integer.valueOf(1), this.newWith(1, 3, 2).min(Integer::compareTo));
}
@Test
public void max()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 3, 2).max(Integer::compareTo));
}
@Test(expected = NullPointerException.class)
public void min_null_throws_without_comparator()
{
this.newWith(1, null, 2).min();
}
@Test(expected = NullPointerException.class)
public void max_null_throws_without_comparator()
{
this.newWith(1, null, 2).max();
}
@Test
public void min_without_comparator()
{
Assert.assertEquals(Integer.valueOf(1), this.newWith(3, 1, 2).min());
}
@Test
public void max_without_comparator()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 3, 2).max());
}
@Test
public void min_null_safe()
{
RichIterable<Integer> integers = this.newWith(1, 3, 2, null);
Assert.assertEquals(Integer.valueOf(1), integers.min(Comparators.safeNullsHigh(Integer::compareTo)));
Assert.assertNull(integers.min(Comparators.safeNullsLow(Integer::compareTo)));
}
@Test
public void max_null_safe()
{
RichIterable<Integer> integers = this.newWith(1, 3, 2, null);
Assert.assertEquals(Integer.valueOf(3), integers.max(Comparators.safeNullsLow(Integer::compareTo)));
Assert.assertNull(integers.max(Comparators.safeNullsHigh(Integer::compareTo)));
}
@Test
public void minBy()
{
Assert.assertEquals(Integer.valueOf(1), this.newWith(1, 3, 2).minBy(String::valueOf));
}
@Test
public void minByOptional()
{
Assert.assertEquals(Integer.valueOf(1), this.newWith(1, 3, 2).minByOptional(String::valueOf).get());
Assert.assertFalse(this.newWith().minByOptional(String::valueOf).isPresent());
}
@Test
public void maxBy()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 3, 2).maxBy(String::valueOf));
}
@Test
public void maxByOptional()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 3, 2).maxByOptional(String::valueOf).get());
Assert.assertFalse(this.newWith().maxByOptional(String::valueOf).isPresent());
}
@Test(expected = NullPointerException.class)
public void minBy_null_throws()
{
this.newWith(1, null, 2).minBy(Integer::valueOf);
}
@Test(expected = NullPointerException.class)
public void maxBy_null_throws()
{
this.newWith(1, null, 2).maxBy(Integer::valueOf);
}
@Test
public void detectWith()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 2, 3, 4, 5).detectWith(Object::equals, 3));
Assert.assertNull(this.newWith(1, 2, 3, 4, 5).detectWith(Object::equals, 6));
}
@Test
public void detectWithOptional()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 2, 3, 4, 5).detectWithOptional(Object::equals, 3).get());
Assert.assertNotNull(this.newWith(1, 2, 3, 4, 5).detectWithOptional(Object::equals, 6));
Verify.assertThrows(NoSuchElementException.class, () -> this.newWith(1, 2, 3, 4, 5).detectWithOptional(Object::equals, 6).get());
}
@Test
public void detectIfNone()
{
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 2, 3, 4, 5).detectIfNone(Integer.valueOf(3)::equals, () -> 6));
Assert.assertEquals(Integer.valueOf(6), this.newWith(1, 2, 3, 4, 5).detectIfNone(Integer.valueOf(6)::equals, () -> 6));
}
@Test
public void detectWithIfNoneBlock()
{
Function0<Integer> function = new PassThruFunction0<>(-42);
Assert.assertEquals(
Integer.valueOf(5),
this.newWith(1, 2, 3, 4, 5).detectWithIfNone(
Predicates2.greaterThan(),
4,
function));
Assert.assertEquals(
Integer.valueOf(-42),
this.newWith(1, 2, 3, 4, 5).detectWithIfNone(
Predicates2.lessThan(),
0,
function));
}
@Test
public void allSatisfy()
{
Assert.assertTrue(this.newWith(1, 2, 3).allSatisfy(Integer.class::isInstance));
Assert.assertFalse(this.newWith(1, 2, 3).allSatisfy(Integer.valueOf(1)::equals));
}
@Test
public void allSatisfyWith()
{
Assert.assertTrue(this.newWith(1, 2, 3).allSatisfyWith(Predicates2.instanceOf(), Integer.class));
Assert.assertFalse(this.newWith(1, 2, 3).allSatisfyWith(Object::equals, 1));
}
@Test
public void noneSatisfy()
{
Assert.assertTrue(this.newWith(1, 2, 3).noneSatisfy(Boolean.class::isInstance));
Assert.assertFalse(this.newWith(1, 1, 3).noneSatisfy(Integer.valueOf(1)::equals));
Assert.assertTrue(this.newWith(1, 2, 3).noneSatisfy(Integer.valueOf(4)::equals));
}
@Test
public void noneSatisfyWith()
{
Assert.assertTrue(this.newWith(1, 2, 3).noneSatisfyWith(Predicates2.instanceOf(), Boolean.class));
Assert.assertFalse(this.newWith(1, 2, 3).noneSatisfyWith(Object::equals, 1));
}
@Test
public void anySatisfy()
{
Assert.assertFalse(this.newWith(1, 2, 3).anySatisfy(String.class::isInstance));
Assert.assertTrue(this.newWith(1, 2, 3).anySatisfy(Integer.class::isInstance));
}
@Test
public void anySatisfyWith()
{
Assert.assertFalse(this.newWith(1, 2, 3).anySatisfyWith(Predicates2.instanceOf(), String.class));
Assert.assertTrue(this.newWith(1, 2, 3).anySatisfyWith(Predicates2.instanceOf(), Integer.class));
}
@Test
public void count()
{
Assert.assertEquals(3, this.newWith(1, 2, 3).count(Integer.class::isInstance));
}
@Test
public void countWith()
{
Assert.assertEquals(3, this.newWith(1, 2, 3).countWith(Predicates2.instanceOf(), Integer.class));
}
@Test
public void collectIf()
{
Verify.assertContainsAll(
this.newWith(1, 2, 3).collectIf(
Integer.class::isInstance,
Object::toString),
"1", "2", "3");
Verify.assertContainsAll(
this.newWith(1, 2, 3).collectIf(
Integer.class::isInstance,
Object::toString,
UnifiedSet.newSet()),
"1", "2", "3");
}
@Test
public void collectWith()
{
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1).toBag());
}
@Test
public void collectWith_target()
{
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, Lists.mutable.empty()).toBag());
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
Bags.mutable.withAll(this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, new ArrayList<>())));
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
Bags.mutable.withAll(this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, new CopyOnWriteArrayList<>())));
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, Bags.mutable.empty()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, Sets.mutable.empty()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, new HashSet<>()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, new CopyOnWriteArraySet<>()));
}
@Test
public void getFirst()
{
Assert.assertEquals(Integer.valueOf(1), this.newWith(1, 2, 3).getFirst());
Assert.assertNotEquals(Integer.valueOf(3), this.newWith(1, 2, 3).getFirst());
}
@Test
public void getLast()
{
Assert.assertNotEquals(Integer.valueOf(1), this.newWith(1, 2, 3).getLast());
Assert.assertEquals(Integer.valueOf(3), this.newWith(1, 2, 3).getLast());
}
@Test
public void getOnly()
{
Assert.assertEquals(Integer.valueOf(2), this.newWith(2).getOnly());
Assert.assertNotEquals(Integer.valueOf(2), this.newWith(1).getOnly());
}
@Test(expected = IllegalStateException.class)
public void getOnly_not_only_one_throws()
{
this.newWith(1, 2).getOnly();
}
@Test(expected = IllegalStateException.class)
public void getOnly_empty_throws()
{
this.newWith().getOnly();
}
@Test
public void isEmpty()
{
Verify.assertIterableEmpty(this.newWith());
Verify.assertIterableNotEmpty(this.newWith(1, 2));
Assert.assertTrue(this.newWith(1, 2).notEmpty());
}
@Test
public void iterator()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
Iterator<Integer> iterator = objects.iterator();
for (int i = objects.size(); i-- > 0; )
{
Assert.assertTrue(iterator.hasNext());
Integer integer = iterator.next();
Assert.assertEquals(3, integer.intValue() + i);
}
Assert.assertFalse(iterator.hasNext());
}
@Test(expected = NoSuchElementException.class)
public void iterator_throws()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
Iterator<Integer> iterator = objects.iterator();
for (int i = objects.size(); i-- > 0; )
{
Assert.assertTrue(iterator.hasNext());
iterator.next();
}
Assert.assertFalse(iterator.hasNext());
iterator.next();
}
@Test
public void injectInto()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
Integer result = objects.injectInto(1, AddFunction.INTEGER);
Assert.assertEquals(Integer.valueOf(7), result);
int sum = objects.injectInto(0, AddFunction.INTEGER_TO_INT);
Assert.assertEquals(6, sum);
}
@Test
public void injectIntoInt()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
int result = objects.injectInto(1, AddFunction.INTEGER_TO_INT);
Assert.assertEquals(7, result);
int sum = objects.injectInto(0, AddFunction.INTEGER_TO_INT);
Assert.assertEquals(6, sum);
}
@Test
public void injectIntoLong()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
long result = objects.injectInto(1, AddFunction.INTEGER_TO_LONG);
Assert.assertEquals(7, result);
long sum = objects.injectInto(0, AddFunction.INTEGER_TO_LONG);
Assert.assertEquals(6, sum);
}
@Test
public void injectIntoDouble()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
double result = objects.injectInto(1, AddFunction.INTEGER_TO_DOUBLE);
Assert.assertEquals(7.0d, result, 0.001);
double sum = objects.injectInto(0, AddFunction.INTEGER_TO_DOUBLE);
Assert.assertEquals(6.0d, sum, 0.001);
}
@Test
public void injectIntoFloat()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
float result = objects.injectInto(1, AddFunction.INTEGER_TO_FLOAT);
Assert.assertEquals(7.0f, result, 0.001f);
float sum = objects.injectInto(0, AddFunction.INTEGER_TO_FLOAT);
Assert.assertEquals(6.0f, sum, 0.001f);
}
@Test
public void sumFloat()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
float expected = objects.injectInto(0, AddFunction.INTEGER_TO_FLOAT);
double actual = objects.sumOfFloat(Integer::floatValue);
Assert.assertEquals(expected, actual, 0.001);
}
@Test
public void summarizeFloat()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
DoubleSummaryStatistics expected = objects.summarizeFloat(Integer::floatValue);
Assert.assertEquals(6.0d, expected.getSum(), 0.0);
Assert.assertEquals(3, expected.getCount());
}
@Test
public void sumFloatConsistentRounding1()
{
MutableList<Integer> list = Interval.oneTo(100_000).toList().shuffleThis();
// 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,
this.newWith(list.toArray(new Integer[]{})).sumOfFloat(i -> 1.0f / (i.floatValue() * i.floatValue() * i.floatValue() * i.floatValue())),
1.0e-15);
}
@Test
public void sumFloatConsistentRounding2()
{
MutableList<Integer> list = Interval.oneTo(99_999).toList().shuffleThis();
// 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(
33333.00099340081,
this.newWith(list.toArray(new Integer[]{})).sumOfFloat(i -> 1.0f / 3.0f),
0.0);
}
@Test
public void sumDouble()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
double expected = objects.injectInto(0, AddFunction.INTEGER_TO_DOUBLE);
double actual = objects.sumOfDouble(Integer::doubleValue);