17 |
|
import java.util.NoSuchElementException; |
18 |
|
import java.util.Queue; |
19 |
|
import java.util.Spliterator; |
20 |
+ |
import java.util.concurrent.BlockingDeque; |
21 |
+ |
import java.util.concurrent.BlockingQueue; |
22 |
|
import java.util.concurrent.CountDownLatch; |
23 |
|
import java.util.concurrent.Executors; |
24 |
|
import java.util.concurrent.ExecutorService; |
59 |
|
} |
60 |
|
|
61 |
|
/** Checks properties of empty collections. */ |
62 |
< |
public void testEmptyMeansEmpty() { |
62 |
> |
public void testEmptyMeansEmpty() throws InterruptedException { |
63 |
|
Collection c = impl.emptyCollection(); |
64 |
+ |
emptyMeansEmpty(c); |
65 |
+ |
|
66 |
+ |
if (c instanceof java.io.Serializable) |
67 |
+ |
emptyMeansEmpty(serialClone(c)); |
68 |
+ |
|
69 |
+ |
Collection clone = cloneableClone(c); |
70 |
+ |
if (clone != null) |
71 |
+ |
emptyMeansEmpty(clone); |
72 |
+ |
} |
73 |
+ |
|
74 |
+ |
void emptyMeansEmpty(Collection c) throws InterruptedException { |
75 |
|
assertTrue(c.isEmpty()); |
76 |
|
assertEquals(0, c.size()); |
77 |
|
assertEquals("[]", c.toString()); |
105 |
|
assertEquals(0, c.spliterator().estimateSize()); |
106 |
|
assertFalse(c.contains(bomb())); |
107 |
|
assertFalse(c.remove(bomb())); |
108 |
< |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
108 |
> |
if (c instanceof Queue) { |
109 |
|
Queue q = (Queue) c; |
110 |
|
assertNull(q.peek()); |
111 |
|
assertNull(q.poll()); |
112 |
|
} |
113 |
< |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
113 |
> |
if (c instanceof Deque) { |
114 |
|
Deque d = (Deque) c; |
115 |
|
assertNull(d.peekFirst()); |
116 |
|
assertNull(d.peekLast()); |
121 |
|
assertFalse(d.removeFirstOccurrence(bomb())); |
122 |
|
assertFalse(d.removeLastOccurrence(bomb())); |
123 |
|
} |
124 |
+ |
if (c instanceof BlockingQueue) { |
125 |
+ |
BlockingQueue q = (BlockingQueue) c; |
126 |
+ |
assertNull(q.poll(0L, MILLISECONDS)); |
127 |
+ |
} |
128 |
+ |
if (c instanceof BlockingDeque) { |
129 |
+ |
BlockingDeque q = (BlockingDeque) c; |
130 |
+ |
assertNull(q.pollFirst(0L, MILLISECONDS)); |
131 |
+ |
assertNull(q.pollLast(0L, MILLISECONDS)); |
132 |
+ |
} |
133 |
|
} |
134 |
|
|
135 |
< |
public void testNullPointerExceptions() { |
135 |
> |
public void testNullPointerExceptions() throws InterruptedException { |
136 |
|
Collection c = impl.emptyCollection(); |
137 |
|
assertThrows( |
138 |
|
NullPointerException.class, |
152 |
|
NullPointerException.class, |
153 |
|
() -> c.add(null)); |
154 |
|
} |
155 |
< |
if (!impl.permitsNulls() |
134 |
< |
&& Queue.class.isAssignableFrom(impl.klazz())) { |
155 |
> |
if (!impl.permitsNulls() && c instanceof Queue) { |
156 |
|
Queue q = (Queue) c; |
157 |
|
assertThrows( |
158 |
|
NullPointerException.class, |
159 |
|
() -> q.offer(null)); |
160 |
|
} |
161 |
< |
if (!impl.permitsNulls() |
141 |
< |
&& Deque.class.isAssignableFrom(impl.klazz())) { |
161 |
> |
if (!impl.permitsNulls() && c instanceof Deque) { |
162 |
|
Deque d = (Deque) c; |
163 |
|
assertThrows( |
164 |
|
NullPointerException.class, |
169 |
|
() -> d.push(null), |
170 |
|
() -> d.descendingIterator().forEachRemaining(null)); |
171 |
|
} |
172 |
+ |
if (c instanceof BlockingQueue) { |
173 |
+ |
BlockingQueue q = (BlockingQueue) c; |
174 |
+ |
assertThrows( |
175 |
+ |
NullPointerException.class, |
176 |
+ |
() -> { |
177 |
+ |
try { q.offer(null, 1L, MILLISECONDS); } |
178 |
+ |
catch (InterruptedException ex) { |
179 |
+ |
throw new AssertionError(ex); |
180 |
+ |
}}, |
181 |
+ |
() -> { |
182 |
+ |
try { q.put(null); } |
183 |
+ |
catch (InterruptedException ex) { |
184 |
+ |
throw new AssertionError(ex); |
185 |
+ |
}}); |
186 |
+ |
} |
187 |
+ |
if (c instanceof BlockingDeque) { |
188 |
+ |
BlockingDeque q = (BlockingDeque) c; |
189 |
+ |
assertThrows( |
190 |
+ |
NullPointerException.class, |
191 |
+ |
() -> { |
192 |
+ |
try { q.offerFirst(null, 1L, MILLISECONDS); } |
193 |
+ |
catch (InterruptedException ex) { |
194 |
+ |
throw new AssertionError(ex); |
195 |
+ |
}}, |
196 |
+ |
() -> { |
197 |
+ |
try { q.offerLast(null, 1L, MILLISECONDS); } |
198 |
+ |
catch (InterruptedException ex) { |
199 |
+ |
throw new AssertionError(ex); |
200 |
+ |
}}, |
201 |
+ |
() -> { |
202 |
+ |
try { q.putFirst(null); } |
203 |
+ |
catch (InterruptedException ex) { |
204 |
+ |
throw new AssertionError(ex); |
205 |
+ |
}}, |
206 |
+ |
() -> { |
207 |
+ |
try { q.putLast(null); } |
208 |
+ |
catch (InterruptedException ex) { |
209 |
+ |
throw new AssertionError(ex); |
210 |
+ |
}}); |
211 |
+ |
} |
212 |
|
} |
213 |
|
|
214 |
|
public void testNoSuchElementExceptions() { |
217 |
|
NoSuchElementException.class, |
218 |
|
() -> c.iterator().next()); |
219 |
|
|
220 |
< |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
220 |
> |
if (c instanceof Queue) { |
221 |
|
Queue q = (Queue) c; |
222 |
|
assertThrows( |
223 |
|
NoSuchElementException.class, |
224 |
|
() -> q.element(), |
225 |
|
() -> q.remove()); |
226 |
|
} |
227 |
< |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
227 |
> |
if (c instanceof Deque) { |
228 |
|
Deque d = (Deque) c; |
229 |
|
assertThrows( |
230 |
|
NoSuchElementException.class, |
294 |
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
295 |
|
ArrayList iterated = new ArrayList(); |
296 |
|
ArrayList iteratedForEachRemaining = new ArrayList(); |
297 |
+ |
ArrayList tryAdvanced = new ArrayList(); |
298 |
|
ArrayList spliterated = new ArrayList(); |
299 |
< |
ArrayList foreached = new ArrayList(); |
299 |
> |
ArrayList forEached = new ArrayList(); |
300 |
> |
ArrayList removeIfed = new ArrayList(); |
301 |
|
for (Object x : c) iterated.add(x); |
302 |
|
c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); |
303 |
+ |
for (Spliterator s = c.spliterator(); |
304 |
+ |
s.tryAdvance(e -> tryAdvanced.add(e)); ) {} |
305 |
|
c.spliterator().forEachRemaining(e -> spliterated.add(e)); |
306 |
< |
c.forEach(e -> foreached.add(e)); |
306 |
> |
c.forEach(e -> forEached.add(e)); |
307 |
> |
c.removeIf(e -> { removeIfed.add(e); return false; }); |
308 |
|
boolean ordered = |
309 |
|
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
310 |
|
if (c instanceof List || c instanceof Deque) |
311 |
|
assertTrue(ordered); |
312 |
|
if (ordered) { |
313 |
|
assertEquals(iterated, iteratedForEachRemaining); |
314 |
+ |
assertEquals(iterated, tryAdvanced); |
315 |
|
assertEquals(iterated, spliterated); |
316 |
< |
assertEquals(iterated, foreached); |
316 |
> |
assertEquals(iterated, forEached); |
317 |
> |
assertEquals(iterated, removeIfed); |
318 |
|
} else { |
319 |
|
HashSet cset = new HashSet(c); |
320 |
|
assertEquals(cset, new HashSet(iterated)); |
321 |
|
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
322 |
+ |
assertEquals(cset, new HashSet(tryAdvanced)); |
323 |
|
assertEquals(cset, new HashSet(spliterated)); |
324 |
< |
assertEquals(cset, new HashSet(foreached)); |
324 |
> |
assertEquals(cset, new HashSet(forEached)); |
325 |
> |
assertEquals(cset, new HashSet(removeIfed)); |
326 |
|
} |
327 |
|
if (c instanceof Deque) { |
328 |
|
Deque d = (Deque) c; |