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()); |
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 (!impl.permitsNulls() && 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 |
+ |
if (!impl.permitsNulls() && c instanceof BlockingDeque) { |
183 |
+ |
BlockingDeque q = (BlockingDeque) c; |
184 |
+ |
assertThrows( |
185 |
+ |
NullPointerException.class, |
186 |
+ |
() -> { |
187 |
+ |
try { q.offerFirst(null, 1L, MILLISECONDS); } |
188 |
+ |
catch (InterruptedException ex) { |
189 |
+ |
throw new AssertionError(ex); |
190 |
+ |
}}, |
191 |
+ |
() -> { |
192 |
+ |
try { q.offerLast(null, 1L, MILLISECONDS); } |
193 |
+ |
catch (InterruptedException ex) { |
194 |
+ |
throw new AssertionError(ex); |
195 |
+ |
}}); |
196 |
+ |
} |
197 |
|
} |
198 |
|
|
199 |
|
public void testNoSuchElementExceptions() { |
202 |
|
NoSuchElementException.class, |
203 |
|
() -> c.iterator().next()); |
204 |
|
|
205 |
< |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
205 |
> |
if (c instanceof Queue) { |
206 |
|
Queue q = (Queue) c; |
207 |
|
assertThrows( |
208 |
|
NoSuchElementException.class, |
209 |
|
() -> q.element(), |
210 |
|
() -> q.remove()); |
211 |
|
} |
212 |
< |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
212 |
> |
if (c instanceof Deque) { |
213 |
|
Deque d = (Deque) c; |
214 |
|
assertThrows( |
215 |
|
NoSuchElementException.class, |
279 |
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
280 |
|
ArrayList iterated = new ArrayList(); |
281 |
|
ArrayList iteratedForEachRemaining = new ArrayList(); |
282 |
+ |
ArrayList tryAdvanced = new ArrayList(); |
283 |
|
ArrayList spliterated = new ArrayList(); |
284 |
< |
ArrayList foreached = new ArrayList(); |
284 |
> |
ArrayList forEached = new ArrayList(); |
285 |
> |
ArrayList removeIfed = new ArrayList(); |
286 |
|
for (Object x : c) iterated.add(x); |
287 |
|
c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); |
288 |
+ |
for (Spliterator s = c.spliterator(); |
289 |
+ |
s.tryAdvance(e -> tryAdvanced.add(e)); ) {} |
290 |
|
c.spliterator().forEachRemaining(e -> spliterated.add(e)); |
291 |
< |
c.forEach(e -> foreached.add(e)); |
291 |
> |
c.forEach(e -> forEached.add(e)); |
292 |
> |
c.removeIf(e -> { removeIfed.add(e); return false; }); |
293 |
|
boolean ordered = |
294 |
|
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
295 |
|
if (c instanceof List || c instanceof Deque) |
296 |
|
assertTrue(ordered); |
297 |
|
if (ordered) { |
298 |
|
assertEquals(iterated, iteratedForEachRemaining); |
299 |
+ |
assertEquals(iterated, tryAdvanced); |
300 |
|
assertEquals(iterated, spliterated); |
301 |
< |
assertEquals(iterated, foreached); |
301 |
> |
assertEquals(iterated, forEached); |
302 |
> |
assertEquals(iterated, removeIfed); |
303 |
|
} else { |
304 |
|
HashSet cset = new HashSet(c); |
305 |
|
assertEquals(cset, new HashSet(iterated)); |
306 |
|
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
307 |
+ |
assertEquals(cset, new HashSet(tryAdvanced)); |
308 |
|
assertEquals(cset, new HashSet(spliterated)); |
309 |
< |
assertEquals(cset, new HashSet(foreached)); |
309 |
> |
assertEquals(cset, new HashSet(forEached)); |
310 |
> |
assertEquals(cset, new HashSet(removeIfed)); |
311 |
|
} |
312 |
|
if (c instanceof Deque) { |
313 |
|
Deque d = (Deque) c; |