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

Comparing jsr166/src/test/tck/CollectionTest.java (file contents):
Revision 1.2 by jsr166, Mon Oct 17 00:57:45 2016 UTC vs.
Revision 1.7 by jsr166, Mon Oct 17 20:45:31 2016 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 + import java.util.ArrayList;
9   import java.util.Collection;
10   import java.util.Deque;
11   import java.util.NoSuchElementException;
12   import java.util.Queue;
13 + import java.util.concurrent.ThreadLocalRandom;
14 + import java.util.concurrent.atomic.AtomicReference;
15 + import java.util.function.Consumer;
16 + import java.util.function.Predicate;
17  
18   import junit.framework.Test;
19  
# Line 45 | Line 50 | public class CollectionTest extends JSR1
50              Object[] a = new Object[0];
51              assertSame(a, c.toArray(a));
52          }
53 <        c.forEach((e) -> { throw new AssertionError(); });
53 >        assertIteratorExhausted(c.iterator());
54 >        Consumer alwaysThrows = (e) -> { throw new AssertionError(); };
55 >        c.forEach(alwaysThrows);
56 >        c.iterator().forEachRemaining(alwaysThrows);
57 >        c.spliterator().forEachRemaining(alwaysThrows);
58 >        assertFalse(c.spliterator().tryAdvance(alwaysThrows));
59          if (Queue.class.isAssignableFrom(impl.klazz())) {
60              Queue q = (Queue) c;
61              assertNull(q.peek());
# Line 57 | Line 67 | public class CollectionTest extends JSR1
67              assertNull(d.peekLast());
68              assertNull(d.pollFirst());
69              assertNull(d.pollLast());
70 +            assertIteratorExhausted(d.descendingIterator());
71          }
72      }
73  
# Line 97 | Line 108 | public class CollectionTest extends JSR1
108  
109      public void testNoSuchElementExceptions() {
110          Collection c = impl.emptyCollection();
111 +        assertThrows(
112 +            NoSuchElementException.class,
113 +            () -> c.iterator().next());
114  
115          if (Queue.class.isAssignableFrom(impl.klazz())) {
116              Queue q = (Queue) c;
# Line 113 | Line 127 | public class CollectionTest extends JSR1
127                  () -> d.getLast(),
128                  () -> d.removeFirst(),
129                  () -> d.removeLast(),
130 <                () -> d.pop());
130 >                () -> d.pop(),
131 >                () -> d.descendingIterator().next());
132          }
133      }
134  
135 <    // public void testCollectionDebugFail() { fail(); }
135 >    public void testRemoveIf() {
136 >        Collection c = impl.emptyCollection();
137 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
138 >        int n = rnd.nextInt(6);
139 >        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
140 >        AtomicReference threwAt = new AtomicReference(null);
141 >        ArrayList survivors = new ArrayList(c);
142 >        ArrayList accepts = new ArrayList();
143 >        ArrayList rejects = new ArrayList();
144 >        Predicate randomPredicate = (e) -> {
145 >            assertNull(threwAt.get());
146 >            switch (rnd.nextInt(3)) {
147 >            case 0: accepts.add(e); return true;
148 >            case 1: rejects.add(e); return false;
149 >            case 2: threwAt.set(e); throw new ArithmeticException();
150 >            default: throw new AssertionError();
151 >            }
152 >        };
153 >        try {
154 >            boolean modified = c.removeIf(randomPredicate);
155 >            if (!modified) {
156 >                assertNull(threwAt.get());
157 >                assertEquals(n, rejects.size());
158 >                assertEquals(0, accepts.size());
159 >            }
160 >        } catch (ArithmeticException ok) {}
161 >        survivors.removeAll(accepts);
162 >        if (n - accepts.size() != c.size()) {
163 >            System.err.println(impl.klazz());
164 >            System.err.println(c);
165 >            System.err.println(accepts);
166 >            System.err.println(rejects);
167 >            System.err.println(survivors);
168 >            System.err.println(threwAt.get());
169 >        }
170 >        assertEquals(n - accepts.size(), c.size());
171 >        assertTrue(c.containsAll(survivors));
172 >        assertTrue(survivors.containsAll(rejects));
173 >        for (Object x : accepts) assertFalse(c.contains(x));
174 >        if (threwAt.get() == null)
175 >            assertEquals(accepts.size() + rejects.size(), n);
176 >    }
177 >
178 > //     public void testCollectionDebugFail() {
179 > //         fail(impl.klazz().getSimpleName());
180 > //     }
181   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines