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.14 by jsr166, Sun Nov 6 03:35:25 2016 UTC vs.
Revision 1.24 by jsr166, Mon Nov 14 23:52:22 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 169 | Line 182 | public class Collection8Test extends JSR
182                  () -> d.push(null),
183                  () -> d.descendingIterator().forEachRemaining(null));
184          }
185 <        if (!impl.permitsNulls() && c instanceof BlockingQueue) {
185 >        if (c instanceof BlockingQueue) {
186              BlockingQueue q = (BlockingQueue) c;
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 >                    }},
194 >                () -> {
195 >                    try { q.put(null); }
196                      catch (InterruptedException ex) {
197                          throw new AssertionError(ex);
198                      }});
199          }
200 <        if (!impl.permitsNulls() && c instanceof BlockingDeque) {
200 >        if (c instanceof BlockingDeque) {
201              BlockingDeque q = (BlockingDeque) c;
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, HOURS); }
211 >                    catch (InterruptedException ex) {
212 >                        throw new AssertionError(ex);
213 >                    }},
214 >                () -> {
215 >                    try { q.putFirst(null); }
216                      catch (InterruptedException ex) {
217                          throw new AssertionError(ex);
218                      }},
219                  () -> {
220 <                    try { q.offerLast(null, 1L, MILLISECONDS); }
220 >                    try { q.putLast(null); }
221                      catch (InterruptedException ex) {
222                          throw new AssertionError(ex);
223                      }});
# Line 224 | 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 241 | Line 279 | public class Collection8Test extends JSR
279              }
280          };
281          try {
244            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 326 | 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();
# Line 339 | 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 >                    it.remove();
417 >                    assertEquals(n - 1, c.size());
418 >                    for (int i = 0; i < n - 1; i++)
419 >                        assertTrue(c.contains(impl.makeElement(i)));
420 >                    assertFalse(c.contains(impl.makeElement(n - 1)));
421 >                }
422 >            }
423          }
424          if (c instanceof Deque) {
425              Deque d = (Deque) impl.emptyCollection();
# Line 355 | Line 430 | public class Collection8Test extends JSR
430              assertEquals(impl.makeElement(n - 1), it.next());
431              assertTrue(it.hasNext());
432              assertEquals(impl.makeElement(n - 2), it.next());
433 <            it.forEachRemaining((e) -> {});
434 <            it.remove();
435 <            assertEquals(n - 1, d.size());
436 <            for (int i = 1; i < n; i++)
437 <                assertTrue(d.contains(impl.makeElement(i)));
438 <            assertFalse(d.contains(impl.makeElement(0)));
433 >            it.forEachRemaining(e -> assertTrue(c.contains(e)));
434 >            if (testImplementationDetails) {
435 >                it.remove();
436 >                assertEquals(n - 1, d.size());
437 >                for (int i = 1; i < n; i++)
438 >                    assertTrue(d.contains(impl.makeElement(i)));
439 >                assertFalse(d.contains(impl.makeElement(0)));
440 >            }
441          }
442      }
443  
# Line 373 | Line 450 | public class Collection8Test extends JSR
450          final Object x = impl.makeElement(1);
451          final Object y = impl.makeElement(2);
452          final ArrayList found = new ArrayList();
453 <        Consumer<Object> spy = (o) -> { found.add(o); };
453 >        Consumer<Object> spy = o -> found.add(o);
454          c.stream().forEach(spy);
455          assertTrue(found.isEmpty());
456  
# Line 407 | Line 484 | public class Collection8Test extends JSR
484              Runnable checkElt = () -> {
485                  threadsStarted.countDown();
486                  while (!done.get())
487 <                    c.stream().forEach((x) -> { assertSame(x, elt); }); };
487 >                    c.stream().forEach(x -> assertSame(x, elt)); };
488              Runnable addRemove = () -> {
489                  threadsStarted.countDown();
490                  while (!done.get()) {
# Line 431 | Line 508 | public class Collection8Test extends JSR
508          final Object x = impl.makeElement(1);
509          final Object y = impl.makeElement(2);
510          final ArrayList found = new ArrayList();
511 <        Consumer<Object> spy = (o) -> { found.add(o); };
511 >        Consumer<Object> spy = o -> found.add(o);
512          c.forEach(spy);
513          assertTrue(found.isEmpty());
514  
# Line 465 | Line 542 | public class Collection8Test extends JSR
542              Runnable checkElt = () -> {
543                  threadsStarted.countDown();
544                  while (!done.get())
545 <                    c.forEach((x) -> { assertSame(x, elt); }); };
545 >                    c.forEach(x -> assertSame(x, elt)); };
546              Runnable addRemove = () -> {
547                  threadsStarted.countDown();
548                  while (!done.get()) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines