5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
+ |
import static java.util.concurrent.TimeUnit.HOURS; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
|
import java.util.ArrayList; |
18 |
|
import java.util.NoSuchElementException; |
19 |
|
import java.util.Queue; |
20 |
|
import java.util.Spliterator; |
21 |
+ |
import java.util.concurrent.BlockingDeque; |
22 |
+ |
import java.util.concurrent.BlockingQueue; |
23 |
|
import java.util.concurrent.CountDownLatch; |
24 |
|
import java.util.concurrent.Executors; |
25 |
|
import java.util.concurrent.ExecutorService; |
52 |
|
impl); |
53 |
|
} |
54 |
|
|
55 |
+ |
Object bomb() { |
56 |
+ |
return new Object() { |
57 |
+ |
public boolean equals(Object x) { throw new AssertionError(); } |
58 |
+ |
public int hashCode() { throw new AssertionError(); } |
59 |
+ |
}; |
60 |
+ |
} |
61 |
+ |
|
62 |
|
/** Checks properties of empty collections. */ |
63 |
< |
public void testEmptyMeansEmpty() { |
63 |
> |
public void testEmptyMeansEmpty() throws InterruptedException { |
64 |
|
Collection c = impl.emptyCollection(); |
65 |
+ |
emptyMeansEmpty(c); |
66 |
+ |
|
67 |
+ |
if (c instanceof java.io.Serializable) |
68 |
+ |
emptyMeansEmpty(serialClone(c)); |
69 |
+ |
|
70 |
+ |
Collection clone = cloneableClone(c); |
71 |
+ |
if (clone != null) |
72 |
+ |
emptyMeansEmpty(clone); |
73 |
+ |
} |
74 |
+ |
|
75 |
+ |
void emptyMeansEmpty(Collection c) throws InterruptedException { |
76 |
|
assertTrue(c.isEmpty()); |
77 |
|
assertEquals(0, c.size()); |
78 |
|
assertEquals("[]", c.toString()); |
102 |
|
c.iterator().forEachRemaining(alwaysThrows); |
103 |
|
c.spliterator().forEachRemaining(alwaysThrows); |
104 |
|
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
105 |
< |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
105 |
> |
if (c.spliterator().hasCharacteristics(Spliterator.SIZED)) |
106 |
> |
assertEquals(0, c.spliterator().estimateSize()); |
107 |
> |
assertFalse(c.contains(bomb())); |
108 |
> |
assertFalse(c.remove(bomb())); |
109 |
> |
if (c instanceof Queue) { |
110 |
|
Queue q = (Queue) c; |
111 |
|
assertNull(q.peek()); |
112 |
|
assertNull(q.poll()); |
113 |
|
} |
114 |
< |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
114 |
> |
if (c instanceof Deque) { |
115 |
|
Deque d = (Deque) c; |
116 |
|
assertNull(d.peekFirst()); |
117 |
|
assertNull(d.peekLast()); |
118 |
|
assertNull(d.pollFirst()); |
119 |
|
assertNull(d.pollLast()); |
120 |
|
assertIteratorExhausted(d.descendingIterator()); |
121 |
+ |
d.descendingIterator().forEachRemaining(alwaysThrows); |
122 |
+ |
assertFalse(d.removeFirstOccurrence(bomb())); |
123 |
+ |
assertFalse(d.removeLastOccurrence(bomb())); |
124 |
+ |
} |
125 |
+ |
if (c instanceof BlockingQueue) { |
126 |
+ |
BlockingQueue q = (BlockingQueue) c; |
127 |
+ |
assertNull(q.poll(0L, MILLISECONDS)); |
128 |
+ |
} |
129 |
+ |
if (c instanceof BlockingDeque) { |
130 |
+ |
BlockingDeque q = (BlockingDeque) c; |
131 |
+ |
assertNull(q.pollFirst(0L, MILLISECONDS)); |
132 |
+ |
assertNull(q.pollLast(0L, MILLISECONDS)); |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
< |
public void testNullPointerExceptions() { |
136 |
> |
public void testNullPointerExceptions() throws InterruptedException { |
137 |
|
Collection c = impl.emptyCollection(); |
138 |
|
assertThrows( |
139 |
|
NullPointerException.class, |
153 |
|
NullPointerException.class, |
154 |
|
() -> c.add(null)); |
155 |
|
} |
156 |
< |
if (!impl.permitsNulls() |
120 |
< |
&& Queue.class.isAssignableFrom(impl.klazz())) { |
156 |
> |
if (!impl.permitsNulls() && c instanceof Queue) { |
157 |
|
Queue q = (Queue) c; |
158 |
|
assertThrows( |
159 |
|
NullPointerException.class, |
160 |
|
() -> q.offer(null)); |
161 |
|
} |
162 |
< |
if (!impl.permitsNulls() |
127 |
< |
&& Deque.class.isAssignableFrom(impl.klazz())) { |
162 |
> |
if (!impl.permitsNulls() && c instanceof Deque) { |
163 |
|
Deque d = (Deque) c; |
164 |
|
assertThrows( |
165 |
|
NullPointerException.class, |
170 |
|
() -> d.push(null), |
171 |
|
() -> d.descendingIterator().forEachRemaining(null)); |
172 |
|
} |
173 |
+ |
if (c instanceof BlockingQueue) { |
174 |
+ |
BlockingQueue q = (BlockingQueue) c; |
175 |
+ |
assertThrows( |
176 |
+ |
NullPointerException.class, |
177 |
+ |
() -> { |
178 |
+ |
try { q.offer(null, 1L, HOURS); } |
179 |
+ |
catch (InterruptedException ex) { |
180 |
+ |
throw new AssertionError(ex); |
181 |
+ |
}}, |
182 |
+ |
() -> { |
183 |
+ |
try { q.put(null); } |
184 |
+ |
catch (InterruptedException ex) { |
185 |
+ |
throw new AssertionError(ex); |
186 |
+ |
}}); |
187 |
+ |
} |
188 |
+ |
if (c instanceof BlockingDeque) { |
189 |
+ |
BlockingDeque q = (BlockingDeque) c; |
190 |
+ |
assertThrows( |
191 |
+ |
NullPointerException.class, |
192 |
+ |
() -> { |
193 |
+ |
try { q.offerFirst(null, 1L, HOURS); } |
194 |
+ |
catch (InterruptedException ex) { |
195 |
+ |
throw new AssertionError(ex); |
196 |
+ |
}}, |
197 |
+ |
() -> { |
198 |
+ |
try { q.offerLast(null, 1L, HOURS); } |
199 |
+ |
catch (InterruptedException ex) { |
200 |
+ |
throw new AssertionError(ex); |
201 |
+ |
}}, |
202 |
+ |
() -> { |
203 |
+ |
try { q.putFirst(null); } |
204 |
+ |
catch (InterruptedException ex) { |
205 |
+ |
throw new AssertionError(ex); |
206 |
+ |
}}, |
207 |
+ |
() -> { |
208 |
+ |
try { q.putLast(null); } |
209 |
+ |
catch (InterruptedException ex) { |
210 |
+ |
throw new AssertionError(ex); |
211 |
+ |
}}); |
212 |
+ |
} |
213 |
|
} |
214 |
|
|
215 |
|
public void testNoSuchElementExceptions() { |
218 |
|
NoSuchElementException.class, |
219 |
|
() -> c.iterator().next()); |
220 |
|
|
221 |
< |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
221 |
> |
if (c instanceof Queue) { |
222 |
|
Queue q = (Queue) c; |
223 |
|
assertThrows( |
224 |
|
NoSuchElementException.class, |
225 |
|
() -> q.element(), |
226 |
|
() -> q.remove()); |
227 |
|
} |
228 |
< |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
228 |
> |
if (c instanceof Deque) { |
229 |
|
Deque d = (Deque) c; |
230 |
|
assertThrows( |
231 |
|
NoSuchElementException.class, |
257 |
|
} |
258 |
|
}; |
259 |
|
try { |
260 |
< |
boolean modified = c.removeIf(randomPredicate); |
261 |
< |
if (!modified) { |
262 |
< |
assertNull(threwAt.get()); |
263 |
< |
assertEquals(n, rejects.size()); |
264 |
< |
assertEquals(0, accepts.size()); |
265 |
< |
} |
266 |
< |
} catch (ArithmeticException ok) {} |
267 |
< |
survivors.removeAll(accepts); |
268 |
< |
if (n - accepts.size() != c.size()) { |
260 |
> |
try { |
261 |
> |
boolean modified = c.removeIf(randomPredicate); |
262 |
> |
if (!modified) { |
263 |
> |
assertNull(threwAt.get()); |
264 |
> |
assertEquals(n, rejects.size()); |
265 |
> |
assertEquals(0, accepts.size()); |
266 |
> |
} |
267 |
> |
} catch (ArithmeticException ok) {} |
268 |
> |
survivors.removeAll(accepts); |
269 |
> |
assertEquals(n - accepts.size(), c.size()); |
270 |
> |
assertTrue(c.containsAll(survivors)); |
271 |
> |
assertTrue(survivors.containsAll(rejects)); |
272 |
> |
for (Object x : accepts) assertFalse(c.contains(x)); |
273 |
> |
if (threwAt.get() == null) |
274 |
> |
assertEquals(accepts.size() + rejects.size(), n); |
275 |
> |
} catch (Throwable ex) { |
276 |
|
System.err.println(impl.klazz()); |
277 |
< |
System.err.println(c); |
278 |
< |
System.err.println(accepts); |
279 |
< |
System.err.println(rejects); |
280 |
< |
System.err.println(survivors); |
281 |
< |
System.err.println(threwAt.get()); |
282 |
< |
} |
283 |
< |
assertEquals(n - accepts.size(), c.size()); |
284 |
< |
assertTrue(c.containsAll(survivors)); |
203 |
< |
assertTrue(survivors.containsAll(rejects)); |
204 |
< |
for (Object x : accepts) assertFalse(c.contains(x)); |
205 |
< |
if (threwAt.get() == null) |
206 |
< |
assertEquals(accepts.size() + rejects.size(), n); |
277 |
> |
System.err.printf("c=%s%n", c); |
278 |
> |
System.err.printf("n=%d%n", n); |
279 |
> |
System.err.printf("accepts=%s%n", accepts); |
280 |
> |
System.err.printf("rejects=%s%n", rejects); |
281 |
> |
System.err.printf("survivors=%s%n", survivors); |
282 |
> |
System.err.printf("threw=%s%n", threwAt.get()); |
283 |
> |
throw ex; |
284 |
> |
} |
285 |
|
} |
286 |
|
|
287 |
|
/** |
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; |