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.13 by jsr166, Sat Nov 5 19:56:46 2016 UTC vs.
Revision 1.18 by jsr166, Sun Nov 6 20:53:53 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 17 | Line 19 | import java.util.List;
19   import java.util.NoSuchElementException;
20   import java.util.Queue;
21   import java.util.Spliterator;
22 + import java.util.concurrent.BlockingDeque;
23 + import java.util.concurrent.BlockingQueue;
24   import java.util.concurrent.CountDownLatch;
25   import java.util.concurrent.Executors;
26   import java.util.concurrent.ExecutorService;
# Line 57 | Line 61 | public class Collection8Test extends JSR
61      }
62  
63      /** Checks properties of empty collections. */
64 <    public void testEmptyMeansEmpty() {
64 >    public void testEmptyMeansEmpty() throws InterruptedException {
65          Collection c = impl.emptyCollection();
66          emptyMeansEmpty(c);
67  
# Line 69 | Line 73 | public class Collection8Test extends JSR
73              emptyMeansEmpty(clone);
74      }
75  
76 <    void emptyMeansEmpty(Collection c) {
76 >    void emptyMeansEmpty(Collection c) throws InterruptedException {
77          assertTrue(c.isEmpty());
78          assertEquals(0, c.size());
79          assertEquals("[]", c.toString());
# Line 119 | Line 123 | public class Collection8Test extends JSR
123              assertFalse(d.removeFirstOccurrence(bomb()));
124              assertFalse(d.removeLastOccurrence(bomb()));
125          }
126 +        if (c instanceof BlockingQueue) {
127 +            BlockingQueue q = (BlockingQueue) c;
128 +            assertNull(q.poll(0L, MILLISECONDS));
129 +        }
130 +        if (c instanceof BlockingDeque) {
131 +            BlockingDeque q = (BlockingDeque) c;
132 +            assertNull(q.pollFirst(0L, MILLISECONDS));
133 +            assertNull(q.pollLast(0L, MILLISECONDS));
134 +        }
135      }
136  
137 <    public void testNullPointerExceptions() {
137 >    public void testNullPointerExceptions() throws InterruptedException {
138          Collection c = impl.emptyCollection();
139          assertThrows(
140              NullPointerException.class,
# Line 141 | Line 154 | public class Collection8Test extends JSR
154                  NullPointerException.class,
155                  () -> c.add(null));
156          }
157 <        if (!impl.permitsNulls()
145 <            && Queue.class.isAssignableFrom(impl.klazz())) {
157 >        if (!impl.permitsNulls() && c instanceof Queue) {
158              Queue q = (Queue) c;
159              assertThrows(
160                  NullPointerException.class,
161                  () -> q.offer(null));
162          }
163 <        if (!impl.permitsNulls()
152 <            && Deque.class.isAssignableFrom(impl.klazz())) {
163 >        if (!impl.permitsNulls() && c instanceof Deque) {
164              Deque d = (Deque) c;
165              assertThrows(
166                  NullPointerException.class,
# Line 160 | Line 171 | public class Collection8Test extends JSR
171                  () -> d.push(null),
172                  () -> d.descendingIterator().forEachRemaining(null));
173          }
174 +        if (c instanceof BlockingQueue) {
175 +            BlockingQueue q = (BlockingQueue) c;
176 +            assertThrows(
177 +                NullPointerException.class,
178 +                () -> {
179 +                    try { q.offer(null, 1L, HOURS); }
180 +                    catch (InterruptedException ex) {
181 +                        throw new AssertionError(ex);
182 +                    }},
183 +                () -> {
184 +                    try { q.put(null); }
185 +                    catch (InterruptedException ex) {
186 +                        throw new AssertionError(ex);
187 +                    }});
188 +        }
189 +        if (c instanceof BlockingDeque) {
190 +            BlockingDeque q = (BlockingDeque) c;
191 +            assertThrows(
192 +                NullPointerException.class,
193 +                () -> {
194 +                    try { q.offerFirst(null, 1L, HOURS); }
195 +                    catch (InterruptedException ex) {
196 +                        throw new AssertionError(ex);
197 +                    }},
198 +                () -> {
199 +                    try { q.offerLast(null, 1L, HOURS); }
200 +                    catch (InterruptedException ex) {
201 +                        throw new AssertionError(ex);
202 +                    }},
203 +                () -> {
204 +                    try { q.putFirst(null); }
205 +                    catch (InterruptedException ex) {
206 +                        throw new AssertionError(ex);
207 +                    }},
208 +                () -> {
209 +                    try { q.putLast(null); }
210 +                    catch (InterruptedException ex) {
211 +                        throw new AssertionError(ex);
212 +                    }});
213 +        }
214      }
215  
216      public void testNoSuchElementExceptions() {
# Line 168 | Line 219 | public class Collection8Test extends JSR
219              NoSuchElementException.class,
220              () -> c.iterator().next());
221  
222 <        if (Queue.class.isAssignableFrom(impl.klazz())) {
222 >        if (c instanceof Queue) {
223              Queue q = (Queue) c;
224              assertThrows(
225                  NoSuchElementException.class,
226                  () -> q.element(),
227                  () -> q.remove());
228          }
229 <        if (Deque.class.isAssignableFrom(impl.klazz())) {
229 >        if (c instanceof Deque) {
230              Deque d = (Deque) c;
231              assertThrows(
232                  NoSuchElementException.class,
# Line 194 | Line 245 | public class Collection8Test extends JSR
245          int n = rnd.nextInt(6);
246          for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
247          AtomicReference threwAt = new AtomicReference(null);
248 <        ArrayList survivors = new ArrayList(c);
248 >        List orig = rnd.nextBoolean()
249 >            ? new ArrayList(c)
250 >            : Arrays.asList(c.toArray());
251 >
252 >        // Merely creating an iterator can change ArrayBlockingQueue behavior
253 >        Iterator it = rnd.nextBoolean() ? c.iterator() : null;
254 >
255 >        ArrayList survivors = new ArrayList();
256          ArrayList accepts = new ArrayList();
257          ArrayList rejects = new ArrayList();
258 +
259          Predicate randomPredicate = (e) -> {
260              assertNull(threwAt.get());
261              switch (rnd.nextInt(3)) {
# Line 207 | Line 266 | public class Collection8Test extends JSR
266              }
267          };
268          try {
210            assertFalse(survivors.contains(null));
269              try {
270                  boolean modified = c.removeIf(randomPredicate);
271 <                if (!modified) {
272 <                    assertNull(threwAt.get());
273 <                    assertEquals(n, rejects.size());
274 <                    assertEquals(0, accepts.size());
275 <                }
276 <            } catch (ArithmeticException ok) {}
277 <            survivors.removeAll(accepts);
278 <            assertEquals(n - accepts.size(), c.size());
271 >                assertNull(threwAt.get());
272 >                assertEquals(modified, accepts.size() > 0);
273 >                assertEquals(modified, rejects.size() != n);
274 >                assertEquals(accepts.size() + rejects.size(), n);
275 >                assertEquals(rejects, Arrays.asList(c.toArray()));
276 >            } catch (ArithmeticException ok) {
277 >                assertNotNull(threwAt.get());
278 >                assertTrue(c.contains(threwAt.get()));
279 >            }
280 >            if (it != null && impl.isConcurrent())
281 >                // check for weakly consistent iterator
282 >                while (it.hasNext()) assertTrue(orig.contains(it.next()));
283 >            switch (rnd.nextInt(4)) {
284 >            case 0: survivors.addAll(c); break;
285 >            case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
286 >            case 2: c.forEach(e -> survivors.add(e)); break;
287 >            case 3: for (Object e : c) survivors.add(e); break;
288 >            }
289 >            assertTrue(orig.containsAll(accepts));
290 >            assertTrue(orig.containsAll(rejects));
291 >            assertTrue(orig.containsAll(survivors));
292 >            assertTrue(orig.containsAll(c));
293 >            assertTrue(c.containsAll(rejects));
294              assertTrue(c.containsAll(survivors));
295              assertTrue(survivors.containsAll(rejects));
296 +            assertEquals(n - accepts.size(), c.size());
297              for (Object x : accepts) assertFalse(c.contains(x));
224            if (threwAt.get() == null)
225                assertEquals(accepts.size() + rejects.size(), n);
298          } catch (Throwable ex) {
299              System.err.println(impl.klazz());
300              System.err.printf("c=%s%n", c);
301              System.err.printf("n=%d%n", n);
302 +            System.err.printf("orig=%s%n", orig);
303              System.err.printf("accepts=%s%n", accepts);
304              System.err.printf("rejects=%s%n", rejects);
305              System.err.printf("survivors=%s%n", survivors);
306 <            System.err.printf("threw=%s%n", threwAt.get());
306 >            System.err.printf("threwAt=%s%n", threwAt.get());
307              throw ex;
308          }
309      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines