-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
IteratorsTest.java
1703 lines (1496 loc) · 55.7 KB
/
IteratorsTest.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) 2007 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect;
import static com.google.common.collect.CollectPreconditions.checkRemove;
import static com.google.common.collect.Iterators.advance;
import static com.google.common.collect.Iterators.get;
import static com.google.common.collect.Iterators.getLast;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.testing.IteratorFeature;
import com.google.common.collect.testing.IteratorTester;
import com.google.common.collect.testing.ListTestSuiteBuilder;
import com.google.common.collect.testing.TestStringListGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.ListFeature;
import com.google.common.primitives.Ints;
import com.google.common.testing.NullPointerTester;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import java.util.Set;
import java.util.Vector;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Unit test for {@code Iterators}.
*
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
public class IteratorsTest extends TestCase {
@J2ktIncompatible
@GwtIncompatible // suite
public static Test suite() {
TestSuite suite = new TestSuite(IteratorsTest.class.getSimpleName());
suite.addTest(testsForRemoveAllAndRetainAll());
suite.addTestSuite(IteratorsTest.class);
return suite;
}
@SuppressWarnings("DoNotCall")
public void testEmptyIterator() {
Iterator<String> iterator = Iterators.emptyIterator();
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("no exception thrown");
} catch (NoSuchElementException expected) {
}
try {
iterator.remove();
fail("no exception thrown");
} catch (UnsupportedOperationException expected) {
}
}
@SuppressWarnings("DoNotCall")
public void testEmptyListIterator() {
ListIterator<String> iterator = Iterators.emptyListIterator();
assertFalse(iterator.hasNext());
assertFalse(iterator.hasPrevious());
assertEquals(0, iterator.nextIndex());
assertEquals(-1, iterator.previousIndex());
try {
iterator.next();
fail("no exception thrown");
} catch (NoSuchElementException expected) {
}
try {
iterator.previous();
fail("no exception thrown");
} catch (NoSuchElementException expected) {
}
try {
iterator.remove();
fail("no exception thrown");
} catch (UnsupportedOperationException expected) {
}
try {
iterator.set("a");
fail("no exception thrown");
} catch (UnsupportedOperationException expected) {
}
try {
iterator.add("a");
fail("no exception thrown");
} catch (UnsupportedOperationException expected) {
}
}
public void testEmptyModifiableIterator() {
Iterator<String> iterator = Iterators.emptyModifiableIterator();
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("Expected NoSuchElementException");
} catch (NoSuchElementException expected) {
}
try {
iterator.remove();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
}
public void testSize0() {
Iterator<String> iterator = Iterators.emptyIterator();
assertEquals(0, Iterators.size(iterator));
}
public void testSize1() {
Iterator<Integer> iterator = Collections.singleton(0).iterator();
assertEquals(1, Iterators.size(iterator));
}
public void testSize_partiallyConsumed() {
Iterator<Integer> iterator = asList(1, 2, 3, 4, 5).iterator();
iterator.next();
iterator.next();
assertEquals(3, Iterators.size(iterator));
}
public void test_contains_nonnull_yes() {
Iterator<@Nullable String> set = Arrays.<@Nullable String>asList("a", null, "b").iterator();
assertTrue(Iterators.contains(set, "b"));
}
public void test_contains_nonnull_no() {
Iterator<String> set = asList("a", "b").iterator();
assertFalse(Iterators.contains(set, "c"));
}
public void test_contains_null_yes() {
Iterator<@Nullable String> set = Arrays.<@Nullable String>asList("a", null, "b").iterator();
assertTrue(Iterators.contains(set, null));
}
public void test_contains_null_no() {
Iterator<String> set = asList("a", "b").iterator();
assertFalse(Iterators.contains(set, null));
}
public void testGetOnlyElement_noDefault_valid() {
Iterator<String> iterator = Collections.singletonList("foo").iterator();
assertEquals("foo", Iterators.getOnlyElement(iterator));
}
public void testGetOnlyElement_noDefault_empty() {
Iterator<String> iterator = Iterators.emptyIterator();
try {
Iterators.getOnlyElement(iterator);
fail();
} catch (NoSuchElementException expected) {
}
}
public void testGetOnlyElement_noDefault_moreThanOneLessThanFiveElements() {
Iterator<String> iterator = asList("one", "two").iterator();
try {
Iterators.getOnlyElement(iterator);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessageThat().isEqualTo("expected one element but was: <one, two>");
}
}
public void testGetOnlyElement_noDefault_fiveElements() {
Iterator<String> iterator = asList("one", "two", "three", "four", "five").iterator();
try {
Iterators.getOnlyElement(iterator);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("expected one element but was: <one, two, three, four, five>");
}
}
public void testGetOnlyElement_noDefault_moreThanFiveElements() {
Iterator<String> iterator = asList("one", "two", "three", "four", "five", "six").iterator();
try {
Iterators.getOnlyElement(iterator);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("expected one element but was: <one, two, three, four, five, ...>");
}
}
public void testGetOnlyElement_withDefault_singleton() {
Iterator<String> iterator = Collections.singletonList("foo").iterator();
assertEquals("foo", Iterators.getOnlyElement(iterator, "bar"));
}
public void testGetOnlyElement_withDefault_empty() {
Iterator<String> iterator = Iterators.emptyIterator();
assertEquals("bar", Iterators.getOnlyElement(iterator, "bar"));
}
public void testGetOnlyElement_withDefault_empty_null() {
Iterator<String> iterator = Iterators.emptyIterator();
assertNull(Iterators.<@Nullable String>getOnlyElement(iterator, null));
}
public void testGetOnlyElement_withDefault_two() {
Iterator<String> iterator = asList("foo", "bar").iterator();
try {
Iterators.getOnlyElement(iterator, "x");
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessageThat().isEqualTo("expected one element but was: <foo, bar>");
}
}
@GwtIncompatible // Iterators.toArray(Iterator, Class)
public void testToArrayEmpty() {
Iterator<String> iterator = Collections.<String>emptyList().iterator();
String[] array = Iterators.toArray(iterator, String.class);
assertTrue(Arrays.equals(new String[0], array));
}
@GwtIncompatible // Iterators.toArray(Iterator, Class)
public void testToArraySingleton() {
Iterator<String> iterator = Collections.singletonList("a").iterator();
String[] array = Iterators.toArray(iterator, String.class);
assertTrue(Arrays.equals(new String[] {"a"}, array));
}
@GwtIncompatible // Iterators.toArray(Iterator, Class)
public void testToArray() {
String[] sourceArray = new String[] {"a", "b", "c"};
Iterator<String> iterator = asList(sourceArray).iterator();
String[] newArray = Iterators.toArray(iterator, String.class);
assertTrue(Arrays.equals(sourceArray, newArray));
}
public void testFilterSimple() {
Iterator<String> unfiltered = Lists.newArrayList("foo", "bar").iterator();
Iterator<String> filtered = Iterators.filter(unfiltered, Predicates.equalTo("foo"));
List<String> expected = Collections.singletonList("foo");
List<String> actual = Lists.newArrayList(filtered);
assertEquals(expected, actual);
}
public void testFilterNoMatch() {
Iterator<String> unfiltered = Lists.newArrayList("foo", "bar").iterator();
Iterator<String> filtered = Iterators.filter(unfiltered, Predicates.alwaysFalse());
List<String> expected = Collections.emptyList();
List<String> actual = Lists.newArrayList(filtered);
assertEquals(expected, actual);
}
public void testFilterMatchAll() {
Iterator<String> unfiltered = Lists.newArrayList("foo", "bar").iterator();
Iterator<String> filtered = Iterators.filter(unfiltered, Predicates.alwaysTrue());
List<String> expected = Lists.newArrayList("foo", "bar");
List<String> actual = Lists.newArrayList(filtered);
assertEquals(expected, actual);
}
public void testFilterNothing() {
Iterator<String> unfiltered = Collections.<String>emptyList().iterator();
Iterator<String> filtered =
Iterators.filter(
unfiltered,
new Predicate<String>() {
@Override
public boolean apply(String s) {
throw new AssertionFailedError("Should never be evaluated");
}
});
List<String> expected = Collections.emptyList();
List<String> actual = Lists.newArrayList(filtered);
assertEquals(expected, actual);
}
@GwtIncompatible // unreasonably slow
public void testFilterUsingIteratorTester() {
final List<Integer> list = asList(1, 2, 3, 4, 5);
final Predicate<Integer> isEven =
new Predicate<Integer>() {
@Override
public boolean apply(Integer integer) {
return integer % 2 == 0;
}
};
new IteratorTester<Integer>(
5, UNMODIFIABLE, asList(2, 4), IteratorTester.KnownOrder.KNOWN_ORDER) {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.filter(list.iterator(), isEven);
}
}.test();
}
public void testAny() {
List<String> list = Lists.newArrayList();
Predicate<String> predicate = Predicates.equalTo("pants");
assertFalse(Iterators.any(list.iterator(), predicate));
list.add("cool");
assertFalse(Iterators.any(list.iterator(), predicate));
list.add("pants");
assertTrue(Iterators.any(list.iterator(), predicate));
}
public void testAll() {
List<String> list = Lists.newArrayList();
Predicate<String> predicate = Predicates.equalTo("cool");
assertTrue(Iterators.all(list.iterator(), predicate));
list.add("cool");
assertTrue(Iterators.all(list.iterator(), predicate));
list.add("pants");
assertFalse(Iterators.all(list.iterator(), predicate));
}
public void testFind_firstElement() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertEquals("cool", Iterators.find(iterator, Predicates.equalTo("cool")));
assertEquals("pants", iterator.next());
}
public void testFind_lastElement() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertEquals("pants", Iterators.find(iterator, Predicates.equalTo("pants")));
assertFalse(iterator.hasNext());
}
public void testFind_notPresent() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
try {
Iterators.find(iterator, Predicates.alwaysFalse());
fail();
} catch (NoSuchElementException e) {
}
assertFalse(iterator.hasNext());
}
public void testFind_matchAlways() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertEquals("cool", Iterators.find(iterator, Predicates.alwaysTrue()));
}
public void testFind_withDefault_first() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertEquals("cool", Iterators.find(iterator, Predicates.equalTo("cool"), "woot"));
assertEquals("pants", iterator.next());
}
public void testFind_withDefault_last() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertEquals("pants", Iterators.find(iterator, Predicates.equalTo("pants"), "woot"));
assertFalse(iterator.hasNext());
}
public void testFind_withDefault_notPresent() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertEquals("woot", Iterators.find(iterator, Predicates.alwaysFalse(), "woot"));
assertFalse(iterator.hasNext());
}
public void testFind_withDefault_notPresent_nullReturn() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertNull(Iterators.find(iterator, Predicates.alwaysFalse(), null));
assertFalse(iterator.hasNext());
}
public void testFind_withDefault_matchAlways() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertEquals("cool", Iterators.find(iterator, Predicates.alwaysTrue(), "woot"));
assertEquals("pants", iterator.next());
}
public void testTryFind_firstElement() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertThat(Iterators.tryFind(iterator, Predicates.equalTo("cool"))).hasValue("cool");
}
public void testTryFind_lastElement() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertThat(Iterators.tryFind(iterator, Predicates.equalTo("pants"))).hasValue("pants");
}
public void testTryFind_alwaysTrue() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertThat(Iterators.tryFind(iterator, Predicates.alwaysTrue())).hasValue("cool");
}
public void testTryFind_alwaysFalse_orDefault() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertEquals("woot", Iterators.tryFind(iterator, Predicates.alwaysFalse()).or("woot"));
assertFalse(iterator.hasNext());
}
public void testTryFind_alwaysFalse_isPresent() {
Iterable<String> list = Lists.newArrayList("cool", "pants");
Iterator<String> iterator = list.iterator();
assertThat(Iterators.tryFind(iterator, Predicates.alwaysFalse())).isAbsent();
assertFalse(iterator.hasNext());
}
public void testTransform() {
Iterator<String> input = asList("1", "2", "3").iterator();
Iterator<Integer> result =
Iterators.transform(
input,
new Function<String, Integer>() {
@Override
public Integer apply(String from) {
return Integer.valueOf(from);
}
});
List<Integer> actual = Lists.newArrayList(result);
List<Integer> expected = asList(1, 2, 3);
assertEquals(expected, actual);
}
public void testTransformRemove() {
List<String> list = Lists.newArrayList("1", "2", "3");
Iterator<String> input = list.iterator();
Iterator<Integer> iterator =
Iterators.transform(
input,
new Function<String, Integer>() {
@Override
public Integer apply(String from) {
return Integer.valueOf(from);
}
});
assertEquals(Integer.valueOf(1), iterator.next());
assertEquals(Integer.valueOf(2), iterator.next());
iterator.remove();
assertEquals(asList("1", "3"), list);
}
public void testPoorlyBehavedTransform() {
Iterator<String> input = asList("1", "not a number", "3").iterator();
Iterator<Integer> result =
Iterators.transform(
input,
new Function<String, Integer>() {
@Override
public Integer apply(String from) {
return Integer.valueOf(from);
}
});
result.next();
try {
result.next();
fail("Expected NFE");
} catch (NumberFormatException expected) {
}
}
public void testNullFriendlyTransform() {
Iterator<@Nullable Integer> input = Arrays.<@Nullable Integer>asList(1, 2, null, 3).iterator();
Iterator<String> result =
Iterators.transform(
input,
new Function<@Nullable Integer, String>() {
@Override
public String apply(@Nullable Integer from) {
return String.valueOf(from);
}
});
List<String> actual = Lists.newArrayList(result);
List<String> expected = asList("1", "2", "null", "3");
assertEquals(expected, actual);
}
public void testCycleOfEmpty() {
// "<String>" for javac 1.5.
Iterator<String> cycle = Iterators.<String>cycle();
assertFalse(cycle.hasNext());
}
public void testCycleOfOne() {
Iterator<String> cycle = Iterators.cycle("a");
for (int i = 0; i < 3; i++) {
assertTrue(cycle.hasNext());
assertEquals("a", cycle.next());
}
}
public void testCycleOfOneWithRemove() {
Iterable<String> iterable = Lists.newArrayList("a");
Iterator<String> cycle = Iterators.cycle(iterable);
assertTrue(cycle.hasNext());
assertEquals("a", cycle.next());
cycle.remove();
assertEquals(Collections.emptyList(), iterable);
assertFalse(cycle.hasNext());
}
public void testCycleOfTwo() {
Iterator<String> cycle = Iterators.cycle("a", "b");
for (int i = 0; i < 3; i++) {
assertTrue(cycle.hasNext());
assertEquals("a", cycle.next());
assertTrue(cycle.hasNext());
assertEquals("b", cycle.next());
}
}
public void testCycleOfTwoWithRemove() {
Iterable<String> iterable = Lists.newArrayList("a", "b");
Iterator<String> cycle = Iterators.cycle(iterable);
assertTrue(cycle.hasNext());
assertEquals("a", cycle.next());
assertTrue(cycle.hasNext());
assertEquals("b", cycle.next());
assertTrue(cycle.hasNext());
assertEquals("a", cycle.next());
cycle.remove();
assertEquals(Collections.singletonList("b"), iterable);
assertTrue(cycle.hasNext());
assertEquals("b", cycle.next());
assertTrue(cycle.hasNext());
assertEquals("b", cycle.next());
cycle.remove();
assertEquals(Collections.emptyList(), iterable);
assertFalse(cycle.hasNext());
}
public void testCycleRemoveWithoutNext() {
Iterator<String> cycle = Iterators.cycle("a", "b");
assertTrue(cycle.hasNext());
try {
cycle.remove();
fail("no exception thrown");
} catch (IllegalStateException expected) {
}
}
public void testCycleRemoveSameElementTwice() {
Iterator<String> cycle = Iterators.cycle("a", "b");
cycle.next();
cycle.remove();
try {
cycle.remove();
fail("no exception thrown");
} catch (IllegalStateException expected) {
}
}
public void testCycleWhenRemoveIsNotSupported() {
Iterable<String> iterable = asList("a", "b");
Iterator<String> cycle = Iterators.cycle(iterable);
cycle.next();
try {
cycle.remove();
fail("no exception thrown");
} catch (UnsupportedOperationException expected) {
}
}
public void testCycleRemoveAfterHasNext() {
Iterable<String> iterable = Lists.newArrayList("a");
Iterator<String> cycle = Iterators.cycle(iterable);
assertTrue(cycle.hasNext());
assertEquals("a", cycle.next());
assertTrue(cycle.hasNext());
cycle.remove();
assertEquals(Collections.emptyList(), iterable);
assertFalse(cycle.hasNext());
}
/** An Iterable whose Iterator is rigorous in checking for concurrent modification. */
private static final class PickyIterable<E> implements Iterable<E> {
final List<E> elements;
int modCount = 0;
PickyIterable(E... elements) {
this.elements = new ArrayList<E>(asList(elements));
}
@Override
public Iterator<E> iterator() {
return new PickyIterator();
}
final class PickyIterator implements Iterator<E> {
int expectedModCount = modCount;
int index = 0;
boolean canRemove;
@Override
public boolean hasNext() {
checkConcurrentModification();
return index < elements.size();
}
@Override
public E next() {
checkConcurrentModification();
if (!hasNext()) {
throw new NoSuchElementException();
}
canRemove = true;
return elements.get(index++);
}
@Override
public void remove() {
checkConcurrentModification();
checkRemove(canRemove);
elements.remove(--index);
expectedModCount = ++modCount;
canRemove = false;
}
void checkConcurrentModification() {
if (expectedModCount != modCount) {
throw new ConcurrentModificationException();
}
}
}
}
public void testCycleRemoveAfterHasNextExtraPicky() {
PickyIterable<String> iterable = new PickyIterable<>("a");
Iterator<String> cycle = Iterators.cycle(iterable);
assertTrue(cycle.hasNext());
assertEquals("a", cycle.next());
assertTrue(cycle.hasNext());
cycle.remove();
assertTrue(iterable.elements.isEmpty());
assertFalse(cycle.hasNext());
}
public void testCycleNoSuchElementException() {
Iterable<String> iterable = Lists.newArrayList("a");
Iterator<String> cycle = Iterators.cycle(iterable);
assertTrue(cycle.hasNext());
assertEquals("a", cycle.next());
cycle.remove();
assertFalse(cycle.hasNext());
try {
cycle.next();
fail();
} catch (NoSuchElementException expected) {
}
}
@GwtIncompatible // unreasonably slow
public void testCycleUsingIteratorTester() {
new IteratorTester<Integer>(
5,
UNMODIFIABLE,
asList(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2),
IteratorTester.KnownOrder.KNOWN_ORDER) {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.cycle(asList(1, 2));
}
}.test();
}
@GwtIncompatible // slow (~5s)
public void testConcatNoIteratorsYieldsEmpty() {
new EmptyIteratorTester() {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.concat();
}
}.test();
}
@GwtIncompatible // slow (~5s)
public void testConcatOneEmptyIteratorYieldsEmpty() {
new EmptyIteratorTester() {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.concat(iterateOver());
}
}.test();
}
@GwtIncompatible // slow (~5s)
public void testConcatMultipleEmptyIteratorsYieldsEmpty() {
new EmptyIteratorTester() {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.concat(iterateOver(), iterateOver());
}
}.test();
}
@GwtIncompatible // slow (~3s)
public void testConcatSingletonYieldsSingleton() {
new SingletonIteratorTester() {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.concat(iterateOver(1));
}
}.test();
}
@GwtIncompatible // slow (~5s)
public void testConcatEmptyAndSingletonAndEmptyYieldsSingleton() {
new SingletonIteratorTester() {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.concat(iterateOver(), iterateOver(1), iterateOver());
}
}.test();
}
@GwtIncompatible // fairly slow (~40s)
public void testConcatSingletonAndSingletonYieldsDoubleton() {
new DoubletonIteratorTester() {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.concat(iterateOver(1), iterateOver(2));
}
}.test();
}
@GwtIncompatible // fairly slow (~40s)
public void testConcatSingletonAndSingletonWithEmptiesYieldsDoubleton() {
new DoubletonIteratorTester() {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.concat(iterateOver(1), iterateOver(), iterateOver(), iterateOver(2));
}
}.test();
}
@GwtIncompatible // fairly slow (~50s)
public void testConcatUnmodifiable() {
new IteratorTester<Integer>(
5, UNMODIFIABLE, asList(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER) {
@Override
protected Iterator<Integer> newTargetIterator() {
return Iterators.concat(
asList(1).iterator(), Arrays.<Integer>asList().iterator(), asList(2).iterator());
}
}.test();
}
public void testConcatPartiallyAdvancedSecond() {
Iterator<String> itr1 =
Iterators.concat(Iterators.singletonIterator("a"), Iterators.forArray("b", "c"));
assertEquals("a", itr1.next());
assertEquals("b", itr1.next());
Iterator<String> itr2 = Iterators.concat(Iterators.singletonIterator("d"), itr1);
assertEquals("d", itr2.next());
assertEquals("c", itr2.next());
}
public void testConcatPartiallyAdvancedFirst() {
Iterator<String> itr1 =
Iterators.concat(Iterators.singletonIterator("a"), Iterators.forArray("b", "c"));
assertEquals("a", itr1.next());
assertEquals("b", itr1.next());
Iterator<String> itr2 = Iterators.concat(itr1, Iterators.singletonIterator("d"));
assertEquals("c", itr2.next());
assertEquals("d", itr2.next());
}
/** Illustrates the somewhat bizarre behavior when a null is passed in. */
public void testConcatContainingNull() {
Iterator<Iterator<Integer>> input =
(Iterator<Iterator<Integer>>)
Arrays.<@Nullable Iterator<Integer>>asList(iterateOver(1, 2), null, iterateOver(3))
.iterator();
Iterator<Integer> result = Iterators.concat(input);
assertEquals(1, (int) result.next());
assertEquals(2, (int) result.next());
try {
result.hasNext();
fail("no exception thrown");
} catch (NullPointerException e) {
}
try {
result.next();
fail("no exception thrown");
} catch (NullPointerException e) {
}
// There is no way to get "through" to the 3. Buh-bye
}
public void testConcatVarArgsContainingNull() {
try {
Iterators.concat(iterateOver(1, 2), null, iterateOver(3), iterateOver(4), iterateOver(5));
fail("no exception thrown");
} catch (NullPointerException e) {
}
}
public void testConcatNested_appendToEnd() {
final int nestingDepth = 128;
Iterator<Integer> iterator = iterateOver();
for (int i = 0; i < nestingDepth; i++) {
iterator = Iterators.concat(iterator, iterateOver(1));
}
assertEquals(nestingDepth, Iterators.size(iterator));
}
public void testConcatNested_appendToBeginning() {
final int nestingDepth = 128;
Iterator<Integer> iterator = iterateOver();
for (int i = 0; i < nestingDepth; i++) {
iterator = Iterators.concat(iterateOver(1), iterator);
}
assertEquals(nestingDepth, Iterators.size(iterator));
}
public void testAddAllWithEmptyIterator() {
List<String> alreadyThere = Lists.newArrayList("already", "there");
boolean changed = Iterators.addAll(alreadyThere, Iterators.<String>emptyIterator());
assertThat(alreadyThere).containsExactly("already", "there").inOrder();
assertFalse(changed);
}
public void testAddAllToList() {
List<String> alreadyThere = Lists.newArrayList("already", "there");
List<String> freshlyAdded = Lists.newArrayList("freshly", "added");
boolean changed = Iterators.addAll(alreadyThere, freshlyAdded.iterator());
assertThat(alreadyThere).containsExactly("already", "there", "freshly", "added");
assertTrue(changed);
}
public void testAddAllToSet() {
Set<String> alreadyThere = Sets.newLinkedHashSet(asList("already", "there"));
List<String> oneMore = Lists.newArrayList("there");
boolean changed = Iterators.addAll(alreadyThere, oneMore.iterator());
assertThat(alreadyThere).containsExactly("already", "there").inOrder();
assertFalse(changed);
}
@J2ktIncompatible
@GwtIncompatible // NullPointerTester
public void testNullPointerExceptions() {
NullPointerTester tester = new NullPointerTester();
tester.testAllPublicStaticMethods(Iterators.class);
}
@GwtIncompatible // Only used by @GwtIncompatible code
private abstract static class EmptyIteratorTester extends IteratorTester<Integer> {
protected EmptyIteratorTester() {
super(3, MODIFIABLE, Collections.<Integer>emptySet(), IteratorTester.KnownOrder.KNOWN_ORDER);
}
}
@GwtIncompatible // Only used by @GwtIncompatible code
private abstract static class SingletonIteratorTester extends IteratorTester<Integer> {
protected SingletonIteratorTester() {
super(3, MODIFIABLE, singleton(1), IteratorTester.KnownOrder.KNOWN_ORDER);
}
}
@GwtIncompatible // Only used by @GwtIncompatible code
private abstract static class DoubletonIteratorTester extends IteratorTester<Integer> {
protected DoubletonIteratorTester() {
super(5, MODIFIABLE, newArrayList(1, 2), IteratorTester.KnownOrder.KNOWN_ORDER);
}
}
private static Iterator<Integer> iterateOver(int... values) {
// Note: Ints.asList's iterator does not support remove which we need for testing.
return new ArrayList<>(Ints.asList(values)).iterator();
}
public void testElementsEqual() {
Iterable<?> a;
Iterable<?> b;
// Base case.
a = Lists.newArrayList();
b = Collections.emptySet();
assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator()));
// A few elements.
a = asList(4, 8, 15, 16, 23, 42);
b = asList(4, 8, 15, 16, 23, 42);
assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator()));
// The same, but with nulls.
a = Arrays.<@Nullable Integer>asList(4, 8, null, 16, 23, 42);
b = Arrays.<@Nullable Integer>asList(4, 8, null, 16, 23, 42);
assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator()));
// Different Iterable types (still equal elements, though).
a = ImmutableList.of(4, 8, 15, 16, 23, 42);
b = asList(4, 8, 15, 16, 23, 42);
assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator()));
// An element differs.
a = asList(4, 8, 15, 12, 23, 42);
b = asList(4, 8, 15, 16, 23, 42);
assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator()));
// null versus non-null.
a = Arrays.<@Nullable Integer>asList(4, 8, 15, null, 23, 42);
b = asList(4, 8, 15, 16, 23, 42);
assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator()));
assertFalse(Iterators.elementsEqual(b.iterator(), a.iterator()));
// Different lengths.
a = asList(4, 8, 15, 16, 23);
b = asList(4, 8, 15, 16, 23, 42);
assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator()));
assertFalse(Iterators.elementsEqual(b.iterator(), a.iterator()));
// Different lengths, one is empty.
a = Collections.emptySet();
b = asList(4, 8, 15, 16, 23, 42);
assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator()));
assertFalse(Iterators.elementsEqual(b.iterator(), a.iterator()));
}
public void testPartition_badSize() {
Iterator<Integer> source = Iterators.singletonIterator(1);
try {
Iterators.partition(source, 0);
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testPartition_empty() {
Iterator<Integer> source = Iterators.emptyIterator();
Iterator<List<Integer>> partitions = Iterators.partition(source, 1);
assertFalse(partitions.hasNext());
}
public void testPartition_singleton1() {
Iterator<Integer> source = Iterators.singletonIterator(1);
Iterator<List<Integer>> partitions = Iterators.partition(source, 1);
assertTrue(partitions.hasNext());
assertTrue(partitions.hasNext());
assertEquals(ImmutableList.of(1), partitions.next());
assertFalse(partitions.hasNext());
}