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.5 by jsr166, Tue Oct 25 01:32:55 2016 UTC vs.
Revision 1.14 by jsr166, Sun Nov 6 03:35:25 2016 UTC

# Line 17 | Line 17 | import java.util.List;
17   import java.util.NoSuchElementException;
18   import java.util.Queue;
19   import java.util.Spliterator;
20 + import java.util.concurrent.BlockingDeque;
21 + import java.util.concurrent.BlockingQueue;
22   import java.util.concurrent.CountDownLatch;
23   import java.util.concurrent.Executors;
24   import java.util.concurrent.ExecutorService;
# Line 49 | Line 51 | public class Collection8Test extends JSR
51                                        impl);
52      }
53  
54 +    Object bomb() {
55 +        return new Object() {
56 +                public boolean equals(Object x) { throw new AssertionError(); }
57 +                public int hashCode() { throw new AssertionError(); }
58 +            };
59 +    }
60 +
61      /** Checks properties of empty collections. */
62 <    public void testEmptyMeansEmpty() {
62 >    public void testEmptyMeansEmpty() throws InterruptedException {
63          Collection c = impl.emptyCollection();
64 +        emptyMeansEmpty(c);
65 +
66 +        if (c instanceof java.io.Serializable)
67 +            emptyMeansEmpty(serialClone(c));
68 +
69 +        Collection clone = cloneableClone(c);
70 +        if (clone != null)
71 +            emptyMeansEmpty(clone);
72 +    }
73 +
74 +    void emptyMeansEmpty(Collection c) throws InterruptedException {
75          assertTrue(c.isEmpty());
76          assertEquals(0, c.size());
77          assertEquals("[]", c.toString());
# Line 81 | Line 101 | public class Collection8Test extends JSR
101          c.iterator().forEachRemaining(alwaysThrows);
102          c.spliterator().forEachRemaining(alwaysThrows);
103          assertFalse(c.spliterator().tryAdvance(alwaysThrows));
104 <        if (Queue.class.isAssignableFrom(impl.klazz())) {
104 >        if (c.spliterator().hasCharacteristics(Spliterator.SIZED))
105 >            assertEquals(0, c.spliterator().estimateSize());
106 >        assertFalse(c.contains(bomb()));
107 >        assertFalse(c.remove(bomb()));
108 >        if (c instanceof Queue) {
109              Queue q = (Queue) c;
110              assertNull(q.peek());
111              assertNull(q.poll());
112          }
113 <        if (Deque.class.isAssignableFrom(impl.klazz())) {
113 >        if (c instanceof Deque) {
114              Deque d = (Deque) c;
115              assertNull(d.peekFirst());
116              assertNull(d.peekLast());
117              assertNull(d.pollFirst());
118              assertNull(d.pollLast());
119              assertIteratorExhausted(d.descendingIterator());
120 +            d.descendingIterator().forEachRemaining(alwaysThrows);
121 +            assertFalse(d.removeFirstOccurrence(bomb()));
122 +            assertFalse(d.removeLastOccurrence(bomb()));
123 +        }
124 +        if (c instanceof BlockingQueue) {
125 +            BlockingQueue q = (BlockingQueue) c;
126 +            assertNull(q.poll(0L, MILLISECONDS));
127 +        }
128 +        if (c instanceof BlockingDeque) {
129 +            BlockingDeque q = (BlockingDeque) c;
130 +            assertNull(q.pollFirst(0L, MILLISECONDS));
131 +            assertNull(q.pollLast(0L, MILLISECONDS));
132          }
133      }
134  
135 <    public void testNullPointerExceptions() {
135 >    public void testNullPointerExceptions() throws InterruptedException {
136          Collection c = impl.emptyCollection();
137          assertThrows(
138              NullPointerException.class,
# Line 105 | Line 141 | public class Collection8Test extends JSR
141              () -> c.retainAll(null),
142              () -> c.removeAll(null),
143              () -> c.removeIf(null),
144 +            () -> c.forEach(null),
145 +            () -> c.iterator().forEachRemaining(null),
146 +            () -> c.spliterator().forEachRemaining(null),
147 +            () -> c.spliterator().tryAdvance(null),
148              () -> c.toArray(null));
149  
150          if (!impl.permitsNulls()) {
# Line 112 | Line 152 | public class Collection8Test extends JSR
152                  NullPointerException.class,
153                  () -> c.add(null));
154          }
155 <        if (!impl.permitsNulls()
116 <            && Queue.class.isAssignableFrom(impl.klazz())) {
155 >        if (!impl.permitsNulls() && c instanceof Queue) {
156              Queue q = (Queue) c;
157              assertThrows(
158                  NullPointerException.class,
159                  () -> q.offer(null));
160          }
161 <        if (!impl.permitsNulls()
123 <            && Deque.class.isAssignableFrom(impl.klazz())) {
161 >        if (!impl.permitsNulls() && c instanceof Deque) {
162              Deque d = (Deque) c;
163              assertThrows(
164                  NullPointerException.class,
# Line 128 | Line 166 | public class Collection8Test extends JSR
166                  () -> d.addLast(null),
167                  () -> d.offerFirst(null),
168                  () -> d.offerLast(null),
169 <                () -> d.push(null));
169 >                () -> d.push(null),
170 >                () -> d.descendingIterator().forEachRemaining(null));
171 >        }
172 >        if (!impl.permitsNulls() && c instanceof BlockingQueue) {
173 >            BlockingQueue q = (BlockingQueue) c;
174 >            assertThrows(
175 >                NullPointerException.class,
176 >                () -> {
177 >                    try { q.offer(null, 1L, MILLISECONDS); }
178 >                    catch (InterruptedException ex) {
179 >                        throw new AssertionError(ex);
180 >                    }});
181 >        }
182 >        if (!impl.permitsNulls() && c instanceof BlockingDeque) {
183 >            BlockingDeque q = (BlockingDeque) c;
184 >            assertThrows(
185 >                NullPointerException.class,
186 >                () -> {
187 >                    try { q.offerFirst(null, 1L, MILLISECONDS); }
188 >                    catch (InterruptedException ex) {
189 >                        throw new AssertionError(ex);
190 >                    }},
191 >                () -> {
192 >                    try { q.offerLast(null, 1L, MILLISECONDS); }
193 >                    catch (InterruptedException ex) {
194 >                        throw new AssertionError(ex);
195 >                    }});
196          }
197      }
198  
# Line 138 | Line 202 | public class Collection8Test extends JSR
202              NoSuchElementException.class,
203              () -> c.iterator().next());
204  
205 <        if (Queue.class.isAssignableFrom(impl.klazz())) {
205 >        if (c instanceof Queue) {
206              Queue q = (Queue) c;
207              assertThrows(
208                  NoSuchElementException.class,
209                  () -> q.element(),
210                  () -> q.remove());
211          }
212 <        if (Deque.class.isAssignableFrom(impl.klazz())) {
212 >        if (c instanceof Deque) {
213              Deque d = (Deque) c;
214              assertThrows(
215                  NoSuchElementException.class,
# Line 177 | Line 241 | public class Collection8Test extends JSR
241              }
242          };
243          try {
244 <            boolean modified = c.removeIf(randomPredicate);
245 <            if (!modified) {
246 <                assertNull(threwAt.get());
247 <                assertEquals(n, rejects.size());
248 <                assertEquals(0, accepts.size());
249 <            }
250 <        } catch (ArithmeticException ok) {}
251 <        survivors.removeAll(accepts);
252 <        if (n - accepts.size() != c.size()) {
244 >            assertFalse(survivors.contains(null));
245 >            try {
246 >                boolean modified = c.removeIf(randomPredicate);
247 >                if (!modified) {
248 >                    assertNull(threwAt.get());
249 >                    assertEquals(n, rejects.size());
250 >                    assertEquals(0, accepts.size());
251 >                }
252 >            } catch (ArithmeticException ok) {}
253 >            survivors.removeAll(accepts);
254 >            assertEquals(n - accepts.size(), c.size());
255 >            assertTrue(c.containsAll(survivors));
256 >            assertTrue(survivors.containsAll(rejects));
257 >            for (Object x : accepts) assertFalse(c.contains(x));
258 >            if (threwAt.get() == null)
259 >                assertEquals(accepts.size() + rejects.size(), n);
260 >        } catch (Throwable ex) {
261              System.err.println(impl.klazz());
262 <            System.err.println(c);
263 <            System.err.println(accepts);
264 <            System.err.println(rejects);
265 <            System.err.println(survivors);
266 <            System.err.println(threwAt.get());
267 <        }
268 <        assertEquals(n - accepts.size(), c.size());
269 <        assertTrue(c.containsAll(survivors));
198 <        assertTrue(survivors.containsAll(rejects));
199 <        for (Object x : accepts) assertFalse(c.contains(x));
200 <        if (threwAt.get() == null)
201 <            assertEquals(accepts.size() + rejects.size(), n);
262 >            System.err.printf("c=%s%n", c);
263 >            System.err.printf("n=%d%n", n);
264 >            System.err.printf("accepts=%s%n", accepts);
265 >            System.err.printf("rejects=%s%n", rejects);
266 >            System.err.printf("survivors=%s%n", survivors);
267 >            System.err.printf("threw=%s%n", threwAt.get());
268 >            throw ex;
269 >        }
270      }
271  
272      /**
# Line 211 | Line 279 | public class Collection8Test extends JSR
279          for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
280          ArrayList iterated = new ArrayList();
281          ArrayList iteratedForEachRemaining = new ArrayList();
282 +        ArrayList tryAdvanced = new ArrayList();
283          ArrayList spliterated = new ArrayList();
284 <        ArrayList foreached = new ArrayList();
284 >        ArrayList forEached = new ArrayList();
285 >        ArrayList removeIfed = new ArrayList();
286          for (Object x : c) iterated.add(x);
287          c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e));
288 +        for (Spliterator s = c.spliterator();
289 +             s.tryAdvance(e -> tryAdvanced.add(e)); ) {}
290          c.spliterator().forEachRemaining(e -> spliterated.add(e));
291 <        c.forEach(e -> foreached.add(e));
291 >        c.forEach(e -> forEached.add(e));
292 >        c.removeIf(e -> { removeIfed.add(e); return false; });
293          boolean ordered =
294              c.spliterator().hasCharacteristics(Spliterator.ORDERED);
295          if (c instanceof List || c instanceof Deque)
296              assertTrue(ordered);
297          if (ordered) {
298              assertEquals(iterated, iteratedForEachRemaining);
299 +            assertEquals(iterated, tryAdvanced);
300              assertEquals(iterated, spliterated);
301 <            assertEquals(iterated, foreached);
301 >            assertEquals(iterated, forEached);
302 >            assertEquals(iterated, removeIfed);
303          } else {
304              HashSet cset = new HashSet(c);
305              assertEquals(cset, new HashSet(iterated));
306              assertEquals(cset, new HashSet(iteratedForEachRemaining));
307 +            assertEquals(cset, new HashSet(tryAdvanced));
308              assertEquals(cset, new HashSet(spliterated));
309 <            assertEquals(cset, new HashSet(foreached));
309 >            assertEquals(cset, new HashSet(forEached));
310 >            assertEquals(cset, new HashSet(removeIfed));
311          }
312          if (c instanceof Deque) {
313              Deque d = (Deque) c;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines