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.8 by jsr166, Thu Nov 3 15:31:25 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 116 | Line 152 | public class Collection8Test extends JSR
152                  NullPointerException.class,
153                  () -> c.add(null));
154          }
155 <        if (!impl.permitsNulls()
120 <            && 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()
127 <            && Deque.class.isAssignableFrom(impl.klazz())) {
161 >        if (!impl.permitsNulls() && c instanceof Deque) {
162              Deque d = (Deque) c;
163              assertThrows(
164                  NullPointerException.class,
# Line 135 | Line 169 | public class Collection8Test extends JSR
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  
199      public void testNoSuchElementExceptions() {
# Line 143 | 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 220 | 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