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.15 by jsr166, Sun Nov 6 04:15:45 2016 UTC vs.
Revision 1.25 by jsr166, Tue Nov 15 00:08:25 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.Deque;
# Line 59 | Line 61 | public class Collection8Test extends JSR
61      }
62  
63      /** Checks properties of empty collections. */
64 <    public void testEmptyMeansEmpty() throws InterruptedException {
64 >    public void testEmptyMeansEmpty() throws Throwable {
65          Collection c = impl.emptyCollection();
66          emptyMeansEmpty(c);
67  
68 <        if (c instanceof java.io.Serializable)
69 <            emptyMeansEmpty(serialClone(c));
68 >        if (c instanceof java.io.Serializable) {
69 >            try {
70 >                emptyMeansEmpty(serialClonePossiblyFailing(c));
71 >            } catch (java.io.NotSerializableException ex) {
72 >                // excusable when we have a serializable wrapper around
73 >                // a non-serializable collection, as can happen with:
74 >                // Vector.subList() => wrapped AbstractList$RandomAccessSubList
75 >                if (testImplementationDetails
76 >                    && (! c.getClass().getName().matches(
77 >                                "java.util.Collections.*")))
78 >                    throw ex;
79 >            }
80 >        }
81  
82          Collection clone = cloneableClone(c);
83          if (clone != null)
# Line 96 | Line 109 | public class Collection8Test extends JSR
109              assertSame(3, a[2]);
110          }
111          assertIteratorExhausted(c.iterator());
112 <        Consumer alwaysThrows = (e) -> { throw new AssertionError(); };
112 >        Consumer alwaysThrows = e -> { throw new AssertionError(); };
113          c.forEach(alwaysThrows);
114          c.iterator().forEachRemaining(alwaysThrows);
115          c.spliterator().forEachRemaining(alwaysThrows);
# Line 174 | Line 187 | public class Collection8Test extends JSR
187              assertThrows(
188                  NullPointerException.class,
189                  () -> {
190 <                    try { q.offer(null, 1L, MILLISECONDS); }
190 >                    try { q.offer(null, 1L, HOURS); }
191                      catch (InterruptedException ex) {
192                          throw new AssertionError(ex);
193                      }},
# Line 189 | Line 202 | public class Collection8Test extends JSR
202              assertThrows(
203                  NullPointerException.class,
204                  () -> {
205 <                    try { q.offerFirst(null, 1L, MILLISECONDS); }
205 >                    try { q.offerFirst(null, 1L, HOURS); }
206                      catch (InterruptedException ex) {
207                          throw new AssertionError(ex);
208                      }},
209                  () -> {
210 <                    try { q.offerLast(null, 1L, MILLISECONDS); }
210 >                    try { q.offerLast(null, 1L, HOURS); }
211                      catch (InterruptedException ex) {
212                          throw new AssertionError(ex);
213                      }},
# Line 239 | Line 252 | public class Collection8Test extends JSR
252  
253      public void testRemoveIf() {
254          Collection c = impl.emptyCollection();
255 +        boolean ordered =
256 +            c.spliterator().hasCharacteristics(Spliterator.ORDERED);
257          ThreadLocalRandom rnd = ThreadLocalRandom.current();
258          int n = rnd.nextInt(6);
259          for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
260          AtomicReference threwAt = new AtomicReference(null);
261 <        ArrayList survivors = new ArrayList(c);
261 >        List orig = rnd.nextBoolean()
262 >            ? new ArrayList(c)
263 >            : Arrays.asList(c.toArray());
264 >
265 >        // Merely creating an iterator can change ArrayBlockingQueue behavior
266 >        Iterator it = rnd.nextBoolean() ? c.iterator() : null;
267 >
268 >        ArrayList survivors = new ArrayList();
269          ArrayList accepts = new ArrayList();
270          ArrayList rejects = new ArrayList();
271 <        Predicate randomPredicate = (e) -> {
271 >
272 >        Predicate randomPredicate = e -> {
273              assertNull(threwAt.get());
274              switch (rnd.nextInt(3)) {
275              case 0: accepts.add(e); return true;
# Line 256 | Line 279 | public class Collection8Test extends JSR
279              }
280          };
281          try {
259            assertFalse(survivors.contains(null));
282              try {
283                  boolean modified = c.removeIf(randomPredicate);
284 <                if (!modified) {
285 <                    assertNull(threwAt.get());
286 <                    assertEquals(n, rejects.size());
287 <                    assertEquals(0, accepts.size());
284 >                assertNull(threwAt.get());
285 >                assertEquals(modified, accepts.size() > 0);
286 >                assertEquals(modified, rejects.size() != n);
287 >                assertEquals(accepts.size() + rejects.size(), n);
288 >                if (ordered) {
289 >                    assertEquals(rejects,
290 >                                 Arrays.asList(c.toArray()));
291 >                } else {
292 >                    assertEquals(new HashSet(rejects),
293 >                                 new HashSet(Arrays.asList(c.toArray())));
294                  }
295 <            } catch (ArithmeticException ok) {}
296 <            survivors.removeAll(accepts);
297 <            assertEquals(n - accepts.size(), c.size());
295 >            } catch (ArithmeticException ok) {
296 >                assertNotNull(threwAt.get());
297 >                assertTrue(c.contains(threwAt.get()));
298 >            }
299 >            if (it != null && impl.isConcurrent())
300 >                // check for weakly consistent iterator
301 >                while (it.hasNext()) assertTrue(orig.contains(it.next()));
302 >            switch (rnd.nextInt(4)) {
303 >            case 0: survivors.addAll(c); break;
304 >            case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
305 >            case 2: c.forEach(e -> survivors.add(e)); break;
306 >            case 3: for (Object e : c) survivors.add(e); break;
307 >            }
308 >            assertTrue(orig.containsAll(accepts));
309 >            assertTrue(orig.containsAll(rejects));
310 >            assertTrue(orig.containsAll(survivors));
311 >            assertTrue(orig.containsAll(c));
312 >            assertTrue(c.containsAll(rejects));
313              assertTrue(c.containsAll(survivors));
314              assertTrue(survivors.containsAll(rejects));
315 <            for (Object x : accepts) assertFalse(c.contains(x));
316 <            if (threwAt.get() == null)
317 <                assertEquals(accepts.size() + rejects.size(), n);
315 >            if (threwAt.get() == null) {
316 >                assertEquals(n - accepts.size(), c.size());
317 >                for (Object x : accepts) assertFalse(c.contains(x));
318 >            } else {
319 >                // Two acceptable behaviors: entire removeIf call is one
320 >                // transaction, or each element processed is one transaction.
321 >                assertTrue(n == c.size() || n == c.size() + accepts.size());
322 >                int k = 0;
323 >                for (Object x : accepts) if (c.contains(x)) k++;
324 >                assertTrue(k == accepts.size() || k == 0);
325 >            }
326          } catch (Throwable ex) {
327              System.err.println(impl.klazz());
328 <            System.err.printf("c=%s%n", c);
328 >            // c is at risk of corruption if we got here, so be lenient
329 >            try { System.err.printf("c=%s%n", c); }
330 >            catch (Throwable t) { t.printStackTrace(); }
331              System.err.printf("n=%d%n", n);
332 +            System.err.printf("orig=%s%n", orig);
333              System.err.printf("accepts=%s%n", accepts);
334              System.err.printf("rejects=%s%n", rejects);
335              System.err.printf("survivors=%s%n", survivors);
336 <            System.err.printf("threw=%s%n", threwAt.get());
336 >            System.err.printf("threwAt=%s%n", threwAt.get());
337              throw ex;
338          }
339      }
# Line 341 | Line 395 | public class Collection8Test extends JSR
395  
396      /**
397       * Calling Iterator#remove() after Iterator#forEachRemaining
398 <     * should remove last element
398 >     * should (maybe) remove last element
399       */
400      public void testRemoveAfterForEachRemaining() {
401          Collection c = impl.emptyCollection();
402          ThreadLocalRandom rnd = ThreadLocalRandom.current();
403 <        {
403 >        testCollection: {
404              int n = 3 + rnd.nextInt(2);
405              for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
406              Iterator it = c.iterator();
# Line 354 | Line 408 | public class Collection8Test extends JSR
408              assertEquals(impl.makeElement(0), it.next());
409              assertTrue(it.hasNext());
410              assertEquals(impl.makeElement(1), it.next());
411 <            it.forEachRemaining((e) -> {});
412 <            it.remove();
413 <            assertEquals(n - 1, c.size());
414 <            for (int i = 0; i < n - 1; i++)
415 <                assertTrue(c.contains(impl.makeElement(i)));
416 <            assertFalse(c.contains(impl.makeElement(n - 1)));
411 >            it.forEachRemaining(e -> assertTrue(c.contains(e)));
412 >            if (testImplementationDetails) {
413 >                if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
414 >                    assertIteratorExhausted(it);
415 >                } else {
416 >                    try { it.remove(); }
417 >                    catch (UnsupportedOperationException ok) {
418 >                        break testCollection;
419 >                    }
420 >                    assertEquals(n - 1, c.size());
421 >                    for (int i = 0; i < n - 1; i++)
422 >                        assertTrue(c.contains(impl.makeElement(i)));
423 >                    assertFalse(c.contains(impl.makeElement(n - 1)));
424 >                }
425 >            }
426          }
427          if (c instanceof Deque) {
428              Deque d = (Deque) impl.emptyCollection();
# Line 370 | Line 433 | public class Collection8Test extends JSR
433              assertEquals(impl.makeElement(n - 1), it.next());
434              assertTrue(it.hasNext());
435              assertEquals(impl.makeElement(n - 2), it.next());
436 <            it.forEachRemaining((e) -> {});
437 <            it.remove();
438 <            assertEquals(n - 1, d.size());
439 <            for (int i = 1; i < n; i++)
440 <                assertTrue(d.contains(impl.makeElement(i)));
441 <            assertFalse(d.contains(impl.makeElement(0)));
436 >            it.forEachRemaining(e -> assertTrue(c.contains(e)));
437 >            if (testImplementationDetails) {
438 >                it.remove();
439 >                assertEquals(n - 1, d.size());
440 >                for (int i = 1; i < n; i++)
441 >                    assertTrue(d.contains(impl.makeElement(i)));
442 >                assertFalse(d.contains(impl.makeElement(0)));
443 >            }
444          }
445      }
446  
# Line 388 | Line 453 | public class Collection8Test extends JSR
453          final Object x = impl.makeElement(1);
454          final Object y = impl.makeElement(2);
455          final ArrayList found = new ArrayList();
456 <        Consumer<Object> spy = (o) -> { found.add(o); };
456 >        Consumer<Object> spy = o -> found.add(o);
457          c.stream().forEach(spy);
458          assertTrue(found.isEmpty());
459  
# Line 422 | Line 487 | public class Collection8Test extends JSR
487              Runnable checkElt = () -> {
488                  threadsStarted.countDown();
489                  while (!done.get())
490 <                    c.stream().forEach((x) -> { assertSame(x, elt); }); };
490 >                    c.stream().forEach(x -> assertSame(x, elt)); };
491              Runnable addRemove = () -> {
492                  threadsStarted.countDown();
493                  while (!done.get()) {
# Line 446 | Line 511 | public class Collection8Test extends JSR
511          final Object x = impl.makeElement(1);
512          final Object y = impl.makeElement(2);
513          final ArrayList found = new ArrayList();
514 <        Consumer<Object> spy = (o) -> { found.add(o); };
514 >        Consumer<Object> spy = o -> found.add(o);
515          c.forEach(spy);
516          assertTrue(found.isEmpty());
517  
# Line 480 | Line 545 | public class Collection8Test extends JSR
545              Runnable checkElt = () -> {
546                  threadsStarted.countDown();
547                  while (!done.get())
548 <                    c.forEach((x) -> { assertSame(x, elt); }); };
548 >                    c.forEach(x -> assertSame(x, elt)); };
549              Runnable addRemove = () -> {
550                  threadsStarted.countDown();
551                  while (!done.get()) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines