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.38 by jsr166, Wed Dec 7 22:03:41 2016 UTC vs.
Revision 1.61 by dl, Tue Jan 26 13:33:05 2021 UTC

# Line 8 | Line 8
8   import static java.util.concurrent.TimeUnit.HOURS;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 + import java.util.ArrayDeque;
12   import java.util.ArrayList;
13   import java.util.Arrays;
14   import java.util.Collection;
15   import java.util.Collections;
16 + import java.util.ConcurrentModificationException;
17   import java.util.Deque;
18   import java.util.HashSet;
19   import java.util.Iterator;
20   import java.util.List;
21   import java.util.NoSuchElementException;
22   import java.util.Queue;
23 + import java.util.Set;
24   import java.util.Spliterator;
25   import java.util.concurrent.BlockingDeque;
26   import java.util.concurrent.BlockingQueue;
# Line 58 | Line 61 | public class Collection8Test extends JSR
61  
62      Object bomb() {
63          return new Object() {
64 <                public boolean equals(Object x) { throw new AssertionError(); }
65 <                public int hashCode() { throw new AssertionError(); }
66 <            };
64 >            @Override public boolean equals(Object x) { throw new AssertionError(); }
65 >            @Override public int hashCode() { throw new AssertionError(); }
66 >            @Override public String toString() { throw new AssertionError(); }
67 >        };
68      }
69  
70      /** Checks properties of empty collections. */
# Line 89 | Line 93 | public class Collection8Test extends JSR
93  
94      void emptyMeansEmpty(Collection c) throws InterruptedException {
95          assertTrue(c.isEmpty());
96 <        assertEquals(0, c.size());
97 <        assertEquals("[]", c.toString());
96 >        mustEqual(0, c.size());
97 >        mustEqual("[]", c.toString());
98 >        if (c instanceof List<?>) {
99 >            List x = (List) c;
100 >            mustEqual(1, x.hashCode());
101 >            mustEqual(x, Collections.emptyList());
102 >            mustEqual(Collections.emptyList(), x);
103 >            mustEqual(-1, x.indexOf(impl.makeElement(86)));
104 >            mustEqual(-1, x.lastIndexOf(impl.makeElement(99)));
105 >            assertThrows(
106 >                IndexOutOfBoundsException.class,
107 >                () -> x.get(0),
108 >                () -> x.set(0, impl.makeElement(42)));
109 >        }
110 >        else if (c instanceof Set<?>) {
111 >            mustEqual(0, c.hashCode());
112 >            mustEqual(c, Collections.emptySet());
113 >            mustEqual(Collections.emptySet(), c);
114 >        }
115          {
116              Object[] a = c.toArray();
117 <            assertEquals(0, a.length);
117 >            mustEqual(0, a.length);
118              assertSame(Object[].class, a.getClass());
119          }
120          {
# Line 101 | Line 122 | public class Collection8Test extends JSR
122              assertSame(a, c.toArray(a));
123          }
124          {
125 <            Integer[] a = new Integer[0];
125 >            Item[] a = new Item[0];
126              assertSame(a, c.toArray(a));
127          }
128          {
129 <            Integer[] a = { 1, 2, 3};
129 >            Item[] a = { one, two, three};
130              assertSame(a, c.toArray(a));
131              assertNull(a[0]);
132 <            assertSame(2, a[1]);
133 <            assertSame(3, a[2]);
132 >            mustEqual(2, a[1]);
133 >            mustEqual(3, a[2]);
134          }
135          assertIteratorExhausted(c.iterator());
136          Consumer alwaysThrows = e -> { throw new AssertionError(); };
# Line 118 | Line 139 | public class Collection8Test extends JSR
139          c.spliterator().forEachRemaining(alwaysThrows);
140          assertFalse(c.spliterator().tryAdvance(alwaysThrows));
141          if (c.spliterator().hasCharacteristics(Spliterator.SIZED))
142 <            assertEquals(0, c.spliterator().estimateSize());
142 >            mustEqual(0, c.spliterator().estimateSize());
143          assertFalse(c.contains(bomb()));
144          assertFalse(c.remove(bomb()));
145          if (c instanceof Queue) {
# Line 139 | Line 160 | public class Collection8Test extends JSR
160          }
161          if (c instanceof BlockingQueue) {
162              BlockingQueue q = (BlockingQueue) c;
163 <            assertNull(q.poll(0L, MILLISECONDS));
163 >            assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit()));
164          }
165          if (c instanceof BlockingDeque) {
166              BlockingDeque q = (BlockingDeque) c;
167 <            assertNull(q.pollFirst(0L, MILLISECONDS));
168 <            assertNull(q.pollLast(0L, MILLISECONDS));
167 >            assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit()));
168 >            assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit()));
169          }
170      }
171  
172      public void testNullPointerExceptions() throws InterruptedException {
173          Collection c = impl.emptyCollection();
174 +        Collection nullCollection = null;
175          assertThrows(
176              NullPointerException.class,
177 <            () -> c.addAll(null),
178 <            () -> c.containsAll(null),
179 <            () -> c.retainAll(null),
180 <            () -> c.removeAll(null),
177 >            () -> c.addAll(nullCollection),
178 >            () -> c.containsAll(nullCollection),
179 >            () -> c.retainAll(nullCollection),
180 >            () -> c.removeAll(nullCollection),
181              () -> c.removeIf(null),
182              () -> c.forEach(null),
183              () -> c.iterator().forEachRemaining(null),
184              () -> c.spliterator().forEachRemaining(null),
185              () -> c.spliterator().tryAdvance(null),
186 <            () -> c.toArray(null));
186 >            () -> c.toArray((Object[])null));
187  
188          if (!impl.permitsNulls()) {
189              assertThrows(
# Line 189 | Line 211 | public class Collection8Test extends JSR
211              BlockingQueue q = (BlockingQueue) c;
212              assertThrows(
213                  NullPointerException.class,
214 <                () -> {
215 <                    try { q.offer(null, 1L, HOURS); }
194 <                    catch (InterruptedException ex) {
195 <                        throw new AssertionError(ex);
196 <                    }},
197 <                () -> {
198 <                    try { q.put(null); }
199 <                    catch (InterruptedException ex) {
200 <                        throw new AssertionError(ex);
201 <                    }});
214 >                () -> q.offer(null, 1L, HOURS),
215 >                () -> q.put(null));
216          }
217          if (c instanceof BlockingDeque) {
218              BlockingDeque q = (BlockingDeque) c;
219              assertThrows(
220                  NullPointerException.class,
221 <                () -> {
222 <                    try { q.offerFirst(null, 1L, HOURS); }
223 <                    catch (InterruptedException ex) {
224 <                        throw new AssertionError(ex);
211 <                    }},
212 <                () -> {
213 <                    try { q.offerLast(null, 1L, HOURS); }
214 <                    catch (InterruptedException ex) {
215 <                        throw new AssertionError(ex);
216 <                    }},
217 <                () -> {
218 <                    try { q.putFirst(null); }
219 <                    catch (InterruptedException ex) {
220 <                        throw new AssertionError(ex);
221 <                    }},
222 <                () -> {
223 <                    try { q.putLast(null); }
224 <                    catch (InterruptedException ex) {
225 <                        throw new AssertionError(ex);
226 <                    }});
221 >                () -> q.offerFirst(null, 1L, HOURS),
222 >                () -> q.offerLast(null, 1L, HOURS),
223 >                () -> q.putFirst(null),
224 >                () -> q.putLast(null));
225          }
226      }
227  
# Line 251 | Line 249 | public class Collection8Test extends JSR
249                  () -> d.pop(),
250                  () -> d.descendingIterator().next());
251          }
252 +        if (c instanceof List) {
253 +            List x = (List) c;
254 +            assertThrows(
255 +                NoSuchElementException.class,
256 +                () -> x.iterator().next(),
257 +                () -> x.listIterator().next(),
258 +                () -> x.listIterator(0).next(),
259 +                () -> x.listIterator().previous(),
260 +                () -> x.listIterator(0).previous());
261 +        }
262      }
263  
264      public void testRemoveIf() {
# Line 285 | Line 293 | public class Collection8Test extends JSR
293              try {
294                  boolean modified = c.removeIf(randomPredicate);
295                  assertNull(threwAt.get());
296 <                assertEquals(modified, accepts.size() > 0);
297 <                assertEquals(modified, rejects.size() != n);
298 <                assertEquals(accepts.size() + rejects.size(), n);
296 >                mustEqual(modified, accepts.size() > 0);
297 >                mustEqual(modified, rejects.size() != n);
298 >                mustEqual(accepts.size() + rejects.size(), n);
299                  if (ordered) {
300 <                    assertEquals(rejects,
300 >                    mustEqual(rejects,
301                                   Arrays.asList(c.toArray()));
302                  } else {
303 <                    assertEquals(new HashSet(rejects),
303 >                    mustEqual(new HashSet(rejects),
304                                   new HashSet(Arrays.asList(c.toArray())));
305                  }
306              } catch (ArithmeticException ok) {
# Line 316 | Line 324 | public class Collection8Test extends JSR
324              assertTrue(c.containsAll(survivors));
325              assertTrue(survivors.containsAll(rejects));
326              if (threwAt.get() == null) {
327 <                assertEquals(n - accepts.size(), c.size());
327 >                mustEqual(n - accepts.size(), c.size());
328                  for (Object x : accepts) assertFalse(c.contains(x));
329              } else {
330                  // Two acceptable behaviors: entire removeIf call is one
# Line 342 | Line 350 | public class Collection8Test extends JSR
350      }
351  
352      /**
353 +     * All elements removed in the middle of CONCURRENT traversal.
354 +     */
355 +    public void testElementRemovalDuringTraversal() {
356 +        Collection c = impl.emptyCollection();
357 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
358 +        int n = rnd.nextInt(6);
359 +        ArrayList copy = new ArrayList();
360 +        for (int i = 0; i < n; i++) {
361 +            Object x = impl.makeElement(i);
362 +            copy.add(x);
363 +            c.add(x);
364 +        }
365 +        ArrayList iterated = new ArrayList();
366 +        ArrayList spliterated = new ArrayList();
367 +        Spliterator s = c.spliterator();
368 +        Iterator it = c.iterator();
369 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
370 +            assertTrue(s.tryAdvance(spliterated::add));
371 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
372 +            iterated.add(it.next());
373 +        }
374 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
375 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
376 +            c.clear();          // TODO: many more removal methods
377 +            if (testImplementationDetails
378 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) {
379 +                if (rnd.nextBoolean())
380 +                    assertFalse(s.tryAdvance(alwaysThrows));
381 +                else
382 +                    s.forEachRemaining(alwaysThrows);
383 +            }
384 +            if (it.hasNext()) iterated.add(it.next());
385 +            if (rnd.nextBoolean()) assertIteratorExhausted(it);
386 +        }
387 +        assertTrue(copy.containsAll(iterated));
388 +        assertTrue(copy.containsAll(spliterated));
389 +    }
390 +
391 +    /**
392 +     * Some elements randomly disappear in the middle of traversal.
393 +     */
394 +    public void testRandomElementRemovalDuringTraversal() {
395 +        Collection c = impl.emptyCollection();
396 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
397 +        int n = rnd.nextInt(6);
398 +        ArrayList copy = new ArrayList();
399 +        for (int i = 0; i < n; i++) {
400 +            Object x = impl.makeElement(i);
401 +            copy.add(x);
402 +            c.add(x);
403 +        }
404 +        ArrayList iterated = new ArrayList();
405 +        ArrayList spliterated = new ArrayList();
406 +        ArrayList removed = new ArrayList();
407 +        Spliterator s = c.spliterator();
408 +        Iterator it = c.iterator();
409 +        if (! (s.hasCharacteristics(Spliterator.CONCURRENT) ||
410 +               s.hasCharacteristics(Spliterator.IMMUTABLE)))
411 +            return;
412 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
413 +            assertTrue(s.tryAdvance(e -> {}));
414 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
415 +            it.next();
416 +        }
417 +        // TODO: many more removal methods
418 +        if (rnd.nextBoolean()) {
419 +            for (Iterator z = c.iterator(); z.hasNext(); ) {
420 +                Object e = z.next();
421 +                if (rnd.nextBoolean()) {
422 +                    try {
423 +                        z.remove();
424 +                    } catch (UnsupportedOperationException ok) { return; }
425 +                    removed.add(e);
426 +                }
427 +            }
428 +        } else {
429 +            Predicate randomlyRemove = e -> {
430 +                if (rnd.nextBoolean()) { removed.add(e); return true; }
431 +                else return false;
432 +            };
433 +            c.removeIf(randomlyRemove);
434 +        }
435 +        s.forEachRemaining(spliterated::add);
436 +        while (it.hasNext())
437 +            iterated.add(it.next());
438 +        assertTrue(copy.containsAll(iterated));
439 +        assertTrue(copy.containsAll(spliterated));
440 +        assertTrue(copy.containsAll(removed));
441 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
442 +            ArrayList iteratedAndRemoved = new ArrayList(iterated);
443 +            ArrayList spliteratedAndRemoved = new ArrayList(spliterated);
444 +            iteratedAndRemoved.retainAll(removed);
445 +            spliteratedAndRemoved.retainAll(removed);
446 +            assertTrue(iteratedAndRemoved.size() <= 1);
447 +            assertTrue(spliteratedAndRemoved.size() <= 1);
448 +            if (testImplementationDetails
449 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue))
450 +                assertTrue(spliteratedAndRemoved.isEmpty());
451 +        }
452 +    }
453 +
454 +    /**
455       * Various ways of traversing a collection yield same elements
456       */
457 <    public void testIteratorEquivalence() {
457 >    public void testTraversalEquivalence() {
458          Collection c = impl.emptyCollection();
459          ThreadLocalRandom rnd = ThreadLocalRandom.current();
460          int n = rnd.nextInt(6);
# Line 377 | Line 487 | public class Collection8Test extends JSR
487          if (c instanceof List || c instanceof Deque)
488              assertTrue(ordered);
489          HashSet cset = new HashSet(c);
490 <        assertEquals(cset, new HashSet(parallelStreamForEached));
490 >        mustEqual(cset, new HashSet(parallelStreamForEached));
491          if (ordered) {
492 <            assertEquals(iterated, iteratedForEachRemaining);
493 <            assertEquals(iterated, tryAdvanced);
494 <            assertEquals(iterated, spliterated);
495 <            assertEquals(iterated, splitonced);
496 <            assertEquals(iterated, forEached);
497 <            assertEquals(iterated, streamForEached);
498 <            assertEquals(iterated, removeIfed);
492 >            mustEqual(iterated, iteratedForEachRemaining);
493 >            mustEqual(iterated, tryAdvanced);
494 >            mustEqual(iterated, spliterated);
495 >            mustEqual(iterated, splitonced);
496 >            mustEqual(iterated, forEached);
497 >            mustEqual(iterated, streamForEached);
498 >            mustEqual(iterated, removeIfed);
499          } else {
500 <            assertEquals(cset, new HashSet(iterated));
501 <            assertEquals(cset, new HashSet(iteratedForEachRemaining));
502 <            assertEquals(cset, new HashSet(tryAdvanced));
503 <            assertEquals(cset, new HashSet(spliterated));
504 <            assertEquals(cset, new HashSet(splitonced));
505 <            assertEquals(cset, new HashSet(forEached));
506 <            assertEquals(cset, new HashSet(streamForEached));
507 <            assertEquals(cset, new HashSet(removeIfed));
500 >            mustEqual(cset, new HashSet(iterated));
501 >            mustEqual(cset, new HashSet(iteratedForEachRemaining));
502 >            mustEqual(cset, new HashSet(tryAdvanced));
503 >            mustEqual(cset, new HashSet(spliterated));
504 >            mustEqual(cset, new HashSet(splitonced));
505 >            mustEqual(cset, new HashSet(forEached));
506 >            mustEqual(cset, new HashSet(streamForEached));
507 >            mustEqual(cset, new HashSet(removeIfed));
508          }
509          if (c instanceof Deque) {
510              Deque d = (Deque) c;
# Line 406 | Line 516 | public class Collection8Test extends JSR
516                  e -> descendingForEachRemaining.add(e));
517              Collections.reverse(descending);
518              Collections.reverse(descendingForEachRemaining);
519 <            assertEquals(iterated, descending);
520 <            assertEquals(iterated, descendingForEachRemaining);
519 >            mustEqual(iterated, descending);
520 >            mustEqual(iterated, descendingForEachRemaining);
521          }
522      }
523  
524      /**
525 +     * Iterator.forEachRemaining has same behavior as Iterator's
526 +     * default implementation.
527 +     */
528 +    public void testForEachRemainingConsistentWithDefaultImplementation() {
529 +        Collection c = impl.emptyCollection();
530 +        if (!testImplementationDetails
531 +            || c.getClass() == java.util.LinkedList.class)
532 +            return;
533 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
534 +        int n = 1 + rnd.nextInt(3);
535 +        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
536 +        ArrayList iterated = new ArrayList();
537 +        ArrayList iteratedForEachRemaining = new ArrayList();
538 +        Iterator it1 = c.iterator();
539 +        Iterator it2 = c.iterator();
540 +        assertTrue(it1.hasNext());
541 +        assertTrue(it2.hasNext());
542 +        c.clear();
543 +        Object r1, r2;
544 +        try {
545 +            while (it1.hasNext()) iterated.add(it1.next());
546 +            r1 = iterated;
547 +        } catch (ConcurrentModificationException ex) {
548 +            r1 = ConcurrentModificationException.class;
549 +            assertFalse(impl.isConcurrent());
550 +        }
551 +        try {
552 +            it2.forEachRemaining(iteratedForEachRemaining::add);
553 +            r2 = iteratedForEachRemaining;
554 +        } catch (ConcurrentModificationException ex) {
555 +            r2 = ConcurrentModificationException.class;
556 +            assertFalse(impl.isConcurrent());
557 +        }
558 +        mustEqual(r1, r2);
559 +    }
560 +
561 +    /**
562       * Calling Iterator#remove() after Iterator#forEachRemaining
563       * should (maybe) remove last element
564       */
565      public void testRemoveAfterForEachRemaining() {
566          Collection c = impl.emptyCollection();
567          ThreadLocalRandom rnd = ThreadLocalRandom.current();
568 +        ArrayList copy = new ArrayList();
569 +        boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED);
570          testCollection: {
571              int n = 3 + rnd.nextInt(2);
572 <            for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
572 >            for (int i = 0; i < n; i++) {
573 >                Object x = impl.makeElement(i);
574 >                c.add(x);
575 >                copy.add(x);
576 >            }
577              Iterator it = c.iterator();
578 <            assertTrue(it.hasNext());
579 <            assertEquals(impl.makeElement(0), it.next());
580 <            assertTrue(it.hasNext());
581 <            assertEquals(impl.makeElement(1), it.next());
582 <            it.forEachRemaining(e -> assertTrue(c.contains(e)));
578 >            if (ordered) {
579 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
580 >                mustEqual(impl.makeElement(0), it.next());
581 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
582 >                mustEqual(impl.makeElement(1), it.next());
583 >            } else {
584 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
585 >                assertTrue(copy.contains(it.next()));
586 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
587 >                assertTrue(copy.contains(it.next()));
588 >            }
589 >            if (rnd.nextBoolean()) assertTrue(it.hasNext());
590 >            it.forEachRemaining(
591 >                e -> {
592 >                    assertTrue(c.contains(e));
593 >                    assertTrue(copy.contains(e));});
594              if (testImplementationDetails) {
595                  if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
596                      assertIteratorExhausted(it);
# Line 435 | Line 599 | public class Collection8Test extends JSR
599                      catch (UnsupportedOperationException ok) {
600                          break testCollection;
601                      }
602 <                    assertEquals(n - 1, c.size());
603 <                    for (int i = 0; i < n - 1; i++)
604 <                        assertTrue(c.contains(impl.makeElement(i)));
605 <                    assertFalse(c.contains(impl.makeElement(n - 1)));
602 >                    mustEqual(n - 1, c.size());
603 >                    if (ordered) {
604 >                        for (int i = 0; i < n - 1; i++)
605 >                            assertTrue(c.contains(impl.makeElement(i)));
606 >                        assertFalse(c.contains(impl.makeElement(n - 1)));
607 >                    }
608                  }
609              }
610          }
611          if (c instanceof Deque) {
612              Deque d = (Deque) impl.emptyCollection();
613 +            assertTrue(ordered);
614              int n = 3 + rnd.nextInt(2);
615              for (int i = 0; i < n; i++) d.add(impl.makeElement(i));
616              Iterator it = d.descendingIterator();
617              assertTrue(it.hasNext());
618 <            assertEquals(impl.makeElement(n - 1), it.next());
618 >            mustEqual(impl.makeElement(n - 1), it.next());
619              assertTrue(it.hasNext());
620 <            assertEquals(impl.makeElement(n - 2), it.next());
620 >            mustEqual(impl.makeElement(n - 2), it.next());
621              it.forEachRemaining(e -> assertTrue(c.contains(e)));
622              if (testImplementationDetails) {
623                  it.remove();
624 <                assertEquals(n - 1, d.size());
624 >                mustEqual(n - 1, d.size());
625                  for (int i = 1; i < n; i++)
626                      assertTrue(d.contains(impl.makeElement(i)));
627                  assertFalse(d.contains(impl.makeElement(0)));
# Line 467 | Line 634 | public class Collection8Test extends JSR
634       */
635      public void testStreamForEach() throws Throwable {
636          final Collection c = impl.emptyCollection();
470        final AtomicLong count = new AtomicLong(0L);
637          final Object x = impl.makeElement(1);
638          final Object y = impl.makeElement(2);
639          final ArrayList found = new ArrayList();
# Line 477 | Line 643 | public class Collection8Test extends JSR
643  
644          assertTrue(c.add(x));
645          c.stream().forEach(spy);
646 <        assertEquals(Collections.singletonList(x), found);
646 >        mustEqual(Collections.singletonList(x), found);
647          found.clear();
648  
649          assertTrue(c.add(y));
650          c.stream().forEach(spy);
651 <        assertEquals(2, found.size());
651 >        mustEqual(2, found.size());
652          assertTrue(found.contains(x));
653          assertTrue(found.contains(y));
654          found.clear();
# Line 525 | Line 691 | public class Collection8Test extends JSR
691       */
692      public void testForEach() throws Throwable {
693          final Collection c = impl.emptyCollection();
528        final AtomicLong count = new AtomicLong(0L);
694          final Object x = impl.makeElement(1);
695          final Object y = impl.makeElement(2);
696          final ArrayList found = new ArrayList();
# Line 535 | Line 700 | public class Collection8Test extends JSR
700  
701          assertTrue(c.add(x));
702          c.forEach(spy);
703 <        assertEquals(Collections.singletonList(x), found);
703 >        mustEqual(Collections.singletonList(x), found);
704          found.clear();
705  
706          assertTrue(c.add(y));
707          c.forEach(spy);
708 <        assertEquals(2, found.size());
708 >        mustEqual(2, found.size());
709          assertTrue(found.contains(x));
710          assertTrue(found.contains(y));
711          found.clear();
# Line 586 | Line 751 | public class Collection8Test extends JSR
751      }
752  
753      /**
754 +     * Concurrent Spliterators, once exhausted, stay exhausted.
755 +     */
756 +    public void testStickySpliteratorExhaustion() throws Throwable {
757 +        if (!impl.isConcurrent()) return;
758 +        if (!testImplementationDetails) return;
759 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
760 +        final Consumer alwaysThrows = e -> { throw new AssertionError(); };
761 +        final Collection c = impl.emptyCollection();
762 +        final Spliterator s = c.spliterator();
763 +        if (rnd.nextBoolean()) {
764 +            assertFalse(s.tryAdvance(alwaysThrows));
765 +        } else {
766 +            s.forEachRemaining(alwaysThrows);
767 +        }
768 +        final Object one = impl.makeElement(1);
769 +        // Spliterator should not notice added element
770 +        c.add(one);
771 +        if (rnd.nextBoolean()) {
772 +            assertFalse(s.tryAdvance(alwaysThrows));
773 +        } else {
774 +            s.forEachRemaining(alwaysThrows);
775 +        }
776 +    }
777 +
778 +    /**
779       * Motley crew of threads concurrently randomly hammer the collection.
780       */
781      public void testDetectRaces() throws Throwable {
# Line 689 | Line 879 | public class Collection8Test extends JSR
879              c.add(one);
880              split.forEachRemaining(
881                  e -> { assertSame(e, one); count.getAndIncrement(); });
882 <            assertEquals(1L, count.get());
882 >            mustEqual(1L, count.get());
883              assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
884              assertTrue(c.contains(one));
885          }
886      }
887  
888 +    /**
889 +     * Spliterator.getComparator throws IllegalStateException iff the
890 +     * spliterator does not report SORTED.
891 +     */
892 +    public void testGetComparator_IllegalStateException() {
893 +        Collection c = impl.emptyCollection();
894 +        Spliterator s = c.spliterator();
895 +        boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED);
896 +        try {
897 +            s.getComparator();
898 +            assertTrue(reportsSorted);
899 +        } catch (IllegalStateException ex) {
900 +            assertFalse(reportsSorted);
901 +        }
902 +    }
903 +
904 +    public void testCollectionCopies() throws Exception {
905 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
906 +        Collection c = impl.emptyCollection();
907 +        for (int n = rnd.nextInt(4); n--> 0; )
908 +            c.add(impl.makeElement(rnd.nextInt()));
909 +        mustEqual(c, c);
910 +        if (c instanceof List)
911 +            assertCollectionsEquals(c, new ArrayList(c));
912 +        else if (c instanceof Set)
913 +            assertCollectionsEquals(c, new HashSet(c));
914 +        else if (c instanceof Deque)
915 +            assertCollectionsEquivalent(c, new ArrayDeque(c));
916 +
917 +        Collection clone = cloneableClone(c);
918 +        if (clone != null) {
919 +            assertSame(c.getClass(), clone.getClass());
920 +            assertCollectionsEquivalent(c, clone);
921 +        }
922 +        try {
923 +            Collection serialClone = serialClonePossiblyFailing(c);
924 +            assertSame(c.getClass(), serialClone.getClass());
925 +            assertCollectionsEquivalent(c, serialClone);
926 +        } catch (java.io.NotSerializableException acceptable) {}
927 +    }
928 +
929 +    /**
930 +     * TODO: move out of limbo
931 +     * 8203662: remove increment of modCount from ArrayList and Vector replaceAll()
932 +     */
933 +    public void DISABLED_testReplaceAllIsNotStructuralModification() {
934 +        Collection c = impl.emptyCollection();
935 +        if (!(c instanceof List))
936 +            return;
937 +        List list = (List) c;
938 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
939 +        for (int n = rnd.nextInt(2, 10); n--> 0; )
940 +            list.add(impl.makeElement(rnd.nextInt()));
941 +        ArrayList copy = new ArrayList(list);
942 +        int size = list.size(), half = size / 2;
943 +        Iterator it = list.iterator();
944 +        for (int i = 0; i < half; i++)
945 +            mustEqual(it.next(), copy.get(i));
946 +        list.replaceAll(n -> n);
947 +        // ConcurrentModificationException must not be thrown here.
948 +        for (int i = half; i < size; i++)
949 +            mustEqual(it.next(), copy.get(i));
950 +    }
951 +
952   //     public void testCollection8DebugFail() {
953   //         fail(impl.klazz().getSimpleName());
954   //     }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines