1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import static com.google.common.collect.Iterators.peekingIterator;
20 import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
21 import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
22 import static java.util.Collections.emptyList;
23
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.annotations.GwtIncompatible;
26 import com.google.common.collect.testing.IteratorTester;
27
28 import junit.framework.TestCase;
29
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.NoSuchElementException;
35
36
37
38
39
40
41 @SuppressWarnings("serial")
42 @GwtCompatible(emulated = true)
43 public class PeekingIteratorTest extends TestCase {
44
45
46
47
48
49
50
51
52
53
54
55 private static class PeekingIteratorTester<T> extends IteratorTester<T> {
56 private Iterable<T> master;
57 private List<T> targetList;
58
59 public PeekingIteratorTester(Collection<T> master) {
60 super(master.size() + 3, MODIFIABLE, master,
61 IteratorTester.KnownOrder.KNOWN_ORDER);
62 this.master = master;
63 }
64 @Override protected Iterator<T> newTargetIterator() {
65
66 targetList = Lists.newArrayList(master);
67 Iterator<T> iterator = targetList.iterator();
68 return Iterators.peekingIterator(iterator);
69 }
70 @Override protected void verify(List<T> elements) {
71
72 assertEquals(elements, targetList);
73 }
74 }
75
76 private <T> void actsLikeIteratorHelper(final List<T> list) {
77
78 new PeekingIteratorTester<T>(list).test();
79
80
81 new IteratorTester<T>(list.size() * 2 + 2, UNMODIFIABLE, list,
82 IteratorTester.KnownOrder.KNOWN_ORDER) {
83 @Override protected Iterator<T> newTargetIterator() {
84 Iterator<T> iterator = Collections.unmodifiableList(list).iterator();
85 return Iterators.peekingIterator(iterator);
86 }
87 }.test();
88 }
89
90 public void testPeekingIteratorBehavesLikeIteratorOnEmptyIterable() {
91 actsLikeIteratorHelper(Collections.emptyList());
92 }
93
94 public void testPeekingIteratorBehavesLikeIteratorOnSingletonIterable() {
95 actsLikeIteratorHelper(Collections.singletonList(new Object()));
96 }
97
98
99 @GwtIncompatible("works but takes 5 minutes to run")
100 public void testPeekingIteratorBehavesLikeIteratorOnThreeElementIterable() {
101 actsLikeIteratorHelper(Lists.newArrayList("A", "B", "C"));
102 }
103
104 @GwtIncompatible("works but takes 5 minutes to run")
105 public void testPeekingIteratorAcceptsNullElements() {
106 actsLikeIteratorHelper(Lists.newArrayList(null, "A", null));
107 }
108
109 public void testPeekOnEmptyList() {
110 List<?> list = Collections.emptyList();
111 Iterator<?> iterator = list.iterator();
112 PeekingIterator<?> peekingIterator = Iterators.peekingIterator(iterator);
113
114 try {
115 peekingIterator.peek();
116 fail("Should throw NoSuchElementException if nothing to peek()");
117 } catch (NoSuchElementException e) { }
118 }
119
120 public void testPeekDoesntChangeIteration() {
121 List<?> list = Lists.newArrayList("A", "B", "C");
122 Iterator<?> iterator = list.iterator();
123 PeekingIterator<?> peekingIterator =
124 Iterators.peekingIterator(iterator);
125
126 assertEquals("Should be able to peek() at first element",
127 "A", peekingIterator.peek());
128 assertEquals("Should be able to peek() first element multiple times",
129 "A", peekingIterator.peek());
130 assertEquals("next() should still return first element after peeking",
131 "A", peekingIterator.next());
132
133 assertEquals("Should be able to peek() at middle element",
134 "B", peekingIterator.peek());
135 assertEquals("Should be able to peek() middle element multiple times",
136 "B", peekingIterator.peek());
137 assertEquals("next() should still return middle element after peeking",
138 "B", peekingIterator.next());
139
140 assertEquals("Should be able to peek() at last element",
141 "C", peekingIterator.peek());
142 assertEquals("Should be able to peek() last element multiple times",
143 "C", peekingIterator.peek());
144 assertEquals("next() should still return last element after peeking",
145 "C", peekingIterator.next());
146
147 try {
148 peekingIterator.peek();
149 fail("Should throw exception if no next to peek()");
150 } catch (NoSuchElementException e) { }
151 try {
152 peekingIterator.peek();
153 fail("Should continue to throw exception if no next to peek()");
154 } catch (NoSuchElementException e) { }
155 try {
156 peekingIterator.next();
157 fail("next() should still throw exception after the end of iteration");
158 } catch (NoSuchElementException e) { }
159 }
160
161 public void testCantRemoveAfterPeek() {
162 List<String> list = Lists.newArrayList("A", "B", "C");
163 Iterator<String> iterator = list.iterator();
164 PeekingIterator<?> peekingIterator = Iterators.peekingIterator(iterator);
165
166 assertEquals("A", peekingIterator.next());
167 assertEquals("B", peekingIterator.peek());
168
169
170 try {
171 peekingIterator.remove();
172 fail("remove() should throw IllegalStateException after a peek()");
173 } catch (IllegalStateException e) { }
174
175 assertEquals("After remove() throws exception, peek should still be ok",
176 "B", peekingIterator.peek());
177
178
179 assertEquals("B", peekingIterator.next());
180 peekingIterator.remove();
181 assertEquals("Should have removed an element", 2, list.size());
182 assertFalse("Second element should be gone", list.contains("B"));
183 }
184
185 static class ThrowsAtEndException extends RuntimeException { }
186
187
188
189
190
191
192 static class ThrowsAtEndIterator<E> implements Iterator<E> {
193 Iterator<E> iterator;
194 public ThrowsAtEndIterator(Iterable<E> iterable) {
195 this.iterator = iterable.iterator();
196 }
197 @Override
198 public boolean hasNext() {
199 return true;
200 }
201 @Override
202 public E next() {
203
204 if (!iterator.hasNext()) {
205 throw new ThrowsAtEndException();
206 }
207 return iterator.next();
208 }
209 @Override
210 public void remove() {
211 iterator.remove();
212 }
213 }
214
215 public void testPeekingIteratorDoesntAdvancePrematurely() throws Exception {
216
217
218
219
220
221
222
223
224
225
226 List<Integer> list = emptyList();
227 Iterator<Integer> iterator =
228 peekingIterator(new ThrowsAtEndIterator<Integer>(list));
229 assertNextThrows(iterator);
230
231
232
233 list = Lists.newArrayList(1, 2);
234 iterator = peekingIterator(new ThrowsAtEndIterator<Integer>(list));
235 assertTrue(iterator.hasNext());
236 iterator.next();
237 assertTrue(iterator.hasNext());
238 iterator.next();
239 assertNextThrows(iterator);
240 }
241
242 private void assertNextThrows(Iterator<?> iterator) {
243 try {
244 iterator.next();
245 fail();
246 } catch (ThrowsAtEndException expected) {
247 }
248 }
249 }