ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/Collection8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/Collection8Test.java (file contents):
Revision 1.1 by jsr166, Sun Jun 14 20:58:14 2015 UTC vs.
Revision 1.42 by jsr166, Mon Dec 12 20:49:34 2016 UTC

# Line 5 | Line 5
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;
12 + import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.Collections;
15 + import java.util.ConcurrentModificationException;
16 + import java.util.Deque;
17 + import java.util.HashSet;
18 + import java.util.Iterator;
19 + import java.util.List;
20 + import java.util.NoSuchElementException;
21 + import java.util.Queue;
22 + import java.util.Spliterator;
23 + import java.util.concurrent.BlockingDeque;
24 + import java.util.concurrent.BlockingQueue;
25 + import java.util.concurrent.ConcurrentLinkedQueue;
26 + import java.util.concurrent.CountDownLatch;
27   import java.util.concurrent.Executors;
28   import java.util.concurrent.ExecutorService;
29   import java.util.concurrent.Future;
30 + import java.util.concurrent.Phaser;
31 + import java.util.concurrent.ThreadLocalRandom;
32   import java.util.concurrent.atomic.AtomicBoolean;
33   import java.util.concurrent.atomic.AtomicLong;
34 + import java.util.concurrent.atomic.AtomicReference;
35   import java.util.function.Consumer;
36 + import java.util.function.Predicate;
37 + import java.util.stream.Collectors;
38  
39   import junit.framework.Test;
40  
# Line 38 | Line 57 | public class Collection8Test extends JSR
57                                        impl);
58      }
59  
60 +    Object bomb() {
61 +        return new Object() {
62 +                public boolean equals(Object x) { throw new AssertionError(); }
63 +                public int hashCode() { throw new AssertionError(); }
64 +            };
65 +    }
66 +
67 +    /** Checks properties of empty collections. */
68 +    public void testEmptyMeansEmpty() throws Throwable {
69 +        Collection c = impl.emptyCollection();
70 +        emptyMeansEmpty(c);
71 +
72 +        if (c instanceof java.io.Serializable) {
73 +            try {
74 +                emptyMeansEmpty(serialClonePossiblyFailing(c));
75 +            } catch (java.io.NotSerializableException ex) {
76 +                // excusable when we have a serializable wrapper around
77 +                // a non-serializable collection, as can happen with:
78 +                // Vector.subList() => wrapped AbstractList$RandomAccessSubList
79 +                if (testImplementationDetails
80 +                    && (! c.getClass().getName().matches(
81 +                                "java.util.Collections.*")))
82 +                    throw ex;
83 +            }
84 +        }
85 +
86 +        Collection clone = cloneableClone(c);
87 +        if (clone != null)
88 +            emptyMeansEmpty(clone);
89 +    }
90 +
91 +    void emptyMeansEmpty(Collection c) throws InterruptedException {
92 +        assertTrue(c.isEmpty());
93 +        assertEquals(0, c.size());
94 +        assertEquals("[]", c.toString());
95 +        {
96 +            Object[] a = c.toArray();
97 +            assertEquals(0, a.length);
98 +            assertSame(Object[].class, a.getClass());
99 +        }
100 +        {
101 +            Object[] a = new Object[0];
102 +            assertSame(a, c.toArray(a));
103 +        }
104 +        {
105 +            Integer[] a = new Integer[0];
106 +            assertSame(a, c.toArray(a));
107 +        }
108 +        {
109 +            Integer[] a = { 1, 2, 3};
110 +            assertSame(a, c.toArray(a));
111 +            assertNull(a[0]);
112 +            assertSame(2, a[1]);
113 +            assertSame(3, a[2]);
114 +        }
115 +        assertIteratorExhausted(c.iterator());
116 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
117 +        c.forEach(alwaysThrows);
118 +        c.iterator().forEachRemaining(alwaysThrows);
119 +        c.spliterator().forEachRemaining(alwaysThrows);
120 +        assertFalse(c.spliterator().tryAdvance(alwaysThrows));
121 +        if (c.spliterator().hasCharacteristics(Spliterator.SIZED))
122 +            assertEquals(0, c.spliterator().estimateSize());
123 +        assertFalse(c.contains(bomb()));
124 +        assertFalse(c.remove(bomb()));
125 +        if (c instanceof Queue) {
126 +            Queue q = (Queue) c;
127 +            assertNull(q.peek());
128 +            assertNull(q.poll());
129 +        }
130 +        if (c instanceof Deque) {
131 +            Deque d = (Deque) c;
132 +            assertNull(d.peekFirst());
133 +            assertNull(d.peekLast());
134 +            assertNull(d.pollFirst());
135 +            assertNull(d.pollLast());
136 +            assertIteratorExhausted(d.descendingIterator());
137 +            d.descendingIterator().forEachRemaining(alwaysThrows);
138 +            assertFalse(d.removeFirstOccurrence(bomb()));
139 +            assertFalse(d.removeLastOccurrence(bomb()));
140 +        }
141 +        if (c instanceof BlockingQueue) {
142 +            BlockingQueue q = (BlockingQueue) c;
143 +            assertNull(q.poll(0L, MILLISECONDS));
144 +        }
145 +        if (c instanceof BlockingDeque) {
146 +            BlockingDeque q = (BlockingDeque) c;
147 +            assertNull(q.pollFirst(0L, MILLISECONDS));
148 +            assertNull(q.pollLast(0L, MILLISECONDS));
149 +        }
150 +    }
151 +
152 +    public void testNullPointerExceptions() throws InterruptedException {
153 +        Collection c = impl.emptyCollection();
154 +        assertThrows(
155 +            NullPointerException.class,
156 +            () -> c.addAll(null),
157 +            () -> c.containsAll(null),
158 +            () -> c.retainAll(null),
159 +            () -> c.removeAll(null),
160 +            () -> c.removeIf(null),
161 +            () -> c.forEach(null),
162 +            () -> c.iterator().forEachRemaining(null),
163 +            () -> c.spliterator().forEachRemaining(null),
164 +            () -> c.spliterator().tryAdvance(null),
165 +            () -> c.toArray(null));
166 +
167 +        if (!impl.permitsNulls()) {
168 +            assertThrows(
169 +                NullPointerException.class,
170 +                () -> c.add(null));
171 +        }
172 +        if (!impl.permitsNulls() && c instanceof Queue) {
173 +            Queue q = (Queue) c;
174 +            assertThrows(
175 +                NullPointerException.class,
176 +                () -> q.offer(null));
177 +        }
178 +        if (!impl.permitsNulls() && c instanceof Deque) {
179 +            Deque d = (Deque) c;
180 +            assertThrows(
181 +                NullPointerException.class,
182 +                () -> d.addFirst(null),
183 +                () -> d.addLast(null),
184 +                () -> d.offerFirst(null),
185 +                () -> d.offerLast(null),
186 +                () -> d.push(null),
187 +                () -> d.descendingIterator().forEachRemaining(null));
188 +        }
189 +        if (c instanceof BlockingQueue) {
190 +            BlockingQueue q = (BlockingQueue) c;
191 +            assertThrows(
192 +                NullPointerException.class,
193 +                () -> {
194 +                    try { q.offer(null, 1L, HOURS); }
195 +                    catch (InterruptedException ex) {
196 +                        throw new AssertionError(ex);
197 +                    }},
198 +                () -> {
199 +                    try { q.put(null); }
200 +                    catch (InterruptedException ex) {
201 +                        throw new AssertionError(ex);
202 +                    }});
203 +        }
204 +        if (c instanceof BlockingDeque) {
205 +            BlockingDeque q = (BlockingDeque) c;
206 +            assertThrows(
207 +                NullPointerException.class,
208 +                () -> {
209 +                    try { q.offerFirst(null, 1L, HOURS); }
210 +                    catch (InterruptedException ex) {
211 +                        throw new AssertionError(ex);
212 +                    }},
213 +                () -> {
214 +                    try { q.offerLast(null, 1L, HOURS); }
215 +                    catch (InterruptedException ex) {
216 +                        throw new AssertionError(ex);
217 +                    }},
218 +                () -> {
219 +                    try { q.putFirst(null); }
220 +                    catch (InterruptedException ex) {
221 +                        throw new AssertionError(ex);
222 +                    }},
223 +                () -> {
224 +                    try { q.putLast(null); }
225 +                    catch (InterruptedException ex) {
226 +                        throw new AssertionError(ex);
227 +                    }});
228 +        }
229 +    }
230 +
231 +    public void testNoSuchElementExceptions() {
232 +        Collection c = impl.emptyCollection();
233 +        assertThrows(
234 +            NoSuchElementException.class,
235 +            () -> c.iterator().next());
236 +
237 +        if (c instanceof Queue) {
238 +            Queue q = (Queue) c;
239 +            assertThrows(
240 +                NoSuchElementException.class,
241 +                () -> q.element(),
242 +                () -> q.remove());
243 +        }
244 +        if (c instanceof Deque) {
245 +            Deque d = (Deque) c;
246 +            assertThrows(
247 +                NoSuchElementException.class,
248 +                () -> d.getFirst(),
249 +                () -> d.getLast(),
250 +                () -> d.removeFirst(),
251 +                () -> d.removeLast(),
252 +                () -> d.pop(),
253 +                () -> d.descendingIterator().next());
254 +        }
255 +    }
256 +
257 +    public void testRemoveIf() {
258 +        Collection c = impl.emptyCollection();
259 +        boolean ordered =
260 +            c.spliterator().hasCharacteristics(Spliterator.ORDERED);
261 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
262 +        int n = rnd.nextInt(6);
263 +        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
264 +        AtomicReference threwAt = new AtomicReference(null);
265 +        List orig = rnd.nextBoolean()
266 +            ? new ArrayList(c)
267 +            : Arrays.asList(c.toArray());
268 +
269 +        // Merely creating an iterator can change ArrayBlockingQueue behavior
270 +        Iterator it = rnd.nextBoolean() ? c.iterator() : null;
271 +
272 +        ArrayList survivors = new ArrayList();
273 +        ArrayList accepts = new ArrayList();
274 +        ArrayList rejects = new ArrayList();
275 +
276 +        Predicate randomPredicate = e -> {
277 +            assertNull(threwAt.get());
278 +            switch (rnd.nextInt(3)) {
279 +            case 0: accepts.add(e); return true;
280 +            case 1: rejects.add(e); return false;
281 +            case 2: threwAt.set(e); throw new ArithmeticException();
282 +            default: throw new AssertionError();
283 +            }
284 +        };
285 +        try {
286 +            try {
287 +                boolean modified = c.removeIf(randomPredicate);
288 +                assertNull(threwAt.get());
289 +                assertEquals(modified, accepts.size() > 0);
290 +                assertEquals(modified, rejects.size() != n);
291 +                assertEquals(accepts.size() + rejects.size(), n);
292 +                if (ordered) {
293 +                    assertEquals(rejects,
294 +                                 Arrays.asList(c.toArray()));
295 +                } else {
296 +                    assertEquals(new HashSet(rejects),
297 +                                 new HashSet(Arrays.asList(c.toArray())));
298 +                }
299 +            } catch (ArithmeticException ok) {
300 +                assertNotNull(threwAt.get());
301 +                assertTrue(c.contains(threwAt.get()));
302 +            }
303 +            if (it != null && impl.isConcurrent())
304 +                // check for weakly consistent iterator
305 +                while (it.hasNext()) assertTrue(orig.contains(it.next()));
306 +            switch (rnd.nextInt(4)) {
307 +            case 0: survivors.addAll(c); break;
308 +            case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
309 +            case 2: c.forEach(survivors::add); break;
310 +            case 3: for (Object e : c) survivors.add(e); break;
311 +            }
312 +            assertTrue(orig.containsAll(accepts));
313 +            assertTrue(orig.containsAll(rejects));
314 +            assertTrue(orig.containsAll(survivors));
315 +            assertTrue(orig.containsAll(c));
316 +            assertTrue(c.containsAll(rejects));
317 +            assertTrue(c.containsAll(survivors));
318 +            assertTrue(survivors.containsAll(rejects));
319 +            if (threwAt.get() == null) {
320 +                assertEquals(n - accepts.size(), c.size());
321 +                for (Object x : accepts) assertFalse(c.contains(x));
322 +            } else {
323 +                // Two acceptable behaviors: entire removeIf call is one
324 +                // transaction, or each element processed is one transaction.
325 +                assertTrue(n == c.size() || n == c.size() + accepts.size());
326 +                int k = 0;
327 +                for (Object x : accepts) if (c.contains(x)) k++;
328 +                assertTrue(k == accepts.size() || k == 0);
329 +            }
330 +        } catch (Throwable ex) {
331 +            System.err.println(impl.klazz());
332 +            // c is at risk of corruption if we got here, so be lenient
333 +            try { System.err.printf("c=%s%n", c); }
334 +            catch (Throwable t) { t.printStackTrace(); }
335 +            System.err.printf("n=%d%n", n);
336 +            System.err.printf("orig=%s%n", orig);
337 +            System.err.printf("accepts=%s%n", accepts);
338 +            System.err.printf("rejects=%s%n", rejects);
339 +            System.err.printf("survivors=%s%n", survivors);
340 +            System.err.printf("threwAt=%s%n", threwAt.get());
341 +            throw ex;
342 +        }
343 +    }
344 +
345 +    /**
346 +     * All elements removed in the middle of CONCURRENT traversal.
347 +     */
348 +    public void testElementRemovalDuringTraversal() {
349 +        Collection c = impl.emptyCollection();
350 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
351 +        int n = rnd.nextInt(6);
352 +        ArrayList copy = new ArrayList();
353 +        for (int i = 0; i < n; i++) {
354 +            Object x = impl.makeElement(i);
355 +            copy.add(x);
356 +            c.add(x);
357 +        }
358 +        ArrayList iterated = new ArrayList();
359 +        ArrayList spliterated = new ArrayList();
360 +        Spliterator s = c.spliterator();
361 +        Iterator it = c.iterator();
362 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
363 +            assertTrue(s.tryAdvance(spliterated::add));
364 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
365 +            iterated.add(it.next());
366 +        }
367 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
368 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
369 +            c.clear();          // TODO: many more removal methods
370 +            if (testImplementationDetails
371 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) {
372 +                if (rnd.nextBoolean())
373 +                    assertFalse(s.tryAdvance(alwaysThrows));
374 +                else
375 +                    s.forEachRemaining(alwaysThrows);
376 +            }
377 +            if (it.hasNext()) iterated.add(it.next());
378 +            if (rnd.nextBoolean()) assertIteratorExhausted(it);
379 +        }
380 +        assertTrue(copy.containsAll(iterated));
381 +        assertTrue(copy.containsAll(spliterated));
382 +    }
383 +
384 +    /**
385 +     * Some elements randomly disappear in the middle of traversal.
386 +     */
387 +    public void testRandomElementRemovalDuringTraversal() {
388 +        Collection c = impl.emptyCollection();
389 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
390 +        int n = rnd.nextInt(6);
391 +        ArrayList copy = new ArrayList();
392 +        for (int i = 0; i < n; i++) {
393 +            Object x = impl.makeElement(i);
394 +            copy.add(x);
395 +            c.add(x);
396 +        }
397 +        ArrayList iterated = new ArrayList();
398 +        ArrayList spliterated = new ArrayList();
399 +        ArrayList removed = new ArrayList();
400 +        Spliterator s = c.spliterator();
401 +        Iterator it = c.iterator();
402 +        if (! (s.hasCharacteristics(Spliterator.CONCURRENT) ||
403 +               s.hasCharacteristics(Spliterator.IMMUTABLE)))
404 +            return;
405 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
406 +            assertTrue(s.tryAdvance(e -> {}));
407 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
408 +            it.next();
409 +        }
410 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
411 +        // TODO: many more removal methods
412 +        if (rnd.nextBoolean()) {
413 +            for (Iterator z = c.iterator(); z.hasNext(); ) {
414 +                Object e = z.next();
415 +                if (rnd.nextBoolean()) {
416 +                    try {
417 +                        z.remove();
418 +                    } catch (UnsupportedOperationException ok) { return; }
419 +                    removed.add(e);
420 +                }
421 +            }
422 +        } else {
423 +            Predicate randomlyRemove = e -> {
424 +                if (rnd.nextBoolean()) { removed.add(e); return true; }
425 +                else return false;
426 +            };
427 +            c.removeIf(randomlyRemove);
428 +        }
429 +        s.forEachRemaining(spliterated::add);
430 +        while (it.hasNext())
431 +            iterated.add(it.next());
432 +        assertTrue(copy.containsAll(iterated));
433 +        assertTrue(copy.containsAll(spliterated));
434 +        assertTrue(copy.containsAll(removed));
435 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
436 +            ArrayList iteratedAndRemoved = new ArrayList(iterated);
437 +            ArrayList spliteratedAndRemoved = new ArrayList(spliterated);
438 +            iteratedAndRemoved.retainAll(removed);
439 +            spliteratedAndRemoved.retainAll(removed);
440 +            assertTrue(iteratedAndRemoved.size() <= 1);
441 +            assertTrue(spliteratedAndRemoved.size() <= 1);
442 +            if (testImplementationDetails
443 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue))
444 +                assertTrue(spliteratedAndRemoved.isEmpty());
445 +        }
446 +    }
447 +
448 +    /**
449 +     * Various ways of traversing a collection yield same elements
450 +     */
451 +    public void testTraversalEquivalence() {
452 +        Collection c = impl.emptyCollection();
453 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
454 +        int n = rnd.nextInt(6);
455 +        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
456 +        ArrayList iterated = new ArrayList();
457 +        ArrayList iteratedForEachRemaining = new ArrayList();
458 +        ArrayList tryAdvanced = new ArrayList();
459 +        ArrayList spliterated = new ArrayList();
460 +        ArrayList splitonced = new ArrayList();
461 +        ArrayList forEached = new ArrayList();
462 +        ArrayList streamForEached = new ArrayList();
463 +        ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue();
464 +        ArrayList removeIfed = new ArrayList();
465 +        for (Object x : c) iterated.add(x);
466 +        c.iterator().forEachRemaining(iteratedForEachRemaining::add);
467 +        for (Spliterator s = c.spliterator();
468 +             s.tryAdvance(tryAdvanced::add); ) {}
469 +        c.spliterator().forEachRemaining(spliterated::add);
470 +        {                       // trySplit returns "strict prefix"
471 +            Spliterator s1 = c.spliterator(), s2 = s1.trySplit();
472 +            if (s2 != null) s2.forEachRemaining(splitonced::add);
473 +            s1.forEachRemaining(splitonced::add);
474 +        }
475 +        c.forEach(forEached::add);
476 +        c.stream().forEach(streamForEached::add);
477 +        c.parallelStream().forEach(parallelStreamForEached::add);
478 +        c.removeIf(e -> { removeIfed.add(e); return false; });
479 +        boolean ordered =
480 +            c.spliterator().hasCharacteristics(Spliterator.ORDERED);
481 +        if (c instanceof List || c instanceof Deque)
482 +            assertTrue(ordered);
483 +        HashSet cset = new HashSet(c);
484 +        assertEquals(cset, new HashSet(parallelStreamForEached));
485 +        if (ordered) {
486 +            assertEquals(iterated, iteratedForEachRemaining);
487 +            assertEquals(iterated, tryAdvanced);
488 +            assertEquals(iterated, spliterated);
489 +            assertEquals(iterated, splitonced);
490 +            assertEquals(iterated, forEached);
491 +            assertEquals(iterated, streamForEached);
492 +            assertEquals(iterated, removeIfed);
493 +        } else {
494 +            assertEquals(cset, new HashSet(iterated));
495 +            assertEquals(cset, new HashSet(iteratedForEachRemaining));
496 +            assertEquals(cset, new HashSet(tryAdvanced));
497 +            assertEquals(cset, new HashSet(spliterated));
498 +            assertEquals(cset, new HashSet(splitonced));
499 +            assertEquals(cset, new HashSet(forEached));
500 +            assertEquals(cset, new HashSet(streamForEached));
501 +            assertEquals(cset, new HashSet(removeIfed));
502 +        }
503 +        if (c instanceof Deque) {
504 +            Deque d = (Deque) c;
505 +            ArrayList descending = new ArrayList();
506 +            ArrayList descendingForEachRemaining = new ArrayList();
507 +            for (Iterator it = d.descendingIterator(); it.hasNext(); )
508 +                descending.add(it.next());
509 +            d.descendingIterator().forEachRemaining(
510 +                e -> descendingForEachRemaining.add(e));
511 +            Collections.reverse(descending);
512 +            Collections.reverse(descendingForEachRemaining);
513 +            assertEquals(iterated, descending);
514 +            assertEquals(iterated, descendingForEachRemaining);
515 +        }
516 +    }
517 +
518 +    /**
519 +     * Iterator.forEachRemaining has same behavior as Iterator's
520 +     * default implementation.
521 +     */
522 +    public void testForEachRemainingConsistentWithDefaultImplementation() {
523 +        Collection c = impl.emptyCollection();
524 +        if (!testImplementationDetails
525 +            || c.getClass() == java.util.LinkedList.class)
526 +            return;
527 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
528 +        int n = 1 + rnd.nextInt(3);
529 +        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
530 +        ArrayList iterated = new ArrayList();
531 +        ArrayList iteratedForEachRemaining = new ArrayList();
532 +        Iterator it1 = c.iterator();
533 +        Iterator it2 = c.iterator();
534 +        assertTrue(it1.hasNext());
535 +        assertTrue(it2.hasNext());
536 +        c.clear();
537 +        Object r1, r2;
538 +        try {
539 +            while (it1.hasNext()) iterated.add(it1.next());
540 +            r1 = iterated;
541 +        } catch (ConcurrentModificationException ex) {
542 +            r1 = ConcurrentModificationException.class;
543 +            assertFalse(impl.isConcurrent());
544 +        } catch (UnsupportedOperationException ex) {
545 +            r1 = UnsupportedOperationException.class;
546 +        }
547 +        try {
548 +            it2.forEachRemaining(iteratedForEachRemaining::add);
549 +            r2 = iteratedForEachRemaining;
550 +        } catch (ConcurrentModificationException ex) {
551 +            r2 = ConcurrentModificationException.class;
552 +        } catch (UnsupportedOperationException ex) {
553 +            r2 = UnsupportedOperationException.class;
554 +        }
555 +        if (!r1.equals(r2)) System.err.println(impl.klazz());
556 +        assertEquals(r1, r2);
557 +    }
558 +
559 +    /**
560 +     * Calling Iterator#remove() after Iterator#forEachRemaining
561 +     * should (maybe) remove last element
562 +     */
563 +    public void testRemoveAfterForEachRemaining() {
564 +        Collection c = impl.emptyCollection();
565 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
566 +        testCollection: {
567 +            int n = 3 + rnd.nextInt(2);
568 +            for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
569 +            Iterator it = c.iterator();
570 +            assertTrue(it.hasNext());
571 +            assertEquals(impl.makeElement(0), it.next());
572 +            assertTrue(it.hasNext());
573 +            assertEquals(impl.makeElement(1), it.next());
574 +            it.forEachRemaining(e -> assertTrue(c.contains(e)));
575 +            if (testImplementationDetails) {
576 +                if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
577 +                    assertIteratorExhausted(it);
578 +                } else {
579 +                    try { it.remove(); }
580 +                    catch (UnsupportedOperationException ok) {
581 +                        break testCollection;
582 +                    }
583 +                    assertEquals(n - 1, c.size());
584 +                    for (int i = 0; i < n - 1; i++)
585 +                        assertTrue(c.contains(impl.makeElement(i)));
586 +                    assertFalse(c.contains(impl.makeElement(n - 1)));
587 +                }
588 +            }
589 +        }
590 +        if (c instanceof Deque) {
591 +            Deque d = (Deque) impl.emptyCollection();
592 +            int n = 3 + rnd.nextInt(2);
593 +            for (int i = 0; i < n; i++) d.add(impl.makeElement(i));
594 +            Iterator it = d.descendingIterator();
595 +            assertTrue(it.hasNext());
596 +            assertEquals(impl.makeElement(n - 1), it.next());
597 +            assertTrue(it.hasNext());
598 +            assertEquals(impl.makeElement(n - 2), it.next());
599 +            it.forEachRemaining(e -> assertTrue(c.contains(e)));
600 +            if (testImplementationDetails) {
601 +                it.remove();
602 +                assertEquals(n - 1, d.size());
603 +                for (int i = 1; i < n; i++)
604 +                    assertTrue(d.contains(impl.makeElement(i)));
605 +                assertFalse(d.contains(impl.makeElement(0)));
606 +            }
607 +        }
608 +    }
609 +
610      /**
611       * stream().forEach returns elements in the collection
612       */
613 <    public void testForEach() throws Throwable {
613 >    public void testStreamForEach() throws Throwable {
614          final Collection c = impl.emptyCollection();
615          final AtomicLong count = new AtomicLong(0L);
616          final Object x = impl.makeElement(1);
617          final Object y = impl.makeElement(2);
618          final ArrayList found = new ArrayList();
619 <        Consumer<Object> spy = (o) -> { found.add(o); };
619 >        Consumer<Object> spy = o -> found.add(o);
620          c.stream().forEach(spy);
621          assertTrue(found.isEmpty());
622  
# Line 68 | Line 637 | public class Collection8Test extends JSR
637          assertTrue(found.isEmpty());
638      }
639  
640 <    public void testForEachConcurrentStressTest() throws Throwable {
640 >    public void testStreamForEachConcurrentStressTest() throws Throwable {
641          if (!impl.isConcurrent()) return;
642          final Collection c = impl.emptyCollection();
643 <        final long testDurationMillis = SHORT_DELAY_MS;
643 >        final long testDurationMillis = timeoutMillis();
644          final AtomicBoolean done = new AtomicBoolean(false);
645          final Object elt = impl.makeElement(1);
646 <        ExecutorService pool = Executors.newCachedThreadPool();
647 <        Runnable checkElt = () -> {
648 <            while (!done.get())
649 <                c.stream().forEach((x) -> { assertSame(x, elt); }); };
650 <        Runnable addRemove = () -> {
651 <            while (!done.get()) {
652 <                assertTrue(c.add(elt));
653 <                assertTrue(c.remove(elt));
654 <            }};
655 <        Future<?> f1 = pool.submit(checkElt);
656 <        Future<?> f2 = pool.submit(addRemove);
657 <        Thread.sleep(testDurationMillis);
658 <        done.set(true);
659 <        pool.shutdown();
660 <        assertTrue(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
661 <        assertNull(f1.get(LONG_DELAY_MS, MILLISECONDS));
662 <        assertNull(f2.get(LONG_DELAY_MS, MILLISECONDS));
646 >        final Future<?> f1, f2;
647 >        final ExecutorService pool = Executors.newCachedThreadPool();
648 >        try (PoolCleaner cleaner = cleaner(pool, done)) {
649 >            final CountDownLatch threadsStarted = new CountDownLatch(2);
650 >            Runnable checkElt = () -> {
651 >                threadsStarted.countDown();
652 >                while (!done.get())
653 >                    c.stream().forEach(x -> assertSame(x, elt)); };
654 >            Runnable addRemove = () -> {
655 >                threadsStarted.countDown();
656 >                while (!done.get()) {
657 >                    assertTrue(c.add(elt));
658 >                    assertTrue(c.remove(elt));
659 >                }};
660 >            f1 = pool.submit(checkElt);
661 >            f2 = pool.submit(addRemove);
662 >            Thread.sleep(testDurationMillis);
663 >        }
664 >        assertNull(f1.get(0L, MILLISECONDS));
665 >        assertNull(f2.get(0L, MILLISECONDS));
666 >    }
667 >
668 >    /**
669 >     * collection.forEach returns elements in the collection
670 >     */
671 >    public void testForEach() throws Throwable {
672 >        final Collection c = impl.emptyCollection();
673 >        final AtomicLong count = new AtomicLong(0L);
674 >        final Object x = impl.makeElement(1);
675 >        final Object y = impl.makeElement(2);
676 >        final ArrayList found = new ArrayList();
677 >        Consumer<Object> spy = o -> found.add(o);
678 >        c.forEach(spy);
679 >        assertTrue(found.isEmpty());
680 >
681 >        assertTrue(c.add(x));
682 >        c.forEach(spy);
683 >        assertEquals(Collections.singletonList(x), found);
684 >        found.clear();
685 >
686 >        assertTrue(c.add(y));
687 >        c.forEach(spy);
688 >        assertEquals(2, found.size());
689 >        assertTrue(found.contains(x));
690 >        assertTrue(found.contains(y));
691 >        found.clear();
692 >
693 >        c.clear();
694 >        c.forEach(spy);
695 >        assertTrue(found.isEmpty());
696 >    }
697 >
698 >    /** TODO: promote to a common utility */
699 >    static <T> T chooseOne(T ... ts) {
700 >        return ts[ThreadLocalRandom.current().nextInt(ts.length)];
701 >    }
702 >
703 >    /** TODO: more random adders and removers */
704 >    static <E> Runnable adderRemover(Collection<E> c, E e) {
705 >        return chooseOne(
706 >            () -> {
707 >                assertTrue(c.add(e));
708 >                assertTrue(c.contains(e));
709 >                assertTrue(c.remove(e));
710 >                assertFalse(c.contains(e));
711 >            },
712 >            () -> {
713 >                assertTrue(c.add(e));
714 >                assertTrue(c.contains(e));
715 >                assertTrue(c.removeIf(x -> x == e));
716 >                assertFalse(c.contains(e));
717 >            },
718 >            () -> {
719 >                assertTrue(c.add(e));
720 >                assertTrue(c.contains(e));
721 >                for (Iterator it = c.iterator();; )
722 >                    if (it.next() == e) {
723 >                        try { it.remove(); }
724 >                        catch (UnsupportedOperationException ok) {
725 >                            c.remove(e);
726 >                        }
727 >                        break;
728 >                    }
729 >                assertFalse(c.contains(e));
730 >            });
731 >    }
732 >
733 >    /**
734 >     * Motley crew of threads concurrently randomly hammer the collection.
735 >     */
736 >    public void testDetectRaces() throws Throwable {
737 >        if (!impl.isConcurrent()) return;
738 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
739 >        final Collection c = impl.emptyCollection();
740 >        final long testDurationMillis
741 >            = expensiveTests ? LONG_DELAY_MS : timeoutMillis();
742 >        final AtomicBoolean done = new AtomicBoolean(false);
743 >        final Object one = impl.makeElement(1);
744 >        final Object two = impl.makeElement(2);
745 >        final Consumer checkSanity = x -> assertTrue(x == one || x == two);
746 >        final Consumer<Object[]> checkArraySanity = array -> {
747 >            // assertTrue(array.length <= 2); // duplicates are permitted
748 >            for (Object x : array) assertTrue(x == one || x == two);
749 >        };
750 >        final Object[] emptyArray =
751 >            (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
752 >        final List<Future<?>> futures;
753 >        final Phaser threadsStarted = new Phaser(1); // register this thread
754 >        final Runnable[] frobbers = {
755 >            () -> c.forEach(checkSanity),
756 >            () -> c.stream().forEach(checkSanity),
757 >            () -> c.parallelStream().forEach(checkSanity),
758 >            () -> c.spliterator().trySplit(),
759 >            () -> {
760 >                Spliterator s = c.spliterator();
761 >                s.tryAdvance(checkSanity);
762 >                s.trySplit();
763 >            },
764 >            () -> {
765 >                Spliterator s = c.spliterator();
766 >                do {} while (s.tryAdvance(checkSanity));
767 >            },
768 >            () -> { for (Object x : c) checkSanity.accept(x); },
769 >            () -> checkArraySanity.accept(c.toArray()),
770 >            () -> checkArraySanity.accept(c.toArray(emptyArray)),
771 >            () -> {
772 >                Object[] a = new Object[5];
773 >                Object three = impl.makeElement(3);
774 >                Arrays.fill(a, 0, a.length, three);
775 >                Object[] x = c.toArray(a);
776 >                if (x == a)
777 >                    for (int i = 0; i < a.length && a[i] != null; i++)
778 >                        checkSanity.accept(a[i]);
779 >                    // A careful reading of the spec does not support:
780 >                    // for (i++; i < a.length; i++) assertSame(three, a[i]);
781 >                else
782 >                    checkArraySanity.accept(x);
783 >                },
784 >            adderRemover(c, one),
785 >            adderRemover(c, two),
786 >        };
787 >        final List<Runnable> tasks =
788 >            Arrays.stream(frobbers)
789 >            .filter(task -> rnd.nextBoolean()) // random subset
790 >            .map(task -> (Runnable) () -> {
791 >                     threadsStarted.arriveAndAwaitAdvance();
792 >                     while (!done.get())
793 >                         task.run();
794 >                 })
795 >            .collect(Collectors.toList());
796 >        final ExecutorService pool = Executors.newCachedThreadPool();
797 >        try (PoolCleaner cleaner = cleaner(pool, done)) {
798 >            threadsStarted.bulkRegister(tasks.size());
799 >            futures = tasks.stream()
800 >                .map(pool::submit)
801 >                .collect(Collectors.toList());
802 >            threadsStarted.arriveAndDeregister();
803 >            Thread.sleep(testDurationMillis);
804 >        }
805 >        for (Future future : futures)
806 >            assertNull(future.get(0L, MILLISECONDS));
807 >    }
808 >
809 >    /**
810 >     * Spliterators are either IMMUTABLE or truly late-binding or, if
811 >     * concurrent, use the same "late-binding style" of returning
812 >     * elements added between creation and first use.
813 >     */
814 >    public void testLateBindingStyle() {
815 >        if (!testImplementationDetails) return;
816 >        if (impl.klazz() == ArrayList.class) return; // for jdk8
817 >        // Immutable (snapshot) spliterators are exempt
818 >        if (impl.emptyCollection().spliterator()
819 >            .hasCharacteristics(Spliterator.IMMUTABLE))
820 >            return;
821 >        final Object one = impl.makeElement(1);
822 >        {
823 >            final Collection c = impl.emptyCollection();
824 >            final Spliterator split = c.spliterator();
825 >            c.add(one);
826 >            assertTrue(split.tryAdvance(e -> { assertSame(e, one); }));
827 >            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
828 >            assertTrue(c.contains(one));
829 >        }
830 >        {
831 >            final AtomicLong count = new AtomicLong(0);
832 >            final Collection c = impl.emptyCollection();
833 >            final Spliterator split = c.spliterator();
834 >            c.add(one);
835 >            split.forEachRemaining(
836 >                e -> { assertSame(e, one); count.getAndIncrement(); });
837 >            assertEquals(1L, count.get());
838 >            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
839 >            assertTrue(c.contains(one));
840 >        }
841 >    }
842 >
843 >    /**
844 >     * Spliterator.getComparator throws IllegalStateException iff the
845 >     * spliterator does not report SORTED.
846 >     */
847 >    public void testGetComparator_IllegalStateException() {
848 >        Collection c = impl.emptyCollection();
849 >        Spliterator s = c.spliterator();
850 >        boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED);
851 >        try {
852 >            s.getComparator();
853 >            assertTrue(reportsSorted);
854 >        } catch (IllegalStateException ex) {
855 >            assertFalse(reportsSorted);
856 >        }
857      }
858  
859 <    // public void testCollection8DebugFail() { fail(); }
859 > //     public void testCollection8DebugFail() {
860 > //         fail(impl.klazz().getSimpleName());
861 > //     }
862   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines